forked from pingcap/dm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
113 lines (97 loc) · 2.89 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
113
// 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"
"github.com/pingcap/dm/dm/config"
"github.com/pingcap/dm/dm/ctl/common"
"github.com/pingcap/dm/dm/pb"
"github.com/pingcap/dm/pkg/log"
"github.com/pingcap/dm/pkg/terror"
"github.com/pingcap/dm/pkg/utils"
"github.com/pingcap/dm/syncer"
"github.com/pingcap/errors"
globalLog "github.com/pingcap/log"
"go.uber.org/zap"
)
func main() {
// 1. init conf
commonConfig := newCommonConfig()
conf, err := commonConfig.parse(os.Args[1:])
switch errors.Cause(err) {
case nil:
case flag.ErrHelp:
os.Exit(0)
default:
common.PrintLinesf("parse cmd flags err: %s", terror.Message(err))
os.Exit(2)
}
conf.Mode = config.ModeIncrement
conf.UseRelay = false
// 2. init logger
err = log.InitLogger(&log.Config{
File: conf.LogFile,
Format: conf.LogFormat,
Level: strings.ToLower(conf.LogLevel),
})
if err != nil {
common.PrintLinesf("init logger error %s", terror.Message(err))
os.Exit(2)
}
// currently only schema tracker use global logger(std logger), simply replace it with `error` level
// may be we should support config logger in mock tidb later
confG := &globalLog.Config{Level: "error", File: globalLog.FileLogConfig{}, Format: conf.LogFormat}
lg, r, _ := globalLog.InitLogger(confG)
lg = lg.With(zap.String("component", "ddl tracker"))
globalLog.ReplaceGlobals(lg, r)
// 3. print process version information
utils.PrintInfo("dm-syncer", func() {
log.L().Info("", zap.Stringer("dm-syncer conf", conf))
})
sync := syncer.NewSyncer(conf, nil, nil) // do not support shard DDL for singleton syncer.
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
ctx, cancel := context.WithCancel(context.Background())
go func() {
sig := <-sc
log.L().Info("got signal to exit", zap.Stringer("signal", sig))
cancel()
}()
// 4. start the syncer
err = sync.Init(ctx)
if err != nil {
fmt.Printf("init syncer error %s", terror.Message(err))
os.Exit(2)
}
pr := make(chan pb.ProcessResult, 1)
sync.Process(ctx, pr)
pResult := <-pr
if len(pResult.Errors) > 0 {
fmt.Printf("run syncer error %v", pResult.Errors)
os.Exit(2)
}
// 5. close the syncer
sync.Close()
log.L().Info("dm-syncer exit")
// 6. flush log
if syncErr := log.L().Sync(); syncErr != nil {
fmt.Fprintln(os.Stderr, "sync log failed", syncErr)
os.Exit(1)
}
}