-
Notifications
You must be signed in to change notification settings - Fork 10
/
redis.go
727 lines (634 loc) · 22.9 KB
/
redis.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
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
package main
import (
"bufio"
"dannytools/constvar"
"dannytools/ehand"
"dannytools/logging"
"dannytools/myredis"
"fmt"
"io"
"math/rand"
"os"
"sort"
"strings"
"sync"
"time"
//"github.com/davecgh/go-spew/spew"
"github.com/go-redis/redis"
)
const (
cThreadEachCpuRedis int = 32
)
var (
//gRedisTop map[string][]string = map[string][]string{}
gKeyFiles map[string]string = map[string]string{}
gCwd string
gTpRedis string = fmt.Sprintf("%d", gc_type_redis)
gTpCluster string = fmt.Sprintf("%d", gc_type_cluster)
)
func CheckRedisInfo(cfg *CmdConf) (map[string]*myredis.RedisServer, map[string][]myredis.RedisAddr) {
var (
redisServers map[string]*myredis.RedisServer = map[string]*myredis.RedisServer{}
err error
redisTypes map[string][]myredis.RedisAddr = map[string][]myredis.RedisAddr{gTpRedis: []myredis.RedisAddr{}, gTpCluster: []myredis.RedisAddr{}}
tpStr string
)
g_loger.WriteToLogByFieldsNormalOnlyMsg("start to check redis info", logging.INFO)
for _, addr := range cfg.redisAddrs {
rServer := &myredis.RedisServer{Addr: addr}
err = rServer.GetAndSetRedisInfo()
if err != nil {
g_loger.WriteToLogByFieldsErrorExtramsgExit(err, "fail to get redis info "+addr.AddrString(), logging.ERROR, ehand.ERR_REDIS_INFO)
}
/*
if cfg.redisType == gc_type_redis && rServer.IsCluster && cfg.commands == gc_command_delete && !cfg.dryRun {
g_loger.WriteToLogByFieldsExitMsgNoErr(fmt.Sprintf("-rt is set to redis but %s is actually cluster, in this case , %s without dry run is not allowed",
addr.AddrString(), gc_command_delete), logging.ERROR, ehand.ERR_ERROR)
}
*/
if rServer.IsCluster {
tpStr = gTpCluster
} else {
tpStr = gTpRedis
}
redisTypes[tpStr] = append(redisTypes[tpStr], addr)
if rServer.Role != myredis.CRoleMaster && !rServer.ReplStatus.IfOnline {
g_loger.WriteToLogByFieldsNormalOnlyMsg(fmt.Sprintf("%s, but replication is stopped", rServer.InfoString()), logging.WARNING)
}
redisServers[addr.AddrString()] = rServer
g_loger.WriteToLogByFieldsNormalOnlyMsg(rServer.InfoString(), logging.INFO)
}
return redisServers, redisTypes
}
func CheckAndSetRedisInfoForAllNodes(cfg *CmdConf) map[string]*myredis.RedisServer {
var (
rServers map[string]*myredis.RedisServer
rTypes map[string][]myredis.RedisAddr
ok bool
addrStr string
)
rServers, rTypes = CheckRedisInfo(cfg)
//spew.Dump(rTypes)
// scan keys from redis and redis type is not specfied, for actual cluster, we need to scan keys from all latest slave nodes
//if len(rTypes[gTpCluster]) > 0 && cfg.keyfileDir != "" && cfg.redisType != gc_type_redis {
// for cluster, need to get all nodes info
if cfg.ifDiscoveryClusterNodes && len(rTypes[gTpCluster]) > 0 {
for _, addr := range rTypes[gTpCluster] {
addrStr = addr.AddrString()
g_loger.WriteToLogByFieldsNormalOnlyMsg(fmt.Sprintf("find peer cluster nodes of %s", addrStr), logging.INFO)
clusterClient, err := addr.CreateConPoolCluster(false, cThreadEachCpuRedis)
if err != nil {
g_loger.WriteToLogByFieldsErrorExtramsgExit(err, "fail to connect to redis "+addrStr, logging.ERROR, ehand.ERR_REDIS_CONNECT)
}
nodeAddr, err := myredis.GetClusterNodesAddr(clusterClient)
if err != nil {
g_loger.WriteToLogByFieldsErrorExtramsgExit(err, "fail to get redis cluster nodes "+addrStr, logging.ERROR, ehand.ERR_REDIS_CLUSTER_NODES)
}
for addrStr = range nodeAddr {
if _, ok = rServers[addrStr]; ok {
continue
}
oneServer := &myredis.RedisServer{Addr: nodeAddr[addrStr]}
err = oneServer.GetAndSetRedisInfo()
if err != nil {
g_loger.WriteToLogByFieldsErrorExtramsgExit(err, "fail to get redis info "+addrStr, logging.ERROR, ehand.ERR_REDIS_INFO)
}
rServers[addrStr] = oneServer
}
}
}
return rServers
}
func GetAllMasterInfo(rServers map[string]*myredis.RedisServer) map[string]*myredis.RedisServer {
var (
masters map[string]*myredis.RedisServer = map[string]*myredis.RedisServer{}
err error
ok bool
)
g_loger.WriteToLogByFieldsNormalOnlyMsg("start to get redis masters", logging.INFO)
for ipPort, rs := range rServers {
if rs.Role == myredis.CRoleMaster {
masters[ipPort] = rs
//gRedisTop[ipPort] = rs.GetAllNodesAddrStr()
continue
} else {
if _, ok = masters[rs.Master.AddrString()]; ok {
continue
}
if _, ok = rServers[rs.Master.AddrString()]; ok {
masters[rs.Master.AddrString()] = rServers[rs.Master.AddrString()]
continue
}
}
g_loger.WriteToLogByFieldsNormalOnlyMsg(fmt.Sprintf("%s is slave, check its master %s info", rs.Addr.AddrString(), rs.Master.AddrString()), logging.INFO)
mServer := &myredis.RedisServer{Addr: rs.Master}
err = mServer.GetAndSetRedisInfo()
if err != nil {
g_loger.WriteToLogByFieldsErrorExtramsgExit(err, "error to check redis info of "+rs.Master.AddrString(), logging.ERROR, ehand.ERR_ERROR)
}
//gRedisTop[ipPort] = mServer.GetAllNodesAddrStr()
masters[rs.Master.AddrString()] = mServer
}
return masters
}
func GetAllNodesForReadOnly(cfg *CmdConf, masters map[string]*myredis.RedisServer) map[string]myredis.RedisAddr {
var (
slaves map[string]myredis.RedisAddr = map[string]myredis.RedisAddr{}
)
g_loger.WriteToLogByFieldsNormalOnlyMsg("start to get latest slave or master to scan keys", logging.INFO)
for ipPort, rs := range masters {
rAddr, err := rs.GetLatestSlave()
if err != nil {
if cfg.forceAddr {
g_loger.WriteToLogByFieldsErrorExtramsg(err, fmt.Sprintf("%s has no suitable slave to scan keys, but you specify -F, so use itself instead", ipPort), logging.ERROR)
slaves[ipPort] = rs.Addr
} else {
g_loger.WriteToLogByFieldsErrorExtramsgExit(err, fmt.Sprintf("%s has no suitable slave to scan keys, you may specify -F to scan keys from master", ipPort), logging.ERROR, ehand.ERR_ERROR)
}
} else {
slaves[rAddr.AddrString()] = rAddr
g_loger.WriteToLogByFieldsNormalOnlyMsg(fmt.Sprintf("use latest slave %s to scan keys", rAddr.AddrString()), logging.INFO)
}
}
return slaves
}
// return master servers and scan node servers
func GetMasterAndLatestSlavesInfo(cfg *CmdConf) (map[string]*myredis.RedisServer, map[string]*myredis.RedisServer) {
var (
//scanArr []string
//infStr string
ifOk bool
oneServer *myredis.RedisServer
err error
ok bool
)
servers := CheckAndSetRedisInfoForAllNodes(cfg)
masters := GetAllMasterInfo(servers)
scanNodeAddrs := GetAllNodesForReadOnly(cfg, masters)
scanNodeServers := map[string]*myredis.RedisServer{}
for ipPort, addr := range scanNodeAddrs {
ifOk = false
if _, ok = servers[ipPort]; ok {
oneServer = servers[ipPort]
ifOk = true
} else if _, ok = masters[ipPort]; ok {
oneServer = masters[ipPort]
ifOk = true
} else {
g_loger.WriteToLogByFieldsNormalOnlyMsg(fmt.Sprintf("strange, %s does not have redis info, get and check it", addr.AddrString()), logging.WARNING)
oneServer = &myredis.RedisServer{Addr: addr}
err = oneServer.GetAndSetRedisInfo()
if err != nil {
g_loger.WriteToLogByFieldsErrorExtramsgExit(err, fmt.Sprintf("error to check redis info of %s", addr.AddrString()),
logging.ERROR, ehand.ERR_ERROR)
ifOk = false
} else {
ifOk = true
}
}
if ifOk {
scanNodeServers[ipPort] = oneServer
/*
infStr = oneServer.InfoString()
if oneServer.IsSlave() {
infStr += " --> " + servers[oneServer.Master.AddrString()].InfoString()
}
scanArr = append(scanArr, infStr)
*/
}
}
//g_loger.WriteToLogByFieldsNormalOnlyMsg(fmt.Sprintf("below nodes are candidate nodes to scan keys:\n\t%s", strings.Join(scanArr, "\n\t")), logging.INFO)
return masters, scanNodeServers
}
func ScanKeysFromOneKeyFile(cfg *CmdConf, keyFile string, keysChan chan string, wg *sync.WaitGroup) {
//wg.Add(1)
defer wg.Done()
defer close(keysChan)
var (
ifReg bool = false
line string
err error
)
g_loger.WriteToLogByFieldsNormalOnlyMsg(fmt.Sprintf("start thread to read keys from file %s", keyFile), logging.INFO)
if cfg.patternReg.String() != "" {
ifReg = true
}
FH, err := os.Open(keyFile)
if err != nil {
g_loger.WriteToLogByFieldsErrorExtramsgExit(err, fmt.Sprintf("fail to open key file %s", keyFile), logging.ERROR, ehand.ERR_FILE_OPEN)
}
defer FH.Close()
bufFH := bufio.NewReader(FH)
for {
line, err = bufFH.ReadString('\n')
if err != nil {
if err == io.EOF {
break
} else {
g_loger.WriteToLogByFieldsErrorExtramsgExit(err, "error to read key from file "+keyFile, logging.ERROR, ehand.ERR_FILE_READ)
}
}
line = strings.TrimSpace(line)
if line == "" {
continue
}
if ifReg {
if cfg.patternReg.MatchString(line) {
keysChan <- line
}
} else {
keysChan <- line
}
}
g_loger.WriteToLogByFieldsNormalOnlyMsg(fmt.Sprintf("exit thread to read keys from file %s", keyFile), logging.INFO)
}
func ScanKeysFromOneRedis(cfg *CmdConf, redisInfoStr string, client *redis.Client, keysChan chan string, wg *sync.WaitGroup) {
defer wg.Done()
defer close(keysChan)
defer client.Close()
g_loger.WriteToLogByFieldsNormalOnlyMsg("start thread to scan keys from redis "+redisInfoStr, logging.INFO)
err := myredis.ScanRedisKeys(client, int64(cfg.elementScanBatch), cfg.patternReg, int(cfg.keyBatch), int(cfg.keyInterval), keysChan)
if err != nil {
g_loger.WriteToLogByFieldsErrorExtramsgExit(err, "error to scan keys from redis "+redisInfoStr, logging.ERROR, ehand.ERR_REDIS_SCAN)
}
g_loger.WriteToLogByFieldsNormalOnlyMsg("exit thread to scan keys from redis "+redisInfoStr, logging.INFO)
}
func ScanKeys(cfg *CmdConf, rServers map[string]*myredis.RedisServer, masters map[string]*myredis.RedisServer, wg *sync.WaitGroup) map[string]chan string {
var (
reChans map[string]chan string = map[string]chan string{}
chanBuf int
database int = int(cfg.database)
addr string
filesArr []string
)
chanBuf = GetChanBuf(cfg)
if cfg.ifKeyFiles {
for addr, f := range cfg.keyFiles {
filesArr = append(filesArr, fmt.Sprintf("%s <=> %s", f, myredis.GetInfoStringOnlyOneNode(rServers[addr], masters)))
}
g_loger.WriteToLogByFieldsNormalOnlyMsg(fmt.Sprintf("read keys from below files:\n\t%s", strings.Join(filesArr, "\n\t")), logging.INFO)
for addr = range cfg.keyFiles {
oneChan := make(chan string, chanBuf)
reChans[addr] = oneChan
oneKeyFile := cfg.keyFiles[addr]
wg.Add(1)
go ScanKeysFromOneKeyFile(cfg, oneKeyFile, oneChan, wg)
}
} else {
g_loger.WriteToLogByFieldsNormalOnlyMsg(myredis.GetInfoStringForNodes("scan keys from below redis:", rServers, masters), logging.INFO)
for addr = range rServers {
oneClient, err := rServers[addr].Addr.CreateConPoolRedis(database, cThreadEachCpuRedis)
if err != nil {
g_loger.WriteToLogByFieldsErrorExtramsgExit(err, "error to connect redis "+rServers[addr].InfoString(),
logging.ERROR, ehand.ERR_REDIS_CONNECT)
}
oneChan := make(chan string, chanBuf)
reChans[addr] = oneChan
wg.Add(1)
go ScanKeysFromOneRedis(cfg, rServers[addr].InfoString(), oneClient, oneChan, wg)
}
}
/*
for addr = range rServers {
oneChan := make(chan string, chanBuf)
reChans[addr] = oneChan
if cfg.ifKeyFiles {
oneKeyFile := cfg.keyFiles[addr]
wg.Add(1)
go ScanKeysFromOneKeyFile(cfg, oneKeyFile, oneChan, wg)
} else {
// addr may not be the same as rServers[addr].Addr.AddrString()? should not
actaulAddr = rServers[addr].Addr.AddrString()
oneClient, err := rServers[addr].Addr.CreateConPoolRedis(database, cThreadEachCpuRedis)
if err != nil {
g_loger.WriteToLogByFieldsErrorExtramsgExit(err, "error to connect redis "+rServers[addr].InfoString(),
logging.ERROR, ehand.ERR_REDIS_CONNECT)
}
wg.Add(1)
go ScanKeysFromOneRedis(cfg, actaulAddr, oneClient, oneChan, wg)
}
}
*/
return reChans
}
// sampleRate: for redis4.0
func GetKeysValuesAndBytes(cfg *CmdConf, sampleRate uint, rserver *myredis.RedisServer, keysChan chan string, printChan chan myredis.RedisKey, wg *sync.WaitGroup) {
defer wg.Done()
var (
err error
key string
keyArr []string
keyTypes map[string]string
alreadyCntPipe int = 0
alreadyCntKey int = 0
keyTypeArr map[string][]string = map[string][]string{}
k string
t string
ok bool
pipeSize int = int(cfg.pipeLineSize)
redisKeys []myredis.RedisKey
minBytes int64 = int64(cfg.sizeLimit)
rangeBatch int64 = int64(cfg.elementScanBatch)
elementBatch int64 = int64(cfg.elementBatch)
elementInterval time.Duration = time.Duration(cfg.elementInterval) * time.Microsecond
keyInterval time.Duration = time.Duration(cfg.keyInterval) * time.Microsecond
ifSleep bool = false
ifBytes bool = false
ifValue bool = false
i int
keyBatch int = int(cfg.keyBatch)
redisInfo string = rserver.InfoString()
)
g_loger.WriteToLogByFieldsNormalOnlyMsg("start thread GetKeysValuesAndBytes from "+redisInfo, logging.INFO)
client, err := rserver.Addr.CreateRedisOrClusterConPool(false, false, int(cfg.database), cThreadEachCpuRedis)
defer client.Close()
if err != nil {
g_loger.WriteToLogByFieldsErrorExtramsgExit(err, "error to connect to redis "+redisInfo, logging.ERROR, ehand.ERR_REDIS_CONNECT)
}
if rserver.IsCluster {
_, err = client.Redis.ReadOnly().Result()
if err != nil {
g_loger.WriteToLogByFieldsErrorExtramsgExit(err, "error to set redis connection read only "+redisInfo, logging.ERROR, ehand.ERR_REDIS_CONNECT)
} else {
g_loger.WriteToLogByFieldsNormalOnlyMsg("successfully set redis connection read only "+redisInfo, logging.INFO)
}
}
if cfg.elementInterval > 0 {
ifSleep = true
}
if cfg.commands == gc_command_dump || cfg.commands == gc_command_bigkey {
ifBytes = true
}
if cfg.commands == gc_command_dump {
ifValue = true
}
for key = range keysChan {
keyArr = append(keyArr, key)
alreadyCntPipe++
if alreadyCntPipe < pipeSize {
continue
} else {
alreadyCntPipe = 0
keyTypes, err = client.GetKeysTypePipe(keyArr)
keyArr = nil
if err != nil {
g_loger.WriteToLogByFieldsErrorExtramsgExit(err, "error to get key type", logging.ERROR, ehand.ERR_REDIS_TYPE)
}
for k, t = range keyTypes {
if _, ok = keyTypeArr[t]; ok {
keyTypeArr[t] = append(keyTypeArr[t], k)
} else {
keyTypeArr[t] = []string{k}
}
}
}
for t = range keyTypeArr {
if len(keyTypeArr[t]) < pipeSize {
continue
}
//fmt.Printf("process keys: %s\n", strings.Join(keyTypeArr[t], ", "))
redisKeys, err = client.GetKeysValueAdaptive(keyTypeArr[t], t, cfg.database, minBytes,
constvar.DATETIME_FORMAT_NOSPACE, cfg.minTtlSeconds, cfg.maxTtlSeconds,
sampleRate, gc_sample_min, rangeBatch, elementBatch, elementInterval, ifSleep, ifBytes, ifValue)
if err != nil {
g_loger.WriteToLogByFieldsErrorExtramsgExit(err, "error to get key value and bytes", logging.ERROR, ehand.ERR_REDIS_GET_KEY_VALUE)
}
for i = range redisKeys {
printChan <- redisKeys[i]
}
redisKeys = nil
if cfg.keyInterval > 0 {
alreadyCntKey += len(keyTypeArr[t])
}
delete(keyTypeArr, t)
if cfg.keyInterval > 0 && alreadyCntKey > keyBatch {
alreadyCntKey = 0
time.Sleep(keyInterval)
}
}
}
if len(keyArr) > 0 {
keyTypes, err = client.GetKeysTypePipe(keyArr)
keyArr = nil
if err != nil {
g_loger.WriteToLogByFieldsErrorExtramsgExit(err, "error to get key type", logging.ERROR, ehand.ERR_REDIS_TYPE)
}
for k, t = range keyTypes {
if _, ok = keyTypeArr[t]; ok {
keyTypeArr[t] = append(keyTypeArr[t], k)
} else {
keyTypeArr[t] = []string{k}
}
}
}
for t = range keyTypeArr {
//fmt.Printf("process keys: %s\n", strings.Join(keyTypeArr[t], ", "))
if len(keyTypeArr[t]) == 0 {
continue
}
redisKeys, err = client.GetKeysValueAdaptive(keyTypeArr[t], t, cfg.database, minBytes,
constvar.DATETIME_FORMAT_NOSPACE, cfg.minTtlSeconds, cfg.maxTtlSeconds,
sampleRate, gc_sample_min, rangeBatch, elementBatch, elementInterval, ifSleep, ifBytes, ifValue)
if err != nil {
g_loger.WriteToLogByFieldsErrorExtramsgExit(err, "error to get key value and bytes", logging.ERROR, ehand.ERR_REDIS_GET_KEY_VALUE)
}
for i = range redisKeys {
printChan <- redisKeys[i]
}
redisKeys = nil
delete(keyTypeArr, t)
}
g_loger.WriteToLogByFieldsNormalOnlyMsg("exit thread GetKeysValuesAndBytes from "+redisInfo, logging.INFO)
}
// deleteType: 0=delete, 1=unlink(for redis4.0), else=expire it
func DeleteKeys(cfg *CmdConf, deleteType uint8, oneMaster *myredis.RedisServer, keysChan chan string, deletedKeysChan chan string, wg *sync.WaitGroup) {
defer wg.Done()
var (
err error
keyArr []string
alreadyCntPipe int = 0
alreadyCntKey int = 0
keyBatch int = int(cfg.keyBatch)
pipeSize int = int(cfg.pipeLineSize)
maxExp int = int(cfg.delKeyExpireMax)
k string
sleepInterval time.Duration = time.Duration(cfg.keyInterval) * time.Microsecond
addStr = oneMaster.InfoString()
)
g_loger.WriteToLogByFieldsNormalOnlyMsg("start thread DeleteKeys from "+addStr, logging.INFO)
client, err := oneMaster.CreateGenClientPool(false, int(g_conf.database), cThreadEachCpuRedis)
defer client.Close()
if err != nil {
g_loger.WriteToLogByFieldsErrorExtramsgExit(err, "error to connect to "+oneMaster.InfoString(), logging.ERROR, ehand.ERR_REDIS_CONNECT)
}
for key := range keysChan {
keyArr = append(keyArr, key)
alreadyCntPipe++
if alreadyCntPipe >= pipeSize {
if cfg.keyInterval > 0 {
alreadyCntKey += alreadyCntPipe
}
alreadyCntPipe = 0
switch deleteType {
case gc_delete_del:
err = client.DeleteKeysByDirectlyDelIt(keyArr)
case gc_delete_unlink:
err = client.DeleteKeysByUnlinkIt(keyArr)
default:
err = client.DeleteKeysByExpireItPipe(keyArr, time.Duration(rand.Intn(maxExp)+1)*time.Second)
}
if err != nil {
g_loger.WriteToLogByFieldsErrorExtramsgExit(err, "error to delete key", logging.ERROR, ehand.ERR_REDIS_DELETE)
}
for _, k = range keyArr {
deletedKeysChan <- k
}
keyArr = nil
if cfg.keyInterval > 0 && alreadyCntKey >= keyBatch {
alreadyCntKey = 0
time.Sleep(sleepInterval)
}
}
}
if len(keyArr) > 0 {
switch deleteType {
case gc_delete_del:
err = client.DeleteKeysByDirectlyDelIt(keyArr)
case gc_delete_unlink:
err = client.DeleteKeysByUnlinkIt(keyArr)
default:
err = client.DeleteKeysByExpireItPipe(keyArr, time.Duration(rand.Intn(maxExp)+1)*time.Second)
}
if err != nil {
g_loger.WriteToLogByFieldsErrorExtramsgExit(err, "error to delete key", logging.ERROR, ehand.ERR_REDIS_DELETE)
}
for _, k = range keyArr {
deletedKeysChan <- k
}
}
g_loger.WriteToLogByFieldsNormalOnlyMsg("exit thread DeleteKeys from "+addStr, logging.INFO)
}
/*
write keys to file, one key one line
*/
func PrintKeysIntoFile(keyFile string, keysChan chan string, wg *sync.WaitGroup) {
defer wg.Done()
var (
err error
)
g_loger.WriteToLogByFieldsNormalOnlyMsg("start thread of write keys to file "+keyFile, logging.INFO)
fh, err := os.OpenFile(keyFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0744)
if fh != nil {
defer fh.Close()
}
if err != nil {
g_loger.WriteToLogByFieldsErrorExtramsgExit(err, "fail to open file "+keyFile, logging.ERROR, ehand.ERR_FILE_OPEN)
}
for key := range keysChan {
_, err = fh.WriteString(key + "\n")
if err != nil {
g_loger.WriteToLogByFieldsErrorExtramsgExit(err, fmt.Sprintf("fail to write key %s into file %s", key, keyFile),
logging.ERROR, ehand.ERR_FILE_WRITE)
}
}
g_loger.WriteToLogByFieldsNormalOnlyMsg("exit thread of write keys to file"+keyFile, logging.INFO)
}
/*
dump key value or big key info into file
*/
func PrintKeyInfoAndValueIntoFileNew(cfg *CmdConf, reFile string, printChan chan myredis.RedisKey, wg *sync.WaitGroup) {
defer wg.Done()
var (
err error
str string
oneVal myredis.RedisKey
ifBigKey bool = false
needSortKeyes []myredis.RedisKey
printCnt int
sortCnt int = 0
outLimit int = int(cfg.limit)
sortSize int = 2 * outLimit
)
if sortSize < 100 {
sortSize = 100
} else if sortSize >= 500 {
sortSize = 500
}
g_loger.WriteToLogByFieldsNormalOnlyMsg("start thread of writing result to file "+reFile, logging.INFO)
if cfg.commands == "bigkey" {
ifBigKey = true
}
fh, err := os.OpenFile(reFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0744)
if fh != nil {
defer fh.Close()
}
if err != nil {
g_loger.WriteToLogByFieldsErrorExtramsgExit(err, "fail to open file "+reFile, logging.ERROR, ehand.ERR_FILE_OPEN)
}
if ifBigKey && !cfg.bigKeyOnlyKeyName {
fh.WriteString(myredis.GetRedisBigkeyHeader())
}
if ifBigKey && cfg.ifSortedKey && !cfg.bigKeyOnlyKeyName {
//bigkey with sorting
for oneVal = range printChan {
sortCnt++
needSortKeyes = append(needSortKeyes, oneVal)
if sortCnt >= sortSize {
sortCnt = 0
if len(needSortKeyes) > outLimit {
sort.SliceStable(needSortKeyes, func(i, j int) bool { return needSortKeyes[i].Bytes > needSortKeyes[j].Bytes })
needSortKeyes = needSortKeyes[0:outLimit]
}
}
}
sort.SliceStable(needSortKeyes, func(i, j int) bool { return needSortKeyes[i].Bytes > needSortKeyes[j].Bytes })
if cfg.limit > 0 && len(needSortKeyes) > outLimit {
printCnt = outLimit
} else {
printCnt = len(needSortKeyes)
}
for _, oneVal = range needSortKeyes[0:printCnt] {
str, err = oneVal.GetRedisKeyPrintLine(ifBigKey, cfg.pretty, g_json_indent)
if err != nil {
g_loger.WriteToLogByFieldsErrorExtramsgExit(err, fmt.Sprintf("fail to get print string for key %s", oneVal.Key),
logging.ERROR, ehand.ERR_JSON_MARSHAL)
}
_, err = fh.WriteString(str)
if err != nil {
g_loger.WriteToLogByFieldsErrorExtramsgExit(err, fmt.Sprintf("fail to write key info of %s into file %s: %s", oneVal.Key, reFile, str),
logging.ERROR, ehand.ERR_FILE_WRITE)
}
}
} else {
//dump key or bigkey without sorting
for oneVal = range printChan {
if ifBigKey && cfg.bigKeyOnlyKeyName {
str = oneVal.Key + "\n"
} else {
str, err = oneVal.GetRedisKeyPrintLine(ifBigKey, cfg.pretty, g_json_indent)
if err != nil {
g_loger.WriteToLogByFieldsErrorExtramsgExit(err, fmt.Sprintf("fail to get print string for key %s", oneVal.Key),
logging.ERROR, ehand.ERR_JSON_MARSHAL)
}
// for pretty dump
if !ifBigKey && cfg.pretty {
_, err = fh.WriteString(gc_json_line_seperator)
if err != nil {
g_loger.WriteToLogByFieldsErrorExtramsgExit(err, fmt.Sprintf("fail to write %s into file %s for key",
gc_json_line_seperator, reFile, oneVal.Key),
logging.ERROR, ehand.ERR_FILE_WRITE)
}
}
if !ifBigKey {
str += "\n"
}
}
_, err = fh.WriteString(str)
if err != nil {
g_loger.WriteToLogByFieldsErrorExtramsgExit(err, fmt.Sprintf("fail to write key info of %s into file %s: %s", oneVal.Key, reFile, str),
logging.ERROR, ehand.ERR_FILE_WRITE)
}
}
}
g_loger.WriteToLogByFieldsNormalOnlyMsg("exit thread of writing result to file "+reFile, logging.INFO)
}