1use ff_core::keys::{ExecKeyContext, IndexKeys};
9use ff_core::partition::{
10 Partition, PartitionConfig, PartitionFamily, execution_partition,
11};
12use ff_core::types::ExecutionId;
13
14pub struct PartitionRouter {
21 config: PartitionConfig,
22}
23
24impl PartitionRouter {
25 pub fn new(config: PartitionConfig) -> Self {
26 Self { config }
27 }
28
29 pub fn partition_for(&self, eid: &ExecutionId) -> Partition {
31 execution_partition(eid, &self.config)
32 }
33
34 pub fn exec_keys(&self, eid: &ExecutionId) -> ExecKeyContext {
36 let partition = self.partition_for(eid);
37 ExecKeyContext::new(&partition, eid)
38 }
39
40 pub fn index_keys(&self, partition_index: u16) -> IndexKeys {
42 let partition = Partition {
43 family: PartitionFamily::Execution,
44 index: partition_index,
45 };
46 IndexKeys::new(&partition)
47 }
48
49 pub fn config(&self) -> &PartitionConfig {
51 &self.config
52 }
53
54 pub fn num_flow_partitions(&self) -> u16 {
60 self.config.num_flow_partitions
61 }
62}
63
64pub async fn dispatch_dependency_resolution(
75 client: &ferriskey::Client,
76 router: &PartitionRouter,
77 eid: &ExecutionId,
78 flow_id: Option<&str>,
79) {
80 dispatch_dependency_resolution_inner(client, router, eid, flow_id, 0).await;
81}
82
83const MAX_CASCADE_DEPTH: u32 = 50;
85
86async fn dispatch_dependency_resolution_inner(
87 client: &ferriskey::Client,
88 router: &PartitionRouter,
89 eid: &ExecutionId,
90 flow_id: Option<&str>,
91 cascade_depth: u32,
92) {
93 if cascade_depth > MAX_CASCADE_DEPTH {
94 tracing::warn!(
95 execution_id = %eid,
96 cascade_depth,
97 "dispatch_dep: cascade depth limit reached, reconciler will catch remaining"
98 );
99 return;
100 }
101
102 let flow_id_str = match flow_id {
103 Some(fid) if !fid.is_empty() => fid,
104 _ => return, };
106
107 let exec_ctx = router.exec_keys(eid);
109 let core_key = exec_ctx.core();
110 let outcome: Option<String> = match client
111 .cmd("HGET")
112 .arg(&core_key)
113 .arg("terminal_outcome")
114 .execute()
115 .await
116 {
117 Ok(v) => v,
118 Err(e) => {
119 tracing::warn!(
120 execution_id = %eid,
121 error = %e,
122 "dispatch_dep: failed to read terminal_outcome"
123 );
124 return;
125 }
126 };
127
128 let outcome_str = outcome.unwrap_or_default();
129 let upstream_outcome = outcome_str.as_str();
133
134 let fid = match ff_core::types::FlowId::parse(flow_id_str) {
137 Ok(id) => id,
138 Err(e) => {
139 tracing::warn!(
140 flow_id = flow_id_str,
141 error = %e,
142 "dispatch_dep: invalid flow_id"
143 );
144 return;
145 }
146 };
147
148 let flow_partition = ff_core::partition::flow_partition(&fid, router.config());
149 let flow_ctx = ff_core::keys::FlowKeyContext::new(&flow_partition, &fid);
150
151 let out_key = flow_ctx.outgoing(eid);
153 let edge_ids: Vec<String> = match client
154 .cmd("SMEMBERS")
155 .arg(&out_key)
156 .execute()
157 .await
158 {
159 Ok(ids) => ids,
160 Err(e) => {
161 tracing::warn!(
162 execution_id = %eid,
163 flow_id = flow_id_str,
164 error = %e,
165 "dispatch_dep: SMEMBERS outgoing failed"
166 );
167 return;
168 }
169 };
170
171 if edge_ids.is_empty() {
172 return;
173 }
174
175 let now_ms = ff_core::types::TimestampMs::now().0.to_string();
176 let mut resolved: u32 = 0;
177 let mut skipped_children: Vec<(ExecutionId, String)> = Vec::new();
178
179 for edge_id in &edge_ids {
180 let parsed_edge_id = match ff_core::types::EdgeId::parse(edge_id) {
186 Ok(id) => id,
187 Err(e) => {
188 tracing::warn!(
189 edge_id = edge_id.as_str(),
190 flow_id = flow_id_str,
191 error = %e,
192 "dispatch_dep: invalid edge_id in outgoing adjacency set, skipping"
193 );
194 continue;
195 }
196 };
197
198 let edge_key = flow_ctx.edge(&parsed_edge_id);
200 let downstream_eid_str: Option<String> = match client
201 .cmd("HGET")
202 .arg(&edge_key)
203 .arg("downstream_execution_id")
204 .execute()
205 .await
206 {
207 Ok(v) => v,
208 Err(_) => continue,
209 };
210
211 let downstream_eid_str = match downstream_eid_str {
212 Some(s) if !s.is_empty() => s,
213 _ => continue,
214 };
215
216 let downstream_eid = match ExecutionId::parse(&downstream_eid_str) {
217 Ok(id) => id,
218 Err(_) => continue,
219 };
220
221 let child_partition = router.partition_for(&downstream_eid);
223 let child_ctx = ExecKeyContext::new(&child_partition, &downstream_eid);
224 let child_idx = IndexKeys::new(&child_partition);
225
226 let child_core_key = child_ctx.core();
232 let lane_str: Option<String> = match client
233 .cmd("HGET")
234 .arg(&child_core_key)
235 .arg("lane_id")
236 .execute()
237 .await
238 {
239 Ok(v) => v,
240 Err(e) => {
241 tracing::warn!(
242 edge_id = edge_id.as_str(),
243 downstream = downstream_eid_str.as_str(),
244 error = %e,
245 "dispatch_dep: HGET lane_id failed, skipping (reconciler will retry)"
246 );
247 continue;
248 }
249 };
250 let lane_id = ff_core::types::LaneId::new(
251 lane_str.as_deref().unwrap_or("default"),
252 );
253
254 let att_idx_str: Option<String> = match client
258 .cmd("HGET")
259 .arg(&child_core_key)
260 .arg("current_attempt_index")
261 .execute()
262 .await
263 {
264 Ok(v) => v,
265 Err(e) => {
266 tracing::warn!(
267 edge_id = edge_id.as_str(),
268 downstream = downstream_eid_str.as_str(),
269 error = %e,
270 "dispatch_dep: HGET current_attempt_index failed, \
271 skipping (reconciler will retry)"
272 );
273 continue;
274 }
275 };
276 let att_idx = ff_core::types::AttemptIndex::new(
277 att_idx_str.as_deref().and_then(|s| s.parse().ok()).unwrap_or(0),
278 );
279
280 let dep_hash = child_ctx.dep_edge(&parsed_edge_id);
281
282 let deps_meta = child_ctx.deps_meta();
292 let unresolved = child_ctx.deps_unresolved();
293 let eligible = child_idx.lane_eligible(&lane_id);
294 let terminal = child_idx.lane_terminal(&lane_id);
295 let blocked_deps = child_idx.lane_blocked_dependencies(&lane_id);
296 let attempt_hash = child_ctx.attempt_hash(att_idx);
297 let stream_meta = child_ctx.stream_meta(att_idx);
298 let downstream_payload = child_ctx.payload();
299 let upstream_ctx = ExecKeyContext::new(&child_partition, eid);
300 let upstream_result = upstream_ctx.result();
301
302 let edgegroup = flow_ctx.edgegroup(&downstream_eid);
303 let incoming_set = flow_ctx.incoming(&downstream_eid);
304 let pending_cancel_groups = ff_core::keys::FlowIndexKeys::new(&flow_partition)
305 .pending_cancel_groups();
306 let downstream_eid_full = downstream_eid.to_string();
307 let keys: [&str; 14] = [
308 &child_core_key, &deps_meta, &unresolved, &dep_hash, &eligible, &terminal, &blocked_deps, &attempt_hash, &stream_meta, &downstream_payload, &upstream_result, &edgegroup, &incoming_set, &pending_cancel_groups, ];
323 let argv: [&str; 5] = [
324 edge_id,
325 upstream_outcome,
326 &now_ms,
327 flow_id_str, &downstream_eid_full, ];
330
331 match client
332 .fcall::<ferriskey::Value>("ff_resolve_dependency", &keys, &argv)
333 .await
334 {
335 Ok(val) => {
336 resolved += 1;
337 tracing::debug!(
338 edge_id = edge_id.as_str(),
339 downstream = downstream_eid_str.as_str(),
340 outcome = upstream_outcome,
341 "dispatch_dep: resolved dependency"
342 );
343 if is_child_skipped_result(&val) {
346 skipped_children.push((
347 downstream_eid.clone(),
348 flow_id_str.to_string(),
349 ));
350 }
351 }
352 Err(e) => {
353 tracing::warn!(
354 edge_id = edge_id.as_str(),
355 downstream = downstream_eid_str.as_str(),
356 error = %e,
357 "dispatch_dep: ff_resolve_dependency failed"
358 );
359 }
360 }
361 }
362
363 if resolved > 0 {
364 tracing::info!(
365 execution_id = %eid,
366 flow_id = flow_id_str,
367 resolved,
368 total_edges = edge_ids.len(),
369 skipped_cascade = skipped_children.len(),
370 "dispatch_dep: dependency resolution complete"
371 );
372 }
373
374 for (child_eid, child_flow_id) in &skipped_children {
378 Box::pin(dispatch_dependency_resolution_inner(
379 client, router, child_eid, Some(child_flow_id.as_str()),
380 cascade_depth + 1,
381 )).await;
382 }
383}
384
385fn is_child_skipped_result(value: &ferriskey::Value) -> bool {
395 match value {
396 ferriskey::Value::Array(arr) => {
397 if arr.len() < 4 {
398 return false;
401 }
402 arr.get(3)
403 .and_then(|v| match v {
404 Ok(ferriskey::Value::BulkString(b)) => {
405 Some(&b[..] == b"child_skipped")
406 }
407 Ok(ferriskey::Value::SimpleString(s)) => {
408 Some(s == "child_skipped")
409 }
410 _ => None,
411 })
412 .unwrap_or(false)
413 }
414 _ => {
415 tracing::warn!(
416 "is_child_skipped_result: expected Array, got non-array value"
417 );
418 false
419 }
420 }
421}