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,1774 @@
1
+ // -*- coding: utf-8 -*-
2
+ // DefaultMQProducer — the default message producer, built on top of MQClient.
3
+ //
4
+ // Send ordering:
5
+ // send() -> _send_default_impl (retry loop) -> _send_with_hooks (SendMessageHook before/after)
6
+ // -> sendKernelImpl (compress -> set UNIQ_KEY -> CheckForbiddenHook [NOT swallowed]
7
+ // -> build request -> resolve broker addr -> actual transport via MQClient).
8
+ import { RemotingClient } from "../remoting/client.js";
9
+ import { RemotingCommand } from "../remoting/remotingCommand.js";
10
+ import { RequestCode, ResponseCode } from "../remoting/codes.js";
11
+ import { EndTransactionRequestHeader } from "../remoting/headers.js";
12
+ import { MessageQueue, MessageBatch } from "../common/message.js";
13
+ import { MessageSysFlag } from "../common/sysflag.js";
14
+ import { compressionTypeByName, compressFor } from "../common/compress.js";
15
+ import { decodeMessageId } from "../common/messageDecoder.js";
16
+ import { MessageConst } from "../common/messageConst.js";
17
+ import { MessageAccessor } from "../common/message_accessor.js";
18
+ import { MessageType } from "../common/messageType.js";
19
+ import { MixAll } from "../common/mixAll.js";
20
+ import { NamespaceUtil } from "../remoting/namespace.js";
21
+ import { createUniqID } from "../common/messageClientIdSetter.js";
22
+ import { TopicValidator } from "../common/topic_validator.js";
23
+ import { Validators } from "../common/validators.js";
24
+ import { RecallMessageHandle } from "../common/recall_message_handle.js";
25
+ import { registerRpcHooks } from "../remoting/rpc_hooks.js";
26
+ import { getLogger } from "../logging.js";
27
+ import { MQClientException, MQBrokerException, RemotingTooMuchRequestException, } from "../remoting/exception.js";
28
+ import { TransactionSendResult, SendStatus } from "./send_result.js";
29
+ import { MQClient, DEFAULT_CONCURRENT_HEARTBEAT_THREAD_POOL_SIZE } from "./mq_client.js";
30
+ import { MQClientManager } from "./mq_client_manager.js";
31
+ import { CommunicationMode, SendMessageContext, CheckForbiddenContext, EndTransactionContext, hookRegistry, } from "./hook.js";
32
+ import { MQFaultStrategy, tcpDetect } from "./latency.js";
33
+ import { getOrCreateProduceAccumulator, } from "./produce_accumulator.js";
34
+ import { FairSemaphore, MIN_ASYNC_SEND_NUM, MIN_ASYNC_SEND_SIZE } from "./backpressure.js";
35
+ import { injectTraceContext, traceContextEnabledFromEnv } from "./traceparent.js";
36
+ import { AccessChannelLocal, AccessChannelCloud, } from "./trace_context.js";
37
+ import { createCorrelationId, createReplyMessage, REQUEST_FUTURE_HOLDER, RequestResponseFuture, } from "./request_reply.js";
38
+ const logger = getLogger('producer');
39
+ // Permit floor: a configured value at or below the floor is raised to the floor, and the log
40
+ // line still fires when it is exactly the floor.
41
+ function backPressurePermits(configured, floor, name) {
42
+ if (configured > floor)
43
+ return configured;
44
+ logger.info('%s can not be smaller than %d.', name, floor);
45
+ return floor;
46
+ }
47
+ // ---- selector / callback / transaction types ----
48
+ export class MessageQueueSelector {
49
+ }
50
+ export class SelectMessageQueueByHash extends MessageQueueSelector {
51
+ select(_msgs, mqList, arg) {
52
+ if (mqList == null || mqList.length === 0)
53
+ return null;
54
+ const s = arg != null ? String(arg) : '';
55
+ let hash = 0;
56
+ for (let i = 0; i < s.length; i++)
57
+ hash = (Math.imul(31, hash) + s.charCodeAt(i)) | 0;
58
+ if (hash < 0)
59
+ hash = -hash;
60
+ return mqList[hash % mqList.length];
61
+ }
62
+ }
63
+ export class SelectMessageQueueByRandom extends MessageQueueSelector {
64
+ select(_msgs, mqList, _arg) {
65
+ if (mqList == null || mqList.length === 0)
66
+ return null;
67
+ const idx = Math.floor(Math.random() * mqList.length);
68
+ return mqList[idx];
69
+ }
70
+ }
71
+ // SelectMessageQueueByMachineRoom picks the first queue whose brokerName
72
+ // starts with arg (broker names are expected to be "<idc>-<broker>"); with no
73
+ // match it falls back to the first queue.
74
+ export class SelectMessageQueueByMachineRoom extends MessageQueueSelector {
75
+ select(_msgs, mqList, arg) {
76
+ if (mqList == null || mqList.length === 0)
77
+ return null;
78
+ const room = arg != null ? String(arg) : '';
79
+ for (const mq of mqList) {
80
+ if (mq.getBrokerName().startsWith(room))
81
+ return mq;
82
+ }
83
+ return mqList[0];
84
+ }
85
+ }
86
+ export const LocalTransactionState = {
87
+ COMMIT_MESSAGE: 0,
88
+ ROLLBACK_MESSAGE: 1,
89
+ UNKNOW: 2,
90
+ };
91
+ // Java-parity MessageType *name* classification for the send context, mirroring
92
+ // go's producer.go: a prepared transaction flag makes TRANS_MSG_HALF, any of
93
+ // the delay-class properties makes DELAY_MSG, otherwise NORMAL_MSG.
94
+ const sendDelayClassProperties = [
95
+ MessageConst.PROPERTY_DELAY_TIME_LEVEL,
96
+ MessageConst.PROPERTY_TIMER_DELIVER_MS,
97
+ MessageConst.PROPERTY_TIMER_DELAY_SEC,
98
+ MessageConst.PROPERTY_TIMER_DELAY_MS,
99
+ ];
100
+ export function classifySendMessageType(msg) {
101
+ if (msg.getProperty(MessageConst.PROPERTY_TRANSACTION_PREPARED) === 'true') {
102
+ return 'TRANS_MSG_HALF';
103
+ }
104
+ for (const key of sendDelayClassProperties) {
105
+ if (msg.getProperty(key) != null)
106
+ return 'DELAY_MSG';
107
+ }
108
+ return 'NORMAL_MSG';
109
+ }
110
+ export class TransactionListener {
111
+ // Returns the local transaction state after executing the local transaction.
112
+ executeLocalTransaction(msg, arg) { void msg; void arg; return LocalTransactionState.UNKNOW; }
113
+ // Called by the broker to check the state of a half message.
114
+ checkLocalTransaction(msg) { void msg; return LocalTransactionState.UNKNOW; }
115
+ }
116
+ export class DefaultMQProducer {
117
+ producerGroup;
118
+ createTopicKey;
119
+ defaultTopicQueueNums;
120
+ sendMsgTimeout;
121
+ // Cap on a single attempt's timeout while retries remain (-1 = no cap). Every
122
+ // attempt draws on the caller's one budget, so without a cap a slow first
123
+ // broker can consume all of it and leave the retry no time to answer.
124
+ sendMsgMaxTimeoutPerRequest;
125
+ compressMsgBodyOverHowmuch;
126
+ maxMessageSize;
127
+ retryTimesWhenSendFailed;
128
+ namespace;
129
+ // Server-side namespaceV2 (Aliyun-style serverless instance id). When
130
+ // non-empty, NamespaceRpcHook stamps every request with nsd=true / ns=<value>
131
+ // extFields. Independent from `namespace`, which rewrites resource names on
132
+ // the client side.
133
+ namespaceV2;
134
+ namesrvAddr;
135
+ tlsEnable;
136
+ // TLS details (see setTlsOptions); null = test-mode (trust the self-signed cert).
137
+ tlsOptions;
138
+ socksProxyConfig;
139
+ rpcHook;
140
+ // Java ClientConfig#accessChannel (default LOCAL). CLOUD routes trace records to
141
+ // rmq_sys_TRACE_DATA_<regionId>; handed to the trace dispatcher at start().
142
+ accessChannel;
143
+ unitName;
144
+ instanceName;
145
+ sendLatencyFaultEnable;
146
+ heartbeatIntervalMillis;
147
+ autoBatch;
148
+ // Auto-batch accumulator knobs, pushed into the accumulator when start()
149
+ // wires it: a buffered key flushes when its group reaches batchMaxBytes or
150
+ // when the batchMaxDelayMs hold timer drains it. -1 means "leave the
151
+ // accumulator's own default" (32 KB / 10 ms).
152
+ batchMaxBytes;
153
+ batchMaxDelayMs;
154
+ // Broker response codes that make a SYNC send worth another attempt on a
155
+ // DIFFERENT broker: the answer says the broker itself could not take the
156
+ // message (busy / internal error), so the next broker may well succeed. Any
157
+ // other broker answer (a rejected message, a permission failure...) is the
158
+ // caller's to see, so it is raised on the first attempt.
159
+ retryResponseCodes;
160
+ // When a send was accepted but the store answer is not SEND_OK (the disk or
161
+ // the slave lagged), re-pick another broker and retry; with the flag off the
162
+ // not-stored result goes back to the caller unchanged. SYNC only.
163
+ retryAnotherBrokerWhenNotStoreOK;
164
+ enableTrace;
165
+ // Custom trace topic; null = RMQ_SYS_TRACE_TOPIC.
166
+ traceTopic;
167
+ // Compression algorithm for oversized bodies: MessageSysFlag type value
168
+ // (1=LZ4, 2=ZSTD, 3=ZLIB). Default ZLIB for backward compatibility with
169
+ // older brokers. Select via setCompressType('ZLIB'|'LZ4'|'ZSTD').
170
+ compressType;
171
+ // When true, send rpcs target the broker's VIP port (listen port - 2).
172
+ // Default false.
173
+ sendMessageWithVIPChannel;
174
+ // ASYNC retry budget (default 2): the first attempt is NOT counted — a value
175
+ // of 2 means up to 3 total attempts. Deliberately a SEPARATE knob from
176
+ // retryTimesWhenSendFailed: the async chain never reads the sync one.
177
+ retryTimesWhenSendAsyncFailed;
178
+ // ZLIB compression level for oversized bodies, 0-9 (default 5).
179
+ // LZ4/ZSTD ignore it.
180
+ compressLevel;
181
+ // Arms the MQFaultStrategy probe thread that re-checks isolated brokers.
182
+ // Default false.
183
+ startDetectorEnable;
184
+ // W3C traceparent passthrough switch. Independent of enableTrace, seeded
185
+ // from ROCKETMQ_TRACE_CONTEXT_ENABLE.
186
+ enableTraceContext;
187
+ // Heartbeat V2: off by default, so heartbeats keep the V1 shape unless enabled.
188
+ useHeartbeatV2;
189
+ // Materialise received message bodies during batch decode (default true).
190
+ decodeReadBody;
191
+ // Internal-API timeout (heartbeat, route fetch, check client, unregister and
192
+ // consumer-list RPCs), in milliseconds. Default 3000.
193
+ mqClientApiTimeout;
194
+ // Concurrent heartbeat round (ignored while heartbeat V2 is on).
195
+ enableConcurrentHeartbeat;
196
+ concurrentHeartbeatThreadPoolSize;
197
+ // Page size of the paged metadata admin calls (maxTopicNum / maxGroupNum).
198
+ maxPageSizeInGetMetadata;
199
+ client;
200
+ mqFaultStrategy;
201
+ producerClientId;
202
+ // Message-trace dispatcher — created in start()
203
+ // only when enableTrace is on, torn down in shutdown().
204
+ _traceDispatcher;
205
+ _traceHook;
206
+ _recallTraceHook;
207
+ _accumulator;
208
+ // Async-send backpressure: two fair semaphores gate the pending byte and
209
+ // message counts. The gate is enableBackpressureForAsyncMode (default false)
210
+ // — with the gate off the semaphores are never consulted.
211
+ enableBackpressureForAsyncMode;
212
+ backPressureForAsyncSendNum;
213
+ backPressureForAsyncSendSize;
214
+ _semaphoreAsyncSendNum;
215
+ _semaphoreAsyncSendSize;
216
+ constructor(producerGroup = MixAll.DEFAULT_PRODUCER_GROUP) {
217
+ this.producerGroup = producerGroup;
218
+ this.createTopicKey = MixAll.DEFAULT_TOPIC;
219
+ this.defaultTopicQueueNums = MixAll.DEFAULT_TOPIC_QUEUE_NUMS;
220
+ this.sendMsgTimeout = 3000;
221
+ this.sendMsgMaxTimeoutPerRequest = -1;
222
+ this.compressMsgBodyOverHowmuch = 4 * 1024;
223
+ this.maxMessageSize = 4 * 1024 * 1024;
224
+ this.retryTimesWhenSendFailed = 2;
225
+ this.retryAnotherBrokerWhenNotStoreOK = false;
226
+ this.retryResponseCodes = new Set([
227
+ ResponseCode.SYSTEM_ERROR, ResponseCode.SYSTEM_BUSY,
228
+ ResponseCode.SERVICE_NOT_AVAILABLE, ResponseCode.NO_PERMISSION,
229
+ ResponseCode.TOPIC_NOT_EXIST, ResponseCode.NO_BUYER_ID,
230
+ ResponseCode.NOT_IN_CURRENT_UNIT, ResponseCode.GO_AWAY,
231
+ ]);
232
+ this.namespace = null;
233
+ this.namespaceV2 = null;
234
+ this.namesrvAddr = null;
235
+ this.tlsEnable = false;
236
+ this.tlsOptions = null;
237
+ this.socksProxyConfig = null;
238
+ this.rpcHook = null;
239
+ this.accessChannel = AccessChannelLocal;
240
+ this.unitName = null;
241
+ this.instanceName = MixAll.DEFAULT_INSTANCE_NAME;
242
+ this.sendLatencyFaultEnable = false;
243
+ this.heartbeatIntervalMillis = 30 * 1000;
244
+ this.autoBatch = false;
245
+ this.batchMaxBytes = -1;
246
+ this.batchMaxDelayMs = -1;
247
+ this.enableTrace = false;
248
+ this.traceTopic = null;
249
+ this.compressType = MessageSysFlag.ZLIB_TYPE;
250
+ this.sendMessageWithVIPChannel = false;
251
+ this.retryTimesWhenSendAsyncFailed = 2;
252
+ this.compressLevel = 5;
253
+ this.startDetectorEnable = false;
254
+ this.enableTraceContext = traceContextEnabledFromEnv();
255
+ this.useHeartbeatV2 = false;
256
+ this.decodeReadBody = true;
257
+ this.mqClientApiTimeout = 3000;
258
+ this.enableConcurrentHeartbeat = false;
259
+ this.concurrentHeartbeatThreadPoolSize = DEFAULT_CONCURRENT_HEARTBEAT_THREAD_POOL_SIZE;
260
+ this.maxPageSizeInGetMetadata = 2000;
261
+ this.client = null;
262
+ this.mqFaultStrategy = new MQFaultStrategy(this.sendLatencyFaultEnable);
263
+ this.producerClientId = this._buildClientId();
264
+ this._traceDispatcher = null;
265
+ this._traceHook = null;
266
+ this._recallTraceHook = null;
267
+ this._accumulator = null;
268
+ this.enableBackpressureForAsyncMode = false;
269
+ this.backPressureForAsyncSendNum = 1024;
270
+ this.backPressureForAsyncSendSize = 100 * 1024 * 1024;
271
+ this._semaphoreAsyncSendNum = new FairSemaphore(backPressurePermits(this.backPressureForAsyncSendNum, MIN_ASYNC_SEND_NUM, 'semaphoreAsyncSendNum'));
272
+ this._semaphoreAsyncSendSize = new FairSemaphore(backPressurePermits(this.backPressureForAsyncSendSize, MIN_ASYNC_SEND_SIZE, 'semaphoreAsyncSendSize'));
273
+ }
274
+ _buildClientId() {
275
+ const instanceName = MixAll.changeInstanceNameToPid(this.instanceName);
276
+ return MixAll.clientIdFor(instanceName, this.unitName);
277
+ }
278
+ // ---- namespace / topic helpers ----
279
+ _withNamespace(resource) {
280
+ if (!this.namespace)
281
+ return resource;
282
+ return NamespaceUtil.wrapNamespace(this.namespace, resource);
283
+ }
284
+ // Every public send-shaped entry applies the namespace to the message topic
285
+ // BEFORE validation, route lookup or the wire: with a namespace set the topic
286
+ // the broker stores is the prefixed one. Set in place, so the caller's Message
287
+ // keeps the wrapped name from then on (idempotent on re-entry).
288
+ _wrapMessageTopic(msg) {
289
+ msg.setTopic(this._withNamespace(msg.getTopic()));
290
+ }
291
+ _checkLegalTopic(topic) {
292
+ TopicValidator.validateTopic(topic);
293
+ if (TopicValidator.isNotAllowedSendTopic(topic)) {
294
+ throw new MQClientException(`topic[${topic}] is not allowed to send`);
295
+ }
296
+ }
297
+ // ---- lifecycle ----
298
+ // ---- fluent config (mirror the consumer) ----
299
+ setNamesrvAddr(addr) { this.namesrvAddr = addr; return this; }
300
+ setNamespace(ns) { this.namespace = ns; return this; }
301
+ // Server-side namespace accessor. The value is read live by the
302
+ // NamespaceRpcHook on every request, so setting it after start() takes
303
+ // effect too.
304
+ setNamespaceV2(ns) { this.namespaceV2 = ns; return this; }
305
+ getNamespaceV2() { return this.namespaceV2; }
306
+ // Java ClientConfig#setAccessChannel: LOCAL (default) keeps traces in
307
+ // RMQ_SYS_TRACE_TOPIC, CLOUD writes rmq_sys_TRACE_DATA_<regionId>. The dispatcher
308
+ // reads it when start() runs, so set it before start().
309
+ setAccessChannel(channel) {
310
+ if (channel !== AccessChannelLocal && channel !== AccessChannelCloud) {
311
+ throw new Error(`invalid accessChannel: ${channel}`);
312
+ }
313
+ this.accessChannel = channel;
314
+ return this;
315
+ }
316
+ getAccessChannel() { return this.accessChannel; }
317
+ setUnitName(name) { this.unitName = name; return this; }
318
+ setInstanceName(name) { this.instanceName = name; return this; }
319
+ getInstanceName() { return this.instanceName; }
320
+ setTlsEnable(enable) { this.tlsEnable = enable; return this; }
321
+ // TLS details (caCert=strict CA verification; clientCert/clientKey=mTLS; serverName=hostname override).
322
+ // Omitting caCert = test-mode (trust the self-signed cert).
323
+ setTlsOptions(opts) {
324
+ this.tlsOptions = opts;
325
+ return this;
326
+ }
327
+ setSocksProxyConfig(config) { this.socksProxyConfig = config; return this; }
328
+ getSocksProxyConfig() { return this.socksProxyConfig; }
329
+ setSendMsgTimeout(ms) { this.sendMsgTimeout = ms; return this; }
330
+ setSendMsgMaxTimeoutPerRequest(ms) { this.sendMsgMaxTimeoutPerRequest = ms; return this; }
331
+ getSendMsgMaxTimeoutPerRequest() { return this.sendMsgMaxTimeoutPerRequest; }
332
+ setEnableTrace(enable) { this.enableTrace = enable; return this; }
333
+ // Heartbeat V2 switch. A broker that does
334
+ // not answer the V2 ext fields keeps receiving subscription-carrying bodies, so
335
+ // turning this on against an old broker is harmless.
336
+ setUseHeartbeatV2(enable) {
337
+ this.useHeartbeatV2 = enable === true;
338
+ if (this.client)
339
+ this.client.setUseHeartbeatV2(this.useHeartbeatV2);
340
+ return this;
341
+ }
342
+ getUseHeartbeatV2() { return this.useHeartbeatV2; }
343
+ // Received-body decode switch. Pushed through to the shared instance so the
344
+ // pull / POP decode paths honour it immediately.
345
+ setDecodeReadBody(enable) {
346
+ this.decodeReadBody = enable === true;
347
+ if (this.client)
348
+ this.client.setDecodeReadBody(this.decodeReadBody);
349
+ return this;
350
+ }
351
+ getDecodeReadBody() { return this.decodeReadBody; }
352
+ // Internal-API timeout, in milliseconds. An explicit per-call timeout passed
353
+ // by a caller still wins.
354
+ setMqClientApiTimeout(ms) {
355
+ if (!Number.isFinite(ms) || ms <= 0)
356
+ throw new Error(`mqClientApiTimeout must be > 0, got ${ms}`);
357
+ this.mqClientApiTimeout = Math.floor(ms);
358
+ if (this.client)
359
+ this.client.setMqClientApiTimeout(this.mqClientApiTimeout);
360
+ return this;
361
+ }
362
+ getMqClientApiTimeout() { return this.mqClientApiTimeout; }
363
+ // Concurrent heartbeat round; heartbeat V2 still wins when it is enabled.
364
+ setEnableConcurrentHeartbeat(enable) {
365
+ this.enableConcurrentHeartbeat = enable === true;
366
+ if (this.client)
367
+ this.client.setEnableConcurrentHeartbeat(this.enableConcurrentHeartbeat);
368
+ return this;
369
+ }
370
+ getEnableConcurrentHeartbeat() { return this.enableConcurrentHeartbeat; }
371
+ setConcurrentHeartbeatThreadPoolSize(size) {
372
+ if (!Number.isInteger(size) || size <= 0) {
373
+ throw new Error(`concurrentHeartbeatThreadPoolSize must be a positive integer, got ${size}`);
374
+ }
375
+ this.concurrentHeartbeatThreadPoolSize = size;
376
+ if (this.client)
377
+ this.client.setConcurrentHeartbeatThreadPoolSize(size);
378
+ return this;
379
+ }
380
+ getConcurrentHeartbeatThreadPoolSize() { return this.concurrentHeartbeatThreadPoolSize; }
381
+ setMaxPageSizeInGetMetadata(size) {
382
+ if (!Number.isInteger(size) || size <= 0) {
383
+ throw new Error(`maxPageSizeInGetMetadata must be a positive integer, got ${size}`);
384
+ }
385
+ this.maxPageSizeInGetMetadata = size;
386
+ if (this.client)
387
+ this.client.setMaxPageSizeInGetMetadata(size);
388
+ return this;
389
+ }
390
+ getMaxPageSizeInGetMetadata() { return this.maxPageSizeInGetMetadata; }
391
+ setTraceTopic(topic) { this.traceTopic = topic; return this; }
392
+ // compressType: 'ZLIB' | 'LZ4' | 'ZSTD'
393
+ // or the raw MessageSysFlag type value (1/2/3).
394
+ setCompressType(compressType) {
395
+ this.compressType = compressionTypeByName(compressType);
396
+ return this;
397
+ }
398
+ setCompressMsgBodyOverHowmuch(howmuch) { this.compressMsgBodyOverHowmuch = howmuch; return this; }
399
+ setSendMessageWithVIPChannel(enable) {
400
+ this.sendMessageWithVIPChannel = enable;
401
+ // The offset / lock / consumer-list RPCs are issued on the shared instance,
402
+ // so keep its VIP view in step with this facade's send switch.
403
+ if (this.client)
404
+ this.client.setVipChannelEnabled(enable);
405
+ return this;
406
+ }
407
+ isSendMessageWithVIPChannel() { return this.sendMessageWithVIPChannel; }
408
+ // ASYNC retry budget (first attempt not counted). Only the async chain reads it.
409
+ setRetryTimesWhenSendAsyncFailed(n) { this.retryTimesWhenSendAsyncFailed = n; return this; }
410
+ getRetryTimesWhenSendAsyncFailed() { return this.retryTimesWhenSendAsyncFailed; }
411
+ // SYNC retry budget (first attempt not counted). Only the sync loop reads it.
412
+ setRetryTimesWhenSendFailed(n) { this.retryTimesWhenSendFailed = n; return this; }
413
+ getRetryTimesWhenSendFailed() { return this.retryTimesWhenSendFailed; }
414
+ // Whether a "stored, but not stored well" answer makes the sync loop move on to
415
+ // another broker. SYNC only — the async chain delivers it as-is.
416
+ setRetryAnotherBrokerWhenNotStoreOK(enable) {
417
+ this.retryAnotherBrokerWhenNotStoreOK = enable === true;
418
+ return this;
419
+ }
420
+ getRetryAnotherBrokerWhenNotStoreOK() { return this.retryAnotherBrokerWhenNotStoreOK; }
421
+ // Auto-batch switch. Enabling it after start() wires the accumulator right
422
+ // away: a switch that only worked before start() would be a silent no-op for
423
+ // the caller that flipped it on a live producer.
424
+ setAutoBatch(enable) {
425
+ this.autoBatch = enable === true;
426
+ if (this.autoBatch && this.client != null)
427
+ this._wireAccumulator();
428
+ return this;
429
+ }
430
+ isAutoBatch() { return this.autoBatch; }
431
+ // Beat period of the instance-level heartbeat loop. A live producer applies it
432
+ // to the shared instance at once (which rebuilds its timer); before start() it
433
+ // is the value handed over at registration.
434
+ setHeartbeatIntervalMillis(ms) {
435
+ this.heartbeatIntervalMillis = ms;
436
+ if (this.client != null)
437
+ this.client.setHeartbeatIntervalMillis(ms);
438
+ return this;
439
+ }
440
+ getHeartbeatIntervalMillis() {
441
+ return this.client != null ? this.client.heartbeatIntervalMillis : this.heartbeatIntervalMillis;
442
+ }
443
+ // Replace the whole retryable-code set (a fresh Set; the old contents are not merged).
444
+ setRetryResponseCodes(codes) {
445
+ this.retryResponseCodes = new Set(codes);
446
+ return this;
447
+ }
448
+ getRetryResponseCodes() { return new Set(this.retryResponseCodes); }
449
+ addRetryResponseCode(code) { this.retryResponseCodes.add(code); return this; }
450
+ removeRetryResponseCode(code) { this.retryResponseCodes.delete(code); return this; }
451
+ isRetryResponseCode(code) {
452
+ return code != null && this.retryResponseCodes.has(code);
453
+ }
454
+ // Auto-batch gates. -1 keeps the accumulator's own default (32 KB / 10 ms);
455
+ // any other value is range-checked by the accumulator and, when one is
456
+ // already wired, applied to it in place (the hold period rebuilds its timer).
457
+ setBatchMaxBytes(bytes) {
458
+ if (bytes > -1)
459
+ this._accumulator?.setMaxHoldSize(bytes);
460
+ this.batchMaxBytes = bytes;
461
+ return this;
462
+ }
463
+ getBatchMaxBytes() { return this._accumulator?.maxHoldSize ?? this.batchMaxBytes; }
464
+ setBatchMaxDelayMs(ms) {
465
+ if (ms > -1)
466
+ this._accumulator?.setHoldMs(ms);
467
+ this.batchMaxDelayMs = ms;
468
+ return this;
469
+ }
470
+ getBatchMaxDelayMs() { return this._accumulator?.holdMs ?? this.batchMaxDelayMs; }
471
+ // ZLIB compression level 0-9 (default 5).
472
+ setCompressLevel(level) { this.compressLevel = level; return this; }
473
+ getCompressLevel() { return this.compressLevel; }
474
+ // Arms the MQFaultStrategy reachability probe. Default off.
475
+ setStartDetectorEnable(enable) { this.startDetectorEnable = enable; return this; }
476
+ // Async-send backpressure gate (default false): when off, the in-flight
477
+ // num/size semaphores are not consulted.
478
+ setEnableBackpressureForAsyncMode(enable) { this.enableBackpressureForAsyncMode = enable; return this; }
479
+ isEnableBackpressureForAsyncMode() { return this.enableBackpressureForAsyncMode; }
480
+ setEnableTraceContext(enable) { this.enableTraceContext = enable; return this; }
481
+ isEnableTraceContext() { return this.enableTraceContext; }
482
+ // Runtime backpressure knobs — take effect on the live semaphores.
483
+ setBackPressureForAsyncSendNum(num) {
484
+ this.backPressureForAsyncSendNum = backPressurePermits(num, MIN_ASYNC_SEND_NUM, 'semaphoreAsyncSendNum');
485
+ this._semaphoreAsyncSendNum.setTotalPermits(this.backPressureForAsyncSendNum);
486
+ return this;
487
+ }
488
+ setBackPressureForAsyncSendSize(size) {
489
+ this.backPressureForAsyncSendSize = backPressurePermits(size, MIN_ASYNC_SEND_SIZE, 'semaphoreAsyncSendSize');
490
+ this._semaphoreAsyncSendSize.setTotalPermits(this.backPressureForAsyncSendSize);
491
+ return this;
492
+ }
493
+ getSemaphoreAsyncSendNumAvailablePermits() { return this._semaphoreAsyncSendNum.availablePermitsCount(); }
494
+ getSemaphoreAsyncSendSizeAvailablePermits() { return this._semaphoreAsyncSendSize.availablePermitsCount(); }
495
+ getBackPressureForAsyncSendNum() { return this.backPressureForAsyncSendNum; }
496
+ getBackPressureForAsyncSendSize() { return this.backPressureForAsyncSendSize; }
497
+ // Acquire both async-send permits or refuse: each stage gets the caller's remaining budget
498
+ // and waits for permits inside it, and a stage that cannot be served -- whether because the
499
+ // budget ran out or because the gate stayed full to the end of it -- answers with
500
+ // RemotingTooMuchRequestException. A partial acquisition is rolled back so permits never
501
+ // leak. The byte charge is the body length as it is NOW: the send path may compress the body
502
+ // afterwards, and the release has to hand back what was taken, not the shrunk length.
503
+ async _acquireAsyncPermits(msg, timeoutMillis, begin) {
504
+ const numBudget = timeoutMillis - (Date.now() - begin);
505
+ if (!(numBudget > 0 && await this._semaphoreAsyncSendNum.tryAcquire(1, numBudget))) {
506
+ throw new RemotingTooMuchRequestException('send message tryAcquire semaphoreAsyncNum timeout');
507
+ }
508
+ const bodyLen = msg.getBody() != null ? msg.getBody().length : 0;
509
+ const sizeBudget = timeoutMillis - (Date.now() - begin);
510
+ if (!(sizeBudget > 0 && await this._semaphoreAsyncSendSize.tryAcquire(bodyLen, sizeBudget))) {
511
+ this._semaphoreAsyncSendNum.release(1);
512
+ throw new RemotingTooMuchRequestException('send message tryAcquire semaphoreAsyncSize timeout');
513
+ }
514
+ return bodyLen;
515
+ }
516
+ _releaseAsyncPermits(bodyLen) {
517
+ this._semaphoreAsyncSendSize.release(bodyLen);
518
+ this._semaphoreAsyncSendNum.release(1);
519
+ }
520
+ async start() {
521
+ if (this.producerGroup == null || !String(this.producerGroup).trim()) {
522
+ throw new MQClientException('producer group is blank');
523
+ }
524
+ // The group is a resource too: with a namespace set, every broker-side
525
+ // registration (heartbeat, send header, transaction end) must carry the
526
+ // prefixed name. wrapNamespace is idempotent, so a repeated start is safe.
527
+ this.producerGroup = this._withNamespace(this.producerGroup);
528
+ Validators.checkGroup(this.producerGroup);
529
+ if (this.producerGroup === MixAll.DEFAULT_PRODUCER_GROUP) {
530
+ throw new MQClientException('producer group can not equal DEFAULT_PRODUCER');
531
+ }
532
+ const instanceName = MixAll.changeInstanceNameToPid(this.instanceName);
533
+ const clientId = MixAll.clientIdFor(instanceName, this.unitName);
534
+ this.producerClientId = clientId;
535
+ const manager = MQClientManager.getInstance();
536
+ const { instance: client, created } = manager.getOrCreateMQClientInstance(clientId, () => {
537
+ const remotingClient = new RemotingClient({
538
+ tlsEnable: this.tlsEnable, tlsOptions: this.tlsOptions, socksProxyConfig: this.socksProxyConfig,
539
+ });
540
+ return new MQClient(clientId, this.namesrvAddr, remotingClient);
541
+ });
542
+ this.client = client;
543
+ manager.registerClient(clientId, client);
544
+ // Keep the shared instance's VIP view aligned with this facade's send switch,
545
+ // so the instance-level offset / lock / consumer-list RPCs honour it too.
546
+ client.setVipChannelEnabled(this.sendMessageWithVIPChannel);
547
+ if (created) {
548
+ if (this.useHeartbeatV2)
549
+ client.setUseHeartbeatV2(true);
550
+ client.setDecodeReadBody(this.decodeReadBody);
551
+ client.setMqClientApiTimeout(this.mqClientApiTimeout);
552
+ client.setEnableConcurrentHeartbeat(this.enableConcurrentHeartbeat);
553
+ client.setConcurrentHeartbeatThreadPoolSize(this.concurrentHeartbeatThreadPoolSize);
554
+ client.setMaxPageSizeInGetMetadata(this.maxPageSizeInGetMetadata);
555
+ // Pass this facade's configured beat period into the shared instance ONCE
556
+ // at registration: the instance-level heartbeat timer and the consumer
557
+ // facades' own beats read it from there.
558
+ client.setHeartbeatIntervalMillis(this.heartbeatIntervalMillis);
559
+ registerRpcHooks(client.remotingClient, {
560
+ namespaceV2: () => this.namespaceV2,
561
+ userHook: this.rpcHook,
562
+ });
563
+ }
564
+ // Register before start(): the early beat fired by client.start() builds its
565
+ // HEARTBEAT from producerTable, so registering afterwards makes that beat carry
566
+ // no ProducerData and the broker never learns this producer's channel.
567
+ client.registerProducer(this.producerGroup, this);
568
+ client.start();
569
+ this.mqFaultStrategy = new MQFaultStrategy(this.sendLatencyFaultEnable);
570
+ // Fault-strategy probe thread: MQFaultStrategy carries a start/stop
571
+ // detector; resolver maps broker name -> publish addr, detector is a plain
572
+ // TCP connect probe. Both are no-ops unless startDetectorEnable is turned on.
573
+ if (this.startDetectorEnable) {
574
+ this.mqFaultStrategy.setResolver((brokerName) => this.client != null ? this.client.findBrokerAddressInPublish(brokerName) : null);
575
+ this.mqFaultStrategy.setServiceDetector((addr, timeout) => tcpDetect(addr, timeout));
576
+ this.mqFaultStrategy.setStartDetectorEnable(true);
577
+ }
578
+ this.mqFaultStrategy.startDetector();
579
+ // RequestFutureHolder housekeeping: the TTL sweep that fails
580
+ // request-reply futures nobody will ever answer.
581
+ REQUEST_FUTURE_HOLDER.startScheduledTask(this);
582
+ if (this.autoBatch)
583
+ this._wireAccumulator();
584
+ // Message trace wiring: without this, enableTrace is a silent no-op on the
585
+ // produce side (SendMessageHook fires before/after every send).
586
+ if (this.enableTrace) {
587
+ try {
588
+ const { AsyncTraceDispatcher } = await import("./trace_dispatcher.js");
589
+ const { SendMessageTraceHookImpl } = await import("./trace_hook.js");
590
+ const dispatcher = new AsyncTraceDispatcher(this.producerGroup, 'PRODUCER', 10, this.traceTopic || undefined);
591
+ dispatcher.setHostProducer(this);
592
+ // Propagate the namespace to the internal trace producer so its rpcs
593
+ // carry nsd/ns as well.
594
+ dispatcher.setNamespaceV2(this.namespaceV2);
595
+ // Java builds the internal trace producer with the host's RPCHook; without
596
+ // it an ACL broker rejects every trace send and no record is written.
597
+ dispatcher.rpcHook = this.rpcHook;
598
+ dispatcher.accessChannel = this.accessChannel;
599
+ this._traceDispatcher = dispatcher;
600
+ this._traceHook = new SendMessageTraceHookImpl(dispatcher);
601
+ hookRegistry.registerSendMessageHook(this._traceHook);
602
+ // Default recall-message trace hook (an RPCHook on the remoting
603
+ // client) gives recall rpcs their own Recall trace records. Gated on this
604
+ // client's own env key com.rocketmq.recall.default.trace.enable, default off.
605
+ const { DefaultRecallMessageTraceHook } = await import("./trace_hook.js");
606
+ this._recallTraceHook = new DefaultRecallMessageTraceHook(dispatcher);
607
+ this.client.remotingClient.registerRpcHook(this._recallTraceHook);
608
+ await dispatcher.start(this.namesrvAddr || undefined, this.accessChannel);
609
+ }
610
+ catch (e) {
611
+ logger.warn('trace dispatcher start failed (trace disabled): %s', e.message);
612
+ this._traceDispatcher = null;
613
+ this._traceHook = null;
614
+ this._recallTraceHook = null;
615
+ }
616
+ }
617
+ logger.info(`producer ${this.producerGroup} started, clientId=${clientId}`);
618
+ }
619
+ shutdown() {
620
+ REQUEST_FUTURE_HOLDER.shutdown(this);
621
+ this.mqFaultStrategy.shutdown();
622
+ if (this._recallTraceHook != null && this.client != null) {
623
+ const idx = this.client.remotingClient.rpcHooks.indexOf(this._recallTraceHook);
624
+ if (idx >= 0)
625
+ this.client.remotingClient.rpcHooks.splice(idx, 1);
626
+ this._recallTraceHook = null;
627
+ }
628
+ // NOTE: the accumulator is NOT stopped here — it is instance-level (keyed by
629
+ // clientId) and stops when the shared MQClient really shuts down (refcount 0).
630
+ if (this._traceHook != null) {
631
+ const idx = hookRegistry.sendMessageHooks.indexOf(this._traceHook);
632
+ if (idx >= 0)
633
+ hookRegistry.sendMessageHooks.splice(idx, 1);
634
+ this._traceHook = null;
635
+ }
636
+ if (this._traceDispatcher != null) {
637
+ const d = this._traceDispatcher;
638
+ this._traceDispatcher = null;
639
+ void d.shutdown().catch(() => { });
640
+ }
641
+ if (this.client != null) {
642
+ this.client.unregisterProducer(this.producerGroup);
643
+ // Hand back to the manager: reference count -1; the REAL shutdown (stop
644
+ // heartbeat/route timers, drop connections) happens only when it hits
645
+ // ZERO. Other facades sharing the same clientId keep the instance alive.
646
+ MQClientManager.getInstance().unregisterClient(this.client.clientId, this.client);
647
+ }
648
+ this.client = null;
649
+ }
650
+ // ---- compression (algorithm selectable; degrade on failure, never throw) ----
651
+ tryToCompressMessage(msg) {
652
+ // sendKernelImpl guards compression with `!(msg instanceof MessageBatch)`:
653
+ // the batch body is the aggregate envelope the broker splits
654
+ // back apart — compressing it would corrupt that contract.
655
+ if (msg instanceof MessageBatch)
656
+ return false;
657
+ const body = msg.getBody();
658
+ if (body == null || body.length < this.compressMsgBodyOverHowmuch)
659
+ return false;
660
+ if (MessageSysFlag.isCompressed(msg._sysFlag != null ? msg._sysFlag : 0))
661
+ return false;
662
+ try {
663
+ const compressed = compressFor(body, this.compressType, this.compressLevel);
664
+ msg._sysFlag = MessageSysFlag.setCompressionType((msg._sysFlag != null ? msg._sysFlag : 0) | MessageSysFlag.COMPRESSED_FLAG, this.compressType);
665
+ msg.setBody(compressed);
666
+ return true;
667
+ }
668
+ catch (e) {
669
+ logger.warn('compress message body failed, send without compression');
670
+ return false;
671
+ }
672
+ }
673
+ // ---- core send path ----
674
+ // tryToFindTopicPublishInfo: first fetch the
675
+ // EXACT topic route; only when that yields no usable info, retry via the
676
+ // DEFAULT topic (TBW102) so the broker auto-creates on first send. The
677
+ // fallback-derived info is tagged (`fromDefaultTopic`) so _sendDefaultImpl
678
+ // refetches the exact route before using its queue list.
679
+ async _tryToFindTopicPublishInfo(topic) {
680
+ if (this.client == null)
681
+ throw new MQClientException('producer not started');
682
+ let info = this.client.getTopicPublishInfo(topic);
683
+ if (info != null && info.ok() && !info.fromDefaultTopic)
684
+ return info;
685
+ this._lastRouteError = null;
686
+ try {
687
+ await this.client.updateTopicRouteInfoFromNameServer(topic, false);
688
+ }
689
+ catch (e) {
690
+ // Keep the reason the route could not be fetched: if an error like a TLS
691
+ // handshake CA verification failure were swallowed here, the layer above
692
+ // would only see "No route info of this topic" — a black box (the user
693
+ // hunts toward "create topic" while the real culprit is the
694
+ // certificate/CA). Stash it in _lastRouteError and let the No-route
695
+ // throw site carry it out.
696
+ this._lastRouteError = e instanceof Error ? e : new Error(String(e));
697
+ logger.warn(`find topic publish info for ${topic} failed: ${e.message}`);
698
+ }
699
+ info = this.client.getTopicPublishInfo(topic);
700
+ if (info != null && info.ok() && !info.fromDefaultTopic)
701
+ return info;
702
+ // Second step: DEFAULT-topic fallback (auto-create path).
703
+ try {
704
+ await this.client.updateTopicRouteInfoFromNameServer(topic, true);
705
+ }
706
+ catch (e) {
707
+ if (this._lastRouteError == null) {
708
+ this._lastRouteError = e instanceof Error ? e : new Error(String(e));
709
+ }
710
+ logger.warn(`find default-topic publish info for ${topic} failed: ${e.message}`);
711
+ }
712
+ info = this.client.getTopicPublishInfo(topic);
713
+ if (info != null && info.ok())
714
+ return info;
715
+ return info;
716
+ }
717
+ // The cause of the last failure across the two route attempts above (cleared
718
+ // on success). Used to bring the real root cause — TLS/CA, connection
719
+ // refused, etc. — into the "No route info" error instead of black-boxing it.
720
+ _lastRouteError = null;
721
+ _routeFailureSuffix() {
722
+ const e = this._lastRouteError;
723
+ if (e == null)
724
+ return '';
725
+ const chain = [];
726
+ for (let x = e; x != null; x = x.cause)
727
+ chain.push(`${x.name ?? 'Error'}: ${x.message}`);
728
+ return ` (route fetch failed: ${chain.join(' <-- ')})`;
729
+ }
730
+ _selectOneMessageQueue(tpInfo, lastBrokerName) {
731
+ if (this.sendLatencyFaultEnable) {
732
+ return this.mqFaultStrategy.selectOneMessageQueue(tpInfo, lastBrokerName);
733
+ }
734
+ return tpInfo.selectOneMessageQueue(lastBrokerName);
735
+ }
736
+ // Build the on-wire request (sets UNIQ_KEY + producer-group correction). Exposed for the
737
+ // smoke test / advanced callers; returns the RemotingCommand.
738
+ buildSendRequest(msg, mq) {
739
+ if (this.client == null)
740
+ throw new MQClientException('producer not started');
741
+ if (!msg.getProperty(MessageConst.PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX)) {
742
+ MessageAccessor.putProperty(msg, MessageConst.PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX, createUniqID());
743
+ }
744
+ MessageAccessor.setCorrectionBeforePublish(msg, this.producerGroup);
745
+ return this.client.buildSendRequest(msg, mq, this.producerGroup);
746
+ }
747
+ // Every send invoke wraps the address with
748
+ // MixAll.brokerVIPChannel(isVipChannelEnabled, addr).
749
+ _sendAddr(addr) {
750
+ if (addr == null)
751
+ return null;
752
+ if (this.sendMessageWithVIPChannel)
753
+ return MixAll.brokerVipChannel(true, addr);
754
+ return addr;
755
+ }
756
+ // Send kernel: compress -> UNIQ_KEY -> CheckForbiddenHook (NOT swallowed) -> transport.
757
+ async sendKernelImpl(msg, mq, communicationMode, sendCallback, tpInfo,
758
+ // The caller's remaining budget for THIS attempt, not the producer default:
759
+ // the retry loop subtracts what earlier attempts already spent.
760
+ timeoutMillis = this.sendMsgTimeout) {
761
+ if (this.client == null)
762
+ throw new MQClientException('producer not started');
763
+ // 1) compression already applied by caller (tryToCompressMessage); ensure sysFlag present.
764
+ let sysFlag = msg._sysFlag != null ? msg._sysFlag : 0;
765
+ // Java DefaultMQProducerImpl:951-954: a half message carries TRAN_MSG="true"
766
+ // (stamped by sendMessageInTransaction) and the send kernel flags it in
767
+ // sysFlag — TRANSACTION_PREPARED_TYPE = 0x1 << 2 = 0x4, the same 2-bit
768
+ // field the commit/rollback header reuses. Without it the broker stores the
769
+ // row as a normal message: it never enters RMQ_SYS_TRANS_HALF_TOPIC and
770
+ // endTransaction cannot find the prepared record.
771
+ if (msg.getProperty(MessageConst.PROPERTY_TRANSACTION_PREPARED) === 'true') {
772
+ sysFlag |= MessageSysFlag.TRANSACTION_PREPARED_TYPE;
773
+ }
774
+ // 2) CheckForbiddenHook — injected AFTER compression/sysflag, exception is NOT swallowed.
775
+ const forbidCtx = new CheckForbiddenContext(this.namespace, this.producerGroup, msg.getTopic(), msg);
776
+ forbidCtx.brokerAddr = this.client.publishAddrFor(mq);
777
+ forbidCtx.message = msg;
778
+ for (const hook of hookRegistry.checkForbiddenHooks) {
779
+ hook.checkForbidden(forbidCtx);
780
+ }
781
+ if (forbidCtx.forbid) {
782
+ throw new MQClientException('the message is forbidden by CheckForbiddenHook');
783
+ }
784
+ if (!msg.getProperty(MessageConst.PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX)) {
785
+ MessageAccessor.putProperty(msg, MessageConst.PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX, createUniqID());
786
+ }
787
+ MessageAccessor.setCorrectionBeforePublish(msg, this.producerGroup);
788
+ msg._sysFlag = sysFlag;
789
+ // W3C traceparent passthrough (opt-in): a message without one gets a root
790
+ // span; a caller-propagated value is never overwritten. This port builds the
791
+ // equivalent of an OTel hook inline, and it
792
+ // runs in the same position (after the forbidden hooks, before the request
793
+ // is built, so the trace hook sees the property).
794
+ if (this.enableTraceContext) {
795
+ injectTraceContext(msg);
796
+ }
797
+ const request = this.client.buildSendRequest(msg, mq, this.producerGroup);
798
+ const addr = this._sendAddr(this.client.publishAddrFor(mq));
799
+ if (addr == null) {
800
+ throw new MQClientException(`no broker address for mq ${mq.toString()}`);
801
+ }
802
+ if (communicationMode === CommunicationMode.SYNC) {
803
+ return this.client.sendMessage(addr, request, mq, timeoutMillis, msg);
804
+ }
805
+ else if (communicationMode === CommunicationMode.ASYNC) {
806
+ // The ASYNC path owns its own retry chain (retryTimesWhenSendAsyncFailed,
807
+ // shared time budget, re-select a queue that avoids the failed broker).
808
+ this._sendAsyncKernel(msg, mq, tpInfo, sendCallback, request, addr, timeoutMillis);
809
+ return null;
810
+ }
811
+ else {
812
+ await this.client.sendMessageOneway(addr, request);
813
+ return null;
814
+ }
815
+ }
816
+ // Async send retry chain. One logical send may issue up to
817
+ // 1 + retryTimesWhenSendAsyncFailed attempts against the SAME shared budget:
818
+ // - transport-level failures (timeout / send failure / connection closed)
819
+ // retry on a queue chosen to avoid the failed broker (needRetry=true);
820
+ // - a broker answer that fails response parsing (MQBrokerException — error
821
+ // code) does NOT retry (needRetry=false);
822
+ // - a parsed SendResult is delivered as-is. The async chain never consults
823
+ // retryAnotherBrokerWhenNotStoreOK (that flag is sync-only) —
824
+ // FLUSH_DISK_TIMEOUT etc. go to the caller's onSuccess.
825
+ // Every attempt updates the fault item; when the budget or the retry budget
826
+ // runs out the user callback fires exactly once with the error.
827
+ _sendAsyncKernel(msg, mq, tpInfo, sendCallback, request, addr, timeoutMillis) {
828
+ void addr; // kept for signature symmetry with sendKernelImpl
829
+ const timesTotal = Math.max(0, this.retryTimesWhenSendAsyncFailed);
830
+ let times = 0;
831
+ const self = this;
832
+ const attempt = (brokerName, attemptMq, budgetMs) => {
833
+ const attemptAddr = brokerName != null
834
+ ? self._sendAddr(self.client.findBrokerAddressInPublish(brokerName))
835
+ : self._sendAddr(self.client.publishAddrFor(attemptMq));
836
+ const attemptBegin = Date.now();
837
+ const onExceptionImpl = (err, needRetry, reachable) => {
838
+ // Async chain: callback failures mark isolation with reachable=true;
839
+ // a synchronous throw (never reached the transport) sets reachable=false.
840
+ if (self.sendLatencyFaultEnable) {
841
+ self.mqFaultStrategy.updateFaultItem(attemptMq.getBrokerName(), Date.now() - attemptBegin, true, reachable);
842
+ }
843
+ times += 1;
844
+ const remaining = budgetMs - (Date.now() - attemptBegin);
845
+ if (needRetry && times <= timesTotal && remaining > 0) {
846
+ let retryBrokerName = brokerName; // by default, retry the same broker
847
+ if (tpInfo != null) {
848
+ const mqChosen = self._selectOneMessageQueue(tpInfo, brokerName);
849
+ if (mqChosen != null)
850
+ retryBrokerName = mqChosen.getBrokerName();
851
+ }
852
+ logger.warning('async send msg by retry %d times. topic=%s, brokerName=%s, err=%s', times, msg.getTopic(), retryBrokerName != null ? retryBrokerName : '', err.message);
853
+ // A retried request must reuse the request body but with a FRESH
854
+ // opaque id: the retried request must not collide with the old
855
+ // opaque in the response table.
856
+ request.opaque = RemotingCommand.createNewRequestId();
857
+ attempt(retryBrokerName, attemptMq, remaining);
858
+ }
859
+ else {
860
+ if (sendCallback)
861
+ sendCallback(null, err);
862
+ }
863
+ };
864
+ if (attemptAddr == null) {
865
+ onExceptionImpl(new MQClientException(`no broker address for mq ${attemptMq.toString()}`), true, false);
866
+ return;
867
+ }
868
+ try {
869
+ self.client.sendMessageAsync(attemptAddr, request, attemptMq, budgetMs, (sr, err) => {
870
+ if (err != null) {
871
+ // Distinguish transport failure (retry) from a broker answer
872
+ // that failed response parsing (MQBrokerException — no retry).
873
+ onExceptionImpl(err, !(err instanceof MQBrokerException), true);
874
+ return;
875
+ }
876
+ if (self.sendLatencyFaultEnable) {
877
+ self.mqFaultStrategy.updateFaultItem(attemptMq.getBrokerName(), Date.now() - attemptBegin, false, true);
878
+ }
879
+ if (sendCallback)
880
+ sendCallback(sr, null);
881
+ }, msg);
882
+ }
883
+ catch (e) {
884
+ onExceptionImpl(e, true, false);
885
+ }
886
+ };
887
+ attempt(mq.getBrokerName(), mq, timeoutMillis);
888
+ }
889
+ // Wrap SendMessageHook around the kernel send.
890
+ async _sendWithHooks(msg, mq, communicationMode, sendCallback, tpInfo, timeoutMillis = this.sendMsgTimeout) {
891
+ const ctx = new SendMessageContext(this.producerGroup, msg, mq, communicationMode, this.client.publishAddrFor(mq));
892
+ ctx.msgType = classifySendMessageType(msg);
893
+ // This port's SendMessageHook registry is global, not per producer.
894
+ // The inner trace producer opts out (_skipSendHooks) so trace-topic writes
895
+ // are not themselves traced.
896
+ const sendHooks = this._skipSendHooks
897
+ ? [] : hookRegistry.sendMessageHooks;
898
+ for (const hook of sendHooks)
899
+ hook.sendMessageBefore(ctx);
900
+ let sendResult = null;
901
+ try {
902
+ sendResult = await this.sendKernelImpl(msg, mq, communicationMode, sendCallback, tpInfo, timeoutMillis);
903
+ ctx.sendResult = sendResult;
904
+ ctx.exception = null;
905
+ }
906
+ catch (e) {
907
+ ctx.exception = e;
908
+ ctx.sendResult = null;
909
+ for (const hook of sendHooks)
910
+ hook.sendMessageAfter(ctx);
911
+ throw e;
912
+ }
913
+ for (const hook of sendHooks)
914
+ hook.sendMessageAfter(ctx);
915
+ return sendResult;
916
+ }
917
+ // Retry-loop default send implementation.
918
+ async _sendDefaultImpl(msg, communicationMode, sendCallback, timeoutMillis) {
919
+ // Entry: Validators.checkMessage — topic legality plus body
920
+ // null/empty/maxMessageSize (and the LMQ INNER_MULTI_DISPATCH separator
921
+ // rule) before any route lookup.
922
+ Validators.checkMessage(msg, this.maxMessageSize);
923
+ this._checkLegalTopic(msg.getTopic());
924
+ let tpInfo = await this._tryToFindTopicPublishInfo(msg.getTopic());
925
+ if (tpInfo == null || !tpInfo.ok()) {
926
+ // Carry the real route-fetch failure reason (TLS/CA, connection
927
+ // refused...); with no failure recorded, keep the original wording (the
928
+ // topic genuinely does not exist).
929
+ throw new MQClientException(`No route info of this topic: ${msg.getTopic()}${this._routeFailureSuffix()}`, this._lastRouteError ?? undefined);
930
+ }
931
+ // A publish info synthesized from the DEFAULT topic (TBW102) carries
932
+ // TBW102's queue list — sending against it yields "request queueId[N] is
933
+ // illegal". Refetch the EXACT topic route before giving the queues to the
934
+ // broker (the tag is set by updateTopicRouteInfoFromNameServer's fallback).
935
+ if (tpInfo.fromDefaultTopic) {
936
+ try {
937
+ await this.client.updateTopicRouteInfoFromNameServer(msg.getTopic(), false);
938
+ const refreshed = this.client.getTopicPublishInfo(msg.getTopic());
939
+ if (refreshed != null && refreshed.ok() && !refreshed.fromDefaultTopic)
940
+ tpInfo = refreshed;
941
+ }
942
+ catch (e) { /* keep the fallback info */ }
943
+ }
944
+ let lastBrokerName = null;
945
+ let sendResult = null;
946
+ // When every attempt failed with a broker ANSWER (not a transport error),
947
+ // the caller needs that answer — response code and remark — not a generic
948
+ // retry-exhausted message. Auth rejections (NO_PERMISSION) in particular
949
+ // are only provable through the code.
950
+ let lastBrokerErr = null;
951
+ // Contract: `timesTotal = SYNC ? 1 + retryTimesWhenSendFailed : 1` — only
952
+ // the SYNC path retries inside this loop. ASYNC owns its own chain
953
+ // (_sendAsyncKernel, retryTimesWhenSendAsyncFailed); ONEWAY never retries.
954
+ const maxTimes = communicationMode === CommunicationMode.SYNC
955
+ ? this.retryTimesWhenSendFailed + 1 : 1;
956
+ // One budget for the whole call: every attempt is charged the time already
957
+ // spent (route lookup, selector, earlier attempts) and only gets what is
958
+ // left, so a caller-supplied timeout is a ceiling on the call, not on each try.
959
+ const beginTimestampFirst = Date.now();
960
+ let callTimeout = false;
961
+ for (let times = 0; times < maxTimes; times++) {
962
+ const mq = this._selectOneMessageQueue(tpInfo, lastBrokerName);
963
+ if (mq == null)
964
+ continue;
965
+ lastBrokerName = mq.getBrokerName();
966
+ try {
967
+ this.tryToCompressMessage(msg);
968
+ const costTime = Date.now() - beginTimestampFirst;
969
+ if (timeoutMillis < costTime) {
970
+ callTimeout = true;
971
+ break;
972
+ }
973
+ let curTimeout = timeoutMillis - costTime;
974
+ const canRetryAgain = times + 1 < maxTimes;
975
+ if (this.sendMsgMaxTimeoutPerRequest > -1 && canRetryAgain
976
+ && curTimeout > this.sendMsgMaxTimeoutPerRequest) {
977
+ curTimeout = this.sendMsgMaxTimeoutPerRequest;
978
+ }
979
+ sendResult = await this._sendWithHooks(msg, mq, communicationMode, sendCallback, tpInfo, curTimeout);
980
+ // ASYNC / ONEWAY succeed as soon as no exception was
981
+ // thrown (sendKernelImpl returns null for both); only SYNC uses the
982
+ // SendResult as the retry-driving value.
983
+ if (communicationMode === CommunicationMode.ASYNC || communicationMode === CommunicationMode.ONEWAY) {
984
+ return null;
985
+ }
986
+ if (sendResult != null) {
987
+ if (this.sendLatencyFaultEnable) {
988
+ this.mqFaultStrategy.updateFaultItem(mq.getBrokerName(), 0, false, true);
989
+ }
990
+ // "Stored, but not stored well" (flush timeouts) is a finished send unless the
991
+ // caller asked to move on for it. lastBrokerName already holds this broker, so
992
+ // the next pick avoids it; once attempts run out the tail returns this result.
993
+ if (sendResult.sendStatus !== SendStatus.SEND_OK && this.retryAnotherBrokerWhenNotStoreOK) {
994
+ continue;
995
+ }
996
+ return sendResult;
997
+ }
998
+ }
999
+ catch (e) {
1000
+ logger.warn(`send to ${mq.toString()} failed (attempt ${times + 1}): ${e.message}`);
1001
+ if (e instanceof MQBrokerException) {
1002
+ lastBrokerErr = e;
1003
+ // A broker answer outside the retryable set is a verdict about THIS
1004
+ // request, not a sick broker; another broker would give the same
1005
+ // answer, so surface it now instead of spending the remaining attempts.
1006
+ if (!this.isRetryResponseCode(e.responseCode))
1007
+ throw e;
1008
+ }
1009
+ if (this.sendLatencyFaultEnable) {
1010
+ this.mqFaultStrategy.updateFaultItem(mq.getBrokerName(), this.sendMsgTimeout, true, false);
1011
+ }
1012
+ // lastBrokerName keeps THIS broker: the next pick is steered away from the
1013
+ // one that just failed, which is the whole point of retrying. Clearing it
1014
+ // would leave the next attempt to plain round-robin and could re-send to
1015
+ // the same broker while another one is healthy.
1016
+ // A failure may mean the cached publish info is still the default-topic
1017
+ // fallback (route registered late). Refetch the exact route between
1018
+ // attempts so the next try does not repeat the same illegal queue.
1019
+ if (tpInfo.fromDefaultTopic) {
1020
+ try {
1021
+ await this.client.updateTopicRouteInfoFromNameServer(msg.getTopic(), false);
1022
+ const refreshed = this.client.getTopicPublishInfo(msg.getTopic());
1023
+ if (refreshed != null && refreshed.ok() && !refreshed.fromDefaultTopic)
1024
+ tpInfo = refreshed;
1025
+ }
1026
+ catch (e2) { /* keep the fallback info */ }
1027
+ }
1028
+ }
1029
+ }
1030
+ if (sendResult == null) {
1031
+ // The budget ran out before an attempt could be issued: that is a
1032
+ // timeout, not "retries exhausted", and callers branch on the type.
1033
+ if (callTimeout)
1034
+ throw new RemotingTooMuchRequestException('sendDefaultImpl call timeout');
1035
+ // Surface the broker's own answer (response code + remark) when every
1036
+ // attempt was rejected BY THE BROKER — a generic "retries exhausted"
1037
+ // would erase the reason (e.g. ACL NO_PERMISSION 16) that the caller
1038
+ // must assert on. Transport-style failures keep the original message.
1039
+ if (lastBrokerErr != null)
1040
+ throw lastBrokerErr;
1041
+ throw new MQClientException('failed to send message after retries');
1042
+ }
1043
+ return sendResult;
1044
+ }
1045
+ // ---- public send API ----
1046
+ async send(msg, timeoutMillis) {
1047
+ this._wrapMessageTopic(msg);
1048
+ // autoBatch: a message with no explicit timeout rides a group and this call
1049
+ // awaits that group's send. An explicit timeout opts out — the hold timer
1050
+ // would add latency the caller already bounded.
1051
+ if (timeoutMillis === undefined && this._canAutoBatch(msg)) {
1052
+ const batched = await this._sendByAccumulator(msg, null, null);
1053
+ if (batched != null)
1054
+ return batched;
1055
+ }
1056
+ const r = await this._sendDefaultImpl(msg, CommunicationMode.SYNC, null, timeoutMillis ?? this.sendMsgTimeout);
1057
+ if (r == null)
1058
+ throw new MQClientException('null send result');
1059
+ return r;
1060
+ }
1061
+ async sendOneway(msg, timeoutMillis) {
1062
+ this._wrapMessageTopic(msg);
1063
+ await this._sendDefaultImpl(msg, CommunicationMode.ONEWAY, null, timeoutMillis ?? this.sendMsgTimeout);
1064
+ }
1065
+ async sendAsync(msg, sendCallback, timeoutMillis) {
1066
+ this._wrapMessageTopic(msg);
1067
+ // autoBatch takes async sends the same way: join the group, and the group's
1068
+ // send settles this callback. Nothing is acquired from the async backpressure
1069
+ // semaphores, because the batch leaves through the sync path.
1070
+ if (timeoutMillis === undefined && this._canAutoBatch(msg)) {
1071
+ await this._sendByAccumulator(msg, null, sendCallback);
1072
+ return;
1073
+ }
1074
+ const timeout = timeoutMillis ?? this.sendMsgTimeout;
1075
+ const begin = Date.now();
1076
+ // Async-send backpressure: the two fair semaphores bound the in-flight
1077
+ // async sends, but ONLY while enableBackpressureForAsyncMode is on
1078
+ // (default false). A full semaphore waits inside the caller's budget and then
1079
+ // refuses. Permits return when the transport-level callback fires — exactly once
1080
+ // — or when the send fails before reaching it.
1081
+ let sendTimeout = timeout;
1082
+ let permitsHeld = false;
1083
+ let heldBytes = 0;
1084
+ const releaseOnce = () => {
1085
+ if (permitsHeld) {
1086
+ permitsHeld = false;
1087
+ this._releaseAsyncPermits(heldBytes);
1088
+ }
1089
+ };
1090
+ const wrapped = (sr, err) => {
1091
+ releaseOnce();
1092
+ if (sendCallback)
1093
+ sendCallback(sr, err);
1094
+ };
1095
+ try {
1096
+ if (this.enableBackpressureForAsyncMode) {
1097
+ heldBytes = await this._acquireAsyncPermits(msg, timeout, begin);
1098
+ permitsHeld = true;
1099
+ // The gate waited inside the caller's budget, so the send gets what is left of it.
1100
+ const cost = Date.now() - begin;
1101
+ if (timeout <= cost) {
1102
+ throw new RemotingTooMuchRequestException('DEFAULT ASYNC send call timeout');
1103
+ }
1104
+ sendTimeout = timeout - cost;
1105
+ }
1106
+ await this._sendDefaultImpl(msg, CommunicationMode.ASYNC, wrapped, sendTimeout);
1107
+ }
1108
+ catch (e) {
1109
+ // A sendAsync caller hands over a SendCallback and normally does not attach
1110
+ // a handler to the returned promise, so re-throwing here would turn an
1111
+ // ordinary send failure -- a refused gate among them -- into an unhandled
1112
+ // rejection that kills the process. With a callback the callback owns the
1113
+ // error; a callback-less caller still gets the rejection.
1114
+ releaseOnce();
1115
+ if (sendCallback) {
1116
+ try {
1117
+ sendCallback(null, e);
1118
+ }
1119
+ catch (cbErr) {
1120
+ logger.warn('execute sendCallback in sendAsync failure, and callback throw: %s', cbErr.message);
1121
+ }
1122
+ return;
1123
+ }
1124
+ throw e;
1125
+ }
1126
+ }
1127
+ // sendSelectImpl: route lookup + selector pick + kernel send, shared by
1128
+ // sendBySelector and the two selector-shaped request forms.
1129
+ async _sendSelectImpl(msg, selector, arg, communicationMode, sendCallback, timeoutMillis) {
1130
+ const beginTimestamp = Date.now();
1131
+ // sendSelectImpl validates via Validators.checkMessage before the route
1132
+ // lookup.
1133
+ Validators.checkMessage(msg, this.maxMessageSize);
1134
+ this._checkLegalTopic(msg.getTopic());
1135
+ const tpInfo = await this._tryToFindTopicPublishInfo(msg.getTopic());
1136
+ if (tpInfo == null || !tpInfo.ok()) {
1137
+ throw new MQClientException(`No route info of this topic: ${msg.getTopic()}${this._routeFailureSuffix()}`, this._lastRouteError ?? undefined);
1138
+ }
1139
+ const mq = selector.select(msg, tpInfo.msgQueueList, arg);
1140
+ if (mq == null)
1141
+ throw new MQClientException('failed to select a message queue');
1142
+ // Route lookup and the selector are part of the caller's budget too.
1143
+ const costTime = Date.now() - beginTimestamp;
1144
+ if (timeoutMillis < costTime)
1145
+ throw new RemotingTooMuchRequestException('sendSelectImpl call timeout');
1146
+ this.tryToCompressMessage(msg);
1147
+ const r = await this._sendWithHooks(msg, mq, communicationMode, sendCallback, tpInfo, timeoutMillis - costTime);
1148
+ if (communicationMode === CommunicationMode.SYNC) {
1149
+ if (r == null)
1150
+ throw new MQClientException('null send result');
1151
+ return r;
1152
+ }
1153
+ return null;
1154
+ }
1155
+ async sendBySelector(msg, selector, arg, timeoutMillis = this.sendMsgTimeout) {
1156
+ this._wrapMessageTopic(msg);
1157
+ return this._sendSelectImpl(msg, selector, arg, CommunicationMode.SYNC, null, timeoutMillis);
1158
+ }
1159
+ // ---- transaction ----
1160
+ // sendMessageInTransaction(msg, arg) resolves the listener from
1161
+ // the producer's own field; also accept it explicitly as the 2nd parameter.
1162
+ async sendMessageInTransaction(msg, listener = null, arg = null, timeoutMillis = this.sendMsgTimeout) {
1163
+ listener = listener ?? this.transactionListener ?? null;
1164
+ if (listener == null)
1165
+ throw new MQClientException('tranExecutor is null');
1166
+ this._wrapMessageTopic(msg);
1167
+ // A half message is committed exactly as it stands, so no schedule asked for here
1168
+ // can be honoured. The key alone decides: a "0" or an empty value asks for nothing
1169
+ // yet is still a stamp this path cannot carry, so the map is consulted directly --
1170
+ // getProperty() folds an empty value to null and would let it through.
1171
+ const properties = msg.getProperties();
1172
+ for (const key of [MessageConst.PROPERTY_DELAY_TIME_LEVEL,
1173
+ MessageConst.PROPERTY_TIMER_DELAY_SEC, MessageConst.PROPERTY_TIMER_DELAY_MS,
1174
+ MessageConst.PROPERTY_TIMER_DELIVER_MS]) {
1175
+ if (properties[key] !== undefined) {
1176
+ throw new MQClientException('Transactional messages do not support delayed delivery');
1177
+ }
1178
+ }
1179
+ MessageAccessor.setTransactionPrepared(msg);
1180
+ MessageAccessor.putProperty(msg, MessageConst.PROPERTY_PRODUCER_GROUP, this.producerGroup);
1181
+ const sendResult = await this._sendDefaultImpl(msg, CommunicationMode.SYNC, null, timeoutMillis);
1182
+ if (sendResult == null)
1183
+ throw new MQClientException('send half message failed');
1184
+ // sendMessageInTransaction contract: a listener exception
1185
+ // becomes ROLLBACK_MESSAGE (it never escapes); the half message must not
1186
+ // be left for the broker's check thread on a local failure we already know.
1187
+ let localState;
1188
+ try {
1189
+ localState = listener.executeLocalTransaction(msg, arg);
1190
+ }
1191
+ catch (e) {
1192
+ logger.warn('executeLocalTransaction raised, rolling back half message: %s', e.message);
1193
+ localState = LocalTransactionState.ROLLBACK_MESSAGE;
1194
+ }
1195
+ await this._endTransaction(sendResult, localState, msg);
1196
+ const tsr = new TransactionSendResult(sendResult.sendStatus, sendResult.msgId, sendResult.messageQueue, sendResult.queueOffset, sendResult.transactionId, sendResult.offsetMsgId, sendResult.regionId, sendResult.traceOn, sendResult.recallHandle);
1197
+ tsr.setLocalTransactionState(localState);
1198
+ return tsr;
1199
+ }
1200
+ async _endTransaction(sendResult, localTransactionState, msg = null) {
1201
+ if (this.client == null)
1202
+ return;
1203
+ const brokerAddr = this.client.publishAddrFor(sendResult.messageQueue);
1204
+ if (brokerAddr == null)
1205
+ return;
1206
+ const header = new EndTransactionRequestHeader();
1207
+ header.producerGroup = this.producerGroup;
1208
+ header.transactionId = sendResult.transactionId != null ? sendResult.transactionId : sendResult.msgId;
1209
+ // ⚠ commitOrRollback is a MessageSysFlag transaction TYPE on the wire
1210
+ // (NOT=0 / COMMIT=8 / ROLLBACK=12), NOT the LocalTransactionState ordinal.
1211
+ // Sending the raw ordinal (COMMIT_MESSAGE=0) makes the broker read
1212
+ // TRANSACTION_NOT_TYPE — the half message is never committed and stays
1213
+ // invisible until the (much later) check-back. The wire protocol requires
1214
+ // the enum be mapped to the sys-flag value.
1215
+ // ⚠ offset semantics on the endTransaction wire contract:
1216
+ // - tranStateTableOffset = sendResult.queueOffset — the half message's
1217
+ // offset in RMQ_SYS_TRANS_HALF_TOPIC's queue. @CFNotNull on the wire;
1218
+ // the broker's checkPrepareMessage compares it to the half msg's
1219
+ // queueOffset and REJECTS the commit on mismatch/null.
1220
+ // - commitLogOffset = the PHYSICAL commitlog offset, decoded from the
1221
+ // offsetMsgId (falling back to msgId) — NOT queueOffset (the old code
1222
+ // sent the half-queue offset here, so every producer-side COMMIT was
1223
+ // rejected with "The commit log offset wrong" and the message stayed
1224
+ // invisible until the 30s check-back).
1225
+ // - topic is compared to PROPERTY_REAL_TOPIC in 5.x checkPrepareMessage.
1226
+ if (localTransactionState === LocalTransactionState.COMMIT_MESSAGE) {
1227
+ header.commitOrRollback = MessageSysFlag.TRANSACTION_COMMIT_TYPE;
1228
+ }
1229
+ else if (localTransactionState === LocalTransactionState.ROLLBACK_MESSAGE) {
1230
+ header.commitOrRollback = MessageSysFlag.TRANSACTION_ROLLBACK_TYPE;
1231
+ }
1232
+ else {
1233
+ header.commitOrRollback = MessageSysFlag.TRANSACTION_NOT_TYPE;
1234
+ }
1235
+ header.tranStateTableOffset = sendResult.queueOffset;
1236
+ try {
1237
+ const decoded = decodeMessageId(sendResult.offsetMsgId != null
1238
+ ? sendResult.offsetMsgId : sendResult.msgId);
1239
+ header.commitLogOffset = decoded.offset;
1240
+ }
1241
+ catch (e) {
1242
+ logger.warn(`decode offsetMsgId failed: ${e.message}`);
1243
+ header.commitLogOffset = sendResult.queueOffset;
1244
+ }
1245
+ header.topic = msg != null ? msg.getTopic() : null;
1246
+ header.fromTransactionCheck = false;
1247
+ const uniqKey = msg != null
1248
+ ? msg.getProperty(MessageConst.PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX)
1249
+ : null;
1250
+ header.msgId = uniqKey != null ? String(uniqKey) : sendResult.msgId;
1251
+ header.bname = sendResult.messageQueue != null ? sendResult.messageQueue.getBrokerName() : null;
1252
+ const request = RemotingCommand.createRequestCommand(RequestCode.END_TRANSACTION, header);
1253
+ // EndTransactionHook wraps the one request that actually ends the
1254
+ // transaction on the wire. Same discipline as the send-hook call site:
1255
+ // before hooks run outside the try, after hooks run on both the success
1256
+ // and the exception path, and a synchronous throw is re-raised after the
1257
+ // after hooks have seen the context.
1258
+ const endCtx = new EndTransactionContext(this.producerGroup, msg, brokerAddr);
1259
+ endCtx.msgId = header.msgId != null ? String(header.msgId) : null;
1260
+ endCtx.transactionId = header.transactionId;
1261
+ endCtx.transactionState = localTransactionState;
1262
+ endCtx.fromTransactionCheck = false;
1263
+ const endHooks = hookRegistry.endTransactionHooks;
1264
+ for (const hook of endHooks)
1265
+ hook.endTransactionBefore(endCtx);
1266
+ try {
1267
+ // A synchronous try around an async call catches nothing: the rejection would
1268
+ // escape as an unhandled rejection and kill the process.
1269
+ this.client.remotingClient.invokeOneway(brokerAddr, request)
1270
+ .catch((e) => { logger.warn(`end transaction failed: ${e.message}`); });
1271
+ // Trace: fire the EndTransactionTraceHook from endTransaction — the
1272
+ // EndTransaction record of a transaction message.
1273
+ if (this._traceDispatcher != null) {
1274
+ try {
1275
+ this._traceDispatcher.appendEndTransaction(this.producerGroup, msg != null ? msg.getTopic() : '', header.msgId || '', header.transactionId, localTransactionState, false, brokerAddr);
1276
+ }
1277
+ catch (e) { /* trace never breaks the data path */ }
1278
+ }
1279
+ }
1280
+ catch (e) {
1281
+ for (const hook of endHooks)
1282
+ hook.endTransactionAfter(endCtx);
1283
+ throw e;
1284
+ }
1285
+ for (const hook of endHooks)
1286
+ hook.endTransactionAfter(endCtx);
1287
+ }
1288
+ // ---- request-reply ----
1289
+ // Public surface: 3 sync forms (msg / msg+selector / msg+mq)
1290
+ // and 3 async forms (msg+callback / msg+selector+callback / msg+mq+callback).
1291
+ // prepareSendRequest: CORRELATION_ID + REPLY_TO_CLIENT (the requester's
1292
+ // clientId — the broker uses it to route the reply back via 326) + TTL, then
1293
+ // make sure the topic route exists (prepared eagerly so the ASYNC send does
1294
+ // not race a cold cache). The reply returns as a
1295
+ // PUSH_REPLY_MESSAGE_TO_CLIENT(326) push handled by
1296
+ // MQClient._registerReplyMessageProcessor.
1297
+ async _prepareSendRequest(msg, timeoutMillis) {
1298
+ const correlationId = createCorrelationId();
1299
+ const requestClientId = this.client != null ? this.client.clientId : this.producerClientId;
1300
+ MessageAccessor.putProperty(msg, MessageConst.PROPERTY_CORRELATION_ID, correlationId);
1301
+ MessageAccessor.putProperty(msg, MessageConst.PROPERTY_MESSAGE_REPLY_TO_CLIENT, requestClientId);
1302
+ MessageAccessor.putProperty(msg, MessageConst.PROPERTY_MESSAGE_TTL, String(timeoutMillis));
1303
+ MessageAccessor.setMessageType(msg, MessageType.REQUEST_REPLY);
1304
+ // The broker routes the reply back by looking REPLY_TO_CLIENT up in its
1305
+ // ProducerManager, which is filled only by a HEARTBEAT carrying this
1306
+ // producer's ProducerData. Neither the beat of start() (no route yet, so no
1307
+ // broker to beat to) nor the 10s/30s refresh reaches a request issued right
1308
+ // after start(), so the reply would be undeliverable and the request would
1309
+ // only ever time out. Warm the route, then beat once, before the caller
1310
+ // registers the future and sends.
1311
+ if (this.client != null) {
1312
+ if (this.client.getTopicRouteData(msg.getTopic()) == null
1313
+ || this.client.getAllBrokerAddrEntries().length === 0) {
1314
+ await this._tryToFindTopicPublishInfo(msg.getTopic());
1315
+ }
1316
+ await this.client.sendHeartbeatToAllBrokers();
1317
+ }
1318
+ }
1319
+ // waitResponse contract: the reply must arrive within (timeout - cost); a
1320
+ // null reply distinguishes send-OK-but-reply-timeout from send-failure.
1321
+ async _waitResponse(msg, timeoutMillis, future, cost) {
1322
+ let timer = null;
1323
+ try {
1324
+ return await new Promise((resolve, reject) => {
1325
+ timer = setTimeout(() => {
1326
+ // This body runs on Node's timer stack: anything thrown here is an
1327
+ // uncaughtException and kills the process, and the awaiting caller is
1328
+ // never told. Settle the future first, then reject.
1329
+ const err = future.sendRequestOk
1330
+ ? new MQClientException(`send request message to <${msg.getTopic()}> OK, but wait reply message timeout, ${timeoutMillis} ms.`)
1331
+ : new MQClientException(`send request message to <${msg.getTopic()}> fail`);
1332
+ future.completeExceptionally(new MQClientException('request timeout, no reply message.'));
1333
+ reject(err);
1334
+ }, Math.max(1, timeoutMillis - cost));
1335
+ future.promise().then(resolve, reject);
1336
+ });
1337
+ }
1338
+ finally {
1339
+ if (timer != null)
1340
+ clearTimeout(timer);
1341
+ }
1342
+ }
1343
+ // Sync request: request(msg, timeout) — sendDefaultImpl ASYNC + wait.
1344
+ async request(msg, timeoutMillis = 3000) {
1345
+ this._wrapMessageTopic(msg);
1346
+ await this._prepareSendRequest(msg, timeoutMillis);
1347
+ const correlationId = msg.getProperty(MessageConst.PROPERTY_CORRELATION_ID);
1348
+ const future = new RequestResponseFuture(correlationId, msg, timeoutMillis);
1349
+ REQUEST_FUTURE_HOLDER.putRequest(correlationId, future);
1350
+ const begin = Date.now();
1351
+ try {
1352
+ await this._sendDefaultImpl(msg, CommunicationMode.ASYNC, (sr, err) => {
1353
+ // SendCallback contract: only mark the request as sent — the caller is
1354
+ // unblocked by the reply arrival, the timeout scan, or requestFail.
1355
+ if (err != null) {
1356
+ future.setSendRequestOk(false);
1357
+ future.putResponseMessage(null);
1358
+ future.setCause(err);
1359
+ }
1360
+ else {
1361
+ future.setSendRequestOk(true);
1362
+ }
1363
+ }, timeoutMillis);
1364
+ return await this._waitResponse(msg, timeoutMillis, future, Date.now() - begin);
1365
+ }
1366
+ finally {
1367
+ REQUEST_FUTURE_HOLDER.removeRequest(correlationId);
1368
+ }
1369
+ }
1370
+ // Sync request through a selector — sendSelectImpl ASYNC + wait.
1371
+ async requestBySelector(msg, selector, arg, timeoutMillis = 3000) {
1372
+ this._wrapMessageTopic(msg);
1373
+ await this._prepareSendRequest(msg, timeoutMillis);
1374
+ const correlationId = msg.getProperty(MessageConst.PROPERTY_CORRELATION_ID);
1375
+ const future = new RequestResponseFuture(correlationId, msg, timeoutMillis);
1376
+ REQUEST_FUTURE_HOLDER.putRequest(correlationId, future);
1377
+ const begin = Date.now();
1378
+ try {
1379
+ await this._sendSelectImpl(msg, selector, arg, CommunicationMode.ASYNC, (sr, err) => {
1380
+ if (err != null) {
1381
+ future.setSendRequestOk(false);
1382
+ future.putResponseMessage(null);
1383
+ future.setCause(err);
1384
+ }
1385
+ else {
1386
+ future.setSendRequestOk(true);
1387
+ }
1388
+ }, timeoutMillis);
1389
+ return await this._waitResponse(msg, timeoutMillis, future, Date.now() - begin);
1390
+ }
1391
+ finally {
1392
+ REQUEST_FUTURE_HOLDER.removeRequest(correlationId);
1393
+ }
1394
+ }
1395
+ // Sync request to a pinned queue — sendKernelImpl ASYNC + wait.
1396
+ async requestByMq(msg, mq, timeoutMillis = 3000) {
1397
+ if (this.client == null)
1398
+ throw new MQClientException('producer not started');
1399
+ this._wrapMessageTopic(msg);
1400
+ await this._prepareSendRequest(msg, timeoutMillis);
1401
+ const correlationId = msg.getProperty(MessageConst.PROPERTY_CORRELATION_ID);
1402
+ const future = new RequestResponseFuture(correlationId, msg, timeoutMillis);
1403
+ REQUEST_FUTURE_HOLDER.putRequest(correlationId, future);
1404
+ const begin = Date.now();
1405
+ try {
1406
+ this.tryToCompressMessage(msg);
1407
+ // topicPublishInfo=null here: a failed attempt can only be
1408
+ // retried on the SAME broker (no route to pick another one from).
1409
+ await this.sendKernelImpl(msg, mq, CommunicationMode.ASYNC, (sr, err) => {
1410
+ if (err != null) {
1411
+ future.setSendRequestOk(false);
1412
+ future.putResponseMessage(null);
1413
+ future.setCause(err);
1414
+ }
1415
+ else {
1416
+ future.setSendRequestOk(true);
1417
+ }
1418
+ }, null, timeoutMillis);
1419
+ return await this._waitResponse(msg, timeoutMillis, future, Date.now() - begin);
1420
+ }
1421
+ finally {
1422
+ REQUEST_FUTURE_HOLDER.removeRequest(correlationId);
1423
+ }
1424
+ }
1425
+ // Async request: request(msg, requestCallback, timeout). The callback
1426
+ // fires exactly once: on reply arrival, on the TTL sweep, or on send failure.
1427
+ requestAsync(msg, requestCallback, timeoutMillis = 3000) {
1428
+ this._wrapMessageTopic(msg);
1429
+ void this._requestAsyncDefault(msg, requestCallback, timeoutMillis);
1430
+ }
1431
+ async _requestAsyncDefault(msg, requestCallback, timeoutMillis) {
1432
+ await this._prepareSendRequest(msg, timeoutMillis);
1433
+ const correlationId = msg.getProperty(MessageConst.PROPERTY_CORRELATION_ID);
1434
+ const future = new RequestResponseFuture(correlationId, msg, timeoutMillis, requestCallback);
1435
+ REQUEST_FUTURE_HOLDER.putRequest(correlationId, future);
1436
+ // Callback-form futures are driven by the callback; silence the promise so
1437
+ // a rejected reply never surfaces as an unhandled rejection.
1438
+ future.promise().catch(() => { });
1439
+ try {
1440
+ await this._sendDefaultImpl(msg, CommunicationMode.ASYNC, (sr, err) => {
1441
+ if (err != null) {
1442
+ future.setCause(err);
1443
+ this._requestFail(correlationId);
1444
+ }
1445
+ else {
1446
+ future.setSendRequestOk(true);
1447
+ }
1448
+ }, timeoutMillis);
1449
+ }
1450
+ catch (e) {
1451
+ // Pre-dispatch failure (no route / validation) routes to the callback.
1452
+ future.setCause(e);
1453
+ this._requestFail(correlationId);
1454
+ }
1455
+ }
1456
+ requestAsyncBySelector(msg, selector, arg, requestCallback, timeoutMillis = 3000) {
1457
+ this._wrapMessageTopic(msg);
1458
+ void this._requestAsyncBySelector(msg, selector, arg, requestCallback, timeoutMillis);
1459
+ }
1460
+ async _requestAsyncBySelector(msg, selector, arg, requestCallback, timeoutMillis) {
1461
+ await this._prepareSendRequest(msg, timeoutMillis);
1462
+ const correlationId = msg.getProperty(MessageConst.PROPERTY_CORRELATION_ID);
1463
+ const future = new RequestResponseFuture(correlationId, msg, timeoutMillis, requestCallback);
1464
+ REQUEST_FUTURE_HOLDER.putRequest(correlationId, future);
1465
+ future.promise().catch(() => { });
1466
+ try {
1467
+ await this._sendSelectImpl(msg, selector, arg, CommunicationMode.ASYNC, (sr, err) => {
1468
+ if (err != null) {
1469
+ future.setCause(err);
1470
+ this._requestFail(correlationId);
1471
+ }
1472
+ else {
1473
+ future.setSendRequestOk(true);
1474
+ }
1475
+ }, timeoutMillis);
1476
+ }
1477
+ catch (e) {
1478
+ future.setCause(e);
1479
+ this._requestFail(correlationId);
1480
+ }
1481
+ }
1482
+ requestAsyncByMq(msg, mq, requestCallback, timeoutMillis = 3000) {
1483
+ this._wrapMessageTopic(msg);
1484
+ void this._requestAsyncByMq(msg, mq, requestCallback, timeoutMillis);
1485
+ }
1486
+ async _requestAsyncByMq(msg, mq, requestCallback, timeoutMillis) {
1487
+ if (this.client == null)
1488
+ throw new MQClientException('producer not started');
1489
+ await this._prepareSendRequest(msg, timeoutMillis);
1490
+ const correlationId = msg.getProperty(MessageConst.PROPERTY_CORRELATION_ID);
1491
+ const future = new RequestResponseFuture(correlationId, msg, timeoutMillis, requestCallback);
1492
+ REQUEST_FUTURE_HOLDER.putRequest(correlationId, future);
1493
+ future.promise().catch(() => { });
1494
+ this.tryToCompressMessage(msg);
1495
+ try {
1496
+ await this.sendKernelImpl(msg, mq, CommunicationMode.ASYNC, (sr, err) => {
1497
+ if (err != null) {
1498
+ future.setCause(err);
1499
+ this._requestFail(correlationId);
1500
+ }
1501
+ else {
1502
+ future.setSendRequestOk(true);
1503
+ }
1504
+ }, null, timeoutMillis);
1505
+ }
1506
+ catch (e) {
1507
+ future.setCause(e);
1508
+ this._requestFail(correlationId);
1509
+ }
1510
+ }
1511
+ // requestFail: remove the future, mark send-not-ok, fire the callback
1512
+ // (once) with the recorded cause.
1513
+ _requestFail(correlationId) {
1514
+ const future = REQUEST_FUTURE_HOLDER.removeRequest(correlationId);
1515
+ if (future != null) {
1516
+ future.setSendRequestOk(false);
1517
+ future.putResponseMessage(null);
1518
+ try {
1519
+ future.executeRequestCallback();
1520
+ }
1521
+ catch (e) {
1522
+ logger.warn('execute requestCallback in requestFail, and callback throw: %s', e.message);
1523
+ }
1524
+ }
1525
+ }
1526
+ // Resolve a reply future (called by the reply-message processor in the consumer agent).
1527
+ static resolveRequestReply(correlationId, replyMsg) {
1528
+ const future = REQUEST_FUTURE_HOLDER.removeRequest(correlationId);
1529
+ if (future != null)
1530
+ future.complete(replyMsg);
1531
+ }
1532
+ // reply(): build the reply from the request (topic =
1533
+ // <cluster>_REPLY_TOPIC, REPLY_TO_CLIENT = the requestor's clientId — the
1534
+ // broker uses it to find the requestor's channel and push 326) and send it
1535
+ // through the normal send path.
1536
+ async reply(requestMsg, body, timeoutMillis = this.sendMsgTimeout) {
1537
+ const replyMsg = createReplyMessage(requestMsg, body);
1538
+ return this.send(replyMsg, timeoutMillis);
1539
+ }
1540
+ // ---- recall ----
1541
+ // recallMessage(topic, recallHandle) asks the broker to withdraw a
1542
+ // delayed/timed message and returns the withdrawn message's id.
1543
+ //
1544
+ // The topic is caller-supplied (the handle carries it too, but the broker
1545
+ // compares its request header topic, so it is checked and routed here): it is
1546
+ // validated, retry/DLQ topics are refused, its route is warmed, and the
1547
+ // broker is the handle's own broker with the topic's publisher as fallback.
1548
+ async recallMessage(topic, recallHandle) {
1549
+ if (this.client == null)
1550
+ throw new MQClientException('producer not started');
1551
+ topic = this._withNamespace(topic);
1552
+ Validators.checkTopic(topic);
1553
+ if (MixAll.isRetryTopic(topic) || MixAll.isDlqTopic(topic)) {
1554
+ throw new MQClientException('topic is not supported');
1555
+ }
1556
+ let handle;
1557
+ try {
1558
+ handle = RecallMessageHandle.parseRecallMessageHandle(recallHandle);
1559
+ }
1560
+ catch (e) {
1561
+ throw new MQClientException(e.message);
1562
+ }
1563
+ await this._tryToFindTopicPublishInfo(topic);
1564
+ let addr = this.client.findBrokerAddressInPublish(handle.brokerName);
1565
+ // Fallback: the handle's broker may be gone (proxy-less multi-endpoint
1566
+ // setups); the topic's own publisher still knows where it is stored.
1567
+ if (addr == null)
1568
+ addr = this.client.findBrokerAddrByTopic(topic);
1569
+ if (addr == null) {
1570
+ logger.warn(`can't find broker service address. ${handle.brokerName}`);
1571
+ throw new MQClientException('The broker service address not found');
1572
+ }
1573
+ return this.client.recallMessage(addr, this.producerGroup, topic, recallHandle, handle.brokerName, this.sendMsgTimeout);
1574
+ }
1575
+ // ---- topic / offset helpers ----
1576
+ // createTopic: fetch the route of `key` from the
1577
+ // nameserver FIRST, then issue UPDATE_AND_CREATE_TOPIC to every master broker
1578
+ // in that route. Never rely on the client-side broker cache here — right
1579
+ // after start() it is empty and would wrongly report "no broker available".
1580
+ async createTopic(topic, key = this.createTopicKey, queueNums = this.defaultTopicQueueNums) {
1581
+ if (this.client == null)
1582
+ throw new MQClientException('producer not started');
1583
+ // Only the NEW topic is namespaced; the route key stays the caller's name.
1584
+ topic = this._withNamespace(topic);
1585
+ await this.client.updateTopicRouteInfoFromNameServer(key, true);
1586
+ const route = this.client.getTopicRouteData(key);
1587
+ const brokerDatas = route?.brokerDatas || [];
1588
+ let sent = false;
1589
+ // createAndUpdateTopicConfig must send the config
1590
+ // to EVERY broker id in the route (master AND slaves) — a slave that never
1591
+ // receives it answers pull with "topic not exist", which kills slave-only
1592
+ // consumption.
1593
+ for (const bd of brokerDatas) {
1594
+ const addrs = bd.brokerAddrs || {};
1595
+ for (const addr of Object.values(addrs)) {
1596
+ if (addr == null)
1597
+ continue;
1598
+ const resp = await this.client.createTopicInBroker(addr, topic, queueNums);
1599
+ if (resp.code !== ResponseCode.SUCCESS) {
1600
+ throw new MQClientException(`create topic ${topic} failed on ${addr}: code=${resp.code} remark=${resp.remark || ''}`);
1601
+ }
1602
+ sent = true;
1603
+ }
1604
+ }
1605
+ if (!sent)
1606
+ throw new MQClientException('Not enough info to create topic');
1607
+ }
1608
+ async maxOffset(topic, queueId) {
1609
+ topic = this._withNamespace(topic);
1610
+ const info = await this._tryToFindTopicPublishInfo(topic);
1611
+ if (info == null)
1612
+ throw new MQClientException(`no route for ${topic}`);
1613
+ const mq = new MessageQueue(topic, info.msgQueueList.length ? info.msgQueueList[0].getBrokerName() : '', queueId);
1614
+ const addr = this.client.publishAddrFor(mq);
1615
+ if (addr == null)
1616
+ throw new MQClientException('no broker address');
1617
+ const request = RemotingCommand.createRequestCommand(RequestCode.GET_MAX_OFFSET, null);
1618
+ request.addExtField('topic', topic);
1619
+ request.addExtField('queueId', String(queueId));
1620
+ const resp = await this.client.remotingClient.invokeSync(addr, request, 3000);
1621
+ if (resp.code === ResponseCode.SUCCESS)
1622
+ return resp.extFields['offset'] != null ? parseInt(resp.extFields['offset'], 10) : 0;
1623
+ throw new MQBrokerException(resp.code, resp.remark || 'GET_MAX_OFFSET failed', addr);
1624
+ }
1625
+ // ---- batch ----
1626
+ // Wire this facade's group buffer onto the shared instance. The accumulator is
1627
+ // instance-level (keyed by clientId) so several producers over one instance
1628
+ // share one buffer, and it is registered on the instance so a real shutdown
1629
+ // stops it and flushes what is still held.
1630
+ _wireAccumulator() {
1631
+ const client = this.client;
1632
+ this._accumulator = getOrCreateProduceAccumulator(client.clientId);
1633
+ // Applied before the timer starts so a flush goes out on the configured
1634
+ // period rather than on the accumulator's default.
1635
+ if (this.batchMaxBytes > -1)
1636
+ this._accumulator.setMaxHoldSize(this.batchMaxBytes);
1637
+ if (this.batchMaxDelayMs > -1)
1638
+ this._accumulator.setHoldMs(this.batchMaxDelayMs);
1639
+ this._accumulator.setSender(async (acc) => { await this._flushAccumulation(acc); });
1640
+ this._accumulator.start();
1641
+ const accs = (client._accumulators ||= []);
1642
+ if (!accs.includes(this._accumulator))
1643
+ accs.push(this._accumulator);
1644
+ }
1645
+ // Whether auto-batch takes this message. False sends it the plain way: no
1646
+ // accumulator is wired (autoBatch off, or start() not done), or the message
1647
+ // must not ride a group.
1648
+ _canAutoBatch(msg) {
1649
+ const accumulator = this._accumulator;
1650
+ return this.autoBatch && accumulator != null && !accumulator.isStopped() && accumulator.isBatchable(msg);
1651
+ }
1652
+ // Auto-batch entry: hand one message to the group of its (topic, queue,
1653
+ // waitStoreMsgOK, tag) key and wait for that group's send. With a callback the
1654
+ // outcome belongs to that callback and the returned promise resolves as soon
1655
+ // as the message joined the group; without one the caller awaits this
1656
+ // message's own SendResult.
1657
+ //
1658
+ // A caller that passed an explicit timeout is NOT routed here: the hold timer
1659
+ // would add latency the caller already bounded.
1660
+ async _sendByAccumulator(msg, mq, sendCallback) {
1661
+ const accumulator = this._accumulator;
1662
+ if (accumulator == null)
1663
+ throw new MQClientException('producer not started');
1664
+ try {
1665
+ // Same entry guard as the direct path: an illegal topic or body must not
1666
+ // reach the accumulator's table.
1667
+ Validators.checkMessage(msg, this.maxMessageSize);
1668
+ this._checkLegalTopic(msg.getTopic());
1669
+ // UNIQ_KEY is stamped BEFORE joining: the group leaves as one packed
1670
+ // message with one answer, and that answer is split back per member by
1671
+ // this key. The direct path stamps it inside sendKernelImpl, which a batch
1672
+ // never reaches per member.
1673
+ if (!msg.getProperty(MessageConst.PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX)) {
1674
+ MessageAccessor.putProperty(msg, MessageConst.PROPERTY_UNIQ_CLIENT_MESSAGE_ID_KEYIDX, createUniqID());
1675
+ }
1676
+ const tpInfo = await this._tryToFindTopicPublishInfo(msg.getTopic());
1677
+ if (tpInfo == null || !tpInfo.ok()) {
1678
+ throw new MQClientException(`No route info of this topic: ${msg.getTopic()}${this._routeFailureSuffix()}`, this._lastRouteError ?? undefined);
1679
+ }
1680
+ const target = mq ?? this._selectOneMessageQueue(tpInfo, null);
1681
+ if (target == null)
1682
+ throw new MQClientException(`no writable queue for topic ${msg.getTopic()}`);
1683
+ const { future, flushNow } = accumulator.accumulate(msg, target);
1684
+ // Crossing the byte gate means this group is complete: it leaves now
1685
+ // instead of waiting for the hold timer.
1686
+ if (flushNow != null)
1687
+ void this._flushAccumulation(flushNow);
1688
+ if (sendCallback == null)
1689
+ return await future.promise;
1690
+ future.promise.then((sr) => sendCallback(sr, null), (e) => sendCallback(null, e)).catch((cbErr) => logger.warn('accumulator send callback threw: %s', cbErr.message));
1691
+ return null;
1692
+ }
1693
+ catch (e) {
1694
+ // A callback-owning caller must not see a rejection on the returned
1695
+ // promise; the callback gets the error instead.
1696
+ if (sendCallback == null)
1697
+ throw e;
1698
+ try {
1699
+ sendCallback(null, e instanceof Error ? e : new Error(String(e)));
1700
+ }
1701
+ catch (cbErr) {
1702
+ logger.warn('accumulator send callback threw: %s', cbErr.message);
1703
+ }
1704
+ return null;
1705
+ }
1706
+ }
1707
+ // Send one buffered group as a single packed message and settle every caller
1708
+ // waiting on it. Every exit settles: no route, no queue, a broker refusal and
1709
+ // a successful send all reach the group's futures, because a group left
1710
+ // unsettled would keep its callers' promises pending forever.
1711
+ async _flushAccumulation(acc) {
1712
+ if (acc.isEmpty())
1713
+ return;
1714
+ const msgs = acc.messages;
1715
+ const topic = msgs[0].getTopic();
1716
+ try {
1717
+ // Read the cached route first and only fall back to the (awaiting) lookup
1718
+ // on a miss. A drain triggered by shutdown has to reach the transport
1719
+ // before the facade detaches its client, and an await in between would
1720
+ // turn "flush what is still buffered" into "fail it with not started".
1721
+ const cached = this.client != null ? this.client.getTopicPublishInfo(topic) : null;
1722
+ const tpInfo = cached != null && cached.ok() && !cached.fromDefaultTopic
1723
+ ? cached
1724
+ : await this._tryToFindTopicPublishInfo(topic);
1725
+ if (tpInfo == null || !tpInfo.ok()) {
1726
+ throw new MQClientException(`No route info of this topic: ${topic}${this._routeFailureSuffix()}`, this._lastRouteError ?? undefined);
1727
+ }
1728
+ // A route lookup that had to go to the name server leaves a gap the
1729
+ // caller's shutdown can fall into; the transport calls below all need the
1730
+ // instance, so say what actually happened instead of dereferencing null.
1731
+ if (this.client == null)
1732
+ throw new MQClientException('producer not started');
1733
+ // The group was keyed around this queue; picking another one would merge
1734
+ // queues the callers deliberately kept apart. A queue that left the route
1735
+ // makes this send fail, and the group's callers learn that failure.
1736
+ const target = acc.entries[0].mq;
1737
+ const batch = MessageBatch.generateFromList(msgs);
1738
+ const result = await this._sendWithHooks(batch, target, CommunicationMode.SYNC, null, tpInfo);
1739
+ if (result == null)
1740
+ throw new MQClientException('null send result');
1741
+ acc.complete(result);
1742
+ }
1743
+ catch (e) {
1744
+ acc.fail(e instanceof Error ? e : new Error(String(e)));
1745
+ }
1746
+ }
1747
+ // ---- accessors ----
1748
+ getMQClient() { return this.client; }
1749
+ getProducerGroup() { return this.producerGroup; }
1750
+ // viewMessage is Java DefaultMQProducerImpl#viewMessage
1751
+ // (DefaultMQProducerImpl.java:517) parity: the facade rides the MQAdminImpl
1752
+ // single-leg path — offset msgId only, decoded and asked
1753
+ // VIEW_MESSAGE_BY_ID(33) on the broker that stamped it. A msgId that does
1754
+ // not decode as an offset id surfaces the NO_MESSAGE client error (no
1755
+ // uniq-key fallback at this layer).
1756
+ async viewMessage(topic, msgId) {
1757
+ if (this.client == null)
1758
+ throw new MQClientException('producer not started');
1759
+ return this.client.viewMessage(topic, msgId);
1760
+ }
1761
+ }
1762
+ // Transaction producer (a DefaultMQProducer that always carries a transaction listener).
1763
+ export class TransactionMQProducer extends DefaultMQProducer {
1764
+ transactionListener;
1765
+ constructor(producerGroup = MixAll.DEFAULT_PRODUCER_GROUP, transactionListener = null) {
1766
+ super(producerGroup);
1767
+ this.transactionListener = transactionListener;
1768
+ }
1769
+ }
1770
+ export default {
1771
+ DefaultMQProducer, TransactionMQProducer, MessageQueueSelector,
1772
+ SelectMessageQueueByHash, SelectMessageQueueByRandom, LocalTransactionState,
1773
+ TransactionListener,
1774
+ };