-
Notifications
You must be signed in to change notification settings - Fork 4
/
rotom.go
273 lines (234 loc) · 5.65 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
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
package main
import (
"fmt"
"github.com/bytedance/sonic"
"github.com/xgzlucario/rotom/internal/iface"
"github.com/xgzlucario/rotom/internal/net"
"github.com/xgzlucario/rotom/internal/resp"
"io"
"os"
"github.com/xgzlucario/rotom/internal/list"
lua "github.com/yuin/gopher-lua"
)
const (
QueryBufSize = 8 * KB
WriteBufSize = 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
argsBuf []resp.RESP
replyWriter *resp.Writer
}
type Server struct {
fd int
config *Config
aeLoop *AeLoop
clients map[int]*Client
lua *lua.LState
}
type Config struct {
Port int `json:"port"`
AppendOnly bool `json:"appendonly"`
AppendFileName string `json:"appendfilename"`
Save bool `json:"save"`
SaveFileName string `json:"savefilename"`
}
var (
db DB
server Server
)
// InitDB initializes database and redo appendonly files if needed.
func InitDB(config *Config) (err error) {
db.dict = New()
if config.Save {
db.rdb = NewRdb(config.SaveFileName)
log.Debug().Msg("start loading rdb file...")
if err = db.rdb.LoadDB(); err != nil {
return err
}
}
if config.AppendOnly {
db.aof, err = NewAof(config.AppendFileName)
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(WriteBufSize)
return db.aof.Read(func(args []resp.RESP) {
command := args[0].ToStringUnsafe()
cmd, err := lookupCommand(command)
if err == nil {
cmd.process(emptyWriter, args[1:])
emptyWriter.Reset()
}
})
}
return nil
}
func LoadConfig(path string) (config *Config, err error) {
jsonStr, err := os.ReadFile(path)
if err != nil {
return
}
config = &Config{}
if err = sonic.Unmarshal(jsonStr, config); err != nil {
return nil, err
}
return
}
// 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(WriteBufSize),
queryBuf: make([]byte, QueryBufSize),
argsBuf: make([]resp.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)...)
log.Warn().Msgf("client %d queryBuf grow up to size %s", fd, readableSize(len(client.queryBuf)))
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)
}
func ProcessQueryBuf(client *Client) {
queryBuf := client.queryBuf[client.readx:client.recvx]
reader := resp.NewReader(queryBuf)
for {
args, n, err := reader.ReadNextCommand(client.argsBuf)
if err != nil {
if err == io.EOF {
break
}
log.Error().Msgf("read resp error: %v", err)
return
}
client.readx += n
command := args[0].ToStringUnsafe()
args = args[1:]
cmd, err := lookupCommand(command)
if err != nil {
client.replyWriter.WriteError(err)
log.Error().Msg(err.Error())
} else {
cmd.process(client.replyWriter, args)
// write aof file
if cmd.persist && server.config.AppendOnly {
_, _ = db.aof.Write(queryBuf)
}
}
resetClient(client)
server.aeLoop.ModWrite(client.fd, SendReplyToClient, client)
}
}
func SendReplyToClient(loop *AeLoop, fd int, extra interface{}) {
client := extra.(*Client)
sentbuf := client.replyWriter.Bytes()
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(config *Config) (err error) {
server.config = config
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(config.Port)
if err != nil {
return err
}
// init lua state
L := lua.NewState()
L.Push(L.NewFunction(OpenRedis))
L.Push(lua.LString("redis"))
L.Call(1, 0)
server.lua = L
return nil
}
func CronSyncAOF(_ *AeLoop, _ int, _ interface{}) {
if err := db.aof.Flush(); err != nil {
log.Error().Msgf("sync aof error: %v", err)
}
}
func CronEvictExpired(_ *AeLoop, _ int, _ interface{}) {
db.dict.EvictExpired()
}
func readableSize[T int | uint64](sz T) string {
switch {
case sz >= GB:
return fmt.Sprintf("%.1fGB", float64(sz)/float64(GB))
case sz >= MB:
return fmt.Sprintf("%.1fMB", float64(sz)/float64(MB))
case sz >= KB:
return fmt.Sprintf("%.1fKB", float64(sz)/float64(KB))
}
return fmt.Sprintf("%dB", sz)
}