ledisdb/rpl/file_table.go

572 lines
10 KiB
Go
Raw Normal View History

2014-11-05 12:34:58 +03:00
package rpl
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"path"
"sync"
"time"
2015-05-04 17:42:28 +03:00
"github.com/siddontang/go/log"
"github.com/siddontang/go/sync2"
2014-11-05 12:34:58 +03:00
)
var (
magic = []byte("\x1c\x1d\xb8\x88\xff\x9e\x45\x55\x40\xf0\x4c\xda\xe0\xce\x47\xde\x65\x48\x71\x17")
errTableNeedFlush = errors.New("write table need flush")
2014-11-11 10:20:26 +03:00
errNilHandler = errors.New("nil write handler")
2014-11-05 12:34:58 +03:00
)
const tableReaderKeepaliveInterval int64 = 30
func fmtTableDataName(base string, index int64) string {
return path.Join(base, fmt.Sprintf("%08d.data", index))
2014-11-05 12:34:58 +03:00
}
func fmtTableMetaName(base string, index int64) string {
return path.Join(base, fmt.Sprintf("%08d.meta", index))
}
2014-11-05 12:34:58 +03:00
type tableReader struct {
sync.Mutex
base string
2014-11-05 12:34:58 +03:00
index int64
data readFile
meta readFile
2014-11-05 12:34:58 +03:00
first uint64
last uint64
2014-11-06 10:05:21 +03:00
lastTime uint32
2014-11-05 12:34:58 +03:00
lastReadTime sync2.AtomicInt64
useMmap bool
2014-11-05 12:34:58 +03:00
}
2014-11-20 17:28:08 +03:00
func newTableReader(base string, index int64, useMmap bool) (*tableReader, error) {
2014-11-06 16:52:18 +03:00
if index <= 0 {
return nil, fmt.Errorf("invalid index %d", index)
}
2014-11-05 12:34:58 +03:00
t := new(tableReader)
t.base = base
2014-11-06 10:05:21 +03:00
t.index = index
2014-11-05 12:34:58 +03:00
t.useMmap = useMmap
2014-11-05 12:34:58 +03:00
var err error
if err = t.check(); err != nil {
2015-01-12 05:32:03 +03:00
log.Errorf("check %d error: %s, try to repair", t.index, err.Error())
2014-11-05 12:34:58 +03:00
if err = t.repair(); err != nil {
2015-01-12 05:32:03 +03:00
log.Errorf("repair %d error: %s", t.index, err.Error())
2014-11-05 12:34:58 +03:00
return nil, err
}
}
t.close()
return t, nil
}
func (t *tableReader) String() string {
return fmt.Sprintf("%d", t.index)
}
2014-11-05 12:34:58 +03:00
func (t *tableReader) Close() {
t.Lock()
t.close()
t.Unlock()
2014-11-05 12:34:58 +03:00
}
func (t *tableReader) close() {
if t.data != nil {
t.data.Close()
t.data = nil
2014-11-05 12:34:58 +03:00
}
if t.meta != nil {
t.meta.Close()
t.meta = nil
2014-11-05 12:34:58 +03:00
}
}
func (t *tableReader) Keepalived() bool {
l := t.lastReadTime.Get()
if l > 0 && time.Now().Unix()-l > tableReaderKeepaliveInterval {
return false
}
return true
}
func (t *tableReader) getLogPos(index int) (uint32, error) {
var buf [4]byte
if _, err := t.meta.ReadAt(buf[0:4], int64(index)*4); err != nil {
return 0, err
}
2014-11-06 14:35:13 +03:00
return binary.BigEndian.Uint32(buf[0:4]), nil
}
func (t *tableReader) checkData() error {
2014-11-05 12:34:58 +03:00
var err error
2014-11-20 17:28:08 +03:00
//check will use raw file mode
if t.data, err = newReadFile(false, fmtTableDataName(t.base, t.index)); err != nil {
2014-11-05 12:34:58 +03:00
return err
}
if t.data.Size() < len(magic) {
return fmt.Errorf("data file %s size %d too short", t.data.Name(), t.data.Size())
2014-11-05 12:34:58 +03:00
}
buf := make([]byte, len(magic))
if _, err := t.data.ReadAt(buf, int64(t.data.Size()-len(magic))); err != nil {
2014-11-05 12:34:58 +03:00
return err
}
if !bytes.Equal(magic, buf) {
return fmt.Errorf("data file %s invalid magic data %q", t.data.Name(), buf)
2014-11-05 12:34:58 +03:00
}
return nil
}
func (t *tableReader) checkMeta() error {
var err error
2014-11-20 17:28:08 +03:00
//check will use raw file mode
if t.meta, err = newReadFile(false, fmtTableMetaName(t.base, t.index)); err != nil {
2014-11-05 12:34:58 +03:00
return err
}
if t.meta.Size()%4 != 0 || t.meta.Size() == 0 {
return fmt.Errorf("meta file %s invalid offset len %d, must 4 multiple and not 0", t.meta.Name(), t.meta.Size())
}
return nil
}
func (t *tableReader) check() error {
var err error
if err := t.checkMeta(); err != nil {
2014-11-05 12:34:58 +03:00
return err
}
if err := t.checkData(); err != nil {
2014-11-05 12:34:58 +03:00
return err
}
firstLogPos, _ := t.getLogPos(0)
lastLogPos, _ := t.getLogPos(t.meta.Size()/4 - 1)
2014-11-05 12:34:58 +03:00
if firstLogPos != 0 {
return fmt.Errorf("invalid first log pos %d, must 0", firstLogPos)
}
var l Log
if _, err = t.decodeLogHead(&l, t.data, int64(firstLogPos)); err != nil {
2014-11-05 12:34:58 +03:00
return fmt.Errorf("decode first log err %s", err.Error())
}
t.first = l.ID
var n int64
if n, err = t.decodeLogHead(&l, t.data, int64(lastLogPos)); err != nil {
2014-11-05 12:34:58 +03:00
return fmt.Errorf("decode last log err %s", err.Error())
} else if n+int64(len(magic)) != int64(t.data.Size()) {
return fmt.Errorf("extra log data at offset %d", n)
2014-11-05 12:34:58 +03:00
}
t.last = l.ID
2014-11-06 10:05:21 +03:00
t.lastTime = l.CreateTime
2014-11-05 12:34:58 +03:00
if t.first > t.last {
return fmt.Errorf("invalid log table first %d > last %d", t.first, t.last)
} else if (t.last - t.first + 1) != uint64(t.meta.Size()/4) {
return fmt.Errorf("invalid log table, first %d, last %d, and log num %d", t.first, t.last, t.meta.Size()/4)
2014-11-05 12:34:58 +03:00
}
return nil
}
func (t *tableReader) repair() error {
t.close()
2014-11-05 17:35:43 +03:00
var err error
var data writeFile
var meta writeFile
2014-11-06 08:46:02 +03:00
2014-11-20 17:28:08 +03:00
//repair will use raw file mode
data, err = newWriteFile(false, fmtTableDataName(t.base, t.index), 0)
data.SetOffset(int64(data.Size()))
2014-11-05 17:35:43 +03:00
2014-11-20 17:28:08 +03:00
meta, err = newWriteFile(false, fmtTableMetaName(t.base, t.index), int64(defaultLogNumInFile*4))
2014-11-05 17:35:43 +03:00
var l Log
var pos int64
var nextPos int64
b := make([]byte, 4)
t.first = 0
t.last = 0
2014-11-05 17:35:43 +03:00
for {
nextPos, err = t.decodeLogHead(&l, data, pos)
if err != nil {
//if error, we may lost all logs from pos
2015-01-12 05:32:03 +03:00
log.Errorf("%s may lost logs from %d", data.Name(), pos)
2014-11-11 10:20:26 +03:00
break
}
if l.ID == 0 {
2015-01-12 05:32:03 +03:00
log.Errorf("%s may lost logs from %d, invalid log 0", data.Name(), pos)
break
2014-11-05 17:35:43 +03:00
}
if t.first == 0 {
t.first = l.ID
}
if t.last == 0 {
t.last = l.ID
} else if l.ID <= t.last {
2015-01-12 05:32:03 +03:00
log.Errorf("%s may lost logs from %d, invalid logid %d", t.data.Name(), pos, l.ID)
2014-11-05 17:35:43 +03:00
break
}
t.last = l.ID
2014-11-06 10:05:21 +03:00
t.lastTime = l.CreateTime
binary.BigEndian.PutUint32(b, uint32(pos))
meta.Write(b)
2014-11-05 17:35:43 +03:00
pos = nextPos
2014-11-05 17:35:43 +03:00
t.lastTime = l.CreateTime
2014-11-05 17:35:43 +03:00
}
var e error
2014-11-20 17:28:08 +03:00
if err := meta.Close(); err != nil {
e = err
}
2014-11-06 08:46:02 +03:00
data.SetOffset(pos)
2014-11-05 17:35:43 +03:00
2014-11-20 17:28:08 +03:00
if _, err = data.Write(magic); err != nil {
2015-01-12 05:32:03 +03:00
log.Errorf("write magic error %s", err.Error())
2014-11-20 17:28:08 +03:00
}
if err = data.Close(); err != nil {
2014-11-05 17:35:43 +03:00
return err
}
return e
2014-11-05 12:34:58 +03:00
}
func (t *tableReader) decodeLogHead(l *Log, r io.ReaderAt, pos int64) (int64, error) {
dataLen, err := l.DecodeHeadAt(r, pos)
2014-11-05 12:34:58 +03:00
if err != nil {
return 0, err
}
return pos + int64(l.HeadSize()) + int64(dataLen), nil
}
2014-11-06 08:46:02 +03:00
func (t *tableReader) GetLog(id uint64, l *Log) error {
2014-11-05 12:34:58 +03:00
if id < t.first || id > t.last {
2014-11-07 11:35:54 +03:00
return ErrLogNotFound
2014-11-05 12:34:58 +03:00
}
t.lastReadTime.Set(time.Now().Unix())
t.Lock()
if err := t.openTable(); err != nil {
t.close()
t.Unlock()
2014-11-05 12:34:58 +03:00
return err
}
t.Unlock()
2014-11-05 12:34:58 +03:00
pos, err := t.getLogPos(int(id - t.first))
if err != nil {
return err
}
2014-11-05 12:34:58 +03:00
if err := l.DecodeAt(t.data, int64(pos)); err != nil {
return err
2014-11-05 12:34:58 +03:00
} else if l.ID != id {
return fmt.Errorf("invalid log id %d != %d", l.ID, id)
}
return nil
}
func (t *tableReader) openTable() error {
var err error
if t.data == nil {
if t.data, err = newReadFile(t.useMmap, fmtTableDataName(t.base, t.index)); err != nil {
return err
2014-11-05 12:34:58 +03:00
}
}
if t.meta == nil {
if t.meta, err = newReadFile(t.useMmap, fmtTableMetaName(t.base, t.index)); err != nil {
2014-11-06 11:06:27 +03:00
return err
2014-11-05 12:34:58 +03:00
}
2014-11-05 12:34:58 +03:00
}
return nil
}
type tableWriter struct {
sync.RWMutex
data writeFile
meta writeFile
2014-11-05 12:34:58 +03:00
base string
index int64
first uint64
last uint64
lastTime uint32
2014-11-05 12:34:58 +03:00
maxLogSize int64
2014-11-07 11:35:54 +03:00
closed bool
syncType int
2014-11-12 06:52:10 +03:00
posBuf []byte
useMmap bool
2014-11-05 12:34:58 +03:00
}
2014-11-20 17:28:08 +03:00
func newTableWriter(base string, index int64, maxLogSize int64, useMmap bool) *tableWriter {
2014-11-06 16:52:18 +03:00
if index <= 0 {
panic(fmt.Errorf("invalid index %d", index))
}
2014-11-05 12:34:58 +03:00
t := new(tableWriter)
t.base = base
t.index = index
t.maxLogSize = maxLogSize
2014-11-07 11:35:54 +03:00
t.closed = false
t.posBuf = make([]byte, 4)
t.useMmap = useMmap
2014-11-12 06:52:10 +03:00
2014-11-05 17:35:43 +03:00
return t
2014-11-05 12:34:58 +03:00
}
func (t *tableWriter) String() string {
return fmt.Sprintf("%d", t.index)
}
2014-11-07 11:35:54 +03:00
func (t *tableWriter) SetMaxLogSize(s int64) {
t.maxLogSize = s
}
func (t *tableWriter) SetSyncType(tp int) {
t.syncType = tp
}
2014-11-05 12:34:58 +03:00
func (t *tableWriter) close() {
if t.meta != nil {
2014-11-20 17:28:08 +03:00
if err := t.meta.Close(); err != nil {
2015-01-12 05:32:03 +03:00
log.Fatalf("close log meta error %s", err.Error())
}
t.meta = nil
2014-11-05 12:34:58 +03:00
}
if t.data != nil {
2014-11-20 17:28:08 +03:00
if _, err := t.data.Write(magic); err != nil {
2015-01-12 05:32:03 +03:00
log.Fatalf("write magic error %s", err.Error())
2014-11-20 17:28:08 +03:00
}
if err := t.data.Close(); err != nil {
2015-01-12 05:32:03 +03:00
log.Fatalf("close log data error %s", err.Error())
}
t.data = nil
2014-11-05 12:34:58 +03:00
}
}
func (t *tableWriter) Close() {
t.Lock()
2014-11-07 11:35:54 +03:00
t.closed = true
2014-11-05 12:34:58 +03:00
t.close()
t.Unlock()
2014-11-05 12:34:58 +03:00
}
2014-11-06 16:52:18 +03:00
func (t *tableWriter) First() uint64 {
t.Lock()
id := t.first
t.Unlock()
return id
}
func (t *tableWriter) Last() uint64 {
t.Lock()
id := t.last
t.Unlock()
return id
}
2014-11-05 12:34:58 +03:00
func (t *tableWriter) Flush() (*tableReader, error) {
t.Lock()
if t.data == nil || t.meta == nil {
t.Unlock()
2014-11-11 10:20:26 +03:00
return nil, errNilHandler
2014-11-05 12:34:58 +03:00
}
tr := new(tableReader)
tr.base = t.base
2014-11-05 12:34:58 +03:00
tr.index = t.index
tr.first = t.first
tr.last = t.last
tr.lastTime = t.lastTime
2014-11-20 17:28:08 +03:00
tr.useMmap = t.useMmap
2014-11-05 12:34:58 +03:00
t.close()
2014-11-05 12:34:58 +03:00
t.first = 0
t.last = 0
t.index = t.index + 1
2014-11-05 12:34:58 +03:00
t.Unlock()
2014-11-05 12:34:58 +03:00
return tr, nil
}
func (t *tableWriter) StoreLog(l *Log) error {
t.Lock()
err := t.storeLog(l)
t.Unlock()
return err
}
func (t *tableWriter) openFile() error {
var err error
if t.data == nil {
if t.data, err = newWriteFile(t.useMmap, fmtTableDataName(t.base, t.index), t.maxLogSize+t.maxLogSize/10+int64(len(magic))); err != nil {
return err
}
}
if t.meta == nil {
if t.meta, err = newWriteFile(t.useMmap, fmtTableMetaName(t.base, t.index), int64(defaultLogNumInFile*4)); err != nil {
return err
}
}
return err
}
func (t *tableWriter) storeLog(l *Log) error {
2014-11-06 08:46:02 +03:00
if l.ID == 0 {
return ErrStoreLogID
}
2014-11-07 11:35:54 +03:00
if t.closed {
return fmt.Errorf("table writer is closed")
}
2014-11-05 12:34:58 +03:00
if t.last > 0 && l.ID != t.last+1 {
return ErrStoreLogID
}
if t.data != nil && t.data.Offset() > t.maxLogSize {
2014-11-05 12:34:58 +03:00
return errTableNeedFlush
}
var err error
if err = t.openFile(); err != nil {
return err
2014-11-05 12:34:58 +03:00
}
offsetPos := t.data.Offset()
if err = l.Encode(t.data); err != nil {
2014-11-18 17:27:39 +03:00
return err
}
binary.BigEndian.PutUint32(t.posBuf, uint32(offsetPos))
if _, err = t.meta.Write(t.posBuf); err != nil {
2014-11-05 12:34:58 +03:00
return err
}
if t.first == 0 {
t.first = l.ID
}
t.last = l.ID
2014-11-07 11:35:54 +03:00
t.lastTime = l.CreateTime
2014-11-11 10:20:26 +03:00
if t.syncType == 2 {
if err := t.data.Sync(); err != nil {
2015-01-12 05:32:03 +03:00
log.Errorf("sync table error %s", err.Error())
2014-11-07 11:35:54 +03:00
}
}
2014-11-05 12:34:58 +03:00
return nil
}
func (t *tableWriter) GetLog(id uint64, l *Log) error {
t.RLock()
defer t.RUnlock()
2014-11-06 08:46:02 +03:00
if id < t.first || id > t.last {
2014-11-07 11:35:54 +03:00
return ErrLogNotFound
2014-11-05 12:34:58 +03:00
}
var buf [4]byte
if _, err := t.meta.ReadAt(buf[0:4], int64((id-t.first)*4)); err != nil {
return err
}
2014-11-05 12:34:58 +03:00
offset := binary.BigEndian.Uint32(buf[0:4])
2014-11-05 12:34:58 +03:00
if err := l.DecodeAt(t.data, int64(offset)); err != nil {
2014-11-05 12:34:58 +03:00
return err
} else if l.ID != id {
return fmt.Errorf("invalid log id %d != %d", id, l.ID)
}
return nil
}
2014-11-11 10:20:26 +03:00
func (t *tableWriter) Sync() error {
t.Lock()
var err error
if t.data != nil {
err = t.data.Sync()
t.Unlock()
2014-11-05 12:34:58 +03:00
return err
}
if t.meta != nil {
err = t.meta.Sync()
2014-11-05 12:34:58 +03:00
}
t.Unlock()
return err
2014-11-05 12:34:58 +03:00
}