Browse Source

support version and basic stats

Xiang Li 12 years ago
parent
commit
0ebd133a0d
4 changed files with 32 additions and 0 deletions
  1. 12 0
      client_handlers.go
  2. 2 0
      etcd.go
  3. 16 0
      store/store.go
  4. 2 0
      version.go

+ 12 - 0
client_handlers.go

@@ -225,6 +225,18 @@ func MachinesHttpHandler(w http.ResponseWriter, req *http.Request) {
 
 
 }
 }
 
 
+// Handler to return the current version of etcd
+func VersionHttpHandler(w http.ResponseWriter, req *http.Request) {
+	w.WriteHeader(http.StatusOK)
+	w.Write([]byte(releaseVersion))
+}
+
+// Handler to return the basic stats of etcd
+func StatsHttpHandler(w http.ResponseWriter, req *http.Request) {
+	w.WriteHeader(http.StatusOK)
+	w.Write(etcdStore.Stats())
+}
+
 // Get Handler
 // Get Handler
 func GetHttpHandler(w *http.ResponseWriter, req *http.Request) {
 func GetHttpHandler(w *http.ResponseWriter, req *http.Request) {
 	key := req.URL.Path[len("/v1/keys/"):]
 	key := req.URL.Path[len("/v1/keys/"):]

+ 2 - 0
etcd.go

@@ -434,6 +434,8 @@ func startClientTransport(port int, st int) {
 	http.HandleFunc("/"+version+"/watch/", WatchHttpHandler)
 	http.HandleFunc("/"+version+"/watch/", WatchHttpHandler)
 	http.HandleFunc("/leader", LeaderHttpHandler)
 	http.HandleFunc("/leader", LeaderHttpHandler)
 	http.HandleFunc("/machines", MachinesHttpHandler)
 	http.HandleFunc("/machines", MachinesHttpHandler)
+	http.HandleFunc("/", VersionHttpHandler)
+	http.HandleFunc("/stats", StatsHttpHandler)
 
 
 	switch st {
 	switch st {
 
 

+ 16 - 0
store/store.go

@@ -42,6 +42,9 @@ type Store struct {
 
 
 	// Current index of the raft machine
 	// Current index of the raft machine
 	Index uint64
 	Index uint64
+
+	// Basic statistics information of etcd storage
+	BasicStats EtcdStats
 }
 }
 
 
 // A Node represents a Value in the Key-Value pair in the store
 // A Node represents a Value in the Key-Value pair in the store
@@ -139,6 +142,9 @@ func (s *Store) Set(key string, value string, expireTime time.Time, index uint64
 	//Update index
 	//Update index
 	s.Index = index
 	s.Index = index
 
 
+	//Update stats
+	s.BasicStats.Sets++
+
 	key = path.Clean("/" + key)
 	key = path.Clean("/" + key)
 
 
 	isExpire := !expireTime.Equal(PERMANENT)
 	isExpire := !expireTime.Equal(PERMANENT)
@@ -285,6 +291,9 @@ func (s *Store) internalGet(key string) *Response {
 // If key is a directory reuturn an array of files
 // If key is a directory reuturn an array of files
 func (s *Store) Get(key string) ([]byte, error) {
 func (s *Store) Get(key string) ([]byte, error) {
 
 
+	//Update stats
+	s.BasicStats.Gets++
+
 	key = path.Clean("/" + key)
 	key = path.Clean("/" + key)
 
 
 	nodes, keys, dirs, ok := s.Tree.list(key)
 	nodes, keys, dirs, ok := s.Tree.list(key)
@@ -331,6 +340,9 @@ func (s *Store) Get(key string) ([]byte, error) {
 // Delete the key
 // Delete the key
 func (s *Store) Delete(key string, index uint64) ([]byte, error) {
 func (s *Store) Delete(key string, index uint64) ([]byte, error) {
 
 
+	//Update stats
+	s.BasicStats.Deletes++
+
 	key = path.Clean("/" + key)
 	key = path.Clean("/" + key)
 
 
 	//Update index
 	//Update index
@@ -381,6 +393,9 @@ func (s *Store) Delete(key string, index uint64) ([]byte, error) {
 
 
 // Set the value of the key to the value if the given prevValue is equal to the value of the key
 // Set the value of the key to the value if the given prevValue is equal to the value of the key
 func (s *Store) TestAndSet(key string, prevValue string, value string, expireTime time.Time, index uint64) ([]byte, error) {
 func (s *Store) TestAndSet(key string, prevValue string, value string, expireTime time.Time, index uint64) ([]byte, error) {
+	//Update stats
+	s.BasicStats.TestAndSets++
+
 	resp := s.internalGet(key)
 	resp := s.internalGet(key)
 
 
 	if resp == nil {
 	if resp == nil {
@@ -540,3 +555,4 @@ func (s *Store) checkNode(key string, node *Node) {
 		}
 		}
 	}
 	}
 }
 }
+

+ 2 - 0
version.go

@@ -1,3 +1,5 @@
 package main
 package main
 
 
 var version = "v1"
 var version = "v1"
+
+var releaseVersion = "etcd <0.1>"