-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhooks.go
More file actions
431 lines (371 loc) · 14 KB
/
hooks.go
File metadata and controls
431 lines (371 loc) · 14 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
package clusterkit
import (
"fmt"
"sync"
"time"
)
// ============================================
// Event Structures
// ============================================
// PartitionChangeEvent represents a partition assignment change
type PartitionChangeEvent struct {
PartitionID string // The partition that changed (e.g., "partition-5")
CopyFromNodes []*Node // Nodes to copy data from (all nodes that have the data)
CopyToNode *Node // Node that needs the data (YOU)
ChangeReason string // "node_join", "node_leave", "rebalance", "manual"
OldPrimary string // Previous primary node ID
NewPrimary string // New primary node ID
Timestamp time.Time // When the change occurred
}
// NodeJoinEvent represents a node joining the cluster
type NodeJoinEvent struct {
Node *Node // The joining node
ClusterSize int // Total nodes after join
IsBootstrap bool // Is this the first node?
Timestamp time.Time // When the node joined
}
// NodeRejoinEvent represents a node rejoining after being offline
type NodeRejoinEvent struct {
Node *Node // The rejoining node
OfflineDuration time.Duration // How long it was offline
LastSeenAt time.Time // When it was last seen
PartitionsBeforeLeave []string // Partitions it had before leaving
Timestamp time.Time // When it rejoined
}
// NodeLeaveEvent represents a node leaving the cluster
type NodeLeaveEvent struct {
Node *Node // Full node info (ID, IP, Name, Status)
Reason string // "health_check_failure", "graceful_shutdown", "removed_by_admin"
PartitionsOwned []string // Partitions this node was primary for
PartitionsReplica []string // Partitions this node was replica for
Timestamp time.Time // When it left
}
// RebalanceEvent represents a rebalancing operation
type RebalanceEvent struct {
Trigger string // "node_join", "node_leave", "manual"
TriggerNodeID string // Which node caused it
PartitionsToMove int // How many partitions will move
NodesAffected []string // Which nodes are affected
Timestamp time.Time // When rebalance started
}
// ClusterHealthEvent represents cluster health status change
type ClusterHealthEvent struct {
HealthyNodes int // Number of healthy nodes
UnhealthyNodes int // Number of unhealthy nodes
TotalNodes int // Total nodes in cluster
Status string // "healthy", "degraded", "critical"
UnhealthyNodeIDs []string // IDs of unhealthy nodes
Timestamp time.Time // When health changed
}
// ============================================
// Hook Function Types
// ============================================
// PartitionChangeHook is called when a partition assignment changes
type PartitionChangeHook func(event *PartitionChangeEvent)
// NodeJoinHook is called when a new node joins the cluster
type NodeJoinHook func(event *NodeJoinEvent)
// NodeRejoinHook is called when an existing node rejoins after being offline
// This fires BEFORE rebalancing - use for preparation (e.g., clearing stale data)
type NodeRejoinHook func(event *NodeRejoinEvent)
// NodeLeaveHook is called when a node leaves or is removed from the cluster
type NodeLeaveHook func(event *NodeLeaveEvent)
// RebalanceStartHook is called when a rebalance operation starts
type RebalanceStartHook func(event *RebalanceEvent)
// RebalanceCompleteHook is called when a rebalance operation completes
type RebalanceCompleteHook func(event *RebalanceEvent, duration time.Duration)
// ClusterHealthChangeHook is called when cluster health status changes
type ClusterHealthChangeHook func(event *ClusterHealthEvent)
// HookManager manages all cluster event hooks
type HookManager struct {
hooks []PartitionChangeHook
nodeJoinHooks []NodeJoinHook
nodeRejoinHooks []NodeRejoinHook
nodeLeaveHooks []NodeLeaveHook
rebalanceStartHooks []RebalanceStartHook
rebalanceCompleteHooks []RebalanceCompleteHook
clusterHealthHooks []ClusterHealthChangeHook
lastPartitionState map[string]*Partition // partitionID -> Partition
lastNodeSet map[string]bool // nodeID -> exists
lastNodeSeenTime map[string]time.Time // nodeID -> last seen timestamp
lastHealthStatus string // Last cluster health status
mu sync.RWMutex
workerPool chan struct{} // Semaphore for limiting concurrent hook executions
}
// NewHookManager creates a new hook manager
func newHookManager() *HookManager {
// Create a worker pool with max 50 concurrent hook executions
workerPool := make(chan struct{}, 50)
return &HookManager{
hooks: make([]PartitionChangeHook, 0),
nodeJoinHooks: make([]NodeJoinHook, 0),
nodeRejoinHooks: make([]NodeRejoinHook, 0),
nodeLeaveHooks: make([]NodeLeaveHook, 0),
rebalanceStartHooks: make([]RebalanceStartHook, 0),
rebalanceCompleteHooks: make([]RebalanceCompleteHook, 0),
clusterHealthHooks: make([]ClusterHealthChangeHook, 0),
lastPartitionState: make(map[string]*Partition),
lastNodeSet: make(map[string]bool),
lastNodeSeenTime: make(map[string]time.Time),
lastHealthStatus: "healthy",
workerPool: workerPool,
}
}
// OnPartitionChange registers a callback for partition changes
func (ck *ClusterKit) OnPartitionChange(hook PartitionChangeHook) {
ck.hookManager.mu.Lock()
defer ck.hookManager.mu.Unlock()
ck.hookManager.hooks = append(ck.hookManager.hooks, hook)
}
// OnNodeJoin registers a callback for when a NEW node joins
func (ck *ClusterKit) OnNodeJoin(hook NodeJoinHook) {
ck.hookManager.mu.Lock()
defer ck.hookManager.mu.Unlock()
ck.hookManager.nodeJoinHooks = append(ck.hookManager.nodeJoinHooks, hook)
}
// OnNodeRejoin registers a callback for when a node REJOINS after being offline
func (ck *ClusterKit) OnNodeRejoin(hook NodeRejoinHook) {
ck.hookManager.mu.Lock()
defer ck.hookManager.mu.Unlock()
ck.hookManager.nodeRejoinHooks = append(ck.hookManager.nodeRejoinHooks, hook)
}
// OnNodeLeave registers a callback for when a node leaves/is removed
func (ck *ClusterKit) OnNodeLeave(hook NodeLeaveHook) {
ck.hookManager.mu.Lock()
defer ck.hookManager.mu.Unlock()
ck.hookManager.nodeLeaveHooks = append(ck.hookManager.nodeLeaveHooks, hook)
}
// OnRebalanceStart registers a callback for when rebalancing starts
func (ck *ClusterKit) OnRebalanceStart(hook RebalanceStartHook) {
ck.hookManager.mu.Lock()
defer ck.hookManager.mu.Unlock()
ck.hookManager.rebalanceStartHooks = append(ck.hookManager.rebalanceStartHooks, hook)
}
// OnRebalanceComplete registers a callback for when rebalancing completes
func (ck *ClusterKit) OnRebalanceComplete(hook RebalanceCompleteHook) {
ck.hookManager.mu.Lock()
defer ck.hookManager.mu.Unlock()
ck.hookManager.rebalanceCompleteHooks = append(ck.hookManager.rebalanceCompleteHooks, hook)
}
// OnClusterHealthChange registers a callback for cluster health changes
func (ck *ClusterKit) OnClusterHealthChange(hook ClusterHealthChangeHook) {
ck.hookManager.mu.Lock()
defer ck.hookManager.mu.Unlock()
ck.hookManager.clusterHealthHooks = append(ck.hookManager.clusterHealthHooks, hook)
}
// checkPartitionChanges detects and notifies about partition changes
func (hm *HookManager) checkPartitionChanges(currentPartitions map[string]*Partition, cluster *Cluster) {
if len(hm.hooks) == 0 {
return // No hooks registered
}
hm.mu.Lock()
defer hm.mu.Unlock()
// First time - just save the state
if len(hm.lastPartitionState) == 0 {
hm.lastPartitionState = hm.copyPartitionMap(currentPartitions)
return
}
// Detect changes
changes := hm.detectChanges(hm.lastPartitionState, currentPartitions, cluster)
// Notify hooks with goroutine pooling
for _, change := range changes {
for _, hook := range hm.hooks {
// Acquire semaphore slot (blocks if pool is full)
hm.workerPool <- struct{}{}
// Execute hook in goroutine
go func(h PartitionChangeHook, event *PartitionChangeEvent) {
defer func() {
// Release semaphore slot
<-hm.workerPool
// Recover from panics in hooks
if r := recover(); r != nil {
fmt.Printf("Hook panic recovered: %v\n", r)
}
}()
h(event)
}(hook, &change)
}
}
// Update state
if len(changes) > 0 {
hm.lastPartitionState = hm.copyPartitionMap(currentPartitions)
}
}
// detectChanges compares old and new partition states
func (hm *HookManager) detectChanges(old, new map[string]*Partition, cluster *Cluster) []PartitionChangeEvent {
var changes []PartitionChangeEvent
for partitionID, newPartition := range new {
oldPartition, exists := old[partitionID]
if !exists {
// New partition created - not a change, skip
continue
}
// Find nodes that need data (new primary or new replicas)
nodesToNotify := make(map[string]bool)
// Check if primary changed
if oldPartition.PrimaryNode != newPartition.PrimaryNode {
nodesToNotify[newPartition.PrimaryNode] = true
}
// Check for new replicas
oldReplicaSet := make(map[string]bool)
for _, r := range oldPartition.ReplicaNodes {
oldReplicaSet[r] = true
}
for _, newReplicaID := range newPartition.ReplicaNodes {
if !oldReplicaSet[newReplicaID] {
// This is a NEW replica that needs data
nodesToNotify[newReplicaID] = true
}
}
// Create a change notification for each node that needs data
for nodeID := range nodesToNotify {
// Collect ALL nodes that have the data (OLD primary + OLD replicas)
copyFromNodes := []*Node{}
// IMPORTANT: Only use nodes that ALREADY HAD the data AND are NOT also migrating
// This ensures we copy from stable nodes with complete data
// Add old primary if still alive and not the target node
if oldPartition.PrimaryNode != nodeID {
if node := getNodeByID(cluster, oldPartition.PrimaryNode); node != nil {
copyFromNodes = append(copyFromNodes, node)
}
}
// Add all old replicas that are NOT also being promoted/changed
for _, replicaID := range oldPartition.ReplicaNodes {
if replicaID != nodeID {
// Check if this replica is also being promoted to primary
// If so, skip it as a source (it's also migrating)
if replicaID == newPartition.PrimaryNode && replicaID != oldPartition.PrimaryNode {
// This replica is being promoted to primary, skip as source
continue
}
if node := getNodeByID(cluster, replicaID); node != nil {
// Check if not already in list
alreadyAdded := false
for _, n := range copyFromNodes {
if n.ID == replicaID {
alreadyAdded = true
break
}
}
if !alreadyAdded {
copyFromNodes = append(copyFromNodes, node)
}
}
}
}
// Find the target node object
copyToNodeObj := getNodeByID(cluster, nodeID)
changes = append(changes, PartitionChangeEvent{
PartitionID: partitionID,
CopyToNode: copyToNodeObj,
CopyFromNodes: copyFromNodes,
ChangeReason: "rebalance", // Default reason, can be enhanced
OldPrimary: oldPartition.PrimaryNode,
NewPrimary: newPartition.PrimaryNode,
Timestamp: time.Now(),
})
}
}
return changes
}
// copyPartitionMap creates a deep copy of partition map
func (hm *HookManager) copyPartitionMap(partitions map[string]*Partition) map[string]*Partition {
result := make(map[string]*Partition)
for id, partition := range partitions {
replicas := make([]string, len(partition.ReplicaNodes))
copy(replicas, partition.ReplicaNodes)
result[id] = &Partition{
ID: partition.ID,
PrimaryNode: partition.PrimaryNode,
ReplicaNodes: replicas,
}
}
return result
}
// getNodeByID finds a node by ID in the cluster
func getNodeByID(cluster *Cluster, nodeID string) *Node {
for i := range cluster.Nodes {
if cluster.Nodes[i].ID == nodeID {
return &cluster.Nodes[i]
}
}
return nil
}
// notifyNodeJoin triggers node join hooks
func (hm *HookManager) notifyNodeJoin(node *Node, clusterSize int, isBootstrap bool) {
hm.mu.RLock()
hooks := make([]NodeJoinHook, len(hm.nodeJoinHooks))
copy(hooks, hm.nodeJoinHooks)
hm.mu.RUnlock()
event := &NodeJoinEvent{
Node: node,
ClusterSize: clusterSize,
IsBootstrap: isBootstrap,
Timestamp: time.Now(),
}
for _, hook := range hooks {
go func(h NodeJoinHook, e *NodeJoinEvent) {
defer func() {
if r := recover(); r != nil {
fmt.Printf("NodeJoin hook panic recovered: %v\n", r)
}
}()
h(e)
}(hook, event)
}
}
// notifyNodeRejoin triggers node rejoin hooks
func (hm *HookManager) notifyNodeRejoin(node *Node, partitionsBeforeLeave []string) {
hm.mu.RLock()
hooks := make([]NodeRejoinHook, len(hm.nodeRejoinHooks))
copy(hooks, hm.nodeRejoinHooks)
// Calculate offline duration
lastSeen := hm.lastNodeSeenTime[node.ID]
offlineDuration := time.Since(lastSeen)
if lastSeen.IsZero() {
offlineDuration = 0 // Unknown
}
hm.mu.RUnlock()
event := &NodeRejoinEvent{
Node: node,
OfflineDuration: offlineDuration,
LastSeenAt: lastSeen,
PartitionsBeforeLeave: partitionsBeforeLeave,
Timestamp: time.Now(),
}
for _, hook := range hooks {
go func(h NodeRejoinHook, e *NodeRejoinEvent) {
defer func() {
if r := recover(); r != nil {
fmt.Printf("NodeRejoin hook panic recovered: %v\n", r)
}
}()
h(e)
}(hook, event)
}
}
// notifyNodeLeave triggers node leave hooks
func (hm *HookManager) notifyNodeLeave(node *Node, reason string, partitionsOwned, partitionsReplica []string) {
hm.mu.RLock()
hooks := make([]NodeLeaveHook, len(hm.nodeLeaveHooks))
copy(hooks, hm.nodeLeaveHooks)
// Update last seen time
hm.lastNodeSeenTime[node.ID] = time.Now()
hm.mu.RUnlock()
event := &NodeLeaveEvent{
Node: node,
Reason: reason,
PartitionsOwned: partitionsOwned,
PartitionsReplica: partitionsReplica,
Timestamp: time.Now(),
}
for _, hook := range hooks {
go func(h NodeLeaveHook, e *NodeLeaveEvent) {
defer func() {
if r := recover(); r != nil {
fmt.Printf("NodeLeave hook panic recovered: %v\n", r)
}
}()
h(e)
}(hook, event)
}
}