7 Star 8 Fork 7

openGauss / ham4db

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
health_dao.go 5.21 KB
一键复制 编辑 原始数据 按行查看 历史
/*
Copyright 2015 Shlomi Noach, courtesy Booking.com
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package process
import (
"time"
"fmt"
"gitee.com/opengauss/ham4db/go/config"
"gitee.com/opengauss/ham4db/go/core/db"
"gitee.com/opengauss/ham4db/go/core/log"
"gitee.com/opengauss/ham4db/go/util/sqlutil"
)
// RegisterNode writes down this node in the ham_node_health table
func WriteRegisterNode(nodeHealth *NodeHealth) (healthy bool, err error) {
timeNow := time.Now()
reportedAgo := timeNow.Sub(nodeHealth.LastReported)
reportedSecondsAgo := int64(reportedAgo.Seconds())
if reportedSecondsAgo > config.HealthPollSeconds*2 {
// This entry is too old. No reason to persist it; already expired.
return false, nil
}
nodeHealth.onceHistory.Do(func() {
db.ExecSQL(`
insert ignore into ham_node_health_history
(hostname, token, first_seen_active_timestamp, extra_info, command, app_version)
values
(?, ?, NOW(), ?, ?, ?)
`,
nodeHealth.Hostname, nodeHealth.Token, nodeHealth.ExtraInfo, nodeHealth.Command,
nodeHealth.AppVersion,
)
})
{
sqlResult, err := db.ExecSQL(`
update ham_node_health set
last_seen_active_timestamp = now() - interval ? second,
extra_info = case when ? != '' then ? else extra_info end,
app_version = ?,
incrementing_indicator = incrementing_indicator + 1
where
hostname = ?
and token = ?
`,
reportedSecondsAgo,
nodeHealth.ExtraInfo, nodeHealth.ExtraInfo,
nodeHealth.AppVersion,
nodeHealth.Hostname, nodeHealth.Token,
)
if err != nil {
return false, log.Errore(err)
}
rows, err := sqlResult.RowsAffected()
if err != nil {
return false, log.Errore(err)
}
if rows > 0 {
return true, nil
}
}
// Got here? The UPDATE didn't work. Row isn't there.
{
dbBackend := ""
if config.Config.IsSQLite() {
dbBackend = config.Config.SQLite3DataFile
} else {
dbBackend = fmt.Sprintf("%s:%d", config.Config.BackendDBHost,
config.Config.BackendDBPort)
}
sqlResult, err := db.ExecSQL(`
insert ignore into ham_node_health
(hostname, token, first_seen_active_timestamp, last_seen_active_timestamp, extra_info, command, app_version, db_backend)
values (
?, ?,
now() - interval ? second, now() - interval ? second,
?, ?, ?, ?)
`,
nodeHealth.Hostname, nodeHealth.Token,
reportedSecondsAgo, reportedSecondsAgo,
nodeHealth.ExtraInfo, nodeHealth.Command,
nodeHealth.AppVersion, dbBackend,
)
if err != nil {
return false, log.Errore(err)
}
rows, err := sqlResult.RowsAffected()
if err != nil {
return false, log.Errore(err)
}
if rows > 0 {
return true, nil
}
}
return false, nil
}
// ExpireAvailableNodes is an aggressive purging method to remove
// node entries who have skipped their keepalive for two times.
func ExpireAvailableNodes() {
_, err := db.ExecSQL(`
delete
from ham_node_health
where
last_seen_active_timestamp < now() - interval ? second
`,
config.HealthPollSeconds*5,
)
if err != nil {
log.Errorf("ExpireAvailableNodes: failed to remove old entries: %+v", err)
}
}
// ExpireNodesHistory cleans up the nodes history and is run by
// the ham4db active node.
func ExpireNodesHistory() error {
_, err := db.ExecSQL(`
delete
from ham_node_health_history
where
first_seen_active_timestamp < now() - interval ? hour
`,
config.Config.UnseenInstanceForgetHours,
)
return log.Errore(err)
}
func ReadAvailableNodes(onlyHttpNodes bool) (nodes [](*NodeHealth), err error) {
extraInfo := ""
if onlyHttpNodes {
extraInfo = string(ExecutionHttpMode)
}
query := `
select
hostname, token, app_version, first_seen_active_timestamp, last_seen_active_timestamp, db_backend
from
ham_node_health
where
last_seen_active_timestamp > now() - interval ? second
and ? in (extra_info, '')
order by
hostname
`
err = db.Query(query, sqlutil.Args(config.HealthPollSeconds*2, extraInfo), func(m sqlutil.RowMap) error {
nodeHealth := &NodeHealth{
Hostname: m.GetString("hostname"),
Token: m.GetString("token"),
AppVersion: m.GetString("app_version"),
FirstSeenActive: m.GetString("first_seen_active_timestamp"),
LastSeenActive: m.GetString("last_seen_active_timestamp"),
DBBackend: m.GetString("db_backend"),
}
nodes = append(nodes, nodeHealth)
return nil
})
return nodes, log.Errore(err)
}
func TokenBelongsToHealthyHttpService(token string) (result bool, err error) {
extraInfo := string(ExecutionHttpMode)
query := `
select
token
from
ham_node_health
where
and token = ?
and extra_info = ?
`
err = db.Query(query, sqlutil.Args(token, extraInfo), func(m sqlutil.RowMap) error {
// Row exists? We're happy
result = true
return nil
})
return result, log.Errore(err)
}
1
https://gitee.com/opengauss/ham4db.git
git@gitee.com:opengauss/ham4db.git
opengauss
ham4db
ham4db
6b32d26d8b59

搜索帮助