Skip to content

Commit eeb0d94

Browse files
authored
Put node roles support back (#2759)
1 parent 94a7274 commit eeb0d94

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

docs/basic-config.asciidoc

+12-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,18 @@ a|`function` - Takes a `Connection` and returns `true` if it can be sent a reque
171171
_Default:_
172172
[source,js]
173173
----
174-
() => true
174+
function defaultNodeFilter (conn) {
175+
if (conn.roles != null) {
176+
if (
177+
// avoid master-only nodes
178+
conn.roles.master &&
179+
!conn.roles.data &&
180+
!conn.roles.ingest &&
181+
!conn.roles.ml
182+
) return false
183+
}
184+
return true
185+
}
175186
----
176187

177188
|`nodeSelector`

src/client.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ export interface NodeOptions {
7878
ssl?: TlsConnectionOptions
7979
/** @property headers Custom HTTP headers that should be sent with each request */
8080
headers?: Record<string, any>
81+
/** @property roles Common Elasticsearch roles that can be assigned to this node. Can be helpful when writing custom nodeFilter or nodeSelector functions. */
82+
roles?: {
83+
master: boolean
84+
data: boolean
85+
ingest: boolean
86+
ml: boolean
87+
}
8188
}
8289

8390
export interface ClientOptions {
@@ -135,7 +142,7 @@ export interface ClientOptions {
135142
* @defaultValue null */
136143
agent?: HttpAgentOptions | UndiciAgentOptions | agentFn | false
137144
/** @property nodeFilter A custom function used by the connection pool to determine which nodes are qualified to receive a request
138-
* @defaultValue () => true */
145+
* @defaultValue A function that uses the Connection `roles` property to avoid master-only nodes */
139146
nodeFilter?: nodeFilterFn
140147
/** @property nodeSelector A custom function used by the connection pool to determine which node should receive the next request
141148
* @defaultValue A "round robin" function that loops sequentially through each node in the pool. */

test/unit/client.test.ts

+25
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,31 @@ test('Missing node(s)', t => {
7777
t.end()
7878
})
7979

80+
test('multi nodes with roles, using default node filter', async t => {
81+
const client = new Client({
82+
nodes: [
83+
{
84+
url: new URL('http://node1:9200'),
85+
roles: { master: true, data: false, ingest: false, ml: false }
86+
},
87+
{
88+
url: new URL('http://node2:9200'),
89+
roles: { master: true, data: true, ingest: false, ml: false }
90+
},
91+
]
92+
})
93+
const conn = client.connectionPool.getConnection({
94+
now: Date.now() + 1000 * 60 * 3,
95+
requestId: 1,
96+
name: 'elasticsearch-js',
97+
context: null
98+
})
99+
100+
t.equal(conn?.url.hostname, 'node2')
101+
102+
t.end()
103+
})
104+
80105
test('Custom headers', t => {
81106
const client = new Client({
82107
node: 'http://localhost:9200',

0 commit comments

Comments
 (0)