From 3092c627fe37dfa97db6e811013ec97dda9c8869 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Mon, 29 Mar 2021 16:29:04 -0400 Subject: [PATCH] state: convert checks.status indexer As part of this change the indexer will now be case insensitive by using the lower case value. This should be safe because previously we always had lower case strings. This change was made out of convenience. All the other indexers use lowercase, so we can re-use the indexFromQuery function by using lowercase here as well. --- agent/consul/state/catalog.go | 7 ++++++- agent/consul/state/catalog_oss.go | 5 ----- agent/consul/state/catalog_oss_test.go | 8 +++----- agent/consul/state/catalog_schema.go | 21 ++++++++++++++++++--- 4 files changed, 27 insertions(+), 14 deletions(-) diff --git a/agent/consul/state/catalog.go b/agent/consul/state/catalog.go index d701649ca2..cff00dc4b4 100644 --- a/agent/consul/state/catalog.go +++ b/agent/consul/state/catalog.go @@ -1709,13 +1709,18 @@ func checksInStateTxn(tx ReadTxn, ws memdb.WatchSet, state string, entMeta *stru // Get the table index. idx := catalogChecksMaxIndex(tx, entMeta) + if entMeta == nil { + entMeta = structs.DefaultEnterpriseMeta() + } + // Query all checks if HealthAny is passed, otherwise use the index. var iter memdb.ResultIterator var err error if state == api.HealthAny { iter, err = tx.Get(tableChecks, indexID+"_prefix", entMeta) } else { - iter, err = catalogListChecksInState(tx, state, entMeta) + q := Query{Value: state, EnterpriseMeta: *entMeta} + iter, err = tx.Get(tableChecks, indexStatus, 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 98d37d3abd..1159bbbb64 100644 --- a/agent/consul/state/catalog_oss.go +++ b/agent/consul/state/catalog_oss.go @@ -139,11 +139,6 @@ func catalogListChecksByService(tx ReadTxn, service string, _ *structs.Enterpris return tx.Get(tableChecks, indexService, service) } -func catalogListChecksInState(tx ReadTxn, state string, _ *structs.EnterpriseMeta) (memdb.ResultIterator, error) { - // simpler than normal due to the use of the CompoundMultiIndex - return tx.Get(tableChecks, indexStatus, state) -} - 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 50a50d0492..3ab492a7f8 100644 --- a/agent/consul/state/catalog_oss_test.go +++ b/agent/consul/state/catalog_oss_test.go @@ -40,14 +40,12 @@ func testIndexerTableChecks() map[string]indexerTestCase { }, indexStatus: { read: indexValue{ - source: []interface{}{ - "PASSING", - }, - expected: []byte("PASSING\x00"), + source: Query{Value: "PASSING"}, + expected: []byte("passing\x00"), }, write: indexValue{ source: obj, - expected: []byte("PASSING\x00"), + expected: []byte("passing\x00"), }, }, indexService: { diff --git a/agent/consul/state/catalog_schema.go b/agent/consul/state/catalog_schema.go index e9915d9c9a..fa1be2c804 100644 --- a/agent/consul/state/catalog_schema.go +++ b/agent/consul/state/catalog_schema.go @@ -258,9 +258,9 @@ func checksTableSchema() *memdb.TableSchema { Name: indexStatus, AllowMissing: false, Unique: false, - Indexer: &memdb.StringFieldIndex{ - Field: "Status", - Lowercase: false, + Indexer: indexerSingle{ + readIndex: indexFromQuery, + writeIndex: indexStatusFromHealthCheck, }, }, indexService: { @@ -342,6 +342,21 @@ func indexNodeServiceFromHealthCheck(raw interface{}) ([]byte, error) { return b.Bytes(), nil } +func indexStatusFromHealthCheck(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.Status == "" { + return nil, errMissingValueForIndex + } + + var b indexBuilder + b.String(strings.ToLower(hc.Status)) + return b.Bytes(), nil +} + // gatewayServicesTableSchema returns a new table schema used to store information // about services associated with terminating gateways. func gatewayServicesTableSchema() *memdb.TableSchema {