Class: Couchbase::GetReplicaStrategy

Inherits:
Object
  • Object
show all
Defined in:
lib/couchbase/get_replica_strategy.rb,
/home/runner/work/couchbase-ruby-client/couchbase-ruby-client/lib/couchbase/get_replica_strategy.rb

Overview

Selects the replica that Collection#get_replica reads from.

Since:

  • 3.9.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#index ⇒ Symbol (readonly)

Returns the requested replica, one of :first, :second or :third.

Returns:

  • (Symbol) —

    the requested replica, one of :first, :second or :third

Since:

  • 3.9.0



28
29
30
# File 'lib/couchbase/get_replica_strategy.rb', line 28

def index
  @index
end

#wrap ⇒ Boolean (readonly)

Returns whether an index that cannot be read resolves to the next replica that can.

Returns:

  • (Boolean) —

    whether an index that cannot be read resolves to the next replica that can

Since:

  • 3.9.0



31
32
33
# File 'lib/couchbase/get_replica_strategy.rb', line 31

def wrap
  @wrap
end

Class Method Details

.from_index(index, wrap: false) ⇒ GetReplicaStrategy

Selects the replica at the given index.

Without wrap, the SDK resolves the index against the vbucket map it already holds. No network round trip is made for these two errors:

The replica is chosen from the vbucket map, not from node health. A replica on a node that is down is still chosen. The read can then wait for that node until the read times out.

Examples:

Read the first replica, or the next readable one

collection.get_replica("airline_10", GetReplicaStrategy.from_index(:first, wrap: true))

Bound the wait for a replica whose node is down, then read any copy

begin
  collection.get_replica("airline_10", GetReplicaStrategy.from_index(:first, wrap: true),
                         Options::GetReplica(timeout: 500))
rescue Error::UnambiguousTimeout
  collection.get_any_replica("airline_10")
end

Parameters:

  • index (Symbol) —

    the replica to read from, one of :first, :second or :third

  • wrap (Boolean) (defaults to: false) —

    if true, an unreadable index moves to the next readable replica. The walk starts at the index modulo the configured replica count. A bucket configured without replicas still raises Error::ReplicaIndexOutOfBounds. If no replica is readable, it still raises Error::ReplicaIndexCurrentlyUnavailable.

Returns:

Raises:

Since:

  • 3.9.0



65
66
67
68
69
70
71
72
# File 'lib/couchbase/get_replica_strategy.rb', line 65

def self.from_index(index, wrap: false)
  unless REPLICA_INDEXES.key?(index)
    raise Error::InvalidArgument, "replica index must be one of #{REPLICA_INDEXES.keys.inspect}, but given #{index.inspect}"
  end
  raise Error::InvalidArgument, "wrap must be a Boolean, but given #{wrap.inspect}" unless [true, false].include?(wrap)

  new(index, wrap)
end