rocketmq-client-remoting-nodejs 0.0.0-stage → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +428 -0
- package/README.md +424 -2
- package/dist/client/admin.d.ts +229 -0
- package/dist/client/admin.js +2462 -0
- package/dist/client/allocate.d.ts +48 -0
- package/dist/client/allocate.js +365 -0
- package/dist/client/backpressure.d.ts +23 -0
- package/dist/client/backpressure.js +95 -0
- package/dist/client/consumer.d.ts +294 -0
- package/dist/client/consumer.js +3095 -0
- package/dist/client/consumer_result.d.ts +129 -0
- package/dist/client/consumer_result.js +138 -0
- package/dist/client/consumer_stats.d.ts +69 -0
- package/dist/client/consumer_stats.js +258 -0
- package/dist/client/exception.d.ts +39 -0
- package/dist/client/exception.js +36 -0
- package/dist/client/hook.d.ts +145 -0
- package/dist/client/hook.js +171 -0
- package/dist/client/latency.d.ts +61 -0
- package/dist/client/latency.js +239 -0
- package/dist/client/lite_pull_consumer.d.ts +151 -0
- package/dist/client/lite_pull_consumer.js +1096 -0
- package/dist/client/mq_client.d.ts +188 -0
- package/dist/client/mq_client.js +1678 -0
- package/dist/client/mq_client_manager.d.ts +21 -0
- package/dist/client/mq_client_manager.js +129 -0
- package/dist/client/offset_store.d.ts +45 -0
- package/dist/client/offset_store.js +389 -0
- package/dist/client/open_tracing_hook.d.ts +52 -0
- package/dist/client/open_tracing_hook.js +241 -0
- package/dist/client/pop_api.d.ts +28 -0
- package/dist/client/pop_api.js +224 -0
- package/dist/client/pop_consumer.d.ts +23 -0
- package/dist/client/pop_consumer.js +719 -0
- package/dist/client/pop_process_queue.d.ts +15 -0
- package/dist/client/pop_process_queue.js +51 -0
- package/dist/client/process_queue.d.ts +49 -0
- package/dist/client/process_queue.js +261 -0
- package/dist/client/produce_accumulator.d.ts +75 -0
- package/dist/client/produce_accumulator.js +288 -0
- package/dist/client/producer.d.ts +231 -0
- package/dist/client/producer.js +1774 -0
- package/dist/client/pull_api.d.ts +26 -0
- package/dist/client/pull_api.js +264 -0
- package/dist/client/pull_consumer.d.ts +119 -0
- package/dist/client/pull_consumer.js +876 -0
- package/dist/client/pull_schedule_service.d.ts +71 -0
- package/dist/client/pull_schedule_service.js +424 -0
- package/dist/client/request_reply.d.ts +61 -0
- package/dist/client/request_reply.js +232 -0
- package/dist/client/retry_message.d.ts +2 -0
- package/dist/client/retry_message.js +27 -0
- package/dist/client/send_result.d.ts +57 -0
- package/dist/client/send_result.js +70 -0
- package/dist/client/top_addressing.d.ts +20 -0
- package/dist/client/top_addressing.js +75 -0
- package/dist/client/trace_context.d.ts +61 -0
- package/dist/client/trace_context.js +373 -0
- package/dist/client/trace_dispatcher.d.ts +31 -0
- package/dist/client/trace_dispatcher.js +236 -0
- package/dist/client/trace_hook.d.ts +23 -0
- package/dist/client/trace_hook.js +228 -0
- package/dist/client/traceparent.d.ts +9 -0
- package/dist/client/traceparent.js +86 -0
- package/dist/common/boundary_type.d.ts +7 -0
- package/dist/common/boundary_type.js +16 -0
- package/dist/common/compress.d.ts +28 -0
- package/dist/common/compress.js +512 -0
- package/dist/common/message.d.ts +129 -0
- package/dist/common/message.js +214 -0
- package/dist/common/messageClientIdSetter.d.ts +7 -0
- package/dist/common/messageClientIdSetter.js +48 -0
- package/dist/common/messageConst.d.ts +68 -0
- package/dist/common/messageConst.js +68 -0
- package/dist/common/messageDecoder.d.ts +40 -0
- package/dist/common/messageDecoder.js +504 -0
- package/dist/common/messageType.d.ts +11 -0
- package/dist/common/messageType.js +11 -0
- package/dist/common/message_accessor.d.ts +39 -0
- package/dist/common/message_accessor.js +135 -0
- package/dist/common/mixAll.d.ts +106 -0
- package/dist/common/mixAll.js +247 -0
- package/dist/common/recall_message_handle.d.ts +25 -0
- package/dist/common/recall_message_handle.js +98 -0
- package/dist/common/subscriptionData.d.ts +36 -0
- package/dist/common/subscriptionData.js +64 -0
- package/dist/common/sysflag.d.ts +129 -0
- package/dist/common/sysflag.js +96 -0
- package/dist/common/topic_config.d.ts +66 -0
- package/dist/common/topic_config.js +106 -0
- package/dist/common/topic_validator.d.ts +49 -0
- package/dist/common/topic_validator.js +115 -0
- package/dist/common/utilAll.d.ts +39 -0
- package/dist/common/utilAll.js +154 -0
- package/dist/common/validators.d.ts +25 -0
- package/dist/common/validators.js +80 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.js +36 -0
- package/dist/logging.d.ts +24 -0
- package/dist/logging.js +246 -0
- package/dist/remoting/acl.d.ts +16 -0
- package/dist/remoting/acl.js +39 -0
- package/dist/remoting/admin_body.d.ts +257 -0
- package/dist/remoting/admin_body.js +669 -0
- package/dist/remoting/bodies.d.ts +475 -0
- package/dist/remoting/bodies.js +1131 -0
- package/dist/remoting/client.d.ts +73 -0
- package/dist/remoting/client.js +591 -0
- package/dist/remoting/codes.d.ts +296 -0
- package/dist/remoting/codes.js +301 -0
- package/dist/remoting/exception.d.ts +45 -0
- package/dist/remoting/exception.js +58 -0
- package/dist/remoting/extra_info.d.ts +61 -0
- package/dist/remoting/extra_info.js +220 -0
- package/dist/remoting/headers.d.ts +758 -0
- package/dist/remoting/headers.js +1307 -0
- package/dist/remoting/heartbeat.d.ts +91 -0
- package/dist/remoting/heartbeat.js +227 -0
- package/dist/remoting/namespace.d.ts +14 -0
- package/dist/remoting/namespace.js +85 -0
- package/dist/remoting/pop_bodies.d.ts +58 -0
- package/dist/remoting/pop_bodies.js +214 -0
- package/dist/remoting/remotingCommand.d.ts +39 -0
- package/dist/remoting/remotingCommand.js +225 -0
- package/dist/remoting/route.d.ts +61 -0
- package/dist/remoting/route.js +288 -0
- package/dist/remoting/rpc_hooks.d.ts +31 -0
- package/dist/remoting/rpc_hooks.js +73 -0
- package/dist/remoting/serialize.d.ts +33 -0
- package/dist/remoting/serialize.js +372 -0
- package/dist/remoting/socks_proxy.d.ts +29 -0
- package/dist/remoting/socks_proxy.js +379 -0
- package/dist/remoting/static_topic.d.ts +49 -0
- package/dist/remoting/static_topic.js +244 -0
- package/dist/remoting/subscription.d.ts +65 -0
- package/dist/remoting/subscription.js +172 -0
- package/package.json +38 -4
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
// OpenTracing-style hook trio for the message lifecycle.
|
|
2
|
+
//
|
|
3
|
+
// Port of the Java client's SendMessageOpenTracingHookImpl /
|
|
4
|
+
// ConsumeMessageOpenTracingHookImpl / EndTransactionOpenTracingHookImpl (the
|
|
5
|
+
// same trio the PHP port carries as OpenTracingHook.php), on top of a
|
|
6
|
+
// duck-typed Tracer SPI with zero third-party deps. The tracer is duck-typed
|
|
7
|
+
// on purpose: any io.opentracing-shaped tracer (or a test fake) plugs in.
|
|
8
|
+
//
|
|
9
|
+
// The span rides in the context's mqTraceContext slot, exactly like the Java
|
|
10
|
+
// original (context.setMqTraceContext(span)): before opens the span and
|
|
11
|
+
// stashes it, after reads it back and closes it. Registering both the trace
|
|
12
|
+
// dispatcher hooks and these hooks on the same producer is not supported
|
|
13
|
+
// (same restriction as Java).
|
|
14
|
+
//
|
|
15
|
+
// Cross-process propagation rides on the message properties themselves: the
|
|
16
|
+
// upstream context is extracted from the property map and the just-started
|
|
17
|
+
// span's context is injected back into it, so whichever keys the tracer's
|
|
18
|
+
// injector writes travel in-band with the message.
|
|
19
|
+
import { MessageExt } from "../common/message.js";
|
|
20
|
+
import { MessageConst } from "../common/messageConst.js";
|
|
21
|
+
import { NamespaceUtil } from "../remoting/namespace.js";
|
|
22
|
+
import { ConsumeMessageHook, EndTransactionHook, SendMessageHook, } from "./hook.js";
|
|
23
|
+
// OpenTracing standard tags (io.opentracing.tag.Tags).
|
|
24
|
+
export const OT_SPAN_KIND = 'span.kind';
|
|
25
|
+
export const OT_SPAN_KIND_PRODUCER = 'producer';
|
|
26
|
+
export const OT_SPAN_KIND_CONSUMER = 'consumer';
|
|
27
|
+
export const OT_PEER_SERVICE = 'peer.service';
|
|
28
|
+
export const OT_MESSAGE_BUS_DESTINATION = 'message_bus.destination';
|
|
29
|
+
// The rocketmq.* tag keys, mirroring the Java TraceConstants / the PHP
|
|
30
|
+
// Trace.php block (the mgs_id/mgs_type spellings are the Java protocol
|
|
31
|
+
// literals -- the typo is load-bearing, do not "fix" it).
|
|
32
|
+
export const OT_ROCKETMQ_SERVICE = 'rocketmq';
|
|
33
|
+
export const OT_ROCKETMQ_SUCCESS = 'rocketmq.success';
|
|
34
|
+
export const OT_ROCKETMQ_TAGS = 'rocketmq.tags';
|
|
35
|
+
export const OT_ROCKETMQ_KEYS = 'rocketmq.keys';
|
|
36
|
+
export const OT_ROCKETMQ_STORE_HOST = 'rocketmq.store_host';
|
|
37
|
+
export const OT_ROCKETMQ_BODY_LENGTH = 'rocketmq.body_length';
|
|
38
|
+
export const OT_ROCKETMQ_MSG_ID = 'rocketmq.mgs_id';
|
|
39
|
+
export const OT_ROCKETMQ_MSG_TYPE = 'rocketmq.mgs_type';
|
|
40
|
+
export const OT_ROCKETMQ_REGION_ID = 'rocketmq.region_id';
|
|
41
|
+
export const OT_ROCKETMQ_TRANSACTION_ID = 'rocketmq.transaction_id';
|
|
42
|
+
export const OT_ROCKETMQ_TRANSACTION_STATE = 'rocketmq.transaction_state';
|
|
43
|
+
export const OT_ROCKETMQ_IS_FROM_TRANSACTION_CHECK = 'rocketmq.is_from_transaction_check';
|
|
44
|
+
export const OT_ROCKETMQ_RETRY_TIMERS = 'rocketmq.retry_times';
|
|
45
|
+
const OT_TO_PREFIX = 'To_';
|
|
46
|
+
const OT_FROM_PREFIX = 'From_';
|
|
47
|
+
const OT_END_TRANSACTION = 'EndTransaction';
|
|
48
|
+
// The transaction leg reports the commit state only (Java MessageType
|
|
49
|
+
// .Trans_msg_Commit.name()).
|
|
50
|
+
const OT_TRANS_MSG_COMMIT = 'Trans_msg_Commit';
|
|
51
|
+
function extractUpstream(tracer, props) {
|
|
52
|
+
if (props == null)
|
|
53
|
+
return undefined;
|
|
54
|
+
try {
|
|
55
|
+
return tracer.extract(props);
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function injectIntoMessage(tracer, span, msg) {
|
|
62
|
+
// The just-started span's context is what travels: the downstream leg must
|
|
63
|
+
// parent onto THIS send, not onto whatever upstream produced the context.
|
|
64
|
+
// getProperties() returns the live object, so the injector's writes land on
|
|
65
|
+
// the message directly.
|
|
66
|
+
const spanAny = span;
|
|
67
|
+
let spanContext = span;
|
|
68
|
+
for (const getter of ['context', 'getContext']) {
|
|
69
|
+
const fn = spanAny[getter];
|
|
70
|
+
if (typeof fn === 'function') {
|
|
71
|
+
const ctx = fn.call(span);
|
|
72
|
+
if (ctx != null) {
|
|
73
|
+
spanContext = ctx;
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
try {
|
|
79
|
+
tracer.inject(spanContext, msg.getProperties());
|
|
80
|
+
}
|
|
81
|
+
catch {
|
|
82
|
+
// propagation must never break the send
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
// ---------------------------------------------------------------- send leg
|
|
86
|
+
// Send leg: span "To_<topic>", kind producer, with the kind + service +
|
|
87
|
+
// destination + tags + keys + store_host + msg_type + body_length tags at
|
|
88
|
+
// open; success / msg_id / region_id at close. No result, or no region id on
|
|
89
|
+
// it: the span is left open, exactly like the Java close-out flow.
|
|
90
|
+
export class SendMessageOpenTracingHook extends SendMessageHook {
|
|
91
|
+
tracer;
|
|
92
|
+
constructor(tracer) {
|
|
93
|
+
super();
|
|
94
|
+
this.tracer = tracer;
|
|
95
|
+
}
|
|
96
|
+
getHookName() { return 'SendMessageOpenTracingHook'; }
|
|
97
|
+
sendMessageBefore(ctx) {
|
|
98
|
+
if (ctx == null || ctx.message == null)
|
|
99
|
+
return;
|
|
100
|
+
const msg = ctx.message;
|
|
101
|
+
const upstream = extractUpstream(this.tracer, msg.getProperties());
|
|
102
|
+
const tags = { [OT_SPAN_KIND]: OT_SPAN_KIND_PRODUCER };
|
|
103
|
+
const options = { tags };
|
|
104
|
+
if (upstream != null)
|
|
105
|
+
options.refs = [upstream];
|
|
106
|
+
const span = this.tracer.startSpan(OT_TO_PREFIX + msg.getTopic(), options);
|
|
107
|
+
span.setTag(OT_PEER_SERVICE, OT_ROCKETMQ_SERVICE);
|
|
108
|
+
span.setTag(OT_MESSAGE_BUS_DESTINATION, msg.getTopic());
|
|
109
|
+
span.setTag(OT_ROCKETMQ_TAGS, msg.getTags() ?? '');
|
|
110
|
+
span.setTag(OT_ROCKETMQ_KEYS, msg.getKeys() ?? '');
|
|
111
|
+
span.setTag(OT_ROCKETMQ_STORE_HOST, ctx.brokerAddr ?? '');
|
|
112
|
+
span.setTag(OT_ROCKETMQ_MSG_TYPE, ctx.msgType ?? 'NORMAL_MSG');
|
|
113
|
+
const body = msg.getBody();
|
|
114
|
+
span.setTag(OT_ROCKETMQ_BODY_LENGTH, body ? body.length : 0);
|
|
115
|
+
injectIntoMessage(this.tracer, span, msg);
|
|
116
|
+
ctx.mqTraceContext = span;
|
|
117
|
+
}
|
|
118
|
+
sendMessageAfter(ctx) {
|
|
119
|
+
if (ctx == null)
|
|
120
|
+
return;
|
|
121
|
+
const span = ctx.mqTraceContext;
|
|
122
|
+
if (span == null)
|
|
123
|
+
return;
|
|
124
|
+
const sr = ctx.sendResult;
|
|
125
|
+
// No result, or no region id on it: nothing to report, span stays open.
|
|
126
|
+
if (sr == null || sr.regionId == null)
|
|
127
|
+
return;
|
|
128
|
+
span.setTag(OT_ROCKETMQ_SUCCESS, sr.sendStatus === SendStatusCodeOK);
|
|
129
|
+
span.setTag(OT_ROCKETMQ_MSG_ID, sr.msgId ?? '');
|
|
130
|
+
span.setTag(OT_ROCKETMQ_REGION_ID, sr.regionId);
|
|
131
|
+
span.finish();
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
// SendStatus.SEND_OK (kept local so the hook module does not import the whole
|
|
135
|
+
// send_result surface for one constant).
|
|
136
|
+
const SendStatusCodeOK = 0;
|
|
137
|
+
// ---------------------------------------------------------------- consume leg
|
|
138
|
+
// Consume leg: one span per message, "From_<topic>", kind consumer; msg_id /
|
|
139
|
+
// tags / keys / body_length (store size) / retry_times / region_id at open;
|
|
140
|
+
// success at close.
|
|
141
|
+
export class ConsumeMessageOpenTracingHook extends ConsumeMessageHook {
|
|
142
|
+
tracer;
|
|
143
|
+
constructor(tracer) {
|
|
144
|
+
super();
|
|
145
|
+
this.tracer = tracer;
|
|
146
|
+
}
|
|
147
|
+
getHookName() { return 'ConsumeMessageOpenTracingHook'; }
|
|
148
|
+
consumeMessageBefore(ctx) {
|
|
149
|
+
if (ctx == null || ctx.msgList == null || ctx.msgList.length === 0)
|
|
150
|
+
return;
|
|
151
|
+
const spans = [];
|
|
152
|
+
for (const msg of ctx.msgList) {
|
|
153
|
+
if (!(msg instanceof MessageExt))
|
|
154
|
+
continue;
|
|
155
|
+
const upstream = extractUpstream(this.tracer, msg.getProperties());
|
|
156
|
+
const tags = { [OT_SPAN_KIND]: OT_SPAN_KIND_CONSUMER };
|
|
157
|
+
const options = { tags };
|
|
158
|
+
if (upstream != null)
|
|
159
|
+
options.refs = [upstream];
|
|
160
|
+
const span = this.tracer.startSpan(OT_FROM_PREFIX + msg.getTopic(), options);
|
|
161
|
+
span.setTag(OT_PEER_SERVICE, OT_ROCKETMQ_SERVICE);
|
|
162
|
+
span.setTag(OT_MESSAGE_BUS_DESTINATION, NamespaceUtil.withoutNamespace(msg.getTopic() ?? ''));
|
|
163
|
+
span.setTag(OT_ROCKETMQ_MSG_ID, msg.msgId ?? '');
|
|
164
|
+
span.setTag(OT_ROCKETMQ_TAGS, msg.getTags() ?? '');
|
|
165
|
+
span.setTag(OT_ROCKETMQ_KEYS, msg.getKeys() ?? '');
|
|
166
|
+
span.setTag(OT_ROCKETMQ_BODY_LENGTH, msg.getStoreSize());
|
|
167
|
+
span.setTag(OT_ROCKETMQ_RETRY_TIMERS, msg.getReconsumeTimes());
|
|
168
|
+
span.setTag(OT_ROCKETMQ_REGION_ID, msg.getProperty(MessageConst.PROPERTY_MSG_REGION) ?? '');
|
|
169
|
+
spans.push(span);
|
|
170
|
+
}
|
|
171
|
+
ctx.mqTraceContext = spans;
|
|
172
|
+
}
|
|
173
|
+
consumeMessageAfter(ctx) {
|
|
174
|
+
if (ctx == null || ctx.msgList == null || ctx.msgList.length === 0)
|
|
175
|
+
return;
|
|
176
|
+
const spans = ctx.mqTraceContext;
|
|
177
|
+
if (spans == null)
|
|
178
|
+
return;
|
|
179
|
+
// The delivery path stamps an explicit boolean (hookCtx.success) after the
|
|
180
|
+
// listener returns; absent means success, like the other ends' default.
|
|
181
|
+
const success = ctx.success !== false;
|
|
182
|
+
for (const span of spans) {
|
|
183
|
+
span.setTag(OT_ROCKETMQ_SUCCESS, success);
|
|
184
|
+
span.finish();
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
// ------------------------------------------------------------ transaction leg
|
|
189
|
+
// Transaction leg: span "EndTransaction" with the transaction identity keys,
|
|
190
|
+
// closed immediately. The message is left untouched on this leg (the send
|
|
191
|
+
// side already put the context on the properties).
|
|
192
|
+
export class EndTransactionOpenTracingHook extends EndTransactionHook {
|
|
193
|
+
tracer;
|
|
194
|
+
constructor(tracer) {
|
|
195
|
+
super();
|
|
196
|
+
this.tracer = tracer;
|
|
197
|
+
}
|
|
198
|
+
getHookName() { return 'EndTransactionOpenTracingHook'; }
|
|
199
|
+
endTransactionBefore(ctx) {
|
|
200
|
+
if (ctx == null)
|
|
201
|
+
return;
|
|
202
|
+
const msg = ctx.message;
|
|
203
|
+
const upstream = extractUpstream(this.tracer, msg ? msg.getProperties() : null);
|
|
204
|
+
const tags = { [OT_SPAN_KIND]: OT_SPAN_KIND_PRODUCER };
|
|
205
|
+
const options = { tags };
|
|
206
|
+
if (upstream != null)
|
|
207
|
+
options.refs = [upstream];
|
|
208
|
+
const span = this.tracer.startSpan(OT_END_TRANSACTION, options);
|
|
209
|
+
span.setTag(OT_PEER_SERVICE, OT_ROCKETMQ_SERVICE);
|
|
210
|
+
span.setTag(OT_MESSAGE_BUS_DESTINATION, msg ? msg.getTopic() : '');
|
|
211
|
+
span.setTag(OT_ROCKETMQ_TAGS, msg ? (msg.getTags() ?? '') : '');
|
|
212
|
+
span.setTag(OT_ROCKETMQ_KEYS, msg ? (msg.getKeys() ?? '') : '');
|
|
213
|
+
span.setTag(OT_ROCKETMQ_STORE_HOST, ctx.brokerAddr ?? '');
|
|
214
|
+
span.setTag(OT_ROCKETMQ_MSG_ID, ctx.msgId ?? '');
|
|
215
|
+
span.setTag(OT_ROCKETMQ_MSG_TYPE, OT_TRANS_MSG_COMMIT);
|
|
216
|
+
span.setTag(OT_ROCKETMQ_TRANSACTION_ID, ctx.transactionId ?? '');
|
|
217
|
+
span.setTag(OT_ROCKETMQ_TRANSACTION_STATE, transactionStateName(ctx.transactionState));
|
|
218
|
+
span.setTag(OT_ROCKETMQ_IS_FROM_TRANSACTION_CHECK, ctx.fromTransactionCheck === true);
|
|
219
|
+
span.finish();
|
|
220
|
+
}
|
|
221
|
+
endTransactionAfter(_ctx) {
|
|
222
|
+
// The Java/PHP leg opens and closes one span synchronously; the pair
|
|
223
|
+
// method exists because the nodeJs EndTransactionHook SPI is two-sided.
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
// LocalTransactionState arrives as either a numeric code or an enum object;
|
|
227
|
+
// both map onto the Java state names.
|
|
228
|
+
function transactionStateName(state) {
|
|
229
|
+
if (state == null)
|
|
230
|
+
return '';
|
|
231
|
+
if (typeof state === 'number') {
|
|
232
|
+
switch (state) {
|
|
233
|
+
case 0: return 'COMMIT_MESSAGE';
|
|
234
|
+
case 1: return 'ROLLBACK_MESSAGE';
|
|
235
|
+
case 2: return 'UNKNOW';
|
|
236
|
+
default: return String(state);
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
const name = state.name;
|
|
240
|
+
return typeof name === 'string' ? name : String(state);
|
|
241
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { MessageExt } from '../common/message.js';
|
|
2
|
+
import { PopMessageResponseHeader } from '../remoting/headers.js';
|
|
3
|
+
export declare const PopStatus: {
|
|
4
|
+
readonly FOUND: "FOUND";
|
|
5
|
+
readonly NO_NEW_MSG: "NO_NEW_MSG";
|
|
6
|
+
readonly POLLING_FULL: "POLLING_FULL";
|
|
7
|
+
readonly POLLING_NOT_FOUND: "POLLING_NOT_FOUND";
|
|
8
|
+
};
|
|
9
|
+
export type PopStatusValue = typeof PopStatus[keyof typeof PopStatus];
|
|
10
|
+
export declare class PopResult {
|
|
11
|
+
popStatus: PopStatusValue;
|
|
12
|
+
msgFoundList: MessageExt[];
|
|
13
|
+
restNum: number;
|
|
14
|
+
popTime: number;
|
|
15
|
+
invisibleTime: number;
|
|
16
|
+
}
|
|
17
|
+
export declare const POP_DELAY_LEVEL: number[];
|
|
18
|
+
export declare function popDelayLevelSeconds(level: number): number;
|
|
19
|
+
export declare function popDelayLevelForElapsed(elapsedMillis: number): number;
|
|
20
|
+
export declare function processPopResponse(brokerName: string, responseCode: number, responseRemark: string, body: Buffer | null, respHeader: PopMessageResponseHeader, requestTopic: string, namespace: string, order: boolean, readBody?: boolean): PopResult;
|
|
21
|
+
export declare function getRealTopicFromCk(extraInfo: string, topic: string, consumerGroup: string): string;
|
|
22
|
+
export declare function popBatchTimedOut(batch: MessageExt[]): boolean;
|
|
23
|
+
export declare function popInvisibleTimeOf(batch: MessageExt[]): number;
|
|
24
|
+
export declare function ckParse(extraInfo: string): {
|
|
25
|
+
brokerName: string;
|
|
26
|
+
queueId: number;
|
|
27
|
+
queueOffset: number;
|
|
28
|
+
} | null;
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import { MessageConst } from "../common/messageConst.js";
|
|
2
|
+
import { MixAll } from "../common/mixAll.js";
|
|
3
|
+
import { decodeMessages } from "../common/messageDecoder.js";
|
|
4
|
+
import { ResponseCode } from "../remoting/codes.js";
|
|
5
|
+
import { MQBrokerException } from "../remoting/exception.js";
|
|
6
|
+
import { buildExtraInfo, getStartOffsetInfoMapKey, getQueueOffsetMapKey, parseMsgOffsetInfo, parseOrderCountInfo, parseStartOffsetInfo, split, getBrokerName, getQueueId, getQueueOffset, getPopTime, getInvisibleTime, } from "../remoting/extra_info.js";
|
|
7
|
+
export const PopStatus = {
|
|
8
|
+
FOUND: 'FOUND',
|
|
9
|
+
NO_NEW_MSG: 'NO_NEW_MSG',
|
|
10
|
+
POLLING_FULL: 'POLLING_FULL',
|
|
11
|
+
POLLING_NOT_FOUND: 'POLLING_NOT_FOUND',
|
|
12
|
+
};
|
|
13
|
+
export class PopResult {
|
|
14
|
+
popStatus = PopStatus.NO_NEW_MSG;
|
|
15
|
+
msgFoundList = [];
|
|
16
|
+
restNum = 0;
|
|
17
|
+
// popTime / invisibleTime come from the response header and are what the
|
|
18
|
+
// per-message checkpoint echoes.
|
|
19
|
+
popTime = 0;
|
|
20
|
+
invisibleTime = 0;
|
|
21
|
+
}
|
|
22
|
+
// ---------------------------------------------------------------------------
|
|
23
|
+
// delay levels
|
|
24
|
+
// ---------------------------------------------------------------------------
|
|
25
|
+
export const POP_DELAY_LEVEL = [
|
|
26
|
+
10, 30, 60, 120, 180, 240, 300, 360, 420, 480, 540, 600, 1200, 1800, 3600, 7200,
|
|
27
|
+
];
|
|
28
|
+
export function popDelayLevelSeconds(level) {
|
|
29
|
+
if (level < 0)
|
|
30
|
+
level = 0;
|
|
31
|
+
if (level >= POP_DELAY_LEVEL.length)
|
|
32
|
+
return POP_DELAY_LEVEL[POP_DELAY_LEVEL.length - 1];
|
|
33
|
+
return POP_DELAY_LEVEL[level];
|
|
34
|
+
}
|
|
35
|
+
export function popDelayLevelForElapsed(elapsedMillis) {
|
|
36
|
+
let level = POP_DELAY_LEVEL.length - 1;
|
|
37
|
+
for (; level >= 0; level--) {
|
|
38
|
+
if (elapsedMillis >= POP_DELAY_LEVEL[level] * 1000) {
|
|
39
|
+
level++;
|
|
40
|
+
break;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return level;
|
|
44
|
+
}
|
|
45
|
+
// ---------------------------------------------------------------------------
|
|
46
|
+
// response processing
|
|
47
|
+
// ---------------------------------------------------------------------------
|
|
48
|
+
// brokerName is the LOGICAL name the checkpoint must carry (segment 5) and must
|
|
49
|
+
// be the same name the ACK later addresses — sending the physical broker name
|
|
50
|
+
// here makes every ACK unresolvable.
|
|
51
|
+
//
|
|
52
|
+
// namespace is the client-side namespace to strip from the topic handed to the
|
|
53
|
+
// listener ('' when unset).
|
|
54
|
+
export function processPopResponse(brokerName, responseCode, responseRemark, body, respHeader, requestTopic, namespace, order, readBody = true) {
|
|
55
|
+
const result = new PopResult();
|
|
56
|
+
switch (responseCode) {
|
|
57
|
+
case ResponseCode.SUCCESS:
|
|
58
|
+
result.popStatus = PopStatus.FOUND;
|
|
59
|
+
if (body && body.length > 0) {
|
|
60
|
+
result.msgFoundList = decodeMessagesForPop(body, readBody);
|
|
61
|
+
}
|
|
62
|
+
break;
|
|
63
|
+
case ResponseCode.POLLING_FULL:
|
|
64
|
+
result.popStatus = PopStatus.POLLING_FULL;
|
|
65
|
+
break;
|
|
66
|
+
case ResponseCode.POLLING_TIMEOUT:
|
|
67
|
+
case ResponseCode.PULL_NOT_FOUND:
|
|
68
|
+
result.popStatus = PopStatus.POLLING_NOT_FOUND;
|
|
69
|
+
break;
|
|
70
|
+
default:
|
|
71
|
+
// The code rides on the typed exception, exactly like the pull kernel's:
|
|
72
|
+
// the POP loop branches on it to tell the broker's own throttle answer (215,
|
|
73
|
+
// short pause) from every other failure, and a bare Error would leave the
|
|
74
|
+
// number only in the text and silently disable that branch.
|
|
75
|
+
throw new MQBrokerException(responseCode, `POP response error, code=${responseCode} remark=${responseRemark || ''}`);
|
|
76
|
+
}
|
|
77
|
+
result.restNum = respHeader.restNum ?? 0;
|
|
78
|
+
if (result.popStatus !== PopStatus.FOUND) {
|
|
79
|
+
return result;
|
|
80
|
+
}
|
|
81
|
+
result.popTime = respHeader.popTime ?? 0;
|
|
82
|
+
result.invisibleTime = respHeader.invisibleTime ?? 0;
|
|
83
|
+
const startOffsetInfo = parseStartOffsetInfo(respHeader.startOffsetInfo);
|
|
84
|
+
const msgOffsetInfo = parseMsgOffsetInfo(respHeader.msgOffsetInfo);
|
|
85
|
+
const orderCountInfo = parseOrderCountInfo(respHeader.orderCountInfo);
|
|
86
|
+
const reviveQid = respHeader.reviveQid ?? 0;
|
|
87
|
+
const built = new Map();
|
|
88
|
+
for (const msg of result.msgFoundList) {
|
|
89
|
+
if (startOffsetInfo == null) {
|
|
90
|
+
const key = msg.getTopic() + msg.getQueueId();
|
|
91
|
+
let checkpoint = built.get(key);
|
|
92
|
+
if (checkpoint === undefined) {
|
|
93
|
+
checkpoint = buildExtraInfo(msg.getQueueOffset(), result.popTime, result.invisibleTime, reviveQid, msg.getTopic(), brokerName, msg.getQueueId());
|
|
94
|
+
built.set(key, checkpoint);
|
|
95
|
+
}
|
|
96
|
+
msg.putProperty(MessageConst.PROPERTY_POP_CK, checkpoint + ' ' + msg.getQueueOffset());
|
|
97
|
+
}
|
|
98
|
+
else if (!msg.getProperty(MessageConst.PROPERTY_POP_CK)) {
|
|
99
|
+
// That guard is load-bearing on the default retry path. With
|
|
100
|
+
// brokerConfig.popResponseReturnActualRetryTopic=false the broker
|
|
101
|
+
// builds the checkpoint from the PHYSICAL retry topic, so segment 4
|
|
102
|
+
// (the retry marker) is "1", stamps it with putIfAbsent, and only THEN
|
|
103
|
+
// rewrites msg.Topic to the business topic
|
|
104
|
+
// (PopMessageProcessor:849-853). By the time the client sees the
|
|
105
|
+
// message the topic has already been rewritten, so rebuilding from the
|
|
106
|
+
// message would resolve the marker to "0" and derive the ACK topic as
|
|
107
|
+
// the business topic — where the broker holds no such checkpoint. The
|
|
108
|
+
// ACK then becomes a silent no-op whose only symptom is "the message
|
|
109
|
+
// keeps coming back after popInvisibleTime".
|
|
110
|
+
//
|
|
111
|
+
// The lookup key is the message's OWN topic as decoded, which is still
|
|
112
|
+
// the physical topic at this point (the request topic is stamped on at
|
|
113
|
+
// the end of the loop). For a retried message with
|
|
114
|
+
// popResponseReturnActualRetryTopic=true that is
|
|
115
|
+
// `%RETRY%<group>_<topic>`, so the marker resolves to "1" and the entry
|
|
116
|
+
// lines up with the one the broker emitted.
|
|
117
|
+
const queueIDKey = getStartOffsetInfoMapKey(msg.getTopic(), msg.getQueueId());
|
|
118
|
+
const queueOffsetKey = getQueueOffsetMapKey(msg.getTopic(), msg.getQueueId(), msg.getQueueOffset());
|
|
119
|
+
const ckOffset = startOffsetInfo[queueIDKey];
|
|
120
|
+
const msgOffsets = msgOffsetInfo ? msgOffsetInfo[queueIDKey] : undefined;
|
|
121
|
+
if (ckOffset == null || msgOffsets == null) {
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
const index = msgOffsets.indexOf(msg.getQueueOffset());
|
|
125
|
+
if (index < 0 || index >= msgOffsets.length)
|
|
126
|
+
continue;
|
|
127
|
+
const msgQueueOffset = msgOffsets[index];
|
|
128
|
+
msg.putProperty(MessageConst.PROPERTY_POP_CK, buildExtraInfo(ckOffset, result.popTime, result.invisibleTime, reviveQid, msg.getTopic(), brokerName, msg.getQueueId(), msgQueueOffset));
|
|
129
|
+
if (order && orderCountInfo != null) {
|
|
130
|
+
// An orderly POP hides the reconsume count in orderCountInfo; the key
|
|
131
|
+
// is the (queueId,queueOffset) form first, then plain queueId.
|
|
132
|
+
let count = orderCountInfo[queueOffsetKey];
|
|
133
|
+
if (count == null)
|
|
134
|
+
count = orderCountInfo[queueIDKey];
|
|
135
|
+
if (count != null && count > 0)
|
|
136
|
+
msg.setReconsumeTimes(count);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
// 1ST_POP_TIME is the FIRST pop of this message across all redeliveries;
|
|
140
|
+
// only set it when absent, never overwrite.
|
|
141
|
+
if (msg.getProperty(MessageConst.PROPERTY_FIRST_POP_TIME) == null) {
|
|
142
|
+
msg.putProperty(MessageConst.PROPERTY_FIRST_POP_TIME, String(result.popTime));
|
|
143
|
+
}
|
|
144
|
+
msg.setBrokerName(brokerName);
|
|
145
|
+
// The topic handed to the listener is the one the caller subscribed to,
|
|
146
|
+
// with the namespace stripped — NOT the physical pop topic.
|
|
147
|
+
msg.setTopic(MixAll.withoutNamespace(requestTopic, namespace));
|
|
148
|
+
}
|
|
149
|
+
return result;
|
|
150
|
+
}
|
|
151
|
+
export function getRealTopicFromCk(extraInfo, topic, consumerGroup) {
|
|
152
|
+
let parts;
|
|
153
|
+
try {
|
|
154
|
+
parts = split(extraInfo);
|
|
155
|
+
}
|
|
156
|
+
catch {
|
|
157
|
+
return topic;
|
|
158
|
+
}
|
|
159
|
+
if (parts.length < 5)
|
|
160
|
+
return topic;
|
|
161
|
+
const retry = parts[4];
|
|
162
|
+
if (retry === '1') {
|
|
163
|
+
return MixAll.RETRY_GROUP_TOPIC_PREFIX + consumerGroup + '_' + topic;
|
|
164
|
+
}
|
|
165
|
+
if (retry === '2') {
|
|
166
|
+
return MixAll.RETRY_GROUP_TOPIC_PREFIX + consumerGroup + '+' + topic;
|
|
167
|
+
}
|
|
168
|
+
return topic;
|
|
169
|
+
}
|
|
170
|
+
// ---------------------------------------------------------------------------
|
|
171
|
+
// checkpoint readers (batch helpers)
|
|
172
|
+
// ---------------------------------------------------------------------------
|
|
173
|
+
export function popBatchTimedOut(batch) {
|
|
174
|
+
if (batch.length === 0)
|
|
175
|
+
return true;
|
|
176
|
+
const ck = batch[0].getProperty(MessageConst.PROPERTY_POP_CK);
|
|
177
|
+
if (!ck)
|
|
178
|
+
return true;
|
|
179
|
+
let popTime, invisible;
|
|
180
|
+
try {
|
|
181
|
+
popTime = getPopTime(split(ck));
|
|
182
|
+
invisible = getInvisibleTime(split(ck));
|
|
183
|
+
}
|
|
184
|
+
catch {
|
|
185
|
+
return true;
|
|
186
|
+
}
|
|
187
|
+
if (popTime <= 0 || invisible <= 0)
|
|
188
|
+
return true;
|
|
189
|
+
return Date.now() - popTime >= invisible;
|
|
190
|
+
}
|
|
191
|
+
// popInvisibleTimeOf reads the batch's invisibility window (0 when unknown),
|
|
192
|
+
// used for the consume-hook timing decision.
|
|
193
|
+
export function popInvisibleTimeOf(batch) {
|
|
194
|
+
if (batch.length === 0)
|
|
195
|
+
return 0;
|
|
196
|
+
const ck = batch[0].getProperty(MessageConst.PROPERTY_POP_CK);
|
|
197
|
+
if (!ck)
|
|
198
|
+
return 0;
|
|
199
|
+
try {
|
|
200
|
+
return getInvisibleTime(split(ck));
|
|
201
|
+
}
|
|
202
|
+
catch {
|
|
203
|
+
return 0;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
// ckParse reads brokerName / queueId / queueOffset out of one checkpoint; null
|
|
207
|
+
// on any failure (the caller logs and skips — an unreadable checkpoint can
|
|
208
|
+
// never be acked).
|
|
209
|
+
export function ckParse(extraInfo) {
|
|
210
|
+
try {
|
|
211
|
+
const parts = split(extraInfo);
|
|
212
|
+
return {
|
|
213
|
+
brokerName: getBrokerName(parts),
|
|
214
|
+
queueId: getQueueId(parts),
|
|
215
|
+
queueOffset: getQueueOffset(parts),
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
catch {
|
|
219
|
+
return null;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
function decodeMessagesForPop(raw, readBody = true) {
|
|
223
|
+
return decodeMessages(raw, readBody);
|
|
224
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { DefaultMQPushConsumer } from './consumer.js';
|
|
2
|
+
import { MessageExt, MessageQueue } from '../common/message.js';
|
|
3
|
+
import { PopResult } from './pop_api.js';
|
|
4
|
+
import type { SubscriptionData } from '../common/subscriptionData.js';
|
|
5
|
+
export declare const POP_BROKER_SUSPEND_MAX_TIME_MILLIS: number;
|
|
6
|
+
export declare const POP_ASYNC_TIMEOUT_MILLIS = 3000;
|
|
7
|
+
export declare const POP_REQUEST_EXTRA_NETWORK_MILLIS: number;
|
|
8
|
+
export declare const POP_MIN_INVISIBLE_TIME = 5000;
|
|
9
|
+
export declare const POP_MAX_INVISIBLE_TIME = 300000;
|
|
10
|
+
export declare const POP_DEFAULT_INVISIBLE_TIME = 60000;
|
|
11
|
+
export declare const POP_INIT_MODE_MIN = 0;
|
|
12
|
+
export declare const POP_INIT_MODE_MAX = 1;
|
|
13
|
+
export declare function consumeInitMode(consumeFromWhere: string | null | undefined): number;
|
|
14
|
+
export declare const POP_DELAY_WHEN_CACHE_FLOW_CONTROL = 50;
|
|
15
|
+
export declare const POP_DELAY_WHEN_BROKER_FLOW = 20;
|
|
16
|
+
export declare const POP_DELAY_WHEN_SUSPEND = 1000;
|
|
17
|
+
export declare function queuePopLoop(c: DefaultMQPushConsumer, mq: MessageQueue, stopFlag: string): Promise<void>;
|
|
18
|
+
export declare function processPopResult(c: DefaultMQPushConsumer, mq: MessageQueue, sub: SubscriptionData | undefined, result: PopResult): PopResult;
|
|
19
|
+
export declare function ackMessagePop(c: DefaultMQPushConsumer, msg: MessageExt): Promise<void>;
|
|
20
|
+
export declare function changePopInvisibleTime(c: DefaultMQPushConsumer, msg: MessageExt, delayLevel: number): void;
|
|
21
|
+
export declare function syncPopLoops(c: DefaultMQPushConsumer): void;
|
|
22
|
+
export declare function retirePopQueue(c: DefaultMQPushConsumer, key: string): void;
|
|
23
|
+
export declare function setMessageRequestModeOnBroker(c: DefaultMQPushConsumer, timeoutMillis: number): Promise<Error | null>;
|