-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhealth.go
More file actions
284 lines (242 loc) · 6.99 KB
/
Copy pathhealth.go
File metadata and controls
284 lines (242 loc) · 6.99 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package clusterkit
import (
"fmt"
"net/http"
"sync"
"time"
)
// HealthChecker monitors node health and triggers removal on failures
type HealthChecker struct {
ck *ClusterKit
interval time.Duration
timeout time.Duration
failureThreshold int
nodeFailures map[string]int
nodeLastSeen map[string]time.Time
mu sync.RWMutex
stopChan chan struct{}
enabled bool
}
// HealthCheckConfig configures the health checker
type HealthCheckConfig struct {
Enabled bool // Enable automatic health checking
Interval time.Duration // How often to check (default: 5s)
Timeout time.Duration // Request timeout (default: 2s)
FailureThreshold int // Failures before removal (default: 3)
}
// NewHealthChecker creates a new health checker
func NewHealthChecker(ck *ClusterKit, config HealthCheckConfig) *HealthChecker {
// Set defaults
if config.Interval == 0 {
config.Interval = 5 * time.Second
}
if config.Timeout == 0 {
config.Timeout = 2 * time.Second
}
if config.FailureThreshold == 0 {
config.FailureThreshold = 3
}
return &HealthChecker{
ck: ck,
interval: config.Interval,
timeout: config.Timeout,
failureThreshold: config.FailureThreshold,
nodeFailures: make(map[string]int),
nodeLastSeen: make(map[string]time.Time),
stopChan: make(chan struct{}),
enabled: config.Enabled,
}
}
// Start begins health checking
func (hc *HealthChecker) Start() {
if !hc.enabled {
fmt.Println("[HEALTH] Health checking disabled")
return
}
fmt.Printf("[HEALTH] Starting health checker (interval: %v, threshold: %d)\n",
hc.interval, hc.failureThreshold)
go hc.monitorLoop()
}
// Stop stops health checking
func (hc *HealthChecker) Stop() {
if !hc.enabled {
return
}
close(hc.stopChan)
}
// monitorLoop continuously monitors node health
func (hc *HealthChecker) monitorLoop() {
ticker := time.NewTicker(hc.interval)
defer ticker.Stop()
fmt.Println("[HEALTH] Monitor loop started")
for {
select {
case <-ticker.C:
fmt.Println("[HEALTH] Running health check cycle...")
hc.checkAllNodes()
case <-hc.stopChan:
fmt.Println("[HEALTH] Monitor loop stopped")
return
}
}
}
// checkAllNodes checks health of all nodes except self
func (hc *HealthChecker) checkAllNodes() {
hc.ck.mu.RLock()
nodes := make([]Node, len(hc.ck.cluster.Nodes))
copy(nodes, hc.ck.cluster.Nodes)
selfID := hc.ck.nodeID
hc.ck.mu.RUnlock()
for _, node := range nodes {
// Don't check self
if node.ID == selfID {
continue
}
// Check node health
healthy := hc.checkNode(node)
if healthy {
hc.markHealthy(node.ID)
} else {
hc.markUnhealthy(node.ID)
}
}
}
// checkNode checks if a single node is healthy
func (hc *HealthChecker) checkNode(node Node) bool {
// Build health check URL
url := fmt.Sprintf("http://%s/ready", node.IP)
// Create client with timeout
client := &http.Client{
Timeout: hc.timeout,
}
// Send health check request
resp, err := client.Get(url)
if err != nil {
fmt.Printf("[HEALTH] Node %s check failed: %v\n", node.ID, err)
return false
}
defer resp.Body.Close()
// Check status code
healthy := resp.StatusCode == http.StatusOK
if !healthy {
fmt.Printf("[HEALTH] Node %s returned status %d\n", node.ID, resp.StatusCode)
}
return healthy
}
// markHealthy marks a node as healthy
func (hc *HealthChecker) markHealthy(nodeID string) {
hc.mu.Lock()
defer hc.mu.Unlock()
// Reset failure count
if hc.nodeFailures[nodeID] > 0 {
fmt.Printf("[HEALTH] Node %s recovered\n", nodeID)
hc.nodeFailures[nodeID] = 0
}
hc.nodeLastSeen[nodeID] = time.Now()
}
// markUnhealthy marks a node as unhealthy
func (hc *HealthChecker) markUnhealthy(nodeID string) {
hc.mu.Lock()
hc.nodeFailures[nodeID]++
failures := hc.nodeFailures[nodeID]
hc.mu.Unlock()
fmt.Printf("[HEALTH] Node %s health check failed (%d/%d)\n",
nodeID, failures, hc.failureThreshold)
// Check if we should remove the node
if failures >= hc.failureThreshold {
fmt.Printf("[HEALTH] ❌ Node %s exceeded failure threshold, removing from cluster\n", nodeID)
hc.removeNode(nodeID)
}
}
// removeNode removes a failed node from the cluster
func (hc *HealthChecker) removeNode(nodeID string) {
// Only leader can remove nodes
if !hc.ck.consensusManager.IsLeader() {
fmt.Printf("[HEALTH] Not leader, skipping node removal for %s\n", nodeID)
return
}
fmt.Printf("[HEALTH] Removing failed node %s from cluster\n", nodeID)
// Get node info and partitions before removal
hc.ck.mu.RLock()
var node *Node
var partitionsOwned, partitionsReplica []string
for i := range hc.ck.cluster.Nodes {
if hc.ck.cluster.Nodes[i].ID == nodeID {
nodeCopy := hc.ck.cluster.Nodes[i]
node = &nodeCopy
break
}
}
if hc.ck.cluster.PartitionMap != nil {
for _, partition := range hc.ck.cluster.PartitionMap.Partitions {
if partition.PrimaryNode == nodeID {
partitionsOwned = append(partitionsOwned, partition.ID)
}
for _, replicaID := range partition.ReplicaNodes {
if replicaID == nodeID {
partitionsReplica = append(partitionsReplica, partition.ID)
break
}
}
}
}
hc.ck.mu.RUnlock()
// Remove from Raft cluster
if err := hc.ck.consensusManager.RemoveServer(nodeID); err != nil {
fmt.Printf("[HEALTH] Failed to remove from Raft: %v\n", err)
return
}
// Propose removal through Raft consensus
if err := hc.ck.consensusManager.ProposeAction("remove_node", map[string]interface{}{
"id": nodeID,
}); err != nil {
fmt.Printf("[HEALTH] Failed to propose node removal: %v\n", err)
return
}
// Trigger node leave hook
if node != nil {
hc.ck.hookManager.notifyNodeLeave(node, "health_check_failure", partitionsOwned, partitionsReplica)
}
// Clear failure tracking
hc.mu.Lock()
delete(hc.nodeFailures, nodeID)
delete(hc.nodeLastSeen, nodeID)
hc.mu.Unlock()
// Trigger rebalancing after removal
go func() {
time.Sleep(2 * time.Second)
hc.ck.mu.RLock()
hasPartitions := hc.ck.cluster.PartitionMap != nil &&
len(hc.ck.cluster.PartitionMap.Partitions) > 0
hc.ck.mu.RUnlock()
if hasPartitions {
fmt.Printf("[HEALTH] Triggering rebalance after node removal\n")
if err := hc.ck.RebalancePartitions(); err != nil {
fmt.Printf("[HEALTH] Failed to rebalance: %v\n", err)
}
}
}()
fmt.Printf("[HEALTH] ✓ Node %s removed successfully\n", nodeID)
}
// GetNodeHealth returns health status of all nodes
func (hc *HealthChecker) GetNodeHealth() map[string]NodeHealth {
hc.mu.RLock()
defer hc.mu.RUnlock()
result := make(map[string]NodeHealth)
for nodeID, failures := range hc.nodeFailures {
result[nodeID] = NodeHealth{
NodeID: nodeID,
Failures: failures,
LastSeen: hc.nodeLastSeen[nodeID],
Healthy: failures == 0,
}
}
return result
}
// NodeHealth represents health status of a node
type NodeHealth struct {
NodeID string `json:"node_id"`
Failures int `json:"failures"`
LastSeen time.Time `json:"last_seen"`
Healthy bool `json:"healthy"`
}