rocketmq-client-nodejs 1.1.0 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/client/BaseClient.d.ts +8 -0
- package/dist/client/BaseClient.js +197 -93
- package/dist/client/Logger.js +9 -6
- package/dist/client/RpcClient.js +29 -133
- package/dist/client/RpcClientManager.d.ts +17 -17
- package/dist/client/RpcClientManager.js +38 -50
- package/dist/client/TelemetrySession.js +68 -13
- package/dist/consumer/ConsumeService.js +6 -4
- package/dist/consumer/ConsumeTask.js +5 -5
- package/dist/consumer/Consumer.js +13 -13
- package/dist/consumer/FifoConsumeService.d.ts +2 -1
- package/dist/consumer/FifoConsumeService.js +3 -3
- package/dist/consumer/LiteConsumerSupport.d.ts +25 -0
- package/dist/consumer/LiteConsumerSupport.js +47 -0
- package/dist/consumer/LiteFifoConsumeService.d.ts +2 -1
- package/dist/consumer/LiteFifoConsumeService.js +4 -4
- package/dist/consumer/LitePushConsumerImpl.d.ts +3 -3
- package/dist/consumer/LitePushConsumerImpl.js +10 -29
- package/dist/consumer/LiteSimpleConsumerImpl.js +6 -14
- package/dist/consumer/LiteStandardConsumeService.d.ts +2 -1
- package/dist/consumer/LiteStandardConsumeService.js +3 -3
- package/dist/consumer/LiteSubscriptionManager.js +10 -9
- package/dist/consumer/OffsetOption.d.ts +1 -0
- package/dist/consumer/OffsetOption.js +10 -4
- package/dist/consumer/ProcessQueue.d.ts +7 -1
- package/dist/consumer/ProcessQueue.js +95 -90
- package/dist/consumer/PushConsumer.js +79 -27
- package/dist/consumer/PushSubscriptionSettings.js +3 -3
- package/dist/consumer/SimpleConsumer.d.ts +5 -0
- package/dist/consumer/SimpleConsumer.js +24 -20
- package/dist/consumer/SimpleSubscriptionSettings.js +3 -3
- package/dist/consumer/StandardConsumeService.d.ts +2 -1
- package/dist/consumer/StandardConsumeService.js +16 -7
- package/dist/consumer/SubscriptionLoadBalancer.js +3 -3
- package/dist/exception/InternalErrorException.d.ts +1 -0
- package/dist/exception/InternalErrorException.js +3 -2
- package/dist/exception/StatusChecker.js +4 -4
- package/dist/exception/receiveStatus.d.ts +18 -0
- package/dist/exception/receiveStatus.js +28 -0
- package/dist/hook/CompositedMessageInterceptor.d.ts +2 -1
- package/dist/hook/CompositedMessageInterceptor.js +8 -5
- package/dist/hook/MessageInterceptorContextImpl.d.ts +1 -0
- package/dist/hook/MessageInterceptorContextImpl.js +18 -4
- package/dist/message/Message.js +26 -4
- package/dist/message/MessageView.js +15 -5
- package/dist/message/PublishingMessage.js +4 -4
- package/dist/metrics/ClientMeterManager.d.ts +4 -0
- package/dist/metrics/ClientMeterManager.js +95 -19
- package/dist/producer/Producer.d.ts +8 -2
- package/dist/producer/Producer.js +66 -45
- package/dist/producer/PublishingLoadBalancer.js +3 -3
- package/dist/producer/PublishingSettings.js +1 -3
- package/dist/producer/SendReceipt.js +3 -2
- package/dist/producer/Transaction.d.ts +2 -1
- package/dist/producer/Transaction.js +9 -8
- package/dist/producer/index.d.ts +1 -0
- package/dist/producer/index.js +2 -2
- package/dist/route/Endpoints.js +17 -9
- package/dist/route/MessageQueue.js +7 -2
- package/dist/route/TopicRouteData.d.ts +5 -0
- package/dist/route/TopicRouteData.js +22 -1
- package/dist/util/index.d.ts +1 -1
- package/dist/util/index.js +9 -6
- package/dist/util/rpcContext.d.ts +20 -0
- package/dist/util/rpcContext.js +35 -0
- package/package.json +6 -3
- package/src/client/BaseClient.ts +211 -100
- package/src/client/Logger.ts +9 -5
- package/src/client/RpcClient.ts +48 -132
- package/src/client/RpcClientManager.ts +76 -52
- package/src/client/TelemetrySession.ts +70 -13
- package/src/consumer/ConsumeService.ts +7 -3
- package/src/consumer/ConsumeTask.ts +4 -4
- package/src/consumer/Consumer.ts +16 -15
- package/src/consumer/FifoConsumeService.ts +2 -2
- package/src/consumer/LiteConsumerSupport.ts +49 -0
- package/src/consumer/LiteFifoConsumeService.ts +3 -3
- package/src/consumer/LitePushConsumerImpl.ts +15 -22
- package/src/consumer/LiteSimpleConsumerImpl.ts +7 -14
- package/src/consumer/LiteStandardConsumeService.ts +2 -2
- package/src/consumer/LiteSubscriptionManager.ts +12 -14
- package/src/consumer/OffsetOption.ts +10 -3
- package/src/consumer/ProcessQueue.ts +123 -108
- package/src/consumer/PushConsumer.ts +83 -26
- package/src/consumer/PushSubscriptionSettings.ts +2 -2
- package/src/consumer/SimpleConsumer.ts +30 -23
- package/src/consumer/SimpleSubscriptionSettings.ts +2 -2
- package/src/consumer/StandardConsumeService.ts +15 -6
- package/src/consumer/SubscriptionLoadBalancer.ts +2 -2
- package/src/exception/InternalErrorException.ts +3 -1
- package/src/exception/StatusChecker.ts +3 -2
- package/src/exception/receiveStatus.ts +25 -0
- package/src/hook/CompositedMessageInterceptor.ts +8 -4
- package/src/hook/MessageInterceptorContextImpl.ts +18 -3
- package/src/message/Message.ts +25 -3
- package/src/message/MessageView.ts +13 -4
- package/src/message/PublishingMessage.ts +3 -3
- package/src/metrics/ClientMeterManager.ts +100 -18
- package/src/producer/Producer.ts +71 -46
- package/src/producer/PublishingLoadBalancer.ts +2 -2
- package/src/producer/PublishingSettings.ts +0 -2
- package/src/producer/SendReceipt.ts +2 -1
- package/src/producer/Transaction.ts +12 -11
- package/src/producer/index.ts +1 -1
- package/src/route/Endpoints.ts +16 -8
- package/src/route/MessageQueue.ts +6 -1
- package/src/route/TopicRouteData.ts +22 -1
- package/src/util/index.ts +8 -5
- package/src/util/rpcContext.ts +34 -0
package/src/client/BaseClient.ts
CHANGED
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
import { debuglog } from 'node:util';
|
|
19
19
|
import { randomUUID } from 'node:crypto';
|
|
20
20
|
import { Metadata } from '@grpc/grpc-js';
|
|
21
|
+
import { getRequestId } from '../util/rpcContext';
|
|
21
22
|
import {
|
|
22
23
|
Settings as SettingsPB,
|
|
23
24
|
Status,
|
|
@@ -63,6 +64,12 @@ const debug = debuglog('rocketmq-client-nodejs:client:BaseClient');
|
|
|
63
64
|
const HEARTBEAT_TRANSPORT_RECOVERY_THRESHOLD = 2;
|
|
64
65
|
// Minimum interval between transport-recovery attempts per endpoints
|
|
65
66
|
const TRANSPORT_RECOVERY_COOLDOWN = 30 * 1000;
|
|
67
|
+
// Default bound for waiting the server-pushed Settings command during startup.
|
|
68
|
+
// Java's startUp does not wait for settings at all; Node keeps the wait so
|
|
69
|
+
// consumers see server-provided settings before startup() resolves, but the
|
|
70
|
+
// wait must be bounded so a silent remote makes startup() reject (notifying
|
|
71
|
+
// the caller) instead of hanging forever.
|
|
72
|
+
const DEFAULT_FIRST_SETTINGS_TIMEOUT = 30 * 1000;
|
|
66
73
|
|
|
67
74
|
export interface BaseClientOptions {
|
|
68
75
|
sslEnabled?: boolean;
|
|
@@ -102,6 +109,10 @@ export abstract class BaseClient {
|
|
|
102
109
|
protected readonly endpoints: Endpoints;
|
|
103
110
|
protected readonly isolated = new Map<string, Endpoints>();
|
|
104
111
|
protected readonly requestTimeout: number;
|
|
112
|
+
// Internal-only bound for the startup wait on the first server-pushed
|
|
113
|
+
// Settings command. Deliberately NOT part of BaseClientOptions so the
|
|
114
|
+
// public API surface stays unchanged; tests may override it via a cast.
|
|
115
|
+
private firstSettingsTimeoutMs = DEFAULT_FIRST_SETTINGS_TIMEOUT;
|
|
105
116
|
protected readonly topics = new Set<string>();
|
|
106
117
|
protected readonly topicRouteCache = new Map<string, TopicRouteData>();
|
|
107
118
|
// In-flight route query futures to avoid duplicate queries for the same topic
|
|
@@ -109,7 +120,7 @@ export abstract class BaseClient {
|
|
|
109
120
|
protected readonly logger: ILogger;
|
|
110
121
|
protected readonly rpcClientManager: RpcClientManager;
|
|
111
122
|
readonly #telemetrySessions = new Map<string, TelemetrySession>();
|
|
112
|
-
readonly #compositedMessageInterceptor
|
|
123
|
+
readonly #compositedMessageInterceptor: CompositedMessageInterceptor;
|
|
113
124
|
protected readonly clientMeterManager: ClientMeterManager;
|
|
114
125
|
#startupResolve?: () => void;
|
|
115
126
|
#startupReject?: (err: Error) => void;
|
|
@@ -146,7 +157,19 @@ export abstract class BaseClient {
|
|
|
146
157
|
}
|
|
147
158
|
}
|
|
148
159
|
|
|
160
|
+
if (options.topics !== undefined) {
|
|
161
|
+
if (!Array.isArray(options.topics)) {
|
|
162
|
+
throw new TypeError('topics must be an array');
|
|
163
|
+
}
|
|
164
|
+
for (const topic of options.topics) {
|
|
165
|
+
if (typeof topic !== 'string' || topic.trim().length === 0) {
|
|
166
|
+
throw new TypeError('topic must be a non-empty string');
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
149
171
|
this.logger = options.logger ?? getDefaultLogger();
|
|
172
|
+
this.#compositedMessageInterceptor = new CompositedMessageInterceptor([], this.logger);
|
|
150
173
|
this.sslEnabled = options.sslEnabled === true;
|
|
151
174
|
this.endpoints = new Endpoints(options.endpoints);
|
|
152
175
|
this.namespace = options.namespace ?? '';
|
|
@@ -163,9 +186,7 @@ export abstract class BaseClient {
|
|
|
163
186
|
new MessageMeterInterceptor(this.clientMeterManager, this.clientId, () => this.getConsumerGroup()));
|
|
164
187
|
if (options.topics) {
|
|
165
188
|
for (const topic of options.topics) {
|
|
166
|
-
|
|
167
|
-
this.topics.add(topic);
|
|
168
|
-
}
|
|
189
|
+
this.topics.add(topic);
|
|
169
190
|
}
|
|
170
191
|
}
|
|
171
192
|
// Register message interceptors provided by the user (allowed before startup only).
|
|
@@ -190,6 +211,17 @@ export abstract class BaseClient {
|
|
|
190
211
|
const err = new Error(`Startup the rocketmq client failed, clientId=${this.clientId}, error=${e}`);
|
|
191
212
|
this.logger.error(err);
|
|
192
213
|
err.cause = e;
|
|
214
|
+
// Release everything acquired by the failed startup before surfacing
|
|
215
|
+
// the error, mirroring Java PushConsumerImpl#startUp which calls
|
|
216
|
+
// shutDown() before rethrowing. Otherwise the route/settings/heartbeat
|
|
217
|
+
// timers and telemetry sessions keep running on an instance whose
|
|
218
|
+
// startup() rejected and which the caller is about to abandon.
|
|
219
|
+
try {
|
|
220
|
+
await this.shutdown();
|
|
221
|
+
} catch (cleanupError) {
|
|
222
|
+
this.logger.warn('Failed to clean up after the startup failure, clientId=%s, error=%s',
|
|
223
|
+
this.clientId, cleanupError instanceof Error ? cleanupError.message : String(cleanupError));
|
|
224
|
+
}
|
|
193
225
|
throw err;
|
|
194
226
|
}
|
|
195
227
|
this.logger.info('Startup the rocketmq client successfully, clientId=%s', this.clientId);
|
|
@@ -199,86 +231,132 @@ export abstract class BaseClient {
|
|
|
199
231
|
this.logger.info('Begin to execute startup flow, clientId=%s, topics=%d',
|
|
200
232
|
this.clientId, this.topics.size);
|
|
201
233
|
|
|
202
|
-
//
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
await this.sleep(backoffMs);
|
|
223
|
-
} else {
|
|
224
|
-
this.logger.error('Fetch topic route failed after %d attempts, clientId=%s, error=%s',
|
|
225
|
-
maxAttempts, this.clientId, e instanceof Error ? e.message : String(e));
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
if (lastError && !this.topicRouteCache.size) {
|
|
231
|
-
throw new Error(`Failed to fetch topic routes after ${maxAttempts} attempts`, { cause: lastError });
|
|
232
|
-
}
|
|
234
|
+
// Create the first-settings promise before any await: updateRoutes() below opens
|
|
235
|
+
// telemetry sessions and syncs settings while it is still iterating the topics, so a
|
|
236
|
+
// server-pushed Settings callback can fire long before the code below reaches the
|
|
237
|
+
// await. Registering the resolvers only at the await point would silently drop that
|
|
238
|
+
// first callback and leave startup hanging until the periodic settings sync fires
|
|
239
|
+
// (5 minutes later).
|
|
240
|
+
const firstSettingsPromise = this.topics.size > 0
|
|
241
|
+
? new Promise<void>((resolve, reject) => {
|
|
242
|
+
this.#startupReject = reject;
|
|
243
|
+
this.#startupResolve = resolve;
|
|
244
|
+
})
|
|
245
|
+
: undefined;
|
|
246
|
+
// A status-only telemetry command can reach onUnknownCommand() while
|
|
247
|
+
// updateRoutes() (or its retry backoff) is still pending, rejecting this
|
|
248
|
+
// promise before the await below attaches a rejection handler. Under
|
|
249
|
+
// Node.js's default unhandled-rejection behavior that terminates the
|
|
250
|
+
// process even though startup() would surface the error to its caller,
|
|
251
|
+
// so attach a no-op handler immediately; the await below still observes
|
|
252
|
+
// the error and propagates it to the caller.
|
|
253
|
+
void firstSettingsPromise?.catch(() => undefined);
|
|
233
254
|
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
this.
|
|
255
|
+
try {
|
|
256
|
+
// Fetch topic route with retry mechanism (align with Java client)
|
|
257
|
+
const maxAttempts = 3;
|
|
258
|
+
let fetched = false;
|
|
259
|
+
let lastError: Error | undefined;
|
|
260
|
+
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
|
|
261
|
+
try {
|
|
262
|
+
await this.updateRoutes();
|
|
263
|
+
this.logger.info('Fetch topic route successfully during startup, clientId=%s, attempt=%d',
|
|
264
|
+
this.clientId, attempt);
|
|
265
|
+
fetched = true;
|
|
266
|
+
break;
|
|
267
|
+
} catch (e) {
|
|
268
|
+
lastError = e as Error;
|
|
269
|
+
// Not-found errors will never succeed on retry — fail fast, aligned
|
|
270
|
+
// with the Java client which surfaces NotFoundException immediately.
|
|
271
|
+
if (e instanceof NotFoundException) {
|
|
272
|
+
throw e;
|
|
273
|
+
}
|
|
274
|
+
if (attempt < maxAttempts) {
|
|
275
|
+
const backoffMs = 1000 * attempt; // Simple linear backoff: 1s, 2s, 3s
|
|
276
|
+
this.logger.warn('Fetch topic route failed during startup, will retry, clientId=%s, attempt=%d/%d, error=%s, backoff=%dms',
|
|
277
|
+
this.clientId, attempt, maxAttempts, e instanceof Error ? e.message : String(e), backoffMs);
|
|
278
|
+
await this.sleep(backoffMs);
|
|
279
|
+
} else {
|
|
280
|
+
this.logger.error('Fetch topic route failed after %d attempts, clientId=%s, error=%s',
|
|
281
|
+
maxAttempts, this.clientId, e instanceof Error ? e.message : String(e));
|
|
282
|
+
}
|
|
283
|
+
}
|
|
242
284
|
}
|
|
243
|
-
}, 30000));
|
|
244
285
|
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
} catch (e) {
|
|
250
|
-
this.logger.error('Failed to sync settings, clientId=%s, error=%s',
|
|
251
|
-
this.clientId, e instanceof Error ? e.message : String(e));
|
|
286
|
+
// Every topic must be routable, mirroring Java ClientImpl#startUp which fails
|
|
287
|
+
// after the last attempt even if some topics were fetched successfully.
|
|
288
|
+
if (!fetched) {
|
|
289
|
+
throw new Error(`Failed to fetch topic routes after ${maxAttempts} attempts`, { cause: lastError });
|
|
252
290
|
}
|
|
253
|
-
}, 5 * 60000));
|
|
254
|
-
|
|
255
|
-
// heartbeat every 10s
|
|
256
|
-
this.#timers.push(setInterval(async () => {
|
|
257
|
-
this.#doHeartbeat();
|
|
258
|
-
}, 10000));
|
|
259
291
|
|
|
260
|
-
|
|
261
|
-
|
|
292
|
+
this.logger.info('Topic routes updated, clientId=%s', this.clientId);
|
|
293
|
+
// update topic route every 30s
|
|
294
|
+
this.#timers.push(setInterval(async () => {
|
|
295
|
+
try {
|
|
296
|
+
await this.updateRoutes();
|
|
297
|
+
} catch (e) {
|
|
298
|
+
this.logger.error('Failed to update routes periodically, clientId=%s, error=%s',
|
|
299
|
+
this.clientId, e instanceof Error ? e.message : String(e));
|
|
300
|
+
}
|
|
301
|
+
}, 30000));
|
|
262
302
|
|
|
263
|
-
|
|
264
|
-
this.
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
303
|
+
// sync settings every 5m
|
|
304
|
+
this.#timers.push(setInterval(async () => {
|
|
305
|
+
try {
|
|
306
|
+
this.#syncSettings();
|
|
307
|
+
} catch (e) {
|
|
308
|
+
this.logger.error('Failed to sync settings, clientId=%s, error=%s',
|
|
309
|
+
this.clientId, e instanceof Error ? e.message : String(e));
|
|
310
|
+
}
|
|
311
|
+
}, 5 * 60000));
|
|
312
|
+
|
|
313
|
+
// heartbeat every 10s
|
|
314
|
+
this.#timers.push(setInterval(async () => {
|
|
315
|
+
this.doHeartbeat();
|
|
316
|
+
}, 10000));
|
|
317
|
+
|
|
318
|
+
if (firstSettingsPromise) {
|
|
319
|
+
this.logger.info('Waiting for first onSettingsCommand, clientId=%s, timeout=%dms',
|
|
320
|
+
this.clientId, this.firstSettingsTimeoutMs);
|
|
321
|
+
await this.#awaitFirstSettings(firstSettingsPromise);
|
|
322
|
+
this.logger.info('Received first onSettingsCommand, clientId=%s', this.clientId);
|
|
323
|
+
}
|
|
324
|
+
this.#running = true;
|
|
325
|
+
} finally {
|
|
326
|
+
// Never leave the resolvers dangling across exits: on the early-throw
|
|
327
|
+
// (route fetch failure) and first-settings rejection paths they would
|
|
328
|
+
// otherwise settle an abandoned promise long after startup is over.
|
|
272
329
|
this.#startupReject = undefined;
|
|
273
330
|
this.#startupResolve = undefined;
|
|
274
331
|
}
|
|
275
|
-
this.#running = true;
|
|
276
332
|
}
|
|
277
333
|
|
|
278
334
|
isRunning(): boolean {
|
|
279
335
|
return this.#running;
|
|
280
336
|
}
|
|
281
337
|
|
|
338
|
+
/**
|
|
339
|
+
* Bound the wait for the first server-pushed Settings command so a silent
|
|
340
|
+
* remote makes startup() reject with a clear error instead of hanging
|
|
341
|
+
* forever; the rejection also triggers the startup cleanup path.
|
|
342
|
+
*/
|
|
343
|
+
async #awaitFirstSettings(firstSettingsPromise: Promise<void>): Promise<void> {
|
|
344
|
+
let timer: NodeJS.Timeout | undefined;
|
|
345
|
+
try {
|
|
346
|
+
const timeoutPromise = new Promise<never>((_, reject) => {
|
|
347
|
+
timer = setTimeout(() => reject(new Error(
|
|
348
|
+
`Timed out waiting for the first settings command within ${this.firstSettingsTimeoutMs}ms, ` +
|
|
349
|
+
`clientId=${this.clientId}`,
|
|
350
|
+
)), this.firstSettingsTimeoutMs);
|
|
351
|
+
});
|
|
352
|
+
await Promise.race([ firstSettingsPromise, timeoutPromise ]);
|
|
353
|
+
} finally {
|
|
354
|
+
if (timer) {
|
|
355
|
+
clearTimeout(timer);
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
|
|
282
360
|
protected sleep(ms: number): Promise<void> {
|
|
283
361
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
284
362
|
}
|
|
@@ -316,14 +394,17 @@ export abstract class BaseClient {
|
|
|
316
394
|
this.isolated.clear();
|
|
317
395
|
|
|
318
396
|
this.logger.info('Shutdown the rocketmq client successfully, clientId=%s', this.clientId);
|
|
319
|
-
//
|
|
320
|
-
//
|
|
321
|
-
// after super.shutdown(), and egg-logger throws "log stream had been closed"
|
|
322
|
-
// if it is used again after close().
|
|
323
|
-
setImmediate(() => this.logger.close && this.logger.close());
|
|
397
|
+
// The default logger is shared by the process and an injected logger is owned
|
|
398
|
+
// by the caller, so the client closes neither; late callbacks may still log.
|
|
324
399
|
}
|
|
325
400
|
|
|
326
|
-
|
|
401
|
+
/**
|
|
402
|
+
* Send one heartbeat round to every route endpoint, mirroring Java
|
|
403
|
+
* ClientImpl#doHeartbeat. TypeScript-private (not #private) so regression
|
|
404
|
+
* tests can invoke it through a cast; it stays invisible on the public API
|
|
405
|
+
* surface (.d.ts exposes it as private and subclasses cannot access it).
|
|
406
|
+
*/
|
|
407
|
+
private async doHeartbeat() {
|
|
327
408
|
try {
|
|
328
409
|
const request = this.wrapHeartbeatRequest();
|
|
329
410
|
const endpointsList = this.getTotalRouteEndpoints();
|
|
@@ -331,29 +412,38 @@ export abstract class BaseClient {
|
|
|
331
412
|
debug('No endpoints available for heartbeat, clientId=%s', this.clientId);
|
|
332
413
|
return;
|
|
333
414
|
}
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
await this.rpcClientManager.heartbeat(endpoints, request, this.requestTimeout);
|
|
337
|
-
// Heartbeat succeeded: the remote is reachable again, so rejoin any
|
|
338
|
-
// isolated endpoints (mirrors Java ClientImpl#doHeartbeat).
|
|
339
|
-
if (this.isolated.delete(endpoints.facade)) {
|
|
340
|
-
this.logger.info('Isolated endpoints rejoined after successful heartbeat, endpoints=%s, clientId=%s',
|
|
341
|
-
endpoints.facade, this.clientId);
|
|
342
|
-
}
|
|
343
|
-
this.#heartbeatFailureCounts.delete(endpoints.facade);
|
|
344
|
-
} catch (e) {
|
|
345
|
-
// Log but don't throw - heartbeat is best-effort
|
|
346
|
-
this.logger.warn('Heartbeat failed for endpoints=%s, clientId=%s, error=%s',
|
|
347
|
-
endpoints.facade, this.clientId, e instanceof Error ? e.message : String(e));
|
|
348
|
-
this.#onHeartbeatFailure(endpoints);
|
|
349
|
-
}
|
|
350
|
-
}
|
|
415
|
+
// Send heartbeats concurrently so a slow endpoint does not block the others.
|
|
416
|
+
await Promise.all(endpointsList.map(endpoints => this.#heartbeat(endpoints, request)));
|
|
351
417
|
} catch (e) {
|
|
352
418
|
this.logger.error('Unexpected error in heartbeat, clientId=%s, error=%s',
|
|
353
419
|
this.clientId, e instanceof Error ? e.message : String(e));
|
|
354
420
|
}
|
|
355
421
|
}
|
|
356
422
|
|
|
423
|
+
async #heartbeat(endpoints: Endpoints, request: HeartbeatRequest) {
|
|
424
|
+
try {
|
|
425
|
+
const response = await this.rpcClientManager.heartbeat(endpoints, request, this.requestTimeout);
|
|
426
|
+
// Business-level errors (e.g. UNRECOGNIZED_CLIENT_TYPE when the
|
|
427
|
+
// remote no longer holds this client's settings) arrive as normal
|
|
428
|
+
// gRPC responses; without this check the client stays blind to
|
|
429
|
+
// settings loss and never re-registers. Mirrors Java
|
|
430
|
+
// ClientImpl#doHeartbeat which checks the response status.
|
|
431
|
+
StatusChecker.check(response.getStatus()?.toObject(), getRequestId(response));
|
|
432
|
+
// Heartbeat succeeded: the remote is reachable again, so rejoin any
|
|
433
|
+
// isolated endpoints (mirrors Java ClientImpl#doHeartbeat).
|
|
434
|
+
if (this.isolated.delete(endpoints.facade)) {
|
|
435
|
+
this.logger.info('Isolated endpoints rejoined after successful heartbeat, endpoints=%s, clientId=%s',
|
|
436
|
+
endpoints.facade, this.clientId);
|
|
437
|
+
}
|
|
438
|
+
this.#heartbeatFailureCounts.delete(endpoints.facade);
|
|
439
|
+
} catch (e) {
|
|
440
|
+
// Log but don't throw - heartbeat is best-effort
|
|
441
|
+
this.logger.warn('Heartbeat failed for endpoints=%s, clientId=%s, error=%s',
|
|
442
|
+
endpoints.facade, this.clientId, e instanceof Error ? e.message : String(e));
|
|
443
|
+
this.#onHeartbeatFailure(endpoints);
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
|
|
357
447
|
/**
|
|
358
448
|
* Handle a heartbeat failure: after consecutive failures, rebuild the transport
|
|
359
449
|
* layer for the affected endpoints (evict the stale RpcClient channel and refresh
|
|
@@ -459,12 +549,12 @@ export abstract class BaseClient {
|
|
|
459
549
|
|
|
460
550
|
async #fetchTopicRoute(topic: string) {
|
|
461
551
|
const req = new QueryRouteRequest();
|
|
462
|
-
req.setTopic(createResource(topic));
|
|
552
|
+
req.setTopic(createResource(topic, this.namespace));
|
|
463
553
|
req.setEndpoints(this.endpoints.toProtobuf());
|
|
464
554
|
|
|
465
555
|
try {
|
|
466
556
|
const response = await this.rpcClientManager.queryRoute(this.endpoints, req, this.requestTimeout);
|
|
467
|
-
StatusChecker.check(response.getStatus()?.toObject());
|
|
557
|
+
StatusChecker.check(response.getStatus()?.toObject(), getRequestId(response));
|
|
468
558
|
const topicRouteData = new TopicRouteData(response.getMessageQueuesList());
|
|
469
559
|
const newEndpoints = this.findNewRouteEndpoints(topicRouteData.getTotalEndpoints());
|
|
470
560
|
for (const endpoints of newEndpoints) {
|
|
@@ -577,13 +667,31 @@ export abstract class BaseClient {
|
|
|
577
667
|
|
|
578
668
|
/**
|
|
579
669
|
* Notify remote that current client is prepared to be terminated.
|
|
670
|
+
*
|
|
671
|
+
* Best-effort like Java ClientImpl#notifyClientTermination: a failing or
|
|
672
|
+
* unreachable endpoint is only logged, so shutdown always goes on to
|
|
673
|
+
* release the telemetry sessions, RPC channels and meters.
|
|
580
674
|
*/
|
|
581
675
|
async #notifyClientTermination() {
|
|
582
676
|
this.logger.info('Notify remote that client is terminated, clientId=%s', this.clientId);
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
677
|
+
let request: NotifyClientTerminationRequest;
|
|
678
|
+
try {
|
|
679
|
+
request = this.wrapNotifyClientTerminationRequest();
|
|
680
|
+
} catch (e) {
|
|
681
|
+
this.logger.error('[Bug] Failed to wrap notify client termination request, clientId=%s, error=%s',
|
|
682
|
+
this.clientId, e instanceof Error ? e.message : String(e));
|
|
683
|
+
return;
|
|
586
684
|
}
|
|
685
|
+
const endpointsList = this.getTotalRouteEndpoints();
|
|
686
|
+
const results = await Promise.allSettled(endpointsList.map(endpoints =>
|
|
687
|
+
this.rpcClientManager.notifyClientTermination(endpoints, request, this.requestTimeout)));
|
|
688
|
+
results.forEach((result, i) => {
|
|
689
|
+
if (result.status === 'rejected') {
|
|
690
|
+
const e = result.reason;
|
|
691
|
+
this.logger.warn('Failed to notify remote that client is terminated, clientId=%s, endpoints=%s, error=%s',
|
|
692
|
+
this.clientId, endpointsList[i].facade, e instanceof Error ? e.message : String(e));
|
|
693
|
+
}
|
|
694
|
+
});
|
|
587
695
|
}
|
|
588
696
|
|
|
589
697
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
@@ -593,7 +701,7 @@ export abstract class BaseClient {
|
|
|
593
701
|
|
|
594
702
|
onUnknownCommand(endpoints: Endpoints, status: Status.AsObject) {
|
|
595
703
|
try {
|
|
596
|
-
StatusChecker.check(status);
|
|
704
|
+
StatusChecker.check(status, getRequestId(status));
|
|
597
705
|
} catch (err) {
|
|
598
706
|
this.logger.error('Get error status from telemetry session, status=%j, endpoints=%j, clientId=%s',
|
|
599
707
|
status, endpoints, this.clientId);
|
|
@@ -706,9 +814,12 @@ export abstract class BaseClient {
|
|
|
706
814
|
* before the client is running, mirroring Java ClientImpl#addMessageInterceptor.
|
|
707
815
|
*/
|
|
708
816
|
addMessageInterceptor(messageInterceptor: MessageInterceptor) {
|
|
709
|
-
if (
|
|
710
|
-
this
|
|
817
|
+
if (this.isRunning()) {
|
|
818
|
+
this.logger.warn('Message interceptor can only be added before the client is running, ignore it, clientId=%s',
|
|
819
|
+
this.clientId);
|
|
820
|
+
return;
|
|
711
821
|
}
|
|
822
|
+
this.#compositedMessageInterceptor.addInterceptor(messageInterceptor);
|
|
712
823
|
}
|
|
713
824
|
|
|
714
825
|
/**
|
package/src/client/Logger.ts
CHANGED
|
@@ -27,10 +27,14 @@ export interface ILogger {
|
|
|
27
27
|
close?(...args: any[]): void;
|
|
28
28
|
}
|
|
29
29
|
|
|
30
|
+
let defaultLogger: EggLogger | undefined;
|
|
31
|
+
|
|
30
32
|
export function getDefaultLogger() {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
33
|
+
if (!defaultLogger) {
|
|
34
|
+
const file = path.join(homedir(), 'logs/rocketmq/rocketmq_client_nodejs.log');
|
|
35
|
+
// The shared logger is never closed, so disable buffering: its flush interval
|
|
36
|
+
// would keep the process alive and buffered lines would be lost on exit.
|
|
37
|
+
defaultLogger = new EggLogger({ file, level: 'INFO', buffer: false });
|
|
38
|
+
}
|
|
39
|
+
return defaultLogger;
|
|
36
40
|
}
|