Skip to content

Commit 1cf746e

Browse files
committed
fix(uraft): Defer promotion request during demotion
Previously, if a promotion was requested while a demotion was still in progress, the request was ignored. This could leave the node stuck in a follower state even when a promotion was desired. This commit adds a `force_promote_` flag to defer the promotion until the current demotion completes. Once demotion finishes, the controller automatically triggers a forced promotion and logs the transition. This ensures consistent role transitions and prevents promotion loss during overlapping commands. Signed-off-by: Crash <[email protected]>
1 parent 2659b5b commit 1cf746e

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/uraft/uraftcontroller.cc

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ uRaftController::uRaftController(boost::asio::io_context &ios)
2626
command_pid_ = -1;
2727
command_type_ = kCmdNone;
2828
force_demote_ = false;
29+
force_promote_ = false;
2930
node_alive_ = false;
3031

3132
opt_.check_node_status_period = 250;
@@ -70,8 +71,9 @@ void uRaftController::nodePromote() {
7071
// Prevent concurrent transitions
7172
if (command_pid_ >= 0) {
7273
if (command_type_ == kCmdDemote) {
73-
syslog(LOG_WARNING, "Cannot promote during active demotion - ignoring request");
74-
force_demote_ = false;
74+
syslog(LOG_WARNING,
75+
"Promotion requested during demotion - will force after completion");
76+
force_promote_ = true;
7577
return;
7678
}
7779
// Already promoting
@@ -160,8 +162,15 @@ void uRaftController::checkCommandStatus(const boost::system::error_code &error)
160162
} else {
161163
syslog(LOG_ERR, "Demotion failed with exit code: %d", WEXITSTATUS(status));
162164
}
165+
163166
command_type_ = kCmdNone;
164167
command_pid_ = -1;
168+
169+
if (force_promote_) {
170+
syslog(LOG_WARNING, "Starting forced switch to master mode");
171+
nodePromote();
172+
force_promote_ = false;
173+
}
165174
} else if (command_type_ == kCmdPromote) {
166175
if (commandSucceeded) {
167176
syslog(LOG_NOTICE, "Metadata server switch to master mode done");

src/uraft/uraftcontroller.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ class uRaftController : public uRaftStatus {
8585
int command_type_; /// Last run command type.
8686
Timer command_timer_;
8787
bool force_demote_;
88+
bool force_promote_;
8889
bool node_alive_; /// Last is_alive node status.
8990
Options opt_;
9091
///< Smart pointer to the floating IP manager.

0 commit comments

Comments
 (0)