2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2017-01-13 19:47:16 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
2017-05-26 00:37:16 +00:00
|
|
|
"math"
|
2017-01-13 19:47:16 +00:00
|
|
|
"math/rand"
|
|
|
|
"testing"
|
|
|
|
|
2017-01-24 16:13:48 +00:00
|
|
|
"github.com/hashicorp/go-memdb"
|
2017-01-13 19:47:16 +00:00
|
|
|
"github.com/hashicorp/serf/coordinate"
|
2020-06-02 16:41:25 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-07-23 18:42:23 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
|
|
|
"github.com/hashicorp/consul/lib"
|
2022-05-10 20:25:51 +00:00
|
|
|
"github.com/hashicorp/consul/sdk/testutil"
|
2017-01-13 19:47:16 +00:00
|
|
|
)
|
|
|
|
|
2021-07-23 18:42:23 +00:00
|
|
|
// TODO(partitions): test partitioned nodes here
|
|
|
|
|
2017-01-13 19:47:16 +00:00
|
|
|
// generateRandomCoordinate creates a random coordinate. This mucks with the
|
|
|
|
// underlying structure directly, so it's not really useful for any particular
|
|
|
|
// position in the network, but it's a good payload to send through to make
|
|
|
|
// sure things come out the other side or get stored correctly.
|
|
|
|
func generateRandomCoordinate() *coordinate.Coordinate {
|
|
|
|
config := coordinate.DefaultConfig()
|
|
|
|
coord := coordinate.NewCoordinate(config)
|
|
|
|
for i := range coord.Vec {
|
|
|
|
coord.Vec[i] = rand.NormFloat64()
|
|
|
|
}
|
|
|
|
coord.Error = rand.NormFloat64()
|
|
|
|
coord.Adjustment = rand.NormFloat64()
|
|
|
|
return coord
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStateStore_Coordinate_Updates(t *testing.T) {
|
2020-12-07 18:42:55 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("too slow for testing.Short")
|
|
|
|
}
|
|
|
|
|
2017-01-13 19:47:16 +00:00
|
|
|
s := testStateStore(t)
|
|
|
|
|
|
|
|
// Make sure the coordinates list starts out empty, and that a query for
|
2017-08-14 14:36:07 +00:00
|
|
|
// a per-node coordinate for a nonexistent node doesn't do anything bad.
|
2017-01-24 16:13:48 +00:00
|
|
|
ws := memdb.NewWatchSet()
|
2021-07-23 18:42:23 +00:00
|
|
|
idx, all, err := s.Coordinates(ws, nil)
|
2021-08-17 18:29:39 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, uint64(0), idx, "bad index")
|
2020-06-02 16:41:25 +00:00
|
|
|
require.Nil(t, all)
|
2017-08-14 14:36:07 +00:00
|
|
|
|
2017-10-31 22:08:14 +00:00
|
|
|
coordinateWs := memdb.NewWatchSet()
|
2021-07-23 18:42:23 +00:00
|
|
|
_, coords, err := s.Coordinate(coordinateWs, "nope", nil)
|
2021-08-17 18:29:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-02 16:41:25 +00:00
|
|
|
require.Equal(t, lib.CoordinateSet{}, coords)
|
2017-01-13 19:47:16 +00:00
|
|
|
|
|
|
|
// Make an update for nodes that don't exist and make sure they get
|
|
|
|
// ignored.
|
|
|
|
updates := structs.Coordinates{
|
|
|
|
&structs.Coordinate{
|
|
|
|
Node: "node1",
|
|
|
|
Coord: generateRandomCoordinate(),
|
|
|
|
},
|
|
|
|
&structs.Coordinate{
|
|
|
|
Node: "node2",
|
|
|
|
Coord: generateRandomCoordinate(),
|
|
|
|
},
|
|
|
|
}
|
2021-08-17 18:29:39 +00:00
|
|
|
require.NoError(t, s.CoordinateBatchUpdate(1, updates))
|
|
|
|
require.False(t, watchFired(ws) || watchFired(coordinateWs))
|
2017-01-13 19:47:16 +00:00
|
|
|
|
2021-08-17 18:29:39 +00:00
|
|
|
// Should still be empty, though applying an empty batch does NOT bump
|
2017-01-13 19:47:16 +00:00
|
|
|
// the table index.
|
2017-01-24 16:13:48 +00:00
|
|
|
ws = memdb.NewWatchSet()
|
2021-07-23 18:42:23 +00:00
|
|
|
idx, all, err = s.Coordinates(ws, nil)
|
2021-08-17 18:29:39 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, uint64(0), idx, "bad index")
|
2020-06-02 16:41:25 +00:00
|
|
|
require.Nil(t, all)
|
2017-01-13 19:47:16 +00:00
|
|
|
|
2017-10-31 22:08:14 +00:00
|
|
|
coordinateWs = memdb.NewWatchSet()
|
2021-07-23 18:42:23 +00:00
|
|
|
idx, _, err = s.Coordinate(coordinateWs, "node1", nil)
|
2021-08-17 18:29:39 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, uint64(0), idx, "bad index")
|
2017-10-31 22:08:14 +00:00
|
|
|
|
2017-01-13 19:47:16 +00:00
|
|
|
// Register the nodes then do the update again.
|
|
|
|
testRegisterNode(t, s, 1, "node1")
|
|
|
|
testRegisterNode(t, s, 2, "node2")
|
2021-08-17 18:29:39 +00:00
|
|
|
require.NoError(t, s.CoordinateBatchUpdate(3, updates))
|
2017-10-31 22:08:14 +00:00
|
|
|
if !watchFired(ws) || !watchFired(coordinateWs) {
|
2017-01-24 16:13:48 +00:00
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
2017-01-13 19:47:16 +00:00
|
|
|
|
|
|
|
// Should go through now.
|
2017-01-24 16:13:48 +00:00
|
|
|
ws = memdb.NewWatchSet()
|
2021-07-23 18:42:23 +00:00
|
|
|
idx, all, err = s.Coordinates(ws, nil)
|
2021-08-17 18:29:39 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, uint64(3), idx, "bad index")
|
2020-06-02 16:41:25 +00:00
|
|
|
require.Equal(t, updates, all)
|
2017-01-13 19:47:16 +00:00
|
|
|
|
2017-08-14 14:36:07 +00:00
|
|
|
// Also verify the per-node coordinate interface.
|
2017-10-31 22:08:14 +00:00
|
|
|
nodeWs := make([]memdb.WatchSet, len(updates))
|
|
|
|
for i, update := range updates {
|
|
|
|
nodeWs[i] = memdb.NewWatchSet()
|
2021-07-23 18:42:23 +00:00
|
|
|
idx, coords, err := s.Coordinate(nodeWs[i], update.Node, nil)
|
2021-08-17 18:29:39 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, uint64(3), idx, "bad index")
|
2017-08-14 14:36:07 +00:00
|
|
|
expected := lib.CoordinateSet{
|
|
|
|
"": update.Coord,
|
2017-01-13 19:47:16 +00:00
|
|
|
}
|
2020-06-02 16:41:25 +00:00
|
|
|
require.Equal(t, expected, coords)
|
2017-01-13 19:47:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update the coordinate for one of the nodes.
|
|
|
|
updates[1].Coord = generateRandomCoordinate()
|
2021-08-17 18:29:39 +00:00
|
|
|
require.NoError(t, s.CoordinateBatchUpdate(4, updates))
|
2017-01-24 16:13:48 +00:00
|
|
|
if !watchFired(ws) {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
2017-10-31 22:08:14 +00:00
|
|
|
for _, ws := range nodeWs {
|
|
|
|
if !watchFired(ws) {
|
|
|
|
t.Fatalf("bad")
|
|
|
|
}
|
|
|
|
}
|
2017-01-13 19:47:16 +00:00
|
|
|
|
|
|
|
// Verify it got applied.
|
2021-07-23 18:42:23 +00:00
|
|
|
idx, all, err = s.Coordinates(nil, nil)
|
2021-08-17 18:29:39 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, uint64(4), idx, "bad index")
|
2020-06-02 16:41:25 +00:00
|
|
|
require.Equal(t, updates, all)
|
2017-01-13 19:47:16 +00:00
|
|
|
|
2017-08-14 14:36:07 +00:00
|
|
|
// And check the per-node coordinate version of the same thing.
|
2017-01-13 19:47:16 +00:00
|
|
|
for _, update := range updates {
|
2021-07-23 18:42:23 +00:00
|
|
|
idx, coords, err := s.Coordinate(nil, update.Node, nil)
|
2021-08-17 18:29:39 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, uint64(4), idx, "bad index")
|
2017-08-14 14:36:07 +00:00
|
|
|
expected := lib.CoordinateSet{
|
|
|
|
"": update.Coord,
|
2017-01-13 19:47:16 +00:00
|
|
|
}
|
2020-06-02 16:41:25 +00:00
|
|
|
require.Equal(t, expected, coords)
|
2017-01-13 19:47:16 +00:00
|
|
|
}
|
2017-05-26 00:37:16 +00:00
|
|
|
|
|
|
|
// Apply an invalid update and make sure it gets ignored.
|
|
|
|
badUpdates := structs.Coordinates{
|
|
|
|
&structs.Coordinate{
|
|
|
|
Node: "node1",
|
|
|
|
Coord: &coordinate.Coordinate{Height: math.NaN()},
|
|
|
|
},
|
|
|
|
}
|
2021-08-17 18:29:39 +00:00
|
|
|
require.NoError(t, s.CoordinateBatchUpdate(5, badUpdates))
|
2017-05-26 00:37:16 +00:00
|
|
|
|
2021-08-17 18:29:39 +00:00
|
|
|
// Verify we are at the previous state, and verify that the empty batch
|
|
|
|
// does NOT bump the table index.
|
2021-07-23 18:42:23 +00:00
|
|
|
idx, all, err = s.Coordinates(nil, nil)
|
2021-08-17 18:29:39 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, uint64(4), idx, "bad index")
|
2020-06-02 16:41:25 +00:00
|
|
|
require.Equal(t, updates, all)
|
2017-01-13 19:47:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStateStore_Coordinate_Cleanup(t *testing.T) {
|
|
|
|
s := testStateStore(t)
|
|
|
|
|
|
|
|
// Register a node and update its coordinate.
|
|
|
|
testRegisterNode(t, s, 1, "node1")
|
|
|
|
updates := structs.Coordinates{
|
|
|
|
&structs.Coordinate{
|
2017-08-14 14:36:07 +00:00
|
|
|
Node: "node1",
|
|
|
|
Segment: "alpha",
|
|
|
|
Coord: generateRandomCoordinate(),
|
|
|
|
},
|
|
|
|
&structs.Coordinate{
|
|
|
|
Node: "node1",
|
|
|
|
Segment: "beta",
|
|
|
|
Coord: generateRandomCoordinate(),
|
2017-01-13 19:47:16 +00:00
|
|
|
},
|
|
|
|
}
|
2021-08-17 18:29:39 +00:00
|
|
|
require.NoError(t, s.CoordinateBatchUpdate(2, updates))
|
2017-01-13 19:47:16 +00:00
|
|
|
|
|
|
|
// Make sure it's in there.
|
2021-07-23 18:42:23 +00:00
|
|
|
_, coords, err := s.Coordinate(nil, "node1", nil)
|
2021-08-17 18:29:39 +00:00
|
|
|
require.NoError(t, err)
|
2017-08-14 14:36:07 +00:00
|
|
|
expected := lib.CoordinateSet{
|
|
|
|
"alpha": updates[0].Coord,
|
|
|
|
"beta": updates[1].Coord,
|
2017-01-13 19:47:16 +00:00
|
|
|
}
|
2020-06-02 16:41:25 +00:00
|
|
|
require.Equal(t, expected, coords)
|
2017-01-13 19:47:16 +00:00
|
|
|
|
|
|
|
// Now delete the node.
|
peering: initial sync (#12842)
- Add endpoints related to peering: read, list, generate token, initiate peering
- Update node/service/check table indexing to account for peers
- Foundational changes for pushing service updates to a peer
- Plumb peer name through Health.ServiceNodes path
see: ENT-1765, ENT-1280, ENT-1283, ENT-1283, ENT-1756, ENT-1739, ENT-1750, ENT-1679,
ENT-1709, ENT-1704, ENT-1690, ENT-1689, ENT-1702, ENT-1701, ENT-1683, ENT-1663,
ENT-1650, ENT-1678, ENT-1628, ENT-1658, ENT-1640, ENT-1637, ENT-1597, ENT-1634,
ENT-1613, ENT-1616, ENT-1617, ENT-1591, ENT-1588, ENT-1596, ENT-1572, ENT-1555
Co-authored-by: R.B. Boyer <rb@hashicorp.com>
Co-authored-by: freddygv <freddy@hashicorp.com>
Co-authored-by: Chris S. Kim <ckim@hashicorp.com>
Co-authored-by: Evan Culver <eculver@hashicorp.com>
Co-authored-by: Nitya Dhanushkodi <nitya@hashicorp.com>
2022-04-21 22:34:40 +00:00
|
|
|
require.NoError(t, s.DeleteNode(3, "node1", nil, ""))
|
2017-01-13 19:47:16 +00:00
|
|
|
|
|
|
|
// Make sure the coordinate is gone.
|
2021-07-23 18:42:23 +00:00
|
|
|
_, coords, err = s.Coordinate(nil, "node1", nil)
|
2021-08-17 18:29:39 +00:00
|
|
|
require.NoError(t, err)
|
2020-06-02 16:41:25 +00:00
|
|
|
require.Equal(t, lib.CoordinateSet{}, coords)
|
2017-01-13 19:47:16 +00:00
|
|
|
|
|
|
|
// Make sure the index got updated.
|
2021-07-23 18:42:23 +00:00
|
|
|
idx, all, err := s.Coordinates(nil, nil)
|
2021-08-17 18:29:39 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, uint64(3), idx, "bad index")
|
2020-06-02 16:41:25 +00:00
|
|
|
require.Nil(t, all)
|
2017-01-13 19:47:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStateStore_Coordinate_Snapshot_Restore(t *testing.T) {
|
|
|
|
s := testStateStore(t)
|
|
|
|
|
|
|
|
// Register two nodes and update their coordinates.
|
|
|
|
testRegisterNode(t, s, 1, "node1")
|
|
|
|
testRegisterNode(t, s, 2, "node2")
|
|
|
|
updates := structs.Coordinates{
|
|
|
|
&structs.Coordinate{
|
|
|
|
Node: "node1",
|
|
|
|
Coord: generateRandomCoordinate(),
|
|
|
|
},
|
|
|
|
&structs.Coordinate{
|
|
|
|
Node: "node2",
|
|
|
|
Coord: generateRandomCoordinate(),
|
|
|
|
},
|
|
|
|
}
|
2021-08-17 18:29:39 +00:00
|
|
|
require.NoError(t, s.CoordinateBatchUpdate(3, updates))
|
2017-01-13 19:47:16 +00:00
|
|
|
|
2017-05-26 00:37:16 +00:00
|
|
|
// Manually put a bad coordinate in for node3.
|
|
|
|
testRegisterNode(t, s, 4, "node3")
|
|
|
|
badUpdate := &structs.Coordinate{
|
|
|
|
Node: "node3",
|
|
|
|
Coord: &coordinate.Coordinate{Height: math.NaN()},
|
|
|
|
}
|
2020-03-19 13:11:20 +00:00
|
|
|
tx := s.db.WriteTxn(5)
|
2021-08-17 18:29:39 +00:00
|
|
|
require.NoError(t, tx.Insert("coordinates", badUpdate))
|
2020-06-02 20:34:56 +00:00
|
|
|
require.NoError(t, tx.Commit())
|
2017-05-26 00:37:16 +00:00
|
|
|
|
2017-01-13 19:47:16 +00:00
|
|
|
// Snapshot the coordinates.
|
|
|
|
snap := s.Snapshot()
|
|
|
|
defer snap.Close()
|
|
|
|
|
|
|
|
// Alter the real state store.
|
|
|
|
trash := structs.Coordinates{
|
|
|
|
&structs.Coordinate{
|
|
|
|
Node: "node1",
|
|
|
|
Coord: generateRandomCoordinate(),
|
|
|
|
},
|
|
|
|
&structs.Coordinate{
|
|
|
|
Node: "node2",
|
|
|
|
Coord: generateRandomCoordinate(),
|
|
|
|
},
|
|
|
|
}
|
2021-08-17 18:29:39 +00:00
|
|
|
require.NoError(t, s.CoordinateBatchUpdate(5, trash))
|
2017-01-13 19:47:16 +00:00
|
|
|
|
|
|
|
// Verify the snapshot.
|
2021-08-17 18:29:39 +00:00
|
|
|
require.Equal(t, uint64(4), snap.LastIndex(), "bad index")
|
2017-01-13 19:47:16 +00:00
|
|
|
iter, err := snap.Coordinates()
|
2021-08-17 18:29:39 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2017-01-13 19:47:16 +00:00
|
|
|
var dump structs.Coordinates
|
|
|
|
for coord := iter.Next(); coord != nil; coord = iter.Next() {
|
|
|
|
dump = append(dump, coord.(*structs.Coordinate))
|
|
|
|
}
|
2017-05-26 00:37:16 +00:00
|
|
|
|
|
|
|
// The snapshot will have the bad update in it, since we don't filter on
|
|
|
|
// the read side.
|
2020-06-02 16:41:25 +00:00
|
|
|
require.Equal(t, append(updates, badUpdate), dump)
|
2017-01-13 19:47:16 +00:00
|
|
|
|
2022-05-10 20:25:51 +00:00
|
|
|
testutil.RunStep(t, "restore the values into a new state store", func(t *testing.T) {
|
2017-01-13 19:47:16 +00:00
|
|
|
s := testStateStore(t)
|
|
|
|
restore := s.Restore()
|
2021-08-17 18:29:39 +00:00
|
|
|
require.NoError(t, restore.Coordinates(6, dump))
|
2017-01-13 19:47:16 +00:00
|
|
|
restore.Commit()
|
|
|
|
|
|
|
|
// Read the restored coordinates back out and verify that they match.
|
2021-07-23 18:42:23 +00:00
|
|
|
idx, res, err := s.Coordinates(nil, nil)
|
2021-08-17 18:29:39 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, uint64(6), idx, "bad index")
|
2020-06-02 16:41:25 +00:00
|
|
|
require.Equal(t, updates, res)
|
2017-01-13 19:47:16 +00:00
|
|
|
|
|
|
|
// Check that the index was updated (note that it got passed
|
|
|
|
// in during the restore).
|
2021-08-17 18:29:39 +00:00
|
|
|
require.Equal(t, uint64(6), s.maxIndex("coordinates"), "bad index")
|
|
|
|
})
|
2017-01-13 19:47:16 +00:00
|
|
|
}
|