-
Notifications
You must be signed in to change notification settings - Fork 595
/
base.rb
344 lines (294 loc) · 14 KB
/
base.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
module Elasticsearch
module Transport
module Transport
# @abstract Module with common functionality for transport implementations.
#
module Base
DEFAULT_PORT = 9200
DEFAULT_PROTOCOL = 'http'
DEFAULT_RELOAD_AFTER = 10_000 # Requests
DEFAULT_RESURRECT_AFTER = 60 # Seconds
DEFAULT_MAX_RETRIES = 3 # Requests
DEFAULT_SERIALIZER_CLASS = Serializer::MultiJson
SANITIZED_PASSWORD = '*' * (rand(14)+1)
attr_reader :hosts, :options, :connections, :counter, :last_request_at, :protocol
attr_accessor :serializer, :sniffer, :logger, :tracer,
:reload_connections, :reload_after,
:resurrect_after, :max_retries
# Creates a new transport object
#
# @param arguments [Hash] Settings and options for the transport
# @param block [Proc] Lambda or Proc which can be evaluated in the context of the "session" object
#
# @option arguments [Array] :hosts An Array of normalized hosts information
# @option arguments [Array] :options A Hash with options (usually passed by {Client})
#
# @see Client#initialize
#
def initialize(arguments={}, &block)
@state_mutex = Mutex.new
@hosts = arguments[:hosts] || []
@options = arguments[:options] || {}
@options[:http] ||= {}
@options[:retry_on_status] ||= []
@block = block
@connections = __build_connections
@serializer = options[:serializer] || ( options[:serializer_class] ? options[:serializer_class].new(self) : DEFAULT_SERIALIZER_CLASS.new(self) )
@protocol = options[:protocol] || DEFAULT_PROTOCOL
@logger = options[:logger]
@tracer = options[:tracer]
@sniffer = options[:sniffer_class] ? options[:sniffer_class].new(self) : Sniffer.new(self)
@counter = 0
@counter_mtx = Mutex.new
@last_request_at = Time.now
@reload_connections = options[:reload_connections]
@reload_after = options[:reload_connections].is_a?(Integer) ? options[:reload_connections] : DEFAULT_RELOAD_AFTER
@resurrect_after = options[:resurrect_after] || DEFAULT_RESURRECT_AFTER
@max_retries = options[:retry_on_failure].is_a?(Integer) ? options[:retry_on_failure] : DEFAULT_MAX_RETRIES
@retry_on_status = Array(options[:retry_on_status]).map { |d| d.to_i }
end
# Returns a connection from the connection pool by delegating to {Connections::Collection#get_connection}.
#
# Resurrects dead connection if the `resurrect_after` timeout has passed.
# Increments the counter and performs connection reloading if the `reload_connections` option is set.
#
# @return [Connections::Connection]
# @see Connections::Collection#get_connection
#
def get_connection(options={})
resurrect_dead_connections! if Time.now > @last_request_at + @resurrect_after
@counter_mtx.synchronize { @counter += 1 }
reload_connections! if reload_connections && counter % reload_after == 0
connections.get_connection(options)
end
# Reloads and replaces the connection collection based on cluster information
#
# @see Sniffer#hosts
#
def reload_connections!
hosts = sniffer.hosts
__rebuild_connections :hosts => hosts, :options => options
self
rescue SnifferTimeoutError
logger.error "[SnifferTimeoutError] Timeout when reloading connections." if logger
self
end
# Tries to "resurrect" all eligible dead connections
#
# @see Connections::Connection#resurrect!
#
def resurrect_dead_connections!
connections.dead.each { |c| c.resurrect! }
end
# Rebuilds the connections collection in the transport.
#
# The methods *adds* new connections from the passed hosts to the collection,
# and *removes* all connections not contained in the passed hosts.
#
# @return [Connections::Collection]
# @api private
#
def __rebuild_connections(arguments={})
@state_mutex.synchronize do
@hosts = arguments[:hosts] || []
@options = arguments[:options] || {}
__close_connections
new_connections = __build_connections
stale_connections = @connections.all.select { |c| ! new_connections.include?(c) }
new_connections = new_connections.reject { |c| @connections.include?(c) }
@connections.remove(stale_connections)
@connections.add(new_connections)
@connections
end
end
# Builds and returns a collection of connections
#
# The adapters have to implement the {Base#__build_connection} method.
#
# @return [Connections::Collection]
# @api private
#
def __build_connections
Connections::Collection.new \
:connections => hosts.map { |host|
host[:protocol] = host[:scheme] || options[:scheme] || options[:http][:scheme] || DEFAULT_PROTOCOL
host[:port] ||= options[:port] || options[:http][:scheme] || DEFAULT_PORT
if (options[:user] || options[:http][:user]) && !host[:user]
host[:user] ||= options[:user] || options[:http][:user]
host[:password] ||= options[:password] || options[:http][:password]
end
__build_connection(host, (options[:transport_options] || {}), @block)
},
:selector_class => options[:selector_class],
:selector => options[:selector]
end
# @abstract Build and return a connection.
# A transport implementation *must* implement this method.
# See {HTTP::Faraday#__build_connection} for an example.
#
# @return [Connections::Connection]
# @api private
#
def __build_connection(host, options={}, block=nil)
raise NoMethodError, "Implement this method in your class"
end
# Closes the connections collection
#
# @api private
#
def __close_connections
# A hook point for specific adapters when they need to close connections
end
# Log request and response information
#
# @api private
#
def __log(method, path, params, body, url, response, json, took, duration)
sanitized_url = url.to_s.gsub(/\/\/(.+):(.+)@/, '//' + '\1:' + SANITIZED_PASSWORD + '@')
logger.info "#{method.to_s.upcase} #{sanitized_url} " +
"[status:#{response.status}, request:#{sprintf('%.3fs', duration)}, query:#{took}]"
logger.debug "> #{__convert_to_json(body)}" if body
logger.debug "< #{response.body}"
end
# Log failed request
#
# @api private
#
def __log_failed(response)
logger.fatal "[#{response.status}] #{response.body}"
end
# Trace the request in the `curl` format
#
# @api private
#
def __trace(method, path, params, body, url, response, json, took, duration)
trace_url = "http://localhost:9200/#{path}?pretty" +
( params.empty? ? '' : "&#{::Faraday::Utils::ParamsHash[params].to_query}" )
trace_body = body ? " -d '#{__convert_to_json(body, :pretty => true)}'" : ''
tracer.info "curl -X #{method.to_s.upcase} '#{trace_url}'#{trace_body}\n"
tracer.debug "# #{Time.now.iso8601} [#{response.status}] (#{format('%.3f', duration)}s)\n#"
tracer.debug json ? serializer.dump(json, :pretty => true).gsub(/^/, '# ').sub(/\}$/, "\n# }")+"\n" : "# #{response.body}\n"
end
# Raise error specific for the HTTP response status or a generic server error
#
# @api private
#
def __raise_transport_error(response)
error = ERRORS[response.status] || ServerError
raise error.new "[#{response.status}] #{response.body}"
end
# Converts any non-String object to JSON
#
# @api private
#
def __convert_to_json(o=nil, options={})
o = o.is_a?(String) ? o : serializer.dump(o, options)
end
# Returns a full URL based on information from host
#
# @param host [Hash] Host configuration passed in from {Client}
#
# @api private
def __full_url(host)
url = "#{host[:protocol]}://"
url += "#{CGI.escape(host[:user])}:#{CGI.escape(host[:password])}@" if host[:user]
url += "#{host[:host]}:#{host[:port]}"
url += "#{host[:path]}" if host[:path]
url
end
# Performs a request to Elasticsearch, while handling logging, tracing, marking dead connections,
# retrying the request and reloading the connections.
#
# @abstract The transport implementation has to implement this method either in full,
# or by invoking this method with a block. See {HTTP::Faraday#perform_request} for an example.
#
# @param method [String] Request method
# @param path [String] The API endpoint
# @param params [Hash] Request parameters (will be serialized by {Connections::Connection#full_url})
# @param body [Hash] Request body (will be serialized by the {#serializer})
# @param block [Proc] Code block to evaluate, passed from the implementation
#
# @return [Response]
# @raise [NoMethodError] If no block is passed
# @raise [ServerError] If request failed on server
# @raise [Error] If no connection is available
#
def perform_request(method, path, params={}, body=nil, &block)
raise NoMethodError, "Implement this method in your transport class" unless block_given?
start = Time.now if logger || tracer
tries = 0
params = params.clone
ignore = Array(params.delete(:ignore)).compact.map { |s| s.to_i }
begin
tries += 1
connection = get_connection or raise Error.new("Cannot get new connection from pool.")
if connection.connection.respond_to?(:params) && connection.connection.params.respond_to?(:to_hash)
params = connection.connection.params.merge(params.to_hash)
end
url = connection.full_url(path, params)
response = block.call(connection, url)
connection.healthy! if connection.failures > 0
# Raise an exception so we can catch it for `retry_on_status`
__raise_transport_error(response) if response.status.to_i >= 300 && @retry_on_status.include?(response.status.to_i)
rescue Elasticsearch::Transport::Transport::ServerError => e
if @retry_on_status.include?(response.status)
logger.warn "[#{e.class}] Attempt #{tries} to get response from #{url}" if logger
if tries <= max_retries
retry
else
logger.fatal "[#{e.class}] Cannot get response from #{url} after #{tries} tries" if logger
raise e
end
else
raise e
end
rescue *host_unreachable_exceptions => e
logger.error "[#{e.class}] #{e.message} #{connection.host.inspect}" if logger
connection.dead!
if @options[:reload_on_failure] and tries < connections.all.size
logger.warn "[#{e.class}] Reloading connections (attempt #{tries} of #{connections.all.size})" if logger
reload_connections! and retry
end
if @options[:retry_on_failure]
logger.warn "[#{e.class}] Attempt #{tries} connecting to #{connection.host.inspect}" if logger
if tries <= max_retries
retry
else
logger.fatal "[#{e.class}] Cannot connect to #{connection.host.inspect} after #{tries} tries" if logger
raise e
end
else
raise e
end
rescue Exception => e
logger.fatal "[#{e.class}] #{e.message} (#{connection.host.inspect if connection})" if logger
raise e
end #/begin
duration = Time.now-start if logger || tracer
if response.status.to_i >= 300
__log method, path, params, body, url, response, nil, 'N/A', duration if logger
__trace method, path, params, body, url, response, nil, 'N/A', duration if tracer
# Log the failure only when `ignore` doesn't match the response status
__log_failed response if logger && !ignore.include?(response.status.to_i)
__raise_transport_error response unless ignore.include?(response.status.to_i)
end
json = serializer.load(response.body) if response.body && !response.body.empty? && response.headers && response.headers["content-type"] =~ /json/
took = (json['took'] ? sprintf('%.3fs', json['took']/1000.0) : 'n/a') rescue 'n/a' if logger || tracer
__log method, path, params, body, url, response, json, took, duration if logger && !ignore.include?(response.status.to_i)
__trace method, path, params, body, url, response, json, took, duration if tracer
Response.new response.status, json || response.body, response.headers
ensure
@last_request_at = Time.now
end
# @abstract Returns an Array of connection errors specific to the transport implementation.
# See {HTTP::Faraday#host_unreachable_exceptions} for an example.
#
# @return [Array]
#
def host_unreachable_exceptions
[Errno::ECONNREFUSED]
end
end
end
end
end