-
Notifications
You must be signed in to change notification settings - Fork 4
/
rotom.go
236 lines (204 loc) · 4.99 KB
/
rotom.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package main
import (
"github.com/dustin/go-humanize"
"github.com/tidwall/redcon"
"github.com/xgzlucario/rotom/internal/iface"
"github.com/xgzlucario/rotom/internal/list"
"github.com/xgzlucario/rotom/internal/net"
"github.com/xgzlucario/rotom/internal/resp"
)
const (
QueryBufSize = 8 * KB
MaxQueryDataLen = 128 * MB
)
type (
Map = iface.MapI
Set = iface.SetI
List = *list.QuickList
ZSet = iface.ZSetI
)
type DB struct {
dict *Dict
aof *Aof
rdb *Rdb
}
type Client struct {
fd int
recvx int
readx int
queryBuf []byte
replyWriter *resp.Writer
argsBuf [][]byte
respBuf []redcon.RESP
}
type Server struct {
fd int
aeLoop *AeLoop
clients map[int]*Client
}
var (
db DB
server Server
)
// InitDB initializes database and redo appendonly files if needed.
func InitDB() (err error) {
db.dict = New()
if configGetBool("save") {
db.rdb = NewRdb()
log.Debug().Msg("start loading rdb file...")
if err = db.rdb.LoadDB(); err != nil {
return err
}
}
if configGetAppendOnly() {
db.aof, err = NewAof(configGetAppendFileName())
if err != nil {
return
}
log.Debug().Msg("start loading aof file...")
// Load the initial data into memory by processing each stored command.
emptyWriter := resp.NewWriter()
return db.aof.Read(func(args []redcon.RESP) {
command := b2s(args[0].Bytes())
cmd, err := lookupCommand(command)
if err == nil {
cmd.process(emptyWriter, args[1:])
emptyWriter.Reset()
}
})
}
return nil
}
// AcceptHandler is the main file event of aeloop.
func AcceptHandler(loop *AeLoop, fd int, _ interface{}) {
cfd, err := net.Accept(fd)
if err != nil {
log.Error().Msgf("accept err: %v", err)
return
}
log.Info().Msgf("accept new client fd: %d", cfd)
client := &Client{
fd: cfd,
replyWriter: resp.NewWriter(),
queryBuf: make([]byte, QueryBufSize),
argsBuf: make([][]byte, 8),
respBuf: make([]redcon.RESP, 8),
}
server.clients[cfd] = client
loop.AddRead(cfd, ReadQueryFromClient, client)
}
func ReadQueryFromClient(_ *AeLoop, fd int, extra interface{}) {
client := extra.(*Client)
readSize := 0
READ:
n, err := net.Read(fd, client.queryBuf[client.recvx:])
if err != nil {
log.Error().Msgf("client %v read err: %v", fd, err)
freeClient(client)
return
}
readSize += n
client.recvx += n
if readSize == 0 {
freeClient(client)
return
}
if client.recvx >= MaxQueryDataLen {
log.Error().Msgf("client %d read query data too large, now free", fd)
freeClient(client)
return
}
// queryBuf need grow up
if client.recvx == len(client.queryBuf) {
client.queryBuf = append(client.queryBuf, make([]byte, client.recvx)...)
sz := uint64(len(client.queryBuf))
log.Warn().Msgf("client %d queryBuf grow up to size %s", fd, humanize.Bytes(sz))
goto READ
}
ProcessQueryBuf(client)
}
func resetClient(client *Client) {
client.readx = 0
client.recvx = 0
}
func freeClient(client *Client) {
delete(server.clients, client.fd)
server.aeLoop.ModDetach(client.fd)
_ = net.Close(client.fd)
log.Info().Msgf("free client %d", client.fd)
}
func ProcessQueryBuf(client *Client) {
for client.readx < client.recvx {
queryBuf := client.queryBuf[client.readx:client.recvx]
// buffer pre alloc
respBuf := client.respBuf[:0]
argsBuf := client.argsBuf[:0]
complete, args, _, left, err := redcon.ReadNextCommand(queryBuf, argsBuf)
if err != nil {
log.Error().Msgf("read next command error: %v", err)
resetClient(client)
return
}
if !complete {
break
}
n := len(queryBuf) - len(left)
client.readx += n
command := b2s(args[0])
for _, arg := range args[1:] {
respBuf = append(respBuf, redcon.RESP{Data: arg})
}
cmd, err := lookupCommand(command)
if err != nil {
client.replyWriter.WriteError(err.Error())
log.Error().Msg(err.Error())
} else {
cmd.process(client.replyWriter, respBuf)
// write aof file
if cmd.persist && configGetAppendOnly() {
_, _ = db.aof.Write(queryBuf[:n])
}
}
}
if client.readx == client.recvx {
resetClient(client)
}
server.aeLoop.ModWrite(client.fd, SendReplyToClient, client)
}
func SendReplyToClient(loop *AeLoop, fd int, extra interface{}) {
client := extra.(*Client)
sentbuf := client.replyWriter.Buffer()
n, err := net.Write(fd, sentbuf)
if err != nil {
log.Error().Msgf("send reply err: %v", err)
freeClient(client)
return
}
if n != len(sentbuf) {
log.Error().Msgf("send packet size error: %d %d", n, len(sentbuf))
}
client.replyWriter.Reset()
loop.ModRead(fd, ReadQueryFromClient, client)
}
func initServer() (err error) {
server.clients = make(map[int]*Client)
// init aeLoop
server.aeLoop, err = AeLoopCreate()
if err != nil {
return err
}
// init tcp server
server.fd, err = net.TcpServer(configGetPort())
if err != nil {
return err
}
return nil
}
func CronSyncAOF(ae *AeLoop, fd int, extra interface{}) {
if err := db.aof.Flush(); err != nil {
log.Error().Msgf("sync aof error: %v", err)
}
}
func CronEvictExpired(ae *AeLoop, fd int, extra interface{}) {
db.dict.EvictExpired()
}