Repository navigation
Expand file tree
/
Copy pathGcpManagedChannelHotChannelReproducerTest.java
More file actions
1141 lines (1048 loc) · 40.6 KB
/
Copy pathGcpManagedChannelHotChannelReproducerTest.java
File metadata and controls
1141 lines (1048 loc) · 40.6 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
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2026 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.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;
import static org.awaitility.Awaitility.await;
import static org.junit.Assume.assumeTrue;
import com.google.cloud.grpc.GcpManagedChannel.ChannelAffinityRef;
import com.google.cloud.grpc.GcpManagedChannel.ChannelRef;
import com.google.cloud.grpc.GcpManagedChannelOptions.GcpChannelPoolOptions;
import com.google.cloud.grpc.proto.AffinityConfig;
import com.google.cloud.grpc.proto.ApiConfig;
import com.google.cloud.grpc.proto.MethodConfig;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.spanner.v1.CreateSessionRequest;
import com.google.spanner.v1.ExecuteSqlRequest;
import com.google.spanner.v1.ResultSet;
import com.google.spanner.v1.Session;
import io.grpc.CallOptions;
import io.grpc.ClientCall;
import io.grpc.ClientInterceptor;
import io.grpc.CompressorRegistry;
import io.grpc.ConnectivityState;
import io.grpc.DecompressorRegistry;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.Metadata;
import io.grpc.MethodDescriptor;
import io.grpc.NameResolver.Factory;
import io.grpc.Status;
import io.grpc.protobuf.ProtoUtils;
import io.grpc.stub.ClientCalls;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
/**
* Dynamic-pool per-channel request-skew tests.
*
* <p>Deterministic regressions always run. The load-shaped reproducer is opt-in with {@code
* -Dhotchannel.load=true}; configure its seeds with {@code -Dhotchannel.seeds=1103,7,42,99,123}.
*/
@RunWith(JUnit4.class)
public final class GcpManagedChannelHotChannelReproducerTest {
private static final int MIN_SIZE = 2;
private static final int INITIAL_SIZE = 4;
private static final int MAX_SIZE = 48;
private static final int MIN_RPC_PER_CHANNEL = 15;
private static final int MAX_RPC_PER_CHANNEL = 25;
private static final int DEFAULT_CALLERS = 300;
private static final int SESSION_COUNT = 100;
private static final Duration TEST_SCALE_DOWN_INTERVAL = Duration.ofMillis(120);
private static final CallOptions.Key<Long> TEST_LATENCY_MILLIS =
CallOptions.Key.create("hotchannel-test-latency-millis");
private static final MethodDescriptor<CreateSessionRequest, Session> CREATE_SESSION_METHOD =
MethodDescriptor.<CreateSessionRequest, Session>newBuilder()
.setType(MethodDescriptor.MethodType.UNARY)
.setFullMethodName("google.spanner.v1.Spanner/CreateSession")
.setRequestMarshaller(ProtoUtils.marshaller(CreateSessionRequest.getDefaultInstance()))
.setResponseMarshaller(ProtoUtils.marshaller(Session.getDefaultInstance()))
.build();
private static final MethodDescriptor<ExecuteSqlRequest, ResultSet> EXECUTE_SQL_METHOD =
MethodDescriptor.<ExecuteSqlRequest, ResultSet>newBuilder()
.setType(MethodDescriptor.MethodType.UNARY)
.setFullMethodName("google.spanner.v1.Spanner/ExecuteSql")
.setRequestMarshaller(ProtoUtils.marshaller(ExecuteSqlRequest.getDefaultInstance()))
.setResponseMarshaller(ProtoUtils.marshaller(ResultSet.getDefaultInstance()))
.build();
private static final CallOptions.Key<ChannelAffinityRef> CHANNEL_AFFINITY_REF_KEY =
GcpManagedChannel.CHANNEL_AFFINITY_REF_KEY;
@Test
public void dynamicPoolDoesNotDevelopOneHotChannel() throws Exception {
assumeTrue("load-shaped reproducer disabled", Boolean.getBoolean("hotchannel.load"));
Variant variant =
Variant.valueOf(
System.getProperty("hotchannel.variant", "REF_6120").toUpperCase(Locale.ROOT));
LoadShape loadShape =
LoadShape.valueOf(
System.getProperty("hotchannel.loadShape", "BURSTY").toUpperCase(Locale.ROOT));
String[] seeds = System.getProperty("hotchannel.seeds", "1103").split(",");
boolean observeOnly = Boolean.getBoolean("hotchannel.observeOnly");
for (String seedValue : seeds) {
long seed = Long.parseLong(seedValue.trim());
RunResult result = runScenario(seed, variant, loadShape);
System.out.println(result.format());
if (!observeOnly) {
assertWithMessage("one-hot-channel skew: %s", result.format())
.that(result.hotToMedian)
.isLessThan(3.0);
}
}
}
@Test
public void warmChannelDoesNotBiasPowerOfTwoTies() throws Exception {
ScheduledExecutorService responses = Executors.newSingleThreadScheduledExecutor();
GcpManagedChannel pool = null;
try {
pool = fixedPool(new RecordingChannelBuilder(responses, 1), 2, 2, false);
ChannelRef warm = pool.channelRefs.get(1);
warm.messageReceived();
int warmPicks = 0;
for (int i = 0; i < 200; i++) {
if (pool.getChannelRef(null) == warm) {
warmPicks++;
}
}
assertThat(warmPicks).isGreaterThan(60);
assertThat(warmPicks).isLessThan(140);
} finally {
if (pool != null) {
pool.shutdownNow();
}
responses.shutdownNow();
}
}
@Test
public void selectedCallsReserveLoadBeforeStart() throws Exception {
ScheduledExecutorService responses = Executors.newSingleThreadScheduledExecutor();
GcpManagedChannel pool = null;
try {
RecordingChannelBuilder delegateBuilder = new RecordingChannelBuilder(responses, 2);
pool = fixedPool(delegateBuilder, 2, 2, false);
pool.channelRefs.get(1).messageReceived();
for (int i = 0; i < 200; i++) {
ChannelAffinityRef affinityRef = new ChannelAffinityRef();
pool.newCall(
EXECUTE_SQL_METHOD,
CallOptions.DEFAULT.withOption(CHANNEL_AFFINITY_REF_KEY, affinityRef));
}
long first = delegateBuilder.stats.get(0).selected.get();
long second = delegateBuilder.stats.get(1).selected.get();
assertThat(first).isGreaterThan(60);
assertThat(second).isGreaterThan(60);
assertThat(pool.channelRefs.get(0).getActiveStreamsCount()).isEqualTo((int) first);
assertThat(pool.channelRefs.get(1).getActiveStreamsCount()).isEqualTo((int) second);
} finally {
if (pool != null) {
pool.shutdownNow();
}
responses.shutdownNow();
}
}
@Test
public void affinityReferencesRedistributeAfterDrainingChannelsShutdown() throws Exception {
ScheduledExecutorService responses = Executors.newSingleThreadScheduledExecutor();
ExecutorService executor = Executors.newFixedThreadPool(100);
GcpManagedChannel pool = null;
try {
pool = fixedPool(new RecordingChannelBuilder(responses, 3), 2, 4, true);
List<ChannelAffinityRef> affinityRefs = new ArrayList<>();
List<Integer> originalIds = new ArrayList<>();
for (int i = 0; i < 200; i++) {
int channelId = pool.channelRefs.get(i % 4).getId();
ChannelAffinityRef affinityRef = new ChannelAffinityRef();
affinityRef.setChannelIdForTest(channelId);
affinityRefs.add(affinityRef);
originalIds.add(channelId);
}
invokeScaleDownCheck(pool, 3);
assertThat(pool.channelRefs).hasSize(2);
Set<Integer> removedIds = new HashSet<>();
for (ChannelRef removed : pool.removedChannelRefs) {
removedIds.add(removed.getId());
}
List<ChannelAffinityRef> orphaned = new ArrayList<>();
for (int i = 0; i < affinityRefs.size(); i++) {
if (removedIds.contains(originalIds.get(i))) {
orphaned.add(affinityRefs.get(i));
}
}
assertThat(orphaned).hasSize(100);
for (int i = 0; i < affinityRefs.size(); i++) {
if (removedIds.contains(originalIds.get(i))) {
assertThat(pool.getChannelRefByAffinityRef(affinityRefs.get(i)).getId())
.isEqualTo(originalIds.get(i));
}
}
for (ChannelRef removed : pool.removedChannelRefs) {
removed.getChannel().shutdownNow();
}
ChannelRef first = pool.channelRefs.get(0);
ChannelRef second = pool.channelRefs.get(1);
second.messageReceived();
CountDownLatch start = new CountDownLatch(1);
List<Future<ChannelRef>> resolutions = new ArrayList<>();
GcpManagedChannel resolvingPool = pool;
for (ChannelAffinityRef affinityRef : orphaned) {
resolutions.add(
executor.submit(
() -> {
start.await();
return resolvingPool.getChannelRefByAffinityRef(affinityRef);
}));
}
start.countDown();
int firstPicks = 0;
int secondPicks = 0;
for (Future<ChannelRef> resolution : resolutions) {
ChannelRef resolved = resolution.get(5, TimeUnit.SECONDS);
if (resolved == first) {
firstPicks++;
} else if (resolved == second) {
secondPicks++;
}
}
assertThat(firstPicks).isGreaterThan(25);
assertThat(secondPicks).isGreaterThan(25);
} finally {
if (pool != null) {
pool.shutdownNow();
}
executor.shutdownNow();
responses.shutdownNow();
}
}
@Test
public void scaleDownMarksAtMostTwoOfFortyEightLiveReferencesPerCheck() throws Exception {
ScheduledExecutorService responses = Executors.newSingleThreadScheduledExecutor();
GcpManagedChannel pool = null;
try {
pool = fixedPool(new RecordingChannelBuilder(responses, 4), 2, 48, true);
List<ChannelAffinityRef> handles = new ArrayList<>();
for (ChannelRef channelRef : pool.channelRefs) {
channelRef.activeStreamsCountIncr();
ChannelAffinityRef handle = new ChannelAffinityRef();
handle.setChannelIdForTest(channelRef.getId());
handles.add(handle);
}
for (ChannelRef channelRef : pool.channelRefs) {
channelRef.activeStreamsCountDecr(System.nanoTime(), Status.OK, false);
}
invokeScaleDownCheck(pool, 2);
assertThat(pool.channelRefs).hasSize(48);
invokeScaleDownCheck(pool, 1);
assertThat(pool.channelRefs).hasSize(46);
assertThat(pool.removedChannelRefs).hasSize(2);
for (ChannelRef removed : pool.removedChannelRefs) {
ChannelRef resolved = pool.getChannelRefByAffinityRef(handles.get(removed.getId()));
assertThat(resolved).isSameInstanceAs(removed);
assertThat(resolved.isActive()).isFalse();
assertThat(resolved.getChannel().isShutdown()).isFalse();
}
invokeScaleDownCheck(pool, 3);
assertThat(pool.channelRefs).hasSize(44);
assertThat(pool.removedChannelRefs).hasSize(4);
} finally {
if (pool != null) {
pool.shutdownNow();
}
responses.shutdownNow();
}
}
private static GcpManagedChannel fixedPool(
RecordingChannelBuilder delegateBuilder, int minimum, int initial, boolean dynamic) {
GcpChannelPoolOptions.Builder options =
GcpChannelPoolOptions.newBuilder()
.setMinSize(minimum)
.setInitSize(initial)
.setMaxSize(initial);
if (dynamic) {
options
.setDynamicScaling(10, 20, Duration.ofMinutes(1))
.setScaleDownConsecutiveLowLoadChecks(3)
.setMaxScaleDownChannels(2);
}
return (GcpManagedChannel)
GcpManagedChannelBuilder.forDelegateBuilder(delegateBuilder)
.withOptions(
GcpManagedChannelOptions.newBuilder()
.withChannelPoolOptions(options.build())
.build())
.build();
}
private static void invokeScaleDownCheck(GcpManagedChannel pool, int times) {
for (int i = 0; i < times; i++) {
pool.checkScaleDown();
}
}
private RunResult runScenario(long seed, Variant variant, LoadShape loadShape) throws Exception {
int callers = Integer.getInteger("hotchannel.callers", DEFAULT_CALLERS);
if (callers < 20) {
throw new IllegalArgumentException("hotchannel.callers must be at least 20");
}
boolean staticPool = Boolean.getBoolean("hotchannel.staticPool");
boolean noScaleDown = Boolean.getBoolean("hotchannel.noScaleDown");
Duration phaseInterval =
Duration.ofMillis(
Long.getLong("hotchannel.scaleDownMillis", TEST_SCALE_DOWN_INTERVAL.toMillis()));
Duration scaleDownInterval = noScaleDown ? Duration.ofHours(1) : phaseInterval;
ScheduledExecutorService responses = Executors.newScheduledThreadPool(32);
ExecutorService callersExecutor = Executors.newFixedThreadPool(callers);
GcpManagedChannel pool = null;
try {
RecordingChannelBuilder delegateBuilder = new RecordingChannelBuilder(responses, seed);
GcpChannelPoolOptions.Builder poolOptions =
GcpChannelPoolOptions.newBuilder()
.setMaxSize(MAX_SIZE)
.setMinSize(staticPool ? MAX_SIZE : MIN_SIZE)
.setInitSize(staticPool ? MAX_SIZE : INITIAL_SIZE)
.setAffinityKeyLifetime(Duration.ofMinutes(10))
.setCleanupInterval(Duration.ofMinutes(1));
if (staticPool) {
poolOptions.disableDynamicScaling();
} else {
poolOptions.setDynamicScaling(MIN_RPC_PER_CHANNEL, MAX_RPC_PER_CHANNEL, scaleDownInterval);
compressBranchSpecificScaleUpTimer(poolOptions);
}
pool =
(GcpManagedChannel)
GcpManagedChannelBuilder.forDelegateBuilder(delegateBuilder)
.withApiConfig(spannerAffinityConfig())
.withOptions(
GcpManagedChannelOptions.newBuilder()
.withChannelPoolOptions(poolOptions.build())
.build())
.build();
// Reach the customer-sized pool before creating the session pool. Each caller owns one
// transaction and fans out five RPCs on its transaction affinity handle.
runWave(pool, callersExecutor, variant, Collections.emptyList(), seed, callers, 5, 0);
List<String> sessionNames = createSessionsInBatches(pool);
if (!staticPool && loadShape == LoadShape.BURSTY) {
runScaleCycles(pool, callersExecutor, variant, sessionNames, seed, callers, phaseInterval);
} else if (loadShape == LoadShape.CONSTANT) {
runConstantLoad(pool, callersExecutor, variant, sessionNames, seed, callers);
}
// Final ramp restores a full active pool. Reset delegate counters so prior channel history
// does not manufacture skew in the measured hold period.
runWave(pool, callersExecutor, variant, sessionNames, seed + 10_000, callers, 5, 10_000);
delegateBuilder.resetMeasurements();
if (variant == Variant.REF_6120 && loadShape == LoadShape.BURSTY) {
runTransactionsAcrossScaleDown(
pool,
callersExecutor,
delegateBuilder,
sessionNames,
seed + 15_000,
callers,
phaseInterval);
}
int measureWaves = Integer.getInteger("hotchannel.measureWaves", 8);
for (int wave = 0; wave < measureWaves; wave++) {
runWave(
pool,
callersExecutor,
variant,
sessionNames,
seed + 20_000,
callers,
5,
20_000 + wave * callers);
}
Set<Integer> activeIds = new HashSet<>();
for (ChannelRef channelRef : pool.channelRefs) {
activeIds.add(channelRef.getId());
}
return delegateBuilder.snapshot(
seed, variant, loadShape, pool.getNumberOfChannels(), activeIds);
} finally {
if (pool != null) {
pool.shutdownNow();
}
callersExecutor.shutdownNow();
responses.shutdownNow();
callersExecutor.awaitTermination(10, TimeUnit.SECONDS);
responses.awaitTermination(10, TimeUnit.SECONDS);
}
}
private static void compressBranchSpecificScaleUpTimer(GcpChannelPoolOptions.Builder builder) {
builder.setScaleUpCooldown(Duration.ofNanos(1));
}
private static void runTransactionsAcrossScaleDown(
GcpManagedChannel pool,
ExecutorService executor,
RecordingChannelBuilder delegateBuilder,
List<String> sessions,
long seed,
int callers,
Duration scaleDownInterval)
throws Exception {
List<TransactionContext> transactions = new ArrayList<>(callers);
for (int transaction = 0; transaction < callers; transaction++) {
String session = sessions.get(Math.floorMod(mix(seed ^ transaction), sessions.size()));
transactions.add(new TransactionContext(transaction, session, new ChannelAffinityRef()));
}
CountDownLatch firstStart = new CountDownLatch(1);
List<Future<ListenableFuture<ResultSet>>> firstRpcStarts = new ArrayList<>(callers);
for (TransactionContext transaction : transactions) {
firstRpcStarts.add(
executor.submit(
() -> {
firstStart.await();
CallOptions options =
CallOptions.DEFAULT.withOption(
CHANNEL_AFFINITY_REF_KEY, transaction.affinityRef);
if (transaction.id < 20) {
options = options.withOption(TEST_LATENCY_MILLIS, 900L);
}
return executeSql(pool, options, transaction.session);
}));
}
firstStart.countDown();
List<ListenableFuture<ResultSet>> firstRpcs = new ArrayList<>(callers);
for (Future<ListenableFuture<ResultSet>> start : firstRpcStarts) {
firstRpcs.add(start.get(10, TimeUnit.SECONDS));
}
for (int transaction = 20; transaction < callers; transaction++) {
firstRpcs.get(transaction).get(10, TimeUnit.SECONDS);
}
// Twenty slow streams remain while a scaled 120 ms interval stands in for Spanner's three
// minutes. Mainline removes almost the entire pool in one check. Caller-owned refs still point
// at those now-inactive channels.
await()
.atMost(scaleDownInterval.multipliedBy(5).plusSeconds(1))
.until(() -> pool.getNumberOfChannels() < MAX_SIZE);
// Select every remaining call before starting any of them. This exposes the real newCall/start
// accounting gap: channel selection happens before SimpleGcpClientCall publishes its stream.
Set<Integer> candidateIds = new HashSet<>();
for (ChannelRef channelRef : pool.channelRefs) {
candidateIds.add(channelRef.getId());
}
delegateBuilder.resetSelections();
List<Future<List<ClientCall<ExecuteSqlRequest, ResultSet>>>> prepared =
new ArrayList<>(callers);
for (TransactionContext transaction : transactions) {
prepared.add(
executor.submit(
() -> {
int remainingRpcCount = 1 + Math.floorMod(mix(seed + transaction.id * 17L), 4);
List<ClientCall<ExecuteSqlRequest, ResultSet>> calls =
new ArrayList<>(remainingRpcCount);
CallOptions options =
CallOptions.DEFAULT.withOption(
CHANNEL_AFFINITY_REF_KEY, transaction.affinityRef);
for (int rpc = 0; rpc < remainingRpcCount; rpc++) {
calls.add(pool.newCall(EXECUTE_SQL_METHOD, options));
}
return calls;
}));
}
List<List<ClientCall<ExecuteSqlRequest, ResultSet>>> preparedCalls = new ArrayList<>(callers);
for (Future<List<ClientCall<ExecuteSqlRequest, ResultSet>>> calls : prepared) {
preparedCalls.add(calls.get(10, TimeUnit.SECONDS));
}
System.out.println(delegateBuilder.formatSelections(seed, candidateIds));
CountDownLatch remainingStart = new CountDownLatch(1);
List<Future<?>> remainingTransactions = new ArrayList<>(callers);
for (int transaction = 0; transaction < callers; transaction++) {
TransactionContext context = transactions.get(transaction);
List<ClientCall<ExecuteSqlRequest, ResultSet>> calls = preparedCalls.get(transaction);
remainingTransactions.add(
executor.submit(
() -> {
remainingStart.await();
List<ListenableFuture<ResultSet>> results = new ArrayList<>(calls.size());
for (ClientCall<ExecuteSqlRequest, ResultSet> call : calls) {
results.add(
ClientCalls.futureUnaryCall(
call,
ExecuteSqlRequest.newBuilder()
.setSession(context.session)
.setSql("SELECT 1")
.build()));
}
for (ListenableFuture<ResultSet> result : results) {
result.get(10, TimeUnit.SECONDS);
}
return null;
}));
}
remainingStart.countDown();
for (Future<?> transaction : remainingTransactions) {
transaction.get(30, TimeUnit.SECONDS);
}
for (int transaction = 0; transaction < 20; transaction++) {
firstRpcs.get(transaction).get(10, TimeUnit.SECONDS);
}
}
private static ListenableFuture<ResultSet> executeSql(
GcpManagedChannel pool, CallOptions callOptions, String session) {
return ClientCalls.futureUnaryCall(
pool.newCall(EXECUTE_SQL_METHOD, callOptions),
ExecuteSqlRequest.newBuilder().setSession(session).setSql("SELECT 1").build());
}
private static void runScaleCycles(
GcpManagedChannel pool,
ExecutorService executor,
Variant variant,
List<String> sessions,
long seed,
int callers,
Duration interval)
throws Exception {
int[] ramp = {callers / 8, callers / 3, (callers * 2) / 3, callers};
for (int cycle = 0; cycle < 3; cycle++) {
for (int waveSize : ramp) {
runWave(
pool,
executor,
variant,
sessions,
seed + cycle * 1_000,
waveSize,
0,
cycle * 10_000 + waveSize);
}
for (int hold = 0; hold < 3; hold++) {
runWave(
pool,
executor,
variant,
sessions,
seed + cycle * 1_000,
callers,
0,
cycle * 10_000 + 1_000 + hold * callers);
}
int sizeBeforeScaleDown = pool.getNumberOfChannels();
await()
.atMost(interval.multipliedBy(5).plusSeconds(1))
.until(
() ->
pool.getNumberOfChannels() < sizeBeforeScaleDown
|| pool.getNumberOfChannels() == MIN_SIZE);
}
}
private static void runConstantLoad(
GcpManagedChannel pool,
ExecutorService executor,
Variant variant,
List<String> sessions,
long seed,
int callers)
throws Exception {
for (int wave = 0; wave < 20; wave++) {
runWave(pool, executor, variant, sessions, seed + 5_000, callers, 0, 5_000 + wave * callers);
}
}
private static List<String> createSessionsInBatches(GcpManagedChannel pool) throws Exception {
List<String> sessions = new ArrayList<>();
for (int batch = 0; batch < SESSION_COUNT / 10; batch++) {
List<ListenableFuture<Session>> futures = new ArrayList<>();
for (int item = 0; item < 10; item++) {
futures.add(
ClientCalls.futureUnaryCall(
pool.newCall(CREATE_SESSION_METHOD, CallOptions.DEFAULT),
CreateSessionRequest.newBuilder()
.setDatabase("projects/p/instances/i/databases/d")
.build()));
}
for (ListenableFuture<Session> future : futures) {
sessions.add(future.get(10, TimeUnit.SECONDS).getName());
}
}
return sessions;
}
private static void runWave(
GcpManagedChannel pool,
ExecutorService executor,
Variant variant,
List<String> sessions,
long seed,
int transactionCount,
int fixedRpcCount,
int transactionOffset)
throws Exception {
CountDownLatch start = new CountDownLatch(1);
List<Future<?>> transactions = new ArrayList<>(transactionCount);
for (int transaction = 0; transaction < transactionCount; transaction++) {
final int transactionId = transactionOffset + transaction;
transactions.add(
executor.submit(
() -> {
start.await();
int rpcCount =
fixedRpcCount > 0
? fixedRpcCount
: 1 + Math.floorMod(mix(seed + transactionId), 5);
ChannelAffinityRef affinityRef = new ChannelAffinityRef();
String session =
sessions.isEmpty()
? "projects/p/instances/i/databases/d/sessions/bootstrap"
: sessions.get(Math.floorMod(mix(seed ^ transactionId), sessions.size()));
List<ListenableFuture<ResultSet>> calls = new ArrayList<>(rpcCount);
for (int rpc = 0; rpc < rpcCount; rpc++) {
CallOptions callOptions = CallOptions.DEFAULT;
if (variant == Variant.REF_6120) {
callOptions = callOptions.withOption(CHANNEL_AFFINITY_REF_KEY, affinityRef);
} else {
int route = Math.floorMod(mix(seed + transactionId * 31L), 100);
if (route < 9) {
callOptions =
callOptions.withOption(
GcpManagedChannel.AFFINITY_KEY,
"projects/p/instances/i/databases/d/sessions/multiplexed");
} else if (route >= 29) {
callOptions =
callOptions.withOption(GcpManagedChannel.DISABLE_AFFINITY_KEY, true);
}
}
calls.add(
ClientCalls.futureUnaryCall(
pool.newCall(EXECUTE_SQL_METHOD, callOptions),
ExecuteSqlRequest.newBuilder()
.setSession(session)
.setSql("SELECT 1")
.build()));
}
for (ListenableFuture<ResultSet> call : calls) {
call.get(10, TimeUnit.SECONDS);
}
return null;
}));
}
start.countDown();
for (Future<?> transaction : transactions) {
transaction.get(30, TimeUnit.SECONDS);
}
}
private static ApiConfig spannerAffinityConfig() {
AffinityConfig bind =
AffinityConfig.newBuilder()
.setCommand(AffinityConfig.Command.BIND)
.setAffinityKey("name")
.build();
AffinityConfig bound =
AffinityConfig.newBuilder()
.setCommand(AffinityConfig.Command.BOUND)
.setAffinityKey("session")
.build();
return ApiConfig.newBuilder()
.addMethod(
MethodConfig.newBuilder()
.addName(CREATE_SESSION_METHOD.getFullMethodName())
.setAffinity(bind))
.addMethod(
MethodConfig.newBuilder()
.addName(EXECUTE_SQL_METHOD.getFullMethodName())
.setAffinity(bound))
.build();
}
private static int mix(long value) {
value = (value ^ (value >>> 33)) * 0xff51afd7ed558ccdL;
value = (value ^ (value >>> 33)) * 0xc4ceb9fe1a85ec53L;
return (int) (value ^ (value >>> 33));
}
private enum Variant {
REF_6120,
KEY_6117
}
private enum LoadShape {
BURSTY,
CONSTANT
}
private static final class RecordingChannelBuilder
extends ManagedChannelBuilder<RecordingChannelBuilder> {
private final ScheduledExecutorService responses;
private final long seed;
private final AtomicInteger nextId = new AtomicInteger();
private final AtomicInteger nextSession = new AtomicInteger();
private final AtomicLong nextCall = new AtomicLong();
private final Map<Integer, ChannelStats> stats = new ConcurrentHashMap<>();
private RecordingChannelBuilder(ScheduledExecutorService responses, long seed) {
this.responses = responses;
this.seed = seed;
}
@Override
public ManagedChannel build() {
int id = nextId.getAndIncrement();
ChannelStats channelStats = new ChannelStats(id);
stats.put(id, channelStats);
return new RecordingManagedChannel(id, channelStats, responses, nextSession, nextCall, seed);
}
private void resetMeasurements() {
for (ChannelStats channelStats : stats.values()) {
channelStats.completed.set(0);
channelStats.maxActive.set(channelStats.active.get());
}
}
private void resetSelections() {
for (ChannelStats channelStats : stats.values()) {
channelStats.selected.set(0);
}
}
private String formatSelections(long runSeed, Set<Integer> candidateIds) {
List<ChannelStats> candidates = new ArrayList<>();
for (int id : candidateIds) {
candidates.add(stats.get(id));
}
candidates.sort(Comparator.comparingInt(candidate -> candidate.id));
ChannelStats hot =
Collections.max(
candidates, Comparator.comparingLong(candidate -> candidate.selected.get()));
long[] selected =
candidates.stream().mapToLong(candidate -> candidate.selected.get()).sorted().toArray();
double median =
selected.length % 2 == 0
? (selected[selected.length / 2 - 1] + selected[selected.length / 2]) / 2.0
: selected[selected.length / 2];
StringBuilder distribution = new StringBuilder();
for (ChannelStats candidate : candidates) {
if (distribution.length() > 0) {
distribution.append(',');
}
distribution.append(candidate.id).append('=').append(candidate.selected.get());
}
return String.format(
Locale.ROOT,
"HOTCHANNEL_EARLIEST seed=%d poolAfterDip=%d hotId=%d hotSelected=%d median=%.1f "
+ "hotToMedian=%.3f preparedDistribution=[%s]",
runSeed,
candidateIds.size(),
hot.id,
hot.selected.get(),
median,
hot.selected.get() / median,
distribution);
}
private RunResult snapshot(
long runSeed,
Variant variant,
LoadShape loadShape,
int activePoolSize,
Set<Integer> activeIds) {
List<ChannelStatsSnapshot> snapshots = new ArrayList<>();
for (int id : activeIds) {
ChannelStats channelStats = stats.get(id);
snapshots.add(
new ChannelStatsSnapshot(
id, channelStats.completed.get(), channelStats.maxActive.get()));
}
snapshots.sort(Comparator.comparingInt(snapshot -> snapshot.id));
return new RunResult(runSeed, variant, loadShape, activePoolSize, nextId.get(), snapshots);
}
@Override
public RecordingChannelBuilder directExecutor() {
return this;
}
@Override
public RecordingChannelBuilder executor(Executor executor) {
return this;
}
@Override
public RecordingChannelBuilder intercept(List<ClientInterceptor> interceptors) {
return this;
}
@Override
public RecordingChannelBuilder intercept(ClientInterceptor... interceptors) {
return this;
}
@Override
public RecordingChannelBuilder userAgent(String userAgent) {
return this;
}
@Override
public RecordingChannelBuilder overrideAuthority(String authority) {
return this;
}
@Override
public RecordingChannelBuilder nameResolverFactory(Factory resolverFactory) {
return this;
}
@Override
public RecordingChannelBuilder decompressorRegistry(DecompressorRegistry registry) {
return this;
}
@Override
public RecordingChannelBuilder compressorRegistry(CompressorRegistry registry) {
return this;
}
@Override
public RecordingChannelBuilder idleTimeout(long value, TimeUnit unit) {
return this;
}
}
private static final class RecordingManagedChannel extends ManagedChannel {
private final int id;
private final ChannelStats stats;
private final ScheduledExecutorService responses;
private final AtomicInteger nextSession;
private final AtomicLong nextCall;
private final long seed;
private final AtomicBoolean shutdown = new AtomicBoolean();
private RecordingManagedChannel(
int id,
ChannelStats stats,
ScheduledExecutorService responses,
AtomicInteger nextSession,
AtomicLong nextCall,
long seed) {
this.id = id;
this.stats = stats;
this.responses = responses;
this.nextSession = nextSession;
this.nextCall = nextCall;
this.seed = seed;
}
@Override
public <ReqT, RespT> ClientCall<ReqT, RespT> newCall(
MethodDescriptor<ReqT, RespT> method, CallOptions callOptions) {
stats.selected.incrementAndGet();
return new RecordingClientCall<>(
id, stats, responses, nextSession, nextCall, seed, method, callOptions);
}
@Override
public ConnectivityState getState(boolean requestConnection) {
return shutdown.get() ? ConnectivityState.SHUTDOWN : ConnectivityState.READY;
}
@Override
public void notifyWhenStateChanged(ConnectivityState source, Runnable callback) {}
@Override
public ManagedChannel shutdown() {
shutdown.set(true);
return this;
}
@Override
public boolean isShutdown() {
return shutdown.get();
}
@Override
public boolean isTerminated() {
return shutdown.get();
}
@Override
public ManagedChannel shutdownNow() {
return shutdown();
}
@Override
public boolean awaitTermination(long timeout, TimeUnit unit) {
return shutdown.get();
}
@Override
public String authority() {
return "in-process-channel-" + id;
}
@Override
public void enterIdle() {}
}
private static final class RecordingClientCall<ReqT, RespT> extends ClientCall<ReqT, RespT> {
private final int channelId;
private final ChannelStats stats;
private final ScheduledExecutorService responses;
private final AtomicInteger nextSession;
private final AtomicLong nextCall;
private final long seed;
private final MethodDescriptor<ReqT, RespT> method;
private final Long latencyMillisOverride;
private final AtomicBoolean completed = new AtomicBoolean();
private Listener<RespT> listener;
private RecordingClientCall(
int channelId,
ChannelStats stats,
ScheduledExecutorService responses,
AtomicInteger nextSession,
AtomicLong nextCall,
long seed,
MethodDescriptor<ReqT, RespT> method,
CallOptions callOptions) {
this.channelId = channelId;
this.stats = stats;
this.responses = responses;
this.nextSession = nextSession;
this.nextCall = nextCall;
this.seed = seed;
this.method = method;
this.latencyMillisOverride = callOptions.getOption(TEST_LATENCY_MILLIS);
}
@Override
public void start(Listener<RespT> listener, Metadata headers) {
this.listener = listener;
int active = stats.active.incrementAndGet();
stats.maxActive.accumulateAndGet(active, Math::max);
}
@Override
public void request(int numMessages) {}
@Override
public void cancel(String message, Throwable cause) {
finish(Status.CANCELLED, null);
}
@Override
public void halfClose() {
long callId = nextCall.getAndIncrement();
long latencyMillis =
latencyMillisOverride == null
? 20 + Math.floorMod(mix(seed ^ callId), 11)
: latencyMillisOverride;
responses.schedule(
() -> finish(Status.OK, responseForMethod()), latencyMillis, TimeUnit.MILLISECONDS);
}