Skip to content

Commit cdfffa5

Browse files
committed
Assert that x-goog-generation is present
1 parent 0886ba7 commit cdfffa5

File tree

4 files changed

+17
-2
lines changed

4 files changed

+17
-2
lines changed

modules/repository-gcs/src/main/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageRetryingInputStream.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,9 @@ private Long parseGenerationHeader(HttpResponse response) {
156156
return Long.parseLong(generationHeader);
157157
} catch (NumberFormatException e) {
158158
assert false : "Unexpected value for x-goog-generation header: " + generationHeader;
159-
return null;
160159
}
160+
} else {
161+
assert false : "Missing x-goog-generation header";
161162
}
162163
return null;
163164
}

modules/repository-gcs/src/test/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageBlobContainerRetriesTests.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.google.cloud.http.HttpTransportOptions;
2020
import com.google.cloud.storage.StorageException;
2121
import com.google.cloud.storage.StorageOptions;
22+
import com.sun.net.httpserver.HttpExchange;
2223
import com.sun.net.httpserver.HttpHandler;
2324

2425
import org.apache.http.HttpStatus;
@@ -216,6 +217,11 @@ public HttpRequestInitializer getHttpRequestInitializer(ServiceOptions<?, ?> ser
216217
);
217218
}
218219

220+
@Override
221+
protected void addSuccessfulDownloadHeaders(HttpExchange exchange) {
222+
exchange.getResponseHeaders().add("x-goog-generation", "1");
223+
}
224+
219225
public void testShouldRetryOnConnectionRefused() {
220226
// port 1 should never be open
221227
endpointUrlOverride = "http://127.0.0.1:1";
@@ -246,6 +252,7 @@ public void testReadLargeBlobWithRetries() throws Exception {
246252
httpServer.createContext(downloadStorageEndpoint(blobContainer, "large_blob_retries"), exchange -> {
247253
Streams.readFully(exchange.getRequestBody());
248254
exchange.getResponseHeaders().add("Content-Type", "application/octet-stream");
255+
addSuccessfulDownloadHeaders(exchange);
249256
final HttpHeaderParser.Range range = getRange(exchange);
250257
final int offset = Math.toIntExact(range.start());
251258
final byte[] chunk = Arrays.copyOfRange(bytes, offset, Math.toIntExact(Math.min(range.end() + 1, bytes.length)));

test/fixtures/gcs-fixture/src/main/java/fixture/gcs/GoogleCloudStorageHttpHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public void handle(final HttpExchange exchange) throws IOException {
144144
// we implement "metageneration", at that point we must incorporate both
145145
// See: https://cloud.google.com/storage/docs/metadata#etags
146146
exchange.getResponseHeaders().add("ETag", String.valueOf(blob.generation()));
147-
exchange.getResponseHeaders().add("X-Goog-Generation", String.valueOf(blob.generation()));
147+
exchange.getResponseHeaders().add("x-goog-generation", String.valueOf(blob.generation()));
148148
exchange.getResponseHeaders().add("Content-Type", "application/octet-stream");
149149
exchange.sendResponseHeaders(statusCode, response.length());
150150
response.writeTo(exchange.getResponseBody());

test/framework/src/main/java/org/elasticsearch/repositories/blobstore/AbstractBlobContainerRetriesTestCase.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ public void tearDown() throws Exception {
7373
super.tearDown();
7474
}
7575

76+
/**
77+
* Override to add any headers you expect on a successful download
78+
*/
79+
protected void addSuccessfulDownloadHeaders(HttpExchange exchange) {}
80+
7681
protected abstract String downloadStorageEndpoint(BlobContainer container, String blob);
7782

7883
protected abstract String bytesContentType();
@@ -118,6 +123,7 @@ public void testReadBlobWithRetries() throws Exception {
118123
if (countDown.countDown()) {
119124
final int rangeStart = getRangeStart(exchange);
120125
assertThat(rangeStart, lessThan(bytes.length));
126+
addSuccessfulDownloadHeaders(exchange);
121127
exchange.getResponseHeaders().add("Content-Type", bytesContentType());
122128
exchange.sendResponseHeaders(HttpStatus.SC_OK, bytes.length - rangeStart);
123129
exchange.getResponseBody().write(bytes, rangeStart, bytes.length - rangeStart);
@@ -401,6 +407,7 @@ protected int sendIncompleteContent(HttpExchange exchange, byte[] bytes) throws
401407
length = bytes.length - rangeStart;
402408
}
403409
exchange.getResponseHeaders().add("Content-Type", bytesContentType());
410+
addSuccessfulDownloadHeaders(exchange);
404411
exchange.sendResponseHeaders(HttpStatus.SC_OK, length);
405412
int minSend = Math.min(0, length - 1);
406413
final int bytesToSend = randomIntBetween(minSend, length - 1);

0 commit comments

Comments
 (0)