varpulis-cluster 0.10.0

Distributed execution cluster for Varpulis streaming analytics
Documentation
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
//! Coordinator High Availability: K8s Lease-based leader election and state replication.
//!
//! ## Architecture
//!
//! - 3 coordinator pods (StatefulSet)
//! - One leader (does all writes: health sweeps, failovers, migrations)
//! - Two followers (receive state replication, serve read-only queries)
//! - K8s Service readiness probe routes worker traffic to leader only
//! - On leader failure: K8s Lease expires (~10s), follower promotes
//!
//! Requires the `k8s` feature flag.

use std::collections::HashMap;

use serde::{Deserialize, Serialize};
use tracing::{error, info, warn};

use crate::connector_config::ClusterConnector;

// ============================================================================
// HA Role
// ============================================================================

/// The HA role of a coordinator instance.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum HaRole {
    /// Default: no HA, single coordinator
    Standalone,
    /// Active coordinator — handles all writes
    Leader,
    /// Passive coordinator — receives state replication
    Follower { leader_id: String },
}

impl std::fmt::Display for HaRole {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            HaRole::Standalone => write!(f, "standalone"),
            HaRole::Leader => write!(f, "leader"),
            HaRole::Follower { leader_id } => write!(f, "follower(leader={})", leader_id),
        }
    }
}

impl HaRole {
    /// Returns true if this instance can perform writes.
    pub fn is_writer(&self) -> bool {
        matches!(self, HaRole::Standalone | HaRole::Leader)
    }
}

// ============================================================================
// State Replication
// ============================================================================

/// Serializable snapshot of a worker node (WorkerNode contains Instant which isn't serializable).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkerNodeSnapshot {
    pub id: String,
    pub address: String,
    pub status: String,
    pub pipelines_running: usize,
    pub max_pipelines: usize,
    pub cpu_cores: usize,
    pub assigned_pipelines: Vec<String>,
    pub events_processed: u64,
}

/// Serializable snapshot of the coordinator state.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CoordinatorSnapshot {
    pub workers: HashMap<String, WorkerNodeSnapshot>,
    pub pipeline_groups: HashMap<String, serde_json::Value>,
    pub connectors: HashMap<String, ClusterConnector>,
    pub scaling_policy: Option<serde_json::Value>,
}

/// Incremental state deltas for replication.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum StateDelta {
    WorkerRegistered {
        id: String,
        node: WorkerNodeSnapshot,
    },
    WorkerHeartbeat {
        id: String,
        pipelines_running: usize,
        events_processed: u64,
    },
    WorkerRemoved {
        id: String,
    },
    WorkerStatusChanged {
        id: String,
        status: String,
    },
    GroupDeployed {
        name: String,
        group: serde_json::Value,
    },
    GroupRemoved {
        name: String,
    },
    MigrationUpdated {
        id: String,
        status: String,
    },
    ConnectorChanged {
        name: String,
        connector: ClusterConnector,
    },
    ConnectorRemoved {
        name: String,
    },
}

/// Messages in the replication protocol (internal WS at /internal/replicate).
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum ReplicationMessage {
    /// Sent on follower connect: full state snapshot.
    FullSnapshot(CoordinatorSnapshot),
    /// Incremental update.
    Delta(StateDelta),
    /// Follower requests a full snapshot.
    RequestSnapshot,
}

// ============================================================================
// Leader Election
// ============================================================================

/// Status of the leader election.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum LeaderStatus {
    Leader,
    Follower { leader_id: String },
    Unknown,
}

/// K8s Lease-based leader elector.
#[cfg(feature = "k8s")]
#[derive(Debug)]
pub struct LeaderElector {
    pub lease_name: String,
    pub namespace: String,
    pub identity: String,
    pub lease_duration: std::time::Duration,
    pub renew_deadline: std::time::Duration,
    pub retry_period: std::time::Duration,
}

#[cfg(feature = "k8s")]
impl LeaderElector {
    pub fn new(namespace: String, identity: String) -> Self {
        Self {
            lease_name: "varpulis-coordinator-leader".to_string(),
            namespace,
            identity,
            lease_duration: std::time::Duration::from_secs(15),
            renew_deadline: std::time::Duration::from_secs(10),
            retry_period: std::time::Duration::from_secs(2),
        }
    }

    /// Run the leader election loop. Calls `on_status_change` when the role changes.
    pub async fn run<F>(&self, on_status_change: F)
    where
        F: Fn(LeaderStatus) + Send + Sync,
    {
        use k8s_openapi::api::coordination::v1::Lease;
        use kube::{Api, Client};

        let client = match Client::try_default().await {
            Ok(c) => c,
            Err(e) => {
                error!("Failed to create K8s client for leader election: {}", e);
                on_status_change(LeaderStatus::Unknown);
                return;
            }
        };

        let leases: Api<Lease> = Api::namespaced(client, &self.namespace);
        let mut current_status = LeaderStatus::Unknown;

        loop {
            let new_status = match self.try_acquire_or_renew(&leases).await {
                Ok(true) => LeaderStatus::Leader,
                Ok(false) => {
                    // Check who the leader is
                    match self.get_current_leader(&leases).await {
                        Some(leader_id) => LeaderStatus::Follower { leader_id },
                        None => LeaderStatus::Unknown,
                    }
                }
                Err(e) => {
                    warn!("Leader election error: {}", e);
                    LeaderStatus::Unknown
                }
            };

            if new_status != current_status {
                info!(
                    "Leader election status changed: {:?} -> {:?}",
                    current_status, new_status
                );
                on_status_change(new_status.clone());
                current_status = new_status;
            }

            tokio::time::sleep(self.retry_period).await;
        }
    }

