From 848fbf34ad2278504442aa57beda6875ee2267af Mon Sep 17 00:00:00 2001 From: siddontang Date: Thu, 9 Oct 2014 15:05:15 +0800 Subject: [PATCH] add sync log config --- config/config.go | 3 ++- config/config.toml | 12 +++++++++--- etc/ledis.conf | 12 +++++++++--- rpl/goleveldb_store.go | 14 +++++++++++++- 4 files changed, 33 insertions(+), 8 deletions(-) diff --git a/config/config.go b/config/config.go index a90e40c..86e54b4 100644 --- a/config/config.go +++ b/config/config.go @@ -37,10 +37,11 @@ type LMDBConfig struct { type ReplicationConfig struct { Path string `toml:"path"` - ExpiredLogDays int `toml:"expired_log_days"` Sync bool `toml:"sync"` WaitSyncTime int `toml:"wait_sync_time"` WaitMaxSlaveAcks int `toml:"wait_max_slave_acks"` + ExpiredLogDays int `toml:"expired_log_days"` + SyncLog int `toml:"sync_log"` Compression bool `toml:"compression"` } diff --git a/config/config.toml b/config/config.toml index b8d80ec..0889933 100644 --- a/config/config.toml +++ b/config/config.toml @@ -50,9 +50,6 @@ nosync = true # if not set, use data_dir/rpl path = "" -# Expire write ahead logs after the given days -expired_log_days = 7 - # If sync is true, the new log must be sent to some slaves, and then commit. # It will reduce performance but have better high availability. sync = true @@ -65,5 +62,14 @@ wait_sync_time = 1 # If 0, wait (n + 1) / 2 acks. wait_max_slave_acks = 2 +# Expire write ahead logs after the given days +expired_log_days = 7 + +# Sync log to disk if possible +# 0: no sync +# 1: sync every second +# 2: sync every commit +sync_log = 0 + # Compress the log or not compression = true diff --git a/etc/ledis.conf b/etc/ledis.conf index b8d80ec..0889933 100644 --- a/etc/ledis.conf +++ b/etc/ledis.conf @@ -50,9 +50,6 @@ nosync = true # if not set, use data_dir/rpl path = "" -# Expire write ahead logs after the given days -expired_log_days = 7 - # If sync is true, the new log must be sent to some slaves, and then commit. # It will reduce performance but have better high availability. sync = true @@ -65,5 +62,14 @@ wait_sync_time = 1 # If 0, wait (n + 1) / 2 acks. wait_max_slave_acks = 2 +# Expire write ahead logs after the given days +expired_log_days = 7 + +# Sync log to disk if possible +# 0: no sync +# 1: sync every second +# 2: sync every commit +sync_log = 0 + # Compress the log or not compression = true diff --git a/rpl/goleveldb_store.go b/rpl/goleveldb_store.go index f9d2a7e..1c70603 100644 --- a/rpl/goleveldb_store.go +++ b/rpl/goleveldb_store.go @@ -21,6 +21,8 @@ type GoLevelDBStore struct { first uint64 last uint64 + + lastCommit time.Time } func (s *GoLevelDBStore) FirstID() (uint64, error) { @@ -132,7 +134,17 @@ func (s *GoLevelDBStore) StoreLogs(logs []*Log) error { w.Put(key, buf.Bytes()) } - if err := w.Commit(); err != nil { + n := time.Now() + if s.cfg.Replication.SyncLog == 2 || + (s.cfg.Replication.SyncLog == 1 && n.Sub(s.lastCommit) > time.Second) { + err = w.SyncCommit() + } else { + err = w.Commit() + } + + s.lastCommit = n + + if err != nil { return err }