forked from tair-opensource/RedisShake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
infooplog.go
87 lines (77 loc) · 2.26 KB
/
infooplog.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
package oplog
import (
"fmt"
"strconv"
)
type RedisInfoOplog struct {
CurrentOpid int64
GtidSet map[uint64]int64 // serverId --> opid
OpdelOpid map[string]int64
}
// ParseRedisOplogInfo convert result of info oplog to map[uint64]int64(<serverID> --> <appliedOpid>).
//
// info oplog reply example:
// # Oplog
// current_opid:1
// opapply_source_count:1
// opapply_source_0:server_id=3171317,applied_opid=1
// opdel_source_count:2
// opdel_source_0:source_name=bls_channel_02,to_del_opid=1,last_update_time=1500279590
// opdel_source_1:source_name=bls_channel_01,to_del_opid=1,last_update_time=1500279587
func ParseRedisInfoOplog(oplogInfo []byte) (*RedisInfoOplog, error) {
p := new(RedisInfoOplog)
var err error
var i int
var opapplySourceCount uint64
// "opapply_source_count:1\r\nopapply_source_0:server_id=3171317,applied_opid=1\r\n" is converted to map[string]string{"opapply_source_count": "1", "opapply_source_0": "server_id=3171317,applied_opid=1"}.
KV := ParseInfo(oplogInfo)
// current_opid
if value, ok := KV["current_opid"]; ok {
p.CurrentOpid, err = strconv.ParseInt(value, 10, 0)
if err != nil {
return nil, err
}
} else {
p.CurrentOpid = -1
}
// opapply_source_count:xxx
if value, ok := KV["opapply_source_count"]; ok {
opapplySourceCount, err = strconv.ParseUint(value, 10, 0)
if err != nil {
return nil, err
}
} else {
goto return_error
}
p.GtidSet = make(map[uint64]int64)
for i = 0; i < int(opapplySourceCount); i++ {
key := fmt.Sprintf("opapply_source_%d", i) // key "opapply_source_0"
value, ok := KV[key] // value "server_id=7031,applied_opid=9842282"
if !ok {
goto return_error
}
subKV := ParseValue(value)
var serverID uint64
var appliedOpid int64
if serverIDStr, ok := subKV["server_id"]; ok {
serverID, err = strconv.ParseUint(serverIDStr, 10, 0)
if err != nil {
return nil, err
}
} else {
goto return_error
}
if appliedOpidStr, ok := subKV["applied_opid"]; ok {
appliedOpid, err = strconv.ParseInt(appliedOpidStr, 10, 0)
if err != nil {
return nil, err
}
} else {
goto return_error
}
p.GtidSet[serverID] = appliedOpid
}
return p, nil
return_error:
return nil, fmt.Errorf("invalid opapply info:\n %s", string(oplogInfo))
}