func BeforePromote(oldMaster string) error { conn, _ := zkhelper.ConnectToZk(*zkAddr) defer conn.Close() groups, err := models.ServerGroups(conn, *productName) if err != nil { log.Errorf("get server groups error %v, give up failover", err) return failover.ErrGiveupFailover } found := false for _, group := range groups { for _, server := range group.Servers { if server.Addr == oldMaster { found = true break } } } if !found { log.Errorf("can not find %s in any groups, give up failover", oldMaster) return failover.ErrGiveupFailover } return nil }
func (e *RebornEnv) NewCoordConn() (zkhelper.Conn, error) { switch e.coordinator { case "zookeeper": return zkhelper.ConnectToZk(e.coordinatorAddr) case "etcd": addr := strings.TrimSpace(e.coordinatorAddr) if !strings.HasPrefix(addr, "http://") { addr = "http://" + addr } return zkhelper.NewEtcdConn(addr) } return nil, errors.Errorf("need coordinator in config file, %+v", e) }
func Promote(oldMaster string, newMaster string) error { conn, _ := zkhelper.ConnectToZk(*zkAddr) defer conn.Close() groups, err := models.ServerGroups(conn, *productName) if err != nil { return err } var groupId int found := false for _, group := range groups { for _, server := range group.Servers { if server.Addr == oldMaster { groupId = group.Id found = true break } } } if !found { return fmt.Errorf("can not find %s in any groups", oldMaster) } lock := utils.GetZkLock(conn, *productName) lock.Lock(fmt.Sprintf("promote server %+v", newMaster)) defer func() { err := lock.Unlock() if err != nil { log.Warning(err) } }() group, err := models.GetGroup(conn, *productName, groupId) if err != nil { return err } err = group.Promote(conn, newMaster) if err != nil { return err } return nil }
func main() { log.SetLevelByString("info") c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt) signal.Notify(c, syscall.SIGTERM) go func() { <-c Fatal("ctrl-c or SIGTERM found, exit") }() // productName, _ = config.ReadString("product", "test") args, err := docopt.Parse(usage, nil, true, "codis config v0.1", true) if err != nil { log.Error(err) } // set config file if args["-c"] != nil { configFile = args["-c"].(string) config, err = utils.InitConfigFromFile(configFile) if err != nil { Fatal(err) } } else { config, err = utils.InitConfig() if err != nil { Fatal(err) } } // set output log file if args["-L"] != nil { log.SetOutputByName(args["-L"].(string)) } // set log level if args["--log-level"] != nil { log.SetLevelByString(args["--log-level"].(string)) } productName, _ = config.ReadString("product", "test") zkAddr, _ = config.ReadString("zk", "localhost:2181") zkConn, _ = zkhelper.ConnectToZk(zkAddr) zkLock = utils.GetZkLock(zkConn, productName) broker, _ = config.ReadString("broker", "ledisdb") slot_num, _ = config.ReadInt("slot_num", 16) log.Debugf("product: %s", productName) log.Debugf("zk: %s", zkAddr) log.Debugf("broker: %s", broker) if err := registerConfigNode(); err != nil { log.Fatal(errors.ErrorStack(err)) } defer unRegisterConfigNode() if err := removeOrphanLocks(); err != nil { log.Fatal(errors.ErrorStack(err)) } cmd := args["<command>"].(string) cmdArgs := args["<args>"].([]string) go http.ListenAndServe(":10086", nil) err = runCommand(cmd, cmdArgs) if err != nil { log.Fatal(errors.ErrorStack(err)) } }
func CreateZkConn() zkhelper.Conn { conn, _ := zkhelper.ConnectToZk(zkAddr) return conn }