Skip to content

Commit c416eb2

Browse files
committed
expose last failover status
1 parent 9e14918 commit c416eb2

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

libs/cluster/Server/ClusterProvider.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,8 @@ public MetricsItem[] GetReplicationInfo()
253253
replicationInfo.Add(new("master_sync_last_io_seconds_ago", replicationManager.LastPrimarySyncSeconds.ToString()));
254254
replicationInfo.Add(new("replication_offset_lag", replicationOffsetLag.ToString()));
255255
replicationInfo.Add(new("replication_offset_max_lag", storeWrapper.serverOptions.ReplicationOffsetMaxLag.ToString()));
256-
replicationInfo.Add(new("recoverStatus", replicationManager.recoverStatus.ToString()));
256+
replicationInfo.Add(new("recover_status", replicationManager.recoverStatus.ToString()));
257+
replicationInfo.Add(new("last_failover_state", !clusterEnabled ? FailoverUtils.GetFailoverStatus(FailoverStatus.NO_FAILOVER) : failoverManager.GetLastFailoverStatus()));
257258
}
258259
else
259260
{

libs/cluster/Server/Failover/FailoverManager.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ internal sealed class FailoverManager(ClusterProvider clusterProvider, ILogger l
1616
readonly TimeSpan clusterTimeout = clusterProvider.serverOptions.ClusterTimeout <= 0 ? Timeout.InfiniteTimeSpan : TimeSpan.FromSeconds(clusterProvider.serverOptions.ClusterTimeout);
1717
readonly ILogger logger = logger;
1818
private SingleWriterMultiReaderLock failoverTaskLock;
19+
public FailoverStatus lastFailoverStatus = FailoverStatus.NO_FAILOVER;
1920

2021
public void Dispose()
2122
{
@@ -46,6 +47,12 @@ public string GetFailoverStatus()
4647
FailoverUtils.GetFailoverStatus(FailoverStatus.NO_FAILOVER);
4748
}
4849

50+
/// <summary>
51+
/// Retrieve the status of the last failover
52+
/// </summary>
53+
/// <returns></returns>
54+
public string GetLastFailoverStatus() => FailoverUtils.GetFailoverStatus(lastFailoverStatus);
55+
4956
/// <summary>
5057
/// Method used to initiate a background failover from a replica (CLUSTER FAILOVER command)
5158
/// </summary>
@@ -66,7 +73,8 @@ public bool TryStartReplicaFailover(FailoverOption option, TimeSpan failoverTime
6673
logger: logger);
6774
_ = Task.Run(async () =>
6875
{
69-
_ = await currentFailoverSession.BeginAsyncReplicaFailover();
76+
var success = await currentFailoverSession.BeginAsyncReplicaFailover();
77+
lastFailoverStatus = success ? FailoverStatus.FAILOVER_COMPLETED : FailoverStatus.FAILOVER_ABORTED;
7078
Reset();
7179
});
7280
return true;

libs/cluster/Server/Failover/FailoverStatus.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ internal enum FailoverStatus : byte
1515
ISSUING_PAUSE_WRITES,
1616
WAITING_FOR_SYNC,
1717
FAILOVER_IN_PROGRESS,
18-
TAKING_OVER_AS_PRIMARY
18+
TAKING_OVER_AS_PRIMARY,
19+
FAILOVER_COMPLETED,
20+
FAILOVER_ABORTED
1921
}
2022

2123
internal static class FailoverUtils
@@ -36,6 +38,8 @@ public static string GetFailoverStatus(FailoverStatus? status)
3638
FailoverStatus.WAITING_FOR_SYNC => "waiting-for-sync",
3739
FailoverStatus.FAILOVER_IN_PROGRESS => "failover-in-progress",
3840
FailoverStatus.TAKING_OVER_AS_PRIMARY => "taking-over-as-primary",
41+
FailoverStatus.FAILOVER_COMPLETED => "failover-completed",
42+
FailoverStatus.FAILOVER_ABORTED => "failover-aborted",
3943
_ => throw new Exception("invalid failover status"),
4044
};
4145
}

0 commit comments

Comments
 (0)