state: convert checks.service index to new pattern

This commit is contained in:
Daniel Nephin 2021-03-29 16:37:37 -04:00
parent f859ba6d4b
commit d785c86db1
4 changed files with 30 additions and 14 deletions

View File

@ -1638,8 +1638,11 @@ func (s *Store) ServiceChecks(ws memdb.WatchSet, serviceName string, entMeta *st
// Get the table index.
idx := catalogChecksMaxIndex(tx, entMeta)
// Return the checks.
iter, err := catalogListChecksByService(tx, serviceName, entMeta)
if entMeta == nil {
entMeta = structs.DefaultEnterpriseMeta()
}
q := Query{Value: serviceName, EnterpriseMeta: *entMeta}
iter, err := tx.Get(tableChecks, indexService, q)
if err != nil {
return 0, nil, fmt.Errorf("failed check lookup: %s", err)
}
@ -1663,8 +1666,12 @@ func (s *Store) ServiceChecksByNodeMeta(ws memdb.WatchSet, serviceName string,
// Get the table index.
idx := maxIndexForService(tx, serviceName, true, true, entMeta)
// Return the checks.
iter, err := catalogListChecksByService(tx, serviceName, entMeta)
if entMeta == nil {
entMeta = structs.DefaultEnterpriseMeta()
}
q := Query{Value: serviceName, EnterpriseMeta: *entMeta}
iter, err := tx.Get(tableChecks, indexService, q)
if err != nil {
return 0, nil, fmt.Errorf("failed check lookup: %s", err)
}

View File

@ -135,10 +135,6 @@ func catalogListChecksByNode(tx ReadTxn, q Query) (memdb.ResultIterator, error)
return tx.Get(tableChecks, indexNode, q)
}
func catalogListChecksByService(tx ReadTxn, service string, _ *structs.EnterpriseMeta) (memdb.ResultIterator, error) {
return tx.Get(tableChecks, indexService, service)
}
func catalogInsertCheck(tx WriteTxn, chk *structs.HealthCheck, idx uint64) error {
// Insert the check
if err := tx.Insert(tableChecks, chk); err != nil {

View File

@ -50,9 +50,7 @@ func testIndexerTableChecks() map[string]indexerTestCase {
},
indexService: {
read: indexValue{
source: []interface{}{
"ServiceName",
},
source: Query{Value: "ServiceName"},
expected: []byte("servicename\x00"),
},
write: indexValue{

View File

@ -267,9 +267,9 @@ func checksTableSchema() *memdb.TableSchema {
Name: indexService,
AllowMissing: true,
Unique: false,
Indexer: &memdb.StringFieldIndex{
Field: "ServiceName",
Lowercase: true,
Indexer: indexerSingle{
readIndex: indexFromQuery,
writeIndex: indexServiceNameFromHealthCheck,
},
},
indexNode: {
@ -357,6 +357,21 @@ func indexStatusFromHealthCheck(raw interface{}) ([]byte, error) {
return b.Bytes(), nil
}
func indexServiceNameFromHealthCheck(raw interface{}) ([]byte, error) {
hc, ok := raw.(*structs.HealthCheck)
if !ok {
return nil, fmt.Errorf("unexpected type %T for structs.HealthCheck index", raw)
}
if hc.ServiceName == "" {
return nil, errMissingValueForIndex
}
var b indexBuilder
b.String(strings.ToLower(hc.ServiceName))
return b.Bytes(), nil
}
// gatewayServicesTableSchema returns a new table schema used to store information
// about services associated with terminating gateways.
func gatewayServicesTableSchema() *memdb.TableSchema {