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



27
28
29
30
31
32
# File 'lib/elasticsearch/client/retrying_client.rb', line 27

def connect!
  @current_server = next_server
  super
rescue ElasticSearch::RetryableError
  retry
end

#disconnect!Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/elasticsearch/client/retrying_client.rb', line 34

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



76
77
78
79
# File 'lib/elasticsearch/client/retrying_client.rb', line 76

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

#execute(method_name, *args) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/elasticsearch/client/retrying_client.rb', line 65

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

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

use cluster status to get server list



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

def initialize(servers, options={})
  super
  @options = RETRYING_DEFAULTS.merge(@options)
  @retries = options[:retries] || @server_list.size
  @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



46
47
48
49
50
51
52
53
54
# File 'lib/elasticsearch/client/retrying_client.rb', line 46

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

#rebuild_live_server_list!Object



56
57
58
59
60
61
62
63
# File 'lib/elasticsearch/client/retrying_client.rb', line 56

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