Skip to content

Commit e1658ba

Browse files
carlhopfleibale
andauthored
fix cluster extractFirstKey skip commandOptions() passed to args (#2439)
* cluster extractFirstKey skip commandOptions() passed to args * cluster with commandOptions unit test * improve performance * fix type * fix type --------- Co-authored-by: Leibale Eidelman <[email protected]>
1 parent c88dea6 commit e1658ba

File tree

3 files changed

+20
-2
lines changed

3 files changed

+20
-2
lines changed

packages/client/lib/cluster/index.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { strict as assert } from 'assert';
22
import testUtils, { GLOBAL, waitTillBeenCalled } from '../test-utils';
33
import RedisCluster from '.';
44
import { ClusterSlotStates } from '../commands/CLUSTER_SETSLOT';
5+
import { commandOptions } from '../command-options';
56
import { SQUARE_SCRIPT } from '../client/index.spec';
67
import { RootNodesUnavailableError } from '../errors';
78
import { spy } from 'sinon';
@@ -178,6 +179,21 @@ describe('Cluster', () => {
178179
await assert.rejects(cluster.mGet(['a', 'b']));
179180
}, GLOBAL.CLUSTERS.OPEN);
180181

182+
testUtils.testWithCluster('should send commands with commandOptions to correct cluster slot (without redirections)', async cluster => {
183+
// 'a' and 'b' hash to different cluster slots (see previous unit test)
184+
// -> maxCommandRedirections 0: rejects on MOVED/ASK reply
185+
await cluster.set(commandOptions({ isolated: true }), 'a', '1'),
186+
await cluster.set(commandOptions({ isolated: true }), 'b', '2'),
187+
188+
assert.equal(await cluster.get('a'), '1');
189+
assert.equal(await cluster.get('b'), '2');
190+
}, {
191+
...GLOBAL.CLUSTERS.OPEN,
192+
clusterConfiguration: {
193+
maxCommandRedirections: 0
194+
}
195+
});
196+
181197
describe('minimizeConnections', () => {
182198
testUtils.testWithCluster('false', async cluster => {
183199
for (const master of cluster.masters) {

packages/client/lib/cluster/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,11 +152,11 @@ export default class RedisCluster<
152152
command: C,
153153
args: Array<unknown>
154154
): Promise<RedisCommandReply<C>> {
155-
const { args: redisArgs, options } = transformCommandArguments(command, args);
155+
const { jsArgs, args: redisArgs, options } = transformCommandArguments(command, args);
156156
return transformCommandReply(
157157
command,
158158
await this.sendCommand(
159-
RedisCluster.extractFirstKey(command, args, redisArgs),
159+
RedisCluster.extractFirstKey(command, jsArgs, redisArgs),
160160
command.IS_READ_ONLY,
161161
redisArgs,
162162
options

packages/client/lib/commander.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ export function transformCommandArguments<T = ClientCommandOptions>(
108108
command: RedisCommand,
109109
args: Array<unknown>
110110
): {
111+
jsArgs: Array<unknown>;
111112
args: RedisCommandArguments;
112113
options: CommandOptions<T> | undefined;
113114
} {
@@ -118,6 +119,7 @@ export function transformCommandArguments<T = ClientCommandOptions>(
118119
}
119120

120121
return {
122+
jsArgs: args,
121123
args: command.transformArguments(...args),
122124
options
123125
};

0 commit comments

Comments
 (0)