-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathinvocation_loop.rs
More file actions
1105 lines (1021 loc) · 42.4 KB
/
invocation_loop.rs
File metadata and controls
1105 lines (1021 loc) · 42.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2024-2026 Golem Cloud
//
// Licensed under the Golem Source License v1.1 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://license.golem.cloud/LICENSE
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use crate::model::{ReadFileResult, TrapType};
use crate::services::events::Event;
use crate::services::golem_config::SnapshotPolicy;
use crate::services::oplog::{CommitLevel, OplogOps};
use crate::services::{HasEvents, HasOplog, HasWorker};
use crate::worker::invocation::{
invoke_observed_and_traced, lower_invocation, InvocationMode, InvokeResult,
};
use crate::worker::{
FinalWorkerState, QueuedWorkerInvocation, RetryDecision, RunningWorker, Worker, WorkerCommand,
};
use crate::workerctx::{PublicWorkerIo, WorkerCtx};
use anyhow::anyhow;
use async_lock::Mutex;
use drop_stream::DropStream;
use futures::channel::oneshot;
use futures::channel::oneshot::Sender;
use golem_common::model::agent::{AgentMode, ParsedAgentId};
use golem_common::model::component::{ComponentFilePath, ComponentRevision};
use golem_common::model::oplog::{AgentError, OplogEntry};
use golem_common::model::{
invocation_context::{AttributeValue, InvocationContextStack},
OplogIndex,
};
use golem_common::model::{
AgentId, AgentInvocation, AgentInvocationKind, AgentInvocationOutput, AgentInvocationResult,
IdempotencyKey, OwnedAgentId, TimestampedAgentInvocation,
};
use golem_common::retries::get_delay;
use golem_service_base::error::worker_executor::{InterruptKind, WorkerExecutorError};
use golem_service_base::model::GetFileSystemNodeResult;
use golem_common::model::agent::structural_format::format_structural;
use std::collections::VecDeque;
use std::ops::DerefMut;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use tokio::sync::mpsc::UnboundedReceiver;
use tokio::sync::RwLock;
use tracing::{debug, span, warn, Instrument, Level, Span};
use wasmtime::component::Instance;
use wasmtime::Store;
/// Context of a running worker's invocation loop
pub struct InvocationLoop<Ctx: WorkerCtx> {
pub receiver: UnboundedReceiver<WorkerCommand>,
pub active: Arc<RwLock<VecDeque<QueuedWorkerInvocation>>>,
pub owned_agent_id: OwnedAgentId,
pub parent: Arc<Worker<Ctx>>, // parent must not be dropped until the invocation_loop is running
pub waiting_for_command: Arc<AtomicBool>,
pub interrupt_signal: Arc<Mutex<Option<InterruptKind>>>,
pub oom_retry_count: u32,
}
impl<Ctx: WorkerCtx> InvocationLoop<Ctx> {
/// Runs the invocation loop of a running worker, responsible for processing incoming
/// invocation and update commands one by one.
///
/// The outer invocation loop consists of the following steps:
///
/// - Creating the worker instance
/// - Recovering the worker state
/// - Processing incoming commands in the inner invocation loop
/// - Suspending the worker
/// - Process the retry decision
pub async fn run(&mut self) {
loop {
debug!("Invocation queue loop creating the instance");
let (instance, store) = if let Some((instance, store)) = self.create_instance().await {
(instance, store)
} else {
// early return, can't retry a failed instance creation
break;
};
debug!("Invocation queue loop preparing the instance");
let mut final_decision = self.recover_instance_state(&instance, &store).await;
if final_decision.is_none() {
let mut inner_loop = InnerInvocationLoop {
receiver: &mut self.receiver,
active: self.active.clone(),
owned_agent_id: self.owned_agent_id.clone(),
parent: self.parent.clone(),
waiting_for_command: self.waiting_for_command.clone(),
interrupt_signal: self.interrupt_signal.clone(),
instance: &instance,
store: &store,
invocations_since_snapshot: 0,
};
final_decision = inner_loop.run().await;
}
self.suspend_worker(&store).await;
match final_decision {
None | Some(RetryDecision::None) => {
debug!("Invocation queue loop notifying parent about being stopped");
self.parent
.stop_internal(
true,
None,
FinalWorkerState::Unloaded {
startup_failure: None,
},
)
.await;
break;
}
Some(RetryDecision::TryStop(ts)) => {
if ts < *self.parent.last_resume_request.lock().await {
debug!(
"Suspend request ignored because there was a resume request since it"
);
continue;
} else {
debug!("Invocation queue loop notifying parent about being stopped");
self.parent
.stop_internal(
true,
None,
FinalWorkerState::Unloaded {
startup_failure: None,
},
)
.await;
break;
}
}
Some(RetryDecision::Immediate) => {
debug!("Invocation queue loop triggering restart immediately");
continue;
}
Some(RetryDecision::Delayed(delay)) => {
debug!("Invocation queue loop sleeping for {delay:?} for delayed restart");
tokio::time::sleep(delay).await;
debug!("Invocation queue loop restarting after delay");
continue;
}
Some(RetryDecision::ReacquirePermits) => {
let delay = get_delay(self.parent.oom_retry_config(), self.oom_retry_count);
debug!(
"Invocation queue loop dropping memory permits and triggering restart with a delay of {delay:?}"
);
let _ = Worker::restart_on_oom(
self.parent.clone(),
true,
delay,
self.oom_retry_count + 1,
)
.await;
break;
}
}
}
}
/// Create the worker instance and publish an event about it
async fn create_instance(&self) -> Option<(Instance, Mutex<Store<Ctx>>)> {
match RunningWorker::create_instance(self.parent.clone()).await {
Ok((instance, store)) => {
self.parent.events().publish(Event::WorkerLoaded {
agent_id: self.owned_agent_id.agent_id(),
result: Ok(()),
});
Some((instance, store))
}
Err(err) => {
warn!("Failed to start the worker: {err}");
self.parent.events().publish(Event::WorkerLoaded {
agent_id: self.owned_agent_id.agent_id(),
result: Err(err.clone()),
});
self.parent
.stop_internal(
true,
Some(err.clone()),
FinalWorkerState::Unloaded {
startup_failure: Some(err),
},
)
.await;
None
}
}
}
/// Prepares the instance for running by recovering its persisted state
///
/// In case of failure to recover the state, it returns the retry decision to be used.
async fn recover_instance_state(
&self,
instance: &Instance,
store: &Mutex<Store<Ctx>>,
) -> Option<RetryDecision> {
let mut store = store.lock().await;
store.data().set_suspended();
let span = span!(
Level::INFO,
"invocation",
agent_id = %self.owned_agent_id.agent_id,
agent_type = self.parent
.parsed_agent_id
.as_ref()
.map(|id| id.agent_type.to_string())
.unwrap_or_else(|| "-".to_string()),
);
let prepare_result =
Ctx::prepare_instance(&self.owned_agent_id.agent_id, instance, &mut *store)
.instrument(span)
.await;
match prepare_result {
Ok(decision) => {
debug!("Recovery decision from prepare_instance: {decision:?}");
decision
}
Err(err) => {
warn!("Failed to start the worker: {err}");
store.data().set_suspended();
self.parent
.stop_internal(
true,
Some(err.clone()),
FinalWorkerState::Unloaded {
startup_failure: Some(err),
},
)
.await;
Some(RetryDecision::None) // early return, we can't retry this
}
}
}
/// Suspends the worker after the invocation loop exited
async fn suspend_worker(&self, store: &Mutex<Store<Ctx>>) {
// Marking the worker as suspended
store.lock().await.data().set_suspended();
// Making sure all pending commits are flushed
// Make sure all pending commits are done
store
.lock()
.await
.data()
.get_public_state()
.worker()
.commit_oplog_and_update_state(CommitLevel::Always)
.await;
}
}
struct InnerInvocationLoop<'a, Ctx: WorkerCtx> {
receiver: &'a mut UnboundedReceiver<WorkerCommand>,
active: Arc<RwLock<VecDeque<QueuedWorkerInvocation>>>,
owned_agent_id: OwnedAgentId,
parent: Arc<Worker<Ctx>>, // parent must not be dropped until the invocation_loop is running
waiting_for_command: Arc<AtomicBool>,
interrupt_signal: Arc<Mutex<Option<InterruptKind>>>,
instance: &'a Instance,
store: &'a Mutex<Store<Ctx>>,
invocations_since_snapshot: u64,
}
impl<Ctx: WorkerCtx> InnerInvocationLoop<'_, Ctx> {
/// The inner invocation loop started when the worker instance state is fully restored
/// and the worker is ready to take invocations.
///
/// This loop exits when the unbounded message queue owned by the RunningWorker is dropped,
/// or when an error occurs in one of the command handlers.
///
/// The inner loop only runs if the retry decision coming from `recover_instance_state` is `None`,
/// meaning there were no errors during the instance preparation. The inner loop can override this
/// decision in the following way:
/// - If it returns `RetryDecision::None`, it means it is not possible to retry the outer loop and the whole invocation loop should be stopped.
/// - Otherwise it returns either `None` if there were no errors, otherwise the retry decision coming from the
/// underlying retry logic.
///
/// The outer loop should either break or use the returned retry decision after the inner loop quits.
pub async fn run(&mut self) -> Option<RetryDecision> {
debug!("Invocation queue loop started");
let mut final_decision = None;
// Exits when RunningWorker is dropped
self.waiting_for_command.store(true, Ordering::Release);
while let Some(cmd) = self.receiver.recv().await {
self.waiting_for_command.store(false, Ordering::Release);
let outcome = match cmd {
WorkerCommand::Unblock => {
loop {
if let Some(kind) = self.interrupt_signal.lock().await.take() {
break self.interrupt(kind).await;
}
let message = self.active.write().await.pop_front();
let result = if let Some(message) = message {
self.internal_invocation(message).await
} else {
// Queue is empty, use last_known_status for pending updates and invocations
break self.drain_pending_from_status().await;
};
match result {
CommandOutcome::Continue => {
// Continue draining the queue
continue;
}
other => {
// Break out of the drain loop and handle the outcome
break other;
}
}
}
}
WorkerCommand::ResumeReplay => self.resume_replay().await,
};
match outcome {
CommandOutcome::BreakOuterLoop => {
final_decision = Some(RetryDecision::None);
break;
}
CommandOutcome::BreakInnerLoop(decision) => {
final_decision = Some(decision);
break;
}
CommandOutcome::Continue => {}
}
self.waiting_for_command.store(true, Ordering::Release);
}
self.waiting_for_command.store(false, Ordering::Release);
debug!(final_decision = ?final_decision, "Invocation queue loop finished");
final_decision
}
/// When the main queue becomes empty, process items from last_known_status:
/// first pending_updates, then pending_invocations
async fn drain_pending_from_status(&mut self) -> CommandOutcome {
loop {
let status = self.parent.get_non_detached_last_known_status().await;
// First, try to process a pending update
if status.pending_updates.front().is_some() {
// if the update made it to pending_updates (instead of pending invocations), it is ready
// to be processed on next restart. So just restart here and let the recovery logic take over
break CommandOutcome::BreakInnerLoop(RetryDecision::Immediate);
}
// Then, try to process a pending invocation
if let Some(timestamped_invocation) = status.pending_invocations.first() {
let idempotency_key = timestamped_invocation.invocation.idempotency_key();
let invocation_span = if let Some(idempotency_key) = idempotency_key {
let spans = self.parent.external_invocation_spans.read().await;
spans.get(idempotency_key).cloned()
} else {
None
};
let invocation_span = invocation_span.unwrap_or(Span::current());
let outcome = async {
let mut store = self.store.lock().await;
let mut invocation = Invocation {
owned_agent_id: self.owned_agent_id.clone(),
parent: self.parent.clone(),
instance: self.instance,
store: store.deref_mut(),
};
invocation
.external_invocation(timestamped_invocation.clone(), &invocation_span)
.await
}
.instrument(span!(parent: &invocation_span, Level::INFO, "invocation_queue_pickup"))
.await;
match outcome {
CommandOutcome::Continue => {
self.on_external_invocation_completed().await;
continue;
}
other => break other,
}
}
break CommandOutcome::Continue;
}
}
async fn on_external_invocation_completed(&mut self) {
self.invocations_since_snapshot += 1;
if let SnapshotPolicy::EveryNInvocation { count } = self.parent.snapshot_policy() {
if self.invocations_since_snapshot >= *count as u64 {
self.invocations_since_snapshot = 0;
self.active
.write()
.await
.push_back(QueuedWorkerInvocation::SaveSnapshot);
}
}
}
/// Resumes an interrupted replay process
///
/// Returns `CommandOutcome` if this fails and the invocation loop should be stopped.
/// Otherwise, it returns the new retry decision to be used by the outer invocation loop.
async fn resume_replay(&self) -> CommandOutcome {
let mut store = self.store.lock().await;
let resume_replay_result = Ctx::resume_replay(&mut *store, self.instance, true).await;
match resume_replay_result {
Ok(None) => CommandOutcome::Continue,
Ok(Some(decision)) => CommandOutcome::BreakInnerLoop(decision),
Err(err) => {
warn!("Failed to resume replay: {err}");
store.data().set_suspended();
self.parent
.stop_internal(
true,
Some(err.clone()),
FinalWorkerState::Unloaded {
startup_failure: Some(err),
},
)
.await;
CommandOutcome::BreakOuterLoop
}
}
}
/// Performs a queued invocation on the worker
///
/// The queued invocations internal invocations that we use for
/// concurrency control.
async fn internal_invocation(&mut self, message: QueuedWorkerInvocation) -> CommandOutcome {
let mut store = self.store.lock().await;
let store = store.deref_mut();
let mut invocation = Invocation {
owned_agent_id: self.owned_agent_id.clone(),
parent: self.parent.clone(),
instance: self.instance,
store,
};
invocation.process(message).await
}
/// Performs an interrupt request
async fn interrupt(&self, kind: InterruptKind) -> CommandOutcome {
match kind {
InterruptKind::Restart | InterruptKind::Jump => {
CommandOutcome::BreakInnerLoop(RetryDecision::Immediate)
}
_ => CommandOutcome::BreakInnerLoop(RetryDecision::None),
}
}
}
/// Context for performing one `QueuedWorkerInvocation`
///
/// The most important part is that unlike the `InnerInvocationLoop`, it holds a locked
/// mutable reference to the instance `Store`. The instance mutex is held for the whole duration
/// of performing an invocation.
struct Invocation<'a, Ctx: WorkerCtx> {
owned_agent_id: OwnedAgentId,
parent: Arc<Worker<Ctx>>, // parent must not be dropped until the invocation_loop is running
instance: &'a Instance,
store: &'a mut Store<Ctx>,
}
impl<Ctx: WorkerCtx> Invocation<'_, Ctx> {
/// Process a queued worker invocation
async fn process(&mut self, message: QueuedWorkerInvocation) -> CommandOutcome {
match message {
QueuedWorkerInvocation::GetFileSystemNode { path, sender } => {
self.get_file_system_node(path, sender).await;
CommandOutcome::Continue
}
QueuedWorkerInvocation::ReadFile { path, sender } => {
self.read_file(path, sender).await;
CommandOutcome::Continue
}
QueuedWorkerInvocation::AwaitReadyToProcessCommands { sender } => {
let _ = sender.send(Ok(()));
CommandOutcome::Continue
}
QueuedWorkerInvocation::SaveSnapshot => self.save_snapshot().await,
}
}
/// Process an external queued worker invocation - this is either an exported function invocation
/// or a manual update request (which involves invoking the exported save-snapshot functions, so
/// it is a special case of the exported function invocation).
async fn external_invocation(
&mut self,
inner: TimestampedAgentInvocation,
invocation_span: &Span,
) -> CommandOutcome {
match inner.invocation {
AgentInvocation::ManualUpdate { target_revision } => {
self.manual_update(target_revision).await
}
invocation => {
if let Some(idempotency_key) = invocation.idempotency_key() {
let has_result = {
let invocation_results = self.parent.invocation_results.read().await;
invocation_results.contains_key(idempotency_key)
};
if !has_result {
self.invoke_agent(invocation, invocation_span).await
} else {
debug!(
"Skipping enqueued invocation with idempotency key {idempotency_key} as it already has a result"
);
CommandOutcome::Continue
}
} else {
self.invoke_agent(invocation, invocation_span).await
}
}
}
}
/// Invokes an agent function on the worker
async fn invoke_agent(
&mut self,
invocation: AgentInvocation,
invocation_span: &Span,
) -> CommandOutcome {
let display_name = invocation.display_name();
let invocation_context = invocation.invocation_context();
let idempotency_key = invocation
.idempotency_key()
.cloned()
.unwrap_or_else(IdempotencyKey::fresh);
let span = span!(
parent: invocation_span,
Level::INFO,
"invocation",
agent_id = %self.owned_agent_id.agent_id,
agent_type = self.parent
.parsed_agent_id
.as_ref()
.map(|id| id.agent_type.to_string())
.unwrap_or_else(|| "-".to_string()),
%idempotency_key,
function = display_name
);
self.invoke_agent_inner(invocation_context, idempotency_key, invocation)
.instrument(span)
.await
}
/// Invokes an agent function on the worker
///
/// The inner implementation of `invoke_agent` to be instrumented with a span.
async fn invoke_agent_inner(
&mut self,
invocation_context: InvocationContextStack,
idempotency_key: IdempotencyKey,
invocation: AgentInvocation,
) -> CommandOutcome {
let kind = invocation.kind();
let display_name = invocation.display_name();
let result = self
.invoke_agent_with_context(invocation_context, idempotency_key, invocation)
.await;
match result {
Ok(InvokeResult::Succeeded {
result: invocation_result,
consumed_fuel,
}) => {
self.agent_invocation_finished(display_name, invocation_result, consumed_fuel, kind)
.await
}
_ => self.agent_invocation_failed(&display_name, result).await,
}
}
/// Sets the necessary contextual information on the worker and performs the actual
/// invocation.
async fn invoke_agent_with_context(
&mut self,
mut invocation_context: InvocationContextStack,
idempotency_key: IdempotencyKey,
invocation: AgentInvocation,
) -> Result<InvokeResult, WorkerExecutorError> {
let (lowered, local_span_ids, inherited_span_ids, component_metadata) = async {
self.store
.data_mut()
.set_current_idempotency_key(idempotency_key.clone())
.await;
let component_metadata = self.store.data().component_metadata().metadata.clone();
Self::extend_invocation_context(
&mut invocation_context,
&idempotency_key,
&invocation,
&self.owned_agent_id.agent_id(),
&self.parent.parsed_agent_id,
);
let (local_span_ids, inherited_span_ids) = invocation_context.span_ids();
self.store
.data_mut()
.set_current_invocation_context(invocation_context)
.await?;
if let Some(idempotency_key) = self.store.data().get_current_idempotency_key().await {
self.store
.data()
.get_public_state()
.worker()
.store_invocation_resuming(&idempotency_key)
.await;
}
let invocation_for_lowering = invocation.clone();
let lowered = lower_invocation(
invocation_for_lowering,
&component_metadata,
self.parent.parsed_agent_id.as_ref(),
)?;
Ok::<_, WorkerExecutorError>((
lowered,
local_span_ids,
inherited_span_ids,
component_metadata,
))
}
.instrument(span!(Level::INFO, "prepare_invocation_context"))
.await?;
let result = invoke_observed_and_traced(
lowered,
self.store,
self.instance,
&component_metadata,
InvocationMode::Live(invocation),
)
.await;
// We are removing the spans introduced by the invocation. Not calling `finish_span` here,
// as it would add FinishSpan oplog entries without corresponding StartSpan ones. Instead,
// the oplog processor should assume that spans implicitly created by AgentInvocationStarted
// are finished at AgentInvocationFinished.
for span_id in local_span_ids {
self.store.data_mut().remove_span(&span_id)?;
}
for span_id in inherited_span_ids {
self.store.data_mut().remove_span(&span_id)?;
}
result
}
/// The logic handling a successfully finished agent invocation
///
/// Successful here means that the invocation function returned with
/// `InvokeResult::Succeeded`. As the returned values get further processing,
/// the whole invocation can still fail during that.
async fn agent_invocation_finished(
&mut self,
full_function_name: String,
invocation_result: AgentInvocationResult,
consumed_fuel: u64,
kind: AgentInvocationKind,
) -> CommandOutcome {
let component_revision = self.store.data().component_metadata().revision;
let output = AgentInvocationOutput {
result: invocation_result,
consumed_fuel: Some(consumed_fuel),
invocation_status: None,
component_revision: Some(component_revision),
};
match self
.store
.data_mut()
.on_agent_invocation_success(&full_function_name, consumed_fuel, &output)
.await
{
Ok(()) => {
if self.parent.agent_mode() == AgentMode::Ephemeral {
if self.store.data().component_metadata().metadata.is_agent()
&& kind == AgentInvocationKind::AgentInitialization
{
CommandOutcome::Continue
} else {
CommandOutcome::BreakInnerLoop(RetryDecision::None)
}
} else {
CommandOutcome::Continue
}
}
Err(error) => {
self.store
.data_mut()
.on_invocation_failure(
&full_function_name,
&TrapType::Error {
error: AgentError::Unknown(error.to_string()),
retry_from: OplogIndex::INITIAL,
},
)
.await;
CommandOutcome::BreakInnerLoop(RetryDecision::None)
}
}
}
/// The logic handling an agent invocation that did not succeed.
async fn agent_invocation_failed(
&mut self,
full_function_name: &str,
result: Result<InvokeResult, WorkerExecutorError>,
) -> CommandOutcome {
let trap_type = match result {
Ok(invoke_result) => invoke_result.as_trap_type::<Ctx>(),
Err(error) => Some(TrapType::from_error::<Ctx>(
&anyhow!(error),
OplogIndex::INITIAL,
)),
};
let decision = match trap_type {
Some(trap_type) => {
self.store
.data_mut()
.on_invocation_failure(full_function_name, &trap_type)
.await
}
None => RetryDecision::None,
};
CommandOutcome::BreakInnerLoop(decision)
}
/// Try to perform the save-snapshot step of a manual update on the worker
async fn manual_update(&mut self, target_revision: ComponentRevision) -> CommandOutcome {
let span = span!(
Level::INFO,
"manual_update",
agent_id = %self.owned_agent_id.agent_id,
target_revision = %target_revision,
agent_type = self.parent
.parsed_agent_id
.as_ref()
.map(|id| id.agent_type.to_string())
.unwrap_or_else(|| "-".to_string()),
);
self.manual_update_inner(target_revision)
.instrument(span)
.await
}
/// The inner implementation of the manual update command
async fn manual_update_inner(&mut self, target_revision: ComponentRevision) -> CommandOutcome {
let idempotency_key = {
let ctx = self.store.data_mut();
let idempotency_key = IdempotencyKey::fresh();
ctx.set_current_idempotency_key(idempotency_key.clone())
.await;
idempotency_key
};
let component_metadata = self.store.data().component_metadata().metadata.clone();
let save_snapshot_invocation = AgentInvocation::SaveSnapshot { idempotency_key };
let lowered = match lower_invocation(
save_snapshot_invocation,
&component_metadata,
self.parent.parsed_agent_id.as_ref(),
) {
Ok(lowered) => lowered,
Err(err) => {
warn!("Failed to lower save-snapshot invocation: {err}");
return self
.fail_update(
target_revision,
format!("failed to lower save-snapshot invocation: {err}"),
)
.await;
}
};
self.store.data_mut().begin_call_snapshotting_function();
let result = invoke_observed_and_traced(
lowered,
self.store,
self.instance,
&component_metadata,
InvocationMode::Replay,
)
.await;
self.store.data_mut().end_call_snapshotting_function();
match result {
Ok(InvokeResult::Succeeded {
result: AgentInvocationResult::SaveSnapshot { snapshot },
..
}) => {
match self
.store
.data()
.get_public_state()
.oplog()
.create_snapshot_based_update_description(
target_revision,
snapshot.data,
snapshot.mime_type,
)
.await
{
Ok(update_description) => {
// Enqueue the update
self.parent.enqueue_update(update_description).await;
// Reactivate the worker
CommandOutcome::BreakInnerLoop(RetryDecision::Immediate)
// Stop processing the queue to avoid race conditions
}
Err(error) => {
self.fail_update(
target_revision,
format!("failed to store the snapshot for manual update: {error}"),
)
.await
}
}
}
Ok(InvokeResult::Succeeded { .. }) => {
self.fail_update(
target_revision,
"failed to get a snapshot for manual update: invalid snapshot result"
.to_string(),
)
.await
}
Ok(InvokeResult::Failed { error, .. }) => {
let stderr = self
.store
.data()
.get_public_state()
.event_service()
.get_last_invocation_errors();
let error = error.to_string(&stderr);
self.fail_update(
target_revision,
format!("failed to get a snapshot for manual update: {error}"),
)
.await
}
Ok(InvokeResult::Exited { .. }) => {
self.fail_update(
target_revision,
"failed to get a snapshot for manual update: it called exit".to_string(),
)
.await
}
Ok(InvokeResult::Interrupted { interrupt_kind, .. }) => {
self.fail_update(
target_revision,
format!("failed to get a snapshot for manual update: {interrupt_kind:?}"),
)
.await
}
Err(error) => {
self.fail_update(
target_revision,
format!("failed to get a snapshot for manual update: {error:?}"),
)
.await
}
}
}
/// Performs a directory listing command on the worker's file system
///
/// These are threaded through the invocation loop to make sure they are not accessing the file system concurrently with invocations
/// that may modify them.
async fn get_file_system_node(
&self,
path: ComponentFilePath,
sender: Sender<Result<GetFileSystemNodeResult, WorkerExecutorError>>,
) {
let result = self.store.data().get_file_system_node(&path).await;
let _ = sender.send(result);
}
/// Performs a read file command on the worker's file system
///
/// These are threaded through the invocation loop to make sure they are not accessing the file system concurrently with invocations
/// that may modify them.
async fn read_file(
&self,
path: ComponentFilePath,
sender: Sender<Result<ReadFileResult, WorkerExecutorError>>,
) {
let result = self.store.data().read_file(&path).await;
match result {
Ok(ReadFileResult::Ok(stream)) => {
// special case. We need to wait until the stream is consumed to avoid corruption
//
// This will delay processing of the next invocation and is quite unfortunate.
// A possible improvement would be to check whether we are on a copy-on-write filesystem
// if yes, we can make a cheap copy of the file here and serve the read from that copy.
let (latch, latch_receiver) = oneshot::channel();
let drop_stream = DropStream::new(stream, || latch.send(()).unwrap());
let _ = sender.send(Ok(ReadFileResult::Ok(Box::pin(drop_stream))));
latch_receiver.await.unwrap();
}
other => {
let _ = sender.send(other);
}
};
}
/// Records an attempted worker update as failed
async fn fail_update(
&self,
target_revision: ComponentRevision,
error: String,
) -> CommandOutcome {
self.store
.data()
.on_worker_update_failed(target_revision, Some(error))
.await;
CommandOutcome::Continue
}
/// Extends the invocation context with a new span containing information about the invocation
fn extend_invocation_context(
invocation_context: &mut InvocationContextStack,
idempotency_key: &IdempotencyKey,
invocation: &AgentInvocation,
agent_id: &AgentId,
parsed_agent_id: &Option<ParsedAgentId>,
) {
let invocation_span = invocation_context.spans.first().start_span(None);
invocation_span.set_attribute(
"name".to_string(),
AttributeValue::String("invoke-exported-function".to_string()),
);
invocation_span.set_attribute(
"idempotency_key".to_string(),
AttributeValue::String(idempotency_key.to_string()),
);
invocation_span.set_attribute(
"function_name".to_string(),
AttributeValue::String(invocation.display_name()),
);
invocation_span.set_attribute(
"invocation_kind".to_string(),
AttributeValue::String(format!("{:?}", invocation.kind())),
);
invocation_span.set_attribute(
"agent_id".to_string(),
AttributeValue::String(agent_id.to_string()),
);
if let Some(parsed_agent_id) = parsed_agent_id {
invocation_span.set_attribute(
"agent_type".to_string(),
AttributeValue::String(parsed_agent_id.agent_type.to_string()),
);
invocation_span.set_attribute(
"agent_parameters".to_string(),
AttributeValue::String(
format_structural(&parsed_agent_id.parameters)
.unwrap_or_else(|err| format!("Cannot render: {}", err)),