Module: Valkey::Commands::ClusterCommands

Included in:
Valkey::Commands
Defined in:
lib/valkey/commands/cluster_commands.rb

Overview

This module contains commands related to Redis Cluster management.

Constant Summary collapse

CLUSTER_FAILOVER_MODES =

Valid modes for CLUSTER FAILOVER: FORCE, TAKEOVER.

%i[force takeover].freeze

Instance Method Summary collapse

Instance Method Details

#askingString

Send ASKING command to the server.

Returns:

  • (String)

    "OK"



13
14
15
# File 'lib/valkey/commands/cluster_commands.rb', line 13

def asking
  send_command(RequestType::ASKING)
end

#cluster_addslots(*slots) ⇒ String

Add slots to the cluster.

Parameters:

  • slots (Array<Integer>)

    array of slot numbers

Returns:

  • (String)

    "OK"



21
22
23
# File 'lib/valkey/commands/cluster_commands.rb', line 21

def cluster_addslots(*slots)
  send_command(RequestType::CLUSTER_ADD_SLOTS, slots)
end

#cluster_addslotsrange(start_slot, end_slot) ⇒ String

Add a range of slots to the cluster.

Parameters:

  • start_slot (Integer)

    starting slot number

  • end_slot (Integer)

    ending slot number

Returns:

  • (String)

    "OK"



30
31
32
# File 'lib/valkey/commands/cluster_commands.rb', line 30

def cluster_addslotsrange(start_slot, end_slot)
  send_command(RequestType::CLUSTER_ADD_SLOTS_RANGE, [start_slot, end_slot])
end

#cluster_bumpepochString

Bump the epoch of the cluster.

Returns:

  • (String)

    "OK"



37
38
39
# File 'lib/valkey/commands/cluster_commands.rb', line 37

def cluster_bumpepoch
  send_command(RequestType::CLUSTER_BUMP_EPOCH)
end

#cluster_count_failure_reports(node_id) ⇒ Integer

Count failure reports for a node.

Parameters:

  • node_id (String)

    the node ID

Returns:

  • (Integer)

    number of failure reports



45
46
47
# File 'lib/valkey/commands/cluster_commands.rb', line 45

def cluster_count_failure_reports(node_id)
  send_command(RequestType::CLUSTER_COUNT_FAILURE_REPORTS, [node_id])
end

#cluster_countkeysinslot(slot) ⇒ Integer

Count keys in a specific slot.

Parameters:

  • slot (Integer)

    the slot number

Returns:

  • (Integer)

    number of keys in the slot



53
54
55
# File 'lib/valkey/commands/cluster_commands.rb', line 53

def cluster_countkeysinslot(slot)
  send_command(RequestType::CLUSTER_COUNT_KEYS_IN_SLOT, [slot])
end

#cluster_delslots(*slots) ⇒ String

Delete slots from the cluster.

Parameters:

  • slots (Array<Integer>)

    array of slot numbers

Returns:

  • (String)

    "OK"



61
62
63
# File 'lib/valkey/commands/cluster_commands.rb', line 61

def cluster_delslots(*slots)
  send_command(RequestType::CLUSTER_DEL_SLOTS, slots)
end

#cluster_delslotsrange(start_slot, end_slot) ⇒ String

Delete a range of slots from the cluster.

Parameters:

  • start_slot (Integer)

    starting slot number

  • end_slot (Integer)

    ending slot number

Returns:

  • (String)

    "OK"



70
71
72
# File 'lib/valkey/commands/cluster_commands.rb', line 70

def cluster_delslotsrange(start_slot, end_slot)
  send_command(RequestType::CLUSTER_DEL_SLOTS_RANGE, [start_slot, end_slot])
end

#cluster_failover(mode = nil) ⇒ String

Force a failover of the cluster.

Must be sent to a replica node. Without a mode, performs a coordinated failover (the primary is asked to pause clients and hand over). :force promotes the replica without coordinating with the primary (useful when the primary is unreachable); :takeover promotes it without any cluster consensus (most aggressive — risks data loss / split brain).

Parameters:

  • mode (Symbol, nil) (defaults to: nil)

    optional failover mode: :force or :takeover

Returns:

  • (String)

    "OK"

Raises:

  • (ArgumentError)

    if mode is not nil, :force, or :takeover

See Also:



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/valkey/commands/cluster_commands.rb', line 89

def cluster_failover(mode = nil)
  args = []
  unless mode.nil?
    unless CLUSTER_FAILOVER_MODES.include?(mode)
      raise ArgumentError, "invalid CLUSTER FAILOVER mode #{mode.inspect}: expected :force or :takeover"
    end

    args << mode.to_s.upcase
  end
  send_command(RequestType::CLUSTER_FAILOVER, args)
