Skip to content

Commit

Permalink
Changed the jitter rule, and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-ext-simba-jy committed Oct 26, 2023
1 parent 6dd5dcf commit dc33cef
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 27 deletions.
2 changes: 1 addition & 1 deletion lib/connection/connection_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -982,7 +982,7 @@ function createParameters()
},
{
name: PARAM_RETRY_SF_MAX_SLEEP_TIME,
defaultValue: 108,
defaultValue: 16,
validate: isNonNegativeNumber
}
];
Expand Down
15 changes: 10 additions & 5 deletions lib/services/sf.js
Original file line number Diff line number Diff line change
Expand Up @@ -1180,10 +1180,10 @@ StateConnecting.prototype.continue = function ()
Date.now() : 'FIXEDTIMESTAMP';
const maxLoginRetries = connectionConfig.getRetrySfMaxLoginRetries();
const maxLoginTimeout = connectionConfig.getLoginTimeout();
let sleep = connectionConfig.getRetrySfStartingSleepTime();
let totalTimeout = sleep;
const cap = connectionConfig.getRetrySfMaxSleepTime();
Logger.getInstance().debug("Retry Max sleep time = " + cap);
const base = connectionConfig.getRetrySfStartingSleepTime();
let sleep = base;
let totalTimeout = base;
Logger.getInstance().debug("Total loginTimeout is for the retries = " + maxLoginTimeout);
const parent = this;
const requestCallback = function (err, body)
{
Expand Down Expand Up @@ -1217,9 +1217,14 @@ StateConnecting.prototype.continue = function ()
totalTimeout < maxLoginTimeout)
{
numRetries++;
const jitter = Util.jitteredSleepTime(numRetries, sleep, totalTimeout, maxLoginTimeout);
const jitter = Util.jitteredSleepTime(numRetries, sleep, totalTimeout, base, maxLoginTimeout);
sleep = jitter.sleep;
totalTimeout = jitter.totalTimeout;

if(sleep <= 0) {
Logger.getInstance().debug("Reached out to the Login Timeout");
parent.snowflakeService.transitionToDisconnected();
}
setTimeout(sendRequest, sleep * 1000);
return;
}
Expand Down
23 changes: 13 additions & 10 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -426,14 +426,14 @@ exports.nextSleepTime = function (
*
* @param {Number} numofRetries
* @param {Number} currentSleepTime
* @param {Number} maxSleepTime
* @param {Number} totalTimeout
* @param {Number} base
* @param {Number} maxLoginTimeout
* @returns {JSON} return next sleep Time and totalTime.
*/
exports.jitteredSleepTime = function (numofRetries, currentSleepTime, totalTimeout, maxLoginTimeout) {
const nextSleepTime = Math.min(maxLoginTimeout - totalTimeout, 4 * numofRetries);
const sleep = nextSleepTime + getJitter(currentSleepTime);
exports.jitteredSleepTime = function (numofRetries, currentSleepTime, totalTimeout, base, maxLoginTimeout) {
const sleep = Math.min((maxLoginTimeout - totalTimeout), getNextSleepTime(numofRetries, base, currentSleepTime) );
totalTimeout += sleep

return {sleep, totalTimeout}
}

Expand All @@ -455,14 +455,17 @@ exports.chooseRandom = chooseRandom;

/**
* return the jitter value.
*
* @param {Number} curWaitTime
* @param {Number} numofRetries
* @param {Number} base
* @param {Number} currentSleepTime
* @returns {Boolean} return jitter.
*/
function getJitter (curWaitTime) {
function getNextSleepTime (numofRetries, base, currentSleepTime) {
const multiplicationFactor = chooseRandom(1, -1);
const jitterAmount = 0.5 * curWaitTime * multiplicationFactor;
return jitterAmount;
const nextSleep = (base * (2 ** (numofRetries - 1)));
const jitterAmount = 0.5 * currentSleepTime * multiplicationFactor;

return nextSleep + jitterAmount;
}

/**
Expand Down
18 changes: 7 additions & 11 deletions test/unit/util_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -600,31 +600,27 @@ describe('Util', function ()
retry403: false,
isRetryable: true,
},
{
statusCode: 525,
retry403: false,
isRetryable: true,
},
];

const maxLoginTimeout = 300;
let currentSleepTime = 4;
const base = 4;
let currentSleepTime = base;
let retryCount = 1;
let totalTimeout = 1;
let totalTimeout = base;
for (const response of errorCodes) {
retryCount++;
assert.strictEqual(Util.isRetryableHttpError(response,true), true);

const result = Util.jitteredSleepTime(retryCount, currentSleepTime, totalTimeout, maxLoginTimeout);
const result = Util.jitteredSleepTime(retryCount, currentSleepTime, totalTimeout, base, maxLoginTimeout);
const jitter = currentSleepTime / 2
const nextSleep = 2 ** retryCount;
const nextSleep = base * (2 ** (retryCount-1));
currentSleepTime = result.sleep;
totalTimeout = result.totalTimeout;

assert.ok(currentSleepTime <= nextSleep + jitter || currentSleepTime >= nextSleep - jitter)
retryCount++;
}

assert.strictEqual(retryCount, 8);
assert.strictEqual(retryCount, 7);
assert.ok(totalTimeout <= maxLoginTimeout);
});

Expand Down

0 comments on commit dc33cef

Please sign in to comment.