-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathGcpMultiEndpointChannel.java
More file actions
857 lines (796 loc) · 33.7 KB
/
GcpMultiEndpointChannel.java
File metadata and controls
857 lines (796 loc) · 33.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
/*
* Copyright 2022 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.grpc;
import static com.google.cloud.grpc.GcpMetricsConstants.COUNT;
import static com.google.cloud.grpc.GcpMetricsConstants.ENDPOINT_LABEL;
import static com.google.cloud.grpc.GcpMetricsConstants.ENDPOINT_LABEL_DESC;
import static com.google.cloud.grpc.GcpMetricsConstants.METRIC_CURRENT_ENDPOINT;
import static com.google.cloud.grpc.GcpMetricsConstants.METRIC_ENDPOINT_STATE;
import static com.google.cloud.grpc.GcpMetricsConstants.METRIC_ENDPOINT_SWITCH;
import static com.google.cloud.grpc.GcpMetricsConstants.ME_NAME_LABEL;
import static com.google.cloud.grpc.GcpMetricsConstants.ME_NAME_LABEL_DESC;
import static com.google.cloud.grpc.GcpMetricsConstants.STATUS_AVAILABLE;
import static com.google.cloud.grpc.GcpMetricsConstants.STATUS_LABEL;
import static com.google.cloud.grpc.GcpMetricsConstants.STATUS_LABEL_DESC;
import static com.google.cloud.grpc.GcpMetricsConstants.STATUS_UNAVAILABLE;
import static com.google.cloud.grpc.GcpMetricsConstants.SWITCH_TYPE_LABEL;
import static com.google.cloud.grpc.GcpMetricsConstants.SWITCH_TYPE_LABEL_DESC;
import static com.google.cloud.grpc.GcpMetricsConstants.TYPE_FALLBACK;
import static com.google.cloud.grpc.GcpMetricsConstants.TYPE_RECOVER;
import static com.google.cloud.grpc.GcpMetricsConstants.TYPE_REPLACE;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.NANOSECONDS;
import com.google.cloud.grpc.GcpManagedChannelOptions.GcpChannelPoolOptions;
import com.google.cloud.grpc.GcpManagedChannelOptions.GcpMetricsOptions;
import com.google.cloud.grpc.multiendpoint.MultiEndpoint;
import com.google.cloud.grpc.proto.ApiConfig;
import com.google.common.base.Preconditions;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.concurrent.GuardedBy;
import io.grpc.CallOptions;
import io.grpc.ClientCall;
import io.grpc.ClientCall.Listener;
import io.grpc.ConnectivityState;
import io.grpc.Context;
import io.grpc.Grpc;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.Metadata;
import io.grpc.MethodDescriptor;
import io.opencensus.metrics.DerivedLongCumulative;
import io.opencensus.metrics.DerivedLongGauge;
import io.opencensus.metrics.LabelKey;
import io.opencensus.metrics.LabelValue;
import io.opencensus.metrics.MetricOptions;
import io.opencensus.metrics.MetricRegistry;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.api.metrics.Meter;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
/**
* The purpose of GcpMultiEndpointChannel is twofold:
*
* <ol>
* <li>Fallback to an alternative endpoint (host:port) of a gRPC service when the original
* endpoint is completely unavailable.
* <li>Be able to route an RPC call to a specific group of endpoints.
* </ol>
*
* <p>A group of endpoints is called a {@link MultiEndpoint} and is essentially a list of endpoints
* where priority is defined by the position in the list with the first endpoint having top
* priority. A MultiEndpoint tracks endpoints' availability. When a MultiEndpoint is picked for an
* RPC call, it picks the top priority endpoint that is currently available. More information on the
* {@link MultiEndpoint} class.
*
* <p>GcpMultiEndpointChannel can have one or more MultiEndpoint identified by its name -- arbitrary
* string provided in the {@link GcpMultiEndpointOptions} when configuring MultiEndpoints. This name
* can be used to route an RPC call to this MultiEndpoint by setting the {@link #ME_KEY} key value
* of the RPC {@link CallOptions}.
*
* <p>GcpMultiEndpointChannel receives a list of GcpMultiEndpointOptions for initial configuration.
* An updated configuration can be provided at any time later using {@link
* GcpMultiEndpointChannel#setMultiEndpoints(List)}. The first item in the GcpMultiEndpointOptions
* list defines the default MultiEndpoint that will be used when no MultiEndpoint name is provided
* with an RPC call.
*
* <p>Example:
*
* <p>Let's assume we have a service with read and write operations and the following backends:
*
* <ul>
* <li>service.example.com -- the main set of backends supporting all operations
* <li>service-fallback.example.com -- read-write replica supporting all operations
* <li>ro-service.example.com -- read-only replica supporting only read operations
* </ul>
*
* <p>Example configuration:
*
* <ul>
* <li>MultiEndpoint named "default" with endpoints:
* <ol>
* <li>service.example.com:443
* <li>service-fallback.example.com:443
* </ol>
* <li>MultiEndpoint named "read" with endpoints:
* <ol>
* <li>ro-service.example.com:443
* <li>service-fallback.example.com:443
* <li>service.example.com:443
* </ol>
* </ul>
*
* <p>With the configuration above GcpMultiEndpointChannel will use the "default" MultiEndpoint by
* default. It means that RPC calls by default will use the main endpoint and if it is not available
* then the read-write replica.
*
* <p>To offload some read calls to the read-only replica we can specify "read" MultiEndpoint in the
* CallOptions. Then these calls will use the read-only replica endpoint and if it is not available
* then the read-write replica and if it is also not available then the main endpoint.
*
* <p>GcpMultiEndpointChannel creates a {@link GcpManagedChannel} channel pool for every unique
* endpoint. For the example above three channel pools will be created.
*/
public class GcpMultiEndpointChannel extends ManagedChannel {
private static final Logger logger = Logger.getLogger(GcpMultiEndpointChannel.class.getName());
public static final CallOptions.Key<String> ME_KEY = CallOptions.Key.create("MultiEndpoint");
public static final Context.Key<String> ME_CONTEXT_KEY = Context.key("MultiEndpoint");
private final LabelKey endpointKey = LabelKey.create(ENDPOINT_LABEL, ENDPOINT_LABEL_DESC);
private final Map<String, MultiEndpoint> multiEndpoints = new ConcurrentHashMap<>();
private MultiEndpoint defaultMultiEndpoint;
private final ApiConfig apiConfig;
private final GcpManagedChannelOptions gcpManagedChannelOptions;
private final GcpMetricsOptions gcpMetricsOptions;
private DerivedLongGauge endpointStateMetric;
private DerivedLongCumulative endpointSwitchMetric;
private DerivedLongGauge currentEndpointMetric;
private Meter otelMeter;
private Attributes otelCommonAttributes;
private final Map<String, CurrentEndpointWatcher> currentEndpointWatchers =
new ConcurrentHashMap<>();
private final Map<String, GcpManagedChannel> pools = new ConcurrentHashMap<>();
@GuardedBy("this")
private final Set<String> currentEndpoints = new HashSet<>();
private final ScheduledExecutorService executor =
new ScheduledThreadPoolExecutor(1, GcpThreadFactory.newThreadFactory("gcp-me-%d"));
/**
* Constructor for {@link GcpMultiEndpointChannel}.
*
* @param meOptions list of MultiEndpoint configurations.
* @param apiConfig the ApiConfig object for configuring GcpManagedChannel.
* @param gcpManagedChannelOptions the options for GcpManagedChannel.
*/
public GcpMultiEndpointChannel(
List<GcpMultiEndpointOptions> meOptions,
ApiConfig apiConfig,
GcpManagedChannelOptions gcpManagedChannelOptions) {
this.apiConfig = apiConfig;
this.gcpManagedChannelOptions = gcpManagedChannelOptions;
this.gcpMetricsOptions = gcpManagedChannelOptions.getMetricsOptions();
createMetrics();
setMultiEndpoints(meOptions);
}
private class EndpointStateMonitor implements Runnable {
private final ManagedChannel channel;
private final String endpoint;
private ConnectivityState currentState;
private EndpointStateMonitor(ManagedChannel channel, String endpoint) {
this.endpoint = endpoint;
this.channel = channel;
setUpMetrics();
run();
}
private void setUpMetrics() {
if (endpointStateMetric == null) {
return;
}
endpointStateMetric.createTimeSeries(
appendCommonValues(LabelValue.create(endpoint), LabelValue.create(STATUS_AVAILABLE)),
this,
EndpointStateMonitor::reportAvailable);
endpointStateMetric.createTimeSeries(
appendCommonValues(LabelValue.create(endpoint), LabelValue.create(STATUS_UNAVAILABLE)),
this,
EndpointStateMonitor::reportUnavailable);
}
private void removeMetrics() {
if (endpointStateMetric == null) {
return;
}
endpointStateMetric.removeTimeSeries(
appendCommonValues(LabelValue.create(endpoint), LabelValue.create(STATUS_AVAILABLE)));
endpointStateMetric.removeTimeSeries(
appendCommonValues(LabelValue.create(endpoint), LabelValue.create(STATUS_UNAVAILABLE)));
}
private long reportAvailable() {
return ConnectivityState.READY.equals(currentState) ? 1 : 0;
}
private long reportUnavailable() {
return ConnectivityState.READY.equals(currentState) ? 0 : 1;
}
@Override
public void run() {
if (channel == null) {
removeMetrics();
return;
}
currentState = checkPoolState(channel, endpoint);
if (currentState != ConnectivityState.SHUTDOWN) {
channel.notifyWhenStateChanged(currentState, this);
} else {
removeMetrics();
}
}
}
// Checks and returns channel pool state. Also notifies all MultiEndpoints of the pool state.
private ConnectivityState checkPoolState(ManagedChannel channel, String endpoint) {
ConnectivityState state = channel.getState(false);
// Update endpoint state in all multiendpoints.
for (MultiEndpoint me : multiEndpoints.values()) {
me.setEndpointAvailable(endpoint, state.equals(ConnectivityState.READY));
}
return state;
}
private GcpManagedChannelOptions prepareGcpManagedChannelConfig(
GcpManagedChannelOptions gcpOptions, String endpoint) {
final GcpMetricsOptions.Builder metricsOptions =
GcpMetricsOptions.newBuilder(gcpOptions.getMetricsOptions());
final List<LabelKey> labelKeys = new ArrayList<>(metricsOptions.build().getLabelKeys());
final List<LabelValue> labelValues = new ArrayList<>(metricsOptions.build().getLabelValues());
labelKeys.add(endpointKey);
labelValues.add(LabelValue.create(endpoint));
// Mirror OpenCensus endpoint label for OpenTelemetry as well.
List<String> otelLabelKeys = metricsOptions.build().getOtelLabelKeys();
List<String> otelLabelValues = metricsOptions.build().getOtelLabelValues();
if (otelLabelKeys != null && otelLabelValues != null) {
List<String> newOtelKeys = new ArrayList<>(otelLabelKeys);
List<String> newOtelValues = new ArrayList<>(otelLabelValues);
newOtelKeys.add(ENDPOINT_LABEL);
newOtelValues.add(endpoint);
metricsOptions.withOtelLabels(newOtelKeys, newOtelValues);
}
// Make sure the pool will have at least 1 channel always connected. If maximum size > 1 then we
// want at least 2 channels or square root of maximum channels whichever is larger.
// Do not override if minSize is already specified as > 0.
final GcpChannelPoolOptions.Builder poolOptions =
GcpChannelPoolOptions.newBuilder(gcpOptions.getChannelPoolOptions());
if (poolOptions.build().getMinSize() < 1) {
int minSize = Math.min(2, poolOptions.build().getMaxSize());
minSize = Math.max(minSize, ((int) Math.sqrt(poolOptions.build().getMaxSize())));
poolOptions.setMinSize(minSize);
}
return GcpManagedChannelOptions.newBuilder(gcpOptions)
.withChannelPoolOptions(poolOptions.build())
.withMetricsOptions(metricsOptions.withLabels(labelKeys, labelValues).build())
.build();
}
private ManagedChannelBuilder<?> channelBuilderForEndpoint(String endpoint) {
String serviceAddress;
// Assume https by default.
int port = 443;
try {
URL url = new URL(endpoint);
serviceAddress = url.getHost();
port = url.getPort() < 0 ? url.getDefaultPort() : url.getPort();
} catch (MalformedURLException ex) {
// When no protocol is specified, fallback to plain host:port parsing.
int colon = endpoint.lastIndexOf(':');
if (colon < 0) {
serviceAddress = endpoint;
} else {
serviceAddress = endpoint.substring(0, colon);
port = Integer.parseInt(endpoint.substring(colon + 1));
}
}
return ManagedChannelBuilder.forAddress(serviceAddress, port);
}
private static class CurrentEndpointWatcher {
private final MultiEndpoint me;
private final String endpoint;
public CurrentEndpointWatcher(MultiEndpoint me, String endpoint) {
this.me = me;
this.endpoint = endpoint;
}
public long getMetricValue() {
return endpoint.equals(me.getCurrentId()) ? 1 : 0;
}
}
private void setUpMetricsForMultiEndpoint(GcpMultiEndpointOptions options, MultiEndpoint me) {
String name = options.getName();
List<String> endpoints = options.getEndpoints();
if (endpointSwitchMetric != null) {
endpointSwitchMetric.createTimeSeries(
appendCommonValues(LabelValue.create(name), LabelValue.create(TYPE_FALLBACK)),
me,
MultiEndpoint::getFallbackCnt);
endpointSwitchMetric.createTimeSeries(
appendCommonValues(LabelValue.create(name), LabelValue.create(TYPE_RECOVER)),
me,
MultiEndpoint::getRecoverCnt);
endpointSwitchMetric.createTimeSeries(
appendCommonValues(LabelValue.create(name), LabelValue.create(TYPE_REPLACE)),
me,
MultiEndpoint::getReplaceCnt);
}
if (currentEndpointMetric != null) {
for (String e : endpoints) {
CurrentEndpointWatcher watcher = new CurrentEndpointWatcher(me, e);
currentEndpointWatchers.put(name + ":" + e, watcher);
currentEndpointMetric.createTimeSeries(
appendCommonValues(LabelValue.create(name), LabelValue.create(e)),
watcher,
CurrentEndpointWatcher::getMetricValue);
}
}
}
private void updateMetricsForMultiEndpoint(GcpMultiEndpointOptions options, MultiEndpoint me) {
if (currentEndpointMetric == null) {
return;
}
Set<String> newEndpoints = new HashSet<>(options.getEndpoints());
Set<String> existingEndpoints = new HashSet<>(me.getEndpoints());
for (String e : existingEndpoints) {
if (!newEndpoints.contains(e)) {
currentEndpointMetric.removeTimeSeries(
appendCommonValues(LabelValue.create(options.getName()), LabelValue.create(e)));
currentEndpointWatchers.remove(options.getName() + ":" + e);
}
}
for (String e : newEndpoints) {
if (!existingEndpoints.contains(e)) {
CurrentEndpointWatcher watcher = new CurrentEndpointWatcher(me, e);
currentEndpointWatchers.put(options.getName() + ":" + e, watcher);
currentEndpointMetric.createTimeSeries(
appendCommonValues(LabelValue.create(options.getName()), LabelValue.create(e)),
watcher,
CurrentEndpointWatcher::getMetricValue);
}
}
}
private void removeMetricsForMultiEndpoint(String name, MultiEndpoint me) {
if (endpointSwitchMetric != null) {
endpointSwitchMetric.removeTimeSeries(
appendCommonValues(LabelValue.create(name), LabelValue.create(TYPE_FALLBACK)));
endpointSwitchMetric.removeTimeSeries(
appendCommonValues(LabelValue.create(name), LabelValue.create(TYPE_RECOVER)));
endpointSwitchMetric.removeTimeSeries(
appendCommonValues(LabelValue.create(name), LabelValue.create(TYPE_REPLACE)));
}
if (currentEndpointMetric != null) {
for (String e : me.getEndpoints()) {
currentEndpointMetric.removeTimeSeries(
appendCommonValues(LabelValue.create(name), LabelValue.create(e)));
currentEndpointWatchers.remove(name + ":" + e);
}
}
}
/**
* Update the list of MultiEndpoint configurations.
*
* <p>MultiEndpoints are matched with the current ones by name.
*
* <ul>
* <li>If a current MultiEndpoint is missing in the updated list, the MultiEndpoint will be
* removed.
* <li>A new MultiEndpoint will be created for every new name in the list.
* <li>For an existing MultiEndpoint only its endpoints will be updated (no recovery timeout
* change).
* </ul>
*
* <p>Endpoints are matched by the endpoint address (usually in the form of address:port).
*
* <ul>
* <li>If an existing endpoint is not used by any MultiEndpoint in the updated list, then the
* channel poll for this endpoint will be shutdown.
* <li>A channel pool will be created for every new endpoint.
* <li>For an existing endpoint nothing will change (the channel pool will not be re-created,
* thus no channel credentials change, nor channel configurator change).
* </ul>
*/
public synchronized void setMultiEndpoints(List<GcpMultiEndpointOptions> meOptions) {
Preconditions.checkNotNull(meOptions);
Preconditions.checkArgument(!meOptions.isEmpty(), "MultiEndpoints list is empty");
Set<String> currentMultiEndpoints = new HashSet<>();
// Must have all multiendpoints before initializing the pools so that all multiendpoints
// can get status update of every pool.
meOptions.forEach(
options -> {
currentMultiEndpoints.add(options.getName());
// Create or update MultiEndpoint
MultiEndpoint existingMe = multiEndpoints.get(options.getName());
if (existingMe != null) {
updateMetricsForMultiEndpoint(options, existingMe);
existingMe.setEndpoints(options.getEndpoints());
} else {
MultiEndpoint me =
new MultiEndpoint.Builder(options.getEndpoints())
.withRecoveryTimeout(options.getRecoveryTimeout())
.withSwitchingDelay(options.getSwitchingDelay())
.build();
setUpMetricsForMultiEndpoint(options, me);
multiEndpoints.put(options.getName(), me);
}
});
final Set<String> existingPools = new HashSet<>(pools.keySet());
currentEndpoints.clear();
// TODO: Support the same endpoint in different MultiEndpoint to use different channel
// credentials.
// TODO: Support different endpoints in the same MultiEndpoint to use different channel
// credentials.
meOptions.forEach(
options -> {
// Create missing pools
options
.getEndpoints()
.forEach(
endpoint -> {
currentEndpoints.add(endpoint);
pools.computeIfAbsent(
endpoint,
e -> {
ManagedChannelBuilder<?> managedChannelBuilder;
if (options.getChannelCredentials() != null) {
managedChannelBuilder =
Grpc.newChannelBuilder(e, options.getChannelCredentials());
} else {
managedChannelBuilder = channelBuilderForEndpoint(e);
}
if (options.getChannelConfigurator() != null) {
managedChannelBuilder =
options.getChannelConfigurator().apply(managedChannelBuilder);
}
GcpManagedChannel channel =
new GcpManagedChannel(
managedChannelBuilder,
apiConfig,
// Add endpoint to metric labels.
prepareGcpManagedChannelConfig(gcpManagedChannelOptions, e));
// Start monitoring the pool state.
new EndpointStateMonitor(channel, e);
return channel;
});
});
});
existingPools.retainAll(currentEndpoints);
existingPools.forEach(
e -> {
// Communicate current state to MultiEndpoints.
checkPoolState(pools.get(e), e);
});
defaultMultiEndpoint = multiEndpoints.get(meOptions.get(0).getName());
// Remove obsolete multiendpoints.
Iterator<String> iter = multiEndpoints.keySet().iterator();
while (iter.hasNext()) {
String name = iter.next();
if (currentMultiEndpoints.contains(name)) {
continue;
}
removeMetricsForMultiEndpoint(name, multiEndpoints.get(name));
iter.remove();
}
// Shutdown and remove the pools not present in options.
final Set<String> poolsToRemove = new HashSet<>(pools.keySet());
poolsToRemove.removeIf(currentEndpoints::contains);
if (!poolsToRemove.isEmpty()) {
// Get max switching delay.
Optional<Duration> maxDelay =
meOptions.stream()
.map(GcpMultiEndpointOptions::getSwitchingDelay)
.max(Comparator.naturalOrder());
if (maxDelay.isPresent() && maxDelay.get().toMillis() > 0) {
executor.schedule(
() -> maybeCleanupPools(poolsToRemove), maxDelay.get().toMillis(), MILLISECONDS);
} else {
maybeCleanupPools(poolsToRemove);
}
}
}
private synchronized void maybeCleanupPools(Set<String> endpoints) {
for (String endpoint : endpoints) {
if (currentEndpoints.contains(endpoint)) {
continue;
}
pools.get(endpoint).shutdown();
pools.remove(endpoint);
}
}
private void createMetrics() {
if (gcpMetricsOptions == null) {
return;
}
MetricRegistry metricRegistry = gcpMetricsOptions.getMetricRegistry();
if (gcpMetricsOptions.getOpenTelemetryMeter() != null) {
// Prefer OpenTelemetry if present.
this.otelMeter = gcpMetricsOptions.getOpenTelemetryMeter();
AttributesBuilder builder = Attributes.builder();
if (gcpMetricsOptions.getOtelLabelKeys() != null
&& gcpMetricsOptions.getOtelLabelValues() != null) {
for (int i = 0;
i
< Math.min(
gcpMetricsOptions.getOtelLabelKeys().size(),
gcpMetricsOptions.getOtelLabelValues().size());
i++) {
String k = gcpMetricsOptions.getOtelLabelKeys().get(i);
String v = gcpMetricsOptions.getOtelLabelValues().get(i);
if (k != null && !k.isEmpty() && v != null) {
builder.put(k, v);
}
}
}
otelCommonAttributes = builder.build();
String prefix = gcpMetricsOptions.getNamePrefix();
otelMeter
.gaugeBuilder(prefix + METRIC_ENDPOINT_SWITCH)
.ofLongs()
.setDescription(
"Reports occurrences of changes of current endpoint for a multi-endpoint with the name, specifying change type.")
.setUnit(COUNT)
.buildWithCallback(
m -> {
for (Map.Entry<String, MultiEndpoint> entry : multiEndpoints.entrySet()) {
String meName = entry.getKey();
MultiEndpoint me = entry.getValue();
m.record(
me.getFallbackCnt(),
Attributes.builder()
.putAll(otelCommonAttributes)
.put(ME_NAME_LABEL, meName)
.put(SWITCH_TYPE_LABEL, TYPE_FALLBACK)
.build());
m.record(
me.getRecoverCnt(),
Attributes.builder()
.putAll(otelCommonAttributes)
.put(ME_NAME_LABEL, meName)
.put(SWITCH_TYPE_LABEL, TYPE_RECOVER)
.build());
m.record(
me.getReplaceCnt(),
Attributes.builder()
.putAll(otelCommonAttributes)
.put(ME_NAME_LABEL, meName)
.put(SWITCH_TYPE_LABEL, TYPE_REPLACE)
.build());
}
});
otelMeter
.gaugeBuilder(prefix + METRIC_CURRENT_ENDPOINT)
.ofLongs()
.setDescription("Reports 1 when an endpoint is current for multi-endpoint with the name.")
.setUnit(COUNT)
.buildWithCallback(
m -> {
for (Map.Entry<String, MultiEndpoint> entry : multiEndpoints.entrySet()) {
String meName = entry.getKey();
MultiEndpoint me = entry.getValue();
for (String endpoint : me.getEndpoints()) {
m.record(
endpoint.equals(me.getCurrentId()) ? 1L : 0L,
Attributes.builder()
.putAll(otelCommonAttributes)
.put(ME_NAME_LABEL, meName)
.put(ENDPOINT_LABEL, endpoint)
.build());
}
}
});
otelMeter
.gaugeBuilder(prefix + METRIC_ENDPOINT_STATE)
.ofLongs()
.setDescription("Reports 1 when endpoint is in the status.")
.setUnit(COUNT)
.buildWithCallback(
m -> {
for (Map.Entry<String, GcpManagedChannel> entry : pools.entrySet()) {
String endpoint = entry.getKey();
GcpManagedChannel channel = entry.getValue();
boolean available = ConnectivityState.READY.equals(channel.getState(false));
m.record(
available ? 1L : 0L,
Attributes.builder()
.putAll(otelCommonAttributes)
.put(ENDPOINT_LABEL, endpoint)
.put(STATUS_LABEL, STATUS_AVAILABLE)
.build());
m.record(
available ? 0L : 1L,
Attributes.builder()
.putAll(otelCommonAttributes)
.put(ENDPOINT_LABEL, endpoint)
.put(STATUS_LABEL, STATUS_UNAVAILABLE)
.build());
}
});
return;
}
if (metricRegistry == null) {
return;
}
if (endpointStateMetric != null) {
return;
}
String prefix = gcpMetricsOptions.getNamePrefix();
endpointStateMetric =
metricRegistry.addDerivedLongGauge(
prefix + METRIC_ENDPOINT_STATE,
createMetricOptions(
"Reports 1 when endpoint is in the status.",
COUNT,
LabelKey.create(ENDPOINT_LABEL, ENDPOINT_LABEL_DESC),
LabelKey.create(STATUS_LABEL, STATUS_LABEL_DESC)));
endpointSwitchMetric =
metricRegistry.addDerivedLongCumulative(
prefix + METRIC_ENDPOINT_SWITCH,
createMetricOptions(
"Reports occurrences of changes of current endpoint for a multi-endpoint with "
+ "the name, specifying change type.",
COUNT,
LabelKey.create(ME_NAME_LABEL, ME_NAME_LABEL_DESC),
LabelKey.create(SWITCH_TYPE_LABEL, SWITCH_TYPE_LABEL_DESC)));
currentEndpointMetric =
metricRegistry.addDerivedLongGauge(
prefix + METRIC_CURRENT_ENDPOINT,
createMetricOptions(
"Reports 1 when an endpoint is current for multi-endpoint with the name.",
COUNT,
LabelKey.create(ME_NAME_LABEL, ME_NAME_LABEL_DESC),
LabelKey.create(ENDPOINT_LABEL, ENDPOINT_LABEL_DESC)));
}
private List<LabelValue> appendCommonValues(LabelValue... labelValues) {
final List<LabelValue> values = new ArrayList<>();
Collections.addAll(values, labelValues);
if (gcpMetricsOptions != null && gcpMetricsOptions.getLabelValues() != null) {
values.addAll(gcpMetricsOptions.getLabelValues());
}
return values;
}
private MetricOptions createMetricOptions(
String description, String unit, LabelKey... labelKeys) {
final List<LabelKey> keys = new ArrayList<>();
Collections.addAll(keys, labelKeys);
if (gcpMetricsOptions != null && gcpMetricsOptions.getLabelKeys() != null) {
keys.addAll(gcpMetricsOptions.getLabelKeys());
}
return MetricOptions.builder()
.setDescription(description)
.setLabelKeys(keys)
.setUnit(unit)
.build();
}
/**
* Initiates an orderly shutdown in which preexisting calls continue but new calls are immediately
* cancelled.
*
* @return this
* @since 1.0.0
*/
@Override
@CanIgnoreReturnValue
public ManagedChannel shutdown() {
pools.values().forEach(GcpManagedChannel::shutdown);
return this;
}
/**
* Returns whether the channel is shutdown. Shutdown channels immediately cancel any new calls,
* but may still have some calls being processed.
*
* @see #shutdown()
* @see #isTerminated()
* @since 1.0.0
*/
@Override
public boolean isShutdown() {
return pools.values().stream().allMatch(GcpManagedChannel::isShutdown);
}
/**
* Returns whether the channel is terminated. Terminated channels have no running calls and
* relevant resources released (like TCP connections).
*
* @see #isShutdown()
* @since 1.0.0
*/
@Override
public boolean isTerminated() {
return pools.values().stream().allMatch(GcpManagedChannel::isTerminated);
}
/**
* Initiates a forceful shutdown in which preexisting and new calls are cancelled. Although
* forceful, the shutdown process is still not instantaneous; {@link #isTerminated()} will likely
* return {@code false} immediately after this method returns.
*
* @return this
* @since 1.0.0
*/
@Override
@CanIgnoreReturnValue
public ManagedChannel shutdownNow() {
pools.values().forEach(GcpManagedChannel::shutdownNow);
return this;
}
/**
* Waits for the channel to become terminated, giving up if the timeout is reached.
*
* @return whether the channel is terminated, as would be done by {@link #isTerminated()}.
* @since 1.0.0
*/
@Override
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
long endTimeNanos = System.nanoTime() + unit.toNanos(timeout);
for (GcpManagedChannel gcpManagedChannel : pools.values()) {
if (gcpManagedChannel.isTerminated()) {
continue;
}
long awaitTimeNanos = endTimeNanos - System.nanoTime();
if (awaitTimeNanos <= 0) {
break;
}
gcpManagedChannel.awaitTermination(awaitTimeNanos, NANOSECONDS);
}
return isTerminated();
}
/**
* Check the value of {@link #ME_KEY} key in the {@link CallOptions} and if found use the
* MultiEndpoint with the same name for this call.
*
* <p>Create a {@link ClientCall} to the remote operation specified by the given {@link
* MethodDescriptor}. The returned {@link ClientCall} does not trigger any remote behavior until
* {@link ClientCall#start(Listener, Metadata)} is invoked.
*
* @param methodDescriptor describes the name and parameter types of the operation to call.
* @param callOptions runtime options to be applied to this call.
* @return a {@link ClientCall} bound to the specified method.
* @since 1.0.0
*/
@Override
public <RequestT, ResponseT> ClientCall<RequestT, ResponseT> newCall(
MethodDescriptor<RequestT, ResponseT> methodDescriptor, CallOptions callOptions) {
String multiEndpointKey = callOptions.getOption(ME_KEY);
if (multiEndpointKey == null) {
multiEndpointKey = ME_CONTEXT_KEY.get(Context.current());
}
MultiEndpoint me = defaultMultiEndpoint;
if (multiEndpointKey != null) {
me = multiEndpoints.getOrDefault(multiEndpointKey, defaultMultiEndpoint);
}
return pools.get(me.getCurrentId()).newCall(methodDescriptor, callOptions);
}
/**
* The authority of the current endpoint of the default MultiEndpoint. Typically, this is in the
* format {@code host:port}.
*
* <p>To get the authority of the current endpoint of another MultiEndpoint use {@link
* #authorityFor(String)} method.
*
* <p>This may return different values over time because MultiEndpoint may switch between
* endpoints.
*
* @since 1.0.0
*/
@Override
public String authority() {
return pools.get(defaultMultiEndpoint.getCurrentId()).authority();
}
/**
* The authority of the current endpoint of the specified MultiEndpoint. Typically, this is in the
* format {@code host:port}.
*
* <p>This may return different values over time because MultiEndpoint may switch between
* endpoints.
*/
public String authorityFor(String multiEndpointName) {
MultiEndpoint multiEndpoint = multiEndpoints.get(multiEndpointName);
if (multiEndpoint == null) {
return null;
}
return pools.get(multiEndpoint.getCurrentId()).authority();
}
}