appcore-gateway 1.0.2-rc

Multi-tenant Gateway capability for the AppCore Runtime.
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
// =============================================================================
//        #######
//     ###       ###     F: coordinator.rs
//    ##   ## ##   ##    P: AppCore-Runtime
//         ## ##
//                       C: 2026/08/26 00:00:00 by dnettoRaw
//    ##   ## ##   ##    U: 2026/08/26 00:00:00 by dnettoRaw
//      ###########      S: 2.0.0-beta.1
// =============================================================================

//! Bounded tenant-lease coordination for an opt-in HA Gateway instance.

use super::coordinator_support::*;
use super::{
    GatewayFederationUrl, GatewayHaLifecycle, GatewayHaLifecycleSnapshot, GatewayHaMode,
    GatewayHaOwnershipSnapshot, GatewayHaOwnershipSource, GatewayInstanceLease,
    GatewayRegistryError, GatewayRegistryFuture, GatewayRegistryProvider, GatewayRegistryResult,
    GatewaySessionRecord, GatewayWorkerRecord, MAX_GATEWAY_INSTANCE_LEASE_TTL_MS,
    MAX_GATEWAY_REGISTRY_CONCURRENCY,
};
use crate::config::MAX_GATEWAY_TENANTS;
use appcore_types::{ClusterId, InstanceId, TenantId};
use futures_util::{stream, StreamExt};
use parking_lot::RwLock;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::{watch, Mutex};

/// Minimum ownership TTL accepted by the HA coordinator.
pub const MIN_GATEWAY_HA_LEASE_TTL_MS: u64 = 1_000;

/// One configured tenant and cluster ownership boundary.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GatewayHaTenantBinding {
    /// Tenant whose local connections this instance may own.
    pub tenant_id: TenantId,
    /// Cluster containing those local connections.
    pub cluster_id: ClusterId,
}

/// Validated fixed ownership configuration for one Gateway process.
#[derive(Debug, Clone)]
pub struct GatewayHaCoordinatorConfig {
    /// Stable identity of this Gateway process instance.
    pub instance_id: InstanceId,
    /// Authenticated federation endpoint advertised to peers.
    pub federation_url: GatewayFederationUrl,
    /// Complete bounded tenant ownership set.
    pub tenants: Vec<GatewayHaTenantBinding>,
    /// Shared ownership lease duration.
    pub lease_ttl: Duration,
    /// Interval between exact lease renewals.
    pub renewal_interval: Duration,
}

impl GatewayHaCoordinatorConfig {
    /// Validates tenant uniqueness and renewal safety bounds.
    pub fn validate(&self) -> GatewayRegistryResult<()> {
        let ttl_ms = duration_ms(self.lease_ttl)?;
        let renewal_ms = duration_ms(self.renewal_interval)?;
        if self.tenants.is_empty()
            || self.tenants.len() > MAX_GATEWAY_TENANTS
            || !(MIN_GATEWAY_HA_LEASE_TTL_MS..=MAX_GATEWAY_INSTANCE_LEASE_TTL_MS).contains(&ttl_ms)
            || renewal_ms > ttl_ms / 3
        {
            return Err(GatewayRegistryError::InvalidContract);
        }
        for (index, binding) in self.tenants.iter().enumerate() {
            if self.tenants[index + 1..]
                .iter()
                .any(|candidate| candidate.tenant_id == binding.tenant_id)
            {
                return Err(GatewayRegistryError::InvalidContract);
            }
        }
        Ok(())
    }
}

/// Safe coordinator telemetry without endpoints, credentials, or tenant IDs.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct GatewayHaCoordinatorSnapshot {
    /// Admission lifecycle and recovery telemetry.
    pub lifecycle: GatewayHaLifecycleSnapshot,
    /// Number of configured tenant ownership boundaries.
    pub configured_tenants: usize,
    /// Number of current locally retained leases.
    pub active_leases: usize,
    /// Number of worker ownership records replayed under the current epochs.
    pub active_workers: usize,
    /// Number of session ownership records replayed under the current epochs.
    pub active_sessions: usize,
    /// Complete acquisition rounds.
    pub acquisitions: u64,
    /// Complete renewal rounds.
    pub renewals: u64,
    /// Provider rounds that failed closed.
    pub failures: u64,
    /// Shared request claims accepted by the provider.
    pub request_claims: u64,
    /// Shared request completions accepted by exact fences.
    pub request_completions: u64,
    /// Shared request cancellations accepted by the origin fence.
    pub request_cancellations: u64,
    /// Successful remote-owner federation responses accepted under a fence.
    pub remote_forwards: u64,
}

