Class: Redis::Client::Connector::Sentinel

Inherits:
Redis::Client::Connector show all
Defined in:
lib/redis/client.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Sentinel

Returns a new instance of Sentinel.



508
509
510
511
512
513
514
515
516
517
# File 'lib/redis/client.rb', line 508

def initialize(options)
  super(options)

  @options[:password] = DEFAULTS.fetch(:password)
  @options[:db] = DEFAULTS.fetch(:db)

  @sentinels = @options.delete(:sentinels).dup
  @role = @options.fetch(:role, "master").to_s
  @master = @options[:host]
end

Instance Method Details

#check(client) ⇒ Object



519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
# File 'lib/redis/client.rb', line 519

def check(client)
  # Check the instance is really of the role we are looking for.
  # We can't assume the command is supported since it was introduced
  # recently and this client should work with old stuff.
  begin
    role = client.call([:role])[0]
  rescue Redis::CommandError
    # Assume the test is passed if we can't get a reply from ROLE...
    role = @role
  end

  if role != @role
    client.disconnect
    raise ConnectionError, "Instance role mismatch. Expected #{@role}, got #{role}."
  end
end

#resolveObject



536
537
538
539
540
541
542
543
544
545
546
547
# File 'lib/redis/client.rb', line 536

def resolve
  result = case @role
           when "master"
             resolve_master
           when "slave"
             resolve_slave
           else
             raise ArgumentError, "Unknown instance role #{@role}"
           end

  result || (raise ConnectionError, "Unable to fetch #{@role} via Sentinel.")
end

#resolve_masterObject



574
575
576
577
578
579
580
# File 'lib/redis/client.rb', line 574

def resolve_master
  sentinel_detect do |client|
    if reply = client.call(["sentinel", "get-master-addr-by-name", @master])
      {:host => reply[0], :port => reply[1]}
    end
  end
end

#resolve_slaveObject



582
583
584
585
586
587
588
589
590
# File 'lib/redis/client.rb', line 582

def resolve_slave
  sentinel_detect do |client|
    if reply = client.call(["sentinel", "slaves", @master])
      slave = Hash[*reply.sample]

      {:host => slave.fetch("ip"), :port => slave.fetch("port")}
    end
  end
end

#sentinel_detectObject

Raises:



549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
# File 'lib/redis/client.rb', line 549

def sentinel_detect
  @sentinels.each do |sentinel|
    client = Client.new(@options.merge({
      :host => sentinel[:host],
      :port => sentinel[:port],
      :reconnect_attempts => 0,
    }))

    begin
      if result = yield(client)
        # This sentinel responded. Make sure we ask it first next time.
        @sentinels.delete(sentinel)
        @sentinels.unshift(sentinel)

        return result
      end
    rescue BaseConnectionError
    ensure
      client.disconnect
    end
  end

  raise CannotConnectError, "No sentinels available."
end