vitess.io/vitess@v0.16.2/go/cmd/zkctld/zkctld.go (about)

     1  /*
     2  Copyright 2019 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  // zkctld is a daemon that starts or initializes ZooKeeper with Vitess-specific
    18  // configuration. It will stay running as long as the underlying ZooKeeper
    19  // server, and will pass along SIGTERM.
    20  package main
    21  
    22  import (
    23  	"os"
    24  	"os/signal"
    25  	"syscall"
    26  
    27  	"github.com/spf13/pflag"
    28  
    29  	"vitess.io/vitess/go/acl"
    30  	"vitess.io/vitess/go/exit"
    31  	"vitess.io/vitess/go/vt/log"
    32  	"vitess.io/vitess/go/vt/logutil"
    33  	"vitess.io/vitess/go/vt/servenv"
    34  	"vitess.io/vitess/go/vt/zkctl"
    35  )
    36  
    37  var (
    38  	zkCfg = "6@<hostname>:3801:3802:3803"
    39  	myID  uint
    40  )
    41  
    42  func init() {
    43  	servenv.OnParse(registerFlags)
    44  }
    45  
    46  func registerFlags(fs *pflag.FlagSet) {
    47  	fs.StringVar(&zkCfg, "zk.cfg", zkCfg,
    48  		"zkid@server1:leaderPort1:electionPort1:clientPort1,...)")
    49  	fs.UintVar(&myID, "zk.myid", myID,
    50  		"which server do you want to be? only needed when running multiple instance on one box, otherwise myid is implied by hostname")
    51  
    52  	acl.RegisterFlags(fs)
    53  }
    54  
    55  func main() {
    56  	defer exit.Recover()
    57  	defer logutil.Flush()
    58  
    59  	servenv.ParseFlags("zkctld")
    60  	zkConfig := zkctl.MakeZkConfigFromString(zkCfg, uint32(myID))
    61  	zkd := zkctl.NewZkd(zkConfig)
    62  
    63  	if zkd.Inited() {
    64  		log.Infof("already initialized, starting without init...")
    65  		if err := zkd.Start(); err != nil {
    66  			log.Errorf("failed start: %v", err)
    67  			exit.Return(255)
    68  		}
    69  	} else {
    70  		log.Infof("initializing...")
    71  		if err := zkd.Init(); err != nil {
    72  			log.Errorf("failed init: %v", err)
    73  			exit.Return(255)
    74  		}
    75  	}
    76  
    77  	log.Infof("waiting for signal or server shutdown...")
    78  	sig := make(chan os.Signal, 1)
    79  	signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
    80  	select {
    81  	case <-zkd.Done():
    82  		log.Infof("server shut down on its own")
    83  	case <-sig:
    84  		log.Infof("signal received, shutting down server")
    85  
    86  		// Action to perform if there is an error
    87  		if err := zkd.Shutdown(); err != nil {
    88  			log.Errorf("error during shutdown:%v", err)
    89  			exit.Return(1)
    90  		}
    91  	}
    92  }