|
@@ -18,8 +18,16 @@
|
|
|
|
|
|
package org.apache.hadoop.fs.azure;
|
|
|
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.OutputStream;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
import java.util.concurrent.CountDownLatch;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+import java.util.concurrent.atomic.AtomicInteger;
|
|
|
+import java.util.concurrent.atomic.AtomicReference;
|
|
|
|
|
|
+import org.apache.hadoop.fs.contract.ContractTestUtils;
|
|
|
import org.apache.hadoop.io.IOUtils;
|
|
|
import org.apache.hadoop.fs.FSDataOutputStream;
|
|
|
import org.apache.hadoop.fs.Path;
|
|
@@ -40,6 +48,70 @@ public class ITestNativeAzureFileSystemLive extends
|
|
|
return AzureBlobStorageTestAccount.create();
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Tests the rename file operation to ensure that when there are multiple
|
|
|
+ * attempts to rename a file to the same destination, only one rename
|
|
|
+ * operation is successful (HADOOP-15086).
|
|
|
+ */
|
|
|
+ @Test
|
|
|
+ public void testMultipleRenameFileOperationsToSameDestination()
|
|
|
+ throws IOException, InterruptedException {
|
|
|
+ final CountDownLatch latch = new CountDownLatch(1);
|
|
|
+ final AtomicInteger successfulRenameCount = new AtomicInteger(0);
|
|
|
+ final AtomicReference<IOException> unexpectedError = new AtomicReference<IOException>();
|
|
|
+ final Path dest = path("dest");
|
|
|
+
|
|
|
+ // Run 10 threads to rename multiple files to the same target path
|
|
|
+ List<Thread> threads = new ArrayList<>();
|
|
|
+
|
|
|
+ for (int i = 0; i < 10; i++) {
|
|
|
+ final int threadNumber = i;
|
|
|
+ Path src = path("test" + threadNumber);
|
|
|
+ threads.add(new Thread(() -> {
|
|
|
+ try {
|
|
|
+ latch.await(Long.MAX_VALUE, TimeUnit.SECONDS);
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ try (OutputStream output = fs.create(src)) {
|
|
|
+ output.write(("Source file number " + threadNumber).getBytes());
|
|
|
+ }
|
|
|
+
|
|
|
+ if (fs.rename(src, dest)) {
|
|
|
+ LOG.info("rename succeeded for thread " + threadNumber);
|
|
|
+ successfulRenameCount.incrementAndGet();
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ unexpectedError.compareAndSet(null, e);
|
|
|
+ ContractTestUtils.fail("Exception unexpected", e);
|
|
|
+ }
|
|
|
+ }));
|
|
|
+ }
|
|
|
+
|
|
|
+ // Start each thread
|
|
|
+ threads.forEach(t -> t.start());
|
|
|
+
|
|
|
+ // Wait for threads to start and wait on latch
|
|
|
+ Thread.sleep(2000);
|
|
|
+
|
|
|
+ // Now start to rename
|
|
|
+ latch.countDown();
|
|
|
+
|
|
|
+ // Wait for all threads to complete
|
|
|
+ threads.forEach(t -> {
|
|
|
+ try {
|
|
|
+ t.join();
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ if (unexpectedError.get() != null) {
|
|
|
+ throw unexpectedError.get();
|
|
|
+ }
|
|
|
+ assertEquals(1, successfulRenameCount.get());
|
|
|
+ LOG.info("Success, only one rename operation succeeded!");
|
|
|
+ }
|
|
|
+
|
|
|
@Test
|
|
|
public void testLazyRenamePendingCanOverwriteExistingFile()
|
|
|
throws Exception {
|