This repository has been archived by the owner on Nov 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 188
/
check_safe_mode.go
183 lines (164 loc) · 4.86 KB
/
check_safe_mode.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
// Copyright 2019 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
//
// script used for checking safeMode in DML is as expected
//
// In test case we send `update` SQL before, in and after a specific sharding DDL
// procedure, and this script checks that update binlog event added during sharding
// DDL not synced phase has safeMode true, otherwise the safeMode is false.
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"sort"
"strings"
"github.com/gogo/protobuf/jsonpb"
"github.com/pingcap/errors"
"github.com/siddontang/go-mysql/mysql"
"github.com/pingcap/dm/dm/pb"
"github.com/pingcap/dm/dm/tracer"
)
const (
opTypeUpdate = 2
opTypeDDL = 4
)
func scan_events(offset, limit int) ([]byte, error) {
uri := fmt.Sprintf("http://127.0.0.1:8264/events/scan?offset=%d&limit=%d", offset, limit)
resp, err := http.Get(uri)
if err != nil {
return nil, errors.Trace(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.Trace(err)
}
return body, nil
}
func traceSourceExtract(ev *pb.SyncerBinlogEvent) string {
return strings.Split(ev.Base.TraceID, ".")[0]
}
func binlogPosition(ev *pb.SyncerBinlogEvent) mysql.Position {
return mysql.Position{
Name: ev.State.CurrentPos.Name,
Pos: ev.State.CurrentPos.Pos,
}
}
func checkSafeMode(obtained, expected bool, ev, startEv, endEv *pb.SyncerBinlogEvent) {
if obtained != expected {
panic(fmt.Sprintf("safe_mode should be %v, got %v\nev: %v\nstart_ev: %v\nend_ev: %v\n",
expected, obtained, ev, startEv, endEv))
}
}
type BinlogEventSlice []*pb.SyncerBinlogEvent
func (c BinlogEventSlice) Len() int {
return len(c)
}
func (c BinlogEventSlice) Swap(i, j int) {
c[i], c[j] = c[j], c[i]
}
func (c BinlogEventSlice) Less(i, j int) bool {
posi := binlogPosition(c[i])
posj := binlogPosition(c[j])
return posi.Compare(posj) < 0
}
func main() {
var (
raw []byte
err error
offset int
limit int = 10
count int
)
updateEvents := make(map[string][]*pb.SyncerBinlogEvent)
ddlEvents := make(map[string][]*pb.SyncerBinlogEvent)
latestDDL := make(map[string][]*pb.SyncerBinlogEvent)
for {
raw, err = scan_events(offset, limit)
if err != nil {
panic(errors.ErrorStack(err))
}
var events [][]tracer.TraceEvent
err = json.Unmarshal(raw, &events)
if err != nil {
panic(err)
}
for _, event_group := range events {
for _, e := range event_group {
if e.Type == pb.TraceType_BinlogEvent {
ev := &pb.SyncerBinlogEvent{}
jsonE, _ := json.Marshal(e.Event)
err = jsonpb.UnmarshalString(string(jsonE), ev)
if err != nil {
panic(err)
}
worker := traceSourceExtract(ev)
if ev.OpType == opTypeUpdate {
if _, ok2 := updateEvents[worker]; !ok2 {
updateEvents[worker] = make([]*pb.SyncerBinlogEvent, 0)
}
updateEvents[worker] = append(updateEvents[worker], ev)
} else if ev.OpType == opTypeDDL {
if _, ok2 := ddlEvents[worker]; !ok2 {
ddlEvents[worker] = make([]*pb.SyncerBinlogEvent, 0)
}
ddlEvents[worker] = append(ddlEvents[worker], ev)
}
}
}
}
offset += len(events)
if len(events) < limit {
break
}
}
// sort and get last sharding DDL tracing events
for worker, events := range ddlEvents {
sort.Sort(BinlogEventSlice(events))
latestDDL[worker] = make([]*pb.SyncerBinlogEvent, 2)
// we have run two round of sharding DDL
if len(events) < 4 {
panic(fmt.Sprintf("invalid ddl events count, events: %v", events))
}
latestDDL[worker][1] = events[len(events)-1]
endPos := binlogPosition(latestDDL[worker][1])
for i := len(events) - 2; i >= 0; i-- {
pos := binlogPosition(events[i])
if pos.Compare(endPos) != 0 {
latestDDL[worker][0] = events[i]
break
}
}
if latestDDL[worker][0] == nil {
panic(fmt.Sprintf("first sharding DDL not found, events: %v", events))
}
}
// check safe mode of each `update` binlog event is reasonable
for worker, evs := range latestDDL {
startPos := binlogPosition(evs[0])
endPos := binlogPosition(evs[1])
for _, ev := range updateEvents[worker] {
pos := binlogPosition(ev)
safeMode := ev.State.SafeMode
if pos.Compare(endPos) > 0 || (pos.Compare(startPos) >= 0 && ev.Base.Tso > evs[1].Base.Tso) {
checkSafeMode(safeMode, false, ev, evs[0], evs[1])
} else if pos.Compare(startPos) >= 0 {
checkSafeMode(safeMode, true, ev, evs[0], evs[1])
}
count += 1
}
}
fmt.Printf("check %d update events passed!\n", count)
}