From dbd3cef1ed90cfd2216462ace1685c7910621999 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Wed, 10 Mar 2021 18:26:44 -0500 Subject: [PATCH] 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 --- agent/consul/state/catalog.go | 10 +++++----- agent/consul/state/catalog_oss.go | 6 +++++- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/agent/consul/state/catalog.go b/agent/consul/state/catalog.go index 56e7c1c607..0171d6b247 100644 --- a/agent/consul/state/catalog.go +++ b/agent/consul/state/catalog.go @@ -691,7 +691,7 @@ func (s *Store) Services(ws memdb.WatchSet, entMeta *structs.EnterpriseMeta) (ui idx := catalogServicesMaxIndex(tx, entMeta) // List all the services. - services, err := catalogServiceList(tx, entMeta, false) + services, err := catalogServiceListNoWildcard(tx, entMeta) if err != nil { 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) { idx := catalogServicesMaxIndex(tx, entMeta) - services, err := catalogServiceList(tx, entMeta, true) + services, err := tx.Get(tableServices, indexID+"_prefix", entMeta) if err != nil { 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 // top-level watch to use as a fallback. - allServices, err := catalogServiceList(tx, entMeta, false) + allServices, err := catalogServiceListNoWildcard(tx, entMeta) if err != nil { 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() // List all the services. - services, err := catalogServiceList(tx, entMeta, true) + services, err := tx.Get(tableServices, indexID+"_prefix", entMeta) if err != nil { 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 idx := catalogMaxIndexWatch(tx, ws, entMeta, true) - services, err := catalogServiceList(tx, entMeta, true) + services, err := tx.Get(tableServices, indexID+"_prefix", entMeta) if err != nil { return 0, nil, fmt.Errorf("failed service lookup: %s", err) } diff --git a/agent/consul/state/catalog_oss.go b/agent/consul/state/catalog_oss.go index e1bcbdd0ea..547ea89b11 100644 --- a/agent/consul/state/catalog_oss.go +++ b/agent/consul/state/catalog_oss.go @@ -106,6 +106,10 @@ func indexFromServiceNode(raw interface{}) ([]byte, error) { func prefixIndexFromQuery(arg interface{}) ([]byte, error) { var b indexBuilder switch v := arg.(type) { + case *structs.EnterpriseMeta: + return nil, nil + case structs.EnterpriseMeta: + return nil, nil case Query: b.String(strings.ToLower(v.Value)) 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)) } -func catalogServiceList(tx ReadTxn, _ *structs.EnterpriseMeta, _ bool) (memdb.ResultIterator, error) { +func catalogServiceListNoWildcard(tx ReadTxn, _ *structs.EnterpriseMeta) (memdb.ResultIterator, error) { return tx.Get(tableServices, indexID) }