Skip to content

Commit

Permalink
Merge pull request #9719 from hashicorp/oss/state-store-4
Browse files Browse the repository at this point in the history
state: remove registerSchema
  • Loading branch information
dnephin authored Feb 5, 2021
2 parents 1c4e0cf + 05d5ec4 commit a4690ac
Show file tree
Hide file tree
Showing 16 changed files with 46 additions and 95 deletions.
8 changes: 0 additions & 8 deletions agent/consul/state/acl.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,14 +212,6 @@ func (s *TokenExpirationIndex) FromArgs(args ...interface{}) ([]byte, error) {
return buf, nil
}

func init() {
registerSchema(tokensTableSchema)
registerSchema(policiesTableSchema)
registerSchema(rolesTableSchema)
registerSchema(bindingRulesTableSchema)
registerSchema(authMethodsTableSchema)
}

// ACLTokens is used when saving a snapshot
func (s *Snapshot) ACLTokens() (memdb.ResultIterator, error) {
iter, err := s.tx.Get("acl-tokens", "id")
Expand Down
7 changes: 2 additions & 5 deletions agent/consul/state/autopilot.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package state
import (
"fmt"

"github.com/hashicorp/consul/agent/structs"
"github.com/hashicorp/go-memdb"

"github.com/hashicorp/consul/agent/structs"
)

// autopilotConfigTableSchema returns a new table schema used for storing
Expand All @@ -25,10 +26,6 @@ func autopilotConfigTableSchema() *memdb.TableSchema {
}
}

func init() {
registerSchema(autopilotConfigTableSchema)
}

// Autopilot is used to pull the autopilot config from the snapshot.
func (s *Snapshot) Autopilot() (*structs.AutopilotConfig, error) {
c, err := s.tx.First("autopilot-config", "id")
Expand Down
8 changes: 0 additions & 8 deletions agent/consul/state/catalog_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,11 +347,3 @@ func (index *ServiceNameIndex) PrefixFromArgs(args ...interface{}) ([]byte, erro
}
return val, nil
}

func init() {
registerSchema(nodesTableSchema)
registerSchema(servicesTableSchema)
registerSchema(checksTableSchema)
registerSchema(gatewayServicesTableSchema)
registerSchema(meshTopologyTableSchema)
}
4 changes: 0 additions & 4 deletions agent/consul/state/config_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,6 @@ func (s *ConfigEntryLinkIndex) PrefixFromArgs(args ...interface{}) ([]byte, erro
return val, nil
}

func init() {
registerSchema(configTableSchema)
}

// ConfigEntries is used to pull all the config entries for the snapshot.
func (s *Snapshot) ConfigEntries() ([]structs.ConfigEntry, error) {
entries, err := s.tx.Get(tableConfigEntries, "id")
Expand Down
6 changes: 0 additions & 6 deletions agent/consul/state/connect_ca.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
func (s *Snapshot) CAConfig() (*structs.CAConfiguration, error) {
c, err := s.tx.First(tableConnectCAConfig, "id")
Expand Down
7 changes: 2 additions & 5 deletions agent/consul/state/coordinate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package state
import (
"fmt"

"github.com/hashicorp/go-memdb"

"github.com/hashicorp/consul/agent/structs"
"github.com/hashicorp/consul/lib"
"github.com/hashicorp/go-memdb"
)

// coordinatesTableSchema returns a new table schema used for storing
Expand Down Expand Up @@ -47,10 +48,6 @@ func coordinatesTableSchema() *memdb.TableSchema {
}
}

func init() {
registerSchema(coordinatesTableSchema)
}

// Coordinates is used to pull all the coordinates from the snapshot.
func (s *Snapshot) Coordinates() (memdb.ResultIterator, error) {
iter, err := s.tx.Get("coordinates", "id")
Expand Down
4 changes: 0 additions & 4 deletions agent/consul/state/federation_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ func federationStateTableSchema() *memdb.TableSchema {
}
}

func init() {
registerSchema(federationStateTableSchema)
}

// FederationStates is used to pull all the federation states for the snapshot.
func (s *Snapshot) FederationStates() ([]*structs.FederationState, error) {
configs, err := s.tx.Get(tableFederationStates, "id")
Expand Down
4 changes: 0 additions & 4 deletions agent/consul/state/intention.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,6 @@ func intentionsTableSchema() *memdb.TableSchema {
}
}

func init() {
registerSchema(intentionsTableSchema)
}

// LegacyIntentions is used to pull all the intentions from the snapshot.
//
// Deprecated: service-intentions config entries are handled as config entries
Expand Down
8 changes: 2 additions & 6 deletions agent/consul/state/kvs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"fmt"
"time"

"github.com/hashicorp/consul/agent/structs"
"github.com/hashicorp/go-memdb"

"github.com/hashicorp/consul/agent/structs"
)

