Browse Source

basic stats

Xiang Li 12 years ago
parent
commit
a97590ff50
9 changed files with 54 additions and 28 deletions
  1. 6 3
      command.go
  2. 3 2
      etcd.go
  3. 5 3
      etcd_handlers.go
  4. 3 2
      etcd_test.go
  5. 2 1
      raft_handlers.go
  6. 17 14
      raft_server.go
  7. 2 1
      store/store.go
  8. 14 1
      transporter.go
  9. 2 1
      util.go

+ 6 - 3
command.go

@@ -4,12 +4,13 @@ import (
 	"encoding/binary"
 	"encoding/binary"
 	"encoding/json"
 	"encoding/json"
 	"fmt"
 	"fmt"
-	etcdErr "github.com/coreos/etcd/error"
-	"github.com/coreos/etcd/store"
-	"github.com/coreos/go-raft"
 	"os"
 	"os"
 	"path"
 	"path"
 	"time"
 	"time"
+
+	etcdErr "github.com/coreos/etcd/error"
+	"github.com/coreos/etcd/store"
+	"github.com/coreos/go-raft"
 )
 )
 
 
 const commandPrefix = "etcd:"
 const commandPrefix = "etcd:"
@@ -168,6 +169,7 @@ func (c *JoinCommand) Apply(raftServer *raft.Server) (interface{}, error) {
 	key := path.Join("_etcd/machines", c.Name)
 	key := path.Join("_etcd/machines", c.Name)
 	value := fmt.Sprintf("raft=%s&etcd=%s&raftVersion=%s", c.RaftURL, c.EtcdURL, c.RaftVersion)
 	value := fmt.Sprintf("raft=%s&etcd=%s&raftVersion=%s", c.RaftURL, c.EtcdURL, c.RaftVersion)
 	etcdStore.Set(key, value, time.Unix(0, 0), raftServer.CommitIndex())
 	etcdStore.Set(key, value, time.Unix(0, 0), raftServer.CommitIndex())
+	r.peersStats[c.Name] = &peerStats{}
 
 
 	return b, err
 	return b, err
 }
 }
