forked from DiceDB/dice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
323 lines (281 loc) · 9.35 KB
/
main.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
package main
import (
"context"
"errors"
"flag"
"fmt"
"log/slog"
"os"
"os/signal"
"runtime"
"runtime/pprof"
"runtime/trace"
"sync"
"syscall"
"github.com/dicedb/dice/config"
diceerrors "github.com/dicedb/dice/internal/errors"
"github.com/dicedb/dice/internal/logger"
"github.com/dicedb/dice/internal/observability"
"github.com/dicedb/dice/internal/server"
"github.com/dicedb/dice/internal/server/resp"
"github.com/dicedb/dice/internal/shard"
dstore "github.com/dicedb/dice/internal/store"
"github.com/dicedb/dice/internal/worker"
)
func init() {
flag.StringVar(&config.Host, "host", "0.0.0.0", "host for the dicedb server")
flag.IntVar(&config.Port, "port", 7379, "port for the dicedb server")
flag.BoolVar(&config.EnableHTTP, "enable-http", true, "run server in HTTP mode as well")
flag.BoolVar(&config.EnableMultiThreading, "enable-multithreading", false, "run server in multithreading mode")
flag.IntVar(&config.HTTPPort, "http-port", 8082, "HTTP port for the dicedb server")
flag.IntVar(&config.WebsocketPort, "websocket-port", 8379, "Websocket port for the dicedb server")
flag.StringVar(&config.RequirePass, "requirepass", config.RequirePass, "enable authentication for the default user")
flag.StringVar(&config.CustomConfigFilePath, "o", config.CustomConfigFilePath, "dir path to create the config file")
flag.StringVar(&config.FileLocation, "c", config.FileLocation, "file path of the config file")
flag.BoolVar(&config.InitConfigCmd, "init-config", false, "initialize a new config file")
flag.IntVar(&config.KeysLimit, "keys-limit", config.KeysLimit, "keys limit for the dicedb server. "+
"This flag controls the number of keys each shard holds at startup. You can multiply this number with the "+
"total number of shard threads to estimate how much memory will be required at system start up.")
flag.BoolVar(&config.EnableProfiling, "enable-profiling", false, "enable profiling for the dicedb server")
flag.Parse()
config.SetupConfig()
iid := observability.GetOrCreateInstanceID()
config.DiceConfig.InstanceID = iid
}
func main() {
logr := logger.New(logger.Opts{WithTimestamp: true})
slog.SetDefault(logr)
go observability.Ping(logr)
ctx, cancel := context.WithCancel(context.Background())
// Handle SIGTERM and SIGINT
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGTERM, syscall.SIGINT)
queryWatchChan := make(chan dstore.QueryWatchEvent, config.DiceConfig.Performance.WatchChanBufSize)
cmdWatchChan := make(chan dstore.CmdWatchEvent, config.DiceConfig.Memory.KeysLimit)
var serverErrCh chan error
// Get the number of available CPU cores on the machine using runtime.NumCPU().
// This determines the total number of logical processors that can be utilized
// for parallel execution. Setting the maximum number of CPUs to the available
// core count ensures the application can make full use of all available hardware.
// If multithreading is not enabled, server will run on a single core.
var numCores int
if config.EnableMultiThreading {
serverErrCh = make(chan error, 1)
numCores = runtime.NumCPU()
logr.Debug("The DiceDB server has started in multi-threaded mode.", slog.Int("number of cores", numCores))
} else {
serverErrCh = make(chan error, 2)
logr.Debug("The DiceDB server has started in single-threaded mode.")
numCores = 1
}
// The runtime.GOMAXPROCS(numCores) call limits the number of operating system
// threads that can execute Go code simultaneously to the number of CPU cores.
// This enables Go to run more efficiently, maximizing CPU utilization and
// improving concurrency performance across multiple goroutines.
runtime.GOMAXPROCS(numCores)
// Initialize the ShardManager
shardManager := shard.NewShardManager(uint8(numCores), queryWatchChan, cmdWatchChan, serverErrCh, logr)
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
shardManager.Run(ctx)
}()
var serverWg sync.WaitGroup
// Initialize the AsyncServer server
// Find a port and bind it
if !config.EnableMultiThreading {
asyncServer := server.NewAsyncServer(shardManager, queryWatchChan, logr)
if err := asyncServer.FindPortAndBind(); err != nil {
cancel()
logr.Error("Error finding and binding port", slog.Any("error", err))
os.Exit(1)
}
serverWg.Add(1)
go func() {
defer serverWg.Done()
// Run the server
err := asyncServer.Run(ctx)
// Handling different server errors
if err != nil {
if errors.Is(err, context.Canceled) {
logr.Debug("Server was canceled")
} else if errors.Is(err, diceerrors.ErrAborted) {
logr.Debug("Server received abort command")
} else {
logr.Error(
"Server error",
slog.Any("error", err),
)
}
serverErrCh <- err
} else {
logr.Debug("Server stopped without error")
}
}()
// Goroutine to handle shutdown signals
wg.Add(1)
go func() {
defer wg.Done()
<-sigs
asyncServer.InitiateShutdown()
cancel()
}()
// Initialize the HTTP server
httpServer := server.NewHTTPServer(shardManager, logr)
serverWg.Add(1)
go func() {
defer serverWg.Done()
// Run the HTTP server
err := httpServer.Run(ctx)
if err != nil {
if errors.Is(err, context.Canceled) {
logr.Debug("HTTP Server was canceled")
} else if errors.Is(err, diceerrors.ErrAborted) {
logr.Debug("HTTP received abort command")
} else {
logr.Error("HTTP Server error", slog.Any("error", err))
}
serverErrCh <- err
} else {
logr.Debug("HTTP Server stopped without error")
}
}()
} else {
if config.EnableProfiling {
stopProfiling, err := startProfiling(logr)
if err != nil {
logr.Error("Profiling could not be started", slog.Any("error", err))
os.Exit(1)
}
defer stopProfiling()
}
workerManager := worker.NewWorkerManager(config.DiceConfig.Performance.MaxClients, shardManager)
// Initialize the RESP Server
respServer := resp.NewServer(shardManager, workerManager, cmdWatchChan, serverErrCh, logr)
serverWg.Add(1)
go func() {
defer serverWg.Done()
// Run the server
err := respServer.Run(ctx)
// Handling different server errors
if err != nil {
if errors.Is(err, context.Canceled) {
logr.Debug("Server was canceled")
} else if errors.Is(err, diceerrors.ErrAborted) {
logr.Debug("Server received abort command")
} else {
logr.Error("Server error", "error", err)
}
serverErrCh <- err
} else {
logr.Debug("Server stopped without error")
}
}()
// Goroutine to handle shutdown signals
wg.Add(1)
go func() {
defer wg.Done()
<-sigs
respServer.Shutdown()
cancel()
}()
}
websocketServer := server.NewWebSocketServer(shardManager, queryWatchChan, config.WebsocketPort, logr)
serverWg.Add(1)
go func() {
defer serverWg.Done()
// Run the Websocket server
err := websocketServer.Run(ctx)
if err != nil {
if errors.Is(err, context.Canceled) {
logr.Debug("Websocket Server was canceled")
} else if errors.Is(err, diceerrors.ErrAborted) {
logr.Debug("Websocket received abort command")
} else {
logr.Error("Websocket Server error", "error", err)
}
serverErrCh <- err
} else {
logr.Debug("Websocket Server stopped without error")
}
}()
go func() {
serverWg.Wait()
close(serverErrCh) // Close the channel when both servers are done
}()
for err := range serverErrCh {
if err != nil && errors.Is(err, diceerrors.ErrAborted) {
// if either the AsyncServer/RESPServer or the HTTPServer received an abort command,
// cancel the context, helping gracefully exiting all servers
cancel()
}
}
close(sigs)
cancel()
wg.Wait()
logr.Debug("Server has shut down gracefully")
}
func startProfiling(logr *slog.Logger) (func(), error) {
// Start CPU profiling
cpuFile, err := os.Create("cpu.prof")
if err != nil {
return nil, fmt.Errorf("could not create cpu.prof: %w", err)
}
if err = pprof.StartCPUProfile(cpuFile); err != nil {
cpuFile.Close()
return nil, fmt.Errorf("could not start CPU profile: %w", err)
}
// Start memory profiling
memFile, err := os.Create("mem.prof")
if err != nil {
pprof.StopCPUProfile()
cpuFile.Close()
return nil, fmt.Errorf("could not create mem.prof: %w", err)
}
// Start block profiling
runtime.SetBlockProfileRate(1)
// Start execution trace
traceFile, err := os.Create("trace.out")
if err != nil {
runtime.SetBlockProfileRate(0)
memFile.Close()
pprof.StopCPUProfile()
cpuFile.Close()
return nil, fmt.Errorf("could not create trace.out: %w", err)
}
if err := trace.Start(traceFile); err != nil {
traceFile.Close()
runtime.SetBlockProfileRate(0)
memFile.Close()
pprof.StopCPUProfile()
cpuFile.Close()
return nil, fmt.Errorf("could not start trace: %w", err)
}
// Return a cleanup function
return func() {
// Stop the CPU profiling and close cpuFile
pprof.StopCPUProfile()
cpuFile.Close()
// Write heap profile
runtime.GC()
if err := pprof.WriteHeapProfile(memFile); err != nil {
logr.Warn("could not write memory profile", slog.Any("error", err))
}
memFile.Close()
// Write block profile
blockFile, err := os.Create("block.prof")
if err != nil {
logr.Warn("could not create block profile", slog.Any("error", err))
} else {
if err := pprof.Lookup("block").WriteTo(blockFile, 0); err != nil {
logr.Warn("could not write block profile", slog.Any("error", err))
}
blockFile.Close()
}
runtime.SetBlockProfileRate(0)
// Stop trace and close traceFile
trace.Stop()
traceFile.Close()
}, nil
}