From ae1548ae1a0324defc65b2358190dab4a565066b Mon Sep 17 00:00:00 2001 From: Avital-Fine Date: Tue, 22 Mar 2022 12:13:16 +0100 Subject: [PATCH 1/2] Support new cluster commands --- packages/client/lib/client/commands.ts | 9 +++++++ .../commands/CLUSTER_ADDSLOTSRANGE.spec.ts | 20 +++++++++++++++ .../lib/commands/CLUSTER_ADDSLOTSRANGE.ts | 11 ++++++++ .../commands/CLUSTER_DELSLOTSRANGE.spec.ts | 20 +++++++++++++++ .../lib/commands/CLUSTER_DELSLOTSRANGE.ts | 11 ++++++++ .../client/lib/commands/CLUSTER_LINKS.spec.ts | 22 ++++++++++++++++ packages/client/lib/commands/CLUSTER_LINKS.ts | 25 +++++++++++++++++++ 7 files changed, 118 insertions(+) create mode 100644 packages/client/lib/commands/CLUSTER_ADDSLOTSRANGE.spec.ts create mode 100644 packages/client/lib/commands/CLUSTER_ADDSLOTSRANGE.ts create mode 100644 packages/client/lib/commands/CLUSTER_DELSLOTSRANGE.spec.ts create mode 100644 packages/client/lib/commands/CLUSTER_DELSLOTSRANGE.ts create mode 100644 packages/client/lib/commands/CLUSTER_LINKS.spec.ts create mode 100644 packages/client/lib/commands/CLUSTER_LINKS.ts diff --git a/packages/client/lib/client/commands.ts b/packages/client/lib/client/commands.ts index 0d9f63b003e..acffd4711a9 100644 --- a/packages/client/lib/client/commands.ts +++ b/packages/client/lib/client/commands.ts @@ -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'; @@ -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, diff --git a/packages/client/lib/commands/CLUSTER_ADDSLOTSRANGE.spec.ts b/packages/client/lib/commands/CLUSTER_ADDSLOTSRANGE.spec.ts new file mode 100644 index 00000000000..93e2a985a00 --- /dev/null +++ b/packages/client/lib/commands/CLUSTER_ADDSLOTSRANGE.spec.ts @@ -0,0 +1,20 @@ +import { strict as assert } from 'assert'; +import { transformArguments } from './CLUSTER_ADDSLOTSRANGE'; + +describe('CLUSTER ADDSLOTSRANGE', () => { + describe('transformArguments', () => { + it('single', () => { + assert.deepEqual( + transformArguments([0, 1]), + ['CLUSTER', 'ADDSLOTSRANGE', '0', '1'] + ); + }); + + it('multiple', () => { + assert.deepEqual( + transformArguments([0, 1], [2, 3]), + ['CLUSTER', 'ADDSLOTSRANGE', '0', '1', '2', '3'] + ); + }); + }); +}); diff --git a/packages/client/lib/commands/CLUSTER_ADDSLOTSRANGE.ts b/packages/client/lib/commands/CLUSTER_ADDSLOTSRANGE.ts new file mode 100644 index 00000000000..6cce60cfa72 --- /dev/null +++ b/packages/client/lib/commands/CLUSTER_ADDSLOTSRANGE.ts @@ -0,0 +1,11 @@ +export function transformArguments(...slots: Array<[startSlot: number, endSlot: number]>): Array { + const args = ['CLUSTER', 'DELSLOTSRANGE']; + + for(const [start, end] of slots) { + args.push(start.toString(), end.toString()); + } + + return args; +} + +export declare function transformReply(): 'OK'; diff --git a/packages/client/lib/commands/CLUSTER_DELSLOTSRANGE.spec.ts b/packages/client/lib/commands/CLUSTER_DELSLOTSRANGE.spec.ts new file mode 100644 index 00000000000..2f31e67c5cb --- /dev/null +++ b/packages/client/lib/commands/CLUSTER_DELSLOTSRANGE.spec.ts @@ -0,0 +1,20 @@ +import { strict as assert } from 'assert'; +import { transformArguments } from './CLUSTER_DELSLOTSRANGE'; + +describe('CLUSTER DELSLOTSRANGE', () => { + describe('transformArguments', () => { + it('single', () => { + assert.deepEqual( + transformArguments([0, 1]), + ['CLUSTER', 'DELSLOTSRANGE', '0', '1'] + ); + }); + + it('multiple', () => { + assert.deepEqual( + transformArguments([0, 1], [2, 3]), + ['CLUSTER', 'DELSLOTSRANGE', '0', '1', '2', '3'] + ); + }); + }); +}); diff --git a/packages/client/lib/commands/CLUSTER_DELSLOTSRANGE.ts b/packages/client/lib/commands/CLUSTER_DELSLOTSRANGE.ts new file mode 100644 index 00000000000..4e639208701 --- /dev/null +++ b/packages/client/lib/commands/CLUSTER_DELSLOTSRANGE.ts @@ -0,0 +1,11 @@ +export function transformArguments(...slots: Array<[startSlot: number, endSlot: number]>): Array { + const args = ['CLUSTER', 'ADDSLOTSRANGE']; + + for(const [start, end] of slots) { + args.push(start.toString(), end.toString()); + } + + return args; +} + +export declare function transformReply(): 'OK'; diff --git a/packages/client/lib/commands/CLUSTER_LINKS.spec.ts b/packages/client/lib/commands/CLUSTER_LINKS.spec.ts new file mode 100644 index 00000000000..cd79794b3ef --- /dev/null +++ b/packages/client/lib/commands/CLUSTER_LINKS.spec.ts @@ -0,0 +1,22 @@ +import { strict as assert } from 'assert'; +import testUtils, { GLOBAL } from '../test-utils'; +import { transformArguments } from './CLUSTER_LINKS'; + +describe('CLUSTER LINKS', () => { + it('transformArguments', () => { + assert.deepEqual( + transformArguments(), + ['CLUSTER', 'LINKS'] + ); + }); + + testUtils.isVersionGreaterThanHook([7, 0]); + + testUtils.testWithCluster('clusterNode.clusterSaveConfig', async cluster => { + const links = await cluster.getSlotMaster(0).client.clusterLinks(); + + assert.notEqual(links, null); + assert.equal(typeof links[0].node, 'string'); + + }, GLOBAL.CLUSTERS.OPEN); +}); diff --git a/packages/client/lib/commands/CLUSTER_LINKS.ts b/packages/client/lib/commands/CLUSTER_LINKS.ts new file mode 100644 index 00000000000..06d8de73b86 --- /dev/null +++ b/packages/client/lib/commands/CLUSTER_LINKS.ts @@ -0,0 +1,25 @@ +export function transformArguments(): Array { + return ['CLUSTER', 'LINKS']; +} + +type ClusterLinksReply = Array<{ + direction: string; + node: string; + createTime: number; + events: string; + sendBufferAllocated: number; + sendBufferUsed: number; +}>; + +export function transformReply(reply: Array>): ClusterLinksReply { + return reply.map(peerLink => { + return { + direction: peerLink[1], + node: peerLink[3], + createTime: Number(peerLink[5]), + events: peerLink[7], + sendBufferAllocated: Number(peerLink[9]), + sendBufferUsed: Number(peerLink[11]) + } + }); +} From d3ea59e4a44fa9dbdeb5643558fbe5d3f6922334 Mon Sep 17 00:00:00 2001 From: leibale Date: Thu, 24 Mar 2022 17:35:47 +0200 Subject: [PATCH 2/2] clean code --- .../commands/CLUSTER_ADDSLOTSRANGE.spec.ts | 13 +++++-- .../lib/commands/CLUSTER_ADDSLOTSRANGE.ts | 16 +++++---- .../commands/CLUSTER_DELSLOTSRANGE.spec.ts | 13 +++++-- .../lib/commands/CLUSTER_DELSLOTSRANGE.ts | 16 +++++---- .../client/lib/commands/CLUSTER_LINKS.spec.ts | 19 ++++++---- packages/client/lib/commands/CLUSTER_LINKS.ts | 35 +++++++++++++------ .../lib/commands/generic-transformers.spec.ts | 28 ++++++++++++++- .../lib/commands/generic-transformers.ts | 30 ++++++++++++++++ 8 files changed, 133 insertions(+), 37 deletions(-) diff --git a/packages/client/lib/commands/CLUSTER_ADDSLOTSRANGE.spec.ts b/packages/client/lib/commands/CLUSTER_ADDSLOTSRANGE.spec.ts index 93e2a985a00..ebd1e3445ff 100644 --- a/packages/client/lib/commands/CLUSTER_ADDSLOTSRANGE.spec.ts +++ b/packages/client/lib/commands/CLUSTER_ADDSLOTSRANGE.spec.ts @@ -5,14 +5,23 @@ describe('CLUSTER ADDSLOTSRANGE', () => { describe('transformArguments', () => { it('single', () => { assert.deepEqual( - transformArguments([0, 1]), + transformArguments({ + start: 0, + end: 1 + }), ['CLUSTER', 'ADDSLOTSRANGE', '0', '1'] ); }); it('multiple', () => { assert.deepEqual( - transformArguments([0, 1], [2, 3]), + transformArguments([{ + start: 0, + end: 1 + }, { + start: 2, + end: 3 + }]), ['CLUSTER', 'ADDSLOTSRANGE', '0', '1', '2', '3'] ); }); diff --git a/packages/client/lib/commands/CLUSTER_ADDSLOTSRANGE.ts b/packages/client/lib/commands/CLUSTER_ADDSLOTSRANGE.ts index 6cce60cfa72..6a8d6dc668f 100644 --- a/packages/client/lib/commands/CLUSTER_ADDSLOTSRANGE.ts +++ b/packages/client/lib/commands/CLUSTER_ADDSLOTSRANGE.ts @@ -1,11 +1,13 @@ -export function transformArguments(...slots: Array<[startSlot: number, endSlot: number]>): Array { - const args = ['CLUSTER', 'DELSLOTSRANGE']; +import { RedisCommandArguments } from '.'; +import { pushSlotRangesArguments, SlotRange } from './generic-transformers'; - for(const [start, end] of slots) { - args.push(start.toString(), end.toString()); - } - - return args; +export function transformArguments( + ranges: SlotRange | Array +): RedisCommandArguments { + return pushSlotRangesArguments( + ['CLUSTER', 'ADDSLOTSRANGE'], + ranges + ); } export declare function transformReply(): 'OK'; diff --git a/packages/client/lib/commands/CLUSTER_DELSLOTSRANGE.spec.ts b/packages/client/lib/commands/CLUSTER_DELSLOTSRANGE.spec.ts index 2f31e67c5cb..8fd50d01a54 100644 --- a/packages/client/lib/commands/CLUSTER_DELSLOTSRANGE.spec.ts +++ b/packages/client/lib/commands/CLUSTER_DELSLOTSRANGE.spec.ts @@ -5,14 +5,23 @@ describe('CLUSTER DELSLOTSRANGE', () => { describe('transformArguments', () => { it('single', () => { assert.deepEqual( - transformArguments([0, 1]), + transformArguments({ + start: 0, + end: 1 + }), ['CLUSTER', 'DELSLOTSRANGE', '0', '1'] ); }); it('multiple', () => { assert.deepEqual( - transformArguments([0, 1], [2, 3]), + transformArguments([{ + start: 0, + end: 1 + }, { + start: 2, + end: 3 + }]), ['CLUSTER', 'DELSLOTSRANGE', '0', '1', '2', '3'] ); }); diff --git a/packages/client/lib/commands/CLUSTER_DELSLOTSRANGE.ts b/packages/client/lib/commands/CLUSTER_DELSLOTSRANGE.ts index 4e639208701..b136113c65f 100644 --- a/packages/client/lib/commands/CLUSTER_DELSLOTSRANGE.ts +++ b/packages/client/lib/commands/CLUSTER_DELSLOTSRANGE.ts @@ -1,11 +1,13 @@ -export function transformArguments(...slots: Array<[startSlot: number, endSlot: number]>): Array { - const args = ['CLUSTER', 'ADDSLOTSRANGE']; +import { RedisCommandArguments } from '.'; +import { pushSlotRangesArguments, SlotRange } from './generic-transformers'; - for(const [start, end] of slots) { - args.push(start.toString(), end.toString()); - } - - return args; +export function transformArguments( + ranges: SlotRange | Array +): RedisCommandArguments { + return pushSlotRangesArguments( + ['CLUSTER', 'DELSLOTSRANGE'], + ranges + ); } export declare function transformReply(): 'OK'; diff --git a/packages/client/lib/commands/CLUSTER_LINKS.spec.ts b/packages/client/lib/commands/CLUSTER_LINKS.spec.ts index cd79794b3ef..242e9012fbc 100644 --- a/packages/client/lib/commands/CLUSTER_LINKS.spec.ts +++ b/packages/client/lib/commands/CLUSTER_LINKS.spec.ts @@ -3,6 +3,8 @@ import testUtils, { GLOBAL } from '../test-utils'; import { transformArguments } from './CLUSTER_LINKS'; describe('CLUSTER LINKS', () => { + testUtils.isVersionGreaterThanHook([7, 0]); + it('transformArguments', () => { assert.deepEqual( transformArguments(), @@ -10,13 +12,16 @@ describe('CLUSTER LINKS', () => { ); }); - testUtils.isVersionGreaterThanHook([7, 0]); - - testUtils.testWithCluster('clusterNode.clusterSaveConfig', async cluster => { + testUtils.testWithCluster('clusterNode.clusterLinks', async cluster => { const links = await cluster.getSlotMaster(0).client.clusterLinks(); - - assert.notEqual(links, null); - assert.equal(typeof links[0].node, 'string'); - + 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); }); diff --git a/packages/client/lib/commands/CLUSTER_LINKS.ts b/packages/client/lib/commands/CLUSTER_LINKS.ts index 06d8de73b86..9a5608c102f 100644 --- a/packages/client/lib/commands/CLUSTER_LINKS.ts +++ b/packages/client/lib/commands/CLUSTER_LINKS.ts @@ -2,6 +2,21 @@ export function transformArguments(): Array { 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; @@ -11,15 +26,13 @@ type ClusterLinksReply = Array<{ sendBufferUsed: number; }>; -export function transformReply(reply: Array>): ClusterLinksReply { - return reply.map(peerLink => { - return { - direction: peerLink[1], - node: peerLink[3], - createTime: Number(peerLink[5]), - events: peerLink[7], - sendBufferAllocated: Number(peerLink[9]), - sendBufferUsed: Number(peerLink[11]) - } - }); +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]) + })); } diff --git a/packages/client/lib/commands/generic-transformers.spec.ts b/packages/client/lib/commands/generic-transformers.spec.ts index 96dca31dad5..6e286e284d9 100644 --- a/packages/client/lib/commands/generic-transformers.spec.ts +++ b/packages/client/lib/commands/generic-transformers.spec.ts @@ -23,7 +23,8 @@ import { pushOptionalVerdictArgument, transformCommandReply, CommandFlags, - CommandCategories + CommandCategories, + pushSlotRangesArguments } from './generic-transformers'; describe('Generic Transformers', () => { @@ -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'] + ); + }); + }); }); diff --git a/packages/client/lib/commands/generic-transformers.ts b/packages/client/lib/commands/generic-transformers.ts index 0799476ae64..a7bc6805dde 100644 --- a/packages/client/lib/commands/generic-transformers.ts +++ b/packages/client/lib/commands/generic-transformers.ts @@ -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 +): RedisCommandArguments { + if (Array.isArray(ranges)) { + for (const range of ranges) { + pushSlotRangeArguments(args, range); + } + } else { + pushSlotRangeArguments(args, ranges); + } + + return args; +}