When to add @Suspendable to methods in a flow?
When to add @Suspendable to methods in a flow?
Whats the best practice here to annotate a method as @Suspendable? In a Flow, there may be multiple private methods that query the vault/ compute business logic. Should these be annotated with @Suspendable so it can recover if a node crashes midway?
@Suspendable
@Suspendable
Or is @Suspendable only for methods where send / sendAndReceived are involved where its waiting for responses from counterparties?
@Suspendable
send
sendAndReceived
What about just normal methods that just do query/computation? Any benefits from adding @Suspendable?
– Adrian
Jun 30 at 9:29
I don't think so. I couldn't find any functions in CordaRPCOps.kt annotated with @Suspendable. so basically functions like sub-flow, sendReceive etc that are marked with suspendable should be called from functions that are itself marked suspendable.
– Kid101
Jun 30 at 10:08
1 Answer
1
From paralleluniverse:
The run methods in Fiber, SuspendableRunnable, and SuspendableCallable
declare that they may throw a SuspendExecution exception.
@Suspendable is our way to specify a suspendable method is by declaring throws SuspendExecution.This is convenient because SuspendExecution is a checked exception, so if f calls g and g is suspendable, the Java compiler will force us to declare that f is suspendable (and it must be because it calls g and g might be suspended).
Sometimes, however, we cannot declare f to throw SuspendExecution. One example is that f is an implementation of an interface method, and we cannot (or don’t want to) change the interface so that it throws SuspendExecution.
So, suppose method f is declared in interface I, and we’d like to make its implementation in class C suspendable. The compiler will not let us declare that we throw SuspendExecution because that will conflict with f’s declaration in I.
What we do, then, is annotate C.f with the @Suspendable annotation (in the co.paralleluniverse.fibers package). Assuming C.f calls park or some other suspendable method g – which does declare throws SuspendExecution, we need to surround f’s body with try {} catch(SuspendExecution) just so the method will compile.
if we want to run h in a fiber, then it must be suspendable because it calls f which is suspendable. We could designate h as suspendable either by annotating it with @Suspendable or by declaring SuspendExecution
If we want any method to run in a fiber, then it must be suspendable.
so basically, any method that calls a method which can throw SuspendExecution or is annotated with @Suspendable.
@Suspendable
In my case I faced error calling SubFlow from a function, since I didn't annotate it with @Suspendable I faced Quasar Exceptions. As SubFlow is annotated with @Suspendable annotating my function helped get rid of those errors.
Error: Uninstrumented whole methods ('**') or single calls ('!!') detected:
SubFlow
@Suspendable
SubFlow
@Suspendable
Uninstrumented whole methods ('**') or single calls ('!!') detected:
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Anything that initiates flow with other nodes or notary. Eg: A function that calls SubFlow(This flow initiates sessions with other nodes.) The function should be annotated with @Suspendable else I see quasar errors all over.
– Kid101
Jun 30 at 7:49