Add 4-letter command for yielding/resigning leadership

This commit is contained in:
Pradeep Chhetri 2023-11-06 14:39:58 +08:00
parent 845a235ad8
commit 361472b29e
8 changed files with 63 additions and 1 deletions

View File

@ -36,7 +36,7 @@ void CoordinationSettings::loadFromConfig(const String & config_elem, const Poco
}
const String KeeperConfigurationAndSettings::DEFAULT_FOUR_LETTER_WORD_CMD = "conf,cons,crst,envi,ruok,srst,srvr,stat,wchs,dirs,mntr,isro,rcvr,apiv,csnp,lgif,rqld,rclc,clrs,ftfl";
const String KeeperConfigurationAndSettings::DEFAULT_FOUR_LETTER_WORD_CMD = "conf,cons,crst,envi,ruok,srst,srvr,stat,wchs,dirs,mntr,isro,rcvr,apiv,csnp,lgif,rqld,rclc,clrs,ftfl,ydld";
KeeperConfigurationAndSettings::KeeperConfigurationAndSettings()
: server_id(NOT_EXIST)

View File

@ -172,6 +172,9 @@ void FourLetterCommandFactory::registerCommands(KeeperDispatcher & keeper_dispat
FourLetterCommandPtr feature_flags_command = std::make_shared<FeatureFlagsCommand>(keeper_dispatcher);
factory.registerCommand(feature_flags_command);
FourLetterCommandPtr yield_leader_command = std::make_shared<YieldLeaderCommand>(keeper_dispatcher);
factory.registerCommand(yield_leader_command);
factory.initializeAllowList(keeper_dispatcher);
factory.setInitialize(true);
}
@ -579,4 +582,9 @@ String FeatureFlagsCommand::run()
return ret.str();
}
String YieldLeaderCommand::run()
{
return keeper_dispatcher.yieldLeader() ? "Sent yield leadership request to leader." : "Failed to send yield leadership request to leader.";
}
}

View File

@ -415,4 +415,17 @@ struct FeatureFlagsCommand : public IFourLetterCommand
~FeatureFlagsCommand() override = default;
};
/// Yield to be leader.
struct YieldLeaderCommand : public IFourLetterCommand
{
explicit YieldLeaderCommand(KeeperDispatcher & keeper_dispatcher_)
: IFourLetterCommand(keeper_dispatcher_)
{
}
String name() override { return "ydld"; }
String run() override;
~YieldLeaderCommand() override = default;
};
}

View File

@ -237,6 +237,12 @@ public:
return server->requestLeader();
}
/// Yield to be leader.
bool yieldLeader()
{
return server->yieldLeader();
}
void recalculateStorageStats()
{
return server->recalculateStorageStats();

View File

@ -1087,6 +1087,11 @@ bool KeeperServer::requestLeader()
return isLeader() || raft_instance->request_leadership();
}
bool KeeperServer::yieldLeader()
{
return isLeader() && raft_instance->yield_leadership();
}
void KeeperServer::recalculateStorageStats()
{
state_machine->recalculateStorageStats();

View File

@ -141,6 +141,8 @@ public:
bool requestLeader();
bool yieldLeader();
void recalculateStorageStats();
};

View File

@ -248,6 +248,11 @@ def is_leader(cluster, node, port=9181):
return "Mode: leader" in stat
def is_follower(cluster, node, port=9181):
stat = send_4lw_cmd(cluster, node, "stat", port)
return "Mode: follower" in stat
def get_leader(cluster, nodes):
for node in nodes:
if is_leader(cluster, node):

View File

@ -718,3 +718,26 @@ def test_cmd_clrs(started_cluster):
finally:
destroy_zk_client(zk)
def test_cmd_ydld(started_cluster):
wait_nodes()
for node in [node1, node3]:
data = keeper_utils.send_4lw_cmd(cluster, node, cmd="ydld")
assert data == "Sent yield leadership request to leader."
print("ydld output -------------------------------------")
print(data)
if keeper_utils.is_leader(cluster, node):
# wait for it to yield leadership
retry = 0
while keeper_utils.is_leader(cluster, node) and retry < 30:
time.sleep(1)
retry += 1
if retry == 30:
print(
node.name
+ " did not yield leadership after 30s, maybe there is something wrong."
)
assert keeper_utils.is_follower(cluster, node)