2019-12-10 02:26:41 +00:00
|
|
|
// +build !consulent
|
|
|
|
|
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-06-03 17:21:00 +00:00
|
|
|
|
2019-12-10 02:26:41 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
|
|
|
memdb "github.com/hashicorp/go-memdb"
|
|
|
|
)
|
|
|
|
|
|
|
|
// servicesTableSchema returns a new table schema used to store information
|
|
|
|
// about services.
|
|
|
|
func servicesTableSchema() *memdb.TableSchema {
|
|
|
|
return &memdb.TableSchema{
|
|
|
|
Name: "services",
|
|
|
|
Indexes: map[string]*memdb.IndexSchema{
|
2020-06-16 17:19:31 +00:00
|
|
|
"id": {
|
2019-12-10 02:26:41 +00:00
|
|
|
Name: "id",
|
|
|
|
AllowMissing: false,
|
|
|
|
Unique: true,
|
|
|
|
Indexer: &memdb.CompoundIndex{
|
|
|
|
Indexes: []memdb.Indexer{
|
|
|
|
&memdb.StringFieldIndex{
|
|
|
|
Field: "Node",
|
|
|
|
Lowercase: true,
|
|
|
|
},
|
|
|
|
&memdb.StringFieldIndex{
|
|
|
|
Field: "ServiceID",
|
|
|
|
Lowercase: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"node": {
|
2019-12-10 02:26:41 +00:00
|
|
|
Name: "node",
|
|
|
|
AllowMissing: false,
|
|
|
|
Unique: false,
|
|
|
|
Indexer: &memdb.StringFieldIndex{
|
|
|
|
Field: "Node",
|
|
|
|
Lowercase: true,
|
|
|
|
},
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"service": {
|
2019-12-10 02:26:41 +00:00
|
|
|
Name: "service",
|
|
|
|
AllowMissing: true,
|
|
|
|
Unique: false,
|
|
|
|
Indexer: &memdb.StringFieldIndex{
|
|
|
|
Field: "ServiceName",
|
|
|
|
Lowercase: true,
|
|
|
|
},
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"connect": {
|
2019-12-10 02:26:41 +00:00
|
|
|
Name: "connect",
|
|
|
|
AllowMissing: true,
|
|
|
|
Unique: false,
|
|
|
|
Indexer: &IndexConnectService{},
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"kind": {
|
2019-12-10 02:26:41 +00:00
|
|
|
Name: "kind",
|
|
|
|
AllowMissing: false,
|
|
|
|
Unique: false,
|
|
|
|
Indexer: &IndexServiceKind{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
func checksTableSchema() *memdb.TableSchema {
|
|
|
|
return &memdb.TableSchema{
|
|
|
|
Name: "checks",
|
|
|
|
Indexes: map[string]*memdb.IndexSchema{
|
2020-06-16 17:19:31 +00:00
|
|
|
"id": {
|
2019-12-10 02:26:41 +00:00
|
|
|
Name: "id",
|
|
|
|
AllowMissing: false,
|
|
|
|
Unique: true,
|
|
|
|
Indexer: &memdb.CompoundIndex{
|
|
|
|
Indexes: []memdb.Indexer{
|
|
|
|
&memdb.StringFieldIndex{
|
|
|
|
Field: "Node",
|
|
|
|
Lowercase: true,
|
|
|
|
},
|
|
|
|
&memdb.StringFieldIndex{
|
|
|
|
Field: "CheckID",
|
|
|
|
Lowercase: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"status": {
|
2019-12-10 02:26:41 +00:00
|
|
|
Name: "status",
|
|
|
|
AllowMissing: false,
|
|
|
|
Unique: false,
|
|
|
|
Indexer: &memdb.StringFieldIndex{
|
|
|
|
Field: "Status",
|
|
|
|
Lowercase: false,
|
|
|
|
},
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"service": {
|
2019-12-10 02:26:41 +00:00
|
|
|
Name: "service",
|
|
|
|
AllowMissing: true,
|
|
|
|
Unique: false,
|
|
|
|
Indexer: &memdb.StringFieldIndex{
|
|
|
|
Field: "ServiceName",
|
|
|
|
Lowercase: true,
|
|
|
|
},
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"node": {
|
2019-12-10 02:26:41 +00:00
|
|
|
Name: "node",
|
|
|
|
AllowMissing: true,
|
|
|
|
Unique: false,
|
|
|
|
Indexer: &memdb.StringFieldIndex{
|
|
|
|
Field: "Node",
|
|
|
|
Lowercase: true,
|
|
|
|
},
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"node_service_check": {
|
2019-12-10 02:26:41 +00:00
|
|
|
Name: "node_service_check",
|
|
|
|
AllowMissing: true,
|
|
|
|
Unique: false,
|
|
|
|
Indexer: &memdb.CompoundIndex{
|
|
|
|
Indexes: []memdb.Indexer{
|
|
|
|
&memdb.StringFieldIndex{
|
|
|
|
Field: "Node",
|
|
|
|
Lowercase: true,
|
|
|
|
},
|
|
|
|
&memdb.FieldSetIndex{
|
|
|
|
Field: "ServiceID",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2020-06-16 17:19:31 +00:00
|
|
|
"node_service": {
|
2019-12-10 02:26:41 +00:00
|
|
|
Name: "node_service",
|
|
|
|
AllowMissing: true,
|
|
|
|
Unique: false,
|
|
|
|
Indexer: &memdb.CompoundIndex{
|
|
|
|
Indexes: []memdb.Indexer{
|
|
|
|
&memdb.StringFieldIndex{
|
|
|
|
Field: "Node",
|
|
|
|
Lowercase: true,
|
|
|
|
},
|
|
|
|
&memdb.StringFieldIndex{
|
|
|
|
Field: "ServiceID",
|
|
|
|
Lowercase: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func serviceIndexName(name string, _ *structs.EnterpriseMeta) string {
|
|
|
|
return fmt.Sprintf("service.%s", name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func serviceKindIndexName(kind structs.ServiceKind, _ *structs.EnterpriseMeta) string {
|
|
|
|
switch kind {
|
|
|
|
case structs.ServiceKindTypical:
|
|
|
|
// needs a special case here
|
|
|
|
return "service_kind.typical"
|
|
|
|
default:
|
|
|
|
return "service_kind." + string(kind)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
if err := indexUpdateMaxTxn(tx, idx, "services"); err != nil {
|
|
|
|
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 {
|
2019-12-10 02:26:41 +00:00
|
|
|
if err := tx.Insert("index", &IndexEntry{serviceLastExtinctionIndexName, idx}); err != nil {
|
|
|
|
return fmt.Errorf("failed updating missing service extinction 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
|
|
|
|
if err := tx.Insert("services", svc); err != nil {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-08-11 20:31:23 +00:00
|
|
|
func catalogServicesMaxIndex(tx ReadTxn, _ *structs.EnterpriseMeta) uint64 {
|
2019-12-10 02:26:41 +00:00
|
|
|
return maxIndexTxn(tx, "services")
|
|
|
|
}
|
|
|
|
|
2020-08-11 20:31:23 +00:00
|
|
|
func catalogServiceMaxIndex(tx ReadTxn, serviceName string, _ *structs.EnterpriseMeta) (<-chan struct{}, interface{}, error) {
|
2019-12-10 02:26:41 +00:00
|
|
|
return tx.FirstWatch("index", "id", serviceIndexName(serviceName, nil))
|
|
|
|
}
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2020-08-11 20:31:23 +00:00
|
|
|
func catalogServiceList(tx ReadTxn, _ *structs.EnterpriseMeta, _ bool) (memdb.ResultIterator, error) {
|
2019-12-10 02:26:41 +00:00
|
|
|
return tx.Get("services", "id")
|
|
|
|
}
|
|
|
|
|
2020-08-11 20:31:23 +00:00
|
|
|
func catalogServiceListByKind(tx ReadTxn, kind structs.ServiceKind, _ *structs.EnterpriseMeta) (memdb.ResultIterator, error) {
|
2019-12-10 02:26:41 +00:00
|
|
|
return tx.Get("services", "kind", string(kind))
|
|
|
|
}
|
|
|
|
|
2020-08-11 20:31:23 +00:00
|
|
|
func catalogServiceListByNode(tx ReadTxn, node string, _ *structs.EnterpriseMeta, _ bool) (memdb.ResultIterator, error) {
|
2019-12-10 02:26:41 +00:00
|
|
|
return tx.Get("services", "node", node)
|
|
|
|
}
|
|
|
|
|
2020-08-11 20:31:23 +00:00
|
|
|
func catalogServiceNodeList(tx ReadTxn, name string, index string, _ *structs.EnterpriseMeta) (memdb.ResultIterator, error) {
|
2019-12-10 02:26:41 +00:00
|
|
|
return tx.Get("services", index, name)
|
|
|
|
}
|
|
|
|
|
2020-08-11 20:31:23 +00:00
|
|
|
func catalogServiceLastExtinctionIndex(tx ReadTxn, _ *structs.EnterpriseMeta) (interface{}, error) {
|
2019-12-10 02:26:41 +00:00
|
|
|
return tx.First("index", "id", serviceLastExtinctionIndexName)
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
return maxIndexTxn(tx, "nodes", "services", "checks")
|
|
|
|
}
|
|
|
|
return maxIndexTxn(tx, "nodes", "services")
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
return maxIndexWatchTxn(tx, ws, "nodes", "services", "checks")
|
|
|
|
}
|
|
|
|
return maxIndexWatchTxn(tx, ws, "nodes", "services")
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
if err := tx.Insert("index", &IndexEntry{"checks", idx}); err != nil {
|
|
|
|
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 {
|
2019-12-10 02:26:41 +00:00
|
|
|
return maxIndexTxn(tx, "checks")
|
|
|
|
}
|
|
|
|
|
2020-08-11 20:31:23 +00:00
|
|
|
func catalogListChecksByNode(tx ReadTxn, node string, _ *structs.EnterpriseMeta) (memdb.ResultIterator, error) {
|
2019-12-10 02:26:41 +00:00
|
|
|
return tx.Get("checks", "node", node)
|
|
|
|
}
|
|
|
|
|
2020-08-11 20:31:23 +00:00
|
|
|
func catalogListChecksByService(tx ReadTxn, service string, _ *structs.EnterpriseMeta) (memdb.ResultIterator, error) {
|
2019-12-10 02:26:41 +00:00
|
|
|
return tx.Get("checks", "service", service)
|
|
|
|
}
|
|
|
|
|
2020-08-11 20:31:23 +00:00
|
|
|
func catalogListChecksInState(tx ReadTxn, state string, _ *structs.EnterpriseMeta) (memdb.ResultIterator, error) {
|
2019-12-10 02:26:41 +00:00
|
|
|
// simpler than normal due to the use of the CompoundMultiIndex
|
|
|
|
return tx.Get("checks", "status", state)
|
|
|
|
}
|
|
|
|
|
2020-08-11 20:31:23 +00:00
|
|
|
func catalogListChecks(tx ReadTxn, _ *structs.EnterpriseMeta) (memdb.ResultIterator, error) {
|
2019-12-10 02:26:41 +00:00
|
|
|
return tx.Get("checks", "id")
|
|
|
|
}
|
|
|
|
|
2020-08-11 20:31:23 +00:00
|
|
|
func catalogListNodeChecks(tx ReadTxn, node string) (memdb.ResultIterator, error) {
|
2019-12-10 02:26:41 +00:00
|
|
|
return tx.Get("checks", "node_service_check", node, false)
|
|
|
|
}
|
|
|
|
|
2020-08-11 20:31:23 +00:00
|
|
|
func catalogListServiceChecks(tx ReadTxn, node string, service string, _ *structs.EnterpriseMeta) (memdb.ResultIterator, error) {
|
2019-12-10 02:26:41 +00:00
|
|
|
return tx.Get("checks", "node_service", node, service)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
if err := tx.Insert("checks", chk); err != nil {
|
|
|
|
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-08-11 20:31:23 +00:00
|
|
|
func catalogChecksForNodeService(tx ReadTxn, node string, service string, entMeta *structs.EnterpriseMeta) (memdb.ResultIterator, error) {
|
2019-12-10 02:26:41 +00:00
|
|
|
return tx.Get("checks", "node_service", node, service)
|
|
|
|
}
|
|
|
|
|
2020-09-03 23:38:03 +00:00
|
|
|
func validateRegisterRequestTxn(_ ReadTxn, _ *structs.RegisterRequest) (*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
|
|
|
|
}
|