Fetch the repository succeeded.
/*
Copyright 2017 Shlomi Noach, GitHub Inc.
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 (
"gitee.com/opengauss/ham4db/go/core/system/osp"
"gitee.com/opengauss/ham4db/go/dtstruct"
"sync"
"sync/atomic"
"time"
"gitee.com/opengauss/ham4db/go/config"
"gitee.com/opengauss/ham4db/go/core/consensus/raft"
"gitee.com/opengauss/ham4db/go/core/log"
"github.com/patrickmn/go-cache"
)
var lastHealthCheckUnixNano int64
var lastGoodHealthCheckUnixNano int64
var LastContinousCheckHealthy int64
var lastHealthCheckCache = cache.New(config.HealthPollSeconds*time.Second, time.Second)
type NodeHealth struct {
Hostname string
Token string
AppVersion string
FirstSeenActive string
LastSeenActive string
ExtraInfo string
Command string
DBBackend string
LastReported time.Time
onceHistory sync.Once
onceUpdate sync.Once
}
func NewNodeHealth() *NodeHealth {
return &NodeHealth{
Hostname: osp.GetHostname(),
Token: dtstruct.ProcessToken.Hash,
AppVersion: dtstruct.RuntimeCLIFlags.ConfiguredVersion,
}
}
func (nodeHealth *NodeHealth) Update() *NodeHealth {
nodeHealth.onceUpdate.Do(func() {
nodeHealth.Hostname = osp.GetHostname()
nodeHealth.Token = dtstruct.ProcessToken.Hash
nodeHealth.AppVersion = dtstruct.RuntimeCLIFlags.ConfiguredVersion
})
nodeHealth.LastReported = time.Now()
return nodeHealth
}
var ThisNodeHealth = NewNodeHealth()
type HealthStatus struct {
Healthy bool
Hostname string
Token string
IsActiveNode bool
ActiveNode NodeHealth
Error error
AvailableNodes [](*NodeHealth)
RaftLeader string
IsRaftLeader bool
RaftLeaderURI string
RaftAdvertise string
RaftHealthyMembers []string
}
type ExecutionMode string
const (
ExecutionCliMode ExecutionMode = "CLIMode"
ExecutionHttpMode = "HttpMode"
)
var continuousRegistrationOnce sync.Once
func RegisterNode(nodeHealth *NodeHealth) (healthy bool, err error) {
nodeHealth.Update()
healthy, err = WriteRegisterNode(nodeHealth)
atomic.StoreInt64(&lastHealthCheckUnixNano, time.Now().UnixNano())
if healthy {
atomic.StoreInt64(&lastGoodHealthCheckUnixNano, time.Now().UnixNano())
}
return healthy, err
}
// HealthTest attempts to write to the backend database and get a result
func HealthTest() (health *HealthStatus, err error) {
cacheKey := dtstruct.ProcessToken.Hash
if healthStatus, found := lastHealthCheckCache.Get(cacheKey); found {
return healthStatus.(*HealthStatus), nil
}
health = &HealthStatus{Healthy: false, Hostname: osp.GetHostname(), Token: dtstruct.ProcessToken.Hash}
defer lastHealthCheckCache.Set(cacheKey, health, cache.DefaultExpiration)
if healthy, err := RegisterNode(ThisNodeHealth); err != nil {
health.Error = err
return health, log.Errore(err)
} else {
health.Healthy = healthy
}
if orcraft.IsRaftEnabled() {
health.ActiveNode.Hostname = orcraft.GetLeader()
health.IsActiveNode = orcraft.IsLeader()
health.RaftLeader = orcraft.GetLeader()
health.RaftLeaderURI = orcraft.LeaderURI.Get()
health.IsRaftLeader = orcraft.IsLeader()
health.RaftAdvertise = config.Config.RaftAdvertise
health.RaftHealthyMembers = orcraft.HealthyMembers()
} else {
if health.ActiveNode, health.IsActiveNode, err = ElectedNode(); err != nil {
health.Error = err
return health, log.Errore(err)
}
}
health.AvailableNodes, err = ReadAvailableNodes(true)
return health, nil
}
func SinceLastHealthCheck() time.Duration {
timeNano := atomic.LoadInt64(&lastHealthCheckUnixNano)
if timeNano == 0 {
return 0
}
return time.Since(time.Unix(0, timeNano))
}
func SinceLastGoodHealthCheck() time.Duration {
timeNano := atomic.LoadInt64(&lastGoodHealthCheckUnixNano)
if timeNano == 0 {
return 0
}
return time.Since(time.Unix(0, timeNano))
}
// ContinuousRegistration will continuously update the ham_node_health
// table showing that the current process is still running.
func ContinuousRegistration(extraInfo string, command string) {
ThisNodeHealth.ExtraInfo = extraInfo
ThisNodeHealth.Command = command
continuousRegistrationOnce.Do(func() {
tickOperation := func() {
healthy, err := RegisterNode(ThisNodeHealth)
if err != nil {
log.Errorf("ContinuousRegistration: RegisterNode failed: %+v", err)
}
if healthy {
atomic.StoreInt64(&LastContinousCheckHealthy, 1)
} else {
atomic.StoreInt64(&LastContinousCheckHealthy, 0)
}
}
// First one is synchronous
tickOperation()
go func() {
registrationTick := time.Tick(config.HealthPollSeconds * time.Second)
for range registrationTick {
// We already run inside a go-routine so
// do not do this asynchronously. If we
// get stuck then we don't want to fill up
// the backend pool with connections running
// this maintenance operation.
tickOperation()
}
}()
})
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。