use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use async_trait::async_trait;
use dataferry_core::{Lease, Partition, PartitionId, Result, WriteSummary};
use dataferry_registry::StateStore;
use dataferry_registry::state::{
NewRun, PartitionCounts, PartitionRows, RunId, RunRecord, RunTotals,
};
use futures::StreamExt;
const RELEASE_TIMEOUT: Duration = Duration::from_secs(5);
const RELEASE_CONCURRENCY: usize = 8;
pub(crate) struct Tracked {
inner: Arc<dyn StateStore>,
held: Mutex<HashMap<PartitionId, (RunId, Lease, Instant)>>,
}
impl Tracked {
pub fn new(inner: Arc<dyn StateStore>) -> Self {
Self {
inner,
held: Mutex::default(),
}
}
fn forget(&self, lease: &Lease) {
let mut held = self.held.lock().expect("leases lock");
if held
.get(&lease.partition)
.is_some_and(|(_, l, _)| l.epoch == lease.epoch)
{
held.remove(&lease.partition);
}
}
pub async fn release(&self) -> usize {
let now = Instant::now();
let held: Vec<(RunId, Lease)> = self
.held
.lock()
.expect("leases lock")
.drain()
.filter(|(_, (_, _, expires))| *expires > now)
.map(|(_, (run, lease, _))| (run, lease))
.collect();
let releases = futures::stream::iter(held.iter().map(|(run, lease)| async move {
match self.inner.is_done(run, &lease.partition).await {
Ok(true) => false,
Ok(false) | Err(_) => self.inner.renew(run, lease, Duration::ZERO).await.is_ok(),
}
}))
.buffer_unordered(RELEASE_CONCURRENCY)
.collect::<Vec<bool>>();
tokio::time::timeout(RELEASE_TIMEOUT, releases)
.await
.map_or(0, |released| released.into_iter().filter(|ok| *ok).count())
}
}
#[async_trait]
impl StateStore for Tracked {
async fn create_run(&self, run: NewRun) -> Result<RunId> {
self.inner.create_run(run).await
}
async fn load_run(&self, run: &RunId) -> Result<Option<RunRecord>> {
self.inner.load_run(run).await
}
async fn store_partitions(
&self,
run: &RunId,
flow: &str,
partitions: &[Partition],
) -> Result<()> {
self.inner.store_partitions(run, flow, partitions).await
}
async fn load_partitions(&self, run: &RunId, flow: &str) -> Result<Vec<Partition>> {
self.inner.load_partitions(run, flow).await
}
async fn lease_next(&self, run: &RunId, flow: &str, ttl: Duration) -> Result<Option<Lease>> {
let asked = Instant::now();
let lease = self.inner.lease_next(run, flow, ttl).await?;
if let Some(lease) = &lease {
let mut held = self.held.lock().expect("leases lock");
let now = Instant::now();
held.retain(|_, (_, _, expires)| *expires > now);
held.insert(
lease.partition.clone(),
(run.clone(), lease.clone(), asked + ttl),
);
}
Ok(lease)
}
async fn renew(&self, run: &RunId, lease: &Lease, ttl: Duration) -> Result<()> {
let asked = Instant::now();
self.inner.renew(run, lease, ttl).await?;
if let Some((_, l, expires)) = self
.held
.lock()
.expect("leases lock")
.get_mut(&lease.partition)
&& l.epoch == lease.epoch
{
*expires = asked + ttl;
}
Ok(())
}
async fn mark_done(&self, run: &RunId, lease: &Lease, summary: &WriteSummary) -> Result<()> {
let done = self.inner.mark_done(run, lease, summary).await;
if done.is_ok() {
self.forget(lease);
}
done
}
async fn mark_failed(&self, run: &RunId, lease: &Lease, error: &str) -> Result<()> {
let failed = self.inner.mark_failed(run, lease, error).await;
if failed.is_ok() {
self.forget(lease);
}
failed
}
async fn record_rows(&self, run: &RunId, lease: &Lease, rows: &PartitionRows) -> Result<()> {
self.inner.record_rows(run, lease, rows).await
}
async fn is_done(&self, run: &RunId, partition: &PartitionId) -> Result<bool> {
self.inner.is_done(run, partition).await
}
async fn counts(&self, run: &RunId) -> Result<PartitionCounts> {
self.inner.counts(run).await
}
async fn flow_counts(&self, run: &RunId, flow: &str) -> Result<PartitionCounts> {
self.inner.flow_counts(run, flow).await
}
async fn reset_failed(&self, run: &RunId) -> Result<u64> {
self.inner.reset_failed(run).await
}
async fn totals(&self, run: &RunId) -> Result<RunTotals> {
self.inner.totals(run).await
}
}