state: convert services.kind to functional indexer pattern

This commit is contained in:
Daniel Nephin 2021-03-26 18:38:52 -04:00
parent b6553af222
commit 9eea19da59
5 changed files with 26 additions and 47 deletions

View File

@ -2304,8 +2304,11 @@ func serviceDumpKindTxn(tx ReadTxn, ws memdb.WatchSet, kind structs.ServiceKind,
// entries
idx := catalogServiceKindMaxIndex(tx, ws, kind, entMeta)
// Query the state store for the service.
services, err := catalogServiceListByKind(tx, kind, entMeta)
if entMeta == nil {
entMeta = structs.DefaultEnterpriseMeta()
}
q := Query{Value: string(kind), EnterpriseMeta: *entMeta}
services, err := tx.Get(tableServices, indexKind, q)
if err != nil {
return 0, nil, fmt.Errorf("failed service lookup: %s", err)
}
@ -2548,7 +2551,11 @@ func terminatingConfigGatewayServices(
// updateGatewayNamespace is used to target all services within a namespace
func updateGatewayNamespace(tx WriteTxn, idx uint64, service *structs.GatewayService, entMeta *structs.EnterpriseMeta) error {
services, err := catalogServiceListByKind(tx, structs.ServiceKindTypical, entMeta)
if entMeta == nil {
entMeta = structs.DefaultEnterpriseMeta()
}
q := Query{Value: string(structs.ServiceKindTypical), EnterpriseMeta: *entMeta}
services, err := tx.Get(tableServices, indexKind, q)
if err != nil {
return fmt.Errorf("failed querying services: %s", err)
}

View File

@ -97,10 +97,6 @@ func catalogServiceListNoWildcard(tx ReadTxn, _ *structs.EnterpriseMeta) (memdb.
return tx.Get(tableServices, indexID)
}
func catalogServiceListByKind(tx ReadTxn, kind structs.ServiceKind, _ *structs.EnterpriseMeta) (memdb.ResultIterator, error) {
return tx.Get(tableServices, "kind", string(kind))
}
func catalogServiceListByNode(tx ReadTxn, node string, _ *structs.EnterpriseMeta, _ bool) (memdb.ResultIterator, error) {
return tx.Get(tableServices, indexNode, Query{Value: node})
}

View File

@ -239,7 +239,7 @@ func testIndexerTableServices() map[string]indexerTestCase {
},
indexKind: {
read: indexValue{
source: "connect-proxy",
source: Query{Value: "connect-proxy"},
expected: []byte("connect-proxy\x00"),
},
write: indexValue{

View File

@ -124,7 +124,10 @@ func servicesTableSchema() *memdb.TableSchema {
Name: indexKind,
AllowMissing: false,
Unique: false,
Indexer: &IndexServiceKind{},
Indexer: indexerSingle{
readIndex: indexFromQuery,
writeIndex: indexKindFromServiceNode,
},
},
},
}
@ -223,6 +226,17 @@ func connectNameFromServiceNode(sn *structs.ServiceNode) (string, bool) {
}
}
func indexKindFromServiceNode(raw interface{}) ([]byte, error) {
n, ok := raw.(*structs.ServiceNode)
if !ok {
return nil, fmt.Errorf("unexpected type %T for structs.ServiceNode index", raw)
}
var b indexBuilder
b.String(strings.ToLower(string(n.ServiceKind)))
return b.Bytes(), nil
}
// checksTableSchema returns a new table schema used for storing and indexing
// health check information. Health checks have a number of different attributes
// we want to filter by, so this table is a bit more complex.

View File

@ -1,38 +0,0 @@
package state
import (
"fmt"
"strings"
"github.com/hashicorp/consul/agent/structs"
)
// IndexServiceKind indexes a *struct.ServiceNode for querying by
// the services kind. We need a custom indexer because of the default
// kind being the empty string. The StringFieldIndex in memdb seems to
// treate the empty string as missing and doesn't work correctly when we actually
// want to index ""
type IndexServiceKind struct{}
func (idx *IndexServiceKind) FromObject(obj interface{}) (bool, []byte, error) {
sn, ok := obj.(*structs.ServiceNode)
if !ok {
return false, nil, fmt.Errorf("Object must be ServiceNode, got %T", obj)
}
return true, append([]byte(strings.ToLower(string(sn.ServiceKind))), '\x00'), nil
}
func (idx *IndexServiceKind) FromArgs(args ...interface{}) ([]byte, error) {
if len(args) != 1 {
return nil, fmt.Errorf("must provide only a single argument")
}
arg, ok := args[0].(string)
if !ok {
return nil, fmt.Errorf("argument must be a structs.ServiceKind: %#v", args[0])
}
// Add the null character as a terminator
return append([]byte(strings.ToLower(arg)), '\x00'), nil
}