17

If there are more than one available runner for a project, how does gitlab ci decide which runner to use?

I have an omnibus gitlab 8.6.6-ee installation, with 2 runners configured. The runners are identical (docker images, config, etc) except that they are running on different computers.

If they are both idle and a job comes in that either of them could run, which one will run?

BM5k
  • 1,210
  • 10
  • 28
  • Why not create a test project and run a few builds? I would imagine it picks the first available runner, and if multiple runners are idle it picks randomly. – BrokenBinary Apr 18 '16 at 22:22
  • For my use case, I created a new runner for the project and disabled the shared runners so it will always use what I want. But there is also an option for "tags" in Gitlab so a project will use only specific runners with that tag which you would like to check out. – Macindows Nov 27 '19 at 22:00

2 Answers2

9

To add to Rubinum's answer the 'first' runner would be whichever runner that checks in first that meets all criteria. For example, labels could limit which runners certain jobs run on.

Runners query the gitlab server every X seconds to check if there are builds. if there's a build queued and multiple meet criteria, the first to ask will win

Update to answer comments:

Runners communicate through the CI API http://docs.gitlab.com/ce/ci/api/builds.html to get build status. This will eventually imply that it will become a more or less random choosing of the runner based on when it finished the last job and the xamount of msit is waiting to check.

To completely answer the question:

Credit goes to BM5k after digging through the code and finding that x = 3 seconds based on this and this. Also found that:

which machine a docker+machine runner will use once that runner has been selected) reveals that the machine selection is more or less (effectively) random as well

Community
  • 1
  • 1
Jose Torres
  • 1,025
  • 11
  • 21
  • Interesting. Do you know where this happens in the code? – BM5k May 16 '16 at 22:18
  • And, to verify that I understand you correctly, you're saying the server doesn't really "decide" which runner to use, it just randomly hands jobs to the next runner that polls (assuming that runner matches all of the job requirements). – BM5k May 16 '16 at 22:24
  • Edited answer to fit the previous comments. I haven't found where the `x`amount is set in the code though but this is the project where it is being configured https://gitlab.com/gitlab-org/gitlab-ci-multi-runner/tree/master. – Jose Torres May 17 '16 at 00:13
  • Your updated answer & comment gave me enough of a starting point to be able to track this through the code (with a bunch of assumptions on my part since I don't know go). It looks like x = 3 seconds based on [this](https://gitlab.com/gitlab-org/gitlab-ci-multi-runner/blob/master/commands/multi.go#L95) and [this](https://gitlab.com/gitlab-org/gitlab-ci-multi-runner/blob/master/common/consts.go#L7). – BM5k May 17 '16 at 04:20
  • Continuing down this rabbit hole (trying to find out which machine a docker+machine runner will use once that runner has been selected) reveals that the [machine selection](https://gitlab.com/gitlab-org/gitlab-ci-multi-runner/blob/master/executors/docker/machine/provider.go#L79-97) is more or less (effectively) random as well. – BM5k May 17 '16 at 04:50
  • Excellent work on getting that info from those links, I'll add them on the answer for future ref. Glad you can't aware the bounty to yourself ;) – Jose Torres May 17 '16 at 05:19
  • Interesting that it is the runner which checks for jobs. It could be nice if it was possible to increase x = 3 seconds if the load of a runnger rises. In that way we would get a load balancing. – Kjeld Flarup May 19 '22 at 13:27
3

Gitlab CI assign jobs to those runners which are available. If a runner is not availble because of beeing busy then Gitlab CI assign the job to other runners which are available. In your case it will always assign the jobs to the first runner (whoever it is).

If you want to specify the execution on a specific runner/mashine then have a look at my post here.

In my opinion its good to not know which runner will run your build if you are using docker because thats the benefit of the runner/docker archeticture.

Community
  • 1
  • 1
Rubinum
  • 547
  • 3
  • 18