|
17 | 17 | import com.google.cloud.http.HttpTransportOptions; |
18 | 18 | import com.google.cloud.storage.StorageOptions; |
19 | 19 | import com.google.cloud.storage.StorageRetryStrategy; |
20 | | -import com.sun.net.httpserver.Headers; |
21 | 20 | import com.sun.net.httpserver.HttpExchange; |
22 | 21 | import com.sun.net.httpserver.HttpHandler; |
23 | 22 |
|
|
57 | 56 | import java.util.Collection; |
58 | 57 | import java.util.Collections; |
59 | 58 | import java.util.Map; |
60 | | -import java.util.regex.Matcher; |
61 | | -import java.util.regex.Pattern; |
62 | 59 |
|
63 | 60 | import static org.elasticsearch.common.io.Streams.readFully; |
64 | 61 | import static org.elasticsearch.repositories.blobstore.BlobStoreTestUtil.randomPurpose; |
@@ -106,7 +103,11 @@ protected Map<String, HttpHandler> createHttpHandlers() { |
106 | 103 |
|
107 | 104 | @Override |
108 | 105 | protected HttpHandler createErroneousHttpHandler(final HttpHandler delegate) { |
109 | | - return new GoogleErroneousHttpHandler(delegate, randomIntBetween(2, 3)); |
| 106 | + if (delegate instanceof FakeOAuth2HttpHandler) { |
| 107 | + return new GoogleErroneousHttpHandler(delegate, randomIntBetween(2, 3)); |
| 108 | + } else { |
| 109 | + return new GoogleCloudStorageStatsCollectorHttpHandler(new GoogleErroneousHttpHandler(delegate, randomIntBetween(2, 3))); |
| 110 | + } |
110 | 111 | } |
111 | 112 |
|
112 | 113 | @Override |
@@ -222,6 +223,11 @@ public void testWriteFileMultipleOfChunkSize() throws IOException { |
222 | 223 | } |
223 | 224 | } |
224 | 225 |
|
| 226 | + @Override |
| 227 | + public void testRequestStats() throws Exception { |
| 228 | + super.testRequestStats(); |
| 229 | + } |
| 230 | + |
225 | 231 | public static class TestGoogleCloudStoragePlugin extends GoogleCloudStoragePlugin { |
226 | 232 |
|
227 | 233 | public TestGoogleCloudStoragePlugin(Settings settings) { |
@@ -357,42 +363,24 @@ protected boolean canFailRequest(final HttpExchange exchange) { |
357 | 363 | @SuppressForbidden(reason = "this tests uses a HttpServer to emulate an GCS endpoint") |
358 | 364 | private static class GoogleCloudStorageStatsCollectorHttpHandler extends HttpStatsCollectorHandler { |
359 | 365 |
|
360 | | - public static final Pattern contentRangeMatcher = Pattern.compile("bytes \\d+-(\\d+)/(\\d+)"); |
361 | | - |
362 | 366 | GoogleCloudStorageStatsCollectorHttpHandler(final HttpHandler delegate) { |
363 | 367 | super(delegate); |
364 | 368 | } |
365 | 369 |
|
366 | 370 | @Override |
367 | 371 | public void maybeTrack(HttpExchange exchange) { |
368 | 372 | final String request = exchange.getRequestMethod() + " " + exchange.getRequestURI().toString(); |
369 | | - final Headers requestHeaders = exchange.getRequestHeaders(); |
370 | 373 | if (Regex.simpleMatch("GET */storage/v1/b/*/o/*", request)) { |
371 | 374 | trackRequest(StorageOperation.GET.key()); |
372 | 375 | } else if (Regex.simpleMatch("GET /storage/v1/b/*/o*", request)) { |
373 | 376 | trackRequest(StorageOperation.LIST.key()); |
374 | 377 | } else if (Regex.simpleMatch("POST /upload/storage/v1/b/*uploadType=resumable*", request)) { |
375 | | - // Resumable uploads are billed as a single operation, that's the reason we're tracking the request only first part |
376 | | - // See https://cloud.google.com/storage/docs/resumable-uploads#introduction |
| 378 | + trackRequest(StorageOperation.INSERT.key()); |
| 379 | + } else if (Regex.simpleMatch("PUT /upload/storage/v1/b/*uploadType=resumable*", request)) { |
377 | 380 | trackRequest(StorageOperation.INSERT.key()); |
378 | 381 | } else if (Regex.simpleMatch("POST /upload/storage/v1/b/*uploadType=multipart*", request)) { |
379 | 382 | trackRequest(StorageOperation.INSERT.key()); |
380 | 383 | } |
381 | 384 | } |
382 | | - |
383 | | - boolean isLastPart(Headers requestHeaders) { |
384 | | - if (requestHeaders.containsKey("Content-range") == false) return false; |
385 | | - |
386 | | - // https://cloud.google.com/storage/docs/json_api/v1/parameters#contentrange |
387 | | - final String contentRange = requestHeaders.getFirst("Content-range"); |
388 | | - |
389 | | - final Matcher matcher = contentRangeMatcher.matcher(contentRange); |
390 | | - |
391 | | - if (matcher.matches() == false) return false; |
392 | | - |
393 | | - String upperBound = matcher.group(1); |
394 | | - String totalLength = matcher.group(2); |
395 | | - return Integer.parseInt(upperBound) == Integer.parseInt(totalLength) - 1; |
396 | | - } |
397 | 385 | } |
398 | 386 | } |
0 commit comments