2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2020-09-02 15:24:16 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2022-07-27 22:34:04 +00:00
|
|
|
"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 (
|
2021-09-30 21:48:21 +00:00
|
|
|
serviceNamesUsageTable = "service-names"
|
|
|
|
kvUsageTable = "kv-entries"
|
|
|
|
connectNativeInstancesTable = "connect-native"
|
2021-10-07 21:16:23 +00:00
|
|
|
connectPrefix = "connect-mesh"
|
2021-08-18 14:27:15 +00:00
|
|
|
|
|
|
|
tableUsage = "usage"
|
2020-09-02 15:24:21 +00:00
|
|
|
)
|
|
|
|
|
2021-09-30 21:15:26 +00:00
|
|
|
var allConnectKind = []string{
|
|
|
|
string(structs.ServiceKindConnectProxy),
|
|
|
|
string(structs.ServiceKindIngressGateway),
|
|
|
|
string(structs.ServiceKindMeshGateway),
|
|
|
|
string(structs.ServiceKindTerminatingGateway),
|
2023-06-09 12:40:03 +00:00
|
|
|
string(structs.ServiceKindAPIGateway),
|
2021-09-30 21:48:21 +00:00
|
|
|
connectNativeInstancesTable,
|
2021-09-30 21:15:26 +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
|
|
|
|
}
|
|
|
|
|
2021-08-18 14:27:15 +00:00
|
|
|
// NodeUsage contains all of the usage data related to nodes
|
|
|
|
type NodeUsage struct {
|
|
|
|
Nodes int
|
|
|
|
EnterpriseNodeUsage
|
|
|
|
}
|
|
|
|
|
2022-07-27 22:34:04 +00:00
|
|
|
// PeeringUsage contains all of the usage data related to peerings.
|
|
|
|
type PeeringUsage struct {
|
|
|
|
// Number of peerings.
|
|
|
|
Peerings int
|
|
|
|
EnterprisePeeringUsage
|
|
|
|
}
|
|
|
|
|
2021-09-17 19:36:34 +00:00
|
|
|
type KVUsage struct {
|
|
|
|
KVCount int
|
|
|
|
EnterpriseKVUsage
|
|
|
|
}
|
|
|
|
|
2021-10-07 21:19:55 +00:00
|
|
|
type ConfigEntryUsage struct {
|
2021-10-01 18:22:30 +00:00
|
|
|
ConfigByKind map[string]int
|
2021-10-08 15:53:27 +00:00
|
|
|
EnterpriseConfigEntryUsage
|
2021-10-01 18:22:30 +00:00
|
|
|
}
|
|
|
|
|
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:
|
2022-07-27 16:08:51 +00:00
|
|
|
node := changeObject(change).(*structs.Node)
|
|
|
|
if node.PeerName != "" {
|
|
|
|
// TODO(peering) track peered nodes separately. For now not tracking to avoid double billing.
|
|
|
|
continue
|
|
|
|
}
|
2020-09-02 15:24:16 +00:00
|
|
|
usageDeltas[change.Table] += delta
|
2021-08-18 14:27:15 +00:00
|
|
|
addEnterpriseNodeUsage(usageDeltas, change)
|
|
|
|
|
2022-07-27 22:34:04 +00:00
|
|
|
case tablePeering:
|
|
|
|
usageDeltas[change.Table] += delta
|
|
|
|
addEnterprisePeeringUsage(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)
|
2022-07-27 16:08:51 +00:00
|
|
|
if svc.PeerName != "" {
|
|
|
|
// TODO(peering) track peered services separately. For now not tracking to avoid double billing.
|
|
|
|
continue
|
|
|
|
}
|
2020-09-02 15:24:16 +00:00
|
|
|
usageDeltas[change.Table] += delta
|
2021-01-12 21:31:47 +00:00
|
|
|
addEnterpriseServiceInstanceUsage(usageDeltas, change)
|
|
|
|
|
2021-09-30 21:15:26 +00:00
|
|
|
connectDeltas(change, usageDeltas, delta)
|
2023-02-08 20:07:21 +00:00
|
|
|
billableServiceInstancesDeltas(change, usageDeltas, delta)
|
|
|
|
|
2021-01-12 21:31:47 +00:00
|
|
|
// 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) {
|
2023-03-08 16:24:03 +00:00
|
|
|
serviceNameChanges[svc.CompoundServiceName().ServiceName] += 1
|
2021-01-12 21:31:47 +00:00
|
|
|
before := change.Before.(*structs.ServiceNode)
|
2023-03-08 16:24:03 +00:00
|
|
|
serviceNameChanges[before.CompoundServiceName().ServiceName] -= 1
|
2021-01-12 21:31:47 +00:00
|
|
|
} else {
|
2023-03-08 16:24:03 +00:00
|
|
|
serviceNameChanges[svc.CompoundServiceName().ServiceName] += delta
|
2020-09-02 15:24:21 +00:00
|
|
|
}
|
2021-09-30 21:15:26 +00:00
|
|
|
|
2021-09-17 19:36:34 +00:00
|
|
|
case "kvs":
|
|
|
|
usageDeltas[change.Table] += delta
|
|
|
|
addEnterpriseKVUsage(usageDeltas, change)
|
2021-10-01 18:22:30 +00:00
|
|
|
case tableConfigEntries:
|
|
|
|
entry := changeObject(change).(structs.ConfigEntry)
|
2021-10-11 17:43:31 +00:00
|
|
|
usageDeltas[configEntryUsageTableName(entry.GetKind())] += delta
|
2021-10-07 21:19:55 +00:00
|
|
|
addEnterpriseConfigEntryUsage(usageDeltas, change)
|
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?)
|
2021-09-30 21:15:26 +00:00
|
|
|
idx = maxIndexTxn(tx, tableNodes, tableServices, "kvs")
|
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.
|
2021-09-30 21:15:26 +00:00
|
|
|
var count int
|
2021-01-12 21:31:47 +00:00
|
|
|
for service := serviceIter.Next(); service != nil; service = serviceIter.Next() {
|
2021-09-30 21:15:26 +00:00
|
|
|
count += 1
|
2021-01-12 21:31:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var serviceState uniqueServiceState
|
|
|
|
switch {
|
2021-09-30 21:15:26 +00:00
|
|
|
case count == 0:
|
2021-01-12 21:31:47 +00:00
|
|
|
// If no services exist, we know we deleted the last service
|
|
|
|
// instance.
|
|
|
|
serviceState = Deleted
|
|
|
|
usageDeltas[serviceNamesUsageTable] -= 1
|
2021-09-30 21:15:26 +00:00
|
|
|
case count == delta:
|
2021-01-12 21:31:47 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2021-10-11 17:43:31 +00:00
|
|
|
// connectUsageTableEntry is a convenience function to make prefix addition in 1 place
|
2021-10-07 21:16:23 +00:00
|
|
|
func connectUsageTableName(kind string) string {
|
|
|
|
return fmt.Sprintf("%s-%s", connectPrefix, kind)
|
|
|
|
}
|
|
|
|
|
2021-10-11 17:43:31 +00:00
|
|
|
// configEntryUsageTableName is a convenience function to easily get the prefix + config entry kind in 1 place
|
|
|
|
func configEntryUsageTableName(kind string) string {
|
|
|
|
return fmt.Sprintf("%s-%s", tableConfigEntries, kind)
|
|
|
|
}
|
|
|
|
|
2021-09-30 21:15:26 +00:00
|
|
|
func connectDeltas(change memdb.Change, usageDeltas map[string]int, delta int) {
|
|
|
|
// Connect metrics for updated services are more complicated. Check for:
|
|
|
|
// 1. Did ServiceKind change?
|
|
|
|
// 2. Is before ServiceKind typical? don't remove from old service kind
|
|
|
|
// 3. Is After ServiceKind typical? don't add to new service kind
|
|
|
|
// 4. Add and remove to both ServiceKind's
|
|
|
|
if change.Updated() {
|
|
|
|
before := change.Before.(*structs.ServiceNode)
|
|
|
|
after := change.After.(*structs.ServiceNode)
|
|
|
|
if before.ServiceKind != structs.ServiceKindTypical {
|
2021-10-07 21:16:23 +00:00
|
|
|
usageDeltas[connectUsageTableName(string(before.ServiceKind))] -= 1
|
2021-09-30 21:15:26 +00:00
|
|
|
addEnterpriseConnectServiceInstanceUsage(usageDeltas, before, -1)
|
|
|
|
}
|
|
|
|
if after.ServiceKind != structs.ServiceKindTypical {
|
2021-10-07 21:16:23 +00:00
|
|
|
usageDeltas[connectUsageTableName(string(after.ServiceKind))] += 1
|
2021-09-30 21:15:26 +00:00
|
|
|
addEnterpriseConnectServiceInstanceUsage(usageDeltas, after, 1)
|
|
|
|
}
|
2021-09-30 21:48:21 +00:00
|
|
|
|
|
|
|
if before.ServiceConnect.Native != after.ServiceConnect.Native {
|
|
|
|
if before.ServiceConnect.Native {
|
2021-10-07 21:16:23 +00:00
|
|
|
usageDeltas[connectUsageTableName(string(connectNativeInstancesTable))] -= 1
|
2021-09-30 21:48:21 +00:00
|
|
|
addEnterpriseConnectServiceInstanceUsage(usageDeltas, before, -1)
|
|
|
|
} else {
|
2021-10-07 21:16:23 +00:00
|
|
|
usageDeltas[connectUsageTableName(connectNativeInstancesTable)] += 1
|
2021-09-30 21:48:21 +00:00
|
|
|
addEnterpriseConnectServiceInstanceUsage(usageDeltas, after, 1)
|
|
|
|
}
|
|
|
|
}
|
2021-09-30 21:15:26 +00:00
|
|
|
} else {
|
|
|
|
svc := changeObject(change).(*structs.ServiceNode)
|
|
|
|
if svc.ServiceKind != structs.ServiceKindTypical {
|
2021-10-07 21:16:23 +00:00
|
|
|
usageDeltas[connectUsageTableName(string(svc.ServiceKind))] += delta
|
2021-09-30 21:15:26 +00:00
|
|
|
}
|
2021-09-30 21:48:21 +00:00
|
|
|
if svc.ServiceConnect.Native {
|
2021-10-07 21:16:23 +00:00
|
|
|
usageDeltas[connectUsageTableName(connectNativeInstancesTable)] += delta
|
2021-09-30 21:48:21 +00:00
|
|
|
}
|
2021-09-30 21:15:26 +00:00
|
|
|
addEnterpriseConnectServiceInstanceUsage(usageDeltas, svc, delta)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-08 20:07:21 +00:00
|
|
|
// billableServiceInstancesDeltas calculates deltas for the billable services. Billable services
|
|
|
|
// are of "typical" service kind (i.e. non-connect or connect-native), excluding the "consul" service.
|
|
|
|
func billableServiceInstancesDeltas(change memdb.Change, usageDeltas map[string]int, delta int) {
|
|
|
|
// Billable service instances = # of typical service instances (i.e. non-connect) + connect-native service instances.
|
|
|
|
// Specifically, it should exclude "consul" service instances from the count.
|
|
|
|
//
|
|
|
|
// If the service has been updated, then we check
|
|
|
|
// 1. If the service name changed to or from "consul" and update deltas such that we exclude consul server service instances.
|
|
|
|
// This case is a bit contrived because we don't expect consul service to change once it's registered (beyond changing its instance count).
|
|
|
|
// a) If changed to "consul" -> decrement deltas by one
|
|
|
|
// b) If changed from "consul" and it's not a "connect" service -> increase deltas by one
|
|
|
|
// 2. If the service kind changed to or from "typical", we need to we need to update deltas so that we only account
|
|
|
|
// for non-connect or connect-native instances.
|
|
|
|
if change.Updated() {
|
|
|
|
// When there's an update, the delta arg passed to this function is 0, and so we need to explicitly increment
|
|
|
|
// or decrement by 1 depending on the situation.
|
|
|
|
before := change.Before.(*structs.ServiceNode)
|
|
|
|
after := change.After.(*structs.ServiceNode)
|
|
|
|
// Service name changed away from "consul" means we now need to account for this service instances unless it's a "connect" service.
|
|
|
|
if before.ServiceName == structs.ConsulServiceName && after.ServiceName != structs.ConsulServiceName {
|
|
|
|
if after.ServiceKind == structs.ServiceKindTypical {
|
|
|
|
usageDeltas[billableServiceInstancesTableName()] += 1
|
|
|
|
addEnterpriseBillableServiceInstanceUsage(usageDeltas, after, 1)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if before.ServiceName != structs.ConsulServiceName && after.ServiceName == structs.ConsulServiceName {
|
|
|
|
usageDeltas[billableServiceInstancesTableName()] -= 1
|
|
|
|
addEnterpriseBillableServiceInstanceUsage(usageDeltas, before, -1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if before.ServiceKind != structs.ServiceKindTypical && after.ServiceKind == structs.ServiceKindTypical {
|
|
|
|
usageDeltas[billableServiceInstancesTableName()] += 1
|
|
|
|
addEnterpriseBillableServiceInstanceUsage(usageDeltas, after, 1)
|
|
|
|
} else if before.ServiceKind == structs.ServiceKindTypical && after.ServiceKind != structs.ServiceKindTypical {
|
|
|
|
usageDeltas[billableServiceInstancesTableName()] -= 1
|
|
|
|
addEnterpriseBillableServiceInstanceUsage(usageDeltas, before, -1)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
svc := changeObject(change).(*structs.ServiceNode)
|
|
|
|
// If it's not an update, only update delta if it's a typical service and not the "consul" service.
|
|
|
|
if svc.ServiceKind == structs.ServiceKindTypical && svc.ServiceName != structs.ConsulServiceName {
|
|
|
|
usageDeltas[billableServiceInstancesTableName()] += delta
|
|
|
|
addEnterpriseBillableServiceInstanceUsage(usageDeltas, svc, delta)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-02 15:24:21 +00:00
|
|
|
// 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
|
|
|
}
|
2023-02-08 20:07:21 +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()
|
|
|
|
|
2022-09-09 14:02:01 +00:00
|
|
|
nodes, err := firstUsageEntry(nil, 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
|
|
|
}
|
|
|
|
|
2022-07-27 22:34:04 +00:00
|
|
|
// PeeringUsage returns the latest seen Raft index, a compiled set of peering usage
|
|
|
|
// data, and any errors.
|
|
|
|
func (s *Store) PeeringUsage() (uint64, PeeringUsage, error) {
|
|
|
|
tx := s.db.ReadTxn()
|
|
|
|
defer tx.Abort()
|
|
|
|
|
2022-09-09 14:02:01 +00:00
|
|
|
peerings, err := firstUsageEntry(nil, tx, tablePeering)
|
2022-07-27 22:34:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, PeeringUsage{}, fmt.Errorf("failed peerings lookup: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
usage := PeeringUsage{
|
|
|
|
Peerings: peerings.Count,
|
|
|
|
}
|
|
|
|
results, err := compileEnterprisePeeringUsage(tx, usage)
|
|
|
|
if err != nil {
|
|
|
|
return 0, PeeringUsage{}, fmt.Errorf("failed peerings lookup: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return peerings.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.
|
2023-02-08 20:07:21 +00:00
|
|
|
func (s *Store) ServiceUsage(ws memdb.WatchSet) (uint64, structs.ServiceUsage, error) {
|
2020-09-02 15:24:16 +00:00
|
|
|
tx := s.db.ReadTxn()
|
|
|
|
defer tx.Abort()
|
|
|
|
|
2022-09-09 14:02:01 +00:00
|
|
|
serviceInstances, err := firstUsageEntry(ws, tx, tableServices)
|
2020-09-02 15:24:16 +00:00
|
|
|
if err != nil {
|
2023-02-08 20:07:21 +00:00
|
|
|
return 0, structs.ServiceUsage{}, fmt.Errorf("failed services lookup: %s", err)
|
2020-09-02 15:24:16 +00:00
|
|
|
}
|
|
|
|
|
2022-09-09 14:02:01 +00:00
|
|
|
services, err := firstUsageEntry(ws, tx, serviceNamesUsageTable)
|
2020-09-02 15:24:21 +00:00
|
|
|
if err != nil {
|
2023-02-08 20:07:21 +00:00
|
|
|
return 0, structs.ServiceUsage{}, fmt.Errorf("failed services lookup: %s", err)
|
2020-09-02 15:24:21 +00:00
|
|
|
}
|
|
|
|
|
2023-07-05 15:37:51 +00:00
|
|
|
nodes, err := firstUsageEntry(ws, tx, tableNodes)
|
|
|
|
if err != nil {
|
|
|
|
return 0, structs.ServiceUsage{}, fmt.Errorf("failed nodes lookup: %s", err)
|
|
|
|
}
|
|
|
|
|
2021-09-30 21:15:26 +00:00
|
|
|
serviceKindInstances := make(map[string]int)
|
|
|
|
for _, kind := range allConnectKind {
|
2022-09-09 14:02:01 +00:00
|
|
|
usage, err := firstUsageEntry(ws, tx, connectUsageTableName(kind))
|
2021-09-30 21:15:26 +00:00
|
|
|
if err != nil {
|
2023-02-08 20:07:21 +00:00
|
|
|
return 0, structs.ServiceUsage{}, fmt.Errorf("failed services lookup: %s", err)
|
2021-09-30 21:15:26 +00:00
|
|
|
}
|
|
|
|
serviceKindInstances[kind] = usage.Count
|
|
|
|
}
|
|
|
|
|
2023-02-08 20:07:21 +00:00
|
|
|
billableServiceInstances, err := firstUsageEntry(ws, tx, billableServiceInstancesTableName())
|
|
|
|
if err != nil {
|
|
|
|
return 0, structs.ServiceUsage{}, fmt.Errorf("failed billable services lookup: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
usage := structs.ServiceUsage{
|
|
|
|
ServiceInstances: serviceInstances.Count,
|
|
|
|
Services: services.Count,
|
|
|
|
ConnectServiceInstances: serviceKindInstances,
|
|
|
|
BillableServiceInstances: billableServiceInstances.Count,
|
2023-07-05 15:37:51 +00:00
|
|
|
Nodes: nodes.Count,
|
2020-09-02 15:24:21 +00:00
|
|
|
}
|
2022-09-09 14:02:01 +00:00
|
|
|
results, err := compileEnterpriseServiceUsage(ws, tx, usage)
|
2020-09-02 15:24:16 +00:00
|
|
|
if err != nil {
|
2023-02-08 20:07:21 +00:00
|
|
|
return 0, structs.ServiceUsage{}, fmt.Errorf("failed services lookup: %s", err)
|
2020-09-02 15:24:16 +00:00
|
|
|
}
|
|
|
|
|
2020-09-02 15:24:21 +00:00
|
|
|
return serviceInstances.Index, results, nil
|
2020-09-02 15:24:16 +00:00
|
|
|
}
|
|
|
|
|
2021-09-17 19:36:34 +00:00
|
|
|
func (s *Store) KVUsage() (uint64, KVUsage, error) {
|
|
|
|
tx := s.db.ReadTxn()
|
|
|
|
defer tx.Abort()
|
|
|
|
|
2022-09-09 14:02:01 +00:00
|
|
|
kvs, err := firstUsageEntry(nil, tx, "kvs")
|
2021-09-17 19:36:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, KVUsage{}, fmt.Errorf("failed kvs lookup: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
usage := KVUsage{
|
|
|
|
KVCount: kvs.Count,
|
|
|
|
}
|
|
|
|
results, err := compileEnterpriseKVUsage(tx, usage)
|
|
|
|
if err != nil {
|
|
|
|
return 0, KVUsage{}, fmt.Errorf("failed kvs lookup: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return kvs.Index, results, nil
|
|
|
|
}
|
|
|
|
|
2021-10-07 21:19:55 +00:00
|
|
|
func (s *Store) ConfigEntryUsage() (uint64, ConfigEntryUsage, error) {
|
2021-10-01 18:22:30 +00:00
|
|
|
tx := s.db.ReadTxn()
|
|
|
|
defer tx.Abort()
|
|
|
|
|
|
|
|
configEntries := make(map[string]int)
|
|
|
|
var maxIdx uint64
|
|
|
|
for _, kind := range structs.AllConfigEntryKinds {
|
2022-09-09 14:02:01 +00:00
|
|
|
configEntry, err := firstUsageEntry(nil, tx, configEntryUsageTableName(kind))
|
2021-10-01 18:22:30 +00:00
|
|
|
if configEntry.Index > maxIdx {
|
|
|
|
maxIdx = configEntry.Index
|
|
|
|
}
|
|
|
|
if err != nil {
|
2021-10-07 21:19:55 +00:00
|
|
|
return 0, ConfigEntryUsage{}, fmt.Errorf("failed config entry usage lookup: %s", err)
|
2021-10-01 18:22:30 +00:00
|
|
|
}
|
|
|
|
configEntries[kind] = configEntry.Count
|
|
|
|
}
|
2021-10-07 21:19:55 +00:00
|
|
|
usage := ConfigEntryUsage{
|
2021-10-01 18:22:30 +00:00
|
|
|
ConfigByKind: configEntries,
|
|
|
|
}
|
2021-10-07 21:19:55 +00:00
|
|
|
results, err := compileEnterpriseConfigEntryUsage(tx, usage)
|
2021-10-01 18:22:30 +00:00
|
|
|
if err != nil {
|
2021-10-07 21:19:55 +00:00
|
|
|
return 0, ConfigEntryUsage{}, fmt.Errorf("failed config entry usage lookup: %s", err)
|
2021-10-01 18:22:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return maxIdx, results, nil
|
|
|
|
}
|
|
|
|
|
2022-09-09 14:02:01 +00:00
|
|
|
func firstUsageEntry(ws memdb.WatchSet, tx ReadTxn, id string) (*UsageEntry, error) {
|
|
|
|
watch, usage, err := tx.FirstWatch(tableUsage, indexID, id)
|
2020-09-02 15:24:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-09-09 14:02:01 +00:00
|
|
|
ws.Add(watch)
|
2020-09-02 15:24:16 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
2023-02-08 20:07:21 +00:00
|
|
|
|
|
|
|
func billableServiceInstancesTableName() string {
|
|
|
|
return fmt.Sprintf("billable-%s", tableServices)
|
|
|
|
}
|