2014-06-04 10:42:02 +04:00
|
|
|
package ledis
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2014-06-10 06:41:50 +04:00
|
|
|
"fmt"
|
2014-06-04 10:42:02 +04:00
|
|
|
"os"
|
|
|
|
"testing"
|
2015-05-04 17:42:28 +03:00
|
|
|
|
|
|
|
"github.com/siddontang/ledisdb/config"
|
|
|
|
"github.com/siddontang/ledisdb/store"
|
2014-06-04 10:42:02 +04:00
|
|
|
)
|
|
|
|
|
2014-06-10 06:41:50 +04:00
|
|
|
func checkLedisEqual(master *Ledis, slave *Ledis) error {
|
2014-07-25 13:58:00 +04:00
|
|
|
it := master.ldb.RangeLimitIterator(nil, nil, store.RangeClose, 0, -1)
|
2014-06-10 06:41:50 +04:00
|
|
|
for ; it.Valid(); it.Next() {
|
|
|
|
key := it.Key()
|
|
|
|
value := it.Value()
|
|
|
|
|
|
|
|
if v, err := slave.ldb.Get(key); err != nil {
|
|
|
|
return err
|
|
|
|
} else if !bytes.Equal(v, value) {
|
2014-11-27 09:03:44 +03:00
|
|
|
return fmt.Errorf("equal error at %q, %d != %d", key, len(v), len(value))
|
2014-06-10 06:41:50 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-06-04 10:42:02 +04:00
|
|
|
func TestReplication(t *testing.T) {
|
|
|
|
var master *Ledis
|
|
|
|
var slave *Ledis
|
|
|
|
var err error
|
|
|
|
|
2014-10-11 12:00:59 +04:00
|
|
|
cfgM := config.NewConfigDefault()
|
2014-08-07 12:49:48 +04:00
|
|
|
cfgM.DataDir = "/tmp/test_repl/master"
|
|
|
|
|
2014-09-23 13:28:09 +04:00
|
|
|
cfgM.UseReplication = true
|
2014-09-27 06:08:45 +04:00
|
|
|
cfgM.Replication.Compression = true
|
2014-08-07 12:49:48 +04:00
|
|
|
|
|
|
|
os.RemoveAll(cfgM.DataDir)
|
|
|
|
|
|
|
|
master, err = Open(cfgM)
|
2014-06-04 10:42:02 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-11-20 12:33:38 +03:00
|
|
|
defer master.Close()
|
2014-06-04 10:42:02 +04:00
|
|
|
|
2014-10-11 12:00:59 +04:00
|
|
|
cfgS := config.NewConfigDefault()
|
2014-08-07 12:49:48 +04:00
|
|
|
cfgS.DataDir = "/tmp/test_repl/slave"
|
2014-09-23 13:28:09 +04:00
|
|
|
cfgS.UseReplication = true
|
2014-10-10 05:49:16 +04:00
|
|
|
cfgS.Readonly = true
|
2014-08-07 12:49:48 +04:00
|
|
|
|
|
|
|
os.RemoveAll(cfgS.DataDir)
|
|
|
|
|
2014-10-10 05:49:16 +04:00
|
|
|
slave, err = Open(cfgS)
|
2014-06-04 10:42:02 +04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-11-20 12:33:38 +03:00
|
|
|
defer slave.Close()
|
2014-06-04 10:42:02 +04:00
|
|
|
|
|
|
|
db, _ := master.Select(0)
|
2014-06-10 06:41:50 +04:00
|
|
|
db.Set([]byte("a"), []byte("value"))
|
|
|
|
db.Set([]byte("b"), []byte("value"))
|
|
|
|
db.Set([]byte("c"), []byte("value"))
|
2014-06-04 10:42:02 +04:00
|
|
|
|
2014-09-22 13:50:51 +04:00
|
|
|
db.HSet([]byte("a"), []byte("1"), []byte("value"))
|
|
|
|
db.HSet([]byte("b"), []byte("2"), []byte("value"))
|
|
|
|
db.HSet([]byte("c"), []byte("3"), []byte("value"))
|
2014-06-04 10:42:02 +04:00
|
|
|
|
2015-03-14 04:10:00 +03:00
|
|
|
// m, _ := db.Multi()
|
|
|
|
// m.Set([]byte("a1"), []byte("value"))
|
|
|
|
// m.Set([]byte("b1"), []byte("value"))
|
|
|
|
// m.Set([]byte("c1"), []byte("value"))
|
|
|
|
// m.Close()
|
2014-09-02 13:55:12 +04:00
|
|
|
|
2014-06-10 06:41:50 +04:00
|
|
|
slave.FlushAll()
|
2014-06-04 10:42:02 +04:00
|
|
|
|
2014-08-25 10:18:23 +04:00
|
|
|
db.Set([]byte("a1"), []byte("value"))
|
|
|
|
db.Set([]byte("b1"), []byte("value"))
|
|
|
|
db.Set([]byte("c1"), []byte("value"))
|
2014-06-10 06:41:50 +04:00
|
|
|
|
|
|
|
db.HSet([]byte("a1"), []byte("1"), []byte("value"))
|
|
|
|
db.HSet([]byte("b1"), []byte("2"), []byte("value"))
|
|
|
|
db.HSet([]byte("c1"), []byte("3"), []byte("value"))
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
var n int
|
2014-09-22 13:50:51 +04:00
|
|
|
var id uint64 = 1
|
2014-06-10 06:41:50 +04:00
|
|
|
for {
|
|
|
|
buf.Reset()
|
2014-09-22 13:50:51 +04:00
|
|
|
n, id, err = master.ReadLogsTo(id, &buf)
|
2014-06-10 06:41:50 +04:00
|
|
|
if err != nil {
|
2014-06-04 10:42:02 +04:00
|
|
|
t.Fatal(err)
|
2014-09-22 13:50:51 +04:00
|
|
|
} else if n != 0 {
|
2014-09-22 18:03:44 +04:00
|
|
|
if err = slave.StoreLogsFromReader(&buf); err != nil {
|
2014-06-10 06:41:50 +04:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-09-22 13:50:51 +04:00
|
|
|
} else if n == 0 {
|
|
|
|
break
|
2014-06-04 10:42:02 +04:00
|
|
|
}
|
|
|
|
}
|
2014-06-10 06:41:50 +04:00
|
|
|
|
2014-09-22 13:50:51 +04:00
|
|
|
slave.WaitReplication()
|
|
|
|
|
2014-06-10 06:41:50 +04:00
|
|
|
if err = checkLedisEqual(master, slave); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2014-06-04 10:42:02 +04:00
|
|
|
}
|