forked from tair-opensource/RedisShake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode.go
244 lines (225 loc) · 5.9 KB
/
decode.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
// Copyright 2016 CodisLabs. All Rights Reserved.
// Licensed under the MIT (MIT-LICENSE.txt) license.
package run
import (
"bufio"
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"time"
"pkg/libs/atomic2"
"pkg/libs/log"
"pkg/rdb"
"redis-shake/common"
"redis-shake/configure"
"redis-shake/base"
)
type CmdDecode struct {
rbytes, wbytes, nentry atomic2.Int64
}
type cmdDecodeStat struct {
rbytes, wbytes, nentry int64
}
func (cmd *CmdDecode) Stat() *cmdDecodeStat {
return &cmdDecodeStat{
rbytes: cmd.rbytes.Get(),
wbytes: cmd.wbytes.Get(),
nentry: cmd.nentry.Get(),
}
}
func (cmd *CmdDecode) GetDetailedInfo() interface{} {
return nil
}
func (cmd *CmdDecode) Main() {
log.Infof("decode from '%s' to '%s'\n", conf.Options.SourceRdbInput, conf.Options.TargetRdbOutput)
for i, input := range conf.Options.SourceRdbInput {
// decode one by one. By now, we don't support decoding concurrence.
output := fmt.Sprintf("%s.%d", conf.Options.TargetRdbOutput, i)
cmd.decode(input, output)
}
log.Info("decode: done")
}
func (cmd *CmdDecode) decode(input, output string) {
readin, nsize := utils.OpenReadFile(input)
defer readin.Close()
saveto := utils.OpenWriteFile(output)
defer saveto.Close()
reader := bufio.NewReaderSize(readin, utils.ReaderBufferSize)
writer := bufio.NewWriterSize(saveto, utils.WriterBufferSize)
ipipe := utils.NewRDBLoader(reader, &cmd.rbytes, base.RDBPipeSize)
opipe := make(chan string, cap(ipipe))
go func() {
defer close(opipe)
group := make(chan int, conf.Options.Parallel)
for i := 0; i < cap(group); i++ {
go func() {
defer func() {
group <- 0
}()
cmd.decoderMain(ipipe, opipe)
}()
}
for i := 0; i < cap(group); i++ {
<-group
}
}()
wait := make(chan struct{})
go func() {
defer close(wait)
for s := range opipe {
cmd.wbytes.Add(int64(len(s)))
if _, err := writer.WriteString(s); err != nil {
log.PanicError(err, "write string failed")
}
utils.FlushWriter(writer)
}
}()
for done := false; !done; {
select {
case <-wait:
done = true
case <-time.After(time.Second):
}
stat := cmd.Stat()
var b bytes.Buffer
fmt.Fprintf(&b, "decode: ")
if nsize != 0 {
fmt.Fprintf(&b, "total = %s - %12s [%3d%%]", utils.GetMetric(nsize), utils.GetMetric(stat.rbytes), 100*stat.rbytes/nsize)
} else {
fmt.Fprintf(&b, "total = %12s", utils.GetMetric(stat.rbytes))
}
fmt.Fprintf(&b, " write=%-12d", stat.wbytes)
fmt.Fprintf(&b, " entry=%-12d", stat.nentry)
log.Info(b.String())
}
}
func (cmd *CmdDecode) decoderMain(ipipe <-chan *rdb.BinEntry, opipe chan<- string) {
toText := func(p []byte) string {
var b bytes.Buffer
for _, c := range p {
switch {
case c >= '#' && c <= '~':
b.WriteByte(c)
default:
b.WriteByte('.')
}
}
return b.String()
}
toBase64 := func(p []byte) string {
return base64.StdEncoding.EncodeToString(p)
}
toJson := func(o interface{}) string {
b, err := json.Marshal(o)
if err != nil {
log.PanicError(err, "encode to json failed")
}
return string(b)
}
for e := range ipipe {
var b bytes.Buffer
if e.Type == rdb.RdbFlagAUX {
o := &struct {
Type string `json:"type"`
Key string `json:"key"`
Value64 string `json:"value64"`
}{
"aux", string(e.Key), string(e.Value),
}
fmt.Fprintf(&b, "%s\n", toJson(o))
cmd.nentry.Incr()
opipe <- b.String()
continue
}
o, err := rdb.DecodeDump(e.Value)
if err != nil {
log.PanicError(err, "decode failed")
}
switch obj := o.(type) {
default:
log.Panicf("unknown object %v", o)
case rdb.String:
o := &struct {
DB uint32 `json:"db"`
Type string `json:"type"`
ExpireAt uint64 `json:"expireat"`
Key string `json:"key"`
Key64 string `json:"key64"`
Value64 string `json:"value64"`
}{
e.DB, "string", e.ExpireAt, toText(e.Key), toBase64(e.Key),
toBase64(obj),
}
fmt.Fprintf(&b, "%s\n", toJson(o))
case rdb.List:
for i, ele := range obj {
o := &struct {
DB uint32 `json:"db"`
Type string `json:"type"`
ExpireAt uint64 `json:"expireat"`
Key string `json:"key"`
Key64 string `json:"key64"`
Index int `json:"index"`
Value64 string `json:"value64"`
}{
e.DB, "list", e.ExpireAt, toText(e.Key), toBase64(e.Key),
i, toBase64(ele),
}
fmt.Fprintf(&b, "%s\n", toJson(o))
}
case rdb.Hash:
for _, ele := range obj {
o := &struct {
DB uint32 `json:"db"`
Type string `json:"type"`
ExpireAt uint64 `json:"expireat"`
Key string `json:"key"`
Key64 string `json:"key64"`
Field string `json:"field"`
Field64 string `json:"field64"`
Value64 string `json:"value64"`
}{
e.DB, "hash", e.ExpireAt, toText(e.Key), toBase64(e.Key),
toText(ele.Field), toBase64(ele.Field), toBase64(ele.Value),
}
fmt.Fprintf(&b, "%s\n", toJson(o))
}
case rdb.Set:
for _, mem := range obj {
o := &struct {
DB uint32 `json:"db"`
Type string `json:"type"`
ExpireAt uint64 `json:"expireat"`
Key string `json:"key"`
Key64 string `json:"key64"`
Member string `json:"member"`
Member64 string `json:"member64"`
}{
e.DB, "set", e.ExpireAt, toText(e.Key), toBase64(e.Key),
toText(mem), toBase64(mem),
}
fmt.Fprintf(&b, "%s\n", toJson(o))
}
case rdb.ZSet:
for _, ele := range obj {
o := &struct {
DB uint32 `json:"db"`
Type string `json:"type"`
ExpireAt uint64 `json:"expireat"`
Key string `json:"key"`
Key64 string `json:"key64"`
Member string `json:"member"`
Member64 string `json:"member64"`
Score float64 `json:"score"`
}{
e.DB, "zset", e.ExpireAt, toText(e.Key), toBase64(e.Key),
toText(ele.Member), toBase64(ele.Member), ele.Score,
}
fmt.Fprintf(&b, "%s\n", toJson(o))
}
}
cmd.nentry.Incr()
opipe <- b.String()
}
}