2014-05-20 04:41:24 +04:00
|
|
|
package ledis
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-09-24 11:51:09 +04:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path"
|
2014-05-29 11:07:14 +04:00
|
|
|
"sync"
|
2014-06-12 06:51:36 +04:00
|
|
|
"time"
|
2015-05-04 17:42:28 +03:00
|
|
|
|
|
|
|
"github.com/siddontang/go/filelock"
|
|
|
|
"github.com/siddontang/go/log"
|
|
|
|
"github.com/siddontang/ledisdb/config"
|
|
|
|
"github.com/siddontang/ledisdb/rpl"
|
|
|
|
"github.com/siddontang/ledisdb/store"
|
2014-05-20 04:41:24 +04:00
|
|
|
)
|
|
|
|
|
2018-03-29 15:33:36 +03:00
|
|
|
// Ledis is the core structure to handle the database.
|
2014-05-20 04:41:24 +04:00
|
|
|
type Ledis struct {
|
2014-08-07 12:49:48 +04:00
|
|
|
cfg *config.Config
|
2014-05-20 04:41:24 +04:00
|
|
|
|
2014-07-25 13:58:00 +04:00
|
|
|
ldb *store.DB
|
2015-03-15 06:39:31 +03:00
|
|
|
|
|
|
|
dbLock sync.Mutex
|
|
|
|
dbs map[int]*DB
|
2014-05-27 12:05:24 +04:00
|
|
|
|
2014-06-05 11:59:10 +04:00
|
|
|
quit chan struct{}
|
2014-09-22 13:50:51 +04:00
|
|
|
wg sync.WaitGroup
|
2014-08-25 10:18:23 +04:00
|
|
|
|
2014-09-23 13:28:09 +04:00
|
|
|
//for replication
|
2015-06-18 16:47:35 +03:00
|
|
|
r *rpl.Replication
|
|
|
|
rc chan struct{}
|
|
|
|
rbatch *store.WriteBatch
|
|
|
|
rDoneCh chan struct{}
|
|
|
|
rhs []NewLogEventHandler
|
2014-08-30 13:39:44 +04:00
|
|
|
|
|
|
|
wLock sync.RWMutex //allow one write at same time
|
|
|
|
commitLock sync.Mutex //allow one write commit at same time
|
2014-09-17 13:54:04 +04:00
|
|
|
|
2014-09-24 11:51:09 +04:00
|
|
|
lock io.Closer
|
2014-10-29 11:02:46 +03:00
|
|
|
|
2015-03-15 06:39:31 +03:00
|
|
|
ttlCheckers []*ttlChecker
|
|
|
|
ttlCheckerCh chan *ttlChecker
|
2014-05-20 04:41:24 +04:00
|
|
|
}
|
|
|
|
|
2018-03-29 15:33:36 +03:00
|
|
|
// Open opens the Ledis with a config.
|
2014-08-07 12:49:48 +04:00
|
|
|
func Open(cfg *config.Config) (*Ledis, error) {
|
2014-06-06 06:34:57 +04:00
|
|
|
if len(cfg.DataDir) == 0 {
|
2014-08-07 12:49:48 +04:00
|
|
|
cfg.DataDir = config.DefaultDataDir
|
2014-06-06 06:34:57 +04:00
|
|
|
}
|
|
|
|
|
2015-03-04 04:15:28 +03:00
|
|
|
if cfg.Databases == 0 {
|
|
|
|
cfg.Databases = 16
|
2015-03-16 08:39:15 +03:00
|
|
|
} else if cfg.Databases > MaxDatabases {
|
|
|
|
cfg.Databases = MaxDatabases
|
2015-03-04 04:15:28 +03:00
|
|
|
}
|
|
|
|
|
2014-09-24 11:51:09 +04:00
|
|
|
os.MkdirAll(cfg.DataDir, 0755)
|
|
|
|
|
|
|
|
var err error
|
2014-05-20 04:41:24 +04:00
|
|
|
|
|
|
|
l := new(Ledis)
|
2014-10-10 05:49:16 +04:00
|
|
|
l.cfg = cfg
|
2014-06-05 11:59:10 +04:00
|
|
|
|
2014-09-24 11:51:09 +04:00
|
|
|
if l.lock, err = filelock.Lock(path.Join(cfg.DataDir, "LOCK")); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-06-05 11:59:10 +04:00
|
|
|
l.quit = make(chan struct{})
|
|
|
|
|
2014-09-24 11:51:09 +04:00
|
|
|
if l.ldb, err = store.Open(cfg); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-05-20 04:41:24 +04:00
|
|
|
|
2014-09-23 13:28:09 +04:00
|
|
|
if cfg.UseReplication {
|
2014-09-22 13:50:51 +04:00
|
|
|
if l.r, err = rpl.NewReplication(cfg); err != nil {
|
2014-05-27 12:05:24 +04:00
|
|
|
return nil, err
|
|
|
|
}
|
2014-09-22 13:50:51 +04:00
|
|
|
|
2014-09-29 13:01:58 +04:00
|
|
|
l.rc = make(chan struct{}, 1)
|
2014-09-22 13:50:51 +04:00
|
|
|
l.rbatch = l.ldb.NewWriteBatch()
|
2015-06-18 16:47:35 +03:00
|
|
|
l.rDoneCh = make(chan struct{}, 1)
|
2014-09-22 13:50:51 +04:00
|
|
|
|
2014-09-27 16:13:13 +04:00
|
|
|
l.wg.Add(1)
|
2014-09-22 13:50:51 +04:00
|
|
|
go l.onReplication()
|
2014-09-23 13:28:09 +04:00
|
|
|
|
|
|
|
//first we must try wait all replication ok
|
|
|
|
//maybe some logs are not committed
|
|
|
|
l.WaitReplication()
|
2014-05-27 12:05:24 +04:00
|
|
|
} else {
|
2014-09-22 13:50:51 +04:00
|
|
|
l.r = nil
|
2014-05-27 12:05:24 +04:00
|
|
|
}
|
|
|
|
|
2015-03-15 06:39:31 +03:00
|
|
|
l.dbs = make(map[int]*DB, 16)
|
2014-05-20 04:41:24 +04:00
|
|
|
|
2014-11-01 18:28:28 +03:00
|
|
|
l.checkTTL()
|
2014-06-12 06:51:36 +04:00
|
|
|
|
2014-05-20 04:41:24 +04:00
|
|
|
return l, nil
|
|
|
|
}
|
|
|
|
|
2018-03-29 15:33:36 +03:00
|
|
|
// Close closes the Ledis.
|
2014-05-20 04:41:24 +04:00
|
|
|
func (l *Ledis) Close() {
|
2014-06-05 11:59:10 +04:00
|
|
|
close(l.quit)
|
2014-09-22 13:50:51 +04:00
|
|
|
l.wg.Wait()
|
2014-06-05 11:59:10 +04:00
|
|
|
|
2014-05-20 04:41:24 +04:00
|
|
|
l.ldb.Close()
|
2014-06-05 11:59:10 +04:00
|
|
|
|
2014-09-22 13:50:51 +04:00
|
|
|
if l.r != nil {
|
|
|
|
l.r.Close()
|
2014-11-01 18:28:28 +03:00
|
|
|
//l.r = nil
|
2014-06-05 11:59:10 +04:00
|
|
|
}
|
2014-09-24 11:51:09 +04:00
|
|
|
|
|
|
|
if l.lock != nil {
|
|
|
|
l.lock.Close()
|
2014-11-01 18:28:28 +03:00
|
|
|
//l.lock = nil
|
2014-09-24 11:51:09 +04:00
|
|
|
}
|
2014-05-20 04:41:24 +04:00
|
|
|
}
|
|
|
|
|
2018-03-29 15:33:36 +03:00
|
|
|
// Select chooses a database.
|
2014-05-20 04:41:24 +04:00
|
|
|
func (l *Ledis) Select(index int) (*DB, error) {
|
2015-03-16 08:39:15 +03:00
|
|
|
if index < 0 || index >= l.cfg.Databases {
|
|
|
|
return nil, fmt.Errorf("invalid db index %d, must in [0, %d]", index, l.cfg.Databases-1)
|
2015-03-15 06:39:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
l.dbLock.Lock()
|
|
|
|
defer l.dbLock.Unlock()
|
|
|
|
|
|
|
|
db, ok := l.dbs[index]
|
|
|
|
if ok {
|
|
|
|
return db, nil
|
2014-05-20 04:41:24 +04:00
|
|
|
}
|
|
|
|
|
2015-03-15 06:39:31 +03:00
|
|
|
db = l.newDB(index)
|
|
|
|
l.dbs[index] = db
|
|
|
|
|
|
|
|
go func(db *DB) {
|
|
|
|
l.ttlCheckerCh <- db.ttlChecker
|
|
|
|
}(db)
|
|
|
|
|
|
|
|
return db, nil
|
2014-05-20 04:41:24 +04:00
|
|
|
}
|
2014-06-08 12:43:59 +04:00
|
|
|
|
2018-03-29 15:33:36 +03:00
|
|
|
// FlushAll will clear all data and replication logs
|
2014-06-08 12:43:59 +04:00
|
|
|
func (l *Ledis) FlushAll() error {
|
2014-09-23 13:28:09 +04:00
|
|
|
l.wLock.Lock()
|
|
|
|
defer l.wLock.Unlock()
|
|
|
|
|
|
|
|
return l.flushAll()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Ledis) flushAll() error {
|
|
|
|
it := l.ldb.NewIterator()
|
|
|
|
defer it.Close()
|
|
|
|
|
2015-03-13 06:18:47 +03:00
|
|
|
it.SeekToFirst()
|
|
|
|
|
2014-09-23 13:28:09 +04:00
|
|
|
w := l.ldb.NewWriteBatch()
|
|
|
|
defer w.Rollback()
|
|
|
|
|
|
|
|
n := 0
|
|
|
|
for ; it.Valid(); it.Next() {
|
|
|
|
n++
|
|
|
|
if n == 10000 {
|
|
|
|
if err := w.Commit(); err != nil {
|
2015-01-12 05:32:03 +03:00
|
|
|
log.Fatalf("flush all commit error: %s", err.Error())
|
2014-09-23 13:28:09 +04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
n = 0
|
|
|
|
}
|
|
|
|
w.Delete(it.RawKey())
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := w.Commit(); err != nil {
|
2015-01-12 05:32:03 +03:00
|
|
|
log.Fatalf("flush all commit error: %s", err.Error())
|
2014-09-23 13:28:09 +04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if l.r != nil {
|
|
|
|
if err := l.r.Clear(); err != nil {
|
2015-01-12 05:32:03 +03:00
|
|
|
log.Fatalf("flush all replication clear error: %s", err.Error())
|
2014-09-23 13:28:09 +04:00
|
|
|
return err
|
2014-06-08 12:43:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2014-06-10 06:41:50 +04:00
|
|
|
|
2018-03-29 15:33:36 +03:00
|
|
|
// IsReadOnly returns whether Ledis is read only or not.
|
2014-09-22 13:50:51 +04:00
|
|
|
func (l *Ledis) IsReadOnly() bool {
|
2014-11-01 18:28:28 +03:00
|
|
|
if l.cfg.GetReadonly() {
|
2014-09-22 13:50:51 +04:00
|
|
|
return true
|
|
|
|
} else if l.r != nil {
|
|
|
|
if b, _ := l.r.CommitIDBehind(); b {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-10-29 11:02:46 +03:00
|
|
|
func (l *Ledis) checkTTL() {
|
2015-03-15 06:39:31 +03:00
|
|
|
l.ttlCheckers = make([]*ttlChecker, 0, 16)
|
|
|
|
l.ttlCheckerCh = make(chan *ttlChecker, 16)
|
2014-06-12 06:51:36 +04:00
|
|
|
|
2014-10-29 11:02:46 +03:00
|
|
|
if l.cfg.TTLCheckInterval == 0 {
|
|
|
|
l.cfg.TTLCheckInterval = 1
|
|
|
|
}
|
2014-09-22 13:50:51 +04:00
|
|
|
|
2014-11-01 18:28:28 +03:00
|
|
|
l.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer l.wg.Done()
|
|
|
|
|
|
|
|
tick := time.NewTicker(time.Duration(l.cfg.TTLCheckInterval) * time.Second)
|
|
|
|
defer tick.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-tick.C:
|
|
|
|
if l.IsReadOnly() {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2015-03-15 06:39:31 +03:00
|
|
|
for _, c := range l.ttlCheckers {
|
2014-11-01 18:28:28 +03:00
|
|
|
c.check()
|
|
|
|
}
|
2015-03-15 06:39:31 +03:00
|
|
|
case c := <-l.ttlCheckerCh:
|
|
|
|
l.ttlCheckers = append(l.ttlCheckers, c)
|
|
|
|
c.check()
|
2014-11-01 18:28:28 +03:00
|
|
|
case <-l.quit:
|
|
|
|
return
|
2014-10-29 11:02:46 +03:00
|
|
|
}
|
2014-06-12 06:51:36 +04:00
|
|
|
}
|
2014-11-01 18:28:28 +03:00
|
|
|
|
|
|
|
}()
|
2014-06-12 06:51:36 +04:00
|
|
|
|
|
|
|
}
|
2014-10-15 06:41:43 +04:00
|
|
|
|
2018-03-29 15:33:36 +03:00
|
|
|
// StoreStat returns the statistics.
|
2014-10-15 06:41:43 +04:00
|
|
|
func (l *Ledis) StoreStat() *store.Stat {
|
|
|
|
return l.ldb.Stat()
|
|
|
|
}
|
2015-05-03 06:24:03 +03:00
|
|
|
|
2018-03-29 15:33:36 +03:00
|
|
|
// CompactStore compacts the backend storage.
|
2015-05-03 06:24:03 +03:00
|
|
|
func (l *Ledis) CompactStore() error {
|
|
|
|
l.wLock.Lock()
|
|
|
|
defer l.wLock.Unlock()
|
|
|
|
|
|
|
|
return l.ldb.Compact()
|
|
|
|
}
|