update, can not run at all

This commit is contained in:
siddontang 2014-09-22 22:03:44 +08:00
parent 22109ed0f1
commit 59e974c258
4 changed files with 28 additions and 18 deletions

View File

@ -58,7 +58,7 @@ func Open2(cfg *config.Config, flags int) (*Ledis, error) {
return nil, err return nil, err
} }
l.rc = make(chan struct{}) l.rc = make(chan struct{}, 1)
l.rbatch = l.ldb.NewWriteBatch() l.rbatch = l.ldb.NewWriteBatch()
go l.onReplication() go l.onReplication()

View File

@ -19,8 +19,11 @@ var (
) )
func (l *Ledis) handleReplication() { func (l *Ledis) handleReplication() {
l.commitLock.Lock()
defer l.commitLock.Unlock()
l.rwg.Add(1) l.rwg.Add(1)
var rl *rpl.Log rl := &rpl.Log{}
for { for {
if err := l.r.NextCommitLog(rl); err != nil { if err := l.r.NextCommitLog(rl); err != nil {
if err != rpl.ErrNoBehindLog { if err != rpl.ErrNoBehindLog {
@ -59,33 +62,37 @@ func (l *Ledis) onReplication() {
} }
func (l *Ledis) WaitReplication() error { func (l *Ledis) WaitReplication() error {
l.rwg.Wait() b, err := l.r.CommitIDBehind()
if err != nil {
return err
} else if b {
l.rc <- struct{}{}
l.rwg.Wait()
}
return nil return nil
} }
func (l *Ledis) StoreLogsFromReader(rb io.Reader) (uint64, error) { func (l *Ledis) StoreLogsFromReader(rb io.Reader) error {
if l.r == nil { if l.r == nil {
return 0, fmt.Errorf("replication not enable") return fmt.Errorf("replication not enable")
} }
var log *rpl.Log log := &rpl.Log{}
var n uint64
for { for {
if err := log.Decode(rb); err != nil { if err := log.Decode(rb); err != nil {
if err == io.EOF { if err == io.EOF {
break break
} else { } else {
return 0, err return err
} }
} }
if err := l.r.StoreLog(log); err != nil { if err := l.r.StoreLog(log); err != nil {
return 0, err return err
} }
n = log.ID
} }
select { select {
@ -94,10 +101,10 @@ func (l *Ledis) StoreLogsFromReader(rb io.Reader) (uint64, error) {
break break
} }
return n, nil return nil
} }
func (l *Ledis) StoreLogsFromData(data []byte) (uint64, error) { func (l *Ledis) StoreLogsFromData(data []byte) error {
rb := bytes.NewReader(data) rb := bytes.NewReader(data)
return l.StoreLogsFromReader(rb) return l.StoreLogsFromReader(rb)
@ -127,7 +134,7 @@ func (l *Ledis) ReadLogsTo(startLogID uint64, w io.Writer) (n int, nextLogID uin
return return
} }
var log *rpl.Log log := &rpl.Log{}
for i := startLogID; i <= lastID; i++ { for i := startLogID; i <= lastID; i++ {
if err = l.r.GetLog(i, log); err != nil { if err = l.r.GetLog(i, log); err != nil {
return return

View File

@ -81,17 +81,14 @@ func TestReplication(t *testing.T) {
var buf bytes.Buffer var buf bytes.Buffer
var n int var n int
var id uint64 = 1 var id uint64 = 1
var nid uint64
for { for {
buf.Reset() buf.Reset()
n, id, err = master.ReadLogsTo(id, &buf) n, id, err = master.ReadLogsTo(id, &buf)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} else if n != 0 { } else if n != 0 {
if nid, err = slave.StoreLogsFromReader(&buf); err != nil { if err = slave.StoreLogsFromReader(&buf); err != nil {
t.Fatal(err) t.Fatal(err)
} else if nid != id {
t.Fatal(nid, id)
} }
} else if n == 0 { } else if n == 0 {
break break

View File

@ -90,7 +90,13 @@ func (l *Log) Decode(r io.Reader) error {
length := binary.BigEndian.Uint32(buf[pos:]) length := binary.BigEndian.Uint32(buf[pos:])
l.Data = make([]byte, length) l.Data = l.Data[0:0]
if cap(l.Data) >= int(length) {
l.Data = l.Data[0:length]
} else {
l.Data = make([]byte, length)
}
if _, err := io.ReadFull(r, l.Data); err != nil { if _, err := io.ReadFull(r, l.Data); err != nil {
return err return err
} }