Skip to content

Commit 4c479f4

Browse files
committed
Test failIfAlreadyExists in S3BlobStoreRepositoryTests with multipart and concurrent write
1 parent c1974ad commit 4c479f4

File tree

1 file changed

+33
-5
lines changed

1 file changed

+33
-5
lines changed

modules/repository-s3/src/internalClusterTest/java/org/elasticsearch/repositories/s3/S3BlobStoreRepositoryTests.java

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,11 @@
8383
import java.util.Objects;
8484
import java.util.Set;
8585
import java.util.concurrent.CountDownLatch;
86+
import java.util.concurrent.Executors;
8687
import java.util.concurrent.Semaphore;
88+
import java.util.concurrent.TimeUnit;
8789
import java.util.concurrent.atomic.AtomicBoolean;
90+
import java.util.concurrent.atomic.AtomicInteger;
8891
import java.util.concurrent.atomic.AtomicLong;
8992
import java.util.stream.Collectors;
9093
import java.util.stream.StreamSupport;
@@ -569,22 +572,47 @@ public void match(LogEvent event) {
569572
}
570573
}
571574

572-
public void testFailIfAlreadyExists() throws IOException {
575+
public void testFailIfAlreadyExists() throws IOException, InterruptedException {
573576
try (BlobStore store = newBlobStore()) {
574577
final BlobContainer container = store.blobContainer(BlobPath.EMPTY);
575578
final String blobName = randomAlphaOfLengthBetween(8, 12);
576-
byte[] data = randomBytes(randomIntBetween(10, scaledRandomIntBetween(1024, 1 << 16)));
579+
580+
final byte[] data;
581+
if (randomBoolean()) {
582+
// single upload
583+
data = randomBytes(randomIntBetween(10, scaledRandomIntBetween(1024, 1 << 16)));
584+
} else {
585+
// multipart upload
586+
int thresholdInBytes = Math.toIntExact(((S3BlobContainer) container).getLargeBlobThresholdInBytes());
587+
data = randomBytes(randomIntBetween(thresholdInBytes, thresholdInBytes + scaledRandomIntBetween(1024, 1 << 16)));
588+
}
577589

578590
// initial write blob
579-
writeBlob(container, blobName, new BytesArray(data), true);
591+
AtomicInteger exceptionCount = new AtomicInteger(0);
592+
try (var executor = Executors.newFixedThreadPool(2)) {
593+
for (int i = 0; i < 2; i++) {
594+
executor.submit(() -> {
595+
try {
596+
writeBlob(container, blobName, new BytesArray(data), true);
597+
} catch (IOException e) {
598+
exceptionCount.incrementAndGet();
599+
}
600+
});
601+
}
602+
executor.shutdown();
603+
var done = executor.awaitTermination(1, TimeUnit.SECONDS);
604+
assertTrue(done);
605+
}
606+
607+
assertEquals(1, exceptionCount.get());
580608

581-
// override if failIfAlreadyExists is set to false
609+
// overwrite if failIfAlreadyExists is set to false
582610
writeBlob(container, blobName, new BytesArray(data), false);
583611

584612
// throw exception if failIfAlreadyExists is set to true
585613
var exception = expectThrows(IOException.class, () -> writeBlob(container, blobName, new BytesArray(data), true));
586614

587-
assertThat(exception.getMessage(), startsWith("Unable to upload object"));
615+
assertThat(exception.getMessage(), startsWith("Unable to upload"));
588616

589617
container.delete(randomPurpose());
590618
}

0 commit comments

Comments
 (0)