Skip to content

Commit

Permalink
add node
Browse files Browse the repository at this point in the history
Signed-off-by: Shoham Elias <[email protected]>
  • Loading branch information
shohamazon committed Aug 15, 2024
1 parent ea2b6e7 commit ae4394b
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 56 deletions.
6 changes: 4 additions & 2 deletions node/npm/glide/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ function initialize() {
GlideClientConfiguration,
FunctionListOptions,
FunctionListResponse,
FunctionStatsResponse,
FunctionStatsSingleResponse,
FunctionStatsFullResponse,
SlotIdTypes,
SlotKeyTypes,
TimeUnit,
Expand Down Expand Up @@ -200,7 +201,8 @@ function initialize() {
GlideClientConfiguration,
FunctionListOptions,
FunctionListResponse,
FunctionStatsResponse,
FunctionStatsSingleResponse,
FunctionStatsFullResponse,
SlotIdTypes,
SlotKeyTypes,
StreamEntries,
Expand Down
20 changes: 16 additions & 4 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2220,12 +2220,24 @@ export function createFunctionList(
return createCommand(RequestType.FunctionList, args);
}

/** Type of the response of `FUNCTION STATS` command. */
export type FunctionStatsResponse = Record<
/** Response for `FUNCTION STATS` command on a single node.
* The response is a map with 2 keys:
* 1. Information about the current running function/script (or null if none).
* 2. Details about the execution engines.
*/
export type FunctionStatsSingleResponse = Record<
string,
| null
| Record<string, string | string[] | number>
| Record<string, Record<string, number>>
| Record<string, string | string[] | number> // Running function/script information
| Record<string, Record<string, number>> // Execution engines information
>;

/** Full response for `FUNCTION STATS` command across multiple nodes.
* It maps node addresses to the per-node response.
*/
export type FunctionStatsFullResponse = Record<
string, // Node address
FunctionStatsSingleResponse
>;

/** @internal */
Expand Down
61 changes: 29 additions & 32 deletions node/src/GlideClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
FlushMode,
FunctionListOptions,
FunctionListResponse,
FunctionStatsResponse,
FunctionStatsFullResponse,
InfoOptions,
LolwutOptions,
SortOptions,
Expand Down Expand Up @@ -589,51 +589,48 @@ export class GlideClient extends BaseClient {
* Returns information about the function that's currently running and information about the
* available execution engines.
*
* See https://valkey.io/commands/function-stats/ for details.
* FUNCTION STATS runs on all nodes of the cluster, including primary and replicas.
* The response includes a mapping from node address to the command response for that node.
*
* since Valkey version 7.0.0.
* See https://valkey.io/commands/function-stats/ for more details.
*
* @returns A `Record` with two keys:
* - `"running_script"` with information about the running script.
* - `"engines"` with information about available engines and their stats.
* Since: Valkey version 7.0.0.
*
* See example for more details.
* @returns A Record where the key is the node address and the value is a Record with two keys:
* - `"running_script"`: Information about the running script, or `null` if no script is running.
* - `"engines"`: Information about available engines and their stats.
*
* @example
* ```typescript
* const response = await client.functionStats();
* console.log(response); // Output:
* console.log(response); // Example output:
* // {
* // "running_script":
* // {
* // "name": "deep_thought",
* // "command": ["fcall", "deep_thought", "0"],
* // "duration_ms": 5008
* // },
* // "engines":
* // {
* // "LUA":
* // {
* // "libraries_count": 2,
* // "functions_count": 3
* // "127.0.0.1:6379": { // Response from the primary node
* // "running_script": {
* // "name": "foo",
* // "command": ["FCALL", "foo", "0", "hello"],
* // "duration_ms": 7758
* // },
* // "engines": {
* // "LUA": {
* // "libraries_count": 1,
* // "functions_count": 1
* // }
* // }
* // }
* // }
* // Output if no scripts running:
* // {
* // "running_script": null
* // "engines":
* // {
* // "LUA":
* // {
* // "libraries_count": 2,
* // "functions_count": 3
* // },
* // "127.0.0.1:6380": { // Response from a replica node
* // "running_script": null,
* // "engines": {
* // "LUA": {
* // "libraries_count": 1,
* // "functions_count": 1
* // }
* // }
* // }
* // }
* ```
*/
public async functionStats(): Promise<FunctionStatsResponse> {
public async functionStats(): Promise<FunctionStatsFullResponse> {
return this.createWritePromise(createFunctionStats());
}

Expand Down
9 changes: 4 additions & 5 deletions node/src/GlideClusterClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
FlushMode,
FunctionListOptions,
FunctionListResponse,
FunctionStatsResponse,
InfoOptions,
LolwutOptions,
SortClusterOptions,
Expand All @@ -36,7 +35,6 @@ import {
createFlushDB,
createFunctionDelete,
createFunctionFlush,
createFunctionKill,
createFunctionList,
createFunctionLoad,
createFunctionStats,
Expand Down Expand Up @@ -912,12 +910,13 @@ export class GlideClusterClient extends BaseClient {
* Returns information about the function that's currently running and information about the
* available execution engines.
*
*
* See https://valkey.io/commands/function-stats/ for details.
*
* since Valkey version 7.0.0.
*
* @param route - The client will route the command to the nodes defined by `route`.
* If not defined, the command will be routed to all primary nodes.
* @param route - The command will be routed automatically to all nodes, unless `route` is provided, in which
* case the client will route the command to the nodes defined by `route`.
* @returns A `Record` with two keys:
* - `"running_script"` with information about the running script.
* - `"engines"` with information about available engines and their stats.
Expand Down Expand Up @@ -960,7 +959,7 @@ export class GlideClusterClient extends BaseClient {
*/
public async functionStats(
route?: Routes,
): Promise<ClusterResponse<FunctionStatsResponse>> {
): Promise<ClusterResponse<FunctionStatsSingleResponse>> {
return this.createWritePromise(createFunctionStats(), {
route: toProtobufRoute(route),
});
Expand Down
4 changes: 2 additions & 2 deletions node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
FlushMode,
FunctionListOptions,
FunctionListResponse, // eslint-disable-line @typescript-eslint/no-unused-vars
FunctionStatsResponse, // eslint-disable-line @typescript-eslint/no-unused-vars
FunctionStatsSingleResponse, // eslint-disable-line @typescript-eslint/no-unused-vars
GeoAddOptions,
GeoBoxShape, // eslint-disable-line @typescript-eslint/no-unused-vars
GeoCircleShape, // eslint-disable-line @typescript-eslint/no-unused-vars
Expand Down Expand Up @@ -2949,7 +2949,7 @@ export class BaseTransaction<T extends BaseTransaction<T>> {
*
* since Valkey version 7.0.0.
*
* Command Response - A `Record` of type {@link FunctionStatsResponse} with two keys:
* Command Response - A `Record` of type {@link FunctionStatsSingleResponse} with two keys:
*
* - `"running_script"` with information about the running script.
* - `"engines"` with information about available engines and their stats.
Expand Down
16 changes: 13 additions & 3 deletions node/tests/GlideClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,10 @@ describe("GlideClient", () => {
).toEqual("one");

let functionStats = await client.functionStats();
checkFunctionStatsResponse(functionStats, [], 1, 1);

for (const response of Object.values(functionStats)) {
checkFunctionStatsResponse(response, [], 1, 1);
}

let functionList = await client.functionList({
libNamePattern: libName,
Expand Down Expand Up @@ -733,7 +736,10 @@ describe("GlideClient", () => {
);

functionStats = await client.functionStats();
checkFunctionStatsResponse(functionStats, [], 1, 2);

for (const response of Object.values(functionStats)) {
checkFunctionStatsResponse(response, [], 1, 2);
}

expect(
await client.fcall(func2Name, [], ["one", "two"]),
Expand All @@ -744,7 +750,11 @@ describe("GlideClient", () => {
} finally {
expect(await client.functionFlush()).toEqual("OK");
const functionStats = await client.functionStats();
checkFunctionStatsResponse(functionStats, [], 0, 0);

for (const response of Object.values(functionStats)) {
checkFunctionStatsResponse(response, [], 0, 0);
}

client.close();
}
},
Expand Down
8 changes: 4 additions & 4 deletions node/tests/GlideClusterClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
import { RedisCluster } from "../../utils/TestUtils.js";
import {
FlushMode,
FunctionStatsResponse,
FunctionStatsSingleResponse,
GeoUnit,
SortOrder,
} from "../build-ts/src/Commands";
Expand Down Expand Up @@ -830,7 +830,7 @@ describe("GlideClusterClient", () => {
singleNodeRoute,
(value) =>
checkFunctionStatsResponse(
value as FunctionStatsResponse,
value as FunctionStatsSingleResponse,
[],
0,
0,
Expand Down Expand Up @@ -872,7 +872,7 @@ describe("GlideClusterClient", () => {
singleNodeRoute,
(value) =>
checkFunctionStatsResponse(
value as FunctionStatsResponse,
value as FunctionStatsSingleResponse,
[],
1,
1,
Expand Down Expand Up @@ -963,7 +963,7 @@ describe("GlideClusterClient", () => {
singleNodeRoute,
(value) =>
checkFunctionStatsResponse(
value as FunctionStatsResponse,
value as FunctionStatsSingleResponse,
[],
1,
2,
Expand Down
4 changes: 2 additions & 2 deletions node/tests/TestUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
ClusterTransaction,
FlushMode,
FunctionListResponse,
FunctionStatsResponse,
FunctionStatsSingleResponse,
GeoUnit,
GeospatialData,
GlideClient,
Expand Down Expand Up @@ -458,7 +458,7 @@ export function checkFunctionListResponse(
* @param functionCount - Expected functions count.
*/
export function checkFunctionStatsResponse(
response: FunctionStatsResponse,
response: FunctionStatsSingleResponse,
runningFunction: string[],
libCount: number,
functionCount: number,
Expand Down
4 changes: 2 additions & 2 deletions python/python/glide/async_commands/standalone_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,8 @@ async def function_stats(self) -> TFunctionStatsFullResponse:
}
}
},
b"addr2": {
b'running_script': None, # Response from a replica
b"addr2": { # Response from a replica
b'running_script': None,
b"engines": {
b'LUA': {
b'libraries_count': 1,
Expand Down

0 comments on commit ae4394b

Please sign in to comment.