|
2 | 2 | //! This is needed to implement Eager Workflow Start, a latency optimization in which the client, |
3 | 3 | //! after reserving a slot, directly forwards a WFT to a local worker. |
4 | 4 |
|
| 5 | +use anyhow::bail; |
5 | 6 | use parking_lot::RwLock; |
6 | | -use std::collections::{HashMap, hash_map::Entry::{ Occupied, Vacant}}; |
| 7 | +use std::collections::{ |
| 8 | + HashMap, |
| 9 | + hash_map::Entry::{Occupied, Vacant}, |
| 10 | +}; |
7 | 11 | use std::sync::Arc; |
8 | | -use anyhow::bail; |
9 | 12 | use temporal_sdk_core_protos::temporal::api::worker::v1::WorkerHeartbeat; |
10 | 13 | use temporal_sdk_core_protos::temporal::api::workflowservice::v1::PollWorkflowTaskQueueResponse; |
11 | 14 | use uuid::Uuid; |
@@ -70,15 +73,21 @@ impl ClientWorkerSetImpl { |
70 | 73 | None |
71 | 74 | } |
72 | 75 |
|
73 | | - fn register(&mut self, worker: Arc<dyn ClientWorker + Send + Sync>) -> Result<(), anyhow::Error> { |
| 76 | + fn register( |
| 77 | + &mut self, |
| 78 | + worker: Arc<dyn ClientWorker + Send + Sync>, |
| 79 | + ) -> Result<(), anyhow::Error> { |
74 | 80 | let slot_key = SlotKey::new( |
75 | 81 | worker.namespace().to_string(), |
76 | 82 | worker.task_queue().to_string(), |
77 | 83 | ); |
78 | 84 | if let Vacant(p) = self.slot_providers.entry(slot_key.clone()) { |
79 | 85 | p.insert(worker.worker_instance_key()); |
80 | 86 | } else { |
81 | | - bail!("Registration of multiple workers on the same namespace and task queue for the same client not allowed: {slot_key:?}, worker_instance_key: {:?}.", worker.worker_instance_key()); |
| 87 | + bail!( |
| 88 | + "Registration of multiple workers on the same namespace and task queue for the same client not allowed: {slot_key:?}, worker_instance_key: {:?}.", |
| 89 | + worker.worker_instance_key() |
| 90 | + ); |
82 | 91 | } |
83 | 92 |
|
84 | 93 | if worker.heartbeat_enabled() |
@@ -107,12 +116,15 @@ impl ClientWorkerSetImpl { |
107 | 116 | &mut self, |
108 | 117 | worker_instance_key: Uuid, |
109 | 118 | ) -> Result<Arc<dyn ClientWorker + Send + Sync>, anyhow::Error> { |
110 | | - let worker = self.all_workers.remove(&worker_instance_key).ok_or_else(|| { |
111 | | - anyhow::anyhow!( |
112 | | - "Worker with worker_instance_key {} not found", |
113 | | - worker_instance_key |
114 | | - ) |
115 | | - })?; |
| 119 | + let worker = self |
| 120 | + .all_workers |
| 121 | + .remove(&worker_instance_key) |
| 122 | + .ok_or_else(|| { |
| 123 | + anyhow::anyhow!( |
| 124 | + "Worker with worker_instance_key {} not found", |
| 125 | + worker_instance_key |
| 126 | + ) |
| 127 | + })?; |
116 | 128 |
|
117 | 129 | let slot_key = SlotKey::new( |
118 | 130 | worker.namespace().to_string(), |
@@ -211,7 +223,10 @@ impl ClientWorkerSet { |
211 | 223 | } |
212 | 224 |
|
213 | 225 | /// Register a local worker that can provide WFT processing slots and potentially worker heartbeating. |
214 | | - pub fn register_worker(&self, worker: Arc<dyn ClientWorker + Send + Sync>) -> Result<(), anyhow::Error> { |
| 226 | + pub fn register_worker( |
| 227 | + &self, |
| 228 | + worker: Arc<dyn ClientWorker + Send + Sync>, |
| 229 | + ) -> Result<(), anyhow::Error> { |
215 | 230 | self.worker_manager.write().register(worker) |
216 | 231 | } |
217 | 232 |
|
@@ -343,7 +358,9 @@ mod tests { |
343 | 358 | worker_keys.push(worker_instance_key); |
344 | 359 | } else { |
345 | 360 | // Should get error for duplicate namespace+task_queue combinations |
346 | | - assert!(result.unwrap_err().to_string().contains("Registration of multiple workers on the same namespace and task queue")); |
| 361 | + assert!(result.unwrap_err().to_string().contains( |
| 362 | + "Registration of multiple workers on the same namespace and task queue" |
| 363 | + )); |
347 | 364 | } |
348 | 365 | } |
349 | 366 |
|
@@ -479,15 +496,20 @@ mod tests { |
479 | 496 | // second worker register should fail due to duplicate namespace+task_queue |
480 | 497 | let result = manager.register_worker(Arc::new(worker2)); |
481 | 498 | assert!(result.is_err()); |
482 | | - assert!(result.unwrap_err().to_string().contains("Registration of multiple workers on the same namespace and task queue")); |
| 499 | + assert!( |
| 500 | + result |
| 501 | + .unwrap_err() |
| 502 | + .to_string() |
| 503 | + .contains("Registration of multiple workers on the same namespace and task queue") |
| 504 | + ); |
483 | 505 |
|
484 | 506 | assert_eq!((1, 1), manager.num_providers()); |
485 | 507 | assert_eq!(manager.num_heartbeat_workers(), 1); |
486 | 508 |
|
487 | 509 | let impl_ref = manager.worker_manager.read(); |
488 | 510 | assert_eq!(impl_ref.shared_worker.len(), 1); |
489 | 511 | assert!(impl_ref.shared_worker.contains_key("test_namespace")); |
490 | | -} |
| 512 | + } |
491 | 513 |
|
492 | 514 | #[test] |
493 | 515 | fn multiple_workers_same_namespace_share_heartbeat_manager() { |
|
0 commit comments