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
/
Copy pathmain.go
112 lines (100 loc) · 2.84 KB
/
main.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
// 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.
package main
import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/pingcap/errors"
"go.uber.org/zap"
"github.com/pingcap/dm/pkg/log"
)
func main() {
cfg := newConfig()
err := cfg.parse(os.Args[1:])
switch errors.Cause(err) {
case nil:
case flag.ErrHelp:
os.Exit(0)
default:
fmt.Printf("parse cmd flags err %s \n", err)
os.Exit(2)
}
err = log.InitLogger(&log.Config{
File: cfg.logFile,
Level: strings.ToLower(cfg.logLevel),
Format: cfg.logFormat,
})
if err != nil {
fmt.Printf("init logger error %v", errors.ErrorStack(err))
os.Exit(2)
}
conn, err := registerSlave(cfg.addr, cfg.username, cfg.password, uint32(cfg.serverID))
if err != nil {
log.L().Error("register slave", zap.Error(err))
os.Exit(2)
}
log.L().Info("registered slave", zap.Uint32("connection ID", conn.GetConnectionID()))
ctx, cancel := context.WithCancel(context.Background())
sc := make(chan os.Signal, 1)
signal.Notify(sc,
syscall.SIGHUP,
syscall.SIGINT,
syscall.SIGTERM,
syscall.SIGQUIT)
go func() {
sig := <-sc
cancel()
log.L().Info("got signal to exit", zap.Stringer("signal", sig))
err2 := closeConn(conn)
if err2 != nil {
log.L().Error("close connection", zap.Error(err2))
}
}()
err = startSync(conn, uint32(cfg.serverID), cfg.binlogName, uint32(cfg.binlogPos))
if err != nil {
log.L().Error("start sync", zap.Error(err))
os.Exit(2)
}
log.L().Info("start sync",
zap.Int("server-id", cfg.serverID), zap.String("binlog-name", cfg.binlogName),
zap.Int("binlog-pos", cfg.binlogPos))
var (
eventCount uint64
byteCount uint64
duration time.Duration
)
switch cfg.mode {
case 1:
eventCount, byteCount, duration, err = readEventsWithGoMySQL(ctx, conn)
case 2:
eventCount, byteCount, duration, err = readEventsWithoutGoMySQL(ctx, conn)
case 3:
byteCount, duration, err = readDataOnly(ctx, conn)
default:
log.L().Error("invalid mode specified`", zap.Int("mode", cfg.mode))
}
if err != nil {
log.L().Error("read events", zap.Error(err))
}
tps := float64(eventCount) / duration.Seconds()
speed := float64(byteCount) / duration.Seconds()
log.L().Info("binlog-event-blackhole exit",
zap.Uint64("event-count", eventCount), zap.Uint64("byte-count", byteCount),
zap.Duration("duration", duration), zap.Float64("tps", tps), zap.Float64("throughput (byte/s)", speed))
}