Skip to content

Commit 6b8a40a

Browse files
Avital-Fineleibale
andauthored
Support new cluster commands (#2050)
* Support new cluster commands * clean code Co-authored-by: leibale <leibale1998@gmail.com>
1 parent fe16dc0 commit 6b8a40a

9 files changed

+215
-1
lines changed

packages/client/lib/client/commands.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ import * as CLIENT_KILL from '../commands/CLIENT_KILL';
2323
import * as CLIENT_SETNAME from '../commands/CLIENT_SETNAME';
2424
import * as CLIENT_INFO from '../commands/CLIENT_INFO';
2525
import * as CLUSTER_ADDSLOTS from '../commands/CLUSTER_ADDSLOTS';
26+
import * as CLUSTER_ADDSLOTSRANGE from '../commands/CLUSTER_ADDSLOTSRANGE';
27+
import * as CLUSTER_DELSLOTSRANGE from '../commands/CLUSTER_DELSLOTSRANGE';
2628
import * as CLUSTER_FLUSHSLOTS from '../commands/CLUSTER_FLUSHSLOTS';
2729
import * as CLUSTER_INFO from '../commands/CLUSTER_INFO';
30+
import * as CLUSTER_LINKS from '../commands/CLUSTER_LINKS';
2831
import * as CLUSTER_NODES from '../commands/CLUSTER_NODES';
2932
import * as CLUSTER_MEET from '../commands/CLUSTER_MEET';
3033
import * as CLUSTER_RESET from '../commands/CLUSTER_RESET';
@@ -132,10 +135,16 @@ export default {
132135
clientInfo: CLIENT_INFO,
133136
CLUSTER_ADDSLOTS,
134137
clusterAddSlots: CLUSTER_ADDSLOTS,
138+
CLUSTER_ADDSLOTSRANGE,
139+
clusterAddSlotsRange: CLUSTER_ADDSLOTSRANGE,
140+
CLUSTER_DELSLOTSRANGE,
141+
clusterDelSlotsRange: CLUSTER_DELSLOTSRANGE,
135142
CLUSTER_FLUSHSLOTS,
136143
clusterFlushSlots: CLUSTER_FLUSHSLOTS,
137144
CLUSTER_INFO,
138145
clusterInfo: CLUSTER_INFO,
146+
CLUSTER_LINKS,
147+
clusterLinks: CLUSTER_LINKS,
139148
CLUSTER_NODES,
140149
clusterNodes: CLUSTER_NODES,
141150
CLUSTER_MEET,
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { strict as assert } from 'assert';
2+
import { transformArguments } from './CLUSTER_ADDSLOTSRANGE';
3+
4+
describe('CLUSTER ADDSLOTSRANGE', () => {
5+
describe('transformArguments', () => {
6+
it('single', () => {
7+
assert.deepEqual(
8+
transformArguments({
9+
start: 0,
10+
end: 1
11+
}),
12+
['CLUSTER', 'ADDSLOTSRANGE', '0', '1']
13+
);
14+
});
15+
16+
it('multiple', () => {
17+
assert.deepEqual(
18+
transformArguments([{
19+
start: 0,
20+
end: 1
21+
}, {
22+
start: 2,
23+
end: 3
24+
}]),
25+
['CLUSTER', 'ADDSLOTSRANGE', '0', '1', '2', '3']
26+
);
27+
});
28+
});
29+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { RedisCommandArguments } from '.';
2+
import { pushSlotRangesArguments, SlotRange } from './generic-transformers';
3+
4+
export function transformArguments(
5+
ranges: SlotRange | Array<SlotRange>
6+
): RedisCommandArguments {
7+
return pushSlotRangesArguments(
8+
['CLUSTER', 'ADDSLOTSRANGE'],
9+
ranges
10+
);
11+
}
12+
13+
export declare function transformReply(): 'OK';
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { strict as assert } from 'assert';
2+
import { transformArguments } from './CLUSTER_DELSLOTSRANGE';
3+
4+
describe('CLUSTER DELSLOTSRANGE', () => {
5+
describe('transformArguments', () => {
6+
it('single', () => {
7+
assert.deepEqual(
8+
transformArguments({
9+
start: 0,
10+
end: 1
11+
}),
12+
['CLUSTER', 'DELSLOTSRANGE', '0', '1']
13+
);
14+
});
15+
16+
it('multiple', () => {
17+
assert.deepEqual(
18+
transformArguments([{
19+
start: 0,
20+
end: 1
21+
}, {
22+
start: 2,
23+
end: 3
24+
}]),
25+
['CLUSTER', 'DELSLOTSRANGE', '0', '1', '2', '3']
26+
);
27+
});
28+
});
29+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { RedisCommandArguments } from '.';
2+
import { pushSlotRangesArguments, SlotRange } from './generic-transformers';
3+
4+
export function transformArguments(
5+
ranges: SlotRange | Array<SlotRange>
6+
): RedisCommandArguments {
7+
return pushSlotRangesArguments(
8+
['CLUSTER', 'DELSLOTSRANGE'],
9+
ranges
10+
);
11+
}
12+
13+
export declare function transformReply(): 'OK';
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { strict as assert } from 'assert';
2+
import testUtils, { GLOBAL } from '../test-utils';
3+
import { transformArguments } from './CLUSTER_LINKS';
4+
5+
describe('CLUSTER LINKS', () => {
6+
testUtils.isVersionGreaterThanHook([7, 0]);
7+
8+
it('transformArguments', () => {
9+
assert.deepEqual(
10+
transformArguments(),
11+
['CLUSTER', 'LINKS']
12+
);
13+
});
14+
15+
testUtils.testWithCluster('clusterNode.clusterLinks', async cluster => {
16+
const links = await cluster.getSlotMaster(0).client.clusterLinks();
17+
assert.ok(Array.isArray(links));
18+
for (const link of links) {
19+
assert.equal(typeof link.direction, 'string');
20+
assert.equal(typeof link.node, 'string');
21+
assert.equal(typeof link.createTime, 'number');
22+
assert.equal(typeof link.events, 'string');
23+
assert.equal(typeof link.sendBufferAllocated, 'number');
24+
assert.equal(typeof link.sendBufferUsed, 'number');
25+
}
26+
}, GLOBAL.CLUSTERS.OPEN);
27+
});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
export function transformArguments(): Array<string> {
2+
return ['CLUSTER', 'LINKS'];
3+
}
4+
5+
type ClusterLinksRawReply = Array<[
6+
'direction',
7+
string,
8+
'node',
9+
string,
10+
'createTime',
11+
number,
12+
'events',
13+
string,
14+
'send-buffer-allocated',
15+
number,
16+
'send-buffer-used',
17+
number
18+
]>;
19+
20+
type ClusterLinksReply = Array<{
21+
direction: string;
22+
node: string;
23+
createTime: number;
24+
events: string;
25+
sendBufferAllocated: number;
26+
sendBufferUsed: number;
27+
}>;
28+
29+
export function transformReply(reply: ClusterLinksRawReply): ClusterLinksReply {
30+
return reply.map(peerLink => ({
31+
direction: peerLink[1],
32+
node: peerLink[3],
33+
createTime: Number(peerLink[5]),
34+
events: peerLink[7],
35+
sendBufferAllocated: Number(peerLink[9]),
36+
sendBufferUsed: Number(peerLink[11])
37+
}));
38+
}

packages/client/lib/commands/generic-transformers.spec.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ import {
2323
pushOptionalVerdictArgument,
2424
transformCommandReply,
2525
CommandFlags,
26-
CommandCategories
26+
CommandCategories,
27+
pushSlotRangesArguments
2728
} from './generic-transformers';
2829

2930
describe('Generic Transformers', () => {
@@ -639,4 +640,29 @@ describe('Generic Transformers', () => {
639640
}
640641
);
641642
});
643+
644+
describe('pushSlotRangesArguments', () => {
645+
it('single range', () => {
646+
assert.deepEqual(
647+
pushSlotRangesArguments([], {
648+
start: 0,
649+
end: 1
650+
}),
651+
['0', '1']
652+
);
653+
});
654+
655+
it('multiple ranges', () => {
656+
assert.deepEqual(
657+
pushSlotRangesArguments([], [{
658+
start: 0,
659+
end: 1
660+
}, {
661+
start: 2,
662+
end: 3
663+
}]),
664+
['0', '1', '2', '3']
665+
);
666+
});
667+
});
642668
});

packages/client/lib/commands/generic-transformers.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,3 +422,33 @@ export function transformCommandReply(
422422
categories: new Set(categories)
423423
};
424424
}
425+
426+
export interface SlotRange {
427+
start: number;
428+
end: number;
429+
}
430+
431+
function pushSlotRangeArguments(
432+
args: RedisCommandArguments,
433+
range: SlotRange
434+
): void {
435+
args.push(
436+
range.start.toString(),
437+
range.end.toString()
438+
);
439+
}
440+
441+
export function pushSlotRangesArguments(
442+
args: RedisCommandArguments,
443+
ranges: SlotRange | Array<SlotRange>
444+
): RedisCommandArguments {
445+
if (Array.isArray(ranges)) {
446+
for (const range of ranges) {
447+
pushSlotRangeArguments(args, range);
448+
}
449+
} else {
450+
pushSlotRangeArguments(args, ranges);
451+
}
452+
453+
return args;
454+
}

0 commit comments

Comments
 (0)