Skip to content

Commit ceb523b

Browse files
committed
Merge branch 'GoRedisDev'
2 parents 6db448d + 574fa98 commit ceb523b

12 files changed

+167
-131
lines changed

doc/release notes.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
GoRedis release notes
22
=====================
33

4+
**GoRedis 1.0.70** @2014.4.16
5+
6+
* [Feature] 优化LRANGE性能,从多次RAW_GET改为单次Enum
7+
* [Coding] 简化启动函数代码
8+
* [Fix] 恢复logpath默认路径为/data
9+
410
**SlaveOf 1.0.4** @2014.4.11
511

612
* [Feature] 支持配置dbpath
@@ -11,6 +17,7 @@ GoRedis release notes
1117

1218

1319
**GoRedis 1.0.69** @2014.4.10
20+
1421
* [Fix] 修正PrefixEnumerate时全部遍历没有quit
1522
* [ADD] 启动参数新增logpath、datapath用于自定义数据和Log路径
1623

goredis_server/go_redis_server.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"GoRedis/libs/uuid"
1010
"container/list"
1111
"errors"
12+
"fmt"
1213
"os"
1314
"reflect"
1415
"runtime/debug"
@@ -17,7 +18,7 @@ import (
1718
)
1819

1920
// 版本号,每次更新都需要增加
20-
const VERSION = "1.0.69"
21+
const VERSION = "1.0.70"
2122
const PREFIX = "__goredis:"
2223

