2016-03-28 20:28:57 -04:00
|
|
|
package networkdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2017-12-12 11:23:49 -05:00
|
|
|
"net"
|
2016-03-28 20:28:57 -04:00
|
|
|
"os"
|
2021-06-01 17:42:15 -04:00
|
|
|
"strconv"
|
2016-03-28 20:28:57 -04:00
|
|
|
"sync/atomic"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2017-09-26 13:29:33 -04:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2016-03-28 20:28:57 -04:00
|
|
|
"github.com/docker/go-events"
|
2017-12-12 11:23:49 -05:00
|
|
|
"github.com/hashicorp/memberlist"
|
2017-07-26 17:18:31 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2020-08-31 11:55:22 -04:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
|
|
|
"gotest.tools/v3/poll"
|
2016-03-28 20:28:57 -04:00
|
|
|
)
|
|
|
|
|
2018-05-18 17:10:14 -04:00
|
|
|
var dbPort int32 = 10000
|
|
|
|
|
2016-03-28 20:28:57 -04:00
|
|
|
func TestMain(m *testing.M) {
|
2021-08-24 06:10:50 -04:00
|
|
|
os.WriteFile("/proc/sys/net/ipv6/conf/lo/disable_ipv6", []byte{'0', '\n'}, 0644)
|
2016-03-28 20:28:57 -04:00
|
|
|
logrus.SetLevel(logrus.ErrorLevel)
|
|
|
|
os.Exit(m.Run())
|
|
|
|
}
|
|
|
|
|
2018-06-21 20:57:35 -04:00
|
|
|
func launchNode(t *testing.T, conf Config) *NetworkDB {
|
2020-02-26 16:27:08 -05:00
|
|
|
t.Helper()
|
2018-06-21 20:57:35 -04:00
|
|
|
db, err := New(&conf)
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.NilError(t, err)
|
2018-06-21 20:57:35 -04:00
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
2017-09-26 13:29:33 -04:00
|
|
|
func createNetworkDBInstances(t *testing.T, num int, namePrefix string, conf *Config) []*NetworkDB {
|
2020-02-26 16:27:08 -05:00
|
|
|
t.Helper()
|
2016-03-28 20:28:57 -04:00
|
|
|
var dbs []*NetworkDB
|
|
|
|
for i := 0; i < num; i++ {
|
2017-09-26 13:29:33 -04:00
|
|
|
localConfig := *conf
|
|
|
|
localConfig.Hostname = fmt.Sprintf("%s%d", namePrefix, i+1)
|
|
|
|
localConfig.NodeID = stringid.TruncateID(stringid.GenerateRandomID())
|
|
|
|
localConfig.BindPort = int(atomic.AddInt32(&dbPort, 1))
|
2018-06-21 20:57:35 -04:00
|
|
|
db := launchNode(t, localConfig)
|
2016-03-28 20:28:57 -04:00
|
|
|
if i != 0 {
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.Check(t, db.Join([]string{fmt.Sprintf("localhost:%d", db.config.BindPort-1)}))
|
2016-03-28 20:28:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
dbs = append(dbs, db)
|
|
|
|
}
|
|
|
|
|
2019-10-01 19:54:01 -04:00
|
|
|
// Wait till the cluster creation is successful
|
|
|
|
check := func(t poll.LogT) poll.Result {
|
|
|
|
// Check that the cluster is properly created
|
|
|
|
for i := 0; i < num; i++ {
|
|
|
|
if num != len(dbs[i].ClusterPeers()) {
|
2022-06-27 09:12:14 -04:00
|
|
|
return poll.Continue("%s:Waiting for cluster peers to be established", dbs[i].config.Hostname)
|
2019-10-01 19:54:01 -04:00
|
|
|
}
|
2017-09-26 13:29:33 -04:00
|
|
|
}
|
2019-10-01 19:54:01 -04:00
|
|
|
return poll.Success()
|
2017-09-26 13:29:33 -04:00
|
|
|
}
|
2019-10-01 19:54:01 -04:00
|
|
|
poll.WaitOn(t, check, poll.WithDelay(2*time.Second), poll.WithTimeout(20*time.Second))
|
2017-09-26 13:29:33 -04:00
|
|
|
|
2016-03-28 20:28:57 -04:00
|
|
|
return dbs
|
|
|
|
}
|
|
|
|
|
2020-02-26 16:27:08 -05:00
|
|
|
func closeNetworkDBInstances(t *testing.T, dbs []*NetworkDB) {
|
|
|
|
t.Helper()
|
2017-09-26 13:29:33 -04:00
|
|
|
log.Print("Closing DB instances...")
|
2016-03-28 20:28:57 -04:00
|
|
|
for _, db := range dbs {
|
|
|
|
db.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *NetworkDB) verifyNodeExistence(t *testing.T, node string, present bool) {
|
2020-02-26 16:27:08 -05:00
|
|
|
t.Helper()
|
2016-03-28 20:28:57 -04:00
|
|
|
for i := 0; i < 80; i++ {
|
Fix panic in networkdb test code
fatal error: concurrent map read and map write
goroutine 264 [running]:
runtime.throw(0x90043c, 0x21)
/usr/local/go/src/runtime/panic.go:566 +0x95 fp=0xc4203d1d68 sp=0xc4203d1d48
runtime.mapaccess2_faststr(0x86df20, 0xc4203f5470, 0xc42044afc0, 0x5, 0xc4203d1e40, 0x4ed6b8)
/usr/local/go/src/runtime/hashmap_fast.go:306 +0x52b fp=0xc4203d1dc8 sp=0xc4203d1d68
github.com/docker/libnetwork/networkdb.(*NetworkDB).verifyNodeExistence(0xc42007e160, 0xc42008a240, 0xc42044afc0, 0x5, 0x1)
/go/src/github.com/docker/libnetwork/networkdb/networkdb_test.go:58 +0x6c fp=0xc4203d1e50 sp=0xc4203d1dc8
Signed-off-by: Alessandro Boch <aboch@docker.com>
2017-03-16 02:24:31 -04:00
|
|
|
db.RLock()
|
2016-03-28 20:28:57 -04:00
|
|
|
_, ok := db.nodes[node]
|
Fix panic in networkdb test code
fatal error: concurrent map read and map write
goroutine 264 [running]:
runtime.throw(0x90043c, 0x21)
/usr/local/go/src/runtime/panic.go:566 +0x95 fp=0xc4203d1d68 sp=0xc4203d1d48
runtime.mapaccess2_faststr(0x86df20, 0xc4203f5470, 0xc42044afc0, 0x5, 0xc4203d1e40, 0x4ed6b8)
/usr/local/go/src/runtime/hashmap_fast.go:306 +0x52b fp=0xc4203d1dc8 sp=0xc4203d1d68
github.com/docker/libnetwork/networkdb.(*NetworkDB).verifyNodeExistence(0xc42007e160, 0xc42008a240, 0xc42044afc0, 0x5, 0x1)
/go/src/github.com/docker/libnetwork/networkdb/networkdb_test.go:58 +0x6c fp=0xc4203d1e50 sp=0xc4203d1dc8
Signed-off-by: Alessandro Boch <aboch@docker.com>
2017-03-16 02:24:31 -04:00
|
|
|
db.RUnlock()
|
2016-03-28 20:28:57 -04:00
|
|
|
if present && ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !present && !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
2018-09-08 09:05:23 -04:00
|
|
|
t.Errorf("%v(%v): Node existence verification for node %s failed", db.config.Hostname, db.config.NodeID, node)
|
2016-03-28 20:28:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (db *NetworkDB) verifyNetworkExistence(t *testing.T, node string, id string, present bool) {
|
2020-02-26 16:27:08 -05:00
|
|
|
t.Helper()
|
2021-08-01 11:04:01 -04:00
|
|
|
|
|
|
|
const sleepInterval = 50 * time.Millisecond
|
|
|
|
var maxRetries int64
|
|
|
|
if dl, ok := t.Deadline(); ok {
|
|
|
|
maxRetries = int64(time.Until(dl) / sleepInterval)
|
|
|
|
} else {
|
|
|
|
maxRetries = 80
|
|
|
|
}
|
|
|
|
for i := int64(0); i < maxRetries; i++ {
|
Fix panic in networkdb test code
fatal error: concurrent map read and map write
goroutine 264 [running]:
runtime.throw(0x90043c, 0x21)
/usr/local/go/src/runtime/panic.go:566 +0x95 fp=0xc4203d1d68 sp=0xc4203d1d48
runtime.mapaccess2_faststr(0x86df20, 0xc4203f5470, 0xc42044afc0, 0x5, 0xc4203d1e40, 0x4ed6b8)
/usr/local/go/src/runtime/hashmap_fast.go:306 +0x52b fp=0xc4203d1dc8 sp=0xc4203d1d68
github.com/docker/libnetwork/networkdb.(*NetworkDB).verifyNodeExistence(0xc42007e160, 0xc42008a240, 0xc42044afc0, 0x5, 0x1)
/go/src/github.com/docker/libnetwork/networkdb/networkdb_test.go:58 +0x6c fp=0xc4203d1e50 sp=0xc4203d1dc8
Signed-off-by: Alessandro Boch <aboch@docker.com>
2017-03-16 02:24:31 -04:00
|
|
|
db.RLock()
|
|
|
|
nn, nnok := db.networks[node]
|
|
|
|
db.RUnlock()
|
|
|
|
if nnok {
|
2016-03-28 20:28:57 -04:00
|
|
|
n, ok := nn[id]
|
|
|
|
if present && ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !present &&
|
|
|
|
((ok && n.leaving) ||
|
|
|
|
!ok) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-01 11:04:01 -04:00
|
|
|
time.Sleep(sleepInterval)
|
2016-03-28 20:28:57 -04:00
|
|
|
}
|
|
|
|
|
2018-07-04 05:01:52 -04:00
|
|
|
t.Error("Network existence verification failed")
|
2016-03-28 20:28:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (db *NetworkDB) verifyEntryExistence(t *testing.T, tname, nid, key, value string, present bool) {
|
2020-02-26 16:27:08 -05:00
|
|
|
t.Helper()
|
2016-03-28 20:28:57 -04:00
|
|
|
n := 80
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
entry, err := db.getEntry(tname, nid, key)
|
|
|
|
if present && err == nil && string(entry.value) == value {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !present &&
|
|
|
|
((err == nil && entry.deleting) ||
|
|
|
|
(err != nil)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if i == n-1 && !present && err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
2018-09-08 09:05:23 -04:00
|
|
|
t.Errorf("Entry existence verification test failed for %v(%v)", db.config.Hostname, db.config.NodeID)
|
2016-03-28 20:28:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func testWatch(t *testing.T, ch chan events.Event, ev interface{}, tname, nid, key, value string) {
|
2020-02-26 16:27:08 -05:00
|
|
|
t.Helper()
|
2016-03-28 20:28:57 -04:00
|
|
|
select {
|
|
|
|
case rcvdEv := <-ch:
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.Check(t, is.Equal(fmt.Sprintf("%T", rcvdEv), fmt.Sprintf("%T", ev)))
|
2021-05-27 20:15:56 -04:00
|
|
|
switch typ := rcvdEv.(type) {
|
2016-03-28 20:28:57 -04:00
|
|
|
case CreateEvent:
|
2021-05-27 20:15:56 -04:00
|
|
|
assert.Check(t, is.Equal(tname, typ.Table))
|
|
|
|
assert.Check(t, is.Equal(nid, typ.NetworkID))
|
|
|
|
assert.Check(t, is.Equal(key, typ.Key))
|
|
|
|
assert.Check(t, is.Equal(value, string(typ.Value)))
|
2016-03-28 20:28:57 -04:00
|
|
|
case UpdateEvent:
|
2021-05-27 20:15:56 -04:00
|
|
|
assert.Check(t, is.Equal(tname, typ.Table))
|
|
|
|
assert.Check(t, is.Equal(nid, typ.NetworkID))
|
|
|
|
assert.Check(t, is.Equal(key, typ.Key))
|
|
|
|
assert.Check(t, is.Equal(value, string(typ.Value)))
|
2016-03-28 20:28:57 -04:00
|
|
|
case DeleteEvent:
|
2021-05-27 20:15:56 -04:00
|
|
|
assert.Check(t, is.Equal(tname, typ.Table))
|
|
|
|
assert.Check(t, is.Equal(nid, typ.NetworkID))
|
|
|
|
assert.Check(t, is.Equal(key, typ.Key))
|
2016-03-28 20:28:57 -04:00
|
|
|
}
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
t.Fail()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNetworkDBSimple(t *testing.T) {
|
2017-09-26 13:29:33 -04:00
|
|
|
dbs := createNetworkDBInstances(t, 2, "node", DefaultConfig())
|
2020-02-26 16:27:08 -05:00
|
|
|
closeNetworkDBInstances(t, dbs)
|
2016-03-28 20:28:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNetworkDBJoinLeaveNetwork(t *testing.T) {
|
2017-09-26 13:29:33 -04:00
|
|
|
dbs := createNetworkDBInstances(t, 2, "node", DefaultConfig())
|
2016-03-28 20:28:57 -04:00
|
|
|
|
|
|
|
err := dbs[0].JoinNetwork("network1")
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.NilError(t, err)
|
2016-03-28 20:28:57 -04:00
|
|
|
|
2017-09-11 14:35:18 -04:00
|
|
|
dbs[1].verifyNetworkExistence(t, dbs[0].config.NodeID, "network1", true)
|
2016-03-28 20:28:57 -04:00
|
|
|
|
|
|
|
err = dbs[0].LeaveNetwork("network1")
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.NilError(t, err)
|
2016-03-28 20:28:57 -04:00
|
|
|
|
2017-09-11 14:35:18 -04:00
|
|
|
dbs[1].verifyNetworkExistence(t, dbs[0].config.NodeID, "network1", false)
|
2020-02-26 16:27:08 -05:00
|
|
|
closeNetworkDBInstances(t, dbs)
|
2016-03-28 20:28:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNetworkDBJoinLeaveNetworks(t *testing.T) {
|
2017-09-26 13:29:33 -04:00
|
|
|
dbs := createNetworkDBInstances(t, 2, "node", DefaultConfig())
|
2016-03-28 20:28:57 -04:00
|
|
|
|
|
|
|
n := 10
|
|
|
|
for i := 1; i <= n; i++ {
|
|
|
|
err := dbs[0].JoinNetwork(fmt.Sprintf("network0%d", i))
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.NilError(t, err)
|
2016-03-28 20:28:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := 1; i <= n; i++ {
|
|
|
|
err := dbs[1].JoinNetwork(fmt.Sprintf("network1%d", i))
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.NilError(t, err)
|
2016-03-28 20:28:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := 1; i <= n; i++ {
|
2017-09-11 14:35:18 -04:00
|
|
|
dbs[1].verifyNetworkExistence(t, dbs[0].config.NodeID, fmt.Sprintf("network0%d", i), true)
|
2016-03-28 20:28:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := 1; i <= n; i++ {
|
2017-09-11 14:35:18 -04:00
|
|
|
dbs[0].verifyNetworkExistence(t, dbs[1].config.NodeID, fmt.Sprintf("network1%d", i), true)
|
2016-03-28 20:28:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := 1; i <= n; i++ {
|
|
|
|
err := dbs[0].LeaveNetwork(fmt.Sprintf("network0%d", i))
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.NilError(t, err)
|
2016-03-28 20:28:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := 1; i <= n; i++ {
|
|
|
|
err := dbs[1].LeaveNetwork(fmt.Sprintf("network1%d", i))
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.NilError(t, err)
|
2016-03-28 20:28:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := 1; i <= n; i++ {
|
2017-09-11 14:35:18 -04:00
|
|
|
dbs[1].verifyNetworkExistence(t, dbs[0].config.NodeID, fmt.Sprintf("network0%d", i), false)
|
2016-03-28 20:28:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := 1; i <= n; i++ {
|
2017-09-11 14:35:18 -04:00
|
|
|
dbs[0].verifyNetworkExistence(t, dbs[1].config.NodeID, fmt.Sprintf("network1%d", i), false)
|
2016-03-28 20:28:57 -04:00
|
|
|
}
|
|
|
|
|
2020-02-26 16:27:08 -05:00
|
|
|
closeNetworkDBInstances(t, dbs)
|
2016-03-28 20:28:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNetworkDBCRUDTableEntry(t *testing.T) {
|
2017-09-26 13:29:33 -04:00
|
|
|
dbs := createNetworkDBInstances(t, 3, "node", DefaultConfig())
|
2016-03-28 20:28:57 -04:00
|
|
|
|
|
|
|
err := dbs[0].JoinNetwork("network1")
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.NilError(t, err)
|
2016-03-28 20:28:57 -04:00
|
|
|
|
2017-09-11 14:35:18 -04:00
|
|
|
dbs[1].verifyNetworkExistence(t, dbs[0].config.NodeID, "network1", true)
|
2016-03-28 20:28:57 -04:00
|
|
|
|
|
|
|
err = dbs[1].JoinNetwork("network1")
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.NilError(t, err)
|
2016-03-28 20:28:57 -04:00
|
|
|
|
|
|
|
err = dbs[0].CreateEntry("test_table", "network1", "test_key", []byte("test_value"))
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.NilError(t, err)
|
2016-03-28 20:28:57 -04:00
|
|
|
|
|
|
|
dbs[1].verifyEntryExistence(t, "test_table", "network1", "test_key", "test_value", true)
|
|
|
|
dbs[2].verifyEntryExistence(t, "test_table", "network1", "test_key", "test_value", false)
|
|
|
|
|
|
|
|
err = dbs[0].UpdateEntry("test_table", "network1", "test_key", []byte("test_updated_value"))
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.NilError(t, err)
|
2016-03-28 20:28:57 -04:00
|
|
|
|
|
|
|
dbs[1].verifyEntryExistence(t, "test_table", "network1", "test_key", "test_updated_value", true)
|
|
|
|
|
|
|
|
err = dbs[0].DeleteEntry("test_table", "network1", "test_key")
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.NilError(t, err)
|
2016-03-28 20:28:57 -04:00
|
|
|
|
|
|
|
dbs[1].verifyEntryExistence(t, "test_table", "network1", "test_key", "", false)
|
|
|
|
|
2020-02-26 16:27:08 -05:00
|
|
|
closeNetworkDBInstances(t, dbs)
|
2016-03-28 20:28:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNetworkDBCRUDTableEntries(t *testing.T) {
|
2017-09-26 13:29:33 -04:00
|
|
|
dbs := createNetworkDBInstances(t, 2, "node", DefaultConfig())
|
2016-03-28 20:28:57 -04:00
|
|
|
|
|
|
|
err := dbs[0].JoinNetwork("network1")
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.NilError(t, err)
|
2016-03-28 20:28:57 -04:00
|
|
|
|
2017-09-11 14:35:18 -04:00
|
|
|
dbs[1].verifyNetworkExistence(t, dbs[0].config.NodeID, "network1", true)
|
2016-03-28 20:28:57 -04:00
|
|
|
|
|
|
|
err = dbs[1].JoinNetwork("network1")
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.NilError(t, err)
|
2016-03-28 20:28:57 -04:00
|
|
|
|
2021-07-12 12:48:04 -04:00
|
|
|
dbs[0].verifyNetworkExistence(t, dbs[1].config.NodeID, "network1", true)
|
|
|
|
|
2016-03-28 20:28:57 -04:00
|
|
|
n := 10
|
|
|
|
for i := 1; i <= n; i++ {
|
|
|
|
err = dbs[0].CreateEntry("test_table", "network1",
|
|
|
|
fmt.Sprintf("test_key0%d", i),
|
|
|
|
[]byte(fmt.Sprintf("test_value0%d", i)))
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.NilError(t, err)
|
2016-03-28 20:28:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := 1; i <= n; i++ {
|
|
|
|
err = dbs[1].CreateEntry("test_table", "network1",
|
|
|
|
fmt.Sprintf("test_key1%d", i),
|
|
|
|
[]byte(fmt.Sprintf("test_value1%d", i)))
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.NilError(t, err)
|
2016-03-28 20:28:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := 1; i <= n; i++ {
|
|
|
|
dbs[0].verifyEntryExistence(t, "test_table", "network1",
|
|
|
|
fmt.Sprintf("test_key1%d", i),
|
|
|
|
fmt.Sprintf("test_value1%d", i), true)
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.NilError(t, err)
|
2016-03-28 20:28:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := 1; i <= n; i++ {
|
|
|
|
dbs[1].verifyEntryExistence(t, "test_table", "network1",
|
|
|
|
fmt.Sprintf("test_key0%d", i),
|
|
|
|
fmt.Sprintf("test_value0%d", i), true)
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.NilError(t, err)
|
2016-03-28 20:28:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Verify deletes
|
|
|
|
for i := 1; i <= n; i++ {
|
|
|
|
err = dbs[0].DeleteEntry("test_table", "network1",
|
|
|
|
fmt.Sprintf("test_key0%d", i))
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.NilError(t, err)
|
2016-03-28 20:28:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := 1; i <= n; i++ {
|
|
|
|
err = dbs[1].DeleteEntry("test_table", "network1",
|
|
|
|
fmt.Sprintf("test_key1%d", i))
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.NilError(t, err)
|
2016-03-28 20:28:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := 1; i <= n; i++ {
|
|
|
|
dbs[0].verifyEntryExistence(t, "test_table", "network1",
|
|
|
|
fmt.Sprintf("test_key1%d", i), "", false)
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.NilError(t, err)
|
2016-03-28 20:28:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := 1; i <= n; i++ {
|
|
|
|
dbs[1].verifyEntryExistence(t, "test_table", "network1",
|
|
|
|
fmt.Sprintf("test_key0%d", i), "", false)
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.NilError(t, err)
|
2016-03-28 20:28:57 -04:00
|
|
|
}
|
|
|
|
|
2020-02-26 16:27:08 -05:00
|
|
|
closeNetworkDBInstances(t, dbs)
|
2016-03-28 20:28:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNetworkDBNodeLeave(t *testing.T) {
|
2017-09-26 13:29:33 -04:00
|
|
|
dbs := createNetworkDBInstances(t, 2, "node", DefaultConfig())
|
2016-03-28 20:28:57 -04:00
|
|
|
|
|
|
|
err := dbs[0].JoinNetwork("network1")
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.NilError(t, err)
|
2016-03-28 20:28:57 -04:00
|
|
|
|
|
|
|
err = dbs[1].JoinNetwork("network1")
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.NilError(t, err)
|
2016-03-28 20:28:57 -04:00
|
|
|
|
|
|
|
err = dbs[0].CreateEntry("test_table", "network1", "test_key", []byte("test_value"))
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.NilError(t, err)
|
2016-03-28 20:28:57 -04:00
|
|
|
|
|
|
|
dbs[1].verifyEntryExistence(t, "test_table", "network1", "test_key", "test_value", true)
|
|
|
|
|
|
|
|
dbs[0].Close()
|
|
|
|
dbs[1].verifyEntryExistence(t, "test_table", "network1", "test_key", "test_value", false)
|
|
|
|
dbs[1].Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNetworkDBWatch(t *testing.T) {
|
2017-09-26 13:29:33 -04:00
|
|
|
dbs := createNetworkDBInstances(t, 2, "node", DefaultConfig())
|
2016-03-28 20:28:57 -04:00
|
|
|
err := dbs[0].JoinNetwork("network1")
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.NilError(t, err)
|
2016-03-28 20:28:57 -04:00
|
|
|
|
|
|
|
err = dbs[1].JoinNetwork("network1")
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.NilError(t, err)
|
2016-03-28 20:28:57 -04:00
|
|
|
|
|
|
|
ch, cancel := dbs[1].Watch("", "", "")
|
|
|
|
|
|
|
|
err = dbs[0].CreateEntry("test_table", "network1", "test_key", []byte("test_value"))
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.NilError(t, err)
|
2016-03-28 20:28:57 -04:00
|
|
|
|
2017-05-31 12:49:55 -04:00
|
|
|
testWatch(t, ch.C, CreateEvent{}, "test_table", "network1", "test_key", "test_value")
|
2016-03-28 20:28:57 -04:00
|
|
|
|
|
|
|
err = dbs[0].UpdateEntry("test_table", "network1", "test_key", []byte("test_updated_value"))
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.NilError(t, err)
|
2016-03-28 20:28:57 -04:00
|
|
|
|
2017-05-31 12:49:55 -04:00
|
|
|
testWatch(t, ch.C, UpdateEvent{}, "test_table", "network1", "test_key", "test_updated_value")
|
2016-03-28 20:28:57 -04:00
|
|
|
|
|
|
|
err = dbs[0].DeleteEntry("test_table", "network1", "test_key")
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.NilError(t, err)
|
2016-03-28 20:28:57 -04:00
|
|
|
|
2017-05-31 12:49:55 -04:00
|
|
|
testWatch(t, ch.C, DeleteEvent{}, "test_table", "network1", "test_key", "")
|
2016-03-28 20:28:57 -04:00
|
|
|
|
|
|
|
cancel()
|
2020-02-26 16:27:08 -05:00
|
|
|
closeNetworkDBInstances(t, dbs)
|
2016-03-28 20:28:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNetworkDBBulkSync(t *testing.T) {
|
2017-09-26 13:29:33 -04:00
|
|
|
dbs := createNetworkDBInstances(t, 2, "node", DefaultConfig())
|
2016-03-28 20:28:57 -04:00
|
|
|
|
|
|
|
err := dbs[0].JoinNetwork("network1")
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.NilError(t, err)
|
2016-03-28 20:28:57 -04:00
|
|
|
|
2017-09-11 14:35:18 -04:00
|
|
|
dbs[1].verifyNetworkExistence(t, dbs[0].config.NodeID, "network1", true)
|
2016-03-28 20:28:57 -04:00
|
|
|
|
|
|
|
n := 1000
|
|
|
|
for i := 1; i <= n; i++ {
|
|
|
|
err = dbs[0].CreateEntry("test_table", "network1",
|
|
|
|
fmt.Sprintf("test_key0%d", i),
|
|
|
|
[]byte(fmt.Sprintf("test_value0%d", i)))
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.NilError(t, err)
|
2016-03-28 20:28:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
err = dbs[1].JoinNetwork("network1")
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.NilError(t, err)
|
2016-03-28 20:28:57 -04:00
|
|
|
|
2017-09-11 14:35:18 -04:00
|
|
|
dbs[0].verifyNetworkExistence(t, dbs[1].config.NodeID, "network1", true)
|
2016-03-28 20:28:57 -04:00
|
|
|
|
|
|
|
for i := 1; i <= n; i++ {
|
|
|
|
dbs[1].verifyEntryExistence(t, "test_table", "network1",
|
|
|
|
fmt.Sprintf("test_key0%d", i),
|
|
|
|
fmt.Sprintf("test_value0%d", i), true)
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.NilError(t, err)
|
2016-03-28 20:28:57 -04:00
|
|
|
}
|
|
|
|
|
2020-02-26 16:27:08 -05:00
|
|
|
closeNetworkDBInstances(t, dbs)
|
2016-03-28 20:28:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNetworkDBCRUDMediumCluster(t *testing.T) {
|
|
|
|
n := 5
|
|
|
|
|
2017-09-26 13:29:33 -04:00
|
|
|
dbs := createNetworkDBInstances(t, n, "node", DefaultConfig())
|
2016-03-28 20:28:57 -04:00
|
|
|
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
for j := 0; j < n; j++ {
|
|
|
|
if i == j {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-09-11 14:35:18 -04:00
|
|
|
dbs[i].verifyNodeExistence(t, dbs[j].config.NodeID, true)
|
2016-03-28 20:28:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
err := dbs[i].JoinNetwork("network1")
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.NilError(t, err)
|
2016-03-28 20:28:57 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
for j := 0; j < n; j++ {
|
2017-09-11 14:35:18 -04:00
|
|
|
dbs[i].verifyNetworkExistence(t, dbs[j].config.NodeID, "network1", true)
|
2016-03-28 20:28:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err := dbs[0].CreateEntry("test_table", "network1", "test_key", []byte("test_value"))
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.NilError(t, err)
|
2016-03-28 20:28:57 -04:00
|
|
|
|
|
|
|
for i := 1; i < n; i++ {
|
|
|
|
dbs[i].verifyEntryExistence(t, "test_table", "network1", "test_key", "test_value", true)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = dbs[0].UpdateEntry("test_table", "network1", "test_key", []byte("test_updated_value"))
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.NilError(t, err)
|
2016-03-28 20:28:57 -04:00
|
|
|
|
|
|
|
for i := 1; i < n; i++ {
|
|
|
|
dbs[i].verifyEntryExistence(t, "test_table", "network1", "test_key", "test_updated_value", true)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = dbs[0].DeleteEntry("test_table", "network1", "test_key")
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.NilError(t, err)
|
2016-03-28 20:28:57 -04:00
|
|
|
|
|
|
|
for i := 1; i < n; i++ {
|
|
|
|
dbs[i].verifyEntryExistence(t, "test_table", "network1", "test_key", "", false)
|
|
|
|
}
|
|
|
|
|
2017-12-06 14:21:51 -05:00
|
|
|
for i := 1; i < n; i++ {
|
|
|
|
_, err = dbs[i].GetEntry("test_table", "network1", "test_key")
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.Check(t, is.ErrorContains(err, ""))
|
2021-05-28 16:59:41 -04:00
|
|
|
assert.Check(t, is.Contains(err.Error(), "deleted and pending garbage collection"), err)
|
2017-12-06 14:21:51 -05:00
|
|
|
}
|
|
|
|
|
2020-02-26 16:27:08 -05:00
|
|
|
closeNetworkDBInstances(t, dbs)
|
2016-03-28 20:28:57 -04:00
|
|
|
}
|
2017-07-05 14:02:04 -04:00
|
|
|
|
|
|
|
func TestNetworkDBNodeJoinLeaveIteration(t *testing.T) {
|
2017-09-26 13:29:33 -04:00
|
|
|
dbs := createNetworkDBInstances(t, 2, "node", DefaultConfig())
|
2017-07-05 14:02:04 -04:00
|
|
|
|
2022-07-22 11:28:45 -04:00
|
|
|
dbChangeWitness := func(db *NetworkDB) func(network string, expectNodeCount int) {
|
|
|
|
staleNetworkTime := db.networkClock.Time()
|
|
|
|
return func(network string, expectNodeCount int) {
|
|
|
|
check := func(t poll.LogT) poll.Result {
|
|
|
|
networkTime := db.networkClock.Time()
|
|
|
|
if networkTime <= staleNetworkTime {
|
|
|
|
return poll.Continue("network time is stale, no change registered yet.")
|
|
|
|
}
|
|
|
|
count := -1
|
|
|
|
db.Lock()
|
|
|
|
if nodes, ok := db.networkNodes[network]; ok {
|
|
|
|
count = len(nodes)
|
|
|
|
}
|
|
|
|
db.Unlock()
|
|
|
|
if count != expectNodeCount {
|
|
|
|
return poll.Continue("current number of nodes is %d, expect %d.", count, expectNodeCount)
|
|
|
|
}
|
|
|
|
return poll.Success()
|
|
|
|
}
|
|
|
|
t.Helper()
|
|
|
|
poll.WaitOn(t, check, poll.WithTimeout(3*time.Second), poll.WithDelay(5*time.Millisecond))
|
2022-07-21 10:14:07 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-05 14:02:04 -04:00
|
|
|
// Single node Join/Leave
|
2022-07-22 11:28:45 -04:00
|
|
|
witness0 := dbChangeWitness(dbs[0])
|
2017-07-05 14:02:04 -04:00
|
|
|
err := dbs[0].JoinNetwork("network1")
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.NilError(t, err)
|
2022-07-22 11:28:45 -04:00
|
|
|
witness0("network1", 1)
|
2017-07-05 14:02:04 -04:00
|
|
|
|
2022-07-22 11:28:45 -04:00
|
|
|
witness0 = dbChangeWitness(dbs[0])
|
2017-07-05 14:02:04 -04:00
|
|
|
err = dbs[0].LeaveNetwork("network1")
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.NilError(t, err)
|
2022-07-22 11:28:45 -04:00
|
|
|
witness0("network1", 0)
|
2017-07-05 14:02:04 -04:00
|
|
|
|
|
|
|
// Multiple nodes Join/Leave
|
2022-07-22 11:28:45 -04:00
|
|
|
witness0, witness1 := dbChangeWitness(dbs[0]), dbChangeWitness(dbs[1])
|
2017-07-05 14:02:04 -04:00
|
|
|
err = dbs[0].JoinNetwork("network1")
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.NilError(t, err)
|
2017-07-05 14:02:04 -04:00
|
|
|
|
|
|
|
err = dbs[1].JoinNetwork("network1")
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.NilError(t, err)
|
2017-07-05 14:02:04 -04:00
|
|
|
|
|
|
|
// Wait for the propagation on db[0]
|
2021-07-12 12:48:04 -04:00
|
|
|
dbs[0].verifyNetworkExistence(t, dbs[1].config.NodeID, "network1", true)
|
2022-07-22 11:28:45 -04:00
|
|
|
witness0("network1", 2)
|
2017-09-11 14:35:18 -04:00
|
|
|
if n, ok := dbs[0].networks[dbs[0].config.NodeID]["network1"]; !ok || n.leaving {
|
2017-09-19 20:36:00 -04:00
|
|
|
t.Fatalf("The network should not be marked as leaving:%t", n.leaving)
|
|
|
|
}
|
2017-07-05 14:02:04 -04:00
|
|
|
|
|
|
|
// Wait for the propagation on db[1]
|
2021-07-12 12:48:04 -04:00
|
|
|
dbs[1].verifyNetworkExistence(t, dbs[0].config.NodeID, "network1", true)
|
2022-07-22 11:28:45 -04:00
|
|
|
witness1("network1", 2)
|
2017-09-11 14:35:18 -04:00
|
|
|
if n, ok := dbs[1].networks[dbs[1].config.NodeID]["network1"]; !ok || n.leaving {
|
2017-09-19 20:36:00 -04:00
|
|
|
t.Fatalf("The network should not be marked as leaving:%t", n.leaving)
|
|
|
|
}
|
2017-07-05 14:02:04 -04:00
|
|
|
|
|
|
|
// Try a quick leave/join
|
2022-07-22 11:28:45 -04:00
|
|
|
witness0, witness1 = dbChangeWitness(dbs[0]), dbChangeWitness(dbs[1])
|
2017-07-05 14:02:04 -04:00
|
|
|
err = dbs[0].LeaveNetwork("network1")
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.NilError(t, err)
|
2017-07-05 14:02:04 -04:00
|
|
|
err = dbs[0].JoinNetwork("network1")
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.NilError(t, err)
|
2017-07-05 14:02:04 -04:00
|
|
|
|
2021-08-01 11:04:48 -04:00
|
|
|
dbs[0].verifyNetworkExistence(t, dbs[1].config.NodeID, "network1", true)
|
2022-07-22 11:28:45 -04:00
|
|
|
witness0("network1", 2)
|
2017-07-05 14:02:04 -04:00
|
|
|
|
2021-08-01 11:04:48 -04:00
|
|
|
dbs[1].verifyNetworkExistence(t, dbs[0].config.NodeID, "network1", true)
|
2022-07-22 11:28:45 -04:00
|
|
|
witness1("network1", 2)
|
2017-07-05 14:02:04 -04:00
|
|
|
|
2020-02-26 16:27:08 -05:00
|
|
|
closeNetworkDBInstances(t, dbs)
|
2017-09-26 13:29:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNetworkDBGarbageCollection(t *testing.T) {
|
|
|
|
keysWriteDelete := 5
|
|
|
|
config := DefaultConfig()
|
|
|
|
config.reapEntryInterval = 30 * time.Second
|
|
|
|
config.StatsPrintPeriod = 15 * time.Second
|
|
|
|
|
|
|
|
dbs := createNetworkDBInstances(t, 3, "node", config)
|
|
|
|
|
|
|
|
// 2 Nodes join network
|
|
|
|
err := dbs[0].JoinNetwork("network1")
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.NilError(t, err)
|
2017-09-26 13:29:33 -04:00
|
|
|
|
|
|
|
err = dbs[1].JoinNetwork("network1")
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.NilError(t, err)
|
2017-09-26 13:29:33 -04:00
|
|
|
|
|
|
|
for i := 0; i < keysWriteDelete; i++ {
|
2021-06-01 17:42:15 -04:00
|
|
|
err = dbs[i%2].CreateEntry("testTable", "network1", "key-"+strconv.Itoa(i), []byte("value"))
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.NilError(t, err)
|
2017-09-26 13:29:33 -04:00
|
|
|
}
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
for i := 0; i < keysWriteDelete; i++ {
|
2021-06-01 17:42:15 -04:00
|
|
|
err = dbs[i%2].DeleteEntry("testTable", "network1", "key-"+strconv.Itoa(i))
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.NilError(t, err)
|
2017-09-26 13:29:33 -04:00
|
|
|
}
|
|
|
|
for i := 0; i < 2; i++ {
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.Check(t, is.Equal(keysWriteDelete, dbs[i].networks[dbs[i].config.NodeID]["network1"].entriesNumber), "entries number should match")
|
2017-09-26 13:29:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// from this point the timer for the garbage collection started, wait 5 seconds and then join a new node
|
|
|
|
time.Sleep(5 * time.Second)
|
|
|
|
|
|
|
|
err = dbs[2].JoinNetwork("network1")
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.NilError(t, err)
|
2017-09-26 13:29:33 -04:00
|
|
|
for i := 0; i < 3; i++ {
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.Check(t, is.Equal(keysWriteDelete, dbs[i].networks[dbs[i].config.NodeID]["network1"].entriesNumber), "entries number should match")
|
2017-09-26 13:29:33 -04:00
|
|
|
}
|
|
|
|
// at this point the entries should had been all deleted
|
|
|
|
time.Sleep(30 * time.Second)
|
|
|
|
for i := 0; i < 3; i++ {
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.Check(t, is.Equal(0, dbs[i].networks[dbs[i].config.NodeID]["network1"].entriesNumber), "entries should had been garbage collected")
|
2017-09-26 13:29:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// make sure that entries are not coming back
|
|
|
|
time.Sleep(15 * time.Second)
|
|
|
|
for i := 0; i < 3; i++ {
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.Check(t, is.Equal(0, dbs[i].networks[dbs[i].config.NodeID]["network1"].entriesNumber), "entries should had been garbage collected")
|
2017-09-26 13:29:33 -04:00
|
|
|
}
|
|
|
|
|
2020-02-26 16:27:08 -05:00
|
|
|
closeNetworkDBInstances(t, dbs)
|
2017-07-05 14:02:04 -04:00
|
|
|
}
|
2017-12-12 11:23:49 -05:00
|
|
|
|
|
|
|
func TestFindNode(t *testing.T) {
|
|
|
|
dbs := createNetworkDBInstances(t, 1, "node", DefaultConfig())
|
|
|
|
|
|
|
|
dbs[0].nodes["active"] = &node{Node: memberlist.Node{Name: "active"}}
|
|
|
|
dbs[0].failedNodes["failed"] = &node{Node: memberlist.Node{Name: "failed"}}
|
|
|
|
dbs[0].leftNodes["left"] = &node{Node: memberlist.Node{Name: "left"}}
|
|
|
|
|
|
|
|
// active nodes is 2 because the testing node is in the list
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.Check(t, is.Len(dbs[0].nodes, 2))
|
|
|
|
assert.Check(t, is.Len(dbs[0].failedNodes, 1))
|
|
|
|
assert.Check(t, is.Len(dbs[0].leftNodes, 1))
|
2017-12-12 11:23:49 -05:00
|
|
|
|
|
|
|
n, currState, m := dbs[0].findNode("active")
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.Check(t, n != nil)
|
|
|
|
assert.Check(t, is.Equal("active", n.Name))
|
|
|
|
assert.Check(t, is.Equal(nodeActiveState, currState))
|
|
|
|
assert.Check(t, m != nil)
|
2017-12-12 11:23:49 -05:00
|
|
|
// delete the entry manually
|
|
|
|
delete(m, "active")
|
|
|
|
|
|
|
|
// test if can be still find
|
|
|
|
n, currState, m = dbs[0].findNode("active")
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.Check(t, is.Nil(n))
|
|
|
|
assert.Check(t, is.Equal(nodeNotFound, currState))
|
|
|
|
assert.Check(t, is.Nil(m))
|
2017-12-12 11:23:49 -05:00
|
|
|
|
|
|
|
n, currState, m = dbs[0].findNode("failed")
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.Check(t, n != nil)
|
|
|
|
assert.Check(t, is.Equal("failed", n.Name))
|
|
|
|
assert.Check(t, is.Equal(nodeFailedState, currState))
|
|
|
|
assert.Check(t, m != nil)
|
2017-12-12 11:23:49 -05:00
|
|
|
|
|
|
|
// find and remove
|
|
|
|
n, currState, m = dbs[0].findNode("left")
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.Check(t, n != nil)
|
|
|
|
assert.Check(t, is.Equal("left", n.Name))
|
|
|
|
assert.Check(t, is.Equal(nodeLeftState, currState))
|
|
|
|
assert.Check(t, m != nil)
|
2017-12-12 11:23:49 -05:00
|
|
|
delete(m, "left")
|
|
|
|
|
|
|
|
n, currState, m = dbs[0].findNode("left")
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.Check(t, is.Nil(n))
|
|
|
|
assert.Check(t, is.Equal(nodeNotFound, currState))
|
|
|
|
assert.Check(t, is.Nil(m))
|
2017-12-12 11:23:49 -05:00
|
|
|
|
2020-02-26 16:27:08 -05:00
|
|
|
closeNetworkDBInstances(t, dbs)
|
2017-12-12 11:23:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestChangeNodeState(t *testing.T) {
|
|
|
|
dbs := createNetworkDBInstances(t, 1, "node", DefaultConfig())
|
|
|
|
|
|
|
|
dbs[0].nodes["node1"] = &node{Node: memberlist.Node{Name: "node1"}}
|
|
|
|
dbs[0].nodes["node2"] = &node{Node: memberlist.Node{Name: "node2"}}
|
|
|
|
dbs[0].nodes["node3"] = &node{Node: memberlist.Node{Name: "node3"}}
|
|
|
|
|
|
|
|
// active nodes is 4 because the testing node is in the list
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.Check(t, is.Len(dbs[0].nodes, 4))
|
2017-12-12 11:23:49 -05:00
|
|
|
|
|
|
|
n, currState, m := dbs[0].findNode("node1")
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.Check(t, n != nil)
|
|
|
|
assert.Check(t, is.Equal(nodeActiveState, currState))
|
|
|
|
assert.Check(t, is.Equal("node1", n.Name))
|
|
|
|
assert.Check(t, m != nil)
|
2017-12-12 11:23:49 -05:00
|
|
|
|
|
|
|
// node1 to failed
|
|
|
|
dbs[0].changeNodeState("node1", nodeFailedState)
|
|
|
|
|
|
|
|
n, currState, m = dbs[0].findNode("node1")
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.Check(t, n != nil)
|
|
|
|
assert.Check(t, is.Equal(nodeFailedState, currState))
|
|
|
|
assert.Check(t, is.Equal("node1", n.Name))
|
|
|
|
assert.Check(t, m != nil)
|
|
|
|
assert.Check(t, time.Duration(0) != n.reapTime)
|
2017-12-12 11:23:49 -05:00
|
|
|
|
|
|
|
// node1 back to active
|
|
|
|
dbs[0].changeNodeState("node1", nodeActiveState)
|
|
|
|
|
|
|
|
n, currState, m = dbs[0].findNode("node1")
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.Check(t, n != nil)
|
|
|
|
assert.Check(t, is.Equal(nodeActiveState, currState))
|
|
|
|
assert.Check(t, is.Equal("node1", n.Name))
|
|
|
|
assert.Check(t, m != nil)
|
|
|
|
assert.Check(t, is.Equal(time.Duration(0), n.reapTime))
|
2017-12-12 11:23:49 -05:00
|
|
|
|
|
|
|
// node1 to left
|
|
|
|
dbs[0].changeNodeState("node1", nodeLeftState)
|
|
|
|
dbs[0].changeNodeState("node2", nodeLeftState)
|
|
|
|
dbs[0].changeNodeState("node3", nodeLeftState)
|
|
|
|
|
|
|
|
n, currState, m = dbs[0].findNode("node1")
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.Check(t, n != nil)
|
|
|
|
assert.Check(t, is.Equal(nodeLeftState, currState))
|
|
|
|
assert.Check(t, is.Equal("node1", n.Name))
|
|
|
|
assert.Check(t, m != nil)
|
|
|
|
assert.Check(t, time.Duration(0) != n.reapTime)
|
2017-12-12 11:23:49 -05:00
|
|
|
|
|
|
|
n, currState, m = dbs[0].findNode("node2")
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.Check(t, n != nil)
|
|
|
|
assert.Check(t, is.Equal(nodeLeftState, currState))
|
|
|
|
assert.Check(t, is.Equal("node2", n.Name))
|
|
|
|
assert.Check(t, m != nil)
|
|
|
|
assert.Check(t, time.Duration(0) != n.reapTime)
|
2017-12-12 11:23:49 -05:00
|
|
|
|
|
|
|
n, currState, m = dbs[0].findNode("node3")
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.Check(t, n != nil)
|
|
|
|
assert.Check(t, is.Equal(nodeLeftState, currState))
|
|
|
|
assert.Check(t, is.Equal("node3", n.Name))
|
|
|
|
assert.Check(t, m != nil)
|
|
|
|
assert.Check(t, time.Duration(0) != n.reapTime)
|
2017-12-12 11:23:49 -05:00
|
|
|
|
|
|
|
// active nodes is 1 because the testing node is in the list
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.Check(t, is.Len(dbs[0].nodes, 1))
|
|
|
|
assert.Check(t, is.Len(dbs[0].failedNodes, 0))
|
|
|
|
assert.Check(t, is.Len(dbs[0].leftNodes, 3))
|
2017-12-12 11:23:49 -05:00
|
|
|
|
2020-02-26 16:27:08 -05:00
|
|
|
closeNetworkDBInstances(t, dbs)
|
2017-12-12 11:23:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestNodeReincarnation(t *testing.T) {
|
|
|
|
dbs := createNetworkDBInstances(t, 1, "node", DefaultConfig())
|
|
|
|
|
|
|
|
dbs[0].nodes["node1"] = &node{Node: memberlist.Node{Name: "node1", Addr: net.ParseIP("192.168.1.1")}}
|
|
|
|
dbs[0].leftNodes["node2"] = &node{Node: memberlist.Node{Name: "node2", Addr: net.ParseIP("192.168.1.2")}}
|
|
|
|
dbs[0].failedNodes["node3"] = &node{Node: memberlist.Node{Name: "node3", Addr: net.ParseIP("192.168.1.3")}}
|
|
|
|
|
|
|
|
// active nodes is 2 because the testing node is in the list
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.Check(t, is.Len(dbs[0].nodes, 2))
|
|
|
|
assert.Check(t, is.Len(dbs[0].failedNodes, 1))
|
|
|
|
assert.Check(t, is.Len(dbs[0].leftNodes, 1))
|
2017-12-12 11:23:49 -05:00
|
|
|
|
|
|
|
b := dbs[0].purgeReincarnation(&memberlist.Node{Name: "node4", Addr: net.ParseIP("192.168.1.1")})
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.Check(t, b)
|
2017-12-12 11:23:49 -05:00
|
|
|
dbs[0].nodes["node4"] = &node{Node: memberlist.Node{Name: "node4", Addr: net.ParseIP("192.168.1.1")}}
|
|
|
|
|
|
|
|
b = dbs[0].purgeReincarnation(&memberlist.Node{Name: "node5", Addr: net.ParseIP("192.168.1.2")})
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.Check(t, b)
|
2017-12-12 11:23:49 -05:00
|
|
|
dbs[0].nodes["node5"] = &node{Node: memberlist.Node{Name: "node5", Addr: net.ParseIP("192.168.1.1")}}
|
|
|
|
|
|
|
|
b = dbs[0].purgeReincarnation(&memberlist.Node{Name: "node6", Addr: net.ParseIP("192.168.1.3")})
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.Check(t, b)
|
2017-12-12 11:23:49 -05:00
|
|
|
dbs[0].nodes["node6"] = &node{Node: memberlist.Node{Name: "node6", Addr: net.ParseIP("192.168.1.1")}}
|
|
|
|
|
|
|
|
b = dbs[0].purgeReincarnation(&memberlist.Node{Name: "node6", Addr: net.ParseIP("192.168.1.10")})
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.Check(t, !b)
|
2017-12-12 11:23:49 -05:00
|
|
|
|
|
|
|
// active nodes is 1 because the testing node is in the list
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.Check(t, is.Len(dbs[0].nodes, 4))
|
|
|
|
assert.Check(t, is.Len(dbs[0].failedNodes, 0))
|
|
|
|
assert.Check(t, is.Len(dbs[0].leftNodes, 3))
|
2017-12-12 11:23:49 -05:00
|
|
|
|
2020-02-26 16:27:08 -05:00
|
|
|
closeNetworkDBInstances(t, dbs)
|
2017-12-12 11:23:49 -05:00
|
|
|
}
|
2017-12-21 17:42:47 -05:00
|
|
|
|
|
|
|
func TestParallelCreate(t *testing.T) {
|
|
|
|
dbs := createNetworkDBInstances(t, 1, "node", DefaultConfig())
|
|
|
|
|
|
|
|
startCh := make(chan int)
|
|
|
|
doneCh := make(chan error)
|
|
|
|
var success int32
|
|
|
|
for i := 0; i < 20; i++ {
|
|
|
|
go func() {
|
|
|
|
<-startCh
|
|
|
|
err := dbs[0].CreateEntry("testTable", "testNetwork", "key", []byte("value"))
|
|
|
|
if err == nil {
|
|
|
|
atomic.AddInt32(&success, 1)
|
|
|
|
}
|
|
|
|
doneCh <- err
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
close(startCh)
|
|
|
|
|
|
|
|
for i := 0; i < 20; i++ {
|
|
|
|
<-doneCh
|
|
|
|
}
|
|
|
|
close(doneCh)
|
|
|
|
// Only 1 write should have succeeded
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.Check(t, is.Equal(int32(1), success))
|
2017-12-21 17:42:47 -05:00
|
|
|
|
2020-02-26 16:27:08 -05:00
|
|
|
closeNetworkDBInstances(t, dbs)
|
2017-12-21 17:42:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParallelDelete(t *testing.T) {
|
|
|
|
dbs := createNetworkDBInstances(t, 1, "node", DefaultConfig())
|
|
|
|
|
|
|
|
err := dbs[0].CreateEntry("testTable", "testNetwork", "key", []byte("value"))
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.NilError(t, err)
|
2017-12-21 17:42:47 -05:00
|
|
|
|
|
|
|
startCh := make(chan int)
|
|
|
|
doneCh := make(chan error)
|
|
|
|
var success int32
|
|
|
|
for i := 0; i < 20; i++ {
|
|
|
|
go func() {
|
|
|
|
<-startCh
|
|
|
|
err := dbs[0].DeleteEntry("testTable", "testNetwork", "key")
|
|
|
|
if err == nil {
|
|
|
|
atomic.AddInt32(&success, 1)
|
|
|
|
}
|
|
|
|
doneCh <- err
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
close(startCh)
|
|
|
|
|
|
|
|
for i := 0; i < 20; i++ {
|
|
|
|
<-doneCh
|
|
|
|
}
|
|
|
|
close(doneCh)
|
|
|
|
// Only 1 write should have succeeded
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.Check(t, is.Equal(int32(1), success))
|
2017-12-21 17:42:47 -05:00
|
|
|
|
2020-02-26 16:27:08 -05:00
|
|
|
closeNetworkDBInstances(t, dbs)
|
2017-12-21 17:42:47 -05:00
|
|
|
}
|
2018-06-21 20:57:35 -04:00
|
|
|
|
|
|
|
func TestNetworkDBIslands(t *testing.T) {
|
2021-07-12 05:12:56 -04:00
|
|
|
pollTimeout := func() time.Duration {
|
|
|
|
const defaultTimeout = 120 * time.Second
|
|
|
|
dl, ok := t.Deadline()
|
|
|
|
if !ok {
|
|
|
|
return defaultTimeout
|
|
|
|
}
|
|
|
|
if d := time.Until(dl); d <= defaultTimeout {
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
return defaultTimeout
|
|
|
|
}
|
|
|
|
|
2018-06-21 20:57:35 -04:00
|
|
|
logrus.SetLevel(logrus.DebugLevel)
|
2021-07-12 05:12:56 -04:00
|
|
|
conf := DefaultConfig()
|
|
|
|
// Shorten durations to speed up test execution.
|
|
|
|
conf.rejoinClusterDuration = conf.rejoinClusterDuration / 10
|
|
|
|
conf.rejoinClusterInterval = conf.rejoinClusterInterval / 10
|
|
|
|
dbs := createNetworkDBInstances(t, 5, "node", conf)
|
2018-06-21 20:57:35 -04:00
|
|
|
|
|
|
|
// Get the node IP used currently
|
2021-05-27 20:15:56 -04:00
|
|
|
node := dbs[0].nodes[dbs[0].config.NodeID]
|
2018-06-21 20:57:35 -04:00
|
|
|
baseIPStr := node.Addr.String()
|
|
|
|
// Node 0,1,2 are going to be the 3 bootstrap nodes
|
|
|
|
members := []string{fmt.Sprintf("%s:%d", baseIPStr, dbs[0].config.BindPort),
|
|
|
|
fmt.Sprintf("%s:%d", baseIPStr, dbs[1].config.BindPort),
|
|
|
|
fmt.Sprintf("%s:%d", baseIPStr, dbs[2].config.BindPort)}
|
|
|
|
// Rejoining will update the list of the bootstrap members
|
|
|
|
for i := 3; i < 5; i++ {
|
2021-06-01 17:42:15 -04:00
|
|
|
t.Logf("Re-joining: %d", i)
|
2018-07-04 05:01:52 -04:00
|
|
|
assert.Check(t, dbs[i].Join(members))
|
2018-06-21 20:57:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now the 3 bootstrap nodes will cleanly leave, and will be properly removed from the other 2 nodes
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
logrus.Infof("node %d leaving", i)
|
|
|
|
dbs[i].Close()
|
|
|
|
}
|
|
|
|
|
2021-06-01 17:42:15 -04:00
|
|
|
checkDBs := make(map[string]*NetworkDB)
|
|
|
|
for i := 3; i < 5; i++ {
|
|
|
|
db := dbs[i]
|
|
|
|
checkDBs[db.config.Hostname] = db
|
|
|
|
}
|
|
|
|
|
2018-06-21 20:57:35 -04:00
|
|
|
// Give some time to let the system propagate the messages and free up the ports
|
2019-10-01 19:54:01 -04:00
|
|
|
check := func(t poll.LogT) poll.Result {
|
|
|
|
// Verify that the nodes are actually all gone and marked appropiately
|
2021-06-01 17:42:15 -04:00
|
|
|
for name, db := range checkDBs {
|
|
|
|
db.RLock()
|
|
|
|
if (len(db.leftNodes) != 3) || (len(db.failedNodes) != 0) {
|
|
|
|
for name := range db.leftNodes {
|
|
|
|
t.Logf("%s: Node %s left", db.config.Hostname, name)
|
|
|
|
}
|
|
|
|
for name := range db.failedNodes {
|
|
|
|
t.Logf("%s: Node %s failed", db.config.Hostname, name)
|
|
|
|
}
|
|
|
|
db.RUnlock()
|
|
|
|
return poll.Continue("%s:Waiting for all nodes to cleanly leave, left: %d, failed nodes: %d", name, len(db.leftNodes), len(db.failedNodes))
|
2019-10-01 19:54:01 -04:00
|
|
|
}
|
2021-06-01 17:42:15 -04:00
|
|
|
db.RUnlock()
|
|
|
|
t.Logf("%s: OK", name)
|
|
|
|
delete(checkDBs, name)
|
2019-10-01 19:54:01 -04:00
|
|
|
}
|
|
|
|
return poll.Success()
|
2018-06-21 20:57:35 -04:00
|
|
|
}
|
2021-07-12 05:12:56 -04:00
|
|
|
poll.WaitOn(t, check, poll.WithDelay(time.Second), poll.WithTimeout(pollTimeout()))
|
2018-06-21 20:57:35 -04:00
|
|
|
|
|
|
|
// Spawn again the first 3 nodes with different names but same IP:port
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
logrus.Infof("node %d coming back", i)
|
|
|
|
dbs[i].config.NodeID = stringid.TruncateID(stringid.GenerateRandomID())
|
|
|
|
dbs[i] = launchNode(t, *dbs[i].config)
|
|
|
|
}
|
|
|
|
|
2021-07-12 05:12:56 -04:00
|
|
|
// Give some time for the reconnect routine to run, it runs every 6s.
|
2019-10-01 19:54:01 -04:00
|
|
|
check = func(t poll.LogT) poll.Result {
|
|
|
|
// Verify that the cluster is again all connected. Note that the 3 previous node did not do any join
|
|
|
|
for i := 0; i < 5; i++ {
|
2021-06-01 17:42:15 -04:00
|
|
|
db := dbs[i]
|
|
|
|
db.RLock()
|
|
|
|
if len(db.nodes) != 5 {
|
|
|
|
db.RUnlock()
|
2019-10-01 19:54:01 -04:00
|
|
|
return poll.Continue("%s:Waiting to connect to all nodes", dbs[i].config.Hostname)
|
|
|
|
}
|
2021-06-01 17:42:15 -04:00
|
|
|
if len(db.failedNodes) != 0 {
|
|
|
|
db.RUnlock()
|
2019-10-01 19:54:01 -04:00
|
|
|
return poll.Continue("%s:Waiting for 0 failedNodes", dbs[i].config.Hostname)
|
|
|
|
}
|
|
|
|
if i < 3 {
|
|
|
|
// nodes from 0 to 3 has no left nodes
|
2021-06-01 17:42:15 -04:00
|
|
|
if len(db.leftNodes) != 0 {
|
|
|
|
db.RUnlock()
|
2019-10-01 19:54:01 -04:00
|
|
|
return poll.Continue("%s:Waiting to have no leftNodes", dbs[i].config.Hostname)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// nodes from 4 to 5 has the 3 previous left nodes
|
2021-06-01 17:42:15 -04:00
|
|
|
if len(db.leftNodes) != 3 {
|
|
|
|
db.RUnlock()
|
2019-10-01 19:54:01 -04:00
|
|
|
return poll.Continue("%s:Waiting to have 3 leftNodes", dbs[i].config.Hostname)
|
|
|
|
}
|
|
|
|
}
|
2021-06-01 17:42:15 -04:00
|
|
|
db.RUnlock()
|
2018-06-21 20:57:35 -04:00
|
|
|
}
|
2019-10-01 19:54:01 -04:00
|
|
|
return poll.Success()
|
2018-06-21 20:57:35 -04:00
|
|
|
}
|
2021-07-12 05:12:56 -04:00
|
|
|
poll.WaitOn(t, check, poll.WithDelay(time.Second), poll.WithTimeout(pollTimeout()))
|
2020-02-26 16:27:08 -05:00
|
|
|
closeNetworkDBInstances(t, dbs)
|
2018-06-21 20:57:35 -04:00
|
|
|
}
|