load-balancer-child-handler.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. "use strict";
  2. /*
  3. * Copyright 2020 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. Object.defineProperty(exports, "__esModule", { value: true });
  19. exports.ChildLoadBalancerHandler = void 0;
  20. const load_balancer_1 = require("./load-balancer");
  21. const connectivity_state_1 = require("./connectivity-state");
  22. const TYPE_NAME = 'child_load_balancer_helper';
  23. class ChildLoadBalancerHandler {
  24. constructor(channelControlHelper, credentials, options) {
  25. this.channelControlHelper = channelControlHelper;
  26. this.credentials = credentials;
  27. this.options = options;
  28. this.currentChild = null;
  29. this.pendingChild = null;
  30. this.latestConfig = null;
  31. this.ChildPolicyHelper = class {
  32. constructor(parent) {
  33. this.parent = parent;
  34. this.child = null;
  35. }
  36. createSubchannel(subchannelAddress, subchannelArgs, credentialsOverride) {
  37. return this.parent.channelControlHelper.createSubchannel(subchannelAddress, subchannelArgs, credentialsOverride);
  38. }
  39. updateState(connectivityState, picker) {
  40. var _a;
  41. if (this.calledByPendingChild()) {
  42. if (connectivityState === connectivity_state_1.ConnectivityState.CONNECTING) {
  43. return;
  44. }
  45. (_a = this.parent.currentChild) === null || _a === void 0 ? void 0 : _a.destroy();
  46. this.parent.currentChild = this.parent.pendingChild;
  47. this.parent.pendingChild = null;
  48. }
  49. else if (!this.calledByCurrentChild()) {
  50. return;
  51. }
  52. this.parent.channelControlHelper.updateState(connectivityState, picker);
  53. }
  54. requestReresolution() {
  55. var _a;
  56. const latestChild = (_a = this.parent.pendingChild) !== null && _a !== void 0 ? _a : this.parent.currentChild;
  57. if (this.child === latestChild) {
  58. this.parent.channelControlHelper.requestReresolution();
  59. }
  60. }
  61. setChild(newChild) {
  62. this.child = newChild;
  63. }
  64. addChannelzChild(child) {
  65. this.parent.channelControlHelper.addChannelzChild(child);
  66. }
  67. removeChannelzChild(child) {
  68. this.parent.channelControlHelper.removeChannelzChild(child);
  69. }
  70. calledByPendingChild() {
  71. return this.child === this.parent.pendingChild;
  72. }
  73. calledByCurrentChild() {
  74. return this.child === this.parent.currentChild;
  75. }
  76. };
  77. }
  78. configUpdateRequiresNewPolicyInstance(oldConfig, newConfig) {
  79. return oldConfig.getLoadBalancerName() !== newConfig.getLoadBalancerName();
  80. }
  81. /**
  82. * Prerequisites: lbConfig !== null and lbConfig.name is registered
  83. * @param endpointList
  84. * @param lbConfig
  85. * @param attributes
  86. */
  87. updateAddressList(endpointList, lbConfig, attributes) {
  88. let childToUpdate;
  89. if (this.currentChild === null ||
  90. this.latestConfig === null ||
  91. this.configUpdateRequiresNewPolicyInstance(this.latestConfig, lbConfig)) {
  92. const newHelper = new this.ChildPolicyHelper(this);
  93. const newChild = (0, load_balancer_1.createLoadBalancer)(lbConfig, newHelper, this.credentials, this.options);
  94. newHelper.setChild(newChild);
  95. if (this.currentChild === null) {
  96. this.currentChild = newChild;
  97. childToUpdate = this.currentChild;
  98. }
  99. else {
  100. if (this.pendingChild) {
  101. this.pendingChild.destroy();
  102. }
  103. this.pendingChild = newChild;
  104. childToUpdate = this.pendingChild;
  105. }
  106. }
  107. else {
  108. if (this.pendingChild === null) {
  109. childToUpdate = this.currentChild;
  110. }
  111. else {
  112. childToUpdate = this.pendingChild;
  113. }
  114. }
  115. this.latestConfig = lbConfig;
  116. childToUpdate.updateAddressList(endpointList, lbConfig, attributes);
  117. }
  118. exitIdle() {
  119. if (this.currentChild) {
  120. this.currentChild.exitIdle();
  121. if (this.pendingChild) {
  122. this.pendingChild.exitIdle();
  123. }
  124. }
  125. }
  126. resetBackoff() {
  127. if (this.currentChild) {
  128. this.currentChild.resetBackoff();
  129. if (this.pendingChild) {
  130. this.pendingChild.resetBackoff();
  131. }
  132. }
  133. }
  134. destroy() {
  135. /* Note: state updates are only propagated from the child balancer if that
  136. * object is equal to this.currentChild or this.pendingChild. Since this
  137. * function sets both of those to null, no further state updates will
  138. * occur after this function returns. */
  139. if (this.currentChild) {
  140. this.currentChild.destroy();
  141. this.currentChild = null;
  142. }
  143. if (this.pendingChild) {
  144. this.pendingChild.destroy();
  145. this.pendingChild = null;
  146. }
  147. }
  148. getTypeName() {
  149. return TYPE_NAME;
  150. }
  151. }
  152. exports.ChildLoadBalancerHandler = ChildLoadBalancerHandler;
  153. //# sourceMappingURL=load-balancer-child-handler.js.map