|
9 | 9 |
|
10 | 10 | package org.elasticsearch.repositories.gcs; |
11 | 11 |
|
12 | | -import com.google.api.client.http.GenericUrl; |
13 | | -import com.google.api.client.http.HttpRequest; |
14 | 12 | import com.google.api.client.http.HttpResponse; |
15 | 13 | import com.google.api.client.http.HttpResponseInterceptor; |
16 | 14 |
|
17 | 15 | import org.elasticsearch.common.blobstore.OperationPurpose; |
18 | 16 |
|
19 | | -import java.util.List; |
20 | | -import java.util.Locale; |
21 | | -import java.util.function.Consumer; |
22 | | -import java.util.function.Function; |
23 | 17 | import java.util.regex.Pattern; |
24 | 18 |
|
25 | | -import static java.lang.String.format; |
| 19 | +import static org.elasticsearch.repositories.gcs.GoogleCloudStorageOperationsStats.Operation; |
26 | 20 |
|
27 | 21 | final class GoogleCloudStorageHttpStatsCollector implements HttpResponseInterceptor { |
28 | | - // The specification for the current API (v1) endpoints can be found at: |
29 | | - // https://cloud.google.com/storage/docs/json_api/v1 |
30 | | - private static final List<Function<String, HttpRequestTracker>> trackerFactories = List.of( |
31 | | - (bucket) -> HttpRequestTracker.get( |
32 | | - format(Locale.ROOT, "/download/storage/v1/b/%s/o/.+", bucket), |
33 | | - GoogleCloudStorageOperationsStats::trackGetOperation |
34 | | - ), |
35 | | - |
36 | | - (bucket) -> HttpRequestTracker.get( |
37 | | - format(Locale.ROOT, "/storage/v1/b/%s/o/.+", bucket), |
38 | | - GoogleCloudStorageOperationsStats::trackGetOperation |
39 | | - ), |
40 | | - |
41 | | - (bucket) -> HttpRequestTracker.get( |
42 | | - format(Locale.ROOT, "/storage/v1/b/%s/o", bucket), |
43 | | - GoogleCloudStorageOperationsStats::trackListOperation |
44 | | - ) |
45 | | - ); |
| 22 | + private final GoogleCloudStorageOperationsStats stats; |
| 23 | + private final OperationPurpose purpose; |
| 24 | + private final Pattern getObjPattern; |
| 25 | + private final Pattern insertObPattern; |
| 26 | + private final Pattern listObjPattern; |
| 27 | + |
| 28 | + GoogleCloudStorageHttpStatsCollector(final GoogleCloudStorageOperationsStats stats, OperationPurpose purpose) { |
| 29 | + this.stats = stats; |
| 30 | + this.purpose = purpose; |
| 31 | + var bucket = stats.bucketName(); |
| 32 | + |
| 33 | + // The specification for the current API (v1) endpoints can be found at: |
| 34 | + // https://cloud.google.com/storage/docs/json_api/v1 |
| 35 | + this.getObjPattern = Pattern.compile("(/download)?/storage/v1/b/" + bucket + "/o/.+"); |
| 36 | + this.insertObPattern = Pattern.compile("(/upload)?/storage/v1/b/" + bucket + "/o"); |
| 37 | + this.listObjPattern = Pattern.compile("/storage/v1/b/" + bucket + "/o"); |
| 38 | + } |
46 | 39 |
|
47 | | - private final GoogleCloudStorageOperationsStats gcsOperationStats; |
48 | | - private final OperationPurpose operationPurpose; |
49 | | - private final List<HttpRequestTracker> trackers; |
| 40 | + private void trackRequest(Operation operation) { |
| 41 | + stats.trackRequest(purpose, operation); |
| 42 | + } |
50 | 43 |
|
51 | | - GoogleCloudStorageHttpStatsCollector(final GoogleCloudStorageOperationsStats gcsOperationStats, OperationPurpose operationPurpose) { |
52 | | - this.gcsOperationStats = gcsOperationStats; |
53 | | - this.operationPurpose = operationPurpose; |
54 | | - this.trackers = trackerFactories.stream() |
55 | | - .map(trackerFactory -> trackerFactory.apply(gcsOperationStats.getTrackedBucket())) |
56 | | - .toList(); |
| 44 | + private void trackRequestAndOperation(Operation operation) { |
| 45 | + stats.trackRequestAndOperation(purpose, operation); |
57 | 46 | } |
58 | 47 |
|
59 | 48 | @Override |
60 | 49 | public void interceptResponse(final HttpResponse response) { |
61 | | - // TODO keep track of unsuccessful requests in different entries |
62 | | - if (response.isSuccessStatusCode() == false) return; |
63 | | - |
64 | | - final HttpRequest request = response.getRequest(); |
65 | | - for (HttpRequestTracker tracker : trackers) { |
66 | | - if (tracker.track(request, gcsOperationStats)) { |
67 | | - return; |
| 50 | + var request = response.getRequest(); |
| 51 | + var path = request.getUrl().getRawPath(); |
| 52 | + switch (request.getRequestMethod()) { |
| 53 | + case "GET" -> { |
| 54 | + // https://cloud.google.com/storage/docs/json_api/v1/objects/get |
| 55 | + if (getObjPattern.matcher(path).matches()) { |
| 56 | + // Retrieves object metadata. When alt=media is included as a query parameter, retrieves object data. |
| 57 | + if (request.getUrl().getFirst("alt").equals("media")) { |
| 58 | + trackRequestAndOperation(Operation.GET_OBJECT); |
| 59 | + } else { |
| 60 | + trackRequestAndOperation(Operation.GET_METADATA); |
| 61 | + } |
| 62 | + } else if (listObjPattern.matcher(path).matches()) { |
| 63 | + trackRequestAndOperation(Operation.LIST_OBJECTS); |
| 64 | + } |
| 65 | + // ignore other get requests |
| 66 | + } |
| 67 | + case "POST", "PUT" -> { |
| 68 | + // https://cloud.google.com/storage/docs/json_api/v1/objects/insert |
| 69 | + if (insertObPattern.matcher(path).matches()) { |
| 70 | + var obj = request.getUrl().getFirst("uploadType"); |
| 71 | + if (obj instanceof String uploadType) { |
| 72 | + switch (uploadType) { |
| 73 | + // We dont track insert operations here, only requests. The reason is billing impact. |
| 74 | + // Any insert, including multipart or resumable parts, are counted as one operation. |
| 75 | + case "multipart" -> trackRequest(Operation.MULTIPART_UPLOAD); |
| 76 | + case "resumable" -> trackRequest(Operation.RESUMABLE_UPLOAD); |
| 77 | + default -> { |
| 78 | + // ignore "media" - Data-only upload. Upload the object data only, without any metadata. |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + // ignore other post requests |
| 84 | + } |
| 85 | + default -> { |
| 86 | + // ignore other http methods |
68 | 87 | } |
69 | | - } |
70 | | - } |
71 | | - |
72 | | - /** |
73 | | - * Http request tracker that allows to track certain HTTP requests based on the following criteria: |
74 | | - * <ul> |
75 | | - * <li>The HTTP request method</li> |
76 | | - * <li>An URI path regex expression</li> |
77 | | - * </ul> |
78 | | - * |
79 | | - * The requests that match the previous criteria are tracked using the {@code statsTracker} function. |
80 | | - */ |
81 | | - private static final class HttpRequestTracker { |
82 | | - private final String method; |
83 | | - private final Pattern pathPattern; |
84 | | - private final Consumer<GoogleCloudStorageOperationsStats> statsTracker; |
85 | | - |
86 | | - private HttpRequestTracker( |
87 | | - final String method, |
88 | | - final String pathPattern, |
89 | | - final Consumer<GoogleCloudStorageOperationsStats> statsTracker |
90 | | - ) { |
91 | | - this.method = method; |
92 | | - this.pathPattern = Pattern.compile(pathPattern); |
93 | | - this.statsTracker = statsTracker; |
94 | | - } |
95 | | - |
96 | | - private static HttpRequestTracker get(final String pathPattern, final Consumer<GoogleCloudStorageOperationsStats> statsConsumer) { |
97 | | - return new HttpRequestTracker("GET", pathPattern, statsConsumer); |
98 | | - } |
99 | | - |
100 | | - /** |
101 | | - * Tracks the provided http request if it matches the criteria defined by this tracker. |
102 | | - * |
103 | | - * @param httpRequest the http request to be tracked |
104 | | - * @param stats the operation tracker |
105 | | - * |
106 | | - * @return {@code true} if the http request was tracked, {@code false} otherwise. |
107 | | - */ |
108 | | - private boolean track(final HttpRequest httpRequest, final GoogleCloudStorageOperationsStats stats) { |
109 | | - if (matchesCriteria(httpRequest) == false) return false; |
110 | | - |
111 | | - statsTracker.accept(stats); |
112 | | - return true; |
113 | | - } |
114 | | - |
115 | | - private boolean matchesCriteria(final HttpRequest httpRequest) { |
116 | | - return method.equalsIgnoreCase(httpRequest.getRequestMethod()) && pathMatches(httpRequest.getUrl()); |
117 | | - } |
118 | | - |
119 | | - private boolean pathMatches(final GenericUrl url) { |
120 | | - return pathPattern.matcher(url.getRawPath()).matches(); |
121 | 88 | } |
122 | 89 | } |
123 | 90 | } |
0 commit comments