end

#cluster_flushslotsString

Flush all slots from the cluster.

Returns:

  • (String)

    "OK"



104
105
106
# File 'lib/valkey/commands/cluster_commands.rb', line 104

def cluster_flushslots
  send_command(RequestType::CLUSTER_FLUSH_SLOTS)
end

#cluster_forget(node_id) ⇒ String

Remove a node from the cluster.

Parameters:

  • node_id (String)

    the node ID to forget

Returns:

  • (String)

    "OK"



112
113
114
# File 'lib/valkey/commands/cluster_commands.rb', line 112

def cluster_forget(node_id)
  send_command(RequestType::CLUSTER_FORGET, [node_id])
end

#cluster_getkeysinslot(slot, count) ⇒ Array<String>

Get keys in a specific slot.

Parameters:

  • slot (Integer)

    the slot number

  • count (Integer)

    maximum number of keys to return

Returns:

  • (Array<String>)

    array of keys



121
122
123
# File 'lib/valkey/commands/cluster_commands.rb', line 121

def cluster_getkeysinslot(slot, count)
  send_command(RequestType::CLUSTER_GET_KEYS_IN_SLOT, [slot, count])
end

#cluster_info(route: nil) ⇒ Hash<String, String>

Get information about the cluster.

Parameters:

  • route (Valkey::Route, nil) (defaults to: nil)

    cluster routing. When routed, may return a Hash of node => value.

Returns:

  • (Hash<String, String>)

    cluster information



129
130
131
132
133
134
135
136
137
# File 'lib/valkey/commands/cluster_commands.rb', line 129

def cluster_info(route: nil)
  send_command(RequestType::CLUSTER_INFO, [], route: route) do |reply|
    if reply.is_a?(Hash)
      reply.transform_values { |v| Utils::HashifyInfo.call(v) }
    else
      Utils::HashifyInfo.call(reply)
    end
  end
end

#cluster_keyslot(key) ⇒ Integer

Get the slot for a key.

Parameters:

  • key (String)

    the key name

Returns:

  • (Integer)

    slot number



143
144
145
# File 'lib/valkey/commands/cluster_commands.rb', line 143

def cluster_keyslot(key)
  send_command(RequestType::CLUSTER_KEY_SLOT, [key])
end

Get information about cluster links.

Parameters:

  • route (Valkey::Route, nil) (defaults to: nil)

    cluster routing. When routed, may return a Hash of node => value.

Returns:

  • (Array<Hash>)

    array of link information



151
152
153
# File 'lib/valkey/commands/cluster_commands.rb', line 151

def cluster_links(route: nil)
  send_command(RequestType::CLUSTER_LINKS, [], route: route)
end

#cluster_meet(ip, port) ⇒ String

Meet another node in the cluster.

Parameters:

  • ip (String)

    IP address of the node

  • port (Integer)

    port of the node

Returns:

  • (String)

    "OK"



160
161
162
# File 'lib/valkey/commands/cluster_commands.rb', line 160

def cluster_meet(ip, port)
  send_command(RequestType::CLUSTER_MEET, [ip, port])
end

#cluster_myid(route: nil) ⇒ String

Get the ID of the current node.

Parameters:

  • route (Valkey::Route, nil) (defaults to: nil)

    cluster routing. When routed, may return a Hash of node => value.

Returns:

  • (String)

    node ID



168
169
170
# File 'lib/valkey/commands/cluster_commands.rb', line 168

def cluster_myid(route: nil)
  send_command(RequestType::CLUSTER_MY_ID, [], route: route)
end

#cluster_myshardid(route: nil) ⇒ String

Get the shard ID of the current node.

Parameters:

  • route (Valkey::Route, nil) (defaults to: nil)

    cluster routing. When routed, may return a Hash of node => value.

Returns:

  • (String)

    shard ID



176
177
178
# File 'lib/valkey/commands/cluster_commands.rb', line 176

def cluster_myshardid(route: nil)
  send_command(RequestType::CLUSTER_MY_SHARD_ID, [], route: route)
end

#cluster_nodes(route: nil) ⇒ Array<Hash>

Get information about all nodes in the cluster.

Parameters:

  • route (Valkey::Route, nil) (defaults to: nil)

    cluster routing. When routed, may return a Hash of node => value.

Returns:

  • (Array<Hash>)

    array of node information



184
185
186
187
188
189
190
191
192
# File 'lib/valkey/commands/cluster_commands.rb', line 184

