state: remove unnecessary kind index

The query can be performed using a prefix query on the ID index.

Also backport some enterprise changes to prevent conflicts.
This commit is contained in:
Daniel Nephin 2021-03-31 16:21:21 -04:00
parent eb7f4b7ea4
commit 95b361ecc8
6 changed files with 54 additions and 42 deletions

View File

@ -336,7 +336,7 @@ func (c *ConfigEntry) ResolveServiceConfig(args *structs.ServiceConfigRequest, r
// Pass the WatchSet to both the service and proxy config lookups. If either is updated during the // Pass the WatchSet to both the service and proxy config lookups. If either is updated during the
// blocking query, this function will be rerun and these state store lookups will both be current. // blocking query, this function will be rerun and these state store lookups will both be current.
// We use the default enterprise meta to look up the global proxy defaults because they are not namespaced. // We use the default enterprise meta to look up the global proxy defaults because they are not namespaced.
_, proxyEntry, err := state.ConfigEntry(ws, structs.ProxyDefaults, structs.ProxyConfigGlobal, structs.DefaultEnterpriseMeta()) _, proxyEntry, err := state.ConfigEntry(ws, structs.ProxyDefaults, structs.ProxyConfigGlobal, &args.EnterpriseMeta)
if err != nil { if err != nil {
return err return err
} }

View File

@ -852,7 +852,7 @@ func readDiscoveryChainConfigEntriesTxn(
sid := structs.NewServiceID(serviceName, entMeta) sid := structs.NewServiceID(serviceName, entMeta)
// Grab the proxy defaults if they exist. // Grab the proxy defaults if they exist.
idx, proxy, err := getProxyConfigEntryTxn(tx, ws, structs.ProxyConfigGlobal, overrides, structs.DefaultEnterpriseMeta()) idx, proxy, err := getProxyConfigEntryTxn(tx, ws, structs.ProxyConfigGlobal, overrides, entMeta)
if err != nil { if err != nil {
return 0, nil, err return 0, nil, err
} else if proxy != nil { } else if proxy != nil {
@ -1168,6 +1168,7 @@ func configEntryWithOverridesTxn(
) (uint64, structs.ConfigEntry, error) { ) (uint64, structs.ConfigEntry, error) {
if len(overrides) > 0 { if len(overrides) > 0 {
kn := NewConfigEntryKindName(kind, name, entMeta) kn := NewConfigEntryKindName(kind, name, entMeta)
kn.Normalize()
entry, ok := overrides[kn] entry, ok := overrides[kn]
if ok { if ok {
return 0, entry, nil // a nil entry implies it should act like it is erased return 0, entry, nil // a nil entry implies it should act like it is erased
@ -1185,7 +1186,7 @@ func protocolForService(
svc structs.ServiceName, svc structs.ServiceName,
) (uint64, string, error) { ) (uint64, string, error) {
// Get the global proxy defaults (for default protocol) // Get the global proxy defaults (for default protocol)
maxIdx, proxyConfig, err := configEntryTxn(tx, ws, structs.ProxyDefaults, structs.ProxyConfigGlobal, structs.DefaultEnterpriseMeta()) maxIdx, proxyConfig, err := configEntryTxn(tx, ws, structs.ProxyDefaults, structs.ProxyConfigGlobal, &svc.EnterpriseMeta)
if err != nil { if err != nil {
return 0, "", err return 0, "", err
} }
@ -1229,6 +1230,11 @@ type ConfigEntryKindName struct {
structs.EnterpriseMeta structs.EnterpriseMeta
} }
// NewConfigEntryKindName returns a new ConfigEntryKindName. The EnterpriseMeta
// values will be normalized based on the kind.
//
// Any caller which modifies the EnterpriseMeta field must call Normalize before
// persisting or using the value as a map key.
func NewConfigEntryKindName(kind, name string, entMeta *structs.EnterpriseMeta) ConfigEntryKindName { func NewConfigEntryKindName(kind, name string, entMeta *structs.EnterpriseMeta) ConfigEntryKindName {
ret := ConfigEntryKindName{ ret := ConfigEntryKindName{
Kind: kind, Kind: kind,
@ -1239,7 +1245,7 @@ func NewConfigEntryKindName(kind, name string, entMeta *structs.EnterpriseMeta)
} }
ret.EnterpriseMeta = *entMeta ret.EnterpriseMeta = *entMeta
ret.EnterpriseMeta.Normalize() ret.Normalize()
return ret return ret
} }
@ -1252,3 +1258,15 @@ type ConfigEntryKindQuery struct {
Kind string Kind string
structs.EnterpriseMeta structs.EnterpriseMeta
} }
// NamespaceOrDefault exists because structs.EnterpriseMeta uses a pointer
// receiver for this method. Remove once that is fixed.
func (q ConfigEntryKindQuery) NamespaceOrDefault() string {
return q.EnterpriseMeta.NamespaceOrDefault()
}
// PartitionOrDefault exists because structs.EnterpriseMeta uses a pointer
// receiver for this method. Remove once that is fixed.
func (q ConfigEntryKindQuery) PartitionOrDefault() string {
return q.EnterpriseMeta.PartitionOrDefault()
}

View File

@ -12,15 +12,23 @@ import (
) )
func indexFromConfigEntryKindName(arg interface{}) ([]byte, error) { func indexFromConfigEntryKindName(arg interface{}) ([]byte, error) {
n, ok := arg.(ConfigEntryKindName) var b indexBuilder
if !ok {
return nil, fmt.Errorf("invalid type for ConfigEntryKindName query: %T", arg) switch n := arg.(type) {
case *structs.EnterpriseMeta:
return nil, nil
case structs.EnterpriseMeta:
return b.Bytes(), nil
case ConfigEntryKindQuery:
b.String(strings.ToLower(n.Kind))
return b.Bytes(), nil
case ConfigEntryKindName:
b.String(strings.ToLower(n.Kind))
b.String(strings.ToLower(n.Name))
return b.Bytes(), nil
} }
var b indexBuilder return nil, fmt.Errorf("invalid type for ConfigEntryKindName query: %T", arg)
b.String(strings.ToLower(n.Kind))
b.String(strings.ToLower(n.Name))
return b.Bytes(), nil
} }
func validateConfigEntryEnterprise(_ ReadTxn, _ structs.ConfigEntry) error { func validateConfigEntryEnterprise(_ ReadTxn, _ structs.ConfigEntry) error {
@ -32,7 +40,7 @@ func getAllConfigEntriesWithTxn(tx ReadTxn, _ *structs.EnterpriseMeta) (memdb.Re
} }
func getConfigEntryKindsWithTxn(tx ReadTxn, kind string, _ *structs.EnterpriseMeta) (memdb.ResultIterator, error) { func getConfigEntryKindsWithTxn(tx ReadTxn, kind string, _ *structs.EnterpriseMeta) (memdb.ResultIterator, error) {
return tx.Get(tableConfigEntries, indexKind, ConfigEntryKindQuery{Kind: kind}) return tx.Get(tableConfigEntries, indexID+"_prefix", ConfigEntryKindQuery{Kind: kind})
} }
func configIntentionsConvertToList(iter memdb.ResultIterator, _ *structs.EnterpriseMeta) structs.Intentions { func configIntentionsConvertToList(iter memdb.ResultIterator, _ *structs.EnterpriseMeta) structs.Intentions {

View File

@ -18,17 +18,15 @@ func testIndexerTableConfigEntries() map[string]indexerTestCase {
source: &structs.ProxyConfigEntry{Name: "NaMe"}, source: &structs.ProxyConfigEntry{Name: "NaMe"},
expected: []byte("proxy-defaults\x00name\x00"), expected: []byte("proxy-defaults\x00name\x00"),
}, },
}, prefix: []indexValue{
indexKind: { {
read: indexValue{ source: structs.EnterpriseMeta{},
source: ConfigEntryKindQuery{ expected: nil,
Kind: "Service-Defaults", },
{
source: ConfigEntryKindQuery{Kind: "Proxy-Defaults"},
expected: []byte("proxy-defaults\x00"),
}, },
expected: []byte("service-defaults\x00"),
},
write: indexValue{
source: &structs.ServiceConfigEntry{},
expected: []byte("service-defaults\x00"),
}, },
}, },
} }

View File

@ -33,15 +33,6 @@ func configTableSchema() *memdb.TableSchema {
prefixIndex: indexFromConfigEntryKindName, prefixIndex: indexFromConfigEntryKindName,
}, },
}, },
indexKind: {
Name: indexKind,
AllowMissing: false,
Unique: false,
Indexer: indexerSingle{
readIndex: indexFromConfigEntryKindQuery,
writeIndex: indexKindFromConfigEntry,
},
},
indexLink: { indexLink: {
Name: indexLink, Name: indexLink,
AllowMissing: true, AllowMissing: true,
@ -80,17 +71,6 @@ func indexFromConfigEntry(raw interface{}) ([]byte, error) {
return b.Bytes(), nil return b.Bytes(), nil
} }
func indexFromConfigEntryKindQuery(raw interface{}) ([]byte, error) {
q, ok := raw.(ConfigEntryKindQuery)
if !ok {
return nil, fmt.Errorf("type must be ConfigEntryKindQuery: %T", raw)
}
var b indexBuilder
b.String(strings.ToLower(q.Kind))
return b.Bytes(), nil
}
// indexKindFromConfigEntry indexes kinds without a namespace for any config // indexKindFromConfigEntry indexes kinds without a namespace for any config
// entries that span all namespaces. // entries that span all namespaces.
func indexKindFromConfigEntry(raw interface{}) ([]byte, error) { func indexKindFromConfigEntry(raw interface{}) ([]byte, error) {

View File

@ -50,6 +50,14 @@ func (m *EnterpriseMeta) NamespaceOrEmpty() string {
return "" return ""
} }
func (m *EnterpriseMeta) PartitionOrDefault() string {
return ""
}
func (m *EnterpriseMeta) PartitionOrEmpty() string {
return ""
}
func NewEnterpriseMeta(_ string) EnterpriseMeta { func NewEnterpriseMeta(_ string) EnterpriseMeta {
return emptyEnterpriseMeta return emptyEnterpriseMeta
} }