rocketmq-client-nodejs 1.0.3 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +84 -1
- package/dist/client/BaseClient.d.ts +24 -2
- package/dist/client/BaseClient.js +173 -28
- package/dist/client/ClientId.js +2 -2
- package/dist/client/Logger.d.ts +1 -0
- package/dist/client/Logger.js +1 -1
- package/dist/client/RpcClient.d.ts +3 -1
- package/dist/client/RpcClient.js +23 -1
- package/dist/client/RpcClientManager.d.ts +3 -1
- package/dist/client/RpcClientManager.js +23 -4
- package/dist/client/TelemetrySession.d.ts +1 -0
- package/dist/client/TelemetrySession.js +47 -9
- package/dist/consumer/Assignment.d.ts +24 -0
- package/dist/consumer/Assignment.js +40 -0
- package/dist/consumer/Assignments.d.ts +25 -0
- package/dist/consumer/Assignments.js +55 -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 +53 -0
- package/dist/consumer/ConsumeTask.d.ts +24 -0
- package/dist/consumer/ConsumeTask.js +39 -0
- package/dist/consumer/Consumer.d.ts +19 -1
- package/dist/consumer/Consumer.js +64 -21
- package/dist/consumer/FifoConsumeService.d.ts +25 -0
- package/dist/consumer/FifoConsumeService.js +130 -0
- package/dist/consumer/FilterExpression.d.ts +2 -0
- package/dist/consumer/FilterExpression.js +18 -1
- package/dist/consumer/LitePushConsumer.d.ts +161 -0
- package/dist/consumer/LitePushConsumer.js +172 -0
- package/dist/consumer/LitePushConsumerImpl.d.ts +167 -0
- package/dist/consumer/LitePushConsumerImpl.js +193 -0
- package/dist/consumer/LiteSubscriptionManager.d.ts +97 -0
- package/dist/consumer/LiteSubscriptionManager.js +238 -0
- package/dist/consumer/MessageListener.d.ts +21 -0
- package/dist/consumer/MessageListener.js +19 -0
- package/dist/consumer/OffsetOption.d.ts +109 -0
- package/dist/consumer/OffsetOption.js +191 -0
- package/dist/consumer/ProcessQueue.d.ts +39 -0
- package/dist/consumer/ProcessQueue.js +293 -0
- package/dist/consumer/PushConsumer.d.ts +88 -0
- package/dist/consumer/PushConsumer.js +449 -0
- package/dist/consumer/PushSubscriptionSettings.d.ts +31 -0
- package/dist/consumer/PushSubscriptionSettings.js +97 -0
- package/dist/consumer/SimpleConsumer.d.ts +12 -1
- package/dist/consumer/SimpleConsumer.js +54 -10
- package/dist/consumer/SimpleSubscriptionSettings.d.ts +2 -2
- package/dist/consumer/SimpleSubscriptionSettings.js +3 -3
- package/dist/consumer/StandardConsumeService.d.ts +25 -0
- package/dist/consumer/StandardConsumeService.js +50 -0
- package/dist/consumer/index.d.ts +15 -0
- package/dist/consumer/index.js +19 -1
- package/dist/exception/InternalErrorException.d.ts +5 -0
- package/dist/exception/InternalErrorException.js +10 -1
- package/dist/exception/LiteSubscriptionQuotaExceededException.d.ts +23 -0
- package/dist/exception/LiteSubscriptionQuotaExceededException.js +31 -0
- package/dist/exception/LiteTopicQuotaExceededException.d.ts +23 -0
- package/dist/exception/LiteTopicQuotaExceededException.js +31 -0
- package/dist/exception/StatusChecker.js +4 -1
- package/dist/exception/index.d.ts +2 -0
- package/dist/exception/index.js +3 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -1
- package/dist/message/Message.d.ts +8 -0
- package/dist/message/Message.js +39 -6
- package/dist/message/MessageId.d.ts +2 -0
- package/dist/message/MessageId.js +17 -1
- package/dist/message/MessageView.d.ts +86 -0
- package/dist/message/MessageView.js +115 -1
- package/dist/message/PublishingMessage.js +21 -5
- package/dist/producer/Producer.d.ts +21 -2
- package/dist/producer/Producer.js +120 -25
- package/dist/producer/PublishingLoadBalancer.d.ts +2 -0
- package/dist/producer/PublishingLoadBalancer.js +23 -1
- package/dist/producer/RecallReceipt.d.ts +25 -0
- package/dist/producer/RecallReceipt.js +34 -0
- package/dist/producer/SendReceipt.d.ts +2 -1
- package/dist/producer/SendReceipt.js +6 -3
- package/dist/producer/Transaction.js +7 -1
- package/dist/producer/index.d.ts +1 -0
- package/dist/producer/index.js +2 -1
- package/dist/route/Broker.d.ts +2 -0
- package/dist/route/Broker.js +21 -1
- package/dist/route/Endpoints.d.ts +2 -0
- package/dist/route/Endpoints.js +36 -1
- package/dist/route/MessageQueue.d.ts +2 -0
- package/dist/route/MessageQueue.js +23 -1
- package/dist/route/Resource.d.ts +54 -0
- package/dist/route/Resource.js +100 -0
- package/dist/route/TopicRouteData.d.ts +12 -0
- package/dist/route/TopicRouteData.js +37 -1
- package/dist/route/index.d.ts +1 -0
- package/dist/route/index.js +2 -1
- package/dist/util/index.d.ts +8 -0
- package/dist/util/index.js +20 -1
- package/package.json +2 -2
- package/proto/apache/rocketmq/v2/definition.proto +56 -0
- package/proto/apache/rocketmq/v2/definition_pb.d.ts +100 -0
- package/proto/apache/rocketmq/v2/definition_pb.js +570 -4
- package/proto/apache/rocketmq/v2/service.proto +65 -0
- package/proto/apache/rocketmq/v2/service_grpc_pb.d.ts +34 -0
- package/proto/apache/rocketmq/v2/service_grpc_pb.js +81 -4
- package/proto/apache/rocketmq/v2/service_pb.d.ts +211 -0
- package/proto/apache/rocketmq/v2/service_pb.js +1691 -25
- package/src/client/BaseClient.ts +196 -28
- package/src/client/ClientId.ts +1 -1
- package/src/client/Logger.ts +1 -0
- package/src/client/RpcClient.ts +26 -0
- package/src/client/RpcClientManager.ts +27 -3
- package/src/client/TelemetrySession.ts +54 -8
- package/src/consumer/Assignment.ts +40 -0
- package/src/consumer/Assignments.ts +55 -0
- package/src/consumer/ConsumeResult.ts +21 -0
- package/src/consumer/ConsumeService.ts +72 -0
- package/src/consumer/ConsumeTask.ts +39 -0
- package/src/consumer/Consumer.ts +76 -20
- package/src/consumer/FifoConsumeService.ts +146 -0
- package/src/consumer/FilterExpression.ts +17 -0
- package/src/consumer/LitePushConsumer.ts +256 -0
- package/src/consumer/LitePushConsumerImpl.ts +245 -0
- package/src/consumer/LiteSubscriptionManager.ts +354 -0
- package/src/consumer/MessageListener.ts +23 -0
- package/src/consumer/OffsetOption.ts +203 -0
- package/src/consumer/ProcessQueue.ts +339 -0
- package/src/consumer/PushConsumer.ts +563 -0
- package/src/consumer/PushSubscriptionSettings.ts +124 -0
- package/src/consumer/SimpleConsumer.ts +68 -11
- package/src/consumer/SimpleSubscriptionSettings.ts +2 -2
- package/src/consumer/StandardConsumeService.ts +57 -0
- package/src/consumer/index.ts +15 -0
- package/src/exception/InternalErrorException.ts +10 -0
- package/src/exception/LiteSubscriptionQuotaExceededException.ts +28 -0
- package/src/exception/LiteTopicQuotaExceededException.ts +28 -0
- package/src/exception/StatusChecker.ts +3 -0
- package/src/exception/index.ts +2 -0
- package/src/index.ts +1 -0
- package/src/message/Message.ts +46 -5
- package/src/message/MessageId.ts +16 -0
- package/src/message/MessageView.ts +127 -0
- package/src/message/PublishingMessage.ts +20 -4
- package/src/producer/Producer.ts +144 -24
- package/src/producer/PublishingLoadBalancer.ts +21 -0
- package/src/producer/RecallReceipt.ts +32 -0
- package/src/producer/SendReceipt.ts +5 -2
- package/src/producer/Transaction.ts +16 -0
- package/src/producer/index.ts +1 -0
- package/src/route/Broker.ts +20 -0
- package/src/route/Endpoints.ts +32 -0
- package/src/route/MessageQueue.ts +23 -1
- package/src/route/Resource.ts +104 -0
- package/src/route/TopicRouteData.ts +37 -0
- package/src/route/index.ts +1 -0
- package/src/util/index.ts +19 -0
|
@@ -0,0 +1,72 @@
|
|
|
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
|
+
import { MessageView } from '../message';
|
|
18
|
+
import { ConsumeResult } from './ConsumeResult';
|
|
19
|
+
import { ConsumeTask } from './ConsumeTask';
|
|
20
|
+
import { MessageListener } from './MessageListener';
|
|
21
|
+
import type { ProcessQueue } from './ProcessQueue';
|
|
22
|
+
|
|
23
|
+
export abstract class ConsumeService {
|
|
24
|
+
protected readonly clientId: string;
|
|
25
|
+
readonly #messageListener: MessageListener;
|
|
26
|
+
#aborted = false;
|
|
27
|
+
#pendingTimers: Set<NodeJS.Timeout> = new Set();
|
|
28
|
+
|
|
29
|
+
constructor(clientId: string, messageListener: MessageListener) {
|
|
30
|
+
this.clientId = clientId;
|
|
31
|
+
this.#messageListener = messageListener;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
abstract consume(pq: ProcessQueue, messageViews: MessageView[]): void;
|
|
35
|
+
|
|
36
|
+
async consumeMessage(messageView: MessageView, delay = 0): Promise<ConsumeResult> {
|
|
37
|
+
if (this.#aborted) {
|
|
38
|
+
return ConsumeResult.FAILURE;
|
|
39
|
+
}
|
|
40
|
+
const task = new ConsumeTask(this.clientId, this.#messageListener, messageView);
|
|
41
|
+
if (delay <= 0) {
|
|
42
|
+
return this.#executeTask(task);
|
|
43
|
+
}
|
|
44
|
+
return new Promise(resolve => {
|
|
45
|
+
const timer = setTimeout(async () => {
|
|
46
|
+
this.#pendingTimers.delete(timer);
|
|
47
|
+
if (this.#aborted) {
|
|
48
|
+
resolve(ConsumeResult.FAILURE);
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
const result = await this.#executeTask(task);
|
|
52
|
+
resolve(result);
|
|
53
|
+
}, delay);
|
|
54
|
+
this.#pendingTimers.add(timer);
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async #executeTask(task: ConsumeTask): Promise<ConsumeResult> {
|
|
59
|
+
try {
|
|
60
|
+
return await task.call();
|
|
61
|
+
} catch (error) {
|
|
62
|
+
return ConsumeResult.FAILURE;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
abort() {
|
|
66
|
+
this.#aborted = true;
|
|
67
|
+
for (const timer of this.#pendingTimers) {
|
|
68
|
+
clearTimeout(timer);
|
|
69
|
+
}
|
|
70
|
+
this.#pendingTimers.clear();
|
|
71
|
+
}
|
|
72
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
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 { MessageView } from '../message';
|
|
19
|
+
import { ConsumeResult } from './ConsumeResult';
|
|
20
|
+
import { MessageListener } from './MessageListener';
|
|
21
|
+
|
|
22
|
+
export class ConsumeTask {
|
|
23
|
+
readonly #messageListener: MessageListener;
|
|
24
|
+
readonly #messageView: MessageView;
|
|
25
|
+
|
|
26
|
+
constructor(_clientId: string, messageListener: MessageListener, messageView: MessageView) {
|
|
27
|
+
this.#messageListener = messageListener;
|
|
28
|
+
this.#messageView = messageView;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async call(): Promise<ConsumeResult> {
|
|
32
|
+
try {
|
|
33
|
+
return await this.#messageListener.consume(this.#messageView);
|
|
34
|
+
} catch (e) {
|
|
35
|
+
// Message listener raised an exception while consuming messages
|
|
36
|
+
return ConsumeResult.FAILURE;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
package/src/consumer/Consumer.ts
CHANGED
|
@@ -21,6 +21,7 @@ import {
|
|
|
21
21
|
ChangeInvisibleDurationRequest,
|
|
22
22
|
ReceiveMessageRequest, ReceiveMessageResponse,
|
|
23
23
|
} from '../../proto/apache/rocketmq/v2/service_pb';
|
|
24
|
+
import { ClientType } from '../../proto/apache/rocketmq/v2/definition_pb';
|
|
24
25
|
import { MessageView } from '../message';
|
|
25
26
|
import { MessageQueue } from '../route';
|
|
26
27
|
import { StatusChecker } from '../exception';
|
|
@@ -56,28 +57,46 @@ export abstract class Consumer extends BaseClient {
|
|
|
56
57
|
const endpoints = mq.broker.endpoints;
|
|
57
58
|
const timeout = this.requestTimeout + awaitDuration;
|
|
58
59
|
let status: Status.AsObject | undefined;
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
60
|
+
|
|
61
|
+
try {
|
|
62
|
+
this.logger.debug?.('Receiving messages from broker, topic=%s, endpoints=%s, batchSize=%d, clientId=%s',
|
|
63
|
+
request.getMessageQueue()?.getTopic()?.getName(), endpoints, request.getBatchSize(), (this as any).clientId);
|
|
64
|
+
|
|
65
|
+
const responses = await this.rpcClientManager.receiveMessage(endpoints, request, timeout);
|
|
66
|
+
const messageList: Message[] = [];
|
|
67
|
+
let transportDeliveryTimestamp: Date | undefined;
|
|
68
|
+
|
|
69
|
+
for (const response of responses) {
|
|
70
|
+
switch (response.getContentCase()) {
|
|
71
|
+
case ReceiveMessageResponse.ContentCase.STATUS:
|
|
72
|
+
status = response.getStatus()?.toObject();
|
|
73
|
+
break;
|
|
74
|
+
case ReceiveMessageResponse.ContentCase.MESSAGE:
|
|
75
|
+
messageList.push(response.getMessage()!);
|
|
76
|
+
break;
|
|
77
|
+
case ReceiveMessageResponse.ContentCase.DELIVERY_TIMESTAMP:
|
|
78
|
+
transportDeliveryTimestamp = response.getDeliveryTimestamp()?.toDate();
|
|
79
|
+
break;
|
|
80
|
+
default:
|
|
81
|
+
// this.logger.warn("[Bug] Not recognized content for receive message response, mq={}, " +
|
|
82
|
+
// "clientId={}, response={}", mq, clientId, response);
|
|
83
|
+
}
|
|
76
84
|
}
|
|
85
|
+
|
|
86
|
+
StatusChecker.check(status);
|
|
87
|
+
|
|
88
|
+
if (messageList.length > 0) {
|
|
89
|
+
this.logger.debug?.('Received %d messages successfully, topic=%s, endpoints=%s, clientId=%s',
|
|
90
|
+
messageList.length, request.getMessageQueue()?.getTopic()?.getName(), endpoints, (this as any).clientId);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const messages = messageList.map(message => new MessageView(message, mq, transportDeliveryTimestamp));
|
|
94
|
+
return messages;
|
|
95
|
+
} catch (err) {
|
|
96
|
+
this.logger.error('Failed to receive messages, topic=%s, endpoints=%s, clientId=%s, error=%s',
|
|
97
|
+
request.getMessageQueue()?.getTopic()?.getName(), endpoints, (this as any).clientId, err);
|
|
98
|
+
throw err;
|
|
77
99
|
}
|
|
78
|
-
StatusChecker.check(status);
|
|
79
|
-
const messages = messageList.map(message => new MessageView(message, mq, transportDeliveryTimestamp));
|
|
80
|
-
return messages;
|
|
81
100
|
}
|
|
82
101
|
|
|
83
102
|
protected async ackMessage(messageView: MessageView) {
|
|
@@ -108,4 +127,41 @@ export abstract class Consumer extends BaseClient {
|
|
|
108
127
|
StatusChecker.check(response.status);
|
|
109
128
|
return response.receiptHandle;
|
|
110
129
|
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Expose public methods for ProcessQueue to access RPC operations
|
|
133
|
+
*/
|
|
134
|
+
async ackMessageViaRpc(endpoints: any, request: AckMessageRequest, timeout: number) {
|
|
135
|
+
const res = await this.rpcClientManager.ackMessage(endpoints, request, timeout);
|
|
136
|
+
return res;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
async changeInvisibleDurationViaRpc(endpoints: any, request: ChangeInvisibleDurationRequest, timeout: number) {
|
|
140
|
+
const res = await this.rpcClientManager.changeInvisibleDuration(endpoints, request, timeout);
|
|
141
|
+
return res;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
async forwardMessageToDeadLetterQueueViaRpc(endpoints: any, request: any, timeout: number) {
|
|
145
|
+
const res = await this.rpcClientManager.forwardMessageToDeadLetterQueue(endpoints, request, timeout);
|
|
146
|
+
return res;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Get the consumer group name.
|
|
151
|
+
*
|
|
152
|
+
* @return Consumer group name
|
|
153
|
+
*/
|
|
154
|
+
getConsumerGroup(): string {
|
|
155
|
+
return this.consumerGroup;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Check if this is a lite consumer.
|
|
160
|
+
*
|
|
161
|
+
* @return true if this is a LITE_PUSH_CONSUMER
|
|
162
|
+
*/
|
|
163
|
+
protected isLiteConsumer(): boolean {
|
|
164
|
+
const clientType = (this as any).getClientType?.();
|
|
165
|
+
return clientType === ClientType.LITE_PUSH_CONSUMER;
|
|
166
|
+
}
|
|
111
167
|
}
|
|
@@ -0,0 +1,146 @@
|
|
|
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 { MessageView } from '../message';
|
|
19
|
+
import { ConsumeService } from './ConsumeService';
|
|
20
|
+
import { MessageListener } from './MessageListener';
|
|
21
|
+
import type { ProcessQueue } from './ProcessQueue';
|
|
22
|
+
import { ILogger, getDefaultLogger } from '../client/Logger';
|
|
23
|
+
|
|
24
|
+
export class FifoConsumeService extends ConsumeService {
|
|
25
|
+
readonly #enableAccelerator: boolean;
|
|
26
|
+
readonly #groupProcessing = new Map<string /* messageGroup */, Promise<void>>();
|
|
27
|
+
readonly #logger: ILogger;
|
|
28
|
+
|
|
29
|
+
constructor(clientId: string, messageListener: MessageListener, enableAccelerator?: boolean) {
|
|
30
|
+
super(clientId, messageListener);
|
|
31
|
+
this.#enableAccelerator = enableAccelerator ?? false;
|
|
32
|
+
this.#logger = getDefaultLogger();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
consume(pq: ProcessQueue, messageViews: MessageView[]): void {
|
|
36
|
+
// Use iterative consumption when accelerator is disabled or only one message
|
|
37
|
+
if (!this.#enableAccelerator || messageViews.length <= 1) {
|
|
38
|
+
this.#consumeIteratively(pq, messageViews, 0);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
this.#consumeWithAccelerator(pq, messageViews);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* FIFO consume accelerator mode:
|
|
46
|
+
* - Messages with the same messageGroup are consumed sequentially
|
|
47
|
+
* - Messages with different messageGroups are consumed in parallel
|
|
48
|
+
* - Messages without messageGroup are consumed in parallel
|
|
49
|
+
*/
|
|
50
|
+
#consumeWithAccelerator(pq: ProcessQueue, messageViews: MessageView[]): void {
|
|
51
|
+
// Group messages by messageGroup
|
|
52
|
+
const groupedMessages = new Map<string, MessageView[]>();
|
|
53
|
+
const ungroupedMessages: MessageView[] = [];
|
|
54
|
+
|
|
55
|
+
for (const messageView of messageViews) {
|
|
56
|
+
if (messageView.corrupted) {
|
|
57
|
+
pq.discardFifoMessage(messageView);
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const messageGroup = messageView.messageGroup || '';
|
|
62
|
+
if (messageGroup) {
|
|
63
|
+
const group = groupedMessages.get(messageGroup) || [];
|
|
64
|
+
group.push(messageView);
|
|
65
|
+
groupedMessages.set(messageGroup, group);
|
|
66
|
+
} else {
|
|
67
|
+
ungroupedMessages.push(messageView);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// Log parallel consumption info
|
|
72
|
+
const groupCount = groupedMessages.size + (ungroupedMessages.length > 0 ? 1 : 0);
|
|
73
|
+
this.#logger.debug?.('FifoConsumeService parallel consume, messageViewsNum=%d, groupNum=%d',
|
|
74
|
+
messageViews.length, groupCount);
|
|
75
|
+
|
|
76
|
+
// Process grouped messages (each group sequentially, groups in parallel)
|
|
77
|
+
for (const [ , messages ] of groupedMessages.entries()) {
|
|
78
|
+
this.#processMessageGroup(pq, messages);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Process ungrouped messages in parallel
|
|
82
|
+
for (const messageView of ungroupedMessages) {
|
|
83
|
+
this.consumeMessage(messageView)
|
|
84
|
+
.then(result => pq.eraseFifoMessage(messageView, result))
|
|
85
|
+
.catch(() => {
|
|
86
|
+
// Error already logged, continue with next message
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Process messages within the same group sequentially
|
|
93
|
+
*/
|
|
94
|
+
async #processMessageGroup(pq: ProcessQueue, messages: MessageView[]): Promise<void> {
|
|
95
|
+
// Check if there's already processing happening for this group
|
|
96
|
+
const messageGroup = messages[0]?.messageGroup || 'NO_GROUP';
|
|
97
|
+
const existingPromise = this.#groupProcessing.get(messageGroup);
|
|
98
|
+
const processTask = (async () => {
|
|
99
|
+
// Wait for previous processing to complete if any
|
|
100
|
+
if (existingPromise) {
|
|
101
|
+
await existingPromise.catch(() => {
|
|
102
|
+
// Ignore previous errors, continue processing
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// Process messages in sequence
|
|
107
|
+
for (const messageView of messages) {
|
|
108
|
+
try {
|
|
109
|
+
const result = await this.consumeMessage(messageView);
|
|
110
|
+
await pq.eraseFifoMessage(messageView, result);
|
|
111
|
+
} catch (error) {
|
|
112
|
+
this.#logger.error('Failed to process FIFO message, messageGroup=%s, messageId=%s, error=%s',
|
|
113
|
+
messageGroup, messageView.messageId, error);
|
|
114
|
+
// Continue with next message even if current fails
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
})();
|
|
118
|
+
|
|
119
|
+
// Store the promise for this group
|
|
120
|
+
this.#groupProcessing.set(messageGroup, processTask);
|
|
121
|
+
|
|
122
|
+
// Clean up when done
|
|
123
|
+
processTask.finally(() => {
|
|
124
|
+
this.#groupProcessing.delete(messageGroup);
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
#consumeIteratively(pq: ProcessQueue, messageViews: MessageView[], index: number): void {
|
|
129
|
+
if (index >= messageViews.length) {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const messageView = messageViews[index];
|
|
134
|
+
|
|
135
|
+
if (messageView.corrupted) {
|
|
136
|
+
pq.discardFifoMessage(messageView);
|
|
137
|
+
this.#consumeIteratively(pq, messageViews, index + 1);
|
|
138
|
+
return;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
this.consumeMessage(messageView)
|
|
142
|
+
.then(result => pq.eraseFifoMessage(messageView, result))
|
|
143
|
+
.then(() => this.#consumeIteratively(pq, messageViews, index + 1))
|
|
144
|
+
.catch(() => this.#consumeIteratively(pq, messageViews, index + 1));
|
|
145
|
+
}
|
|
146
|
+
}
|
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
*/
|
|
17
17
|
|
|
18
18
|
import { FilterType, FilterExpression as FilterExpressionPB } from '../../proto/apache/rocketmq/v2/definition_pb';
|
|
19
|
+
import { hashCodeOfString } from '../util';
|
|
19
20
|
|
|
20
21
|
const TAG_EXPRESSION_SUB_ALL = '*';
|
|
21
22
|
|
|
@@ -36,6 +37,22 @@ export class FilterExpression {
|
|
|
36
37
|
.setExpression(this.expression);
|
|
37
38
|
}
|
|
38
39
|
|
|
40
|
+
equals(other: FilterExpression): boolean {
|
|
41
|
+
if (this === other) return true;
|
|
42
|
+
if (!other) return false;
|
|
43
|
+
return this.expression === other.expression &&
|
|
44
|
+
this.filterType === other.filterType;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
hashCode(): number {
|
|
48
|
+
let hash = 17;
|
|
49
|
+
// eslint-disable-next-line no-bitwise
|
|
50
|
+
hash = (hash * 31 + hashCodeOfString(this.expression)) | 0;
|
|
51
|
+
// eslint-disable-next-line no-bitwise
|
|
52
|
+
hash = (hash * 31 + this.filterType) | 0;
|
|
53
|
+
return hash;
|
|
54
|
+
}
|
|
55
|
+
|
|
39
56
|
toString() {
|
|
40
57
|
return `FilterExpression(${this.filterType},${this.expression})`;
|
|
41
58
|
}
|
|
@@ -0,0 +1,256 @@
|
|
|
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 { BaseClientOptions } from '../client';
|
|
19
|
+
import { MessageListener } from './MessageListener';
|
|
20
|
+
import { OffsetOption } from './OffsetOption';
|
|
21
|
+
import { LitePushConsumerImpl } from './LitePushConsumerImpl';
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* LitePushConsumer interface for consuming messages from lite topics.
|
|
25
|
+
*
|
|
26
|
+
* <p>LitePushConsumer is a specialized consumer designed for lightweight scenarios
|
|
27
|
+
* with reduced metadata and storage overhead. It supports dynamic subscription
|
|
28
|
+
* management for lite topics.</p>
|
|
29
|
+
*/
|
|
30
|
+
export interface LitePushConsumer {
|
|
31
|
+
/**
|
|
32
|
+
* Subscribe to a lite topic.
|
|
33
|
+
*
|
|
34
|
+
* <p>The subscribeLite() method initiates network requests and performs quota verification,
|
|
35
|
+
* so it may fail. It's important to check the result of this call to ensure that the
|
|
36
|
+
* subscription was successfully added. Possible failure scenarios include:</p>
|
|
37
|
+
* <ul>
|
|
38
|
+
* <li>Network request errors, which can be retried.</li>
|
|
39
|
+
* <li>Quota verification failures, indicated by LiteSubscriptionQuotaExceededException.
|
|
40
|
+
* In this case, evaluate whether the quota is insufficient and promptly unsubscribe
|
|
41
|
+
* from unused subscriptions using unsubscribeLite() to free up resources.</li>
|
|
42
|
+
* </ul>
|
|
43
|
+
*
|
|
44
|
+
* @param liteTopic - The name of the lite topic to subscribe
|
|
45
|
+
* @throws ClientException if an error occurs during subscription
|
|
46
|
+
*/
|
|
47
|
+
subscribeLite(liteTopic: string): Promise<void>;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Subscribe to a lite topic with consumeFromOption to specify the consume from offset.
|
|
51
|
+
*
|
|
52
|
+
* @param liteTopic - The name of the lite topic to subscribe
|
|
53
|
+
* @param offsetOption - The consume from offset option
|
|
54
|
+
* @throws ClientException if an error occurs during subscription
|
|
55
|
+
*/
|
|
56
|
+
subscribeLite(liteTopic: string, offsetOption: OffsetOption): Promise<void>;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Unsubscribe from a lite topic.
|
|
60
|
+
*
|
|
61
|
+
* @param liteTopic - The name of the lite topic to unsubscribe from
|
|
62
|
+
* @throws ClientException if an error occurs during unsubscription
|
|
63
|
+
*/
|
|
64
|
+
unsubscribeLite(liteTopic: string): Promise<void>;
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Get the lite topic immutable set.
|
|
68
|
+
*
|
|
69
|
+
* @return Lite topic immutable set
|
|
70
|
+
*/
|
|
71
|
+
getLiteTopicSet(): Set<string>;
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Get the load balancing group for the consumer.
|
|
75
|
+
*
|
|
76
|
+
* @return Consumer load balancing group
|
|
77
|
+
*/
|
|
78
|
+
getConsumerGroup(): string;
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Close the consumer and release all related resources.
|
|
82
|
+
*
|
|
83
|
+
* <p>Once the consumer is closed, <strong>it could not be started once again.</strong>
|
|
84
|
+
* We maintain an FSM (finite-state machine) to record the different states for each
|
|
85
|
+
* push consumer.</p>
|
|
86
|
+
*/
|
|
87
|
+
close(): Promise<void>;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export interface LitePushConsumerOptions extends BaseClientOptions {
|
|
91
|
+
consumerGroup: string;
|
|
92
|
+
bindTopic: string;
|
|
93
|
+
messageListener: MessageListener;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const CONSUMER_GROUP_PATTERN = /^[a-zA-Z0-9_-]+$/;
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* LitePushConsumer builder class.
|
|
100
|
+
*
|
|
101
|
+
* <p>This class provides a fluent API for configuring and creating
|
|
102
|
+
* lite push consumers with reduced overhead for lightweight scenarios.</p>
|
|
103
|
+
*/
|
|
104
|
+
export class LitePushConsumerBuilder {
|
|
105
|
+
private options: Partial<LitePushConsumerOptions> = {};
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Set the bind topic for the lite push consumer.
|
|
109
|
+
*
|
|
110
|
+
* @param bindTopic - The parent topic to bind
|
|
111
|
+
* @return This builder instance
|
|
112
|
+
* @throws Error if bindTopic is blank
|
|
113
|
+
*/
|
|
114
|
+
bindTopic(bindTopic: string): LitePushConsumerBuilder {
|
|
115
|
+
if (!bindTopic || bindTopic.trim().length === 0) {
|
|
116
|
+
throw new Error('bindTopic should not be blank');
|
|
117
|
+
}
|
|
118
|
+
this.options.bindTopic = bindTopic;
|
|
119
|
+
return this;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Set the client configuration.
|
|
124
|
+
*
|
|
125
|
+
* @param options - Client configuration options
|
|
126
|
+
* @return This builder instance
|
|
127
|
+
* @throws Error if options is null/undefined
|
|
128
|
+
*/
|
|
129
|
+
setClientConfiguration(options: BaseClientOptions): LitePushConsumerBuilder {
|
|
130
|
+
if (!options) {
|
|
131
|
+
throw new Error('clientConfiguration should not be null');
|
|
132
|
+
}
|
|
133
|
+
Object.assign(this.options, options);
|
|
134
|
+
return this;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Set the consumer group.
|
|
139
|
+
*
|
|
140
|
+
* @param consumerGroup - Consumer group name
|
|
141
|
+
* @return This builder instance
|
|
142
|
+
* @throws Error if consumerGroup is null, doesn't match the pattern, or starts with 'GID-'
|
|
143
|
+
*/
|
|
144
|
+
setConsumerGroup(consumerGroup: string): LitePushConsumerBuilder {
|
|
145
|
+
if (!consumerGroup) {
|
|
146
|
+
throw new Error('consumerGroup should not be null');
|
|
147
|
+
}
|
|
148
|
+
if (!CONSUMER_GROUP_PATTERN.test(consumerGroup)) {
|
|
149
|
+
throw new Error(`consumerGroup does not match the pattern ${CONSUMER_GROUP_PATTERN.source}`);
|
|
150
|
+
}
|
|
151
|
+
this.options.consumerGroup = consumerGroup;
|
|
152
|
+
return this;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Set the message listener.
|
|
157
|
+
*
|
|
158
|
+
* @param messageListener - Message listener implementation
|
|
159
|
+
* @return This builder instance
|
|
160
|
+
* @throws Error if messageListener is null/undefined
|
|
161
|
+
*/
|
|
162
|
+
setMessageListener(messageListener: MessageListener): LitePushConsumerBuilder {
|
|
163
|
+
if (!messageListener) {
|
|
164
|
+
throw new Error('messageListener should not be null');
|
|
165
|
+
}
|
|
166
|
+
this.options.messageListener = messageListener;
|
|
167
|
+
return this;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Set the maximum cache message count.
|
|
172
|
+
*
|
|
173
|
+
* @param maxCacheMessageCount - Maximum number of messages to cache
|
|
174
|
+
* @return This builder instance
|
|
175
|
+
* @throws Error if maxCacheMessageCount is not positive
|
|
176
|
+
*/
|
|
177
|
+
setMaxCacheMessageCount(maxCacheMessageCount: number): LitePushConsumerBuilder {
|
|
178
|
+
if (maxCacheMessageCount <= 0) {
|
|
179
|
+
throw new Error('maxCacheMessageCount should be positive');
|
|
180
|
+
}
|
|
181
|
+
// TODO: Will be used when implementing full consumer
|
|
182
|
+
return this;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Set the maximum cache message size in bytes.
|
|
187
|
+
*
|
|
188
|
+
* @param maxCacheMessageSizeInBytes - Maximum cache size in bytes
|
|
189
|
+
* @return This builder instance
|
|
190
|
+
* @throws Error if maxCacheMessageSizeInBytes is not positive
|
|
191
|
+
*/
|
|
192
|
+
setMaxCacheMessageSizeInBytes(maxCacheMessageSizeInBytes: number): LitePushConsumerBuilder {
|
|
193
|
+
if (maxCacheMessageSizeInBytes <= 0) {
|
|
194
|
+
throw new Error('maxCacheMessageSizeInBytes should be positive');
|
|
195
|
+
}
|
|
196
|
+
// TODO: Will be used when implementing full consumer
|
|
197
|
+
return this;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Set the consumption thread count.
|
|
202
|
+
*
|
|
203
|
+
* @param consumptionThreadCount - Number of threads for consumption
|
|
204
|
+
* @return This builder instance
|
|
205
|
+
* @throws Error if consumptionThreadCount is not positive
|
|
206
|
+
*/
|
|
207
|
+
setConsumptionThreadCount(consumptionThreadCount: number): LitePushConsumerBuilder {
|
|
208
|
+
if (consumptionThreadCount <= 0) {
|
|
209
|
+
throw new Error('consumptionThreadCount should be positive');
|
|
210
|
+
}
|
|
211
|
+
// TODO: Will be used when implementing full consumer
|
|
212
|
+
return this;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Finalize the build of LitePushConsumer and start.
|
|
217
|
+
*
|
|
218
|
+
* <p>This method will block until the push consumer starts successfully.
|
|
219
|
+
*
|
|
220
|
+
* <p>Especially, if this method is invoked more than once, different push consumers will be created and started.
|
|
221
|
+
*
|
|
222
|
+
* @return Promise resolving to started LitePushConsumer instance
|
|
223
|
+
* @throws Error if required parameters are not set
|
|
224
|
+
*/
|
|
225
|
+
async build(): Promise<LitePushConsumer> {
|
|
226
|
+
if (!this.options.endpoints) {
|
|
227
|
+
throw new Error('clientConfiguration has not been set yet');
|
|
228
|
+
}
|
|
229
|
+
if (!this.options.consumerGroup) {
|
|
230
|
+
throw new Error('consumerGroup has not been set yet');
|
|
231
|
+
}
|
|
232
|
+
if (!this.options.messageListener) {
|
|
233
|
+
throw new Error('messageListener has not been set yet');
|
|
234
|
+
}
|
|
235
|
+
if (!this.options.bindTopic) {
|
|
236
|
+
throw new Error('bindTopic has not been set yet');
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// Build LitePushConsumerImpl
|
|
240
|
+
const options: any = {
|
|
241
|
+
endpoints: this.options.endpoints,
|
|
242
|
+
namespace: this.options.namespace ?? '',
|
|
243
|
+
consumerGroup: this.options.consumerGroup,
|
|
244
|
+
bindTopic: this.options.bindTopic,
|
|
245
|
+
messageListener: this.options.messageListener,
|
|
246
|
+
sslEnabled: this.options.sslEnabled,
|
|
247
|
+
sessionCredentials: this.options.sessionCredentials,
|
|
248
|
+
requestTimeout: this.options.requestTimeout,
|
|
249
|
+
logger: this.options.logger,
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
const litePushConsumer = new LitePushConsumerImpl(options);
|
|
253
|
+
await litePushConsumer.startup();
|
|
254
|
+
return litePushConsumer;
|
|
255
|
+
}
|
|
256
|
+
}
|