2020-09-02 15:24:16 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
memdb "github.com/hashicorp/go-memdb"
|
2021-01-29 01:48:51 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2020-09-02 15:24:16 +00:00
|
|
|
)
|
|
|
|
|
2020-09-02 15:24:21 +00:00
|
|
|
const (
|
|
|
|
serviceNamesUsageTable = "service-names"
|
2021-08-18 14:27:15 +00:00
|
|
|
|
|
|
|
tableUsage = "usage"
|
2020-09-02 15:24:21 +00:00
|
|
|
)
|
|
|
|
|
2020-09-02 15:24:16 +00:00
|
|
|
// usageTableSchema returns a new table schema used for tracking various indexes
|
|
|
|
// for the Raft log.
|
|
|
|
func usageTableSchema() *memdb.TableSchema {
|
|
|
|
return &memdb.TableSchema{
|
2021-08-18 14:27:15 +00:00
|
|
|
Name: tableUsage,
|
2020-09-02 15:24:16 +00:00
|
|
|
Indexes: map[string]*memdb.IndexSchema{
|
2021-08-18 14:27:15 +00:00
|
|
|
indexID: {
|
|
|
|
Name: indexID,
|
2020-09-02 15:24:16 +00:00
|
|
|
AllowMissing: false,
|
|
|
|
Unique: true,
|
|
|
|
Indexer: &memdb.StringFieldIndex{
|
|
|
|
Field: "ID",
|
|
|
|
Lowercase: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-02 15:24:21 +00:00
|
|
|
// UsageEntry represents a count of some arbitrary identifier within the
|
|
|
|
// state store, along with the last seen index.
|
2020-09-02 15:24:16 +00:00
|
|
|
type UsageEntry struct {
|
|
|
|
ID string
|
|
|
|
Index uint64
|
|
|
|
Count int
|
|
|
|
}
|
|
|
|
|
2020-09-02 15:24:21 +00:00
|
|
|
// ServiceUsage contains all of the usage data related to services
|
|
|
|
type ServiceUsage struct {
|
|
|
|
Services int
|
|
|
|
ServiceInstances int
|
|
|
|
EnterpriseServiceUsage
|
|
|
|
}
|
|
|
|
|
2021-08-18 14:27:15 +00:00
|
|
|
// NodeUsage contains all of the usage data related to nodes
|
|
|
|
type NodeUsage struct {
|
|
|
|
Nodes int
|
|
|
|
EnterpriseNodeUsage
|
|
|
|
}
|
|
|
|
|
2020-09-02 15:24:21 +00:00
|
|
|
type uniqueServiceState int
|
|
|
|
|
|
|
|
const (
|
|
|
|
NoChange uniqueServiceState = 0
|
|
|
|
Deleted uniqueServiceState = 1
|
|
|
|
Created uniqueServiceState = 2
|
|
|
|
)
|
|
|
|
|
2020-09-02 15:24:16 +00:00
|
|
|
// updateUsage takes a set of memdb changes and computes a delta for specific
|
|
|
|
// usage metrics that we track.
|
2020-09-02 15:24:19 +00:00
|
|
|
func updateUsage(tx WriteTxn, changes Changes) error {
|
2020-09-02 15:24:16 +00:00
|
|
|
usageDeltas := make(map[string]int)
|
2021-01-12 21:31:47 +00:00
|
|
|
serviceNameChanges := make(map[structs.ServiceName]int)
|
2020-09-02 15:24:16 +00:00
|
|
|
for _, change := range changes.Changes {
|
|
|
|
var delta int
|
|
|
|
if change.Created() {
|
|
|
|
delta = 1
|
|
|
|
} else if change.Deleted() {
|
|
|
|
delta = -1
|
|
|
|
}
|
2020-09-02 15:24:21 +00:00
|
|
|
|
2020-09-02 15:24:16 +00:00
|
|
|
switch change.Table {
|
2021-08-18 14:27:15 +00:00
|
|
|
case tableNodes:
|
2020-09-02 15:24:16 +00:00
|
|
|
usageDeltas[change.Table] += delta
|
2021-08-18 14:27:15 +00:00
|
|
|
addEnterpriseNodeUsage(usageDeltas, change)
|
|
|
|
|
2021-03-26 20:04:45 +00:00
|
|
|
case tableServices:
|
2020-09-02 15:24:21 +00:00
|
|
|
svc := changeObject(change).(*structs.ServiceNode)
|
2020-09-02 15:24:16 +00:00
|
|
|
usageDeltas[change.Table] += delta
|
2021-01-12 21:31:47 +00:00
|
|
|
addEnterpriseServiceInstanceUsage(usageDeltas, change)
|
|
|
|
|
|
|
|
// Construct a mapping of all of the various service names that were
|
|
|
|
// changed, in order to compare it with the finished memdb state.
|
|
|
|
// Make sure to account for the fact that services can change their names.
|
|
|
|
if serviceNameChanged(change) {
|
|
|
|
serviceNameChanges[svc.CompoundServiceName()] += 1
|
|
|
|
before := change.Before.(*structs.ServiceNode)
|
|
|
|
serviceNameChanges[before.CompoundServiceName()] -= 1
|
|
|
|
} else {
|
|
|
|
serviceNameChanges[svc.CompoundServiceName()] += delta
|
2020-09-02 15:24:21 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-02 15:24:16 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 21:31:47 +00:00
|
|
|
serviceStates, err := updateServiceNameUsage(tx, usageDeltas, serviceNameChanges)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
addEnterpriseServiceUsage(usageDeltas, serviceStates)
|
|
|
|
|
2020-09-02 15:24:16 +00:00
|
|
|
idx := changes.Index
|
|
|
|
// This will happen when restoring from a snapshot, just take the max index
|
|
|
|
// of the tables we are tracking.
|
|
|
|
if idx == 0 {
|
2021-08-18 14:27:15 +00:00
|
|
|
// TODO(partitions? namespaces?)
|
|
|
|
idx = maxIndexTxn(tx, tableNodes, tableServices)
|
2020-09-02 15:24:16 +00:00
|
|
|
}
|
|
|
|
|
2020-09-02 15:24:21 +00:00
|
|
|
return writeUsageDeltas(tx, idx, usageDeltas)
|
|
|
|
}
|
|
|
|
|
2021-01-12 21:31:47 +00:00
|
|
|
func updateServiceNameUsage(tx WriteTxn, usageDeltas map[string]int, serviceNameChanges map[structs.ServiceName]int) (map[structs.ServiceName]uniqueServiceState, error) {
|
|
|
|
serviceStates := make(map[structs.ServiceName]uniqueServiceState, len(serviceNameChanges))
|
|
|
|
for svc, delta := range serviceNameChanges {
|
2021-08-18 14:27:15 +00:00
|
|
|
q := Query{
|
|
|
|
Value: svc.Name,
|
|
|
|
EnterpriseMeta: svc.EnterpriseMeta,
|
|
|
|
}
|
2021-03-26 21:21:38 +00:00
|
|
|
serviceIter, err := tx.Get(tableServices, indexService, q)
|
2021-01-12 21:31:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Count the number of service instances associated with the given service
|
|
|
|
// name at the end of this transaction, and compare that with how many were
|
|
|
|
// added/removed during the transaction. This allows us to handle a single
|
|
|
|
// transaction committing multiple changes related to a single service
|
|
|
|
// name.
|
|
|
|
var svcCount int
|
|
|
|
for service := serviceIter.Next(); service != nil; service = serviceIter.Next() {
|
|
|
|
svcCount += 1
|
|
|
|
}
|
|
|
|
|
|
|
|
var serviceState uniqueServiceState
|
|
|
|
switch {
|
|
|
|
case svcCount == 0:
|
|
|
|
// If no services exist, we know we deleted the last service
|
|
|
|
// instance.
|
|
|
|
serviceState = Deleted
|
|
|
|
usageDeltas[serviceNamesUsageTable] -= 1
|
|
|
|
case svcCount == delta:
|
|
|
|
// If the current number of service instances equals the number added,
|
|
|
|
// than we know we created a new service name.
|
|
|
|
serviceState = Created
|
|
|
|
usageDeltas[serviceNamesUsageTable] += 1
|
|
|
|
default:
|
|
|
|
serviceState = NoChange
|
|
|
|
}
|
|
|
|
|
|
|
|
serviceStates[svc] = serviceState
|
|
|
|
}
|
|
|
|
|
|
|
|
return serviceStates, nil
|
|
|
|
}
|
|
|
|
|
2020-09-02 15:24:21 +00:00
|
|
|
// serviceNameChanged returns a boolean that indicates whether the
|
|
|
|
// provided change resulted in an update to the service's service name.
|
|
|
|
func serviceNameChanged(change memdb.Change) bool {
|
|
|
|
if change.Updated() {
|
|
|
|
before := change.Before.(*structs.ServiceNode)
|
|
|
|
after := change.After.(*structs.ServiceNode)
|
|
|
|
return before.ServiceName != after.ServiceName
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// writeUsageDeltas will take in a map of IDs to deltas and update each
|
|
|
|
// entry accordingly, checking for integer underflow. The index that is
|
|
|
|
// passed in will be recorded on the entry as well.
|
|
|
|
func writeUsageDeltas(tx WriteTxn, idx uint64, usageDeltas map[string]int) error {
|
2020-09-02 15:24:16 +00:00
|
|
|
for id, delta := range usageDeltas {
|
2021-08-18 14:27:15 +00:00
|
|
|
u, err := tx.First(tableUsage, indexID, id)
|
2020-09-02 15:24:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to retrieve existing usage entry: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if u == nil {
|
|
|
|
if delta < 0 {
|
2021-01-12 21:31:47 +00:00
|
|
|
// Don't return an error here, since we don't want to block updates
|
|
|
|
// from happening to the state store. But, set the delta to 0 so that
|
|
|
|
// we do not accidentally underflow the uint64 and begin reporting
|
|
|
|
// large numbers.
|
|
|
|
delta = 0
|
2020-09-02 15:24:16 +00:00
|
|
|
}
|
2021-08-18 14:27:15 +00:00
|
|
|
err := tx.Insert(tableUsage, &UsageEntry{
|
2020-09-02 15:24:16 +00:00
|
|
|
ID: id,
|
|
|
|
Count: delta,
|
|
|
|
Index: idx,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to update usage entry: %s", err)
|
|
|
|
}
|
|
|
|
} else if cur, ok := u.(*UsageEntry); ok {
|
2021-01-12 21:31:47 +00:00
|
|
|
updated := cur.Count + delta
|
|
|
|
if updated < 0 {
|
|
|
|
// Don't return an error here, since we don't want to block updates
|
|
|
|
// from happening to the state store. But, set the delta to 0 so that
|
|
|
|
// we do not accidentally underflow the uint64 and begin reporting
|
|
|
|
// large numbers.
|
|
|
|
updated = 0
|
2020-09-02 15:24:16 +00:00
|
|
|
}
|
2021-08-18 14:27:15 +00:00
|
|
|
err := tx.Insert(tableUsage, &UsageEntry{
|
2020-09-02 15:24:16 +00:00
|
|
|
ID: id,
|
2021-01-12 21:31:47 +00:00
|
|
|
Count: updated,
|
2020-09-02 15:24:16 +00:00
|
|
|
Index: idx,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to update usage entry: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-18 14:27:15 +00:00
|
|
|
// NodeUsage returns the latest seen Raft index, a compiled set of node usage
|
|
|
|
// data, and any errors.
|
|
|
|
func (s *Store) NodeUsage() (uint64, NodeUsage, error) {
|
2020-09-02 15:24:16 +00:00
|
|
|
tx := s.db.ReadTxn()
|
|
|
|
defer tx.Abort()
|
|
|
|
|
2021-08-18 14:27:15 +00:00
|
|
|
nodes, err := firstUsageEntry(tx, tableNodes)
|
2020-09-02 15:24:16 +00:00
|
|
|
if err != nil {
|
2021-08-18 14:27:15 +00:00
|
|
|
return 0, NodeUsage{}, fmt.Errorf("failed nodes lookup: %s", err)
|
2020-09-02 15:24:16 +00:00
|
|
|
}
|
2021-08-18 14:27:15 +00:00
|
|
|
|
|
|
|
usage := NodeUsage{
|
|
|
|
Nodes: nodes.Count,
|
|
|
|
}
|
|
|
|
results, err := compileEnterpriseNodeUsage(tx, usage)
|
|
|
|
if err != nil {
|
|
|
|
return 0, NodeUsage{}, fmt.Errorf("failed nodes lookup: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodes.Index, results, nil
|
2020-09-02 15:24:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ServiceUsage returns the latest seen Raft index, a compiled set of service
|
|
|
|
// usage data, and any errors.
|
|
|
|
func (s *Store) ServiceUsage() (uint64, ServiceUsage, error) {
|
|
|
|
tx := s.db.ReadTxn()
|
|
|
|
defer tx.Abort()
|
|
|
|
|
2021-01-29 01:48:51 +00:00
|
|
|
serviceInstances, err := firstUsageEntry(tx, tableServices)
|
2020-09-02 15:24:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, ServiceUsage{}, fmt.Errorf("failed services lookup: %s", err)
|
|
|
|
}
|
|
|
|
|
2020-09-02 15:24:21 +00:00
|
|
|
services, err := firstUsageEntry(tx, serviceNamesUsageTable)
|
|
|
|
if err != nil {
|
|
|
|
return 0, ServiceUsage{}, fmt.Errorf("failed services lookup: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
usage := ServiceUsage{
|
|
|
|
ServiceInstances: serviceInstances.Count,
|
|
|
|
Services: services.Count,
|
|
|
|
}
|
2021-08-18 14:27:15 +00:00
|
|
|
results, err := compileEnterpriseServiceUsage(tx, usage)
|
2020-09-02 15:24:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, ServiceUsage{}, fmt.Errorf("failed services lookup: %s", err)
|
|
|
|
}
|
|
|
|
|
2020-09-02 15:24:21 +00:00
|
|
|
return serviceInstances.Index, results, nil
|
2020-09-02 15:24:16 +00:00
|
|
|
}
|
|
|
|
|
2020-09-02 15:24:19 +00:00
|
|
|
func firstUsageEntry(tx ReadTxn, id string) (*UsageEntry, error) {
|
2021-08-18 14:27:15 +00:00
|
|
|
usage, err := tx.First(tableUsage, indexID, id)
|
2020-09-02 15:24:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If no elements have been inserted, the usage entry will not exist. We
|
|
|
|
// return a valid value so that can be certain the return value is not nil
|
|
|
|
// when no error has occurred.
|
|
|
|
if usage == nil {
|
|
|
|
return &UsageEntry{ID: id, Count: 0}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
realUsage, ok := usage.(*UsageEntry)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("failed usage lookup: type %T is not *UsageEntry", usage)
|
|
|
|
}
|
|
|
|
|
|
|
|
return realUsage, nil
|
|
|
|
}
|