Skip to content

Commit

Permalink
Node: Add command FLUSHALL (valkey-io#1958)
Browse files Browse the repository at this point in the history
  • Loading branch information
tjzhang-BQ authored Jul 17, 2024
1 parent 6baf4af commit 4ddb11d
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
* Python: Added FUNCTION STATS command ([#1794](https://github.com/valkey-io/valkey-glide/pull/1794))
* Python: Added XINFO STREAM command ([#1816](https://github.com/valkey-io/valkey-glide/pull/1816))
* Python: Added transaction supports for DUMP, RESTORE, FUNCTION DUMP and FUNCTION RESTORE ([#1814](https://github.com/valkey-io/valkey-glide/pull/1814))
* Node: Added FlushAll command ([#1958](https://github.com/valkey-io/valkey-glide/pull/1958))

#### Breaking Changes
* Node: Update XREAD to return a Map of Map ([#1494](https://github.com/valkey-io/valkey-glide/pull/1494))
Expand Down
29 changes: 29 additions & 0 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1649,3 +1649,32 @@ export function createLolwut(options?: LolwutOptions): command_request.Command {

return createCommand(RequestType.Lolwut, args);
}

/**
* Defines flushing mode for:
*
* `FLUSHALL` command.
*
* See https://valkey.io/commands/flushall/ for details.
*/
export enum FlushMode {
/**
* Flushes synchronously.
*
* since Valkey 6.2 and above.
*/
SYNC = "SYNC",
/** Flushes asynchronously. */
ASYNC = "ASYNC",
}

/**
* @internal
*/
export function createFlushAll(mode?: FlushMode): command_request.Command {
if (mode) {
return createCommand(RequestType.FlushAll, [mode.toString()]);
} else {
return createCommand(RequestType.FlushAll, []);
}
}
25 changes: 25 additions & 0 deletions node/src/GlideClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import * as net from "net";
import { BaseClient, BaseClientConfiguration, ReturnType } from "./BaseClient";
import {
FlushMode,
InfoOptions,
LolwutOptions,
createClientGetName,
Expand All @@ -15,6 +16,7 @@ import {
createConfigSet,
createCustomCommand,
createEcho,
createFlushAll,
createInfo,
createLolwut,
createPing,
Expand Down Expand Up @@ -330,4 +332,27 @@ export class GlideClient extends BaseClient {
public lolwut(options?: LolwutOptions): Promise<string> {
return this.createWritePromise(createLolwut(options));
}

/**
* Deletes all the keys of all the existing databases. This command never fails.
* The command will be routed to all primary nodes.
*
* See https://valkey.io/commands/flushall/ for more details.
*
* @param mode - The flushing mode, could be either {@link FlushMode.SYNC} or {@link FlushMode.ASYNC}.
* @returns `OK`.
*
* @example
* ```typescript
* const result = await client.flushall(FlushMode.SYNC);
* console.log(result); // Output: 'OK'
* ```
*/
public flushall(mode?: FlushMode): Promise<string> {
if (mode) {
return this.createWritePromise(createFlushAll(mode));
} else {
return this.createWritePromise(createFlushAll());
}
}
}
25 changes: 25 additions & 0 deletions node/src/GlideClusterClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import * as net from "net";
import { BaseClient, BaseClientConfiguration, ReturnType } from "./BaseClient";
import {
FlushMode,
InfoOptions,
LolwutOptions,
createClientGetName,
Expand All @@ -15,6 +16,7 @@ import {
createConfigSet,
createCustomCommand,
createEcho,
createFlushAll,
createInfo,
createLolwut,
createPing,
Expand Down Expand Up @@ -595,4 +597,27 @@ export class GlideClusterClient extends BaseClient {
toProtobufRoute(route),
);
}

/**
* Deletes all the keys of all the existing databases. This command never fails.
*
* See https://valkey.io/commands/flushall/ for more details.
*
* @param mode - The flushing mode, could be either {@link FlushMode.SYNC} or {@link FlushMode.ASYNC}.
* @param route - The command will be routed to all primaries, unless `route` is provided, in which
* case the client will route the command to the nodes defined by `route`.
* @returns `OK`.
*
* @example
* ```typescript
* const result = await client.flushall(FlushMode.SYNC);
* console.log(result); // Output: 'OK'
* ```
*/
public flushall(mode?: FlushMode, route?: Routes): Promise<string> {
return this.createWritePromise(
createFlushAll(mode),
toProtobufRoute(route),
);
}
}
14 changes: 14 additions & 0 deletions node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import {
AggregationType,
ExpireOptions,
FlushMode,
InfoOptions,
InsertPosition,
KeyWeight,
Expand Down Expand Up @@ -34,6 +35,7 @@ import {
createExists,
createExpire,
createExpireAt,
createFlushAll,
createGet,
createHDel,
createHExists,
Expand Down Expand Up @@ -1681,6 +1683,18 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
public lolwut(options?: LolwutOptions): T {
return this.addAndReturn(createLolwut(options));
}

/**
* Deletes all the keys of all the existing databases. This command never fails.
*
* See https://valkey.io/commands/flushall/ for more details.
*
* @param mode - The flushing mode, could be either {@link FlushMode.SYNC} or {@link FlushMode.ASYNC}.
* Command Response - `OK`.
*/
public flushall(mode?: FlushMode): T {
return this.addAndReturn(createFlushAll(mode));
}
}

/**
Expand Down
3 changes: 2 additions & 1 deletion node/tests/RedisClusterClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ describe("GlideClusterClient", () => {
? RedisCluster.initFromExistingCluster(
parseEndpoints(clusterAddresses),
)
: await RedisCluster.createCluster(true, 3, 0);
: // setting replicaCount to 1 to facilitate tests routed to replicas
await RedisCluster.createCluster(true, 3, 1);
}, 20000);

afterEach(async () => {
Expand Down
48 changes: 48 additions & 0 deletions node/tests/SharedTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { v4 as uuidv4 } from "uuid";
import {
ClosingError,
ExpireOptions,
FlushMode,
GlideClient,
GlideClusterClient,
InfoOptions,
Expand All @@ -26,6 +27,7 @@ import {
intoArray,
intoString,
} from "./TestUtilities";
import { SingleNodeRoute } from "../build-ts/src/GlideClusterClient";

async function getVersion(): Promise<[number, number, number]> {
const versionString = await new Promise<string>((resolve, reject) => {
Expand Down Expand Up @@ -3815,6 +3817,52 @@ export function runBaseTests<Context>(config: {
},
config.timeout,
);

it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
`flushall test_%p`,
async (protocol) => {
await runTest(async (client: BaseClient) => {
// Test FLUSHALL SYNC
expect(await client.flushall(FlushMode.SYNC)).toBe("OK");

// TODO: replace with KEYS command when implemented
const keysAfter = (await client.customCommand([
"keys",
"*",
])) as string[];
expect(keysAfter.length).toBe(0);

// Test various FLUSHALL calls
expect(await client.flushall()).toBe("OK");
expect(await client.flushall(FlushMode.ASYNC)).toBe("OK");

if (client instanceof GlideClusterClient) {
const key = uuidv4();
const primaryRoute: SingleNodeRoute = {
type: "primarySlotKey",
key: key,
};
expect(await client.flushall(undefined, primaryRoute)).toBe(
"OK",
);
expect(
await client.flushall(FlushMode.ASYNC, primaryRoute),
).toBe("OK");

//Test FLUSHALL on replica (should fail)
const key2 = uuidv4();
const replicaRoute: SingleNodeRoute = {
type: "replicaSlotKey",
key: key2,
};
await expect(
client.flushall(undefined, replicaRoute),
).rejects.toThrowError();
}
}, protocol);
},
config.timeout,
);
}

export function runCommonTests<Context>(config: {
Expand Down
2 changes: 2 additions & 0 deletions node/tests/TestUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,8 @@ export async function transactionTest(
const field = uuidv4();
const value = uuidv4();
const args: ReturnType[] = [];
baseTransaction.flushall();
args.push("OK");
baseTransaction.set(key1, "bar");
args.push("OK");
baseTransaction.objectEncoding(key1);
Expand Down

0 comments on commit 4ddb11d

Please sign in to comment.