From 32df6f92fc9598e285058927b0c295b2b584eda8 Mon Sep 17 00:00:00 2001 From: Brian Waldon Date: Tue, 14 Jan 2014 08:53:02 -0800 Subject: [PATCH 1/2] fix(peer): Pass peer server timeouts through factory The peer's heartbeat and election timeouts are needed to build the transporter in the factory method. --- etcd.go | 10 +++------- server/config.go | 4 ++-- server/peer_server.go | 6 +++--- server/timeout.go | 12 ++++-------- tests/server_utils.go | 8 +++----- 5 files changed, 15 insertions(+), 25 deletions(-) diff --git a/etcd.go b/etcd.go index 1bb3bd15f..ddfc29cff 100644 --- a/etcd.go +++ b/etcd.go @@ -86,15 +86,11 @@ func main() { registry := server.NewRegistry(store) // Create peer server. - ps := server.NewPeerServer(info.Name, config.DataDir, info.RaftURL, info.RaftListenHost, &peerTLSConfig, &info.RaftTLS, registry, store, config.SnapshotCount) + heartbeatTimeout := time.Duration(config.Peer.HeartbeatTimeout) * time.Millisecond + electionTimeout := time.Duration(config.Peer.ElectionTimeout) * time.Millisecond + ps := server.NewPeerServer(info.Name, config.DataDir, info.RaftURL, info.RaftListenHost, &peerTLSConfig, &info.RaftTLS, registry, store, config.SnapshotCount, heartbeatTimeout, electionTimeout) ps.MaxClusterSize = config.MaxClusterSize ps.RetryTimes = config.MaxRetryAttempts - if config.Peer.HeartbeatTimeout > 0 { - ps.HeartbeatTimeout = time.Duration(config.Peer.HeartbeatTimeout) * time.Millisecond - } - if config.Peer.ElectionTimeout > 0 { - ps.ElectionTimeout = time.Duration(config.Peer.ElectionTimeout) * time.Millisecond - } // Create client server. s := server.New(info.Name, info.EtcdURL, info.EtcdListenHost, &tlsConfig, &info.EtcdTLS, ps, registry, store) diff --git a/server/config.go b/server/config.go index 2b34e33d7..9fd2c119c 100644 --- a/server/config.go +++ b/server/config.go @@ -89,8 +89,8 @@ func NewConfig() *Config { c.MaxRetryAttempts = 3 c.SnapshotCount = 10000 c.Peer.Addr = "127.0.0.1:7001" - c.Peer.HeartbeatTimeout = 0 - c.Peer.ElectionTimeout = 0 + c.Peer.HeartbeatTimeout = defaultHeartbeatTimeout + c.Peer.ElectionTimeout = defaultElectionTimeout return c } diff --git a/server/peer_server.go b/server/peer_server.go index 87e450eb1..a24e7577b 100644 --- a/server/peer_server.go +++ b/server/peer_server.go @@ -62,7 +62,7 @@ type snapshotConf struct { snapshotThr uint64 } -func NewPeerServer(name string, path string, url string, bindAddr string, tlsConf *TLSConfig, tlsInfo *TLSInfo, registry *Registry, store store.Store, snapshotCount int) *PeerServer { +func NewPeerServer(name string, path string, url string, bindAddr string, tlsConf *TLSConfig, tlsInfo *TLSInfo, registry *Registry, store store.Store, snapshotCount int, heartbeatTimeout, electionTimeout time.Duration) *PeerServer { s := &PeerServer{ name: name, url: url, @@ -85,8 +85,8 @@ func NewPeerServer(name string, path string, url string, bindAddr string, tlsCon back: -1, }, }, - HeartbeatTimeout: defaultHeartbeatTimeout, - ElectionTimeout: defaultElectionTimeout, + HeartbeatTimeout: heartbeatTimeout, + ElectionTimeout: electionTimeout, timeoutThresholdChan: make(chan interface{}, 1), } diff --git a/server/timeout.go b/server/timeout.go index 2be2cd59c..9d117f9a2 100644 --- a/server/timeout.go +++ b/server/timeout.go @@ -1,13 +1,9 @@ package server -import ( - "time" -) - const ( - // The amount of time to elapse without a heartbeat before becoming a candidate - defaultElectionTimeout = 200 * time.Millisecond + // The amount of time (in ms) to elapse without a heartbeat before becoming a candidate + defaultElectionTimeout = 200 - // The frequency by which heartbeats are sent to followers. - defaultHeartbeatTimeout = 50 * time.Millisecond + // The frequency (in ms) by which heartbeats are sent to followers. + defaultHeartbeatTimeout = 50 ) diff --git a/tests/server_utils.go b/tests/server_utils.go index c396ed877..10f7a6e1d 100644 --- a/tests/server_utils.go +++ b/tests/server_utils.go @@ -14,8 +14,8 @@ const ( testClientURL = "localhost:4401" testRaftURL = "localhost:7701" testSnapshotCount = 10000 - testHeartbeatTimeout = 50 - testElectionTimeout = 200 + testHeartbeatTimeout = time.Duration(50) * time.Millisecond + testElectionTimeout = time.Duration(200) * time.Millisecond ) // Starts a server in a temporary directory. @@ -26,10 +26,8 @@ func RunServer(f func(*server.Server)) { store := store.New() registry := server.NewRegistry(store) - ps := server.NewPeerServer(testName, path, "http://"+testRaftURL, testRaftURL, &server.TLSConfig{Scheme: "http"}, &server.TLSInfo{}, registry, store, testSnapshotCount) + ps := server.NewPeerServer(testName, path, "http://"+testRaftURL, testRaftURL, &server.TLSConfig{Scheme: "http"}, &server.TLSInfo{}, registry, store, testSnapshotCount, testHeartbeatTimeout, testElectionTimeout) ps.MaxClusterSize = 9 - ps.ElectionTimeout = testElectionTimeout - ps.HeartbeatTimeout = testHeartbeatTimeout s := server.New(testName, "http://"+testClientURL, testClientURL, &server.TLSConfig{Scheme: "http"}, &server.TLSInfo{}, ps, registry, store) ps.SetServer(s) From 48e36422b51a59c7ff9397954e29e4ae36eb60cd Mon Sep 17 00:00:00 2001 From: Brian Waldon Date: Tue, 14 Jan 2014 09:18:09 -0800 Subject: [PATCH 2/2] chore(gofmt): Run gofmt on server/config.go --- server/config.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/server/config.go b/server/config.go index 9fd2c119c..d007d5a9d 100644 --- a/server/config.go +++ b/server/config.go @@ -69,13 +69,13 @@ type Config struct { VeryVerbose bool `toml:"very_verbose" env:"ETCD_VERY_VERBOSE"` VeryVeryVerbose bool `toml:"very_very_verbose" env:"ETCD_VERY_VERY_VERBOSE"` Peer struct { - Addr string `toml:"addr" env:"ETCD_PEER_ADDR"` - BindAddr string `toml:"bind_addr" env:"ETCD_PEER_BIND_ADDR"` - CAFile string `toml:"ca_file" env:"ETCD_PEER_CA_FILE"` - CertFile string `toml:"cert_file" env:"ETCD_PEER_CERT_FILE"` - KeyFile string `toml:"key_file" env:"ETCD_PEER_KEY_FILE"` - HeartbeatTimeout int `toml:"heartbeat_timeout" env:"ETCD_PEER_HEARTBEAT_TIMEOUT"` - ElectionTimeout int `toml:"election_timeout" env:"ETCD_PEER_ELECTION_TIMEOUT"` + Addr string `toml:"addr" env:"ETCD_PEER_ADDR"` + BindAddr string `toml:"bind_addr" env:"ETCD_PEER_BIND_ADDR"` + CAFile string `toml:"ca_file" env:"ETCD_PEER_CA_FILE"` + CertFile string `toml:"cert_file" env:"ETCD_PEER_CERT_FILE"` + KeyFile string `toml:"key_file" env:"ETCD_PEER_KEY_FILE"` + HeartbeatTimeout int `toml:"heartbeat_timeout" env:"ETCD_PEER_HEARTBEAT_TIMEOUT"` + ElectionTimeout int `toml:"election_timeout" env:"ETCD_PEER_ELECTION_TIMEOUT"` } }