|
| 1 | +//! Resource management |
| 2 | +
|
| 3 | +use crate::error::ActiveStorageError; |
| 4 | + |
| 5 | +use tokio::sync::{Semaphore, SemaphorePermit}; |
| 6 | + |
| 7 | +/// [crate::resource_manager::ResourceManager] provides a simple way to allocate various resources |
| 8 | +/// to tasks. Resource management is performed using a Tokio Semaphore for each type of resource. |
| 9 | +pub struct ResourceManager { |
| 10 | + /// Optional semaphore for S3 connections. |
| 11 | + s3_connections: Option<Semaphore>, |
| 12 | + |
| 13 | + /// Optional semaphore for memory (bytes). |
| 14 | + memory: Option<Semaphore>, |
| 15 | + |
| 16 | + /// Optional total memory pool in bytes. |
| 17 | + total_memory: Option<usize>, |
| 18 | + |
| 19 | + /// Optional semaphore for tasks. |
| 20 | + tasks: Option<Semaphore>, |
| 21 | +} |
| 22 | + |
| 23 | +impl ResourceManager { |
| 24 | + /// Returns a new ResourceManager object. |
| 25 | + pub fn new( |
| 26 | + s3_connection_limit: Option<usize>, |
| 27 | + memory_limit: Option<usize>, |
| 28 | + task_limit: Option<usize>, |
| 29 | + ) -> Self { |
| 30 | + Self { |
| 31 | + s3_connections: s3_connection_limit.map(Semaphore::new), |
| 32 | + memory: memory_limit.map(Semaphore::new), |
| 33 | + total_memory: memory_limit, |
| 34 | + tasks: task_limit.map(Semaphore::new), |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + /// Acquire an S3 connection resource. |
| 39 | + pub async fn s3_connection(&self) -> Result<Option<SemaphorePermit>, ActiveStorageError> { |
| 40 | + optional_acquire(&self.s3_connections, 1).await |
| 41 | + } |
| 42 | + |
| 43 | + /// Acquire memory resource. |
| 44 | + pub async fn memory( |
| 45 | + &self, |
| 46 | + bytes: usize, |
| 47 | + ) -> Result<Option<SemaphorePermit>, ActiveStorageError> { |
| 48 | + if let Some(total_memory) = self.total_memory { |
| 49 | + if bytes > total_memory { |
| 50 | + return Err(ActiveStorageError::InsufficientMemory { |
| 51 | + requested: bytes, |
| 52 | + total: total_memory, |
| 53 | + }); |
| 54 | + }; |
| 55 | + }; |
| 56 | + optional_acquire(&self.memory, bytes).await |
| 57 | + } |
| 58 | + |
| 59 | + /// Acquire a task resource. |
| 60 | + pub async fn task(&self) -> Result<Option<SemaphorePermit>, ActiveStorageError> { |
| 61 | + optional_acquire(&self.tasks, 1).await |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +/// Acquire permits on an optional Semaphore, if present. |
| 66 | +async fn optional_acquire( |
| 67 | + sem: &Option<Semaphore>, |
| 68 | + n: usize, |
| 69 | +) -> Result<Option<SemaphorePermit>, ActiveStorageError> { |
| 70 | + let n = n.try_into()?; |
| 71 | + if let Some(sem) = sem { |
| 72 | + sem.acquire_many(n) |
| 73 | + .await |
| 74 | + .map(Some) |
| 75 | + .map_err(|err| err.into()) |
| 76 | + } else { |
| 77 | + Ok(None) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +#[cfg(test)] |
| 82 | +mod tests { |
| 83 | + use super::*; |
| 84 | + |
| 85 | + use tokio::sync::TryAcquireError; |
| 86 | + |
| 87 | + #[tokio::test] |
| 88 | + async fn no_resource_management() { |
| 89 | + let rm = ResourceManager::new(None, None, None); |
| 90 | + assert!(rm.s3_connections.is_none()); |
| 91 | + assert!(rm.memory.is_none()); |
| 92 | + assert!(rm.tasks.is_none()); |
| 93 | + let _c = rm.s3_connection().await.unwrap(); |
| 94 | + let _m = rm.memory(1).await.unwrap(); |
| 95 | + let _t = rm.task().await.unwrap(); |
| 96 | + assert!(_c.is_none()); |
| 97 | + assert!(_m.is_none()); |
| 98 | + assert!(_t.is_none()); |
| 99 | + } |
| 100 | + |
| 101 | + #[tokio::test] |
| 102 | + async fn full_resource_management() { |
| 103 | + let rm = ResourceManager::new(Some(1), Some(1), Some(1)); |
| 104 | + assert!(rm.s3_connections.is_some()); |
| 105 | + assert!(rm.memory.is_some()); |
| 106 | + assert!(rm.tasks.is_some()); |
| 107 | + let _c = rm.s3_connection().await.unwrap(); |
| 108 | + let _m = rm.memory(1).await.unwrap(); |
| 109 | + let _t = rm.task().await.unwrap(); |
| 110 | + assert!(_c.is_some()); |
| 111 | + assert!(_m.is_some()); |
| 112 | + assert!(_t.is_some()); |
| 113 | + // Check that there are no more resources (without blocking). |
| 114 | + assert_eq!( |
| 115 | + rm.s3_connections.as_ref().unwrap().try_acquire().err(), |
| 116 | + Some(TryAcquireError::NoPermits) |
| 117 | + ); |
| 118 | + assert_eq!( |
| 119 | + rm.memory.as_ref().unwrap().try_acquire().err(), |
| 120 | + Some(TryAcquireError::NoPermits) |
| 121 | + ); |
| 122 | + assert_eq!( |
| 123 | + rm.tasks.as_ref().unwrap().try_acquire().err(), |
| 124 | + Some(TryAcquireError::NoPermits) |
| 125 | + ); |
| 126 | + } |
| 127 | +} |
0 commit comments