state: handle wildcard for services.ID index

When listing services, use the id_prefix directly if wildcards are allowed.

Error if a wildcard is used for a query that does not index the wildcard
This commit is contained in:
Daniel Nephin 2021-03-10 18:26:44 -05:00
parent 627c469f08
commit dbd3cef1ed
2 changed files with 10 additions and 6 deletions

View File

@ -691,7 +691,7 @@ func (s *Store) Services(ws memdb.WatchSet, entMeta *structs.EnterpriseMeta) (ui
idx := catalogServicesMaxIndex(tx, entMeta) idx := catalogServicesMaxIndex(tx, entMeta)
// List all the services. // List all the services.
services, err := catalogServiceList(tx, entMeta, false) services, err := catalogServiceListNoWildcard(tx, entMeta)
if err != nil { if err != nil {
return 0, nil, fmt.Errorf("failed querying services: %s", err) return 0, nil, fmt.Errorf("failed querying services: %s", err)
} }
@ -735,7 +735,7 @@ func serviceListTxn(tx ReadTxn, ws memdb.WatchSet,
include func(svc *structs.ServiceNode) bool, entMeta *structs.EnterpriseMeta) (uint64, structs.ServiceList, error) { include func(svc *structs.ServiceNode) bool, entMeta *structs.EnterpriseMeta) (uint64, structs.ServiceList, error) {
idx := catalogServicesMaxIndex(tx, entMeta) idx := catalogServicesMaxIndex(tx, entMeta)
services, err := catalogServiceList(tx, entMeta, true) services, err := tx.Get(tableServices, indexID+"_prefix", entMeta)
if err != nil { if err != nil {
return 0, nil, fmt.Errorf("failed querying services: %s", err) return 0, nil, fmt.Errorf("failed querying services: %s", err)
} }
@ -784,7 +784,7 @@ func (s *Store) ServicesByNodeMeta(ws memdb.WatchSet, filters map[string]string,
// We don't want to track an unlimited number of services, so we pull a // We don't want to track an unlimited number of services, so we pull a
// top-level watch to use as a fallback. // top-level watch to use as a fallback.
allServices, err := catalogServiceList(tx, entMeta, false) allServices, err := catalogServiceListNoWildcard(tx, entMeta)
if err != nil { if err != nil {
return 0, nil, fmt.Errorf("failed services lookup: %s", err) return 0, nil, fmt.Errorf("failed services lookup: %s", err)
} }
@ -1052,7 +1052,7 @@ func (s *Store) ServiceAddressNodes(ws memdb.WatchSet, address string, entMeta *
defer tx.Abort() defer tx.Abort()
// List all the services. // List all the services.
services, err := catalogServiceList(tx, entMeta, true) services, err := tx.Get(tableServices, indexID+"_prefix", entMeta)
if err != nil { if err != nil {
return 0, nil, fmt.Errorf("failed service lookup: %s", err) return 0, nil, fmt.Errorf("failed service lookup: %s", err)
} }
@ -2256,7 +2256,7 @@ func serviceDumpAllTxn(tx ReadTxn, ws memdb.WatchSet, entMeta *structs.Enterpris
// Get the table index // Get the table index
idx := catalogMaxIndexWatch(tx, ws, entMeta, true) idx := catalogMaxIndexWatch(tx, ws, entMeta, true)
services, err := catalogServiceList(tx, entMeta, true) services, err := tx.Get(tableServices, indexID+"_prefix", entMeta)
if err != nil { if err != nil {
return 0, nil, fmt.Errorf("failed service lookup: %s", err) return 0, nil, fmt.Errorf("failed service lookup: %s", err)
} }

View File

@ -106,6 +106,10 @@ func indexFromServiceNode(raw interface{}) ([]byte, error) {
func prefixIndexFromQuery(arg interface{}) ([]byte, error) { func prefixIndexFromQuery(arg interface{}) ([]byte, error) {
var b indexBuilder var b indexBuilder
switch v := arg.(type) { switch v := arg.(type) {
case *structs.EnterpriseMeta:
return nil, nil
case structs.EnterpriseMeta:
return nil, nil
case Query: case Query:
b.String(strings.ToLower(v.Value)) b.String(strings.ToLower(v.Value))
return b.Bytes(), nil return b.Bytes(), nil
@ -195,7 +199,7 @@ func catalogServiceKindMaxIndex(tx ReadTxn, ws memdb.WatchSet, kind structs.Serv
return maxIndexWatchTxn(tx, ws, serviceKindIndexName(kind, nil)) return maxIndexWatchTxn(tx, ws, serviceKindIndexName(kind, nil))
} }
func catalogServiceList(tx ReadTxn, _ *structs.EnterpriseMeta, _ bool) (memdb.ResultIterator, error) { func catalogServiceListNoWildcard(tx ReadTxn, _ *structs.EnterpriseMeta) (memdb.ResultIterator, error) {
return tx.Get(tableServices, indexID) return tx.Get(tableServices, indexID)
} }