rocketmq-client-nodejs 1.0.8 → 1.1.0
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 +61 -0
- package/dist/client/BaseClient.d.ts +36 -1
- package/dist/client/BaseClient.js +176 -12
- package/dist/client/RpcClient.js +14 -2
- package/dist/client/RpcClientManager.d.ts +5 -0
- package/dist/client/RpcClientManager.js +25 -8
- package/dist/client/TelemetrySession.js +45 -10
- package/dist/consumer/ConsumeService.d.ts +2 -1
- package/dist/consumer/ConsumeService.js +7 -3
- package/dist/consumer/ConsumeTask.d.ts +6 -1
- package/dist/consumer/ConsumeTask.js +36 -5
- package/dist/consumer/Consumer.d.ts +13 -5
- package/dist/consumer/Consumer.js +85 -24
- package/dist/consumer/FifoConsumeService.d.ts +2 -1
- package/dist/consumer/FifoConsumeService.js +3 -3
- package/dist/consumer/LiteFifoConsumeService.d.ts +2 -1
- package/dist/consumer/LiteFifoConsumeService.js +3 -3
- package/dist/consumer/LitePushConsumerImpl.d.ts +12 -1
- package/dist/consumer/LitePushConsumerImpl.js +19 -4
- package/dist/consumer/LiteSimpleConsumer.d.ts +177 -0
- package/dist/consumer/LiteSimpleConsumer.js +126 -0
- package/dist/consumer/LiteSimpleConsumerImpl.d.ts +133 -0
- package/dist/consumer/LiteSimpleConsumerImpl.js +187 -0
- package/dist/consumer/LiteStandardConsumeService.d.ts +2 -1
- package/dist/consumer/LiteStandardConsumeService.js +3 -3
- package/dist/consumer/LiteSubscriptionManager.d.ts +27 -6
- package/dist/consumer/LiteSubscriptionManager.js +32 -24
- package/dist/consumer/ProcessQueue.d.ts +10 -0
- package/dist/consumer/ProcessQueue.js +43 -5
- package/dist/consumer/PushConsumer.d.ts +17 -0
- package/dist/consumer/PushConsumer.js +43 -4
- package/dist/consumer/PushConsumerGaugeObserver.d.ts +33 -0
- package/dist/consumer/PushConsumerGaugeObserver.js +60 -0
- package/dist/consumer/PushSubscriptionSettings.d.ts +8 -1
- package/dist/consumer/PushSubscriptionSettings.js +33 -6
- package/dist/consumer/SimpleConsumer.js +10 -1
- package/dist/consumer/StandardConsumeService.d.ts +2 -1
- package/dist/consumer/StandardConsumeService.js +3 -3
- package/dist/consumer/index.d.ts +1 -0
- package/dist/consumer/index.js +2 -1
- package/dist/exception/PayloadEmptyException.d.ts +20 -0
- package/dist/exception/PayloadEmptyException.js +28 -0
- package/dist/exception/StatusChecker.js +8 -1
- package/dist/exception/index.d.ts +1 -0
- package/dist/exception/index.js +2 -1
- package/dist/hook/Attribute.d.ts +26 -0
- package/dist/hook/Attribute.js +37 -0
- package/dist/hook/AttributeKey.d.ts +32 -0
- package/dist/hook/AttributeKey.js +43 -0
- package/dist/hook/CompositedMessageInterceptor.d.ts +31 -0
- package/dist/hook/CompositedMessageInterceptor.js +84 -0
- package/dist/hook/InflightRequestCountInterceptor.d.ts +28 -0
- package/dist/hook/InflightRequestCountInterceptor.js +44 -0
- package/dist/hook/MessageHookPoints.d.ts +54 -0
- package/dist/hook/MessageHookPoints.js +59 -0
- package/dist/hook/MessageHookPointsStatus.d.ts +24 -0
- package/dist/hook/MessageHookPointsStatus.js +29 -0
- package/dist/hook/MessageInterceptor.d.ts +34 -0
- package/dist/hook/MessageInterceptor.js +19 -0
- package/dist/hook/MessageInterceptorContext.d.ts +31 -0
- package/dist/hook/MessageInterceptorContext.js +19 -0
- package/dist/hook/MessageInterceptorContextImpl.d.ts +39 -0
- package/dist/hook/MessageInterceptorContextImpl.js +57 -0
- package/dist/hook/MessageMeterInterceptor.d.ts +39 -0
- package/dist/hook/MessageMeterInterceptor.js +149 -0
- package/dist/hook/index.d.ts +26 -0
- package/dist/hook/index.js +43 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +2 -1
- package/dist/metrics/ClientMeter.d.ts +42 -0
- package/dist/metrics/ClientMeter.js +75 -0
- package/dist/metrics/ClientMeterManager.d.ts +42 -0
- package/dist/metrics/ClientMeterManager.js +127 -0
- package/dist/metrics/EmptyGaugeObserver.d.ts +28 -0
- package/dist/metrics/EmptyGaugeObserver.js +34 -0
- package/dist/metrics/GaugeEnum.d.ts +32 -0
- package/dist/metrics/GaugeEnum.js +37 -0
- package/dist/metrics/GaugeObserver.d.ts +29 -0
- package/dist/metrics/GaugeObserver.js +19 -0
- package/dist/metrics/HistogramEnum.d.ts +54 -0
- package/dist/metrics/HistogramEnum.js +71 -0
- package/dist/metrics/InvocationStatus.d.ts +26 -0
- package/dist/metrics/InvocationStatus.js +33 -0
- package/dist/metrics/Metric.d.ts +30 -0
- package/dist/metrics/Metric.js +44 -0
- package/dist/metrics/MetricLabels.d.ts +27 -0
- package/dist/metrics/MetricLabels.js +34 -0
- package/dist/metrics/index.d.ts +25 -0
- package/dist/metrics/index.js +42 -0
- package/dist/producer/Producer.js +33 -5
- package/dist/producer/PublishingLoadBalancer.js +9 -3
- package/dist/retry/CustomizedBackoffRetryPolicy.d.ts +37 -0
- package/dist/retry/CustomizedBackoffRetryPolicy.js +72 -0
- package/dist/retry/ExponentialBackoffRetryPolicy.js +10 -6
- package/dist/retry/RetryPolicy.d.ts +1 -1
- package/dist/retry/index.d.ts +1 -0
- package/dist/retry/index.js +2 -1
- package/dist/route/Endpoints.d.ts +6 -0
- package/dist/route/Endpoints.js +56 -4
- package/dist/route/IpNameResolver.d.ts +28 -0
- package/dist/route/IpNameResolver.js +104 -0
- package/dist/util/Semaphore.d.ts +47 -0
- package/dist/util/Semaphore.js +82 -0
- package/package.json +6 -2
- package/src/client/BaseClient.ts +202 -12
- package/src/client/RpcClient.ts +14 -1
- package/src/client/RpcClientManager.ts +27 -8
- package/src/client/TelemetrySession.ts +47 -9
- package/src/consumer/ConsumeService.ts +9 -2
- package/src/consumer/ConsumeTask.ts +43 -3
- package/src/consumer/Consumer.ts +103 -23
- package/src/consumer/FifoConsumeService.ts +4 -2
- package/src/consumer/LiteFifoConsumeService.ts +4 -2
- package/src/consumer/LitePushConsumerImpl.ts +21 -3
- package/src/consumer/LiteSimpleConsumer.ts +251 -0
- package/src/consumer/LiteSimpleConsumerImpl.ts +221 -0
- package/src/consumer/LiteStandardConsumeService.ts +4 -2
- package/src/consumer/LiteSubscriptionManager.ts +63 -36
- package/src/consumer/ProcessQueue.ts +44 -1
- package/src/consumer/PushConsumer.ts +54 -2
- package/src/consumer/PushConsumerGaugeObserver.ts +60 -0
- package/src/consumer/PushSubscriptionSettings.ts +38 -7
- package/src/consumer/SimpleConsumer.ts +9 -0
- package/src/consumer/StandardConsumeService.ts +4 -2
- package/src/consumer/index.ts +1 -0
- package/src/exception/PayloadEmptyException.ts +25 -0
- package/src/exception/StatusChecker.ts +7 -0
- package/src/exception/index.ts +1 -0
- package/src/hook/Attribute.ts +36 -0
- package/src/hook/AttributeKey.ts +42 -0
- package/src/hook/CompositedMessageInterceptor.ts +92 -0
- package/src/hook/InflightRequestCountInterceptor.ts +46 -0
- package/src/hook/MessageHookPoints.ts +55 -0
- package/src/hook/MessageHookPointsStatus.ts +25 -0
- package/src/hook/MessageInterceptor.ts +35 -0
- package/src/hook/MessageInterceptorContext.ts +33 -0
- package/src/hook/MessageInterceptorContextImpl.ts +68 -0
- package/src/hook/MessageMeterInterceptor.ts +162 -0
- package/src/hook/index.ts +27 -0
- package/src/index.ts +1 -0
- package/src/metrics/ClientMeter.ts +81 -0
- package/src/metrics/ClientMeterManager.ts +145 -0
- package/src/metrics/EmptyGaugeObserver.ts +36 -0
- package/src/metrics/GaugeEnum.ts +33 -0
- package/src/metrics/GaugeObserver.ts +32 -0
- package/src/metrics/HistogramEnum.ts +69 -0
- package/src/metrics/InvocationStatus.ts +28 -0
- package/src/metrics/Metric.ts +42 -0
- package/src/metrics/MetricLabels.ts +31 -0
- package/src/metrics/index.ts +26 -0
- package/src/producer/Producer.ts +37 -6
- package/src/producer/PublishingLoadBalancer.ts +8 -2
- package/src/retry/CustomizedBackoffRetryPolicy.ts +81 -0
- package/src/retry/ExponentialBackoffRetryPolicy.ts +11 -6
- package/src/retry/RetryPolicy.ts +1 -1
- package/src/retry/index.ts +1 -0
- package/src/route/Endpoints.ts +52 -3
- package/src/route/IpNameResolver.ts +119 -0
- package/src/util/Semaphore.ts +82 -0
|
@@ -0,0 +1,33 @@
|
|
|
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
|
+
/**
|
|
19
|
+
* The two consumer gauges exported by the RocketMQ client, mirroring
|
|
20
|
+
* org.apache.rocketmq.client.java.metrics.GaugeEnum.
|
|
21
|
+
*/
|
|
22
|
+
export enum GaugeEnum {
|
|
23
|
+
/**
|
|
24
|
+
* Cached message count of push consumer.
|
|
25
|
+
* Labels: topic, client_id, consumer_group.
|
|
26
|
+
*/
|
|
27
|
+
CONSUMER_CACHED_MESSAGES = 'rocketmq_consumer_cached_messages',
|
|
28
|
+
/**
|
|
29
|
+
* Cached message bytes of push consumer.
|
|
30
|
+
* Labels: topic, client_id, consumer_group.
|
|
31
|
+
*/
|
|
32
|
+
CONSUMER_CACHED_BYTES = 'rocketmq_consumer_cached_bytes',
|
|
33
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
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 { Attributes } from '@opentelemetry/api';
|
|
19
|
+
import { GaugeEnum } from './GaugeEnum';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Supplies the live values observed by the periodic metric reader for each
|
|
23
|
+
* consumer gauge, mirroring org.apache.rocketmq.client.java.metrics.GaugeObserver.
|
|
24
|
+
*
|
|
25
|
+
* The map key is the attribute set (topic / client_id / consumer_group) and the
|
|
26
|
+
* value is the observed gauge reading.
|
|
27
|
+
*/
|
|
28
|
+
export interface GaugeObserver {
|
|
29
|
+
getGauges(): GaugeEnum[];
|
|
30
|
+
|
|
31
|
+
getValues(gauge: GaugeEnum): Map<Attributes, number>;
|
|
32
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
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 { ExplicitBucketHistogramAggregation, InstrumentType, ViewOptions } from '@opentelemetry/sdk-metrics';
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The four histograms exported by the RocketMQ client, mirroring
|
|
22
|
+
* org.apache.rocketmq.client.java.metrics.HistogramEnum. The bucket
|
|
23
|
+
* boundaries (in milliseconds) are copied verbatim from the Java client.
|
|
24
|
+
*/
|
|
25
|
+
export enum HistogramEnum {
|
|
26
|
+
/**
|
|
27
|
+
* Cost time of successful api calls of message publishing.
|
|
28
|
+
* Labels: topic, client_id, invocation_status.
|
|
29
|
+
*/
|
|
30
|
+
SEND_COST_TIME = 'rocketmq_send_cost_time',
|
|
31
|
+
/**
|
|
32
|
+
* Latency of message delivery from remote.
|
|
33
|
+
* Labels: topic, client_id, consumer_group.
|
|
34
|
+
*/
|
|
35
|
+
DELIVERY_LATENCY = 'rocketmq_delivery_latency',
|
|
36
|
+
/**
|
|
37
|
+
* Await time of message consumption.
|
|
38
|
+
* Labels: topic, client_id, consumer_group.
|
|
39
|
+
*/
|
|
40
|
+
AWAIT_TIME = 'rocketmq_await_time',
|
|
41
|
+
/**
|
|
42
|
+
* Process time of message consumption.
|
|
43
|
+
* Labels: topic, client_id, consumer_group, invocation_status.
|
|
44
|
+
*/
|
|
45
|
+
PROCESS_TIME = 'rocketmq_process_time',
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Explicit bucket boundaries (milliseconds) for each histogram, identical to
|
|
50
|
+
* the Java client's Aggregation.explicitBucketHistogram(...) calls.
|
|
51
|
+
*/
|
|
52
|
+
export const HISTOGRAM_BUCKETS: Record<HistogramEnum, number[]> = {
|
|
53
|
+
[HistogramEnum.SEND_COST_TIME]: [ 1.0, 5.0, 10.0, 20.0, 50.0, 200.0, 500.0 ],
|
|
54
|
+
[HistogramEnum.DELIVERY_LATENCY]: [ 1.0, 5.0, 10.0, 20.0, 50.0, 200.0, 500.0 ],
|
|
55
|
+
[HistogramEnum.AWAIT_TIME]: [ 1.0, 5.0, 20.0, 100.0, 1000.0, 5 * 1000.0, 10 * 1000.0 ],
|
|
56
|
+
[HistogramEnum.PROCESS_TIME]: [ 1.0, 5.0, 10.0, 100.0, 1000.0, 10 * 1000.0, 60 * 1000.0 ],
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Build the OpenTelemetry view that forces the given histogram onto the exact
|
|
61
|
+
* explicit-bucket aggregation used by the Java client.
|
|
62
|
+
*/
|
|
63
|
+
export function buildHistogramView(histogramEnum: HistogramEnum): ViewOptions {
|
|
64
|
+
return {
|
|
65
|
+
instrumentName: histogramEnum,
|
|
66
|
+
instrumentType: InstrumentType.HISTOGRAM,
|
|
67
|
+
aggregation: new ExplicitBucketHistogramAggregation(HISTOGRAM_BUCKETS[histogramEnum]),
|
|
68
|
+
};
|
|
69
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
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
|
+
/**
|
|
19
|
+
* Outcome of an instrumented operation (send / consume), mirroring
|
|
20
|
+
* org.apache.rocketmq.client.java.metrics.InvocationStatus.
|
|
21
|
+
*/
|
|
22
|
+
export class InvocationStatus {
|
|
23
|
+
static readonly SUCCESS = new InvocationStatus('success');
|
|
24
|
+
static readonly FAILURE = new InvocationStatus('failure');
|
|
25
|
+
|
|
26
|
+
private constructor(readonly name: string) {
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
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 { Endpoints } from '../route';
|
|
19
|
+
import { Metric as MetricPB } from '../../proto/apache/rocketmq/v2/definition_pb';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Local view of the metric switch received from the broker via the telemetry
|
|
23
|
+
* settings command, mirroring org.apache.rocketmq.client.java.metrics.Metric.
|
|
24
|
+
*
|
|
25
|
+
* Metrics are only considered enabled when both the `on` flag is set AND a
|
|
26
|
+
* valid endpoints is present.
|
|
27
|
+
*/
|
|
28
|
+
export class Metric {
|
|
29
|
+
readonly endpoints: Endpoints | null;
|
|
30
|
+
readonly on: boolean;
|
|
31
|
+
|
|
32
|
+
constructor(metric: MetricPB | undefined | null) {
|
|
33
|
+
if (metric && metric.hasEndpoints() && metric.getOn()) {
|
|
34
|
+
// Endpoints expects the plain AsObject shape, so unwrap the proto message.
|
|
35
|
+
this.endpoints = new Endpoints(metric.getEndpoints()!.toObject());
|
|
36
|
+
this.on = true;
|
|
37
|
+
} else {
|
|
38
|
+
this.endpoints = null;
|
|
39
|
+
this.on = false;
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
@@ -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
|
+
/**
|
|
19
|
+
* Canonical attribute (label) keys attached to every RocketMQ metric,
|
|
20
|
+
* mirroring org.apache.rocketmq.client.java.metrics.MetricLabels.
|
|
21
|
+
*/
|
|
22
|
+
export class MetricLabels {
|
|
23
|
+
static readonly TOPIC = 'topic';
|
|
24
|
+
static readonly CLIENT_ID = 'client_id';
|
|
25
|
+
static readonly CONSUMER_GROUP = 'consumer_group';
|
|
26
|
+
static readonly INVOCATION_STATUS = 'invocation_status';
|
|
27
|
+
|
|
28
|
+
private constructor() {
|
|
29
|
+
// constant holder
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
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
|
+
export * from './MetricLabels';
|
|
19
|
+
export * from './InvocationStatus';
|
|
20
|
+
export * from './HistogramEnum';
|
|
21
|
+
export * from './GaugeEnum';
|
|
22
|
+
export * from './GaugeObserver';
|
|
23
|
+
export * from './EmptyGaugeObserver';
|
|
24
|
+
export * from './Metric';
|
|
25
|
+
export * from './ClientMeter';
|
|
26
|
+
export * from './ClientMeterManager';
|
package/src/producer/Producer.ts
CHANGED
|
@@ -50,6 +50,12 @@ import { Transaction } from './Transaction';
|
|
|
50
50
|
import { createResource } from '../util';
|
|
51
51
|
import { RecallReceipt } from './RecallReceipt';
|
|
52
52
|
import { RecallMessageRequest } from '../../proto/apache/rocketmq/v2/service_pb';
|
|
53
|
+
import {
|
|
54
|
+
GeneralMessage,
|
|
55
|
+
MessageHookPoints,
|
|
56
|
+
MessageHookPointsStatus,
|
|
57
|
+
MessageInterceptorContextImpl,
|
|
58
|
+
} from '../hook';
|
|
53
59
|
|
|
54
60
|
export interface ProducerOptions extends BaseClientOptions {
|
|
55
61
|
topic?: string | string[];
|
|
@@ -114,11 +120,25 @@ export class Producer extends BaseClient {
|
|
|
114
120
|
.setTopic(createResource(message.topic).setResourceNamespace(this.namespace))
|
|
115
121
|
.setResolution(resolution)
|
|
116
122
|
.setSource(source);
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
123
|
+
// COMMIT_TRANSACTION / ROLLBACK_TRANSACTION hook point, mirroring Java ProducerImpl#endTransaction.
|
|
124
|
+
const messageHookPoints = resolution === TransactionResolution.COMMIT ?
|
|
125
|
+
MessageHookPoints.COMMIT_TRANSACTION : MessageHookPoints.ROLLBACK_TRANSACTION;
|
|
126
|
+
const context = new MessageInterceptorContextImpl(messageHookPoints);
|
|
127
|
+
const generalMessages: GeneralMessage[] = [ message ];
|
|
128
|
+
this.doBefore(context, generalMessages);
|
|
129
|
+
try {
|
|
130
|
+
const response = await this.rpcClientManager.endTransaction(endpoints, request, this.requestTimeout);
|
|
131
|
+
const statusObj = response.getStatus()?.toObject();
|
|
132
|
+
const hookStatus = statusObj?.code === Code.OK ? MessageHookPointsStatus.OK : MessageHookPointsStatus.ERROR;
|
|
133
|
+
StatusChecker.check(statusObj);
|
|
134
|
+
this.doAfter(MessageInterceptorContextImpl.withStatus(context, hookStatus), generalMessages);
|
|
135
|
+
|
|
136
|
+
this.logger.debug?.('End transaction successfully, messageId=%s, transactionId=%s, resolution=%s, source=%s, clientId=%s',
|
|
137
|
+
messageId, transactionId, resolutionStr, sourceStr, this.clientId);
|
|
138
|
+
} catch (err) {
|
|
139
|
+
this.doAfter(MessageInterceptorContextImpl.withStatus(context, MessageHookPointsStatus.ERROR), generalMessages);
|
|
140
|
+
throw err;
|
|
141
|
+
}
|
|
122
142
|
}
|
|
123
143
|
|
|
124
144
|
async onRecoverOrphanedTransactionCommand(endpoints: Endpoints, command: RecoverOrphanedTransactionCommand) {
|
|
@@ -241,7 +261,18 @@ export class Producer extends BaseClient {
|
|
|
241
261
|
// Prepare the candidate message queue(s) for retry-sending in advance.
|
|
242
262
|
const candidates = messageGroup ? [ loadBalancer.takeMessageQueueByMessageGroup(messageGroup) ] :
|
|
243
263
|
this.#takeMessageQueues(loadBalancer);
|
|
244
|
-
|
|
264
|
+
// SEND hook point, mirroring Java ProducerImpl#send.
|
|
265
|
+
const context = new MessageInterceptorContextImpl(MessageHookPoints.SEND);
|
|
266
|
+
const generalMessages = pubMessages as unknown as GeneralMessage[];
|
|
267
|
+
this.doBefore(context, generalMessages);
|
|
268
|
+
try {
|
|
269
|
+
const receipts = await this.#send0(topic, messageType, candidates, pubMessages, 1);
|
|
270
|
+
this.doAfter(MessageInterceptorContextImpl.withStatus(context, MessageHookPointsStatus.OK), generalMessages);
|
|
271
|
+
return receipts;
|
|
272
|
+
} catch (err) {
|
|
273
|
+
this.doAfter(MessageInterceptorContextImpl.withStatus(context, MessageHookPointsStatus.ERROR), generalMessages);
|
|
274
|
+
throw err;
|
|
275
|
+
}
|
|
245
276
|
}
|
|
246
277
|
|
|
247
278
|
#wrapSendMessageRequest(pubMessages: PublishingMessage[], mq: MessageQueue) {
|
|
@@ -78,8 +78,14 @@ export class PublishingLoadBalancer {
|
|
|
78
78
|
}
|
|
79
79
|
|
|
80
80
|
takeMessageQueueByMessageGroup(messageGroup: string) {
|
|
81
|
-
|
|
82
|
-
|
|
81
|
+
// SipHash-2-4 returns an unsigned 64-bit value in JS (readBigUInt64BE), while
|
|
82
|
+
// Java's String.hashCode-style paths use signed semantics. Interpret the hash as
|
|
83
|
+
// a signed 64-bit integer and apply floorMod so the result is always non-negative,
|
|
84
|
+
// exactly matching Java LongMath.mod(hashCode, size) — otherwise FIFO messages
|
|
85
|
+
// with "negative" hashes route to different queues than the Java client.
|
|
86
|
+
const hashCode = BigInt.asIntN(64, calculateStringSipHash24(messageGroup));
|
|
87
|
+
const size = this.#messageQueues.length;
|
|
88
|
+
const index = Number(((hashCode % BigInt(size)) + BigInt(size)) % BigInt(size));
|
|
83
89
|
return this.#messageQueues[index];
|
|
84
90
|
}
|
|
85
91
|
|
|
@@ -0,0 +1,81 @@
|
|
|
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 assert from 'node:assert';
|
|
19
|
+
import { Duration } from 'google-protobuf/google/protobuf/duration_pb';
|
|
20
|
+
import {
|
|
21
|
+
RetryPolicy as RetryPolicyPB,
|
|
22
|
+
CustomizedBackoff,
|
|
23
|
+
} from '../../proto/apache/rocketmq/v2/definition_pb';
|
|
24
|
+
import { createDuration } from '../util';
|
|
25
|
+
import { RetryPolicy } from './RetryPolicy';
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Backoff policy whose durations are customized by the server side,
|
|
29
|
+
* mirroring Java's CustomizedBackoffRetryPolicy: the Nth attempt waits
|
|
30
|
+
* durations[N - 1], and once attempts run past the list, the last
|
|
31
|
+
* duration applies.
|
|
32
|
+
*/
|
|
33
|
+
export class CustomizedBackoffRetryPolicy implements RetryPolicy {
|
|
34
|
+
#maxAttempts: number;
|
|
35
|
+
// Backoff durations in milliseconds.
|
|
36
|
+
#durations: number[];
|
|
37
|
+
|
|
38
|
+
constructor(durations: number[], maxAttempts: number) {
|
|
39
|
+
assert(Array.isArray(durations) && durations.length > 0, 'durations must not be empty');
|
|
40
|
+
this.#durations = durations;
|
|
41
|
+
this.#maxAttempts = maxAttempts;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
getMaxAttempts(): number {
|
|
45
|
+
return this.#maxAttempts;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
getNextAttemptDelay(attempt: number): number {
|
|
49
|
+
assert(attempt > 0, 'attempt must be positive');
|
|
50
|
+
return attempt > this.#durations.length
|
|
51
|
+
? this.#durations[this.#durations.length - 1]
|
|
52
|
+
: this.#durations[attempt - 1];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
inheritBackoff(retryPolicy: RetryPolicyPB): RetryPolicy {
|
|
56
|
+
assert(retryPolicy.getStrategyCase() === RetryPolicyPB.StrategyCase.CUSTOMIZED_BACKOFF,
|
|
57
|
+
'strategy must be customized backoff');
|
|
58
|
+
return new CustomizedBackoffRetryPolicy(
|
|
59
|
+
CustomizedBackoffRetryPolicy.durationsToMillis(retryPolicy.getCustomizedBackoff()!),
|
|
60
|
+
this.#maxAttempts);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
toProtobuf(): RetryPolicyPB {
|
|
64
|
+
const customizedBackoff = new CustomizedBackoff();
|
|
65
|
+
for (const duration of this.#durations) {
|
|
66
|
+
customizedBackoff.addNext(createDuration(duration));
|
|
67
|
+
}
|
|
68
|
+
return new RetryPolicyPB()
|
|
69
|
+
.setMaxAttempts(this.#maxAttempts)
|
|
70
|
+
.setCustomizedBackoff(customizedBackoff);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Convert the protobuf CustomizedBackoff durations (seconds + nanos) to
|
|
75
|
+
* milliseconds without losing sub-second precision.
|
|
76
|
+
*/
|
|
77
|
+
static durationsToMillis(customizedBackoff: CustomizedBackoff): number[] {
|
|
78
|
+
return customizedBackoff.getNextList().map((duration: Duration) =>
|
|
79
|
+
duration.getSeconds() * 1000 + duration.getNanos() / 1e6);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
@@ -16,16 +16,16 @@
|
|
|
16
16
|
*/
|
|
17
17
|
|
|
18
18
|
import assert from 'node:assert';
|
|
19
|
-
import { Duration } from 'google-protobuf/google/protobuf/duration_pb';
|
|
20
19
|
import {
|
|
21
20
|
RetryPolicy as RetryPolicyPB,
|
|
22
21
|
ExponentialBackoff,
|
|
23
22
|
} from '../../proto/apache/rocketmq/v2/definition_pb';
|
|
23
|
+
import { createDuration } from '../util';
|
|
24
24
|
import { RetryPolicy } from './RetryPolicy';
|
|
25
25
|
|
|
26
26
|
export class ExponentialBackoffRetryPolicy implements RetryPolicy {
|
|
27
27
|
#maxAttempts: number;
|
|
28
|
-
//
|
|
28
|
+
// milliseconds
|
|
29
29
|
#initialBackoff: number;
|
|
30
30
|
#maxBackoff: number;
|
|
31
31
|
#backoffMultiplier: number;
|
|
@@ -58,9 +58,14 @@ export class ExponentialBackoffRetryPolicy implements RetryPolicy {
|
|
|
58
58
|
assert(retryPolicy.getStrategyCase() === RetryPolicyPB.StrategyCase.EXPONENTIAL_BACKOFF,
|
|
59
59
|
'strategy must be exponential backoff');
|
|
60
60
|
const backoff = retryPolicy.getExponentialBackoff()!.toObject();
|
|
61
|
+
// Convert protobuf Duration (seconds + nanos) to milliseconds without
|
|
62
|
+
// losing sub-second precision (e.g. an initial backoff of 0.5s used to
|
|
63
|
+
// be truncated to plain 0, causing immediate retries).
|
|
64
|
+
const toMillis = (duration?: { seconds?: number; nanos?: number }) =>
|
|
65
|
+
(duration ? (duration.seconds ?? 0) * 1000 + (duration.nanos ?? 0) / 1e6 : 0);
|
|
61
66
|
return new ExponentialBackoffRetryPolicy(this.#maxAttempts,
|
|
62
|
-
backoff.initial
|
|
63
|
-
backoff.max
|
|
67
|
+
toMillis(backoff.initial),
|
|
68
|
+
toMillis(backoff.max),
|
|
64
69
|
backoff.multiplier);
|
|
65
70
|
}
|
|
66
71
|
|
|
@@ -69,8 +74,8 @@ export class ExponentialBackoffRetryPolicy implements RetryPolicy {
|
|
|
69
74
|
.setMaxAttempts(this.#maxAttempts)
|
|
70
75
|
.setExponentialBackoff(
|
|
71
76
|
new ExponentialBackoff()
|
|
72
|
-
.setInitial(
|
|
73
|
-
.setMax(
|
|
77
|
+
.setInitial(createDuration(this.#initialBackoff))
|
|
78
|
+
.setMax(createDuration(this.#maxBackoff))
|
|
74
79
|
.setMultiplier(this.#backoffMultiplier));
|
|
75
80
|
}
|
|
76
81
|
}
|
package/src/retry/RetryPolicy.ts
CHANGED
|
@@ -32,7 +32,7 @@ export interface RetryPolicy {
|
|
|
32
32
|
* Get await time after current attempts, the attempt index starts at 1.
|
|
33
33
|
*
|
|
34
34
|
* @param attempt current attempt.
|
|
35
|
-
* @return await time in
|
|
35
|
+
* @return await time in milliseconds.
|
|
36
36
|
*/
|
|
37
37
|
getNextAttemptDelay(attempt: number): number;
|
|
38
38
|
|
package/src/retry/index.ts
CHANGED
package/src/route/Endpoints.ts
CHANGED
|
@@ -18,6 +18,9 @@
|
|
|
18
18
|
import { isIPv4, isIPv6 } from 'node:net';
|
|
19
19
|
import { hashCodeOfString } from '../util';
|
|
20
20
|
import { Address, AddressScheme, Endpoints as EndpointsPB } from '../../proto/apache/rocketmq/v2/definition_pb';
|
|
21
|
+
// Side-effect import: registers the custom `ip` name resolver (shuffle + multi-address
|
|
22
|
+
// load balancing), mirroring the Java client's IpNameResolverFactory.
|
|
23
|
+
import './IpNameResolver';
|
|
21
24
|
|
|
22
25
|
const DEFAULT_PORT = 80;
|
|
23
26
|
|
|
@@ -35,7 +38,33 @@ export class Endpoints {
|
|
|
35
38
|
const splits = endpoints.split(';');
|
|
36
39
|
this.addressesList = [];
|
|
37
40
|
for (const endpoint of splits) {
|
|
38
|
-
|
|
41
|
+
// Strip the optional http:// or https:// prefix, mirroring the Java client.
|
|
42
|
+
const candidate = endpoint.trim().replace(/^https?:\/\//, '');
|
|
43
|
+
let host: string;
|
|
44
|
+
let port: number;
|
|
45
|
+
if (candidate.startsWith('[')) {
|
|
46
|
+
// Bracketed IPv6 address, e.g. [::1]:10911 or [fe80::1]
|
|
47
|
+
const match = candidate.match(/^\[([^\]]+)\](?::(\d+))?$/);
|
|
48
|
+
if (!match) {
|
|
49
|
+
throw new TypeError(`Invalid IPv6 endpoint: ${endpoint}`);
|
|
50
|
+
}
|
|
51
|
+
host = match[1];
|
|
52
|
+
port = match[2] ? parseInt(match[2], 10) : DEFAULT_PORT;
|
|
53
|
+
} else if (isIPv6(candidate)) {
|
|
54
|
+
// Bare IPv6 address without brackets, e.g. ::1
|
|
55
|
+
host = candidate;
|
|
56
|
+
port = DEFAULT_PORT;
|
|
57
|
+
} else {
|
|
58
|
+
// IPv4 address or domain name, e.g. 127.0.0.1:10911, example.com:80
|
|
59
|
+
const index = candidate.lastIndexOf(':');
|
|
60
|
+
if (index > 0) {
|
|
61
|
+
host = candidate.substring(0, index);
|
|
62
|
+
port = parseInt(candidate.substring(index + 1), 10) || DEFAULT_PORT;
|
|
63
|
+
} else {
|
|
64
|
+
host = candidate;
|
|
65
|
+
port = DEFAULT_PORT;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
39
68
|
if (isIPv4(host)) {
|
|
40
69
|
this.scheme = AddressScheme.IPV4;
|
|
41
70
|
} else if (isIPv6(host)) {
|
|
@@ -43,7 +72,7 @@ export class Endpoints {
|
|
|
43
72
|
} else {
|
|
44
73
|
this.scheme = AddressScheme.DOMAIN_NAME;
|
|
45
74
|
}
|
|
46
|
-
this.addressesList.push({ host, port
|
|
75
|
+
this.addressesList.push({ host, port });
|
|
47
76
|
}
|
|
48
77
|
} else {
|
|
49
78
|
this.scheme = endpoints.scheme;
|
|
@@ -52,8 +81,28 @@ export class Endpoints {
|
|
|
52
81
|
this.facade = this.addressesList.map(addr => `${addr.host}:${addr.port}`).join(',');
|
|
53
82
|
}
|
|
54
83
|
|
|
84
|
+
/**
|
|
85
|
+
* gRPC target with resolver scheme prefix:
|
|
86
|
+
* - IP addresses: ip:127.0.0.1:10911,127.0.0.2:10912 (custom resolver, IPv6 hosts bracketed)
|
|
87
|
+
* - Domain names: dns:example.com:8080,example.org:8081
|
|
88
|
+
*/
|
|
55
89
|
getGrpcTarget() {
|
|
56
|
-
|
|
90
|
+
const targets = this.addressesList.map(addr => {
|
|
91
|
+
const host = this.scheme === AddressScheme.IPV6 ? `[${addr.host}]` : addr.host;
|
|
92
|
+
return `${host}:${addr.port}`;
|
|
93
|
+
}).join(',');
|
|
94
|
+
switch (this.scheme) {
|
|
95
|
+
case AddressScheme.IPV4:
|
|
96
|
+
case AddressScheme.IPV6:
|
|
97
|
+
// Route IP addresses through the custom `ip` resolver registered in
|
|
98
|
+
// IpNameResolver, which shuffles the address list for multi-proxy load
|
|
99
|
+
// balancing (equivalent to Java's IpNameResolverFactory).
|
|
100
|
+
return `ip:${targets}`;
|
|
101
|
+
case AddressScheme.DOMAIN_NAME:
|
|
102
|
+
return `dns:${targets}`;
|
|
103
|
+
default:
|
|
104
|
+
return targets;
|
|
105
|
+
}
|
|
57
106
|
}
|
|
58
107
|
|
|
59
108
|
toString() {
|