Guest User

Untitled

a guest
Jan 20th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. /**
  2. * DowngradeRetryPolicy - This module is used to retry the READ / WRITE operation
  3. * by downgrading the value of consistency to minimum value.
  4. * @constructor
  5. */
  6. function DowngradeRetryPolicy() {
  7. }
  8.  
  9. // Inherit the retry policy
  10. util.inherits(DowngradeRetryPolicy, RetryPolicy);
  11.  
  12. /**
  13. * In case of node unavailability, this function will be triggered and the consisteny level is updated and tried again.
  14. *
  15. * @param {OperationInfo} info
  16. * @param {Number} consistency The [consistency]{@link module:types~consistencies} level of the query that triggered
  17. * the exception.
  18. * @param {Number} required The number of replicas whose response is required to achieve the
  19. * required [consistency]{@link module:types~consistencies}.
  20. * @param {Number} alive The number of replicas that were known to be alive when the request had been processed
  21. * (since an unavailable exception has been triggered, there will be alive < required)
  22. *
  23. * @returns {DecisionInfo}
  24. */
  25. DowngradeRetryPolicy.prototype.onUnavailable = function (info, consistency, required, alive) {
  26. console.log('consistency',consistency);
  27.  
  28. // Initialization
  29. var currentRetry = info.nbRetry;
  30. var totalRetry = 2;
  31. var minimumConsistency = 1;
  32.  
  33. console.log('OnUnavailable - retry : ', currentRetry);
  34. console.log('Total Retries Made : ', currentRetry);
  35. console.log('Total number of Replicas alive : ', alive);
  36.  
  37. if (currentRetry > totalRetry) {
  38. return this.rethrowResult();
  39. } else if(alive == minimumConsistency) {
  40. console.log('Retries with Minimum consistency', minimumConsistency);
  41. return this.retryResult(minimumConsistency, true);
  42. }
  43.  
  44. };
  45.  
  46. /**
  47. * On Read Timeout, this function will check whether to reduce the consistency level and retry.
  48. */
  49. DowngradeRetryPolicy.prototype.onReadTimeout = function (info, consistency, received, blockFor, isDataPresent) {
  50.  
  51. // Initialization
  52. var totalRetry = 2;
  53. var currentRetry = info.nbRetry;
  54. var minimumConsistency = 1;
  55.  
  56. console.log('OnRead Timeout - retry :', currentRetry);
  57.  
  58. if (currentRetry > totalRetry) {
  59. return this.rethrowResult();
  60. } else if (received == minimumConsistency) {
  61. // Retries with the minimum consistency
  62. console.log('Retries with Minimum consistency', minimumConsistency);
  63. return this.retryResult(minimumConsistency, true);
  64. }
  65. };
  66.  
  67. /**
  68. * On Write Timeout, this function will check whether to reduce the consistency level and retry.
  69. */
  70. DowngradeRetryPolicy.prototype.onWriteTimeout = function (info, consistency, received, blockFor, writeType) {
  71. // Initializatio
  72. var totalRetry = 2;
  73. var currentRetry = info.nbRetry;
  74. var minimumConsistency = 1;
  75.  
  76. console.log('OnWrite Timeout - retry : ', currentRetry);
  77.  
  78. if (currentRetry > totalRetry) {
  79. return this.rethrowResult();
  80. } else if (received == minimumConsistency) {
  81. // Retries with the minimum consistency
  82. console.log('Retries with Minimum consistency', minimumConsistency);
  83. return this.retryResult(minimumConsistency, true);
  84. }
  85. };
  86.  
  87. /**
  88. * Returns a {@link DecisionInfo} to callback in error when a err is obtained for a given request.
  89. * @returns {DecisionInfo}
  90. */
  91. DowngradeRetryPolicy.prototype.rethrowResult = function () {
  92. return { decision: DowngradeRetryPolicy.retryDecision.rethrow };
  93. };
  94.  
  95. /**
  96. * Method which frame the Retry Policy paramters.
  97. */
  98. DowngradeRetryPolicy.prototype.retryResult = function (consistency, useCurrentHost) {
  99. console.log('Retry Result consistency : ' + consistency);
  100. console.log('current host : ', useCurrentHost);
  101. return {
  102. decision: RetryPolicy.retryDecision.retry,
  103. consistency: consistency,
  104. useCurrentHost: useCurrentHost !== false
  105. };
  106. };
  107.  
  108. /**
  109. * Determines the retry decision for the retry policies.
  110. * @type {Object}
  111. * @property {Number} rethrow
  112. * @property {Number} retry
  113. * @property {Number} ignore
  114. * @static
  115. */
  116. DowngradeRetryPolicy.retryDecision = {
  117. rethrow: 0,
  118. retry: 1,
  119. ignore: 2
  120. };
Add Comment
Please, Sign In to add comment