@opensearch-project/opensearch 1.0.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/.dockerignore +5 -0
- package/ADMINS.md +34 -0
- package/DEVELOPER_GUIDE.md +60 -0
- package/LICENSE.txt +201 -0
- package/MAINTAINERS.md +73 -0
- package/Makefile +27 -0
- package/NOTICE.txt +8 -0
- package/README.md +183 -0
- package/RELEASING.md +34 -0
- package/SECURITY.md +3 -0
- package/api/api/bulk.js +81 -0
- package/api/api/cat.js +525 -0
- package/api/api/clear_scroll.js +66 -0
- package/api/api/cluster.js +431 -0
- package/api/api/count.js +75 -0
- package/api/api/create.js +80 -0
- package/api/api/dangling_indices.js +126 -0
- package/api/api/delete.js +76 -0
- package/api/api/delete_by_query.js +82 -0
- package/api/api/delete_by_query_rethrottle.js +71 -0
- package/api/api/delete_script.js +67 -0
- package/api/api/exists.js +76 -0
- package/api/api/exists_source.js +85 -0
- package/api/api/explain.js +76 -0
- package/api/api/features.js +92 -0
- package/api/api/field_caps.js +66 -0
- package/api/api/get.js +76 -0
- package/api/api/get_script.js +67 -0
- package/api/api/get_script_context.js +61 -0
- package/api/api/get_script_languages.js +61 -0
- package/api/api/get_source.js +76 -0
- package/api/api/index.js +82 -0
- package/api/api/indices.js +1516 -0
- package/api/api/info.js +61 -0
- package/api/api/ingest.js +211 -0
- package/api/api/mget.js +81 -0
- package/api/api/msearch.js +81 -0
- package/api/api/msearch_template.js +81 -0
- package/api/api/mtermvectors.js +75 -0
- package/api/api/nodes.js +279 -0
- package/api/api/ping.js +61 -0
- package/api/api/put_script.js +82 -0
- package/api/api/rank_eval.js +72 -0
- package/api/api/reindex.js +67 -0
- package/api/api/reindex_rethrottle.js +71 -0
- package/api/api/render_search_template.js +66 -0
- package/api/api/scripts_painless_execute.js +61 -0
- package/api/api/scroll.js +66 -0
- package/api/api/search.js +75 -0
- package/api/api/search_shards.js +66 -0
- package/api/api/search_template.js +81 -0
- package/api/api/shutdown.js +135 -0
- package/api/api/snapshot.js +450 -0
- package/api/api/tasks.js +119 -0
- package/api/api/terms_enum.js +67 -0
- package/api/api/termvectors.js +78 -0
- package/api/api/update.js +80 -0
- package/api/api/update_by_query.js +78 -0
- package/api/api/update_by_query_rethrottle.js +71 -0
- package/api/index.js +254 -0
- package/api/new.d.ts +716 -0
- package/api/requestParams.d.ts +1583 -0
- package/api/types.d.ts +9481 -0
- package/api/utils.js +69 -0
- package/codecov.yml +14 -0
- package/index.d.ts +1210 -0
- package/index.js +338 -0
- package/index.mjs +40 -0
- package/lib/Connection.d.ts +107 -0
- package/lib/Connection.js +345 -0
- package/lib/Helpers.d.ts +135 -0
- package/lib/Helpers.js +779 -0
- package/lib/Serializer.d.ts +41 -0
- package/lib/Serializer.js +105 -0
- package/lib/Transport.d.ts +173 -0
- package/lib/Transport.js +646 -0
- package/lib/errors.d.ts +101 -0
- package/lib/errors.js +168 -0
- package/lib/pool/BaseConnectionPool.js +269 -0
- package/lib/pool/CloudConnectionPool.js +75 -0
- package/lib/pool/ConnectionPool.js +257 -0
- package/lib/pool/index.d.ts +215 -0
- package/lib/pool/index.js +41 -0
- package/package.json +102 -0
package/lib/errors.d.ts
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* The OpenSearch Contributors require contributions made to
|
|
5
|
+
* this file be licensed under the Apache-2.0 license or a
|
|
6
|
+
* compatible open source license.
|
|
7
|
+
*
|
|
8
|
+
* Modifications Copyright OpenSearch Contributors. See
|
|
9
|
+
* GitHub history for details.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/*
|
|
13
|
+
* Licensed to Elasticsearch B.V. under one or more contributor
|
|
14
|
+
* license agreements. See the NOTICE file distributed with
|
|
15
|
+
* this work for additional information regarding copyright
|
|
16
|
+
* ownership. Elasticsearch B.V. licenses this file to you under
|
|
17
|
+
* the Apache License, Version 2.0 (the "License"); you may
|
|
18
|
+
* not use this file except in compliance with the License.
|
|
19
|
+
* You may obtain a copy of the License at
|
|
20
|
+
*
|
|
21
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
22
|
+
*
|
|
23
|
+
* Unless required by applicable law or agreed to in writing,
|
|
24
|
+
* software distributed under the License is distributed on an
|
|
25
|
+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
26
|
+
* KIND, either express or implied. See the License for the
|
|
27
|
+
* specific language governing permissions and limitations
|
|
28
|
+
* under the License.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
import { ApiResponse, Context } from './Transport'
|
|
32
|
+
|
|
33
|
+
export declare class OpenSearchClientError extends Error {
|
|
34
|
+
name: string;
|
|
35
|
+
message: string;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export declare class TimeoutError<TResponse = Record<string, any>, TContext = Context> extends OpenSearchClientError {
|
|
39
|
+
name: string;
|
|
40
|
+
message: string;
|
|
41
|
+
meta: ApiResponse<TResponse, TContext>;
|
|
42
|
+
constructor(message: string, meta: ApiResponse);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export declare class ConnectionError<TResponse = Record<string, any>, TContext = Context> extends OpenSearchClientError {
|
|
46
|
+
name: string;
|
|
47
|
+
message: string;
|
|
48
|
+
meta: ApiResponse<TResponse, TContext>;
|
|
49
|
+
constructor(message: string, meta: ApiResponse);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export declare class NoLivingConnectionsError<TResponse = Record<string, any>, TContext = Context> extends OpenSearchClientError {
|
|
53
|
+
name: string;
|
|
54
|
+
message: string;
|
|
55
|
+
meta: ApiResponse<TResponse, TContext>;
|
|
56
|
+
constructor(message: string, meta: ApiResponse);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export declare class SerializationError extends OpenSearchClientError {
|
|
60
|
+
name: string;
|
|
61
|
+
message: string;
|
|
62
|
+
data: any;
|
|
63
|
+
constructor(message: string, data: any);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export declare class DeserializationError extends OpenSearchClientError {
|
|
67
|
+
name: string;
|
|
68
|
+
message: string;
|
|
69
|
+
data: string;
|
|
70
|
+
constructor(message: string, data: string);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export declare class ConfigurationError extends OpenSearchClientError {
|
|
74
|
+
name: string;
|
|
75
|
+
message: string;
|
|
76
|
+
constructor(message: string);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export declare class ResponseError<TResponse = Record<string, any>, TContext = Context> extends OpenSearchClientError {
|
|
80
|
+
name: string;
|
|
81
|
+
message: string;
|
|
82
|
+
meta: ApiResponse<TResponse, TContext>;
|
|
83
|
+
body: TResponse;
|
|
84
|
+
statusCode: number;
|
|
85
|
+
headers: Record<string, any>;
|
|
86
|
+
constructor(meta: ApiResponse);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export declare class RequestAbortedError<TResponse = Record<string, any>, TContext = Context> extends OpenSearchClientError {
|
|
90
|
+
name: string;
|
|
91
|
+
message: string;
|
|
92
|
+
meta: ApiResponse<TResponse, TContext>;
|
|
93
|
+
constructor(message: string, meta: ApiResponse);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export declare class NotCompatibleError<TResponse = Record<string, any>, TContext = Context> extends OpenSearchClientError {
|
|
97
|
+
name: string;
|
|
98
|
+
message: string;
|
|
99
|
+
meta: ApiResponse<TResponse, TContext>;
|
|
100
|
+
constructor(meta: ApiResponse);
|
|
101
|
+
}
|
package/lib/errors.js
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* The OpenSearch Contributors require contributions made to
|
|
5
|
+
* this file be licensed under the Apache-2.0 license or a
|
|
6
|
+
* compatible open source license.
|
|
7
|
+
*
|
|
8
|
+
* Modifications Copyright OpenSearch Contributors. See
|
|
9
|
+
* GitHub history for details.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/*
|
|
13
|
+
* Licensed to Elasticsearch B.V. under one or more contributor
|
|
14
|
+
* license agreements. See the NOTICE file distributed with
|
|
15
|
+
* this work for additional information regarding copyright
|
|
16
|
+
* ownership. Elasticsearch B.V. licenses this file to you under
|
|
17
|
+
* the Apache License, Version 2.0 (the "License"); you may
|
|
18
|
+
* not use this file except in compliance with the License.
|
|
19
|
+
* You may obtain a copy of the License at
|
|
20
|
+
*
|
|
21
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
22
|
+
*
|
|
23
|
+
* Unless required by applicable law or agreed to in writing,
|
|
24
|
+
* software distributed under the License is distributed on an
|
|
25
|
+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
26
|
+
* KIND, either express or implied. See the License for the
|
|
27
|
+
* specific language governing permissions and limitations
|
|
28
|
+
* under the License.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
'use strict'
|
|
32
|
+
|
|
33
|
+
class OpenSearchClientError extends Error {
|
|
34
|
+
constructor (message) {
|
|
35
|
+
super(message)
|
|
36
|
+
this.name = 'OpenSearchClientError'
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
class TimeoutError extends OpenSearchClientError {
|
|
41
|
+
constructor (message, meta) {
|
|
42
|
+
super(message)
|
|
43
|
+
Error.captureStackTrace(this, TimeoutError)
|
|
44
|
+
this.name = 'TimeoutError'
|
|
45
|
+
this.message = message || 'Timeout Error'
|
|
46
|
+
this.meta = meta
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
class ConnectionError extends OpenSearchClientError {
|
|
51
|
+
constructor (message, meta) {
|
|
52
|
+
super(message)
|
|
53
|
+
Error.captureStackTrace(this, ConnectionError)
|
|
54
|
+
this.name = 'ConnectionError'
|
|
55
|
+
this.message = message || 'Connection Error'
|
|
56
|
+
this.meta = meta
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
class NoLivingConnectionsError extends OpenSearchClientError {
|
|
61
|
+
constructor (message, meta) {
|
|
62
|
+
super(message)
|
|
63
|
+
Error.captureStackTrace(this, NoLivingConnectionsError)
|
|
64
|
+
this.name = 'NoLivingConnectionsError'
|
|
65
|
+
this.message = message || 'Given the configuration, the ConnectionPool was not able to find a usable Connection for this request.'
|
|
66
|
+
this.meta = meta
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
class SerializationError extends OpenSearchClientError {
|
|
71
|
+
constructor (message, data) {
|
|
72
|
+
super(message, data)
|
|
73
|
+
Error.captureStackTrace(this, SerializationError)
|
|
74
|
+
this.name = 'SerializationError'
|
|
75
|
+
this.message = message || 'Serialization Error'
|
|
76
|
+
this.data = data
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
class DeserializationError extends OpenSearchClientError {
|
|
81
|
+
constructor (message, data) {
|
|
82
|
+
super(message, data)
|
|
83
|
+
Error.captureStackTrace(this, DeserializationError)
|
|
84
|
+
this.name = 'DeserializationError'
|
|
85
|
+
this.message = message || 'Deserialization Error'
|
|
86
|
+
this.data = data
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
class ConfigurationError extends OpenSearchClientError {
|
|
91
|
+
constructor (message) {
|
|
92
|
+
super(message)
|
|
93
|
+
Error.captureStackTrace(this, ConfigurationError)
|
|
94
|
+
this.name = 'ConfigurationError'
|
|
95
|
+
this.message = message || 'Configuration Error'
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
class ResponseError extends OpenSearchClientError {
|
|
100
|
+
constructor (meta) {
|
|
101
|
+
super('Response Error')
|
|
102
|
+
Error.captureStackTrace(this, ResponseError)
|
|
103
|
+
this.name = 'ResponseError'
|
|
104
|
+
if (meta.body && meta.body.error && meta.body.error.type) {
|
|
105
|
+
if (Array.isArray(meta.body.error.root_cause)) {
|
|
106
|
+
this.message = meta.body.error.type + ': '
|
|
107
|
+
this.message += meta.body.error.root_cause.map(entry => `[${entry.type}] Reason: ${entry.reason}`).join('; ')
|
|
108
|
+
} else {
|
|
109
|
+
this.message = meta.body.error.type
|
|
110
|
+
}
|
|
111
|
+
} else {
|
|
112
|
+
this.message = 'Response Error'
|
|
113
|
+
}
|
|
114
|
+
this.meta = meta
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
get body () {
|
|
118
|
+
return this.meta.body
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
get statusCode () {
|
|
122
|
+
if (this.meta.body && typeof this.meta.body.status === 'number') {
|
|
123
|
+
return this.meta.body.status
|
|
124
|
+
}
|
|
125
|
+
return this.meta.statusCode
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
get headers () {
|
|
129
|
+
return this.meta.headers
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
toString () {
|
|
133
|
+
return JSON.stringify(this.meta.body)
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
class RequestAbortedError extends OpenSearchClientError {
|
|
138
|
+
constructor (message, meta) {
|
|
139
|
+
super(message)
|
|
140
|
+
Error.captureStackTrace(this, RequestAbortedError)
|
|
141
|
+
this.name = 'RequestAbortedError'
|
|
142
|
+
this.message = message || 'Request aborted'
|
|
143
|
+
this.meta = meta
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
class NotCompatibleError extends OpenSearchClientError {
|
|
148
|
+
constructor (meta) {
|
|
149
|
+
super('Not Compatible Error')
|
|
150
|
+
Error.captureStackTrace(this, NotCompatibleError)
|
|
151
|
+
this.name = 'NotCompatibleError'
|
|
152
|
+
this.message = 'The client noticed that the server is not a supported distribution'
|
|
153
|
+
this.meta = meta
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
module.exports = {
|
|
158
|
+
OpenSearchClientError,
|
|
159
|
+
TimeoutError,
|
|
160
|
+
ConnectionError,
|
|
161
|
+
NoLivingConnectionsError,
|
|
162
|
+
SerializationError,
|
|
163
|
+
DeserializationError,
|
|
164
|
+
ConfigurationError,
|
|
165
|
+
ResponseError,
|
|
166
|
+
RequestAbortedError,
|
|
167
|
+
NotCompatibleError
|
|
168
|
+
}
|
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* The OpenSearch Contributors require contributions made to
|
|
5
|
+
* this file be licensed under the Apache-2.0 license or a
|
|
6
|
+
* compatible open source license.
|
|
7
|
+
*
|
|
8
|
+
* Modifications Copyright OpenSearch Contributors. See
|
|
9
|
+
* GitHub history for details.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/*
|
|
13
|
+
* Licensed to Elasticsearch B.V. under one or more contributor
|
|
14
|
+
* license agreements. See the NOTICE file distributed with
|
|
15
|
+
* this work for additional information regarding copyright
|
|
16
|
+
* ownership. Elasticsearch B.V. licenses this file to you under
|
|
17
|
+
* the Apache License, Version 2.0 (the "License"); you may
|
|
18
|
+
* not use this file except in compliance with the License.
|
|
19
|
+
* You may obtain a copy of the License at
|
|
20
|
+
*
|
|
21
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
22
|
+
*
|
|
23
|
+
* Unless required by applicable law or agreed to in writing,
|
|
24
|
+
* software distributed under the License is distributed on an
|
|
25
|
+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
26
|
+
* KIND, either express or implied. See the License for the
|
|
27
|
+
* specific language governing permissions and limitations
|
|
28
|
+
* under the License.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
'use strict'
|
|
32
|
+
|
|
33
|
+
const { URL } = require('url')
|
|
34
|
+
const debug = require('debug')('opensearch')
|
|
35
|
+
const Connection = require('../Connection')
|
|
36
|
+
const noop = () => { }
|
|
37
|
+
|
|
38
|
+
class BaseConnectionPool {
|
|
39
|
+
constructor (opts) {
|
|
40
|
+
// list of nodes and weights
|
|
41
|
+
this.connections = []
|
|
42
|
+
// how many nodes we have in our scheduler
|
|
43
|
+
this.size = this.connections.length
|
|
44
|
+
this.Connection = opts.Connection
|
|
45
|
+
this.emit = opts.emit || noop
|
|
46
|
+
this.auth = opts.auth || null
|
|
47
|
+
this._ssl = opts.ssl
|
|
48
|
+
this._agent = opts.agent
|
|
49
|
+
this._proxy = opts.proxy || null
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
getConnection () {
|
|
53
|
+
throw new Error('getConnection must be implemented')
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
markAlive () {
|
|
57
|
+
return this
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
markDead () {
|
|
61
|
+
return this
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Creates a new connection instance.
|
|
66
|
+
*/
|
|
67
|
+
createConnection (opts) {
|
|
68
|
+
if (typeof opts === 'string') {
|
|
69
|
+
opts = this.urlToHost(opts)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (this.auth !== null) {
|
|
73
|
+
opts.auth = this.auth
|
|
74
|
+
} else if (opts.url.username !== '' && opts.url.password !== '') {
|
|
75
|
+
opts.auth = {
|
|
76
|
+
username: decodeURIComponent(opts.url.username),
|
|
77
|
+
password: decodeURIComponent(opts.url.password)
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (opts.ssl == null) opts.ssl = this._ssl
|
|
82
|
+
/* istanbul ignore else */
|
|
83
|
+
if (opts.agent == null) opts.agent = this._agent
|
|
84
|
+
/* istanbul ignore else */
|
|
85
|
+
if (opts.proxy == null) opts.proxy = this._proxy
|
|
86
|
+
|
|
87
|
+
const connection = new this.Connection(opts)
|
|
88
|
+
|
|
89
|
+
for (const conn of this.connections) {
|
|
90
|
+
if (conn.id === connection.id) {
|
|
91
|
+
throw new Error(`Connection with id '${connection.id}' is already present`)
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
return connection
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Adds a new connection to the pool.
|
|
100
|
+
*
|
|
101
|
+
* @param {object|string} host
|
|
102
|
+
* @returns {ConnectionPool}
|
|
103
|
+
*/
|
|
104
|
+
addConnection (opts) {
|
|
105
|
+
if (Array.isArray(opts)) {
|
|
106
|
+
return opts.forEach(o => this.addConnection(o))
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (typeof opts === 'string') {
|
|
110
|
+
opts = this.urlToHost(opts)
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const connectionById = this.connections.find(c => c.id === opts.id)
|
|
114
|
+
const connectionByUrl = this.connections.find(c => c.id === opts.url.href)
|
|
115
|
+
|
|
116
|
+
if (connectionById || connectionByUrl) {
|
|
117
|
+
throw new Error(`Connection with id '${opts.id || opts.url.href}' is already present`)
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
this.update([...this.connections, opts])
|
|
121
|
+
return this.connections[this.size - 1]
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Removes a new connection to the pool.
|
|
126
|
+
*
|
|
127
|
+
* @param {object} connection
|
|
128
|
+
* @returns {ConnectionPool}
|
|
129
|
+
*/
|
|
130
|
+
removeConnection (connection) {
|
|
131
|
+
debug('Removing connection', connection)
|
|
132
|
+
return this.update(this.connections.filter(c => c.id !== connection.id))
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Empties the connection pool.
|
|
137
|
+
*
|
|
138
|
+
* @returns {ConnectionPool}
|
|
139
|
+
*/
|
|
140
|
+
empty (callback) {
|
|
141
|
+
debug('Emptying the connection pool')
|
|
142
|
+
let openConnections = this.size
|
|
143
|
+
this.connections.forEach(connection => {
|
|
144
|
+
connection.close(() => {
|
|
145
|
+
if (--openConnections === 0) {
|
|
146
|
+
this.connections = []
|
|
147
|
+
this.size = this.connections.length
|
|
148
|
+
callback()
|
|
149
|
+
}
|
|
150
|
+
})
|
|
151
|
+
})
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Update the ConnectionPool with new connections.
|
|
156
|
+
*
|
|
157
|
+
* @param {array} array of connections
|
|
158
|
+
* @returns {ConnectionPool}
|
|
159
|
+
*/
|
|
160
|
+
update (nodes) {
|
|
161
|
+
debug('Updating the connection pool')
|
|
162
|
+
const newConnections = []
|
|
163
|
+
const oldConnections = []
|
|
164
|
+
|
|
165
|
+
for (const node of nodes) {
|
|
166
|
+
// if we already have a given connection in the pool
|
|
167
|
+
// we mark it as alive and we do not close the connection
|
|
168
|
+
// to avoid socket issues
|
|
169
|
+
const connectionById = this.connections.find(c => c.id === node.id)
|
|
170
|
+
const connectionByUrl = this.connections.find(c => c.id === node.url.href)
|
|
171
|
+
if (connectionById) {
|
|
172
|
+
debug(`The connection with id '${node.id}' is already present`)
|
|
173
|
+
this.markAlive(connectionById)
|
|
174
|
+
newConnections.push(connectionById)
|
|
175
|
+
// in case the user has passed a single url (or an array of urls),
|
|
176
|
+
// the connection id will be the full href; to avoid closing valid connections
|
|
177
|
+
// because are not present in the pool, we check also the node url,
|
|
178
|
+
// and if is already present we update its id with the opensearch provided one.
|
|
179
|
+
} else if (connectionByUrl) {
|
|
180
|
+
connectionByUrl.id = node.id
|
|
181
|
+
this.markAlive(connectionByUrl)
|
|
182
|
+
newConnections.push(connectionByUrl)
|
|
183
|
+
} else {
|
|
184
|
+
newConnections.push(this.createConnection(node))
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const ids = nodes.map(c => c.id)
|
|
189
|
+
// remove all the dead connections and old connections
|
|
190
|
+
for (const connection of this.connections) {
|
|
191
|
+
if (ids.indexOf(connection.id) === -1) {
|
|
192
|
+
oldConnections.push(connection)
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// close old connections
|
|
197
|
+
oldConnections.forEach(connection => connection.close())
|
|
198
|
+
|
|
199
|
+
this.connections = newConnections
|
|
200
|
+
this.size = this.connections.length
|
|
201
|
+
|
|
202
|
+
return this
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Transforms the nodes objects to a host object.
|
|
207
|
+
*
|
|
208
|
+
* @param {object} nodes
|
|
209
|
+
* @returns {array} hosts
|
|
210
|
+
*/
|
|
211
|
+
nodesToHost (nodes, protocol) {
|
|
212
|
+
const ids = Object.keys(nodes)
|
|
213
|
+
const hosts = []
|
|
214
|
+
|
|
215
|
+
for (let i = 0, len = ids.length; i < len; i++) {
|
|
216
|
+
const node = nodes[ids[i]]
|
|
217
|
+
// If there is no protocol in
|
|
218
|
+
// the `publish_address` new URL will throw
|
|
219
|
+
// the publish_address can have two forms:
|
|
220
|
+
// - ip:port
|
|
221
|
+
// - hostname/ip:port
|
|
222
|
+
// if we encounter the second case, we should
|
|
223
|
+
// use the hostname instead of the ip
|
|
224
|
+
let address = node.http.publish_address
|
|
225
|
+
const parts = address.split('/')
|
|
226
|
+
// the url is in the form of hostname/ip:port
|
|
227
|
+
if (parts.length > 1) {
|
|
228
|
+
const hostname = parts[0]
|
|
229
|
+
const port = parts[1].match(/((?::))(?:[0-9]+)$/g)[0].slice(1)
|
|
230
|
+
address = `${hostname}:${port}`
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
address = address.slice(0, 4) === 'http'
|
|
234
|
+
/* istanbul ignore next */
|
|
235
|
+
? address
|
|
236
|
+
: `${protocol}//${address}`
|
|
237
|
+
const roles = node.roles.reduce((acc, role) => {
|
|
238
|
+
acc[role] = true
|
|
239
|
+
return acc
|
|
240
|
+
}, {})
|
|
241
|
+
|
|
242
|
+
hosts.push({
|
|
243
|
+
url: new URL(address),
|
|
244
|
+
id: ids[i],
|
|
245
|
+
roles: Object.assign({
|
|
246
|
+
[Connection.roles.MASTER]: false,
|
|
247
|
+
[Connection.roles.DATA]: false,
|
|
248
|
+
[Connection.roles.INGEST]: false
|
|
249
|
+
}, roles)
|
|
250
|
+
})
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
return hosts
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Transforms an url string to a host object
|
|
258
|
+
*
|
|
259
|
+
* @param {string} url
|
|
260
|
+
* @returns {object} host
|
|
261
|
+
*/
|
|
262
|
+
urlToHost (url) {
|
|
263
|
+
return {
|
|
264
|
+
url: new URL(url)
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
module.exports = BaseConnectionPool
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
*
|
|
4
|
+
* The OpenSearch Contributors require contributions made to
|
|
5
|
+
* this file be licensed under the Apache-2.0 license or a
|
|
6
|
+
* compatible open source license.
|
|
7
|
+
*
|
|
8
|
+
* Modifications Copyright OpenSearch Contributors. See
|
|
9
|
+
* GitHub history for details.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/*
|
|
13
|
+
* Licensed to Elasticsearch B.V. under one or more contributor
|
|
14
|
+
* license agreements. See the NOTICE file distributed with
|
|
15
|
+
* this work for additional information regarding copyright
|
|
16
|
+
* ownership. Elasticsearch B.V. licenses this file to you under
|
|
17
|
+
* the Apache License, Version 2.0 (the "License"); you may
|
|
18
|
+
* not use this file except in compliance with the License.
|
|
19
|
+
* You may obtain a copy of the License at
|
|
20
|
+
*
|
|
21
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
22
|
+
*
|
|
23
|
+
* Unless required by applicable law or agreed to in writing,
|
|
24
|
+
* software distributed under the License is distributed on an
|
|
25
|
+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
26
|
+
* KIND, either express or implied. See the License for the
|
|
27
|
+
* specific language governing permissions and limitations
|
|
28
|
+
* under the License.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
'use strict'
|
|
32
|
+
|
|
33
|
+
const BaseConnectionPool = require('./BaseConnectionPool')
|
|
34
|
+
|
|
35
|
+
class CloudConnectionPool extends BaseConnectionPool {
|
|
36
|
+
constructor (opts) {
|
|
37
|
+
super(opts)
|
|
38
|
+
this.cloudConnection = null
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Returns the only cloud connection.
|
|
43
|
+
*
|
|
44
|
+
* @returns {object} connection
|
|
45
|
+
*/
|
|
46
|
+
getConnection () {
|
|
47
|
+
return this.cloudConnection
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Empties the connection pool.
|
|
52
|
+
*
|
|
53
|
+
* @returns {ConnectionPool}
|
|
54
|
+
*/
|
|
55
|
+
empty (callback) {
|
|
56
|
+
super.empty(() => {
|
|
57
|
+
this.cloudConnection = null
|
|
58
|
+
callback()
|
|
59
|
+
})
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Update the ConnectionPool with new connections.
|
|
64
|
+
*
|
|
65
|
+
* @param {array} array of connections
|
|
66
|
+
* @returns {ConnectionPool}
|
|
67
|
+
*/
|
|
68
|
+
update (connections) {
|
|
69
|
+
super.update(connections)
|
|
70
|
+
this.cloudConnection = this.connections[0]
|
|
71
|
+
return this
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
module.exports = CloudConnectionPool
|