2014-06-10 06:41:50 +04:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-03-11 06:54:02 +03:00
|
|
|
"github.com/siddontang/goredis"
|
2014-08-07 12:49:48 +04:00
|
|
|
"github.com/siddontang/ledisdb/config"
|
2014-06-10 06:41:50 +04:00
|
|
|
"os"
|
2014-09-18 18:20:18 +04:00
|
|
|
"reflect"
|
2014-06-10 06:41:50 +04:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func checkDataEqual(master *App, slave *App) error {
|
2014-09-18 18:20:18 +04:00
|
|
|
mdb, _ := master.ldb.Select(0)
|
|
|
|
sdb, _ := slave.ldb.Select(0)
|
|
|
|
|
2015-03-02 06:10:54 +03:00
|
|
|
mkeys, _ := mdb.Scan(KV, nil, 100, true, "")
|
|
|
|
skeys, _ := sdb.Scan(KV, nil, 100, true, "")
|
2014-09-18 18:20:18 +04:00
|
|
|
|
|
|
|
if len(mkeys) != len(skeys) {
|
2014-09-23 13:28:09 +04:00
|
|
|
return fmt.Errorf("keys number not equal %d != %d", len(mkeys), len(skeys))
|
2014-09-18 18:20:18 +04:00
|
|
|
} else if !reflect.DeepEqual(mkeys, skeys) {
|
|
|
|
return fmt.Errorf("keys not equal")
|
|
|
|
} else {
|
|
|
|
for _, k := range mkeys {
|
|
|
|
v1, _ := mdb.Get(k)
|
|
|
|
v2, _ := sdb.Get(k)
|
|
|
|
if !reflect.DeepEqual(v1, v2) {
|
|
|
|
return fmt.Errorf("value not equal")
|
|
|
|
}
|
2014-06-10 06:41:50 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReplication(t *testing.T) {
|
|
|
|
data_dir := "/tmp/test_replication"
|
|
|
|
os.RemoveAll(data_dir)
|
|
|
|
|
2014-10-11 12:00:59 +04:00
|
|
|
masterCfg := config.NewConfigDefault()
|
2014-06-10 06:41:50 +04:00
|
|
|
masterCfg.DataDir = fmt.Sprintf("%s/master", data_dir)
|
|
|
|
masterCfg.Addr = "127.0.0.1:11182"
|
2014-09-23 13:28:09 +04:00
|
|
|
masterCfg.UseReplication = true
|
|
|
|
masterCfg.Replication.Sync = true
|
2014-10-11 12:00:59 +04:00
|
|
|
masterCfg.Replication.WaitSyncTime = 5000
|
2014-06-10 06:41:50 +04:00
|
|
|
|
|
|
|
var master *App
|
|
|
|
var slave *App
|
|
|
|
var err error
|
|
|
|
master, err = NewApp(masterCfg)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-08-26 19:21:45 +04:00
|
|
|
defer master.Close()
|
2014-06-10 06:41:50 +04:00
|
|
|
|
2014-10-11 12:00:59 +04:00
|
|
|
slaveCfg := config.NewConfigDefault()
|
2014-06-10 06:41:50 +04:00
|
|
|
slaveCfg.DataDir = fmt.Sprintf("%s/slave", data_dir)
|
|
|
|
slaveCfg.Addr = "127.0.0.1:11183"
|
|
|
|
slaveCfg.SlaveOf = masterCfg.Addr
|
2014-09-23 13:28:09 +04:00
|
|
|
slaveCfg.UseReplication = true
|
2014-06-10 06:41:50 +04:00
|
|
|
|
|
|
|
slave, err = NewApp(slaveCfg)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-08-26 19:21:45 +04:00
|
|
|
defer slave.Close()
|
2014-06-10 06:41:50 +04:00
|
|
|
|
|
|
|
go master.Run()
|
|
|
|
|
2014-09-23 13:28:09 +04:00
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
go slave.Run()
|
|
|
|
|
2014-06-10 06:41:50 +04:00
|
|
|
db, _ := master.ldb.Select(0)
|
|
|
|
|
|
|
|
value := make([]byte, 10)
|
|
|
|
|
|
|
|
db.Set([]byte("a"), value)
|
|
|
|
db.Set([]byte("b"), value)
|
2014-09-18 18:20:18 +04:00
|
|
|
db.Set([]byte("c"), value)
|
|
|
|
db.Set([]byte("d"), value)
|
2014-06-10 06:41:50 +04:00
|
|
|
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
if err = checkDataEqual(master, slave); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
db.Set([]byte("a1"), value)
|
|
|
|
db.Set([]byte("b1"), value)
|
2014-09-18 18:20:18 +04:00
|
|
|
db.Set([]byte("c1"), value)
|
|
|
|
db.Set([]byte("d1"), value)
|
2014-06-10 06:41:50 +04:00
|
|
|
|
2014-09-23 13:28:09 +04:00
|
|
|
//time.Sleep(1 * time.Second)
|
|
|
|
slave.ldb.WaitReplication()
|
|
|
|
|
2014-06-10 06:41:50 +04:00
|
|
|
if err = checkDataEqual(master, slave); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-10-10 05:49:16 +04:00
|
|
|
slave.slaveof("", false, false)
|
2014-06-10 06:41:50 +04:00
|
|
|
|
|
|
|
db.Set([]byte("a2"), value)
|
|
|
|
db.Set([]byte("b2"), value)
|
2014-09-18 18:20:18 +04:00
|
|
|
db.Set([]byte("c2"), value)
|
|
|
|
db.Set([]byte("d2"), value)
|
2014-06-10 06:41:50 +04:00
|
|
|
|
|
|
|
db.Set([]byte("a3"), value)
|
|
|
|
db.Set([]byte("b3"), value)
|
2014-09-18 18:20:18 +04:00
|
|
|
db.Set([]byte("c3"), value)
|
|
|
|
db.Set([]byte("d3"), value)
|
2014-06-10 06:41:50 +04:00
|
|
|
|
|
|
|
if err = checkDataEqual(master, slave); err == nil {
|
|
|
|
t.Fatal("must error")
|
|
|
|
}
|
|
|
|
|
2014-10-10 05:49:16 +04:00
|
|
|
slave.slaveof(masterCfg.Addr, false, false)
|
2014-09-23 13:28:09 +04:00
|
|
|
|
2014-06-10 06:41:50 +04:00
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
|
|
|
if err = checkDataEqual(master, slave); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2015-02-03 09:15:30 +03:00
|
|
|
mStat, _ := master.ldb.ReplicationStat()
|
|
|
|
sStat, _ := slave.ldb.ReplicationStat()
|
|
|
|
|
|
|
|
if err = checkTestRole(masterCfg.Addr, []interface{}{
|
|
|
|
[]byte("master"),
|
|
|
|
int64(mStat.LastID),
|
|
|
|
[]interface{}{
|
|
|
|
[]interface{}{
|
|
|
|
[]byte("127.0.0.1"),
|
|
|
|
[]byte("11183"),
|
|
|
|
[]byte(fmt.Sprintf("%d", sStat.LastID)),
|
|
|
|
}},
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = checkTestRole(slaveCfg.Addr, []interface{}{
|
|
|
|
[]byte("slave"),
|
|
|
|
[]byte("127.0.0.1"),
|
2015-02-14 04:45:07 +03:00
|
|
|
int64(11182),
|
2015-02-03 09:15:30 +03:00
|
|
|
[]byte("connected"),
|
|
|
|
int64(sStat.LastID),
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2014-10-03 16:31:24 +04:00
|
|
|
slave.tryReSlaveof()
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
|
|
|
|
slave.ldb.WaitReplication()
|
|
|
|
|
|
|
|
if err = checkDataEqual(master, slave); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-02-03 09:15:30 +03:00
|
|
|
}
|
2014-10-03 16:31:24 +04:00
|
|
|
|
2015-02-03 09:15:30 +03:00
|
|
|
func checkTestRole(addr string, checkRoles []interface{}) error {
|
2015-03-11 06:54:02 +03:00
|
|
|
conn, _ := goredis.Connect(addr)
|
2015-02-03 09:15:30 +03:00
|
|
|
defer conn.Close()
|
2015-03-11 06:54:02 +03:00
|
|
|
roles, err := goredis.MultiBulk(conn.Do("ROLE"))
|
2015-02-03 09:15:30 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if !reflect.DeepEqual(roles, checkRoles) {
|
|
|
|
return fmt.Errorf("%v != %v", roles, checkRoles)
|
|
|
|
}
|
|
|
|
return nil
|
2014-06-10 06:41:50 +04:00
|
|
|
}
|