Maintenance: Make ES connection establishment more stable by:

- performing a retry when Net::OpenTimeout exceptions occur
- cache the result of ES version check
This commit is contained in:
Thorsten Eckel 2019-09-16 11:15:12 +02:00
parent 256e7513b3
commit 429b95e65d
3 changed files with 87 additions and 56 deletions

View file

@ -19,11 +19,12 @@ info about used search index machine
url, url,
{}, {},
{ {
json: true, json: true,
open_timeout: 8, open_timeout: 8,
read_timeout: 14, read_timeout: 14,
user: Setting.get('es_user'), open_socket_tries: 3,
password: Setting.get('es_password'), user: Setting.get('es_user'),
password: Setting.get('es_password'),
} }
) )
Rails.logger.info "# #{response.code}" Rails.logger.info "# #{response.code}"
@ -82,11 +83,12 @@ update processors
response = UserAgent.delete( response = UserAgent.delete(
url, url,
{ {
json: true, json: true,
open_timeout: 8, open_timeout: 8,
read_timeout: 60, read_timeout: 60,
user: Setting.get('es_user'), open_socket_tries: 3,
password: Setting.get('es_password'), user: Setting.get('es_user'),
password: Setting.get('es_password'),
} }
) )
Rails.logger.info "# #{response.code}" Rails.logger.info "# #{response.code}"
@ -106,11 +108,12 @@ update processors
url, url,
item, item,
{ {
json: true, json: true,
open_timeout: 8, open_timeout: 8,
read_timeout: 60, read_timeout: 60,
user: Setting.get('es_user'), open_socket_tries: 3,
password: Setting.get('es_password'), user: Setting.get('es_user'),
password: Setting.get('es_password'),
} }
) )
Rails.logger.info "# #{response.code}" Rails.logger.info "# #{response.code}"
@ -179,11 +182,12 @@ create/update/delete index
url, url,
data[:data], data[:data],
{ {
json: true, json: true,
open_timeout: 8, open_timeout: 8,
read_timeout: 60, read_timeout: 60,
user: Setting.get('es_user'), open_socket_tries: 3,
password: Setting.get('es_password'), user: Setting.get('es_user'),
password: Setting.get('es_password'),
} }
) )
Rails.logger.info "# #{response.code}" Rails.logger.info "# #{response.code}"
@ -217,11 +221,12 @@ add new object to search index
url, url,
data, data,
{ {
json: true, json: true,
open_timeout: 8, open_timeout: 8,
read_timeout: 60, read_timeout: 60,
user: Setting.get('es_user'), open_socket_tries: 3,
password: Setting.get('es_password'), user: Setting.get('es_user'),
password: Setting.get('es_password'),
} }
) )
Rails.logger.info "# #{response.code}" Rails.logger.info "# #{response.code}"
@ -254,10 +259,11 @@ remove whole data from index
response = UserAgent.delete( response = UserAgent.delete(
url, url,
{ {
open_timeout: 8, open_timeout: 8,
read_timeout: 60, read_timeout: 60,
user: Setting.get('es_user'), open_socket_tries: 3,
password: Setting.get('es_password'), user: Setting.get('es_user'),
password: Setting.get('es_password'),
} }
) )
Rails.logger.info "# #{response.code}" Rails.logger.info "# #{response.code}"
@ -365,11 +371,12 @@ remove whole data from index
url, url,
query_data, query_data,
{ {
json: true, json: true,
open_timeout: 5, open_timeout: 5,
read_timeout: 14, read_timeout: 14,
user: Setting.get('es_user'), open_socket_tries: 3,
password: Setting.get('es_password'), user: Setting.get('es_user'),
password: Setting.get('es_password'),
} }
) )
@ -512,11 +519,12 @@ example for aggregations within one year
url, url,
data, data,
{ {
json: true, json: true,
open_timeout: 5, open_timeout: 5,
read_timeout: 14, read_timeout: 14,
user: Setting.get('es_user'), open_socket_tries: 3,
password: Setting.get('es_password'), user: Setting.get('es_user'),
password: Setting.get('es_password'),
} }
) )

View file

@ -275,12 +275,14 @@ end
# get es version # get es version
def es_version def es_version
info = SearchIndexBackend.info @es_version ||= begin
number = nil info = SearchIndexBackend.info
if info.present? number = nil
number = info['version']['number'].to_s if info.present?
number = info['version']['number'].to_s
end
number
end end
number
end end
# no es_pipeline for elasticsearch 5.5 and lower # no es_pipeline for elasticsearch 5.5 and lower

View file

@ -59,9 +59,12 @@ returns
# start http call # start http call
begin begin
total_timeout = options[:total_timeout] || 60 total_timeout = options[:total_timeout] || 60
Timeout.timeout(total_timeout) do
response = http.request(request) handled_open_timeout(options[:open_socket_tries]) do
return process(request, response, uri, count, params, options) Timeout.timeout(total_timeout) do
response = http.request(request)
return process(request, response, uri, count, params, options)
end
end end
rescue => e rescue => e
log(url, request, nil, options) log(url, request, nil, options)
@ -112,9 +115,12 @@ returns
# start http call # start http call
begin begin
total_timeout = options[:total_timeout] || 60 total_timeout = options[:total_timeout] || 60
Timeout.timeout(total_timeout) do
response = http.request(request) handled_open_timeout(options[:open_socket_tries]) do
return process(request, response, uri, count, params, options) Timeout.timeout(total_timeout) do
response = http.request(request)
return process(request, response, uri, count, params, options)
end
end end
rescue => e rescue => e
log(url, request, nil, options) log(url, request, nil, options)
@ -164,9 +170,12 @@ returns
# start http call # start http call
begin begin
total_timeout = options[:total_timeout] || 60 total_timeout = options[:total_timeout] || 60
Timeout.timeout(total_timeout) do
response = http.request(request) handled_open_timeout(options[:open_socket_tries]) do
return process(request, response, uri, count, params, options) Timeout.timeout(total_timeout) do
response = http.request(request)
return process(request, response, uri, count, params, options)
end
end end
rescue => e rescue => e
log(url, request, nil, options) log(url, request, nil, options)
@ -209,9 +218,11 @@ returns
# start http call # start http call
begin begin
total_timeout = options[:total_timeout] || 60 total_timeout = options[:total_timeout] || 60
Timeout.timeout(total_timeout) do handled_open_timeout(options[:open_socket_tries]) do
response = http.request(request) Timeout.timeout(total_timeout) do
return process(request, response, uri, count, {}, options) response = http.request(request)
return process(request, response, uri, count, {}, options)
end
end end
rescue => e rescue => e
log(url, request, nil, options) log(url, request, nil, options)
@ -488,6 +499,16 @@ returns
) )
end end
def self.handled_open_timeout(tries)
tries ||= 1
tries.times do |index|
yield
rescue Net::OpenTimeout
raise if (index + 1) == tries
end
end
class Result class Result
attr_reader :error attr_reader :error