forked from tair-opensource/RedisShake
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
243 lines (218 loc) · 6.22 KB
/
Copy pathmain.go
File metadata and controls
243 lines (218 loc) · 6.22 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
package main
import (
"context"
_ "net/http/pprof"
"os"
"os/signal"
"sync/atomic"
"syscall"
"time"
"RedisShake/internal/config"
"RedisShake/internal/entry"
"RedisShake/internal/filter"
"RedisShake/internal/log"
"RedisShake/internal/reader"
"RedisShake/internal/status"
"RedisShake/internal/utils"
"RedisShake/internal/writer"
"github.com/mcuadros/go-defaults"
)
func main() {
v := config.LoadConfig()
log.Init(config.Opt.Advanced.LogLevel, config.Opt.Advanced.LogFile, config.Opt.Advanced.Dir)
utils.ChdirAndAcquireFileLock()
utils.SetNcpu()
utils.SetPprofPort()
luaRuntime := filter.NewFunctionFilter(config.Opt.Filter.Function)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// create reader
var theReader reader.Reader
switch {
case v.IsSet("sync_reader"):
opts := new(reader.SyncReaderOptions)
defaults.SetDefaults(opts)
err := v.UnmarshalKey("sync_reader", opts)
if err != nil {
log.Panicf("failed to read the SyncReader config entry. err: %v", err)
}
if opts.Cluster {
theReader = reader.NewSyncClusterReader(ctx, opts)
log.Infof("create SyncClusterReader: %v", opts.Address)
} else {
theReader = reader.NewSyncStandaloneReader(ctx, opts)
log.Infof("create SyncStandaloneReader: %v", opts.Address)
}
case v.IsSet("scan_reader"):
opts := new(reader.ScanReaderOptions)
defaults.SetDefaults(opts)
err := v.UnmarshalKey("scan_reader", opts)
if err != nil {
log.Panicf("failed to read the ScanReader config entry. err: %v", err)
}
if opts.Cluster {
theReader = reader.NewScanClusterReader(ctx, opts)
log.Infof("create ScanClusterReader: %v", opts.Address)
} else {
theReader = reader.NewScanStandaloneReader(ctx, opts)
log.Infof("create ScanStandaloneReader: %v", opts.Address)
}
case v.IsSet("rdb_reader"):
opts := new(reader.RdbReaderOptions)
defaults.SetDefaults(opts)
err := v.UnmarshalKey("rdb_reader", opts)
if err != nil {
log.Panicf("failed to read the RdbReader config entry. err: %v", err)
}
theReader = reader.NewRDBReader(opts)
log.Infof("create RdbReader: %v", opts.Filepath)
case v.IsSet("aof_reader"):
opts := new(reader.AOFReaderOptions)
defaults.SetDefaults(opts)
err := v.UnmarshalKey("aof_reader", opts)
if err != nil {
log.Panicf("failed to read the AOFReader config entry. err: %v", err)
}
theReader = reader.NewAOFReader(opts)
log.Infof("create AOFReader: %v", opts.Filepath)
default:
log.Panicf("no reader config entry found")
}
// create writer
var theWriter writer.Writer
switch {
case v.IsSet("redis_writer"):
opts := new(writer.RedisWriterOptions)
defaults.SetDefaults(opts)
err := v.UnmarshalKey("redis_writer", opts)
if err != nil {
log.Panicf("failed to read the RedisStandaloneWriter config entry. err: %v", err)
}
if opts.OffReply && config.Opt.Advanced.RDBRestoreCommandBehavior == "panic" {
log.Panicf("the RDBRestoreCommandBehavior can't be 'panic' when the server not reply to commands")
}
if opts.Cluster {
theWriter = writer.NewRedisClusterWriter(ctx, opts)
log.Infof("create RedisClusterWriter: %v", opts.Address)
} else if opts.Sentinel {
theWriter = writer.NewRedisSentinelWriter(ctx, opts)
log.Infof("create RedisSentinelWriter: %v", opts.Address)
} else {
theWriter = writer.NewRedisStandaloneWriter(ctx, opts)
log.Infof("create RedisStandaloneWriter: %v", opts.Address)
}
if config.Opt.Advanced.EmptyDBBeforeSync {
// exec FLUSHALL command to flush db
entry := entry.NewEntry()
entry.Argv = []string{"FLUSHALL"}
theWriter.Write(entry)
}
default:
log.Panicf("no writer config entry found")
}
// create status
if config.Opt.Advanced.StatusPort != 0 {
status.Init(theReader, theWriter)
}
// create log entry count
logEntryCount := status.EntryCount{
ReadCount: 0,
WriteCount: 0,
}
log.Infof("start syncing...")
go waitShutdown(cancel)
chrs := theReader.StartRead(ctx)
theWriter.StartWrite(ctx)
readerDone := make(chan bool)
for _, chr := range chrs {
go func(ch chan *entry.Entry) {
for e := range ch {
// calc arguments
e.Parse()
// update reader status
if config.Opt.Advanced.StatusPort != 0 {
status.AddReadCount(e.CmdName)
}
// update log entry count
atomic.AddUint64(&logEntryCount.ReadCount, 1)
// filter
if !filter.Filter(e) {
log.Debugf("skip command: %v", e)
continue
}
// run lua function
log.Debugf("function before: %v", e)
entries := luaRuntime.RunFunction(e)
log.Debugf("function after: %v", entries)
// write
for _, theEntry := range entries {
theEntry.Parse()
theWriter.Write(theEntry)
// update writer status
if config.Opt.Advanced.StatusPort != 0 {
status.AddWriteCount(theEntry.CmdName)
}
// update log entry count
atomic.AddUint64(&logEntryCount.WriteCount, 1)
}
}
readerDone <- true
}(chr)
}
// caluate ops and log to screen
go func() {
if config.Opt.Advanced.LogInterval <= 0 {
log.Infof("log interval is 0, will not log to screen")
return
}
ticker := time.NewTicker(time.Duration(config.Opt.Advanced.LogInterval) * time.Second)
defer ticker.Stop()
for range ticker.C {
logEntryCount.UpdateOPS()
log.Infof("%s, %s", logEntryCount.String(), theReader.StatusString())
}
}()
ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()
readerCnt := len(chrs)
Loop:
for {
select {
case done := <-readerDone:
if done {
readerCnt--
}
if readerCnt == 0 {
break Loop
}
case <-ticker.C:
pingEntry := entry.NewEntry()
pingEntry.DbId = 0
pingEntry.CmdName = "PING"
pingEntry.Argv = []string{"PING"}
pingEntry.Group = "connection"
theWriter.Write(pingEntry)
}
}
theWriter.Close() // Wait for all writing operations to complete
utils.ReleaseFileLock() // Release file lock
log.Infof("all done")
}
func waitShutdown(cancel context.CancelFunc) {
quitCh := make(chan os.Signal, 1)
signal.Notify(quitCh, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
sigTimes := 0
for {
sig := <-quitCh
sigTimes++
if sig != syscall.SIGINT {
log.Infof("Got signal: %s.", sig)
} else {
log.Infof("Got signal: %s to exit. Press Ctrl+C again to force exit.", sig)
if sigTimes >= 2 {
os.Exit(0)
}
cancel()
}
}
}