    /// Try to acquire or renew the lease. Returns true if we're the leader.
    async fn try_acquire_or_renew(
        &self,
        leases: &kube::Api<k8s_openapi::api::coordination::v1::Lease>,
    ) -> Result<bool, String> {
        use k8s_openapi::api::coordination::v1::Lease;
        use k8s_openapi::apimachinery::pkg::apis::meta::v1::MicroTime;
        use kube::api::{ObjectMeta, PostParams};

        let now = k8s_openapi::jiff::Timestamp::now();

        match leases.get(&self.lease_name).await {
            Ok(existing) => {
                let spec = existing.spec.as_ref();
                let holder = spec
                    .and_then(|s| s.holder_identity.as_deref())
                    .unwrap_or("");

                if holder == self.identity {
                    // We hold it — renew
                    let mut updated = existing.clone();
                    if let Some(ref mut s) = updated.spec {
                        s.renew_time = Some(MicroTime(now));
                    }
                    leases
                        .replace(&self.lease_name, &PostParams::default(), &updated)
                        .await
                        .map_err(|e| format!("Lease renew failed: {}", e))?;
                    return Ok(true);
                }

                // Someone else holds it — check if expired
                let renew_time = spec.and_then(|s| s.renew_time.as_ref()).map(|t| t.0);
                let lease_duration_secs = spec.and_then(|s| s.lease_duration_seconds).unwrap_or(15);

                if let Some(renew) = renew_time {
                    let expiry = renew + std::time::Duration::from_secs(lease_duration_secs as u64);
                    if now > expiry {
                        // Expired — try to take over
                        let mut updated = existing.clone();
                        if let Some(ref mut s) = updated.spec {
                            s.holder_identity = Some(self.identity.clone());
                            s.renew_time = Some(MicroTime(now));
                            s.acquire_time = Some(MicroTime(now));
                            s.lease_duration_seconds = Some(self.lease_duration.as_secs() as i32);
                        }
                        match leases
                            .replace(&self.lease_name, &PostParams::default(), &updated)
                            .await
                        {
                            Ok(_) => return Ok(true),
                            Err(_) => return Ok(false), // Another node got it
                        }
                    }
                }

                Ok(false) // Someone else holds it and it's not expired
            }
            Err(kube::Error::Api(ae)) if ae.code == 404 => {
                // Lease doesn't exist — create it
                let lease = Lease {
                    metadata: ObjectMeta {
                        name: Some(self.lease_name.clone()),
                        namespace: Some(self.namespace.clone()),
                        ..Default::default()
                    },
                    spec: Some(k8s_openapi::api::coordination::v1::LeaseSpec {
                        holder_identity: Some(self.identity.clone()),
                        lease_duration_seconds: Some(self.lease_duration.as_secs() as i32),
                        acquire_time: Some(MicroTime(now)),
                        renew_time: Some(MicroTime(now)),
                        ..Default::default()
                    }),
                };
                match leases.create(&PostParams::default(), &lease).await {
                    Ok(_) => Ok(true),
                    Err(_) => Ok(false), // Race condition — another node created it
                }
            }
            Err(e) => Err(format!("Lease get failed: {}", e)),
        }
    }

    /// Get the current lease holder identity.
    async fn get_current_leader(
        &self,
        leases: &kube::Api<k8s_openapi::api::coordination::v1::Lease>,
    ) -> Option<String> {
        match leases.get(&self.lease_name).await {
            Ok(lease) => lease.spec.as_ref().and_then(|s| s.holder_identity.clone()),
            Err(_) => None,
        }
    }
}

