2023-06-15 10:02:58 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
import argparse
|
2024-02-29 09:59:54 +00:00
|
|
|
from argparse import RawTextHelpFormatter
|
2023-06-15 10:02:58 +00:00
|
|
|
import json
|
|
|
|
import math
|
|
|
|
import redis
|
|
|
|
import subprocess
|
|
|
|
import time
|
|
|
|
|
2024-02-20 11:58:13 +00:00
|
|
|
"""
|
2023-06-15 10:02:58 +00:00
|
|
|
To install: pip install -r requirements.txt
|
2024-02-20 11:58:13 +00:00
|
|
|
"""
|
2023-06-15 10:02:58 +00:00
|
|
|
|
|
|
|
|
2024-06-03 19:05:17 +00:00
|
|
|
def die_with_err(err):
|
|
|
|
print("!!!", err)
|
|
|
|
exit(-1)
|
|
|
|
|
|
|
|
|
2023-06-15 10:02:58 +00:00
|
|
|
class Node:
|
2024-02-29 09:59:54 +00:00
|
|
|
def __init__(self, host, port):
|
2024-02-20 11:58:13 +00:00
|
|
|
self.id = ""
|
2024-02-29 09:59:54 +00:00
|
|
|
self.host = host
|
2023-06-15 10:02:58 +00:00
|
|
|
self.port = port
|
2024-02-29 09:59:54 +00:00
|
|
|
|
|
|
|
def update_id(node):
|
2024-08-05 07:29:31 +00:00
|
|
|
node.id = send_command(node, ["cluster", "myid"])
|
2024-02-29 09:59:54 +00:00
|
|
|
print(f"- ID {node.id}")
|
2023-06-15 10:02:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Master:
|
2024-02-29 09:59:54 +00:00
|
|
|
def __init__(self, host, port):
|
|
|
|
self.node = Node(host, port)
|
2023-06-15 10:02:58 +00:00
|
|
|
self.replicas = []
|
|
|
|
|
|
|
|
|
|
|
|
def start_node(node, threads):
|
2024-02-20 11:58:13 +00:00
|
|
|
f = open(f"/tmp/dfly.cluster.node.{node.port}.log", "w")
|
|
|
|
print(f"- Log file for node {node.port}: {f.name}")
|
|
|
|
subprocess.Popen(
|
|
|
|
[
|
|
|
|
"../build-opt/dragonfly",
|
|
|
|
f"--port={node.port}",
|
|
|
|
"--cluster_mode=yes",
|
|
|
|
f"--proactor_threads={threads}",
|
|
|
|
"--dbfilename=",
|
|
|
|
f"--logtostderr",
|
2024-02-20 13:43:17 +00:00
|
|
|
"--proactor_affinity_mode=off",
|
2024-02-20 11:58:13 +00:00
|
|
|
],
|
|
|
|
stderr=f,
|
|
|
|
)
|
2023-06-15 10:02:58 +00:00
|
|
|
|
|
|
|
|
2024-02-29 09:59:54 +00:00
|
|
|
def send_command(node, command, print_errors=True):
|
|
|
|
client = redis.Redis(decode_responses=True, host=node.host, port=node.port)
|
2023-06-15 10:02:58 +00:00
|
|
|
|
|
|
|
for i in range(0, 5):
|
|
|
|
try:
|
|
|
|
result = client.execute_command(*command)
|
|
|
|
client.close()
|
|
|
|
return result
|
2024-02-20 11:58:13 +00:00
|
|
|
except Exception as e:
|
2024-02-29 09:59:54 +00:00
|
|
|
if print_errors:
|
|
|
|
print(e)
|
2023-06-20 07:13:57 +00:00
|
|
|
time.sleep(0.1 * i)
|
2023-06-15 10:02:58 +00:00
|
|
|
|
2024-02-29 09:59:54 +00:00
|
|
|
if print_errors:
|
|
|
|
print(f"Unable to run command {command} against {node.host}:{node.port} after 5 attempts!")
|
2023-06-15 10:02:58 +00:00
|
|
|
|
2024-02-29 09:59:54 +00:00
|
|
|
return Exception()
|
2023-06-15 10:02:58 +00:00
|
|
|
|
|
|
|
|
2024-05-30 12:29:58 +00:00
|
|
|
def build_node(node):
|
|
|
|
return {"id": node.id, "ip": node.host, "port": node.port}
|
|
|
|
|
|
|
|
|
2023-06-20 07:13:57 +00:00
|
|
|
def build_config_from_list(masters):
|
2023-06-15 10:02:58 +00:00
|
|
|
total_slots = 16384
|
|
|
|
slots_per_node = math.floor(total_slots / len(masters))
|
|
|
|
|
|
|
|
config = []
|
|
|
|
for i, master in enumerate(masters):
|
|
|
|
c = {
|
2024-02-20 11:58:13 +00:00
|
|
|
"slot_ranges": [{"start": i * slots_per_node, "end": (i + 1) * slots_per_node - 1}],
|
2023-06-15 10:02:58 +00:00
|
|
|
"master": build_node(master.node),
|
2024-02-20 11:58:13 +00:00
|
|
|
"replicas": [build_node(replica) for replica in master.replicas],
|
2023-06-15 10:02:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
config.append(c)
|
|
|
|
|
|
|
|
config[-1]["slot_ranges"][-1]["end"] += total_slots % len(masters)
|
2023-06-20 07:13:57 +00:00
|
|
|
return config
|
2023-06-15 10:02:58 +00:00
|
|
|
|
|
|
|
|
2023-06-20 07:13:57 +00:00
|
|
|
def get_nodes_from_config(config):
|
|
|
|
nodes = []
|
|
|
|
for shard in config:
|
2024-02-29 09:59:54 +00:00
|
|
|
nodes.append(Node(shard["master"]["ip"], shard["master"]["port"]))
|
2023-06-20 07:13:57 +00:00
|
|
|
for replica in shard["replicas"]:
|
2024-02-29 09:59:54 +00:00
|
|
|
nodes.append(Node(replica["ip"], replica["port"]))
|
|
|
|
for node in nodes:
|
|
|
|
node.update_id()
|
2023-06-20 07:13:57 +00:00
|
|
|
return nodes
|
2023-06-15 10:02:58 +00:00
|
|
|
|
|
|
|
|
2023-06-20 07:13:57 +00:00
|
|
|
def push_config(config):
|
|
|
|
def push_to_node(node, config):
|
|
|
|
config_str = json.dumps(config, indent=2)
|
2024-02-20 11:58:13 +00:00
|
|
|
response = send_command(node, ["dflycluster", "config", config_str])
|
|
|
|
print(f"- Push to {node.port}: {response}")
|
2023-06-20 07:13:57 +00:00
|
|
|
|
|
|
|
for node in get_nodes_from_config(config):
|
|
|
|
push_to_node(node, config)
|
|
|
|
|
2023-06-15 10:02:58 +00:00
|
|
|
|
2024-02-29 09:59:54 +00:00
|
|
|
def create_locally(args):
|
2024-02-20 11:58:13 +00:00
|
|
|
print(f"Setting up a Dragonfly cluster:")
|
|
|
|
print(f"- Master nodes: {args.num_masters}")
|
|
|
|
print(f"- Ports: {args.first_port}...{args.first_port + args.num_masters - 1}")
|
|
|
|
print(f"- Replicas for each master: {args.replicas_per_master}")
|
2023-06-15 10:02:58 +00:00
|
|
|
print()
|
|
|
|
|
|
|
|
next_port = args.first_port
|
|
|
|
masters = []
|
|
|
|
for i in range(args.num_masters):
|
2024-02-29 09:59:54 +00:00
|
|
|
master = Master("localhost", next_port)
|
2023-06-15 10:02:58 +00:00
|
|
|
next_port += 1
|
|
|
|
for j in range(args.replicas_per_master):
|
2024-02-29 09:59:54 +00:00
|
|
|
replica = Node("localhost", next_port)
|
2023-06-15 10:02:58 +00:00
|
|
|
master.replicas.append(replica)
|
|
|
|
next_port += 1
|
|
|
|
masters.append(master)
|
|
|
|
|
|
|
|
nodes = []
|
|
|
|
for master in masters:
|
|
|
|
nodes.append(master.node)
|
|
|
|
for replica in master.replicas:
|
|
|
|
nodes.append(replica)
|
|
|
|
|
2024-02-20 11:58:13 +00:00
|
|
|
print("Starting nodes...")
|
2023-06-15 10:02:58 +00:00
|
|
|
for node in nodes:
|
|
|
|
start_node(node, args.threads)
|
|
|
|
print()
|
|
|
|
|
|
|
|
if args.replicas_per_master > 0:
|
2024-02-20 11:58:13 +00:00
|
|
|
print("Configuring replication...")
|
2023-06-15 10:02:58 +00:00
|
|
|
for master in masters:
|
|
|
|
for replica in master.replicas:
|
2024-02-29 09:59:54 +00:00
|
|
|
response = send_command(replica, ["replicaof", master.node.host, master.node.port])
|
2024-02-20 11:58:13 +00:00
|
|
|
print(f"- {replica.port} replicating {master.node.port}: {response}")
|
2023-06-15 10:02:58 +00:00
|
|
|
print()
|
|
|
|
|
2024-02-20 11:58:13 +00:00
|
|
|
print(f"Getting IDs...")
|
2023-06-15 10:02:58 +00:00
|
|
|
for n in nodes:
|
2024-02-29 09:59:54 +00:00
|
|
|
n.update_id()
|
2023-06-15 10:02:58 +00:00
|
|
|
print()
|
|
|
|
|
2023-06-20 07:13:57 +00:00
|
|
|
config = build_config_from_list(masters)
|
2024-02-20 11:58:13 +00:00
|
|
|
print(f"Pushing config:\n{config}\n")
|
2023-06-20 07:13:57 +00:00
|
|
|
push_config(config)
|
2023-06-15 10:02:58 +00:00
|
|
|
print()
|
|
|
|
|
|
|
|
|
2024-02-29 09:59:54 +00:00
|
|
|
def config_single_remote(args):
|
|
|
|
print(
|
|
|
|
f"Configuring remote Dragonfly {args.target_host}:{args.target_port} to be a single-server cluster"
|
|
|
|
)
|
|
|
|
|
|
|
|
master = Master(args.target_host, args.target_port)
|
|
|
|
master.node.update_id()
|
|
|
|
|
|
|
|
test = send_command(master.node, ["get", "x"], print_errors=False)
|
|
|
|
if type(test) is not Exception:
|
2024-06-03 19:05:17 +00:00
|
|
|
die_with_err("Node either not found or already configured")
|
2024-02-29 09:59:54 +00:00
|
|
|
|
|
|
|
config = build_config_from_list([master])
|
|
|
|
print(f"Pushing config:\n{config}\n")
|
|
|
|
push_config(config)
|
|
|
|
print()
|
|
|
|
|
|
|
|
|
2023-06-20 07:13:57 +00:00
|
|
|
def build_config_from_existing(args):
|
|
|
|
def list_to_dict(l):
|
|
|
|
return {l[i]: l[i + 1] for i in range(0, len(l), 2)}
|
|
|
|
|
|
|
|
def build_node(node_list):
|
|
|
|
d = list_to_dict(node_list)
|
2024-02-20 11:58:13 +00:00
|
|
|
return {"id": d["id"], "ip": d["endpoint"], "port": d["port"]}
|
2023-06-20 07:13:57 +00:00
|
|
|
|
|
|
|
def build_slots(slot_list):
|
|
|
|
slots = []
|
|
|
|
for i in range(0, len(slot_list), 2):
|
2024-02-20 11:58:13 +00:00
|
|
|
slots.append({"start": slot_list[i], "end": slot_list[i + 1]})
|
2023-06-20 07:13:57 +00:00
|
|
|
return slots
|
|
|
|
|
2024-02-29 09:59:54 +00:00
|
|
|
client = redis.Redis(decode_responses=True, host=args.target_host, port=args.target_port)
|
2023-06-20 07:13:57 +00:00
|
|
|
existing = client.execute_command("cluster", "shards")
|
|
|
|
config = []
|
|
|
|
for shard_list in existing:
|
|
|
|
shard = list_to_dict(shard_list)
|
2024-02-20 11:58:13 +00:00
|
|
|
config.append(
|
|
|
|
{
|
|
|
|
"slot_ranges": build_slots(shard["slots"]),
|
|
|
|
"master": build_node(shard["nodes"][0]),
|
|
|
|
"replicas": [build_node(replica) for replica in shard["nodes"][1::]],
|
|
|
|
}
|
|
|
|
)
|
2023-06-20 07:13:57 +00:00
|
|
|
return config
|
|
|
|
|
|
|
|
|
2024-06-03 19:05:17 +00:00
|
|
|
def find_master(config, host, port, die_if_not_found=True):
|
2023-06-20 07:13:57 +00:00
|
|
|
new_owner = None
|
|
|
|
for shard in config:
|
2024-02-29 09:59:54 +00:00
|
|
|
if shard["master"]["ip"] == host and shard["master"]["port"] == port:
|
2023-06-20 07:13:57 +00:00
|
|
|
new_owner = shard
|
|
|
|
break
|
2024-06-03 19:05:17 +00:00
|
|
|
|
|
|
|
if new_owner == None and die_if_not_found:
|
|
|
|
die_with_err(f"Can't find master (hint: use flag --target_host / --target_port).")
|
|
|
|
|
2024-02-20 11:58:13 +00:00
|
|
|
return new_owner
|
|
|
|
|
|
|
|
|
2024-06-04 08:39:08 +00:00
|
|
|
def find_replica(config, host, port):
|
|
|
|
for master in config:
|
|
|
|
for replica in master["replicas"]:
|
|
|
|
if replica["ip"] == host and replica["port"] == port:
|
|
|
|
return replica, master
|
|
|
|
die_with_err("Can't find target node")
|
|
|
|
|
|
|
|
|
2024-02-29 09:59:54 +00:00
|
|
|
def attach(args):
|
|
|
|
print(f"Attaching remote Dragonfly {args.attach_host}:{args.attach_port} to cluster")
|
2024-05-30 12:29:58 +00:00
|
|
|
if args.attach_as_replica:
|
|
|
|
newcomer = Node(args.attach_host, args.attach_port)
|
2024-06-03 19:05:17 +00:00
|
|
|
replica_resp = send_command(newcomer, ["info", "replication"])
|
2024-08-08 18:42:58 +00:00
|
|
|
if replica_resp["role"] != "slave":
|
2024-06-03 19:05:17 +00:00
|
|
|
die_with_err("Node is not in replica mode")
|
|
|
|
if (
|
|
|
|
replica_resp["master_host"] != args.target_host
|
|
|
|
or replica_resp["master_port"] != args.target_port
|
|
|
|
):
|
|
|
|
die_with_err("Node is not a replica of target")
|
|
|
|
|
2024-05-30 12:29:58 +00:00
|
|
|
newcomer.update_id()
|
|
|
|
newcomer_node = build_node(newcomer)
|
|
|
|
|
2024-06-03 19:05:17 +00:00
|
|
|
config = build_config_from_existing(args)
|
|
|
|
master_node = find_master(config, args.target_host, args.target_port)
|
|
|
|
|
2024-05-30 12:29:58 +00:00
|
|
|
master_node["replicas"].append(newcomer_node)
|
|
|
|
print(f"Pushing config:\n{config}\n")
|
|
|
|
push_config(config)
|
|
|
|
else:
|
|
|
|
newcomer = Master(args.attach_host, args.attach_port)
|
2024-06-03 19:05:17 +00:00
|
|
|
replica_resp = send_command(newcomer.node, ["info", "replication"])
|
|
|
|
if replica_resp["role"] != "master":
|
|
|
|
die_with_err("Node is not in master mode")
|
2024-05-30 12:29:58 +00:00
|
|
|
newcomer.node.update_id()
|
|
|
|
|
|
|
|
newcomer_config = build_config_from_list([newcomer])
|
|
|
|
newcomer_config[0]["slot_ranges"] = []
|
|
|
|
config = build_config_from_existing(args)
|
|
|
|
print(f"Pushing config:\n{config}\n")
|
|
|
|
push_config([*config, newcomer_config[0]])
|
2024-02-29 09:59:54 +00:00
|
|
|
print()
|
|
|
|
|
|
|
|
|
2024-06-03 19:05:17 +00:00
|
|
|
def detach(args):
|
|
|
|
print(f"Detaching remote Dragonfly {args.target_host}:{args.target_port} from cluster")
|
|
|
|
print(
|
|
|
|
"Important: detached node will not receive a new config! This means that the detached node will still 'think' that it belongs to the cluster"
|
|
|
|
)
|
|
|
|
config = build_config_from_existing(args)
|
|
|
|
node = find_master(config, args.target_host, args.target_port, die_if_not_found=False)
|
|
|
|
if node == None:
|
2024-06-04 08:39:08 +00:00
|
|
|
replica, master = find_replica(config, args.target_host, args.target_port)
|
|
|
|
master["replicas"].remove(replica)
|
2024-06-03 19:05:17 +00:00
|
|
|
else:
|
|
|
|
if len(node["slot_ranges"]) != 0:
|
|
|
|
die_with_err("Can't detach a master with assigned slots")
|
|
|
|
if len(node["replicas"]) != 0:
|
|
|
|
die_with_err("Can't detach a master with replicas")
|
|
|
|
config = [m for m in config if m != node]
|
|
|
|
push_config(config)
|
|
|
|
|
|
|
|
|
2024-06-04 08:39:08 +00:00
|
|
|
def takeover(args):
|
|
|
|
print(f"Promoting Dragonfly {args.target_host}:{args.target_port} from replica to master")
|
|
|
|
print(
|
|
|
|
"Important: do not forget to send command REPLICAOF NO ONE to new master, and update "
|
|
|
|
" additional replicas if such exist"
|
|
|
|
)
|
|
|
|
print("Important: previous master will be detached from the cluster")
|
|
|
|
|
|
|
|
config = build_config_from_existing(args)
|
|
|
|
replica, master = find_replica(config, args.target_host, args.target_port)
|
|
|
|
master["replicas"].remove(replica)
|
|
|
|
master["master"] = replica
|
|
|
|
|
|
|
|
push_config(config)
|
|
|
|
|
|
|
|
|
2024-02-20 11:58:13 +00:00
|
|
|
def move(args):
|
|
|
|
config = build_config_from_existing(args)
|
2024-06-03 19:05:17 +00:00
|
|
|
new_owner = find_master(config, args.target_host, args.target_port)
|
2023-06-20 07:13:57 +00:00
|
|
|
|
|
|
|
def remove_slot(slot, from_range, from_shard):
|
|
|
|
if from_range["start"] == slot:
|
|
|
|
from_range["start"] += 1
|
|
|
|
if from_range["start"] > from_range["end"]:
|
|
|
|
from_shard["slot_ranges"].remove(from_range)
|
|
|
|
elif from_range["end"] == slot:
|
|
|
|
from_range["end"] -= 1
|
|
|
|
if from_range["start"] > from_range["end"]:
|
|
|
|
from_shard["slot_ranges"].remove(from_range)
|
|
|
|
else:
|
2024-02-20 11:58:13 +00:00
|
|
|
assert (
|
|
|
|
slot > from_range["start"] and slot < from_range["end"]
|
|
|
|
), f'{slot} {from_range["start"]} {from_range["end"]}'
|
|
|
|
from_shard["slot_ranges"].append({"start": slot + 1, "end": from_range["end"]})
|
2023-06-20 07:13:57 +00:00
|
|
|
from_range["end"] = slot - 1
|
|
|
|
|
|
|
|
def add_slot(slot, to_shard):
|
|
|
|
for slot_range in to_shard["slot_ranges"]:
|
|
|
|
if slot == slot_range["start"] - 1:
|
|
|
|
slot_range["start"] -= 1
|
|
|
|
return
|
|
|
|
if slot == slot_range["end"] + 1:
|
|
|
|
slot_range["end"] += 1
|
|
|
|
return
|
|
|
|
to_shard["slot_ranges"].append({"start": slot, "end": slot})
|
|
|
|
|
|
|
|
def find_slot(slot, config):
|
|
|
|
for shard in config:
|
|
|
|
if shard == new_owner:
|
|
|
|
continue
|
|
|
|
for slot_range in shard["slot_ranges"]:
|
|
|
|
if slot >= slot_range["start"] and slot <= slot_range["end"]:
|
|
|
|
return shard, slot_range
|
|
|
|
return None, None
|
|
|
|
|
|
|
|
def pack(slot_ranges):
|
|
|
|
new_range = []
|
|
|
|
while True:
|
|
|
|
changed = False
|
|
|
|
new_range = []
|
|
|
|
slot_ranges.sort(key=lambda x: x["start"])
|
|
|
|
for i, slot_range in enumerate(slot_ranges):
|
|
|
|
added = False
|
|
|
|
for j in range(i):
|
|
|
|
prev_slot_range = slot_ranges[j]
|
|
|
|
if prev_slot_range["end"] + 1 == slot_range["start"]:
|
|
|
|
prev_slot_range["end"] = slot_range["end"]
|
|
|
|
changed = True
|
|
|
|
added = True
|
|
|
|
break
|
|
|
|
if not added:
|
|
|
|
new_range.append(slot_range)
|
|
|
|
slot_ranges = new_range
|
|
|
|
if not changed:
|
|
|
|
break
|
|
|
|
return new_range
|
|
|
|
|
2024-02-20 11:58:13 +00:00
|
|
|
for slot in range(args.slot_start, args.slot_end + 1):
|
2023-06-20 07:13:57 +00:00
|
|
|
shard, slot_range = find_slot(slot, config)
|
|
|
|
if shard == None:
|
|
|
|
continue
|
|
|
|
if shard == new_owner:
|
|
|
|
continue
|
|
|
|
remove_slot(slot, slot_range, shard)
|
|
|
|
add_slot(slot, new_owner)
|
|
|
|
|
|
|
|
for shard in config:
|
|
|
|
shard["slot_ranges"] = pack(shard["slot_ranges"])
|
|
|
|
|
2024-02-20 11:58:13 +00:00
|
|
|
print(f"Pushing new config:\n{json.dumps(config, indent=2)}\n")
|
2023-06-20 07:13:57 +00:00
|
|
|
push_config(config)
|
|
|
|
|
|
|
|
|
2024-02-20 11:58:13 +00:00
|
|
|
def migrate(args):
|
|
|
|
config = build_config_from_existing(args)
|
2024-06-03 19:05:17 +00:00
|
|
|
target = find_master(config, args.target_host, args.target_port)
|
2024-02-29 09:59:54 +00:00
|
|
|
target_node = Node(target["master"]["ip"], target["master"]["port"])
|
|
|
|
target_node.update_id()
|
2024-02-20 11:58:13 +00:00
|
|
|
|
|
|
|
# Find source node
|
|
|
|
source = None
|
|
|
|
for node in config:
|
|
|
|
slots = node["slot_ranges"]
|
|
|
|
for slot in slots:
|
2024-02-29 09:59:54 +00:00
|
|
|
if slot["start"] <= args.slot_start and slot["end"] >= args.slot_end:
|
2024-02-20 11:58:13 +00:00
|
|
|
source = node
|
|
|
|
break
|
|
|
|
if source == None:
|
2024-06-03 19:05:17 +00:00
|
|
|
die_with_err("Unsupported slot range migration (currently only 1-node migration supported)")
|
2024-02-20 11:58:13 +00:00
|
|
|
|
2024-06-04 10:27:42 +00:00
|
|
|
source["migrations"] = [
|
|
|
|
{
|
|
|
|
"slot_ranges": [{"start": args.slot_start, "end": args.slot_end}],
|
|
|
|
"node_id": target_node.id,
|
|
|
|
"ip": target_node.host,
|
|
|
|
"port": target_node.port,
|
|
|
|
}
|
|
|
|
]
|
|
|
|
push_config(config)
|
2024-02-20 11:58:13 +00:00
|
|
|
|
|
|
|
# wait for migration finish
|
|
|
|
sync_status = []
|
|
|
|
while True:
|
|
|
|
sync_status = send_command(target_node, ["DFLYCLUSTER", "SLOT-MIGRATION-STATUS"])
|
2024-06-04 10:27:42 +00:00
|
|
|
if sync_status == "NO_STATE":
|
|
|
|
continue
|
|
|
|
if len(sync_status) != 1:
|
|
|
|
die_with_err(f"Unexpected number of migrations {len(sync_status)}: {sync_status}")
|
|
|
|
if "FINISHED" in sync_status[0]:
|
|
|
|
print(f"Migration finished: {sync_status[0]}")
|
2024-02-20 11:58:13 +00:00
|
|
|
break
|
|
|
|
|
|
|
|
# Push new config to all nodes
|
2024-06-04 10:27:42 +00:00
|
|
|
print("Updating all nodes with new slots state")
|
2024-02-20 11:58:13 +00:00
|
|
|
move(args)
|
|
|
|
|
|
|
|
|
2023-06-20 07:13:57 +00:00
|
|
|
def print_config(args):
|
|
|
|
config = build_config_from_existing(args)
|
|
|
|
print(json.dumps(config, indent=2))
|
|
|
|
|
|
|
|
|
|
|
|
def shutdown(args):
|
|
|
|
config = build_config_from_existing(args)
|
|
|
|
for node in get_nodes_from_config(config):
|
|
|
|
send_command(node, ["shutdown"])
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2024-02-20 11:58:13 +00:00
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
description="""
|
2024-02-29 09:59:54 +00:00
|
|
|
Dragonfly Manual Cluster Manager
|
|
|
|
|
|
|
|
This tool helps managing a Dragonfly cluster manually.
|
|
|
|
Cluster can either be local or remote:
|
|
|
|
- Starting Dragonfly instances must be done locally, binary is assumed to be under ../build-opt
|
|
|
|
- Remote Dragonflies must already be started, and initialized with `--cluster_mode=yes`
|
2023-06-20 07:13:57 +00:00
|
|
|
|
|
|
|
Example usage:
|
|
|
|
|
2024-02-29 09:59:54 +00:00
|
|
|
Create a 3 node cluster locally:
|
|
|
|
./cluster_mgr.py --action=create_locally --num_masters=3
|
|
|
|
This will create 3 Dragonfly processes with ports 7001-7003.
|
|
|
|
Ports can be overridden with `--first_port`.
|
2023-06-20 07:13:57 +00:00
|
|
|
|
2024-02-29 09:59:54 +00:00
|
|
|
Create a 6 node cluster locally, 3 of them masters with 1 replica each:
|
|
|
|
./cluster_mgr.py --action=create_locally --num_masters=3 --replicas_per_master=1
|
2023-06-20 07:13:57 +00:00
|
|
|
|
2024-02-29 09:59:54 +00:00
|
|
|
Connect to existing cluster and print current config:
|
|
|
|
./cluster_mgr.py --action=print_config
|
|
|
|
This will connect to localhost:6379 by default. Override with `--target_host` and `--target_port`
|
2023-06-20 07:13:57 +00:00
|
|
|
|
2024-02-29 09:59:54 +00:00
|
|
|
Configure an existing Dragonfly server to be a standalone cluster (owning all slots):
|
|
|
|
./cluster_mgr.py --action=config_single_remote
|
|
|
|
This connects to an *existing* Dragonfly server, and pushes a config telling it to own all slots.
|
|
|
|
This will connect to localhost:6379 by default. Override with `--target_host` and `--target_port`
|
2024-02-20 11:58:13 +00:00
|
|
|
|
2024-02-29 09:59:54 +00:00
|
|
|
Attach an existing Dragonfly server to an existing cluster (owning no slots):
|
|
|
|
./cluster_mgr.py --action=attach --attach_host=HOST --attach_port=PORT
|
|
|
|
This will connect to existing cluster present at localhost:6379 by default. Override with
|
2024-06-03 19:05:17 +00:00
|
|
|
`--target_host` and `--target_port`.
|
|
|
|
To attach node as a replica - use --attach_as_replica=True. In such case, the node will be a
|
|
|
|
replica of --target_host/--target_port.
|
2024-02-29 09:59:54 +00:00
|
|
|
|
|
|
|
To set up a new cluster - start the servers and then use
|
|
|
|
./cluster_mgr.py --action=config_single_remote ...
|
|
|
|
./cluster_mgr.py --action=attach ...
|
|
|
|
And repeat `--action=attach` for all servers.
|
|
|
|
Afterwards, distribute the slots between the servers as desired with `--action=move` or
|
2024-06-03 19:05:17 +00:00
|
|
|
`--action=migrate`.
|
|
|
|
|
|
|
|
To detach (remove) a node from the cluster:
|
|
|
|
./cluster_mgr.py --action=detach --target_host=X --target_port=X
|
|
|
|
Notes:
|
|
|
|
- If the node is a master, it must not have any slots assigned to it.
|
|
|
|
- The node will not be notified that it's no longer in a cluster. It's a good idea to shut it down
|
|
|
|
after detaching it from the cluster.
|
2024-02-29 09:59:54 +00:00
|
|
|
|
2024-06-04 08:39:08 +00:00
|
|
|
To take over (turn replica to master):
|
|
|
|
./cluster_mgr.py --action=takeover --target_host=X --target_port=X
|
|
|
|
Notes:
|
|
|
|
- You'll need to run REPLICAOF NO ONE on the new master
|
|
|
|
- If previous master had other replicas, you'll need to update them with REPLICAOF as well
|
|
|
|
- Previous master will be detached from cluster. It's a good idea to shut it down.
|
|
|
|
|
2024-02-29 09:59:54 +00:00
|
|
|
Connect to cluster and move slots 10-20 to target:
|
|
|
|
./cluster_mgr.py --action=move --slot_start=10 --slot_end=20 --target_host=X --target_port=X
|
2024-06-03 19:05:17 +00:00
|
|
|
WARNING: This will NOT migrate existing data, i.e. data in slots 10-20 will be erased.
|
2024-02-29 09:59:54 +00:00
|
|
|
|
|
|
|
Migrate slots 10-20 to target:
|
|
|
|
./cluster_mgr.py --action=migrate --slot_start=10 --slot_end=20 --target_host=X --target_port=X
|
2024-06-03 19:05:17 +00:00
|
|
|
Unlike --action=move above, this will migrate the data to the new owner.
|
2024-02-29 09:59:54 +00:00
|
|
|
|
|
|
|
Connect to cluster and shutdown all nodes:
|
|
|
|
./cluster_mgr.py --action=shutdown
|
|
|
|
WARNING: Be careful! This will close all Dragonfly servers connected to the cluster.
|
|
|
|
""",
|
|
|
|
formatter_class=RawTextHelpFormatter,
|
2024-02-20 11:58:13 +00:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--action",
|
|
|
|
default="",
|
2024-02-29 09:59:54 +00:00
|
|
|
help="Which action to take? See `--help`",
|
2024-02-20 11:58:13 +00:00
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--num_masters", type=int, default=3, help="Number of master nodes in cluster"
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--replicas_per_master", type=int, default=0, help="How many replicas for each master"
|
|
|
|
)
|
|
|
|
parser.add_argument("--first_port", type=int, default=7001, help="First master's port")
|
|
|
|
parser.add_argument("--threads", type=int, default=2, help="Threads per node")
|
|
|
|
parser.add_argument(
|
2024-02-29 09:59:54 +00:00
|
|
|
"--slot_start", type=int, default=0, help="First slot to move / migrate (inclusive)"
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--slot_end", type=int, default=100, help="Last slot to move / migrate (inclusive)"
|
|
|
|
)
|
|
|
|
parser.add_argument("--target_host", default="localhost", help="Master host/ip")
|
|
|
|
parser.add_argument("--target_port", type=int, default=6379, help="Master port")
|
|
|
|
parser.add_argument(
|
|
|
|
"--attach_host", default="localhost", help="New cluster node master host/ip"
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--attach_port", type=int, default=6379, help="New cluster node master port"
|
2024-02-20 11:58:13 +00:00
|
|
|
)
|
2024-05-30 12:29:58 +00:00
|
|
|
parser.add_argument(
|
|
|
|
"--attach_as_replica", type=bool, default=False, help="Is the attached node a replica?"
|
|
|
|
)
|
2023-06-20 07:13:57 +00:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
2024-02-29 09:59:54 +00:00
|
|
|
actions = dict(
|
|
|
|
[
|
|
|
|
(f.__name__, f)
|
|
|
|
for f in [
|
|
|
|
create_locally,
|
|
|
|
shutdown,
|
|
|
|
config_single_remote,
|
|
|
|
attach,
|
2024-06-03 19:05:17 +00:00
|
|
|
detach,
|
2024-06-04 08:39:08 +00:00
|
|
|
takeover,
|
2024-02-29 09:59:54 +00:00
|
|
|
move,
|
|
|
|
print_config,
|
|
|
|
migrate,
|
|
|
|
]
|
|
|
|
]
|
|
|
|
)
|
2023-06-20 07:13:57 +00:00
|
|
|
action = actions.get(args.action.lower())
|
2024-02-20 11:58:13 +00:00
|
|
|
if action:
|
2023-06-20 07:13:57 +00:00
|
|
|
action(args)
|
|
|
|
else:
|
2024-06-03 19:05:17 +00:00
|
|
|
die_with_err(f'Error - unknown action "{args.action}". See --help')
|
2023-06-20 07:13:57 +00:00
|
|
|
|
|
|
|
|
2023-06-15 10:02:58 +00:00
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|