From 7a763da082fbd57e7a9b78de13359b1fa9b71613 Mon Sep 17 00:00:00 2001 From: siddontang Date: Wed, 23 Jul 2014 07:55:32 +0800 Subject: [PATCH] change configuration Config struct now not includes other configs, but shows all People can see configuration clearly --- bootstrap.sh | 5 ++-- etc/ledis.json | 7 ++++-- ledis/config.go | 19 ++++++++++++++ ledis/ledis.go | 45 +++++++++++++++++----------------- ledis/ledis_test.go | 2 +- ledis/replication_test.go | 4 +-- server/app.go | 20 +++++++++++---- server/app_test.go | 13 +++++----- server/cmd_replication_test.go | 2 +- server/config.go | 16 +++++++++--- 10 files changed, 88 insertions(+), 45 deletions(-) create mode 100644 ledis/config.go diff --git a/bootstrap.sh b/bootstrap.sh index 17eeb35..0ba6b65 100644 --- a/bootstrap.sh +++ b/bootstrap.sh @@ -2,5 +2,6 @@ . ./dev.sh -go get -u github.com/siddontang/go-log/log -go get -u github.com/siddontang/go-snappy/snappy \ No newline at end of file +go get github.com/siddontang/go-log/log +go get github.com/siddontang/go-snappy/snappy +go get github.com/siddontang/copier \ No newline at end of file diff --git a/etc/ledis.json b/etc/ledis.json index 0d93e88..a683a81 100644 --- a/etc/ledis.json +++ b/etc/ledis.json @@ -2,13 +2,16 @@ "addr": "127.0.0.1:6380", "data_dir": "/tmp/ledis_server", "db": { - "data_db" : { "compression": false, "block_size": 32768, "write_buffer_size": 67108864, "cache_size": 524288000, "max_open_files":1024 - } + }, + + "binlog" : { + "use" : false, + "max_open_files" : 1024 }, "access_log" : "access.log" diff --git a/ledis/config.go b/ledis/config.go new file mode 100644 index 0000000..fa4eede --- /dev/null +++ b/ledis/config.go @@ -0,0 +1,19 @@ +package ledis + +type Config struct { + DataDir string `json:"data_dir"` + + DB struct { + Compression bool `json:"compression"` + BlockSize int `json:"block_size"` + WriteBufferSize int `json:"write_buffer_size"` + CacheSize int `json:"cache_size"` + MaxOpenFiles int `json:"max_open_files"` + } `json:"db"` + + BinLog struct { + Use bool `json:"use"` + MaxFileSize int `json:"max_file_size"` + MaxFileNum int `json:"max_file_num"` + } `json:"binlog"` +} diff --git a/ledis/ledis.go b/ledis/ledis.go index cb83d01..80dcf29 100644 --- a/ledis/ledis.go +++ b/ledis/ledis.go @@ -3,6 +3,7 @@ package ledis import ( "encoding/json" "fmt" + "github.com/siddontang/copier" "github.com/siddontang/go-log/log" "github.com/siddontang/ledisdb/leveldb" "path" @@ -10,18 +11,6 @@ import ( "time" ) -type Config struct { - DataDir string `json:"data_dir"` - - //if you not set leveldb path, use data_dir/data - DataDB leveldb.Config `json:"data_db"` - - UseBinLog bool `json:"use_bin_log"` - - //if you not set bin log path, use data_dir/bin_log - BinLog BinLogConfig `json:"bin_log"` -} - type DB struct { l *Ledis @@ -60,16 +49,31 @@ func OpenWithJsonConfig(configJson json.RawMessage) (*Ledis, error) { return Open(&cfg) } +func openDB(cfg *Config) (*leveldb.DB, error) { + dbPath := path.Join(cfg.DataDir, "data") + + dbCfg := new(leveldb.Config) + copier.Copy(dbCfg, &cfg.DB) + dbCfg.Path = dbPath + + return leveldb.Open(dbCfg) +} + +func openBinLog(cfg *Config) (*BinLog, error) { + binLogPath := path.Join(cfg.DataDir, "bin_log") + c := new(BinLogConfig) + copier.Copy(c, &cfg.BinLog) + c.Path = binLogPath + + return NewBinLog(c) +} + func Open(cfg *Config) (*Ledis, error) { if len(cfg.DataDir) == 0 { return nil, fmt.Errorf("must set correct data_dir") } - if len(cfg.DataDB.Path) == 0 { - cfg.DataDB.Path = path.Join(cfg.DataDir, "data") - } - - ldb, err := leveldb.Open(&cfg.DataDB) + ldb, err := openDB(cfg) if err != nil { return nil, err } @@ -81,11 +85,8 @@ func Open(cfg *Config) (*Ledis, error) { l.ldb = ldb - if cfg.UseBinLog { - if len(cfg.BinLog.Path) == 0 { - cfg.BinLog.Path = path.Join(cfg.DataDir, "bin_log") - } - l.binlog, err = NewBinLog(&cfg.BinLog) + if cfg.BinLog.Use { + l.binlog, err = openBinLog(cfg) if err != nil { return nil, err } diff --git a/ledis/ledis_test.go b/ledis/ledis_test.go index aba2a62..07b285a 100644 --- a/ledis/ledis_test.go +++ b/ledis/ledis_test.go @@ -14,7 +14,7 @@ func getTestDB() *DB { var d = []byte(` { "data_dir" : "/tmp/test_ledis", - "data_db" : { + "db" : { "compression":true, "block_size" : 32768, "write_buffer_size" : 2097152, diff --git a/ledis/replication_test.go b/ledis/replication_test.go index bc33f78..b5a939b 100644 --- a/ledis/replication_test.go +++ b/ledis/replication_test.go @@ -35,8 +35,8 @@ func TestReplication(t *testing.T) { master, err = OpenWithJsonConfig([]byte(` { "data_dir" : "/tmp/test_repl/master", - "use_bin_log" : true, - "bin_log" : { + "binlog" : { + "use" : true, "max_file_size" : 50 } } diff --git a/server/app.go b/server/app.go index 9f155c1..756535c 100644 --- a/server/app.go +++ b/server/app.go @@ -2,6 +2,7 @@ package server import ( "fmt" + "github.com/siddontang/copier" "github.com/siddontang/ledisdb/ledis" "net" "path" @@ -30,10 +31,6 @@ func NewApp(cfg *Config) (*App, error) { return nil, fmt.Errorf("must set data_dir first") } - if len(cfg.DB.DataDir) == 0 { - cfg.DB.DataDir = cfg.DataDir - } - app := new(App) app.quit = make(chan struct{}) @@ -66,7 +63,7 @@ func NewApp(cfg *Config) (*App, error) { } } - if app.ldb, err = ledis.Open(&cfg.DB); err != nil { + if app.ldb, err = openLedis(cfg); err != nil { return nil, err } @@ -75,6 +72,19 @@ func NewApp(cfg *Config) (*App, error) { return app, nil } +func openLedis(cfg *Config) (*ledis.Ledis, error) { + c := new(ledis.Config) + + c.DataDir = cfg.DataDir + + copier.Copy(&c.DB, &cfg.DB) + copier.Copy(&c.BinLog, &cfg.BinLog) + + println("max open files", c.DB.MaxOpenFiles) + + return ledis.Open(c) +} + func (app *App) Close() { if app.closed { return diff --git a/server/app_test.go b/server/app_test.go index b4db346..2b73ab5 100644 --- a/server/app_test.go +++ b/server/app_test.go @@ -34,13 +34,12 @@ func startTestApp() { { "data_dir" : "/tmp/testdb", "addr" : "127.0.0.1:16380", - "db" : { - "data_db" : { - "compression":true, - "block_size" : 32768, - "write_buffer_size" : 2097152, - "cache_size" : 20971520 - } + "db" : { + "compression":true, + "block_size" : 32768, + "write_buffer_size" : 2097152, + "cache_size" : 20971520, + "max_open_files" : 1024 } } `) diff --git a/server/cmd_replication_test.go b/server/cmd_replication_test.go index 290d94b..dce4b6f 100644 --- a/server/cmd_replication_test.go +++ b/server/cmd_replication_test.go @@ -32,7 +32,7 @@ func TestReplication(t *testing.T) { masterCfg := new(Config) masterCfg.DataDir = fmt.Sprintf("%s/master", data_dir) masterCfg.Addr = "127.0.0.1:11182" - masterCfg.DB.UseBinLog = true + masterCfg.BinLog.Use = true var master *App var slave *App diff --git a/server/config.go b/server/config.go index 89aba2c..64243e8 100644 --- a/server/config.go +++ b/server/config.go @@ -2,7 +2,6 @@ package server import ( "encoding/json" - "github.com/siddontang/ledisdb/ledis" "io/ioutil" ) @@ -11,8 +10,19 @@ type Config struct { DataDir string `json:"data_dir"` - //if you not set db path, use data_dir - DB ledis.Config `json:"db"` + DB struct { + Compression bool `json:"compression"` + BlockSize int `json:"block_size"` + WriteBufferSize int `json:"write_buffer_size"` + CacheSize int `json:"cache_size"` + MaxOpenFiles int `json:"max_open_files"` + } `json:"db"` + + BinLog struct { + Use bool `json:"use"` + MaxFileSize int `json:"max_file_size"` + MaxFileNum int `json:"max_file_num"` + } `json:"binlog"` //set slaveof to enable replication from master //empty, no replication