-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
1303 lines (1175 loc) · 38.6 KB
/
server.go
File metadata and controls
1303 lines (1175 loc) · 38.6 KB
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"errors"
"fmt"
"github.com/pengdafu/redis-golang/adlist"
"github.com/pengdafu/redis-golang/ae"
"github.com/pengdafu/redis-golang/anet"
"github.com/pengdafu/redis-golang/dict"
"github.com/pengdafu/redis-golang/sds"
"github.com/pengdafu/redis-golang/util"
"log"
"os"
"runtime/debug"
"strings"
"syscall"
"time"
"unsafe"
)
const (
CONFIG_DEFAULT_HZ = 10
CRON_DBS_PER_CALL = 16
CONFIG_MIN_RESERVED_FDS = 32
CONFIG_FDSET_INCR = CONFIG_MIN_RESERVED_FDS + 96
CONFIG_BINDADDR_MAX = 16
CONFIG_RUN_ID_SIZE = 40
)
const (
PROTO_REPLY_CHUNK_BYTES = 16 * 1024
PROTO_IOBUF_LEN
PROTO_INLINE_MAX_SIZE
PROTO_MBULK_BIG_ARG = 32 * 1024
)
const (
PROTO_REQ_INLINE = 1
PROTO_REQ_MULTIBULK = 2
)
const (
USER_FLAG_ENABLED = 1 << iota
USER_FLAG_DISABLED
USER_FLAG_ALLKEYS
USER_FLAG_ALLCOMMANDS
USER_FLAG_NOPASS
USER_FLAG_ALLCHANNELS
USER_FLAG_SANITIZE_PAYLOAD
USER_FLAG_SANITIZE_PAYLOAD_SKIP
)
const (
REPL_STATE_NONE = iota
REPL_STATE_CONNECT
REPL_STATE_CONNECTING
REPL_STATE_RECEIVE_PING_REPLY
REPL_STATE_SEND_HANDSHAKE
REPL_STATE_RECEIVE_AUTH_REPLY
REPL_STATE_RECEIVE_PORT_REPLY
REPL_STATE_RECEIVE_IP_REPLY
REPL_STATE_RECEIVE_CAPA_REPLY
REPL_STATE_SEND_PSYNC
REPL_STATE_RECEIVE_PSYNC_REPLY
REPL_STATE_TRANSFER
REPL_STATE_CONNECTED
)
const (
SLAVE_CAPA_NONE = 0
SLAVE_CAPA_EOF = 1 << iota
SLAVE_CAPA_PSYNC2
)
const (
SLAVE_STATE_ONLINE = 9
)
const (
BLOCKED_NONE = iota
BLOCKED_LIST
BLOCKED_WAIT
BLOCKED_MODULE
BLOCKED_STREAM
BLOCKED_ZSET
BLOCKED_PAUSE
BLOCKED_NUM
)
const (
CLIENT_TYPE_NORMAL = iota
CLIENT_TYPE_SLAVE
CLIENT_TYPE_PUBSUB
CLIENT_TYPE_MASTER
CLIENT_TYPE_COUNT
CLIENT_TYPE_OBUF_COUNT = 3
)
const (
CLIENT_SLAVE = 1 << iota
CLIENT_MASTER
CLIENT_MONITOR
CLIENT_MULTI
CLIENT_BLOCKED
CLIENT_DIRTY_CAS
CLIENT_CLOSE_AFTER_REPLY
CLIENT_UNBLOCKED
CLIENT_LUA
CLIENT_ASKING
CLIENT_CLOSE_ASAP
CLIENT_UNIX_SOCKET
CLIENT_DIRTY_EXEC
CLIENT_MASTER_FORCE_REPLY
CLIENT_FORCE_AOF
CLIENT_FORCE_REPL
CLIENT_PRE_PSYNC
CLIENT_READONLY
CLIENT_PUBSUB
CLIENT_PREVENT_AOF_PROP
CLIENT_PREVENT_REPL_PROP
CLIENT_PENDING_WRITE
CLIENT_REPLY_OFF
CLIENT_REPLY_SKIP_NEXT
CLIENT_REPLY_SKIP
CLIENT_LUA_DEBUG
CLIENT_LUA_DEBUG_SYNC
CLIENT_MODULE
CLIENT_PROTECTED
CLIENT_PENDING_READ
CLIENT_PENDING_COMMAND
CLIENT_TRACKING
CLIENT_TRACKING_BROKEN_REDIR
CLIENT_TRACKING_BCAST
CLIENT_TRACKING_OPTIN
CLIENT_TRACKING_OPTOUT
CLIENT_TRACKING_CACHING
CLIENT_TRACKING_NOLOOP
CLIENT_IN_TO_TABLE
CLIENT_PROTOCOL_ERROR
CLIENT_CLOSE_AFTER_COMMAND
CLIENT_DENY_BLOCKING
CLIENT_REPL_RDBONLY
CLIENT_PREVENT_PROP = CLIENT_PREVENT_AOF_PROP | CLIENT_PREVENT_REPL_PROP
)
const (
MaxMemoryFlagLru = 1 << 0
MaxMemoryFlagLfu = 1 << 1
MaxMemoryFlagAllKeys = 1 << 2
MaxMemoryFlagNoSharedIntegers = MaxMemoryFlagLru | MaxMemoryFlagLfu
)
// server static configuration
const (
ObjSharedIntegers = 10000
ObjSharedBulkHdrLen = 32
ProtoSharedSelectCmds = 10
)
var (
C_OK error = nil
C_ERR error = errors.New("error")
)
// RedisModuleUserChangedFunc 会在每次调用moduleNotifyUserChanged()函数后执行
/**
* a user authenticated via the module API is associated with a different
* user or gets disconnected. This needs to be exposed since you can't cast
* a function pointer to (void *).
*/
type RedisModuleUserChangedFunc func(clientId uint64, privateData interface{})
type socketFds struct {
fd [CONFIG_BINDADDR_MAX]int
count int
}
type RedisServer struct {
el *ae.EventLoop
masterhost string
commands *dict.Dict
origCommands *dict.Dict
protectedMode int
port int
ipfd *socketFds
tcpBacklog int
bindAddr [CONFIG_BINDADDR_MAX]string
bindAddrCount int
hashMaxZipListValue int // 超过64字节转ht
hashMaxZipListEntries int // 超过512个元素转ht
setMaxIntSetEntries int // 超过512个元素转ht
zsetMaxZipListEntries int // 超过128个转skiplist
zsetMaxZipListValue int // 超过64字节转skiplist
clients []*Client
currentClient *Client
clusterEnabled bool
lazyFreeLazyUserDel bool
lazyFreeLazyExpire bool
lazyFreeLazyServerDel bool
loading bool
activeExpireEnabled bool
activeRehashing bool
activeExpireEffort int
statRejectedConn uint64 // 拒绝客户端连接的次数
statNumConnections uint64 // 成功连接客户端的次数
statTotalReadsProcessed uint64 // 成功处理read的次数
statExpiredKeys uint64 // 成功处理过期key的次数
statExpiredStalePerc int // 成功处理过期key的次数
statExpiredTimeCapReachedCount int
tcpKeepalive int
maxclients int
protoMaxBulkLen int64
clientMaxQueryBufLen int
dirty int
maxMemoryPolicy int
maxMemory int64
lruClock uint32
hz int
cronLoops int
dbnum int
db []*redisDb
nextClientId uint64
unixtime int64 // 秒
ustime int64 // 微秒
mstime int64 // 毫秒
luaTimeout uint32
luaCaller bool
luaTimeStart int64
fixedTimeExpire int64
clientsPendWrite *adlist.List
readyKeys *adlist.List
aofState int
aofFsync int
statNetOutputBytes int
rdbChildPid, aofChildPid, moduleChildPid int
delCommand *redisCommand
slaves *adlist.List
}
const (
aofOff = iota
aofOn
aofWaitRewrite
)
const (
aofFsyncOn = iota
aofFsyncAlways
aofFsyncEverySec
)
const (
unitSeconds = 0
unitMilliSeconds = 1
)
var server *RedisServer
type Client struct {
id uint64 // 自增唯一ID
conn *Connection
resp int // resp 协议版本,可以是2或者3
db *redisDb
name *robj // 客户端名字,通过SETNAME设置
querybuf sds.SDS // 缓存客户端请求的buf
qbPos int // querybuf 读到的位置
pendingQueryBuf sds.SDS // 如果此客户端被标记为主服务器,则此缓冲区表示从主服务器接收的复制流中尚未应用的部分。
querybufPeak int // 最近100ms或者更长时间querybuf的峰值
argc int // 当前command有多少个参数
argv []*robj // 当前command的参数列表
originalArgc int // 如果参数被重写,原始参数的个数
originalArgv []*robj // 如果参数被重写,原始的参数列表
argvLenSum int // len(argv)
cmd, lastCmd *redisCommand // 最后一次执行的command
user *user // user与connect关联,如果为nil,则代表是admin,可以做任何操作
reqType uint8 // request protocol type: PROTO_REQ_*
multiBulkLen int // 要读取的多个批量参数的数量
bulkLen int // 批量请求的参数长度
reply *adlist.List
replyBytes int // 要响应的字节长度
sentLen int // 当前缓冲区或者正在发送中的对象已经发送的字节数
ctime int64 // 客户端创建时间
duration int64 // 当前command的运行时间,用来阻塞或非阻塞命令的延迟
lastInteraction int64 // 上次交互时间,用于超时,单位秒
obufSoftLimitReachedTime time.Duration
flags int // 客户端的flag,CLIENT_* 宏定义
authenticated bool // 当默认用户需要认证
replState int // 如果client是一个从节点,则为从节点的复制状态
replPutOnlineOnAck int // 在第一个ACK的时候,安装从节点的写处理器
replDBFd int // 复制database 的文件描述符
replDBOff int // 复制database的文件的偏移
replDBSize int // 复制db的文件的大小
replPreamble int // 复制DB序言
readReplOff int // Read replication offset if this is a master
replOff int // Applied replication offset if this is a master
replAckOff int // Replication ack offset, if this is a slave
replAckTime int64 // Replication ack time, if this is a slave
psyncInitialOffset int // FULLRESYNC reply offset other slaves copying this slave output buffer should use.
replId [CONFIG_RUN_ID_SIZE + 1]byte // 主复制Id,如果是主节点
slaveListeningPort int // As configured with: REPLCONF listening-port
slaveAddr string // Optionally given by REPLCONF ip-address
slaveCapa int // 从节点容量:SLAVE_CAPA_* bitwise OR
mstate multiState // MULTI/EXEC state
bType int // 如果是CLIENT_BLOCKED类型,表示阻塞
bpop blockingState // blocking state
woff int // 最后一次写的全局复制偏移量
watchedKeys *adlist.List // Keys WATCHED for MULTI/EXEC CAS
pubSubChannels *dict.Dict // 客户端关注的渠道(SUBSCRIBE)
pubSubPatterns *adlist.List // 客户端关注的模式(SUBSCRIBE)
peerId sds.SDS // Cached peer ID
sockName sds.SDS // Cached connection target address.
clientListNode *adlist.ListNode //list node in client list
pausedListNode *adlist.ListNode //list node within the pause list
authCallback RedisModuleUserChangedFunc // 当认证的用户被改变是,回调模块将被执行
authCallbackPrivdata interface{} // 当auth回调被执行的时候,该值当参数传递过去
authModule interface{} // 拥有回调函数的模块,当模块被卸载进行清理时,该模块用于断开客户端。不透明的Redis核心
clientTrackingRedirection uint64 // 如果处于追踪模式并且该字段不为0,那么该客户端获取keys的无效信息,将会发送到特殊的clientId
clientTrackingPrefixes *rax // 在客户端缓存上下文中,我们在BCAST模式下已经订阅的前缀字典
clientCronLastMemoryUsage uint64 //
clientCronLastMemoryType int
// response buf
bufpos int
buf [PROTO_REPLY_CHUNK_BYTES]byte
}
const MaxKeysBuffer = 256
type getKeysResult struct {
keysBuf [MaxKeysBuffer]int
keys []int
numKeys int
size int
}
type RedisCommandProc func(c *Client)
type RedisGetKeysProc func(cmd *redisCommand, argv []*robj, argc int) (*getKeysResult, error)
type redisCommand struct {
name string
proc RedisCommandProc
arity int
sflags string
flags uint64
getKeysProc RedisGetKeysProc
firstKey, lastKey, keyStep int
microseconds, calls uint64
id int
}
type user struct {
flags uint64
}
type multiState struct {
cmdFlags uint64
}
type blockingState struct {
timeout time.Duration
keys *dict.Dict
target *robj
listPos struct {
wherefrom int
whereto int
}
xReadCount int
xReadGroup *robj
xReadConsumer *robj
xReadGroupNoAck int
numReplicas int
replOffset uint
moduleBlockedHandle interface{}
}
func New() *RedisServer {
server = new(RedisServer)
return server
}
func (server *RedisServer) InitServer() {
dict.SetHashFunctionSeed(util.GetRandomBytes(16))
initServerConfig()
var err error
createSharedObjects()
// 创建aeEventLoop
server.el, err = ae.CreateEventLoop(server.maxclients + CONFIG_FDSET_INCR)
if err != nil {
panic(fmt.Sprintf("create aeEventLoop err: %v", err))
}
server.db = make([]*redisDb, server.dbnum)
for i := 0; i < server.dbnum; i++ {
db := &redisDb{}
db.dict = dict.Create(dbDictType, nil)
db.expires = dict.Create(keyPtrDictType, nil)
db.blockingKeys = dict.Create(keyListDictType, nil)
db.watchedKeys = dict.Create(keyListDictType, nil)
db.readyKeys = dict.Create(objectKeyPointValueDictType, nil)
db.id = i
db.defragLater = adlist.Create()
server.db[i] = db
}
server.el.AeSetBeforeSleepProc(beforeSleep)
if server.port != 0 {
if err := server.listenToPort(server.port, server.ipfd); err != C_OK {
log.Println(err)
os.Exit(1)
}
}
// 创建aeTimeEvent
if err := server.el.CreateTimeEvent(1, server.serverCron, nil, nil); err == ae.ERR {
panic("Can't create event loop timer.")
}
// 创建连接处理
if server.createSocketAcceptHandler(server.ipfd, acceptTcpHandler) != C_OK {
panic("Unrecoverable error creating TCP socket accept handler.")
}
}
func initServerConfig() {
server.ipfd = new(socketFds)
server.port = 6380
server.bindAddr[0] = "127.0.0.1"
server.bindAddrCount = 1
server.maxclients = 100
server.clientsPendWrite = adlist.Create()
server.readyKeys = adlist.Create()
server.hz = 10
server.clientMaxQueryBufLen = 1024 * 1024
server.dbnum = 16
server.protoMaxBulkLen = 1024 * 1024
server.activeExpireEnabled = true
server.activeRehashing = true
server.hashMaxZipListValue = 64
server.hashMaxZipListEntries = 512
server.setMaxIntSetEntries = 512
server.zsetMaxZipListEntries = 128
server.zsetMaxZipListValue = 64
server.activeExpireEffort = 1
server.rdbChildPid = -1
server.moduleChildPid = -1
server.aofChildPid = -1
server.commands = dict.Create(commandTableDictType, nil)
server.origCommands = dict.Create(commandTableDictType, nil)
populateCommandTable()
}
func (server *RedisServer) Start() {
defer func() {
if err := recover(); err != nil {
log.Println("recovery err: ", err)
debug.PrintStack()
server.Stop()
}
}()
server.el.AeMain()
}
func (server *RedisServer) Stop() {
for i := 0; i < server.ipfd.count; i++ {
syscall.Close(server.ipfd.fd[i])
}
server.el.AeDeleteEventLoop()
}
// todo serverCron
func (server *RedisServer) serverCron(el *ae.EventLoop, id int64, clientData interface{}) int {
//if server.watchDogPeriod > 0 {
// watchdogScheduleSignal(server.watchDogPeriod)
//}
updateCachedTime(1)
//server.hz = server.config_hz
if runWithPeriod(100) { // metrics
}
if runWithPeriod(5000) {
for j := 0; j < server.dbnum; j++ {
size := server.db[j].dict.Slots()
used := server.db[j].dict.Size()
vkeys := server.db[j].expires.Size()
if size > 0 || used > 0 || vkeys > 0 {
log.Printf("DB %d: %d keys (%d volatile) in %d slots HT.\n", j, used, vkeys, size)
}
}
}
databaseCron()
server.lruClock = getLRUClock()
server.cronLoops++
return 1000 / server.hz
}
func databaseCron() {
if server.activeExpireEnabled {
if iAmMaster() {
activeExpireCycle(activeExpireCycleSlow)
} else {
expireSlaveKeys()
}
}
//activeDefragCycle()
if !hasActiveChildProcess() {
var resizeDb = 0
var rehashDb = 0
dbsPereCall := CRON_DBS_PER_CALL
if dbsPereCall > server.dbnum {
dbsPereCall = server.dbnum
}
for j := 0; j < dbsPereCall; j++ {
tryResizeHashTables(resizeDb % server.dbnum)
resizeDb++
}
if server.activeRehashing {
for j := 0; j < dbsPereCall; j++ {
if incrementallyRehash(rehashDb) {
break
} else {
rehashDb++
rehashDb %= server.dbnum
}
}
}
}
}
func tryResizeHashTables(dbId int) {
if htNeedResize(server.db[dbId].dict) {
server.db[dbId].dict.Resize()
}
if htNeedResize(server.db[dbId].expires) {
server.db[dbId].expires.Resize()
}
}
func incrementallyRehash(dbId int) bool {
if server.db[dbId].dict.IsRehashing() {
server.db[dbId].dict.RehashMilliseconds(1)
return true
}
if server.db[dbId].expires.IsRehashing() {
server.db[dbId].expires.RehashMilliseconds(1)
return true
}
return false
}
func htNeedResize(dt *dict.Dict) bool {
size := dt.Slots()
used := dt.Size()
return size > dict.HtInitialSize && used*100/size < 10
}
func iAmMaster() bool {
return (!server.clusterEnabled && server.masterhost == "") ||
(server.clusterEnabled && nodeIsMaster(nil))
}
func runWithPeriod(_ms_ int) bool {
return _ms_ <= 1000/server.hz || server.cronLoops%(_ms_/(1000/server.hz)) == 0
}
func updateCachedTime(updateDayLightInfo int) {
server.ustime = ustime()
server.mstime = server.ustime / 1000
server.unixtime = server.mstime / 1000
//if (update_daylight_info) {
// struct tm tm;
// time_t ut = server.unixtime;
// localtime_r(&ut,&tm);
// server.daylight_active = tm.tm_isdst;
//}
}
func (server *RedisServer) createSocketAcceptHandler(sfd *socketFds, accessHandle ae.FileProc) error {
for i := 0; i < sfd.count; i++ {
if err := server.el.AeCreateFileEvent(sfd.fd[i], ae.Readable, accessHandle, nil); err != nil {
for j := i - 1; j >= 0; j-- {
server.el.AeDeleteFileEvent(sfd.fd[j], ae.Readable)
}
}
}
return nil
}
func (server *RedisServer) listenToPort(port int, sfd *socketFds) (err error) {
bindAddr := server.bindAddr
bindAddrCount := server.bindAddrCount
defaultBindAddr := [2]string{"*", "-::*"}
if server.bindAddrCount == 0 {
bindAddrCount = 2
bindAddr[0], bindAddr[1] = defaultBindAddr[0], defaultBindAddr[1]
}
for j := 0; j < bindAddrCount; j++ {
addr := bindAddr[j]
if strings.Contains(addr, ":") {
sfd.fd[sfd.count], err = anet.Tcp6Server(port, addr, server.tcpBacklog)
} else {
sfd.fd[sfd.count], err = anet.TcpServer(port, addr, server.tcpBacklog)
}
if err != nil {
server.closeSocketListeners(sfd)
return err
}
_ = anet.NonBlock(sfd.fd[sfd.count])
_ = anet.Cloexec(sfd.fd[sfd.count])
sfd.count++
}
return nil
}
func (server *RedisServer) closeSocketListeners(sfd *socketFds) {
for i := 0; i < sfd.count; i++ {
if sfd.fd[i] == -1 {
continue
}
server.el.AeDeleteFileEvent(sfd.fd[i], ae.Readable)
}
sfd.count = 0
}
func (server *RedisServer) connSocketClose(conn *Connection) {
}
var redisCommandTable = []redisCommand{
//{"module", moduleCommand, -2,
// "admin no-script",
// 0, nil, 0, 0, 0, 0, 0, 0},
{"select", selectCommand, 2,
"ok-loading fast ok-stale @keyspace",
0, nil, 0, 0, 0, 0, 0, 0},
{"expire", expireCommand, 3,
"write fast @keyspace",
0, nil, 1, 1, 1, 0, 0, 0},
{"get", getCommand, 2,
"read-only fast @string",
0, nil, 1, 1, 1, 0, 0, 0},
/* Note that we can't flag set as fast, since it may perform an
* implicit DEL of a large key. */
{"set", setCommand, -3,
"write use-memory @string",
0, nil, 1, 1, 1, 0, 0, 0},
//{"exec", execCommand, 1,
// "no-script no-monitor no-slowlog ok-loading ok-stale @transaction",
// 0, nil, 0, 0, 0, 0, 0, 0},
{"del", delCommand, -2,
"write @keyspace",
0, nil, 1, -1, 1, 0, 0, 0},
{"unlink", unlinkCommand, -2,
"write fast @keyspace",
0, nil, 1, -1, 1, 0, 0, 0},
{"hset", hsetCommand, -4,
"write use-memory fast @hash",
0, nil, 1, 1, 1, 0, 0, 0},
{"hmset", hsetCommand, -4,
"write use-memory fast @hash",
0, nil, 1, 1, 1, 0, 0, 0},
{"hget", hgetCommand, 3,
"read-only fast @hash",
0, nil, 1, 1, 1, 0, 0, 0},
{"hmget", hmgetCommand, -3,
"read-only fast @hash",
0, nil, 1, 1, 1, 0, 0, 0},
{"hdel", hdelCommand, -3,
"write fast @hash",
0, nil, 1, 1, 1, 0, 0, 0},
{"hkeys", hkeysCommand, 2,
"read-only to-sort @hash",
0, nil, 1, 1, 1, 0, 0, 0},
{"hvals", hvalsCommand, 2,
"read-only to-sort @hash",
0, nil, 1, 1, 1, 0, 0, 0},
{"hgetall", hgetallCommand, 2,
"read-only random @hash",
0, nil, 1, 1, 1, 0, 0, 0},
{"sadd", saddCommand, -3,
"write use-memory fast @set",
0, nil, 1, 1, 1, 0, 0, 0},
{"srem", sremCommand, -3,
"write fast @set",
0, nil, 1, 1, 1, 0, 0, 0},
{"scard", scardCommand, 2,
"read-only fast @set",
0, nil, 1, 1, 1, 0, 0, 0},
{"sinter", sinterCommand, -2,
"read-only to-sort @set",
0, nil, 1, -1, 1, 0, 0, 0},
{"smembers", sinterCommand, 2,
"read-only to-sort @set",
0, nil, 1, 1, 1, 0, 0, 0},
{"zadd", zaddCommand, -4,
"write use-memory fast @sortedset",
0, nil, 1, 1, 1, 0, 0, 0},
{"zincrby", zincrbyCommand, 4,
"write use-memory fast @sortedset",
0, nil, 1, 1, 1, 0, 0, 0},
{"zrem", zremCommand, -3,
"write fast @sortedset",
0, nil, 1, 1, 1, 0, 0, 0},
{"zcount", zcountCommand, 4,
"read-only fast @sortedset",
0, nil, 1, 1, 1, 0, 0, 0},
{"zcard", zcardCommand, 2,
"read-only fast @sortedset",
0, nil, 1, 1, 1, 0, 0, 0},
{"zscore", zscoreCommand, 3,
"read-only fast @sortedset",
0, nil, 1, 1, 1, 0, 0, 0},
}
func populateCommandTable() {
for i := 0; i < len(redisCommandTable); i++ {
c := &redisCommandTable[i]
if populateCommandTableParseFlags(c, c.sflags) != C_OK {
panic("Unsupported command flag")
}
c.id = ACLGetCommandId(c.name)
s := sds.NewLen(c.name)
if server.commands.Add(unsafe.Pointer(&s), unsafe.Pointer(c)) &&
server.origCommands.Add(unsafe.Pointer(&s), unsafe.Pointer(c)) {
continue
}
panic("add command to server.commands err")
}
}
// todo rax tree
var m = map[string]int{}
var commandId int
func ACLGetCommandId(name string) int {
if id, ok := m[name]; ok {
return id
}
commandId++
m[name] = commandId
return commandId
}
const (
CmdWrite = 1 << iota
CmdReadOnly
CmdDenyOom
CmdModule
CmdAdmin
CmdPubSub
CmdNoScript
CmdRandom
CmdSortForScript
CmdLoading
CmdStale
CmdSkipMonitor
CmdSkipSlowLog
CmdAsking
CmdFast
CmdNoAuth
CmdModuleGetKeys
CmdModuleNoCluster
CmdCategoryKeySpace
CmdCategoryRead
CmdCategoryWrite
CmdCategorySet
CmdCategorySortedSet
CmdCategoryList
CmdCategoryHash
CmdCategoryString
CmdCategoryBitmap
CmdCategoryHyperloglog
CmdCategoryGeo
CmdCategoryStream
CmdCategoryPubSub
CmdCategoryAdmin
CmdCategoryFast
CmdCategorySlow
CmdCategoryBlocking
CmdCategoryDangerous
CmdCategoryConnection
CmdCategoryTransaction
CmdCategoryScripting
)
func populateCommandTableParseFlags(c *redisCommand, sflags string) error {
argv, argc := sds.SplitArgs(sds.NewLen(sflags))
if argv == nil {
return C_ERR
}
for i := 0; i < argc; i++ {
flag := argv[i].BufData(0)
if util.StrCmp(flag, "write") {
c.flags |= CmdWrite | CmdCategoryWrite
} else if util.StrCmp(flag, "read-only") {
c.flags |= CmdReadOnly | CmdCategoryRead
} else if util.StrCmp(flag, "use-memory") {
c.flags |= CmdDenyOom
} else if util.StrCmp(flag, "admin") {
c.flags |= CmdAdmin | CmdCategoryAdmin | CmdCategoryDangerous
} else if util.StrCmp(flag, "pubsub") {
c.flags |= CmdPubSub | CmdCategoryPubSub
} else if util.StrCmp(flag, "no-script") {
c.flags |= CmdNoScript
} else if util.StrCmp(flag, "random") {
c.flags |= CmdRandom
} else if util.StrCmp(flag, "to-sort") {
c.flags |= CmdSortForScript
} else if util.StrCmp(flag, "ok-loading") {
c.flags |= CmdLoading
} else if util.StrCmp(flag, "ok-stale") {
c.flags |= CmdStale
} else if util.StrCmp(flag, "no-monitor") {
c.flags |= CmdSkipMonitor
} else if util.StrCmp(flag, "no-slowlog") {
c.flags |= CmdSkipSlowLog
} else if util.StrCmp(flag, "cluster-asking") {
c.flags |= CmdAsking
} else if util.StrCmp(flag, "fast") {
c.flags |= CmdFast | CmdCategoryFast
} else if util.StrCmp(flag, "no-auth") {
c.flags |= CmdNoAuth
} else {
if catflag := ACLGetCommandCategoryFlagByName(string(flag[1:])); catflag > 0 && flag[0] == '@' {
c.flags |= catflag
} else {
return C_ERR
}
}
}
if c.flags&CmdCategoryFast == 0 {
c.flags |= CmdCategorySlow
}
return C_OK
}
var ACLCommandCategories = []struct {
name string
flag uint64
}{
{"keyspace", CmdCategoryKeySpace},
{"read", CmdCategoryRead},
{"write", CmdCategoryWrite},
{"set", CmdCategorySet},
{"sortedset", CmdCategorySortedSet},
{"list", CmdCategoryList},
{"hash", CmdCategoryHash},
{"string", CmdCategoryString},
{"bitmap", CmdCategoryBitmap},
{"hyperloglog", CmdCategoryHyperloglog},
{"geo", CmdCategoryGeo},
{"stream", CmdCategoryStream},
{"pubsub", CmdCategoryPubSub},
{"admin", CmdCategoryAdmin},
{"fast", CmdCategoryFast},
{"slow", CmdCategorySlow},
{"blocking", CmdCategoryBlocking},
{"dangerous", CmdCategoryDangerous},
{"connection", CmdCategoryConnection},
{"transaction", CmdCategoryTransaction},
{"scripting", CmdCategoryScripting},
}
func ACLGetCommandCategoryFlagByName(flag string) uint64 {
for _, category := range ACLCommandCategories {
if category.flag > 0 && category.name == flag {
return category.flag
}
}
return 0
}
// clientReplyBlock.size = cap(buf)
type clientReplyBlock struct {
//size int
used int
buf []byte
}
const (
CmdCallNone = 0
CmdCallSlowLog = 1 << iota
CmdCallStats
CmdCallPropacateAof
CmdCallPropacateRepl
CmdCallNowRap
CmdCallPropacate = CmdCallPropacateAof | CmdCallPropacateRepl
CmdCallFull = CmdCallSlowLog | CmdCallStats | CmdCallPropacate
)
func processCommand(c *Client) error {
moduleCallCommandFilters(c)
if util.StrCmp((*sds.SDS)(c.argv[0].ptr).BufData(0), "quit") {
addReply(c, shared.ok)
c.flags |= CLIENT_CLOSE_AFTER_REPLY
return C_ERR
}
c.cmd = lookupCommand(c.argv[0].ptr)
c.lastCmd = c.cmd
if c.cmd == nil {
ss := make([]sds.SDS, len(c.argv)-1)
for i := 1; i < c.argc; i++ {
ss[i-1] = *(*sds.SDS)(c.argv[i].ptr)
}
args := sds.CatPrintf(ss...)
rejectCommandFormat(c, "unknown command `%s`, with args beginning with: %s",
(*sds.SDS)(c.argv[0].ptr).BufData(0), args)
return C_OK
} else if (c.cmd.arity > 0 && c.cmd.arity != c.argc) || c.argc < -c.cmd.arity {
rejectCommandFormat(c, "wrong number of arguments for '%s' command",
c.cmd.name)
return C_OK
}
//isWriteCommand := c.cmd.flags&CmdWrite > 0 ||
// (c.cmd.name == "exec" && c.mstate.cmdFlags&CmdWrite > 0)
//isDenyOOMCommand := c.cmd.flags&CmdDenyOom > 0 ||
// (c.cmd.name == "exec" && c.mstate.cmdFlags&CmdDenyOom > 0)
//isDenyStaleCommand := c.cmd.flags&CmdStale > 0 ||
// (c.cmd.name == "exec" && c.mstate.cmdFlags&CmdStale > 0)
//isDenyLoadingCommand := c.cmd.flags&CmdLoading > 0 ||
// (c.cmd.name == "exec" && c.mstate.cmdFlags&CmdLoading > 0)
// todo other
if authRequired(c) && c.flags&CmdNoAuth == 0 {
rejectCommand(c, shared.noAuthErr)
return C_OK
}
/**
忽略了很多....todo todo
*/
call(c, CmdCallFull)
return C_OK
}
func call(c *Client, flags int) {
realCmd := c.cmd
start := server.ustime
c.cmd.proc(c)
duration := time.Now().UnixMicro() - start
if flags&CmdCallStats > 0 {
realCmd.calls++
realCmd.microseconds += uint64(duration)
}
}
func rejectCommand(c *Client, reply *robj) {
flagTransaction(c)
if c.cmd != nil && c.cmd.name == "exec" {
execCommandAbort(c, (*sds.SDS)(reply.ptr).BufData(0))
} else {
addReplyErrorObject(c, reply)
}
}
func addReplyErrorObject(c *Client, reply *robj) {
addReply(c, reply)
afterErrorReply(c, (*sds.SDS)(reply.ptr).BufData(0))
}
func rejectCommandFormat(c *Client, format string, vv ...interface{}) {
flagTransaction(c)