|
| 1 | +/* |
| 2 | + * Copyright 2022 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.google.cloud.bigquery.storage.v1; |
| 17 | + |
| 18 | +import com.google.api.core.ApiFuture; |
| 19 | +import com.google.api.gax.batching.FlowController; |
| 20 | +import com.google.auto.value.AutoValue; |
| 21 | +import javax.annotation.concurrent.GuardedBy; |
| 22 | + |
| 23 | +public class ConnectionWorkerPool { |
| 24 | + /* |
| 25 | + * Max allowed inflight requests in the stream. Method append is blocked at this. |
| 26 | + */ |
| 27 | + private final long maxInflightRequests; |
| 28 | + |
| 29 | + /* |
| 30 | + * Max allowed inflight bytes in the stream. Method append is blocked at this. |
| 31 | + */ |
| 32 | + private final long maxInflightBytes; |
| 33 | + |
| 34 | + /* |
| 35 | + * Behavior when inflight queue is exceeded. Only supports Block or Throw, default is Block. |
| 36 | + */ |
| 37 | + private final FlowController.LimitExceededBehavior limitExceededBehavior; |
| 38 | + |
| 39 | + /* |
| 40 | + * TraceId for debugging purpose. |
| 41 | + */ |
| 42 | + private final String traceId; |
| 43 | + |
| 44 | + /* |
| 45 | + * Tracks current inflight requests in the stream. |
| 46 | + */ |
| 47 | + @GuardedBy("lock") |
| 48 | + private long inflightRequests = 0; |
| 49 | + |
| 50 | + /* |
| 51 | + * Tracks current inflight bytes in the stream. |
| 52 | + */ |
| 53 | + @GuardedBy("lock") |
| 54 | + private long inflightBytes = 0; |
| 55 | + |
| 56 | + /* |
| 57 | + * Tracks how often the stream was closed due to a retriable error. Streaming will stop when the |
| 58 | + * count hits a threshold. Streaming should only be halted, if it isn't possible to establish a |
| 59 | + * connection. Keep track of the number of reconnections in succession. This will be reset if |
| 60 | + * a row is successfully called back. |
| 61 | + */ |
| 62 | + @GuardedBy("lock") |
| 63 | + private long conectionRetryCountWithoutCallback = 0; |
| 64 | + |
| 65 | + /* |
| 66 | + * If false, streamConnection needs to be reset. |
| 67 | + */ |
| 68 | + @GuardedBy("lock") |
| 69 | + private boolean streamConnectionIsConnected = false; |
| 70 | + |
| 71 | + /* |
| 72 | + * A boolean to track if we cleaned up inflight queue. |
| 73 | + */ |
| 74 | + @GuardedBy("lock") |
| 75 | + private boolean inflightCleanuped = false; |
| 76 | + |
| 77 | + /* |
| 78 | + * Indicates whether user has called Close() or not. |
| 79 | + */ |
| 80 | + @GuardedBy("lock") |
| 81 | + private boolean userClosed = false; |
| 82 | + |
| 83 | + /* |
| 84 | + * The final status of connection. Set to nonnull when connection is permanently closed. |
| 85 | + */ |
| 86 | + @GuardedBy("lock") |
| 87 | + private Throwable connectionFinalStatus = null; |
| 88 | + |
| 89 | + /* |
| 90 | + * Contains the updated TableSchema. |
| 91 | + */ |
| 92 | + @GuardedBy("lock") |
| 93 | + private TableSchema updatedSchema; |
| 94 | + |
| 95 | + /* |
| 96 | + * A client used to interact with BigQuery. |
| 97 | + */ |
| 98 | + private BigQueryWriteClient client; |
| 99 | + |
| 100 | + /* |
| 101 | + * If true, the client above is created by this writer and should be closed. |
| 102 | + */ |
| 103 | + private boolean ownsBigQueryWriteClient = false; |
| 104 | + |
| 105 | + /** Settings for connection pool. */ |
| 106 | + @AutoValue |
| 107 | + public abstract static class Settings { |
| 108 | + /** |
| 109 | + * The minimum connections each pool created before trying to reuse the previously created |
| 110 | + * connection in multiplexing mode. |
| 111 | + */ |
| 112 | + abstract int minConnectionsPerPool(); |
| 113 | + |
| 114 | + /** The maximum connections per connection pool. */ |
| 115 | + abstract int maxConnectionsPerPool(); |
| 116 | + |
| 117 | + public static Builder builder() { |
| 118 | + return new AutoValue_ConnectionWorkerPool_Settings.Builder() |
| 119 | + .setMinConnectionsPerPool(2) |
| 120 | + .setMaxConnectionsPerPool(10); |
| 121 | + } |
| 122 | + |
| 123 | + /** Builder for the options to config {@link ConnectionWorkerPool}. */ |
| 124 | + @AutoValue.Builder |
| 125 | + public abstract static class Builder { |
| 126 | + public abstract Builder setMinConnectionsPerPool(int value); |
| 127 | + |
| 128 | + public abstract Builder setMaxConnectionsPerPool(int value); |
| 129 | + |
| 130 | + public abstract Settings build(); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + /** Static setting for connection pool. */ |
| 135 | + private static Settings settings = Settings.builder().build(); |
| 136 | + |
| 137 | + public ConnectionWorkerPool( |
| 138 | + long maxInflightRequests, |
| 139 | + long maxInflightBytes, |
| 140 | + FlowController.LimitExceededBehavior limitExceededBehavior, |
| 141 | + String traceId, |
| 142 | + BigQueryWriteClient client, |
| 143 | + boolean ownsBigQueryWriteClient) { |
| 144 | + this.maxInflightRequests = maxInflightRequests; |
| 145 | + this.maxInflightBytes = maxInflightBytes; |
| 146 | + this.limitExceededBehavior = limitExceededBehavior; |
| 147 | + this.traceId = traceId; |
| 148 | + this.client = client; |
| 149 | + this.ownsBigQueryWriteClient = ownsBigQueryWriteClient; |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Sets static connection pool options. |
| 154 | + * |
| 155 | + * <p>Note: this method should be triggered prior to the construction of connection pool. |
| 156 | + */ |
| 157 | + public static void setOptions(Settings settings) { |
| 158 | + ConnectionWorkerPool.settings = settings; |
| 159 | + } |
| 160 | + |
| 161 | + /** Distributes the writing of a message to an underlying connection. */ |
| 162 | + public ApiFuture<AppendRowsResponse> append(StreamWriter streamWriter, ProtoRows rows) { |
| 163 | + throw new RuntimeException("Append is not implemented!"); |
| 164 | + } |
| 165 | + |
| 166 | + /** Distributes the writing of a message to an underlying connection. */ |
| 167 | + public ApiFuture<AppendRowsResponse> append( |
| 168 | + StreamWriter streamWriter, ProtoRows rows, long offset) { |
| 169 | + throw new RuntimeException("append with offset is not implemented on connection pool!"); |
| 170 | + } |
| 171 | + |
| 172 | + /** Close the stream writer. Shut down all resources. */ |
| 173 | + public void close(StreamWriter streamWriter) { |
| 174 | + throw new RuntimeException("close is implemented on connection pool"); |
| 175 | + } |
| 176 | +} |
0 commit comments