-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
health.go
167 lines (141 loc) · 4.46 KB
/
health.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
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 (
"sync"
"sync/atomic"
"time"
"vitess.io/vitess/go/vt/log"
"vitess.io/vitess/go/vt/vtorc/config"
"vitess.io/vitess/go/vt/vtorc/util"
"github.com/patrickmn/go-cache"
)
var lastHealthCheckUnixNano int64
var lastGoodHealthCheckUnixNano int64
var LastContinousCheckHealthy int64
var FirstDiscoveryCycleComplete atomic.Bool
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: ThisHostname,
Token: util.ProcessToken.Hash,
}
}
func (nodeHealth *NodeHealth) Update() *NodeHealth {
nodeHealth.onceUpdate.Do(func() {
nodeHealth.Hostname = ThisHostname
nodeHealth.Token = util.ProcessToken.Hash
})
nodeHealth.LastReported = time.Now()
return nodeHealth
}
var ThisNodeHealth = NewNodeHealth()
type HealthStatus struct {
Healthy bool
Hostname string
Token string
IsActiveNode bool
DiscoveredOnce bool
ActiveNode *NodeHealth
Error error
AvailableNodes [](*NodeHealth)
RaftLeader string
IsRaftLeader bool
RaftLeaderURI string
RaftAdvertise string
RaftHealthyMembers []string
}
type VTOrcExecutionMode string
const (
VTOrcExecutionCliMode VTOrcExecutionMode = "CLIMode"
VTOrcExecutionHTTPMode VTOrcExecutionMode = "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 := util.ProcessToken.Hash
if healthStatus, found := lastHealthCheckCache.Get(cacheKey); found {
return healthStatus.(*HealthStatus), nil
}
health = &HealthStatus{Healthy: false, Hostname: ThisHostname, Token: util.ProcessToken.Hash}
defer lastHealthCheckCache.Set(cacheKey, health, cache.DefaultExpiration)
healthy, err := RegisterNode(ThisNodeHealth)
if err != nil {
health.Error = err
log.Error(err)
return health, err
}
health.Healthy = healthy
health.DiscoveredOnce = FirstDiscoveryCycleComplete.Load()
if health.ActiveNode, health.IsActiveNode, err = ElectedNode(); err != nil {
health.Error = err
log.Error(err)
return health, err
}
health.AvailableNodes, _ = ReadAvailableNodes(true)
return health, nil
}
// ContinuousRegistration will continuously update the 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()
}
}()
})
}