@@ -193,6 +195,7 @@ func (c *RemoveCommand) Apply(raftServer *raft.Server) (interface{}, error) {
 	key := path.Join("_etcd/machines", c.Name)
 	key := path.Join("_etcd/machines", c.Name)
 
 
 	_, err := etcdStore.Delete(key, raftServer.CommitIndex())
 	_, err := etcdStore.Delete(key, raftServer.CommitIndex())
+	r.peersStats[c.Name] = nil
 
 
 	if err != nil {
 	if err != nil {
 		return []byte{0}, err
 		return []byte{0}, err

+ 3 - 2
etcd.go

@@ -3,12 +3,13 @@ package main
 import (
 import (
 	"crypto/tls"
 	"crypto/tls"
 	"flag"
 	"flag"
-	"github.com/coreos/etcd/store"
-	"github.com/coreos/go-raft"
 	"io/ioutil"
 	"io/ioutil"
 	"os"
 	"os"
 	"strings"
 	"strings"
 	"time"
 	"time"
+
+	"github.com/coreos/etcd/store"
+	"github.com/coreos/go-raft"
 )
 )
 
 
 //------------------------------------------------------------------------------
 //------------------------------------------------------------------------------

+ 5 - 3
etcd_handlers.go

@@ -2,12 +2,13 @@ package main
 
 
 import (
 import (
 	"fmt"
 	"fmt"
-	etcdErr "github.com/coreos/etcd/error"
-	"github.com/coreos/etcd/store"
-	"github.com/coreos/go-raft"
 	"net/http"
 	"net/http"
 	"strconv"
 	"strconv"
 	"strings"
 	"strings"
+
+	etcdErr "github.com/coreos/etcd/error"
+	"github.com/coreos/etcd/store"
+	"github.com/coreos/go-raft"
 )
 )
 
 
 //-------------------------------------------------------------------
 //-------------------------------------------------------------------
@@ -207,6 +208,7 @@ func VersionHttpHandler(w http.ResponseWriter, req *http.Request) error {
 func StatsHttpHandler(w http.ResponseWriter, req *http.Request) error {
 func StatsHttpHandler(w http.ResponseWriter, req *http.Request) error {
 	w.WriteHeader(http.StatusOK)
 	w.WriteHeader(http.StatusOK)
 	w.Write(etcdStore.Stats())
 	w.Write(etcdStore.Stats())
+	w.Write(r.Stats())
 	return nil
 	return nil
 }
 }
 
 

+ 3 - 2
etcd_test.go

@@ -2,8 +2,6 @@ package main
 
 
 import (
 import (
 	"fmt"
 	"fmt"
-	"github.com/coreos/etcd/test"
-	"github.com/coreos/go-etcd/etcd"
 	"math/rand"
 	"math/rand"
 	"net/http"
 	"net/http"
 	"net/http/httptest"
 	"net/http/httptest"
@@ -13,6 +11,9 @@ import (
 	"strings"
 	"strings"
 	"testing"
 	"testing"
 	"time"
 	"time"
+
+	"github.com/coreos/etcd/test"
+	"github.com/coreos/go-etcd/etcd"
 )
 )
 
 
 // Create a single node and try to set value
 // Create a single node and try to set value

+ 2 - 1
raft_handlers.go

@@ -2,8 +2,9 @@ package main
 
 
 import (
 import (
 	"encoding/json"
 	"encoding/json"
-	"github.com/coreos/go-raft"
 	"net/http"
 	"net/http"
+
+	"github.com/coreos/go-raft"
 )
 )
 
 
 //-------------------------------------------------------------
 //-------------------------------------------------------------

+ 17 - 14
raft_server.go

@@ -6,22 +6,24 @@ import (
 	"encoding/binary"
 	"encoding/binary"
 	"encoding/json"
 	"encoding/json"
 	"fmt"
 	"fmt"
-	etcdErr "github.com/coreos/etcd/error"
-	"github.com/coreos/go-raft"
 	"io/ioutil"
 	"io/ioutil"
 	"net/http"
 	"net/http"
 	"net/url"
 	"net/url"
 	"time"
 	"time"
+
+	etcdErr "github.com/coreos/etcd/error"
+	"github.com/coreos/go-raft"
 )
 )
 
 
 type raftServer struct {
 type raftServer struct {
 	*raft.Server
 	*raft.Server
-	version   string
-	joinIndex uint64
-	name      string
-	url       string
-	tlsConf   *TLSConfig
-	tlsInfo   *TLSInfo
+	version    string
+	joinIndex  uint64
+	name       string
+	url        string
+	tlsConf    *TLSConfig
+	tlsInfo    *TLSInfo
+	peersStats map[string]*peerStats
 }
 }
 
 
 var r *raftServer
 var r *raftServer
@@ -37,12 +39,13 @@ func newRaftServer(name string, url string, tlsConf *TLSConfig, tlsInfo *TLSInfo
 	check(err)
 	check(err)
 
 
 	return &raftServer{
 	return &raftServer{
-		Server:  server,
-		version: raftVersion,
-		name:    name,
-		url:     url,
-		tlsConf: tlsConf,
-		tlsInfo: tlsInfo,
+		Server:     server,
+		version:    raftVersion,
+		name:       name,
+		url:        url,
+		tlsConf:    tlsConf,
+		tlsInfo:    tlsInfo,
+		peersStats: make(map[string]*peerStats),
 	}
 	}
 }
 }
 
 

+ 2 - 1
store/store.go

@@ -3,11 +3,12 @@ package store
 import (
 import (
 	"encoding/json"
 	"encoding/json"
 	"fmt"
 	"fmt"
-	etcdErr "github.com/coreos/etcd/error"
 	"path"
 	"path"
 	"strconv"
 	"strconv"
 	"sync"
 	"sync"
 	"time"
 	"time"
+
+	etcdErr "github.com/coreos/etcd/error"
 )
 )
 
 
 //------------------------------------------------------------------------------
 //------------------------------------------------------------------------------

+ 14 - 1
transporter.go

@@ -5,10 +5,12 @@ import (
 	"crypto/tls"
 	"crypto/tls"
 	"encoding/json"
 	"encoding/json"
 	"fmt"
 	"fmt"
-	"github.com/coreos/go-raft"
 	"io"
 	"io"
 	"net"
 	"net"
 	"net/http"
 	"net/http"
+	"time"
+
+	"github.com/coreos/go-raft"
 )
 )
 
 
 // Transporter layer for communication between raft nodes
 // Transporter layer for communication between raft nodes
@@ -50,12 +52,23 @@ func (t transporter) SendAppendEntriesRequest(server *raft.Server, peer *raft.Pe
 	u, _ := nameToRaftURL(peer.Name)
 	u, _ := nameToRaftURL(peer.Name)
 	debugf("Send LogEntries to %s ", u)
 	debugf("Send LogEntries to %s ", u)
 
 
+	thisPeerStats := r.peersStats[peer.Name]
+
+	start := time.Now()
+
 	resp, err := t.Post(fmt.Sprintf("%s/log/append", u), &b)
 	resp, err := t.Post(fmt.Sprintf("%s/log/append", u), &b)
 
 
+	end := time.Now()
+
 	if err != nil {
 	if err != nil {
 		debugf("Cannot send AppendEntriesRequest to %s: %s", u, err)
 		debugf("Cannot send AppendEntriesRequest to %s: %s", u, err)
+		thisPeerStats.Failcnt++
+	} else {
+		thisPeerStats.Latency = float64(end.Sub(start)) / (1000000.0)
 	}
 	}
 
 
+	r.peersStats[peer.Name] = thisPeerStats
+
 	if resp != nil {
 	if resp != nil {
 		defer resp.Body.Close()
 		defer resp.Body.Close()
 		aersp = &raft.AppendEntriesResponse{}
 		aersp = &raft.AppendEntriesResponse{}

+ 2 - 1
util.go

@@ -3,7 +3,6 @@ package main
 import (
 import (
 	"encoding/json"
 	"encoding/json"
 	"fmt"
 	"fmt"
-	"github.com/coreos/etcd/web"
 	"io"
 	"io"
 	"log"
 	"log"
 	"net"
 	"net"
@@ -14,6 +13,8 @@ import (
 	"runtime/pprof"
 	"runtime/pprof"
 	"strconv"
 	"strconv"
 	"time"
 	"time"
+
+	"github.com/coreos/etcd/web"
 )
 )
 
 
 //--------------------------------------
 //--------------------------------------