2021-11-16 18:04:01 +00:00
|
|
|
//go:build !consulent
|
2019-12-10 02:26:41 +00:00
|
|
|
// +build !consulent
|
|
|
|
|
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-12-02 00:44:13 +00:00
|
|
|
"strings"
|
2020-06-03 17:21:00 +00:00
|
|
|
|
2019-12-10 02:26:41 +00:00
|
|
|
memdb "github.com/hashicorp/go-memdb"
|
|
|
|
|
2020-12-16 22:12:42 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
|
|
|
)
|
2019-12-10 02:26:41 +00:00
|
|
|
|
2020-12-16 22:12:42 +00:00
|
|
|
func withEnterpriseSchema(_ *memdb.DBSchema) {}
|
2019-12-10 02:26:41 +00:00
|
|
|
|
|
|
|
func serviceIndexName(name string, _ *structs.EnterpriseMeta) string {
|
|
|
|
return fmt.Sprintf("service.%s", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func serviceKindIndexName(kind structs.ServiceKind, _ *structs.EnterpriseMeta) string {
|
2021-12-02 00:44:13 +00:00
|
|
|
return "service_kind." + kind.Normalized()
|
2019-12-10 02:26:41 +00:00
|
|
|
}
|
|
|
|
|
2021-08-17 18:29:39 +00:00
|
|
|
func catalogUpdateNodesIndexes(tx WriteTxn, idx uint64, entMeta *structs.EnterpriseMeta) error {
|
|
|
|
// overall nodes index
|
|
|
|
if err := indexUpdateMaxTxn(tx, idx, tableNodes); err != nil {
|
|
|
|
return fmt.Errorf("failed updating index: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-03 23:38:03 +00:00
|
|
|
func catalogUpdateServicesIndexes(tx WriteTxn, idx uint64, _ *structs.EnterpriseMeta) error {
|
2019-12-10 02:26:41 +00:00
|
|
|
// overall services index
|
2021-03-26 20:04:45 +00:00
|
|
|
if err := indexUpdateMaxTxn(tx, idx, tableServices); err != nil {
|
2019-12-10 02:26:41 +00:00
|
|
|
return fmt.Errorf("failed updating index: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-03 23:38:03 +00:00
|
|
|
func catalogUpdateServiceKindIndexes(tx WriteTxn, kind structs.ServiceKind, idx uint64, _ *structs.EnterpriseMeta) error {
|
2019-12-10 02:26:41 +00:00
|
|
|
// service-kind index
|
|
|
|
if err := indexUpdateMaxTxn(tx, idx, serviceKindIndexName(kind, nil)); err != nil {
|
|
|
|
return fmt.Errorf("failed updating index: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-03 23:38:03 +00:00
|
|
|
func catalogUpdateServiceIndexes(tx WriteTxn, serviceName string, idx uint64, _ *structs.EnterpriseMeta) error {
|
2019-12-10 02:26:41 +00:00
|
|
|
// per-service index
|
|
|
|
if err := indexUpdateMaxTxn(tx, idx, serviceIndexName(serviceName, nil)); err != nil {
|
|
|
|
return fmt.Errorf("failed updating index: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-03 23:38:03 +00:00
|
|
|
func catalogUpdateServiceExtinctionIndex(tx WriteTxn, idx uint64, _ *structs.EnterpriseMeta) error {
|
2021-02-05 22:53:08 +00:00
|
|
|
if err := tx.Insert(tableIndex, &IndexEntry{indexServiceExtinction, idx}); err != nil {
|
2019-12-10 02:26:41 +00:00
|
|
|
return fmt.Errorf("failed updating missing service extinction index: %s", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-17 18:29:39 +00:00
|
|
|
func catalogInsertNode(tx WriteTxn, node *structs.Node) error {
|
|
|
|
// ensure that the Partition is always clear within the state store in OSS
|
|
|
|
node.Partition = ""
|
|
|
|
|
|
|
|
// Insert the node and update the index.
|
|
|
|
if err := tx.Insert(tableNodes, node); err != nil {
|
|
|
|
return fmt.Errorf("failed inserting node: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := catalogUpdateNodesIndexes(tx, node.ModifyIndex, node.GetEnterpriseMeta()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the node's service indexes as the node information is included
|
|
|
|
// in health queries and we would otherwise miss node updates in some cases
|
|
|
|
// for those queries.
|
|
|
|
if err := updateAllServiceIndexesOfNode(tx, node.ModifyIndex, node.Node, node.GetEnterpriseMeta()); err != nil {
|
|
|
|
return fmt.Errorf("failed updating index: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-03 23:38:03 +00:00
|
|
|
func catalogInsertService(tx WriteTxn, svc *structs.ServiceNode) error {
|
2019-12-10 02:26:41 +00:00
|
|
|
// Insert the service and update the index
|
2021-03-26 20:04:45 +00:00
|
|
|
if err := tx.Insert(tableServices, svc); err != nil {
|
2019-12-10 02:26:41 +00:00
|
|
|
return fmt.Errorf("failed inserting service: %s", err)
|
|
|
|
}
|
|
|
|
|
2020-07-10 00:56:43 +00:00
|
|
|
if err := catalogUpdateServicesIndexes(tx, svc.ModifyIndex, &svc.EnterpriseMeta); err != nil {
|
2019-12-10 18:58:30 +00:00
|
|
|
return err
|
2019-12-10 02:26:41 +00:00
|
|
|
}
|
|
|
|
|
2020-07-10 00:56:43 +00:00
|
|
|
if err := catalogUpdateServiceIndexes(tx, svc.ServiceName, svc.ModifyIndex, &svc.EnterpriseMeta); err != nil {
|
2019-12-10 02:26:41 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-12-10 18:58:30 +00:00
|
|
|
|
2020-07-10 00:56:43 +00:00
|
|
|
if err := catalogUpdateServiceKindIndexes(tx, svc.ServiceKind, svc.ModifyIndex, &svc.EnterpriseMeta); err != nil {
|
2019-12-10 18:58:30 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-10 02:26:41 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-17 18:29:39 +00:00
|
|
|
func catalogNodesMaxIndex(tx ReadTxn, entMeta *structs.EnterpriseMeta) uint64 {
|
|
|
|
return maxIndexTxn(tx, tableNodes)
|
|
|
|
}
|
|
|
|
|
2020-08-11 20:31:23 +00:00
|
|
|
func catalogServicesMaxIndex(tx ReadTxn, _ *structs.EnterpriseMeta) uint64 {
|
2021-03-26 20:04:45 +00:00
|
|
|
return maxIndexTxn(tx, tableServices)
|
2019-12-10 02:26:41 +00:00
|
|
|
}
|
|
|
|
|
2020-08-11 20:31:23 +00:00
|
|
|
func catalogServiceMaxIndex(tx ReadTxn, serviceName string, _ *structs.EnterpriseMeta) (<-chan struct{}, interface{}, error) {
|
2021-02-05 22:53:08 +00:00
|
|
|
return tx.FirstWatch(tableIndex, "id", serviceIndexName(serviceName, nil))
|
2019-12-10 02:26:41 +00:00
|
|
|
}
|
|
|
|
|
2020-08-11 20:31:23 +00:00
|
|
|
func catalogServiceKindMaxIndex(tx ReadTxn, ws memdb.WatchSet, kind structs.ServiceKind, entMeta *structs.EnterpriseMeta) uint64 {
|
2019-12-10 02:26:41 +00:00
|
|
|
return maxIndexWatchTxn(tx, ws, serviceKindIndexName(kind, nil))
|
|
|
|
}
|
|
|
|
|
2021-03-10 23:26:44 +00:00
|
|
|
func catalogServiceListNoWildcard(tx ReadTxn, _ *structs.EnterpriseMeta) (memdb.ResultIterator, error) {
|
2021-02-12 18:39:38 +00:00
|
|
|
return tx.Get(tableServices, indexID)
|
2019-12-10 02:26:41 +00:00
|
|
|
}
|
|
|
|
|
2020-08-11 20:31:23 +00:00
|
|
|
func catalogServiceListByNode(tx ReadTxn, node string, _ *structs.EnterpriseMeta, _ bool) (memdb.ResultIterator, error) {
|
2021-02-12 22:31:02 +00:00
|
|
|
return tx.Get(tableServices, indexNode, Query{Value: node})
|
2019-12-10 02:26:41 +00:00
|
|
|
}
|
|
|
|
|
2020-08-11 20:31:23 +00:00
|
|
|
func catalogServiceLastExtinctionIndex(tx ReadTxn, _ *structs.EnterpriseMeta) (interface{}, error) {
|
2021-02-05 22:53:08 +00:00
|
|
|
return tx.First(tableIndex, "id", indexServiceExtinction)
|
2019-12-10 02:26:41 +00:00
|
|
|
}
|
|
|
|
|
2020-08-11 20:31:23 +00:00
|
|
|
func catalogMaxIndex(tx ReadTxn, _ *structs.EnterpriseMeta, checks bool) uint64 {
|
2019-12-10 02:26:41 +00:00
|
|
|
if checks {
|
2021-08-17 18:29:39 +00:00
|
|
|
return maxIndexTxn(tx, tableNodes, tableServices, tableChecks)
|
2019-12-10 02:26:41 +00:00
|
|
|
}
|
2021-08-17 18:29:39 +00:00
|
|
|
return maxIndexTxn(tx, tableNodes, tableServices)
|
2019-12-10 02:26:41 +00:00
|
|
|
}
|
|
|
|
|
2020-08-11 20:31:23 +00:00
|
|
|
func catalogMaxIndexWatch(tx ReadTxn, ws memdb.WatchSet, _ *structs.EnterpriseMeta, checks bool) uint64 {
|
2019-12-19 16:15:37 +00:00
|
|
|
if checks {
|
2021-08-17 18:29:39 +00:00
|
|
|
return maxIndexWatchTxn(tx, ws, tableNodes, tableServices, tableChecks)
|
2019-12-19 16:15:37 +00:00
|
|
|
}
|
2021-08-17 18:29:39 +00:00
|
|
|
return maxIndexWatchTxn(tx, ws, tableNodes, tableServices)
|
2019-12-19 16:15:37 +00:00
|
|
|
}
|
|
|
|
|
2020-09-03 23:38:03 +00:00
|
|
|
func catalogUpdateCheckIndexes(tx WriteTxn, idx uint64, _ *structs.EnterpriseMeta) error {
|
2019-12-10 02:26:41 +00:00
|
|
|
// update the universal index entry
|
2021-03-26 23:33:10 +00:00
|
|
|
if err := tx.Insert(tableIndex, &IndexEntry{tableChecks, idx}); err != nil {
|
2019-12-10 02:26:41 +00:00
|
|
|
return fmt.Errorf("failed updating index: %s", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-11 20:31:23 +00:00
|
|
|
func catalogChecksMaxIndex(tx ReadTxn, _ *structs.EnterpriseMeta) uint64 {
|
2021-03-26 23:33:10 +00:00
|
|
|
return maxIndexTxn(tx, tableChecks)
|
2019-12-10 02:26:41 +00:00
|
|
|
}
|
|
|
|
|
2021-02-12 22:31:02 +00:00
|
|
|
func catalogListChecksByNode(tx ReadTxn, q Query) (memdb.ResultIterator, error) {
|
|
|
|
return tx.Get(tableChecks, indexNode, q)
|
2019-12-10 02:26:41 +00:00
|
|
|
}
|
|
|
|
|
2020-09-03 23:38:03 +00:00
|
|
|
func catalogInsertCheck(tx WriteTxn, chk *structs.HealthCheck, idx uint64) error {
|
2019-12-10 02:26:41 +00:00
|
|
|
// Insert the check
|
2021-03-26 23:33:10 +00:00
|
|
|
if err := tx.Insert(tableChecks, chk); err != nil {
|
2019-12-10 02:26:41 +00:00
|
|
|
return fmt.Errorf("failed inserting check: %s", err)
|
|
|
|
}
|
|
|
|
|
2020-07-10 00:56:43 +00:00
|
|
|
if err := catalogUpdateCheckIndexes(tx, idx, &chk.EnterpriseMeta); err != nil {
|
2019-12-10 02:26:41 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-11 16:21:42 +00:00
|
|
|
func validateRegisterRequestTxn(_ ReadTxn, _ *structs.RegisterRequest, _ bool) (*structs.EnterpriseMeta, error) {
|
2019-12-10 02:26:41 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2020-08-11 20:31:23 +00:00
|
|
|
func (s *Store) ValidateRegisterRequest(_ *structs.RegisterRequest) (*structs.EnterpriseMeta, error) {
|
2019-12-10 02:26:41 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
2021-12-02 00:44:13 +00:00
|
|
|
|
|
|
|
func indexFromKindServiceName(arg interface{}) ([]byte, error) {
|
|
|
|
var b indexBuilder
|
|
|
|
|
|
|
|
switch n := arg.(type) {
|
|
|
|
case KindServiceNameQuery:
|
|
|
|
b.String(strings.ToLower(string(n.Kind)))
|
|
|
|
b.String(strings.ToLower(n.Name))
|
|
|
|
return b.Bytes(), nil
|
|
|
|
|
|
|
|
case *KindServiceName:
|
|
|
|
b.String(strings.ToLower(string(n.Kind)))
|
|
|
|
b.String(strings.ToLower(n.Service.Name))
|
|
|
|
return b.Bytes(), nil
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("type must be KindServiceNameQuery or *KindServiceName: %T", arg)
|
|
|
|
}
|
|
|
|
}
|