Enable Raft index optimization per service name on health endpoint

Had to fix unit test in order to check properly indexes.
This commit is contained in:
Pierre Souchay 2018-02-20 01:28:06 +01:00
parent d15f9d9f8c
commit a05d38737c
2 changed files with 22 additions and 9 deletions

View File

@ -762,13 +762,16 @@ func (s *Store) ServicesByNodeMeta(ws memdb.WatchSet, filters map[string]string)
// maxIndexForService return the maximum Raft Index for a service // maxIndexForService return the maximum Raft Index for a service
// If the index is not set for the service, it will return the max // If the index is not set for the service, it will return the max
// Raft Index of "nodes", "services" // 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)) transaction, err := tx.First("index", "id", serviceIndexName(serviceName))
if err == nil { if err == nil {
if idx, ok := transaction.(*IndexEntry); ok { if idx, ok := transaction.(*IndexEntry); ok {
return idx.Value, nil return idx.Value, nil
} }
} }
if checks {
return maxIndexTxn(tx, "nodes", "services", "checks"), nil
}
return maxIndexTxn(tx, "nodes", "services"), 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() defer tx.Abort()
// Get the table index. // Get the table index.
idx, err := maxIndexForService(tx, serviceName) idx, err := maxIndexForService(tx, serviceName, false)
if err != nil { if err != nil {
panic(fmt.Sprintf("Could not retrieve maxIndex for %s: %s", serviceName, err)) 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() defer tx.Abort()
// Get the table index. // Get the table index.
idx, err := maxIndexForService(tx, service) idx, err := maxIndexForService(tx, service, false)
if err != nil { if err != nil {
panic(fmt.Sprintf("Could not retrieve maxIndex for %s: %s", service, err)) 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() defer tx.Abort()
// Get the table index. // 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. // Return the checks.
iter, err := tx.Get("checks", "service", serviceName) iter, err := tx.Get("checks", "service", serviceName)
if err != nil { if err != nil {
@ -1443,7 +1448,10 @@ func (s *Store) CheckServiceNodes(ws memdb.WatchSet, serviceName string) (uint64
defer tx.Abort() defer tx.Abort()
// Get the table index. // 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. // Query the state store for the service.
iter, err := tx.Get("services", "service", serviceName) iter, err := tx.Get("services", "service", serviceName)
@ -1467,7 +1475,10 @@ func (s *Store) CheckServiceTagNodes(ws memdb.WatchSet, serviceName, tag string)
defer tx.Abort() defer tx.Abort()
// Get the table index. // 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. // Query the state store for the service.
iter, err := tx.Get("services", "service", serviceName) iter, err := tx.Get("services", "service", serviceName)

View File

@ -2304,7 +2304,8 @@ func TestStateStore_CheckServiceNodes(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("err: %s", err) 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) t.Fatalf("bad index: %d", idx)
} }
@ -2329,7 +2330,8 @@ func TestStateStore_CheckServiceNodes(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("err: %s", err) 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) t.Fatalf("bad index: %d", idx)
} }