[
https://issues.apache.org/jira/browse/HADOOP-16948?focusedWorklogId=536478&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-536478
]
ASF GitHub Bot logged work on HADOOP-16948:
-------------------------------------------
Author: ASF GitHub Bot
Created on: 15/Jan/21 13:26
Start Date: 15/Jan/21 13:26
Worklog Time Spent: 10m
Work Description: steveloughran commented on a change in pull request
#1925:
URL: https://github.com/apache/hadoop/pull/1925#discussion_r558290813
##########
File path:
hadoop-tools/hadoop-azure/src/main/java/org/apache/hadoop/fs/azurebfs/services/AbfsClient.java
##########
@@ -29,13 +29,23 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
+import java.util.UUID;
+import java.util.concurrent.Callable;
+import java.util.concurrent.Executors;
+import java.util.concurrent.TimeUnit;
import
org.apache.hadoop.thirdparty.com.google.common.annotations.VisibleForTesting;
import org.apache.hadoop.thirdparty.com.google.common.base.Strings;
import org.apache.hadoop.security.ssl.DelegatingSSLSocketFactory;
import org.apache.hadoop.fs.azurebfs.constants.AbfsHttpConstants;
import org.apache.hadoop.fs.azurebfs.constants.HttpHeaderConfigurations;
import org.apache.hadoop.fs.azurebfs.constants.HttpQueryParams;
+import
org.apache.hadoop.thirdparty.com.google.common.util.concurrent.FutureCallback;
Review comment:
can you pull up to the .thirdparty section and add a newline after. Our
guava shading project is confusing IDEs and making backporting/cherrypicking
harder
##########
File path:
hadoop-tools/hadoop-azure/src/main/java/org/apache/hadoop/fs/azurebfs/services/AbfsClient.java
##########
@@ -106,6 +118,9 @@ private AbfsClient(final URL baseUrl, final
SharedKeyCredentials sharedKeyCreden
this.userAgent = initializeUserAgent(abfsConfiguration, sslProviderName);
this.abfsPerfTracker = abfsClientContext.getAbfsPerfTracker();
this.abfsCounters = abfsClientContext.getAbfsCounters();
+
+ this.executorService = MoreExecutors.listeningDecorator(
+
Executors.newScheduledThreadPool(this.abfsConfiguration.getNumLeaseThreads()));
Review comment:
Prefer you use HadoopExecutors here.
##########
File path:
hadoop-tools/hadoop-azure/src/main/java/org/apache/hadoop/fs/azurebfs/services/AbfsClient.java
##########
@@ -306,6 +322,83 @@ public AbfsRestOperation createPath(final String path,
final boolean isFile, fin
return op;
}
+ public AbfsRestOperation acquireLease(final String path, int duration)
throws AzureBlobFileSystemException {
+ final List<AbfsHttpHeader> requestHeaders = createDefaultHeaders();
+
+ requestHeaders.add(new AbfsHttpHeader(X_MS_LEASE_ACTION,
ACQUIRE_LEASE_ACTION));
+ requestHeaders.add(new AbfsHttpHeader(X_MS_LEASE_DURATION,
Integer.toString(duration)));
+ requestHeaders.add(new AbfsHttpHeader(X_MS_PROPOSED_LEASE_ID,
UUID.randomUUID().toString()));
+
+ final AbfsUriQueryBuilder abfsUriQueryBuilder =
createDefaultUriQueryBuilder();
+
+ final URL url = createRequestUrl(path, abfsUriQueryBuilder.toString());
+ final AbfsRestOperation op = new AbfsRestOperation(
Review comment:
if the RestOperation doesn't log anything, add something here. Will help
debug locking problems
##########
File path:
hadoop-tools/hadoop-azure/src/main/java/org/apache/hadoop/fs/azurebfs/services/AbfsClient.java
##########
@@ -306,6 +322,83 @@ public AbfsRestOperation createPath(final String path,
final boolean isFile, fin
return op;
}
+ public AbfsRestOperation acquireLease(final String path, int duration)
throws AzureBlobFileSystemException {
+ final List<AbfsHttpHeader> requestHeaders = createDefaultHeaders();
+
+ requestHeaders.add(new AbfsHttpHeader(X_MS_LEASE_ACTION,
ACQUIRE_LEASE_ACTION));
+ requestHeaders.add(new AbfsHttpHeader(X_MS_LEASE_DURATION,
Integer.toString(duration)));
+ requestHeaders.add(new AbfsHttpHeader(X_MS_PROPOSED_LEASE_ID,
UUID.randomUUID().toString()));
+
+ final AbfsUriQueryBuilder abfsUriQueryBuilder =
createDefaultUriQueryBuilder();
+
+ final URL url = createRequestUrl(path, abfsUriQueryBuilder.toString());
+ final AbfsRestOperation op = new AbfsRestOperation(
+ AbfsRestOperationType.LeasePath,
+ this,
+ HTTP_METHOD_POST,
+ url,
+ requestHeaders);
+ op.execute();
+ return op;
+ }
+
+ public AbfsRestOperation renewLease(final String path, final String leaseId)
throws AzureBlobFileSystemException {
+ final List<AbfsHttpHeader> requestHeaders = createDefaultHeaders();
+
+ requestHeaders.add(new AbfsHttpHeader(X_MS_LEASE_ACTION,
RENEW_LEASE_ACTION));
+ requestHeaders.add(new AbfsHttpHeader(X_MS_LEASE_ID, leaseId));
+
+ final AbfsUriQueryBuilder abfsUriQueryBuilder =
createDefaultUriQueryBuilder();
+
+ final URL url = createRequestUrl(path, abfsUriQueryBuilder.toString());
+ final AbfsRestOperation op = new AbfsRestOperation(
+ AbfsRestOperationType.LeasePath,
+ this,
+ HTTP_METHOD_POST,
+ url,
+ requestHeaders);
+ op.execute();
+ return op;
+ }
+
+ public AbfsRestOperation releaseLease(final String path, final String
leaseId) throws AzureBlobFileSystemException {
+ final List<AbfsHttpHeader> requestHeaders = createDefaultHeaders();
+
+ requestHeaders.add(new AbfsHttpHeader(X_MS_LEASE_ACTION,
RELEASE_LEASE_ACTION));
+ requestHeaders.add(new AbfsHttpHeader(X_MS_LEASE_ID, leaseId));
+
+ final AbfsUriQueryBuilder abfsUriQueryBuilder =
createDefaultUriQueryBuilder();
+
+ final URL url = createRequestUrl(path, abfsUriQueryBuilder.toString());
+ final AbfsRestOperation op = new AbfsRestOperation(
Review comment:
+log
##########
File path:
hadoop-tools/hadoop-azure/src/main/java/org/apache/hadoop/fs/azurebfs/services/SelfRenewingLease.java
##########
@@ -0,0 +1,217 @@
+/**
+ * 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.services;
+
+import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.fs.azurebfs.constants.HttpHeaderConfigurations;
+import
org.apache.hadoop.fs.azurebfs.contracts.exceptions.AbfsRestOperationException;
+import
org.apache.hadoop.fs.azurebfs.contracts.exceptions.AzureBlobFileSystemException;
+import org.apache.hadoop.io.retry.RetryPolicies;
+import org.apache.hadoop.io.retry.RetryPolicy;
+import org.apache.hadoop.thirdparty.com.google.common.base.Preconditions;
+import
org.apache.hadoop.thirdparty.com.google.common.util.concurrent.FutureCallback;
+import
org.apache.hadoop.thirdparty.com.google.common.util.concurrent.ListenableScheduledFuture;
+import
org.apache.hadoop.thirdparty.org.checkerframework.checker.nullness.qual.Nullable;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.IOException;
+import java.util.concurrent.CancellationException;
+import java.util.concurrent.TimeUnit;
+
+import static java.net.HttpURLConnection.HTTP_INTERNAL_ERROR;
+import static
org.apache.hadoop.fs.azurebfs.services.AbfsErrors.ERR_ACQUIRING_LEASE;
+import static
org.apache.hadoop.fs.azurebfs.services.AbfsErrors.ERR_LEASE_FUTURE_EXISTS;
+import static
org.apache.hadoop.fs.azurebfs.services.AbfsErrors.ERR_NO_LEASE_THREADS;
+
+/**
+ * An Azure blob lease that automatically renews itself indefinitely by
scheduling lease
+ * operations through the ABFS client. Use it to prevent writes to the blob by
other processes
+ * that don't have the lease.
+ *
+ * Creating a new Lease object blocks the caller until the Azure blob lease is
acquired. It will
+ * retry a fixed number of times before failing if there is a problem
acquiring the lease.
+ *
+ * Call free() to release the Lease. If the holder process dies, the lease
will time out since it
+ * won't be renewed.
+ */
+public final class SelfRenewingLease {
+ private static final Logger LOG =
LoggerFactory.getLogger(SelfRenewingLease.class);
+
+ static final int LEASE_DURATION = 60; // Lease duration in seconds
+ static final int LEASE_RENEWAL_PERIOD = 40; // Lease renewal interval in
seconds
+
+ static final int LEASE_ACQUIRE_RETRY_INTERVAL = 10; // Retry interval for
acquiring lease in secs
+ static final int LEASE_ACQUIRE_MAX_RETRIES = 7; // Number of retries for
acquiring lease
+
+ private final AbfsClient client;
+ private final String path;
+
+ // Lease status variables
+ private volatile boolean leaseFreed;
+ private volatile String leaseID = null;
+ private volatile Throwable exception = null;
+ private volatile ListenableScheduledFuture<AbfsRestOperation> future = null;
+
+ public static class LeaseException extends AzureBlobFileSystemException {
+ public LeaseException(Throwable t) {
+ super(ERR_ACQUIRING_LEASE + ": " + t.getMessage());
Review comment:
keep t's stack trace by passing up to superclass or in initCause()
##########
File path:
hadoop-tools/hadoop-azure/src/main/java/org/apache/hadoop/fs/azurebfs/AzureBlobFileSystemStore.java
##########
@@ -243,7 +255,24 @@ public String getPrimaryGroup() {
@Override
public void close() throws IOException {
- IOUtils.cleanupWithLogger(LOG, client);
+ List<ListenableFuture<?>> futures = new ArrayList<>();
+ for (SelfRenewingLease lease : leaseRefs.keySet()) {
+ if (lease == null) {
+ continue;
+ }
+ ListenableFuture<?> future = client.submit(() -> lease.free());
Review comment:
do we need to worry about running out of workers here, timeouts etc?
##########
File path:
hadoop-tools/hadoop-azure/src/test/java/org/apache/hadoop/fs/azurebfs/ITestAzureBlobFileSystemLease.java
##########
@@ -0,0 +1,295 @@
+/**
+ * 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.conf.Configuration;
+import org.apache.hadoop.fs.FSDataOutputStream;
+import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.fs.azurebfs.services.AbfsOutputStream;
+import org.apache.hadoop.test.GenericTestUtils;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.util.concurrent.RejectedExecutionException;
+
+import static
org.apache.hadoop.fs.azurebfs.constants.ConfigurationKeys.FS_AZURE_LEASE_THREADS;
+import static
org.apache.hadoop.fs.azurebfs.constants.ConfigurationKeys.FS_AZURE_SINGLE_WRITER_KEY;
+import static
org.apache.hadoop.fs.azurebfs.constants.TestConfigurationKeys.FS_AZURE_TEST_NAMESPACE_ENABLED_ACCOUNT;
+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_LEASE_NOT_PRESENT;
+import static
org.apache.hadoop.fs.azurebfs.services.AbfsErrors.ERR_NO_LEASE_ID_SPECIFIED;
+import static
org.apache.hadoop.fs.azurebfs.services.AbfsErrors.ERR_NO_LEASE_THREADS;
+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";
+ private final boolean isHNSEnabled;
+
+ public ITestAzureBlobFileSystemLease() throws Exception {
+ super();
+
+ this.isHNSEnabled = getConfiguration()
+ .getBoolean(FS_AZURE_TEST_NAMESPACE_ENABLED_ACCOUNT, false);
+ }
+
+ private AzureBlobFileSystem getCustomFileSystem(String singleWriterDirs, int
numLeaseThreads)
+ throws Exception {
+ Configuration conf = getRawConfiguration();
+ conf.setBoolean(String.format("fs.%s.impl.disable.cache",
getAbfsScheme()), true);
+ conf.set(FS_AZURE_SINGLE_WRITER_KEY, singleWriterDirs);
+ conf.setInt(FS_AZURE_LEASE_THREADS, numLeaseThreads);
+ return getFileSystem(conf);
+ }
+
+ @Test
+ public void testNoSingleWriter() throws IOException {
+ final Path testFilePath = new Path(path(methodName.getMethodName()),
TEST_FILE);
+ final AzureBlobFileSystem fs = getFileSystem();
+ 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 testNoLeaseThreads() throws Exception {
+ final Path testFilePath = new Path(path(methodName.getMethodName()),
TEST_FILE);
+ final AzureBlobFileSystem fs =
getCustomFileSystem(testFilePath.getParent().toString(), 0);
+ fs.mkdirs(testFilePath.getParent());
+ try (FSDataOutputStream out = fs.create(testFilePath)) {
+ Assert.fail("No failure when lease requested with 0 lease threads");
+ } catch (Exception e) {
+ GenericTestUtils.assertExceptionContains(ERR_NO_LEASE_THREADS, e);
+ }
+ }
+
+ @Test
+ public void testOneWriter() throws Exception {
+ final Path testFilePath = new Path(path(methodName.getMethodName()),
TEST_FILE);
+ final AzureBlobFileSystem fs =
getCustomFileSystem(testFilePath.getParent().toString(), 1);
+ fs.mkdirs(testFilePath.getParent());
+
+ 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 Exception {
+ final Path testFilePath = new Path(new
Path(path(methodName.getMethodName()), "subdir"),
+ TEST_FILE);
+ final AzureBlobFileSystem fs =
+ getCustomFileSystem(testFilePath.getParent().getParent().toString(),
1);
+ fs.mkdirs(testFilePath.getParent().getParent());
+
+ 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 Exception {
+ final Path testFilePath = new Path(path(methodName.getMethodName()),
TEST_FILE);
+ final AzureBlobFileSystem fs =
getCustomFileSystem(testFilePath.getParent().toString(), 1);
+ fs.mkdirs(testFilePath.getParent());
+
+ try (FSDataOutputStream out = fs.create(testFilePath)) {
+ try (FSDataOutputStream out2 = fs.create(testFilePath)) {
+ Assert.fail("Second create succeeded");
+ } catch (IOException e) {
+ if (isHNSEnabled) {
+
GenericTestUtils.assertExceptionContains(ERR_PARALLEL_ACCESS_DETECTED, e);
+ } else {
+ GenericTestUtils.assertExceptionContains(ERR_NO_LEASE_ID_SPECIFIED,
e);
+ }
+ }
+ }
+ 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 Path testFilePath = new Path(path(methodName.getMethodName()),
TEST_FILE);
+ final AzureBlobFileSystem fs = getFileSystem();
+ fs.mkdirs(testFilePath.getParent());
+
+ twoWriters(fs, testFilePath, false);
+ }
+
+ @Test(timeout = LONG_TEST_EXECUTION_TIMEOUT)
+ public void testTwoWritersCreateAppendWithSingleWriterEnabled() throws
Exception {
+ final Path testFilePath = new Path(path(methodName.getMethodName()),
TEST_FILE);
+ final AzureBlobFileSystem fs =
getCustomFileSystem(testFilePath.getParent().toString(), 1);
+ fs.mkdirs(testFilePath.getParent());
+
+ twoWriters(fs, testFilePath, true);
+ }
+
+ @Test(timeout = TEST_EXECUTION_TIMEOUT)
+ public void testLeaseFreedOnClose() throws Exception {
+ final Path testFilePath = new Path(path(methodName.getMethodName()),
TEST_FILE);
+ final AzureBlobFileSystem fs =
getCustomFileSystem(testFilePath.getParent().toString(), 1);
+ fs.mkdirs(testFilePath.getParent());
+
+ 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 Exception {
+ final Path testFilePath = new Path(path(methodName.getMethodName()),
TEST_FILE);
+ final AzureBlobFileSystem fs =
getCustomFileSystem(testFilePath.getParent().toString(), 1);
+ fs.mkdirs(testFilePath.getParent());
+
+ FSDataOutputStream out;
+ out = fs.create(testFilePath);
+ out.write(0);
+ out.hsync();
+
+ fs.breakLease(testFilePath);
+ try {
Review comment:
use LambdaTestUtils; return a string with that error message in the
closure for it to be used in the exception. Ideally add out.toString() too. eg.
```
intercept(ioe, ERR_LEASE_EXPIRED, () -> {
out..write(1);
out.hsync();
return "expected exception but got " + out;
});
##########
File path:
hadoop-tools/hadoop-azure/src/main/java/org/apache/hadoop/fs/azurebfs/services/AbfsClient.java
##########
@@ -129,6 +144,7 @@ public void close() throws IOException {
if (tokenProvider instanceof Closeable) {
IOUtils.cleanupWithLogger(LOG, (Closeable) tokenProvider);
}
+ executorService.shutdownNow();
Review comment:
HadoopExecutors.shutdown has some error handling here
##########
File path:
hadoop-tools/hadoop-azure/src/main/java/org/apache/hadoop/fs/azurebfs/services/AbfsOutputStream.java
##########
@@ -312,6 +333,10 @@ public synchronized void close() throws IOException {
// See HADOOP-16785
throw wrapException(path, e.getMessage(), e);
} finally {
+ if (lease != null) {
Review comment:
use hasLease()
##########
File path:
hadoop-tools/hadoop-azure/src/main/java/org/apache/hadoop/fs/azurebfs/services/AbfsOutputStream.java
##########
@@ -209,6 +226,10 @@ public synchronized void write(final byte[] data, final
int off, final int lengt
throw new IndexOutOfBoundsException();
}
+ if (lease != null && lease.isFreed()) {
Review comment:
is `isLeaseFreed()`
##########
File path:
hadoop-tools/hadoop-azure/src/main/java/org/apache/hadoop/fs/azurebfs/services/AbfsOutputStream.java
##########
@@ -113,7 +120,8 @@ public AbfsOutputStream(
final Statistics statistics,
final String path,
final long position,
- AbfsOutputStreamContext abfsOutputStreamContext) {
+ final Map<SelfRenewingLease, Object> leaseRefs,
Review comment:
this should go into the AbfsOutputStreamContext, its where we are adding
more state for a stream constructor
##########
File path:
hadoop-tools/hadoop-azure/src/main/java/org/apache/hadoop/fs/azurebfs/services/SelfRenewingLease.java
##########
@@ -0,0 +1,217 @@
+/**
+ * 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.services;
+
+import org.apache.hadoop.fs.Path;
Review comment:
import ordering. should be java.* javax.* \n
non-asf-and-hadoop.thirdparty \n o.a.a
##########
File path:
hadoop-tools/hadoop-azure/src/main/java/org/apache/hadoop/fs/azurebfs/services/AbfsClient.java
##########
@@ -306,6 +322,83 @@ public AbfsRestOperation createPath(final String path,
final boolean isFile, fin
return op;
}
+ public AbfsRestOperation acquireLease(final String path, int duration)
throws AzureBlobFileSystemException {
+ final List<AbfsHttpHeader> requestHeaders = createDefaultHeaders();
+
+ requestHeaders.add(new AbfsHttpHeader(X_MS_LEASE_ACTION,
ACQUIRE_LEASE_ACTION));
+ requestHeaders.add(new AbfsHttpHeader(X_MS_LEASE_DURATION,
Integer.toString(duration)));
+ requestHeaders.add(new AbfsHttpHeader(X_MS_PROPOSED_LEASE_ID,
UUID.randomUUID().toString()));
+
+ final AbfsUriQueryBuilder abfsUriQueryBuilder =
createDefaultUriQueryBuilder();
+
+ final URL url = createRequestUrl(path, abfsUriQueryBuilder.toString());
+ final AbfsRestOperation op = new AbfsRestOperation(
+ AbfsRestOperationType.LeasePath,
+ this,
+ HTTP_METHOD_POST,
+ url,
+ requestHeaders);
+ op.execute();
+ return op;
+ }
+
+ public AbfsRestOperation renewLease(final String path, final String leaseId)
throws AzureBlobFileSystemException {
+ final List<AbfsHttpHeader> requestHeaders = createDefaultHeaders();
+
+ requestHeaders.add(new AbfsHttpHeader(X_MS_LEASE_ACTION,
RENEW_LEASE_ACTION));
+ requestHeaders.add(new AbfsHttpHeader(X_MS_LEASE_ID, leaseId));
+
+ final AbfsUriQueryBuilder abfsUriQueryBuilder =
createDefaultUriQueryBuilder();
+
+ final URL url = createRequestUrl(path, abfsUriQueryBuilder.toString());
+ final AbfsRestOperation op = new AbfsRestOperation(
Review comment:
add a log if needed
##########
File path:
hadoop-tools/hadoop-azure/src/test/java/org/apache/hadoop/fs/azurebfs/ITestAzureBlobFileSystemLease.java
##########
@@ -0,0 +1,295 @@
+/**
+ * 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.conf.Configuration;
Review comment:
usual comment about import ordering.
##########
File path:
hadoop-tools/hadoop-azure/src/main/java/org/apache/hadoop/fs/azurebfs/AzureBlobFileSystem.java
##########
@@ -39,6 +39,7 @@
import
org.apache.hadoop.thirdparty.com.google.common.annotations.VisibleForTesting;
import org.apache.hadoop.thirdparty.com.google.common.base.Preconditions;
+import org.apache.hadoop.util.DurationInfo;
Review comment:
move to lower group
##########
File path:
hadoop-tools/hadoop-azure/src/test/java/org/apache/hadoop/fs/azurebfs/ITestAzureBlobFileSystemLease.java
##########
@@ -0,0 +1,295 @@
+/**
+ * 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.conf.Configuration;
+import org.apache.hadoop.fs.FSDataOutputStream;
+import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.fs.azurebfs.services.AbfsOutputStream;
+import org.apache.hadoop.test.GenericTestUtils;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.util.concurrent.RejectedExecutionException;
+
+import static
org.apache.hadoop.fs.azurebfs.constants.ConfigurationKeys.FS_AZURE_LEASE_THREADS;
+import static
org.apache.hadoop.fs.azurebfs.constants.ConfigurationKeys.FS_AZURE_SINGLE_WRITER_KEY;
+import static
org.apache.hadoop.fs.azurebfs.constants.TestConfigurationKeys.FS_AZURE_TEST_NAMESPACE_ENABLED_ACCOUNT;
+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_LEASE_NOT_PRESENT;
+import static
org.apache.hadoop.fs.azurebfs.services.AbfsErrors.ERR_NO_LEASE_ID_SPECIFIED;
+import static
org.apache.hadoop.fs.azurebfs.services.AbfsErrors.ERR_NO_LEASE_THREADS;
+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";
+ private final boolean isHNSEnabled;
+
+ public ITestAzureBlobFileSystemLease() throws Exception {
+ super();
+
+ this.isHNSEnabled = getConfiguration()
+ .getBoolean(FS_AZURE_TEST_NAMESPACE_ENABLED_ACCOUNT, false);
+ }
+
+ private AzureBlobFileSystem getCustomFileSystem(String singleWriterDirs, int
numLeaseThreads)
+ throws Exception {
+ Configuration conf = getRawConfiguration();
+ conf.setBoolean(String.format("fs.%s.impl.disable.cache",
getAbfsScheme()), true);
+ conf.set(FS_AZURE_SINGLE_WRITER_KEY, singleWriterDirs);
+ conf.setInt(FS_AZURE_LEASE_THREADS, numLeaseThreads);
+ return getFileSystem(conf);
+ }
+
+ @Test
+ public void testNoSingleWriter() throws IOException {
+ final Path testFilePath = new Path(path(methodName.getMethodName()),
TEST_FILE);
+ final AzureBlobFileSystem fs = getFileSystem();
+ 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 testNoLeaseThreads() throws Exception {
+ final Path testFilePath = new Path(path(methodName.getMethodName()),
TEST_FILE);
+ final AzureBlobFileSystem fs =
getCustomFileSystem(testFilePath.getParent().toString(), 0);
+ fs.mkdirs(testFilePath.getParent());
+ try (FSDataOutputStream out = fs.create(testFilePath)) {
+ Assert.fail("No failure when lease requested with 0 lease threads");
+ } catch (Exception e) {
+ GenericTestUtils.assertExceptionContains(ERR_NO_LEASE_THREADS, e);
+ }
+ }
+
+ @Test
+ public void testOneWriter() throws Exception {
+ final Path testFilePath = new Path(path(methodName.getMethodName()),
TEST_FILE);
+ final AzureBlobFileSystem fs =
getCustomFileSystem(testFilePath.getParent().toString(), 1);
+ fs.mkdirs(testFilePath.getParent());
+
+ 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 Exception {
+ final Path testFilePath = new Path(new
Path(path(methodName.getMethodName()), "subdir"),
+ TEST_FILE);
+ final AzureBlobFileSystem fs =
+ getCustomFileSystem(testFilePath.getParent().getParent().toString(),
1);
+ fs.mkdirs(testFilePath.getParent().getParent());
+
+ 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 Exception {
+ final Path testFilePath = new Path(path(methodName.getMethodName()),
TEST_FILE);
+ final AzureBlobFileSystem fs =
getCustomFileSystem(testFilePath.getParent().toString(), 1);
+ fs.mkdirs(testFilePath.getParent());
+
+ try (FSDataOutputStream out = fs.create(testFilePath)) {
+ try (FSDataOutputStream out2 = fs.create(testFilePath)) {
+ Assert.fail("Second create succeeded");
+ } catch (IOException e) {
+ if (isHNSEnabled) {
+
GenericTestUtils.assertExceptionContains(ERR_PARALLEL_ACCESS_DETECTED, e);
+ } else {
+ GenericTestUtils.assertExceptionContains(ERR_NO_LEASE_ID_SPECIFIED,
e);
+ }
+ }
+ }
+ 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 Path testFilePath = new Path(path(methodName.getMethodName()),
TEST_FILE);
+ final AzureBlobFileSystem fs = getFileSystem();
+ fs.mkdirs(testFilePath.getParent());
+
+ twoWriters(fs, testFilePath, false);
+ }
+
+ @Test(timeout = LONG_TEST_EXECUTION_TIMEOUT)
+ public void testTwoWritersCreateAppendWithSingleWriterEnabled() throws
Exception {
+ final Path testFilePath = new Path(path(methodName.getMethodName()),
TEST_FILE);
+ final AzureBlobFileSystem fs =
getCustomFileSystem(testFilePath.getParent().toString(), 1);
+ fs.mkdirs(testFilePath.getParent());
+
+ twoWriters(fs, testFilePath, true);
+ }
+
+ @Test(timeout = TEST_EXECUTION_TIMEOUT)
+ public void testLeaseFreedOnClose() throws Exception {
+ final Path testFilePath = new Path(path(methodName.getMethodName()),
TEST_FILE);
+ final AzureBlobFileSystem fs =
getCustomFileSystem(testFilePath.getParent().toString(), 1);
+ fs.mkdirs(testFilePath.getParent());
+
+ 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 Exception {
+ final Path testFilePath = new Path(path(methodName.getMethodName()),
TEST_FILE);
+ final AzureBlobFileSystem fs =
getCustomFileSystem(testFilePath.getParent().toString(), 1);
+ fs.mkdirs(testFilePath.getParent());
+
+ 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) {
+ GenericTestUtils.assertExceptionContains(ERR_LEASE_EXPIRED, e);
+ }
+ try {
+ out.close();
Review comment:
do we really want a failure in close?
----------------------------------------------------------------
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:
[email protected]
Issue Time Tracking
-------------------
Worklog Id: (was: 536478)
Time Spent: 1h 40m (was: 1.5h)
> ABFS: Support single writer dirs
> --------------------------------
>
> Key: HADOOP-16948
> URL: https://issues.apache.org/jira/browse/HADOOP-16948
> Project: Hadoop Common
> Issue Type: Sub-task
> Reporter: Billie Rinaldi
> Assignee: Billie Rinaldi
> Priority: Minor
> Labels: abfsactive, pull-request-available
> Time Spent: 1h 40m
> Remaining Estimate: 0h
>
> This would allow some directories to be configured as single writer
> directories. The ABFS driver would obtain a lease when creating or opening a
> file for writing and would automatically renew the lease and release the
> lease when closing the file.
--
This message was sent by Atlassian Jira
(v8.3.4#803005)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]