rocketmq-client-nodejs-bate 1.0.12
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/LICENSE +201 -0
- package/README.md +186 -0
- package/dist/client/BaseClient.d.ts +95 -0
- package/dist/client/BaseClient.js +324 -0
- package/dist/client/ClientId.d.ts +24 -0
- package/dist/client/ClientId.js +33 -0
- package/dist/client/Logger.d.ts +25 -0
- package/dist/client/Logger.js +33 -0
- package/dist/client/RpcClient.d.ts +48 -0
- package/dist/client/RpcClient.js +219 -0
- package/dist/client/RpcClientManager.d.ts +42 -0
- package/dist/client/RpcClientManager.js +144 -0
- package/dist/client/SessionCredentials.d.ts +21 -0
- package/dist/client/SessionCredentials.js +19 -0
- package/dist/client/Settings.d.ts +31 -0
- package/dist/client/Settings.js +40 -0
- package/dist/client/TelemetrySession.d.ts +27 -0
- package/dist/client/TelemetrySession.js +110 -0
- package/dist/client/UserAgent.d.ts +25 -0
- package/dist/client/UserAgent.js +47 -0
- package/dist/client/index.d.ts +26 -0
- package/dist/client/index.js +43 -0
- package/dist/consumer/Assignment.d.ts +23 -0
- package/dist/consumer/Assignment.js +40 -0
- package/dist/consumer/Assignments.d.ts +24 -0
- package/dist/consumer/Assignments.js +47 -0
- package/dist/consumer/ConsumeResult.d.ts +20 -0
- package/dist/consumer/ConsumeResult.js +25 -0
- package/dist/consumer/ConsumeService.d.ts +28 -0
- package/dist/consumer/ConsumeService.js +49 -0
- package/dist/consumer/ConsumeTask.d.ts +24 -0
- package/dist/consumer/ConsumeTask.js +40 -0
- package/dist/consumer/Consumer.d.ts +38 -0
- package/dist/consumer/Consumer.js +111 -0
- package/dist/consumer/FifoConsumeService.d.ts +25 -0
- package/dist/consumer/FifoConsumeService.js +123 -0
- package/dist/consumer/FilterExpression.d.ts +25 -0
- package/dist/consumer/FilterExpression.js +40 -0
- package/dist/consumer/MessageListener.d.ts +21 -0
- package/dist/consumer/MessageListener.js +19 -0
- package/dist/consumer/ProcessQueue.d.ts +39 -0
- package/dist/consumer/ProcessQueue.js +286 -0
- package/dist/consumer/PushConsumer.d.ts +68 -0
- package/dist/consumer/PushConsumer.js +377 -0
- package/dist/consumer/PushSubscriptionSettings.d.ts +31 -0
- package/dist/consumer/PushSubscriptionSettings.js +97 -0
- package/dist/consumer/SimpleConsumer.d.ts +50 -0
- package/dist/consumer/SimpleConsumer.js +122 -0
- package/dist/consumer/SimpleSubscriptionSettings.d.ts +28 -0
- package/dist/consumer/SimpleSubscriptionSettings.js +57 -0
- package/dist/consumer/StandardConsumeService.d.ts +24 -0
- package/dist/consumer/StandardConsumeService.js +42 -0
- package/dist/consumer/SubscriptionLoadBalancer.d.ts +23 -0
- package/dist/consumer/SubscriptionLoadBalancer.js +46 -0
- package/dist/consumer/index.d.ts +32 -0
- package/dist/consumer/index.js +49 -0
- package/dist/exception/BadRequestException.d.ts +20 -0
- package/dist/exception/BadRequestException.js +28 -0
- package/dist/exception/ClientException.d.ts +20 -0
- package/dist/exception/ClientException.js +31 -0
- package/dist/exception/ForbiddenException.d.ts +20 -0
- package/dist/exception/ForbiddenException.js +28 -0
- package/dist/exception/InternalErrorException.d.ts +20 -0
- package/dist/exception/InternalErrorException.js +28 -0
- package/dist/exception/NotFoundException.d.ts +20 -0
- package/dist/exception/NotFoundException.js +28 -0
- package/dist/exception/PayloadTooLargeException.d.ts +20 -0
- package/dist/exception/PayloadTooLargeException.js +28 -0
- package/dist/exception/PaymentRequiredException.d.ts +20 -0
- package/dist/exception/PaymentRequiredException.js +28 -0
- package/dist/exception/ProxyTimeoutException.d.ts +20 -0
- package/dist/exception/ProxyTimeoutException.js +28 -0
- package/dist/exception/RequestHeaderFieldsTooLargeException.d.ts +20 -0
- package/dist/exception/RequestHeaderFieldsTooLargeException.js +28 -0
- package/dist/exception/StatusChecker.d.ts +20 -0
- package/dist/exception/StatusChecker.js +98 -0
- package/dist/exception/TooManyRequestsException.d.ts +20 -0
- package/dist/exception/TooManyRequestsException.js +28 -0
- package/dist/exception/UnauthorizedException.d.ts +20 -0
- package/dist/exception/UnauthorizedException.js +28 -0
- package/dist/exception/UnsupportedException.d.ts +20 -0
- package/dist/exception/UnsupportedException.js +28 -0
- package/dist/exception/index.d.ts +29 -0
- package/dist/exception/index.js +46 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.js +39 -0
- package/dist/message/Message.d.ts +38 -0
- package/dist/message/Message.js +57 -0
- package/dist/message/MessageId.d.ts +59 -0
- package/dist/message/MessageId.js +123 -0
- package/dist/message/MessageView.d.ts +38 -0
- package/dist/message/MessageView.js +90 -0
- package/dist/message/PublishingMessage.d.ts +30 -0
- package/dist/message/PublishingMessage.js +100 -0
- package/dist/message/index.d.ts +20 -0
- package/dist/message/index.js +37 -0
- package/dist/producer/Producer.d.ts +55 -0
- package/dist/producer/Producer.js +318 -0
- package/dist/producer/PublishingLoadBalancer.d.ts +24 -0
- package/dist/producer/PublishingLoadBalancer.js +82 -0
- package/dist/producer/PublishingSettings.d.ts +28 -0
- package/dist/producer/PublishingSettings.js +70 -0
- package/dist/producer/RecallReceipt.d.ts +25 -0
- package/dist/producer/RecallReceipt.js +34 -0
- package/dist/producer/SendReceipt.d.ts +29 -0
- package/dist/producer/SendReceipt.js +60 -0
- package/dist/producer/Transaction.d.ts +28 -0
- package/dist/producer/Transaction.js +70 -0
- package/dist/producer/TransactionChecker.d.ts +21 -0
- package/dist/producer/TransactionChecker.js +19 -0
- package/dist/producer/index.d.ts +22 -0
- package/dist/producer/index.js +40 -0
- package/dist/retry/ExponentialBackoffRetryPolicy.d.ts +27 -0
- package/dist/retry/ExponentialBackoffRetryPolicy.js +64 -0
- package/dist/retry/RetryPolicy.d.ts +46 -0
- package/dist/retry/RetryPolicy.js +19 -0
- package/dist/retry/index.d.ts +18 -0
- package/dist/retry/index.js +35 -0
- package/dist/route/Broker.d.ts +25 -0
- package/dist/route/Broker.js +40 -0
- package/dist/route/Endpoints.d.ts +30 -0
- package/dist/route/Endpoints.js +71 -0
- package/dist/route/MessageQueue.d.ts +27 -0
- package/dist/route/MessageQueue.js +47 -0
- package/dist/route/TopicRouteData.d.ts +24 -0
- package/dist/route/TopicRouteData.js +37 -0
- package/dist/route/index.d.ts +20 -0
- package/dist/route/index.js +37 -0
- package/dist/util/index.d.ts +35 -0
- package/dist/util/index.js +88 -0
- package/package.json +62 -0
- package/proto/apache/rocketmq/v2/admin.proto +43 -0
- package/proto/apache/rocketmq/v2/admin_grpc_pb.d.ts +41 -0
- package/proto/apache/rocketmq/v2/admin_grpc_pb.js +60 -0
- package/proto/apache/rocketmq/v2/admin_pb.d.ts +56 -0
- package/proto/apache/rocketmq/v2/admin_pb.js +340 -0
- package/proto/apache/rocketmq/v2/definition.proto +570 -0
- package/proto/apache/rocketmq/v2/definition_grpc_pb.js +1 -0
- package/proto/apache/rocketmq/v2/definition_pb.d.ts +885 -0
- package/proto/apache/rocketmq/v2/definition_pb.js +6141 -0
- package/proto/apache/rocketmq/v2/service.proto +443 -0
- package/proto/apache/rocketmq/v2/service_grpc_pb.d.ts +294 -0
- package/proto/apache/rocketmq/v2/service_grpc_pb.js +637 -0
- package/proto/apache/rocketmq/v2/service_pb.d.ts +1249 -0
- package/proto/apache/rocketmq/v2/service_pb.js +9723 -0
- package/src/client/BaseClient.ts +404 -0
- package/src/client/ClientId.ts +31 -0
- package/src/client/Logger.ts +36 -0
- package/src/client/RpcClient.ts +258 -0
- package/src/client/RpcClientManager.ts +180 -0
- package/src/client/SessionCredentials.ts +22 -0
- package/src/client/Settings.ts +46 -0
- package/src/client/TelemetrySession.ts +130 -0
- package/src/client/UserAgent.ts +45 -0
- package/src/client/index.ts +27 -0
- package/src/consumer/Assignment.ts +39 -0
- package/src/consumer/Assignments.ts +46 -0
- package/src/consumer/ConsumeResult.ts +21 -0
- package/src/consumer/ConsumeService.ts +54 -0
- package/src/consumer/ConsumeTask.ts +40 -0
- package/src/consumer/Consumer.ts +129 -0
- package/src/consumer/FifoConsumeService.ts +136 -0
- package/src/consumer/FilterExpression.ts +42 -0
- package/src/consumer/MessageListener.ts +23 -0
- package/src/consumer/ProcessQueue.ts +326 -0
- package/src/consumer/PushConsumer.ts +473 -0
- package/src/consumer/PushSubscriptionSettings.ts +123 -0
- package/src/consumer/SimpleConsumer.ts +153 -0
- package/src/consumer/SimpleSubscriptionSettings.ts +64 -0
- package/src/consumer/StandardConsumeService.ts +44 -0
- package/src/consumer/SubscriptionLoadBalancer.ts +47 -0
- package/src/consumer/index.ts +33 -0
- package/src/exception/BadRequestException.ts +25 -0
- package/src/exception/ClientException.ts +29 -0
- package/src/exception/ForbiddenException.ts +25 -0
- package/src/exception/InternalErrorException.ts +25 -0
- package/src/exception/NotFoundException.ts +25 -0
- package/src/exception/PayloadTooLargeException.ts +25 -0
- package/src/exception/PaymentRequiredException.ts +25 -0
- package/src/exception/ProxyTimeoutException.ts +25 -0
- package/src/exception/RequestHeaderFieldsTooLargeException.ts +25 -0
- package/src/exception/StatusChecker.ts +94 -0
- package/src/exception/TooManyRequestsException.ts +25 -0
- package/src/exception/UnauthorizedException.ts +25 -0
- package/src/exception/UnsupportedException.ts +25 -0
- package/src/exception/index.ts +30 -0
- package/src/index.ts +23 -0
- package/src/message/Message.ts +67 -0
- package/src/message/MessageId.ts +123 -0
- package/src/message/MessageView.ts +94 -0
- package/src/message/PublishingMessage.ts +104 -0
- package/src/message/index.ts +21 -0
- package/src/producer/Producer.ts +388 -0
- package/src/producer/PublishingLoadBalancer.ts +85 -0
- package/src/producer/PublishingSettings.ts +78 -0
- package/src/producer/RecallReceipt.ts +32 -0
- package/src/producer/SendReceipt.ts +63 -0
- package/src/producer/Transaction.ts +86 -0
- package/src/producer/TransactionChecker.ts +23 -0
- package/src/producer/index.ts +24 -0
- package/src/retry/ExponentialBackoffRetryPolicy.ts +76 -0
- package/src/retry/RetryPolicy.ts +51 -0
- package/src/retry/index.ts +19 -0
- package/src/route/Broker.ts +39 -0
- package/src/route/Endpoints.ts +70 -0
- package/src/route/MessageQueue.ts +49 -0
- package/src/route/TopicRouteData.ts +38 -0
- package/src/route/index.ts +21 -0
- package/src/util/index.ts +83 -0
|
@@ -0,0 +1,404 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
3
|
+
* contributor license agreements. See the NOTICE file distributed with
|
|
4
|
+
* this work for additional information regarding copyright ownership.
|
|
5
|
+
* The ASF licenses this file to You under the Apache License, Version 2.0
|
|
6
|
+
* (the "License"); you may not use this file except in compliance with
|
|
7
|
+
* the License. You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { debuglog } from 'node:util';
|
|
19
|
+
import { randomUUID } from 'node:crypto';
|
|
20
|
+
import { Metadata } from '@grpc/grpc-js';
|
|
21
|
+
import {
|
|
22
|
+
Settings as SettingsPB,
|
|
23
|
+
Status,
|
|
24
|
+
ClientType,
|
|
25
|
+
Code,
|
|
26
|
+
} from '../../proto/apache/rocketmq/v2/definition_pb';
|
|
27
|
+
import {
|
|
28
|
+
QueryRouteRequest,
|
|
29
|
+
RecoverOrphanedTransactionCommand,
|
|
30
|
+
VerifyMessageCommand,
|
|
31
|
+
PrintThreadStackTraceCommand,
|
|
32
|
+
TelemetryCommand,
|
|
33
|
+
ThreadStackTrace,
|
|
34
|
+
HeartbeatRequest,
|
|
35
|
+
NotifyClientTerminationRequest,
|
|
36
|
+
} from '../../proto/apache/rocketmq/v2/service_pb';
|
|
37
|
+
import { createResource, getRequestDateTime, sign } from '../util';
|
|
38
|
+
import { TopicRouteData, Endpoints } from '../route';
|
|
39
|
+
import { ClientException, StatusChecker } from '../exception';
|
|
40
|
+
import { Settings } from './Settings';
|
|
41
|
+
import { UserAgent } from './UserAgent';
|
|
42
|
+
import { ILogger, getDefaultLogger } from './Logger';
|
|
43
|
+
import { SessionCredentials } from './SessionCredentials';
|
|
44
|
+
import { RpcClientManager } from './RpcClientManager';
|
|
45
|
+
import { TelemetrySession } from './TelemetrySession';
|
|
46
|
+
import { ClientId } from './ClientId';
|
|
47
|
+
|
|
48
|
+
const debug = debuglog('rocketmq-client-nodejs:client:BaseClient');
|
|
49
|
+
|
|
50
|
+
export interface BaseClientOptions {
|
|
51
|
+
sslEnabled?: boolean;
|
|
52
|
+
/**
|
|
53
|
+
* rocketmq cluster endpoints, e.g.:
|
|
54
|
+
* - 127.0.0.1:8081;127.0.0.1:8082
|
|
55
|
+
* - 127.0.0.1:8081
|
|
56
|
+
* - example.com
|
|
57
|
+
* - example.com:8443
|
|
58
|
+
*/
|
|
59
|
+
endpoints: string;
|
|
60
|
+
namespace: string;
|
|
61
|
+
sessionCredentials?: SessionCredentials;
|
|
62
|
+
requestTimeout?: number;
|
|
63
|
+
logger?: ILogger;
|
|
64
|
+
topics?: string[];
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* RocketMQ Base Client, Consumer and Producer should extends this class
|
|
69
|
+
*
|
|
70
|
+
* it handle:
|
|
71
|
+
* - RpcClient lifecycle, e.g: cleanup the idle clients
|
|
72
|
+
* - startup flow
|
|
73
|
+
* - periodic Task
|
|
74
|
+
*/
|
|
75
|
+
export abstract class BaseClient {
|
|
76
|
+
readonly clientId = ClientId.create();
|
|
77
|
+
readonly clientType = ClientType.CLIENT_TYPE_UNSPECIFIED;
|
|
78
|
+
readonly sslEnabled: boolean;
|
|
79
|
+
readonly #sessionCredentials?: SessionCredentials;
|
|
80
|
+
readonly namespace: string;
|
|
81
|
+
protected readonly endpoints: Endpoints;
|
|
82
|
+
protected readonly isolated = new Map<string, Endpoints>();
|
|
83
|
+
protected readonly requestTimeout: number;
|
|
84
|
+
protected readonly topics = new Set<string>();
|
|
85
|
+
protected readonly topicRouteCache = new Map<string, TopicRouteData>();
|
|
86
|
+
protected readonly logger: ILogger;
|
|
87
|
+
protected readonly rpcClientManager: RpcClientManager;
|
|
88
|
+
readonly #telemetrySessions = new Map<string, TelemetrySession>();
|
|
89
|
+
#startupResolve?: () => void;
|
|
90
|
+
#startupReject?: (err: Error) => void;
|
|
91
|
+
#timers: NodeJS.Timeout[] = [];
|
|
92
|
+
#running = false;
|
|
93
|
+
|
|
94
|
+
constructor(options: BaseClientOptions) {
|
|
95
|
+
this.logger = options.logger ?? getDefaultLogger();
|
|
96
|
+
this.sslEnabled = options.sslEnabled === true;
|
|
97
|
+
this.endpoints = new Endpoints(options.endpoints);
|
|
98
|
+
this.namespace = options.namespace;
|
|
99
|
+
this.#sessionCredentials = options.sessionCredentials;
|
|
100
|
+
// https://rocketmq.apache.org/docs/introduction/03limits/
|
|
101
|
+
// Default request timeout is 3000ms
|
|
102
|
+
this.requestTimeout = options.requestTimeout ?? 3000;
|
|
103
|
+
this.rpcClientManager = new RpcClientManager(this, this.logger);
|
|
104
|
+
if (options.topics) {
|
|
105
|
+
for (const topic of options.topics) {
|
|
106
|
+
this.topics.add(topic);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Startup flow
|
|
113
|
+
* https://github.com/apache/rocketmq-clients/blob/master/docs/workflow.md#startup
|
|
114
|
+
*/
|
|
115
|
+
async startup() {
|
|
116
|
+
this.logger.info('Begin to startup the rocketmq client, clientId=%s', this.clientId);
|
|
117
|
+
try {
|
|
118
|
+
await this.#startup();
|
|
119
|
+
} catch (e) {
|
|
120
|
+
const err = new Error(`Startup the rocketmq client failed, clientId=${this.clientId}, error=${e}`);
|
|
121
|
+
this.logger.error(err);
|
|
122
|
+
err.cause = e;
|
|
123
|
+
throw err;
|
|
124
|
+
}
|
|
125
|
+
this.logger.info('Startup the rocketmq client successfully, clientId=%s', this.clientId);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
async #startup() {
|
|
129
|
+
this.logger.info('Begin to execute startup flow, clientId=%s, topics=%d',
|
|
130
|
+
this.clientId, this.topics.size);
|
|
131
|
+
// fetch topic route
|
|
132
|
+
await this.updateRoutes();
|
|
133
|
+
this.logger.info('Topic routes updated, clientId=%s', this.clientId);
|
|
134
|
+
// update topic route every 30s
|
|
135
|
+
this.#timers.push(setInterval(async () => {
|
|
136
|
+
this.updateRoutes();
|
|
137
|
+
}, 30000));
|
|
138
|
+
|
|
139
|
+
// sync settings every 5m
|
|
140
|
+
this.#timers.push(setInterval(async () => {
|
|
141
|
+
this.#syncSettings();
|
|
142
|
+
}, 5 * 60000));
|
|
143
|
+
|
|
144
|
+
// heartbeat every 10s
|
|
145
|
+
this.#timers.push(setInterval(async () => {
|
|
146
|
+
this.#doHeartbeat();
|
|
147
|
+
}, 10000));
|
|
148
|
+
|
|
149
|
+
// doStats every 60s
|
|
150
|
+
// doStats()
|
|
151
|
+
|
|
152
|
+
if (this.topics.size > 0) {
|
|
153
|
+
this.logger.info('Waiting for first onSettingsCommand, clientId=%s', this.clientId);
|
|
154
|
+
// wait for this first onSettingsCommand call
|
|
155
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
156
|
+
await new Promise<void>((resolve, reject) => {
|
|
157
|
+
this.#startupReject = reject;
|
|
158
|
+
this.#startupResolve = resolve;
|
|
159
|
+
});
|
|
160
|
+
this.logger.info('Received first onSettingsCommand, clientId=%s', this.clientId);
|
|
161
|
+
this.#startupReject = undefined;
|
|
162
|
+
this.#startupResolve = undefined;
|
|
163
|
+
}
|
|
164
|
+
this.#running = true;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
isRunning(): boolean {
|
|
168
|
+
return this.#running;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
async shutdown() {
|
|
172
|
+
this.logger.info('Begin to shutdown the rocketmq client, clientId=%s', this.clientId);
|
|
173
|
+
this.#running = false;
|
|
174
|
+
while (this.#timers.length > 0) {
|
|
175
|
+
const timer = this.#timers.pop();
|
|
176
|
+
clearInterval(timer);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
await this.#notifyClientTermination();
|
|
180
|
+
|
|
181
|
+
this.logger.info('Begin to release all telemetry sessions, clientId=%s', this.clientId);
|
|
182
|
+
this.#releaseTelemetrySessions();
|
|
183
|
+
this.logger.info('Release all telemetry sessions successfully, clientId=%s', this.clientId);
|
|
184
|
+
|
|
185
|
+
this.rpcClientManager.close();
|
|
186
|
+
this.logger.info('Shutdown the rocketmq client successfully, clientId=%s', this.clientId);
|
|
187
|
+
this.logger.close && this.logger.close();
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
async #doHeartbeat() {
|
|
191
|
+
const request = this.wrapHeartbeatRequest();
|
|
192
|
+
for (const endpoints of this.getTotalRouteEndpoints()) {
|
|
193
|
+
await this.rpcClientManager.heartbeat(endpoints, request, this.requestTimeout);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
#getTotalRouteEndpointsMap() {
|
|
198
|
+
const endpointsMap = new Map<string, Endpoints>();
|
|
199
|
+
for (const topicRoute of this.topicRouteCache.values()) {
|
|
200
|
+
for (const endpoints of topicRoute.getTotalEndpoints()) {
|
|
201
|
+
endpointsMap.set(endpoints.facade, endpoints);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
return endpointsMap;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
protected getTotalRouteEndpoints() {
|
|
208
|
+
const endpointsMap = this.#getTotalRouteEndpointsMap();
|
|
209
|
+
return Array.from(endpointsMap.values());
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
protected findNewRouteEndpoints(endpointsList: Endpoints[]) {
|
|
213
|
+
const endpointsMap = this.#getTotalRouteEndpointsMap();
|
|
214
|
+
const newEndpoints: Endpoints[] = [];
|
|
215
|
+
for (const endpoints of endpointsList) {
|
|
216
|
+
if (!endpointsMap.has(endpoints.facade)) {
|
|
217
|
+
newEndpoints.push(endpoints);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
return newEndpoints;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
protected async updateRoutes() {
|
|
224
|
+
for (const topic of this.topics) {
|
|
225
|
+
await this.#fetchTopicRoute(topic);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
protected async getRouteData(topic: string) {
|
|
230
|
+
let topicRouteData = this.topicRouteCache.get(topic);
|
|
231
|
+
if (!topicRouteData) {
|
|
232
|
+
this.topics.add(topic);
|
|
233
|
+
topicRouteData = await this.#fetchTopicRoute(topic);
|
|
234
|
+
}
|
|
235
|
+
return topicRouteData;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
async #fetchTopicRoute(topic: string) {
|
|
239
|
+
const req = new QueryRouteRequest();
|
|
240
|
+
req.setTopic(createResource(topic));
|
|
241
|
+
req.setEndpoints(this.endpoints.toProtobuf());
|
|
242
|
+
const response = await this.rpcClientManager.queryRoute(this.endpoints, req, this.requestTimeout);
|
|
243
|
+
StatusChecker.check(response.getStatus()?.toObject());
|
|
244
|
+
const topicRouteData = new TopicRouteData(response.getMessageQueuesList());
|
|
245
|
+
const newEndpoints = this.findNewRouteEndpoints(topicRouteData.getTotalEndpoints());
|
|
246
|
+
for (const endpoints of newEndpoints) {
|
|
247
|
+
// sync current settings to new endpoints
|
|
248
|
+
this.getTelemetrySession(endpoints).syncSettings();
|
|
249
|
+
}
|
|
250
|
+
this.topicRouteCache.set(topic, topicRouteData);
|
|
251
|
+
this.onTopicRouteDataUpdate(topic, topicRouteData);
|
|
252
|
+
debug('fetchTopicRoute topic=%o topicRouteData=%j', topic, topicRouteData);
|
|
253
|
+
return topicRouteData;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
#syncSettings() {
|
|
257
|
+
const command = this.settingsCommand();
|
|
258
|
+
for (const endpoints of this.getTotalRouteEndpoints()) {
|
|
259
|
+
this.telemetry(endpoints, command);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
settingsCommand() {
|
|
264
|
+
const command = new TelemetryCommand();
|
|
265
|
+
command.setSettings(this.getSettings().toProtobuf());
|
|
266
|
+
return command;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
getTelemetrySession(endpoints: Endpoints) {
|
|
270
|
+
let session = this.#telemetrySessions.get(endpoints.facade);
|
|
271
|
+
if (!session) {
|
|
272
|
+
session = new TelemetrySession(this, endpoints, this.logger);
|
|
273
|
+
this.#telemetrySessions.set(endpoints.facade, session);
|
|
274
|
+
}
|
|
275
|
+
return session;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
createTelemetryStream(endpoints: Endpoints) {
|
|
279
|
+
const metadata = this.getRequestMetadata();
|
|
280
|
+
return this.rpcClientManager.telemetry(endpoints, metadata);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
telemetry(endpoints: Endpoints, command: TelemetryCommand) {
|
|
284
|
+
this.getTelemetrySession(endpoints).write(command);
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
getRequestMetadata() {
|
|
288
|
+
// https://github.com/apache/rocketmq-clients/blob/master/docs/transport.md
|
|
289
|
+
// Transport Header
|
|
290
|
+
const metadata = new Metadata();
|
|
291
|
+
// version of protocol
|
|
292
|
+
metadata.set('x-mq-protocol', 'v2');
|
|
293
|
+
// client unique identifier: mbp@78774@2@3549a8wsr
|
|
294
|
+
metadata.set('x-mq-client-id', this.clientId);
|
|
295
|
+
// current timestamp: 20210309T195445Z, DATE_TIME_FORMAT = "yyyyMMdd'T'HHmmss'Z'"
|
|
296
|
+
const dateTime = getRequestDateTime();
|
|
297
|
+
metadata.set('x-mq-date-time', dateTime);
|
|
298
|
+
// request id for each gRPC header: f122a1e0-dbcf-4ca4-9db7-221903354be7
|
|
299
|
+
metadata.set('x-mq-request-id', randomUUID());
|
|
300
|
+
// language of client
|
|
301
|
+
// FIXME: java.lang.IllegalArgumentException: No enum constant org.apache.rocketmq.remoting.protocol.LanguageCode.nodejs
|
|
302
|
+
// https://github.com/apache/rocketmq/blob/master/remoting/src/main/java/org/apache/rocketmq/remoting/protocol/LanguageCode.java
|
|
303
|
+
metadata.set('x-mq-language', 'HTTP');
|
|
304
|
+
// version of client
|
|
305
|
+
metadata.set('x-mq-client-version', UserAgent.INSTANCE.version);
|
|
306
|
+
if (this.namespace) {
|
|
307
|
+
metadata.set('x-mq-namespace', this.namespace);
|
|
308
|
+
}
|
|
309
|
+
if (this.#sessionCredentials) {
|
|
310
|
+
if (this.#sessionCredentials.securityToken) {
|
|
311
|
+
metadata.set('x-mq-session-token', this.#sessionCredentials.securityToken);
|
|
312
|
+
}
|
|
313
|
+
const signature = sign(this.#sessionCredentials.accessSecret, dateTime);
|
|
314
|
+
const authorization = `MQv2-HMAC-SHA1 Credential=${this.#sessionCredentials.accessKey}, SignedHeaders=x-mq-date-time, Signature=${signature}`;
|
|
315
|
+
metadata.set('authorization', authorization);
|
|
316
|
+
}
|
|
317
|
+
return metadata;
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
protected abstract getSettings(): Settings;
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* Wrap heartbeat request
|
|
324
|
+
*/
|
|
325
|
+
protected abstract wrapHeartbeatRequest(): HeartbeatRequest;
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* Wrap notify client termination request.
|
|
329
|
+
*/
|
|
330
|
+
protected abstract wrapNotifyClientTerminationRequest(): NotifyClientTerminationRequest;
|
|
331
|
+
|
|
332
|
+
#releaseTelemetrySessions() {
|
|
333
|
+
for (const session of this.#telemetrySessions.values()) {
|
|
334
|
+
session.release();
|
|
335
|
+
}
|
|
336
|
+
this.#telemetrySessions.clear();
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* Notify remote that current client is prepared to be terminated.
|
|
341
|
+
*/
|
|
342
|
+
async #notifyClientTermination() {
|
|
343
|
+
this.logger.info('Notify remote that client is terminated, clientId=%s', this.clientId);
|
|
344
|
+
const request = this.wrapNotifyClientTerminationRequest();
|
|
345
|
+
for (const endpoints of this.getTotalRouteEndpoints()) {
|
|
346
|
+
await this.rpcClientManager.notifyClientTermination(endpoints, request, this.requestTimeout);
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
351
|
+
protected onTopicRouteDataUpdate(_topic: string, _topicRouteData: TopicRouteData) {
|
|
352
|
+
// sub class can monitor topic route data change here
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
onUnknownCommand(endpoints: Endpoints, status: Status.AsObject) {
|
|
356
|
+
try {
|
|
357
|
+
StatusChecker.check(status);
|
|
358
|
+
} catch (err) {
|
|
359
|
+
this.logger.error('Get error status from telemetry session, status=%j, endpoints=%j, clientId=%s',
|
|
360
|
+
status, endpoints, this.clientId);
|
|
361
|
+
this.#startupReject && this.#startupReject(err as ClientException);
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
onSettingsCommand(_endpoints: Endpoints, settings: SettingsPB) {
|
|
366
|
+
this.logger.info('Received settings command, clientId=%s, settings=%j',
|
|
367
|
+
this.clientId, settings.toObject());
|
|
368
|
+
// final Metric metric = new Metric(settings.getMetric());
|
|
369
|
+
// clientMeterManager.reset(metric);
|
|
370
|
+
this.getSettings().sync(settings);
|
|
371
|
+
this.logger.info('Sync settings=%j, clientId=%s', this.getSettings(), this.clientId);
|
|
372
|
+
this.#startupResolve && this.#startupResolve();
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
onRecoverOrphanedTransactionCommand(_endpoints: Endpoints, command: RecoverOrphanedTransactionCommand) {
|
|
376
|
+
this.logger.warn('Ignore orphaned transaction recovery command from remote, which is not expected, clientId=%s, command=%j',
|
|
377
|
+
this.clientId, command.toObject());
|
|
378
|
+
// const telemetryCommand = new TelemetryCommand();
|
|
379
|
+
// telemetryCommand.setStatus(new Status().setCode(Code.NOT_IMPLEMENTED));
|
|
380
|
+
// telemetryCommand.setRecoverOrphanedTransactionCommand(new RecoverOrphanedTransactionCommand());
|
|
381
|
+
// this.telemetry(endpoints, telemetryCommand);
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
onVerifyMessageCommand(endpoints: Endpoints, command: VerifyMessageCommand) {
|
|
385
|
+
const obj = command.toObject();
|
|
386
|
+
this.logger.warn('Ignore verify message command from remote, which is not expected, clientId=%s, command=%j',
|
|
387
|
+
this.clientId, obj);
|
|
388
|
+
const telemetryCommand = new TelemetryCommand();
|
|
389
|
+
telemetryCommand.setStatus(new Status().setCode(Code.NOT_IMPLEMENTED));
|
|
390
|
+
telemetryCommand.setVerifyMessageCommand(new VerifyMessageCommand().setNonce(obj.nonce));
|
|
391
|
+
this.telemetry(endpoints, telemetryCommand);
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
onPrintThreadStackTraceCommand(endpoints: Endpoints, command: PrintThreadStackTraceCommand) {
|
|
395
|
+
const obj = command.toObject();
|
|
396
|
+
this.logger.warn('Ignore orphaned transaction recovery command from remote, which is not expected, clientId=%s, command=%j',
|
|
397
|
+
this.clientId, obj);
|
|
398
|
+
const nonce = obj.nonce;
|
|
399
|
+
const telemetryCommand = new TelemetryCommand();
|
|
400
|
+
telemetryCommand.setThreadStackTrace(new ThreadStackTrace().setThreadStackTrace('mock stack').setNonce(nonce));
|
|
401
|
+
telemetryCommand.setStatus(new Status().setCode(Code.OK));
|
|
402
|
+
this.telemetry(endpoints, telemetryCommand);
|
|
403
|
+
}
|
|
404
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
3
|
+
* contributor license agreements. See the NOTICE file distributed with
|
|
4
|
+
* this work for additional information regarding copyright ownership.
|
|
5
|
+
* The ASF licenses this file to You under the Apache License, Version 2.0
|
|
6
|
+
* (the "License"); you may not use this file except in compliance with
|
|
7
|
+
* the License. You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import { hostname } from 'node:os';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Client Identifier Helper
|
|
22
|
+
* https://github.com/apache/rocketmq-clients/blob/master/docs/design.md#client-identifier
|
|
23
|
+
*/
|
|
24
|
+
export class ClientId {
|
|
25
|
+
static #hostname = hostname();
|
|
26
|
+
static #index = 0n;
|
|
27
|
+
|
|
28
|
+
static create() {
|
|
29
|
+
return `${this.#hostname}@${process.pid}@${this.#index++}@${Date.now().toString(36)}`;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
3
|
+
* contributor license agreements. See the NOTICE file distributed with
|
|
4
|
+
* this work for additional information regarding copyright ownership.
|
|
5
|
+
* The ASF licenses this file to You under the Apache License, Version 2.0
|
|
6
|
+
* (the "License"); you may not use this file except in compliance with
|
|
7
|
+
* the License. You may obtain a copy of the License at
|
|
8
|
+
*
|
|
9
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
*
|
|
11
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
* See the License for the specific language governing permissions and
|
|
15
|
+
* limitations under the License.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
import path from 'node:path';
|
|
19
|
+
import { homedir } from 'node:os';
|
|
20
|
+
import { EggLogger } from 'egg-logger';
|
|
21
|
+
|
|
22
|
+
export interface ILogger {
|
|
23
|
+
info(...args: any[]): void;
|
|
24
|
+
warn(...args: any[]): void;
|
|
25
|
+
error(...args: any[]): void;
|
|
26
|
+
debug?(...args: any[]): void;
|
|
27
|
+
close?(...args: any[]): void;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function getDefaultLogger() {
|
|
31
|
+
const file = path.join(homedir(), 'logs/rocketmq/rocketmq_client_nodejs.log');
|
|
32
|
+
return new EggLogger({
|
|
33
|
+
file,
|
|
34
|
+
level: 'INFO',
|
|
35
|
+
});
|
|
36
|
+
}
|