Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/changelog/125452.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 125452
summary: Add GCS telemetry with `ThreadLocal`
area: Snapshot/Restore
type: enhancement
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import com.google.cloud.http.HttpTransportOptions;
import com.google.cloud.storage.StorageOptions;
import com.google.cloud.storage.StorageRetryStrategy;
import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;

Expand Down Expand Up @@ -57,15 +56,12 @@
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.elasticsearch.common.io.Streams.readFully;
import static org.elasticsearch.repositories.blobstore.BlobStoreTestUtil.randomPurpose;
import static org.elasticsearch.repositories.gcs.GoogleCloudStorageClientSettings.CREDENTIALS_FILE_SETTING;
import static org.elasticsearch.repositories.gcs.GoogleCloudStorageClientSettings.ENDPOINT_SETTING;
import static org.elasticsearch.repositories.gcs.GoogleCloudStorageClientSettings.TOKEN_URI_SETTING;
import static org.elasticsearch.repositories.gcs.GoogleCloudStorageOperationsStats.Operation;
import static org.elasticsearch.repositories.gcs.GoogleCloudStorageRepository.BASE_PATH;
import static org.elasticsearch.repositories.gcs.GoogleCloudStorageRepository.BUCKET;
import static org.elasticsearch.repositories.gcs.GoogleCloudStorageRepository.CLIENT_NAME;
Expand Down Expand Up @@ -107,7 +103,11 @@ protected Map<String, HttpHandler> createHttpHandlers() {

@Override
protected HttpHandler createErroneousHttpHandler(final HttpHandler delegate) {
return new GoogleErroneousHttpHandler(delegate, randomIntBetween(2, 3));
if (delegate instanceof FakeOAuth2HttpHandler) {
return new GoogleErroneousHttpHandler(delegate, randomIntBetween(2, 3));
} else {
return new GoogleCloudStorageStatsCollectorHttpHandler(new GoogleErroneousHttpHandler(delegate, randomIntBetween(2, 3)));
}
}

@Override
Expand Down Expand Up @@ -223,15 +223,20 @@ public void testWriteFileMultipleOfChunkSize() throws IOException {
}
}

@Override
public void testRequestStats() throws Exception {
super.testRequestStats();
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

redundant?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

helper to run from ide


public static class TestGoogleCloudStoragePlugin extends GoogleCloudStoragePlugin {

public TestGoogleCloudStoragePlugin(Settings settings) {
super(settings);
}

@Override
protected GoogleCloudStorageService createStorageService(Settings settings) {
return new GoogleCloudStorageService(settings) {
protected GoogleCloudStorageService createStorageService(boolean isServerless) {
return new GoogleCloudStorageService() {
@Override
StorageOptions createStorageOptions(
final GoogleCloudStorageClientSettings gcsClientSettings,
Expand Down Expand Up @@ -277,7 +282,8 @@ public Map<String, Repository.Factory> getRepositories(
this.storageService,
clusterService,
bigArrays,
recoverySettings
recoverySettings,
new GcsRepositoryStatsCollector()
) {
@Override
protected GoogleCloudStorageBlobStore createBlobStore() {
Expand All @@ -288,7 +294,8 @@ protected GoogleCloudStorageBlobStore createBlobStore() {
storageService,
bigArrays,
randomIntBetween(1, 8) * 1024,
BackoffPolicy.noBackoff()
BackoffPolicy.noBackoff(),
this.statsCollector()
) {
@Override
long getLargeBlobThresholdInBytes() {
Expand Down Expand Up @@ -356,43 +363,24 @@ protected boolean canFailRequest(final HttpExchange exchange) {
@SuppressForbidden(reason = "this tests uses a HttpServer to emulate an GCS endpoint")
private static class GoogleCloudStorageStatsCollectorHttpHandler extends HttpStatsCollectorHandler {

public static final Pattern contentRangeMatcher = Pattern.compile("bytes \\d+-(\\d+)/(\\d+)");

GoogleCloudStorageStatsCollectorHttpHandler(final HttpHandler delegate) {
super(delegate);
}

@Override
public void maybeTrack(HttpExchange exchange) {
final String request = exchange.getRequestMethod() + " " + exchange.getRequestURI().toString();
final Headers requestHeaders = exchange.getRequestHeaders();
if (Regex.simpleMatch("GET */storage/v1/b/*/o/*", request)) {
trackRequest(Operation.GET_OBJECT.key());
trackRequest(StorageOperation.GET.key());
} else if (Regex.simpleMatch("GET /storage/v1/b/*/o*", request)) {
trackRequest(Operation.LIST_OBJECTS.key());
} else if (Regex.simpleMatch("PUT /upload/storage/v1/b/*uploadType=resumable*", request) && isLastPart(requestHeaders)) {
// Resumable uploads are billed as a single operation, that's the reason we're tracking
// the request only when it's the last part.
// See https://cloud.google.com/storage/docs/resumable-uploads#introduction
trackRequest(Operation.INSERT_OBJECT.key());
trackRequest(StorageOperation.LIST.key());
} else if (Regex.simpleMatch("POST /upload/storage/v1/b/*uploadType=resumable*", request)) {
trackRequest(StorageOperation.INSERT.key());
} else if (Regex.simpleMatch("PUT /upload/storage/v1/b/*uploadType=resumable*", request)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

count all requests now, not operations

trackRequest(StorageOperation.INSERT.key());
} else if (Regex.simpleMatch("POST /upload/storage/v1/b/*uploadType=multipart*", request)) {
trackRequest(Operation.INSERT_OBJECT.key());
trackRequest(StorageOperation.INSERT.key());
}
}

boolean isLastPart(Headers requestHeaders) {
if (requestHeaders.containsKey("Content-range") == false) return false;

// https://cloud.google.com/storage/docs/json_api/v1/parameters#contentrange
final String contentRange = requestHeaders.getFirst("Content-range");

final Matcher matcher = contentRangeMatcher.matcher(contentRange);

if (matcher.matches() == false) return false;

String upperBound = matcher.group(1);
String totalLength = matcher.group(2);
return Integer.parseInt(upperBound) == Integer.parseInt(totalLength) - 1;
}
}
}
Loading
Loading