/// Serializes acquisition, renewal, recovery, and release for one HA owner.
pub struct GatewayHaCoordinator {
    pub(super) provider: Arc<dyn GatewayRegistryProvider>,
    pub(super) lifecycle: Arc<GatewayHaLifecycle>,
    config: GatewayHaCoordinatorConfig,
    pub(super) ownership: RwLock<CoordinatorOwnership>,
    pub(super) operation: Mutex<()>,
    acquisitions: AtomicU64,
    renewals: AtomicU64,
    failures: AtomicU64,
    pub(super) request_claims: AtomicU64,
    pub(super) request_completions: AtomicU64,
    pub(super) request_cancellations: AtomicU64,
    pub(super) remote_forwards: AtomicU64,
}

#[derive(Default)]
pub(super) struct CoordinatorOwnership {
    pub(super) leases: Vec<GatewayInstanceLease>,
    pub(super) workers: Vec<GatewayWorkerRecord>,
    pub(super) sessions: Vec<GatewaySessionRecord>,
}

pub(super) enum RecoveredOwnership {
    Worker(GatewayWorkerRecord),
    Session(GatewaySessionRecord),
}

impl GatewayHaCoordinator {
    /// Creates a stopped coordinator. No work is admitted before recovery.
    pub fn new(
        provider: Arc<dyn GatewayRegistryProvider>,
        lifecycle: Arc<GatewayHaLifecycle>,
        config: GatewayHaCoordinatorConfig,
    ) -> GatewayRegistryResult<Self> {
        config.validate()?;
        Ok(Self {
            provider,
            lifecycle,
            config,
            ownership: RwLock::new(CoordinatorOwnership::default()),
            operation: Mutex::new(()),
            acquisitions: AtomicU64::new(0),
            renewals: AtomicU64::new(0),
            failures: AtomicU64::new(0),
            request_claims: AtomicU64::new(0),
            request_completions: AtomicU64::new(0),
            request_cancellations: AtomicU64::new(0),
            remote_forwards: AtomicU64::new(0),
        })
    }

    /// Acquires a fresh epoch for every configured tenant before admission.
    pub async fn recover(
        &self,
        source: &dyn GatewayHaOwnershipSource,
        now_ms: u64,
    ) -> GatewayRegistryResult<()> {
        let _operation = self.operation.lock().await;
        self.lifecycle.begin_recovery(now_ms)?;
        *self.ownership.write() = CoordinatorOwnership::default();
        let acquired = match self.acquire_all(now_ms).await {
            Ok(acquired) => acquired,
            Err((error, acquired)) => {
                let _ = self.release_all(&acquired).await;
                self.fail(error);
                return Err(error);
            }
        };
        let snapshot = match source.snapshot(now_ms).and_then(|snapshot| {
            snapshot.validate(&self.config.tenants, now_ms)?;
            Ok(snapshot)
        }) {
            Ok(snapshot) => snapshot,
            Err(error) => {
                let _ = self.release_all(&acquired).await;
                self.fail(error);
                return Err(error);
            }
        };
        let ownership = match self.replay(acquired, &snapshot, now_ms).await {
            Ok(ownership) => ownership,
            Err((error, leases)) => {
                let _ = self.release_all(&leases).await;
                self.fail(error);
                return Err(error);
            }
        };
        *self.ownership.write() = ownership;
        increment(&self.acquisitions);
        self.lifecycle.mark_healthy(now_ms)
    }

    /// Renews every exact epoch; any uncertainty clears admission and leases.
    pub async fn renew(&self, now_ms: u64) -> GatewayRegistryResult<()> {
        let _operation = self.operation.lock().await;
        self.lifecycle.admit()?;
        let (current, workers) = {
            let ownership = self.ownership.read();
            (ownership.leases.clone(), ownership.workers.clone())
        };
        if current.len() != self.config.tenants.len() {
            self.fail(GatewayRegistryError::Unavailable);
            return Err(GatewayRegistryError::Unavailable);
        }
        let renewed = match self.renew_all(&current, now_ms).await {
            Ok(renewed) => renewed,
            Err(error) => {
                *self.ownership.write() = CoordinatorOwnership::default();
                self.fail(error);
                return Err(error);
            }
        };
        let renewed_workers = match self.renew_workers(&renewed, &workers, now_ms).await {
            Ok(workers) => workers,
            Err(error) => {
                *self.ownership.write() = CoordinatorOwnership::default();
                self.fail(error);
                return Err(error);
            }
        };
        let mut ownership = self.ownership.write();
        ownership.leases = renewed;
        ownership.workers = renewed_workers;
        increment(&self.renewals);
        Ok(())
    }

