|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using Seq.Api.Model.Cluster; |
| 5 | +using SeqCli.Cli.Features; |
| 6 | +using SeqCli.Connection; |
| 7 | +using Serilog; |
| 8 | + |
| 9 | +#nullable enable |
| 10 | + |
| 11 | +namespace SeqCli.Cli.Commands.Node |
| 12 | +{ |
| 13 | + [Command("node", "demote", "Begin demotion of the current leader node", |
| 14 | + Example = "seqcli node demote -v --wait")] |
| 15 | + class DemoteCommand : Command |
| 16 | + { |
| 17 | + readonly SeqConnectionFactory _connectionFactory; |
| 18 | + |
| 19 | + readonly ConnectionFeature _connection; |
| 20 | + readonly ConfirmFeature _confirm; |
| 21 | + bool _wait; |
| 22 | + |
| 23 | + public DemoteCommand(SeqConnectionFactory connectionFactory) |
| 24 | + { |
| 25 | + _connectionFactory = connectionFactory ?? throw new ArgumentNullException(nameof(connectionFactory)); |
| 26 | + |
| 27 | + Options.Add("wait", "Wait for the leader to be demoted before exiting", _ => _wait = true); |
| 28 | + _confirm = Enable<ConfirmFeature>(); |
| 29 | + |
| 30 | + _connection = Enable<ConnectionFeature>(); |
| 31 | + } |
| 32 | + |
| 33 | + protected override async Task<int> Run() |
| 34 | + { |
| 35 | + var connection = _connectionFactory.Connect(_connection); |
| 36 | + |
| 37 | + if (!_confirm.TryConfirm("This will demote the current cluster leader.")) |
| 38 | + { |
| 39 | + await Console.Error.WriteLineAsync("Canceled by user."); |
| 40 | + return 1; |
| 41 | + } |
| 42 | + |
| 43 | + var demoting = (await connection.ClusterNodes.ListAsync()).SingleOrDefault(n => n.Role == NodeRole.Leader); |
| 44 | + |
| 45 | + if (demoting == null) |
| 46 | + { |
| 47 | + Log.Error("No cluster node is in the leader role"); |
| 48 | + return 1; |
| 49 | + } |
| 50 | + |
| 51 | + await connection.ClusterNodes.DemoteAsync(demoting); |
| 52 | + |
| 53 | + if (!_wait) |
| 54 | + { |
| 55 | + Log.Information("Demotion of node {ClusterNodeId}/{ClusterNodeName} commenced", demoting.Id, demoting.Name); |
| 56 | + return 0; |
| 57 | + } |
| 58 | + |
| 59 | + var lastStatus = demoting.StateDescription; |
| 60 | + Log.Information("Waiting for demotion of node {ClusterNodeId}/{ClusterNodeName} to complete ({LeaderNodeStatus})", demoting.Id, demoting.Name, lastStatus); |
| 61 | + |
| 62 | + while (true) |
| 63 | + { |
| 64 | + await Task.Delay(100); |
| 65 | + |
| 66 | + var nodes = await connection.ClusterNodes.ListAsync(); |
| 67 | + demoting = nodes.FirstOrDefault(n => n.Id == demoting.Id); |
| 68 | + |
| 69 | + if (demoting == null) |
| 70 | + { |
| 71 | + var newLeader = nodes.Single(n => n.Role == NodeRole.Leader); |
| 72 | + Log.Information("Demotion completed; old leader is out of service, new leader is {ClusterNodeId}/{ClusterNodeName}", newLeader.Id, newLeader.Name); |
| 73 | + return 0; |
| 74 | + } |
| 75 | + |
| 76 | + if (demoting.StateDescription != lastStatus) |
| 77 | + { |
| 78 | + lastStatus = demoting.StateDescription; |
| 79 | + Log.Information("Demotion in progress ({LeaderNodeStatus})", lastStatus); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments