From 97ae531eda997eeb74991e09b965244bc65936b1 Mon Sep 17 00:00:00 2001 From: Jonathan Boulle Date: Sat, 11 Oct 2014 13:33:11 -0700 Subject: [PATCH] etcdserver: split out storestats and serverstats --- etcdserver/etcdhttp/http.go | 9 +++++---- etcdserver/etcdhttp/http_test.go | 14 +++++++++----- etcdserver/server.go | 16 ++++++++++------ 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/etcdserver/etcdhttp/http.go b/etcdserver/etcdhttp/http.go index f2347be83..a3ae9a61e 100644 --- a/etcdserver/etcdhttp/http.go +++ b/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) diff --git a/etcdserver/etcdhttp/http_test.go b/etcdserver/etcdhttp/http_test.go index df7b04a13..117e8249f 100644 --- a/etcdserver/etcdhttp/http_test.go +++ b/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) diff --git a/etcdserver/server.go b/etcdserver/server.go index 9100badf8..3dafd31f8 100644 --- a/etcdserver/server.go +++ b/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()