Skip to content

Commit

Permalink
Randomly select exponent
Browse files Browse the repository at this point in the history
  • Loading branch information
konskov committed Aug 30, 2022
1 parent 07abe53 commit 1c5a38f
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/bgw/job_stat.c
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,19 @@ calculate_next_start_on_failure(TimestampTz finish_time, int consecutive_failure
TimestampTz res = 0;
volatile bool res_set = false;
TimestampTz last_finish = finish_time;
float8 exponent = (consecutive_failures > MAX_FAILURES_MULTIPLIER ? MAX_FAILURES_MULTIPLIER :
/* the aim of backoff is to reduce conflicts in the case of jobs that fail to launch,
* as there are jobs competing for available background workers in that case.
* therefore we introduce some randomness in backoff calculation for that type of failure
* by randomly selecting the exponent in the interval [0, consecutive_failures]
* Otherwise as soon as a worker becomes available, they'll all try to grab it, but only 1 will anyway
* so why should they all try at once? so jobs that experience failures will not only delay
* their next_start but they will also delay it in a non-deterministic way
* to reduce the probability of collisions
*/

int exponent = (consecutive_failures > MAX_FAILURES_MULTIPLIER ? MAX_FAILURES_MULTIPLIER :
consecutive_failures);
int rand_exponent = random() % exponent + 1;
MemoryContext oldctx;

if (!IS_VALID_TIMESTAMP(finish_time))
Expand Down Expand Up @@ -228,7 +239,7 @@ calculate_next_start_on_failure(TimestampTz finish_time, int consecutive_failure

/* ival = retry_period ^ (consecutive_failures - 1) */
/* arbitrarily choose 2 for exponential growth */
for (i = 0; i < exponent - 1; i++)
for (i = 0; i < (job == NULL) ? rand_exponent : exponent - 1; i++)
{
ival = DirectFunctionCall2(interval_mul, ival, Float8GetDatum(2));
}
Expand Down

0 comments on commit 1c5a38f

Please sign in to comment.