Class: Kestrel::Client::Reliable

Inherits:
Proxy
  • Object
show all
Defined in:
lib/kestrel/client/reliable.rb

Overview

– TODO: Pull out the sticky server logic into Client. This class

should only be responsible for the retry semantics.

TODO: Ensure that errors are pushed onto the error queue on the

same server on which the error occurred.

++

Defined Under Namespace

Classes: MultipleQueueException, RetryableJob

Constant Summary collapse

DEFAULT_RETRIES =

Number of times to retry a job before giving up

100
ERROR_PROCESSING_RATE =

Pct. of the time during ‘normal’ processing we check the error queue first

0.1
MAX_PER_SERVER =

Maximum number of gets to execute before switching servers

100_000

Instance Attribute Summary

Attributes inherited from Proxy

#client

Instance Method Summary collapse

Methods inherited from Proxy

#method_missing

Constructor Details

#initialize(client, retry_count = nil, error_rate = nil, per_server = nil) ⇒ Reliable

Parameters

client<Kestrel::Client>

Client

retry_count<Integer>

Number of times to retry a job before giving up. Defaults to DEFAULT_RETRIES

error_rate<Float>

Pct. of the time during ‘normal’ processing we check the error queue first. Defaults to ERROR_PROCESSING_RATE

per_server<Integer>

Number of gets to execute against a single server, before changing servers. Defaults to MAX_PER_SERVER



36
37
38
39
40
41
42
# File 'lib/kestrel/client/reliable.rb', line 36

def initialize(client, retry_count = nil, error_rate = nil, per_server = nil)
  @retry_count = retry_count || DEFAULT_RETRIES
  @error_rate  = error_rate || ERROR_PROCESSING_RATE
  @per_server  = per_server || MAX_PER_SERVER
  @counter     = 0 # Command counter
  super(client)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Kestrel::Client::Proxy

Instance Method Details

#current_tryObject



72
73
74
# File 'lib/kestrel/client/reliable.rb', line 72

def current_try
  @job ? @job.retries + 1 : 1
end

#get(key, opts = false) ⇒ Object

Returns job from the key queue 1 - ERROR_PROCESSING_RATE pct. of the time. Every so often, checks the error queue for jobs and returns a retryable job. If either the error queue or key queue are empty, attempts to pull a job from the alternate queue before giving up.

Returns

Job, possibly retryable, or nil



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/kestrel/client/reliable.rb', line 53

def get(key, opts = false)
  raise MultipleQueueException if @key && key != @key

  job =
    if rand < @error_rate
      get_with_fallback(key + "_errors", key, opts)
    else
      get_with_fallback(key, key + "_errors", opts)
    end

  if job
    @key = key
    @job = job.is_a?(RetryableJob) ? job : RetryableJob.new(0, job)
    @job.job
  else
    @key = @job = nil
  end
end

#retryObject

Enqueues the current job on the error queue for later retry. If the job has been retried DEFAULT_RETRIES times, gives up entirely.

Returns

Boolean

true if the job is retryable, false otherwise



83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/kestrel/client/reliable.rb', line 83

def retry
  return unless @job

  close_open_transaction!
  @job.retries += 1

  if @job.retries < @retry_count
    client.set(@key + "_errors", @job)
    true
  else
    false
  end
end