Class: Redis::Cluster

Inherits:
Redis
  • Object
show all
Defined in:
lib/redis/cluster.rb,
lib/redis/cluster/client.rb,
lib/redis/cluster/version.rb,
lib/redis/cluster/transaction_adapter.rb

Defined Under Namespace

Classes: AmbiguousNodeError, Client, CommandErrorCollection, InitialSetupError, NodeMightBeDown, OrchestrationCommandNotSupported, TransactionAdapter, TransactionConsistencyError

Constant Summary collapse

VERSION =
Redis::VERSION

Instance Method Summary collapse

Constructor Details

#initializeRedis::Cluster

Create a new client instance

Parameters:

  • options (Hash)


72
73
74
# File 'lib/redis/cluster.rb', line 72

def initialize(*)
  super
end

Instance Method Details

#cluster(subcommand, *args) ⇒ Object

Sends CLUSTER * command to random node and returns its reply.

Parameters:

  • subcommand (String, Symbol)

    the subcommand of cluster command e.g. :slots, :nodes, :slaves, :info

Returns:

  • (Object)

    depends on the subcommand

See Also:



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/redis/cluster.rb', line 85

def cluster(subcommand, *args)
  subcommand = subcommand.to_s.downcase
  block = case subcommand
  when 'slots'
    HashifyClusterSlots
  when 'nodes'
    HashifyClusterNodes
  when 'slaves'
    HashifyClusterSlaves
  when 'info'
    HashifyInfo
  else
    Noop
  end

  send_command([:cluster, subcommand] + args, &block)
end

#connectionObject

Raises:

  • (NotImplementedError)


47
48
49
# File 'lib/redis/cluster.rb', line 47

def connection
  raise NotImplementedError, "Redis::Cluster doesn't implement #connection"
end

#himport_discard(fieldset_name) ⇒ Object



155
156
157
158
# File 'lib/redis/cluster.rb', line 155

def himport_discard(fieldset_name)
  reply = super
  reply.is_a?(Array) ? reply.max : reply
end

#himport_discard_allObject



160
161
162
163
# File 'lib/redis/cluster.rb', line 160

def himport_discard_all
  reply = super
  reply.is_a?(Array) ? reply.max : reply
end

#himport_prepare(fieldset_name, *fields) ⇒ Object

HIMPORT on cluster: redis-cluster-client (>= 0.16.6) routes the whole family natively from the server's command metadata — PREPARE/DISCARD/DISCARDALL fan out to all primaries per their request_policy:all_shards tip (fieldsets are per-connection session state, so every primary's connection needs its own copy), and SET routes by its key's slot via the per-subcommand key spec. HIMPORT defines no response_policy today, so fan-out replies arrive as one-per-primary arrays; these overrides aggregate them for standalone/cluster API parity: PREPARE returns the first "OK" (all-or-raise), the discards return the max across primaries — per-node integers can legitimately diverge when a node joined after the PREPARE. The Array guard keeps the user-facing contract ("OK" / Integer) stable if a future server release adds a response_policy: the driver then aggregates the fan-out itself and hands us a scalar, which we pass through instead of crashing on String#first. Partial fan-out failures raise Redis::Cluster::CommandErrorCollection whose #errors hash tells you which nodes failed; nodes that replied OK keep their fieldsets.

The fieldset registry, monitor atomicity and loss-recovery (on "no such fieldset" the last prepared schema is re-fanned out — through these overrides — and the SET retried once) are inherited from the Redis superclass unchanged; himport_set needs no override.



150
151
152
153
# File 'lib/redis/cluster.rb', line 150

def himport_prepare(fieldset_name, *fields)
  reply = super
  reply.is_a?(Array) ? reply.first : reply
end

#watch(*keys, &block) ⇒ Object

Watch the given keys to determine execution of the MULTI/EXEC block.

Using a block is required for a cluster client. It's different from a standalone client. And you should use the block argument as a receiver if you call commands.

An #unwatch is automatically issued if an exception is raised within the block that is a subclass of StandardError and is not a ConnectionError.

Examples:

A typical use case.

# The client is an instance of the internal adapter for the optimistic locking
redis.watch("{my}key") do |client|
  if client.get("{my}key") == "some value"
    # The tx is an instance of the internal adapter for the transaction
    client.multi do |tx|
      tx.set("{my}key", "other value")
      tx.incr("{my}counter")
    end
  else
    client.unwatch
  end
end
#=> ["OK", 6]

Parameters:

  • keys (String, Array<String>)

    one or more keys to watch

Returns:

  • (Object)

    returns the return value of the block



128
129
130
# File 'lib/redis/cluster.rb', line 128

def watch(*keys, &block)
  synchronize { |c| c.watch(*keys, &block) }
end