Skip to content

Commit

Permalink
Drop explicit check for file existence while creating GCS blobs
Browse files Browse the repository at this point in the history
Defer the file existence check to GCS API when the blob gets actually
created.
  • Loading branch information
findinpath authored and wendigo committed Mar 17, 2024
1 parent e21ed01 commit a1c27da
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.nio.file.FileAlreadyExistsException;

import static com.google.common.base.Preconditions.checkArgument;
import static io.trino.filesystem.gcs.GcsUtils.getBlob;
import static io.trino.filesystem.gcs.GcsUtils.handleGcsException;
import static java.net.HttpURLConnection.HTTP_PRECON_FAILED;
import static java.util.Objects.requireNonNull;
Expand Down Expand Up @@ -65,9 +64,6 @@ public void createExclusive(byte[] data)
throws IOException
{
try {
if (getBlob(storage, location).isPresent()) {
throw new FileAlreadyExistsException("File %s already exists".formatted(location));
}
storage.create(blobInfo(), data, BlobTargetOption.doesNotExist());
}
catch (RuntimeException e) {
Expand All @@ -81,9 +77,6 @@ public OutputStream create(AggregatedMemoryContext memoryContext)
throws IOException
{
try {
if (getBlob(storage, location).isPresent()) {
throw new FileAlreadyExistsException("File %s already exists".formatted(location));
}
WriteChannel writeChannel = storage.writer(blobInfo(), BlobWriteOption.doesNotExist());
return new GcsOutputStream(location, writeChannel, memoryContext, writeBlockSizeBytes);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,19 @@
package io.trino.filesystem.gcs;

import com.google.cloud.WriteChannel;
import com.google.cloud.storage.StorageException;
import com.google.common.primitives.Ints;
import io.trino.memory.context.AggregatedMemoryContext;
import io.trino.memory.context.LocalMemoryContext;

import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.file.FileAlreadyExistsException;

import static com.google.common.base.Preconditions.checkArgument;
import static java.lang.Math.min;
import static java.net.HttpURLConnection.HTTP_PRECON_FAILED;
import static java.util.Objects.requireNonNull;

public class GcsOutputStream
Expand Down Expand Up @@ -134,6 +137,11 @@ public void close()
try {
writeChannel.close();
}
catch (StorageException e) {
if (e.getCode() == HTTP_PRECON_FAILED) {
throw new FileAlreadyExistsException(location.toString());
}
}
catch (IOException e) {
throw new IOException("Error closing file: " + location, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ void testOutputFile()

if (isCreateExclusive()) {
// re-create without overwrite is an error
assertThatThrownBy(outputFile::create)
assertThatThrownBy(() -> outputFile.create().close())
.isInstanceOf(FileAlreadyExistsException.class)
.hasMessageContaining(tempBlob.location().toString());

Expand Down

0 comments on commit a1c27da

Please sign in to comment.