Schedule cache task, when threadpool's current running task exceed certain ratio, start random drop disk cache task
| 207 | // Schedule cache task, when threadpool's current running task exceed certain ratio, start random |
| 208 | // drop disk cache task |
| 209 | bool IDiskCache::scheduleCacheTask(const std::function<void()> & task) |
| 210 | { |
| 211 | if (shutdown_called) |
| 212 | return false; |
| 213 | |
| 214 | auto & thread_pool = IDiskCache::getThreadPool(); |
| 215 | size_t active_task_size = thread_pool.active(); |
| 216 | size_t max_queue_size = thread_pool.getMaxQueueSize(); |
| 217 | // (Running + Pending tasks) / (Max Running + Max Pending tasks) |
| 218 | size_t current_ratio = max_queue_size == 0 ? 0 : ((active_task_size * 100) / max_queue_size); |
| 219 | |
| 220 | if (current_ratio <= settings.random_drop_threshold || settings.random_drop_threshold >= 100) |
| 221 | { |
| 222 | return thread_pool.trySchedule(task); |
| 223 | } |
| 224 | else |
| 225 | { |
| 226 | // Drop disk cache task base on queue's full ratio |
| 227 | // (current task queue full ratio/ (100 - random_drop_threshold)) * 100 |
| 228 | // The drop possibility when current_ratio == random_drop_threshold is 0% |
| 229 | // The drop possibility when current_ratio == 100 is 100% |
| 230 | size_t drop_possibility = (100 * (current_ratio - settings.random_drop_threshold)) / (100 - settings.random_drop_threshold); |
| 231 | std::random_device rd; |
| 232 | std::mt19937 random_generator(rd()); |
| 233 | std::uniform_int_distribution<size_t> dist(1, 100); |
| 234 | if (dist(random_generator) <= drop_possibility) |
| 235 | { |
| 236 | LOG_DEBUG(log, "Drop disk cache since queue is almost full, Queue length: {}, Max: {}, curren_ratio: {} ", active_task_size, max_queue_size, current_ratio); |
| 237 | ProfileEvents::increment(ProfileEvents::DiskCacheTaskDropCount, 1, Metrics::MetricType::Meter); |
| 238 | return false; |
| 239 | } |
| 240 | else |
| 241 | { |
| 242 | return thread_pool.trySchedule(task); |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | } |
nothing calls this directly
no test coverage detected