How does history replay works in cadence?

501 Views Asked by At

How does history replay work in cadence?

I have a workflow which calls two activity sequentially.

Say, the first activity got completed and the second has 100 no of lines of code. If the app server restarts when executing the 50th line of the code in activity2, is it exactly starts the execution from the 50th line. If yes, what magic is happening inside cadence?

@Override
    public String composeGreeting(String greeting, String name) throws Exception {
      FileWriter fw =
          new FileWriter(
              "/Users/kumble-004/Documents/Uber_Cadence/Sample_Projects/TestCadence/src/com/company/"+name+".txt");

      System.out.println(
          DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss").format(LocalDateTime.now())
              + " [Activity] started");

      long time = System.currentTimeMillis() + 240000;
      int i = 0, j=1;

      while (System.currentTimeMillis() != time) {
        if(i++ %10000000 == 0) {
          fw.write("print - " + j++ + " " +
                  DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss").format(LocalDateTime.now()) +"\n");
        }
      }
      fw.close();
      System.out.println(
          DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss").format(LocalDateTime.now())
              + " [Activity] ended");

      return greeting + " " + name + "!";
    }
  }

I have the above code in my hello activity. this code will run for 4 minutes and it will be writing data in a file when a condition meets

I started a workflow and have quit the cadence server after printing [Activity] started. I didn't start it just stops it. But after 4 minutes it is exactly printing [Activity] Ended in the console. I am wondering how is this possible because I stop the server but code is executing, data is writtening in file.

While I am checking it via cadence UI it shows that the last history is ActivityTaskStarted. And I started my server. After 15 mins(beacuse scheduleToCloseTimeoutSeconds is 15 mins) Activity returns with event ActivityTaskTimedOut and the whole whorkflow has failed due to this timeout.

Kindly explain what is happening when restarting cadence server ?

1

There are 1 best solutions below

0
On BEST ANSWER

If the app server restarts when executing the 50th line of the code in activity2, is it exactly starts the execution from the 50th line

No, it will not resume from 50th line of activity automatically for you.

Replay is only happening for Workflow. It is relaying on History to replay and rebuild the memory stack. Everything happens in the workflow is stored in the history:

  • Every step in the workflow code, generates a bunch of results called "Decision"
  • Activity/ChildWorkflow results
  • External events like Signals
  • Timers
  • Etc.

For more details, please refer to the doc about replay history and What exactly is a Cadence decision task?

But after 4 minutes it is exactly printing [Activity] Ended in the console. I am wondering how is this possible because I stop the server but code is executing, data is writtening in file.

That's because your activity worker is still running. The code you are running is purely activity code.

However, it activity results will not be able reported to server when the server is down. Which means the history will lose it and workflow may reschedule another activity(if retry is enabled).

Please refer to the doc about activity timeout and retry