@opensearch-project/opensearch 1.0.2 → 1.1.0

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.d.ts CHANGED
@@ -126,6 +126,10 @@ interface ClientOptions {
126
126
  password?: string;
127
127
  };
128
128
  disablePrototypePoisoningProtection?: boolean | 'proto' | 'constructor';
129
+ memoryCircuitBreaker?: {
130
+ enabled: boolean;
131
+ maxPercentage: number;
132
+ }
129
133
  }
130
134
 
131
135
  declare class Client {
package/index.js CHANGED
@@ -178,7 +178,8 @@ class Client extends OpenSearchAPI {
178
178
  generateRequestId: options.generateRequestId,
179
179
  name: options.name,
180
180
  opaqueIdPrefix: options.opaqueIdPrefix,
181
- context: options.context
181
+ context: options.context,
182
+ memoryCircuitBreaker: options.memoryCircuitBreaker
182
183
  })
183
184
 
184
185
  this.helpers = new Helpers({
package/lib/Helpers.d.ts CHANGED
@@ -115,6 +115,7 @@ export interface OnDropDocument<TDocument = unknown> {
115
115
  reason: string
116
116
  }
117
117
  }
118
+ operation: Action
118
119
  document: TDocument
119
120
  retried: boolean
120
121
  }
package/lib/Transport.js CHANGED
@@ -51,6 +51,7 @@ const clientVersion = require('../package.json').version
51
51
  const userAgent = `opensearch-js/${clientVersion} (${os.platform()} ${os.release()}-${os.arch()}; Node.js ${process.version})`
52
52
  const MAX_BUFFER_LENGTH = buffer.constants.MAX_LENGTH
53
53
  const MAX_STRING_LENGTH = buffer.constants.MAX_STRING_LENGTH
54
+ const HEAP_SIZE_LIMIT = require('v8').getHeapStatistics().heap_size_limit
54
55
  const kCompatibleCheck = Symbol('compatible check')
55
56
  const kApiVersioning = Symbol('api versioning')
56
57
 
@@ -81,6 +82,7 @@ class Transport {
81
82
  this.opaqueIdPrefix = opts.opaqueIdPrefix
82
83
  this[kCompatibleCheck] = 0 // 0 = to be checked, 1 = checking, 2 = checked-ok, 3 checked-notok
83
84
  this[kApiVersioning] = process.env.OPENSEARCH_CLIENT_APIVERSIONING === 'true'
85
+ this.memoryCircuitBreaker = opts.memoryCircuitBreaker
84
86
 
85
87
  this.nodeFilter = opts.nodeFilter || defaultNodeFilter
86
88
  if (typeof opts.nodeSelector === 'function') {
@@ -247,10 +249,10 @@ class Transport {
247
249
 
248
250
  const contentEncoding = (result.headers['content-encoding'] || '').toLowerCase()
249
251
  const isCompressed = contentEncoding.indexOf('gzip') > -1 || contentEncoding.indexOf('deflate') > -1
250
-
251
252
  /* istanbul ignore else */
252
253
  if (result.headers['content-length'] !== undefined) {
253
254
  const contentLength = Number(result.headers['content-length'])
255
+ // nodeJS data type limit check
254
256
  if (isCompressed && contentLength > MAX_BUFFER_LENGTH) {
255
257
  response.destroy()
256
258
  return onConnectionError(
@@ -261,6 +263,12 @@ class Transport {
261
263
  return onConnectionError(
262
264
  new RequestAbortedError(`The content length (${contentLength}) is bigger than the maximum allowed string (${MAX_STRING_LENGTH})`, result)
263
265
  )
266
+ } else if (shouldApplyCircuitBreaker(contentLength)) {
267
+ // Abort this response to avoid OOM crash of dashboards.
268
+ response.destroy()
269
+ return onConnectionError(
270
+ new RequestAbortedError(`The content length (${contentLength}) is bigger than the maximum allowed heap memory limit.`, result)
271
+ )
264
272
  }
265
273
  }
266
274
  // if the response is compressed, we must handle it
@@ -301,6 +309,13 @@ class Transport {
301
309
  response.on('end', onEnd)
302
310
  response.on('aborted', onAbort)
303
311
  }
312
+ // Helper function to check if memory circuit breaker enabled and the response payload is too large to fit into available heap memory.
313
+ const shouldApplyCircuitBreaker = (contentLength) => {
314
+ if (!this.memoryCircuitBreaker || !this.memoryCircuitBreaker.enabled) return false
315
+ const maxPercentage = validateMemoryPercentage(this.memoryCircuitBreaker.maxPercentage)
316
+ const heapUsed = process.memoryUsage().heapUsed
317
+ return contentLength + heapUsed > HEAP_SIZE_LIMIT * maxPercentage
318
+ }
304
319
 
305
320
  const onBody = (err, payload) => {
306
321
  if (err) {
@@ -636,6 +651,11 @@ function lowerCaseHeaders (oldHeaders) {
636
651
  return newHeaders
637
652
  }
638
653
 
654
+ function validateMemoryPercentage (percentage) {
655
+ if (percentage < 0 || percentage > 1) return 1.0
656
+ return percentage
657
+ }
658
+
639
659
  module.exports = Transport
640
660
  module.exports.internals = {
641
661
  defaultNodeFilter,
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "./": "./"
12
12
  },
13
13
  "homepage": "https://www.opensearch.org/",
14
- "version": "1.0.2",
14
+ "version": "1.1.0",
15
15
  "versionCanary": "7.10.0-canary.6",
16
16
  "keywords": [
17
17
  "opensearch",