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.
Files changed (137) hide show
  1. package/README.en.md +428 -0
  2. package/README.md +424 -2
  3. package/dist/client/admin.d.ts +229 -0
  4. package/dist/client/admin.js +2462 -0
  5. package/dist/client/allocate.d.ts +48 -0
  6. package/dist/client/allocate.js +365 -0
  7. package/dist/client/backpressure.d.ts +23 -0
  8. package/dist/client/backpressure.js +95 -0
  9. package/dist/client/consumer.d.ts +294 -0
  10. package/dist/client/consumer.js +3095 -0
  11. package/dist/client/consumer_result.d.ts +129 -0
  12. package/dist/client/consumer_result.js +138 -0
  13. package/dist/client/consumer_stats.d.ts +69 -0
  14. package/dist/client/consumer_stats.js +258 -0
  15. package/dist/client/exception.d.ts +39 -0
  16. package/dist/client/exception.js +36 -0
  17. package/dist/client/hook.d.ts +145 -0
  18. package/dist/client/hook.js +171 -0
  19. package/dist/client/latency.d.ts +61 -0
  20. package/dist/client/latency.js +239 -0
  21. package/dist/client/lite_pull_consumer.d.ts +151 -0
  22. package/dist/client/lite_pull_consumer.js +1096 -0
  23. package/dist/client/mq_client.d.ts +188 -0
  24. package/dist/client/mq_client.js +1678 -0
  25. package/dist/client/mq_client_manager.d.ts +21 -0
  26. package/dist/client/mq_client_manager.js +129 -0
  27. package/dist/client/offset_store.d.ts +45 -0
  28. package/dist/client/offset_store.js +389 -0
  29. package/dist/client/open_tracing_hook.d.ts +52 -0
  30. package/dist/client/open_tracing_hook.js +241 -0
  31. package/dist/client/pop_api.d.ts +28 -0
  32. package/dist/client/pop_api.js +224 -0
  33. package/dist/client/pop_consumer.d.ts +23 -0
  34. package/dist/client/pop_consumer.js +719 -0
  35. package/dist/client/pop_process_queue.d.ts +15 -0
  36. package/dist/client/pop_process_queue.js +51 -0
  37. package/dist/client/process_queue.d.ts +49 -0
  38. package/dist/client/process_queue.js +261 -0
  39. package/dist/client/produce_accumulator.d.ts +75 -0
  40. package/dist/client/produce_accumulator.js +288 -0
  41. package/dist/client/producer.d.ts +231 -0
  42. package/dist/client/producer.js +1774 -0
  43. package/dist/client/pull_api.d.ts +26 -0
  44. package/dist/client/pull_api.js +264 -0
  45. package/dist/client/pull_consumer.d.ts +119 -0
  46. package/dist/client/pull_consumer.js +876 -0
  47. package/dist/client/pull_schedule_service.d.ts +71 -0
  48. package/dist/client/pull_schedule_service.js +424 -0
  49. package/dist/client/request_reply.d.ts +61 -0
  50. package/dist/client/request_reply.js +232 -0
  51. package/dist/client/retry_message.d.ts +2 -0
  52. package/dist/client/retry_message.js +27 -0
  53. package/dist/client/send_result.d.ts +57 -0
  54. package/dist/client/send_result.js +70 -0
  55. package/dist/client/top_addressing.d.ts +20 -0
  56. package/dist/client/top_addressing.js +75 -0
  57. package/dist/client/trace_context.d.ts +61 -0
  58. package/dist/client/trace_context.js +373 -0
  59. package/dist/client/trace_dispatcher.d.ts +31 -0
  60. package/dist/client/trace_dispatcher.js +236 -0
  61. package/dist/client/trace_hook.d.ts +23 -0
  62. package/dist/client/trace_hook.js +228 -0
  63. package/dist/client/traceparent.d.ts +9 -0
  64. package/dist/client/traceparent.js +86 -0
  65. package/dist/common/boundary_type.d.ts +7 -0
  66. package/dist/common/boundary_type.js +16 -0
  67. package/dist/common/compress.d.ts +28 -0
  68. package/dist/common/compress.js +512 -0
  69. package/dist/common/message.d.ts +129 -0
  70. package/dist/common/message.js +214 -0
  71. package/dist/common/messageClientIdSetter.d.ts +7 -0
  72. package/dist/common/messageClientIdSetter.js +48 -0
  73. package/dist/common/messageConst.d.ts +68 -0
  74. package/dist/common/messageConst.js +68 -0
  75. package/dist/common/messageDecoder.d.ts +40 -0
  76. package/dist/common/messageDecoder.js +504 -0
  77. package/dist/common/messageType.d.ts +11 -0
  78. package/dist/common/messageType.js +11 -0
  79. package/dist/common/message_accessor.d.ts +39 -0
  80. package/dist/common/message_accessor.js +135 -0
  81. package/dist/common/mixAll.d.ts +106 -0
  82. package/dist/common/mixAll.js +247 -0
  83. package/dist/common/recall_message_handle.d.ts +25 -0
  84. package/dist/common/recall_message_handle.js +98 -0
  85. package/dist/common/subscriptionData.d.ts +36 -0
  86. package/dist/common/subscriptionData.js +64 -0
  87. package/dist/common/sysflag.d.ts +129 -0
  88. package/dist/common/sysflag.js +96 -0
  89. package/dist/common/topic_config.d.ts +66 -0
  90. package/dist/common/topic_config.js +106 -0
  91. package/dist/common/topic_validator.d.ts +49 -0
  92. package/dist/common/topic_validator.js +115 -0
  93. package/dist/common/utilAll.d.ts +39 -0
  94. package/dist/common/utilAll.js +154 -0
  95. package/dist/common/validators.d.ts +25 -0
  96. package/dist/common/validators.js +80 -0
  97. package/dist/index.d.ts +24 -0
  98. package/dist/index.js +36 -0
  99. package/dist/logging.d.ts +24 -0
  100. package/dist/logging.js +246 -0
  101. package/dist/remoting/acl.d.ts +16 -0
  102. package/dist/remoting/acl.js +39 -0
  103. package/dist/remoting/admin_body.d.ts +257 -0
  104. package/dist/remoting/admin_body.js +669 -0
  105. package/dist/remoting/bodies.d.ts +475 -0
  106. package/dist/remoting/bodies.js +1131 -0
  107. package/dist/remoting/client.d.ts +73 -0
  108. package/dist/remoting/client.js +591 -0
  109. package/dist/remoting/codes.d.ts +296 -0
  110. package/dist/remoting/codes.js +301 -0
  111. package/dist/remoting/exception.d.ts +45 -0
  112. package/dist/remoting/exception.js +58 -0
  113. package/dist/remoting/extra_info.d.ts +61 -0
  114. package/dist/remoting/extra_info.js +220 -0
  115. package/dist/remoting/headers.d.ts +758 -0
  116. package/dist/remoting/headers.js +1307 -0
  117. package/dist/remoting/heartbeat.d.ts +91 -0
  118. package/dist/remoting/heartbeat.js +227 -0
  119. package/dist/remoting/namespace.d.ts +14 -0
  120. package/dist/remoting/namespace.js +85 -0
  121. package/dist/remoting/pop_bodies.d.ts +58 -0
  122. package/dist/remoting/pop_bodies.js +214 -0
  123. package/dist/remoting/remotingCommand.d.ts +39 -0
  124. package/dist/remoting/remotingCommand.js +225 -0
  125. package/dist/remoting/route.d.ts +61 -0
  126. package/dist/remoting/route.js +288 -0
  127. package/dist/remoting/rpc_hooks.d.ts +31 -0
  128. package/dist/remoting/rpc_hooks.js +73 -0
  129. package/dist/remoting/serialize.d.ts +33 -0
  130. package/dist/remoting/serialize.js +372 -0
  131. package/dist/remoting/socks_proxy.d.ts +29 -0
  132. package/dist/remoting/socks_proxy.js +379 -0
  133. package/dist/remoting/static_topic.d.ts +49 -0
  134. package/dist/remoting/static_topic.js +244 -0
  135. package/dist/remoting/subscription.d.ts +65 -0
  136. package/dist/remoting/subscription.js +172 -0
  137. package/package.json +38 -4
