1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | import {
|
19 | LoadBalancer,
|
20 | ChannelControlHelper,
|
21 | LoadBalancingConfig,
|
22 | registerLoadBalancerType,
|
23 | } from './load-balancer';
|
24 | import { ConnectivityState } from './connectivity-state';
|
25 | import {
|
26 | QueuePicker,
|
27 | Picker,
|
28 | PickArgs,
|
29 | CompletePickResult,
|
30 | PickResultType,
|
31 | UnavailablePicker,
|
32 | } from './picker';
|
33 | import {
|
34 | SubchannelAddress,
|
35 | subchannelAddressToString,
|
36 | } from './subchannel-address';
|
37 | import * as logging from './logging';
|
38 | import { LogVerbosity } from './constants';
|
39 | import {
|
40 | ConnectivityStateListener,
|
41 | SubchannelInterface,
|
42 | } from './subchannel-interface';
|
43 |
|
44 | const TRACER_NAME = 'round_robin';
|
45 |
|
46 | function trace(text: string): void {
|
47 | logging.trace(LogVerbosity.DEBUG, TRACER_NAME, text);
|
48 | }
|
49 |
|
50 | const TYPE_NAME = 'round_robin';
|
51 |
|
52 | class RoundRobinLoadBalancingConfig implements LoadBalancingConfig {
|
53 | getLoadBalancerName(): string {
|
54 | return TYPE_NAME;
|
55 | }
|
56 |
|
57 | constructor() {}
|
58 |
|
59 | toJsonObject(): object {
|
60 | return {
|
61 | [TYPE_NAME]: {},
|
62 | };
|
63 | }
|
64 |
|
65 |
|
66 | static createFromJson(obj: any) {
|
67 | return new RoundRobinLoadBalancingConfig();
|
68 | }
|
69 | }
|
70 |
|
71 | class RoundRobinPicker implements Picker {
|
72 | constructor(
|
73 | private readonly subchannelList: SubchannelInterface[],
|
74 | private nextIndex = 0
|
75 | ) {}
|
76 |
|
77 | pick(pickArgs: PickArgs): CompletePickResult {
|
78 | const pickedSubchannel = this.subchannelList[this.nextIndex];
|
79 | this.nextIndex = (this.nextIndex + 1) % this.subchannelList.length;
|
80 | return {
|
81 | pickResultType: PickResultType.COMPLETE,
|
82 | subchannel: pickedSubchannel,
|
83 | status: null,
|
84 | onCallStarted: null,
|
85 | onCallEnded: null,
|
86 | };
|
87 | }
|
88 |
|
89 | |
90 |
|
91 |
|
92 |
|
93 |
|
94 | peekNextSubchannel(): SubchannelInterface {
|
95 | return this.subchannelList[this.nextIndex];
|
96 | }
|
97 | }
|
98 |
|
99 | export class RoundRobinLoadBalancer implements LoadBalancer {
|
100 | private subchannels: SubchannelInterface[] = [];
|
101 |
|
102 | private currentState: ConnectivityState = ConnectivityState.IDLE;
|
103 |
|
104 | private subchannelStateListener: ConnectivityStateListener;
|
105 |
|
106 | private currentReadyPicker: RoundRobinPicker | null = null;
|
107 |
|
108 | constructor(private readonly channelControlHelper: ChannelControlHelper) {
|
109 | this.subchannelStateListener = (
|
110 | subchannel: SubchannelInterface,
|
111 | previousState: ConnectivityState,
|
112 | newState: ConnectivityState
|
113 | ) => {
|
114 | this.calculateAndUpdateState();
|
115 |
|
116 | if (
|
117 | newState === ConnectivityState.TRANSIENT_FAILURE ||
|
118 | newState === ConnectivityState.IDLE
|
119 | ) {
|
120 | this.channelControlHelper.requestReresolution();
|
121 | subchannel.startConnecting();
|
122 | }
|
123 | };
|
124 | }
|
125 |
|
126 | private countSubchannelsWithState(state: ConnectivityState) {
|
127 | return this.subchannels.filter(
|
128 | subchannel => subchannel.getConnectivityState() === state
|
129 | ).length;
|
130 | }
|
131 |
|
132 | private calculateAndUpdateState() {
|
133 | if (this.countSubchannelsWithState(ConnectivityState.READY) > 0) {
|
134 | const readySubchannels = this.subchannels.filter(
|
135 | subchannel =>
|
136 | subchannel.getConnectivityState() === ConnectivityState.READY
|
137 | );
|
138 | let index = 0;
|
139 | if (this.currentReadyPicker !== null) {
|
140 | index = readySubchannels.indexOf(
|
141 | this.currentReadyPicker.peekNextSubchannel()
|
142 | );
|
143 | if (index < 0) {
|
144 | index = 0;
|
145 | }
|
146 | }
|
147 | this.updateState(
|
148 | ConnectivityState.READY,
|
149 | new RoundRobinPicker(readySubchannels, index)
|
150 | );
|
151 | } else if (
|
152 | this.countSubchannelsWithState(ConnectivityState.CONNECTING) > 0
|
153 | ) {
|
154 | this.updateState(ConnectivityState.CONNECTING, new QueuePicker(this));
|
155 | } else if (
|
156 | this.countSubchannelsWithState(ConnectivityState.TRANSIENT_FAILURE) > 0
|
157 | ) {
|
158 | this.updateState(
|
159 | ConnectivityState.TRANSIENT_FAILURE,
|
160 | new UnavailablePicker()
|
161 | );
|
162 | } else {
|
163 | this.updateState(ConnectivityState.IDLE, new QueuePicker(this));
|
164 | }
|
165 | }
|
166 |
|
167 | private updateState(newState: ConnectivityState, picker: Picker) {
|
168 | trace(
|
169 | ConnectivityState[this.currentState] +
|
170 | ' -> ' +
|
171 | ConnectivityState[newState]
|
172 | );
|
173 | if (newState === ConnectivityState.READY) {
|
174 | this.currentReadyPicker = picker as RoundRobinPicker;
|
175 | } else {
|
176 | this.currentReadyPicker = null;
|
177 | }
|
178 | this.currentState = newState;
|
179 | this.channelControlHelper.updateState(newState, picker);
|
180 | }
|
181 |
|
182 | private resetSubchannelList() {
|
183 | for (const subchannel of this.subchannels) {
|
184 | subchannel.removeConnectivityStateListener(this.subchannelStateListener);
|
185 | subchannel.unref();
|
186 | this.channelControlHelper.removeChannelzChild(
|
187 | subchannel.getChannelzRef()
|
188 | );
|
189 | }
|
190 | this.subchannels = [];
|
191 | }
|
192 |
|
193 | updateAddressList(
|
194 | addressList: SubchannelAddress[],
|
195 | lbConfig: LoadBalancingConfig
|
196 | ): void {
|
197 | this.resetSubchannelList();
|
198 | trace(
|
199 | 'Connect to address list ' +
|
200 | addressList.map(address => subchannelAddressToString(address))
|
201 | );
|
202 | this.subchannels = addressList.map(address =>
|
203 | this.channelControlHelper.createSubchannel(address, {})
|
204 | );
|
205 | for (const subchannel of this.subchannels) {
|
206 | subchannel.ref();
|
207 | subchannel.addConnectivityStateListener(this.subchannelStateListener);
|
208 | this.channelControlHelper.addChannelzChild(subchannel.getChannelzRef());
|
209 | const subchannelState = subchannel.getConnectivityState();
|
210 | if (
|
211 | subchannelState === ConnectivityState.IDLE ||
|
212 | subchannelState === ConnectivityState.TRANSIENT_FAILURE
|
213 | ) {
|
214 | subchannel.startConnecting();
|
215 | }
|
216 | }
|
217 | this.calculateAndUpdateState();
|
218 | }
|
219 |
|
220 | exitIdle(): void {
|
221 | for (const subchannel of this.subchannels) {
|
222 | subchannel.startConnecting();
|
223 | }
|
224 | }
|
225 | resetBackoff(): void {
|
226 | |
227 |
|
228 | }
|
229 | destroy(): void {
|
230 | this.resetSubchannelList();
|
231 | }
|
232 | getTypeName(): string {
|
233 | return TYPE_NAME;
|
234 | }
|
235 | }
|
236 |
|
237 | export function setup() {
|
238 | registerLoadBalancerType(
|
239 | TYPE_NAME,
|
240 | RoundRobinLoadBalancer,
|
241 | RoundRobinLoadBalancingConfig
|
242 | );
|
243 | }
|