diff --git a/agent/consul/state/catalog.go b/agent/consul/state/catalog.go index cff00dc4b..c66d9de40 100644 --- a/agent/consul/state/catalog.go +++ b/agent/consul/state/catalog.go @@ -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) } diff --git a/agent/consul/state/catalog_oss.go b/agent/consul/state/catalog_oss.go index 1159bbbb6..842e0f397 100644 --- a/agent/consul/state/catalog_oss.go +++ b/agent/consul/state/catalog_oss.go @@ -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 { diff --git a/agent/consul/state/catalog_oss_test.go b/agent/consul/state/catalog_oss_test.go index 3ab492a7f..70d9be30c 100644 --- a/agent/consul/state/catalog_oss_test.go +++ b/agent/consul/state/catalog_oss_test.go @@ -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{ diff --git a/agent/consul/state/catalog_schema.go b/agent/consul/state/catalog_schema.go index fa1be2c80..565b4bf1b 100644 --- a/agent/consul/state/catalog_schema.go +++ b/agent/consul/state/catalog_schema.go @@ -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 {