state: remove the need for registerSchema

registerSchema creates some indirection which is not necessary in this
case. newDBSchema can call each of the tables.

Enterprise tables can be added from the existing withEnterpriseSchema
shim.
This commit is contained in:
Daniel Nephin 2021-01-29 19:24:41 -05:00
parent 2cbf8b5fd0
commit 05d5ec4804
16 changed files with 46 additions and 95 deletions

View File

@ -212,14 +212,6 @@ func (s *TokenExpirationIndex) FromArgs(args ...interface{}) ([]byte, error) {
return buf, nil return buf, nil
} }
func init() {
registerSchema(tokensTableSchema)
registerSchema(policiesTableSchema)
registerSchema(rolesTableSchema)
registerSchema(bindingRulesTableSchema)
registerSchema(authMethodsTableSchema)
}
// ACLTokens is used when saving a snapshot // ACLTokens is used when saving a snapshot
func (s *Snapshot) ACLTokens() (memdb.ResultIterator, error) { func (s *Snapshot) ACLTokens() (memdb.ResultIterator, error) {
iter, err := s.tx.Get("acl-tokens", "id") iter, err := s.tx.Get("acl-tokens", "id")

View File

@ -3,8 +3,9 @@ package state
import ( import (
"fmt" "fmt"
"github.com/hashicorp/consul/agent/structs"
"github.com/hashicorp/go-memdb" "github.com/hashicorp/go-memdb"
"github.com/hashicorp/consul/agent/structs"
) )
// autopilotConfigTableSchema returns a new table schema used for storing // autopilotConfigTableSchema returns a new table schema used for storing
@ -25,10 +26,6 @@ func autopilotConfigTableSchema() *memdb.TableSchema {
} }
} }
func init() {
registerSchema(autopilotConfigTableSchema)
}
// Autopilot is used to pull the autopilot config from the snapshot. // Autopilot is used to pull the autopilot config from the snapshot.
func (s *Snapshot) Autopilot() (*structs.AutopilotConfig, error) { func (s *Snapshot) Autopilot() (*structs.AutopilotConfig, error) {
c, err := s.tx.First("autopilot-config", "id") c, err := s.tx.First("autopilot-config", "id")

View File

@ -347,11 +347,3 @@ func (index *ServiceNameIndex) PrefixFromArgs(args ...interface{}) ([]byte, erro
} }
return val, nil return val, nil
} }
func init() {
registerSchema(nodesTableSchema)
registerSchema(servicesTableSchema)
registerSchema(checksTableSchema)
registerSchema(gatewayServicesTableSchema)
registerSchema(meshTopologyTableSchema)
}

View File

@ -74,10 +74,6 @@ func (s *ConfigEntryLinkIndex) PrefixFromArgs(args ...interface{}) ([]byte, erro
return val, nil return val, nil
} }
func init() {
registerSchema(configTableSchema)
}
// 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(tableConfigEntries, "id") entries, err := s.tx.Get(tableConfigEntries, "id")

View File

@ -73,12 +73,6 @@ func caRootTableSchema() *memdb.TableSchema {
} }
} }
func init() {
registerSchema(caBuiltinProviderTableSchema)
registerSchema(caConfigTableSchema)
registerSchema(caRootTableSchema)
}
// CAConfig is used to pull the CA config from the snapshot. // CAConfig is used to pull the CA config from the snapshot.
func (s *Snapshot) CAConfig() (*structs.CAConfiguration, error) { func (s *Snapshot) CAConfig() (*structs.CAConfiguration, error) {
c, err := s.tx.First(tableConnectCAConfig, "id") c, err := s.tx.First(tableConnectCAConfig, "id")

View File

@ -3,9 +3,10 @@ package state
import ( import (
"fmt" "fmt"
"github.com/hashicorp/go-memdb"
"github.com/hashicorp/consul/agent/structs" "github.com/hashicorp/consul/agent/structs"
"github.com/hashicorp/consul/lib" "github.com/hashicorp/consul/lib"
"github.com/hashicorp/go-memdb"
) )
// coordinatesTableSchema returns a new table schema used for storing // coordinatesTableSchema returns a new table schema used for storing
@ -47,10 +48,6 @@ func coordinatesTableSchema() *memdb.TableSchema {
} }
} }
func init() {
registerSchema(coordinatesTableSchema)
}
// Coordinates is used to pull all the coordinates from the snapshot. // Coordinates is used to pull all the coordinates from the snapshot.
func (s *Snapshot) Coordinates() (memdb.ResultIterator, error) { func (s *Snapshot) Coordinates() (memdb.ResultIterator, error) {
iter, err := s.tx.Get("coordinates", "id") iter, err := s.tx.Get("coordinates", "id")

View File

@ -27,10 +27,6 @@ func federationStateTableSchema() *memdb.TableSchema {
} }
} }
func init() {
registerSchema(federationStateTableSchema)
}
// FederationStates is used to pull all the federation states for the snapshot. // FederationStates is used to pull all the federation states for the snapshot.
func (s *Snapshot) FederationStates() ([]*structs.FederationState, error) { func (s *Snapshot) FederationStates() ([]*structs.FederationState, error) {
configs, err := s.tx.Get(tableFederationStates, "id") configs, err := s.tx.Get(tableFederationStates, "id")

View File

@ -95,10 +95,6 @@ func intentionsTableSchema() *memdb.TableSchema {
} }
} }
func init() {
registerSchema(intentionsTableSchema)
}
// LegacyIntentions is used to pull all the intentions from the snapshot. // LegacyIntentions is used to pull all the intentions from the snapshot.
// //
// Deprecated: service-intentions config entries are handled as config entries // Deprecated: service-intentions config entries are handled as config entries

