Created
January 11, 2018 07:41
-
-
Save harigist/f74b29976702a84f8f37e1bf7b509e0e to your computer and use it in GitHub Desktop.
Downgrade Retry policies in Cassandra using nodejs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* DowngradeRetryPolicy - This module is used to retry the READ / WRITE operation | |
* by downgrading the value of consistency to minimum value. | |
* @constructor | |
*/ | |
function DowngradeRetryPolicy() { | |
} | |
// Inherit the retry policy | |
util.inherits(DowngradeRetryPolicy, RetryPolicy); | |
/** | |
* In case of node unavailability, this function will be triggered and the consisteny level is updated and tried again. | |
* | |
* @param {OperationInfo} info | |
* @param {Number} consistency The [consistency]{@link module:types~consistencies} level of the query that triggered | |
* the exception. | |
* @param {Number} required The number of replicas whose response is required to achieve the | |
* required [consistency]{@link module:types~consistencies}. | |
* @param {Number} alive The number of replicas that were known to be alive when the request had been processed | |
* (since an unavailable exception has been triggered, there will be alive < required) | |
* | |
* @returns {DecisionInfo} | |
*/ | |
DowngradeRetryPolicy.prototype.onUnavailable = function (info, consistency, required, alive) { | |
console.log('consistency',consistency); | |
// Initialization | |
var currentRetry = info.nbRetry; | |
var totalRetry = 2; | |
var minimumConsistency = 1; | |
console.log('OnUnavailable - retry : ', currentRetry); | |
console.log('Total Retries Made : ', currentRetry); | |
console.log('Total number of Replicas alive : ', alive); | |
if (currentRetry > totalRetry) { | |
return this.rethrowResult(); | |
} else if(alive == minimumConsistency) { | |
console.log('Retries with Minimum consistency', minimumConsistency); | |
return this.retryResult(minimumConsistency, true); | |
} | |
}; | |
/** | |
* On Read Timeout, this function will check whether to reduce the consistency level and retry. | |
*/ | |
DowngradeRetryPolicy.prototype.onReadTimeout = function (info, consistency, received, blockFor, isDataPresent) { | |
// Initialization | |
var totalRetry = 2; | |
var currentRetry = info.nbRetry; | |
var minimumConsistency = 1; | |
console.log('OnRead Timeout - retry :', currentRetry); | |
if (currentRetry > totalRetry) { | |
return this.rethrowResult(); | |
} else if (received == minimumConsistency) { | |
// Retries with the minimum consistency | |
console.log('Retries with Minimum consistency', minimumConsistency); | |
return this.retryResult(minimumConsistency, true); | |
} | |
}; | |
/** | |
* On Write Timeout, this function will check whether to reduce the consistency level and retry. | |
*/ | |
DowngradeRetryPolicy.prototype.onWriteTimeout = function (info, consistency, received, blockFor, writeType) { | |
// Initializatio | |
var totalRetry = 2; | |
var currentRetry = info.nbRetry; | |
var minimumConsistency = 1; | |
console.log('OnWrite Timeout - retry : ', currentRetry); | |
if (currentRetry > totalRetry) { | |
return this.rethrowResult(); | |
} else if (received == minimumConsistency) { | |
// Retries with the minimum consistency | |
console.log('Retries with Minimum consistency', minimumConsistency); | |
return this.retryResult(minimumConsistency, true); | |
} | |
}; | |
/** | |
* Returns a {@link DecisionInfo} to callback in error when a err is obtained for a given request. | |
* @returns {DecisionInfo} | |
*/ | |
DowngradeRetryPolicy.prototype.rethrowResult = function () { | |
return { decision: DowngradeRetryPolicy.retryDecision.rethrow }; | |
}; | |
/** | |
* Method which frame the Retry Policy paramters. | |
*/ | |
DowngradeRetryPolicy.prototype.retryResult = function (consistency, useCurrentHost) { | |
console.log('Retry Result consistency : ' + consistency); | |
console.log('current host : ', useCurrentHost); | |
return { | |
decision: RetryPolicy.retryDecision.retry, | |
consistency: consistency, | |
useCurrentHost: useCurrentHost !== false | |
}; | |
}; | |
/** | |
* Determines the retry decision for the retry policies. | |
* @type {Object} | |
* @property {Number} rethrow | |
* @property {Number} retry | |
* @property {Number} ignore | |
* @static | |
*/ | |
DowngradeRetryPolicy.retryDecision = { | |
rethrow: 0, | |
retry: 1, | |
ignore: 2 | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment