diff --git a/.worktrees/backport-2.x b/.worktrees/backport-2.x new file mode 160000 index 000000000..3b93dec2d --- /dev/null +++ b/.worktrees/backport-2.x @@ -0,0 +1 @@ +Subproject commit 3b93dec2dc5e995398fb45f5466bf96fc0bd0c7d diff --git a/CHANGELOG.md b/CHANGELOG.md index 311cc5a64..a9336fb58 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -45,6 +45,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) - Added missing types for AwsSigv4SignerOptions.service ([#377](https://github.com/opensearch-project/opensearch-js/pull/377)) - Fixed deprecated folder mapping "./" in the "exports" field module resolution ([#416](https://github.com/opensearch-project/opensearch-js/pull/416)) +- Fixed `BaseConnectionPool` markAlive and markDead and their tests ([#449](https://github.com/opensearch-project/opensearch-js/pull/449)) ### Security diff --git a/USER_GUIDE.md b/USER_GUIDE.md index 579c170e3..91aa047a2 100644 --- a/USER_GUIDE.md +++ b/USER_GUIDE.md @@ -249,4 +249,74 @@ console.log('Deleting all PITs:'); var response = await client.deleteAllPits(); console.log(response.body); +``` + +## Mark connection as `dead` +```javascript +console.log('Marking connection as dead:'); + +var connection = client.connectionPool.getConnection() +// or var connection = client.connectionPool.connections[0] + +if (connection !== null) { + client.connectionPool.markDead(connection) +} +``` + +## Mark connection as `alive` +```javascript +console.log('Marking connection as alive:'); + +var connection = client.connectionPool.getConnection() +// or var connection = client.connectionPool.connections[0] + +if (connection !== null) { + client.connectionPool.markAlive(connection) +} +``` +## Mark connection as `dead` +```javascript +console.log('Marking connection as dead:'); + +var connection = client.connectionPool.getConnection() +// or var connection = client.connectionPool.connections[0] + +if (connection !== null) { + client.connectionPool.markDead(connection) +} +``` + +## Mark connection as `alive` +```javascript +console.log('Marking connection as alive:'); + +var connection = client.connectionPool.getConnection() +// or var connection = client.connectionPool.connections[0] + +if (connection !== null) { + client.connectionPool.markAlive(connection) +} +``` +## Mark connection as `dead` +```javascript +console.log('Marking connection as dead:'); + +var connection = client.connectionPool.getConnection() +// or var connection = client.connectionPool.connections[0] + +if (connection !== null) { + client.connectionPool.markDead(connection) +} +``` + +## Mark connection as `alive` +```javascript +console.log('Marking connection as alive:'); + +var connection = client.connectionPool.getConnection() +// or var connection = client.connectionPool.connections[0] + +if (connection !== null) { + client.connectionPool.markAlive(connection) +} ``` \ No newline at end of file diff --git a/lib/pool/BaseConnectionPool.js b/lib/pool/BaseConnectionPool.js index c2a901f64..29dab490f 100644 --- a/lib/pool/BaseConnectionPool.js +++ b/lib/pool/BaseConnectionPool.js @@ -52,11 +52,13 @@ class BaseConnectionPool { throw new Error('getConnection must be implemented'); } - markAlive() { + markAlive(connection) { + connection.status = Connection.statuses.ALIVE; return this; } - markDead() { + markDead(connection) { + connection.status = Connection.statuses.DEAD; return this; } diff --git a/test/unit/base-connection-pool.test.js b/test/unit/base-connection-pool.test.js index 8af26a393..059dfca3d 100644 --- a/test/unit/base-connection-pool.test.js +++ b/test/unit/base-connection-pool.test.js @@ -76,7 +76,18 @@ test('API', (t) => { let connection = pool.addConnection(href); t.same(pool.markDead(connection), pool); connection = pool.connections.find((c) => c.id === href); - t.equal(connection.status, Connection.statuses.ALIVE); + t.equal(connection.status, Connection.statuses.DEAD); + t.end(); + }); + + t.test('call markDead twice', (t) => { + const pool = new BaseConnectionPool({ Connection, sniffEnabled: true }); + const href = 'http://localhost:9200/'; + let connection = pool.addConnection(href); + t.same(pool.markDead(connection), pool); + t.same(pool.markDead(connection), pool); + connection = pool.connections.find((c) => c.id === href); + t.equal(connection.status, Connection.statuses.DEAD); t.end(); }); @@ -90,6 +101,31 @@ test('API', (t) => { t.end(); }); + t.test('call markAlive twice', (t) => { + const pool = new BaseConnectionPool({ Connection, sniffEnabled: true }); + const href = 'http://localhost:9200/'; + let connection = pool.addConnection(href); + t.same(pool.markAlive(connection), pool); + t.same(pool.markAlive(connection), pool); + connection = pool.connections.find((c) => c.id === href); + t.equal(connection.status, Connection.statuses.ALIVE); + t.end(); + }); + + t.test('call markDead and markAlive', (t) => { + const pool = new BaseConnectionPool({ Connection, sniffEnabled: true }); + const href = 'http://localhost:9200/'; + let connection = pool.addConnection(href); + t.same(pool.markDead(connection), pool); + t.equal(connection.status, Connection.statuses.DEAD); + t.same(pool.markAlive(connection), pool); + t.equal(connection.status, Connection.statuses.ALIVE); + connection = pool.connections.find((c) => c.id === href); + t.equal(connection.status, Connection.statuses.ALIVE); + t.not(connection.status, Connection.statuses.DEAD); + t.end(); + }); + t.test('getConnection should throw', (t) => { const pool = new BaseConnectionPool({ Connection }); const href = 'http://localhost:9200/';