Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* Node: Added LOLWUT command ([#1934](https://github.com/valkey-io/valkey-glide/pull/1934))
* Node: Added LPOS command ([#1927](https://github.com/valkey-io/valkey-glide/pull/1927))
* Node: Added FUNCTION LOAD command ([#1969](https://github.com/valkey-io/valkey-glide/pull/1969))
* Node: Added FUNCTION FLUSH command ([#1984](https://github.com/valkey-io/valkey-glide/pull/1984))

## 1.0.0 (2024-07-09)

Expand Down
11 changes: 11 additions & 0 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1537,6 +1537,17 @@ export function createFunctionLoad(
return createCommand(RequestType.FunctionLoad, args);
}

/**
* @internal
*/
export function createFunctionFlush(mode?: FlushMode): command_request.Command {
if (mode) {
return createCommand(RequestType.FunctionFlush, [mode.toString()]);
} else {
return createCommand(RequestType.FunctionFlush, []);
}
}

export type StreamReadOptions = {
/**
* If set, the read request will block for the set amount of milliseconds or
Expand Down
27 changes: 26 additions & 1 deletion node/src/GlideClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ import {
createCustomCommand,
createDBSize,
createEcho,
createFunctionLoad,
createFlushAll,
createFunctionFlush,
createFunctionLoad,
createInfo,
createLolwut,
createPing,
Expand Down Expand Up @@ -414,6 +415,30 @@ export class GlideClient extends BaseClient {
);
}

/**
* Deletes all function libraries.
*
* See https://valkey.io/commands/function-flush/ for details.
*
* since Valkey version 7.0.0.
*
* @param mode - The flushing mode, could be either {@link FlushMode.SYNC} or {@link FlushMode.ASYNC}.
* @returns A simple OK response.
*
* @example
* ```typescript
* const result = await client.functionFlush(FlushMode.SYNC);
* console.log(result); // Output: 'OK'
* ```
*/
public functionFlush(mode?: FlushMode): Promise<string> {
if (mode) {
return this.createWritePromise(createFunctionFlush(mode));
} else {
return this.createWritePromise(createFunctionFlush());
}
Comment thread
yipin-chen marked this conversation as resolved.
Outdated
}

/**
* Deletes all the keys of all the existing databases. This command never fails.
* The command will be routed to all primary nodes.
Expand Down
35 changes: 34 additions & 1 deletion node/src/GlideClusterClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ import {
createCustomCommand,
createDBSize,
createEcho,
createFunctionLoad,
createFlushAll,
createFunctionFlush,
createFunctionLoad,
createInfo,
createLolwut,
createPing,
Expand Down Expand Up @@ -688,6 +689,38 @@ export class GlideClusterClient extends BaseClient {
);
}

/**
* Deletes all function libraries.
*
* See https://valkey.io/commands/function-flush/ for details.
*
* since Valkey version 7.0.0.
*
* @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
Comment thread
yipin-chen marked this conversation as resolved.
Outdated
* case the client will route the command to the nodes defined by `route`.
* @returns A simple OK response.
*
* @example
* ```typescript
* const result = await client.functionFlush(FlushMode.SYNC);
* console.log(result); // Output: 'OK'
* ```
*/
public functionFlush(mode?: FlushMode, route?: Routes): Promise<string> {
if (mode) {
return this.createWritePromise(
createFunctionFlush(mode),
toProtobufRoute(route),
);
} else {
return this.createWritePromise(
createFunctionFlush(),
toProtobufRoute(route),
);
}
Comment thread
yipin-chen marked this conversation as resolved.
Outdated
}

/**
* Deletes all the keys of all the existing databases. This command never fails.
*
Expand Down
19 changes: 17 additions & 2 deletions node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ import {
createExpire,
createExpireAt,
createFlushAll,
createFunctionFlush,
createFunctionLoad,
createGet,
createGetDel,
createHDel,
Expand Down Expand Up @@ -93,8 +95,8 @@ import {
createSInterCard,
createSInterStore,
createSIsMember,
createSMembers,
createSMIsMember,
createSMembers,
createSMove,
createSPop,
createSRem,
Expand Down Expand Up @@ -127,7 +129,6 @@ import {
createZRemRangeByRank,
createZRemRangeByScore,
createZScore,
createFunctionLoad,
} from "./Commands";
import { command_request } from "./ProtobufMessage";

Expand Down Expand Up @@ -1755,6 +1756,20 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
return this.addAndReturn(createFunctionLoad(libraryCode, replace));
}

/**
* Deletes all function libraries.
*
* See https://valkey.io/commands/function-flush/ for details.
*
* since Valkey version 7.0.0.
*
* @param mode - The flushing mode, could be either {@link FlushMode.SYNC} or {@link FlushMode.ASYNC}.
* Command Response - `OK`.
*/
public functionFlush(mode?: FlushMode): T {
return this.addAndReturn(createFunctionFlush(mode));
}

/**
* Deletes all the keys of all the existing databases. This command never fails.
*
Expand Down
60 changes: 60 additions & 0 deletions node/tests/RedisClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import { BufferReader, BufferWriter } from "protobufjs";
import { v4 as uuidv4 } from "uuid";
import {
FlushMode,
GlideClient,
GlideClientConfiguration,
ProtocolVersion,
Expand Down Expand Up @@ -464,6 +465,65 @@ describe("GlideClient", () => {
},
);

it.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
Comment thread
yipin-chen marked this conversation as resolved.
"function flush test_%p",
async (protocol) => {
if (await checkIfServerVersionLessThan("7.0.0")) return;

const client = await GlideClient.createClient(
getClientConfigurationOption(cluster.getAddresses(), protocol),
);

try {
const libName = "mylib1C" + uuidv4().replaceAll("-", "");
const funcName = "myfunc1c" + uuidv4().replaceAll("-", "");
const code = generateLuaLibCode(
libName,
new Map([[funcName, "return args[1]"]]),
true,
);

// TODO use commands instead of customCommand once implemented
// verify function does not yet exist
expect(
await client.customCommand([
"FUNCTION",
"LIST",
"LIBRARYNAME",
libName,
]),
).toEqual([]);

checkSimple(await client.functionLoad(code)).toEqual(libName);

// Flush functions
expect(await client.functionFlush(FlushMode.SYNC)).toEqual(
"OK",
);
expect(await client.functionFlush(FlushMode.ASYNC)).toEqual(
"OK",
);

// TODO use commands instead of customCommand once implemented
// verify function does not yet exist
expect(
await client.customCommand([
"FUNCTION",
"LIST",
"LIBRARYNAME",
libName,
]),
).toEqual([]);

// Attempt to re-load library without overwriting to ensure FLUSH was effective
checkSimple(await client.functionLoad(code)).toEqual(libName);
} finally {
expect(await client.functionFlush()).toEqual("OK");
client.close();
}
},
);

it.each([ProtocolVersion.RESP3])("simple pubsub test", async (protocol) => {
const pattern = "*";
const channel = "test-channel";
Expand Down
110 changes: 110 additions & 0 deletions node/tests/RedisClusterClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { v4 as uuidv4 } from "uuid";
import {
ClusterClientConfiguration,
ClusterTransaction,
FlushMode,
GlideClusterClient,
InfoOptions,
ProtocolVersion,
Expand Down Expand Up @@ -661,6 +662,115 @@ describe("GlideClusterClient", () => {
},
);

describe.each([ProtocolVersion.RESP2, ProtocolVersion.RESP3])(
Comment thread
yipin-chen marked this conversation as resolved.
"Protocol is RESP2 = %s",
(protocol) => {
describe.each([true, false])(
"Single node route = %s",
(singleNodeRoute) => {
it(
"function flush",
async () => {
if (await checkIfServerVersionLessThan("7.0.0"))
return;

const client =
await GlideClusterClient.createClient(
getClientConfigurationOption(
cluster.getAddresses(),
protocol,
),
);

try {
const libName =
"mylib1C" + uuidv4().replaceAll("-", "");
const funcName =
"myfunc1c" + uuidv4().replaceAll("-", "");
const code = generateLuaLibCode(
libName,
new Map([[funcName, "return args[1]"]]),
true,
);
const route: Routes = singleNodeRoute
? { type: "primarySlotKey", key: "1" }
: "allPrimaries";

// TODO use commands instead of customCommand once implemented
// verify function does not yet exist
const functionList1 =
await client.customCommand([
"FUNCTION",
"LIST",
"LIBRARYNAME",
libName,
]);
checkClusterResponse(
functionList1 as object,
singleNodeRoute,
(value) => expect(value).toEqual([]),
);

// load the library
checkSimple(
await client.functionLoad(
code,
undefined,
route,
),
).toEqual(libName);

// flush functions
expect(
await client.functionFlush(
FlushMode.SYNC,
route,
),
).toEqual("OK");
expect(
await client.functionFlush(
FlushMode.ASYNC,
route,
),
).toEqual("OK");

// TODO use commands instead of customCommand once implemented
// verify function does not exist
const functionList2 =
await client.customCommand([
"FUNCTION",
"LIST",
"LIBRARYNAME",
libName,
]);
checkClusterResponse(
functionList2 as object,
singleNodeRoute,
(value) => expect(value).toEqual([]),
);

// Attempt to re-load library without overwriting to ensure FLUSH was effective
checkSimple(
await client.functionLoad(
code,
undefined,
route,
),
).toEqual(libName);
} finally {
expect(await client.functionFlush()).toEqual(
"OK",
);
client.close();
}
},
TIMEOUT,
);
},
);
},
);

it.each([
[true, ProtocolVersion.RESP3],
[false, ProtocolVersion.RESP3],
Expand Down
9 changes: 8 additions & 1 deletion node/tests/TestUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
BaseClient,
BaseClientConfiguration,
ClusterTransaction,
FlushMode,
GlideClient,
GlideClusterClient,
InsertPosition,
Expand All @@ -18,8 +19,8 @@ import {
ReturnType,
Transaction,
} from "..";
import { checkIfServerVersionLessThan } from "./SharedTests";
import { LPosOptions } from "../build-ts/src/command-options/LPosOptions";
import { checkIfServerVersionLessThan } from "./SharedTests";

beforeAll(() => {
Logger.init("info");
Expand Down Expand Up @@ -622,6 +623,12 @@ export async function transactionTest(
args.push(libName);
baseTransaction.functionLoad(code, true);
args.push(libName);
baseTransaction.functionFlush();
args.push("OK");
baseTransaction.functionFlush(FlushMode.ASYNC);
args.push("OK");
baseTransaction.functionFlush(FlushMode.SYNC);
args.push("OK");
}

return args;
Expand Down