Skip to content

Support new cluster commands #2050

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

Merged
merged 2 commits into from
Mar 27, 2022
Merged
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
9 changes: 9 additions & 0 deletions packages/client/lib/client/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,11 @@ import * as CLIENT_KILL from '../commands/CLIENT_KILL';
import * as CLIENT_SETNAME from '../commands/CLIENT_SETNAME';
import * as CLIENT_INFO from '../commands/CLIENT_INFO';
import * as CLUSTER_ADDSLOTS from '../commands/CLUSTER_ADDSLOTS';
import * as CLUSTER_ADDSLOTSRANGE from '../commands/CLUSTER_ADDSLOTSRANGE';
import * as CLUSTER_DELSLOTSRANGE from '../commands/CLUSTER_DELSLOTSRANGE';
import * as CLUSTER_FLUSHSLOTS from '../commands/CLUSTER_FLUSHSLOTS';
import * as CLUSTER_INFO from '../commands/CLUSTER_INFO';
import * as CLUSTER_LINKS from '../commands/CLUSTER_LINKS';
import * as CLUSTER_NODES from '../commands/CLUSTER_NODES';
import * as CLUSTER_MEET from '../commands/CLUSTER_MEET';
import * as CLUSTER_RESET from '../commands/CLUSTER_RESET';
Expand Down Expand Up @@ -132,10 +135,16 @@ export default {
clientInfo: CLIENT_INFO,
CLUSTER_ADDSLOTS,
clusterAddSlots: CLUSTER_ADDSLOTS,
CLUSTER_ADDSLOTSRANGE,
clusterAddSlotsRange: CLUSTER_ADDSLOTSRANGE,
CLUSTER_DELSLOTSRANGE,
clusterDelSlotsRange: CLUSTER_DELSLOTSRANGE,
CLUSTER_FLUSHSLOTS,
clusterFlushSlots: CLUSTER_FLUSHSLOTS,
CLUSTER_INFO,
clusterInfo: CLUSTER_INFO,
CLUSTER_LINKS,
clusterLinks: CLUSTER_LINKS,
CLUSTER_NODES,
clusterNodes: CLUSTER_NODES,
CLUSTER_MEET,
Expand Down
29 changes: 29 additions & 0 deletions packages/client/lib/commands/CLUSTER_ADDSLOTSRANGE.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { strict as assert } from 'assert';
import { transformArguments } from './CLUSTER_ADDSLOTSRANGE';

describe('CLUSTER ADDSLOTSRANGE', () => {
describe('transformArguments', () => {
it('single', () => {
assert.deepEqual(
transformArguments({
start: 0,
end: 1
}),
['CLUSTER', 'ADDSLOTSRANGE', '0', '1']
);
});

it('multiple', () => {
assert.deepEqual(
transformArguments([{
start: 0,
end: 1
}, {
start: 2,
end: 3
}]),
['CLUSTER', 'ADDSLOTSRANGE', '0', '1', '2', '3']
);
});
});
});
13 changes: 13 additions & 0 deletions packages/client/lib/commands/CLUSTER_ADDSLOTSRANGE.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { RedisCommandArguments } from '.';
import { pushSlotRangesArguments, SlotRange } from './generic-transformers';

export function transformArguments(
ranges: SlotRange | Array<SlotRange>
): RedisCommandArguments {
return pushSlotRangesArguments(
['CLUSTER', 'ADDSLOTSRANGE'],
ranges
);
}

export declare function transformReply(): 'OK';
29 changes: 29 additions & 0 deletions packages/client/lib/commands/CLUSTER_DELSLOTSRANGE.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { strict as assert } from 'assert';
import { transformArguments } from './CLUSTER_DELSLOTSRANGE';

describe('CLUSTER DELSLOTSRANGE', () => {
describe('transformArguments', () => {
it('single', () => {
assert.deepEqual(
transformArguments({
start: 0,
end: 1
}),
['CLUSTER', 'DELSLOTSRANGE', '0', '1']
);
});

it('multiple', () => {
assert.deepEqual(
transformArguments([{
start: 0,
end: 1
}, {
start: 2,
end: 3
}]),
['CLUSTER', 'DELSLOTSRANGE', '0', '1', '2', '3']
);
});
});
});
13 changes: 13 additions & 0 deletions packages/client/lib/commands/CLUSTER_DELSLOTSRANGE.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { RedisCommandArguments } from '.';
import { pushSlotRangesArguments, SlotRange } from './generic-transformers';

export function transformArguments(
ranges: SlotRange | Array<SlotRange>
): RedisCommandArguments {
return pushSlotRangesArguments(
['CLUSTER', 'DELSLOTSRANGE'],
ranges
);
}

export declare function transformReply(): 'OK';
27 changes: 27 additions & 0 deletions packages/client/lib/commands/CLUSTER_LINKS.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { strict as assert } from 'assert';
import testUtils, { GLOBAL } from '../test-utils';
import { transformArguments } from './CLUSTER_LINKS';

describe('CLUSTER LINKS', () => {
testUtils.isVersionGreaterThanHook([7, 0]);

it('transformArguments', () => {
assert.deepEqual(
transformArguments(),
['CLUSTER', 'LINKS']
);
});

testUtils.testWithCluster('clusterNode.clusterLinks', async cluster => {
const links = await cluster.getSlotMaster(0).client.clusterLinks();
assert.ok(Array.isArray(links));
for (const link of links) {
assert.equal(typeof link.direction, 'string');
assert.equal(typeof link.node, 'string');
assert.equal(typeof link.createTime, 'number');
assert.equal(typeof link.events, 'string');
assert.equal(typeof link.sendBufferAllocated, 'number');
assert.equal(typeof link.sendBufferUsed, 'number');
}
}, GLOBAL.CLUSTERS.OPEN);
});
38 changes: 38 additions & 0 deletions packages/client/lib/commands/CLUSTER_LINKS.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
export function transformArguments(): Array<string> {
return ['CLUSTER', 'LINKS'];
}

type ClusterLinksRawReply = Array<[
'direction',
string,
'node',
string,
'createTime',
number,
'events',
string,
'send-buffer-allocated',
number,
'send-buffer-used',
number
]>;

type ClusterLinksReply = Array<{
direction: string;
node: string;
createTime: number;
events: string;
sendBufferAllocated: number;
sendBufferUsed: number;
}>;

export function transformReply(reply: ClusterLinksRawReply): ClusterLinksReply {
return reply.map(peerLink => ({
direction: peerLink[1],
node: peerLink[3],
createTime: Number(peerLink[5]),
events: peerLink[7],
sendBufferAllocated: Number(peerLink[9]),
sendBufferUsed: Number(peerLink[11])
}));
}
28 changes: 27 additions & 1 deletion packages/client/lib/commands/generic-transformers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import {
pushOptionalVerdictArgument,
transformCommandReply,
CommandFlags,
CommandCategories
CommandCategories,
pushSlotRangesArguments
} from './generic-transformers';

describe('Generic Transformers', () => {
Expand Down Expand Up @@ -639,4 +640,29 @@ describe('Generic Transformers', () => {
}
);
});

describe('pushSlotRangesArguments', () => {
it('single range', () => {
assert.deepEqual(
pushSlotRangesArguments([], {
start: 0,
end: 1
}),
['0', '1']
);
});

it('multiple ranges', () => {
assert.deepEqual(
pushSlotRangesArguments([], [{
start: 0,
end: 1
}, {
start: 2,
end: 3
}]),
['0', '1', '2', '3']
);
});
});
});
30 changes: 30 additions & 0 deletions packages/client/lib/commands/generic-transformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,3 +422,33 @@ export function transformCommandReply(
categories: new Set(categories)
};
}

export interface SlotRange {
start: number;
end: number;
}

function pushSlotRangeArguments(
args: RedisCommandArguments,
range: SlotRange
): void {
args.push(
range.start.toString(),
range.end.toString()
);
}

export function pushSlotRangesArguments(
args: RedisCommandArguments,
ranges: SlotRange | Array<SlotRange>
): RedisCommandArguments {
if (Array.isArray(ranges)) {
for (const range of ranges) {
pushSlotRangeArguments(args, range);
}
} else {
pushSlotRangeArguments(args, ranges);
}

return args;
}