Skip to main content

Advanced: Short-lived Parent

View as Markdown

Drain the queued requests, then complete the parent without losing a request that arrives at the end.

The Init Step fills RequestChannel, initializes CurrSubFlowNum, and starts N workers. CurrSubFlowNum counts the running SubFlows. The HandleRequest Step's Execute locks it and adds one. The HandleSubFlow Step's Execute takes the same lock and subtracts one.

The last active worker returns ForceCompleteIfChannelsEmpty. Dex then checks RequestChannel and completes the Flow only if the Channel is empty. If an RPC has published a request first, Dex schedules another HandleRequest Step instead. The check and the completion happen together, so no request can arrive between them. Checking the Channel size in application code cannot give this guarantee.

Core implementation

advanced_short_live_parent_flow.pyPython
DEFAULT_CONCURRENCY = 10
MAX_BUFFERED_REQUESTS = 100


class ShortLiveInit(Step[ParentInput]):
def __init__(
self,
request_channel: Channel[str],
curr_subflow_num: Attribute[int],
) -> None:
self.request_channel = request_channel
self.curr_subflow_num = curr_subflow_num

def get_step_type(self) -> str:
return "Init"

def execute(self, context: Context, input: ParentInput) -> StepDecision:
for request in input.requests:
self.request_channel.publish(context, request)
self.curr_subflow_num.set(context, 0)
concurrency = input.concurrency if input.concurrency > 0 else DEFAULT_CONCURRENCY
return go_to_many(
*(StepMovement.of(ShortLiveHandleRequest, None) for _ in range(concurrency))
)


class ShortLiveHandleRequest(Step[None]):
def __init__(
self,
request_channel: Channel[str],

Example: examples/python/dex_examples/patterns/parallel-subflows/advanced_short_live_parent_flow.py

Empty batches and early requests

The example is simplified: its RPC can publish as soon as the Flow starts, and it starts the workers even for an empty batch. A parent that must handle an empty batch adds an intake gate:

  • The intake opens only when the first Step has queued the start batch. The RPC reads the gate and rejects a request before that. An empty start batch then completes at once, because no request can be waiting.
  • The Steps that change the child count lock the gate's Attribute. Keep the RPC free of locks.
  • The buffer limit is a soft limit. Concurrent RPCs can read the same size, and the Channel size counts only queued requests, not work already taken. A hard limit needs atomic admission, such as a locked reservation. Check the start batch against the limit where the Flow is started.
  • When the caller must confirm each request was handled, the consuming Step records the request's business ID in the same commit, and the caller reads that record; see Confirm that a message was handled.

These steps describe the variant with an intake gate. Its extra Step, AdmitBatch, is not in the graph above.

  1. AdmitBatch takes the start batch while it holds the lock on the admission Attribute. An empty batch returns GracefulComplete, because the intake never opened.
  2. Otherwise AdmitBatch, in one commit, queues each request in the Requests Channel, opens the intake, sets the active child count to 0, and starts the workers in HandleRequest.
  3. sendRequest reads the admission Attribute without a lock. It accepts a request only while the intake is open and the current queue length is below the limit. It queues the request in the same Channel.

Next