-
Notifications
You must be signed in to change notification settings - Fork 2.5k
[HUDI-4065] Add FileBasedLockProvider #6071
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
be4aeae
[HUDI-2413] fix Sql source's checkpoint
02d8da9
Revert "[HUDI-2413] fix Sql source's checkpoint"
eb14274
Merge pull request #1 from apache/master
fengjian428 c281194
Merge pull request #2 from apache/master
fengjian428 7f4c141
Merge pull request #3 from apache/master
fengjian428 06ea24c
Merge pull request #4 from apache/master
fengjian428 4885c98
Merge pull request #5 from apache/master
fengjian428 179dc04
Merge pull request #6 from apache/master
fengjian428 8a533e8
Merge pull request #7 from apache/master
fengjian428 ee3559c
Merge pull request #8 from apache/master
fengjian428 e2eca01
Merge pull request #9 from apache/master
fengjian428 25ea8db
Merge pull request #10 from apache/master
fengjian428 424c1da
Merge pull request #12 from apache/master
fengjian428 b12f205
[HUDI-4065] FileBasedLockProvider
b57aab2
[HUDI-4065] FileBasedLockProvider review comments
63568d7
[HUDI-4065] minor update
cae68ae
[HUDI-4065] minor update
4dca5ab
[HUDI-4065] add multiple writers ut for file lock
4766951
[HUDI-4065] delete the retry logic in FileSystemBasedLockProvider sin…
d5254f4
[HUDI-4065] fix ut
68c8625
[HUDI-4065] fix ut
8fce774
[HUDI-4065] fix ut
ef58952
[HUDI-4065] use meta folder as default
5fa8809
[HUDI-4065] minor update
4d68900
Merge remote-tracking branch 'upstream/master' into HUDI-4065-NEW
35d61e0
Merge remote-tracking branch 'upstream/master' into HUDI-4065-NEW
be5e2b1
[HUDI-4065] minor update
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
152 changes: 152 additions & 0 deletions
152
...on/src/main/java/org/apache/hudi/client/transaction/lock/FileSystemBasedLockProvider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,152 @@ | ||
| /* | ||
| * 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.hudi.client.transaction.lock; | ||
|
|
||
| import org.apache.hadoop.conf.Configuration; | ||
| import org.apache.hadoop.fs.FileSystem; | ||
| import org.apache.hadoop.fs.Path; | ||
| import org.apache.hudi.common.config.LockConfiguration; | ||
| import org.apache.hudi.common.fs.FSUtils; | ||
| import org.apache.hudi.common.lock.LockProvider; | ||
| import org.apache.hudi.common.lock.LockState; | ||
| import org.apache.hudi.common.table.HoodieTableMetaClient; | ||
| import org.apache.hudi.common.util.StringUtils; | ||
| import org.apache.hudi.common.util.ValidationUtils; | ||
| import org.apache.hudi.config.HoodieWriteConfig; | ||
| import org.apache.hudi.exception.HoodieIOException; | ||
| import org.apache.hudi.exception.HoodieLockException; | ||
| import org.apache.log4j.LogManager; | ||
| import org.apache.log4j.Logger; | ||
|
|
||
| import java.io.IOException; | ||
| import java.io.Serializable; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import static org.apache.hudi.common.config.LockConfiguration.FILESYSTEM_LOCK_EXPIRE_PROP_KEY; | ||
| import static org.apache.hudi.common.config.LockConfiguration.FILESYSTEM_LOCK_PATH_PROP_KEY; | ||
|
|
||
| /** | ||
| * A FileSystem based lock. This {@link LockProvider} implementation allows to lock table operations | ||
| * using DFS. Users might need to manually clean the Locker's path if writeClient crash and never run again. | ||
| * NOTE: This only works for DFS with atomic create/delete operation | ||
| */ | ||
| public class FileSystemBasedLockProvider implements LockProvider<String>, Serializable { | ||
|
|
||
| private static final Logger LOG = LogManager.getLogger(FileSystemBasedLockProvider.class); | ||
|
|
||
| private static final String LOCK_FILE_NAME = "lock"; | ||
|
|
||
| private final int lockTimeoutMinutes; | ||
| private transient FileSystem fs; | ||
| private transient Path lockFile; | ||
| protected LockConfiguration lockConfiguration; | ||
|
|
||
| public FileSystemBasedLockProvider(final LockConfiguration lockConfiguration, final Configuration configuration) { | ||
| checkRequiredProps(lockConfiguration); | ||
| this.lockConfiguration = lockConfiguration; | ||
| String lockDirectory = lockConfiguration.getConfig().getString(FILESYSTEM_LOCK_PATH_PROP_KEY, null); | ||
| if (StringUtils.isNullOrEmpty(lockDirectory)) { | ||
| lockDirectory = lockConfiguration.getConfig().getString(HoodieWriteConfig.BASE_PATH.key()) | ||
| + Path.SEPARATOR + HoodieTableMetaClient.METAFOLDER_NAME; | ||
| } | ||
| this.lockTimeoutMinutes = lockConfiguration.getConfig().getInteger(FILESYSTEM_LOCK_EXPIRE_PROP_KEY); | ||
| this.lockFile = new Path(lockDirectory + Path.SEPARATOR + LOCK_FILE_NAME); | ||
| this.fs = FSUtils.getFs(this.lockFile.toString(), configuration); | ||
| } | ||
|
|
||
| @Override | ||
| public void close() { | ||
| synchronized (LOCK_FILE_NAME) { | ||
| try { | ||
| fs.delete(this.lockFile, true); | ||
| } catch (IOException e) { | ||
| throw new HoodieLockException(generateLogStatement(LockState.FAILED_TO_RELEASE), e); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public boolean tryLock(long time, TimeUnit unit) { | ||
| try { | ||
| synchronized (LOCK_FILE_NAME) { | ||
| // Check whether lock is already expired, if so try to delete lock file | ||
| if (fs.exists(this.lockFile) && checkIfExpired()) { | ||
| fs.delete(this.lockFile, true); | ||
| } | ||
| acquireLock(); | ||
| return fs.exists(this.lockFile); | ||
| } | ||
| } catch (IOException | HoodieIOException e) { | ||
| LOG.info(generateLogStatement(LockState.FAILED_TO_ACQUIRE), e); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void unlock() { | ||
| synchronized (LOCK_FILE_NAME) { | ||
| try { | ||
| if (fs.exists(this.lockFile)) { | ||
| fs.delete(this.lockFile, true); | ||
| } | ||
| } catch (IOException io) { | ||
| throw new HoodieIOException(generateLogStatement(LockState.FAILED_TO_RELEASE), io); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public String getLock() { | ||
| return this.lockFile.toString(); | ||
| } | ||
|
|
||
| private boolean checkIfExpired() { | ||
| if (lockTimeoutMinutes == 0) { | ||
| return false; | ||
| } | ||
| try { | ||
| long modificationTime = fs.getFileStatus(this.lockFile).getModificationTime(); | ||
| if (System.currentTimeMillis() - modificationTime > lockTimeoutMinutes * 60 * 1000) { | ||
| return true; | ||
| } | ||
| } catch (IOException | HoodieIOException e) { | ||
| LOG.error(generateLogStatement(LockState.ALREADY_RELEASED) + " failed to get lockFile's modification time", e); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| private void acquireLock() { | ||
| try { | ||
| fs.create(this.lockFile, false).close(); | ||
| } catch (IOException e) { | ||
| throw new HoodieIOException(generateLogStatement(LockState.FAILED_TO_ACQUIRE), e); | ||
| } | ||
| } | ||
|
|
||
| protected String generateLogStatement(LockState state) { | ||
| return StringUtils.join(state.name(), " lock at: ", getLock()); | ||
| } | ||
|
|
||
| private void checkRequiredProps(final LockConfiguration config) { | ||
| ValidationUtils.checkArgument(config.getConfig().getString(FILESYSTEM_LOCK_PATH_PROP_KEY, null) != null | ||
| || config.getConfig().getString(HoodieWriteConfig.BASE_PATH.key(), null) != null); | ||
fengjian428 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ValidationUtils.checkArgument(config.getConfig().getInteger(FILESYSTEM_LOCK_EXPIRE_PROP_KEY) >= 0); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
...ent/hudi-spark-client/src/test/java/org/apache/hudi/client/TestFileBasedLockProvider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| /* | ||
| * 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.hudi.client; | ||
|
|
||
| import org.apache.hadoop.conf.Configuration; | ||
| import org.apache.hadoop.fs.FileSystem; | ||
| import org.apache.hadoop.fs.Path; | ||
| import org.apache.hadoop.hdfs.MiniDFSCluster; | ||
| import org.apache.hudi.client.transaction.lock.FileSystemBasedLockProvider; | ||
| import org.apache.hudi.common.config.LockConfiguration; | ||
| import org.apache.hudi.common.testutils.minicluster.HdfsTestService; | ||
| import org.apache.hudi.config.HoodieWriteConfig; | ||
| import org.apache.hudi.exception.HoodieLockException; | ||
| import org.junit.jupiter.api.AfterAll; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Properties; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| import static org.apache.hudi.common.config.LockConfiguration.FILESYSTEM_LOCK_EXPIRE_PROP_KEY; | ||
| import static org.apache.hudi.common.config.LockConfiguration.FILESYSTEM_LOCK_PATH_PROP_KEY; | ||
| import static org.apache.hudi.common.config.LockConfiguration.LOCK_ACQUIRE_NUM_RETRIES_PROP_KEY; | ||
| import static org.apache.hudi.common.config.LockConfiguration.LOCK_ACQUIRE_RETRY_WAIT_TIME_IN_MILLIS_PROP_KEY; | ||
| import static org.apache.hudi.common.config.LockConfiguration.LOCK_ACQUIRE_WAIT_TIMEOUT_MS_PROP_KEY; | ||
|
|
||
| public class TestFileBasedLockProvider { | ||
| private static HdfsTestService hdfsTestService; | ||
| private static MiniDFSCluster dfsCluster; | ||
| private static LockConfiguration lockConfiguration; | ||
| private static Configuration hadoopConf; | ||
|
|
||
| @BeforeAll | ||
| public static void setup() throws IOException { | ||
| hdfsTestService = new HdfsTestService(); | ||
| dfsCluster = hdfsTestService.start(true); | ||
| hadoopConf = dfsCluster.getFileSystem().getConf(); | ||
|
|
||
| Properties properties = new Properties(); | ||
| properties.setProperty(FILESYSTEM_LOCK_PATH_PROP_KEY, "/tmp/"); | ||
| properties.setProperty(FILESYSTEM_LOCK_EXPIRE_PROP_KEY, "1"); | ||
| properties.setProperty(LOCK_ACQUIRE_WAIT_TIMEOUT_MS_PROP_KEY, "1000"); | ||
| properties.setProperty(LOCK_ACQUIRE_RETRY_WAIT_TIME_IN_MILLIS_PROP_KEY, "1000"); | ||
| properties.setProperty(LOCK_ACQUIRE_NUM_RETRIES_PROP_KEY, "3"); | ||
| lockConfiguration = new LockConfiguration(properties); | ||
| } | ||
|
|
||
| @AfterAll | ||
| public static void cleanUpAfterAll() throws IOException { | ||
| Path workDir = dfsCluster.getFileSystem().getWorkingDirectory(); | ||
| FileSystem fs = workDir.getFileSystem(hdfsTestService.getHadoopConf()); | ||
| fs.delete(new Path("/tmp"), true); | ||
| if (hdfsTestService != null) { | ||
| hdfsTestService.stop(); | ||
| hdfsTestService = null; | ||
| } | ||
| } | ||
|
|
||
| @AfterEach | ||
| public void cleanUpAfterEach() throws IOException { | ||
| Path workDir = dfsCluster.getFileSystem().getWorkingDirectory(); | ||
| FileSystem fs = workDir.getFileSystem(hdfsTestService.getHadoopConf()); | ||
| fs.delete(new Path("/tmp/lock"), true); | ||
| } | ||
|
|
||
| @Test | ||
| public void testAcquireLock() { | ||
| FileSystemBasedLockProvider fileBasedLockProvider = new FileSystemBasedLockProvider(lockConfiguration, hadoopConf); | ||
| Assertions.assertTrue(fileBasedLockProvider.tryLock(lockConfiguration.getConfig() | ||
| .getLong(LOCK_ACQUIRE_WAIT_TIMEOUT_MS_PROP_KEY), TimeUnit.MILLISECONDS)); | ||
| fileBasedLockProvider.unlock(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testAcquireLockWithDefaultPath() { | ||
| lockConfiguration.getConfig().remove(FILESYSTEM_LOCK_PATH_PROP_KEY); | ||
| lockConfiguration.getConfig().setProperty(HoodieWriteConfig.BASE_PATH.key(), "/tmp/"); | ||
| FileSystemBasedLockProvider fileBasedLockProvider = new FileSystemBasedLockProvider(lockConfiguration, hadoopConf); | ||
| Assertions.assertTrue(fileBasedLockProvider.tryLock(lockConfiguration.getConfig() | ||
| .getLong(LOCK_ACQUIRE_WAIT_TIMEOUT_MS_PROP_KEY), TimeUnit.MILLISECONDS)); | ||
| fileBasedLockProvider.unlock(); | ||
| lockConfiguration.getConfig().setProperty(FILESYSTEM_LOCK_PATH_PROP_KEY, "/tmp/"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testUnLock() { | ||
| FileSystemBasedLockProvider fileBasedLockProvider = new FileSystemBasedLockProvider(lockConfiguration, hadoopConf); | ||
| Assertions.assertTrue(fileBasedLockProvider.tryLock(lockConfiguration.getConfig() | ||
| .getLong(LOCK_ACQUIRE_WAIT_TIMEOUT_MS_PROP_KEY), TimeUnit.MILLISECONDS)); | ||
| fileBasedLockProvider.unlock(); | ||
| Assertions.assertTrue(fileBasedLockProvider.tryLock(lockConfiguration.getConfig() | ||
| .getLong(LOCK_ACQUIRE_WAIT_TIMEOUT_MS_PROP_KEY), TimeUnit.MILLISECONDS)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testReentrantLock() { | ||
| FileSystemBasedLockProvider fileBasedLockProvider = new FileSystemBasedLockProvider(lockConfiguration, hadoopConf); | ||
| Assertions.assertTrue(fileBasedLockProvider.tryLock(lockConfiguration.getConfig() | ||
| .getLong(LOCK_ACQUIRE_WAIT_TIMEOUT_MS_PROP_KEY), TimeUnit.MILLISECONDS)); | ||
| Assertions.assertFalse(fileBasedLockProvider.tryLock(lockConfiguration.getConfig() | ||
| .getLong(LOCK_ACQUIRE_WAIT_TIMEOUT_MS_PROP_KEY), TimeUnit.MILLISECONDS)); | ||
| fileBasedLockProvider.unlock(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testUnlockWithoutLock() { | ||
| try { | ||
| FileSystemBasedLockProvider fileBasedLockProvider = new FileSystemBasedLockProvider(lockConfiguration, hadoopConf); | ||
| fileBasedLockProvider.unlock(); | ||
| } catch (HoodieLockException e) { | ||
| Assertions.fail(); | ||
| } | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fengjian428 :
May I know if you guys tested against both hdfs and S3.
Atleast in S3, not sure if this works across all race conditions.
what if two processes exactly checks fs.exists() concurrently and both detects that file does not exist and goes ahead and creates the file. both creation could succeed. So, how do we ensure only one of them succeed.
@yanghua
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have left a comment in other PR as well https://github.com/apache/hudi/pull/7440/files#r1061068482