| 1 | package Langertha::Raid::Parallel; | ||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Parallel Raid orchestrator with branched context isolation | ||||||||||||||||||||||||||||||||||
| 3 | 2 | our $VERSION = '0.502'; | |||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use Future::AsyncAwait; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Future; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Langertha::Result; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||
| 10 | 2 | extends 'Langertha::Raid'; | |||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||
| 13 | 2 | has merge_slot => ( | |||||||||||||||||||||||||||||||||
| 14 | is => 'ro', | ||||||||||||||||||||||||||||||||||
| 15 | isa => 'Str', | ||||||||||||||||||||||||||||||||||
| 16 | default => 'parallel_branches', | ||||||||||||||||||||||||||||||||||
| 17 | ); | ||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||
| 20 | 5 | ▶ | async sub run_f { | ||||||||||||||||||||||||||||||||
|
Subroutine
run_f: called CC=9 SCAR=22.1
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 21 | 5 | my ( $self, $ctx ) = @_; | |||||||||||||||||||||||||||||||||
| 22 | 5 | $ctx = $self->_coerce_context($ctx); | |||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||
| 24 | 5 | my @steps = @{$self->steps}; | |||||||||||||||||||||||||||||||||
| 25 | 5 | ▶ | return Langertha::Result->final($ctx->input // '')->with_context($ctx) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$ctx->input // ""
MC/DC0%0/1
$ctx->input // ""
$ctx->input
Truth table0%0/2
A
A= $ctx->input
| |||||||||||||||||||||||||||||||||||
| 26 | unless @steps; | ||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||
| 28 | 5 | $ctx->add_trace({ | |||||||||||||||||||||||||||||||||
| 29 | node => ref($self), | ||||||||||||||||||||||||||||||||||
| 30 | event => 'parallel_start', | ||||||||||||||||||||||||||||||||||
| 31 | steps => scalar @steps, | ||||||||||||||||||||||||||||||||||
| 32 | }); | ||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||
| 34 | 5 | my @futures; | |||||||||||||||||||||||||||||||||
| 35 | 5 | for my $idx (0..$#steps) { | |||||||||||||||||||||||||||||||||
| 36 | 9 | my $step = $steps[$idx]; | |||||||||||||||||||||||||||||||||
| 37 | 9 | my $step_name = $self->_step_label($step, $idx); | |||||||||||||||||||||||||||||||||
| 38 | 9 | my $branch_ctx = $ctx->branch(metadata => { | |||||||||||||||||||||||||||||||||
| 39 | parallel_index => $idx, | ||||||||||||||||||||||||||||||||||
| 40 | parallel_step => $step_name, | ||||||||||||||||||||||||||||||||||
| 41 | }); | ||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||
| 43 | my $future = $step->run_f($branch_ctx)->then(sub { | ||||||||||||||||||||||||||||||||||
| 44 | 8 | ▶ | my ( $raw_result ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 45 | 8 | my $result = $self->_normalize_result($raw_result); | |||||||||||||||||||||||||||||||||
| 46 | 8 | Future->done({ | |||||||||||||||||||||||||||||||||
| 47 | index => $idx, | ||||||||||||||||||||||||||||||||||
| 48 | step_name => $step_name, | ||||||||||||||||||||||||||||||||||
| 49 | result => $result, | ||||||||||||||||||||||||||||||||||
| 50 | context => $branch_ctx, | ||||||||||||||||||||||||||||||||||
| 51 | }); | ||||||||||||||||||||||||||||||||||
| 52 | })->else(sub { | ||||||||||||||||||||||||||||||||||
| 53 | 1 | ▶ | my ( $err ) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||||||||
| 54 | 1 | ▶ | chomp($err) if defined $err; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 55 | 1 | Future->done({ | |||||||||||||||||||||||||||||||||
| 56 | index => $idx, | ||||||||||||||||||||||||||||||||||
| 57 | step_name => $step_name, | ||||||||||||||||||||||||||||||||||
| 58 | result => Langertha::Result->abort("Parallel step '$step_name' failed: $err"), | ||||||||||||||||||||||||||||||||||
| 59 | context => $branch_ctx, | ||||||||||||||||||||||||||||||||||
| 60 | }); | ||||||||||||||||||||||||||||||||||
| 61 | 9 | }); | |||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||
| 63 | 9 | push @futures, $future; | |||||||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||
| 66 | 5 | my @outcomes = await Future->needs_all(@futures); | |||||||||||||||||||||||||||||||||
| 67 | 5 | @outcomes = sort { $a->{index} <=> $b->{index} } @outcomes; | |||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||
| 69 | 5 | for my $outcome (@outcomes) { | |||||||||||||||||||||||||||||||||
| 70 | 9 | my $slot_name = sprintf('%02d_%s', $outcome->{index}, $outcome->{step_name}); | |||||||||||||||||||||||||||||||||
| 71 | 9 | $ctx->merge_branch($outcome->{context}, slot => $self->merge_slot, name => $slot_name); | |||||||||||||||||||||||||||||||||
| 72 | $ctx->add_trace({ | ||||||||||||||||||||||||||||||||||
| 73 | node => ref($self), | ||||||||||||||||||||||||||||||||||
| 74 | event => 'parallel_branch_result', | ||||||||||||||||||||||||||||||||||
| 75 | step_index => $outcome->{index}, | ||||||||||||||||||||||||||||||||||
| 76 | step_name => $outcome->{step_name}, | ||||||||||||||||||||||||||||||||||
| 77 | result_type => $outcome->{result}->type, | ||||||||||||||||||||||||||||||||||
| 78 | 9 | }); | |||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||
| 81 | my $winner = | ||||||||||||||||||||||||||||||||||
| 82 | 9 | (grep { $_->{result}->is_abort } @outcomes)[0] | |||||||||||||||||||||||||||||||||
| 83 | 6 | || (grep { $_->{result}->is_question } @outcomes)[0] | |||||||||||||||||||||||||||||||||
| 84 | 5 | ▶ | || (grep { $_->{result}->is_pause } @outcomes)[0]; | ||||||||||||||||||||||||||||||||
|
Condition83%5/6
(grep {$$_{'result'}->is_abort;} @outcomes)[0] || (grep {$$_{'result'}->is_question;} @outcomes)[0] || (grep {$$_{'result'}->is_pause;} @outcomes)[0]
(grep {$$_{"result"}->is_abort;} @outcomes)[0] or (grep {$$_{"result"}->is_question;} @outcomes)[0]
MC/DC66%2/3
(grep {$$_{'result'}->is_abort;} @outcomes)[0] || (grep {$$_{'result'}->is_question;} @outcomes)[0] || (grep {$$_{'result'}->is_pause;} @outcomes)[0]
(grep {$$_{"result"}->is_abort;} @outcomes)[0]
(grep {$$_{"result"}->is_question;} @outcomes)[0]
(grep {$$_{'result'}->is_pause;} @outcomes)[0]
Truth table75%3/4
A or B || C
A= (grep {$$_{"result"}->is_abort;} @outcomes)[0]B= (grep {$$_{"result"}->is_question;} @outcomes)[0]C= (grep {$$_{'result'}->is_pause;} @outcomes)[0]
| |||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||
| 86 | 5 | ▶ | if ($winner) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 87 | 3 | $ctx->state->{last_result_type} = $winner->{result}->type; | |||||||||||||||||||||||||||||||||
| 88 | 3 | $ctx->state->{last_result} = $winner->{result}->as_hash; | |||||||||||||||||||||||||||||||||
| 89 | 3 | return $self->_with_context_result($winner->{result}, $ctx); | |||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||
| 92 | my @texts = map { | ||||||||||||||||||||||||||||||||||
| 93 | 2 | $_->{result}->has_text ? $_->{result}->text : () | |||||||||||||||||||||||||||||||||
| 94 | 4 | ▶ | } @outcomes; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||
| 96 | 2 | my $joined = join("\n", @texts); | |||||||||||||||||||||||||||||||||
| 97 | 2 | my $final = Langertha::Result->final($joined, data => { | |||||||||||||||||||||||||||||||||
| 98 | branch_count => scalar @outcomes, | ||||||||||||||||||||||||||||||||||
| 99 | }); | ||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||
| 101 | $ctx->state->{parallel_results} = [ | ||||||||||||||||||||||||||||||||||
| 102 | 2 | map { $_->{result}->as_hash } @outcomes | |||||||||||||||||||||||||||||||||
| 103 | ]; | ||||||||||||||||||||||||||||||||||
| 104 | 2 | $self->_apply_final_result_to_context($ctx, $final, step_name => ref($self)); | |||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||
| 106 | 2 | return $self->_with_context_result($final, $ctx); | |||||||||||||||||||||||||||||||||
| 107 | 2 | } | |||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||
| 110 | 2 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||
| 112 | 2 | 1; | |||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||
| 114 | __END__ | ||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||
| 116 | =pod | ||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||
| 118 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||
| 120 | =head1 NAME | ||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||
| 122 | Langertha::Raid::Parallel - Parallel Raid orchestrator with branched context isolation | ||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||
| 124 | =head1 VERSION | ||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||
| 126 | version 0.502 | ||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||
| 128 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||
| 130 | my $raid = Langertha::Raid::Parallel->new( | ||||||||||||||||||||||||||||||||||
| 131 | steps => [ $a, $b, $c ], | ||||||||||||||||||||||||||||||||||
| 132 | ); | ||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||
| 134 | my $result = await $raid->run_f($ctx); | ||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||
| 136 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||
| 138 | Runs child steps concurrently using Futures. Each branch receives a cloned | ||||||||||||||||||||||||||||||||||
| 139 | context (via C<< $ctx->branch >>), so branch mutations are isolated. | ||||||||||||||||||||||||||||||||||
| 140 | Branch snapshots are merged back into parent artifacts under C<merge_slot>. | ||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||
| 142 | Aggregation strategy: | ||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||
| 144 | =over 4 | ||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||
| 146 | =item * If any branch aborts, first abort is propagated. | ||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||
| 148 | =item * Otherwise first question is propagated. | ||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||
| 150 | =item * Otherwise first pause is propagated. | ||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||
| 152 | =item * Otherwise final branch texts are concatenated with newlines. | ||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||
| 154 | =back | ||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||
| 156 | =head2 merge_slot | ||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||
| 158 | Artifact slot name where merged branch snapshots are stored. | ||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||
| 160 | =head2 run_f | ||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||
| 162 | my $result = await $raid->run_f($ctx); | ||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||
| 164 | Executes all child steps concurrently with branched context isolation and | ||||||||||||||||||||||||||||||||||
| 165 | deterministic result aggregation. | ||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||
| 167 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||
| 169 | =head2 Issues | ||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||
| 171 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||||||||||||||||
| 172 | L<https://github.com/Getty/langertha/issues>. | ||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||
| 174 | =head2 IRC | ||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||
| 176 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||
| 178 | =head1 CONTRIBUTING | ||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||
| 180 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||
| 182 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||
| 184 | Torsten Raudssus <getty@cpan.org> | ||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||
| 186 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||
| 188 | This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>. | ||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||
| 190 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||
| 191 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||
| 193 | =cut | ||||||||||||||||||||||||||||||||||