2014-06-04 10:42:02 +04:00
|
|
|
package ledis
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
2014-09-24 05:46:36 +04:00
|
|
|
"github.com/siddontang/go/log"
|
2014-09-22 13:50:51 +04:00
|
|
|
"github.com/siddontang/ledisdb/rpl"
|
2014-06-04 10:42:02 +04:00
|
|
|
"io"
|
2014-09-04 19:52:09 +04:00
|
|
|
"time"
|
2014-06-04 10:42:02 +04:00
|
|
|
)
|
|
|
|
|
2014-09-01 06:16:20 +04:00
|
|
|
const (
|
2014-09-22 13:50:51 +04:00
|
|
|
maxReplLogSize = 1 * 1024 * 1024
|
2014-09-01 06:16:20 +04:00
|
|
|
)
|
|
|
|
|
2014-07-11 09:28:34 +04:00
|
|
|
var (
|
2014-09-22 13:50:51 +04:00
|
|
|
ErrLogMissed = errors.New("log is pured in server")
|
2014-07-11 09:28:34 +04:00
|
|
|
)
|
|
|
|
|
2014-09-23 13:28:09 +04:00
|
|
|
func (l *Ledis) ReplicationUsed() bool {
|
|
|
|
return l.r != nil
|
|
|
|
}
|
|
|
|
|
2014-09-22 13:50:51 +04:00
|
|
|
func (l *Ledis) handleReplication() {
|
2014-09-22 18:03:44 +04:00
|
|
|
l.commitLock.Lock()
|
|
|
|
defer l.commitLock.Unlock()
|
|
|
|
|
2014-09-22 13:50:51 +04:00
|
|
|
l.rwg.Add(1)
|
2014-09-22 18:03:44 +04:00
|
|
|
rl := &rpl.Log{}
|
2014-09-22 13:50:51 +04:00
|
|
|
for {
|
2014-09-23 13:28:09 +04:00
|
|
|
if err := l.r.NextNeedCommitLog(rl); err != nil {
|
2014-09-22 13:50:51 +04:00
|
|
|
if err != rpl.ErrNoBehindLog {
|
|
|
|
log.Error("get next commit log err, %s", err.Error)
|
|
|
|
} else {
|
|
|
|
l.rwg.Done()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
l.rbatch.Rollback()
|
|
|
|
decodeEventBatch(l.rbatch, rl.Data)
|
2014-08-30 13:39:44 +04:00
|
|
|
|
2014-09-22 13:50:51 +04:00
|
|
|
if err := l.rbatch.Commit(); err != nil {
|
|
|
|
log.Error("commit log error %s", err.Error())
|
|
|
|
} else if err = l.r.UpdateCommitID(rl.ID); err != nil {
|
|
|
|
log.Error("update commit id error %s", err.Error())
|
|
|
|
}
|
2014-08-30 13:39:44 +04:00
|
|
|
}
|
|
|
|
|
2014-09-22 13:50:51 +04:00
|
|
|
}
|
2014-08-30 13:39:44 +04:00
|
|
|
}
|
|
|
|
|
2014-09-22 13:50:51 +04:00
|
|
|
func (l *Ledis) onReplication() {
|
2014-09-25 06:21:50 +04:00
|
|
|
l.wg.Add(1)
|
|
|
|
defer l.wg.Done()
|
|
|
|
|
2014-09-23 13:28:09 +04:00
|
|
|
AsyncNotify(l.rc)
|
2014-06-04 10:42:02 +04:00
|
|
|
|
2014-09-22 13:50:51 +04:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-l.rc:
|
|
|
|
l.handleReplication()
|
|
|
|
case <-time.After(5 * time.Second):
|
|
|
|
l.handleReplication()
|
2014-09-25 06:21:50 +04:00
|
|
|
case <-l.quit:
|
|
|
|
return
|
2014-09-22 13:50:51 +04:00
|
|
|
}
|
2014-06-04 10:42:02 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-22 13:50:51 +04:00
|
|
|
func (l *Ledis) WaitReplication() error {
|
2014-09-23 13:28:09 +04:00
|
|
|
if !l.ReplicationUsed() {
|
|
|
|
return ErrRplNotSupport
|
|
|
|
|
|
|
|
}
|
|
|
|
AsyncNotify(l.rc)
|
|
|
|
|
|
|
|
l.rwg.Wait()
|
|
|
|
|
2014-09-22 18:03:44 +04:00
|
|
|
b, err := l.r.CommitIDBehind()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if b {
|
2014-09-23 13:28:09 +04:00
|
|
|
AsyncNotify(l.rc)
|
2014-09-22 18:03:44 +04:00
|
|
|
l.rwg.Wait()
|
|
|
|
}
|
2014-06-04 10:42:02 +04:00
|
|
|
|
2014-08-30 13:39:44 +04:00
|
|
|
return nil
|
2014-06-04 10:42:02 +04:00
|
|
|
}
|
|
|
|
|
2014-09-22 18:03:44 +04:00
|
|
|
func (l *Ledis) StoreLogsFromReader(rb io.Reader) error {
|
2014-09-23 13:28:09 +04:00
|
|
|
if !l.ReplicationUsed() {
|
|
|
|
return ErrRplNotSupport
|
|
|
|
} else if !l.readOnly {
|
|
|
|
return ErrRplInRDWR
|
2014-06-04 10:42:02 +04:00
|
|
|
}
|
|
|
|
|
2014-09-22 18:03:44 +04:00
|
|
|
log := &rpl.Log{}
|
2014-06-09 13:23:32 +04:00
|
|
|
|
|
|
|
for {
|
2014-09-22 13:50:51 +04:00
|
|
|
if err := log.Decode(rb); err != nil {
|
2014-06-09 13:23:32 +04:00
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
} else {
|
2014-09-22 18:03:44 +04:00
|
|
|
return err
|
2014-06-09 13:23:32 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-22 13:50:51 +04:00
|
|
|
if err := l.r.StoreLog(log); err != nil {
|
2014-09-22 18:03:44 +04:00
|
|
|
return err
|
2014-06-09 13:23:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-09-23 13:28:09 +04:00
|
|
|
AsyncNotify(l.rc)
|
2014-07-11 09:28:34 +04:00
|
|
|
|
2014-09-22 18:03:44 +04:00
|
|
|
return nil
|
2014-07-11 09:28:34 +04:00
|
|
|
}
|
|
|
|
|
2014-09-22 18:03:44 +04:00
|
|
|
func (l *Ledis) StoreLogsFromData(data []byte) error {
|
2014-06-09 13:23:32 +04:00
|
|
|
rb := bytes.NewReader(data)
|
|
|
|
|
2014-09-22 13:50:51 +04:00
|
|
|
return l.StoreLogsFromReader(rb)
|
2014-06-09 13:23:32 +04:00
|
|
|
}
|
|
|
|
|
2014-09-22 13:50:51 +04:00
|
|
|
func (l *Ledis) ReadLogsTo(startLogID uint64, w io.Writer) (n int, nextLogID uint64, err error) {
|
2014-09-23 13:28:09 +04:00
|
|
|
if !l.ReplicationUsed() {
|
2014-09-22 13:50:51 +04:00
|
|
|
// no replication log
|
|
|
|
nextLogID = 0
|
2014-09-23 13:28:09 +04:00
|
|
|
err = ErrRplNotSupport
|
2014-09-22 13:50:51 +04:00
|
|
|
return
|
2014-06-04 10:42:02 +04:00
|
|
|
}
|
|
|
|
|
2014-09-22 13:50:51 +04:00
|
|
|
var firtID, lastID uint64
|
2014-06-04 10:42:02 +04:00
|
|
|
|
2014-09-22 13:50:51 +04:00
|
|
|
firtID, err = l.r.FirstLogID()
|
|
|
|
if err != nil {
|
2014-09-04 19:52:09 +04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-09-22 13:50:51 +04:00
|
|
|
if startLogID < firtID {
|
|
|
|
err = ErrLogMissed
|
|
|
|
return
|
2014-09-04 19:52:09 +04:00
|
|
|
}
|
|
|
|
|
2014-09-22 13:50:51 +04:00
|
|
|
lastID, err = l.r.LastLogID()
|
|
|
|
if err != nil {
|
2014-06-09 13:23:32 +04:00
|
|
|
return
|
2014-06-04 10:42:02 +04:00
|
|
|
}
|
|
|
|
|
2014-09-23 13:28:09 +04:00
|
|
|
nextLogID = startLogID
|
|
|
|
|
2014-09-22 18:03:44 +04:00
|
|
|
log := &rpl.Log{}
|
2014-09-22 13:50:51 +04:00
|
|
|
for i := startLogID; i <= lastID; i++ {
|
|
|
|
if err = l.r.GetLog(i, log); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2014-06-04 10:42:02 +04:00
|
|
|
|
2014-09-22 13:50:51 +04:00
|
|
|
if err = log.Encode(w); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2014-06-09 13:23:32 +04:00
|
|
|
|
2014-09-22 13:50:51 +04:00
|
|
|
nextLogID = i + 1
|
2014-06-10 06:41:50 +04:00
|
|
|
|
2014-09-22 13:50:51 +04:00
|
|
|
n += log.Size()
|
2014-06-09 13:23:32 +04:00
|
|
|
|
2014-09-22 13:50:51 +04:00
|
|
|
if n > maxReplLogSize {
|
|
|
|
break
|
2014-06-10 06:41:50 +04:00
|
|
|
}
|
2014-06-09 13:23:32 +04:00
|
|
|
}
|
|
|
|
|
2014-09-22 13:50:51 +04:00
|
|
|
return
|
|
|
|
}
|
2014-06-10 06:41:50 +04:00
|
|
|
|
2014-09-22 13:50:51 +04:00
|
|
|
// try to read events, if no events read, try to wait the new event singal until timeout seconds
|
|
|
|
func (l *Ledis) ReadLogsToTimeout(startLogID uint64, w io.Writer, timeout int) (n int, nextLogID uint64, err error) {
|
|
|
|
n, nextLogID, err = l.ReadLogsTo(startLogID, w)
|
|
|
|
if err != nil {
|
2014-06-10 06:41:50 +04:00
|
|
|
return
|
2014-09-23 13:28:09 +04:00
|
|
|
} else if n != 0 {
|
2014-06-09 13:23:32 +04:00
|
|
|
return
|
|
|
|
}
|
2014-09-22 13:50:51 +04:00
|
|
|
//no events read
|
|
|
|
select {
|
2014-09-23 13:28:09 +04:00
|
|
|
case <-l.r.WaitLog():
|
2014-09-22 13:50:51 +04:00
|
|
|
case <-time.After(time.Duration(timeout) * time.Second):
|
2014-06-04 10:42:02 +04:00
|
|
|
}
|
2014-09-22 13:50:51 +04:00
|
|
|
return l.ReadLogsTo(startLogID, w)
|
2014-09-23 13:28:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Ledis) NextSyncLogID() (uint64, error) {
|
|
|
|
if !l.ReplicationUsed() {
|
|
|
|
return 0, ErrRplNotSupport
|
|
|
|
}
|
2014-06-04 10:42:02 +04:00
|
|
|
|
2014-09-23 13:28:09 +04:00
|
|
|
s, err := l.r.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.LastID > s.CommitID {
|
|
|
|
return s.LastID + 1, nil
|
|
|
|
} else {
|
|
|
|
return s.CommitID + 1, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Ledis) propagate(rl *rpl.Log) {
|
|
|
|
for _, h := range l.rhs {
|
|
|
|
h(rl)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type NewLogEventHandler func(rl *rpl.Log)
|
|
|
|
|
|
|
|
func (l *Ledis) AddNewLogEventHandler(h NewLogEventHandler) error {
|
|
|
|
if !l.ReplicationUsed() {
|
|
|
|
return ErrRplNotSupport
|
|
|
|
}
|
|
|
|
|
|
|
|
l.rhs = append(l.rhs, h)
|
|
|
|
|
|
|
|
return nil
|
2014-06-04 10:42:02 +04:00
|
|
|
}
|
2014-09-23 13:53:52 +04:00
|
|
|
|
|
|
|
func (l *Ledis) ReplicationStat() (*rpl.Stat, error) {
|
|
|
|
if !l.ReplicationUsed() {
|
|
|
|
return nil, ErrRplNotSupport
|
|
|
|
}
|
|
|
|
|
|
|
|
return l.r.Stat()
|
|
|
|
}
|