    /// Returns the current exact tenant lease only while admission is healthy.
    pub fn lease_for(&self, tenant_id: &TenantId) -> GatewayRegistryResult<GatewayInstanceLease> {
        self.lifecycle.admit()?;
        self.ownership
            .read()
            .leases
            .iter()
            .find(|lease| lease.tenant_id() == tenant_id)
            .cloned()
            .ok_or(GatewayRegistryError::InvalidContract)
    }

    /// Returns the shared admission lifecycle driven by this coordinator.
    pub fn lifecycle(&self) -> Arc<GatewayHaLifecycle> {
        Arc::clone(&self.lifecycle)
    }

    /// Runs renewal and bounded recovery attempts until cooperative shutdown.
    pub async fn run(
        self: Arc<Self>,
        source: Arc<dyn GatewayHaOwnershipSource>,
        mut shutdown: watch::Receiver<bool>,
    ) {
        while !*shutdown.borrow() {
            let now_ms = unix_now_ms();
            let mode = self.lifecycle.snapshot().mode;
            let _ = match mode {
                GatewayHaMode::Healthy => self.renew(now_ms).await,
                GatewayHaMode::Stopped | GatewayHaMode::Isolated => {
                    self.recover(source.as_ref(), now_ms).await
                }
                GatewayHaMode::Recovering => Err(GatewayRegistryError::Unavailable),
            };
            tokio::select! {
                _ = tokio::time::sleep(self.config.renewal_interval) => {}
                changed = shutdown.changed() => {
                    if changed.is_err() || *shutdown.borrow() {
                        break;
                    }
                }
            }
        }
        let _ = self.stop().await;
    }

    /// Stops admission first, then best-effort releases all exact leases.
    pub async fn stop(&self) -> GatewayRegistryResult<()> {
        let _operation = self.operation.lock().await;
        self.lifecycle.stop();
        let leases = std::mem::take(&mut self.ownership.write().leases);
        *self.ownership.write() = CoordinatorOwnership::default();
        self.release_all(&leases).await
    }

    /// Returns bounded, redacted operational telemetry.
    pub fn snapshot(&self) -> GatewayHaCoordinatorSnapshot {
        let ownership = self.ownership.read();
        GatewayHaCoordinatorSnapshot {
            lifecycle: self.lifecycle.snapshot(),
            configured_tenants: self.config.tenants.len(),
            active_leases: ownership.leases.len(),
            active_workers: ownership.workers.len(),
            active_sessions: ownership.sessions.len(),
            acquisitions: self.acquisitions.load(Ordering::Relaxed),
            renewals: self.renewals.load(Ordering::Relaxed),
            failures: self.failures.load(Ordering::Relaxed),
            request_claims: self.request_claims.load(Ordering::Relaxed),
            request_completions: self.request_completions.load(Ordering::Relaxed),
            request_cancellations: self.request_cancellations.load(Ordering::Relaxed),
            remote_forwards: self.remote_forwards.load(Ordering::Relaxed),
        }
    }

    async fn acquire(
        &self,
        binding: &GatewayHaTenantBinding,
        now_ms: u64,
    ) -> GatewayRegistryResult<GatewayInstanceLease> {
        self.provider
            .acquire_instance(
                &binding.tenant_id,
                &binding.cluster_id,
                &self.config.instance_id,
                &self.config.federation_url,
                self.lease_ttl_ms(),
                now_ms,
            )
            .await
    }

    async fn acquire_all(
        &self,
        now_ms: u64,
    ) -> Result<Vec<GatewayInstanceLease>, (GatewayRegistryError, Vec<GatewayInstanceLease>)> {
        let operations = stream::iter(self.config.tenants.clone())
            .map(|binding| async move { self.acquire(&binding, now_ms).await })
            .buffer_unordered(MAX_GATEWAY_REGISTRY_CONCURRENCY)
            .collect::<Vec<_>>();
        let results = match tokio::time::timeout(operation_round_timeout(), operations).await {
            Ok(results) => results,
            Err(_) => return Err((GatewayRegistryError::Unavailable, Vec::new())),
        };
        collect_acquired(results)
    }

