|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.hudi.client; |
| 21 | + |
| 22 | +import org.apache.hadoop.conf.Configuration; |
| 23 | +import org.apache.hadoop.fs.FileSystem; |
| 24 | +import org.apache.hadoop.fs.Path; |
| 25 | +import org.apache.hadoop.hdfs.MiniDFSCluster; |
| 26 | +import org.apache.hudi.client.transaction.lock.FileSystemBasedLockProvider; |
| 27 | +import org.apache.hudi.common.config.LockConfiguration; |
| 28 | +import org.apache.hudi.common.testutils.minicluster.HdfsTestService; |
| 29 | +import org.apache.hudi.config.HoodieWriteConfig; |
| 30 | +import org.apache.hudi.exception.HoodieLockException; |
| 31 | +import org.junit.jupiter.api.AfterAll; |
| 32 | +import org.junit.jupiter.api.AfterEach; |
| 33 | +import org.junit.jupiter.api.Assertions; |
| 34 | +import org.junit.jupiter.api.BeforeAll; |
| 35 | +import org.junit.jupiter.api.Test; |
| 36 | + |
| 37 | +import java.io.IOException; |
| 38 | +import java.util.Properties; |
| 39 | +import java.util.concurrent.TimeUnit; |
| 40 | + |
| 41 | +import static org.apache.hudi.common.config.LockConfiguration.FILESYSTEM_LOCK_EXPIRE_PROP_KEY; |
| 42 | +import static org.apache.hudi.common.config.LockConfiguration.FILESYSTEM_LOCK_PATH_PROP_KEY; |
| 43 | +import static org.apache.hudi.common.config.LockConfiguration.LOCK_ACQUIRE_NUM_RETRIES_PROP_KEY; |
| 44 | +import static org.apache.hudi.common.config.LockConfiguration.LOCK_ACQUIRE_RETRY_WAIT_TIME_IN_MILLIS_PROP_KEY; |
| 45 | +import static org.apache.hudi.common.config.LockConfiguration.LOCK_ACQUIRE_WAIT_TIMEOUT_MS_PROP_KEY; |
| 46 | + |
| 47 | +public class TestFileBasedLockProvider { |
| 48 | + private static HdfsTestService hdfsTestService; |
| 49 | + private static MiniDFSCluster dfsCluster; |
| 50 | + private static LockConfiguration lockConfiguration; |
| 51 | + private static Configuration hadoopConf; |
| 52 | + |
| 53 | + @BeforeAll |
| 54 | + public static void setup() throws IOException { |
| 55 | + hdfsTestService = new HdfsTestService(); |
| 56 | + dfsCluster = hdfsTestService.start(true); |
| 57 | + hadoopConf = dfsCluster.getFileSystem().getConf(); |
| 58 | + |
| 59 | + Properties properties = new Properties(); |
| 60 | + properties.setProperty(FILESYSTEM_LOCK_PATH_PROP_KEY, "/tmp/"); |
| 61 | + properties.setProperty(FILESYSTEM_LOCK_EXPIRE_PROP_KEY, "1"); |
| 62 | + properties.setProperty(LOCK_ACQUIRE_WAIT_TIMEOUT_MS_PROP_KEY, "1000"); |
| 63 | + properties.setProperty(LOCK_ACQUIRE_RETRY_WAIT_TIME_IN_MILLIS_PROP_KEY, "1000"); |
| 64 | + properties.setProperty(LOCK_ACQUIRE_NUM_RETRIES_PROP_KEY, "3"); |
| 65 | + lockConfiguration = new LockConfiguration(properties); |
| 66 | + } |
| 67 | + |
| 68 | + @AfterAll |
| 69 | + public static void cleanUpAfterAll() throws IOException { |
| 70 | + Path workDir = dfsCluster.getFileSystem().getWorkingDirectory(); |
| 71 | + FileSystem fs = workDir.getFileSystem(hdfsTestService.getHadoopConf()); |
| 72 | + fs.delete(new Path("/tmp"), true); |
| 73 | + if (hdfsTestService != null) { |
| 74 | + hdfsTestService.stop(); |
| 75 | + hdfsTestService = null; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + @AfterEach |
| 80 | + public void cleanUpAfterEach() throws IOException { |
| 81 | + Path workDir = dfsCluster.getFileSystem().getWorkingDirectory(); |
| 82 | + FileSystem fs = workDir.getFileSystem(hdfsTestService.getHadoopConf()); |
| 83 | + fs.delete(new Path("/tmp/lock"), true); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void testAcquireLock() { |
| 88 | + FileSystemBasedLockProvider fileBasedLockProvider = new FileSystemBasedLockProvider(lockConfiguration, hadoopConf); |
| 89 | + Assertions.assertTrue(fileBasedLockProvider.tryLock(lockConfiguration.getConfig() |
| 90 | + .getLong(LOCK_ACQUIRE_WAIT_TIMEOUT_MS_PROP_KEY), TimeUnit.MILLISECONDS)); |
| 91 | + fileBasedLockProvider.unlock(); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void testAcquireLockWithDefaultPath() { |
| 96 | + lockConfiguration.getConfig().remove(FILESYSTEM_LOCK_PATH_PROP_KEY); |
| 97 | + lockConfiguration.getConfig().setProperty(HoodieWriteConfig.BASE_PATH.key(), "/tmp/"); |
| 98 | + FileSystemBasedLockProvider fileBasedLockProvider = new FileSystemBasedLockProvider(lockConfiguration, hadoopConf); |
| 99 | + Assertions.assertTrue(fileBasedLockProvider.tryLock(lockConfiguration.getConfig() |
| 100 | + .getLong(LOCK_ACQUIRE_WAIT_TIMEOUT_MS_PROP_KEY), TimeUnit.MILLISECONDS)); |
| 101 | + fileBasedLockProvider.unlock(); |
| 102 | + lockConfiguration.getConfig().setProperty(FILESYSTEM_LOCK_PATH_PROP_KEY, "/tmp/"); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void testUnLock() { |
| 107 | + FileSystemBasedLockProvider fileBasedLockProvider = new FileSystemBasedLockProvider(lockConfiguration, hadoopConf); |
| 108 | + Assertions.assertTrue(fileBasedLockProvider.tryLock(lockConfiguration.getConfig() |
| 109 | + .getLong(LOCK_ACQUIRE_WAIT_TIMEOUT_MS_PROP_KEY), TimeUnit.MILLISECONDS)); |
| 110 | + fileBasedLockProvider.unlock(); |
| 111 | + Assertions.assertTrue(fileBasedLockProvider.tryLock(lockConfiguration.getConfig() |
| 112 | + .getLong(LOCK_ACQUIRE_WAIT_TIMEOUT_MS_PROP_KEY), TimeUnit.MILLISECONDS)); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + public void testReentrantLock() { |
| 117 | + FileSystemBasedLockProvider fileBasedLockProvider = new FileSystemBasedLockProvider(lockConfiguration, hadoopConf); |
| 118 | + Assertions.assertTrue(fileBasedLockProvider.tryLock(lockConfiguration.getConfig() |
| 119 | + .getLong(LOCK_ACQUIRE_WAIT_TIMEOUT_MS_PROP_KEY), TimeUnit.MILLISECONDS)); |
| 120 | + Assertions.assertFalse(fileBasedLockProvider.tryLock(lockConfiguration.getConfig() |
| 121 | + .getLong(LOCK_ACQUIRE_WAIT_TIMEOUT_MS_PROP_KEY), TimeUnit.MILLISECONDS)); |
| 122 | + fileBasedLockProvider.unlock(); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void testUnlockWithoutLock() { |
| 127 | + try { |
| 128 | + FileSystemBasedLockProvider fileBasedLockProvider = new FileSystemBasedLockProvider(lockConfiguration, hadoopConf); |
| 129 | + fileBasedLockProvider.unlock(); |
| 130 | + } catch (HoodieLockException e) { |
| 131 | + Assertions.fail(); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | +} |
0 commit comments