@terascope/elasticsearch-api 3.3.2 → 3.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -792,19 +792,33 @@ module.exports = function elasticsearchApi(client, logger, _opConfig) {
792
792
  });
793
793
  }
794
794
 
795
+ function isConnectionErrorMessage(err) {
796
+ const msg = get(err, 'message', '');
797
+ return msg.includes('No Living connections') || msg.includes('ECONNREFUSED');
798
+ }
799
+
800
+ function isErrorRetryable(err) {
801
+ const checkErrorMsg = isRetryableError(err);
802
+
803
+ if (checkErrorMsg) {
804
+ return true;
805
+ }
806
+
807
+ const isRejectedError = get(err, 'body.error.type') === 'es_rejected_execution_exception';
808
+ const isConnectionError = isConnectionErrorMessage(err);
809
+
810
+ if (isRejectedError || isConnectionError) {
811
+ return true;
812
+ }
813
+
814
+ return false;
815
+ }
816
+
795
817
  function _errorHandler(fn, data, reject, fnName = '->unknown()') {
796
818
  const retry = _retryFn(fn, data, reject);
819
+
797
820
  return function _errorHandlerFn(err) {
798
- let retryable = false;
799
- if (isRetryableError(err)) {
800
- retryable = true;
801
- } else {
802
- const isRejectedError = get(err, 'body.error.type') === 'es_rejected_execution_exception';
803
- const isConnectionError = get(err, 'message', '').includes('No Living connections');
804
- if (isRejectedError || isConnectionError) {
805
- retryable = true;
806
- }
807
- }
821
+ const retryable = isErrorRetryable(err);
808
822
 
809
823
  if (retryable) {
810
824
  retry();
@@ -1254,5 +1268,6 @@ module.exports = function elasticsearchApi(client, logger, _opConfig) {
1254
1268
  index_refresh: indexRefresh,
1255
1269
  index_recovery: indexRecovery,
1256
1270
  getESVersion,
1271
+ isErrorRetryable
1257
1272
  };
1258
1273
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@terascope/elasticsearch-api",
3
3
  "displayName": "Elasticsearch API",
4
- "version": "3.3.2",
4
+ "version": "3.3.3",
5
5
  "description": "Elasticsearch client api used across multiple services, handles retries and exponential backoff",
6
6
  "homepage": "https://github.com/terascope/teraslice/tree/master/packages/elasticsearch-api#readme",
7
7
  "bugs": {
@@ -31,7 +31,7 @@
31
31
  "@opensearch-project/opensearch": "^1.1.0",
32
32
  "@types/elasticsearch": "^5.0.40",
33
33
  "elasticsearch": "^15.4.1",
34
- "elasticsearch-store": "^0.64.0",
34
+ "elasticsearch-store": "^0.65.1",
35
35
  "elasticsearch6": "npm:@elastic/elasticsearch@^6.7.0",
36
36
  "elasticsearch7": "npm:@elastic/elasticsearch@^7.0.0",
37
37
  "elasticsearch8": "npm:@elastic/elasticsearch@^8.0.0"
@@ -0,0 +1,38 @@
1
+ 'use strict';
2
+
3
+ const { ElasticsearchTestHelpers } = require('elasticsearch-store');
4
+ const { debugLogger } = require('@terascope/utils');
5
+ const opensearch = require('@opensearch-project/opensearch');
6
+ const API = require('../index');
7
+
8
+ const logger = debugLogger('retry spec');
9
+ const port = '1111';
10
+
11
+ const { data } = ElasticsearchTestHelpers.EvenDateData;
12
+
13
+ const body = ElasticsearchTestHelpers.formatUploadData('retry-error-test', data);
14
+
15
+ describe('retry behavior', () => {
16
+ it('will work with opensearch when connection cannot be established', async () => {
17
+ expect.assertions(4);
18
+ const client = new opensearch.Client({ node: `http://127.0.0.1:${port}` });
19
+ const { isErrorRetryable } = API(client, logger);
20
+
21
+ try {
22
+ await client.bulk({ body });
23
+ throw Error('should have thrown');
24
+ } catch (err) {
25
+ expect(err).toBeDefined();
26
+ expect(isErrorRetryable(err)).toBeTrue();
27
+ }
28
+
29
+ // we try again as we have seen the second call be marked as unretryable
30
+ try {
31
+ await client.bulk({ body });
32
+ throw Error('should have thrown');
33
+ } catch (err) {
34
+ expect(err).toBeDefined();
35
+ expect(isErrorRetryable(err)).toBeTrue();
36
+ }
37
+ });
38
+ });