2017-01-13 19:47:16 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-08-17 18:29:39 +00:00
|
|
|
"strings"
|
2017-01-13 19:47:16 +00:00
|
|
|
|
2021-01-30 00:24:41 +00:00
|
|
|
"github.com/hashicorp/go-memdb"
|
|
|
|
|
2022-04-05 21:10:06 +00:00
|
|
|
"github.com/hashicorp/consul/acl"
|
2017-07-06 10:34:00 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2017-08-14 14:36:07 +00:00
|
|
|
"github.com/hashicorp/consul/lib"
|
2017-01-13 19:47:16 +00:00
|
|
|
)
|
|
|
|
|
2021-08-17 18:29:39 +00:00
|
|
|
const tableCoordinates = "coordinates"
|
|
|
|
|
|
|
|
func indexFromCoordinate(raw interface{}) ([]byte, error) {
|
|
|
|
c, ok := raw.(*structs.Coordinate)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("unexpected type %T for structs.Coordinate index", raw)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Node == "" {
|
|
|
|
return nil, errMissingValueForIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
var b indexBuilder
|
|
|
|
b.String(strings.ToLower(c.Node))
|
|
|
|
b.String(strings.ToLower(c.Segment))
|
|
|
|
return b.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func indexNodeFromCoordinate(raw interface{}) ([]byte, error) {
|
|
|
|
c, ok := raw.(*structs.Coordinate)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("unexpected type %T for structs.Coordinate index", raw)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Node == "" {
|
|
|
|
return nil, errMissingValueForIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
var b indexBuilder
|
|
|
|
b.String(strings.ToLower(c.Node))
|
|
|
|
return b.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func indexFromCoordinateQuery(raw interface{}) ([]byte, error) {
|
|
|
|
q, ok := raw.(CoordinateQuery)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("unexpected type %T for CoordinateQuery index", raw)
|
|
|
|
}
|
|
|
|
|
|
|
|
if q.Node == "" {
|
|
|
|
return nil, errMissingValueForIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
var b indexBuilder
|
|
|
|
b.String(strings.ToLower(q.Node))
|
|
|
|
b.String(strings.ToLower(q.Segment))
|
|
|
|
return b.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type CoordinateQuery struct {
|
|
|
|
Node string
|
|
|
|
Segment string
|
|
|
|
Partition string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c CoordinateQuery) PartitionOrDefault() string {
|
2022-04-05 21:10:06 +00:00
|
|
|
return acl.PartitionOrDefault(c.Partition)
|
2021-08-17 18:29:39 +00:00
|
|
|
}
|
|
|
|
|
2017-11-29 01:03:34 +00:00
|
|
|
// coordinatesTableSchema returns a new table schema used for storing
|
|
|
|
// network coordinates.
|
|
|
|
func coordinatesTableSchema() *memdb.TableSchema {
|
|
|
|
return &memdb.TableSchema{
|
2021-08-17 18:29:39 +00:00
|
|
|
Name: tableCoordinates,
|
2017-11-29 01:03:34 +00:00
|
|
|
Indexes: map[string]*memdb.IndexSchema{
|
2021-08-17 18:29:39 +00:00
|
|
|
indexID: {
|
|
|
|
Name: indexID,
|
2017-11-29 01:03:34 +00:00
|
|
|
AllowMissing: false,
|
|
|
|
Unique: true,
|
2021-08-17 18:29:39 +00:00
|
|
|
Indexer: indexerSingleWithPrefix{
|
|
|
|
readIndex: indexFromCoordinateQuery,
|
|
|
|
writeIndex: indexFromCoordinate,
|
|
|
|
prefixIndex: prefixIndexFromQueryNoNamespace,
|
2017-11-29 01:03:34 +00:00
|
|
|
},
|
|
|
|
},
|
2021-08-17 18:29:39 +00:00
|
|
|
indexNode: {
|
|
|
|
Name: indexNode,
|
2017-11-29 01:03:34 +00:00
|
|
|
AllowMissing: false,
|
|
|
|
Unique: false,
|
2021-08-17 18:29:39 +00:00
|
|
|
Indexer: indexerSingle{
|
|
|
|
readIndex: indexFromQuery,
|
|
|
|
writeIndex: indexNodeFromCoordinate,
|
2017-11-29 01:03:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-13 19:47:16 +00:00
|
|
|
// Coordinates is used to pull all the coordinates from the snapshot.
|
2017-04-21 00:46:29 +00:00
|
|
|
func (s *Snapshot) Coordinates() (memdb.ResultIterator, error) {
|
2021-08-17 18:29:39 +00:00
|
|
|
iter, err := s.tx.Get(tableCoordinates, indexID)
|
2017-01-13 19:47:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return iter, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Coordinates is used when restoring from a snapshot. For general inserts, use
|
|
|
|
// CoordinateBatchUpdate. We do less vetting of the updates here because they
|
|
|
|
// already got checked on the way in during a batch update.
|
2017-04-21 00:46:29 +00:00
|
|
|
func (s *Restore) Coordinates(idx uint64, updates structs.Coordinates) error {
|
2017-01-13 19:47:16 +00:00
|
|
|
for _, update := range updates {
|
2017-05-26 00:37:16 +00:00
|
|
|
// Skip any bad data that may have gotten into the database from
|
|
|
|
// a bad client in the past.
|
|
|
|
if !update.Coord.IsValid() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-08-17 18:29:39 +00:00
|
|
|
if err := ensureCoordinateTxn(s.tx, idx, update); err != nil {
|
2017-01-13 19:47:16 +00:00
|
|
|
return fmt.Errorf("failed restoring coordinate: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-08-14 14:36:07 +00:00
|
|
|
// Coordinate returns a map of coordinates for the given node, indexed by
|
|
|
|
// network segment.
|
2022-04-05 21:10:06 +00:00
|
|
|
func (s *Store) Coordinate(ws memdb.WatchSet, node string, entMeta *acl.EnterpriseMeta) (uint64, lib.CoordinateSet, error) {
|
2017-01-13 19:47:16 +00:00
|
|
|
tx := s.db.Txn(false)
|
|
|
|
defer tx.Abort()
|
|
|
|
|
2021-08-17 18:29:39 +00:00
|
|
|
// TODO: accept non-pointer value
|
|
|
|
if entMeta == nil {
|
|
|
|
entMeta = structs.NodeEnterpriseMetaInDefaultPartition()
|
|
|
|
}
|
|
|
|
|
|
|
|
tableIdx := coordinatesMaxIndex(tx, entMeta)
|
2017-10-27 02:16:40 +00:00
|
|
|
|
2021-08-17 18:29:39 +00:00
|
|
|
iter, err := tx.Get(tableCoordinates, indexNode, Query{
|
|
|
|
Value: node,
|
|
|
|
EnterpriseMeta: *entMeta,
|
|
|
|
})
|
2017-01-13 19:47:16 +00:00
|
|
|
if err != nil {
|
2017-10-27 02:16:40 +00:00
|
|
|
return 0, nil, fmt.Errorf("failed coordinate lookup: %s", err)
|
2017-01-13 19:47:16 +00:00
|
|
|
}
|
2017-10-27 02:16:40 +00:00
|
|
|
ws.Add(iter.WatchCh())
|
2017-01-13 19:47:16 +00:00
|
|
|
|
2017-08-14 14:36:07 +00:00
|
|
|
results := make(lib.CoordinateSet)
|
|
|
|
for raw := iter.Next(); raw != nil; raw = iter.Next() {
|
|
|
|
coord := raw.(*structs.Coordinate)
|
|
|
|
results[coord.Segment] = coord.Coord
|
2017-01-13 19:47:16 +00:00
|
|
|
}
|
2017-10-31 20:34:49 +00:00
|
|
|
return tableIdx, results, nil
|
2017-01-13 19:47:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Coordinates queries for all nodes with coordinates.
|
2022-04-05 21:10:06 +00:00
|
|
|
func (s *Store) Coordinates(ws memdb.WatchSet, entMeta *acl.EnterpriseMeta) (uint64, structs.Coordinates, error) {
|
2017-01-13 19:47:16 +00:00
|
|
|
tx := s.db.Txn(false)
|
|
|
|
defer tx.Abort()
|
|
|
|
|
2021-08-17 18:29:39 +00:00
|
|
|
// TODO: accept non-pointer value
|
|
|
|
if entMeta == nil {
|
|
|
|
entMeta = structs.NodeEnterpriseMetaInDefaultPartition()
|
|
|
|
}
|
|
|
|
|
2017-01-13 19:47:16 +00:00
|
|
|
// Get the table index.
|
2021-08-17 18:29:39 +00:00
|
|
|
idx := coordinatesMaxIndex(tx, entMeta)
|
2017-01-13 19:47:16 +00:00
|
|
|
|
|
|
|
// Pull all the coordinates.
|
2021-08-17 18:29:39 +00:00
|
|
|
iter, err := tx.Get(tableCoordinates, indexID+"_prefix", entMeta)
|
2017-01-13 19:47:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, nil, fmt.Errorf("failed coordinate lookup: %s", err)
|
|
|
|
}
|
2017-01-24 16:13:48 +00:00
|
|
|
ws.Add(iter.WatchCh())
|
|
|
|
|
2017-01-13 19:47:16 +00:00
|
|
|
var results structs.Coordinates
|
2017-01-24 16:13:48 +00:00
|
|
|
for coord := iter.Next(); coord != nil; coord = iter.Next() {
|
2017-01-13 19:47:16 +00:00
|
|
|
results = append(results, coord.(*structs.Coordinate))
|
|
|
|
}
|
|
|
|
return idx, results, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CoordinateBatchUpdate processes a batch of coordinate updates and applies
|
|
|
|
// them in a single transaction.
|
2017-04-21 00:46:29 +00:00
|
|
|
func (s *Store) CoordinateBatchUpdate(idx uint64, updates structs.Coordinates) error {
|
2020-03-19 13:11:20 +00:00
|
|
|
tx := s.db.WriteTxn(idx)
|
2017-01-13 19:47:16 +00:00
|
|
|
defer tx.Abort()
|
|
|
|
|
|
|
|
// Upsert the coordinates.
|
|
|
|
for _, update := range updates {
|
2017-05-26 00:37:16 +00:00
|
|
|
// Skip any bad data that may have gotten into the database from
|
|
|
|
// a bad client in the past.
|
|
|
|
if !update.Coord.IsValid() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-08-17 18:29:39 +00:00
|
|
|
entMeta := update.GetEnterpriseMeta()
|
|
|
|
|
2017-01-13 19:47:16 +00:00
|
|
|
// Since the cleanup of coordinates is tied to deletion of
|
|
|
|
// nodes, we silently drop any updates for nodes that we don't
|
|
|
|
// know about. This might be possible during normal operation
|
|
|
|
// if we happen to get a coordinate update for a node that
|
|
|
|
// hasn't been able to add itself to the catalog yet. Since we
|
|
|
|
// don't carefully sequence this, and since it will fix itself
|
|
|
|
// on the next coordinate update from that node, we don't return
|
|
|
|
// an error or log anything.
|
2021-08-17 18:29:39 +00:00
|
|
|
node, err := tx.First(tableNodes, indexID, Query{
|
|
|
|
Value: update.Node,
|
|
|
|
EnterpriseMeta: *entMeta,
|
|
|
|
})
|
2017-01-13 19:47:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed node lookup: %s", err)
|
|
|
|
}
|
|
|
|
if node == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-08-17 18:29:39 +00:00
|
|
|
if err := ensureCoordinateTxn(tx, idx, update); err != nil {
|
2017-01-13 19:47:16 +00:00
|
|
|
return fmt.Errorf("failed inserting coordinate: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-17 18:29:39 +00:00
|
|
|
return tx.Commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteCoordinateTxn(tx WriteTxn, idx uint64, coord *structs.Coordinate) error {
|
|
|
|
if err := tx.Delete(tableCoordinates, coord); err != nil {
|
|
|
|
return fmt.Errorf("failed deleting coordinate: %s", err)
|
2017-01-13 19:47:16 +00:00
|
|
|
}
|
|
|
|
|
2021-08-17 18:29:39 +00:00
|
|
|
if err := updateCoordinatesIndexes(tx, idx, coord.GetEnterpriseMeta()); err != nil {
|
|
|
|
return fmt.Errorf("failed updating coordinate index: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2017-01-13 19:47:16 +00:00
|
|
|
}
|