Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions components/spider-storage/src/cache/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ pub enum InternalError {
#[error("invalid config: {0}")]
TaskInstancePoolInvalidConfig(&'static str),

#[error("invalid config: {0}")]
JobCacheGcInvalidConfig(&'static str),

#[error("ready queue channel is closed")]
ReadyQueueChannelClosed,

Expand Down
2 changes: 2 additions & 0 deletions components/spider-storage/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
pub mod error;
pub mod job_cache;
pub mod job_cache_gc;
pub mod runtime;
pub mod service;

pub use error::StorageServerError;
pub use job_cache::JobCache;
pub use job_cache_gc::{JobCacheGcConfig, JobCacheGcHandle, create_job_cache_gc};
pub use runtime::{Runtime, create_runtime};
pub use service::ServiceState;

Expand Down
82 changes: 70 additions & 12 deletions components/spider-storage/src/state/job_cache.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::collections::{HashMap, hash_map::Entry};
use std::{
collections::{HashMap, hash_map::Entry},
sync::Arc,
};

use spider_core::types::id::JobId;
use tokio::sync::RwLock;
Expand All @@ -11,6 +14,14 @@ use crate::{
task_instance_pool::TaskInstancePoolConnector,
};

type JobMap<ReadyQueueSenderType, DbConnectorType, TaskInstancePoolConnectorType> = HashMap<
JobId,
SharedJobControlBlock<ReadyQueueSenderType, DbConnectorType, TaskInstancePoolConnectorType>,
>;

type SharedJobMap<ReadyQueueSenderType, DbConnectorType, TaskInstancePoolConnectorType> =
Arc<RwLock<JobMap<ReadyQueueSenderType, DbConnectorType, TaskInstancePoolConnectorType>>>;

/// An in-memory cache for job control blocks.
///
/// This type provides concurrent access to job control blocks via a [`tokio::sync::RwLock`] over a
Expand All @@ -26,16 +37,7 @@ pub struct JobCache<
DbConnectorType: InternalJobOrchestration,
TaskInstancePoolConnectorType: TaskInstancePoolConnector,
> {
jobs: RwLock<
HashMap<
JobId,
SharedJobControlBlock<
ReadyQueueSenderType,
DbConnectorType,
TaskInstancePoolConnectorType,
>,
>,
>,
jobs: SharedJobMap<ReadyQueueSenderType, DbConnectorType, TaskInstancePoolConnectorType>,
}

impl<
Expand All @@ -48,7 +50,7 @@ impl<
#[must_use]
pub fn new() -> Self {
Self {
jobs: RwLock::new(HashMap::new()),
jobs: Arc::new(RwLock::new(HashMap::new())),
}
}

Expand Down Expand Up @@ -106,6 +108,19 @@ impl<
self.jobs.write().await.remove(&job_id)
}

/// Removes multiple job control blocks from the cache.
///
/// # Returns
///
/// The number of job control blocks that existed and were removed.
pub async fn remove_batch(&self, job_ids: &[JobId]) -> usize {
let mut jobs = self.jobs.write().await;
job_ids
.iter()
.filter(|job_id| jobs.remove(job_id).is_some())
.count()
}

/// Resends all ready tasks for every job in the cache to the ready queue.
///
/// # Errors
Expand All @@ -132,6 +147,19 @@ impl<
}
}

impl<
ReadyQueueSenderType: ReadyQueueSender,
DbConnectorType: InternalJobOrchestration,
TaskInstancePoolConnectorType: TaskInstancePoolConnector,
> Clone for JobCache<ReadyQueueSenderType, DbConnectorType, TaskInstancePoolConnectorType>
{
fn clone(&self) -> Self {
Self {
jobs: Arc::clone(&self.jobs),
}
}
}

#[cfg(test)]
mod tests {
use std::sync::Arc;
Expand Down Expand Up @@ -225,6 +253,36 @@ mod tests {
Ok(())
}

#[tokio::test]
async fn job_cache_remove_batch_removes_existing_jobs_once() -> anyhow::Result<()> {
let cache: JobCache<MockReadyQueueSender, MockDbConnector, MockTaskInstancePoolConnector> =
JobCache::new();
let first_job_id = JobId::random();
let second_job_id = JobId::random();
let missing_job_id = JobId::random();

cache.insert(create_test_jcb(first_job_id).await).await?;
cache.insert(create_test_jcb(second_job_id).await).await?;

let num_removed_jobs = cache
.remove_batch(&[first_job_id, missing_job_id, second_job_id])
.await;

assert_eq!(
num_removed_jobs, 2,
"remove_batch should count only existing jobs"
);
assert!(
cache.get(first_job_id).await.is_none(),
"first job should be removed"
);
assert!(
cache.get(second_job_id).await.is_none(),
"second job should be removed"
);
Ok(())
}

#[tokio::test]
async fn job_cache_get_returns_none_for_nonexistent_job() -> anyhow::Result<()> {
let cache: JobCache<MockReadyQueueSender, MockDbConnector, MockTaskInstancePoolConnector> =
Expand Down
Loading
Loading