Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update component check #345

Merged
merged 16 commits into from
Dec 25, 2023
Merged
28 changes: 20 additions & 8 deletions tools/component/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,24 +65,36 @@ func successPrint(s string, hide bool) {

func newZkClient() (*zk.Conn, error) {
var c *zk.Conn
c, _, err := zk.Connect(config.Config.Zookeeper.ZkAddr, time.Second, zk.WithLogger(log.NewZkLogger()))
fmt.Println("zk addr=", config.Config.Zookeeper.ZkAddr)
var err error
c, eventChan, err := zk.Connect(config.Config.Zookeeper.ZkAddr, time.Second*5, zk.WithLogger(log.NewZkLogger()))
if err != nil {
fmt.Println("zookeeper connect error:", err)
return nil, errs.Wrap(err, "Zookeeper Addr: "+strings.Join(config.Config.Zookeeper.ZkAddr, " "))
}

// wait for successfully connect
timeout := time.After(5 * time.Second)
for {
select {
case event := <-eventChan:
if event.State == zk.StateConnected {
fmt.Println("Connected to Zookeeper")
goto Connected
}
case <-timeout:
return nil, errs.Wrap(errors.New("timeout waiting for Zookeeper connection"), "Zookeeper Addr: "+strings.Join(config.Config.Zookeeper.ZkAddr, " "))
}
}
Connected:

if config.Config.Zookeeper.Username != "" && config.Config.Zookeeper.Password != "" {
if err := c.AddAuth("digest", []byte(config.Config.Zookeeper.Username+":"+config.Config.Zookeeper.Password)); err != nil {
return nil, errs.Wrap(err, "Zookeeper Username: "+config.Config.Zookeeper.Username+
", Zookeeper Password: "+config.Config.Zookeeper.Password+
", Zookeeper Addr: "+strings.Join(config.Config.Zookeeper.ZkAddr, " "))
}
}
result, _, _ := c.Exists("/zookeeper")
if !result {
err = errors.New("zookeeper not exist")
return nil, errs.Wrap(err, "Zookeeper Addr: "+strings.Join(config.Config.Zookeeper.ZkAddr, " "))
}

return c, nil
}

Expand All @@ -99,7 +111,7 @@ func checkNewZkClient(hide bool) (*zk.Conn, error) {
errorPrint(fmt.Sprintf("Starting Zookeeper failed: %v.Please make sure your Zookeeper service has started", err.Error()), hide)
continue
}
successPrint(fmt.Sprintf("zk starts successfully after: %v times ", i), hide)
successPrint(fmt.Sprintf("zk starts successfully after: %v times ", i+1), hide)
return zkConn, nil
}
return nil, errs.Wrap(errors.New("Connecting to zk fails"))
Expand Down
Loading