PDGGK opened a new pull request, #1474:
URL: https://github.com/apache/rocketmq-clients/pull/1474
### Which Issue(s) This PR Fixes
Fixes #866
### Brief Description
The Node.js `PublishingLoadBalancer` and `SubscriptionLoadBalancer` are
meant to keep the writable (or readable) queues of master brokers, as the Java
client does. They compare the wrong field:
```ts
return mq.queueId === MASTER_BROKER_ID && (mq.permission ===
Permission.WRITE || mq.permission === Permission.READ_WRITE);
```
`MASTER_BROKER_ID` is a broker id, but `mq.queueId` is the queue number. So
only queue 0 of every broker is kept, and queue 0 of a slave broker is kept as
well.
**What users see.** This is the `RangeError` from `new
PublishingLoadBalancer` reported in #866, which the stale bot closed without a
fix. The proxy numbers a topic's queues read-only first, then write-only, then
read-write. When the read and write queue counts differ, for example while a
topic is scaled in or out, queue 0 is read-only or write-only. The filter then
keeps nothing. `randomInt(0)` runs before the "No writable message queue found"
check and throws:
- `Producer.send()` to a topic with readQueueNums=8, writeQueueNums=4 fails
with `RangeError: The value of "max" is out of range. It must be greater than
the value of "min" (0). Received 0`.
- `SimpleConsumer.receive()` from a topic with readQueueNums=4,
writeQueueNums=8 fails with the same `RangeError`.
With equal counts nothing fails, because queue 0 is read-write.
**Change.**
- Compare `mq.broker.id` with `MASTER_BROKER_ID`. This matches Java
`PublishingLoadBalancer` / `SubscriptionLoadBalancer` and the Node.js
`firstReadableMasterQueue` used by `LiteSimpleConsumer`.
- Check for an empty queue list before picking the random start index. A
route without a usable queue then fails with "No writable message queue found"
/ "No readable message queue found", as the code intended.
Note for reviewers: `takeMessageQueueByMessageGroup` now hashes over the
same queue list as Java (all writable master queues) instead of one queue per
broker. The existing floorMod tests from #1388 pass unchanged.
### How Did You Test This Change?
**Unit tests.** I added 3 tests for each load balancer. Each test builds a
route the way the proxy does:
- writable (or readable) queues are picked whatever their queue id;
- queues of a slave broker (broker id 1) are not picked;
- a route without a usable master queue is rejected with the "No ... message
queue found" error.
With the two production changes reverted, all 6 tests fail for the reasons
above:
- the first test in each file throws the `RangeError`;
- the slave test picks `1/0` (broker 1, queue 0);
- the rejection test reports `Missing expected exception`.
With the change, all 8 tests in the two files pass:
```
$ TS_NODE_TRANSPILE_ONLY=true node --require ts-node/register --test \
test/producer/PublishingLoadBalancer.test.ts
test/consumer/SubscriptionLoadBalancer.test.ts
ℹ tests 8
ℹ pass 8
ℹ fail 0
```
`egg-bin test` runs these `node:test` files as well and prints the same
results. However, it exits 0 even when a `node:test` case fails, so I used
Node's runner to get the exit code for the before/after check.
**End to end.** I ran this against Apache RocketMQ 5.3.3 in Docker
(`mqbroker --enable-proxy`) with three topics, created with `mqadmin
updateTopic -r <read> -w <write>`. A small script sends one message to each
topic with `Producer.send()`. It then calls `SimpleConsumer.receive()` once per
topic.
Before (current master; the released 1.1.0 from npm prints the same two
failures):
```
send HuntTopicRW OK messageId=0186AF42699A8DF1960AFB7FEF00000000
send HuntTopicR8W4 FAIL RangeError: The value of "max" is out of
range. It must be greater than the value of "min" (0). Received 0
send HuntTopicW8R4 OK messageId=0186AF42699A8DF1960AFB7FEF00000002
receive HuntTopicRW OK got 1 message(s)
receive HuntTopicR8W4 OK got 0 message(s)
receive HuntTopicW8R4 FAIL RangeError: The value of "max" is out of
range. It must be greater than the value of "min" (0). Received 0
```
After (topics recreated empty):
```
send HuntTopicRW OK messageId=0186AF42699A8DF60B0AFB801700000000
send HuntTopicR8W4 OK messageId=0186AF42699A8DF60B0AFB801700000001
send HuntTopicW8R4 OK messageId=0186AF42699A8DF60B0AFB801700000002
receive HuntTopicRW OK got 1 message(s)
receive HuntTopicR8W4 OK got 1 message(s)
receive HuntTopicW8R4 OK got 0 message(s)
```
(`HuntTopicW8R4` has 8 write queues and 4 read queues. A message is only
readable when the proxy writes it to queues 0–3, so `got 0` can happen there.
What matters is that `receive()` no longer throws.)
**Other checks.**
- `test/producer/Producer.test.ts` and
`test/consumer/SimpleConsumer.test.ts` against the same broker: 10 passing.
- `test/consumer/LiteSimpleConsumer.test.ts` and `test/fixes.test.ts`: no
failures.
- `npx eslint src test`: 0 errors. The 54 warnings are all in files this PR
does not touch.
- `npx tsc -p tsconfig.test.json --noEmit`: clean.
I did not test the slave-broker case end to end, since it needs a
master/slave deployment. The unit test covers it.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]