Skip to content

Commit e75624b

Browse files
Merge pull request #386 from code0-tech/hotfix/scanned-key-resolvement
fix: scanned for key for testexecution
2 parents feb5b89 + e8d66c8 commit e75624b

3 files changed

Lines changed: 73 additions & 12 deletions

File tree

src/flow/mod.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,27 @@ pub fn get_flow_identifier(flow: &ValidationFlow) -> String {
88
flow.r#type, flow.project_slug, flow.project_id, flow.flow_id
99
)
1010
}
11+
12+
pub fn key_has_flow_id(key: &str, flow_id: i64) -> bool {
13+
key.rsplit_once('.')
14+
.and_then(|(_, id)| id.parse::<i64>().ok())
15+
== Some(flow_id)
16+
}
17+
18+
#[cfg(test)]
19+
mod tests {
20+
use super::key_has_flow_id;
21+
22+
#[test]
23+
fn matches_flow_id_in_final_key_segment() {
24+
assert!(key_has_flow_id("CRON.test.1.1", 1));
25+
assert!(key_has_flow_id("REST.project.42.123", 123));
26+
}
27+
28+
#[test]
29+
fn rejects_partial_or_non_final_flow_id_matches() {
30+
assert!(!key_has_flow_id("CRON.test.1.11", 1));
31+
assert!(!key_has_flow_id("CRON.test.1.1.extra", 1));
32+
assert!(!key_has_flow_id("CRON.test.1.invalid", 1));
33+
}
34+
}

src/sagittarius/flow_service_client_impl.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::{
2-
authorization::authorization::get_authorization_metadata, flow::get_flow_identifier,
2+
authorization::authorization::get_authorization_metadata,
3+
flow::{get_flow_identifier, key_has_flow_id},
34
telemetry::metrics,
45
};
56
use futures::{StreamExt, TryStreamExt};
@@ -28,12 +29,6 @@ fn module_config_stats(configs: &tucana::shared::ModuleConfigurations) -> (usize
2829
(project_count, config_count)
2930
}
3031

31-
fn key_has_flow_id(key: &str, flow_id: i64) -> bool {
32-
key.rsplit_once('.')
33-
.and_then(|(_, id)| id.parse::<i64>().ok())
34-
== Some(flow_id)
35-
}
36-
3732
#[derive(Clone)]
3833
pub struct SagittariusFlowClient {
3934
store: Arc<async_nats::jetstream::kv::Store>,

src/sagittarius/test_execution_client_impl.rs

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
In some conditions Sagittarius can't connect to Aquila
55
Thus Aquila sends a `Logon` request to connect to Sagittarius establishing the connection
66
*/
7-
use futures::StreamExt;
7+
use futures::{StreamExt, TryStreamExt};
88
use prost::Message;
99
use std::{
1010
collections::HashMap,
@@ -20,7 +20,7 @@ use tucana::sagittarius::execution_service_client::ExecutionServiceClient;
2020
use tucana::sagittarius::{ExecutionLogonRequest, Logon};
2121
use tucana::shared::{ExecutionFlow, ExecutionResult, ValidationFlow};
2222

23-
use crate::authorization::authorization::get_authorization_metadata;
23+
use crate::{authorization::authorization::get_authorization_metadata, flow::key_has_flow_id};
2424

2525
const EXECUTION_FLOW_ID_TTL: Duration = Duration::from_secs(30 * 60);
2626
const MAX_EXECUTION_FLOW_IDS: usize = 10_000;
@@ -276,7 +276,44 @@ impl SagittariusTestExecutionServiceClient {
276276
}
277277

278278
async fn load_validation_flow(&self, flow_id: i64) -> Option<ValidationFlow> {
279-
match self.store.get(format!("*.*.*.{}", flow_id)).await {
279+
let mut keys = match self.store.keys().await {
280+
Ok(keys) => keys,
281+
Err(err) => {
282+
log::error!(
283+
"Failed to list validation flow keys flow_id={} error={:?}",
284+
flow_id,
285+
err
286+
);
287+
return None;
288+
}
289+
};
290+
291+
let key = loop {
292+
match keys.try_next().await {
293+
Ok(Some(key)) if key_has_flow_id(&key, flow_id) => break key,
294+
Ok(Some(_)) => {}
295+
Ok(None) => {
296+
log::error!("Validation flow was not found flow_id={}", flow_id);
297+
return None;
298+
}
299+
Err(err) => {
300+
log::error!(
301+
"Failed while scanning validation flow keys flow_id={} error={:?}",
302+
flow_id,
303+
err
304+
);
305+
return None;
306+
}
307+
}
308+
};
309+
310+
log::debug!(
311+
"Resolved validation flow key flow_id={} key={}",
312+
flow_id,
313+
key
314+
);
315+
316+
match self.store.get(&key).await {
280317
Ok(Some(bytes)) => match ValidationFlow::decode(bytes) {
281318
Ok(flow) => {
282319
log::debug!(
@@ -298,13 +335,18 @@ impl SagittariusTestExecutionServiceClient {
298335
}
299336
},
300337
Ok(None) => {
301-
log::error!("Validation flow was not found flow_id={}", flow_id);
338+
log::error!(
339+
"Validation flow disappeared after key resolution flow_id={} key={}",
340+
flow_id,
341+
key
342+
);
302343
None
303344
}
304345
Err(err) => {
305346
log::error!(
306-
"Failed to fetch validation flow flow_id={} error={:?}",
347+
"Failed to fetch validation flow flow_id={} key={} error={:?}",
307348
flow_id,
349+
key,
308350
err
309351
);
310352
None

0 commit comments

Comments
 (0)