@@ -0,0 +1,2462 @@
1
+ // -*- coding: utf-8 -*-
2
+ // DefaultMQAdminExt — the management client: topic/group/offset/config/cluster
3
+ // administration over the remoting protocol.
4
+ //
5
+ // This port issues the admin RPCs through MQClient's RemotingClient directly —
6
+ // no separate admin API layer.
7
+ //
8
+ // Wire facts that were expensive to establish — do NOT "simplify" them:
9
+ //
10
+ // - GET_BROKER_CONFIG(26)'s body is Properties-format TEXT (`k=v\n`) on the
11
+ // broker side, not JSON. Parsing it as a KVTable fails on every real broker.
12
+ // - UPDATE_AND_CREATE_SUBSCRIPTIONGROUP(200)'s body is a
13
+ // SubscriptionGroupConfig JSON object.
14
+ // - GET_TOPIC_CONFIG(351) sends `topic` + `lo`, and its body is a plain
15
+ // TopicConfig JSON.
16
+ // - GET_ALL_SUBSCRIPTIONGROUP_CONFIG(201) and GET_ALL_TOPIC_CONFIG(21) are
17
+ // PAGED: seq / page size / dataVersion, accumulating until seq >= total-1.
18
+ // The first page sends dataVersion as the EMPTY STRING. A broker that
19
+ // omits totalNum returns everything in one round.
20
+ // - fetchTopicsByCluster sends ext key `cluster` (NOT `clusterName`); with
21
+ // the wrong key the nameserver NPEs internally, swallows it, and answers
22
+ // SUCCESS with an empty list.
23
+ // - The KV-config PUT/DELETE requests go to the NAMESERVER and must be
24
+ // BROADCAST to every nameserver — one nameserver alone would not replicate.
25
+ // - ResetOffsetBody.offsetTable is keyed by MessageQueue; the broker's fastjson
26
+ // codec inlines the queue object as the map key, so decoding uses
27
+ // decodeMessageQueueMap.
28
+ // - UPDATE_AND_CREATE_TOPIC(17) MUST carry `topicFilterType`: the broker's
29
+ // create-topic header validation rejects a null with
30
+ // `topicFilterType = [null] value invalid`.
31
+ // - The auth user/ACL management RPCs (3001..3010) are dialled at the address
32
+ // the caller passed, VERBATIM: the auth service listens on the plain broker
33
+ // port, so neither the VIP translation nor a route lookup belongs on that
34
+ // path. Their header sets are exactly the field names listed per method
35
+ // below — an extra key or an empty-string value is a different request.
36
+ import { RemotingCommand } from "../remoting/remotingCommand.js";
37
+ import { RequestCode, ResponseCode } from "../remoting/codes.js";
38
+ import { TopicRouteData } from "../remoting/route.js";
39
+ import { ClusterInfo, ConsumerConnection, ConsumeStats, ConsumeStatsList, GetConsumerListByGroupResponseBody, GroupList, KVTable, ProducerConnection, ResetOffsetBody, TopicConfig, TopicConfigSerializeWrapper, TopicList, TopicFilterType, ConsumeMessageDirectlyResult, QueryConsumeTimeSpanBody, QueryCorrectionOffsetBody, ProducerTableInfo, HARuntimeInfo, EpochEntryCache, BrokerMemberGroup, } from "../remoting/bodies.js";
40
+ import { QueryCorrectionOffsetHeader, ResetMasterFlushOffsetHeader, ElectMasterRequestHeader, ElectMasterResponseHeader, } from "../remoting/headers.js";
41
+ import { TopicStatsTable, UserInfo, AclInfo, QueryConsumeQueueResponseBody, GroupForbidden, BrokerReplicasInfo, GetMetaDataResponseHeader, CheckRocksdbCqWriteResult, GetBrokerLiteInfoResponseBody, GetParentTopicInfoResponseBody, GetLiteTopicInfoResponseBody, GetLiteClientInfoResponseBody, GetLiteGroupInfoResponseBody, BrokerOperatorResult, AdminToolResult, AdminToolsResultCodeEnum, } from "../remoting/admin_body.js";
42
+ import { RemotingSerializable } from "../remoting/serialize.js";
43
+ import { ConsumeType } from "../remoting/heartbeat.js";
44
+ import { MQClientManager } from "./mq_client_manager.js";
45
+ import { MixAll } from "../common/mixAll.js";
46
+ import { MessageQueue } from "../common/message.js";
47
+ import { MessageConst } from "../common/messageConst.js";
48
+ import { BoundaryType } from "../common/boundary_type.js";
49
+ import { decodeMessages } from "../common/messageDecoder.js";
50
+ import { QueryResult } from "../remoting/bodies.js";
51
+ import { getLogger } from "../logging.js";
52
+ import { MQClientException, MQBrokerException, RemotingConnectException, RemotingSendRequestException, RemotingTimeoutException, InterruptedException, } from "../remoting/exception.js";
53
+ import { isBlank } from "../common/utilAll.js";
54
+ import { MQClient } from "./mq_client.js";
55
+ import { registerRpcHooks } from "../remoting/rpc_hooks.js";
56
+ const logger = getLogger('admin');
57
+ // Default admin RPC timeout: 20000 ms.
58
+ export const DefaultAdminTimeoutMillis = 20000;
59
+ // Default admin consumer group: `admin_ext_group`.
60
+ export const DefaultAdminExtGroup = 'admin_ext_group';
61
+ // KV namespace holding ordered-topic config — the one cleaned on shutdown.
62
+ export const NAMESPACE_ORDER_TOPIC_CONFIG = 'ORDER_TOPIC_CONFIG';
63
+ // POP retry topic separator on the wire: `_`.
64
+ const POP_RETRY_SEPARATOR_V1 = '_';
65
+ // Header key of SET_COMMITLOG_READ_MODE(2004) — the request has no header
66
+ // class, this one field is written by hand.
67
+ const FILE_READAHEAD_MODE_KEY = 'READ_AHEAD_MODE';
68
+ // Header key of SWITCH_TIMER_ENGINE(5001) — MessageConst.TIMER_ENGINE_TYPE on
69
+ // the Java side; there is no header class for it either.
70
+ const TIMER_ENGINE_TYPE_KEY = 'E_T';
71
+ // Fixed budget of the leader-routed controller RPCs (metadata / sync state /
72
+ // clean): the controller answers locally, a longer wait only hides a dead
73
+ // controller.
74
+ const ControllerInvokeTimeoutMillis = 3000;
75
+ // ---------------- Properties-text codec ----------------
76
+ // Parses/serialises the broker-side Properties TEXT format: blank lines and
77
+ // `#`/`!` comments are skipped, an UNESCAPED trailing `\` continues onto the
78
+ // next line, the key ends at the first `=`, `:` or whitespace, and the value's
79
+ // trailing whitespace is preserved. Broker config exports never contain
80
+ // `\t`/`\n`/`\uXXXX` escapes, so this codec deliberately does not expand them.
81
+ function splitLines(text) {
82
+ return text.split(/\r\n|\n|\r/);
83
+ }
84
+ export function string2Properties(text) {
85
+ const result = {};
86
+ if (!text)
87
+ return result;
88
+ const propWS = ' \t\f';
89
+ // Merge continuation lines into logical lines.
90
+ const logical = [];
91
+ let pending = '';
92
+ let hasPending = false;
93
+ for (const raw of splitLines(text)) {
94
+ let line = raw;
95
+ if (hasPending) {
96
+ line = pending + line.replace(new RegExp(`^[${propWS}]+`), '');
97
+ hasPending = false;
98
+ }
99
+ let trailing = 0;
100
+ for (let k = line.length - 1; k >= 0 && line[k] === '\\'; k--)
101
+ trailing++;
102
+ if (trailing % 2 === 1) {
103
+ pending = line.slice(0, -1);
104
+ hasPending = true;
105
+ continue;
106
+ }
107
+ logical.push(line);
108
+ }
109
+ if (hasPending)
110
+ logical.push(pending);
111
+ for (const line of logical) {
112
+ const stripped = line.trim();
113
+ if (stripped === '' || stripped[0] === '#' || stripped[0] === '!')
114
+ continue;
115
+ const n = line.length;
116
+ let pos = 0;
117
+ while (pos < n && propWS.includes(line[pos]))
118
+ pos++;
119
+ const keyStart = pos;
120
+ while (pos < n && line[pos] !== '=' && line[pos] !== ':' && !propWS.includes(line[pos]))
121
+ pos++;
122
+ const key = line.slice(keyStart, pos);
123
+ while (pos < n && propWS.includes(line[pos]))
124
+ pos++;
125
+ let value = '';
126
+ if (pos < n && (line[pos] === '=' || line[pos] === ':')) {
127
+ pos++;
128
+ while (pos < n && propWS.includes(line[pos]))
129
+ pos++;
130
+ value = line.slice(pos);
131
+ }
132
+ if (key)
133
+ result[key] = value;
134
+ }
135
+ return result;
136
+ }
137
+ function propertiesToString(properties) {
138
+ const keys = Object.keys(properties).sort();
139
+ return keys.map((k) => `${k}=${properties[k]}\n`).join('');
140
+ }
141
+ function sameDataVersion(a, b) {
142
+ return JSON.stringify(a) === JSON.stringify(b);
143
+ }
144
+ // Renders ext values the way the remoting header-to-wire encoding does:
145
+ // plain string for numbers, "true"/"false" for booleans. Keys written here
146
+ // MUST be the exact protocol field names — the broker reads them back by
147
+ // reflection (the `cluster`-not-`clusterName` trap lives exactly here).
148
+ function extValue(v) {
149
+ if (v == null)
150
+ return '';
151
+ if (typeof v === 'boolean')
152
+ return v ? 'true' : 'false';
153
+ return String(v);
154
+ }
155
+ function adminExt(kv) {
156
+ const out = {};
157
+ for (const [k, v] of Object.entries(kv))
158
+ out[k] = extValue(v);
159
+ return out;
160
+ }
161
+ // Header map for the auth RPCs. A field with no value is dropped rather than
162
+ // sent: `adminExt` above renders an unset value as "", and the auth-side header
163
+ // validation reads an empty string as a supplied (empty) value — a different
164
+ // request from an absent one.
165
+ function authExt(kv) {
166
+ const out = {};
167
+ for (const [k, v] of Object.entries(kv)) {
168
+ if (v === null || v === undefined)
169
+ continue;
170
+ out[k] = extValue(v);
171
+ }
172
+ return out;
173
+ }
174
+ export class DefaultMQAdminExt {
175
+ adminExtGroup;
176
+ createTopicKey;
177
+ namesrvAddr = null;
178
+ instanceName = 'ADMIN';
179
+ unitName = null;
180
+ enableStreamRequestType = false;
181
+ // Server-side namespaceV2 (Aliyun-style serverless instance id). When
182
+ // non-empty, NamespaceRpcHook (registered FIRST on the remoting client, so
183
+ // the ACL signature covers it) stamps every admin rpc with nsd=true /
184
+ // ns=<value>.
185
+ namespaceV2 = null;
186
+ // VIP channel flag — false by default. When true, broker requests go to the
187
+ // VIP port (port - 2); NEVER applied to the nameserver.
188
+ vipChannelEnabled = false;
189
+ pollNameServerIntervalMillis = 30000;
190
+ // Page size of the paged metadata reads (GET_ALL_TOPIC_CONFIG /
191
+ // GET_ALL_SUBSCRIPTIONGROUP_CONFIG): maxTopicNum / maxGroupNum on the wire.
192
+ // Default 2000.
193
+ maxPageSizeInGetMetadata = 2000;
194
+ rpcHook;
195
+ timeoutMillis;
196
+ // KV namespaces cleaned up when a topic is deleted: ORDER_TOPIC_CONFIG.
197
+ kvNamespaceToDeleteList = [NAMESPACE_ORDER_TOPIC_CONFIG];
198
+ client;
199
+ _started = false;
200
+ constructor(rpcHook = null, timeoutMillis = DefaultAdminTimeoutMillis, adminExtGroup = DefaultAdminExtGroup) {
201
+ this.rpcHook = rpcHook;
202
+ this.timeoutMillis = timeoutMillis;
203
+ this.adminExtGroup = adminExtGroup;
204
+ this.createTopicKey = MixAll.DEFAULT_TOPIC;
205
+ this.client = null;
206
+ }
207
+ setNamesrvAddr(addr) { this.namesrvAddr = addr; }
208
+ setNameServerAddressList(addrs) { this.namesrvAddr = addrs.join(';'); }
209
+ setInstanceName(name) { this.instanceName = name; }
210
+ setUnitName(unitName) { this.unitName = unitName; }
211
+ // Java DefaultMQAdminExt#setAdminExtGroup / #getAdminExtGroup. The group identity the admin
212
+ // announces; this port's admin sends no synthetic consumer heartbeat, so nothing travels
213
+ // under this name — present for Java-configured code and for the start/shutdown log lines.
214
+ setAdminExtGroup(group) { this.adminExtGroup = group; }
215
+ getAdminExtGroup() { return this.adminExtGroup; }
216
+ setVipChannelEnabled(enable) {
217
+ this.vipChannelEnabled = enable;
218
+ // The offset / consumer-list requests are issued on the shared instance, so
219
+ // keep its VIP view in step with this facade's switch.
220
+ if (this.client != null)
221
+ this.client.setVipChannelEnabled(enable);
222
+ }
223
+ // Server-side namespace accessor, read live per request by NamespaceRpcHook.
224
+ setNamespaceV2(namespaceV2) { this.namespaceV2 = namespaceV2; }
225
+ getNamespaceV2() { return this.namespaceV2; }
226
+ setTimeoutMillis(timeoutMillis) { this.timeoutMillis = timeoutMillis; }
227
+ // Page size used as maxTopicNum / maxGroupNum by the paged metadata calls.
228
+ // Applied to the shared instance when it is already up, and at start().
229
+ setMaxPageSizeInGetMetadata(size) {
230
+ if (!Number.isInteger(size) || size <= 0) {
231
+ throw new Error(`maxPageSizeInGetMetadata must be a positive integer, got ${size}`);
232
+ }
233
+ this.maxPageSizeInGetMetadata = size;
234
+ if (this.client != null)
235
+ this.client.setMaxPageSizeInGetMetadata(size);
236
+ }
237
+ getNameServerAddressList() {
238
+ return this.client != null ? this.client.getNameServerAddressList() : [];
239
+ }
240
+ // Only the "ADMIN" default instance name becomes pid-unique
241
+ // (instanceName = "ADMIN#<pid>"); a custom name is left alone.
242
+ _makeAdminInstancePidUnique() {
243
+ if (this.instanceName === 'ADMIN') {
244
+ this.instanceName = `ADMIN#${process.pid}`;
245
+ }
246
+ }
247
+ start() {
248
+ if (this._started)
249
+ return;
250
+ if (!this.namesrvAddr) {
251
+ throw new MQClientException('name server address is not set, call setNamesrvAddr() first');
252
+ }
253
+ // Unconditionally make the instance name pid-unique, then build the
254
+ // clientId as `<ip>@<instanceName>[@<unitName>][@STREAM]`.
255
+ this._makeAdminInstancePidUnique();
256
+ const clientId = MixAll.clientIdFor(this.instanceName, this.unitName, this.enableStreamRequestType);
257
+ const presetRC = this.client != null ? this.client.remotingClient : null;
258
+ const manager = MQClientManager.getInstance();
259
+ const { instance: client, created } = manager.getOrCreateMQClientInstance(clientId, () => new MQClient(clientId, this.namesrvAddr, presetRC));
260
+ this.client = client;
261
+ if (created) {
262
+ // Pass this facade's configured route-refresh period into the shared
263
+ // instance ONCE at registration; the instance-level route timer reads
264
+ // it from there instead of this facade field.
265
+ client.setPollNameServerIntervalMillis(this.pollNameServerIntervalMillis);
266
+ registerRpcHooks(this.client.remotingClient, {
267
+ namespaceV2: () => this.namespaceV2,
268
+ enableStreamRequestType: this.enableStreamRequestType,
269
+ userHook: this.rpcHook,
270
+ });
271
+ }
272
+ manager.registerClient(clientId, client);
273
+ this.client.start();
274
+ // Paging goes through the shared instance's page size; this facade's
275
+ // configured value wins for its own metadata reads.
276
+ this.client.setMaxPageSizeInGetMetadata(this.maxPageSizeInGetMetadata);
277
+ // A facade created with the VIP switch already on (before start()) must have
278
+ // it reflected on the instance it just obtained.
279
+ this.client.setVipChannelEnabled(this.vipChannelEnabled);
280
+ this._started = true;
281
+ logger.info(`adminExt ${this.adminExtGroup} started, clientId=${clientId}`);
282
+ }
283
+ shutdown() {
284
+ if (!this._started)
285
+ return;
286
+ this._started = false;
287
+ if (this.client != null) {
288
+ // Hand back to the manager: decrement the count; the real shutdown
289
+ // (stop timers, close connections) happens only when it hits zero.
290
+ MQClientManager.getInstance().unregisterClient(this.client.clientId, this.client);
291
+ }
292
+ this.client = null;
293
+ logger.info(`adminExt ${this.adminExtGroup} shutdown OK`);
294
+ }
295
+ _requireClient() {
296
+ if (!this._started || this.client == null) {
297
+ throw new MQClientException('admin not started, call start() first');
298
+ }
299
+ return this.client;
300
+ }
301
+ _timeout(millis) {
302
+ if (millis != null && millis > 0)
303
+ return millis;
304
+ return this.timeoutMillis > 0 ? this.timeoutMillis : DefaultAdminTimeoutMillis;
305
+ }
306
+ // ---------------- low-level invoke helpers ----------------
307
+ _vipChannel(addr) {
308
+ return MixAll.brokerVipChannel(this.vipChannelEnabled, addr);
309
+ }
310
+ _request(code, ext, body = null) {
311
+ const request = RemotingCommand.createRequestCommand(code, null);
312
+ if (ext != null) {
313
+ for (const [k, v] of Object.entries(ext))
314
+ request.addExtField(k, v);
315
+ }
316
+ if (body != null)
317
+ request.body = body;
318
+ return request;
319
+ }
320
+ // Sends to one broker (with VIP translation) and raises on non-SUCCESS.
321
+ async _invokeBroker(addr, code, ext, body = null, timeoutMillis) {
322
+ return this._invokeChecked(this._vipChannel(addr), code, ext, body, timeoutMillis);
323
+ }
324
+ // Sends to an explicitly named name-server address and raises on non-SUCCESS.
325
+ // The VIP port (listen port - 2) is a broker-only contract, so a name-server
326
+ // address is used as-is and NEVER passed through the VIP translation.
327
+ async _invokeNameServerAddr(addr, code, ext, body = null, timeoutMillis) {
328
+ return this._invokeChecked(addr, code, ext, body, timeoutMillis);
329
+ }
330
+ // Shared checked invoke: raise on any non-SUCCESS response. `target` is the
331
+ // already-resolved address to dial.
332
+ async _invokeChecked(target, code, ext, body = null, timeoutMillis) {
333
+ const client = this._requireClient();
334
+ const response = await client.remotingClient.invokeSync(target, this._request(code, ext, body), this._timeout(timeoutMillis));
335
+ if (response.code !== ResponseCode.SUCCESS) {
336
+ throw new MQBrokerException(response.code, response.remark || `CODE: ${response.code}`, target);
337
+ }
338
+ return response;
339
+ }
340
+ // Broadcasts to EVERY nameserver for KV-config PUT/DELETE and raises if any
341
+ // of them failed — a partial broadcast would leave one nameserver serving
342
+ // stale config.
343
+ async _invokeNameServerAll(code, ext, timeoutMillis) {
344
+ const client = this._requireClient();
345
+ const request = this._request(code, ext);
346
+ let errResponse = null;
347
+ for (const nsAddr of client.getNameServerAddressList()) {
348
+ const response = await client.remotingClient.invokeSync(nsAddr, request, this._timeout(timeoutMillis));
349
+ if (response.code !== ResponseCode.SUCCESS)
350
+ errResponse = response;
351
+ }
352
+ if (errResponse != null) {
353
+ throw new MQBrokerException(errResponse.code, errResponse.remark || 'put/delete kv config failed');
354
+ }
355
+ }
356
+ // Sends to the FIRST nameserver that answers; the response is returned
357
+ // unchecked so callers can branch.
358
+ async _invokeNameServerOne(code, ext, timeoutMillis) {
359
+ const client = this._requireClient();
360
+ const request = this._request(code, ext);
361
+ let lastErr = null;
362
+ for (const nsAddr of client.getNameServerAddressList()) {
363
+ try {
364
+ return await client.remotingClient.invokeSync(nsAddr, request, this._timeout(timeoutMillis));
365
+ }
366
+ catch (e) {
367
+ lastErr = e;
368
+ }
369
+ }
370
+ throw new MQClientException(`all name servers unreachable: ${lastErr != null ? lastErr.message : ''}`);
371
+ }
372
+ // Unchecked invoke at an explicitly named target with NO VIP translation —
373
+ // the controller-leader RPCs and other requests whose target is not a broker
374
+ // listener dial the address verbatim. Callers decide the error shape.
375
+ async _invokeRaw(target, code, ext, body = null, timeoutMillis) {
376
+ const client = this._requireClient();
377
+ return client.remotingClient.invokeSync(target, this._request(code, ext, body), this._timeout(timeoutMillis));
378
+ }
379
+ // Broker invoke with the VIP translation applied but WITHOUT the SUCCESS
380
+ // check — a few admin RPCs surface a client exception rather than a broker
381
+ // one, so the caller decides.
382
+ async _invokeBrokerRaw(addr, code, ext, body = null, timeoutMillis) {
383
+ return this._invokeRaw(this._vipChannel(addr), code, ext, body, timeoutMillis);
384
+ }
385
+ // Client-side exception carrying the failing response code, for the admin
386
+ // RPCs whose error mapping is a client exception rather than a broker one.
387
+ _clientError(code, remark) {
388
+ const e = new MQClientException(remark || `CODE: ${code}`);
389
+ e.responseCode = code;
390
+ return e;
391
+ }
392
+ // ---------------- *Concurrent admin tooling ----------------
393
+ // Error mapping of the concurrent wrappers: remoting failures report as
394
+ // such, a missing topic route is its own code, and everything else lands on
395
+ // the client / broker error codes.
396
+ _adminToolFailure(e) {
397
+ if (e instanceof RemotingConnectException || e instanceof RemotingSendRequestException
398
+ || e instanceof RemotingTimeoutException) {
399
+ return AdminToolResult.failure(AdminToolsResultCodeEnum.REMOTING_ERROR, e.message);
400
+ }
401
+ if (e instanceof MQClientException) {
402
+ const code = e.responseCode;
403
+ if (code === ResponseCode.TOPIC_NOT_EXIST) {
404
+ return AdminToolResult.failure(AdminToolsResultCodeEnum.TOPIC_ROUTE_INFO_NOT_EXIST, e.message);
405
+ }
406
+ return AdminToolResult.failure(AdminToolsResultCodeEnum.MQ_CLIENT_ERROR, e.message);
407
+ }
408
+ if (e instanceof InterruptedException) {
409
+ return AdminToolResult.failure(AdminToolsResultCodeEnum.INTERRUPT_ERROR, e.message);
410
+ }
411
+ return AdminToolResult.failure(AdminToolsResultCodeEnum.MQ_BROKER_ERROR, e?.message ?? String(e));
412
+ }
413
+ // Runs one concurrent-wrapper body: the value it returns is wrapped as a
414
+ // success result, a returned AdminToolResult passes through as-is (the leaf
415
+ // decided the failure itself), and a throw maps onto the error codes.
416
+ async _adminToolExecute(fn) {
417
+ try {
418
+ const data = await fn();
419
+ if (data instanceof AdminToolResult)
420
+ return data;
421
+ return AdminToolResult.success(data);
422
+ }
423
+ catch (e) {
424
+ logger.warn(`admin tool operation failed: ${e?.message}`);
425
+ return this._adminToolFailure(e);
426
+ }
427
+ }
428
+ // Fetch a topic's route from the nameserver.
429
+ // NOTE: NO default-topic fallback here — the admin must surface TOPIC_NOT_EXIST
430
+ // (the client's updateTopicRouteInfoFromNameServer falls back to TBW102, which
431
+ // would silently mask a missing topic).
432
+ async examineTopicRouteInfo(topic, timeoutMillis) {
433
+ const client = this._requireClient();
434
+ const timeout = this._timeout(timeoutMillis);
435
+ const ns = client.getNameServerAddressList()[0];
436
+ if (ns == null)
437
+ throw new MQClientException('No name server address, please set it first.');
438
+ const request = this._request(RequestCode.GET_ROUTEINFO_BY_TOPIC, adminExt({ topic }));
439
+ const response = await client.remotingClient.invokeSync(ns, request, timeout);
440
+ if (response.code !== ResponseCode.SUCCESS) {
441
+ const e = new MQClientException(`Not exist route info for this topic: ${topic}`);
442
+ e.responseCode = ResponseCode.TOPIC_NOT_EXIST;
443
+ throw e;
444
+ }
445
+ const routeData = TopicRouteData.decode(response.body);
446
+ // Cache into the client's route/broker-addr tables so subsequent
447
+ // publish/subscribe paths see the fresh route.
448
+ client.topicRouteData2TopicPublishInfo(topic, routeData);
449
+ return routeData;
450
+ }
451
+ // ---------------- Topic management ----------------
452
+ // createTopic pushes the topic to every MASTER in the
453
+ // createTopicKey (TBW102) route with READ|WRITE perm. Succeeding on at least
454
+ // one broker is enough.
455
+ async createTopic(key, newTopic, queueNum, topicSysFlag = 0, attributes = '') {
456
+ const client = this._requireClient();
457
+ let route = client.getTopicRouteData(key);
458
+ if (route == null) {
459
+ await client.updateTopicRouteInfoFromNameServer(key, true);
460
+ route = client.getTopicRouteData(key);
461
+ }
462
+ if (route == null) {
463
+ throw new MQClientException(`No route info of default topic ${key}`);
464
+ }
465
+ if (queueNum <= 0)
466
+ queueNum = MixAll.DEFAULT_TOPIC_QUEUE_NUMS;
467
+ const perm = MixAll.READ_PERM_BY_DEFAULT; // PermName.PERM_READ | PERM_WRITE
468
+ let created = false;
469
+ let lastErr = null;
470
+ for (const bd of route.brokerDatas) {
471
+ const addr = bd.selectBrokerAddr();
472
+ if (addr == null)
473
+ continue;
474
+ try {
475
+ await this._createTopicInBroker(addr, key, newTopic, queueNum, queueNum, perm, topicSysFlag, TopicFilterType.SINGLE_TAG, false, attributes);
476
+ created = true;
477
+ }
478
+ catch (e) {
479
+ lastErr = e;
480
+ }
481
+ }
482
+ if (!created && lastErr != null) {
483
+ throw new MQClientException(`create new topic failed: ${lastErr.message}`);
484
+ }
485
+ }
486
+ // UPDATE_AND_CREATE_TOPIC(17) to one broker. `topicFilterType` MUST be sent
487
+ // (broker rejects null); only transport errors are retried — a broker
488
+ // rejection is final (MQBrokerException from the retry loop is not retried).
489
+ async _createTopicInBroker(brokerAddr, defaultTopic, topic, readQueueNums, writeQueueNums, perm, topicSysFlag, topicFilterType, order, attributes) {
490
+ const retryTimes = 5; // per-broker retry count for createTopic
491
+ let lastErr = null;
492
+ for (let attempt = 0; attempt < retryTimes; attempt++) {
493
+ try {
494
+ await this._invokeBroker(brokerAddr, RequestCode.UPDATE_AND_CREATE_TOPIC, adminExt({
495
+ topic,
496
+ defaultTopic,
497
+ readQueueNums,
498
+ writeQueueNums,
499
+ perm,
500
+ topicFilterType,
501
+ topicSysFlag,
502
+ order,
503
+ // Attribute-map serialization: an empty map renders as "", not null.
504
+ attributes,
505
+ force: false,
506
+ }));
507
+ return;
508
+ }
509
+ catch (e) {
510
+ if (e instanceof MQBrokerException)
511
+ throw e; // broker rejection is final
512
+ lastErr = e;
513
+ }
514
+ }
515
+ if (lastErr != null)
516
+ throw lastErr;
517
+ }
518
+ // createAndUpdateTopicConfig sends UPDATE_AND_CREATE_TOPIC(17) to one addr.
519
+ async createAndUpdateTopicConfig(addr, config) {
520
+ // TopicConfig's field name is `topic` (topicName is only the JSON key of
521
+ // toDict) — reading the wrong field leaves topic=undefined in the header,
522
+ // and the broker answers "specified topic is blank" (caught on the
523
+ // real-machine static-topic leg).
524
+ await this._createTopicInBroker(addr, this.createTopicKey, config.topic, config.readQueueNums, config.writeQueueNums, config.perm, config.topicSysFlag, config.topicFilterType || TopicFilterType.SINGLE_TAG, config.order === true, config.attributes != null ? JSON.stringify(config.attributes) : '');
525
+ }
526
+ // UPDATE_AND_CREATE_TOPIC_LIST(18): the list header has no fields of its
527
+ // own, so the request carries no ext field keys; the topic configs ride the
528
+ // body as {"topicConfigList":[…]}. No per-config validation happens on the
529
+ // client side — unlike the single-config form, whatever the caller passes
530
+ // goes out verbatim.
531
+ async createAndUpdateTopicConfigList(brokerAddr, topicConfigList, timeoutMillis) {
532
+ const body = RemotingSerializable.encode({
533
+ topicConfigList: topicConfigList.map((cfg) => cfg.toDict()),
534
+ });
535
+ const response = await this._invokeBrokerRaw(brokerAddr, RequestCode.UPDATE_AND_CREATE_TOPIC_LIST, {}, body, timeoutMillis);
536
+ if (response.code !== ResponseCode.SUCCESS)
537
+ throw this._clientError(response.code, response.remark);
538
+ }
539
+ async createStaticTopic(brokerAddr, defaultTopic, config, mappingDetail, force = false) {
540
+ if (mappingDetail == null) {
541
+ throw new MQClientException('createStaticTopic: mappingDetail must not be null');
542
+ }
543
+ await this._invokeBroker(brokerAddr, RequestCode.UPDATE_AND_CREATE_STATIC_TOPIC, adminExt({
544
+ topic: config.topic,
545
+ defaultTopic,
546
+ readQueueNums: config.readQueueNums,
547
+ writeQueueNums: config.writeQueueNums,
548
+ perm: config.perm,
549
+ topicFilterType: config.topicFilterType || TopicFilterType.SINGLE_TAG,
550
+ topicSysFlag: config.topicSysFlag,
551
+ order: config.order === true,
552
+ force,
553
+ }), mappingDetail.encode());
554
+ }
555
+ // DELETE_TOPIC_IN_BROKER(215) leaf for one address.
556
+ async _deleteTopicInBrokerAt(brokerAddr, topic) {
557
+ await this._invokeBroker(brokerAddr, RequestCode.DELETE_TOPIC_IN_BROKER, adminExt({ topic }));
558
+ }
559
+ // DELETE_TOPIC_IN_BROKER(215) for each addr.
560
+ async deleteTopicInBroker(addrs, topic) {
561
+ for (const addr of addrs) {
562
+ await this._deleteTopicInBrokerAt(addr, topic);
563
+ }
564
+ }
565
+ // Fan-out flavor of deleteTopicInBroker over the caller's address set: every
566
+ // address is asked at once, each outcome is collected into the success /
567
+ // failure lists, and the sweep itself never throws.
568
+ async deleteTopicInBrokerConcurrent(addrs, topic) {
569
+ const targetAddrs = Array.isArray(addrs) ? addrs : Array.from(addrs);
570
+ return this._adminToolExecute(async () => {
571
+ const result = new BrokerOperatorResult();
572
+ const settled = await Promise.allSettled(targetAddrs.map(async (addr) => {
573
+ await this._deleteTopicInBrokerAt(addr, topic);
574
+ return addr;
575
+ }));
576
+ for (let i = 0; i < settled.length; i++) {
577
+ const s = settled[i];
578
+ if (s.status === 'fulfilled') {
579
+ result.successList.push(targetAddrs[i]);
580
+ }
581
+ else {
582
+ logger.warn(`deleteTopicInBroker error. topic=${topic}, broker=${targetAddrs[i]}: `
583
+ + `${s.reason?.message}`);
584
+ result.failureList.push(targetAddrs[i]);
585
+ }
586
+ }
587
+ return result;
588
+ });
589
+ }
590
+ // DELETE_TOPIC_IN_NAMESRV(216) for each addr; addrs == null → all nameservers.
591
+ async deleteTopicInNameServer(addrs, topic) {
592
+ const client = this._requireClient();
593
+ const targets = addrs != null ? addrs : client.getNameServerAddressList();
594
+ for (const nsAddr of targets) {
595
+ await this._invokeNameServerAddr(nsAddr, RequestCode.DELETE_TOPIC_IN_NAMESRV, adminExt({ topic }), null, 5000);
596
+ }
597
+ }
598
+ // deleteTopic removes from ALL brokers (master AND slave) of the cluster,
599
+ // then the nameserver route, then the KV namespaces (ORDER_TOPIC_CONFIG).
600
+ async deleteTopic(topic, clusterName) {
601
+ const client = this._requireClient();
602
+ const clusterInfo = await this.examineBrokerClusterInfo();
603
+ const names = clusterInfo.clusterAddrTable[clusterName];
604
+ if (names == null || names.length === 0) {
605
+ throw new MQClientException(`The cluster [${clusterName}] not exist`);
606
+ }
607
+ // Collect master AND slave addrs of the cluster.
608
+ const addrs = [];
609
+ for (const brokerName of names) {
610
+ const inner = clusterInfo.brokerAddrTable[brokerName];
611
+ if (inner != null)
612
+ addrs.push(...Object.values(inner));
613
+ }
614
+ await this.deleteTopicInBroker(addrs, topic);
615
+ await this.deleteTopicInNameServer(null, topic);
616
+ for (const namespace of this.kvNamespaceToDeleteList) {
617
+ await this.deleteKvConfig(namespace, topic);
618
+ }
619
+ void client;
620
+ }
621
+ // Fetch every topic from the nameserver: GET_ALL_TOPIC_LIST_FROM_NAMESERVER.
622
+ async fetchAllTopicList(timeoutMillis) {
623
+ const response = await this._invokeNameServerOne(RequestCode.GET_ALL_TOPIC_LIST_FROM_NAMESERVER, null, timeoutMillis);
624
+ if (response.body == null || response.body.length === 0) {
625
+ const t = new TopicList();
626
+ return t;
627
+ }
628
+ return TopicList.decode(response.body);
629
+ }
630
+ // GET_TOPICS_BY_CLUSTER(224). The ext key MUST be `cluster`; with
631
+ // `clusterName` the nameserver NPEs internally, swallows it, and answers
632
+ // SUCCESS with an empty list.
633
+ async fetchTopicsByCluster(clusterName, timeoutMillis) {
634
+ const response = await this._invokeNameServerOne(RequestCode.GET_TOPICS_BY_CLUSTER, adminExt({ cluster: clusterName }), timeoutMillis);
635
+ const topics = new Set();
636
+ if (response.code === ResponseCode.SUCCESS && response.body != null) {
637
+ const obj = RemotingSerializable.decode(response.body);
638
+ for (const item of (obj['topicList'] || [])) {
639
+ if (typeof item === 'string')
640
+ topics.add(item);
641
+ }
642
+ }
643
+ return topics;
644
+ }
645
+ // examineTopicStats(topic) merges GET_TOPIC_STATS_INFO(202) across the
646
+ // topic's brokers. The synchronous path does NOT sum topicPutTps (only the
647
+ // *Concurrent variant does).
648
+ async examineTopicStats(topic) {
649
+ const route = await this.examineTopicRouteInfo(topic);
650
+ const merged = new TopicStatsTable();
651
+ for (const bd of route.brokerDatas) {
652
+ const addr = bd.selectBrokerAddr();
653
+ if (addr == null)
654
+ continue;
655
+ try {
656
+ const part = await this.examineTopicStatsByBroker(addr, topic);
657
+ for (const [q, off] of part.offsetTable)
658
+ merged.offsetTable.set(q, off);
659
+ }
660
+ catch (e) {
661
+ logger.warn(`getTopicStatsInfo error. topic=${topic} broker=${addr}: ${e.message}`);
662
+ }
663
+ }
664
+ if (merged.offsetTable.size === 0) {
665
+ throw new MQClientException('Not found the topic stats info');
666
+ }
667
+ return merged;
668
+ }
669
+ async examineTopicStatsByBroker(brokerAddr, topic) {
670
+ const response = await this._invokeBroker(brokerAddr, RequestCode.GET_TOPIC_STATS_INFO, adminExt({ topic }));
671
+ return TopicStatsTable.decode(response.body);
672
+ }
673
+ // Fan-out flavor of examineTopicStats: every master of the route is asked at
674
+ // once, offset tables are merged and topicPutTps is SUMMED. A broker that
675
+ // fails is logged and skipped — the sweep never fails because one broker
676
+ // did. A topic without a route is reported as a failure result, not a throw.
677
+ async examineTopicStatsConcurrent(topic) {
678
+ return this._adminToolExecute(async () => {
679
+ const merged = new TopicStatsTable();
680
+ const route = await this.examineTopicRouteInfo(topic);
681
+ if (route == null || route.brokerDatas.length === 0)
682
+ return merged;
683
+ const settled = await Promise.allSettled(route.brokerDatas.map(async (bd) => {
684
+ const addr = bd.selectBrokerAddr();
685
+ if (addr == null)
686
+ return null;
687
+ return this.examineTopicStatsByBroker(addr, topic);
688
+ }));
689
+ for (const s of settled) {
690
+ if (s.status === 'rejected') {
691
+ logger.warn(`getTopicStatsInfo error. topic=${topic}: ${s.reason?.message}`);
692
+ continue;
693
+ }
694
+ if (s.value == null)
695
+ continue;
696
+ for (const [q, off] of s.value.offsetTable)
697
+ merged.offsetTable.set(q, off);
698
+ merged.topicPutTps += s.value.topicPutTps;
699
+ }
700
+ return merged;
701
+ });
702
+ }
703
+ // QUERY_CONSUME_TIME_SPAN(303) leaf for one address.
704
+ async _queryConsumeTimeSpanAt(brokerAddr, topic, group) {
705
+ const response = await this._invokeBroker(brokerAddr, RequestCode.QUERY_CONSUME_TIME_SPAN, adminExt({ topic, group }), null);
706
+ const body = response.body;
707
+ if (body == null || body.length === 0)
708
+ return [];
709
+ return QueryConsumeTimeSpanBody.decode(body).consumeTimeSpanSet;
710
+ }
711
+ // QUERY_CONSUME_TIME_SPAN(303): walk the route's masters and concatenate each
712
+ // broker's consumeTimeSpanSet. A broker that answers with an empty body
713
+ // contributes nothing; a broker that ERRORS propagates -- unlike the topic-stats
714
+ // sweep above, which is best-effort, this one never swallows a per-broker error.
715
+ async queryConsumeTimeSpan(topic, group, timeoutMillis) {
716
+ const route = await this.examineTopicRouteInfo(topic);
717
+ const spans = [];
718
+ for (const bd of route.brokerDatas) {
719
+ const addr = bd.selectBrokerAddr();
720
+ if (addr == null)
721
+ continue;
722
+ const response = await this._invokeBroker(addr, RequestCode.QUERY_CONSUME_TIME_SPAN, adminExt({ topic, group }), null, timeoutMillis);
723
+ const body = response.body;
724
+ if (body == null || body.length === 0)
725
+ continue;
726
+ spans.push(...QueryConsumeTimeSpanBody.decode(body).consumeTimeSpanSet);
727
+ }
728
+ return spans;
729
+ }
730
+ // Fan-out flavor of queryConsumeTimeSpan: the masters are asked at once and
731
+ // the consumeTimeSpanSet entries are concatenated. Task failures are logged;
732
+ // a missing route answers with an EMPTY span list as a success result (this
733
+ // one sweep, unlike its topic-list sibling, does not treat a missing route
734
+ // as a failure).
735
+ async queryConsumeTimeSpanConcurrent(topic, group) {
736
+ return this._adminToolExecute(async () => {
737
+ const spans = [];
738
+ const route = await this.examineTopicRouteInfo(topic);
739
+ if (route == null || route.brokerDatas.length === 0)
740
+ return spans;
741
+ const settled = await Promise.allSettled(route.brokerDatas.map(async (bd) => {
742
+ const addr = bd.selectBrokerAddr();
743
+ if (addr == null)
744
+ return null;
745
+ return this._queryConsumeTimeSpanAt(addr, topic, group);
746
+ }));
747
+ for (const s of settled) {
748
+ if (s.status === 'rejected') {
749
+ logger.warn(`queryConsumeTimeSpan error. topic=${topic}, group=${group}: `
750
+ + `${s.reason?.message}`);
751
+ continue;
752
+ }
753
+ if (s.value != null)
754
+ spans.push(...s.value);
755
+ }
756
+ return spans;
757
+ });
758
+ }
759
+ // ---------------- Cluster / broker ----------------
760
+ // GET_BROKER_CLUSTER_INFO(106), first nameserver that answers.
761
+ async examineBrokerClusterInfo(timeoutMillis) {
762
+ const response = await this._invokeNameServerOne(RequestCode.GET_BROKER_CLUSTER_INFO, null, timeoutMillis);
763
+ return ClusterInfo.decode(response.body);
764
+ }
765
+ // GET_BROKER_RUNTIME_INFO(28), KVTable body.
766
+ async fetchBrokerRuntimeStats(brokerAddr, timeoutMillis) {
767
+ const response = await this._invokeBroker(brokerAddr, RequestCode.GET_BROKER_RUNTIME_INFO, null, null, timeoutMillis);
768
+ return KVTable.decode(response.body);
769
+ }
770
+ // GET_BROKER_CONFIG(26). The body is Properties-format TEXT on the broker
771
+ // side, NOT JSON — a client that parses it as a KVTable fails on every real
772
+ // broker.
773
+ //
774
+ // This read is the one broker RPC that always uses the listening port: the fast channel
775
+ // serves the other admin RPCs, not this one, so the address goes out untranslated even
776
+ // when the VIP switch is on.
777
+ async getBrokerConfig(brokerAddr, timeoutMillis) {
778
+ const response = await this._invokeChecked(brokerAddr, RequestCode.GET_BROKER_CONFIG, null, null, timeoutMillis);
779
+ return string2Properties(response.body.toString('utf8'));
780
+ }
781
+ // UPDATE_BROKER_CONFIG(25), body = `k=v\n` text.
782
+ async updateBrokerConfig(brokerAddr, properties, timeoutMillis) {
783
+ const text = propertiesToString(properties);
784
+ if (!text)
785
+ return;
786
+ await this._invokeBroker(brokerAddr, RequestCode.UPDATE_BROKER_CONFIG, null, Buffer.from(text, 'utf8'), timeoutMillis);
787
+ }
788
+ // WIPE_WRITE_PERM_OF_BROKER(205) — returns the number of topics affected.
789
+ async wipeWritePermOfBroker(namesrvAddr, brokerName) {
790
+ const response = await this._invokeNameServerAddr(namesrvAddr, RequestCode.WIPE_WRITE_PERM_OF_BROKER, adminExt({ brokerName }));
791
+ const v = response.extFields ? response.extFields['wipeTopicCount'] : null;
792
+ return v != null ? parseInt(v, 10) : 0;
793
+ }
794
+ // ADD_WRITE_PERM_OF_BROKER(327) — returns the number of topics affected.
795
+ async addWritePermOfBroker(namesrvAddr, brokerName) {
796
+ const response = await this._invokeNameServerAddr(namesrvAddr, RequestCode.ADD_WRITE_PERM_OF_BROKER, adminExt({ brokerName }));
797
+ const v = response.extFields ? response.extFields['addTopicCount'] : null;
798
+ return v != null ? parseInt(v, 10) : 0;
799
+ }
800
+ // CLEAN_UNUSED_TOPIC(316) on one broker.
801
+ async cleanUnusedTopicByAddr(addr) {
802
+ await this._invokeBroker(addr, RequestCode.CLEAN_UNUSED_TOPIC, null);
803
+ return true;
804
+ }
805
+ // CLEAN_UNUSED_TOPIC(316) across every broker of the cluster; the last
806
+ // result wins.
807
+ async cleanUnusedTopic(cluster) {
808
+ const clusterInfo = await this.examineBrokerClusterInfo();
809
+ const clusters = cluster ? [cluster] : Object.keys(clusterInfo.clusterAddrTable);
810
+ let result = false;
811
+ for (const c of clusters) {
812
+ for (const addr of clusterInfo.clusterAddrTable[c] || []) {
813
+ const inner = clusterInfo.brokerAddrTable[addr];
814
+ const addrs = inner != null ? Object.values(inner) : [];
815
+ for (const a of addrs)
816
+ result = await this.cleanUnusedTopicByAddr(a);
817
+ }
818
+ }
819
+ return result;
820
+ }
821
+ // CLEAN_EXPIRED_CONSUMEQUEUE(306).
822
+ async cleanExpiredConsumerQueueByAddr(addr) {
823
+ await this._invokeBroker(addr, RequestCode.CLEAN_EXPIRED_CONSUMEQUEUE, null);
824
+ return true;
825
+ }
826
+ // DELETE_EXPIRED_COMMITLOG(329).
827
+ async deleteExpiredCommitLogByAddr(addr) {
828
+ await this._invokeBroker(addr, RequestCode.DELETE_EXPIRED_COMMITLOG, null);
829
+ return true;
830
+ }
831
+ // VIEW_BROKER_STATS_DATA(315) — raw JSON object.
832
+ async viewBrokerStatsData(brokerAddr, statsName, statsKey) {
833
+ const response = await this._invokeBroker(brokerAddr, RequestCode.VIEW_BROKER_STATS_DATA, adminExt({ statsName, statsKey }));
834
+ return RemotingSerializable.decode(response.body);
835
+ }
836
+ // getClusterList(topic): the clusters whose brokers appear in the topic's
837
+ // route.
838
+ async getClusterList(topic) {
839
+ const clusterInfo = await this.examineBrokerClusterInfo();
840
+ const route = await this.examineTopicRouteInfo(topic);
841
+ const brokerNames = new Set(route.brokerDatas.map((bd) => bd.brokerName));
842
+ const clusters = new Set();
843
+ for (const [clusterName, names] of Object.entries(clusterInfo.clusterAddrTable)) {
844
+ if (names.some((n) => brokerNames.has(n)))
845
+ clusters.add(clusterName);
846
+ }
847
+ return clusters;
848
+ }
849
+ // getTopicClusterList(topic): uses the FIRST BrokerData's brokerName only —
850
+ // the nameserver's cluster-per-topic convention keeps that shape.
851
+ async getTopicClusterList(topic) {
852
+ const clusterInfo = await this.examineBrokerClusterInfo();
853
+ const route = await this.examineTopicRouteInfo(topic);
854
+ const clusterSet = new Set();
855
+ if (route.brokerDatas.length === 0)
856
+ return clusterSet;
857
+ const brokerName = route.brokerDatas[0].brokerName;
858
+ for (const [clusterName, names] of Object.entries(clusterInfo.clusterAddrTable)) {
859
+ if (names.includes(brokerName))
860
+ clusterSet.add(clusterName);
861
+ }
862
+ return clusterSet;
863
+ }
864
+ // ---------------- Topic config (broker side) ----------------
865
+ // GET_ALL_TOPIC_CONFIG(21) on one broker, PAGED: topicSeq / maxTopicNum /
866
+ // dataVersion, accumulating until topicSeq >= totalTopicNum-1. A broker that
867
+ // does not send `totalTopicNum` (older versions) returns everything in one
868
+ // round. The FIRST page sends dataVersion as the EMPTY STRING (the key is
869
+ // present, not omitted); later pages send the JSON of the last seen version.
870
+ async getAllTopicConfig(brokerAddr, timeoutMillis) {
871
+ const client = this._requireClient();
872
+ const timeout = this._timeout(timeoutMillis);
873
+ // The VIP port is a broker-only contract; every page of this one request is
874
+ // dialled at the resolved target, never at the cached route address.
875
+ const target = this._vipChannel(brokerAddr);
876
+ let currentDataVersion = null;
877
+ let topicSeq = 0;
878
+ const table = {};
879
+ const begin = Date.now();
880
+ for (;;) {
881
+ const left = timeout - (Date.now() - begin);
882
+ if (left < 0)
883
+ throw new MQClientException('invokeSync call timeout');
884
+ const ext = adminExt({
885
+ topicSeq,
886
+ maxTopicNum: client.maxPageSizeInGetMetadata,
887
+ dataVersion: currentDataVersion != null ? JSON.stringify(currentDataVersion) : '',
888
+ });
889
+ const response = await client.remotingClient.invokeSync(target, this._request(RequestCode.GET_ALL_TOPIC_CONFIG, ext), left);
890
+ if (response.code !== ResponseCode.SUCCESS) {
891
+ throw new MQBrokerException(response.code, response.remark || '', target);
892
+ }
893
+ const wrapper = TopicConfigSerializeWrapper.decode(response.body);
894
+ const page = wrapper.topicConfigTable || {};
895
+ for (const [k, v] of Object.entries(page))
896
+ table[k] = v;
897
+ const newVersion = (wrapper.dataVersion || null);
898
+ if (currentDataVersion == null) {
899
+ // Fill the version before the break so an old broker (no totalTopicNum)
900
+ // still returns the version it sent.
901
+ currentDataVersion = newVersion;
902
+ }
903
+ const totalText = response.extFields ? response.extFields['totalTopicNum'] : null;
904
+ if (totalText == null)
905
+ break; // older broker: one round returns everything
906
+ const total = parseInt(totalText, 10);
907
+ if (Number.isNaN(total))
908
+ break;
909
+ if (!sameDataVersion(currentDataVersion, newVersion)) {
910
+ logger.warn('topic config dataVersion changed, restart paging');
911
+ currentDataVersion = newVersion;
912
+ topicSeq = 0;
913
+ for (const k of Object.keys(table))
914
+ delete table[k];
915
+ continue;
916
+ }
917
+ topicSeq += Object.keys(page).length;
918
+ if (topicSeq >= total - 1)
919
+ break;
920
+ }
921
+ const out = new TopicConfigSerializeWrapper();
922
+ out.topicConfigTable = table;
923
+ out.dataVersion = currentDataVersion != null ? currentDataVersion : {};
924
+ return out;
925
+ }
926
+ // GET_SYSTEM_TOPIC_LIST_FROM_BROKER(305).
927
+ async getSystemTopicListFromBroker(brokerAddr, timeoutMillis) {
928
+ const response = await this._invokeBroker(brokerAddr, RequestCode.GET_SYSTEM_TOPIC_LIST_FROM_BROKER, null, null, timeoutMillis);
929
+ return TopicList.decode(response.body);
930
+ }
931
+ // getUserTopicConfig drops system topics and (unless specialTopic) the
932
+ // %RETRY% / %DLQ% topics, and topics with an invalid perm (perm >= 0 &&
933
+ // perm < PERM_PRIORITY=8).
934
+ async getUserTopicConfig(brokerAddr, specialTopic, timeoutMillis) {
935
+ const wrapper = await this.getAllTopicConfig(brokerAddr, timeoutMillis);
936
+ const sysList = await this.getSystemTopicListFromBroker(brokerAddr, timeoutMillis);
937
+ const sysTopics = new Set(sysList.topicList);
938
+ const kept = {};
939
+ for (const [name, cfg] of Object.entries(wrapper.topicConfigTable)) {
940
+ if (sysTopics.has(name) || MixAll.isSysTopic(name))
941
+ continue;
942
+ if (!specialTopic && (MixAll.isRetryTopic(name) || MixAll.isDlqTopic(name)))
943
+ continue;
944
+ if (!(cfg.perm >= 0 && cfg.perm < 8))
945
+ continue; // PermName.isValid
946
+ kept[name] = cfg;
947
+ }
948
+ wrapper.topicConfigTable = kept;
949
+ return wrapper;
950
+ }
951
+ // GET_TOPIC_CONFIG(351) — sends `topic` + `lo`, body is a plain TopicConfig
952
+ // JSON.
953
+ async examineTopicConfig(addr, topic) {
954
+ const response = await this._invokeBroker(addr, RequestCode.GET_TOPIC_CONFIG, adminExt({ topic, lo: true }));
955
+ if (response.body == null || response.body.length === 0) {
956
+ throw new MQBrokerException(ResponseCode.SYSTEM_ERROR, `empty topic config for ${topic}`, addr);
957
+ }
958
+ return TopicConfig.decode(response.body);
959
+ }
960
+ // ---------------- Nameserver KV config ----------------
961
+ // PUT_KV_CONFIG(100), BROADCAST to every nameserver so none serves stale
962
+ // config.
963
+ async createAndUpdateKvConfig(namespace, key, value) {
964
+ await this._invokeNameServerAll(RequestCode.PUT_KV_CONFIG, adminExt({ namespace, key, value }));
965
+ }
966
+ async putKVConfig(namespace, key, value) {
967
+ await this.createAndUpdateKvConfig(namespace, key, value);
968
+ }
969
+ // GET_KV_CONFIG(101) — ONE nameserver.
970
+ async getKVConfig(namespace, key) {
971
+ const response = await this._invokeNameServerOne(RequestCode.GET_KV_CONFIG, adminExt({ namespace, key }));
972
+ if (response.code === ResponseCode.SUCCESS && response.extFields) {
973
+ return response.extFields['value'] != null ? response.extFields['value'] : null;
974
+ }
975
+ return null;
976
+ }
977
+ // DELETE_KV_CONFIG(102), BROADCAST to every nameserver.
978
+ async deleteKvConfig(namespace, key) {
979
+ await this._invokeNameServerAll(RequestCode.DELETE_KV_CONFIG, adminExt({ namespace, key }));
980
+ }
981
+ // GET_KVLIST_BY_NAMESPACE(219).
982
+ async getKVListByNamespace(namespace) {
983
+ const response = await this._invokeNameServerOne(RequestCode.GET_KVLIST_BY_NAMESPACE, adminExt({ namespace }));
984
+ return KVTable.decode(response.body);
985
+ }
986
+ // ---------------- Subscription groups ----------------
987
+ // UPDATE_AND_CREATE_SUBSCRIPTIONGROUP(200); the body is a
988
+ // SubscriptionGroupConfig JSON object (plain RemotingSerializable JSON).
989
+ async createAndUpdateSubscriptionGroupConfig(addr, config) {
990
+ await this._invokeBroker(addr, RequestCode.UPDATE_AND_CREATE_SUBSCRIPTIONGROUP, null, RemotingSerializable.encode(config));
991
+ }
992
+ // UPDATE_AND_CREATE_SUBSCRIPTIONGROUP_LIST(225): header null, the group
993
+ // configs ride the body under the groupConfigList key.
994
+ async createAndUpdateSubscriptionGroupConfigList(brokerAddr, configs, timeoutMillis) {
995
+ const body = RemotingSerializable.encode({ groupConfigList: configs });
996
+ const response = await this._invokeBrokerRaw(brokerAddr, RequestCode.UPDATE_AND_CREATE_SUBSCRIPTIONGROUP_LIST, null, body, timeoutMillis);
997
+ if (response.code !== ResponseCode.SUCCESS)
998
+ throw this._clientError(response.code, response.remark);
999
+ }
1000
+ // GET_ALL_SUBSCRIPTIONGROUP_CONFIG(201), PAGED: accumulate until
1001
+ // groupSeq >= totalGroupNum-1. A broker that does not send `totalGroupNum`
1002
+ // (older versions) returns everything in one round.
1003
+ async getAllSubscriptionGroup(brokerAddr, timeoutMillis) {
1004
+ const client = this._requireClient();
1005
+ const timeout = this._timeout(timeoutMillis);
1006
+ // The VIP port is a broker-only contract; every page of this one request is
1007
+ // dialled at the resolved target, never at the cached route address.
1008
+ const target = this._vipChannel(brokerAddr);
1009
+ let currentDataVersion = null;
1010
+ let groupSeq = 0;
1011
+ const table = {};
1012
+ const forbidden = {};
1013
+ const begin = Date.now();
1014
+ for (;;) {
1015
+ const left = timeout - (Date.now() - begin);
1016
+ if (left < 0)
1017
+ throw new MQClientException('invokeSync call timeout');
1018
+ // dataVersion MUST be present on the first page as the EMPTY STRING
1019
+ // (not omitted), and carries the JSON of the last seen version afterwards.
1020
+ const ext = adminExt({
1021
+ groupSeq,
1022
+ maxGroupNum: client.maxPageSizeInGetMetadata,
1023
+ dataVersion: currentDataVersion != null ? JSON.stringify(currentDataVersion) : '',
1024
+ });
1025
+ const response = await client.remotingClient.invokeSync(target, this._request(RequestCode.GET_ALL_SUBSCRIPTIONGROUP_CONFIG, ext), left);
1026
+ if (response.code !== ResponseCode.SUCCESS) {
1027
+ throw new MQBrokerException(response.code, response.remark || '', target);
1028
+ }
1029
+ const wrapper = RemotingSerializable.decode(response.body);
1030
+ const subTable = (wrapper['subscriptionGroupTable'] || {});
1031
+ for (const [k, v] of Object.entries(subTable))
1032
+ table[k] = v;
1033
+ for (const [k, v] of Object.entries((wrapper['forbiddenTable'] || {}))) {
1034
+ forbidden[k] = v;
1035
+ }
1036
+ const newVersion = (wrapper['dataVersion'] || null);
1037
+ if (currentDataVersion == null)
1038
+ currentDataVersion = newVersion;
1039
+ groupSeq += Object.keys(subTable).length;
1040
+ const totalText = response.extFields ? response.extFields['totalGroupNum'] : null;
1041
+ if (totalText == null)
1042
+ break; // older broker: one round returns everything
1043
+ const total = parseInt(totalText, 10);
1044
+ if (Number.isNaN(total))
1045
+ break;
1046
+ if (!sameDataVersion(currentDataVersion, newVersion)) {
1047
+ logger.warn('subscription group dataVersion changed, restart paging');
1048
+ currentDataVersion = newVersion;
1049
+ groupSeq = 0;
1050
+ for (const k of Object.keys(table))
1051
+ delete table[k];
1052
+ for (const k of Object.keys(forbidden))
1053
+ delete forbidden[k];
1054
+ continue;
1055
+ }
1056
+ if (groupSeq >= total - 1)
1057
+ break;
1058
+ }
1059
+ return {
1060
+ dataVersion: currentDataVersion != null ? currentDataVersion : {},
1061
+ forbiddenTable: forbidden,
1062
+ subscriptionGroupTable: table,
1063
+ };
1064
+ }
1065
+ // Drops system (CID_RMQ_SYS_) and predefined groups.
1066
+ async getUserSubscriptionGroup(brokerAddr, timeoutMillis) {
1067
+ const wrapper = await this.getAllSubscriptionGroup(brokerAddr, timeoutMillis);
1068
+ const kept = {};
1069
+ for (const [k, v] of Object.entries(wrapper.subscriptionGroupTable)) {
1070
+ if (MixAll.isSysConsumerGroup(k) || MixAll.isPredefinedGroup(k))
1071
+ continue;
1072
+ kept[k] = v;
1073
+ }
1074
+ wrapper.subscriptionGroupTable = kept;
1075
+ return wrapper;
1076
+ }
1077
+ // Read one group out of the full subscription-group wrapper (NOT
1078
+ // GET_SUBSCRIPTIONGROUP_CONFIG(352) — that call is broker-version dependent).
1079
+ async examineSubscriptionGroupConfig(addr, group) {
1080
+ const wrapper = await this.getAllSubscriptionGroup(addr);
1081
+ return wrapper.subscriptionGroupTable[group] != null ? wrapper.subscriptionGroupTable[group] : null;
1082
+ }
1083
+ // DELETE_SUBSCRIPTIONGROUP(207).
1084
+ async deleteSubscriptionGroup(addr, groupName, removeOffset = false) {
1085
+ await this._invokeBroker(addr, RequestCode.DELETE_SUBSCRIPTIONGROUP, adminExt({ groupName, cleanOffset: removeOffset }));
1086
+ }
1087
+ // ---------------- Consumer / producer connections ----------------
1088
+ // GET_CONSUMER_CONNECTION_LIST(203). brokerAddr == null → a RANDOM broker of
1089
+ // the %RETRY%<group> route. Empty connectionSet → CONSUMER_NOT_ONLINE.
1090
+ async examineConsumerConnectionInfo(consumerGroup, brokerAddr = null) {
1091
+ let addr = brokerAddr;
1092
+ if (addr == null) {
1093
+ const route = await this.examineTopicRouteInfo(MixAll.getRetryTopic(consumerGroup));
1094
+ const brokers = route.brokerDatas;
1095
+ if (brokers.length === 0) {
1096
+ const e = new MQClientException('Not found the consumer group connection');
1097
+ e.responseCode = ResponseCode.CONSUMER_NOT_ONLINE;
1098
+ throw e;
1099
+ }
1100
+ addr = brokers[Math.floor(Math.random() * brokers.length)].selectBrokerAddr();
1101
+ if (addr == null) {
1102
+ const e = new MQClientException('Not found the consumer group connection');
1103
+ e.responseCode = ResponseCode.CONSUMER_NOT_ONLINE;
1104
+ throw e;
1105
+ }
1106
+ }
1107
+ const response = await this._invokeBroker(addr, RequestCode.GET_CONSUMER_CONNECTION_LIST, adminExt({ consumerGroup }));
1108
+ const result = ConsumerConnection.decode(response.body);
1109
+ if (result.connectionSet.length === 0) {
1110
+ logger.warn(`the consumer group not online. brokerAddr=${addr}, group=${consumerGroup}`);
1111
+ const e = new MQClientException('Not found the consumer group connection');
1112
+ e.responseCode = ResponseCode.CONSUMER_NOT_ONLINE;
1113
+ throw e;
1114
+ }
1115
+ return result;
1116
+ }
1117
+ // GET_PRODUCER_CONNECTION_LIST(204) — random broker of the TOPIC route.
1118
+ async examineProducerConnectionInfo(producerGroup, topic) {
1119
+ const route = await this.examineTopicRouteInfo(topic);
1120
+ const brokers = route.brokerDatas;
1121
+ if (brokers.length === 0) {
1122
+ throw new MQClientException('Not found the producer group connection');
1123
+ }
1124
+ const addr = brokers[Math.floor(Math.random() * brokers.length)].selectBrokerAddr();
1125
+ if (addr == null)
1126
+ throw new MQClientException('Not found the producer group connection');
1127
+ const response = await this._invokeBroker(addr, RequestCode.GET_PRODUCER_CONNECTION_LIST, adminExt({ producerGroup }));
1128
+ const result = ProducerConnection.decode(response.body);
1129
+ if (result.connectionSet.length === 0) {
1130
+ logger.warn(`the producer group not online. brokerAddr=${addr}, group=${producerGroup}`);
1131
+ throw new MQClientException('Not found the producer group connection');
1132
+ }
1133
+ return result;
1134
+ }
1135
+ // GET_CONSUMER_RUNNING_INFO(307) — walks the %RETRY%<group> route IN ORDER
1136
+ // and asks the first broker with a master addr.
1137
+ async getConsumerRunningInfo(consumerGroup, clientId, jstack = false, metrics = false) {
1138
+ void metrics;
1139
+ const topic = MixAll.getRetryTopic(consumerGroup);
1140
+ const route = await this.examineTopicRouteInfo(topic);
1141
+ for (const bd of route.brokerDatas) {
1142
+ const addr = bd.selectBrokerAddr();
1143
+ if (addr == null)
1144
+ continue;
1145
+ const response = await this._invokeBroker(addr, RequestCode.GET_CONSUMER_RUNNING_INFO, adminExt({ consumerGroup, clientId, jstackEnable: jstack }));
1146
+ if (response.body == null || response.body.length === 0)
1147
+ return null;
1148
+ const { ConsumerRunningInfo } = await import("../remoting/bodies.js");
1149
+ return ConsumerRunningInfo.decode(response.body);
1150
+ }
1151
+ return null;
1152
+ }
1153
+ // GET_CONSUMER_LIST_BY_GROUP(38) — brokerAddr is required (pick one via the
1154
+ // %RETRY%<group> route).
1155
+ async getConsumerListByGroup(consumerGroup, brokerAddr) {
1156
+ const response = await this._invokeBroker(brokerAddr, RequestCode.GET_CONSUMER_LIST_BY_GROUP, adminExt({ consumerGroup }));
1157
+ const body = GetConsumerListByGroupResponseBody.decode(response.body);
1158
+ return body.consumerIdList;
1159
+ }
1160
+ // ---------------- Consume stats ----------------
1161
+ // GET_CONSUME_STATS(208) against ONE broker. The composite path multiplies
1162
+ // the timeout by 3 so a fan-out stays within one round-trip budget.
1163
+ async examineConsumeStatsByBroker(brokerAddr, consumerGroup, topic = null, timeoutMillis) {
1164
+ const ext = { consumerGroup };
1165
+ if (topic)
1166
+ ext['topic'] = topic;
1167
+ const response = await this._invokeBroker(brokerAddr, RequestCode.GET_CONSUME_STATS, ext, null, timeoutMillis != null ? timeoutMillis * 3 : undefined);
1168
+ return ConsumeStats.decode(response.body);
1169
+ }
1170
+ // GET_CONSUME_STATS(208) leaf at the PLAIN timeout: the concurrent sweep
1171
+ // bounds the whole fan-out with one budget, so the per-broker call must not
1172
+ // multiply it.
1173
+ async _getConsumeStatsAt(brokerAddr, consumerGroup, topic, timeoutMillis) {
1174
+ const ext = { consumerGroup };
1175
+ if (topic)
1176
+ ext['topic'] = topic;
1177
+ const response = await this._invokeBroker(brokerAddr, RequestCode.GET_CONSUME_STATS, ext, null, timeoutMillis);
1178
+ return ConsumeStats.decode(response.body);
1179
+ }
1180
+ // examineConsumeStats(group[, topic]): route candidates are the
1181
+ // %RETRY%<group> topic, then the topic itself, then the POP retry topic —
1182
+ // the first route that resolves wins. Fan out over all brokers, merge the
1183
+ // offset tables and sum the tps. An empty table raises CONSUMER_NOT_ONLINE
1184
+ // (or BROADCAST_CONSUMPTION when the group consumes in broadcast mode).
1185
+ async examineConsumeStats(consumerGroup, topic = null) {
1186
+ const routeTopics = [MixAll.getRetryTopic(consumerGroup)];
1187
+ if (topic != null) {
1188
+ routeTopics.push(topic);
1189
+ // POP retry topic name: %RETRY%<group>_<topic>
1190
+ routeTopics.push(`${MixAll.RETRY_GROUP_TOPIC_PREFIX}${consumerGroup}${POP_RETRY_SEPARATOR_V1}${topic}`);
1191
+ }
1192
+ let route = null;
1193
+ for (let i = 0; i < routeTopics.length; i++) {
1194
+ try {
1195
+ route = await this.examineTopicRouteInfo(routeTopics[i]);
1196
+ break;
1197
+ }
1198
+ catch (e) {
1199
+ if (i === routeTopics.length - 1)
1200
+ throw e;
1201
+ }
1202
+ }
1203
+ const result = new ConsumeStats();
1204
+ for (const bd of route.brokerDatas) {
1205
+ const addr = bd.selectBrokerAddr();
1206
+ if (addr == null)
1207
+ continue;
1208
+ const part = await this.examineConsumeStatsByBroker(addr, consumerGroup, topic);
1209
+ for (const [q, w] of part.offsetTable)
1210
+ result.offsetTable.set(q, w);
1211
+ result.consumeTps += part.consumeTps;
1212
+ }
1213
+ if (result.offsetTable.size === 0) {
1214
+ let broadcast = false;
1215
+ try {
1216
+ const connection = await this.examineConsumerConnectionInfo(consumerGroup);
1217
+ broadcast = connection.messageModel === 'BROADCASTING';
1218
+ }
1219
+ catch (e) {
1220
+ const err = new MQClientException('Not found the consumer group consume stats, because return '
1221
+ + 'offset table is empty, maybe the consumer not online');
1222
+ err.responseCode = ResponseCode.CONSUMER_NOT_ONLINE;
1223
+ throw err;
1224
+ }
1225
+ if (broadcast) {
1226
+ const err = new MQClientException('Not found the consumer group consume stats, because return '
1227
+ + 'offset table is empty, the consumer is under the broadcast mode');
1228
+ err.responseCode = ResponseCode.BROADCAST_CONSUMPTION;
1229
+ throw err;
1230
+ }
1231
+ }
1232
+ return result;
1233
+ }
1234
+ // Fan-out flavor of examineConsumeStats: the route is discovered over the
1235
+ // retry topic, then the topic, then the POP retry topic (the first that
1236
+ // resolves wins, a failed probe is skipped), every master is asked at once,
1237
+ // offset tables are merged and consumeTps is SUMMED. Task failures are
1238
+ // logged, never propagated. An empty merged table is reported as
1239
+ // CONSUMER_NOT_ONLINE / BROADCAST_CONSUMPTION after the group's connections
1240
+ // have been probed, and a missing route as TOPIC_ROUTE_INFO_NOT_EXIST.
1241
+ async examineConsumeStatsConcurrent(consumerGroup, topic) {
1242
+ return this._adminToolExecute(async () => {
1243
+ const routeTopics = [MixAll.getRetryTopic(consumerGroup)];
1244
+ if (topic != null) {
1245
+ routeTopics.push(topic);
1246
+ routeTopics.push(`${MixAll.RETRY_GROUP_TOPIC_PREFIX}${consumerGroup}${POP_RETRY_SEPARATOR_V1}${topic}`);
1247
+ }
1248
+ let route = null;
1249
+ for (const routeTopic of routeTopics) {
1250
+ try {
1251
+ route = await this.examineTopicRouteInfo(routeTopic);
1252
+ break;
1253
+ }
1254
+ catch (e) {
1255
+ continue; // the sweep reports a missing route, it does not throw
1256
+ }
1257
+ }
1258
+ if (route == null || route.brokerDatas.length === 0) {
1259
+ return AdminToolResult.failure(AdminToolsResultCodeEnum.TOPIC_ROUTE_INFO_NOT_EXIST, 'topic router info not found');
1260
+ }
1261
+ const timeout = this._timeout();
1262
+ const result = new ConsumeStats();
1263
+ const settled = await Promise.allSettled(route.brokerDatas.map(async (bd) => {
1264
+ const addr = bd.selectBrokerAddr();
1265
+ if (addr == null)
1266
+ return null;
1267
+ return this._getConsumeStatsAt(addr, consumerGroup, topic, timeout);
1268
+ }));
1269
+ for (const s of settled) {
1270
+ if (s.status === 'rejected') {
1271
+ logger.warn(`getConsumeStats error. topic=${topic}, consumerGroup=${consumerGroup}: `
1272
+ + `${s.reason?.message}`);
1273
+ continue;
1274
+ }
1275
+ if (s.value == null)
1276
+ continue;
1277
+ for (const [q, w] of s.value.offsetTable)
1278
+ result.offsetTable.set(q, w);
1279
+ result.consumeTps += s.value.consumeTps;
1280
+ }
1281
+ if (result.offsetTable.size === 0) {
1282
+ let connection;
1283
+ try {
1284
+ connection = await this.examineConsumerConnectionInfo(consumerGroup);
1285
+ }
1286
+ catch (e) {
1287
+ return AdminToolResult.failure(AdminToolsResultCodeEnum.CONSUMER_NOT_ONLINE, 'Not found the '
1288
+ + 'consumer group consume stats, because return offset table is empty, maybe the consumer '
1289
+ + 'not consume any message');
1290
+ }
1291
+ if (connection.messageModel === 'BROADCASTING') {
1292
+ return AdminToolResult.failure(AdminToolsResultCodeEnum.BROADCAST_CONSUMPTION, 'Not found the '
1293
+ + 'consumer group consume stats, because return offset table is empty, the consumer is '
1294
+ + 'under the broadcast mode');
1295
+ }
1296
+ }
1297
+ return result;
1298
+ });
1299
+ }
1300
+ // GET_BROKER_CONSUME_STATS(317).
1301
+ async fetchConsumeStatsInBroker(brokerAddr, isOrder, timeoutMillis) {
1302
+ const response = await this._invokeBroker(brokerAddr, RequestCode.GET_BROKER_CONSUME_STATS, adminExt({ isOrder }), null, timeoutMillis);
1303
+ return ConsumeStatsList.decode(response.body);
1304
+ }
1305
+ // QUERY_TOPIC_CONSUME_BY_WHO(300) — FIRST broker of the topic route wins.
1306
+ async queryTopicConsumeByWho(topic) {
1307
+ const route = await this.examineTopicRouteInfo(topic);
1308
+ for (const bd of route.brokerDatas) {
1309
+ const addr = bd.selectBrokerAddr();
1310
+ if (addr == null)
1311
+ continue;
1312
+ const response = await this._invokeBroker(addr, RequestCode.QUERY_TOPIC_CONSUME_BY_WHO, adminExt({ topic }));
1313
+ return GroupList.decode(response.body);
1314
+ }
1315
+ return null;
1316
+ }
1317
+ // QUERY_TOPICS_BY_CONSUMER(343) leaf for one address.
1318
+ async _queryTopicsByConsumerAt(brokerAddr, group) {
1319
+ const response = await this._invokeBroker(brokerAddr, RequestCode.QUERY_TOPICS_BY_CONSUMER, adminExt({ group }));
1320
+ return TopicList.decode(response.body);
1321
+ }
1322
+ // QUERY_TOPICS_BY_CONSUMER(343) — fan out over the %RETRY%<group> route and
1323
+ // merge; the response's topicList is a Set, hence de-duplication.
1324
+ async queryTopicsByConsumer(group) {
1325
+ const retryTopic = MixAll.getRetryTopic(group);
1326
+ const route = await this.examineTopicRouteInfo(retryTopic);
1327
+ const result = new TopicList();
1328
+ const seen = new Set();
1329
+ for (const bd of route.brokerDatas) {
1330
+ const addr = bd.selectBrokerAddr();
1331
+ if (addr == null)
1332
+ continue;
1333
+ const part = await this._queryTopicsByConsumerAt(addr, group);
1334
+ for (const t of part.topicList) {
1335
+ if (!seen.has(t)) {
1336
+ seen.add(t);
1337
+ result.topicList.push(t);
1338
+ }
1339
+ }
1340
+ }
1341
+ return result;
1342
+ }
1343
+ // Fan-out flavor of queryTopicsByConsumer: route on the group's retry topic
1344
+ // and union only the topicList entries across the brokers. Task failures are
1345
+ // logged; a missing retry route is reported as TOPIC_ROUTE_INFO_NOT_EXIST.
1346
+ async queryTopicsByConsumerConcurrent(group) {
1347
+ return this._adminToolExecute(async () => {
1348
+ const retryTopic = MixAll.getRetryTopic(group);
1349
+ const route = await this.examineTopicRouteInfo(retryTopic);
1350
+ if (route == null || route.brokerDatas.length === 0) {
1351
+ return AdminToolResult.failure(AdminToolsResultCodeEnum.TOPIC_ROUTE_INFO_NOT_EXIST, 'router info not found.');
1352
+ }
1353
+ const result = new TopicList();
1354
+ const seen = new Set();
1355
+ const settled = await Promise.allSettled(route.brokerDatas.map(async (bd) => {
1356
+ const addr = bd.selectBrokerAddr();
1357
+ if (addr == null)
1358
+ return null;
1359
+ return this._queryTopicsByConsumerAt(addr, group);
1360
+ }));
1361
+ for (const s of settled) {
1362
+ if (s.status === 'rejected') {
1363
+ logger.warn(`queryTopicsByConsumer error. group=${group}: ${s.reason?.message}`);
1364
+ continue;
1365
+ }
1366
+ if (s.value == null)
1367
+ continue;
1368
+ for (const t of s.value.topicList) {
1369
+ if (!seen.has(t)) {
1370
+ seen.add(t);
1371
+ result.topicList.push(t);
1372
+ }
1373
+ }
1374
+ }
1375
+ return result;
1376
+ });
1377
+ }
1378
+ // QUERY_SUBSCRIPTION_BY_CONSUMER(345) — FIRST broker of the topic route.
1379
+ async querySubscription(group, topic) {
1380
+ const route = await this.examineTopicRouteInfo(topic);
1381
+ for (const bd of route.brokerDatas) {
1382
+ const addr = bd.selectBrokerAddr();
1383
+ if (addr == null)
1384
+ continue;
1385
+ const response = await this._invokeBroker(addr, RequestCode.QUERY_SUBSCRIPTION_BY_CONSUMER, adminExt({ group, topic }));
1386
+ if (response.body == null || response.body.length === 0)
1387
+ return null;
1388
+ return RemotingSerializable.decode(response.body);
1389
+ }
1390
+ return null;
1391
+ }
1392
+ // INVOKE_BROKER_TO_GET_CONSUMER_STATUS(223) — FIRST broker of the topic
1393
+ // route.
1394
+ async getConsumeStatus(topic, group, clientAddr = '') {
1395
+ const route = await this.examineTopicRouteInfo(topic);
1396
+ if (route.brokerDatas.length === 0)
1397
+ return {};
1398
+ const addr = route.brokerDatas[0].selectBrokerAddr();
1399
+ if (addr == null)
1400
+ return {};
1401
+ const response = await this._invokeBroker(addr, RequestCode.INVOKE_BROKER_TO_GET_CONSUMER_STATUS, adminExt({ topic, group, clientAddr }));
1402
+ const out = {};
1403
+ if (response.body == null || response.body.length === 0)
1404
+ return out;
1405
+ const obj = RemotingSerializable.decode(response.body);
1406
+ const table = obj['consumerTable'];
1407
+ if (table != null && typeof table === 'object') {
1408
+ for (const [k, v] of Object.entries(table))
1409
+ out[k] = v;
1410
+ }
1411
+ return out;
1412
+ }
1413
+ // CLONE_GROUP_OFFSET(314) — fan out over the SRC group's retry route.
1414
+ async cloneGroupOffset(srcGroup, destGroup, topic, isOffline) {
1415
+ const retryTopic = MixAll.getRetryTopic(srcGroup);
1416
+ const route = await this.examineTopicRouteInfo(retryTopic);
1417
+ for (const bd of route.brokerDatas) {
1418
+ const addr = bd.selectBrokerAddr();
1419
+ if (addr == null)
1420
+ continue;
1421
+ await this._invokeBroker(addr, RequestCode.CLONE_GROUP_OFFSET, adminExt({
1422
+ srcGroup, destGroup, topic, offline: isOffline,
1423
+ }));
1424
+ }
1425
+ }
1426
+ // ---------------- Offset reset ----------------
1427
+ // INVOKE_BROKER_TO_RESET_OFFSET(222) → ResetOffsetBody (a MessageQueue-keyed
1428
+ // map whose keys the broker's fastjson codec inlines). `isC` selects the
1429
+ // C-compatible response shape via the `isC` ext field.
1430
+ // When `queueId` is given the reset is scoped to that ONE queue and the
1431
+ // broker reads the pair (queueId, offset) literally; without them the same
1432
+ // request code resets EVERY queue of the group at `timestamp`.
1433
+ async invokeBrokerToResetOffset(brokerAddr, topic, group, timestamp, isForce, isC = false, queueId = null, resetOffset = null) {
1434
+ const ext = adminExt({ topic, consumerGroup: group, timestamp, isForce });
1435
+ if (queueId != null) {
1436
+ ext['queueId'] = extValue(queueId);
1437
+ ext['offset'] = extValue(resetOffset != null ? resetOffset : -1);
1438
+ }
1439
+ let code = RequestCode.INVOKE_BROKER_TO_RESET_OFFSET;
1440
+ if (isC) {
1441
+ // Same request code for both shapes; the `isC` flag tells the broker to
1442
+ // serialise the body in the C-compatible form.
1443
+ code = RequestCode.INVOKE_BROKER_TO_RESET_OFFSET;
1444
+ void isC;
1445
+ }
1446
+ const response = await this._invokeBroker(brokerAddr, code, ext);
1447
+ if (response.body == null || response.body.length === 0)
1448
+ return null;
1449
+ const body = ResetOffsetBody.decode(response.body);
1450
+ return body.offsetTable;
1451
+ }
1452
+ // resetOffsetByTimestamp fans out over the topic route and merges.
1453
+ async resetOffsetByTimestamp(topic, group, timestamp, isForce = true, clusterName = null) {
1454
+ void clusterName; // clusterName is only consulted for LMQ topics; unused here
1455
+ const route = await this.examineTopicRouteInfo(topic);
1456
+ const allOffsetTable = new Map();
1457
+ for (const bd of route.brokerDatas) {
1458
+ const addr = bd.selectBrokerAddr();
1459
+ if (addr == null)
1460
+ continue;
1461
+ const offsetTable = await this.invokeBrokerToResetOffset(addr, topic, group, timestamp, isForce);
1462
+ if (offsetTable != null) {
1463
+ for (const [mq, off] of offsetTable)
1464
+ allOffsetTable.set(mq, off);
1465
+ }
1466
+ }
1467
+ return allOffsetTable;
1468
+ }
1469
+ // resetOffsetNew prefers the broker-side reset; when the consumer is
1470
+ // offline fall back to the old per-queue UPDATE_CONSUMER_OFFSET path.
1471
+ async resetOffsetNew(consumerGroup, topic, timestamp) {
1472
+ try {
1473
+ await this.resetOffsetByTimestamp(topic, consumerGroup, timestamp, true);
1474
+ }
1475
+ catch (e) {
1476
+ if (e.responseCode === ResponseCode.CONSUMER_NOT_ONLINE) {
1477
+ await this.resetOffsetByTimestampOld(consumerGroup, topic, timestamp, true);
1478
+ return;
1479
+ }
1480
+ throw e;
1481
+ }
1482
+ }
1483
+ // Concurrent resetOffsetNew: the broker-side reset fans over every broker of
1484
+ // the route; a broker whose consumer is offline falls back to the old
1485
+ // per-queue walk on THAT broker alone, and the whole run only succeeds when
1486
+ // every broker did.
1487
+ async resetOffsetNewConcurrent(group, topic, timestamp) {
1488
+ return this._adminToolExecute(async () => {
1489
+ const route = await this.examineTopicRouteInfo(topic);
1490
+ if (route == null || route.brokerDatas == null || route.brokerDatas.length === 0) {
1491
+ return AdminToolResult.failure(AdminToolsResultCodeEnum.TOPIC_ROUTE_INFO_NOT_EXIST, 'topic router info not found');
1492
+ }
1493
+ const queueDataByBroker = {};
1494
+ for (const qd of route.queueDatas)
1495
+ queueDataByBroker[qd.brokerName] = qd;
1496
+ const tasks = route.brokerDatas.map(async (bd) => {
1497
+ const addr = bd.selectBrokerAddr();
1498
+ if (addr == null)
1499
+ return { addr: null, ok: false };
1500
+ try {
1501
+ const offsetTable = await this.invokeBrokerToResetOffset(addr, topic, group, timestamp, true);
1502
+ return { addr, ok: offsetTable != null };
1503
+ }
1504
+ catch (e) {
1505
+ // The reset leaf raises with a responseCode whether it wraps the
1506
+ // answer in the broker or the client exception — match on the code.
1507
+ if (e.responseCode === ResponseCode.CONSUMER_NOT_ONLINE) {
1508
+ try {
1509
+ const qd = queueDataByBroker[bd.brokerName];
1510
+ if (qd == null) {
1511
+ throw new MQClientException(`No route data of broker ${bd.brokerName} for topic ${topic}`);
1512
+ }
1513
+ await this._resetOffsetOldForBroker(addr, bd.brokerName, qd, group, topic, timestamp, true);
1514
+ return { addr, ok: true };
1515
+ }
1516
+ catch (e2) {
1517
+ logger.warn(`resetOffsetByTimestampOld error. addr=${addr}, topic=${topic}, group=${group}, `
1518
+ + `timestamp=${timestamp}: ${e2?.message}`);
1519
+ return { addr, ok: false };
1520
+ }
1521
+ }
1522
+ if (e.responseCode === ResponseCode.SYSTEM_ERROR) {
1523
+ // The group never came online on this broker — nothing to reset.
1524
+ return { addr, ok: true };
1525
+ }
1526
+ logger.warn(`resetOffsetNewConcurrent error. addr=${addr}, topic=${topic}, group=${group}, `
1527
+ + `timestamp=${timestamp}: ${e?.message}`);
1528
+ return { addr, ok: false };
1529
+ }
1530
+ });
1531
+ const outcomes = await Promise.allSettled(tasks);
1532
+ const result = new BrokerOperatorResult();
1533
+ for (const s of outcomes) {
1534
+ if (s.status === 'fulfilled') {
1535
+ if (s.value.addr != null) {
1536
+ if (s.value.ok)
1537
+ result.successList.push(s.value.addr);
1538
+ else
1539
+ result.failureList.push(s.value.addr);
1540
+ }
1541
+ }
1542
+ else {
1543
+ logger.warn(`resetOffsetNewConcurrent task rejected: ${s.reason?.message}`);
1544
+ }
1545
+ }
1546
+ if (result.successList.length === route.brokerDatas.length) {
1547
+ return AdminToolResult.success(result);
1548
+ }
1549
+ return AdminToolResult.failure(AdminToolsResultCodeEnum.MQ_BROKER_ERROR, 'operator failure', result);
1550
+ });
1551
+ }
1552
+ // resetOffsetByTimestampOld: per queue — when the group consumed the
1553
+ // topic, reset each queue it touched; otherwise reset EVERY read queue.
1554
+ async resetOffsetByTimestampOld(consumerGroup, topic, timestamp, force) {
1555
+ const route = await this.examineTopicRouteInfo(topic);
1556
+ const queueDataByBroker = {};
1557
+ for (const qd of route.queueDatas)
1558
+ queueDataByBroker[qd.brokerName] = qd;
1559
+ const rollbackStatsList = [];
1560
+ for (const bd of route.brokerDatas) {
1561
+ const addr = bd.selectBrokerAddr();
1562
+ if (addr == null)
1563
+ continue;
1564
+ rollbackStatsList.push(...await this._resetOffsetOldForBroker(addr, bd.brokerName, queueDataByBroker[bd.brokerName] != null ? queueDataByBroker[bd.brokerName] : null, consumerGroup, topic, timestamp, force));
1565
+ }
1566
+ return rollbackStatsList;
1567
+ }
1568
+ // One broker's leg of the old per-queue reset walk: the queues the group
1569
+ // consumed are reset from their consume offsets, an untouched group falls
1570
+ // back to every read queue of the broker's route entry.
1571
+ async _resetOffsetOldForBroker(brokerAddr, brokerName, queueData, consumerGroup, topic, timestamp, force) {
1572
+ const rollbackStatsList = [];
1573
+ const consumeStats = await this.examineConsumeStatsByBroker(brokerAddr, consumerGroup);
1574
+ let hasConsumed = false;
1575
+ for (const [queue, offsetWrapper] of consumeStats.offsetTable) {
1576
+ if (queue.getTopic() === topic) {
1577
+ hasConsumed = true;
1578
+ rollbackStatsList.push(await this._resetOffsetConsumeOffset(brokerAddr, consumerGroup, queue, offsetWrapper, timestamp, force));
1579
+ }
1580
+ }
1581
+ if (!hasConsumed) {
1582
+ const topicStatus = await this.examineTopicStatsByBroker(brokerAddr, topic);
1583
+ const readQueueNums = queueData != null ? queueData.readQueueNums : 0;
1584
+ for (let i = 0; i < readQueueNums; i++) {
1585
+ const queue = new MessageQueue(topic, brokerName, i);
1586
+ const to = topicStatus.offsetTable.get(queue);
1587
+ const offsetWrapper = {
1588
+ brokerOffset: to != null ? to.maxOffset : 0,
1589
+ consumerOffset: to != null ? to.minOffset : 0,
1590
+ };
1591
+ rollbackStatsList.push(await this._resetOffsetConsumeOffset(brokerAddr, consumerGroup, queue, offsetWrapper, timestamp, force));
1592
+ }
1593
+ }
1594
+ return rollbackStatsList;
1595
+ }
1596
+ async _resetOffsetConsumeOffset(brokerAddr, consumeGroup, queue, offsetWrapper, timestamp, force) {
1597
+ const client = this._requireClient();
1598
+ let resetOffset;
1599
+ if (timestamp === -1) {
1600
+ resetOffset = await client.getMaxOffset(brokerAddr, queue.getTopic(), queue.getQueueId());
1601
+ }
1602
+ else {
1603
+ resetOffset = await client.searchOffsetByTimestamp(brokerAddr, queue.getTopic(), queue.getQueueId(), timestamp);
1604
+ }
1605
+ const stats = {
1606
+ brokerName: queue.getBrokerName(),
1607
+ queueId: queue.getQueueId(),
1608
+ brokerOffset: offsetWrapper.brokerOffset,
1609
+ consumerOffset: offsetWrapper.consumerOffset,
1610
+ timestampOffset: resetOffset,
1611
+ rollbackOffset: offsetWrapper.consumerOffset,
1612
+ };
1613
+ if (force || resetOffset <= offsetWrapper.consumerOffset) {
1614
+ stats.rollbackOffset = resetOffset;
1615
+ await client.updateConsumerOffset(brokerAddr, consumeGroup, queue, resetOffset);
1616
+ }
1617
+ return stats;
1618
+ }
1619
+ // resetOffsetByQueueId: direct UPDATE_CONSUMER_OFFSET, then a
1620
+ // broker-side reset of that single queue.
1621
+ async resetOffsetByQueueId(brokerAddr, consumeGroup, topicName, queueId, resetOffset) {
1622
+ const client = this._requireClient();
1623
+ await client.updateConsumerOffset(brokerAddr, consumeGroup, new MessageQueue(topicName, '', queueId), resetOffset);
1624
+ await this.invokeBrokerToResetOffset(brokerAddr, topicName, consumeGroup, 0, false, false, queueId, resetOffset);
1625
+ }
1626
+ // ---------------- Offset queries (admin-side offset surface) ----------------
1627
+ _masterAddrForMq(mq) {
1628
+ const client = this._requireClient();
1629
+ const addr = client.findBrokerAddressInPublish(mq.getBrokerName());
1630
+ if (addr == null) {
1631
+ throw new MQClientException(`The broker[${mq.getBrokerName()}] not exist`);
1632
+ }
1633
+ return addr;
1634
+ }
1635
+ async maxOffset(mq) {
1636
+ return this._requireClient().getMaxOffset(this._masterAddrForMq(mq), mq.getTopic(), mq.getQueueId());
1637
+ }
1638
+ async minOffset(mq) {
1639
+ return this._requireClient().getMinOffset(this._masterAddrForMq(mq), mq.getTopic(), mq.getQueueId());
1640
+ }
1641
+ async searchOffset(mq, timestamp) {
1642
+ return this._requireClient().searchOffsetByTimestamp(this._masterAddrForMq(mq), mq.getTopic(), mq.getQueueId(), timestamp);
1643
+ }
1644
+ async earliestMsgStoreTime(mq) {
1645
+ return this._requireClient().getEarliestMsgStoretime(this._masterAddrForMq(mq), mq.getTopic(), mq.getQueueId());
1646
+ }
1647
+ // updateConsumeOffset writes the committed offset for one queue at one
1648
+ // broker via UPDATE_CONSUMER_OFFSET(15).
1649
+ async updateConsumeOffset(brokerAddr, consumeGroup, mq, offset) {
1650
+ await this._requireClient().updateConsumerOffset(brokerAddr, consumeGroup, mq, offset);
1651
+ }
1652
+ // examineConsumerOffset issues QUERY_CONSUMER_OFFSET(14). Returns the
1653
+ // committed offset; `hasOffset` is false when the broker answers
1654
+ // QUERY_NOT_FOUND (no offset recorded yet).
1655
+ async examineConsumerOffset(consumerGroup, mq) {
1656
+ const brokerAddr = this._masterAddrForMq(mq);
1657
+ const r = await this._requireClient().queryConsumerOffset(brokerAddr, consumerGroup, mq);
1658
+ return { offset: r.offset, hasOffset: r.found };
1659
+ }
1660
+ // searchBoundaryOffset issues SEARCH_OFFSET_BY_TIMESTAMP(29) with the
1661
+ // `boundaryType` ext field. The lower/upper wrappers pick
1662
+ // BoundaryType.LOWER / UPPER.
1663
+ async searchBoundaryOffset(mq, timestamp, boundaryType = BoundaryType.LOWER) {
1664
+ const brokerAddr = this._masterAddrForMq(mq);
1665
+ const client = this._requireClient();
1666
+ const request = this._request(RequestCode.SEARCH_OFFSET_BY_TIMESTAMP, adminExt({
1667
+ topic: mq.getTopic(),
1668
+ queueId: String(mq.getQueueId()),
1669
+ timestamp: String(timestamp),
1670
+ boundaryType: BoundaryType.get_type(boundaryType),
1671
+ }));
1672
+ const target = this._vipChannel(brokerAddr);
1673
+ const response = await client.remotingClient.invokeSync(target, request, this._timeout());
1674
+ if (response.code !== ResponseCode.SUCCESS) {
1675
+ throw new MQBrokerException(response.code, response.remark || `CODE: ${response.code}`, target);
1676
+ }
1677
+ const offset = response.extFields != null ? response.extFields['offset'] : null;
1678
+ return offset != null ? parseInt(offset, 10) : 0;
1679
+ }
1680
+ async searchLowerBoundaryOffset(mq, timestamp) {
1681
+ return this.searchBoundaryOffset(mq, timestamp, BoundaryType.LOWER);
1682
+ }
1683
+ async searchUpperBoundaryOffset(mq, timestamp) {
1684
+ return this.searchBoundaryOffset(mq, timestamp, BoundaryType.UPPER);
1685
+ }
1686
+ // ---------------- Message query (QUERY_MESSAGE / VIEW_MESSAGE_BY_ID) ----------------
1687
+ // queryMessage issues QUERY_MESSAGE(12) against one broker's index.
1688
+ //
1689
+ // ⚠ WIRE: the response body is NOT JSON — the broker streams the matched
1690
+ // messages as CONCATENATED 17-segment stored records. Parsing it as
1691
+ // RemotingSerializable JSON was the bug behind "query by key always fails".
1692
+ // indexType: K (default) / U (uniq key); 'U' additionally sets
1693
+ // MixAll.UNIQUE_MSG_QUERY_FLAG so the broker takes the unique-key path.
1694
+ async queryMessage(brokerAddr, topic, key, maxNum = 32, beginTimestamp = 0, endTimestamp = 0, indexType = 'K') {
1695
+ // The time window is unbounded when begin/end are unset: the broker
1696
+ // matches index FILES against [begin, end], so end=0 matches NOTHING
1697
+ // (every real index file has a start timestamp > 0) and every query would
1698
+ // answer QUERY_NOT_FOUND. Normalise: begin<0/0 → unbounded past,
1699
+ // end<=0 → now.
1700
+ const begin = beginTimestamp > 0 ? beginTimestamp : 0;
1701
+ // Long.MAX_VALUE as a STRING — as a JS Number it rounds up to 2^63 and
1702
+ // the broker's Long.parseLong rejects it, leaving the header field null
1703
+ // (NPE inside QueryMessageProcessor).
1704
+ const end = endTimestamp > 0 ? endTimestamp : '9223372036854775807';
1705
+ const ext = {
1706
+ topic,
1707
+ key,
1708
+ maxNum: String(maxNum),
1709
+ beginTimestamp: String(begin),
1710
+ endTimestamp: String(end),
1711
+ indexType,
1712
+ };
1713
+ if (indexType === MessageConst.INDEX_UNIQUE_TYPE) {
1714
+ // MixAll.UNIQUE_MSG_QUERY_FLAG — forces the U-index path and the
1715
+ // broker's defaultQueryMaxNum on the server side.
1716
+ ext['_UNIQUE_KEY_QUERY'] = 'true';
1717
+ }
1718
+ const response = await this._invokeBroker(brokerAddr, RequestCode.QUERY_MESSAGE, ext);
1719
+ const result = new QueryResult();
1720
+ // The response's QueryMessageResponseHeader fields ride the extFields.
1721
+ const extResp = response.extFields || {};
1722
+ if (extResp['indexLastUpdateTimestamp'] != null) {
1723
+ result.indexLastUpdateTimestamp = parseInt(extResp['indexLastUpdateTimestamp'], 10);
1724
+ }
1725
+ if (response.body != null && response.body.length > 0) {
1726
+ result.messageList = decodeMessages(response.body, true);
1727
+ }
1728
+ return result;
1729
+ }
1730
+ async queryMessageByKey(brokerAddr, topic, key, maxNum = 32) {
1731
+ return this.queryMessage(brokerAddr, topic, key, maxNum);
1732
+ }
1733
+ // queryMessageByUniqKey: the U-index query. The stored records
1734
+ // are already in the response body, so no per-hit viewMessage round trip is
1735
+ // needed; only the first hit is returned for this shape.
1736
+ async queryMessageByUniqKey(brokerAddr, topic, uniqKey, maxNum = 32) {
1737
+ const result = await this.queryMessage(brokerAddr, topic, uniqKey, maxNum, 0, 0, 'U');
1738
+ return result.messageList.length > 0 ? result.messageList[0] : null;
1739
+ }
1740
+ // queryMessageFromRoute resolves the topic route and queries EVERY
1741
+ // broker (selectBrokerAddr of each BrokerData: master preferred, else the
1742
+ // lowest id — a slave-only topology must still be queryable), merging the
1743
+ // hits. QUERY_NOT_FOUND from one broker is not an error (log and
1744
+ // continue).
1745
+ async queryMessageFromRoute(topic, key, maxNum = 32, beginTimestamp = 0, endTimestamp = 0, indexType = 'K') {
1746
+ const client = this._requireClient();
1747
+ if (client.getTopicRouteData(topic) == null) {
1748
+ await client.updateTopicRouteInfoFromNameServer(topic, false).catch(() => { });
1749
+ }
1750
+ const route = client.getTopicRouteData(topic);
1751
+ const merged = new QueryResult();
1752
+ if (route == null)
1753
+ return merged;
1754
+ for (const bd of route.brokerDatas || []) {
1755
+ const addrs = bd.brokerAddrs || {};
1756
+ const addr = addrs['0'] != null ? addrs['0'] : Object.values(addrs)[0];
1757
+ if (addr == null)
1758
+ continue;
1759
+ try {
1760
+ const one = await this.queryMessage(addr, topic, key, maxNum, beginTimestamp, endTimestamp, indexType);
1761
+ merged.messageList.push(...one.messageList);
1762
+ if (one.indexLastUpdateTimestamp > merged.indexLastUpdateTimestamp) {
1763
+ merged.indexLastUpdateTimestamp = one.indexLastUpdateTimestamp;
1764
+ }
1765
+ }
1766
+ catch (e) {
1767
+ if (e instanceof MQBrokerException && e.responseCode === ResponseCode.QUERY_NOT_FOUND)
1768
+ continue;
1769
+ throw e;
1770
+ }
1771
+ }
1772
+ return merged;
1773
+ }
1774
+ // viewMessage is Java DefaultMQAdminExtImpl#viewMessage
1775
+ // (DefaultMQAdminExtImpl.java:578): try the offset-msgId leg first, and
1776
+ // when it fails for any reason (the msgId "maybe created by new client")
1777
+ // fall back to the U-index query across the route's brokers. When neither
1778
+ // leg matches, Java returns null here instead of raising — so does this.
1779
+ async viewMessage(topic, msgId) {
1780
+ const client = this._requireClient();
1781
+ try {
1782
+ return await client.viewMessage(topic, msgId, this._timeout());
1783
+ }
1784
+ catch {
1785
+ // The offset-msgId leg failed — the uniq-key leg decides.
1786
+ }
1787
+ return this.viewMessageByUniqKey(topic, msgId);
1788
+ }
1789
+ // The uniq-key leg of the viewMessage fallback: the
1790
+ // U-index query is sent across every broker in
1791
+ // the route, first hit wins, verified against the UNIQ_KEY property so a
1792
+ // stray key collision can't satisfy the lookup. Java's queryMessageByUniqKey
1793
+ // returns null when nothing matches — so does this.
1794
+ async viewMessageByUniqKey(topic, uniqKey) {
1795
+ const result = await this.queryMessageFromRoute(topic, uniqKey, 32, 0, 0, 'U');
1796
+ for (const m of result.messageList) {
1797
+ if (m.getProperty(MessageConst.PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX) === uniqKey)
1798
+ return m;
1799
+ }
1800
+ return null;
1801
+ }
1802
+ // consumeMessageDirectly sends CONSUME_MESSAGE_DIRECTLY(309)
1803
+ // to the broker that STORES the message; the broker relays it to the
1804
+ // named client and answers with the client's verdict body. The store host is
1805
+ // resolved through viewMessage and the OFFSET msgId is substituted when the
1806
+ // resolved message carries a client uniq key.
1807
+ async consumeMessageDirectly(consumerGroup, clientId, topic, msgId, brokerAddr) {
1808
+ let addr = brokerAddr;
1809
+ let outMsgId = msgId;
1810
+ if (addr == null || addr === '') {
1811
+ const msg = await this.viewMessage(topic, msgId);
1812
+ // Java dereferences the viewMessage null unguarded here (a latent NPE);
1813
+ // fail with a clear client error instead.
1814
+ if (msg == null) {
1815
+ throw new MQClientException(`message ${msgId} of ${topic} not found`);
1816
+ }
1817
+ addr = msg.getStoreHostString();
1818
+ if (msg.getProperty(MessageConst.PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX) != null && msg.getOffsetMsgId() != null) {
1819
+ outMsgId = msg.getOffsetMsgId();
1820
+ }
1821
+ }
1822
+ const response = await this._invokeBroker(addr, RequestCode.CONSUME_MESSAGE_DIRECTLY, adminExt({
1823
+ consumerGroup,
1824
+ clientId,
1825
+ msgId: outMsgId,
1826
+ topic,
1827
+ }));
1828
+ if (response.body == null || response.body.length === 0) {
1829
+ throw new MQClientException(`no consume result for client ${clientId}`);
1830
+ }
1831
+ return ConsumeMessageDirectlyResult.decode(response.body);
1832
+ }
1833
+ // ---------------- Name-server config ----------------
1834
+ // UPDATE_NAMESRV_CONFIG(318) broadcast: the properties ride in the BODY as
1835
+ // Properties-format TEXT; the first failing nameserver decides the error.
1836
+ async updateNameServerConfig(properties, timeoutMillis) {
1837
+ const text = propertiesToString(properties);
1838
+ if (!text)
1839
+ return;
1840
+ const client = this._requireClient();
1841
+ const request = this._request(RequestCode.UPDATE_NAMESRV_CONFIG, null, Buffer.from(text, 'utf8'));
1842
+ let errResponse = null;
1843
+ for (const nsAddr of client.getNameServerAddressList()) {
1844
+ const response = await client.remotingClient.invokeSync(nsAddr, request, this._timeout(timeoutMillis));
1845
+ if (response.code !== ResponseCode.SUCCESS)
1846
+ errResponse = response;
1847
+ }
1848
+ if (errResponse != null) {
1849
+ throw new MQBrokerException(errResponse.code, errResponse.remark || 'update name server config failed');
1850
+ }
1851
+ }
1852
+ // GET_NAMESRV_CONFIG(319), one request per nameserver (null/empty = all
1853
+ // known servers); the Properties-text body is parsed back per server.
1854
+ async getNameServerConfig(nameServers, timeoutMillis) {
1855
+ const client = this._requireClient();
1856
+ const targets = (nameServers != null && nameServers.length > 0)
1857
+ ? nameServers
1858
+ : client.getNameServerAddressList();
1859
+ if (targets == null || targets.length === 0) {
1860
+ throw new MQClientException('no name server address available');
1861
+ }
1862
+ const out = {};
1863
+ for (const ns of targets) {
1864
+ const response = await client.remotingClient.invokeSync(ns, this._request(RequestCode.GET_NAMESRV_CONFIG, null), this._timeout(timeoutMillis));
1865
+ if (response.code !== ResponseCode.SUCCESS) {
1866
+ throw new MQBrokerException(response.code, response.remark || `CODE: ${response.code}`, ns);
1867
+ }
1868
+ out[ns] = string2Properties(response.body != null ? response.body.toString('utf8') : '');
1869
+ }
1870
+ return out;
1871
+ }
1872
+ // ---------------- Container membership ----------------
1873
+ // ADD_BROKER(902) hands a broker config path to a broker container. The
1874
+ // container address is dialled VERBATIM — the VIP port is a broker-listener
1875
+ // contract and a container is not a broker — and the timeout is fixed at
1876
+ // 20000 ms regardless of the admin-wide timeout.
1877
+ async addBrokerToContainer(brokerContainerAddr, brokerConfig) {
1878
+ await this._invokeChecked(brokerContainerAddr, RequestCode.ADD_BROKER, adminExt({ configPath: brokerConfig }), null, DefaultAdminTimeoutMillis);
1879
+ }
1880
+ // REMOVE_BROKER(903): brokerName + brokerClusterName + brokerId, no body,
1881
+ // same verbatim-address / fixed-timeout rules as the add.
1882
+ async removeBrokerFromContainer(brokerContainerAddr, clusterName, brokerName, brokerId) {
1883
+ await this._invokeChecked(brokerContainerAddr, RequestCode.REMOVE_BROKER, adminExt({ brokerName, brokerClusterName: clusterName, brokerId }), null, DefaultAdminTimeoutMillis);
1884
+ }
1885
+ // ---------------- Ordered-topic config (KV-config based) ----------------
1886
+ // createOrUpdateOrderConf stores one ordered-topic config entry in the
1887
+ // ORDER_TOPIC_CONFIG namespace. isCluster stores the value verbatim;
1888
+ // otherwise the stored value is a ';'-joined merge keyed by the part before
1889
+ // the first ':': existing segments are kept verbatim (a replaced key keeps
1890
+ // its position, a new key is appended) and the new segment wins. The merge
1891
+ // reads the stored value from one name server (a failed read is ignored and
1892
+ // the merge starts from the new value alone; the value rides the GET
1893
+ // response HEADER, there is no body) and broadcasts the PUT to every name
1894
+ // server, where the LAST failing answer is the one rethrown.
1895
+ async createOrUpdateOrderConf(key, value, isCluster) {
1896
+ if (isCluster) {
1897
+ await this.putKVConfig(NAMESPACE_ORDER_TOPIC_CONFIG, key, value);
1898
+ return;
1899
+ }
1900
+ let oldOrderConfs = null;
1901
+ try {
1902
+ oldOrderConfs = await this.getKVConfig(NAMESPACE_ORDER_TOPIC_CONFIG, key);
1903
+ }
1904
+ catch (e) {
1905
+ logger.warn(`read order conf for ${key} failed: ${e.message}`);
1906
+ }
1907
+ const orderConfMap = {};
1908
+ if (!isBlank(oldOrderConfs)) {
1909
+ for (const oldOrderConf of oldOrderConfs.split(';')) {
1910
+ const items = oldOrderConf.split(':');
1911
+ orderConfMap[items[0]] = oldOrderConf;
1912
+ }
1913
+ }
1914
+ const newItems = value.split(':');
1915
+ orderConfMap[newItems[0]] = value;
1916
+ const segments = Object.keys(orderConfMap).map((k) => orderConfMap[k]);
1917
+ await this.putKVConfig(NAMESPACE_ORDER_TOPIC_CONFIG, key, segments.join(';'));
1918
+ }
1919
+ // ---------------- Controller (leader-routed) ----------------
1920
+ // CONTROLLER_GET_METADATA_INFO(1005): which controller of the group leads.
1921
+ // The answer rides the response extFields.
1922
+ async getControllerMetaData(controllerAddr) {
1923
+ const response = await this._invokeRaw(controllerAddr, RequestCode.CONTROLLER_GET_METADATA_INFO, null, null, ControllerInvokeTimeoutMillis);
1924
+ if (response.code !== ResponseCode.SUCCESS) {
1925
+ throw new MQBrokerException(response.code, response.remark || `CODE: ${response.code}`, controllerAddr);
1926
+ }
1927
+ return GetMetaDataResponseHeader.fromDict(response.extFields || {});
1928
+ }
1929
+ // CONTROLLER_GET_SYNC_STATE_DATA(1006), two-phase: resolve the leader first,
1930
+ // then ask the LEADER for the replica state of `brokers` — a raw JSON array
1931
+ // of broker-name strings in the body, no header at all.
1932
+ async getInSyncStateData(controllerAddress, brokers) {
1933
+ const meta = await this.getControllerMetaData(controllerAddress);
1934
+ const leaderAddress = meta.controllerLeaderAddress;
1935
+ if (leaderAddress == null) {
1936
+ throw new MQBrokerException(ResponseCode.SYSTEM_ERROR, 'controller leader address not found', controllerAddress);
1937
+ }
1938
+ const response = await this._invokeRaw(leaderAddress, RequestCode.CONTROLLER_GET_SYNC_STATE_DATA, null, RemotingSerializable.encode(brokers), ControllerInvokeTimeoutMillis);
1939
+ if (response.code !== ResponseCode.SUCCESS) {
1940
+ throw new MQBrokerException(response.code, response.remark || `CODE: ${response.code}`, leaderAddress);
1941
+ }
1942
+ const body = response.body;
1943
+ if (body == null || body.length === 0)
1944
+ return new BrokerReplicasInfo();
1945
+ return BrokerReplicasInfo.decode(body);
1946
+ }
1947
+ // CLEAN_BROKER_DATA(1011) on the leader. The header's nullable strings are
1948
+ // omitted when unset, while the two primitives — isCleanLivingBroker and
1949
+ // invokeTime (stamped when the request is built) — are ALWAYS sent, even
1950
+ // when false / freshly stamped.
1951
+ async cleanControllerBrokerData(controllerAddr, clusterName, brokerName, brokerControllerIdsToClean = null, isCleanLivingBroker = false) {
1952
+ const meta = await this.getControllerMetaData(controllerAddr);
1953
+ const leaderAddress = meta.controllerLeaderAddress;
1954
+ if (leaderAddress == null) {
1955
+ throw new MQBrokerException(ResponseCode.SYSTEM_ERROR, 'controller leader address not found', controllerAddr);
1956
+ }
1957
+ const ext = {};
1958
+ if (clusterName != null)
1959
+ ext['clusterName'] = clusterName;
1960
+ ext['brokerName'] = brokerName;
1961
+ if (brokerControllerIdsToClean != null)
1962
+ ext['brokerControllerIdsToClean'] = brokerControllerIdsToClean;
1963
+ ext['isCleanLivingBroker'] = isCleanLivingBroker ? 'true' : 'false';
1964
+ ext['invokeTime'] = String(Date.now());
1965
+ const response = await this._invokeRaw(leaderAddress, RequestCode.CLEAN_BROKER_DATA, ext, null, ControllerInvokeTimeoutMillis);
1966
+ if (response.code !== ResponseCode.SUCCESS) {
1967
+ throw new MQBrokerException(response.code, response.remark || `CODE: ${response.code}`, leaderAddress);
1968
+ }
1969
+ }
1970
+ // GET_CONTROLLER_CONFIG(1010) from every controller server — the given list,
1971
+ // or every name server when none is given, null when there is no address at
1972
+ // all. The body is Properties TEXT. One failing controller aborts the sweep
1973
+ // with a client exception.
1974
+ async getControllerConfig(controllerServers, timeoutMillis) {
1975
+ const client = this._requireClient();
1976
+ const targets = (controllerServers == null || controllerServers.length === 0)
1977
+ ? client.getNameServerAddressList()
1978
+ : controllerServers;
1979
+ if (targets == null || targets.length === 0)
1980
+ return null;
1981
+ const configMap = {};
1982
+ for (const controller of targets) {
1983
+ const response = await this._invokeRaw(controller, RequestCode.GET_CONTROLLER_CONFIG, null, null, timeoutMillis);
1984
+ if (response.code !== ResponseCode.SUCCESS)
1985
+ throw this._clientError(response.code, response.remark);
1986
+ configMap[controller] = string2Properties(response.body != null ? response.body.toString('utf8') : '');
1987
+ }
1988
+ return configMap;
1989
+ }
1990
+ // UPDATE_CONTROLLER_CONFIG(1009): broadcast to every controller. An empty
1991
+ // property set (or an empty controller list) is a silent no-op; the sweep
1992
+ // keeps going after a failure and the LAST failing answer is rethrown.
1993
+ async updateControllerConfig(properties, controllers, timeoutMillis) {
1994
+ const text = propertiesToString(properties);
1995
+ if (text.length < 1 || controllers == null || controllers.length === 0)
1996
+ return;
1997
+ const body = Buffer.from(text, 'utf8');
1998
+ let errResponse = null;
1999
+ for (const controller of controllers) {
2000
+ const response = await this._invokeRaw(controller, RequestCode.UPDATE_CONTROLLER_CONFIG, null, body, timeoutMillis);
2001
+ if (response.code !== ResponseCode.SUCCESS)
2002
+ errResponse = response;
2003
+ }
2004
+ if (errResponse != null)
2005
+ throw this._clientError(errResponse.code, errResponse.remark);
2006
+ }
2007
+ // ---------------- #47 mqadmin ops RPC group ----------------
2008
+ // QUERY_CORRECTION_OFFSET(308): the broker's suggested consumer-offset
2009
+ // corrections for `group` on `topic`. filterGroups travels comma-joined in
2010
+ // the header; Java wraps this one RPC in brokerVIPChannel(flag, addr).
2011
+ async queryCorrectionOffset(brokerAddr, topic, group, filterGroups = null, timeoutMillis) {
2012
+ const header = new QueryCorrectionOffsetHeader();
2013
+ header.topic = topic;
2014
+ header.compareGroup = group;
2015
+ header.filterGroups = (filterGroups != null && filterGroups.size > 0)
2016
+ ? Array.from(filterGroups).sort().join(',') : null;
2017
+ const response = await this._invokeBroker(brokerAddr, RequestCode.QUERY_CORRECTION_OFFSET, adminExt(header.toExtFields()), null, timeoutMillis);
2018
+ const body = response.body;
2019
+ if (body == null || body.length === 0)
2020
+ return {};
2021
+ return QueryCorrectionOffsetBody.decode(body).correctionOffsets;
2022
+ }
2023
+ // GET_UNIT_TOPIC_LIST(311) / GET_HAS_UNIT_SUB_TOPIC_LIST(312) /
2024
+ // GET_HAS_UNIT_SUB_UNUNIT_TOPIC_LIST(313) share the shape: null header to a
2025
+ // name server, TopicList body, and the %RETRY% topics filtered client-side
2026
+ // when containRetry is false (Java MQClientAPIImpl:2817-2825) — the raw
2027
+ // answer is untouched on the wire.
2028
+ async _unitTopicList(code, containRetry, timeoutMillis) {
2029
+ const response = await this._invokeNameServerOne(code, null, timeoutMillis);
2030
+ if (response.code !== ResponseCode.SUCCESS) {
2031
+ throw this._clientError(response.code, response.remark || 'get unit topic list failed');
2032
+ }
2033
+ const body = response.body;
2034
+ if (body == null || body.length === 0) {
2035
+ throw this._clientError(response.code, 'the name server answered without a body');
2036
+ }
2037
+ const topicList = TopicList.decode(body);
2038
+ if (!containRetry && topicList.topicList != null) {
2039
+ topicList.topicList = topicList.topicList.filter((t) => !MixAll.isRetryTopic(t));
2040
+ }
2041
+ return topicList;
2042
+ }
2043
+ async getUnitTopicList(containRetry = true, timeoutMillis) {
2044
+ return this._unitTopicList(RequestCode.GET_UNIT_TOPIC_LIST, containRetry, timeoutMillis);
2045
+ }
2046
+ async getHasUnitSubTopicList(containRetry = true, timeoutMillis) {
2047
+ return this._unitTopicList(RequestCode.GET_HAS_UNIT_SUB_TOPIC_LIST, containRetry, timeoutMillis);
2048
+ }
2049
+ async getHasUnitSubUnUnitTopicList(containRetry = true, timeoutMillis) {
2050
+ return this._unitTopicList(RequestCode.GET_HAS_UNIT_SUB_UNUNIT_TOPIC_LIST, containRetry, timeoutMillis);
2051
+ }
2052
+ // GET_ALL_PRODUCER_INFO(328): group -> producer table, empty (zero-field)
2053
+ // header. Java wraps this one in brokerVIPChannel(flag, addr).
2054
+ async getAllProducerInfo(brokerAddr, timeoutMillis) {
2055
+ const response = await this._invokeBroker(brokerAddr, RequestCode.GET_ALL_PRODUCER_INFO, {}, null, timeoutMillis);
2056
+ const body = response.body;
2057
+ if (body == null || body.length === 0) {
2058
+ throw new MQBrokerException(response.code, 'empty producer table body', brokerAddr);
2059
+ }
2060
+ return ProducerTableInfo.decode(body);
2061
+ }
2062
+ // GET_BROKER_HA_STATUS(907) — dialled at the plain broker address, never
2063
+ // translated to the VIP port.
2064
+ async getBrokerHAStatus(brokerAddr, timeoutMillis) {
2065
+ const response = await this._invokeChecked(brokerAddr, RequestCode.GET_BROKER_HA_STATUS, null, null, timeoutMillis);
2066
+ const body = response.body;
2067
+ if (body == null || body.length === 0) {
2068
+ throw new MQBrokerException(response.code, 'empty HA status body', brokerAddr);
2069
+ }
2070
+ return HARuntimeInfo.decode(body);
2071
+ }
2072
+ // RESET_MASTER_FLUSH_OFFSET(908): one primitive long in the header, code-only
2073
+ // answer. Java pins this family to a 3000 ms budget.
2074
+ async resetMasterFlushOffset(brokerAddr, masterFlushOffset, timeoutMillis) {
2075
+ const header = new ResetMasterFlushOffsetHeader();
2076
+ header.masterFlushOffset = masterFlushOffset;
2077
+ const budget = (timeoutMillis != null && timeoutMillis > 0)
2078
+ ? timeoutMillis : ControllerInvokeTimeoutMillis;
2079
+ await this._invokeChecked(brokerAddr, RequestCode.RESET_MASTER_FLUSH_OFFSET, adminExt(header.toExtFields()), null, budget);
2080
+ }
2081
+ // CONTROLLER_ELECT_MASTER(1002), two-phase: resolve the leader through 1005,
2082
+ // then send the elect request to the LEADER with Java's ofAdminTrigger shape
2083
+ // (designateElect=true, brokerId = the assigned master id or -1). The answer
2084
+ // carries the header in the response extFields and the broker set in the body;
2085
+ // the elect leg is pinned to the 3000 ms controller budget.
2086
+ async electMaster(controllerAddr, clusterName, brokerName, brokerId = null, timeoutMillis) {
2087
+ const meta = await this.getControllerMetaData(controllerAddr);
2088
+ const leaderAddress = meta.controllerLeaderAddress;
2089
+ if (leaderAddress == null) {
2090
+ throw new MQBrokerException(ResponseCode.SYSTEM_ERROR, 'controller leader address not found', controllerAddr);
2091
+ }
2092
+ const header = ElectMasterRequestHeader.ofAdminTrigger(clusterName, brokerName, brokerId);
2093
+ const budget = (timeoutMillis != null && timeoutMillis > 0)
2094
+ ? timeoutMillis : ControllerInvokeTimeoutMillis;
2095
+ const response = await this._invokeRaw(leaderAddress, RequestCode.CONTROLLER_ELECT_MASTER, adminExt(header.toExtFields()), null, budget);
2096
+ if (response.code !== ResponseCode.SUCCESS) {
2097
+ throw new MQBrokerException(response.code, response.remark || `CODE: ${response.code}`, leaderAddress);
2098
+ }
2099
+ const responseHeader = new ElectMasterResponseHeader();
2100
+ responseHeader.fromExtFields(response.extFields || {});
2101
+ const body = response.body;
2102
+ const memberGroup = (body != null && body.length > 0)
2103
+ ? BrokerMemberGroup.decode(body) : new BrokerMemberGroup();
2104
+ return [responseHeader, memberGroup];
2105
+ }
2106
+ // GET_BROKER_EPOCH_CACHE(1007): the commitlog epoch list, plain broker
2107
+ // address, 3000 ms budget.
2108
+ async getBrokerEpochCache(brokerAddr, timeoutMillis) {
2109
+ const budget = (timeoutMillis != null && timeoutMillis > 0)
2110
+ ? timeoutMillis : ControllerInvokeTimeoutMillis;
2111
+ const response = await this._invokeChecked(brokerAddr, RequestCode.GET_BROKER_EPOCH_CACHE, null, null, budget);
2112
+ const body = response.body;
2113
+ if (body == null || body.length === 0) {
2114
+ throw new MQBrokerException(response.code, 'empty epoch cache body', brokerAddr);
2115
+ }
2116
+ return EpochEntryCache.decode(body);
2117
+ }
2118
+ // SWITCH_TIMER_ENGINE(5001): the engine type rides in the single ext field
2119
+ // E_T (MessageConst.TIMER_ENGINE_TYPE) — no header class for it.
2120
+ async switchTimerEngine(brokerAddr, engineType, timeoutMillis) {
2121
+ await this._invokeChecked(brokerAddr, RequestCode.SWITCH_TIMER_ENGINE, { [TIMER_ENGINE_TYPE_KEY]: engineType }, null, timeoutMillis);
2122
+ }
2123
+ // ---------------- Cold-data flow control ----------------
2124
+ // UPDATE_COLD_DATA_FLOW_CTR_CONFIG(2001), body = Properties TEXT on the fast
2125
+ // channel. An empty property set sends NOTHING.
2126
+ async updateColdDataFlowCtrGroupConfig(brokerAddr, properties, timeoutMillis) {
2127
+ const text = propertiesToString(properties);
2128
+ if (!text)
2129
+ return;
2130
+ await this._invokeBroker(brokerAddr, RequestCode.UPDATE_COLD_DATA_FLOW_CTR_CONFIG, null, Buffer.from(text, 'utf8'), timeoutMillis);
2131
+ }
2132
+ // REMOVE_COLD_DATA_FLOW_CTR_CONFIG(2002): the body is the group name
2133
+ // VERBATIM, not JSON. An empty group sends nothing.
2134
+ async removeColdDataFlowCtrGroupConfig(brokerAddr, consumerGroup, timeoutMillis) {
2135
+ if (consumerGroup == null || consumerGroup.length === 0)
2136
+ return;
2137
+ await this._invokeBroker(brokerAddr, RequestCode.REMOVE_COLD_DATA_FLOW_CTR_CONFIG, null, Buffer.from(consumerGroup, 'utf8'), timeoutMillis);
2138
+ }
2139
+ // GET_COLD_DATA_FLOW_CTR_INFO(2003) — the raw body text, null when the
2140
+ // broker answers without one. This read is dialled at the listening port
2141
+ // even when the VIP switch is on.
2142
+ async getColdDataFlowCtrInfo(brokerAddr, timeoutMillis) {
2143
+ const response = await this._invokeChecked(brokerAddr, RequestCode.GET_COLD_DATA_FLOW_CTR_INFO, null, null, timeoutMillis);
2144
+ const body = response.body;
2145
+ if (body != null && body.length > 0)
2146
+ return body.toString('utf8');
2147
+ return null;
2148
+ }
2149
+ // ---------------- CommitLog read-ahead ----------------
2150
+ // SET_COMMITLOG_READ_MODE(2004). The request has no header class — the
2151
+ // single field is written by hand — and the broker's answer rides the
2152
+ // response REMARK, not a body. Never translated to the VIP port.
2153
+ async setCommitLogReadAheadMode(brokerAddr, mode, timeoutMillis) {
2154
+ const response = await this._invokeChecked(brokerAddr, RequestCode.SET_COMMITLOG_READ_MODE, { [FILE_READAHEAD_MODE_KEY]: mode }, null, timeoutMillis);
2155
+ if (response.remark != null && response.remark.length > 0)
2156
+ return response.remark;
2157
+ return null;
2158
+ }
2159
+ // ---------------- Pop record export ----------------
2160
+ // POP_ROLLBACK(200056) asks the broker to dump its pop records. The
2161
+ // CALLER's timeout is forwarded verbatim — the admin default does not apply
2162
+ // here — and the address is dialled as passed.
2163
+ async exportPopRecords(brokerAddr, timeout) {
2164
+ const client = this._requireClient();
2165
+ const response = await client.remotingClient.invokeSync(brokerAddr, this._request(RequestCode.POP_ROLLBACK, null), timeout);
2166
+ if (response.code !== ResponseCode.SUCCESS) {
2167
+ throw new MQBrokerException(response.code, response.remark || `CODE: ${response.code}`, brokerAddr);
2168
+ }
2169
+ }
2170
+ // ---------------- RocksDB consume-queue ops ----------------
2171
+ // CHECK_ROCKSDB_CQ_WRITE_PROGRESS(354). checkStoreTime is a primitive header
2172
+ // field and is ALWAYS sent — including 0 — and the request is never
2173
+ // translated to the VIP port.
2174
+ async checkRocksdbCqWriteProgress(brokerAddr, topic, checkStoreTime, timeoutMillis) {
2175
+ const response = await this._invokeChecked(brokerAddr, RequestCode.CHECK_ROCKSDB_CQ_WRITE_PROGRESS, adminExt({ topic, checkStoreTime }), null, timeoutMillis);
2176
+ const body = response.body;
2177
+ if (body == null || body.length === 0) {
2178
+ throw new MQClientException(`empty checkRocksdbCqWriteProgress body from ${brokerAddr}`);
2179
+ }
2180
+ return CheckRocksdbCqWriteResult.decode(body);
2181
+ }
2182
+ // EXPORT_ROCKSDB_CONFIG_TO_JSON(355): the requested config types ride the
2183
+ // header as lowercase names, each followed by the ';' separator. Code-only:
2184
+ // the broker answers without a body.
2185
+ async exportRocksDBConfigToJson(brokerAddr, configType, timeoutMillis) {
2186
+ const value = configType.map((t) => `${t.toLowerCase()};`).join('');
2187
+ await this._invokeChecked(brokerAddr, RequestCode.EXPORT_ROCKSDB_CONFIG_TO_JSON, { configType: value }, null, timeoutMillis);
2188
+ }
2189
+ // ---------------- Lite topic admin ----------------
2190
+ // Shared leaf of the lite-admin RPCs: VIP translate, raise a broker
2191
+ // exception on non-SUCCESS, and decode the body only when one actually
2192
+ // arrived (a code-only answer decodes as null, not as an empty object).
2193
+ async _invokeBrokerMethod(brokerAddr, code, ext, decode, timeoutMillis) {
2194
+ const response = await this._invokeBrokerRaw(brokerAddr, code, ext, null, timeoutMillis);
2195
+ if (response.code !== ResponseCode.SUCCESS) {
2196
+ throw new MQBrokerException(response.code, response.remark || `CODE: ${response.code}`, brokerAddr);
2197
+ }
2198
+ const body = response.body;
2199
+ if (body == null || body.length === 0 || decode == null)
2200
+ return null;
2201
+ return decode(body);
2202
+ }
2203
+ // GET_BROKER_LITE_INFO(200074): broker-level lite-store summary. No header
2204
+ // fields at all.
2205
+ async getBrokerLiteInfo(brokerAddr, timeoutMillis) {
2206
+ return this._invokeBrokerMethod(brokerAddr, RequestCode.GET_BROKER_LITE_INFO, null, GetBrokerLiteInfoResponseBody.decode, timeoutMillis);
2207
+ }
2208
+ // GET_PARENT_TOPIC_INFO(200075): one parent topic's lite-store summary.
2209
+ async getParentTopicInfo(brokerAddr, topic, timeoutMillis) {
2210
+ return this._invokeBrokerMethod(brokerAddr, RequestCode.GET_PARENT_TOPIC_INFO, authExt({ topic }), GetParentTopicInfoResponseBody.decode, timeoutMillis);
2211
+ }
2212
+ // GET_LITE_TOPIC_INFO(200076): one lite topic's detail; liteTopic is
2213
+ // optional and an unset value is OMITTED, not sent empty.
2214
+ async getLiteTopicInfo(brokerAddr, parentTopic, liteTopic, timeoutMillis) {
2215
+ return this._invokeBrokerMethod(brokerAddr, RequestCode.GET_LITE_TOPIC_INFO, authExt({ parentTopic, liteTopic }), GetLiteTopicInfoResponseBody.decode, timeoutMillis);
2216
+ }
2217
+ // GET_LITE_CLIENT_INFO(200077): one client's lite view. maxCount is a
2218
+ // primitive header field and rides along ALWAYS (even 0).
2219
+ async getLiteClientInfo(brokerAddr, parentTopic, group, clientId, timeoutMillis) {
2220
+ return this._invokeBrokerMethod(brokerAddr, RequestCode.GET_LITE_CLIENT_INFO, authExt({ parentTopic, group, clientId, maxCount: 1000 }), GetLiteClientInfoResponseBody.decode, timeoutMillis);
2221
+ }
2222
+ // GET_LITE_GROUP_INFO(200078): one group's lite lag view. topK is a
2223
+ // primitive and rides along ALWAYS; liteTopic is optional and omitted when
2224
+ // unset.
2225
+ async getLiteGroupInfo(brokerAddr, group, liteTopic, topK, timeoutMillis) {
2226
+ return this._invokeBrokerMethod(brokerAddr, RequestCode.GET_LITE_GROUP_INFO, authExt({ group, liteTopic, topK }), GetLiteGroupInfoResponseBody.decode, timeoutMillis);
2227
+ }
2228
+ // TRIGGER_LITE_DISPATCH(200079): fire-and-forget dispatch nudge, no body.
2229
+ async triggerLiteDispatch(brokerAddr, group, clientId, timeoutMillis) {
2230
+ await this._invokeBrokerMethod(brokerAddr, RequestCode.TRIGGER_LITE_DISPATCH, authExt({ group, clientId }), null, timeoutMillis);
2231
+ }
2232
+ // ---------------- POP assignment ----------------
2233
+ // SET_MESSAGE_REQUEST_MODE(401), body = MessageRequestModeRequestBody JSON.
2234
+ async setMessageRequestMode(brokerAddr, topic, consumerGroup, mode, popShareQueueNum, timeoutMillis) {
2235
+ await this._invokeBroker(brokerAddr, RequestCode.SET_MESSAGE_REQUEST_MODE, null, RemotingSerializable.encode({
2236
+ topic, consumerGroup, mode, popShareQueueNum,
2237
+ }), timeoutMillis);
2238
+ }
2239
+ // ---------------- Auth user / ACL management ----------------
2240
+ // createUser / updateUser: AUTH_CREATE_USER(3001) / AUTH_UPDATE_USER(3002).
2241
+ // Header carries `username`; the body is the user object. Either hand over a
2242
+ // UserInfo or the (username, password, userType) triple — the triple leaves
2243
+ // userStatus unset, so that key is ABSENT from the body.
2244
+ async createUser(addr, user, password = null, userType = null, timeoutMillis) {
2245
+ await this._invokeUser(RequestCode.AUTH_CREATE_USER, addr, user, password, userType, timeoutMillis);
2246
+ }
2247
+ async updateUser(addr, user, password = null, userType = null, timeoutMillis) {
2248
+ await this._invokeUser(RequestCode.AUTH_UPDATE_USER, addr, user, password, userType, timeoutMillis);
2249
+ }
2250
+ // AUTH_DELETE_USER(3003): header `username`, no body.
2251
+ async deleteUser(addr, username, timeoutMillis) {
2252
+ await this._invokeChecked(addr, RequestCode.AUTH_DELETE_USER, authExt({ username }), null, timeoutMillis);
2253
+ }
2254
+ // AUTH_GET_USER(3004): header `username`; the body is one user object, an
2255
+ // empty body being no user at all.
2256
+ async getUser(addr, username, timeoutMillis) {
2257
+ const response = await this._invokeChecked(addr, RequestCode.AUTH_GET_USER, authExt({ username }), null, timeoutMillis);
2258
+ const body = response.body;
2259
+ if (body == null || body.length === 0)
2260
+ return null;
2261
+ return UserInfo.decode(body);
2262
+ }
2263
+ // AUTH_LIST_USER(3005): header `filter` (unset means no filter header at all);
2264
+ // the body is a JSON array of user objects.
2265
+ async listUser(addr, filter = null, timeoutMillis) {
2266
+ const response = await this._invokeChecked(addr, RequestCode.AUTH_LIST_USER, authExt({ filter }), null, timeoutMillis);
2267
+ const body = response.body;
2268
+ if (body == null || body.length === 0)
2269
+ return [];
2270
+ return UserInfo.decodeList(body);
2271
+ }
2272
+ // createAcl / updateAcl: AUTH_CREATE_ACL(3006) / AUTH_UPDATE_ACL(3007).
2273
+ // Header carries `subject`; the body is the acl object. Either hand over an
2274
+ // AclInfo or the (subject, resources, actions, sourceIps, decision) triple,
2275
+ // which builds one policy with policyType ABSENT and one entry per resource.
2276
+ async createAcl(addr, acl, resources = null, actions = null, sourceIps = null, decision = null, timeoutMillis) {
2277
+ await this._invokeAcl(RequestCode.AUTH_CREATE_ACL, addr, acl, resources, actions, sourceIps, decision, timeoutMillis);
2278
+ }
2279
+ async updateAcl(addr, acl, resources = null, actions = null, sourceIps = null, decision = null, timeoutMillis) {
2280
+ await this._invokeAcl(RequestCode.AUTH_UPDATE_ACL, addr, acl, resources, actions, sourceIps, decision, timeoutMillis);
2281
+ }
2282
+ // AUTH_DELETE_ACL(3008): headers `subject` + `resource` and NOTHING else —
2283
+ // the policy shape is not part of this request. An unset resource is
2284
+ // omitted, which asks for the subject's whole grant set.
2285
+ async deleteAcl(addr, subject, resource = null, timeoutMillis) {
2286
+ await this._invokeChecked(addr, RequestCode.AUTH_DELETE_ACL, authExt({ subject, resource }), null, timeoutMillis);
2287
+ }
2288
+ // AUTH_GET_ACL(3009): header `subject`; the body is one acl object.
2289
+ async getAcl(addr, subject, timeoutMillis) {
2290
+ const response = await this._invokeChecked(addr, RequestCode.AUTH_GET_ACL, authExt({ subject }), null, timeoutMillis);
2291
+ const body = response.body;
2292
+ if (body == null || body.length === 0)
2293
+ return null;
2294
+ return AclInfo.decode(body);
2295
+ }
2296
+ // AUTH_LIST_ACL(3010): headers `subjectFilter` + `resourceFilter`; the body is
2297
+ // a JSON array of acl objects.
2298
+ async listAcl(addr, subjectFilter = null, resourceFilter = null, timeoutMillis) {
2299
+ const response = await this._invokeChecked(addr, RequestCode.AUTH_LIST_ACL, authExt({ subjectFilter, resourceFilter }), null, timeoutMillis);
2300
+ const body = response.body;
2301
+ if (body == null || body.length === 0)
2302
+ return [];
2303
+ return AclInfo.decodeList(body);
2304
+ }
2305
+ // QUERY_CONSUME_QUEUE(321) — the consume-queue slice the broker serves for a
2306
+ // topic/queue, optionally narrowed by the group's subscription.
2307
+ async queryConsumeQueue(brokerAddr, topic, queueId, index, count = 32, consumerGroup = '', timeoutMillis) {
2308
+ const response = await this._invokeBroker(brokerAddr, RequestCode.QUERY_CONSUME_QUEUE, adminExt({ topic, queueId, index, count, consumerGroup }), null, timeoutMillis);
2309
+ const body = response.body;
2310
+ if (body == null || body.length === 0)
2311
+ return new QueryConsumeQueueResponseBody();
2312
+ return QueryConsumeQueueResponseBody.decode(body);
2313
+ }
2314
+ // UPDATE_AND_GET_GROUP_FORBIDDEN(353) — flips (readable=null: query-only) a
2315
+ // group's read permission on one broker and returns the stored entry. An
2316
+ // unset readable is omitted from the request entirely: the broker treats
2317
+ // "absent" and "false" as different operations.
2318
+ async updateAndGetGroupReadForbidden(brokerAddr, group, topic, readable, timeoutMillis) {
2319
+ if (!group || !topic) {
2320
+ throw new MQClientException('updateAndGetGroupReadForbidden: group/topic required');
2321
+ }
2322
+ const ext = { group, topic };
2323
+ if (readable != null)
2324
+ ext['readable'] = readable ? 'true' : 'false';
2325
+ const response = await this._invokeBroker(brokerAddr, RequestCode.UPDATE_AND_GET_GROUP_FORBIDDEN, ext, null, timeoutMillis);
2326
+ const body = response.body;
2327
+ if (body == null || body.length === 0) {
2328
+ throw new MQClientException('updateAndGetGroupReadForbidden: empty response body');
2329
+ }
2330
+ return GroupForbidden.decode(body);
2331
+ }
2332
+ // Whether the group has consumed past the message: only the consumer offset
2333
+ // of the message's own queue on its own master counts.
2334
+ async _consumed(msg, group) {
2335
+ const cstats = await this.examineConsumeStats(group);
2336
+ const clusterInfo = await this.examineBrokerClusterInfo();
2337
+ for (const [mq, wrapper] of cstats.offsetTable) {
2338
+ if (mq.topic !== msg.topic || mq.queueId !== msg.queueId)
2339
+ continue;
2340
+ const brokerAddrs = clusterInfo.brokerAddrTable[mq.brokerName];
2341
+ if (brokerAddrs == null)
2342
+ continue;
2343
+ const addr = brokerAddrs[MixAll.MASTER_ID];
2344
+ const storeHost = msg.getStoreHostString();
2345
+ if (addr != null && storeHost != null && addr === storeHost
2346
+ && wrapper.consumerOffset > msg.getQueueOffset()) {
2347
+ return true;
2348
+ }
2349
+ }
2350
+ return false;
2351
+ }
2352
+ // Who consumes this topic, and what happened to the message in each group:
2353
+ // CONSUMED / CONSUMED_BUT_FILTERED / PULL / NOT_CONSUME_YET / NOT_ONLINE /
2354
+ // CONSUME_BROADCASTING. A group whose probing fails carries the failure in
2355
+ // exceptionDesc and stays UNKNOWN. Groups are sorted for deterministic output.
2356
+ async messageTrackDetail(msg) {
2357
+ const groups = await this.queryTopicConsumeByWho(msg.topic);
2358
+ const names = (groups != null && groups.groupList != null) ? groups.groupList.slice() : [];
2359
+ names.sort();
2360
+ const result = [];
2361
+ for (const group of names) {
2362
+ result.push(await this._trackOneGroup(msg, group));
2363
+ }
2364
+ return result;
2365
+ }
2366
+ // Concurrent flavor: every group's verdict is probed in parallel. The group
2367
+ // list is NOT sorted here — each track lands as its probe completes the
2368
+ // shared walk order, and the verdict logic is the per-group leaf above.
2369
+ async messageTrackDetailConcurrent(msg) {
2370
+ const groups = await this.queryTopicConsumeByWho(msg.topic);
2371
+ const names = (groups != null && groups.groupList != null) ? groups.groupList.slice() : [];
2372
+ return Promise.all(names.map((group) => this._trackOneGroup(msg, group)));
2373
+ }
2374
+ // One group's leg of the message track: connection probe, pull/passive
2375
+ // split, consumed check, tag-filter verdict.
2376
+ async _trackOneGroup(msg, group) {
2377
+ const mt = new MessageTrack(group, TrackType.UNKNOWN);
2378
+ let cc;
2379
+ try {
2380
+ cc = await this.examineConsumerConnectionInfo(group);
2381
+ }
2382
+ catch (e) {
2383
+ this._fillTrackFailure(mt, e);
2384
+ return mt;
2385
+ }
2386
+ if (cc.consumeType === ConsumeType.CONSUME_ACTIVELY) {
2387
+ mt.trackType = TrackType.PULL;
2388
+ return mt;
2389
+ }
2390
+ if (cc.consumeType !== ConsumeType.CONSUME_PASSIVELY) {
2391
+ return mt;
2392
+ }
2393
+ let ifConsumed = false;
2394
+ try {
2395
+ ifConsumed = await this._consumed(msg, group);
2396
+ }
2397
+ catch (e) {
2398
+ this._fillTrackFailure(mt, e);
2399
+ return mt;
2400
+ }
2401
+ if (ifConsumed) {
2402
+ mt.trackType = TrackType.CONSUMED;
2403
+ // A narrower subscription than the message's tag means this group
2404
+ // receives-but-filters: SQL92 subscriptions keep an empty tagsSet and
2405
+ // fall back to CONSUMED.
2406
+ const sub = cc.subscriptionTable[msg.topic];
2407
+ const tagsSet = (sub != null && Array.isArray(sub['tagsSet']))
2408
+ ? sub['tagsSet'].map((t) => String(t)) : [];
2409
+ const tag = msg.getTags();
2410
+ if (tagsSet.length > 0 && !tagsSet.includes('*') && !tagsSet.includes(String(tag))) {
2411
+ mt.trackType = TrackType.CONSUMED_BUT_FILTERED;
2412
+ }
2413
+ }
2414
+ else {
2415
+ mt.trackType = TrackType.NOT_CONSUME_YET;
2416
+ }
2417
+ return mt;
2418
+ }
2419
+ _fillTrackFailure(mt, e) {
2420
+ const err = e;
2421
+ const code = err?.responseCode;
2422
+ if (code === ResponseCode.CONSUMER_NOT_ONLINE)
2423
+ mt.trackType = TrackType.NOT_ONLINE;
2424
+ else if (code === ResponseCode.BROADCAST_CONSUMPTION)
2425
+ mt.trackType = TrackType.CONSUME_BROADCASTING;
2426
+ mt.exceptionDesc = code != null
2427
+ ? `CODE:${code} DESC:${err?.message ?? ''}`
2428
+ : (err?.message ?? String(e));
2429
+ }
2430
+ async _invokeUser(code, addr, user, password, userType, timeoutMillis) {
2431
+ const info = typeof user === 'string' ? new UserInfo(user, password, userType, null) : user;
2432
+ await this._invokeChecked(addr, code, authExt({ username: info.username }), info.encode(), timeoutMillis);
2433
+ }
2434
+ async _invokeAcl(code, addr, acl, resources, actions, sourceIps, decision, timeoutMillis) {
2435
+ const info = typeof acl === 'string'
2436
+ ? AclInfo.fromResources(acl, resources, actions, sourceIps, decision)
2437
+ : acl;
2438
+ await this._invokeChecked(addr, code, authExt({ subject: info.subject }), info.encode(), timeoutMillis);
2439
+ }
2440
+ }
2441
+ export default DefaultMQAdminExt;
2442
+ // Track verdicts for messageTrackDetail. Kept as string constants so the
2443
+ // values travel unchanged through JSON.
2444
+ export const TrackType = {
2445
+ CONSUMED: 'CONSUMED',
2446
+ CONSUMED_BUT_FILTERED: 'CONSUMED_BUT_FILTERED',
2447
+ PULL: 'PULL',
2448
+ NOT_CONSUME_YET: 'NOT_CONSUME_YET',
2449
+ NOT_ONLINE: 'NOT_ONLINE',
2450
+ CONSUME_BROADCASTING: 'CONSUME_BROADCASTING',
2451
+ UNKNOWN: 'UNKNOWN',
2452
+ };
2453
+ export class MessageTrack {
2454
+ consumerGroup;
2455
+ trackType;
2456
+ exceptionDesc;
2457
+ constructor(consumerGroup, trackType = TrackType.UNKNOWN) {
2458
+ this.consumerGroup = consumerGroup;
2459
+ this.trackType = trackType;
2460
+ this.exceptionDesc = null;
2461
+ }
2462
+ }