state: convert config-entries table to new indexer pattern
Using functional indexes to isolate enterprise differentiation and remove reflection.
This commit is contained in:
parent
98c32599e4
commit
b43977423f
|
@ -106,7 +106,7 @@ func configEntryTxn(tx ReadTxn, ws memdb.WatchSet, kind, name string, entMeta *s
|
||||||
idx := maxIndexTxn(tx, tableConfigEntries)
|
idx := maxIndexTxn(tx, tableConfigEntries)
|
||||||
|
|
||||||
// Get the existing config entry.
|
// Get the existing config entry.
|
||||||
watchCh, existing, err := firstWatchConfigEntryWithTxn(tx, kind, name, entMeta)
|
watchCh, existing, err := tx.FirstWatch(tableConfigEntries, "id", NewConfigEntryKindName(kind, name, entMeta))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, nil, fmt.Errorf("failed config entry lookup: %s", err)
|
return 0, nil, fmt.Errorf("failed config entry lookup: %s", err)
|
||||||
}
|
}
|
||||||
|
@ -175,7 +175,7 @@ func (s *Store) EnsureConfigEntry(idx uint64, conf structs.ConfigEntry) error {
|
||||||
// ensureConfigEntryTxn upserts a config entry inside of a transaction.
|
// ensureConfigEntryTxn upserts a config entry inside of a transaction.
|
||||||
func ensureConfigEntryTxn(tx WriteTxn, idx uint64, conf structs.ConfigEntry) error {
|
func ensureConfigEntryTxn(tx WriteTxn, idx uint64, conf structs.ConfigEntry) error {
|
||||||
// Check for existing configuration.
|
// Check for existing configuration.
|
||||||
existing, err := firstConfigEntryWithTxn(tx, conf.GetKind(), conf.GetName(), conf.GetEnterpriseMeta())
|
existing, err := tx.First(tableConfigEntries, indexID, newConfigEntryQuery(conf))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed configuration lookup: %s", err)
|
return fmt.Errorf("failed configuration lookup: %s", err)
|
||||||
}
|
}
|
||||||
|
@ -214,7 +214,7 @@ func (s *Store) EnsureConfigEntryCAS(idx, cidx uint64, conf structs.ConfigEntry)
|
||||||
defer tx.Abort()
|
defer tx.Abort()
|
||||||
|
|
||||||
// Check for existing configuration.
|
// Check for existing configuration.
|
||||||
existing, err := firstConfigEntryWithTxn(tx, conf.GetKind(), conf.GetName(), conf.GetEnterpriseMeta())
|
existing, err := tx.First(tableConfigEntries, indexID, newConfigEntryQuery(conf))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, fmt.Errorf("failed configuration lookup: %s", err)
|
return false, fmt.Errorf("failed configuration lookup: %s", err)
|
||||||
}
|
}
|
||||||
|
@ -254,9 +254,9 @@ func (s *Store) DeleteConfigEntry(idx uint64, kind, name string, entMeta *struct
|
||||||
return tx.Commit()
|
return tx.Commit()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: accept structs.ConfigEntry instead of individual fields
|
||||||
func deleteConfigEntryTxn(tx WriteTxn, idx uint64, kind, name string, entMeta *structs.EnterpriseMeta) error {
|
func deleteConfigEntryTxn(tx WriteTxn, idx uint64, kind, name string, entMeta *structs.EnterpriseMeta) error {
|
||||||
// Try to retrieve the existing config entry.
|
existing, err := tx.First(tableConfigEntries, indexID, NewConfigEntryKindName(kind, name, entMeta))
|
||||||
existing, err := firstConfigEntryWithTxn(tx, kind, name, entMeta)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed config entry lookup: %s", err)
|
return fmt.Errorf("failed config entry lookup: %s", err)
|
||||||
}
|
}
|
||||||
|
@ -1242,3 +1242,7 @@ func NewConfigEntryKindName(kind, name string, entMeta *structs.EnterpriseMeta)
|
||||||
ret.EnterpriseMeta.Normalize()
|
ret.EnterpriseMeta.Normalize()
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func newConfigEntryQuery(c structs.ConfigEntry) ConfigEntryKindName {
|
||||||
|
return NewConfigEntryKindName(c.GetKind(), c.GetName(), c.GetEnterpriseMeta())
|
||||||
|
}
|
||||||
|
|
|
@ -3,22 +3,40 @@
|
||||||
package state
|
package state
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
memdb "github.com/hashicorp/go-memdb"
|
memdb "github.com/hashicorp/go-memdb"
|
||||||
|
|
||||||
"github.com/hashicorp/consul/agent/structs"
|
"github.com/hashicorp/consul/agent/structs"
|
||||||
)
|
)
|
||||||
|
|
||||||
func firstConfigEntryWithTxn(tx ReadTxn, kind, name string, _ *structs.EnterpriseMeta) (interface{}, error) {
|
func indexFromConfigEntryKindName(arg interface{}) ([]byte, error) {
|
||||||
return tx.First(tableConfigEntries, "id", kind, name)
|
n, ok := arg.(ConfigEntryKindName)
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("invalid type for ConfigEntryKindName query: %T", arg)
|
||||||
|
}
|
||||||
|
|
||||||
|
var b indexBuilder
|
||||||
|
b.String(strings.ToLower(n.Kind))
|
||||||
|
b.String(strings.ToLower(n.Name))
|
||||||
|
return b.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func firstWatchConfigEntryWithTxn(
|
func indexFromConfigEntry(raw interface{}) ([]byte, error) {
|
||||||
tx ReadTxn,
|
c, ok := raw.(structs.ConfigEntry)
|
||||||
kind string,
|
if !ok {
|
||||||
name string,
|
return nil, fmt.Errorf("type must be structs.ConfigEntry: %T", raw)
|
||||||
_ *structs.EnterpriseMeta,
|
}
|
||||||
) (<-chan struct{}, interface{}, error) {
|
|
||||||
return tx.FirstWatch(tableConfigEntries, "id", kind, name)
|
if c.GetName() == "" || c.GetKind() == "" {
|
||||||
|
return nil, errMissingValueForIndex
|
||||||
|
}
|
||||||
|
|
||||||
|
var b indexBuilder
|
||||||
|
b.String(strings.ToLower(c.GetKind()))
|
||||||
|
b.String(strings.ToLower(c.GetName()))
|
||||||
|
return b.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func validateConfigEntryEnterprise(_ ReadTxn, _ structs.ConfigEntry) error {
|
func validateConfigEntryEnterprise(_ ReadTxn, _ structs.ConfigEntry) error {
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
package state
|
package state
|
||||||
|
|
||||||
import "github.com/hashicorp/go-memdb"
|
import (
|
||||||
|
"github.com/hashicorp/go-memdb"
|
||||||
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
tableConfigEntries = "config-entries"
|
tableConfigEntries = "config-entries"
|
||||||
|
@ -20,17 +22,9 @@ func configTableSchema() *memdb.TableSchema {
|
||||||
Name: indexID,
|
Name: indexID,
|
||||||
AllowMissing: false,
|
AllowMissing: false,
|
||||||
Unique: true,
|
Unique: true,
|
||||||
Indexer: &memdb.CompoundIndex{
|
Indexer: indexerSingle{
|
||||||
Indexes: []memdb.Indexer{
|
readIndex: readIndex(indexFromConfigEntryKindName),
|
||||||
&memdb.StringFieldIndex{
|
writeIndex: writeIndex(indexFromConfigEntry),
|
||||||
Field: "Kind",
|
|
||||||
Lowercase: true,
|
|
||||||
},
|
|
||||||
&memdb.StringFieldIndex{
|
|
||||||
Field: "Name",
|
|
||||||
Lowercase: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
indexKind: {
|
indexKind: {
|
||||||
|
|
|
@ -60,7 +60,7 @@ table=checks
|
||||||
|
|
||||||
table=config-entries
|
table=config-entries
|
||||||
index=id unique
|
index=id unique
|
||||||
indexer=github.com/hashicorp/go-memdb.CompoundIndex Indexes=[github.com/hashicorp/go-memdb.StringFieldIndex Field=Kind Lowercase=true, github.com/hashicorp/go-memdb.StringFieldIndex Field=Name Lowercase=true] AllowMissing=false
|
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
|
||||||
index=intention-legacy-id unique allow-missing
|
index=intention-legacy-id unique allow-missing
|
||||||
indexer=github.com/hashicorp/consul/agent/consul/state.ServiceIntentionLegacyIDIndex uuidFieldIndex={}
|
indexer=github.com/hashicorp/consul/agent/consul/state.ServiceIntentionLegacyIDIndex uuidFieldIndex={}
|
||||||
index=intention-source allow-missing
|
index=intention-source allow-missing
|
||||||
|
|
Loading…
Reference in New Issue