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-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-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"
|
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) {
|
|
|
|
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()
|
2017-08-14 14:36:07 +00:00
|
|
|
idx, all, err := s.Coordinates(ws)
|
2017-01-13 19:47:16 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if idx != 0 {
|
|
|
|
t.Fatalf("bad index: %d", idx)
|
|
|
|
}
|
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()
|
|
|
|
_, coords, err := s.Coordinate("nope", coordinateWs)
|
2017-01-13 19:47:16 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", 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(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := s.CoordinateBatchUpdate(1, updates); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
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 still be empty, though applying an empty batch does bump
|
|
|
|
// the table index.
|
2017-01-24 16:13:48 +00:00
|
|
|
ws = memdb.NewWatchSet()
|
2017-08-14 14:36:07 +00:00
|
|
|
idx, all, err = s.Coordinates(ws)
|
2017-01-13 19:47:16 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if idx != 1 {
|
|
|
|
t.Fatalf("bad index: %d", idx)
|
|
|
|
}
|
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()
|
|
|
|
idx, coords, err = s.Coordinate("node1", coordinateWs)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if idx != 1 {
|
|
|
|
t.Fatalf("bad index: %d", idx)
|
|
|
|
}
|
|
|
|
|
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")
|
|
|
|
if err := s.CoordinateBatchUpdate(3, updates); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
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()
|
2017-08-14 14:36:07 +00:00
|
|
|
idx, all, err = s.Coordinates(ws)
|
2017-01-13 19:47:16 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if idx != 3 {
|
|
|
|
t.Fatalf("bad index: %d", idx)
|
|
|
|
}
|
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()
|
|
|
|
idx, coords, err := s.Coordinate(update.Node, nodeWs[i])
|
2017-01-13 19:47:16 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2017-10-31 22:08:14 +00:00
|
|
|
if idx != 3 {
|
|
|
|
t.Fatalf("bad index: %d", idx)
|
|
|
|
}
|
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()
|
|
|
|
if err := s.CoordinateBatchUpdate(4, updates); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
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.
|
2017-08-14 14:36:07 +00:00
|
|
|
idx, all, err = s.Coordinates(nil)
|
2017-01-13 19:47:16 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if idx != 4 {
|
|
|
|
t.Fatalf("bad index: %d", idx)
|
|
|
|
}
|
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 {
|
2017-10-31 22:08:14 +00:00
|
|
|
idx, coords, err := s.Coordinate(update.Node, nil)
|
2017-01-13 19:47:16 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2017-10-31 22:08:14 +00:00
|
|
|
if idx != 4 {
|
|
|
|
t.Fatalf("bad index: %d", idx)
|
|
|
|
}
|
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()},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := s.CoordinateBatchUpdate(5, badUpdates); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify we are at the previous state, though the empty batch does bump
|
|
|
|
// the table index.
|
2017-08-14 14:36:07 +00:00
|
|
|
idx, all, err = s.Coordinates(nil)
|
2017-05-26 00:37:16 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if idx != 5 {
|
|
|
|
t.Fatalf("bad index: %d", idx)
|
|
|
|
}
|
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
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := s.CoordinateBatchUpdate(2, updates); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure it's in there.
|
2017-10-27 02:16:40 +00:00
|
|
|
_, coords, err := s.Coordinate("node1", nil)
|
2017-01-13 19:47:16 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", 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.
|
|
|
|
if err := s.DeleteNode(3, "node1"); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the coordinate is gone.
|
2017-10-27 02:16:40 +00:00
|
|
|
_, coords, err = s.Coordinate("node1", nil)
|
2017-01-13 19:47:16 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", 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.
|
2017-08-14 14:36:07 +00:00
|
|
|
idx, all, err := s.Coordinates(nil)
|
2017-01-13 19:47:16 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if idx != 3 {
|
|
|
|
t.Fatalf("bad index: %d", idx)
|
|
|
|
}
|
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(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err := s.CoordinateBatchUpdate(3, updates); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
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()},
|
|
|
|
}
|
|
|
|
tx := s.db.Txn(true)
|
|
|
|
if err := tx.Insert("coordinates", badUpdate); err != nil {
|
|
|
|
t.Fatalf("err: %v", err)
|
|
|
|
}
|
|
|
|
tx.Commit()
|
|
|
|
|
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(),
|
|
|
|
},
|
|
|
|
}
|
2017-05-26 00:37:16 +00:00
|
|
|
if err := s.CoordinateBatchUpdate(5, trash); err != nil {
|
2017-01-13 19:47:16 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the snapshot.
|
2017-05-26 00:37:16 +00:00
|
|
|
if idx := snap.LastIndex(); idx != 4 {
|
2017-01-13 19:47:16 +00:00
|
|
|
t.Fatalf("bad index: %d", idx)
|
|
|
|
}
|
|
|
|
iter, err := snap.Coordinates()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
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
|
|
|
|
|
|
|
// Restore the values into a new state store.
|
|
|
|
func() {
|
|
|
|
s := testStateStore(t)
|
|
|
|
restore := s.Restore()
|
2017-05-26 00:37:16 +00:00
|
|
|
if err := restore.Coordinates(6, dump); err != nil {
|
2017-01-13 19:47:16 +00:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
restore.Commit()
|
|
|
|
|
|
|
|
// Read the restored coordinates back out and verify that they match.
|
2017-01-24 16:13:48 +00:00
|
|
|
idx, res, err := s.Coordinates(nil)
|
2017-01-13 19:47:16 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
2017-05-26 00:37:16 +00:00
|
|
|
if idx != 6 {
|
2017-01-13 19:47:16 +00:00
|
|
|
t.Fatalf("bad index: %d", idx)
|
|
|
|
}
|
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).
|
2017-05-26 00:37:16 +00:00
|
|
|
if idx := s.maxIndex("coordinates"); idx != 6 {
|
2017-01-13 19:47:16 +00:00
|
|
|
t.Fatalf("bad index: %d", idx)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
}
|