From c94e1fcbaaa71d78f47f62f6294f15ce4d29d553 Mon Sep 17 00:00:00 2001 From: Josh Mock Date: Wed, 16 Apr 2025 13:02:13 -0500 Subject: [PATCH 1/3] Add test to verify default node filter function --- src/client.ts | 2 +- test/unit/client.test.ts | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/client.ts b/src/client.ts index f4cedef16..f758553e2 100644 --- a/src/client.ts +++ b/src/client.ts @@ -132,7 +132,7 @@ export interface ClientOptions { * @defaultValue null */ agent?: HttpAgentOptions | UndiciAgentOptions | agentFn | false /** @property nodeFilter A custom function used by the connection pool to determine which nodes are qualified to receive a request - * @defaultValue () => true */ + * @defaultValue A function that uses the Connection `roles` property to avoid master-only nodes */ nodeFilter?: nodeFilterFn /** @property nodeSelector A custom function used by the connection pool to determine which node should receive the next request * @defaultValue A "round robin" function that loops sequentially through each node in the pool. */ diff --git a/test/unit/client.test.ts b/test/unit/client.test.ts index 7c4aa3339..fc4016a22 100644 --- a/test/unit/client.test.ts +++ b/test/unit/client.test.ts @@ -64,6 +64,31 @@ test('Missing node(s)', t => { t.end() }) +test('multi nodes with roles, using default node filter', async t => { + const client = new Client({ + nodes: [ + { + url: new URL('http://node1:9200'), + roles: { master: true, data: false, ingest: false, ml: false } + }, + { + url: new URL('http://node2:9200'), + roles: { master: true, data: true, ingest: false, ml: false } + }, + ] + }) + const conn = client.connectionPool.getConnection({ + now: Date.now() + 1000 * 60 * 3, + requestId: 1, + name: 'elasticsearch-js', + context: null + }) + + t.equal(conn?.url.hostname, 'node2') + + t.end() +}) + test('Custom headers', t => { const client = new Client({ node: 'http://localhost:9200', From 570d3ecb7a8d0cefdb1a6721aed4ed9e857ff3dd Mon Sep 17 00:00:00 2001 From: Josh Mock Date: Wed, 16 Apr 2025 13:05:18 -0500 Subject: [PATCH 2/3] Update docs to reflect new default --- docs/reference/basic-config.md | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/docs/reference/basic-config.md b/docs/reference/basic-config.md index 7b523cbeb..82a537260 100644 --- a/docs/reference/basic-config.md +++ b/docs/reference/basic-config.md @@ -246,12 +246,15 @@ Type: `function` Filter that indicates whether a node should be used for a request. Default function definition: ```js -function defaultNodeFilter (node) { - // avoid master only nodes - if (node.roles.master === true && - node.roles.data === false && - node.roles.ingest === false) { - return false +function defaultNodeFilter (conn) { + if (conn.roles != null) { + if ( + // avoid master-only nodes + conn.roles.master && + !conn.roles.data && + !conn.roles.ingest && + !conn.roles.ml + ) return false } return true } From a555872db165262266f99635e3237d7b6b6ee3d1 Mon Sep 17 00:00:00 2001 From: Josh Mock Date: Wed, 16 Apr 2025 13:23:28 -0500 Subject: [PATCH 3/3] Bump transport --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 4754d0d18..1c52966c3 100644 --- a/package.json +++ b/package.json @@ -90,7 +90,7 @@ "zx": "7.2.3" }, "dependencies": { - "@elastic/transport": "^9.0.0", + "@elastic/transport": "^9.0.1", "apache-arrow": "^18.0.0", "tslib": "^2.4.0" },