Browse Source

Merge pull request #624 from unihorn/36

chore(server/transporter): set RequestTimout reasonable
Yicheng Qin 11 years ago
parent
commit
b0ac8a4b4b
3 changed files with 17 additions and 10 deletions
  1. 5 0
      config/config.go
  2. 8 2
      etcd/etcd.go
  3. 4 8
      server/transporter.go

+ 5 - 0
config/config.go

@@ -4,6 +4,7 @@ import (
 	"flag"
 	"flag"
 	"fmt"
 	"fmt"
 	"io/ioutil"
 	"io/ioutil"
+	"math/rand"
 	"net"
 	"net"
 	"net/url"
 	"net/url"
 	"os"
 	"os"
@@ -11,6 +12,7 @@ import (
 	"reflect"
 	"reflect"
 	"strconv"
 	"strconv"
 	"strings"
 	"strings"
+	"time"
 
 
 	"github.com/coreos/etcd/third_party/github.com/BurntSushi/toml"
 	"github.com/coreos/etcd/third_party/github.com/BurntSushi/toml"
 
 
@@ -98,6 +100,9 @@ func New() *Config {
 	c.Peer.Addr = "127.0.0.1:7001"
 	c.Peer.Addr = "127.0.0.1:7001"
 	c.Peer.HeartbeatInterval = defaultHeartbeatInterval
 	c.Peer.HeartbeatInterval = defaultHeartbeatInterval
 	c.Peer.ElectionTimeout = defaultElectionTimeout
 	c.Peer.ElectionTimeout = defaultElectionTimeout
+	rand.Seed(time.Now().UTC().UnixNano())
+	// Make maximum twice as minimum.
+	c.RetryInterval = float64(50+rand.Int()%50) * defaultHeartbeatInterval / 1000
 	return c
 	return c
 }
 }
 
 

+ 8 - 2
etcd/etcd.go

@@ -134,8 +134,14 @@ func (e *Etcd) Run() {
 	// Calculate all of our timeouts
 	// Calculate all of our timeouts
 	heartbeatInterval := time.Duration(e.Config.Peer.HeartbeatInterval) * time.Millisecond
 	heartbeatInterval := time.Duration(e.Config.Peer.HeartbeatInterval) * time.Millisecond
 	electionTimeout := time.Duration(e.Config.Peer.ElectionTimeout) * time.Millisecond
 	electionTimeout := time.Duration(e.Config.Peer.ElectionTimeout) * time.Millisecond
-	dialTimeout := (3 * heartbeatInterval) + electionTimeout
-	responseHeaderTimeout := (3 * heartbeatInterval) + electionTimeout
+	// TODO(yichengq): constant 1000 is a hack here. The reason to use this
+	// is to ensure etcd instances could start successfully at the same time.
+	// Current problem for the failure comes from the lag between join command
+	// execution and join success.
+	// Fix it later. It should be removed when proper method is found and
+	// enough tests are provided.
+	dialTimeout := (3 * heartbeatInterval) + electionTimeout + 1000
+	responseHeaderTimeout := (3 * heartbeatInterval) + electionTimeout + 1000
 
 
 	// Create peer server
 	// Create peer server
 	psConfig := server.PeerServerConfig{
 	psConfig := server.PeerServerConfig{

+ 4 - 8
server/transporter.go

@@ -20,7 +20,6 @@ const (
 
 
 // Transporter layer for communication between raft nodes
 // Transporter layer for communication between raft nodes
 type transporter struct {
 type transporter struct {
-	requestTimeout time.Duration
 	followersStats *raftFollowersStats
 	followersStats *raftFollowersStats
 	serverStats    *raftServerStats
 	serverStats    *raftServerStats
 	registry       *Registry
 	registry       *Registry
@@ -43,9 +42,8 @@ func NewTransporter(followersStats *raftFollowersStats, serverStats *raftServerS
 		// HTTPS connections blocked. The patch for it is in progress,
 		// HTTPS connections blocked. The patch for it is in progress,
 		// and would be available in Go1.3
 		// and would be available in Go1.3
 		// More: https://codereview.appspot.com/69280043/
 		// More: https://codereview.appspot.com/69280043/
-		ConnectTimeout:   dialTimeout,
-		RequestTimeout:   dialTimeout + responseHeaderTimeout,
-		ReadWriteTimeout: responseHeaderTimeout,
+		ConnectTimeout: dialTimeout,
+		RequestTimeout: requestTimeout,
 	}
 	}
 
 
 	// Sending snapshot might take a long time so we use a different HTTP transporter
 	// Sending snapshot might take a long time so we use a different HTTP transporter
@@ -55,9 +53,8 @@ func NewTransporter(followersStats *raftFollowersStats, serverStats *raftServerS
 	// average RTT.
 	// average RTT.
 	// It should be equal to (TCP max window size/RTT).
 	// It should be equal to (TCP max window size/RTT).
 	sTr := &httpclient.Transport{
 	sTr := &httpclient.Transport{
-		ConnectTimeout:   dialTimeout,
-		RequestTimeout:   snapshotTimeout,
-		ReadWriteTimeout: snapshotTimeout,
+		ConnectTimeout: dialTimeout,
+		RequestTimeout: snapshotTimeout,
 	}
 	}
 
 
 	t := transporter{
 	t := transporter{
@@ -65,7 +62,6 @@ func NewTransporter(followersStats *raftFollowersStats, serverStats *raftServerS
 		transport:         tr,
 		transport:         tr,
 		snapshotClient:    &http.Client{Transport: sTr},
 		snapshotClient:    &http.Client{Transport: sTr},
 		snapshotTransport: sTr,
 		snapshotTransport: sTr,
-		requestTimeout:    requestTimeout,
 		followersStats:    followersStats,
 		followersStats:    followersStats,
 		serverStats:       serverStats,
 		serverStats:       serverStats,
 		registry:          registry,
 		registry:          registry,