adjust config, add some replication fund

This commit is contained in:
siddontang 2014-06-06 14:57:18 +08:00
parent 778f8f4a2b
commit 8fab454223
8 changed files with 74 additions and 17 deletions

View File

@ -12,12 +12,11 @@ import (
type Config struct {
DataDir string `json:"data_dir"`
//data_db path is data_dir/data
//if you not set leveldb path, use data_dir/data
DataDB leveldb.Config `json:"data_db"`
//binlog path is data_dir/binlog
//you muse set binlog name to enable binlog
UseBinLog bool `json:"use_bin_log"`
//if you not set bin log path, use data_dir/bin_log
BinLog replication.BinLogConfig `json:"bin_log"`
}
@ -78,7 +77,7 @@ func OpenWithConfig(cfg *Config) (*Ledis, error) {
l.ldb = ldb
if len(cfg.BinLog.Name) > 0 {
if cfg.UseBinLog {
if len(cfg.BinLog.Path) == 0 {
cfg.BinLog.Path = path.Join(cfg.DataDir, "bin_log")
}

View File

@ -17,9 +17,7 @@ func TestReplication(t *testing.T) {
master, err = Open([]byte(`
{
"data_dir" : "/tmp/test_repl/master",
"bin_log": {
"name" : "ledis"
}
"use_bin_log" : true
}
`))
if err != nil {
@ -40,7 +38,7 @@ func TestReplication(t *testing.T) {
db.Set([]byte("b"), []byte("2"))
db.Set([]byte("c"), []byte("3"))
relayLog := "/tmp/test_repl/master/binlog/ledis-bin.0000001"
relayLog := "/tmp/test_repl/master/bin_log/ledis-bin.0000001"
var offset int64
offset, err = slave.RepliateRelayLog(relayLog, 0)

View File

@ -52,7 +52,7 @@ func newLog(handler logHandler, cfg *LogConfig) (*Log, error) {
l.handler = handler
if len(l.cfg.Name) == 0 {
return nil, fmt.Errorf("you must set log name first")
l.cfg.Name = "ledis"
}
if err := os.MkdirAll(cfg.Path, os.ModePerm); err != nil {

View File

@ -3,6 +3,7 @@ package server
import (
"fmt"
"github.com/siddontang/ledisdb/ledis"
"github.com/siddontang/ledisdb/replication"
"net"
"strings"
)
@ -15,6 +16,12 @@ type App struct {
ldb *ledis.Ledis
closed bool
slaveMode bool
relayLog *replication.Log
quit chan struct{}
}
func NewApp(cfg *Config) (*App, error) {
@ -28,6 +35,8 @@ func NewApp(cfg *Config) (*App, error) {
app := new(App)
app.quit = make(chan struct{})
app.closed = false
app.cfg = cfg
@ -44,8 +53,17 @@ func NewApp(cfg *Config) (*App, error) {
return nil, err
}
app.ldb, err = ledis.OpenWithConfig(&cfg.DB)
if err != nil {
app.slaveMode = false
if len(app.cfg.SlaveOf) > 0 {
app.slaveMode = true
if app.relayLog, err = replication.NewRelayLogWithConfig(&cfg.RelayLog); err != nil {
return nil, err
}
}
if app.ldb, err = ledis.OpenWithConfig(&cfg.DB); err != nil {
return nil, err
}
@ -57,20 +75,26 @@ func (app *App) Close() {
return
}
app.closed = true
close(app.quit)
app.listener.Close()
app.ldb.Close()
app.closed = true
}
func (app *App) Run() {
if app.slaveMode {
app.runReplication()
}
for !app.closed {
conn, err := app.listener.Accept()
if err != nil {
continue
}
newClient(conn, app.ldb)
newClient(conn, app)
}
}

View File

@ -15,6 +15,7 @@ import (
var errReadRequest = errors.New("invalid request protocol")
type client struct {
app *App
ldb *ledis.Ledis
db *ledis.DB
@ -29,11 +30,13 @@ type client struct {
reqC chan error
}
func newClient(c net.Conn, ldb *ledis.Ledis) {
func newClient(c net.Conn, app *App) {
co := new(client)
co.ldb = ldb
co.app = app
co.ldb = app.ldb
//use default db
co.db, _ = ldb.Select(0)
co.db, _ = app.ldb.Select(0)
co.c = c
co.rb = bufio.NewReaderSize(c, 256)

20
server/cmd_replication.go Normal file
View File

@ -0,0 +1,20 @@
package server
func slaveofCommand(c *client) error {
if len(c.args) > 1 {
return ErrCmdParams
}
master := ""
if len(c.args) == 1 {
master = string(c.args[0])
}
if err := c.app.slaveof(master); err != nil {
return err
}
c.writeStatus(OK)
return nil
}

View File

@ -15,6 +15,10 @@ type Config struct {
//if you not set db path, use data_dir
DB ledis.Config `json:"db"`
//set slaveof to enable replication from master
//empty, no replication
SlaveOf string `json:"slaveof"`
//if you not set relay log path, use data_dir/realy_log
RelayLog replication.RelayLogConfig `json:"relay_log"`
}

9
server/replication.go Normal file
View File

@ -0,0 +1,9 @@
package server
func (app *App) slaveof(master string) error {
return nil
}
func (app *App) runReplication() {
}