Browse Source

etcdserver: split out storestats and serverstats

Jonathan Boulle 11 years ago
parent
commit
97ae531eda
3 changed files with 24 additions and 15 deletions
  1. 5 4
      etcdserver/etcdhttp/http.go
  2. 9 5
      etcdserver/etcdhttp/http_test.go
  3. 10 6
      etcdserver/server.go

+ 5 - 4
etcdserver/etcdhttp/http.go

@@ -75,7 +75,8 @@ func NewPeerHandler(server etcdserver.Server) http.Handler {
 type serverHandler struct {
 	timeout      time.Duration
 	server       etcdserver.Server
-	stats        etcdserver.Stats
+	stats        etcdserver.ServerStats
+	storestats   etcdserver.StoreStats
 	timer        etcdserver.RaftTimer
 	clusterStore etcdserver.ClusterStore
 }
@@ -173,14 +174,14 @@ func (h serverHandler) serveStoreStats(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 	w.Header().Set("Content-Type", "application/json")
-	w.Write(h.stats.StoreStats())
+	w.Write(h.storestats.JSON())
 }
 
 func (h serverHandler) serveSelfStats(w http.ResponseWriter, r *http.Request) {
 	if !allowMethod(w, r.Method, "GET") {
 		return
 	}
-	s := h.stats.ServerStats()
+	s := h.stats.SelfStats()
 	b, err := json.Marshal(s)
 	if err != nil {
 		log.Printf("error marshalling stats: %v\n", err)
@@ -226,7 +227,7 @@ func (h serverHandler) serveRaft(w http.ResponseWriter, r *http.Request) {
 	log.Printf("etcdhttp: raft recv message from %#x: %+v", m.From, m)
 	if m.Type == raftpb.MsgApp {
 		// TODO(jonboulle): standardize id uint-->string process: always base 16?
-		h.stats.ServerStats().RecvAppendReq(strconv.FormatUint(m.From, 16), int(r.ContentLength))
+		h.stats.SelfStats().RecvAppendReq(strconv.FormatUint(m.From, 16), int(r.ContentLength))
 	}
 	if err := h.server.Process(context.TODO(), m); err != nil {
 		log.Println("etcdhttp: error processing raft message:", err)

+ 9 - 5
etcdserver/etcdhttp/http_test.go

@@ -638,18 +638,22 @@ func TestServeMachines(t *testing.T) {
 	}
 }
 
-type ds struct {
+type dummyServerStats struct {
+}
+
+func (dss *dummyServerStats) SelfStats() *stats.ServerStats   { return nil }
+func (dss *dummyServerStats) LeaderStats() *stats.LeaderStats { return nil }
+
+type dummyStoreStats struct {
 	data []byte
 }
 
-func (s *ds) ServerStats() *stats.ServerStats { return nil }
-func (s *ds) LeaderStats() *stats.LeaderStats { return nil }
-func (s *ds) StoreStats() []byte              { return s.data }
+func (dss *dummyStoreStats) JSON() []byte { return dss.data }
 
 func TestServeStoreStats(t *testing.T) {
 	w := "foobarbaz"
 	sh := &serverHandler{
-		stats: &ds{data: []byte(w)},
+		storestats: &dummyStoreStats{data: []byte(w)},
 	}
 	rw := httptest.NewRecorder()
 	req, err := http.NewRequest("GET", "", nil)

+ 10 - 6
etcdserver/server.go

@@ -90,14 +90,18 @@ type Server interface {
 	RemoveMember(ctx context.Context, id uint64) error
 }
 
-type Stats interface {
-	// ServerStats returns the statistics of this server
-	ServerStats() *stats.ServerStats
+type ServerStats interface {
+	// SelfStats returns the statistics of this server
+	SelfStats() *stats.ServerStats
 	// LeaderStats returns the statistics of all followers in the cluster
 	// if this server is leader. Otherwise, nil is returned.
 	LeaderStats() *stats.LeaderStats
-	// StoreStats returns statistics of the underlying Store used by the etcdserver
-	StoreStats() []byte
+}
+
+type StoreStats interface {
+	// JSON returns statistics of the underlying Store used by the
+	// EtcdServer, in JSON format
+	JSON() []byte
 }
 
 type RaftTimer interface {
@@ -359,7 +363,7 @@ func (s *EtcdServer) Do(ctx context.Context, r pb.Request) (Response, error) {
 	}
 }
 
-func (s *EtcdServer) ServerStats() *stats.ServerStats {
+func (s *EtcdServer) SelfStats() *stats.ServerStats {
 	s.stats.LeaderInfo.Uptime = time.Now().Sub(s.stats.LeaderInfo.StartTime).String()
 	s.stats.SendingPkgRate, s.stats.SendingBandwidthRate = s.stats.SendRates()
 	s.stats.RecvingPkgRate, s.stats.RecvingBandwidthRate = s.stats.RecvRates()