ledisdb/rpl/file_store.go

275 lines
4.9 KiB
Go
Raw Normal View History

2014-09-22 13:50:51 +04:00
package rpl
2014-09-15 18:42:25 +04:00
import (
2014-09-17 13:54:04 +04:00
"fmt"
2014-09-24 05:46:36 +04:00
"github.com/siddontang/go/log"
2014-11-05 17:35:43 +03:00
"github.com/siddontang/go/num"
2014-09-17 13:54:04 +04:00
"io/ioutil"
2014-09-15 18:42:25 +04:00
"os"
2014-11-06 16:52:18 +03:00
"sort"
2014-09-15 18:42:25 +04:00
"sync"
2014-11-06 16:52:18 +03:00
"time"
2014-09-15 18:42:25 +04:00
)
const (
2014-11-05 17:35:43 +03:00
defaultMaxLogFileSize = int64(1024 * 1024 * 1024)
2014-10-31 10:40:47 +03:00
//why 4G, we can use uint32 as offset, reduce memory useage
2014-11-05 17:35:43 +03:00
maxLogFileSize = int64(uint32(4*1024*1024*1024 - 1))
2014-11-05 12:34:14 +03:00
maxLogNumInFile = uint64(10000000)
2014-09-15 18:42:25 +04:00
)
2014-09-17 13:54:04 +04:00
/*
2014-10-31 10:40:47 +03:00
File Store:
2014-11-05 12:34:14 +03:00
00000001.ldb
00000002.ldb
2014-10-31 10:40:47 +03:00
2014-11-03 12:53:46 +03:00
log: log1 data | log2 data | split data | log1 offset | log 2 offset | offset start pos | offset length | magic data
2014-10-31 10:40:47 +03:00
2014-11-03 12:53:46 +03:00
log id can not be 0, we use here for split data
2014-11-03 06:22:13 +03:00
if data has no magic data, it means that we don't close replication gracefully.
so we must repair the log data
2014-11-05 12:34:14 +03:00
log data: id (bigendian uint64), create time (bigendian uint32), compression (byte), data len(bigendian uint32), data
split data = log0 data + [padding 0] -> file % pagesize() == 0
log0: id 0, create time 0, compression 0, data len 7, data "ledisdb"
2014-11-05 12:34:14 +03:00
log offset: bigendian uint32 | bigendian uint32
offset start pos: bigendian uint64
offset length: bigendian uint32
2014-11-03 12:53:46 +03:00
//sha1 of github.com/siddontang/ledisdb 20 bytes
magic data = "\x1c\x1d\xb8\x88\xff\x9e\x45\x55\x40\xf0\x4c\xda\xe0\xce\x47\xde\x65\x48\x71\x17"
2014-11-03 06:22:13 +03:00
we must guarantee that the log id is monotonic increment strictly.
if log1's id is 1, log2 must be 2
2014-09-17 13:54:04 +04:00
*/
2014-09-15 18:42:25 +04:00
type FileStore struct {
2014-09-22 13:50:51 +04:00
LogStore
2014-09-15 18:42:25 +04:00
2014-11-05 17:35:43 +03:00
maxFileSize int64
2014-09-15 18:42:25 +04:00
2014-11-06 16:52:18 +03:00
base string
2014-09-17 13:54:04 +04:00
2014-11-06 16:52:18 +03:00
rm sync.RWMutex
wm sync.Mutex
2014-09-17 13:54:04 +04:00
2014-11-06 16:52:18 +03:00
rs tableReaders
w *tableWriter
2014-09-15 18:42:25 +04:00
}
2014-11-06 16:52:18 +03:00
func NewFileStore(base string, maxSize int64) (*FileStore, error) {
2014-09-15 18:42:25 +04:00
s := new(FileStore)
2014-11-06 16:52:18 +03:00
var err error
if err = os.MkdirAll(base, 0755); err != nil {
2014-09-15 18:42:25 +04:00
return nil, err
}
2014-11-06 16:52:18 +03:00
s.base = base
2014-09-15 18:42:25 +04:00
2014-11-06 16:52:18 +03:00
s.maxFileSize = num.MinInt64(maxLogFileSize, maxSize)
2014-09-15 18:42:25 +04:00
2014-11-06 16:52:18 +03:00
if err = s.load(); err != nil {
2014-09-17 13:54:04 +04:00
return nil, err
}
2014-11-06 16:52:18 +03:00
index := int64(1)
if len(s.rs) != 0 {
index = s.rs[len(s.rs)-1].index + 1
}
2014-09-15 18:42:25 +04:00
2014-11-06 16:52:18 +03:00
s.w = newTableWriter(s.base, index, s.maxFileSize)
return s, nil
2014-09-15 18:42:25 +04:00
}
func (s *FileStore) GetLog(id uint64, log *Log) error {
2014-09-22 13:50:51 +04:00
panic("not implementation")
2014-09-15 18:42:25 +04:00
return nil
}
func (s *FileStore) FirstID() (uint64, error) {
return 0, nil
}
func (s *FileStore) LastID() (uint64, error) {
return 0, nil
}
2014-11-06 16:52:18 +03:00
func (s *FileStore) StoreLog(l *Log) error {
s.wm.Lock()
defer s.wm.Unlock()
if s.w == nil {
return fmt.Errorf("nil table writer, cannot store")
}
err := s.w.StoreLog(l)
if err == nil {
return nil
} else if err != errTableNeedFlush {
return err
}
var r *tableReader
if r, err = s.w.Flush(); err != nil {
log.Error("write table flush error %s, can not store now", err.Error())
s.w.Close()
s.w = nil
return err
}
s.rm.Lock()
s.rs = append(s.rs, r)
s.rm.Unlock()
2014-09-15 18:42:25 +04:00
2014-09-17 13:54:04 +04:00
return nil
}
2014-09-22 13:50:51 +04:00
func (s *FileStore) PuregeExpired(n int64) error {
2014-11-06 16:52:18 +03:00
s.rm.Lock()
2014-09-15 18:42:25 +04:00
2014-11-06 16:52:18 +03:00
purges := []*tableReader{}
lefts := []*tableReader{}
2014-09-15 18:42:25 +04:00
2014-11-06 16:52:18 +03:00
t := uint32(time.Now().Unix() - int64(n))
2014-09-17 13:54:04 +04:00
2014-11-06 16:52:18 +03:00
for _, r := range s.rs {
if r.lastTime < t {
purges = append(purges, r)
} else {
lefts = append(lefts, r)
}
}
2014-09-17 13:54:04 +04:00
2014-11-06 16:52:18 +03:00
s.rs = lefts
s.rm.Unlock()
for _, r := range purges {
name := r.name
r.Close()
if err := os.Remove(name); err != nil {
log.Error("purge table %s err: %s", name, err.Error())
}
2014-09-17 13:54:04 +04:00
}
return nil
}
2014-11-06 16:52:18 +03:00
func (s *FileStore) Clear() error {
return nil
2014-09-17 13:54:04 +04:00
}
2014-11-06 16:52:18 +03:00
func (s *FileStore) Close() error {
s.wm.Lock()
if s.w != nil {
if r, err := s.w.Flush(); err != nil {
log.Error("close err: %s", err.Error())
} else {
r.Close()
s.w.Close()
s.w = nil
2014-09-17 13:54:04 +04:00
}
}
2014-11-06 16:52:18 +03:00
s.wm.Unlock()
2014-09-17 13:54:04 +04:00
2014-11-06 16:52:18 +03:00
s.rm.Lock()
2014-09-17 13:54:04 +04:00
2014-11-06 16:52:18 +03:00
for i := range s.rs {
s.rs[i].Close()
2014-09-17 13:54:04 +04:00
}
2014-11-06 16:52:18 +03:00
s.rs = nil
s.rm.Unlock()
2014-09-17 13:54:04 +04:00
return nil
}
2014-11-06 16:52:18 +03:00
func (s *FileStore) load() error {
fs, err := ioutil.ReadDir(s.base)
if err != nil {
2014-09-17 13:54:04 +04:00
return err
}
2014-11-06 16:52:18 +03:00
var r *tableReader
var index int64
for _, f := range fs {
if _, err := fmt.Sscanf(f.Name(), "%08d.ldb", &index); err == nil {
if r, err = newTableReader(s.base, index); err != nil {
log.Error("load table %s err: %s", f.Name(), err.Error())
} else {
s.rs = append(s.rs, r)
}
}
}
2014-09-17 13:54:04 +04:00
2014-11-06 16:52:18 +03:00
if err := s.rs.check(); err != nil {
2014-09-17 13:54:04 +04:00
return err
}
return nil
}
2014-11-06 16:52:18 +03:00
type tableReaders []*tableReader
func (ts tableReaders) Len() int {
return len(ts)
}
func (ts tableReaders) Swap(i, j int) {
ts[i], ts[j] = ts[j], ts[i]
}
func (ts tableReaders) Less(i, j int) bool {
return ts[i].index < ts[j].index
}
func (ts tableReaders) Search(id uint64) *tableReader {
n := sort.Search(len(ts), func(i int) bool {
return id >= ts[i].first && id <= ts[i].last
})
if n < len(ts) {
return ts[n]
} else {
return nil
2014-09-17 13:54:04 +04:00
}
2014-11-06 16:52:18 +03:00
}
2014-09-17 13:54:04 +04:00
2014-11-06 16:52:18 +03:00
func (ts tableReaders) check() error {
if len(ts) == 0 {
return nil
2014-09-17 13:54:04 +04:00
}
2014-11-06 16:52:18 +03:00
sort.Sort(ts)
first := ts[0].first
last := ts[0].last
index := ts[0].index
2014-09-17 13:54:04 +04:00
2014-11-06 16:52:18 +03:00
if first == 0 || first > last {
return fmt.Errorf("invalid log in table %s", ts[0].name)
2014-09-17 13:54:04 +04:00
}
2014-11-06 16:52:18 +03:00
for i := 1; i < len(ts); i++ {
if ts[i].first <= last {
return fmt.Errorf("invalid first log id %d in table %s", ts[i].first, ts[i].name)
}
2014-09-17 13:54:04 +04:00
2014-11-06 16:52:18 +03:00
if ts[i].index == index {
return fmt.Errorf("invalid index %d in table %s", ts[i].index, ts[i].name)
}
2014-09-17 13:54:04 +04:00
2014-11-06 16:52:18 +03:00
first = ts[i].first
last = ts[i].last
index = ts[i].index
}
return nil
2014-09-17 13:54:04 +04:00
}