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