View File

@ -4,8 +4,9 @@ import (
"fmt" "fmt"
"time" "time"
"github.com/hashicorp/consul/agent/structs"
"github.com/hashicorp/go-memdb" "github.com/hashicorp/go-memdb"
"github.com/hashicorp/consul/agent/structs"
) )
// kvsTableSchema returns a new table schema used for storing key/value data for // kvsTableSchema returns a new table schema used for storing key/value data for
@ -48,11 +49,6 @@ func tombstonesTableSchema() *memdb.TableSchema {
} }
} }
func init() {
registerSchema(kvsTableSchema)
registerSchema(tombstonesTableSchema)
}
// KVs is used to pull the full list of KVS entries for use during snapshots. // KVs is used to pull the full list of KVS entries for use during snapshots.
func (s *Snapshot) KVs() (memdb.ResultIterator, error) { func (s *Snapshot) KVs() (memdb.ResultIterator, error) {
iter, err := s.tx.Get("kvs", "id_prefix") iter, err := s.tx.Get("kvs", "id_prefix")

View File

@ -4,9 +4,10 @@ import (
"fmt" "fmt"
"regexp" "regexp"
"github.com/hashicorp/go-memdb"
"github.com/hashicorp/consul/agent/consul/prepared_query" "github.com/hashicorp/consul/agent/consul/prepared_query"
"github.com/hashicorp/consul/agent/structs" "github.com/hashicorp/consul/agent/structs"
"github.com/hashicorp/go-memdb"
) )
// preparedQueriesTableSchema returns a new table schema used for storing // preparedQueriesTableSchema returns a new table schema used for storing
@ -50,10 +51,6 @@ func preparedQueriesTableSchema() *memdb.TableSchema {
} }
} }
func init() {
registerSchema(preparedQueriesTableSchema)
}
// validUUID is used to check if a given string looks like a UUID // validUUID is used to check if a given string looks like a UUID
var validUUID = regexp.MustCompile(`(?i)^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$`) var validUUID = regexp.MustCompile(`(?i)^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$`)

View File

