From a05d38737c8501c8a28257400b0effc0190a5435 Mon Sep 17 00:00:00 2001 From: Pierre Souchay Date: Tue, 20 Feb 2018 01:28:06 +0100 Subject: [PATCH] Enable Raft index optimization per service name on health endpoint Had to fix unit test in order to check properly indexes. --- agent/consul/state/catalog.go | 25 ++++++++++++++++++------- agent/consul/state/catalog_test.go | 6 ++++-- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/agent/consul/state/catalog.go b/agent/consul/state/catalog.go index cd0ff2f0a1..07975f53c4 100644 --- a/agent/consul/state/catalog.go +++ b/agent/consul/state/catalog.go @@ -762,13 +762,16 @@ func (s *Store) ServicesByNodeMeta(ws memdb.WatchSet, filters map[string]string) // maxIndexForService return the maximum Raft Index for a service // If the index is not set for the service, it will return the max // Raft Index of "nodes", "services" -func maxIndexForService(tx *memdb.Txn, serviceName string) (uint64, error) { +func maxIndexForService(tx *memdb.Txn, serviceName string, checks bool) (uint64, error) { transaction, err := tx.First("index", "id", serviceIndexName(serviceName)) if err == nil { if idx, ok := transaction.(*IndexEntry); ok { return idx.Value, nil } } + if checks { + return maxIndexTxn(tx, "nodes", "services", "checks"), nil + } return maxIndexTxn(tx, "nodes", "services"), nil } @@ -778,7 +781,7 @@ func (s *Store) ServiceNodes(ws memdb.WatchSet, serviceName string) (uint64, str defer tx.Abort() // Get the table index. - idx, err := maxIndexForService(tx, serviceName) + idx, err := maxIndexForService(tx, serviceName, false) if err != nil { panic(fmt.Sprintf("Could not retrieve maxIndex for %s: %s", serviceName, err)) } @@ -809,7 +812,7 @@ func (s *Store) ServiceTagNodes(ws memdb.WatchSet, service string, tag string) ( defer tx.Abort() // Get the table index. - idx, err := maxIndexForService(tx, service) + idx, err := maxIndexForService(tx, service, false) if err != nil { panic(fmt.Sprintf("Could not retrieve maxIndex for %s: %s", service, err)) } @@ -1263,8 +1266,10 @@ func (s *Store) ServiceChecksByNodeMeta(ws memdb.WatchSet, serviceName string, defer tx.Abort() // Get the table index. - idx := maxIndexTxn(tx, "nodes", "checks") - + idx, err := maxIndexForService(tx, serviceName, true) + if err != nil { + panic(fmt.Sprintf("Could not retrieve maxIndex for %s: %s", serviceName, err)) + } // Return the checks. iter, err := tx.Get("checks", "service", serviceName) if err != nil { @@ -1443,7 +1448,10 @@ func (s *Store) CheckServiceNodes(ws memdb.WatchSet, serviceName string) (uint64 defer tx.Abort() // Get the table index. - idx := maxIndexTxn(tx, "nodes", "services", "checks") + idx, err := maxIndexForService(tx, serviceName, true) + if err != nil { + panic(fmt.Sprintf("Could not retrieve maxIndex for %s: %s", serviceName, err)) + } // Query the state store for the service. iter, err := tx.Get("services", "service", serviceName) @@ -1467,7 +1475,10 @@ func (s *Store) CheckServiceTagNodes(ws memdb.WatchSet, serviceName, tag string) defer tx.Abort() // Get the table index. - idx := maxIndexTxn(tx, "nodes", "services", "checks") + idx, err := maxIndexForService(tx, serviceName, true) + if err != nil { + panic(fmt.Sprintf("Could not retrieve maxIndex for %s: %s", serviceName, err)) + } // Query the state store for the service. iter, err := tx.Get("services", "service", serviceName) diff --git a/agent/consul/state/catalog_test.go b/agent/consul/state/catalog_test.go index 89745389ec..bad625b496 100644 --- a/agent/consul/state/catalog_test.go +++ b/agent/consul/state/catalog_test.go @@ -2304,7 +2304,8 @@ func TestStateStore_CheckServiceNodes(t *testing.T) { if err != nil { t.Fatalf("err: %s", err) } - if idx != 7 { + // registered with ensureServiceVersion(t, s, ws, "service1", 6, 1) + if idx != 6 { t.Fatalf("bad index: %d", idx) } @@ -2329,7 +2330,8 @@ func TestStateStore_CheckServiceNodes(t *testing.T) { if err != nil { t.Fatalf("err: %s", err) } - if idx != 8 { + // service1 has been registered at idx=6, other different registrations do not count + if idx != 6 { t.Fatalf("bad index: %d", idx) }