    async fn renew_all(
        &self,
        leases: &[GatewayInstanceLease],
        now_ms: u64,
    ) -> GatewayRegistryResult<Vec<GatewayInstanceLease>> {
        let ttl_ms = self.lease_ttl_ms();
        let operations = stream::iter(leases.to_vec())
            .map(|lease| async move { self.provider.renew_instance(&lease, ttl_ms, now_ms).await })
            .buffer_unordered(MAX_GATEWAY_REGISTRY_CONCURRENCY)
            .collect::<Vec<_>>();
        let results = tokio::time::timeout(operation_round_timeout(), operations)
            .await
            .map_err(|_| GatewayRegistryError::Unavailable)?;
        results.into_iter().collect()
    }

    async fn replay(
        &self,
        leases: Vec<GatewayInstanceLease>,
        snapshot: &GatewayHaOwnershipSnapshot,
        now_ms: u64,
    ) -> Result<CoordinatorOwnership, (GatewayRegistryError, Vec<GatewayInstanceLease>)> {
        let mut operations = Vec::with_capacity(
            snapshot
                .workers
                .len()
                .saturating_add(snapshot.sessions.len()),
        );
        for worker in &snapshot.workers {
            let Some(lease) = find_lease(&leases, &worker.tenant_id).cloned() else {
                return Err((GatewayRegistryError::InvalidContract, leases));
            };
            let registration = worker.registration.clone();
            let operation: GatewayRegistryFuture<'_, RecoveredOwnership> = Box::pin(async move {
                self.provider
                    .register_worker(&lease, registration, self.lease_ttl_ms(), now_ms)
                    .await
                    .map(RecoveredOwnership::Worker)
            });
            operations.push(operation);
        }
        for session in &snapshot.sessions {
            let Some(lease) = find_lease(&leases, &session.tenant_id).cloned() else {
                return Err((GatewayRegistryError::InvalidContract, leases));
            };
            let record = match GatewaySessionRecord::new(
                lease.clone(),
                session.session_id.clone(),
                session.expires_at_ms,
            ) {
                Ok(record) => record,
                Err(error) => return Err((error, leases)),
            };
            let operation: GatewayRegistryFuture<'_, RecoveredOwnership> = Box::pin(async move {
                self.provider
                    .register_session(&lease, record, now_ms)
                    .await
                    .map(RecoveredOwnership::Session)
            });
            operations.push(operation);
        }
        let results = replay_operations(operations).await;
        collect_replayed(results, leases)
    }

    async fn renew_workers(
        &self,
        leases: &[GatewayInstanceLease],
        workers: &[GatewayWorkerRecord],
        now_ms: u64,
    ) -> GatewayRegistryResult<Vec<GatewayWorkerRecord>> {
        let ttl_ms = self.lease_ttl_ms();
        let operations = stream::iter(workers.to_vec()).map(|worker| {
            let lease = find_lease(leases, worker.owner.tenant_id()).cloned();
            async move {
                let lease = lease.ok_or(GatewayRegistryError::InvalidContract)?;
                self.provider
                    .renew_worker(&lease, &worker, ttl_ms, now_ms)
                    .await
            }
        });
        let results = operations
            .buffer_unordered(MAX_GATEWAY_REGISTRY_CONCURRENCY)
            .collect::<Vec<_>>();
        tokio::time::timeout(operation_round_timeout(), results)
            .await
            .map_err(|_| GatewayRegistryError::Unavailable)?
            .into_iter()
            .collect()
    }

    async fn release_all(&self, leases: &[GatewayInstanceLease]) -> GatewayRegistryResult<()> {
        let operations = stream::iter(leases.to_vec())
            .map(|lease| async move { self.provider.release_instance(&lease).await })
            .buffer_unordered(MAX_GATEWAY_REGISTRY_CONCURRENCY)
            .collect::<Vec<_>>();
        let results = tokio::time::timeout(operation_round_timeout(), operations)
            .await
            .map_err(|_| GatewayRegistryError::Unavailable)?;
        let mut first_error = None;
        for result in results {
            if let Err(error) = result {
                first_error.get_or_insert(error);
            }
        }
        first_error.map_or(Ok(()), Err)
    }

    pub(super) fn fail(&self, error: GatewayRegistryError) {
        increment(&self.failures);
        if matches!(
            error,
            GatewayRegistryError::StaleOwner | GatewayRegistryError::Conflict
        ) {
            self.lifecycle.record_fencing_rejection();
        }
        let _ = self.lifecycle.isolate();
    }

    pub(super) fn lease_ttl_ms(&self) -> u64 {
        u64::try_from(self.config.lease_ttl.as_millis()).unwrap_or(u64::MAX)
    }
}

#[cfg(test)]
#[path = "coordinator_tests.rs"]
pub(crate) mod tests;