/// Check if the coordinator is allowed to perform write operations.
pub fn require_writer(role: &HaRole) -> Result<(), crate::ClusterError> {
    if role.is_writer() {
        Ok(())
    } else {
        let leader = match role {
            HaRole::Follower { leader_id } => leader_id.clone(),
            _ => "unknown".to_string(),
        };
        Err(crate::ClusterError::NotLeader(leader))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_ha_role_standalone_allows_writes() {
        assert!(require_writer(&HaRole::Standalone).is_ok());
    }

    #[test]
    fn test_ha_role_leader_allows_writes() {
        assert!(require_writer(&HaRole::Leader).is_ok());
    }

    #[test]
    fn test_ha_role_follower_rejects_writes() {
        let role = HaRole::Follower {
            leader_id: "coord-0".into(),
        };
        let err = require_writer(&role).unwrap_err();
        assert!(err.to_string().contains("coord-0"));
    }

    #[test]
    fn test_ha_role_display() {
        assert_eq!(HaRole::Standalone.to_string(), "standalone");
        assert_eq!(HaRole::Leader.to_string(), "leader");
        assert_eq!(
            HaRole::Follower {
                leader_id: "c0".into()
            }
            .to_string(),
            "follower(leader=c0)"
        );
    }

    #[test]
    fn test_ha_role_serde() {
        let role = HaRole::Follower {
            leader_id: "coord-1".into(),
        };
        let json = serde_json::to_string(&role).unwrap();
        let parsed: HaRole = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed, role);

        for r in [
            HaRole::Standalone,
            HaRole::Leader,
            HaRole::Follower {
                leader_id: "x".into(),
            },
        ] {
            let json = serde_json::to_string(&r).unwrap();
            let parsed: HaRole = serde_json::from_str(&json).unwrap();
            assert_eq!(parsed, r);
        }
    }

    #[test]
    fn test_coordinator_snapshot_serde() {
        let snapshot = CoordinatorSnapshot {
            workers: HashMap::new(),
            pipeline_groups: HashMap::new(),
            connectors: HashMap::new(),
            scaling_policy: None,
        };
        let json = serde_json::to_string(&snapshot).unwrap();
        let parsed: CoordinatorSnapshot = serde_json::from_str(&json).unwrap();
        assert!(parsed.workers.is_empty());
        assert!(parsed.pipeline_groups.is_empty());
    }

    #[test]
    fn test_coordinator_snapshot_with_workers() {
        let mut workers = HashMap::new();
        workers.insert(
            "w1".to_string(),
            WorkerNodeSnapshot {
                id: "w1".into(),
                address: "http://localhost:9000".into(),
                status: "ready".into(),
                pipelines_running: 3,
                max_pipelines: 100,
                cpu_cores: 4,
                assigned_pipelines: vec!["p1".into(), "p2".into()],
                events_processed: 42000,
            },
        );
        let snapshot = CoordinatorSnapshot {
            workers,
            pipeline_groups: HashMap::new(),
            connectors: HashMap::new(),
            scaling_policy: None,
        };
        let json = serde_json::to_string(&snapshot).unwrap();
        let parsed: CoordinatorSnapshot = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed.workers.len(), 1);
        assert_eq!(parsed.workers["w1"].pipelines_running, 3);
    }

    #[test]
    fn test_state_delta_serde() {
        let deltas = vec![
            StateDelta::WorkerRegistered {
                id: "w1".into(),
                node: WorkerNodeSnapshot {
                    id: "w1".into(),
                    address: "http://localhost:9000".into(),
                    status: "registering".into(),
                    pipelines_running: 0,
                    max_pipelines: 100,
                    cpu_cores: 4,
                    assigned_pipelines: vec![],
                    events_processed: 0,
                },
            },
            StateDelta::WorkerHeartbeat {
                id: "w1".into(),
                pipelines_running: 3,
                events_processed: 500,
            },
            StateDelta::WorkerRemoved { id: "w1".into() },
            StateDelta::WorkerStatusChanged {
                id: "w1".into(),
                status: "unhealthy".into(),
            },
            StateDelta::GroupDeployed {
                name: "g1".into(),
                group: serde_json::json!({"name": "test-group"}),
            },
            StateDelta::GroupRemoved { name: "g1".into() },
            StateDelta::MigrationUpdated {
                id: "m1".into(),
                status: "completed".into(),
            },
            StateDelta::ConnectorRemoved {
                name: "mqtt1".into(),
            },
        ];

        for delta in deltas {
            let json = serde_json::to_string(&delta).unwrap();
            let parsed: StateDelta = serde_json::from_str(&json).unwrap();
            // Just verify round-trip doesn't panic
            let _ = serde_json::to_string(&parsed).unwrap();
        }
    }

    #[test]
    fn test_replication_message_serde() {
        let snapshot = CoordinatorSnapshot {
            workers: HashMap::new(),
            pipeline_groups: HashMap::new(),
            connectors: HashMap::new(),
            scaling_policy: None,
        };
        let msg = ReplicationMessage::FullSnapshot(snapshot);
        let json = serde_json::to_string(&msg).unwrap();
        let parsed: ReplicationMessage = serde_json::from_str(&json).unwrap();
        assert!(matches!(parsed, ReplicationMessage::FullSnapshot(_)));

        let msg = ReplicationMessage::RequestSnapshot;
        let json = serde_json::to_string(&msg).unwrap();
        let parsed: ReplicationMessage = serde_json::from_str(&json).unwrap();
        assert!(matches!(parsed, ReplicationMessage::RequestSnapshot));
    }

    #[test]
    fn test_leader_status_equality() {
        assert_eq!(LeaderStatus::Leader, LeaderStatus::Leader);
        assert_eq!(LeaderStatus::Unknown, LeaderStatus::Unknown);
        assert_eq!(
            LeaderStatus::Follower {
                leader_id: "a".into()
            },
            LeaderStatus::Follower {
                leader_id: "a".into()
            }
        );
        assert_ne!(LeaderStatus::Leader, LeaderStatus::Unknown);
    }
}