@ -6,28 +6,43 @@ import (
"github.com/hashicorp/go-memdb" "github.com/hashicorp/go-memdb"
) )
// schemaFn is an interface function used to create and return // newDBSchema creates and returns the memdb schema for the Store.
// new memdb schema structs for constructing an in-memory db. func newDBSchema() *memdb.DBSchema {
type schemaFn func() *memdb.TableSchema db := &memdb.DBSchema{Tables: make(map[string]*memdb.TableSchema)}
// schemas is used to register schemas with the state store. addTableSchemas(db,
var schemas []schemaFn authMethodsTableSchema,
autopilotConfigTableSchema,
// registerSchema registers a new schema with the state store. This should bindingRulesTableSchema,
// get called at package init() time. caBuiltinProviderTableSchema,
func registerSchema(fn schemaFn) { caConfigTableSchema,
schemas = append(schemas, fn) caRootTableSchema,
checksTableSchema,
configTableSchema,
coordinatesTableSchema,
federationStateTableSchema,
gatewayServicesTableSchema,
indexTableSchema,
intentionsTableSchema,
kvsTableSchema,
meshTopologyTableSchema,
nodesTableSchema,
policiesTableSchema,
preparedQueriesTableSchema,
rolesTableSchema,
servicesTableSchema,
sessionChecksTableSchema,
sessionsTableSchema,
systemMetadataTableSchema,
tokensTableSchema,
tombstonesTableSchema,
usageTableSchema,
)
withEnterpriseSchema(db)
return db
} }
// stateStoreSchema is used to return the combined schema for func addTableSchemas(db *memdb.DBSchema, schemas ...func() *memdb.TableSchema) {
// the state store.
func stateStoreSchema() *memdb.DBSchema {
// Create the root DB schema
db := &memdb.DBSchema{
Tables: make(map[string]*memdb.TableSchema),
}
// Add the tables to the root schema
for _, fn := range schemas { for _, fn := range schemas {
schema := fn() schema := fn()
if _, ok := db.Tables[schema.Name]; ok { if _, ok := db.Tables[schema.Name]; ok {
@ -35,8 +50,6 @@ func stateStoreSchema() *memdb.DBSchema {
} }
db.Tables[schema.Name] = schema db.Tables[schema.Name] = schema
} }
withEnterpriseSchema(db)
return db
} }
// indexTableSchema returns a new table schema used for tracking various indexes // indexTableSchema returns a new table schema used for tracking various indexes
@ -57,7 +70,3 @@ func indexTableSchema() *memdb.TableSchema {
}, },
} }
} }
func init() {
registerSchema(indexTableSchema)
}

View File

@ -15,7 +15,7 @@ import (
) )
func TestStateStoreSchema(t *testing.T) { func TestStateStoreSchema(t *testing.T) {
schema := stateStoreSchema() schema := newDBSchema()
require.NoError(t, schema.Validate()) require.NoError(t, schema.Validate())
_, err := memdb.NewMemDB(schema) _, err := memdb.NewMemDB(schema)

View File

@ -6,8 +6,9 @@ import (
"strings" "strings"
"time" "time"
"github.com/hashicorp/consul/agent/structs"
"github.com/hashicorp/go-memdb" "github.com/hashicorp/go-memdb"
"github.com/hashicorp/consul/agent/structs"
) )
// sessionsTableSchema returns a new table schema used for storing session // sessionsTableSchema returns a new table schema used for storing session
@ -129,11 +130,6 @@ func (index *CheckIDIndex) PrefixFromArgs(args ...interface{}) ([]byte, error) {
return val, nil return val, nil
} }
func init() {
registerSchema(sessionsTableSchema)
registerSchema(sessionChecksTableSchema)
}
// Sessions is used to pull the full list of sessions for use during snapshots. // Sessions is used to pull the full list of sessions for use during snapshots.
func (s *Snapshot) Sessions() (memdb.ResultIterator, error) { func (s *Snapshot) Sessions() (memdb.ResultIterator, error) {
iter, err := s.tx.Get("sessions", "id") iter, err := s.tx.Get("sessions", "id")

View File

@ -155,7 +155,7 @@ type sessionCheck struct {
// NewStateStore creates a new in-memory state storage layer. // NewStateStore creates a new in-memory state storage layer.
func NewStateStore(gc *TombstoneGC) *Store { func NewStateStore(gc *TombstoneGC) *Store {
// Create the in-memory DB. // Create the in-memory DB.
schema := stateStoreSchema() schema := newDBSchema()
db, err := memdb.NewMemDB(schema) db, err := memdb.NewMemDB(schema)
if err != nil { if err != nil {
// the only way for NewMemDB to error is if the schema is invalid. The // the only way for NewMemDB to error is if the schema is invalid. The

View File

@ -26,9 +26,6 @@ func systemMetadataTableSchema() *memdb.TableSchema {
}, },
} }
} }
func init() {
registerSchema(systemMetadataTableSchema)
}
// SystemMetadataEntries used to pull all the system metadata entries for the snapshot. // SystemMetadataEntries used to pull all the system metadata entries for the snapshot.
func (s *Snapshot) SystemMetadataEntries() ([]*structs.SystemMetadataEntry, error) { func (s *Snapshot) SystemMetadataEntries() ([]*structs.SystemMetadataEntry, error) {

View File

@ -31,10 +31,6 @@ func usageTableSchema() *memdb.TableSchema {
} }
} }
func init() {
registerSchema(usageTableSchema)
}
// UsageEntry represents a count of some arbitrary identifier within the // UsageEntry represents a count of some arbitrary identifier within the
// state store, along with the last seen index. // state store, along with the last seen index.
type UsageEntry struct { type UsageEntry struct {