mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
Add automatic node promotion / demotion.
This commit is contained in:
49
server/demote_command.go
Normal file
49
server/demote_command.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"github.com/coreos/etcd/log"
|
||||
"github.com/coreos/etcd/third_party/github.com/coreos/raft"
|
||||
)
|
||||
|
||||
func init() {
|
||||
raft.RegisterCommand(&DemoteCommand{})
|
||||
}
|
||||
|
||||
// DemoteCommand represents a command to change a peer to a proxy.
|
||||
type DemoteCommand struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
// CommandName returns the name of the command.
|
||||
func (c *DemoteCommand) CommandName() string {
|
||||
return "etcd:demote"
|
||||
}
|
||||
|
||||
// Apply executes the command.
|
||||
func (c *DemoteCommand) Apply(context raft.Context) (interface{}, error) {
|
||||
ps, _ := context.Server().Context().(*PeerServer)
|
||||
|
||||
// Save URLs.
|
||||
clientURL, _ := ps.registry.ClientURL(c.Name)
|
||||
peerURL, _ := ps.registry.PeerURL(c.Name)
|
||||
|
||||
// Perform a removal.
|
||||
(&RemoveCommand{Name: c.Name}).Apply(context)
|
||||
|
||||
// Register node as a proxy.
|
||||
ps.registry.RegisterProxy(c.Name, peerURL, clientURL)
|
||||
|
||||
// Update mode if this change applies to this server.
|
||||
if c.Name == ps.Config.Name {
|
||||
log.Infof("Set mode after demotion: %s", c.Name)
|
||||
ps.SetMode(ProxyMode)
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// NodeName returns the name of the affected node.
|
||||
func (c *DemoteCommand) NodeName() string {
|
||||
return c.Name
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ import (
|
||||
)
|
||||
|
||||
const ThresholdMonitorTimeout = 5 * time.Second
|
||||
const ActiveMonitorTimeout = 5 * time.Second
|
||||
const ActiveMonitorTimeout = 1 * time.Second
|
||||
|
||||
type PeerServerConfig struct {
|
||||
Name string
|
||||
@@ -644,7 +644,7 @@ func (s *PeerServer) monitorActive(closeChan chan bool) {
|
||||
if peerCount > activeSize {
|
||||
peer := peers[rand.Intn(len(peers))]
|
||||
fmt.Println("active.demote»", peer)
|
||||
if _, err := s.raftServer.Do(&RemoveCommand{Name: peer}); err != nil {
|
||||
if _, err := s.raftServer.Do(&DemoteCommand{Name: peer}); err != nil {
|
||||
log.Infof("%s: warning: demotion error: %v", s.Config.Name, err)
|
||||
}
|
||||
continue
|
||||
|
||||
Reference in New Issue
Block a user