Browse Source

etcdserver: do not send v2 sync if ttl keys do not exist

Xiang Li 9 years ago
parent
commit
2f96a68a20
3 changed files with 18 additions and 1 deletions
  1. 3 1
      etcdserver/server.go
  2. 7 0
      pkg/mock/mockstore/store_recorder.go
  3. 8 0
      store/store.go

+ 3 - 1
etcdserver/server.go

@@ -733,7 +733,9 @@ func (s *EtcdServer) run() {
 			plog.Infof("the data-dir used by this member must be removed.")
 			return
 		case <-getSyncC():
-			s.sync(s.Cfg.ReqTimeout())
+			if s.store.HasTTLKeys() {
+				s.sync(s.Cfg.ReqTimeout())
+			}
 		case <-s.stop:
 			return
 		}

+ 7 - 0
pkg/mock/mockstore/store_recorder.go

@@ -127,6 +127,13 @@ func (s *storeRecorder) DeleteExpiredKeys(cutoff time.Time) {
 	})
 }
 
+func (s *storeRecorder) HasTTLKeys() bool {
+	s.Record(testutil.Action{
+		Name: "HasTTLKeys",
+	})
+	return true
+}
+
 // errStoreRecorder is a storeRecorder, but returns the given error on
 // Get, Watch methods.
 type errStoreRecorder struct {

+ 8 - 0
store/store.go

@@ -61,6 +61,8 @@ type Store interface {
 
 	JsonStats() []byte
 	DeleteExpiredKeys(cutoff time.Time)
+
+	HasTTLKeys() bool
 }
 
 type TTLOptionSet struct {
@@ -778,3 +780,9 @@ func (s *store) JsonStats() []byte {
 	s.Stats.Watchers = uint64(s.WatcherHub.count)
 	return s.Stats.toJson()
 }
+
+func (s *store) HasTTLKeys() bool {
+	s.worldLock.RLock()
+	defer s.worldLock.RUnlock()
+	return s.ttlKeyHeap.Len() != 0
+}