Description
This looks familiar with #24543, but it's quite different.
Background
Gitea will start a transaction to find and assign a task to the runner when it requests a new one. However, we know that there may not be a task available most of the time, so Gitea has to roll back the transaction and respond with "no task yet, try again later."
Starting a transaction is an expensive operation. However, we don't have to do it every time.
Solution
Record a version number for table of Actions jobs
PS: You might wonder why it's not "table of Actions tasks". Never mind, just a few implementation details.
Increase the version number once the data table changes. The number can be stored in DB and cached in memory.
When Gitea receives a request for a task, it compares the version number in the request with the current version number. If they are equal, it responds with "no task available yet".
Otherwise, start a transaction to query and assign a task.
Gitea responds with the current version number regardless of whether there is a task available.
Runners fetch tasks with the version number
- Fetch tasks with
0
version number when it’s the first time. - Fetch tasks with the responded version number when it didn't get a task last time.
- Fetch tasks with
0
version number when it got a task last time.
Here's a simulation:
Gitea | Runner | |
---|---|---|
← 0 | ||
13 != 0, query db | → 13, no task | |
← 13 | ||
13 == 13, skip query | → 13, no task | |
← 13 | ||
13 != 14, query db | → 14, a task | |
← 0 | ||
14 != 0, query db | → 14, no task | |
← 14 | ||
14 == 14, skip query | → 14, no task |
Advantages
- Easy to implement. No need to modify existing mechanisms.
- Compatible with other improvement measures.