Skip to content

asking / command can be separated on cluster redirect. #2713

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions packages/client/lib/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,8 @@ export default class RedisClient<
*/
async _executeMulti(
commands: Array<RedisMultiQueuedCommand>,
selectedDB?: number
selectedDB?: number,
options?: CommandOptions
) {
const dirtyWatch = this._self.#dirtyWatch;
this._self.#dirtyWatch = undefined;
Expand All @@ -831,9 +832,10 @@ export default class RedisClient<
throw new WatchError('Client reconnected after WATCH');
}

const typeMapping = this._commandOptions?.typeMapping,
chainId = Symbol('MULTI Chain'),
promises = [
const chainId = options?.chainId ? options.chainId : Symbol('MULTI Chain');
const typeMapping = options?.typeMapping ? options.typeMapping : this._commandOptions?.typeMapping;

const promises = [
this._self.#queue.addCommand(['MULTI'], { chainId }),
];

Expand Down
51 changes: 42 additions & 9 deletions packages/client/lib/cluster/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import RedisClusterMultiCommand, { RedisClusterMultiCommandType } from './multi-
import { PubSubListener } from '../client/pub-sub';
import { ErrorReply } from '../errors';
import { RedisTcpSocketOptions } from '../client/socket';
import ASKING from '../commands/ASKING';

interface ClusterCommander<
M extends RedisModules,
Expand Down Expand Up @@ -433,18 +434,43 @@ export default class RedisCluster<
// return this._commandOptionsProxy('policies', policies);
// }

#handleAsk<T>(
fn: (client: RedisClientType<M, F, S, RESP, TYPE_MAPPING>, opts?: ClusterCommandOptions) => Promise<T>
) {
return async (client: RedisClientType<M, F, S, RESP, TYPE_MAPPING>, options?: ClusterCommandOptions) => {
const chainId = Symbol("asking chain");
const opts = options ? {...options} : {};
opts.chainId = chainId;

const ret = await Promise.all(
[
client.sendCommand(ASKING.transformArguments(), {chainId: chainId}),
fn(client, opts)
]
);

return ret[1];
};
}

async #execute<T>(
firstKey: RedisArgument | undefined,
isReadonly: boolean | undefined,
fn: (client: RedisClientType<M, F, S, RESP, TYPE_MAPPING>) => Promise<T>
options: ClusterCommandOptions | undefined,
fn: (client: RedisClientType<M, F, S, RESP, TYPE_MAPPING>, opts?: ClusterCommandOptions) => Promise<T>
): Promise<T> {
const maxCommandRedirections = this.#options.maxCommandRedirections ?? 16;
let client = await this.#slots.getClient(firstKey, isReadonly),
i = 0;
let client = await this.#slots.getClient(firstKey, isReadonly);
let i = 0;

let myFn = fn;

while (true) {
try {
return await fn(client);
return await myFn(client, options);
} catch (err) {
myFn = fn;

// TODO: error class
if (++i > maxCommandRedirections || !(err instanceof Error)) {
throw err;
Expand All @@ -462,7 +488,8 @@ export default class RedisCluster<
throw new Error(`Cannot find node ${address}`);
}

await redirectTo.asking();
myFn = this.#handleAsk(fn);

client = redirectTo;
continue;
}
Expand All @@ -488,7 +515,8 @@ export default class RedisCluster<
return this._self.#execute(
firstKey,
isReadonly,
client => client.sendCommand(args, options)
options,
(client, opts) => client.sendCommand(args, opts)
);
}

Expand All @@ -502,16 +530,21 @@ export default class RedisCluster<
return this._self.#execute(
firstKey,
isReadonly,
client => client.executeScript(script, args, options)
options,
(client, opts) => client.executeScript(script, args, opts)
);
}

MULTI(routing?: RedisArgument) {
type Multi = new (...args: ConstructorParameters<typeof RedisClusterMultiCommand>) => RedisClusterMultiCommandType<[], M, F, S, RESP, TYPE_MAPPING>;
return new ((this as any).Multi as Multi)(
async (firstKey, isReadonly, commands) => {
const client = await this._self.#slots.getClient(firstKey, isReadonly);
return client._executeMulti(commands);
return this._self.#execute(
firstKey,
isReadonly,
this._self._commandOptions,
(client, opts) => client._executeMulti(commands, undefined, opts)
)
},
async (firstKey, isReadonly, commands) => {
const client = await this._self.#slots.getClient(firstKey, isReadonly);
Expand Down