From aadf187094bbb7f131e681a03df58bcdbdd193a6 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Wed, 10 Mar 2021 13:37:17 -0500 Subject: [PATCH] state: remove config-entries namespace index Use a prefix of the ID index instead. --- agent/consul/state/config_entry_oss.go | 4 ++-- agent/consul/state/config_entry_schema.go | 7 ++++--- agent/consul/state/indexer.go | 18 ++++++++++++++++++ .../state/testdata/TestStateStoreSchema.golden | 2 +- 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/agent/consul/state/config_entry_oss.go b/agent/consul/state/config_entry_oss.go index 9ef1d374b2..e265d09058 100644 --- a/agent/consul/state/config_entry_oss.go +++ b/agent/consul/state/config_entry_oss.go @@ -44,11 +44,11 @@ func validateConfigEntryEnterprise(_ ReadTxn, _ structs.ConfigEntry) error { } func getAllConfigEntriesWithTxn(tx ReadTxn, _ *structs.EnterpriseMeta) (memdb.ResultIterator, error) { - return tx.Get(tableConfigEntries, "id") + return tx.Get(tableConfigEntries, indexID) } func getConfigEntryKindsWithTxn(tx ReadTxn, kind string, _ *structs.EnterpriseMeta) (memdb.ResultIterator, error) { - return tx.Get(tableConfigEntries, "kind", kind) + return tx.Get(tableConfigEntries, indexKind, kind) } func configIntentionsConvertToList(iter memdb.ResultIterator, _ *structs.EnterpriseMeta) structs.Intentions { diff --git a/agent/consul/state/config_entry_schema.go b/agent/consul/state/config_entry_schema.go index 11ad01e4d1..746b659f3e 100644 --- a/agent/consul/state/config_entry_schema.go +++ b/agent/consul/state/config_entry_schema.go @@ -22,9 +22,10 @@ func configTableSchema() *memdb.TableSchema { Name: indexID, AllowMissing: false, Unique: true, - Indexer: indexerSingle{ - readIndex: readIndex(indexFromConfigEntryKindName), - writeIndex: writeIndex(indexFromConfigEntry), + Indexer: indexerSingleWithPrefix{ + readIndex: readIndex(indexFromConfigEntryKindName), + writeIndex: writeIndex(indexFromConfigEntry), + prefixIndex: prefixIndex(indexFromConfigEntryKindName), }, }, indexKind: { diff --git a/agent/consul/state/indexer.go b/agent/consul/state/indexer.go index dc91e2af8d..22474fc82f 100644 --- a/agent/consul/state/indexer.go +++ b/agent/consul/state/indexer.go @@ -30,6 +30,13 @@ type indexerMulti struct { writeIndexMulti } +// indexerSingleWithPrefix is a indexerSingle which also supports prefix queries. +type indexerSingleWithPrefix struct { + readIndex + writeIndex + prefixIndex +} + // readIndex implements memdb.Indexer. It exists so that a function can be used // to provide the interface. // @@ -78,6 +85,17 @@ func (f writeIndexMulti) FromObject(raw interface{}) (bool, [][]byte, error) { return err == nil, v, err } +// prefixIndex implements memdb.PrefixIndexer. It exists so that a function +// can be used to provide this interface. +type prefixIndex func(args interface{}) ([]byte, error) + +func (f prefixIndex) PrefixFromArgs(args ...interface{}) ([]byte, error) { + if len(args) != 1 { + return nil, fmt.Errorf("index supports only a single arg") + } + return f(args[0]) +} + const null = "\x00" // indexBuilder is a buffer used to construct memdb index values. diff --git a/agent/consul/state/testdata/TestStateStoreSchema.golden b/agent/consul/state/testdata/TestStateStoreSchema.golden index 83f59d8b61..2ef93dfcf7 100644 --- a/agent/consul/state/testdata/TestStateStoreSchema.golden +++ b/agent/consul/state/testdata/TestStateStoreSchema.golden @@ -60,7 +60,7 @@ table=checks table=config-entries index=id unique - indexer=github.com/hashicorp/consul/agent/consul/state.indexerSingle readIndex=github.com/hashicorp/consul/agent/consul/state.indexFromConfigEntryKindName writeIndex=github.com/hashicorp/consul/agent/consul/state.indexFromConfigEntry + indexer=github.com/hashicorp/consul/agent/consul/state.indexerSingleWithPrefix readIndex=github.com/hashicorp/consul/agent/consul/state.indexFromConfigEntryKindName writeIndex=github.com/hashicorp/consul/agent/consul/state.indexFromConfigEntry prefixIndex=github.com/hashicorp/consul/agent/consul/state.indexFromConfigEntryKindName index=intention-legacy-id unique allow-missing indexer=github.com/hashicorp/consul/agent/consul/state.ServiceIntentionLegacyIDIndex uuidFieldIndex={} index=intention-source allow-missing