forked from etcd-io/etcd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request etcd-io#582 from benbjohnson/proxy
Proxies & Config API
- Loading branch information
Showing
107 changed files
with
2,456 additions
and
1,439 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
## Proxies | ||
|
||
Adding peers in an etcd cluster adds network, CPU, and disk overhead to the leader since each one requires replication. | ||
Peers primarily provide resiliency in the event of a leader failure but the benefit of more failover nodes decreases as the cluster size increases. | ||
A lightweight alternative is the proxy. | ||
|
||
Proxies are a way for an etcd node to forward requests along to the cluster but the proxies are not part of the Raft cluster themselves. | ||
This provides an easier API for local applications while reducing the overhead required by a regular peer node. | ||
Proxies also act as standby nodes in the event that a peer node in the cluster has not recovered after a long duration. | ||
|
||
|
||
## Configuration Parameters | ||
|
||
Proxies require two additional configuration parameters: active size & promotion delay. | ||
The active size specifies a target size for the number of peers in the cluster. | ||
If there are not enough peers to meet the active size then proxies are promoted to peers until the peer count is equal to the active size. | ||
If there are more peers than the target active size then peers are demoted to proxies. | ||
|
||
The promotion delay specifies how long the cluster should wait before removing a dead peer and promoting a proxy. | ||
By default this is 30 minutes. | ||
If a peer is inactive for 30 minutes then the peer is removed and a live proxy is found to take its place. | ||
|
||
|
||
## Logical Workflow | ||
|
||
Start a etcd machine and join the cluster: | ||
|
||
``` | ||
If peer count less than active size: | ||
If machine already exists as a proxy: | ||
Remove machine from proxy list | ||
Join as peer | ||
If peer count greater than or equal to active size: | ||
Join as proxy | ||
``` | ||
|
||
Remove an existing etcd machine from the cluster: | ||
|
||
``` | ||
If machine exists in peer list: | ||
Remove from peer list | ||
If machine exists in proxy list: | ||
Remove from proxy list | ||
``` | ||
|
||
Leader's active size monitor: | ||
|
||
``` | ||
Loop: | ||
Sleep 5 seconds | ||
If peer count less than active size: | ||
If proxy count greater than zero: | ||
Request a random proxy to rejoin | ||
Goto Loop | ||
If peer count greater than active size: | ||
Demote randomly selected peer | ||
Goto Loop | ||
``` | ||
|
||
Leader's peer activity monitor: | ||
|
||
``` | ||
Loop: | ||
Sleep 5 seconds | ||
For each peer: | ||
If peer last activity time greater than promote delay: | ||
Demote peer | ||
Goto Loop | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package server | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
const ( | ||
// DefaultActiveSize is the default number of active followers allowed. | ||
DefaultActiveSize = 9 | ||
|
||
// MinActiveSize is the minimum active size allowed. | ||
MinActiveSize = 3 | ||
|
||
// DefaultPromoteDelay is the default elapsed time before promotion. | ||
DefaultPromoteDelay = int((30 * time.Minute) / time.Second) | ||
|
||
// MinPromoteDelay is the minimum promote delay allowed. | ||
MinPromoteDelay = int((2 * time.Second) / time.Second) | ||
) | ||
|
||
// ClusterConfig represents cluster-wide configuration settings. | ||
// These settings can only be changed through Raft. | ||
type ClusterConfig struct { | ||
// ActiveSize is the maximum number of node that can join as Raft followers. | ||
// Nodes that join the cluster after the limit is reached are proxies. | ||
ActiveSize int `json:"activeSize"` | ||
|
||
// PromoteDelay is the amount of time, in seconds, after a node is | ||
// unreachable that it will be swapped out for a proxy node, if available. | ||
PromoteDelay int `json:"promoteDelay"` | ||
} | ||
|
||
// NewClusterConfig returns a cluster configuration with default settings. | ||
func NewClusterConfig() *ClusterConfig { | ||
return &ClusterConfig{ | ||
ActiveSize: DefaultActiveSize, | ||
PromoteDelay: DefaultPromoteDelay, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package server | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/coreos/etcd/log" | ||
"github.com/coreos/etcd/third_party/github.com/goraft/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) | ||
|
||
// Ignore this command if there is no peer. | ||
if !ps.registry.PeerExists(c.Name) { | ||
return nil, fmt.Errorf("peer does not exist: %s", c.Name) | ||
} | ||
|
||
// Save URLs. | ||
clientURL, _ := ps.registry.ClientURL(c.Name) | ||
peerURL, _ := ps.registry.PeerURL(c.Name) | ||
|
||
// Remove node from the shared registry. | ||
err := ps.registry.UnregisterPeer(c.Name) | ||
if err != nil { | ||
log.Debugf("Demote peer %s: Error while unregistering (%v)", c.Name, err) | ||
return nil, err | ||
} | ||
|
||
// Delete from stats | ||
delete(ps.followersStats.Followers, c.Name) | ||
|
||
// Remove peer in raft | ||
err = context.Server().RemovePeer(c.Name) | ||
if err != nil { | ||
log.Debugf("Demote peer %s: (%v)", c.Name, err) | ||
return nil, err | ||
} | ||
|
||
// 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("Demote peer %s: Set mode to proxy with %s", c.Name, ps.server.Leader()) | ||
ps.proxyPeerURL, _ = ps.registry.PeerURL(ps.server.Leader()) | ||
go ps.setMode(ProxyMode) | ||
} | ||
|
||
return nil, nil | ||
} | ||
|
||
// NodeName returns the name of the affected node. | ||
func (c *DemoteCommand) NodeName() string { | ||
return c.Name | ||
} |
Oops, something went wrong.