diff --git a/cmd/ledis-server/main.go b/cmd/ledis-server/main.go index c03bb84..e132506 100644 --- a/cmd/ledis-server/main.go +++ b/cmd/ledis-server/main.go @@ -20,6 +20,7 @@ var usePprof = flag.Bool("pprof", false, "enable pprof") var pprofPort = flag.Int("pprof_port", 6060, "pprof http port") var slaveof = flag.String("slaveof", "", "make the server a slave of another instance") var readonly = flag.Bool("readonly", false, "set readonly mode, salve server is always readonly") +var rpl = flag.Bool("rpl", false, "enable replication or not, slave server is always enabled") func main() { runtime.GOMAXPROCS(runtime.NumCPU()) @@ -48,8 +49,10 @@ func main() { if len(*slaveof) > 0 { cfg.SlaveOf = *slaveof cfg.Readonly = true + cfg.UseReplication = true } else { cfg.Readonly = *readonly + cfg.UseReplication = *rpl } var app *server.App diff --git a/config/config.go b/config/config.go index ed0c2f2..23a33de 100644 --- a/config/config.go +++ b/config/config.go @@ -14,8 +14,7 @@ var ( ) const ( - DefaultAddr string = "127.0.0.1:6380" - DefaultHttpAddr string = "127.0.0.1:11181" + DefaultAddr string = "127.0.0.1:6380" DefaultDBName string = "goleveldb" @@ -101,6 +100,8 @@ func NewConfigWithData(data []byte) (*Config, error) { return nil, err } + cfg.adjust() + return cfg, nil } @@ -108,7 +109,7 @@ func NewConfigDefault() *Config { cfg := new(Config) cfg.Addr = DefaultAddr - cfg.HttpAddr = DefaultHttpAddr + cfg.HttpAddr = "" cfg.DataDir = DefaultDataDir @@ -123,28 +124,36 @@ func NewConfigDefault() *Config { cfg.LMDB.MapSize = 20 * 1024 * 1024 cfg.LMDB.NoSync = true - cfg.Replication.WaitSyncTime = 1 + cfg.UseReplication = false + cfg.Replication.WaitSyncTime = 500 cfg.Replication.Compression = true cfg.Replication.WaitMaxSlaveAcks = 2 + cfg.Replication.SyncLog = 0 + + cfg.adjust() return cfg } -func (cfg *LevelDBConfig) Adjust() { - if cfg.CacheSize <= 0 { - cfg.CacheSize = 4 * 1024 * 1024 +func (cfg *Config) adjust() { + if cfg.LevelDB.CacheSize <= 0 { + cfg.LevelDB.CacheSize = 4 * 1024 * 1024 } - if cfg.BlockSize <= 0 { - cfg.BlockSize = 4 * 1024 + if cfg.LevelDB.BlockSize <= 0 { + cfg.LevelDB.BlockSize = 4 * 1024 } - if cfg.WriteBufferSize <= 0 { - cfg.WriteBufferSize = 4 * 1024 * 1024 + if cfg.LevelDB.WriteBufferSize <= 0 { + cfg.LevelDB.WriteBufferSize = 4 * 1024 * 1024 } - if cfg.MaxOpenFiles < 1024 { - cfg.MaxOpenFiles = 1024 + if cfg.LevelDB.MaxOpenFiles < 1024 { + cfg.LevelDB.MaxOpenFiles = 1024 + } + + if cfg.Replication.ExpiredLogDays <= 0 { + cfg.Replication.ExpiredLogDays = 7 } } diff --git a/config/config.toml b/config/config.toml index 87036db..27de6c5 100644 --- a/config/config.toml +++ b/config/config.toml @@ -58,8 +58,8 @@ path = "" # It will reduce performance but have better high availability. sync = true -# If sync is true, wait at last wait_sync_time seconds for slave syncing this log -wait_sync_time = 1 +# If sync is true, wait at last wait_sync_time milliseconds for slave syncing this log +wait_sync_time = 500 # If sync is true, wait at most min(wait_max_slave_acks, (n + 1) / 2) to promise syncing ok. # n is slave number diff --git a/etc/ledis.conf b/etc/ledis.conf index 9b69fc2..27de6c5 100644 --- a/etc/ledis.conf +++ b/etc/ledis.conf @@ -58,8 +58,8 @@ path = "" # It will reduce performance but have better high availability. sync = true -# If sync is true, wait at last wait_sync_time seconds for slave syncing this log -wait_sync_time = 1 +# If sync is true, wait at last wait_sync_time milliseconds for slave syncing this log +wait_sync_time = 500 # If sync is true, wait at most min(wait_max_slave_acks, (n + 1) / 2) to promise syncing ok. # n is slave number @@ -81,6 +81,7 @@ compression = true [snapshot] # Path to store snapshot dump file # if not set, use data_dir/snapshot +# snapshot file name format is snap-2006-01-02T15:04:05.999999999.dmp path = "" # Reserve newest max_num snapshot dump files diff --git a/ledis/dump_test.go b/ledis/dump_test.go index e29d928..98e4c9e 100644 --- a/ledis/dump_test.go +++ b/ledis/dump_test.go @@ -9,7 +9,7 @@ import ( ) func TestDump(t *testing.T) { - cfgM := new(config.Config) + cfgM := config.NewConfigDefault() cfgM.DataDir = "/tmp/test_ledis_master" os.RemoveAll(cfgM.DataDir) @@ -19,7 +19,7 @@ func TestDump(t *testing.T) { t.Fatal(err) } - cfgS := new(config.Config) + cfgS := config.NewConfigDefault() cfgS.DataDir = "/tmp/test_ledis_slave" os.RemoveAll(cfgM.DataDir) diff --git a/ledis/ledis_test.go b/ledis/ledis_test.go index 45f1c7f..51e4d0d 100644 --- a/ledis/ledis_test.go +++ b/ledis/ledis_test.go @@ -12,7 +12,7 @@ var testLedisOnce sync.Once func getTestDB() *DB { f := func() { - cfg := new(config.Config) + cfg := config.NewConfigDefault() cfg.DataDir = "/tmp/test_ledis" os.RemoveAll(cfg.DataDir) diff --git a/ledis/replication_test.go b/ledis/replication_test.go index 920ed63..fc5e210 100644 --- a/ledis/replication_test.go +++ b/ledis/replication_test.go @@ -30,7 +30,7 @@ func TestReplication(t *testing.T) { var slave *Ledis var err error - cfgM := new(config.Config) + cfgM := config.NewConfigDefault() cfgM.DataDir = "/tmp/test_repl/master" cfgM.UseReplication = true @@ -43,7 +43,7 @@ func TestReplication(t *testing.T) { t.Fatal(err) } - cfgS := new(config.Config) + cfgS := config.NewConfigDefault() cfgS.DataDir = "/tmp/test_repl/slave" cfgS.UseReplication = true cfgS.Readonly = true diff --git a/ledis/tx_test.go b/ledis/tx_test.go index cb3a7f0..e21c0a8 100644 --- a/ledis/tx_test.go +++ b/ledis/tx_test.go @@ -190,7 +190,7 @@ func testTxSelect(t *testing.T, db *DB) { } func testTx(t *testing.T, name string) { - cfg := new(config.Config) + cfg := config.NewConfigDefault() cfg.DataDir = "/tmp/ledis_test_tx" cfg.DBName = name diff --git a/rpl/goleveldb_store.go b/rpl/goleveldb_store.go index 1c70603..c00f36c 100644 --- a/rpl/goleveldb_store.go +++ b/rpl/goleveldb_store.go @@ -269,12 +269,12 @@ func (s *GoLevelDBStore) open() error { } func NewGoLevelDBStore(base string) (*GoLevelDBStore, error) { - cfg := new(config.Config) + cfg := config.NewConfigDefault() cfg.DBName = "goleveldb" cfg.DBPath = base - cfg.LevelDB.BlockSize = 4 * 1024 * 1024 - cfg.LevelDB.CacheSize = 16 * 1024 * 1024 - cfg.LevelDB.WriteBufferSize = 4 * 1024 * 1024 + cfg.LevelDB.BlockSize = 16 * 1024 * 1024 + cfg.LevelDB.CacheSize = 64 * 1024 * 1024 + cfg.LevelDB.WriteBufferSize = 64 * 1024 * 1024 cfg.LevelDB.Compression = false s := new(GoLevelDBStore) diff --git a/rpl/rpl_test.go b/rpl/rpl_test.go index 06fcf7d..63ff427 100644 --- a/rpl/rpl_test.go +++ b/rpl/rpl_test.go @@ -14,7 +14,7 @@ func TestReplication(t *testing.T) { } defer os.RemoveAll(dir) - c := new(config.Config) + c := config.NewConfigDefault() c.Replication.Path = dir r, err := NewReplication(c) diff --git a/server/app_test.go b/server/app_test.go index aa4daba..17e2989 100644 --- a/server/app_test.go +++ b/server/app_test.go @@ -29,7 +29,7 @@ func startTestApp() { f := func() { newTestLedisClient() - cfg := new(config.Config) + cfg := config.NewConfigDefault() cfg.DataDir = "/tmp/testdb" os.RemoveAll(cfg.DataDir) diff --git a/server/cmd_replication_test.go b/server/cmd_replication_test.go index 1dfde6b..3c79836 100644 --- a/server/cmd_replication_test.go +++ b/server/cmd_replication_test.go @@ -37,12 +37,12 @@ func TestReplication(t *testing.T) { data_dir := "/tmp/test_replication" os.RemoveAll(data_dir) - masterCfg := new(config.Config) + masterCfg := config.NewConfigDefault() masterCfg.DataDir = fmt.Sprintf("%s/master", data_dir) masterCfg.Addr = "127.0.0.1:11182" masterCfg.UseReplication = true masterCfg.Replication.Sync = true - masterCfg.Replication.WaitSyncTime = 5 + masterCfg.Replication.WaitSyncTime = 5000 var master *App var slave *App @@ -53,7 +53,7 @@ func TestReplication(t *testing.T) { } defer master.Close() - slaveCfg := new(config.Config) + slaveCfg := config.NewConfigDefault() slaveCfg.DataDir = fmt.Sprintf("%s/slave", data_dir) slaveCfg.Addr = "127.0.0.1:11183" slaveCfg.SlaveOf = masterCfg.Addr diff --git a/server/doc.go b/server/doc.go index 7dc47ff..e5a5dd4 100644 --- a/server/doc.go +++ b/server/doc.go @@ -9,7 +9,7 @@ // // Start a ledis server is very simple: // -// cfg := new(config.Config) +// cfg := config.NewConfigDefault() // cfg.Addr = "127.0.0.1:6380" // cfg.DataDir = "/tmp/ledis" // app := server.NewApp(cfg) diff --git a/server/replication.go b/server/replication.go index 68f2bc9..a21c46f 100644 --- a/server/replication.go +++ b/server/replication.go @@ -366,7 +366,7 @@ func (app *App) publishNewLog(l *rpl.Log) { select { case <-done: - case <-time.After(time.Duration(app.cfg.Replication.WaitSyncTime) * time.Second): + case <-time.After(time.Duration(app.cfg.Replication.WaitSyncTime) * time.Millisecond): log.Info("replication wait timeout") } } diff --git a/server/scan_test.go b/server/scan_test.go index bc47395..ed9b71c 100644 --- a/server/scan_test.go +++ b/server/scan_test.go @@ -9,7 +9,7 @@ import ( ) func TestScan(t *testing.T) { - cfg := new(config.Config) + cfg := config.NewConfigDefault() cfg.DataDir = "/tmp/test_scan" cfg.Addr = "127.0.0.1:11185" diff --git a/server/script_test.go b/server/script_test.go index a422a82..280d86e 100644 --- a/server/script_test.go +++ b/server/script_test.go @@ -101,7 +101,7 @@ var testScript4 = ` ` func TestLuaCall(t *testing.T) { - cfg := new(config.Config) + cfg := config.NewConfigDefault() cfg.Addr = ":11188" cfg.DataDir = "/tmp/testscript" cfg.DBName = "memory" diff --git a/server/snapshot_test.go b/server/snapshot_test.go index 11051a8..5e9ba46 100644 --- a/server/snapshot_test.go +++ b/server/snapshot_test.go @@ -18,7 +18,7 @@ func (d *testSnapshotDumper) Dump(w io.Writer) error { } func TestSnapshot(t *testing.T) { - cfg := new(config.Config) + cfg := config.NewConfigDefault() cfg.Snapshot.MaxNum = 2 cfg.Snapshot.Path = path.Join(os.TempDir(), "snapshot") defer os.RemoveAll(cfg.Snapshot.Path) diff --git a/store/goleveldb/db.go b/store/goleveldb/db.go index 5e76f74..9924067 100644 --- a/store/goleveldb/db.go +++ b/store/goleveldb/db.go @@ -113,8 +113,6 @@ func newOptions(cfg *config.LevelDBConfig) *opt.Options { opts := &opt.Options{} opts.ErrorIfMissing = false - cfg.Adjust() - opts.BlockCache = cache.NewLRUCache(cfg.CacheSize) //we must use bloomfilter diff --git a/store/hyperleveldb/db.go b/store/hyperleveldb/db.go index 0f13a0b..90af5be 100644 --- a/store/hyperleveldb/db.go +++ b/store/hyperleveldb/db.go @@ -108,8 +108,6 @@ func (db *DB) initOptions(cfg *config.LevelDBConfig) { opts.SetCreateIfMissing(true) - cfg.Adjust() - db.cache = NewLRUCache(cfg.CacheSize) opts.SetCache(db.cache) diff --git a/store/leveldb/db.go b/store/leveldb/db.go index 3705f78..cd1fb1e 100644 --- a/store/leveldb/db.go +++ b/store/leveldb/db.go @@ -108,8 +108,6 @@ func (db *DB) initOptions(cfg *config.LevelDBConfig) { opts.SetCreateIfMissing(true) - cfg.Adjust() - db.cache = NewLRUCache(cfg.CacheSize) opts.SetCache(db.cache) diff --git a/store/rocksdb/db.go b/store/rocksdb/db.go index f5cecf4..35a1c7e 100644 --- a/store/rocksdb/db.go +++ b/store/rocksdb/db.go @@ -113,8 +113,6 @@ func (db *DB) initOptions(cfg *config.LevelDBConfig) { opts.SetCreateIfMissing(true) - cfg.Adjust() - db.env = NewDefaultEnv() db.env.SetBackgroundThreads(runtime.NumCPU() * 2) db.env.SetHighPriorityBackgroundThreads(1) diff --git a/store/store_test.go b/store/store_test.go index d2f2ca6..b488158 100644 --- a/store/store_test.go +++ b/store/store_test.go @@ -10,7 +10,7 @@ import ( ) func TestStore(t *testing.T) { - cfg := new(config.Config) + cfg := config.NewConfigDefault() cfg.DataDir = "/tmp/testdb" cfg.LMDB.MapSize = 10 * 1024 * 1024