mirror of
https://github.com/status-im/consul.git
synced 2025-02-03 01:14:23 +00:00
state: reduce duplication in catalog table schema
This commit is contained in:
parent
7de186f291
commit
72960388a3
@ -1794,6 +1794,12 @@ type NodeServiceQuery struct {
|
|||||||
structs.EnterpriseMeta
|
structs.EnterpriseMeta
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NamespaceOrDefault exists because structs.EnterpriseMeta uses a pointer
|
||||||
|
// receiver for this method. Remove once that is fixed.
|
||||||
|
func (q NodeServiceQuery) NamespaceOrDefault() string {
|
||||||
|
return q.EnterpriseMeta.NamespaceOrDefault()
|
||||||
|
}
|
||||||
|
|
||||||
// deleteCheckTxn is the inner method used to call a health
|
// deleteCheckTxn is the inner method used to call a health
|
||||||
// check deletion within an existing transaction.
|
// check deletion within an existing transaction.
|
||||||
func (s *Store) deleteCheckTxn(tx WriteTxn, idx uint64, node string, checkID types.CheckID, entMeta *structs.EnterpriseMeta) error {
|
func (s *Store) deleteCheckTxn(tx WriteTxn, idx uint64, node string, checkID types.CheckID, entMeta *structs.EnterpriseMeta) error {
|
||||||
|
@ -4,7 +4,6 @@ package state
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
|
||||||
|
|
||||||
memdb "github.com/hashicorp/go-memdb"
|
memdb "github.com/hashicorp/go-memdb"
|
||||||
|
|
||||||
@ -13,128 +12,6 @@ import (
|
|||||||
|
|
||||||
func withEnterpriseSchema(_ *memdb.DBSchema) {}
|
func withEnterpriseSchema(_ *memdb.DBSchema) {}
|
||||||
|
|
||||||
func indexNodeServiceFromHealthCheck(raw interface{}) ([]byte, error) {
|
|
||||||
hc, ok := raw.(*structs.HealthCheck)
|
|
||||||
if !ok {
|
|
||||||
return nil, fmt.Errorf("unexpected type %T for structs.HealthCheck index", raw)
|
|
||||||
}
|
|
||||||
|
|
||||||
if hc.Node == "" {
|
|
||||||
return nil, errMissingValueForIndex
|
|
||||||
}
|
|
||||||
|
|
||||||
var b indexBuilder
|
|
||||||
b.String(strings.ToLower(hc.Node))
|
|
||||||
b.String(strings.ToLower(hc.ServiceID))
|
|
||||||
return b.Bytes(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func indexFromNodeServiceQuery(arg interface{}) ([]byte, error) {
|
|
||||||
hc, ok := arg.(NodeServiceQuery)
|
|
||||||
if !ok {
|
|
||||||
return nil, fmt.Errorf("unexpected type %T for NodeServiceQuery index", arg)
|
|
||||||
}
|
|
||||||
|
|
||||||
var b indexBuilder
|
|
||||||
b.String(strings.ToLower(hc.Node))
|
|
||||||
b.String(strings.ToLower(hc.Service))
|
|
||||||
return b.Bytes(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func indexFromNode(raw interface{}) ([]byte, error) {
|
|
||||||
n, ok := raw.(*structs.Node)
|
|
||||||
if !ok {
|
|
||||||
return nil, fmt.Errorf("unexpected type %T for structs.Node index", raw)
|
|
||||||
}
|
|
||||||
|
|
||||||
if n.Node == "" {
|
|
||||||
return nil, errMissingValueForIndex
|
|
||||||
}
|
|
||||||
|
|
||||||
var b indexBuilder
|
|
||||||
b.String(strings.ToLower(n.Node))
|
|
||||||
return b.Bytes(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// indexFromNodeQuery builds an index key where Query.Value is lowercase, and is
|
|
||||||
// a required value.
|
|
||||||
func indexFromNodeQuery(arg interface{}) ([]byte, error) {
|
|
||||||
q, ok := arg.(Query)
|
|
||||||
if !ok {
|
|
||||||
return nil, fmt.Errorf("unexpected type %T for Query index", arg)
|
|
||||||
}
|
|
||||||
|
|
||||||
var b indexBuilder
|
|
||||||
b.String(strings.ToLower(q.Value))
|
|
||||||
return b.Bytes(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func indexFromNodeIdentity(raw interface{}) ([]byte, error) {
|
|
||||||
n, ok := raw.(interface {
|
|
||||||
NodeIdentity() structs.Identity
|
|
||||||
})
|
|
||||||
if !ok {
|
|
||||||
return nil, fmt.Errorf("unexpected type %T for index, type must provide NodeIdentity()", raw)
|
|
||||||
}
|
|
||||||
|
|
||||||
id := n.NodeIdentity()
|
|
||||||
if id.ID == "" {
|
|
||||||
return nil, errMissingValueForIndex
|
|
||||||
}
|
|
||||||
|
|
||||||
var b indexBuilder
|
|
||||||
b.String(strings.ToLower(id.ID))
|
|
||||||
return b.Bytes(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func indexFromServiceNode(raw interface{}) ([]byte, error) {
|
|
||||||
n, ok := raw.(*structs.ServiceNode)
|
|
||||||
if !ok {
|
|
||||||
return nil, fmt.Errorf("unexpected type %T for structs.ServiceNode index", raw)
|
|
||||||
}
|
|
||||||
|
|
||||||
if n.Node == "" {
|
|
||||||
return nil, errMissingValueForIndex
|
|
||||||
}
|
|
||||||
|
|
||||||
var b indexBuilder
|
|
||||||
b.String(strings.ToLower(n.Node))
|
|
||||||
b.String(strings.ToLower(n.ServiceID))
|
|
||||||
return b.Bytes(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func indexFromHealthCheck(raw interface{}) ([]byte, error) {
|
|
||||||
hc, ok := raw.(*structs.HealthCheck)
|
|
||||||
if !ok {
|
|
||||||
return nil, fmt.Errorf("unexpected type %T for structs.HealthCheck index", raw)
|
|
||||||
}
|
|
||||||
|
|
||||||
if hc.Node == "" || hc.CheckID == "" {
|
|
||||||
return nil, errMissingValueForIndex
|
|
||||||
}
|
|
||||||
|
|
||||||
var b indexBuilder
|
|
||||||
b.String(strings.ToLower(hc.Node))
|
|
||||||
b.String(strings.ToLower(string(hc.CheckID)))
|
|
||||||
return b.Bytes(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func indexFromNodeCheckID(raw interface{}) ([]byte, error) {
|
|
||||||
hc, ok := raw.(NodeCheckQuery)
|
|
||||||
if !ok {
|
|
||||||
return nil, fmt.Errorf("unexpected type %T for NodeCheckQuery index", raw)
|
|
||||||
}
|
|
||||||
|
|
||||||
if hc.Node == "" || hc.CheckID == "" {
|
|
||||||
return nil, errMissingValueForIndex
|
|
||||||
}
|
|
||||||
|
|
||||||
var b indexBuilder
|
|
||||||
b.String(strings.ToLower(hc.Node))
|
|
||||||
b.String(strings.ToLower(hc.CheckID))
|
|
||||||
return b.Bytes(), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func serviceIndexName(name string, _ *structs.EnterpriseMeta) string {
|
func serviceIndexName(name string, _ *structs.EnterpriseMeta) string {
|
||||||
return fmt.Sprintf("service.%s", name)
|
return fmt.Sprintf("service.%s", name)
|
||||||
}
|
}
|
||||||
|
@ -39,8 +39,8 @@ func nodesTableSchema() *memdb.TableSchema {
|
|||||||
AllowMissing: false,
|
AllowMissing: false,
|
||||||
Unique: true,
|
Unique: true,
|
||||||
Indexer: indexerSingle{
|
Indexer: indexerSingle{
|
||||||
readIndex: readIndex(indexFromNodeQuery),
|
readIndex: indexFromNodeQuery,
|
||||||
writeIndex: writeIndex(indexFromNode),
|
writeIndex: indexFromNode,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
"uuid": {
|
"uuid": {
|
||||||
@ -62,6 +62,33 @@ func nodesTableSchema() *memdb.TableSchema {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func indexFromNode(raw interface{}) ([]byte, error) {
|
||||||
|
n, ok := raw.(*structs.Node)
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("unexpected type %T for structs.Node index", raw)
|
||||||
|
}
|
||||||
|
|
||||||
|
if n.Node == "" {
|
||||||
|
return nil, errMissingValueForIndex
|
||||||
|
}
|
||||||
|
|
||||||
|
var b indexBuilder
|
||||||
|
b.String(strings.ToLower(n.Node))
|
||||||
|
return b.Bytes(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: remove once all uses of indexFromQuery are ported
|
||||||
|
func indexFromNodeQuery(arg interface{}) ([]byte, error) {
|
||||||
|
q, ok := arg.(Query)
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("unexpected type %T for Query index", arg)
|
||||||
|
}
|
||||||
|
|
||||||
|
var b indexBuilder
|
||||||
|
b.String(strings.ToLower(q.Value))
|
||||||
|
return b.Bytes(), nil
|
||||||
|
}
|
||||||
|
|
||||||
// servicesTableSchema returns a new table schema used to store information
|
// servicesTableSchema returns a new table schema used to store information
|
||||||
// about services.
|
// about services.
|
||||||
func servicesTableSchema() *memdb.TableSchema {
|
func servicesTableSchema() *memdb.TableSchema {
|
||||||
@ -73,9 +100,9 @@ func servicesTableSchema() *memdb.TableSchema {
|
|||||||
AllowMissing: false,
|
AllowMissing: false,
|
||||||
Unique: true,
|
Unique: true,
|
||||||
Indexer: indexerSingleWithPrefix{
|
Indexer: indexerSingleWithPrefix{
|
||||||
readIndex: readIndex(indexFromNodeServiceQuery),
|
readIndex: indexFromNodeServiceQuery,
|
||||||
writeIndex: writeIndex(indexFromServiceNode),
|
writeIndex: indexFromServiceNode,
|
||||||
prefixIndex: prefixIndex(prefixIndexFromQuery),
|
prefixIndex: prefixIndexFromQuery,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
indexNode: {
|
indexNode: {
|
||||||
@ -83,8 +110,8 @@ func servicesTableSchema() *memdb.TableSchema {
|
|||||||
AllowMissing: false,
|
AllowMissing: false,
|
||||||
Unique: false,
|
Unique: false,
|
||||||
Indexer: indexerSingle{
|
Indexer: indexerSingle{
|
||||||
readIndex: readIndex(indexFromNodeQuery),
|
readIndex: indexFromNodeQuery,
|
||||||
writeIndex: writeIndex(indexFromNodeIdentity),
|
writeIndex: indexFromNodeIdentity,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
indexService: {
|
indexService: {
|
||||||
@ -112,6 +139,52 @@ func servicesTableSchema() *memdb.TableSchema {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func indexFromNodeServiceQuery(arg interface{}) ([]byte, error) {
|
||||||
|
q, ok := arg.(NodeServiceQuery)
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("unexpected type %T for NodeServiceQuery index", arg)
|
||||||
|
}
|
||||||
|
|
||||||
|
var b indexBuilder
|
||||||
|
b.String(strings.ToLower(q.Node))
|
||||||
|
b.String(strings.ToLower(q.Service))
|
||||||
|
return b.Bytes(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func indexFromServiceNode(raw interface{}) ([]byte, error) {
|
||||||
|
n, ok := raw.(*structs.ServiceNode)
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("unexpected type %T for structs.ServiceNode index", raw)
|
||||||
|
}
|
||||||
|
|
||||||
|
if n.Node == "" {
|
||||||
|
return nil, errMissingValueForIndex
|
||||||
|
}
|
||||||
|
|
||||||
|
var b indexBuilder
|
||||||
|
b.String(strings.ToLower(n.Node))
|
||||||
|
b.String(strings.ToLower(n.ServiceID))
|
||||||
|
return b.Bytes(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func indexFromNodeIdentity(raw interface{}) ([]byte, error) {
|
||||||
|
n, ok := raw.(interface {
|
||||||
|
NodeIdentity() structs.Identity
|
||||||
|
})
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("unexpected type %T for index, type must provide NodeIdentity()", raw)
|
||||||
|
}
|
||||||
|
|
||||||
|
id := n.NodeIdentity()
|
||||||
|
if id.ID == "" {
|
||||||
|
return nil, errMissingValueForIndex
|
||||||
|
}
|
||||||
|
|
||||||
|
var b indexBuilder
|
||||||
|
b.String(strings.ToLower(id.ID))
|
||||||
|
return b.Bytes(), nil
|
||||||
|
}
|
||||||
|
|
||||||
// checksTableSchema returns a new table schema used for storing and indexing
|
// checksTableSchema returns a new table schema used for storing and indexing
|
||||||
// health check information. Health checks have a number of different attributes
|
// health check information. Health checks have a number of different attributes
|
||||||
// we want to filter by, so this table is a bit more complex.
|
// we want to filter by, so this table is a bit more complex.
|
||||||
@ -124,9 +197,9 @@ func checksTableSchema() *memdb.TableSchema {
|
|||||||
AllowMissing: false,
|
AllowMissing: false,
|
||||||
Unique: true,
|
Unique: true,
|
||||||
Indexer: indexerSingleWithPrefix{
|
Indexer: indexerSingleWithPrefix{
|
||||||
readIndex: readIndex(indexFromNodeCheckID),
|
readIndex: indexFromNodeCheckQuery,
|
||||||
prefixIndex: prefixIndex(prefixIndexFromQuery),
|
writeIndex: indexFromHealthCheck,
|
||||||
writeIndex: writeIndex(indexFromHealthCheck),
|
prefixIndex: prefixIndexFromQuery,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
indexStatus: {
|
indexStatus: {
|
||||||
@ -152,8 +225,8 @@ func checksTableSchema() *memdb.TableSchema {
|
|||||||
AllowMissing: true,
|
AllowMissing: true,
|
||||||
Unique: false,
|
Unique: false,
|
||||||
Indexer: indexerSingle{
|
Indexer: indexerSingle{
|
||||||
readIndex: readIndex(indexFromNodeQuery),
|
readIndex: indexFromNodeQuery,
|
||||||
writeIndex: writeIndex(indexFromNodeIdentity),
|
writeIndex: indexFromNodeIdentity,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
indexNodeService: {
|
indexNodeService: {
|
||||||
@ -161,14 +234,62 @@ func checksTableSchema() *memdb.TableSchema {
|
|||||||
AllowMissing: true,
|
AllowMissing: true,
|
||||||
Unique: false,
|
Unique: false,
|
||||||
Indexer: indexerSingle{
|
Indexer: indexerSingle{
|
||||||
readIndex: readIndex(indexFromNodeServiceQuery),
|
readIndex: indexFromNodeServiceQuery,
|
||||||
writeIndex: writeIndex(indexNodeServiceFromHealthCheck),
|
writeIndex: indexNodeServiceFromHealthCheck,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func indexFromNodeCheckQuery(raw interface{}) ([]byte, error) {
|
||||||
|
hc, ok := raw.(NodeCheckQuery)
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("unexpected type %T for NodeCheckQuery index", raw)
|
||||||
|
}
|
||||||
|
|
||||||
|
if hc.Node == "" || hc.CheckID == "" {
|
||||||
|
return nil, errMissingValueForIndex
|
||||||
|
}
|
||||||
|
|
||||||
|
var b indexBuilder
|
||||||
|
b.String(strings.ToLower(hc.Node))
|
||||||
|
b.String(strings.ToLower(hc.CheckID))
|
||||||
|
return b.Bytes(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func indexFromHealthCheck(raw interface{}) ([]byte, error) {
|
||||||
|
hc, ok := raw.(*structs.HealthCheck)
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("unexpected type %T for structs.HealthCheck index", raw)
|
||||||
|
}
|
||||||
|
|
||||||
|
if hc.Node == "" || hc.CheckID == "" {
|
||||||
|
return nil, errMissingValueForIndex
|
||||||
|
}
|
||||||
|
|
||||||
|
var b indexBuilder
|
||||||
|
b.String(strings.ToLower(hc.Node))
|
||||||
|
b.String(strings.ToLower(string(hc.CheckID)))
|
||||||
|
return b.Bytes(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func indexNodeServiceFromHealthCheck(raw interface{}) ([]byte, error) {
|
||||||
|
hc, ok := raw.(*structs.HealthCheck)
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("unexpected type %T for structs.HealthCheck index", raw)
|
||||||
|
}
|
||||||
|
|
||||||
|
if hc.Node == "" {
|
||||||
|
return nil, errMissingValueForIndex
|
||||||
|
}
|
||||||
|
|
||||||
|
var b indexBuilder
|
||||||
|
b.String(strings.ToLower(hc.Node))
|
||||||
|
b.String(strings.ToLower(hc.ServiceID))
|
||||||
|
return b.Bytes(), nil
|
||||||
|
}
|
||||||
|
|
||||||
// gatewayServicesTableSchema returns a new table schema used to store information
|
// gatewayServicesTableSchema returns a new table schema used to store information
|
||||||
// about services associated with terminating gateways.
|
// about services associated with terminating gateways.
|
||||||
func gatewayServicesTableSchema() *memdb.TableSchema {
|
func gatewayServicesTableSchema() *memdb.TableSchema {
|
||||||
@ -331,3 +452,9 @@ type NodeCheckQuery struct {
|
|||||||
CheckID string
|
CheckID string
|
||||||
structs.EnterpriseMeta
|
structs.EnterpriseMeta
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NamespaceOrDefault exists because structs.EnterpriseMeta uses a pointer
|
||||||
|
// receiver for this method. Remove once that is fixed.
|
||||||
|
func (q NodeCheckQuery) NamespaceOrDefault() string {
|
||||||
|
return q.EnterpriseMeta.NamespaceOrDefault()
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user