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,373 @@
|
|
|
1
|
+
// Message trace types and codecs.
|
|
2
|
+
//
|
|
3
|
+
// The encoded text is exactly what the broker persists to RMQ_SYS_TRACE_TOPIC
|
|
4
|
+
// and the console reads back verbatim; the field order is the wire format
|
|
5
|
+
// mandated by the classic remoting protocol, not an implementation detail.
|
|
6
|
+
//
|
|
7
|
+
// The splitting rules are easy to get wrong; two key points must be followed:
|
|
8
|
+
//
|
|
9
|
+
// - CONTENT_SPLITOR (\x01) separates fields within a record, FIELD_SPLITOR (\x02) separates records,
|
|
10
|
+
// and EVERY record ends with an extra FIELD_SPLITOR. Decoding must drop the
|
|
11
|
+
// trailing empty segment so the appended separator is harmless; the on-wire
|
|
12
|
+
// record count equals the number of "\x02" occurrences.
|
|
13
|
+
// - JS's native split keeps the trailing empty string; splitting directly
|
|
14
|
+
// would add an extra empty segment and shift every field. Always go through
|
|
15
|
+
// splitTrimmingTrailingEmpty below.
|
|
16
|
+
import { MessageConst } from "../common/messageConst.js";
|
|
17
|
+
import { clientMsgId } from "../common/messageClientIdSetter.js";
|
|
18
|
+
// Trace-related constants; the last two accessChannel values are wire literals too.
|
|
19
|
+
export const TraceGroupNamePrefix = '_INNER_TRACE_PRODUCER';
|
|
20
|
+
export const TraceContentSplitor = '\u0001';
|
|
21
|
+
export const TraceFieldSplitor = '\u0002';
|
|
22
|
+
export const TraceInstanceName = 'PID_CLIENT_INNER_TRACE_PRODUCER';
|
|
23
|
+
export const TraceTopicPrefix = 'rmq_sys_TRACE_DATA_';
|
|
24
|
+
export const AccessChannelLocal = 'LOCAL';
|
|
25
|
+
export const AccessChannelCloud = 'CLOUD';
|
|
26
|
+
// Java MixAll.DEFAULT_TRACE_REGION_ID: the region the broker stamps on messages when it
|
|
27
|
+
// does not carry MSG_REGION. A TraceContext with no region at all is *dropped* by
|
|
28
|
+
// AsyncTraceDispatcher#sendTraceData (:316-318), so every hook must fill this in.
|
|
29
|
+
export const DefaultTraceRegionId = 'DefaultRegion';
|
|
30
|
+
// Trace event types. The type string is the first field of every encoded record, a wire literal.
|
|
31
|
+
export const TraceType = {
|
|
32
|
+
PUB: 'Pub',
|
|
33
|
+
RECALL: 'Recall',
|
|
34
|
+
SUB_BEFORE: 'SubBefore',
|
|
35
|
+
SUB_AFTER: 'SubAfter',
|
|
36
|
+
END_TRANSACTION: 'EndTransaction',
|
|
37
|
+
};
|
|
38
|
+
// Message type ordinals — field values encoded onto the wire.
|
|
39
|
+
export const TraceMsgType = { NORMAL: 0, ORDER: 1, TRANSACTION: 2, DELAY: 3, BATCH: 4, REPLY: 5 };
|
|
40
|
+
// TraceBean: the message being tracked behind one trace record.
|
|
41
|
+
export class TraceBean {
|
|
42
|
+
topic = '';
|
|
43
|
+
msgId = '';
|
|
44
|
+
offsetMsgId = '';
|
|
45
|
+
tags = '';
|
|
46
|
+
keys = '';
|
|
47
|
+
storeHost = '';
|
|
48
|
+
clientHost = '';
|
|
49
|
+
storeTime = 0;
|
|
50
|
+
retryTimes = 0;
|
|
51
|
+
bodyLength = 0;
|
|
52
|
+
// MsgType is written as the enum ORDINAL; the zero value is NormalMsg.
|
|
53
|
+
msgType = TraceMsgType.NORMAL;
|
|
54
|
+
// TransactionState holds the state NAME ("COMMIT_MESSAGE", ...).
|
|
55
|
+
transactionState = '';
|
|
56
|
+
transactionId = '';
|
|
57
|
+
fromTransactionCheck = false;
|
|
58
|
+
}
|
|
59
|
+
// TraceContext: one trace event.
|
|
60
|
+
export class TraceContext {
|
|
61
|
+
traceType = TraceType.PUB;
|
|
62
|
+
timeStamp = 0;
|
|
63
|
+
regionId = '';
|
|
64
|
+
regionName = '';
|
|
65
|
+
groupName = '';
|
|
66
|
+
costTime = 0;
|
|
67
|
+
isSuccess = true;
|
|
68
|
+
requestId = '';
|
|
69
|
+
contextCode = 0;
|
|
70
|
+
// Java TraceContext.accessChannel is NULL by default (:38, no initializer): a
|
|
71
|
+
// context without one falls back to the dispatcher's channel in sendTraceData
|
|
72
|
+
// (:311-315). Defaulting to LOCAL would break that fallback, so this stays null.
|
|
73
|
+
accessChannel = null;
|
|
74
|
+
traceBeans = [];
|
|
75
|
+
}
|
|
76
|
+
// TraceTransferBean: one encoded payload, plus the key set for downstream lookup by key.
|
|
77
|
+
export class TraceTransferBean {
|
|
78
|
+
transData = '';
|
|
79
|
+
transKey = new Set();
|
|
80
|
+
}
|
|
81
|
+
// traceField reads one segment, treating a missing one as an empty string.
|
|
82
|
+
//
|
|
83
|
+
// Deliberate robustness handling here: if a SubBefore record comes from a
|
|
84
|
+
// message WITHOUT keys, its keys segment is eaten by the "drop the trailing
|
|
85
|
+
// empty segment" rule, and blindly taking index 7 would go out of bounds and
|
|
86
|
+
// throw, losing the whole batch of trace messages.
|
|
87
|
+
// The read side must not crash on a legal record missing a segment, so a
|
|
88
|
+
// missing segment reads as an empty string.
|
|
89
|
+
function traceField(line, index) {
|
|
90
|
+
return index < line.length ? line[index] : '';
|
|
91
|
+
}
|
|
92
|
+
// splitTrimmingTrailingEmpty: splits by the separator, DROPPING the trailing
|
|
93
|
+
// empty segment (the protocol appends a FIELD_SPLITOR to every record); a
|
|
94
|
+
// string with no separator is returned as-is.
|
|
95
|
+
export function splitTrimmingTrailingEmpty(text, sep) {
|
|
96
|
+
const parts = text.split(sep);
|
|
97
|
+
if (parts.length === 1)
|
|
98
|
+
return parts;
|
|
99
|
+
while (parts.length > 0 && parts[parts.length - 1] === '')
|
|
100
|
+
parts.pop();
|
|
101
|
+
return parts;
|
|
102
|
+
}
|
|
103
|
+
function traceMsgType(text) {
|
|
104
|
+
const n = parseInt(text, 10);
|
|
105
|
+
if (Number.isNaN(n) || n < 0 || n > TraceMsgType.REPLY)
|
|
106
|
+
return null;
|
|
107
|
+
return n;
|
|
108
|
+
}
|
|
109
|
+
// EncodeTraceContext encodes one trace event into a transfer block; returns null when the context is null.
|
|
110
|
+
export function EncodeTraceContext(ctx) {
|
|
111
|
+
if (ctx == null)
|
|
112
|
+
return null;
|
|
113
|
+
const tb = new TraceTransferBean();
|
|
114
|
+
const join = (fields) => fields.join(TraceContentSplitor) + TraceFieldSplitor;
|
|
115
|
+
switch (ctx.traceType) {
|
|
116
|
+
case TraceType.PUB: {
|
|
117
|
+
if (ctx.traceBeans.length === 0)
|
|
118
|
+
return tb;
|
|
119
|
+
const bean = ctx.traceBeans[0];
|
|
120
|
+
tb.transData = join([
|
|
121
|
+
TraceType.PUB, String(ctx.timeStamp), ctx.regionId, ctx.groupName,
|
|
122
|
+
bean.topic, bean.msgId, bean.tags, bean.keys, bean.storeHost,
|
|
123
|
+
String(bean.bodyLength), String(ctx.costTime),
|
|
124
|
+
String(bean.msgType), bean.offsetMsgId,
|
|
125
|
+
String(ctx.isSuccess),
|
|
126
|
+
]);
|
|
127
|
+
break;
|
|
128
|
+
}
|
|
129
|
+
case TraceType.SUB_BEFORE: {
|
|
130
|
+
// One record per bean: a batch consume traces every message separately
|
|
131
|
+
// (they share the timestamp/region/group/requestId).
|
|
132
|
+
for (const bean of ctx.traceBeans) {
|
|
133
|
+
tb.transData += join([
|
|
134
|
+
TraceType.SUB_BEFORE, String(ctx.timeStamp), ctx.regionId, ctx.groupName,
|
|
135
|
+
ctx.requestId, bean.msgId, String(bean.retryTimes), bean.keys,
|
|
136
|
+
]);
|
|
137
|
+
}
|
|
138
|
+
break;
|
|
139
|
+
}
|
|
140
|
+
case TraceType.SUB_AFTER: {
|
|
141
|
+
for (const bean of ctx.traceBeans) {
|
|
142
|
+
const fields = [
|
|
143
|
+
TraceType.SUB_AFTER, ctx.requestId, bean.msgId, String(ctx.costTime),
|
|
144
|
+
String(ctx.isSuccess), bean.keys, String(ctx.contextCode),
|
|
145
|
+
];
|
|
146
|
+
if (ctx.accessChannel !== AccessChannelCloud) {
|
|
147
|
+
// A SubAfter on a NON-cloud channel carries two extra trailing
|
|
148
|
+
// fields (timestamp + groupName).
|
|
149
|
+
fields.push(String(ctx.timeStamp), ctx.groupName);
|
|
150
|
+
}
|
|
151
|
+
tb.transData += join(fields);
|
|
152
|
+
}
|
|
153
|
+
break;
|
|
154
|
+
}
|
|
155
|
+
case TraceType.END_TRANSACTION: {
|
|
156
|
+
if (ctx.traceBeans.length === 0)
|
|
157
|
+
return tb;
|
|
158
|
+
const bean = ctx.traceBeans[0];
|
|
159
|
+
tb.transData = join([
|
|
160
|
+
TraceType.END_TRANSACTION, String(ctx.timeStamp), ctx.regionId, ctx.groupName,
|
|
161
|
+
bean.topic, bean.msgId, bean.tags, bean.keys, bean.storeHost,
|
|
162
|
+
String(bean.msgType), bean.transactionId, bean.transactionState,
|
|
163
|
+
String(bean.fromTransactionCheck),
|
|
164
|
+
]);
|
|
165
|
+
break;
|
|
166
|
+
}
|
|
167
|
+
case TraceType.RECALL: {
|
|
168
|
+
if (ctx.traceBeans.length === 0)
|
|
169
|
+
return tb;
|
|
170
|
+
const bean = ctx.traceBeans[0];
|
|
171
|
+
tb.transData = join([
|
|
172
|
+
TraceType.RECALL, String(ctx.timeStamp), ctx.regionId, ctx.groupName,
|
|
173
|
+
bean.topic, bean.msgId, String(ctx.isSuccess),
|
|
174
|
+
]);
|
|
175
|
+
break;
|
|
176
|
+
}
|
|
177
|
+
default:
|
|
178
|
+
return tb;
|
|
179
|
+
}
|
|
180
|
+
for (const bean of ctx.traceBeans) {
|
|
181
|
+
tb.transKey.add(bean.msgId);
|
|
182
|
+
if (bean.keys) {
|
|
183
|
+
for (const k of bean.keys.split(' '))
|
|
184
|
+
tb.transKey.add(k);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
return tb;
|
|
188
|
+
}
|
|
189
|
+
// DecodeTraceDataString decodes a batch of trace records.
|
|
190
|
+
//
|
|
191
|
+
// Robustness policy: skip a single record that fails to decode without
|
|
192
|
+
// aborting the whole parse — otherwise one bad record loses the entire batch
|
|
193
|
+
// of traces.
|
|
194
|
+
export function DecodeTraceDataString(traceData) {
|
|
195
|
+
if (!traceData)
|
|
196
|
+
return [];
|
|
197
|
+
const out = [];
|
|
198
|
+
for (const record of splitTrimmingTrailingEmpty(traceData, TraceFieldSplitor)) {
|
|
199
|
+
if (record === '')
|
|
200
|
+
continue;
|
|
201
|
+
const ctx = decodeTraceContext(record);
|
|
202
|
+
if (ctx !== null)
|
|
203
|
+
out.push(ctx);
|
|
204
|
+
}
|
|
205
|
+
return out;
|
|
206
|
+
}
|
|
207
|
+
function decodeTraceContext(record) {
|
|
208
|
+
const line = splitTrimmingTrailingEmpty(record, TraceContentSplitor);
|
|
209
|
+
if (line.length === 0)
|
|
210
|
+
return null;
|
|
211
|
+
const newCtx = () => new TraceContext();
|
|
212
|
+
const newBean = () => new TraceBean();
|
|
213
|
+
switch (line[0]) {
|
|
214
|
+
case TraceType.PUB: {
|
|
215
|
+
if (line.length < 12)
|
|
216
|
+
return null;
|
|
217
|
+
const ts = parseInt(line[1], 10);
|
|
218
|
+
const bodyLen = parseInt(line[9], 10);
|
|
219
|
+
const cost = parseInt(line[10], 10);
|
|
220
|
+
const msgType = traceMsgType(line[11]);
|
|
221
|
+
if (Number.isNaN(ts) || Number.isNaN(bodyLen) || Number.isNaN(cost) || msgType == null)
|
|
222
|
+
return null;
|
|
223
|
+
const ctx = newCtx();
|
|
224
|
+
ctx.traceType = TraceType.PUB;
|
|
225
|
+
ctx.timeStamp = ts;
|
|
226
|
+
ctx.regionId = line[2];
|
|
227
|
+
ctx.groupName = line[3];
|
|
228
|
+
const bean = newBean();
|
|
229
|
+
bean.topic = line[4];
|
|
230
|
+
bean.msgId = line[5];
|
|
231
|
+
bean.tags = line[6];
|
|
232
|
+
bean.keys = line[7];
|
|
233
|
+
bean.storeHost = line[8];
|
|
234
|
+
bean.bodyLength = bodyLen;
|
|
235
|
+
ctx.costTime = cost;
|
|
236
|
+
bean.msgType = msgType;
|
|
237
|
+
// Version tolerance: 13 fields is the pre-offsetMsgId layout, 14 adds
|
|
238
|
+
// it, 15+ adds clientHost.
|
|
239
|
+
if (line.length === 13) {
|
|
240
|
+
ctx.isSuccess = line[12] === 'true';
|
|
241
|
+
}
|
|
242
|
+
else if (line.length === 14) {
|
|
243
|
+
bean.offsetMsgId = line[12];
|
|
244
|
+
ctx.isSuccess = line[13] === 'true';
|
|
245
|
+
}
|
|
246
|
+
else if (line.length >= 15) {
|
|
247
|
+
bean.offsetMsgId = line[12];
|
|
248
|
+
ctx.isSuccess = line[13] === 'true';
|
|
249
|
+
bean.clientHost = line[14];
|
|
250
|
+
}
|
|
251
|
+
ctx.traceBeans = [bean];
|
|
252
|
+
return ctx;
|
|
253
|
+
}
|
|
254
|
+
case TraceType.SUB_BEFORE: {
|
|
255
|
+
if (line.length < 7)
|
|
256
|
+
return null;
|
|
257
|
+
const ts = parseInt(line[1], 10);
|
|
258
|
+
const retry = parseInt(line[6], 10);
|
|
259
|
+
if (Number.isNaN(ts) || Number.isNaN(retry))
|
|
260
|
+
return null;
|
|
261
|
+
const ctx = newCtx();
|
|
262
|
+
ctx.traceType = TraceType.SUB_BEFORE;
|
|
263
|
+
ctx.timeStamp = ts;
|
|
264
|
+
ctx.regionId = line[2];
|
|
265
|
+
ctx.groupName = line[3];
|
|
266
|
+
ctx.requestId = line[4];
|
|
267
|
+
const bean = newBean();
|
|
268
|
+
bean.msgId = line[5];
|
|
269
|
+
bean.retryTimes = retry;
|
|
270
|
+
// Segment 7 is the segment cut away for a message without keys; use traceField as the fallback.
|
|
271
|
+
bean.keys = traceField(line, 7);
|
|
272
|
+
ctx.traceBeans = [bean];
|
|
273
|
+
return ctx;
|
|
274
|
+
}
|
|
275
|
+
case TraceType.SUB_AFTER: {
|
|
276
|
+
if (line.length < 6)
|
|
277
|
+
return null;
|
|
278
|
+
const cost = parseInt(line[3], 10);
|
|
279
|
+
if (Number.isNaN(cost))
|
|
280
|
+
return null;
|
|
281
|
+
const ctx = newCtx();
|
|
282
|
+
ctx.traceType = TraceType.SUB_AFTER;
|
|
283
|
+
ctx.requestId = line[1];
|
|
284
|
+
const bean = newBean();
|
|
285
|
+
bean.msgId = line[2];
|
|
286
|
+
bean.keys = line[5];
|
|
287
|
+
ctx.costTime = cost;
|
|
288
|
+
ctx.isSuccess = line[4] === 'true';
|
|
289
|
+
if (line.length >= 7) {
|
|
290
|
+
const code = parseInt(line[6], 10);
|
|
291
|
+
if (Number.isNaN(code))
|
|
292
|
+
return null;
|
|
293
|
+
ctx.contextCode = code;
|
|
294
|
+
}
|
|
295
|
+
if (line.length >= 9) {
|
|
296
|
+
const ts = parseInt(line[7], 10);
|
|
297
|
+
if (Number.isNaN(ts))
|
|
298
|
+
return null;
|
|
299
|
+
ctx.timeStamp = ts;
|
|
300
|
+
ctx.groupName = line[8];
|
|
301
|
+
}
|
|
302
|
+
ctx.traceBeans = [bean];
|
|
303
|
+
return ctx;
|
|
304
|
+
}
|
|
305
|
+
case TraceType.END_TRANSACTION: {
|
|
306
|
+
if (line.length < 13)
|
|
307
|
+
return null;
|
|
308
|
+
const ts = parseInt(line[1], 10);
|
|
309
|
+
if (Number.isNaN(ts))
|
|
310
|
+
return null;
|
|
311
|
+
const msgType = traceMsgType(line[9]);
|
|
312
|
+
if (msgType == null)
|
|
313
|
+
return null;
|
|
314
|
+
const ctx = newCtx();
|
|
315
|
+
ctx.traceType = TraceType.END_TRANSACTION;
|
|
316
|
+
ctx.timeStamp = ts;
|
|
317
|
+
ctx.regionId = line[2];
|
|
318
|
+
ctx.groupName = line[3];
|
|
319
|
+
const bean = newBean();
|
|
320
|
+
bean.topic = line[4];
|
|
321
|
+
bean.msgId = line[5];
|
|
322
|
+
bean.tags = line[6];
|
|
323
|
+
bean.keys = line[7];
|
|
324
|
+
bean.storeHost = line[8];
|
|
325
|
+
bean.msgType = msgType;
|
|
326
|
+
bean.transactionId = line[10];
|
|
327
|
+
bean.transactionState = line[11];
|
|
328
|
+
bean.fromTransactionCheck = line[12] === 'true';
|
|
329
|
+
ctx.traceBeans = [bean];
|
|
330
|
+
return ctx;
|
|
331
|
+
}
|
|
332
|
+
case TraceType.RECALL: {
|
|
333
|
+
if (line.length < 7)
|
|
334
|
+
return null;
|
|
335
|
+
const ts = parseInt(line[1], 10);
|
|
336
|
+
if (Number.isNaN(ts))
|
|
337
|
+
return null;
|
|
338
|
+
const ctx = newCtx();
|
|
339
|
+
ctx.traceType = TraceType.RECALL;
|
|
340
|
+
ctx.timeStamp = ts;
|
|
341
|
+
ctx.regionId = line[2];
|
|
342
|
+
ctx.groupName = line[3];
|
|
343
|
+
const bean = newBean();
|
|
344
|
+
bean.topic = line[4];
|
|
345
|
+
bean.msgId = line[5];
|
|
346
|
+
ctx.isSuccess = line[6] === 'true';
|
|
347
|
+
ctx.traceBeans = [bean];
|
|
348
|
+
return ctx;
|
|
349
|
+
}
|
|
350
|
+
default:
|
|
351
|
+
return null;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
// Build a TraceBean from a MessageExt (consumer side). The two modes:
|
|
355
|
+
// - offsetBasedId=true -> the broker OFFSET-form id (SendResult.offsetMsgId);
|
|
356
|
+
// - offsetBasedId=false -> Java MessageClientExt.getMsgId() parity: the UNIQ_KEY
|
|
357
|
+
// property, falling back to the offset id (see clientMsgId()).
|
|
358
|
+
// The consumer hooks use the UNIQ_KEY mode; the producer-side Pub trace is a
|
|
359
|
+
// separate path and always uses UNIQ_KEY.
|
|
360
|
+
export function traceBeanFromMessageExt(msg, offsetBasedId) {
|
|
361
|
+
const bean = new TraceBean();
|
|
362
|
+
bean.topic = msg.getTopic();
|
|
363
|
+
bean.msgId = offsetBasedId
|
|
364
|
+
? (msg.getOffsetMsgId() || msg.getMsgId())
|
|
365
|
+
: clientMsgId(msg);
|
|
366
|
+
bean.tags = msg.getProperty(MessageConst.PROPERTY_TAGS) || '';
|
|
367
|
+
bean.keys = msg.getProperty(MessageConst.PROPERTY_KEYS) || '';
|
|
368
|
+
bean.storeHost = msg.getStoreHostString();
|
|
369
|
+
const body = msg.getBody();
|
|
370
|
+
bean.bodyLength = body ? body.length : 0;
|
|
371
|
+
bean.retryTimes = msg.getReconsumeTimes();
|
|
372
|
+
return bean;
|
|
373
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { TraceContext } from './trace_context.js';
|
|
2
|
+
import type { AclRPCHook } from '../remoting/acl.js';
|
|
3
|
+
export type TraceDispatcherType = 'PRODUCER' | 'CONSUME';
|
|
4
|
+
export declare class AsyncTraceDispatcher {
|
|
5
|
+
groupName: string;
|
|
6
|
+
traceType: TraceDispatcherType;
|
|
7
|
+
batchNum: number;
|
|
8
|
+
traceTopic: string;
|
|
9
|
+
accessChannel: string;
|
|
10
|
+
rpcHook: AclRPCHook | null;
|
|
11
|
+
regionId: string;
|
|
12
|
+
namespaceV2: string | null;
|
|
13
|
+
maxMsgSize: number;
|
|
14
|
+
private _producer;
|
|
15
|
+
private _queue;
|
|
16
|
+
private _queueMax;
|
|
17
|
+
private _flushTimer;
|
|
18
|
+
private _running;
|
|
19
|
+
private _nameSrvAddr;
|
|
20
|
+
constructor(groupName: string, traceType?: TraceDispatcherType, batchNum?: number, traceTopic?: string);
|
|
21
|
+
setHostConsumer(_hostConsumer: any): void;
|
|
22
|
+
setHostProducer(_hostProducer: any): void;
|
|
23
|
+
setNamespaceV2(namespaceV2: string | null): void;
|
|
24
|
+
getNamespaceV2(): string | null;
|
|
25
|
+
start(nameSrvAddr?: string, accessChannel?: string): Promise<void>;
|
|
26
|
+
createInternalProducerForTest(): Promise<any>;
|
|
27
|
+
append(ctx: TraceContext | null): boolean;
|
|
28
|
+
appendEndTransaction(producerGroup: string, topic: string, msgId: string, transactionId: string | null, localTransactionState: number, fromTransactionCheck: boolean, brokerAddr: string): boolean;
|
|
29
|
+
flush(): Promise<void>;
|
|
30
|
+
shutdown(): Promise<void>;
|
|
31
|
+
}
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
// AsyncTraceDispatcher: batches TraceContext records, assembles them with the
|
|
2
|
+
// wire codecs, and sends them into the trace topic via an internal producer.
|
|
3
|
+
//
|
|
4
|
+
// Degradation first: tracing must never take the client down. Broker offline,
|
|
5
|
+
// missing topic, producer failure — all are logged and dropped, and the data
|
|
6
|
+
// plane keeps running.
|
|
7
|
+
import { Buffer } from 'node:buffer';
|
|
8
|
+
import { TraceContext, TraceBean, TraceType, TraceMsgType, EncodeTraceContext, TraceGroupNamePrefix, TraceContentSplitor, TraceTopicPrefix, AccessChannelLocal, AccessChannelCloud, DefaultTraceRegionId, } from "./trace_context.js";
|
|
9
|
+
import { getLogger } from "../logging.js";
|
|
10
|
+
const logger = getLogger('client.trace_dispatcher');
|
|
11
|
+
export class AsyncTraceDispatcher {
|
|
12
|
+
groupName;
|
|
13
|
+
traceType;
|
|
14
|
+
batchNum;
|
|
15
|
+
traceTopic;
|
|
16
|
+
// Java AsyncTraceDispatcher#accessChannel (default LOCAL). CLOUD routes records to
|
|
17
|
+
// TraceTopicPrefix + regionId; the facade's setAccessChannel value arrives via start().
|
|
18
|
+
accessChannel = AccessChannelLocal;
|
|
19
|
+
// The host's ACL hook. Java builds the internal trace producer with
|
|
20
|
+
// `new DefaultMQProducer(rpcHook)`: without it an ACL-enabled broker rejects every
|
|
21
|
+
// trace request and no record lands (neither in LOCAL nor in CLOUD mode).
|
|
22
|
+
rpcHook = null;
|
|
23
|
+
regionId = '';
|
|
24
|
+
// namespaceV2 goes out with the internal trace producer so trace rpcs also carry the nsd/ns ext fields, letting the broker side tell instances apart.
|
|
25
|
+
namespaceV2 = null;
|
|
26
|
+
// The trace payload is sent in slices no larger than maxMsgSize; the internal trace producer's maxMessageSize takes the same value, default 128000.
|
|
27
|
+
maxMsgSize = 128000;
|
|
28
|
+
// The internal producer is created lazily on start() (zero-cost when trace
|
|
29
|
+
// is disabled).
|
|
30
|
+
_producer = null;
|
|
31
|
+
_queue = [];
|
|
32
|
+
_queueMax = 2048;
|
|
33
|
+
_flushTimer = null;
|
|
34
|
+
_running = false;
|
|
35
|
+
_nameSrvAddr = '';
|
|
36
|
+
constructor(groupName, traceType = 'PRODUCER', batchNum = 10, traceTopic) {
|
|
37
|
+
this.groupName = groupName;
|
|
38
|
+
this.traceType = traceType;
|
|
39
|
+
this.batchNum = Math.min(Math.max(1, batchNum), 20);
|
|
40
|
+
// The default trace topic is RMQ_SYS_TRACE_TOPIC; an explicit traceTopic overrides the default.
|
|
41
|
+
this.traceTopic = traceTopic || 'RMQ_SYS_TRACE_TOPIC';
|
|
42
|
+
}
|
|
43
|
+
setHostConsumer(_hostConsumer) { }
|
|
44
|
+
setHostProducer(_hostProducer) { }
|
|
45
|
+
setNamespaceV2(namespaceV2) { this.namespaceV2 = namespaceV2; }
|
|
46
|
+
getNamespaceV2() { return this.namespaceV2; }
|
|
47
|
+
async start(nameSrvAddr, accessChannel) {
|
|
48
|
+
if (nameSrvAddr)
|
|
49
|
+
this._nameSrvAddr = nameSrvAddr;
|
|
50
|
+
// Java AsyncTraceDispatcher#start(nameSrvAddr, accessChannel) (:151-159): remember
|
|
51
|
+
// the channel even when the worker was already running. A blank argument keeps the
|
|
52
|
+
// current value so repeated starts from a facade cannot silently reset CLOUD.
|
|
53
|
+
if (accessChannel)
|
|
54
|
+
this.accessChannel = accessChannel;
|
|
55
|
+
if (this._running)
|
|
56
|
+
return;
|
|
57
|
+
this._running = true;
|
|
58
|
+
try {
|
|
59
|
+
const producer = await this.createInternalProducerForTest();
|
|
60
|
+
await producer.start();
|
|
61
|
+
this._producer = producer;
|
|
62
|
+
}
|
|
63
|
+
catch (e) {
|
|
64
|
+
logger.warning('trace internal producer start failed (trace disabled): %s', e.message);
|
|
65
|
+
this._producer = null;
|
|
66
|
+
}
|
|
67
|
+
this._flushTimer = setInterval(() => {
|
|
68
|
+
this.flush().catch((e) => logger.debug('trace flush error: %s', e.message));
|
|
69
|
+
}, 5000);
|
|
70
|
+
if (typeof this._flushTimer.unref === 'function')
|
|
71
|
+
this._flushTimer.unref();
|
|
72
|
+
}
|
|
73
|
+
// Test-only factory: build the internal producer exactly the way start() does, without
|
|
74
|
+
// starting it. Kept public so the offline suite can assert the ACL hook / namespace are
|
|
75
|
+
// really attached (a trace producer without the host's signature writes nothing).
|
|
76
|
+
async createInternalProducerForTest() {
|
|
77
|
+
const { DefaultMQProducer } = await import("./producer.js");
|
|
78
|
+
// The internal producer group is named <group>_INNER_TRACE_PRODUCER.
|
|
79
|
+
const producer = new DefaultMQProducer(`${this.groupName}${TraceGroupNamePrefix}`);
|
|
80
|
+
producer.setNamesrvAddr(this._nameSrvAddr);
|
|
81
|
+
// If the client configures namespaceV2, the internal trace producer must carry it too; otherwise trace rpcs become the only requests the broker cannot attribute to an instance.
|
|
82
|
+
producer.setNamespaceV2(this.namespaceV2);
|
|
83
|
+
// Java getAndCreateTraceProducer (:167-177) is `new DefaultMQProducer(rpcHook)`.
|
|
84
|
+
// The host's ACL hook must sign trace requests as well, otherwise an ACL broker
|
|
85
|
+
// rejects every send and the trace topic stays empty.
|
|
86
|
+
producer.rpcHook = this.rpcHook;
|
|
87
|
+
// The hook list is supposed to hang on each producer — the internal trace
|
|
88
|
+
// producer's own hook list is empty, so its messages are not traced. On
|
|
89
|
+
// the node side SendMessageHook is a global registry, so a skip flag
|
|
90
|
+
// exempts the internal producer from the shared hooks; otherwise every
|
|
91
|
+
// write to the trace topic would spawn one extra fake Pub record.
|
|
92
|
+
producer._skipSendHooks = true;
|
|
93
|
+
return producer;
|
|
94
|
+
}
|
|
95
|
+
// append buffers one event; the queue is bounded so a slow broker cannot
|
|
96
|
+
// grow memory without limit (oldest records are dropped first).
|
|
97
|
+
append(ctx) {
|
|
98
|
+
if (ctx == null)
|
|
99
|
+
return false;
|
|
100
|
+
if (this._queue.length >= this._queueMax) {
|
|
101
|
+
this._queue.shift();
|
|
102
|
+
}
|
|
103
|
+
this._queue.push(ctx);
|
|
104
|
+
if (this._queue.length >= this.batchNum) {
|
|
105
|
+
this.flush().catch((e) => logger.debug('trace flush error: %s', e.message));
|
|
106
|
+
}
|
|
107
|
+
return true;
|
|
108
|
+
}
|
|
109
|
+
// appendEndTransaction builds a transactional message's EndTransaction
|
|
110
|
+
// record. transactionState uses the LocalTransactionState enum name (a wire
|
|
111
|
+
// literal).
|
|
112
|
+
appendEndTransaction(producerGroup, topic, msgId, transactionId, localTransactionState, fromTransactionCheck, brokerAddr) {
|
|
113
|
+
try {
|
|
114
|
+
const ctx = new TraceContext();
|
|
115
|
+
ctx.traceType = TraceType.END_TRANSACTION;
|
|
116
|
+
ctx.timeStamp = Date.now();
|
|
117
|
+
ctx.isSuccess = true;
|
|
118
|
+
// The dispatcher's channel (Java falls back to it when a context carries none),
|
|
119
|
+
// not a hard-coded LOCAL, otherwise a CLOUD facade's transaction records would
|
|
120
|
+
// still land in RMQ_SYS_TRACE_TOPIC.
|
|
121
|
+
ctx.accessChannel = this.accessChannel;
|
|
122
|
+
ctx.regionId = brokerAddr || DefaultTraceRegionId;
|
|
123
|
+
ctx.groupName = producerGroup || '';
|
|
124
|
+
const bean = new TraceBean();
|
|
125
|
+
bean.topic = topic || '';
|
|
126
|
+
bean.msgId = msgId || '';
|
|
127
|
+
bean.msgType = TraceMsgType.TRANSACTION;
|
|
128
|
+
bean.transactionId = transactionId || '';
|
|
129
|
+
// LocalTransactionState ordinal -> state name (for console queries).
|
|
130
|
+
bean.transactionState =
|
|
131
|
+
localTransactionState === 0 ? 'COMMIT_MESSAGE'
|
|
132
|
+
: localTransactionState === 1 ? 'ROLLBACK_MESSAGE' : 'UNKNOW';
|
|
133
|
+
bean.fromTransactionCheck = fromTransactionCheck;
|
|
134
|
+
ctx.traceBeans = [bean];
|
|
135
|
+
return this.append(ctx);
|
|
136
|
+
}
|
|
137
|
+
catch (e) {
|
|
138
|
+
logger.debug('append end-transaction trace failed: %s', e.message);
|
|
139
|
+
return false;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
// flush encodes every buffered context and sends the joined payload. Each
|
|
143
|
+
// chunk's message keys are the transKey set (the console resolves a
|
|
144
|
+
// message's trace by these).
|
|
145
|
+
//
|
|
146
|
+
// Java AsyncTraceDispatcher#sendTraceData (:309-334): every TraceContext picks its
|
|
147
|
+
// own accessChannel, falling back to the dispatcher's when it carries none; CLOUD
|
|
148
|
+
// sends to "rmq_sys_TRACE_DATA_" + regionId instead of traceTopic. Records are
|
|
149
|
+
// grouped by (business topic, trace topic) BEFORE encoding/chunking, so a CLOUD
|
|
150
|
+
// record can never be flushed into the LOCAL topic. Contexts with no region or no
|
|
151
|
+
// beans are skipped (Java :316-318) -- every hook fills regionId for that reason.
|
|
152
|
+
async flush() {
|
|
153
|
+
if (this._queue.length === 0)
|
|
154
|
+
return;
|
|
155
|
+
if (!this._producer) {
|
|
156
|
+
this._queue = []; // no producer: drop, never block the data path
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
// Drain the queue under a snapshot; appends during the send are kept for
|
|
160
|
+
// the next round.
|
|
161
|
+
const batch = this._queue.splice(0, this._queue.length);
|
|
162
|
+
const groups = new Map();
|
|
163
|
+
const sendChunk = async (g) => {
|
|
164
|
+
if (!g.buffer)
|
|
165
|
+
return;
|
|
166
|
+
const data = g.buffer;
|
|
167
|
+
const chunkKeys = new Set(g.keys);
|
|
168
|
+
g.buffer = '';
|
|
169
|
+
g.keys.clear();
|
|
170
|
+
const { Message } = await import("../common/message.js");
|
|
171
|
+
const msg = new Message(g.traceTopic, Buffer.from(data, 'utf-8'));
|
|
172
|
+
if (chunkKeys.size > 0)
|
|
173
|
+
msg.setKeys(Array.from(chunkKeys).slice(0, 100).join(' '));
|
|
174
|
+
await this._producer.send(msg);
|
|
175
|
+
};
|
|
176
|
+
for (const ctx of batch) {
|
|
177
|
+
const channel = ctx.accessChannel || this.accessChannel;
|
|
178
|
+
const regionId = ctx.regionId;
|
|
179
|
+
if (!regionId || !ctx.traceBeans || ctx.traceBeans.length === 0)
|
|
180
|
+
continue;
|
|
181
|
+
const traceTopic = channel === AccessChannelCloud
|
|
182
|
+
? TraceTopicPrefix + regionId
|
|
183
|
+
: this.traceTopic;
|
|
184
|
+
const businessTopic = ctx.traceBeans[0].topic || '';
|
|
185
|
+
// TraceConstants.CONTENT_SPLITOR is the Java grouping delimiter.
|
|
186
|
+
const key = businessTopic + TraceContentSplitor + traceTopic;
|
|
187
|
+
let g = groups.get(key);
|
|
188
|
+
if (!g) {
|
|
189
|
+
g = { traceTopic, buffer: '', keys: new Set() };
|
|
190
|
+
groups.set(key, g);
|
|
191
|
+
}
|
|
192
|
+
const tb = EncodeTraceContext(ctx);
|
|
193
|
+
if (tb == null || !tb.transData)
|
|
194
|
+
continue;
|
|
195
|
+
g.buffer += tb.transData;
|
|
196
|
+
for (const k of tb.transKey)
|
|
197
|
+
g.keys.add(k);
|
|
198
|
+
if (g.buffer.length >= this.maxMsgSize) {
|
|
199
|
+
// One big batch exceeding the broker's maxMessageSize would be rejected.
|
|
200
|
+
try {
|
|
201
|
+
await sendChunk(g);
|
|
202
|
+
}
|
|
203
|
+
catch (e) {
|
|
204
|
+
logger.debug('trace send failed (records dropped): %s', e.message);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
for (const g of groups.values()) {
|
|
209
|
+
if (!g.buffer)
|
|
210
|
+
continue;
|
|
211
|
+
try {
|
|
212
|
+
await sendChunk(g);
|
|
213
|
+
}
|
|
214
|
+
catch (e) {
|
|
215
|
+
logger.debug('trace send failed (records dropped): %s', e.message);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
async shutdown() {
|
|
220
|
+
if (!this._running)
|
|
221
|
+
return;
|
|
222
|
+
this._running = false;
|
|
223
|
+
if (this._flushTimer) {
|
|
224
|
+
clearInterval(this._flushTimer);
|
|
225
|
+
this._flushTimer = null;
|
|
226
|
+
}
|
|
227
|
+
await this.flush(); // last records must land before the producer dies
|
|
228
|
+
if (this._producer) {
|
|
229
|
+
try {
|
|
230
|
+
this._producer.shutdown();
|
|
231
|
+
}
|
|
232
|
+
catch (e) { /* ignore */ }
|
|
233
|
+
this._producer = null;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { ConsumeMessageHook, SendMessageHook, SendMessageContext, ConsumeMessageContext } from './hook.js';
|
|
2
|
+
import type { AsyncTraceDispatcher } from './trace_dispatcher.js';
|
|
3
|
+
export declare class SendMessageTraceHookImpl extends SendMessageHook {
|
|
4
|
+
private dispatcher;
|
|
5
|
+
constructor(dispatcher: AsyncTraceDispatcher);
|
|
6
|
+
getHookName(): string;
|
|
7
|
+
sendMessageBefore(ctx: SendMessageContext): void;
|
|
8
|
+
sendMessageAfter(ctx: SendMessageContext): void;
|
|
9
|
+
}
|
|
10
|
+
export declare class ConsumeMessageTraceHookImpl extends ConsumeMessageHook {
|
|
11
|
+
private dispatcher;
|
|
12
|
+
constructor(dispatcher: AsyncTraceDispatcher);
|
|
13
|
+
getHookName(): string;
|
|
14
|
+
consumeMessageBefore(ctx: ConsumeMessageContext): void;
|
|
15
|
+
consumeMessageAfter(ctx: ConsumeMessageContext): void;
|
|
16
|
+
}
|
|
17
|
+
export declare class DefaultRecallMessageTraceHook {
|
|
18
|
+
private dispatcher;
|
|
19
|
+
private enableDefaultTrace;
|
|
20
|
+
constructor(dispatcher: AsyncTraceDispatcher | null);
|
|
21
|
+
doBeforeRequest(_remoteAddr: string, _request: any): void;
|
|
22
|
+
doAfterResponse(remoteAddr: string, request: any, response: any): void;
|
|
23
|
+
}
|