github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/cmd/dm-syncer/main.go (about)

     1  // Copyright 2019 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package main
    15  
    16  import (
    17  	"context"
    18  	"flag"
    19  	"fmt"
    20  	"os"
    21  	"os/signal"
    22  	"strings"
    23  	"syscall"
    24  
    25  	"github.com/pingcap/errors"
    26  	"github.com/pingcap/tiflow/dm/config"
    27  	"github.com/pingcap/tiflow/dm/ctl/common"
    28  	"github.com/pingcap/tiflow/dm/pb"
    29  	"github.com/pingcap/tiflow/dm/pkg/log"
    30  	"github.com/pingcap/tiflow/dm/pkg/terror"
    31  	"github.com/pingcap/tiflow/dm/syncer"
    32  	"github.com/pingcap/tiflow/pkg/version"
    33  	"go.uber.org/zap"
    34  )
    35  
    36  func main() {
    37  	// 1. init conf
    38  	commonConfig := newCommonConfig()
    39  	conf, err := commonConfig.parse(os.Args[1:])
    40  	switch errors.Cause(err) {
    41  	case nil:
    42  	case flag.ErrHelp:
    43  		os.Exit(0)
    44  	default:
    45  		common.PrintLinesf("parse cmd flags err: %s", terror.Message(err))
    46  		os.Exit(2)
    47  	}
    48  
    49  	conf.Mode = config.ModeIncrement
    50  	conf.UseRelay = false
    51  
    52  	// 2. init logger
    53  	err = log.InitLogger(&log.Config{
    54  		File:   conf.LogFile,
    55  		Format: conf.LogFormat,
    56  		Level:  strings.ToLower(conf.LogLevel),
    57  	})
    58  	if err != nil {
    59  		common.PrintLinesf("init logger error %s", terror.Message(err))
    60  		os.Exit(2)
    61  	}
    62  
    63  	// 3. print process version information
    64  	version.LogVersionInfo("dm-syncer")
    65  	log.L().Info("", zap.Stringer("dm-syncer conf", conf))
    66  
    67  	sync := syncer.NewSyncer(conf, nil, nil) // do not support shard DDL for singleton syncer.
    68  	sc := make(chan os.Signal, 1)
    69  	signal.Notify(sc, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
    70  
    71  	ctx, cancel := context.WithCancel(context.Background())
    72  
    73  	go func() {
    74  		sig := <-sc
    75  		log.L().Info("got signal to exit", zap.Stringer("signal", sig))
    76  		cancel()
    77  	}()
    78  
    79  	// 4. start the syncer
    80  	err = sync.Init(ctx)
    81  	if err != nil {
    82  		fmt.Printf("init syncer error %s", terror.Message(err))
    83  		os.Exit(2)
    84  	}
    85  	pr := make(chan pb.ProcessResult, 1)
    86  	sync.Process(ctx, pr)
    87  
    88  	pResult := <-pr
    89  	if len(pResult.Errors) > 0 {
    90  		fmt.Printf("run syncer error %v", pResult.Errors)
    91  		os.Exit(2)
    92  	}
    93  
    94  	// 5. close the syncer
    95  	sync.Close()
    96  	log.L().Info("dm-syncer exit")
    97  
    98  	// 6. flush log
    99  	if syncErr := log.L().Sync(); syncErr != nil {
   100  		fmt.Fprintln(os.Stderr, "sync log failed", syncErr)
   101  		os.Exit(1)
   102  	}
   103  }