def cluster_nodes(route: nil)
  send_command(RequestType::CLUSTER_NODES, [], route: route) do |reply|
    if reply.is_a?(Hash)
      reply.transform_values { |v| Utils::HashifyClusterNodes.call(v) }
    else
      Utils::HashifyClusterNodes.call(reply)
    end
  end
end

#cluster_replicas(node_id) ⇒ Array<Hash>

Get information about replica nodes.

Parameters:

  • node_id (String)

    the master node ID

Returns:

  • (Array<Hash>)

    array of replica information



198
199
200
201
202
# File 'lib/valkey/commands/cluster_commands.rb', line 198

def cluster_replicas(node_id)
  send_command(RequestType::CLUSTER_REPLICAS, [node_id]) do |reply|
    Utils::HashifyClusterSlaves.call(reply)
  end
end

#cluster_replicate(node_id) ⇒ String

Set a node as a replica of another node.

Parameters:

  • node_id (String)

    the master node ID

Returns:

  • (String)

    "OK"



208
209
210
# File 'lib/valkey/commands/cluster_commands.rb', line 208

def cluster_replicate(node_id)
  send_command(RequestType::CLUSTER_REPLICATE, [node_id])
end

#cluster_reset(hard = nil) ⇒ String

Reset the cluster.

Parameters:

  • hard (String) (defaults to: nil)

    hard reset

Returns:

  • (String)

    "OK"



216
217
218
219
220
# File 'lib/valkey/commands/cluster_commands.rb', line 216

def cluster_reset(hard = nil)
  args = []
  args << "HARD" if hard
  send_command(RequestType::CLUSTER_RESET, args)
end

#cluster_saveconfigString

Save the cluster configuration.

Returns:

  • (String)

    "OK"



225
226
227
# File 'lib/valkey/commands/cluster_commands.rb', line 225

def cluster_saveconfig
  send_command(RequestType::CLUSTER_SAVE_CONFIG)
end

#cluster_set_config_epoch(epoch) ⇒ String

Set the config epoch for a node.

Parameters:

  • epoch (Integer)

    the config epoch

Returns:

  • (String)

    "OK"



233
234
235
# File 'lib/valkey/commands/cluster_commands.rb', line 233

def cluster_set_config_epoch(epoch)
  send_command(RequestType::CLUSTER_SET_CONFIG_EPOCH, [epoch])
end

#cluster_setslot(slot, state, node_id = nil) ⇒ String

Set the state of a slot.

Parameters:

  • slot (Integer)

    the slot number

  • state (String)

    the state (importing, migrating, node, stable)

  • node_id (String) (defaults to: nil)

    the node ID (optional)

Returns:

  • (String)

    "OK"



243
244
245
246
247
# File 'lib/valkey/commands/cluster_commands.rb', line 243

def cluster_setslot(slot, state, node_id = nil)
  args = [slot, state]
  args << node_id if node_id
  send_command(RequestType::CLUSTER_SETSLOT, args)
end

#cluster_shards(route: nil) ⇒ Array<Hash>

Get information about cluster shards.

Parameters:

  • route (Valkey::Route, nil) (defaults to: nil)

    cluster routing. When routed, may return a Hash of node => value.

Returns:

  • (Array<Hash>)

    array of shard information



253
254
255
# File 'lib/valkey/commands/cluster_commands.rb', line 253

def cluster_shards(route: nil)
  send_command(RequestType::CLUSTER_SHARDS, [], route: route)
end

#cluster_slaves(node_id) ⇒ Array<Hash>

Get information about slave nodes (deprecated, use cluster_replicas).

Returns:

  • (Array<Hash>)

    array of slave information



260
261
262
263
264
# File 'lib/valkey/commands/cluster_commands.rb', line 260

def cluster_slaves(node_id)
  send_command(RequestType::CLUSTER_SLAVES, [node_id]) do |reply|
    Utils::HashifyClusterSlaves.call(reply)
  end
end

#cluster_slotsArray<Hash>

Get information about cluster slots.

Returns:

  • (Array<Hash>)

    array of slot information



269
270
271
272
273
# File 'lib/valkey/commands/cluster_commands.rb', line 269

def cluster_slots
  send_command(RequestType::CLUSTER_SLOTS) do |reply|
    Utils::HashifyClusterSlots.call(reply)
  end
end

#readonlyString

Set the connection to read-only mode.

Returns:

  • (String)

    "OK"



278
279
280
# File 'lib/valkey/commands/cluster_commands.rb', line 278

def readonly
  send_command(RequestType::READ_ONLY)
end

#readwriteString

Set the connection to read-write mode.

Returns:

  • (String)

    "OK"



285
286
287
# File 'lib/valkey/commands/cluster_commands.rb', line 285

def readwrite
  send_command(RequestType::READ_WRITE)
end