2324
var (
@@ -36,7 +37,6 @@ type GoRedisServer struct {
3637
RedisServer
3738
// 数据源
3839
opt *Options // 选项
39-
directory string
4040
levelRedis *levelredis.LevelRedis
4141
config *Config
4242
// counters
@@ -82,8 +82,6 @@ func NewGoRedisServer(opt *Options) (server *GoRedisServer) {
8282
server.slavemgr = NewSessionManager()
8383
server.sessmgr = NewSessionManager()
8484
server.info = NewInfo(server)
85-
// default datasource
86-
server.directory = opt.Directory()
8785
// counter
8886
server.counters = counter.NewCounters()
8987
server.cmdCounters = counter.NewCounters()
@@ -93,8 +91,9 @@ func NewGoRedisServer(opt *Options) (server *GoRedisServer) {
9391
}
9492

9593
func (server *GoRedisServer) Listen() error {
96-
stdlog.Printf("listen %s\n", server.opt.Bind())
97-
return server.RedisServer.Listen(server.opt.Bind())
94+
addr := fmt.Sprintf("%s:%d", server.opt.Host(), server.opt.Port())
95+
stdlog.Printf("listen %s\n", addr)
96+
return server.RedisServer.Listen(addr)
9897
}
9998

10099
func (server *GoRedisServer) UID() (uid string) {

goredis_server/go_redis_server_aof.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func (server *GoRedisServer) OnAOF(session *Session, cmd *Command) (reply *Reply
4545

4646
func (server *GoRedisServer) aofStart() (err error) {
4747
if server.aofwriter == nil {
48-
filename := server.directory + "appendonly.aof"
48+
filename := server.opt.LogPath() + "/appendonly.aof"
4949
f, e := os.OpenFile(filename, os.O_WRONLY|os.O_TRUNC|os.O_CREATE, os.ModePerm)
5050
if e != nil {
5151
return e

goredis_server/go_redis_server_init.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ func (server *GoRedisServer) Init() (err error) {
1616

1717
server.initSignalNotify()
1818

19-
stdlog.Println("server init, version", VERSION, "...")
2019
err = server.initLevelDB()
2120
if err != nil {
2221
return
@@ -27,17 +26,17 @@ func (server *GoRedisServer) Init() (err error) {
2726
}
2827
server.config = NewConfig(server.levelRedis, PREFIX+"config:")
2928
// monitor
30-
server.initCommandMonitor(server.opt.LogDir() + "/cmd.log")
29+
server.initCommandMonitor(server.opt.LogPath() + "/cmd.log")
3130
server.initCommandCounterLog("string", []string{"GET", "SET", "MGET", "MSET", "INCR", "DECR", "INCRBY", "DECRBY"})
3231
server.initCommandCounterLog("hash", []string{"HGETALL", "HGET", "HSET", "HDEL", "HMGET", "HMSET", "HINCRBY", "HLEN"})
3332
server.initCommandCounterLog("set", []string{"SADD", "SCARD", "SISMEMBER", "SMEMBERS", "SREM"})
3433
server.initCommandCounterLog("list", []string{"LPUSH", "RPUSH", "LPOP", "RPOP", "LINDEX", "LLEN", "LRANGE", "LTRIM"})
3534
server.initCommandCounterLog("zset", []string{"ZADD", "ZCARD", "ZSCORE", "ZINCRBY", "ZRANGE", "ZRANGEBYSCORE", "ZRANK", "ZREM", "ZREMRANGEBYRANK", "ZREMRANGEBYSCORE", "ZREVRANGE", "ZREVRANGEBYSCORE", "ZREVRANK"})
36-
server.initSeqLog(server.opt.LogDir() + "/seq.log")
37-
server.initLeveldbIOLog(server.opt.LogDir() + "/leveldb.io.log")
38-
server.initLeveldbStatsLog(server.opt.LogDir() + "/leveldb.stats.log")
39-
server.initExecLog(server.opt.LogDir() + "/exec.time.log")
40-
server.initSlowlog(server.opt.LogDir() + "/slow.log")
35+
server.initSeqLog(server.opt.LogPath() + "/seq.log")
36+
server.initLeveldbIOLog(server.opt.LogPath() + "/leveldb.io.log")
37+
server.initLeveldbStatsLog(server.opt.LogPath() + "/leveldb.stats.log")
38+
server.initExecLog(server.opt.LogPath() + "/exec.time.log")
39+
server.initSlowlog(server.opt.LogPath() + "/slow.log")
4140
stdlog.Printf("init uid %s\n", server.UID())
4241
server.initSlaveOf()
4342
return
@@ -110,7 +109,7 @@ func (server *GoRedisServer) initLevelDB() (err error) {
110109
env.SetBackgroundThreads(6)
111110
env.SetHighPriorityBackgroundThreads(2)
112111
opts.SetEnv(env)
113-
db, e1 := levelredis.Open(server.directory+"/db0", opts)
112+
db, e1 := levelredis.Open(server.opt.DBPath()+"/db0", opts)
114113
if e1 != nil {
115114
return e1
116115
}
@@ -139,7 +138,7 @@ func (server *GoRedisServer) initSyncLog() error {
139138
env.SetBackgroundThreads(2)
140139
env.SetHighPriorityBackgroundThreads(1)
141140
opts.SetEnv(env)
142-
db, e1 := levelredis.Open(server.opt.LogDir()+"/synclog", opts)
141+
db, e1 := levelredis.Open(server.opt.LogPath()+"/synclog", opts)
143142
if e1 != nil {
144143
return e1
145144
}
@@ -288,7 +287,7 @@ func (server *GoRedisServer) initLeveldbStatsLog(path string) {
288287
}
289288

290289
func (server *GoRedisServer) initCommandCounterLog(cate string, cmds []string) {
291-
path := fmt.Sprintf("%s/cmd.%s.log", server.opt.LogDir(), cate)
290+
path := fmt.Sprintf("%s/cmd.%s.log", server.opt.LogPath(), cate)
292291
file, err := openfile(path)
293292
if err != nil {
294293
panic(err)

goredis_server/go_redis_server_list.go

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -103,30 +103,25 @@ func (server *GoRedisServer) OnLTRIM(cmd *Command) (reply *Reply) {
103103

104104
func (server *GoRedisServer) OnLRANGE(cmd *Command) (reply *Reply) {
105105
key := cmd.StringAtIndex(1)
106-
lst := server.levelRedis.GetList(key)
107-
start, e1 := cmd.IntAtIndex(2)
108-
end, e2 := cmd.IntAtIndex(3)
106+
start, e1 := cmd.Int64AtIndex(2)
107+
end, e2 := cmd.Int64AtIndex(3)
109108
if e1 != nil || e2 != nil {
110109
return ErrorReply("bad start/end")
111110
} else if start < 0 {
112111
return ErrorReply("start > end")
113112
} else if end != -1 && start > end {
114113
return ErrorReply("start > end")
115114
}
116-
// 初始缓冲
117-
buflen := end - start + 1
118-
if end <= 0 || end > 100 {
119-
buflen = 100
120-
}
121-
bulks := make([]interface{}, 0, buflen)
122-
for i := start; end == -1 || i <= end; i++ {
123-
elem, e2 := lst.Index(int64(i))
124-
if e2 != nil {
125-
return ErrorReply(e2)
126-
} else if elem == nil {
127-
break
128-
}
129-
bulks = append(bulks, elem.Value.([]byte))
115+
116+
lst := server.levelRedis.GetList(key)
117+
elems, err := lst.Range(start, end)
118+
if err != nil {
119+
return ErrorReply(err)
120+
}
121+
122+
bulks := make([]interface{}, len(elems))
123+
for i := 0; i < len(elems); i++ {
124+
bulks[i] = elems[i].Value
130125
}
131126
reply = MultiBulksReply(bulks)
132127
return

goredis_server/go_redis_server_sys.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func (server *GoRedisServer) OnPPROF(cmd *Command) (reply *Reply) {
2020
action := cmd.StringAtIndex(1)
2121
switch action {
2222
case "mem":
23-
f, err := os.Create(server.directory + "mem.prof")
23+
f, err := os.Create(server.opt.LogPath() + "mem.prof")
2424
if err != nil {
2525
return ErrorReply(err)
2626
}

goredis_server/info.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func (i *Info) uptime_in_seconds() int64 {
8888

8989
func (i *Info) db_size() int64 {
9090
if time.Now().Sub(i.dbsizeTime).Seconds() > 15 {
91-
i.dbsize = directoryTotalSize(i.server.directory + "db0")
91+
i.dbsize = directoryTotalSize(i.server.opt.DBPath() + "db0")
9292
i.dbsizeTime = time.Now()
9393
}
9494
return i.dbsize

goredis_server/options.go

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,55 @@ package goredis_server
22

33
// 运行配置
44
type Options struct {
5-
bind string
5+
host string
6+
port int
7+
dbpath string
8+
logpath string
69
slaveofHost string
710
slaveofPort int
8-
directory string
9-
logdir string
1011
}
1112

1213
func NewOptions() (o *Options) {
1314
o = &Options{}
1415
return
1516
}
1617

17-
func (o *Options) SetBind(host string) {
18-
o.bind = host
18+
func (o *Options) SetHost(host string) {
19+
o.host = host
1920
}
2021

21-
func (o *Options) Bind() string {
22-
return o.bind
22+
func (o *Options) Host() string {
23+
return o.host
2324
}
2425

25-
func (o *Options) SetSlaveOf(host string, port int) {
26-
o.slaveofHost, o.slaveofPort = host, port
26+
func (o *Options) SetPort(port int) {
27+
o.port = port
2728
}
2829

29-
func (o *Options) SlaveOf() (host string, port int) {
30-
return o.slaveofHost, o.slaveofPort
30+
func (o *Options) Port() int {
31+
return o.port
32+
}
33+
34+
func (o *Options) SetDBPath(dbpath string) {
35+
o.dbpath = dbpath
3136
}
3237

33-
func (o *Options) SetDirectory(path string) {
34-
o.directory = path
38+
func (o *Options) DBPath() string {
39+
return o.dbpath
3540
}
3641

37-
func (o *Options) Directory() string {
38-
return o.directory
42+
func (o *Options) SetLogPath(logpath string) {
43+
o.logpath = logpath
3944
}
4045

41-
func (o *Options) LogDir() string {
42-
return o.logdir
46+
func (o *Options) LogPath() string {
47+
return o.logpath
4348
}
4449

45-
func (o *Options) SetLogDir(logdir string) {
46-
o.logdir = logdir
50+
func (o *Options) SetSlaveOf(host string, port int) {
51+
o.slaveofHost, o.slaveofPort = host, port
52+
}
53+
54+
func (o *Options) SlaveOf() (host string, port int) {
55+
return o.slaveofHost, o.slaveofPort
4756
}

goredis_server/slave_client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (s *SlaveClient) Session() *Session {
6464
}
6565

6666
func (s *SlaveClient) directory() string {
67-
return s.server.directory + "sync_" + fmt.Sprint(s.session.RemoteAddr()) + "/"
67+
return s.server.opt.LogPath() + "/sync_" + fmt.Sprint(s.session.RemoteAddr()) + "/"
6868
}
6969

7070
func (s *SlaveClient) rdbfilename() string {

goredis_server/slave_client_v2.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func NewSlaveClientV2(server *GoRedisServer, session *Session) (s *SlaveClientV2
3434
}
3535

3636
func (s *SlaveClientV2) initLog() error {
37-
path := fmt.Sprintf("%s/sync_%s.log", s.server.directory, s.session.RemoteAddr())
37+
path := fmt.Sprintf("%s/sync_%s.log", s.server.opt.LogPath(), s.session.RemoteAddr())
3838
file, err := os.OpenFile(path, os.O_RDWR|os.O_APPEND|os.O_CREATE, os.ModePerm)
3939
if err != nil {
4040
return err
@@ -56,8 +56,7 @@ func (s *SlaveClientV2) Session() *Session {
5656

5757
func (s *SlaveClientV2) Sync() (err error) {
5858
s.lastseq = s.masterSeq(s.session.RemoteAddr().String())
59-
_, port := splitHostPort(s.server.opt.Bind())
60-
args := formatByteSlice("SYNC", "UID", s.server.UID(), "PORT", port)
59+
args := formatByteSlice("SYNC", "UID", s.server.UID(), "PORT", s.server.opt.Port())
6160
if s.lastseq < 0 {
6261
args = append(args, formatByteSlice("SNAP", "1")...)
6362
} else {

0 commit comments

Comments
 (0)