// kvsTableSchema returns a new table schema used for storing key/value data for
Expand Down Expand Up @@ -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.
func (s *Snapshot) KVs() (memdb.ResultIterator, error) {
iter, err := s.tx.Get("kvs", "id_prefix")
Expand Down
7 changes: 2 additions & 5 deletions agent/consul/state/prepared_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"fmt"
"regexp"

"github.com/hashicorp/go-memdb"

"github.com/hashicorp/consul/agent/consul/prepared_query"
"github.com/hashicorp/consul/agent/structs"
"github.com/hashicorp/go-memdb"
)

// preparedQueriesTableSchema returns a new table schema used for storing
Expand Down Expand Up @@ -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
var validUUID = regexp.MustCompile(`(?i)^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$`)

Expand Down
59 changes: 34 additions & 25 deletions agent/consul/state/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,50 @@ import (
"github.com/hashicorp/go-memdb"
)

// schemaFn is an interface function used to create and return
// new memdb schema structs for constructing an in-memory db.
type schemaFn func() *memdb.TableSchema
// newDBSchema creates and returns the memdb schema for the Store.
func newDBSchema() *memdb.DBSchema {
db := &memdb.DBSchema{Tables: make(map[string]*memdb.TableSchema)}

// schemas is used to register schemas with the state store.
var schemas []schemaFn

// registerSchema registers a new schema with the state store. This should
// get called at package init() time.
func registerSchema(fn schemaFn) {
schemas = append(schemas, fn)
addTableSchemas(db,
authMethodsTableSchema,
autopilotConfigTableSchema,
bindingRulesTableSchema,
caBuiltinProviderTableSchema,
caConfigTableSchema,
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
// 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
func addTableSchemas(db *memdb.DBSchema, schemas ...func() *memdb.TableSchema) {
for _, fn := range schemas {
schema := fn()
if _, ok := db.Tables[schema.Name]; ok {
panic(fmt.Sprintf("duplicate table name: %s", schema.Name))
}
db.Tables[schema.Name] = schema
}
withEnterpriseSchema(db)
return db
}

// indexTableSchema returns a new table schema used for tracking various indexes
Expand All @@ -57,7 +70,3 @@ func indexTableSchema() *memdb.TableSchema {
},
}
}

func init() {
registerSchema(indexTableSchema)
}
2 changes: 1 addition & 1 deletion agent/consul/state/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

func TestStateStoreSchema(t *testing.T) {
schema := stateStoreSchema()
schema := newDBSchema()
require.NoError(t, schema.Validate())

_, err := memdb.NewMemDB(schema)
Expand Down
8 changes: 2 additions & 6 deletions agent/consul/state/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (
"strings"
"time"

"github.com/hashicorp/consul/agent/structs"
"github.com/hashicorp/go-memdb"

"github.com/hashicorp/consul/agent/structs"
)

// sessionsTableSchema returns a new table schema used for storing session
Expand Down Expand Up @@ -129,11 +130,6 @@ func (index *CheckIDIndex) PrefixFromArgs(args ...interface{}) ([]byte, error) {
return val, nil
}

func init() {
registerSchema(sessionsTableSchema)
registerSchema(sessionChecksTableSchema)
}

// Sessions is used to pull the full list of sessions for use during snapshots.
func (s *Snapshot) Sessions() (memdb.ResultIterator, error) {
iter, err := s.tx.Get("sessions", "id")
Expand Down
2 changes: 1 addition & 1 deletion agent/consul/state/state_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ type sessionCheck struct {
// NewStateStore creates a new in-memory state storage layer.
func NewStateStore(gc *TombstoneGC) *Store {
// Create the in-memory DB.
schema := stateStoreSchema()
schema := newDBSchema()
db, err := memdb.NewMemDB(schema)
if err != nil {
// the only way for NewMemDB to error is if the schema is invalid. The
Expand Down
3 changes: 0 additions & 3 deletions agent/consul/state/system_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ func systemMetadataTableSchema() *memdb.TableSchema {
},
}
}
func init() {
registerSchema(systemMetadataTableSchema)
}

// SystemMetadataEntries used to pull all the system metadata entries for the snapshot.
func (s *Snapshot) SystemMetadataEntries() ([]*structs.SystemMetadataEntry, error) {
Expand Down
4 changes: 0 additions & 4 deletions agent/consul/state/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ func usageTableSchema() *memdb.TableSchema {
}
}

func init() {
registerSchema(usageTableSchema)
}

// UsageEntry represents a count of some arbitrary identifier within the
// state store, along with the last seen index.
type UsageEntry struct {
Expand Down

0 comments on commit a4690ac

Please sign in to comment.