Stopping a journey when its log fails
What you want to do
The shop's log now writes each line to a logging service, over the network, and a write can fail. You want to know what happens to a journey whose log can't take a line, so that no order goes on with no record of what it did.
The code
The shop is chapter 25's, with its log changed. The faked logging service can't take the line saying that the charge of order A-15 succeeded, and throws LoggingDown, a RuntimeException, since report declares no checked exception:
/** The logging service's error: it can't take a line. */
final class LoggingDown extends RuntimeException {
private static final long serialVersionUID = 1L;
LoggingDown() {
super("the logging service is down");
}
}
/**
* The shop's logging service, faked: it prints each line it takes. It is down when the line saying
* that order A-15's charge succeeded reaches it.
*/
final class LoggingService {
void write(String line) {
if (line.contains(" A-15 ") && line.endsWith(" step_succeeded charge")) {
throw new LoggingDown();
}
System.out.println(line);
}
}
The log turns each event into a line, as before, and hands it to the service. It doesn't catch what the service throws, so it escapes report:
/**
* The shop's log: it writes a line for every event of a journey to the logging service, starting
* with the workflow's name and the journey's ID. A line the service refuses fails the report.
*/
final class Log implements Reporter {
private final LoggingService service = new LoggingService();
@Override
public void report(Event event) {
String line =
"log "
+ event.getWorkflow()
+ " "
+ event.getJourneyId()
+ " #"
+ event.getSequence()
+ " "
+ event.getKind();
service.write(line + detail(event).map(text -> " " + text).orElse(""));
}
detail is chapter 22's, with the parts that page points to. The workflow still lists the log and the desk among its reporters, as chapter 23 shows, the desk is chapter 11's and Notify is chapter 19's.
whatWentWrong, from chapter 25, now also names the error of a reporter that failed:
/**
* The reason of an abort, with the key that was missing or the reporter's error when that is what
* happened.
*/
static String whatWentWrong(Abort abort) {
if (abort.details() instanceof RequiredDataMissing missingData
&& missingData.missing() instanceof MissingData.Key missing) {
return abort.reason() + ": " + missing.key();
}
if (abort.details() instanceof ReporterFailed) {
return abort.reason() + ": " + abort.errorMessage();
}
return abort.reason().toString();
}
main runs two orders: Ann's, and the order of a new customer, Pat, who pays 50 for a parcel of 3 kg:
var orders =
List.of(
new NewOrder(
"A-1",
"Ann",
40,
Optional.of(10L),
new Parcel(2, List.of(30L, 20L, 10L)),
Optional.of("ann-2028"),
false),
new NewOrder("A-15", "Pat", 50, new Parcel(3, List.of(40L, 30L, 20L)), "pat-2028"));It runs them one after the other with one executor, as chapter 25's main does, and hands each result to that chapter's followUp. NewOrder is chapter 25's, and Parcel chapter 21's. It prints:
Ann's order:
log orders A-1 #1 journey_started order, customer, price, discount, parcel, card, giftCard
log orders A-1 #2 attempt_started lookup, attempt 1
log orders A-1 #3 service_provided lookup gets orderBook from ledger, a ch31_stopping_a_journey_when_its_log_fails.Ledger
the order book has no order A-1 yet
log orders A-1 #4 step_succeeded lookup
log orders A-1 #5 contribution_committed completed
log orders A-1 #6 hook_called alreadyCompleted: on step success of lookup
log orders A-1 #7 attempt_started price, attempt 1
log orders A-1 #8 step_succeeded price
log orders A-1 #9 contribution_committed amount
log orders A-1 #10 attempt_started charge, attempt 1
log orders A-1 #11 service_provided charge gets payments from bank, a ch31_stopping_a_journey_when_its_log_fails.Bank
the bank charges 30 for order A-1
log orders A-1 #12 step_succeeded charge
log orders A-1 #13 contribution_committed receipt
log orders A-1 #14 hook_called audit: on step success of charge
log orders A-1 #15 contribution_committed audit
log orders A-1 #16 attempt_started ship, attempt 1
log orders A-1 #17 input_adapter_supplied ship gets weight from parcels
log orders A-1 #18 service_provided ship gets carrier from carriers, a ch31_stopping_a_journey_when_its_log_fails.MainCarrier
the main carrier books order A-1
log orders A-1 #19 step_info ship: the parcel leaves with receipt R-A-1
log orders A-1 #20 step_succeeded ship
log orders A-1 #21 contribution_committed tracking
log orders A-1 #22 optional_input_absent code
email to Ann: Order A-1 is on its way, tracking T-A-1.
log orders A-1 #23 hook_called notify: on workflow success
log orders A-1 #24 journey_succeeded
the journey succeeded, with receipt R-A-1 and tracking T-A-1
audit: charge took 30, receipt R-A-1
Pat's order:
log orders A-15 #1 journey_started order, customer, price, parcel, card, giftCard
log orders A-15 #2 attempt_started lookup, attempt 1
log orders A-15 #3 service_provided lookup gets orderBook from ledger, a ch31_stopping_a_journey_when_its_log_fails.Ledger
the order book has no order A-15 yet
log orders A-15 #4 step_succeeded lookup
log orders A-15 #5 contribution_committed completed
log orders A-15 #6 hook_called alreadyCompleted: on step success of lookup
log orders A-15 #7 attempt_started price, attempt 1
log orders A-15 #8 optional_input_absent discount
log orders A-15 #9 step_succeeded price
log orders A-15 #10 contribution_committed amount
log orders A-15 #11 attempt_started charge, attempt 1
log orders A-15 #12 service_provided charge gets payments from bank, a ch31_stopping_a_journey_when_its_log_fails.Bank
the bank charges 50 for order A-15
the journey was aborted (reporter failed: the logging service is down)How it works
- A reporter that fails aborts the journey.
LoggingDownescaped the log'sreporton Pat'sstep_succeeded, so the executor aborted the journey, with the reasonreporter failed. A reporter can't report an error and carry on, and a journey with no record of what it did is not one the shop can trust, so it doesn't go on. - The abort is recorded at once. Nothing after it runs: the shipping never starts, so no carrier books Pat's parcel, and no workflow hook runs, so Pat is not told, as chapter 25 showed of an aborted journey.
- What the journey did stays done. The bank took Pat's 50 before the abort. No hook of
FailedShipping, from chapter 23, is called, since no shipping was given up, so nothing gives the money back. An abort is a fault to fix, as chapter 25 said: here, the shop finds Pat's payment with the bank, and refunds it before running the order again, since a new journey would charge Pat again. - The abort carries the reporter's error. For this reason, the abort's
details()is aReporterFailed, fromorg.itineradev.itinera.event, andabort.errorMessage()is the message of what the log threw, which is howwhatWentWrongprints the service's own words.abort.error()holds theLoggingDownitself, for code that needs more than its message. - Reporters should be written so that they never fail. A reporter's failure costs the whole journey, at whatever point it happens. A log that can lose its service should keep its lines, and send them once the service is back, rather than let the exception out.
Pat's journey stopped before it did anything it couldn't record. But no record holds Pat's last two events, the charge's success and the abort: the log was the only reporter that printed events, and it was the one that failed.
Outcomes
We have shown how a journey is aborted when one of its reporters fails, and what it leaves behind.
| The usual way | With Itinera |
|---|---|
| A log call that fails is often ignored, or crashes the request at whatever line made it. | A reporter that fails aborts the journey with reporter failed, at once, and the result carries its error. |
| What a request did before it crashed is often found only by reading the code that ran. | The events before the abort say what the journey did, up to the charge, and the shipping never started. |