2022-09-06 10:58:14 +00:00
|
|
|
import socket
|
|
|
|
import time
|
2022-11-08 11:56:20 +00:00
|
|
|
|
2022-09-06 10:58:14 +00:00
|
|
|
|
|
|
|
def get_keeper_socket(cluster, node, port=9181):
|
|
|
|
hosts = cluster.get_instance_ip(node.name)
|
|
|
|
client = socket.socket()
|
|
|
|
client.settimeout(10)
|
|
|
|
client.connect((hosts, port))
|
|
|
|
return client
|
|
|
|
|
|
|
|
|
|
|
|
def send_4lw_cmd(cluster, node, cmd="ruok", port=9181):
|
|
|
|
client = None
|
|
|
|
try:
|
|
|
|
client = get_keeper_socket(cluster, node, port)
|
|
|
|
client.send(cmd.encode())
|
|
|
|
data = client.recv(100_000)
|
|
|
|
data = data.decode()
|
|
|
|
return data
|
|
|
|
finally:
|
|
|
|
if client is not None:
|
|
|
|
client.close()
|
|
|
|
|
|
|
|
|
|
|
|
NOT_SERVING_REQUESTS_ERROR_MSG = "This instance is not currently serving requests"
|
|
|
|
|
|
|
|
|
|
|
|
def wait_until_connected(cluster, node, port=9181):
|
|
|
|
while send_4lw_cmd(cluster, node, "mntr", port) == NOT_SERVING_REQUESTS_ERROR_MSG:
|
|
|
|
time.sleep(0.1)
|
|
|
|
|
|
|
|
|
|
|
|
def wait_until_quorum_lost(cluster, node, port=9181):
|
|
|
|
while send_4lw_cmd(cluster, node, "mntr", port) != NOT_SERVING_REQUESTS_ERROR_MSG:
|
|
|
|
time.sleep(0.1)
|
|
|
|
|
|
|
|
|
|
|
|
def wait_nodes(cluster, nodes):
|
|
|
|
for node in nodes:
|
|
|
|
wait_until_connected(cluster, node)
|
2022-11-08 10:44:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
def is_leader(cluster, node, port=9181):
|
|
|
|
stat = send_4lw_cmd(cluster, node, "stat", port)
|
2022-11-08 11:43:36 +00:00
|
|
|
return "Mode: leader" in stat
|
2022-11-16 01:33:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_leader(cluster, nodes):
|
|
|
|
for node in nodes:
|
|
|
|
if is_leader(cluster, node):
|
|
|
|
return node
|
|
|
|
raise Exception("No leader in Keeper cluster.")
|