First, I really want to understand how temporal server to handle timer tasks, Is there any material here that I can refer to or learn from? Then, if host system time is changed, what happens to running task in temporal server?
understand how temporal server to handle timer tasks
Temporal server has a timer queue processor which is used to dispatch workflow tasks to task queues. This processor is used for durable timers that get created from requests of your workers (when for example you define a workflow.sleep in your code).
Your worker(s) execute you workflow code and if it encounters apis that need to create a timer during execution it can send a “create timer” command to the server when the workflow task completes (it can send multiple commands depending on your workflow code). Server then creates a durable timer and when it fires server will place a task on the task queue which includes a timer fired event. This is then picked up by a worker (same one or could be a completely different worker), starts a new workflow task and evaluates based on updated server commands that the timer has fired and unblock the sleep/wait an continue execution of your workflow code until it blocks again when new commands need to be send to the server.
Timers are created and persisted on the server and the server keeps track of workflow execution time and the timer durations.
It’s important that you do not use system clock directly or use libs that use system time in your workflow code but instead use Temporal SDK provided apis such as
Workflow.currentTimeMillis in Java or
workflow.Now(ctx) in Go.
These do not use local system time but get the time from workflow history (eventTime of the workflow task within which the code is executed in). Hope this helps.
Thanks tihomir, now I have known that worker will use eventTime instead of local system time, but will the timer queue processor in history service be affected when system time changed? I try to read temporal server’s source code, I see many points in time are defined by current local time, so whether the trigger time of a durable timer will be changed？In addition, I see that the timer gate is used in timer queue processor, I don’ t know why need a timer gate here, what’s the function of timer gate?