|
38 | 38 | import com.google.api.core.SettableApiFuture; |
39 | 39 | import com.google.api.gax.resumable.ChunkUploadRequest; |
40 | 40 | import com.google.api.gax.resumable.ChunkUploadResponse; |
| 41 | +import com.google.api.gax.resumable.QueryStatusRequest; |
| 42 | +import com.google.api.gax.resumable.QueryStatusResponse; |
41 | 43 | import com.google.api.gax.resumable.ResumableUploadStatus; |
| 44 | +import com.google.api.gax.rpc.ResumableUploadErrorClassifier.Category; |
42 | 45 | import com.google.common.util.concurrent.MoreExecutors; |
43 | 46 | import java.io.IOException; |
44 | 47 | import java.io.InputStream; |
@@ -57,6 +60,8 @@ final class ResumableUploadChunkCoordinator<ResponseT> { |
57 | 60 |
|
58 | 61 | private final UnaryCallable<ChunkUploadRequest, ChunkUploadResponse<ResponseT>> |
59 | 62 | uploadChunkCallable; |
| 63 | + private final UnaryCallable<QueryStatusRequest, QueryStatusResponse<ResponseT>> |
| 64 | + queryStatusCallable; |
60 | 65 | private final String uploadUrl; |
61 | 66 | private final RewindableStreamBuffer buffer; |
62 | 67 | private final ApiCallContext callContext; |
@@ -65,12 +70,15 @@ final class ResumableUploadChunkCoordinator<ResponseT> { |
65 | 70 |
|
66 | 71 | ResumableUploadChunkCoordinator( |
67 | 72 | UnaryCallable<ChunkUploadRequest, ChunkUploadResponse<ResponseT>> uploadChunkCallable, |
| 73 | + UnaryCallable<QueryStatusRequest, QueryStatusResponse<ResponseT>> queryStatusCallable, |
68 | 74 | String uploadUrl, |
69 | 75 | InputStream payload, |
70 | 76 | int chunkSize, |
71 | 77 | ApiCallContext callContext) { |
72 | 78 | this.uploadChunkCallable = |
73 | 79 | checkNotNull(uploadChunkCallable, "uploadChunkCallable must not be null"); |
| 80 | + this.queryStatusCallable = |
| 81 | + checkNotNull(queryStatusCallable, "queryStatusCallable must not be null"); |
74 | 82 | this.uploadUrl = checkNotNull(uploadUrl, "uploadUrl must not be null"); |
75 | 83 | checkNotNull(payload, "payload must not be null"); |
76 | 84 | this.callContext = checkNotNull(callContext, "callContext must not be null"); |
@@ -98,34 +106,25 @@ private void transmitChunk(long currentOffset) { |
98 | 106 | if (uploadResultFuture.isDone()) { |
99 | 107 | return; |
100 | 108 | } |
101 | | - |
102 | 109 | // Read the next chunk slice from the payload stream. |
103 | 110 | try { |
104 | 111 | buffer.fill(currentOffset); |
105 | | - } catch (IOException e) { |
106 | | - uploadResultFuture.setException(e); |
107 | | - return; |
| 112 | + dispatchCurrentChunk(); |
| 113 | + } catch (Throwable t) { |
| 114 | + uploadResultFuture.setException(t); |
108 | 115 | } |
| 116 | + } |
109 | 117 |
|
110 | | - // Determine if this is the final chunk and build the chunk request. |
111 | | - ChunkUploadRequest chunkRequest = |
112 | | - ChunkUploadRequest.newBuilder() |
113 | | - .setUploadUrl(uploadUrl) |
114 | | - .setPayload(buffer.getPayload()) |
115 | | - .setOffset(buffer.getBufferBaseOffset()) |
116 | | - .setFinal(buffer.isFinal()) |
117 | | - .build(); |
118 | | - |
119 | | - // Dispatch the chunk upload call and register the in-flight future for cancellation. |
120 | | - long chunkLength = chunkRequest.getPayload().length; |
121 | | - boolean isFinal = chunkRequest.isFinal(); |
| 118 | + private void dispatchCurrentChunk() { |
122 | 119 | try { |
| 120 | + ChunkUploadRequest chunkRequest = buildCurrentChunkRequest(); |
| 121 | + |
| 122 | + // Dispatch the chunk upload call and register the in-flight future for cancellation. |
123 | 123 | ApiFuture<ChunkUploadResponse<ResponseT>> chunkFuture = |
124 | 124 | uploadChunkCallable.futureCall(chunkRequest, callContext); |
125 | 125 | if (!tryRegisterInFlightFuture(chunkFuture)) { |
126 | 126 | return; |
127 | 127 | } |
128 | | - |
129 | 128 | ApiFutures.addCallback( |
130 | 129 | chunkFuture, |
131 | 130 | new ApiFutureCallback<ChunkUploadResponse<ResponseT>>() { |
@@ -134,16 +133,55 @@ public void onSuccess(ChunkUploadResponse<ResponseT> response) { |
134 | 133 | if (uploadResultFuture.isDone()) { |
135 | 134 | return; |
136 | 135 | } |
137 | | - long nextOffset = currentOffset + chunkLength; |
138 | | - if (response.getUploadStatus() == ResumableUploadStatus.FINAL) { |
139 | | - uploadResultFuture.set(response.getResponse()); |
140 | | - } else if (isFinal) { |
141 | | - uploadResultFuture.setException( |
142 | | - new IllegalStateException( |
143 | | - "Upload stream ended and final chunk was transmitted, but server returned" |
144 | | - + " incomplete status")); |
| 136 | + if (response.getUploadStatus() == ResumableUploadStatus.UNKNOWN) { |
| 137 | + recover(); |
145 | 138 | } else { |
146 | | - transmitChunk(nextOffset); |
| 139 | + onChunkUploaded(response); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public void onFailure(Throwable t) { |
| 145 | + if (t instanceof CancellationException || uploadResultFuture.isDone()) { |
| 146 | + return; |
| 147 | + } |
| 148 | + Category category = |
| 149 | + ResumableUploadErrorClassifier.classify(t, ResumableUploadCommand.UPLOAD); |
| 150 | + if (category == Category.RECOVERABLE) { |
| 151 | + recover(); |
| 152 | + } else { |
| 153 | + // Category.TRANSIENT errors reaching here have already exhausted their retry budget |
| 154 | + // in the underlying RetryingCallable and become fatal per protocol specification. |
| 155 | + uploadResultFuture.setException(t); |
| 156 | + } |
| 157 | + } |
| 158 | + }, |
| 159 | + MoreExecutors.directExecutor()); |
| 160 | + } catch (Throwable t) { |
| 161 | + uploadResultFuture.setException(t); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + private void recover() { |
| 166 | + try { |
| 167 | + // Dispatch the query status call and register the in-flight future for cancellation. |
| 168 | + ApiFuture<QueryStatusResponse<ResponseT>> queryFuture = |
| 169 | + queryStatusCallable.futureCall(QueryStatusRequest.create(uploadUrl), callContext); |
| 170 | + if (!tryRegisterInFlightFuture(queryFuture)) { |
| 171 | + return; |
| 172 | + } |
| 173 | + ApiFutures.addCallback( |
| 174 | + queryFuture, |
| 175 | + new ApiFutureCallback<QueryStatusResponse<ResponseT>>() { |
| 176 | + @Override |
| 177 | + public void onSuccess(QueryStatusResponse<ResponseT> queryResponse) { |
| 178 | + if (uploadResultFuture.isDone()) { |
| 179 | + return; |
| 180 | + } |
| 181 | + try { |
| 182 | + handleQueryResponse(queryResponse); |
| 183 | + } catch (Throwable t) { |
| 184 | + uploadResultFuture.setException(t); |
147 | 185 | } |
148 | 186 | } |
149 | 187 |
|
@@ -173,5 +211,50 @@ private boolean tryRegisterInFlightFuture(ApiFuture<?> future) { |
173 | 211 | } |
174 | 212 | return true; |
175 | 213 | } |
176 | | -} |
177 | 214 |
|
| 215 | + private void handleQueryResponse(QueryStatusResponse<ResponseT> queryResponse) |
| 216 | + throws IOException { |
| 217 | + if (queryResponse.getUploadStatus() == ResumableUploadStatus.UNKNOWN) { |
| 218 | + throw new IllegalStateException( |
| 219 | + "Query status response missing X-Goog-Upload-Status header for upload URL: " + uploadUrl); |
| 220 | + } |
| 221 | + if (queryResponse.getUploadStatus() == ResumableUploadStatus.FINAL) { |
| 222 | + onChunkUploaded( |
| 223 | + ChunkUploadResponse.create(ResumableUploadStatus.FINAL, queryResponse.getResponse())); |
| 224 | + return; |
| 225 | + } |
| 226 | + Long committedOffset = queryResponse.getCommittedOffset(); |
| 227 | + if (committedOffset == null) { |
| 228 | + throw new IllegalStateException( |
| 229 | + "Incomplete query status response did not include a committed offset for upload URL: " |
| 230 | + + uploadUrl); |
| 231 | + } |
| 232 | + buffer.realignTo(committedOffset); |
| 233 | + dispatchCurrentChunk(); |
| 234 | + } |
| 235 | + |
| 236 | + private void onChunkUploaded(ChunkUploadResponse<ResponseT> response) { |
| 237 | + long nextOffset = buffer.getBufferBaseOffset() + buffer.getPayloadLength(); |
| 238 | + if (response.getUploadStatus() == ResumableUploadStatus.FINAL) { |
| 239 | + uploadResultFuture.set(response.getResponse()); |
| 240 | + } else if (buffer.isFinal()) { |
| 241 | + uploadResultFuture.setException( |
| 242 | + new IllegalStateException( |
| 243 | + "Upload stream ended and final chunk was transmitted, but server returned" |
| 244 | + + " incomplete status for upload URL: " |
| 245 | + + uploadUrl)); |
| 246 | + } else { |
| 247 | + transmitChunk(nextOffset); |
| 248 | + } |
| 249 | + } |
| 250 | + |
| 251 | + private ChunkUploadRequest buildCurrentChunkRequest() { |
| 252 | + // Determine if this is the final chunk and build the chunk request. |
| 253 | + return ChunkUploadRequest.newBuilder() |
| 254 | + .setUploadUrl(uploadUrl) |
| 255 | + .setPayload(buffer.getPayload()) |
| 256 | + .setOffset(buffer.getBufferBaseOffset()) |
| 257 | + .setFinal(buffer.isFinal()) |
| 258 | + .build(); |
| 259 | + } |
| 260 | +} |