1
0
Fork 0

Support stepping down before shutting down

master
Philip O'Toole 2 years ago
parent 70ea78fd6c
commit a2899a1cdd

@ -163,6 +163,9 @@ type Config struct {
// RaftShutdownOnRemove sets whether Raft should be shutdown if the node is removed
RaftShutdownOnRemove bool
// RaftStepdownOnShutdown sets whether Leadership should be relinquished on shutdown
RaftStepdownOnShutdown bool
// RaftNoFreelistSync disables syncing Raft database freelist to disk. When true,
// it improves the database write performance under normal operation, but requires
// a full database re-sync during recovery.
@ -393,6 +396,7 @@ func ParseFlags(name, desc string, build *BuildInfo) (*Config, error) {
flag.Uint64Var(&config.RaftSnapThreshold, "raft-snap", 8192, "Number of outstanding log entries that trigger snapshot")
flag.DurationVar(&config.RaftSnapInterval, "raft-snap-int", 30*time.Second, "Snapshot threshold check interval")
flag.DurationVar(&config.RaftLeaderLeaseTimeout, "raft-leader-lease-timeout", 0, "Raft leader lease timeout. Use 0s for Raft default")
flag.BoolVar(&config.RaftStepdownOnShutdown, "raft-shutdown-stepdown", false, "Stepdown as leader before shutting down")
flag.BoolVar(&config.RaftShutdownOnRemove, "raft-remove-shutdown", false, "Shutdown Raft if node removed")
flag.BoolVar(&config.RaftNoFreelistSync, "raft-no-freelist-sync", false, "Do not sync Raft log database freelist to disk")
flag.StringVar(&config.RaftLogLevel, "raft-log-level", "INFO", "Minimum log level for Raft module")

@ -14,6 +14,7 @@ import (
"path/filepath"
"runtime"
"strings"
"syscall"
"time"
consul "github.com/rqlite/rqlite-disco-clients/consul"
@ -158,8 +159,17 @@ func main() {
// Block until signalled.
terminate := make(chan os.Signal, 1)
signal.Notify(terminate, os.Interrupt)
signal.Notify(terminate, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
<-terminate
if (cfg.RaftStepdownOnShutdown) {
if str.IsLeader() {
// Don't log a confusing message if not (probably) Leader
log.Printf("explicitly stepping down before shutdown")
}
// Perform a stepdown, ignore any errors.
str.Stepdown(true)
}
if err := str.Close(true); err != nil {
log.Printf("failed to close store: %s", err.Error())
}

@ -417,6 +417,16 @@ func (s *Store) Bootstrap(servers ...*Server) error {
return nil
}
// Stepdown forces this node to relinquish leadership to another node in
// the cluster. If this node is not the leader an error will be returned.
func (s *Store) Stepdown(wait bool) error {
f := s.raft.LeadershipTransfer()
if !wait {
return nil
}
return f.(raft.Future).Error()
}
// Close closes the store. If wait is true, waits for a graceful shutdown.
func (s *Store) Close(wait bool) (retErr error) {
defer func() {

Loading…
Cancel
Save