/
ConnectionWorker.java
1132 lines (1030 loc) · 41.3 KB
/
ConnectionWorker.java
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 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.bigquery.storage.v1;
import com.google.api.core.ApiFuture;
import com.google.api.core.SettableApiFuture;
import com.google.api.gax.batching.FlowController;
import com.google.api.gax.rpc.FixedHeaderProvider;
import com.google.auto.value.AutoValue;
import com.google.cloud.bigquery.storage.v1.AppendRowsRequest.ProtoData;
import com.google.cloud.bigquery.storage.v1.Exceptions.AppendSerializationError;
import com.google.cloud.bigquery.storage.v1.StreamConnection.DoneCallback;
import com.google.cloud.bigquery.storage.v1.StreamConnection.RequestCallback;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
import com.google.common.util.concurrent.Uninterruptibles;
import com.google.protobuf.Int64Value;
import io.grpc.Status;
import io.grpc.Status.Code;
import io.grpc.StatusRuntimeException;
import java.io.IOException;
import java.time.Duration;
import java.time.Instant;
import java.util.Comparator;
import java.util.Deque;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.concurrent.GuardedBy;
/**
* A BigQuery Stream Writer that can be used to write data into BigQuery Table.
*
* <p>TODO: Support batching.
*
* <p>TODO: support updated schema
*/
class ConnectionWorker implements AutoCloseable {
private static final Logger log = Logger.getLogger(StreamWriter.class.getName());
// Maximum wait time on inflight quota before error out.
private static long INFLIGHT_QUOTA_MAX_WAIT_TIME_MILLI = 300000;
/*
* Maximum time waiting for request callback before shutting down the connection.
*
* We will constantly checking how much time we have been waiting for the next request callback
* if we wait too much time we will start shutting down the connections and clean up the queues.
*/
static Duration MAXIMUM_REQUEST_CALLBACK_WAIT_TIME = Duration.ofMinutes(5);
private Lock lock;
private Condition hasMessageInWaitingQueue;
private Condition inflightReduced;
private static Duration maxRetryDuration = Duration.ofMinutes(5);
private ExecutorService threadPool = Executors.newFixedThreadPool(1);
/*
* The identifier of the current stream to write to. This stream name can change during
* multiplexing.
*/
private String streamName;
/*
* The location of this connection.
*/
private String location = null;
/*
* The proto schema of rows to write. This schema can change during multiplexing.
*/
private ProtoSchema writerSchema;
/*
* Max allowed inflight requests in the stream. Method append is blocked at this.
*/
private final long maxInflightRequests;
/*
* Max allowed inflight bytes in the stream. Method append is blocked at this.
*/
private final long maxInflightBytes;
/*
* Behavior when inflight queue is exceeded. Only supports Block or Throw, default is Block.
*/
private final FlowController.LimitExceededBehavior limitExceededBehavior;
/*
* TraceId for debugging purpose.
*/
private final String traceId;
/*
* Tracks current inflight requests in the stream.
*/
@GuardedBy("lock")
private long inflightRequests = 0;
/*
* Tracks current inflight bytes in the stream.
*/
@GuardedBy("lock")
private long inflightBytes = 0;
/*
* Tracks how often the stream was closed due to a retriable error. Streaming will stop when the
* count hits a threshold. Streaming should only be halted, if it isn't possible to establish a
* connection. Keep track of the number of reconnections in succession. This will be reset if
* a row is successfully called back.
*/
@GuardedBy("lock")
private long conectionRetryCountWithoutCallback = 0;
@GuardedBy("lock")
private long connectionRetryStartTime = 0;
/*
* If false, streamConnection needs to be reset.
*/
@GuardedBy("lock")
private boolean streamConnectionIsConnected = false;
/*
* A boolean to track if we cleaned up inflight queue.
*/
@GuardedBy("lock")
private boolean inflightCleanuped = false;
/*
* Indicates whether user has called Close() or not.
*/
@GuardedBy("lock")
private boolean userClosed = false;
/*
* The final status of connection. Set to nonnull when connection is permanently closed.
*/
@GuardedBy("lock")
private Throwable connectionFinalStatus = null;
/*
* Contains requests buffered in the client and not yet sent to server.
*/
@GuardedBy("lock")
private final Deque<AppendRequestAndResponse> waitingRequestQueue;
/*
* Contains sent append requests waiting for response from server.
*/
@GuardedBy("lock")
private final Deque<AppendRequestAndResponse> inflightRequestQueue;
/*
* Tracks number of destinations handled by this connection.
*/
private final Set<String> destinationSet = ConcurrentHashMap.newKeySet();
/*
* Contains the updated TableSchema.
*/
@GuardedBy("lock")
private TableSchemaAndTimestamp updatedSchema;
/*
* A client used to interact with BigQuery.
*/
private BigQueryWriteClient client;
/*
* Wraps the underlying bi-directional stream connection with server.
*/
private StreamConnection streamConnection;
/*
* A separate thread to handle actual communication with server.
*/
private Thread appendThread;
/*
* The inflight wait time for the previous sent request.
*/
private final AtomicLong inflightWaitSec = new AtomicLong(0);
/*
* A String that uniquely identifies this writer.
*/
private final String writerId = UUID.randomUUID().toString();
/*
* Test only exception behavior testing params.
*/
private RuntimeException testOnlyRunTimeExceptionInAppendLoop = null;
private long testOnlyAppendLoopSleepTime = 0;
private static String projectMatching = "projects/[^/]+/";
private static Pattern streamPatternProject = Pattern.compile(projectMatching);
/** The maximum size of one request. Defined by the API. */
public static long getApiMaxRequestBytes() {
return 10L * 1000L * 1000L; // 10 megabytes (https://en.wikipedia.org/wiki/Megabyte)
}
static String extractProjectName(String streamName) {
Matcher streamMatcher = streamPatternProject.matcher(streamName);
if (streamMatcher.find()) {
return streamMatcher.group();
} else {
throw new IllegalStateException(
String.format("The passed in stream name does not match standard format %s", streamName));
}
}
static String getRoutingHeader(String streamName, String location) {
String project = extractProjectName(streamName);
return project + "locations/" + location;
}
public ConnectionWorker(
String streamName,
String location,
ProtoSchema writerSchema,
long maxInflightRequests,
long maxInflightBytes,
Duration maxRetryDuration,
FlowController.LimitExceededBehavior limitExceededBehavior,
String traceId,
BigQueryWriteSettings clientSettings)
throws IOException {
this.lock = new ReentrantLock();
this.hasMessageInWaitingQueue = lock.newCondition();
this.inflightReduced = lock.newCondition();
this.streamName = streamName;
if (location != null && !location.isEmpty()) {
this.location = location;
}
this.maxRetryDuration = maxRetryDuration;
if (writerSchema == null) {
throw new StatusRuntimeException(
Status.fromCode(Code.INVALID_ARGUMENT)
.withDescription("Writer schema must be provided when building this writer."));
}
this.maxInflightRequests = maxInflightRequests;
this.maxInflightBytes = maxInflightBytes;
this.limitExceededBehavior = limitExceededBehavior;
this.traceId = traceId;
this.waitingRequestQueue = new LinkedList<AppendRequestAndResponse>();
this.inflightRequestQueue = new LinkedList<AppendRequestAndResponse>();
// Always recreate a client for connection worker.
HashMap<String, String> newHeaders = new HashMap<>();
newHeaders.putAll(clientSettings.toBuilder().getHeaderProvider().getHeaders());
if (this.location == null) {
newHeaders.put("x-goog-request-params", "write_stream=" + this.streamName);
} else {
newHeaders.put(
"x-goog-request-params",
"write_location=" + getRoutingHeader(this.streamName, this.location));
}
BigQueryWriteSettings stubSettings =
clientSettings
.toBuilder()
.setHeaderProvider(FixedHeaderProvider.create(newHeaders))
.build();
this.client = BigQueryWriteClient.create(clientSettings);
this.appendThread =
new Thread(
new Runnable() {
@Override
public void run() {
appendLoop();
}
});
appendThread.setUncaughtExceptionHandler(
(Thread t, Throwable e) -> {
log.warning(
"Exception thrown from append loop, thus stream writer is shutdown due to exception: "
+ e.toString());
lock.lock();
try {
connectionFinalStatus = e;
// Move all current waiting requests to in flight queue.
while (!this.waitingRequestQueue.isEmpty()) {
AppendRequestAndResponse requestWrapper = this.waitingRequestQueue.pollFirst();
this.inflightRequestQueue.addLast(requestWrapper);
}
} finally {
lock.unlock();
}
cleanupInflightRequests();
});
this.appendThread.start();
}
private void resetConnection() {
log.info("Start connecting stream: " + streamName + " id: " + writerId);
if (this.streamConnection != null) {
// It's safe to directly close the previous connection as the in flight messages
// will be picked up by the next connection.
this.streamConnection.close();
Uninterruptibles.sleepUninterruptibly(
calculateSleepTimeMilli(conectionRetryCountWithoutCallback), TimeUnit.MILLISECONDS);
}
this.streamConnection =
new StreamConnection(
this.client,
new RequestCallback() {
@Override
public void run(AppendRowsResponse response) {
requestCallback(response);
}
},
new DoneCallback() {
@Override
public void run(Throwable finalStatus) {
doneCallback(finalStatus);
}
});
log.info("Finish connecting stream: " + streamName + " id: " + writerId);
}
/** Schedules the writing of rows at given offset. */
ApiFuture<AppendRowsResponse> append(StreamWriter streamWriter, ProtoRows rows, long offset) {
if (this.location != null && this.location != streamWriter.getLocation()) {
throw new StatusRuntimeException(
Status.fromCode(Code.INVALID_ARGUMENT)
.withDescription(
"StreamWriter with location "
+ streamWriter.getLocation()
+ " is scheduled to use a connection with location "
+ this.location));
} else if (this.location == null && streamWriter.getStreamName() != this.streamName) {
// Location is null implies this is non-multiplexed connection.
throw new StatusRuntimeException(
Status.fromCode(Code.INVALID_ARGUMENT)
.withDescription(
"StreamWriter with stream name "
+ streamWriter.getStreamName()
+ " is scheduled to use a connection with stream name "
+ this.streamName));
}
Preconditions.checkNotNull(streamWriter);
AppendRowsRequest.Builder requestBuilder = AppendRowsRequest.newBuilder();
requestBuilder.setProtoRows(
ProtoData.newBuilder()
.setWriterSchema(streamWriter.getProtoSchema())
.setRows(rows)
.build());
if (offset >= 0) {
requestBuilder.setOffset(Int64Value.of(offset));
}
requestBuilder.setWriteStream(streamWriter.getStreamName());
requestBuilder.putAllMissingValueInterpretations(
streamWriter.getMissingValueInterpretationMap());
return appendInternal(streamWriter, requestBuilder.build());
}
Boolean isUserClosed() {
this.lock.lock();
try {
return userClosed;
} finally {
this.lock.unlock();
}
}
String getWriteLocation() {
return this.location;
}
private ApiFuture<AppendRowsResponse> appendInternal(
StreamWriter streamWriter, AppendRowsRequest message) {
AppendRequestAndResponse requestWrapper = new AppendRequestAndResponse(message, streamWriter);
if (requestWrapper.messageSize > getApiMaxRequestBytes()) {
requestWrapper.appendResult.setException(
new StatusRuntimeException(
Status.fromCode(Code.INVALID_ARGUMENT)
.withDescription(
"MessageSize is too large. Max allow: "
+ getApiMaxRequestBytes()
+ " Actual: "
+ requestWrapper.messageSize)));
return requestWrapper.appendResult;
}
this.lock.lock();
try {
if (userClosed) {
requestWrapper.appendResult.setException(
new Exceptions.StreamWriterClosedException(
Status.fromCode(Status.Code.FAILED_PRECONDITION)
.withDescription("Connection is already closed during append"),
streamName,
writerId));
return requestWrapper.appendResult;
}
// Check if queue is going to be full before adding the request.
if (this.limitExceededBehavior == FlowController.LimitExceededBehavior.ThrowException) {
if (this.inflightRequests + 1 >= this.maxInflightRequests) {
throw new Exceptions.InflightRequestsLimitExceededException(
writerId, this.maxInflightRequests);
}
if (this.inflightBytes + requestWrapper.messageSize >= this.maxInflightBytes) {
throw new Exceptions.InflightBytesLimitExceededException(writerId, this.maxInflightBytes);
}
}
if (connectionFinalStatus != null) {
requestWrapper.appendResult.setException(
new Exceptions.StreamWriterClosedException(
Status.fromCode(Status.Code.FAILED_PRECONDITION)
.withDescription(
"Connection is closed due to " + connectionFinalStatus.toString()),
streamName,
writerId));
return requestWrapper.appendResult;
}
++this.inflightRequests;
this.inflightBytes += requestWrapper.messageSize;
waitingRequestQueue.addLast(requestWrapper);
hasMessageInWaitingQueue.signal();
try {
maybeWaitForInflightQuota();
} catch (StatusRuntimeException ex) {
--this.inflightRequests;
waitingRequestQueue.pollLast();
this.inflightBytes -= requestWrapper.messageSize;
throw ex;
}
return requestWrapper.appendResult;
} finally {
this.lock.unlock();
}
}
@GuardedBy("lock")
private void maybeWaitForInflightQuota() {
long start_time = System.currentTimeMillis();
while (this.inflightRequests >= this.maxInflightRequests
|| this.inflightBytes >= this.maxInflightBytes) {
try {
inflightReduced.await(100, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
log.warning(
"Interrupted while waiting for inflight quota. Stream: "
+ streamName
+ " Error: "
+ e.toString());
throw new StatusRuntimeException(
Status.fromCode(Code.CANCELLED)
.withCause(e)
.withDescription("Interrupted while waiting for quota."));
}
long current_wait_time = System.currentTimeMillis() - start_time;
if (current_wait_time > INFLIGHT_QUOTA_MAX_WAIT_TIME_MILLI) {
throw new StatusRuntimeException(
Status.fromCode(Code.CANCELLED)
.withDescription(
String.format(
"Interrupted while waiting for quota due to long waiting time %sms",
current_wait_time)));
}
}
inflightWaitSec.set((System.currentTimeMillis() - start_time) / 1000);
}
@VisibleForTesting
static long calculateSleepTimeMilli(long retryCount) {
return Math.min((long) Math.pow(2, retryCount), 60000);
}
@VisibleForTesting
void setTestOnlyAppendLoopSleepTime(long testOnlyAppendLoopSleepTime) {
this.testOnlyAppendLoopSleepTime = testOnlyAppendLoopSleepTime;
}
@VisibleForTesting
void setTestOnlyRunTimeExceptionInAppendLoop(
RuntimeException testOnlyRunTimeExceptionInAppendLoop) {
this.testOnlyRunTimeExceptionInAppendLoop = testOnlyRunTimeExceptionInAppendLoop;
}
public long getInflightWaitSeconds() {
return inflightWaitSec.longValue();
}
/** @return a unique Id for the writer. */
public String getWriterId() {
return writerId;
}
boolean isConnectionInUnrecoverableState() {
this.lock.lock();
try {
// If final status is set, there's no
return connectionFinalStatus != null;
} finally {
this.lock.unlock();
}
}
/** Close the stream writer. Shut down all resources. */
@Override
public void close() {
log.info("User closing stream: " + streamName);
this.lock.lock();
try {
this.userClosed = true;
} finally {
this.lock.unlock();
}
log.info("Waiting for append thread to finish. Stream: " + streamName + " id: " + writerId);
try {
appendThread.join();
} catch (InterruptedException e) {
// Unexpected. Just swallow the exception with logging.
log.warning(
"Append handler join is interrupted. Stream: "
+ streamName
+ " id: "
+ writerId
+ " Error: "
+ e.toString());
}
this.client.close();
try {
// Backend request has a 2 minute timeout, so wait a little longer than that.
this.client.awaitTermination(150, TimeUnit.SECONDS);
} catch (InterruptedException ignored) {
log.warning("Client await termination timeout in writer id " + writerId);
}
try {
log.fine(
"Begin shutting down user callback thread pool for stream "
+ streamName
+ " id: "
+ writerId);
threadPool.shutdown();
threadPool.awaitTermination(3, TimeUnit.MINUTES);
} catch (InterruptedException e) {
// Unexpected. Just swallow the exception with logging.
log.warning(
"Close on thread pool for "
+ streamName
+ " id: "
+ writerId
+ " is interrupted with exception: "
+ e.toString());
throw new IllegalStateException(
"Thread pool shutdown is interrupted for stream " + streamName);
}
log.info("User close finishes for stream " + streamName);
}
/*
* This loop is executed in a separate thread.
*
* It takes requests from waiting queue and sends them to server.
*/
private void appendLoop() {
Deque<AppendRequestAndResponse> localQueue = new LinkedList<AppendRequestAndResponse>();
boolean streamNeedsConnecting = false;
// Indicate whether we are at the first request after switching destination.
// True means the schema and other metadata are needed.
boolean firstRequestForTableOrSchemaSwitch = true;
// Represent whether we have entered multiplexing.
boolean isMultiplexing = false;
while (!waitingQueueDrained()) {
this.lock.lock();
try {
hasMessageInWaitingQueue.await(100, TimeUnit.MILLISECONDS);
// Check whether we should error out the current append loop.
if (inflightRequestQueue.size() > 0) {
throwIfWaitCallbackTooLong(inflightRequestQueue.getFirst().requestCreationTimeStamp);
}
// Copy the streamConnectionIsConnected guarded by lock to a local variable.
// In addition, only reconnect if there is a retriable error.
streamNeedsConnecting = !streamConnectionIsConnected && connectionFinalStatus == null;
if (streamNeedsConnecting) {
// If the stream connection is broken, any requests on inflightRequestQueue will need
// to be resent, as the new connection has no knowledge of the requests. Copy the requests
// from inflightRequestQueue and prepent them onto the waitinRequestQueue. They need to be
// prepended as they need to be sent before new requests.
while (!inflightRequestQueue.isEmpty()) {
waitingRequestQueue.addFirst(inflightRequestQueue.pollLast());
}
}
while (!this.waitingRequestQueue.isEmpty()) {
AppendRequestAndResponse requestWrapper = this.waitingRequestQueue.pollFirst();
requestWrapper.trySetRequestInsertQueueTime();
this.inflightRequestQueue.addLast(requestWrapper);
localQueue.addLast(requestWrapper);
}
} catch (InterruptedException e) {
log.warning(
"Interrupted while waiting for message. Stream: "
+ streamName
+ " id: "
+ writerId
+ " Error: "
+ e.toString());
} finally {
this.lock.unlock();
}
if (localQueue.isEmpty()) {
continue;
}
if (streamNeedsConnecting) {
// Set streamConnectionIsConnected to true, to indicate the stream has been connected. This
// should happen before the call to resetConnection. As it is unknown when the connection
// could be closed and the doneCallback called, and thus clearing the flag.
lock.lock();
try {
this.streamConnectionIsConnected = true;
} finally {
lock.unlock();
}
if (testOnlyRunTimeExceptionInAppendLoop != null) {
Uninterruptibles.sleepUninterruptibly(testOnlyAppendLoopSleepTime, TimeUnit.MILLISECONDS);
throw testOnlyRunTimeExceptionInAppendLoop;
}
resetConnection();
// Set firstRequestInConnection to indicate the next request to be sent should include
// metedata. Reset everytime after reconnection.
firstRequestForTableOrSchemaSwitch = true;
}
while (!localQueue.isEmpty()) {
AppendRowsRequest originalRequest = localQueue.pollFirst().message;
AppendRowsRequest.Builder originalRequestBuilder = originalRequest.toBuilder();
// Always respect the first writer schema seen by the loop.
if (writerSchema == null) {
writerSchema = originalRequest.getProtoRows().getWriterSchema();
}
// Consider we enter multiplexing if we met a different non empty stream name or we meet
// a new schema for the same stream name.
// For the schema comparision we don't use message differencer to speed up the comparing
// process. `equals(...)` can bring us false positive, e.g. two repeated field can be
// considered the same but is not considered equals(). However as long as it's never provide
// false negative we will always correctly pass writer schema to backend.
if ((!originalRequest.getWriteStream().isEmpty()
&& !streamName.isEmpty()
&& !originalRequest.getWriteStream().equals(streamName))
|| (originalRequest.getProtoRows().hasWriterSchema()
&& !originalRequest.getProtoRows().getWriterSchema().equals(writerSchema))) {
streamName = originalRequest.getWriteStream();
writerSchema = originalRequest.getProtoRows().getWriterSchema();
isMultiplexing = true;
firstRequestForTableOrSchemaSwitch = true;
}
if (firstRequestForTableOrSchemaSwitch) {
// If we are at the first request for every table switch, including the first request in
// the connection, we will attach both stream name and table schema to the request.
destinationSet.add(streamName);
if (this.traceId != null) {
originalRequestBuilder.setTraceId(this.traceId);
}
} else if (!isMultiplexing) {
// If we are not in multiplexing and not in the first request, clear the stream name.
originalRequestBuilder.clearWriteStream();
}
// During non table/schema switch requests, clear writer schema.
if (!firstRequestForTableOrSchemaSwitch) {
originalRequestBuilder.getProtoRowsBuilder().clearWriterSchema();
}
firstRequestForTableOrSchemaSwitch = false;
// Send should only throw an exception if there is a problem with the request. The catch
// block will handle this case, and return the exception with the result.
// Otherwise send will return:
// SUCCESS: Message was sent, wait for the callback.
// STREAM_CLOSED: Stream was closed, normally or due to en error
// NOT_ENOUGH_QUOTA: Message wasn't sent due to not enough quota.
// TODO: Handle NOT_ENOUGH_QUOTA.
// In the close case, the request is in the inflight queue, and will either be returned
// to the user with an error, or will be resent.
this.streamConnection.send(originalRequestBuilder.build());
}
}
log.info(
"Cleanup starts. Stream: "
+ streamName
+ " id: "
+ writerId
+ " userClose: "
+ userClosed
+ " final exception: "
+ (this.connectionFinalStatus == null
? "null"
: this.connectionFinalStatus.toString()));
// At this point, the waiting queue is drained, so no more requests.
// We can close the stream connection and handle the remaining inflight requests.
if (streamConnection != null) {
this.streamConnection.close();
waitForDoneCallback(3, TimeUnit.MINUTES);
}
// At this point, there cannot be more callback. It is safe to clean up all inflight requests.
log.info(
"Stream connection is fully closed. Cleaning up inflight requests. Stream: "
+ streamName
+ " id: "
+ writerId);
cleanupInflightRequests();
log.info("Append thread is done. Stream: " + streamName + " id: " + writerId);
}
private void throwIfWaitCallbackTooLong(Instant timeToCheck) {
Duration milliSinceLastCallback = Duration.between(timeToCheck, Instant.now());
if (milliSinceLastCallback.compareTo(MAXIMUM_REQUEST_CALLBACK_WAIT_TIME) > 0) {
throw new RuntimeException(
String.format(
"Request has waited in inflight queue for %sms for writer %s, "
+ "which is over maximum wait time %s",
milliSinceLastCallback, writerId, MAXIMUM_REQUEST_CALLBACK_WAIT_TIME.toString()));
}
}
/*
* Returns true if waiting queue is drain, a.k.a. no more requests in the waiting queue.
*
* It serves as a signal to append thread that there cannot be any more requests in the waiting
* queue and it can prepare to stop.
*/
private boolean waitingQueueDrained() {
this.lock.lock();
try {
return (this.userClosed || this.connectionFinalStatus != null)
&& this.waitingRequestQueue.isEmpty();
} finally {
this.lock.unlock();
}
}
private void waitForDoneCallback(long duration, TimeUnit timeUnit) {
log.fine(
"Waiting for done callback from stream connection. Stream: "
+ streamName
+ " id: "
+ writerId);
long deadline = System.nanoTime() + timeUnit.toNanos(duration);
while (System.nanoTime() <= deadline) {
this.lock.lock();
try {
if (!this.streamConnectionIsConnected) {
// Done callback is received, return.
return;
}
} finally {
this.lock.unlock();
}
Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
}
this.lock.lock();
try {
log.warning("Donecallback is not triggered within timeout frame for writer " + writerId);
if (connectionFinalStatus == null) {
connectionFinalStatus =
new StatusRuntimeException(
Status.fromCode(Code.CANCELLED)
.withDescription("Timeout waiting for DoneCallback."));
}
} finally {
this.lock.unlock();
}
return;
}
private void cleanupInflightRequests() {
Throwable finalStatus =
new Exceptions.StreamWriterClosedException(
Status.fromCode(Status.Code.FAILED_PRECONDITION)
.withDescription("Connection is already closed, cleanup inflight request"),
streamName,
writerId);
Deque<AppendRequestAndResponse> localQueue = new LinkedList<AppendRequestAndResponse>();
this.lock.lock();
try {
if (this.connectionFinalStatus != null) {
finalStatus = this.connectionFinalStatus;
}
while (!this.inflightRequestQueue.isEmpty()) {
localQueue.addLast(pollInflightRequestQueue());
}
this.inflightCleanuped = true;
} finally {
this.lock.unlock();
}
log.fine(
"Cleaning "
+ localQueue.size()
+ " inflight requests with error: "
+ finalStatus
+ " for Stream "
+ streamName
+ " id: "
+ writerId);
while (!localQueue.isEmpty()) {
localQueue.pollFirst().appendResult.setException(finalStatus);
}
}
private void requestCallback(AppendRowsResponse response) {
if (response.hasUpdatedSchema()) {
AppendRowsResponse responseWithUpdatedSchemaRemoved =
response.toBuilder().clearUpdatedSchema().build();
log.fine(
String.format(
"Got response with schema updated (omitting updated schema in response here): %s writer id %s",
responseWithUpdatedSchemaRemoved.toString(), writerId));
}
AppendRequestAndResponse requestWrapper;
this.lock.lock();
if (response.hasUpdatedSchema()) {
this.updatedSchema =
TableSchemaAndTimestamp.create(System.nanoTime(), response.getUpdatedSchema());
}
try {
// Had a successful connection with at least one result, reset retries.
// conectionRetryCountWithoutCallback is reset so that only multiple retries, without
// successful records sent, will cause the stream to fail.
if (conectionRetryCountWithoutCallback != 0) {
conectionRetryCountWithoutCallback = 0;
}
if (connectionRetryStartTime != 0) {
connectionRetryStartTime = 0;
}
if (!this.inflightRequestQueue.isEmpty()) {
requestWrapper = pollInflightRequestQueue();
} else if (inflightCleanuped) {
// It is possible when requestCallback is called, the inflight queue is already drained
// because we timed out waiting for done.
return;
} else {
// This is something not expected, we shouldn't have an empty inflight queue otherwise.
log.log(Level.WARNING, "Unexpected: request callback called on an empty inflight queue.");
connectionFinalStatus =
new StatusRuntimeException(
Status.fromCode(Code.FAILED_PRECONDITION)
.withDescription("Request callback called on an empty inflight queue."));
return;
}
} finally {
this.lock.unlock();
}
// We need a separte thread pool to unblock the next request callback.
// Otherwise user may call append inside request callback, which may be blocked on waiting
// on in flight quota, causing deadlock as requests can't be popped out of queue until
// the current request callback finishes.
threadPool.submit(
() -> {
if (response.hasError()) {
Exceptions.StorageException storageException =
Exceptions.toStorageException(response.getError(), null);
log.fine(String.format("Got error message: %s", response.toString()));
if (storageException != null) {
requestWrapper.appendResult.setException(storageException);
} else if (response.getRowErrorsCount() > 0) {
Map<Integer, String> rowIndexToErrorMessage = new HashMap<>();
for (int i = 0; i < response.getRowErrorsCount(); i++) {
RowError rowError = response.getRowErrors(i);
rowIndexToErrorMessage.put(
Math.toIntExact(rowError.getIndex()), rowError.getMessage());
}
AppendSerializationError exception =
new AppendSerializationError(
response.getError().getCode(),
response.getError().getMessage(),
streamName,
rowIndexToErrorMessage);
requestWrapper.appendResult.setException(exception);
} else {
StatusRuntimeException exception =
new StatusRuntimeException(
Status.fromCodeValue(response.getError().getCode())
.withDescription(response.getError().getMessage()));
requestWrapper.appendResult.setException(exception);
}
} else {
requestWrapper.appendResult.set(response);
}
});
}
private boolean isConnectionErrorRetriable(Throwable t) {
Status status = Status.fromThrowable(t);
return status.getCode() == Code.ABORTED
|| status.getCode() == Code.UNAVAILABLE
|| status.getCode() == Code.CANCELLED
|| status.getCode() == Code.INTERNAL
|| status.getCode() == Code.FAILED_PRECONDITION
|| status.getCode() == Code.DEADLINE_EXCEEDED;
}
private void doneCallback(Throwable finalStatus) {
log.info(
"Received done callback. Stream: "
+ streamName
+ " worker id: "
+ writerId
+ " Final status: "
+ finalStatus.toString());
this.lock.lock();
try {
this.streamConnectionIsConnected = false;
if (connectionFinalStatus == null) {
if (connectionRetryStartTime == 0) {
connectionRetryStartTime = System.currentTimeMillis();
}
// If the error can be retried, don't set it here, let it try to retry later on.
if (isConnectionErrorRetriable(finalStatus)
&& !userClosed
&& (maxRetryDuration.toMillis() == 0f
|| System.currentTimeMillis() - connectionRetryStartTime
<= maxRetryDuration.toMillis())) {
this.conectionRetryCountWithoutCallback++;
log.info(
"Retriable error "
+ finalStatus.toString()
+ " received, retry count "
+ conectionRetryCountWithoutCallback
+ ", millis left to retry "
+ (maxRetryDuration.toMillis()
- (System.currentTimeMillis() - connectionRetryStartTime))
+ ", for stream "
+ streamName
+ " id:"
+ writerId);
} else {
Exceptions.StorageException storageException = Exceptions.toStorageException(finalStatus);
this.connectionFinalStatus = storageException != null ? storageException : finalStatus;
log.info(
"Connection finished with error "
+ finalStatus.toString()
+ " for stream "
+ streamName
+ " with write id: "
+ writerId);
}
}
} finally {
this.lock.unlock();
}
}
@GuardedBy("lock")
private AppendRequestAndResponse pollInflightRequestQueue() {
AppendRequestAndResponse requestWrapper = this.inflightRequestQueue.pollFirst();
--this.inflightRequests;
this.inflightBytes -= requestWrapper.messageSize;
this.inflightReduced.signal();
return requestWrapper;
}
/** Thread-safe getter of updated TableSchema */
synchronized TableSchemaAndTimestamp getUpdatedSchema() {
return this.updatedSchema;
}
// Class that wraps AppendRowsRequest and its corresponding Response future.
static final class AppendRequestAndResponse {