Skip to content

Commit

Permalink
update component check (#345)
Browse files Browse the repository at this point in the history
* update init config

* update init

* update component check

* update component check

* update component check

* update component check

* update component check

* update component check

* update component check

* update component check
  • Loading branch information
AndrewZuo01 committed Dec 25, 2023
1 parent 5bc7f9e commit 90e3679
Showing 1 changed file with 20 additions and 8 deletions.
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

0 comments on commit 90e3679

Please sign in to comment.