-
Notifications
You must be signed in to change notification settings - Fork 20
/
godis.go
466 lines (423 loc) · 10.1 KB
/
godis.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
package main
import (
"errors"
"fmt"
"hash/fnv"
"log"
"os"
"strconv"
"strings"
"time"
)
type CmdType = byte
const (
COMMAND_UNKNOWN CmdType = 0x00
COMMAND_INLINE CmdType = 0x01
COMMAND_BULK CmdType = 0x02
)
const (
GODIS_IO_BUF int = 1024 * 16
GODIS_MAX_BULK int = 1024 * 4
GODIS_MAX_INLINE int = 1024 * 4
)
type GodisDB struct {
data *Dict
expire *Dict
}
type GodisServer struct {
fd int
port int
db *GodisDB
clients map[int]*GodisClient
aeLoop *AeLoop
}
type GodisClient struct {
fd int
db *GodisDB
args []*Gobj
reply *List
sentLen int
queryBuf []byte
queryLen int
cmdTy CmdType
bulkNum int
bulkLen int
}
type CommandProc func(c *GodisClient)
// do not support bulk command
type GodisCommand struct {
name string
proc CommandProc
arity int
}
// Global Varibles
var server GodisServer
var cmdTable []GodisCommand = []GodisCommand{
{"get", getCommand, 2},
{"set", setCommand, 3},
{"expire", expireCommand, 3},
//TODO
}
func expireIfNeeded(key *Gobj) {
entry := server.db.expire.Find(key)
if entry == nil {
return
}
when := entry.Val.IntVal()
if when > GetMsTime() {
return
}
server.db.expire.Delete(key)
server.db.data.Delete(key)
}
func findKeyRead(key *Gobj) *Gobj {
expireIfNeeded(key)
return server.db.data.Get(key)
}
func getCommand(c *GodisClient) {
key := c.args[1]
val := findKeyRead(key)
if val == nil {
//TODO: extract shared.strings
c.AddReplyStr("$-1\r\n")
} else if val.Type_ != GSTR {
//TODO: extract shared.strings
c.AddReplyStr("-ERR: wrong type\r\n")
} else {
str := val.StrVal()
c.AddReplyStr(fmt.Sprintf("$%d%v\r\n", len(str), str))
}
}
func setCommand(c *GodisClient) {
key := c.args[1]
val := c.args[2]
if val.Type_ != GSTR {
//TODO: extract shared.strings
c.AddReplyStr("-ERR: wrong type\r\n")
}
server.db.data.Set(key, val)
server.db.expire.Delete(key)
c.AddReplyStr("+OK\r\n")
}
func expireCommand(c *GodisClient) {
key := c.args[1]
val := c.args[2]
if val.Type_ != GSTR {
//TODO: extract shared.strings
c.AddReplyStr("-ERR: wrong type\r\n")
}
expire := GetMsTime() + (val.IntVal() * 1000)
expObj := CreateFromInt(expire)
server.db.expire.Set(key, expObj)
expObj.DecrRefCount()
c.AddReplyStr("+OK\r\n")
}
func lookupCommand(cmdStr string) *GodisCommand {
for _, c := range cmdTable {
if c.name == cmdStr {
return &c
}
}
return nil
}
func (c *GodisClient) AddReply(o *Gobj) {
c.reply.Append(o)
o.IncrRefCount()
server.aeLoop.AddFileEvent(c.fd, AE_WRITABLE, SendReplyToClient, c)
}
func (c *GodisClient) AddReplyStr(str string) {
o := CreateObject(GSTR, str)
c.AddReply(o)
o.DecrRefCount()
}
func ProcessCommand(c *GodisClient) {
cmdStr := c.args[0].StrVal()
log.Printf("process command: %v\n", cmdStr)
if cmdStr == "quit" {
freeClient(c)
return
}
cmd := lookupCommand(cmdStr)
if cmd == nil {
c.AddReplyStr("-ERR: unknow command")
resetClient(c)
return
} else if cmd.arity != len(c.args) {
c.AddReplyStr("-ERR: wrong number of args")
resetClient(c)
return
}
cmd.proc(c)
resetClient(c)
}
func freeArgs(client *GodisClient) {
for _, v := range client.args {
v.DecrRefCount()
}
}
func freeReplyList(client *GodisClient) {
for client.reply.length != 0 {
n := client.reply.head
client.reply.DelNode(n)
n.Val.DecrRefCount()
}
}
func freeClient(client *GodisClient) {
freeArgs(client)
delete(server.clients, client.fd)
server.aeLoop.RemoveFileEvent(client.fd, AE_READABLE)
server.aeLoop.RemoveFileEvent(client.fd, AE_WRITABLE)
freeReplyList(client)
Close(client.fd)
}
func resetClient(client *GodisClient) {
freeArgs(client)
client.cmdTy = COMMAND_UNKNOWN
client.bulkLen = 0
client.bulkNum = 0
}
func (client *GodisClient) findLineInQuery() (int, error) {
index := strings.Index(string(client.queryBuf[:client.queryLen]), "\r\n")
if index < 0 && client.queryLen > GODIS_MAX_INLINE {
return index, errors.New("too big inline cmd")
}
return index, nil
}
func (client *GodisClient) getNumInQuery(s, e int) (int, error) {
num, err := strconv.Atoi(string(client.queryBuf[s:e]))
client.queryBuf = client.queryBuf[e+2:]
client.queryLen -= e + 2
return num, err
}
func handleInlineBuf(client *GodisClient) (bool, error) {
index, err := client.findLineInQuery()
if index < 0 {
return false, err
}
subs := strings.Split(string(client.queryBuf[:index]), " ")
client.queryBuf = client.queryBuf[index+2:]
client.queryLen -= index + 2
client.args = make([]*Gobj, len(subs))
for i, v := range subs {
client.args[i] = CreateObject(GSTR, v)
}
return true, nil
}
func handleBulkBuf(client *GodisClient) (bool, error) {
// read bulk num
if client.bulkNum == 0 {
index, err := client.findLineInQuery()
if index < 0 {
return false, err
}
bnum, err := client.getNumInQuery(1, index)
if err != nil {
return false, err
}
if bnum == 0 {
return true, nil
}
client.bulkNum = bnum
client.args = make([]*Gobj, bnum)
}
// read every bulk string
for client.bulkNum > 0 {
// read bulk length
if client.bulkLen == 0 {
index, err := client.findLineInQuery()
if index < 0 {
return false, err
}
if client.queryBuf[0] != '$' {
return false, errors.New("expect $ for bulk length")
}
blen, err := client.getNumInQuery(1, index)
if err != nil || blen == 0 {
return false, err
}
if blen > GODIS_MAX_BULK {
return false, errors.New("too big bulk")
}
client.bulkLen = blen
}
// read bulk string
if client.queryLen < client.bulkLen+2 {
return false, nil
}
index := client.bulkLen
if client.queryBuf[index] != '\r' || client.queryBuf[index+1] != '\n' {
return false, errors.New("expect CRLF for bulk end")
}
client.args[len(client.args)-client.bulkNum] = CreateObject(GSTR, string(client.queryBuf[:index]))
client.queryBuf = client.queryBuf[index+2:]
client.queryLen -= index + 2
client.bulkLen = 0
client.bulkNum -= 1
}
// complete reading every bulk
return true, nil
}
func ProcessQueryBuf(client *GodisClient) error {
for client.queryLen > 0 {
if client.cmdTy == COMMAND_UNKNOWN {
if client.queryBuf[0] == '*' {
client.cmdTy = COMMAND_BULK
} else {
client.cmdTy = COMMAND_INLINE
}
}
// trans query -> args
var ok bool
var err error
if client.cmdTy == COMMAND_INLINE {
ok, err = handleInlineBuf(client)
} else if client.cmdTy == COMMAND_BULK {
ok, err = handleBulkBuf(client)
} else {
return errors.New("unknow Godis Command Type")
}
if err != nil {
return err
}
// after query -> args
if ok {
if len(client.args) == 0 {
resetClient(client)
} else {
ProcessCommand(client)
}
} else {
// cmd incomplete
break
}
}
return nil
}
func ReadQueryFromClient(loop *AeLoop, fd int, extra interface{}) {
client := extra.(*GodisClient)
if len(client.queryBuf)-client.queryLen < GODIS_MAX_BULK {
client.queryBuf = append(client.queryBuf, make([]byte, GODIS_MAX_BULK)...)
}
n, err := Read(fd, client.queryBuf[client.queryLen:])
if err != nil {
log.Printf("client %v read err: %v\n", fd, err)
freeClient(client)
return
}
client.queryLen += n
log.Printf("read %v bytes from client:%v\n", n, client.fd)
log.Printf("ReadQueryFromClient, queryBuf : %v\n", string(client.queryBuf))
err = ProcessQueryBuf(client)
if err != nil {
log.Printf("process query buf err: %v\n", err)
freeClient(client)
return
}
}
func SendReplyToClient(loop *AeLoop, fd int, extra interface{}) {
client := extra.(*GodisClient)
log.Printf("SendReplyToClient, reply len:%v\n", client.reply.Length())
for client.reply.Length() > 0 {
rep := client.reply.First()
buf := []byte(rep.Val.StrVal())
bufLen := len(buf)
if client.sentLen < bufLen {
n, err := Write(fd, buf[client.sentLen:])
if err != nil {
log.Printf("send reply err: %v\n", err)
freeClient(client)
return
}
client.sentLen += n
log.Printf("send %v bytes to client:%v\n", n, client.fd)
if client.sentLen == bufLen {
client.reply.DelNode(rep)
rep.Val.DecrRefCount()
client.sentLen = 0
} else {
break
}
}
}
if client.reply.Length() == 0 {
client.sentLen = 0
loop.RemoveFileEvent(fd, AE_WRITABLE)
}
}
func GStrEqual(a, b *Gobj) bool {
if a.Type_ != GSTR || b.Type_ != GSTR {
return false
}
return a.StrVal() == b.StrVal()
}
func GStrHash(key *Gobj) int64 {
if key.Type_ != GSTR {
return 0
}
hash := fnv.New64()
hash.Write([]byte(key.StrVal()))
return int64(hash.Sum64())
}
func CreateClient(fd int) *GodisClient {
var client GodisClient
client.fd = fd
client.db = server.db
client.queryBuf = make([]byte, GODIS_IO_BUF)
client.reply = ListCreate(ListType{EqualFunc: GStrEqual})
return &client
}
func AcceptHandler(loop *AeLoop, fd int, extra interface{}) {
cfd, err := Accept(fd)
if err != nil {
log.Printf("accept err: %v\n", err)
return
}
client := CreateClient(cfd)
//TODO: check max clients limit
server.clients[cfd] = client
server.aeLoop.AddFileEvent(cfd, AE_READABLE, ReadQueryFromClient, client)
log.Printf("accept client, fd: %v\n", cfd)
}
const EXPIRE_CHECK_COUNT int = 100
// background job, runs every 100ms
func ServerCron(loop *AeLoop, id int, extra interface{}) {
for i := 0; i < EXPIRE_CHECK_COUNT; i++ {
entry := server.db.expire.RandomGet()
if entry == nil {
break
}
if entry.Val.IntVal() < time.Now().Unix() {
server.db.data.Delete(entry.Key)
server.db.expire.Delete(entry.Key)
}
}
}
func initServer(config *Config) error {
server.port = config.Port
server.clients = make(map[int]*GodisClient)
server.db = &GodisDB{
data: DictCreate(DictType{HashFunc: GStrHash, EqualFunc: GStrEqual}),
expire: DictCreate(DictType{HashFunc: GStrHash, EqualFunc: GStrEqual}),
}
var err error
if server.aeLoop, err = AeLoopCreate(); err != nil {
return err
}
server.fd, err = TcpServer(server.port)
return err
}
func main() {
path := os.Args[1]
config, err := LoadConfig(path)
if err != nil {
log.Printf("config error: %v\n", err)
}
err = initServer(config)
if err != nil {
log.Printf("init server error: %v\n", err)
}
server.aeLoop.AddFileEvent(server.fd, AE_READABLE, AcceptHandler, nil)
server.aeLoop.AddTimeEvent(AE_NORMAL, 100, ServerCron, nil)
log.Println("godis server is up.")
server.aeLoop.AeMain()
}