From 255ab8965fcbbd33f6005fd644b4c6a4cd8278f0 Mon Sep 17 00:00:00 2001 From: Armin Braun Date: Mon, 10 Jun 2019 15:25:07 +0200 Subject: [PATCH] Fix GCS Blob Repository 3rd Party Tests (#43030) * We have to strip the trailing slash from child names here like we do for AWS * closes #43029 --- .../gcs/GoogleCloudStorageBlobStore.java | 28 +++++++++---------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/plugins/repository-gcs/src/main/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageBlobStore.java b/plugins/repository-gcs/src/main/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageBlobStore.java index 40e00c496662..84c5cf78839e 100644 --- a/plugins/repository-gcs/src/main/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageBlobStore.java +++ b/plugins/repository-gcs/src/main/java/org/elasticsearch/repositories/gcs/GoogleCloudStorageBlobStore.java @@ -30,7 +30,6 @@ import com.google.cloud.storage.Storage.BlobListOption; import com.google.cloud.storage.StorageBatch; import com.google.cloud.storage.StorageException; - import org.elasticsearch.common.SuppressForbidden; import org.elasticsearch.common.blobstore.BlobContainer; import org.elasticsearch.common.blobstore.BlobMetaData; @@ -50,11 +49,11 @@ import java.nio.channels.WritableByteChannel; import java.nio.file.FileAlreadyExistsException; import java.nio.file.NoSuchFileException; -import java.util.Map; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.Map; import java.util.concurrent.atomic.AtomicReference; import java.util.stream.Collectors; @@ -132,13 +131,15 @@ Map listBlobs(String path) throws IOException { Map listBlobsByPrefix(String path, String prefix) throws IOException { final String pathPrefix = buildKey(path, prefix); final MapBuilder mapBuilder = MapBuilder.newMapBuilder(); - SocketAccess.doPrivilegedVoidIOException(() -> { - client().get(bucketName).list(BlobListOption.prefix(pathPrefix)).iterateAll().forEach(blob -> { - assert blob.getName().startsWith(path); - final String suffixName = blob.getName().substring(path.length()); - mapBuilder.put(suffixName, new PlainBlobMetaData(suffixName, blob.getSize())); - }); - }); + SocketAccess.doPrivilegedVoidIOException( + () -> client().get(bucketName).list(BlobListOption.currentDirectory(), BlobListOption.prefix(pathPrefix)).iterateAll().forEach( + blob -> { + assert blob.getName().startsWith(path); + if (blob.isDirectory() == false) { + final String suffixName = blob.getName().substring(path.length()); + mapBuilder.put(suffixName, new PlainBlobMetaData(suffixName, blob.getSize())); + } + })); return mapBuilder.immutableMap(); } @@ -150,7 +151,9 @@ Map listChildren(BlobPath path) throws IOException { blob -> { if (blob.isDirectory()) { assert blob.getName().startsWith(pathStr); - final String suffixName = blob.getName().substring(pathStr.length()); + assert blob.getName().endsWith("/"); + // Strip path prefix and trailing slash + final String suffixName = blob.getName().substring(pathStr.length(), blob.getName().length() - 1); if (suffixName.isEmpty() == false) { mapBuilder.put(suffixName, new GoogleCloudStorageBlobContainer(path.add(suffixName), this)); } @@ -312,11 +315,6 @@ void deleteBlobsIgnoringIfNotExists(Collection blobNames) throws IOExcep if (blobNames.isEmpty()) { return; } - // for a single op submit a simple delete instead of a batch of size 1 - if (blobNames.size() == 1) { - deleteBlob(blobNames.iterator().next()); - return; - } final List blobIdsToDelete = blobNames.stream().map(blob -> BlobId.of(bucketName, blob)).collect(Collectors.toList()); final List failedBlobs = Collections.synchronizedList(new ArrayList<>()); final StorageException e = SocketAccess.doPrivilegedIOException(() -> {