Module: ElasticSearch::RetryingClient

Included in:
Client
Defined in:
lib/elasticsearch/client/retrying_client.rb

Constant Summary collapse

RETRYING_DEFAULTS =
{
  :randomize_server_list => true,
  :retries => nil,
  :server_retry_period => 1,
  :server_max_requests => nil,
  :retry_overrides => {}
}.freeze

Instance Method Summary collapse

Instance Method Details

#connect!Object



29
30
31
32
33
34
35
36
37
# File 'lib/elasticsearch/client/retrying_client.rb', line 29

def connect!
  @current_server = next_server
  clear_connect_retries_count_after do
    super
  end
rescue ElasticSearch::RetryableError => exception
  increment_or_raise_on_connect_retry( exception )
  retry
end

#disconnect!Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/elasticsearch/client/retrying_client.rb', line 39

def disconnect!
  # Keep live servers in the list if we have a retry period. Otherwise,
  # always eject, because we will always re-add them.
  if @retry_period && @current_server
    @live_server_list.unshift(@current_server)
  end

  super
  @request_count = 0
end

#disconnect_on_max!Object



84
85
86
87
# File 'lib/elasticsearch/client/retrying_client.rb', line 84

def disconnect_on_max!
  @live_server_list.push(@current_server)
  disconnect!
end

#execute(method_name, *args) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/elasticsearch/client/retrying_client.rb', line 70

def execute(method_name, *args)
  disconnect_on_max! if @max_requests and @request_count >= @max_requests
  @request_count += 1
  begin
    clear_execute_retries_count_after do
      super
    end
  rescue ElasticSearch::RetryableError => exception
    disconnect!
    increment_or_raise_on_execute_retry( exception )
    retry
  end
end

#initialize(servers_or_url, options = {}) ⇒ Object

use cluster status to get server list



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/elasticsearch/client/retrying_client.rb', line 17

def initialize(servers_or_url, options={})
  super
  @options = RETRYING_DEFAULTS.merge(@options)
  @retries = options[:retries] || @servers.size
  @connect_retries_count = 0
  @execute_retries_count = 0
  @request_count = 0
  @max_requests = @options[:server_max_requests]
  @retry_period = @options[:server_retry_period]
  rebuild_live_server_list!
end

#next_serverObject

TODO this can spin indefinitely if timeout > retry_period



51
52
53
54
55
56
57
58
59
# File 'lib/elasticsearch/client/retrying_client.rb', line 51

def next_server
  if @retry_period
    rebuild_live_server_list! if Time.now > @last_rebuild + @retry_period
    raise NoServersAvailable, "No live servers in #{@servers.inspect} since #{@last_rebuild.inspect}." if @live_server_list.empty?
  elsif @live_server_list.empty?
    rebuild_live_server_list!
  end
  @live_server_list.shift
end

#rebuild_live_server_list!Object



61
62
63
64
65
66
67
68
# File 'lib/elasticsearch/client/retrying_client.rb', line 61

def rebuild_live_server_list!
  @last_rebuild = Time.now
  if @options[:randomize_server_list]
    @live_server_list = @servers.sort_by { rand }
  else
    @live_server_list = @servers.dup
  end
end