Skip to content

Commit

Permalink
rename macros.xml to host.xml; fix check error when upgrade and config
Browse files Browse the repository at this point in the history
  • Loading branch information
YenchangChan committed Sep 17, 2021
1 parent a16408e commit 7e63515
Show file tree
Hide file tree
Showing 10 changed files with 55 additions and 38 deletions.
8 changes: 4 additions & 4 deletions ckconfig/macros.go → ckconfig/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@ import (
"github.com/housepower/ckman/model"
)

func GenerateMacrosXML(filename string, conf *model.CkDeployConfig, host string)(string, error){
func GenerateHostXML(filename string, conf *model.CkDeployConfig, host string)(string, error){
shardIndex := 0
hostName := ""
for i, shard := range conf.Shards {
for _, replica := range shard.Replicas {
if host == replica.Ip {
shardIndex = i + 1
hostName = replica.HostName
break
}
}
}

xml := common.NewXmlFile(filename)
xml.Begin("yandex")
xml.Comment("This xml file contains every node's special configuration self.")
xml.Write("interserver_http_host", host)
xml.Begin("macros")
xml.Write("cluster", conf.ClusterName)
xml.Write("shard", shardIndex)
xml.Write("replica", hostName)
xml.Write("replica", host)
xml.End("macros")
xml.End("yandex")
err := xml.Dump()
Expand Down
2 changes: 1 addition & 1 deletion ckconfig/metrika.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func GenLocalMetrika(indent int, conf *model.CkDeployConfig)string {
xml.Write("internal_replication", conf.IsReplica)
for _, replica := range shard.Replicas {
xml.Begin("replica")
xml.Write("host", replica.HostName)
xml.Write("host", replica.Ip)
xml.Write("port", conf.CkTcpPort)
xml.End("replica")
}
Expand Down
2 changes: 1 addition & 1 deletion common/xml.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (xml *XMLFile) WritewithAttr(tag string, value interface{}, attrs []XMLAttr
}
xml.context += fmt.Sprintf("%s=\"%v\"", attr.Key, finalValue(attr.Value))
}
xml.context += fmt.Sprintf("%v</%s>\n", tag, value)
xml.context += fmt.Sprintf(">%v</%s>\n", value, tag)
}

func (xml *XMLFile) Begin(tag string) {
Expand Down
2 changes: 1 addition & 1 deletion controller/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func DeployPackage(d deploy.Deploy) (string, error) {
}

log.Logger.Infof("start check service")
if err := d.Check(); err != nil {
if err := d.Check(5); err != nil {
return model.CHECK_PACKAGE_FAIL, err
}

Expand Down
67 changes: 41 additions & 26 deletions deploy/ck.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ type CkUpdateNodeParam struct {
Op int
}

var (
CheckTimeOutErr = errors.New("check clickhouse timeout error")
)

func (d *CKDeploy) Init() error {
d.Conf.Normalize()
d.HostInfos = make([]ckconfig.HostInfo, len(d.Hosts))
Expand Down Expand Up @@ -278,18 +282,20 @@ func (d *CKDeploy) Config() error {
}
userFiles = append(userFiles, profiles)

macrosFile, err := common.NewTempFile(path.Join(config.GetWorkDirectory(), "package"), "macros")
hostFile, err := common.NewTempFile(path.Join(config.GetWorkDirectory(), "package"), "host")
if err != nil {
lastError = err
return
}
defer os.Remove(macrosFile.FullName)
macros, err := ckconfig.GenerateMacrosXML(macrosFile.FullName, d.Conf, innerHost)
defer os.Remove(hostFile.FullName)
hostXml, err := ckconfig.GenerateHostXML(hostFile.FullName, d.Conf, innerHost)
if err != nil {
lastError = err
return
}
confFiles = append(confFiles, macros)
confFiles = append(confFiles, hostXml)

_, _ = common.RemoteExecute(d.User, d.Password, host, d.Port, "rm -rf /etc/clickhouse-server/config.d/* /etc/clickhouse-server/users.d/*")

if err := common.ScpUploadFiles(confFiles, "/etc/clickhouse-server/config.d/", d.User, d.Password, innerHost, d.Port); err != nil {
lastError = err
Expand All @@ -301,7 +307,7 @@ func (d *CKDeploy) Config() error {
}

cmds := make([]string, 0)
cmds = append(cmds, fmt.Sprintf("mv /etc/clickhouse-server/config.d/%s /etc/clickhouse-server/config.d/macros.xml", macrosFile.BaseName))
cmds = append(cmds, fmt.Sprintf("mv /etc/clickhouse-server/config.d/%s /etc/clickhouse-server/config.d/host.xml", hostFile.BaseName))
cmds = append(cmds, fmt.Sprintf("mv /etc/clickhouse-server/users.d/%s /etc/clickhouse-server/users.d/profiles.xml", profilesFile.BaseName))
cmds = append(cmds, "chown -R clickhouse:clickhouse /etc/clickhouse-server")

Expand Down Expand Up @@ -415,23 +421,31 @@ func (d *CKDeploy) Restart() error {
return nil
}

func (d *CKDeploy) Check() error {
time.Sleep(5 * time.Second)

func (d *CKDeploy) Check(timeout int) error {
var lastError error
for _, host := range d.Hosts {
innerHost := host
_ = d.Pool.Submit(func() {
db, err := common.ConnectClickHouse(innerHost, d.Conf.CkTcpPort, model.ClickHouseDefaultDB, d.Conf.User, d.Conf.Password)
if err != nil {
lastError = err
return
}
if err = db.Ping(); err != nil {
lastError = err
return
ticker := time.NewTicker(5*time.Second)
for {
select {
case <-ticker.C:
db, err := common.ConnectClickHouse(innerHost, d.Conf.CkTcpPort, model.ClickHouseDefaultDB, d.Conf.User, d.Conf.Password)
if err != nil {
continue
}
if err = db.Ping(); err != nil {
continue
}
if err == nil {
log.Logger.Debugf("host %s check done", innerHost)
return
}
case <-time.After(time.Duration(timeout)*time.Second):
lastError = CheckTimeOutErr
return
}
}
log.Logger.Debugf("host %s check done", innerHost)
})
}

Expand Down Expand Up @@ -470,20 +484,23 @@ func UpgradeCkCluster(conf *model.CKManClickHouseConfig, req model.CkUpgradeCkRe
switch req.Policy {
case model.UpgradePolicyRolling:
for _, host := range chHosts {
if err := upgradePackage(conf, []string{host}, packages); err != nil {
if err := upgradePackage(conf, []string{host}, packages, model.MaxTimeOut); err != nil {
return err
}
}
case model.UpgradePolicyFull:
return upgradePackage(conf, chHosts, packages)
err := upgradePackage(conf, chHosts, packages, 5)
if err != CheckTimeOutErr {
return err
}
default:
return fmt.Errorf("not support policy %s yet", req.Policy)
}

return nil
}

func upgradePackage(conf *model.CKManClickHouseConfig, hosts []string, packages []string) error {
func upgradePackage(conf *model.CKManClickHouseConfig, hosts []string, packages []string, timeout int) error {
deploy := ConvertCKDeploy(conf)
deploy.Hosts = hosts
deploy.Packages = packages
Expand All @@ -510,7 +527,7 @@ func upgradePackage(conf *model.CKManClickHouseConfig, hosts []string, packages
return err
}
log.Logger.Infof("cluster start succeed ")
if err := deploy.Check(); err != nil {
if err := deploy.Check(timeout); err != nil {
return err
}
log.Logger.Infof("cluster checked succeed ")
Expand All @@ -537,7 +554,7 @@ func StartCkCluster(conf *model.CKManClickHouseConfig) error {
if err := deploy.Start(); err != nil {
return err
}
if err := deploy.Check(); err != nil {
if err := deploy.Check(model.MaxTimeOut); err != nil {
return err
}
return nil
Expand Down Expand Up @@ -667,7 +684,7 @@ func AddCkClusterNode(conf *model.CKManClickHouseConfig, req *model.AddNodeReq)
if err := deploy.Start(); err != nil {
return err
}
if err := deploy.Check(); err != nil {
if err := deploy.Check(5); err != nil {
return err
}

Expand Down Expand Up @@ -821,9 +838,7 @@ func ConfigCkCluster(conf *model.CKManClickHouseConfig, restart bool)error {
if err := d.Restart(); err != nil {
return err
}
if err := d.Check(); err != nil {
return err
}
_ = d.Check(5)
}
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ type Deploy interface {
Start() error
Stop() error
Restart() error
Check() error
Check(timeout int) error
}
2 changes: 1 addition & 1 deletion deploy/zk.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ func (d *ZKDeploy) Restart() error {
return nil
}

func (d *ZKDeploy) Check() error {
func (d *ZKDeploy) Check(timeout int) error {
return nil
}
6 changes: 3 additions & 3 deletions docs/guide/deploy.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# 部署依赖
- `prometheus`(非必需)
- `node_exporter`(非必需)
- `nacos`(>`1.4`,目前不支持`2.0`)(非必需)
- `zookeeper`(>`3.5.0`, 推荐 )
- `nacos`(>`1.4`)(非必需)
- `zookeeper`(>`3.6.0`, 推荐 )

# 监控配置(可选,不影响ckman核心功能)

Expand All @@ -14,7 +14,7 @@

```ini
metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
metricsProvider.httpPort=7070 #暴露给promethues的监控端口
metricsProvider.httpPort=7000 #暴露给promethues的监控端口
admin.enableServer=true
admin.serverPort=8080 #暴露给四字命令如mntr等的监控端口,3.5.0以上版本支持
```
Expand Down
Binary file added docs/img/image-20210907094043322.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions model/deploy_ck.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const (
SshPasswordSave int = 0
SshPasswordNotSave int = 1
SshPasswordUsePubkey int = 2

MaxTimeOut int = 3600
)

type CkDeployConfig struct {
Expand Down

0 comments on commit 7e63515

Please sign in to comment.