steveloughran commented on a change in pull request #1925: URL: https://github.com/apache/hadoop/pull/1925#discussion_r531177661
########## File path: hadoop-tools/hadoop-azure/src/test/java/org/apache/hadoop/fs/azurebfs/ITestAzureBlobFileSystemLease.java ########## @@ -0,0 +1,249 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.fs.azurebfs; + +import org.apache.hadoop.fs.FSDataOutputStream; +import org.apache.hadoop.fs.Path; +import org.apache.hadoop.fs.azurebfs.services.AbfsOutputStream; +import org.junit.Assert; +import org.junit.Test; + +import java.io.IOException; + +import static org.apache.hadoop.fs.azurebfs.services.AbfsErrors.ERR_ACQUIRING_LEASE; +import static org.apache.hadoop.fs.azurebfs.services.AbfsErrors.ERR_LEASE_EXPIRED; +import static org.apache.hadoop.fs.azurebfs.services.AbfsErrors.ERR_PARALLEL_ACCESS_DETECTED; + +/** + * Test lease operations. + */ +public class ITestAzureBlobFileSystemLease extends AbstractAbfsIntegrationTest { + private static final int TEST_EXECUTION_TIMEOUT = 30 * 1000; + private static final int LONG_TEST_EXECUTION_TIMEOUT = 90 * 1000; + private static final String TEST_FILE = "testfile"; + + public ITestAzureBlobFileSystemLease() throws Exception { + super(); + } + + @Test + public void testNoSingleWriter() throws IOException { + final AzureBlobFileSystem fs = getFileSystem(); + final Path testFilePath = new Path(path(methodName.getMethodName()), TEST_FILE); + fs.mkdirs(testFilePath.getParent()); + try (FSDataOutputStream out = fs.create(testFilePath)) { + Assert.assertFalse("Output stream should not have lease", + ((AbfsOutputStream) out.getWrappedStream()).hasLease()); + } + Assert.assertTrue(fs.getAbfsStore().areLeasesFreed()); + } + + @Test + public void testOneWriter() throws IOException { + final AzureBlobFileSystem fs = getFileSystem(); + final Path testFilePath = new Path(path(methodName.getMethodName()), TEST_FILE); + fs.mkdirs(testFilePath.getParent()); + fs.getAbfsStore().getAbfsConfiguration() + .setAzureSingleWriterDirs(testFilePath.getParent().toString()); + fs.getAbfsStore().updateSingleWriterDirs(); + + FSDataOutputStream out = fs.create(testFilePath); + Assert.assertTrue("Output stream should have lease", + ((AbfsOutputStream) out.getWrappedStream()).hasLease()); + out.close(); + Assert.assertFalse("Output stream should not have lease", + ((AbfsOutputStream) out.getWrappedStream()).hasLease()); + Assert.assertTrue(fs.getAbfsStore().areLeasesFreed()); + } + + @Test + public void testSubDir() throws IOException { + final AzureBlobFileSystem fs = getFileSystem(); + final Path testFilePath = new Path(new Path(path(methodName.getMethodName()), "subdir"), + TEST_FILE); + fs.mkdirs(testFilePath.getParent().getParent()); + fs.getAbfsStore().getAbfsConfiguration() + .setAzureSingleWriterDirs(testFilePath.getParent().getParent().toString()); + fs.getAbfsStore().updateSingleWriterDirs(); + + FSDataOutputStream out = fs.create(testFilePath); + Assert.assertTrue("Output stream should have lease", + ((AbfsOutputStream) out.getWrappedStream()).hasLease()); + out.close(); + Assert.assertFalse("Output stream should not have lease", + ((AbfsOutputStream) out.getWrappedStream()).hasLease()); + Assert.assertTrue(fs.getAbfsStore().areLeasesFreed()); + } + + @Test + public void testTwoCreate() throws IOException { + final AzureBlobFileSystem fs = getFileSystem(); + final Path testFilePath = new Path(path(methodName.getMethodName()), TEST_FILE); + fs.mkdirs(testFilePath.getParent()); + fs.getAbfsStore().getAbfsConfiguration() + .setAzureSingleWriterDirs(testFilePath.getParent().toString()); + fs.getAbfsStore().updateSingleWriterDirs(); + + try (FSDataOutputStream out = fs.create(testFilePath)) { + try (FSDataOutputStream out2 = fs.create(testFilePath)) { + Assert.fail("Second create succeeded"); + } catch (IOException e) { + Assert.assertTrue("Unexpected error message: " + e.getMessage(), + e.getMessage().contains(ERR_PARALLEL_ACCESS_DETECTED)); + } + } + Assert.assertTrue(fs.getAbfsStore().areLeasesFreed()); + } + + private void twoWriters(AzureBlobFileSystem fs, Path testFilePath, boolean expectException) throws Exception { + try (FSDataOutputStream out = fs.create(testFilePath)) { + try (FSDataOutputStream out2 = fs.append(testFilePath)) { + out2.writeInt(2); + out2.hsync(); + } catch (IOException e) { + if (expectException) { + Assert.assertTrue("Unexpected error message: " + e.getMessage(), + e.getMessage().contains(ERR_ACQUIRING_LEASE)); + } else { + Assert.fail("Unexpected exception " + e.getMessage()); + } + } + out.writeInt(1); + out.hsync(); + } + + Assert.assertTrue(fs.getAbfsStore().areLeasesFreed()); + } + + @Test(timeout = TEST_EXECUTION_TIMEOUT) + public void testTwoWritersCreateAppendNoSingleWriter() throws Exception { + final AzureBlobFileSystem fs = getFileSystem(); + final Path testFilePath = new Path(path(methodName.getMethodName()), TEST_FILE); + fs.mkdirs(testFilePath.getParent()); + + twoWriters(fs, testFilePath, false); + } + + @Test(timeout = TEST_EXECUTION_TIMEOUT) + public void testTwoWritersCreateAppendWithSingleWriterEnabled() throws Exception { + final AzureBlobFileSystem fs = getFileSystem(); + final Path testFilePath = new Path(path(methodName.getMethodName()), TEST_FILE); + fs.mkdirs(testFilePath.getParent()); + fs.getAbfsStore().getAbfsConfiguration() + .setAzureSingleWriterDirs(testFilePath.getParent().toString()); + fs.getAbfsStore().updateSingleWriterDirs(); + + twoWriters(fs, testFilePath, true); + } + + @Test(timeout = TEST_EXECUTION_TIMEOUT) + public void testLeaseFreedOnClose() throws IOException { + final AzureBlobFileSystem fs = getFileSystem(); + final Path testFilePath = new Path(path(methodName.getMethodName()), TEST_FILE); + fs.mkdirs(testFilePath.getParent()); + fs.getAbfsStore().getAbfsConfiguration() + .setAzureSingleWriterDirs(testFilePath.getParent().toString()); + fs.getAbfsStore().updateSingleWriterDirs(); + + FSDataOutputStream out; + out = fs.create(testFilePath); + out.write(0); + Assert.assertTrue("Output stream should have lease", + ((AbfsOutputStream) out.getWrappedStream()).hasLease()); + out.close(); + Assert.assertFalse("Output stream should not have lease after close", + ((AbfsOutputStream) out.getWrappedStream()).hasLease()); + Assert.assertTrue(fs.getAbfsStore().areLeasesFreed()); + } + + @Test(timeout = TEST_EXECUTION_TIMEOUT) + public void testWriteAfterBreakLease() throws IOException { + final AzureBlobFileSystem fs = getFileSystem(); + final Path testFilePath = new Path(path(methodName.getMethodName()), TEST_FILE); + fs.mkdirs(testFilePath.getParent()); + fs.getAbfsStore().getAbfsConfiguration() + .setAzureSingleWriterDirs(testFilePath.getParent().toString()); + fs.getAbfsStore().updateSingleWriterDirs(); + + FSDataOutputStream out; + out = fs.create(testFilePath); + out.write(0); + out.hsync(); + + fs.breakLease(testFilePath); + try { + out.write(1); + out.hsync(); + Assert.fail("Expected exception on write after lease break"); + } catch (IOException e) { + Assert.assertTrue("Unexpected error message: " + e.getMessage(), + e.getMessage().contains(ERR_LEASE_EXPIRED)); + } + try { + out.close(); + Assert.fail("Expected exception on close after lease break"); + } catch (IOException e) { + Assert.assertTrue("Unexpected error message: " + e.getMessage(), + e.getMessage().contains(ERR_LEASE_EXPIRED)); + } + + Assert.assertTrue(((AbfsOutputStream) out.getWrappedStream()).isLeaseFreed()); + + try (FSDataOutputStream out2 = fs.append(testFilePath)) { + out2.write(2); + out2.hsync(); + } + + Assert.assertTrue(fs.getAbfsStore().areLeasesFreed()); + } + + @Test(timeout = LONG_TEST_EXECUTION_TIMEOUT) + public void testLeaseFreedAfterBreak() throws IOException { + final AzureBlobFileSystem fs = getFileSystem(); + final Path testFilePath = new Path(path(methodName.getMethodName()), TEST_FILE); + fs.mkdirs(testFilePath.getParent()); + fs.getAbfsStore().getAbfsConfiguration() + .setAzureSingleWriterDirs(testFilePath.getParent().toString()); + fs.getAbfsStore().updateSingleWriterDirs(); + + FSDataOutputStream out = null; + try { + out = fs.create(testFilePath); + out.write(0); + + fs.breakLease(testFilePath); + while (!((AbfsOutputStream) out.getWrappedStream()).isLeaseFreed()) { + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + } + } + } finally { + try { + if (out != null) { Review comment: o.a.h.utils.IOUtils.close methods do this ########## File path: hadoop-tools/hadoop-azure/src/main/java/org/apache/hadoop/fs/azurebfs/AzureBlobFileSystem.java ########## @@ -475,6 +475,55 @@ public FileStatus getFileStatus(final Path f) throws IOException { } } + public String acquireLease(final Path f, final int duration) throws IOException { + LOG.debug("AzureBlobFileSystem.acquireLease path: {}", f); + + Path qualifiedPath = makeQualified(f); + + try { + return abfsStore.acquireLease(qualifiedPath, duration); Review comment: how about using DurationInfo in the try with resources (logging @ debug) to track how long acquire/release took. I can imagine it can take a while to acquire. Indeed, do we have to worry about timeouts, heartbeats, etc? ########## File path: hadoop-tools/hadoop-azure/src/test/java/org/apache/hadoop/fs/azurebfs/ITestAzureBlobFileSystemLease.java ########## @@ -0,0 +1,249 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.fs.azurebfs; + +import org.apache.hadoop.fs.FSDataOutputStream; +import org.apache.hadoop.fs.Path; +import org.apache.hadoop.fs.azurebfs.services.AbfsOutputStream; +import org.junit.Assert; +import org.junit.Test; + +import java.io.IOException; + +import static org.apache.hadoop.fs.azurebfs.services.AbfsErrors.ERR_ACQUIRING_LEASE; +import static org.apache.hadoop.fs.azurebfs.services.AbfsErrors.ERR_LEASE_EXPIRED; +import static org.apache.hadoop.fs.azurebfs.services.AbfsErrors.ERR_PARALLEL_ACCESS_DETECTED; + +/** + * Test lease operations. + */ +public class ITestAzureBlobFileSystemLease extends AbstractAbfsIntegrationTest { + private static final int TEST_EXECUTION_TIMEOUT = 30 * 1000; + private static final int LONG_TEST_EXECUTION_TIMEOUT = 90 * 1000; + private static final String TEST_FILE = "testfile"; + + public ITestAzureBlobFileSystemLease() throws Exception { + super(); + } + + @Test + public void testNoSingleWriter() throws IOException { + final AzureBlobFileSystem fs = getFileSystem(); + final Path testFilePath = new Path(path(methodName.getMethodName()), TEST_FILE); + fs.mkdirs(testFilePath.getParent()); + try (FSDataOutputStream out = fs.create(testFilePath)) { + Assert.assertFalse("Output stream should not have lease", + ((AbfsOutputStream) out.getWrappedStream()).hasLease()); + } + Assert.assertTrue(fs.getAbfsStore().areLeasesFreed()); + } + + @Test + public void testOneWriter() throws IOException { + final AzureBlobFileSystem fs = getFileSystem(); + final Path testFilePath = new Path(path(methodName.getMethodName()), TEST_FILE); + fs.mkdirs(testFilePath.getParent()); + fs.getAbfsStore().getAbfsConfiguration() + .setAzureSingleWriterDirs(testFilePath.getParent().toString()); + fs.getAbfsStore().updateSingleWriterDirs(); + + FSDataOutputStream out = fs.create(testFilePath); + Assert.assertTrue("Output stream should have lease", + ((AbfsOutputStream) out.getWrappedStream()).hasLease()); + out.close(); + Assert.assertFalse("Output stream should not have lease", + ((AbfsOutputStream) out.getWrappedStream()).hasLease()); + Assert.assertTrue(fs.getAbfsStore().areLeasesFreed()); + } + + @Test + public void testSubDir() throws IOException { + final AzureBlobFileSystem fs = getFileSystem(); + final Path testFilePath = new Path(new Path(path(methodName.getMethodName()), "subdir"), + TEST_FILE); + fs.mkdirs(testFilePath.getParent().getParent()); + fs.getAbfsStore().getAbfsConfiguration() + .setAzureSingleWriterDirs(testFilePath.getParent().getParent().toString()); + fs.getAbfsStore().updateSingleWriterDirs(); + + FSDataOutputStream out = fs.create(testFilePath); + Assert.assertTrue("Output stream should have lease", + ((AbfsOutputStream) out.getWrappedStream()).hasLease()); + out.close(); + Assert.assertFalse("Output stream should not have lease", + ((AbfsOutputStream) out.getWrappedStream()).hasLease()); + Assert.assertTrue(fs.getAbfsStore().areLeasesFreed()); + } + + @Test + public void testTwoCreate() throws IOException { + final AzureBlobFileSystem fs = getFileSystem(); + final Path testFilePath = new Path(path(methodName.getMethodName()), TEST_FILE); + fs.mkdirs(testFilePath.getParent()); + fs.getAbfsStore().getAbfsConfiguration() + .setAzureSingleWriterDirs(testFilePath.getParent().toString()); + fs.getAbfsStore().updateSingleWriterDirs(); + + try (FSDataOutputStream out = fs.create(testFilePath)) { + try (FSDataOutputStream out2 = fs.create(testFilePath)) { + Assert.fail("Second create succeeded"); + } catch (IOException e) { + Assert.assertTrue("Unexpected error message: " + e.getMessage(), + e.getMessage().contains(ERR_PARALLEL_ACCESS_DETECTED)); + } + } + Assert.assertTrue(fs.getAbfsStore().areLeasesFreed()); + } + + private void twoWriters(AzureBlobFileSystem fs, Path testFilePath, boolean expectException) throws Exception { + try (FSDataOutputStream out = fs.create(testFilePath)) { + try (FSDataOutputStream out2 = fs.append(testFilePath)) { + out2.writeInt(2); + out2.hsync(); + } catch (IOException e) { + if (expectException) { + Assert.assertTrue("Unexpected error message: " + e.getMessage(), + e.getMessage().contains(ERR_ACQUIRING_LEASE)); + } else { + Assert.fail("Unexpected exception " + e.getMessage()); + } + } + out.writeInt(1); + out.hsync(); + } + + Assert.assertTrue(fs.getAbfsStore().areLeasesFreed()); + } + + @Test(timeout = TEST_EXECUTION_TIMEOUT) + public void testTwoWritersCreateAppendNoSingleWriter() throws Exception { + final AzureBlobFileSystem fs = getFileSystem(); + final Path testFilePath = new Path(path(methodName.getMethodName()), TEST_FILE); + fs.mkdirs(testFilePath.getParent()); + + twoWriters(fs, testFilePath, false); + } + + @Test(timeout = TEST_EXECUTION_TIMEOUT) + public void testTwoWritersCreateAppendWithSingleWriterEnabled() throws Exception { + final AzureBlobFileSystem fs = getFileSystem(); + final Path testFilePath = new Path(path(methodName.getMethodName()), TEST_FILE); + fs.mkdirs(testFilePath.getParent()); + fs.getAbfsStore().getAbfsConfiguration() + .setAzureSingleWriterDirs(testFilePath.getParent().toString()); + fs.getAbfsStore().updateSingleWriterDirs(); + + twoWriters(fs, testFilePath, true); + } + + @Test(timeout = TEST_EXECUTION_TIMEOUT) + public void testLeaseFreedOnClose() throws IOException { + final AzureBlobFileSystem fs = getFileSystem(); + final Path testFilePath = new Path(path(methodName.getMethodName()), TEST_FILE); + fs.mkdirs(testFilePath.getParent()); + fs.getAbfsStore().getAbfsConfiguration() + .setAzureSingleWriterDirs(testFilePath.getParent().toString()); + fs.getAbfsStore().updateSingleWriterDirs(); + + FSDataOutputStream out; + out = fs.create(testFilePath); + out.write(0); + Assert.assertTrue("Output stream should have lease", + ((AbfsOutputStream) out.getWrappedStream()).hasLease()); + out.close(); + Assert.assertFalse("Output stream should not have lease after close", + ((AbfsOutputStream) out.getWrappedStream()).hasLease()); + Assert.assertTrue(fs.getAbfsStore().areLeasesFreed()); + } + + @Test(timeout = TEST_EXECUTION_TIMEOUT) + public void testWriteAfterBreakLease() throws IOException { + final AzureBlobFileSystem fs = getFileSystem(); + final Path testFilePath = new Path(path(methodName.getMethodName()), TEST_FILE); + fs.mkdirs(testFilePath.getParent()); + fs.getAbfsStore().getAbfsConfiguration() + .setAzureSingleWriterDirs(testFilePath.getParent().toString()); + fs.getAbfsStore().updateSingleWriterDirs(); + + FSDataOutputStream out; + out = fs.create(testFilePath); + out.write(0); + out.hsync(); + + fs.breakLease(testFilePath); + try { + out.write(1); + out.hsync(); + Assert.fail("Expected exception on write after lease break"); + } catch (IOException e) { + Assert.assertTrue("Unexpected error message: " + e.getMessage(), + e.getMessage().contains(ERR_LEASE_EXPIRED)); + } + try { + out.close(); + Assert.fail("Expected exception on close after lease break"); + } catch (IOException e) { + Assert.assertTrue("Unexpected error message: " + e.getMessage(), + e.getMessage().contains(ERR_LEASE_EXPIRED)); + } + + Assert.assertTrue(((AbfsOutputStream) out.getWrappedStream()).isLeaseFreed()); + + try (FSDataOutputStream out2 = fs.append(testFilePath)) { + out2.write(2); + out2.hsync(); + } + + Assert.assertTrue(fs.getAbfsStore().areLeasesFreed()); + } + + @Test(timeout = LONG_TEST_EXECUTION_TIMEOUT) + public void testLeaseFreedAfterBreak() throws IOException { + final AzureBlobFileSystem fs = getFileSystem(); + final Path testFilePath = new Path(path(methodName.getMethodName()), TEST_FILE); + fs.mkdirs(testFilePath.getParent()); + fs.getAbfsStore().getAbfsConfiguration() + .setAzureSingleWriterDirs(testFilePath.getParent().toString()); + fs.getAbfsStore().updateSingleWriterDirs(); + + FSDataOutputStream out = null; + try { + out = fs.create(testFilePath); + out.write(0); + + fs.breakLease(testFilePath); + while (!((AbfsOutputStream) out.getWrappedStream()).isLeaseFreed()) { + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + } + } + } finally { + try { + if (out != null) { + out.close(); + } + // exception might or might not occur + } catch (IOException e) { + Assert.assertTrue("Unexpected error message: " + e.getMessage(), + e.getMessage().contains(ERR_LEASE_EXPIRED)); + } + } + Assert.assertTrue(fs.getAbfsStore().areLeasesFreed()); Review comment: and add a message to raise if the condition is met. note, it's ok to use AssertJ for your asserts, we are adopting it more broadly and enjoying its diagnostics. ########## File path: hadoop-tools/hadoop-azure/src/test/java/org/apache/hadoop/fs/azurebfs/ITestAzureBlobFileSystemLease.java ########## @@ -0,0 +1,249 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.fs.azurebfs; + +import org.apache.hadoop.fs.FSDataOutputStream; +import org.apache.hadoop.fs.Path; +import org.apache.hadoop.fs.azurebfs.services.AbfsOutputStream; +import org.junit.Assert; +import org.junit.Test; + +import java.io.IOException; + +import static org.apache.hadoop.fs.azurebfs.services.AbfsErrors.ERR_ACQUIRING_LEASE; +import static org.apache.hadoop.fs.azurebfs.services.AbfsErrors.ERR_LEASE_EXPIRED; +import static org.apache.hadoop.fs.azurebfs.services.AbfsErrors.ERR_PARALLEL_ACCESS_DETECTED; + +/** + * Test lease operations. + */ +public class ITestAzureBlobFileSystemLease extends AbstractAbfsIntegrationTest { + private static final int TEST_EXECUTION_TIMEOUT = 30 * 1000; + private static final int LONG_TEST_EXECUTION_TIMEOUT = 90 * 1000; + private static final String TEST_FILE = "testfile"; + + public ITestAzureBlobFileSystemLease() throws Exception { + super(); + } + + @Test + public void testNoSingleWriter() throws IOException { + final AzureBlobFileSystem fs = getFileSystem(); + final Path testFilePath = new Path(path(methodName.getMethodName()), TEST_FILE); + fs.mkdirs(testFilePath.getParent()); + try (FSDataOutputStream out = fs.create(testFilePath)) { + Assert.assertFalse("Output stream should not have lease", + ((AbfsOutputStream) out.getWrappedStream()).hasLease()); + } + Assert.assertTrue(fs.getAbfsStore().areLeasesFreed()); + } + + @Test + public void testOneWriter() throws IOException { + final AzureBlobFileSystem fs = getFileSystem(); + final Path testFilePath = new Path(path(methodName.getMethodName()), TEST_FILE); + fs.mkdirs(testFilePath.getParent()); + fs.getAbfsStore().getAbfsConfiguration() + .setAzureSingleWriterDirs(testFilePath.getParent().toString()); + fs.getAbfsStore().updateSingleWriterDirs(); + + FSDataOutputStream out = fs.create(testFilePath); + Assert.assertTrue("Output stream should have lease", + ((AbfsOutputStream) out.getWrappedStream()).hasLease()); + out.close(); + Assert.assertFalse("Output stream should not have lease", + ((AbfsOutputStream) out.getWrappedStream()).hasLease()); + Assert.assertTrue(fs.getAbfsStore().areLeasesFreed()); + } + + @Test + public void testSubDir() throws IOException { + final AzureBlobFileSystem fs = getFileSystem(); + final Path testFilePath = new Path(new Path(path(methodName.getMethodName()), "subdir"), + TEST_FILE); + fs.mkdirs(testFilePath.getParent().getParent()); + fs.getAbfsStore().getAbfsConfiguration() + .setAzureSingleWriterDirs(testFilePath.getParent().getParent().toString()); + fs.getAbfsStore().updateSingleWriterDirs(); + + FSDataOutputStream out = fs.create(testFilePath); + Assert.assertTrue("Output stream should have lease", + ((AbfsOutputStream) out.getWrappedStream()).hasLease()); + out.close(); + Assert.assertFalse("Output stream should not have lease", + ((AbfsOutputStream) out.getWrappedStream()).hasLease()); + Assert.assertTrue(fs.getAbfsStore().areLeasesFreed()); + } + + @Test + public void testTwoCreate() throws IOException { + final AzureBlobFileSystem fs = getFileSystem(); + final Path testFilePath = new Path(path(methodName.getMethodName()), TEST_FILE); + fs.mkdirs(testFilePath.getParent()); + fs.getAbfsStore().getAbfsConfiguration() + .setAzureSingleWriterDirs(testFilePath.getParent().toString()); + fs.getAbfsStore().updateSingleWriterDirs(); + + try (FSDataOutputStream out = fs.create(testFilePath)) { + try (FSDataOutputStream out2 = fs.create(testFilePath)) { + Assert.fail("Second create succeeded"); + } catch (IOException e) { + Assert.assertTrue("Unexpected error message: " + e.getMessage(), + e.getMessage().contains(ERR_PARALLEL_ACCESS_DETECTED)); + } + } + Assert.assertTrue(fs.getAbfsStore().areLeasesFreed()); + } + + private void twoWriters(AzureBlobFileSystem fs, Path testFilePath, boolean expectException) throws Exception { + try (FSDataOutputStream out = fs.create(testFilePath)) { + try (FSDataOutputStream out2 = fs.append(testFilePath)) { + out2.writeInt(2); + out2.hsync(); + } catch (IOException e) { + if (expectException) { + Assert.assertTrue("Unexpected error message: " + e.getMessage(), + e.getMessage().contains(ERR_ACQUIRING_LEASE)); + } else { + Assert.fail("Unexpected exception " + e.getMessage()); + } + } + out.writeInt(1); + out.hsync(); + } + + Assert.assertTrue(fs.getAbfsStore().areLeasesFreed()); + } + + @Test(timeout = TEST_EXECUTION_TIMEOUT) + public void testTwoWritersCreateAppendNoSingleWriter() throws Exception { + final AzureBlobFileSystem fs = getFileSystem(); + final Path testFilePath = new Path(path(methodName.getMethodName()), TEST_FILE); + fs.mkdirs(testFilePath.getParent()); + + twoWriters(fs, testFilePath, false); + } + + @Test(timeout = TEST_EXECUTION_TIMEOUT) + public void testTwoWritersCreateAppendWithSingleWriterEnabled() throws Exception { + final AzureBlobFileSystem fs = getFileSystem(); + final Path testFilePath = new Path(path(methodName.getMethodName()), TEST_FILE); + fs.mkdirs(testFilePath.getParent()); + fs.getAbfsStore().getAbfsConfiguration() + .setAzureSingleWriterDirs(testFilePath.getParent().toString()); + fs.getAbfsStore().updateSingleWriterDirs(); + + twoWriters(fs, testFilePath, true); + } + + @Test(timeout = TEST_EXECUTION_TIMEOUT) + public void testLeaseFreedOnClose() throws IOException { + final AzureBlobFileSystem fs = getFileSystem(); + final Path testFilePath = new Path(path(methodName.getMethodName()), TEST_FILE); + fs.mkdirs(testFilePath.getParent()); + fs.getAbfsStore().getAbfsConfiguration() + .setAzureSingleWriterDirs(testFilePath.getParent().toString()); + fs.getAbfsStore().updateSingleWriterDirs(); + + FSDataOutputStream out; + out = fs.create(testFilePath); + out.write(0); + Assert.assertTrue("Output stream should have lease", + ((AbfsOutputStream) out.getWrappedStream()).hasLease()); + out.close(); + Assert.assertFalse("Output stream should not have lease after close", + ((AbfsOutputStream) out.getWrappedStream()).hasLease()); + Assert.assertTrue(fs.getAbfsStore().areLeasesFreed()); + } + + @Test(timeout = TEST_EXECUTION_TIMEOUT) + public void testWriteAfterBreakLease() throws IOException { + final AzureBlobFileSystem fs = getFileSystem(); + final Path testFilePath = new Path(path(methodName.getMethodName()), TEST_FILE); + fs.mkdirs(testFilePath.getParent()); + fs.getAbfsStore().getAbfsConfiguration() + .setAzureSingleWriterDirs(testFilePath.getParent().toString()); + fs.getAbfsStore().updateSingleWriterDirs(); + + FSDataOutputStream out; + out = fs.create(testFilePath); + out.write(0); + out.hsync(); + + fs.breakLease(testFilePath); + try { + out.write(1); + out.hsync(); + Assert.fail("Expected exception on write after lease break"); + } catch (IOException e) { + Assert.assertTrue("Unexpected error message: " + e.getMessage(), + e.getMessage().contains(ERR_LEASE_EXPIRED)); + } + try { + out.close(); + Assert.fail("Expected exception on close after lease break"); + } catch (IOException e) { + Assert.assertTrue("Unexpected error message: " + e.getMessage(), + e.getMessage().contains(ERR_LEASE_EXPIRED)); + } + + Assert.assertTrue(((AbfsOutputStream) out.getWrappedStream()).isLeaseFreed()); + + try (FSDataOutputStream out2 = fs.append(testFilePath)) { + out2.write(2); + out2.hsync(); + } + + Assert.assertTrue(fs.getAbfsStore().areLeasesFreed()); + } + + @Test(timeout = LONG_TEST_EXECUTION_TIMEOUT) + public void testLeaseFreedAfterBreak() throws IOException { + final AzureBlobFileSystem fs = getFileSystem(); + final Path testFilePath = new Path(path(methodName.getMethodName()), TEST_FILE); + fs.mkdirs(testFilePath.getParent()); + fs.getAbfsStore().getAbfsConfiguration() + .setAzureSingleWriterDirs(testFilePath.getParent().toString()); + fs.getAbfsStore().updateSingleWriterDirs(); + + FSDataOutputStream out = null; + try { + out = fs.create(testFilePath); + out.write(0); + + fs.breakLease(testFilePath); + while (!((AbfsOutputStream) out.getWrappedStream()).isLeaseFreed()) { + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + } + } + } finally { + try { + if (out != null) { + out.close(); + } + // exception might or might not occur + } catch (IOException e) { Review comment: GenericTestUtils lets you assert something is in the error message. Its critical to rethrow (maybe wrapped) the exception if it is not the one you were expecting ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: common-issues-unsubscr...@hadoop.apache.org For additional commands, e-mail: common-issues-h...@hadoop.apache.org