Merge pull request #9665 from hashicorp/dnephin/state-store-indexes-2

state: move config-entries table definition to config_entries_schema.go
This commit is contained in:
Daniel Nephin 2021-02-05 14:01:08 -05:00 committed by GitHub
commit eb5d71fd19
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 97 additions and 89 deletions

View File

@ -12,10 +12,6 @@ import (
"github.com/hashicorp/consul/lib" "github.com/hashicorp/consul/lib"
) )
const (
configTableName = "config-entries"
)
type ConfigEntryLinkIndex struct { type ConfigEntryLinkIndex struct {
} }
@ -84,7 +80,7 @@ func init() {
// ConfigEntries is used to pull all the config entries for the snapshot. // ConfigEntries is used to pull all the config entries for the snapshot.
func (s *Snapshot) ConfigEntries() ([]structs.ConfigEntry, error) { func (s *Snapshot) ConfigEntries() ([]structs.ConfigEntry, error) {
entries, err := s.tx.Get(configTableName, "id") entries, err := s.tx.Get(tableConfigEntries, "id")
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -111,7 +107,7 @@ func (s *Store) ConfigEntry(ws memdb.WatchSet, kind, name string, entMeta *struc
func configEntryTxn(tx ReadTxn, ws memdb.WatchSet, kind, name string, entMeta *structs.EnterpriseMeta) (uint64, structs.ConfigEntry, error) { func configEntryTxn(tx ReadTxn, ws memdb.WatchSet, kind, name string, entMeta *structs.EnterpriseMeta) (uint64, structs.ConfigEntry, error) {
// Get the index // Get the index
idx := maxIndexTxn(tx, configTableName) 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 := firstWatchConfigEntryWithTxn(tx, kind, name, entMeta)
@ -146,7 +142,7 @@ func (s *Store) ConfigEntriesByKind(ws memdb.WatchSet, kind string, entMeta *str
func configEntriesByKindTxn(tx ReadTxn, ws memdb.WatchSet, kind string, entMeta *structs.EnterpriseMeta) (uint64, []structs.ConfigEntry, error) { func configEntriesByKindTxn(tx ReadTxn, ws memdb.WatchSet, kind string, entMeta *structs.EnterpriseMeta) (uint64, []structs.ConfigEntry, error) {
// Get the index and watch for updates // Get the index and watch for updates
idx := maxIndexWatchTxn(tx, ws, configTableName) idx := maxIndexWatchTxn(tx, ws, tableConfigEntries)
// Lookup by kind, or all if kind is empty // Lookup by kind, or all if kind is empty
var iter memdb.ResultIterator var iter memdb.ResultIterator
@ -300,10 +296,10 @@ func deleteConfigEntryTxn(tx WriteTxn, idx uint64, kind, name string, entMeta *s
} }
// Delete the config entry from the DB and update the index. // Delete the config entry from the DB and update the index.
if err := tx.Delete(configTableName, existing); err != nil { if err := tx.Delete(tableConfigEntries, existing); err != nil {
return fmt.Errorf("failed removing config entry: %s", err) return fmt.Errorf("failed removing config entry: %s", err)
} }
if err := tx.Insert("index", &IndexEntry{configTableName, idx}); err != nil { if err := tx.Insert("index", &IndexEntry{tableConfigEntries, idx}); err != nil {
return fmt.Errorf("failed updating index: %s", err) return fmt.Errorf("failed updating index: %s", err)
} }
@ -324,10 +320,10 @@ func insertConfigEntryWithTxn(tx WriteTxn, idx uint64, conf structs.ConfigEntry)
} }
// Insert the config entry and update the index // Insert the config entry and update the index
if err := tx.Insert(configTableName, conf); err != nil { if err := tx.Insert(tableConfigEntries, conf); err != nil {
return fmt.Errorf("failed inserting config entry: %s", err) return fmt.Errorf("failed inserting config entry: %s", err)
} }
if err := indexUpdateMaxTxn(tx, idx, configTableName); err != nil { if err := indexUpdateMaxTxn(tx, idx, tableConfigEntries); err != nil {
return fmt.Errorf("failed updating index: %v", err) return fmt.Errorf("failed updating index: %v", err)
} }
@ -433,7 +429,7 @@ func (s *Store) discoveryChainSourcesTxn(tx ReadTxn, ws memdb.WatchSet, dc strin
queue := []structs.ServiceName{destination} queue := []structs.ServiceName{destination}
for len(queue) > 0 { for len(queue) > 0 {
// The "link" index returns config entries that reference a service // The "link" index returns config entries that reference a service
iter, err := tx.Get(configTableName, "link", queue[0].ToServiceID()) iter, err := tx.Get(tableConfigEntries, "link", queue[0].ToServiceID())
if err != nil { if err != nil {
return 0, nil, err return 0, nil, err
} }
@ -605,7 +601,7 @@ func validateProposedConfigEntryInServiceGraph(
sid := structs.NewServiceID(name, entMeta) sid := structs.NewServiceID(name, entMeta)
checkChains[sid] = struct{}{} checkChains[sid] = struct{}{}
iter, err := tx.Get(configTableName, "link", sid) iter, err := tx.Get(tableConfigEntries, "link", sid)
if err != nil { if err != nil {
return err return err
} }

View File

@ -126,7 +126,7 @@ func (s *ServiceIntentionSourceIndex) FromArgs(args ...interface{}) ([]byte, err
func (s *Store) configIntentionsListTxn(tx ReadTxn, ws memdb.WatchSet, entMeta *structs.EnterpriseMeta) (uint64, structs.Intentions, bool, error) { func (s *Store) configIntentionsListTxn(tx ReadTxn, ws memdb.WatchSet, entMeta *structs.EnterpriseMeta) (uint64, structs.Intentions, bool, error) {
// unrolled part of configEntriesByKindTxn // unrolled part of configEntriesByKindTxn
idx := maxIndexTxn(tx, configTableName) idx := maxIndexTxn(tx, tableConfigEntries)
iter, err := getConfigEntryKindsWithTxn(tx, structs.ServiceIntentions, structs.WildcardEnterpriseMeta()) iter, err := getConfigEntryKindsWithTxn(tx, structs.ServiceIntentions, structs.WildcardEnterpriseMeta())
if err != nil { if err != nil {
@ -145,12 +145,12 @@ func (s *Store) configIntentionsListTxn(tx ReadTxn, ws memdb.WatchSet, entMeta *
} }
func (s *Store) configIntentionGetTxn(tx ReadTxn, ws memdb.WatchSet, id string) (uint64, *structs.ServiceIntentionsConfigEntry, *structs.Intention, error) { func (s *Store) configIntentionGetTxn(tx ReadTxn, ws memdb.WatchSet, id string) (uint64, *structs.ServiceIntentionsConfigEntry, *structs.Intention, error) {
idx := maxIndexTxn(tx, configTableName) idx := maxIndexTxn(tx, tableConfigEntries)
if idx < 1 { if idx < 1 {
idx = 1 idx = 1
} }
watchCh, existing, err := tx.FirstWatch(configTableName, "intention-legacy-id", id) watchCh, existing, err := tx.FirstWatch(tableConfigEntries, "intention-legacy-id", id)
if err != nil { if err != nil {
return 0, nil, nil, fmt.Errorf("failed config entry lookup: %s", err) return 0, nil, nil, fmt.Errorf("failed config entry lookup: %s", err)
} }
@ -239,7 +239,7 @@ func configIntentionMatchOneTxn(
} }
func readSourceIntentionsFromConfigEntriesTxn(tx ReadTxn, ws memdb.WatchSet, serviceName string, entMeta *structs.EnterpriseMeta) (uint64, structs.Intentions, error) { func readSourceIntentionsFromConfigEntriesTxn(tx ReadTxn, ws memdb.WatchSet, serviceName string, entMeta *structs.EnterpriseMeta) (uint64, structs.Intentions, error) {
idx := maxIndexTxn(tx, configTableName) idx := maxIndexTxn(tx, tableConfigEntries)
var ( var (
results structs.Intentions results structs.Intentions
@ -265,7 +265,7 @@ func readSourceIntentionsFromConfigEntriesTxn(tx ReadTxn, ws memdb.WatchSet, ser
func readSourceIntentionsFromConfigEntriesForServiceTxn(tx ReadTxn, ws memdb.WatchSet, serviceName string, entMeta *structs.EnterpriseMeta, results structs.Intentions) (structs.Intentions, error) { func readSourceIntentionsFromConfigEntriesForServiceTxn(tx ReadTxn, ws memdb.WatchSet, serviceName string, entMeta *structs.EnterpriseMeta, results structs.Intentions) (structs.Intentions, error) {
sn := structs.NewServiceName(serviceName, entMeta) sn := structs.NewServiceName(serviceName, entMeta)
iter, err := tx.Get(configTableName, "intention-source", sn) iter, err := tx.Get(tableConfigEntries, "intention-source", sn)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed config entry lookup: %s", err) return nil, fmt.Errorf("failed config entry lookup: %s", err)
} }
@ -284,7 +284,7 @@ func readSourceIntentionsFromConfigEntriesForServiceTxn(tx ReadTxn, ws memdb.Wat
} }
func readDestinationIntentionsFromConfigEntriesTxn(tx ReadTxn, ws memdb.WatchSet, serviceName string, entMeta *structs.EnterpriseMeta) (uint64, structs.Intentions, error) { func readDestinationIntentionsFromConfigEntriesTxn(tx ReadTxn, ws memdb.WatchSet, serviceName string, entMeta *structs.EnterpriseMeta) (uint64, structs.Intentions, error) {
idx := maxIndexTxn(tx, configTableName) idx := maxIndexTxn(tx, tableConfigEntries)
var results structs.Intentions var results structs.Intentions

View File

@ -3,66 +3,13 @@
package state package state
import ( import (
"github.com/hashicorp/consul/agent/structs"
memdb "github.com/hashicorp/go-memdb" memdb "github.com/hashicorp/go-memdb"
"github.com/hashicorp/consul/agent/structs"
) )
// configTableSchema returns a new table schema used to store global
// config entries.
func configTableSchema() *memdb.TableSchema {
return &memdb.TableSchema{
Name: configTableName,
Indexes: map[string]*memdb.IndexSchema{
"id": {
Name: "id",
AllowMissing: false,
Unique: true,
Indexer: &memdb.CompoundIndex{
Indexes: []memdb.Indexer{
&memdb.StringFieldIndex{
Field: "Kind",
Lowercase: true,
},
&memdb.StringFieldIndex{
Field: "Name",
Lowercase: true,
},
},
},
},
"kind": {
Name: "kind",
AllowMissing: false,
Unique: false,
Indexer: &memdb.StringFieldIndex{
Field: "Kind",
Lowercase: true,
},
},
"link": {
Name: "link",
AllowMissing: true,
Unique: false,
Indexer: &ConfigEntryLinkIndex{},
},
"intention-legacy-id": {
Name: "intention-legacy-id",
AllowMissing: true,
Unique: true,
Indexer: &ServiceIntentionLegacyIDIndex{},
},
"intention-source": {
Name: "intention-source",
AllowMissing: true,
Unique: false,
Indexer: &ServiceIntentionSourceIndex{},
},
},
}
}
func firstConfigEntryWithTxn(tx ReadTxn, kind, name string, _ *structs.EnterpriseMeta) (interface{}, error) { func firstConfigEntryWithTxn(tx ReadTxn, kind, name string, _ *structs.EnterpriseMeta) (interface{}, error) {
return tx.First(configTableName, "id", kind, name) return tx.First(tableConfigEntries, "id", kind, name)
} }
func firstWatchConfigEntryWithTxn( func firstWatchConfigEntryWithTxn(
@ -71,7 +18,7 @@ func firstWatchConfigEntryWithTxn(
name string, name string,
_ *structs.EnterpriseMeta, _ *structs.EnterpriseMeta,
) (<-chan struct{}, interface{}, error) { ) (<-chan struct{}, interface{}, error) {
return tx.FirstWatch(configTableName, "id", kind, name) return tx.FirstWatch(tableConfigEntries, "id", kind, name)
} }
func validateConfigEntryEnterprise(_ ReadTxn, _ structs.ConfigEntry) error { func validateConfigEntryEnterprise(_ ReadTxn, _ structs.ConfigEntry) error {
@ -79,11 +26,11 @@ func validateConfigEntryEnterprise(_ ReadTxn, _ structs.ConfigEntry) error {
} }
func getAllConfigEntriesWithTxn(tx ReadTxn, _ *structs.EnterpriseMeta) (memdb.ResultIterator, error) { func getAllConfigEntriesWithTxn(tx ReadTxn, _ *structs.EnterpriseMeta) (memdb.ResultIterator, error) {
return tx.Get(configTableName, "id") return tx.Get(tableConfigEntries, "id")
} }
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(configTableName, "kind", kind) return tx.Get(tableConfigEntries, "kind", kind)
} }
func configIntentionsConvertToList(iter memdb.ResultIterator, _ *structs.EnterpriseMeta) structs.Intentions { func configIntentionsConvertToList(iter memdb.ResultIterator, _ *structs.EnterpriseMeta) structs.Intentions {

View File

@ -0,0 +1,65 @@
package state
import "github.com/hashicorp/go-memdb"
const (
tableConfigEntries = "config-entries"
indexLink = "link"
indexIntentionLegacyID = "intention-legacy-id"
indexSource = "intention-source"
)
// configTableSchema returns a new table schema used to store global
// config entries.
func configTableSchema() *memdb.TableSchema {
return &memdb.TableSchema{
Name: tableConfigEntries,
Indexes: map[string]*memdb.IndexSchema{
indexID: {
Name: indexID,
AllowMissing: false,
Unique: true,
Indexer: &memdb.CompoundIndex{
Indexes: []memdb.Indexer{
&memdb.StringFieldIndex{
Field: "Kind",
Lowercase: true,
},
&memdb.StringFieldIndex{
Field: "Name",
Lowercase: true,
},
},
},
},
indexKind: {
Name: indexKind,
AllowMissing: false,
Unique: false,
Indexer: &memdb.StringFieldIndex{
Field: "Kind",
Lowercase: true,
},
},
indexLink: {
Name: indexLink,
AllowMissing: true,
Unique: false,
Indexer: &ConfigEntryLinkIndex{},
},
indexIntentionLegacyID: {
Name: indexIntentionLegacyID,
AllowMissing: true,
Unique: true,
Indexer: &ServiceIntentionLegacyIDIndex{},
},
indexSource: {
Name: indexSource,
AllowMissing: true,
Unique: false,
Indexer: &ServiceIntentionSourceIndex{},
},
},
}
}

View File

@ -718,7 +718,7 @@ func (s *Store) LegacyIntentionDeleteAll(idx uint64) error {
// Also bump the index for the config entry table so that // Also bump the index for the config entry table so that
// secondaries can correctly know when they've replicated all of the service-intentions // secondaries can correctly know when they've replicated all of the service-intentions
// config entries that USED to exist in the old intentions table. // config entries that USED to exist in the old intentions table.
if err := tx.Insert("index", &IndexEntry{configTableName, idx}); err != nil { if err := tx.Insert("index", &IndexEntry{tableConfigEntries, idx}); err != nil {
return fmt.Errorf("failed updating index: %s", err) return fmt.Errorf("failed updating index: %s", err)
} }

View File

@ -91,7 +91,7 @@ func TestStore_IntentionSetGet_basic(t *testing.T) {
// Make sure the right index got updated. // Make sure the right index got updated.
require.Equal(t, lastIndex, s.maxIndex(intentionsTableName)) require.Equal(t, lastIndex, s.maxIndex(intentionsTableName))
require.Equal(t, uint64(0), s.maxIndex(configTableName)) require.Equal(t, uint64(0), s.maxIndex(tableConfigEntries))
expected = &structs.Intention{ expected = &structs.Intention{
ID: legacyIxn.ID, ID: legacyIxn.ID,
@ -132,7 +132,7 @@ func TestStore_IntentionSetGet_basic(t *testing.T) {
require.NoError(t, s.EnsureConfigEntry(lastIndex, configEntry.Clone())) require.NoError(t, s.EnsureConfigEntry(lastIndex, configEntry.Clone()))
// Make sure the config entry index got updated instead of the old intentions one // Make sure the config entry index got updated instead of the old intentions one
require.Equal(t, lastIndex, s.maxIndex(configTableName)) require.Equal(t, lastIndex, s.maxIndex(tableConfigEntries))
require.Equal(t, uint64(0), s.maxIndex(intentionsTableName)) require.Equal(t, uint64(0), s.maxIndex(intentionsTableName))
expected = &structs.Intention{ expected = &structs.Intention{
@ -179,7 +179,7 @@ func TestStore_IntentionSetGet_basic(t *testing.T) {
// Make sure the index got updated. // Make sure the index got updated.
require.Equal(t, lastIndex, s.maxIndex(intentionsTableName)) require.Equal(t, lastIndex, s.maxIndex(intentionsTableName))
require.Equal(t, uint64(0), s.maxIndex(configTableName)) require.Equal(t, uint64(0), s.maxIndex(tableConfigEntries))
expected.SourceNS = legacyIxn.SourceNS expected.SourceNS = legacyIxn.SourceNS
expected.Action = structs.IntentionActionDeny expected.Action = structs.IntentionActionDeny
@ -202,7 +202,7 @@ func TestStore_IntentionSetGet_basic(t *testing.T) {
require.NoError(t, s.EnsureConfigEntry(lastIndex, configEntry.Clone())) require.NoError(t, s.EnsureConfigEntry(lastIndex, configEntry.Clone()))
// Make sure the config entry index got updated instead of the old intentions one // Make sure the config entry index got updated instead of the old intentions one
require.Equal(t, lastIndex, s.maxIndex(configTableName)) require.Equal(t, lastIndex, s.maxIndex(tableConfigEntries))
require.Equal(t, uint64(0), s.maxIndex(intentionsTableName)) require.Equal(t, uint64(0), s.maxIndex(intentionsTableName))
expected.Description = configEntry.Sources[0].Description expected.Description = configEntry.Sources[0].Description
@ -241,7 +241,7 @@ func TestStore_IntentionSetGet_basic(t *testing.T) {
// Make sure the index did NOT get updated. // Make sure the index did NOT get updated.
require.Equal(t, lastIndex-1, s.maxIndex(intentionsTableName)) require.Equal(t, lastIndex-1, s.maxIndex(intentionsTableName))
require.Equal(t, uint64(0), s.maxIndex(configTableName)) require.Equal(t, uint64(0), s.maxIndex(tableConfigEntries))
require.False(t, watchFired(ws), "watch not fired") require.False(t, watchFired(ws), "watch not fired")
} }
}) })
@ -816,7 +816,7 @@ func TestStore_LegacyIntentionSet_emptyId(t *testing.T) {
// Index is not updated if nothing is saved. // Index is not updated if nothing is saved.
require.Equal(t, s.maxIndex(intentionsTableName), uint64(0)) require.Equal(t, s.maxIndex(intentionsTableName), uint64(0))
require.Equal(t, uint64(0), s.maxIndex(configTableName)) require.Equal(t, uint64(0), s.maxIndex(tableConfigEntries))
require.False(t, watchFired(ws), "watch fired") require.False(t, watchFired(ws), "watch fired")
} }
@ -1006,7 +1006,7 @@ func TestStore_IntentionDelete(t *testing.T) {
// Make sure the index got updated. // Make sure the index got updated.
require.Equal(t, s.maxIndex(intentionsTableName), lastIndex) require.Equal(t, s.maxIndex(intentionsTableName), lastIndex)
require.Equal(t, uint64(0), s.maxIndex(configTableName)) require.Equal(t, uint64(0), s.maxIndex(tableConfigEntries))
} else { } else {
conf := &structs.ServiceIntentionsConfigEntry{ conf := &structs.ServiceIntentionsConfigEntry{
Kind: structs.ServiceIntentions, Kind: structs.ServiceIntentions,
@ -1028,7 +1028,7 @@ func TestStore_IntentionDelete(t *testing.T) {
require.NoError(t, s.EnsureConfigEntry(1, conf.Clone())) require.NoError(t, s.EnsureConfigEntry(1, conf.Clone()))
// Make sure the index got updated. // Make sure the index got updated.
require.Equal(t, s.maxIndex(configTableName), lastIndex) require.Equal(t, s.maxIndex(tableConfigEntries), lastIndex)
require.Equal(t, uint64(0), s.maxIndex(intentionsTableName)) require.Equal(t, uint64(0), s.maxIndex(intentionsTableName))
} }
require.True(t, watchFired(ws), "watch fired") require.True(t, watchFired(ws), "watch fired")
@ -1046,13 +1046,13 @@ func TestStore_IntentionDelete(t *testing.T) {
// Make sure the index got updated. // Make sure the index got updated.
require.Equal(t, s.maxIndex(intentionsTableName), lastIndex) require.Equal(t, s.maxIndex(intentionsTableName), lastIndex)
require.Equal(t, uint64(0), s.maxIndex(configTableName)) require.Equal(t, uint64(0), s.maxIndex(tableConfigEntries))
} else { } else {
lastIndex++ lastIndex++
require.NoError(t, s.DeleteConfigEntry(lastIndex, structs.ServiceIntentions, "web", nil)) require.NoError(t, s.DeleteConfigEntry(lastIndex, structs.ServiceIntentions, "web", nil))
// Make sure the index got updated. // Make sure the index got updated.
require.Equal(t, s.maxIndex(configTableName), lastIndex) require.Equal(t, s.maxIndex(tableConfigEntries), lastIndex)
require.Equal(t, uint64(0), s.maxIndex(intentionsTableName)) require.Equal(t, uint64(0), s.maxIndex(intentionsTableName))
} }
require.True(t, watchFired(ws), "watch fired") require.True(t, watchFired(ws), "watch fired")