nsivabalan commented on a change in pull request #4259: URL: https://github.com/apache/hudi/pull/4259#discussion_r766954296
########## File path: hudi-client/hudi-client-common/src/main/java/org/apache/hudi/client/transaction/lock/LocalProcessLockProvider.java ########## @@ -0,0 +1,127 @@ +/* + * 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.hudi.common.config.LockConfiguration; +import org.apache.hudi.common.config.TypedProperties; +import org.apache.hudi.common.lock.LockProvider; +import org.apache.hudi.common.lock.LockState; +import org.apache.hudi.common.util.StringUtils; +import org.apache.hudi.exception.HoodieLockException; +import org.apache.log4j.LogManager; +import org.apache.log4j.Logger; +import org.jetbrains.annotations.NotNull; + +import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +/** + * Local process level lock. This {@link LockProvider} implementation is to + * guard table from concurrent operations happening in the local JVM process. + * <p> + * Note: This Lock provider implementation doesn't allow lock reentrancy. + * Attempting to reacquire the lock from the same thread will throw + * HoodieLockException. Threads other than the current lock owner, will + * block on lock() and return false on tryLock(). + */ +public class LocalProcessLockProvider implements LockProvider<ReentrantReadWriteLock> { + + private static final Logger LOG = LogManager.getLogger(ZookeeperBasedLockProvider.class); + private static final ReentrantReadWriteLock LOCK = new ReentrantReadWriteLock(); + private final long maxWaitTimeMillis; + + public LocalProcessLockProvider(final LockConfiguration lockConfiguration, final Configuration conf) { + TypedProperties typedProperties = lockConfiguration.getConfig(); + maxWaitTimeMillis = (typedProperties.containsKey(LockConfiguration.LOCK_ACQUIRE_WAIT_TIMEOUT_MS_PROP_KEY) + ? lockConfiguration.getConfig().getLong(LockConfiguration.LOCK_ACQUIRE_WAIT_TIMEOUT_MS_PROP_KEY) : 0); + } + + @Override + public void lock() { + LOG.info(getLogMessage(LockState.ACQUIRING)); + if (LOCK.isWriteLockedByCurrentThread()) { + throw new HoodieLockException(getLogMessage(LockState.ALREADY_ACQUIRED)); + } + LOCK.writeLock().lock(); + LOG.info(getLogMessage(LockState.ACQUIRED)); + } + + @Override + public boolean tryLock() { + LOG.info(getLogMessage(LockState.ACQUIRING)); + if (LOCK.writeLock().isHeldByCurrentThread()) { + throw new HoodieLockException(getLogMessage(LockState.ALREADY_ACQUIRED)); + } + final boolean isLockAcquired; + try { + isLockAcquired = LOCK.writeLock().tryLock(maxWaitTimeMillis, TimeUnit.MILLISECONDS); Review comment: so, we are not using the synchronized block here, but relying on the reentrant lock to ensure only one caller acquires the lock and other fails if called concurrently ? ########## File path: hudi-client/hudi-client-common/src/main/java/org/apache/hudi/client/transaction/lock/LocalProcessLockProvider.java ########## @@ -0,0 +1,127 @@ +/* + * 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.hudi.common.config.LockConfiguration; +import org.apache.hudi.common.config.TypedProperties; +import org.apache.hudi.common.lock.LockProvider; +import org.apache.hudi.common.lock.LockState; +import org.apache.hudi.common.util.StringUtils; +import org.apache.hudi.exception.HoodieLockException; +import org.apache.log4j.LogManager; +import org.apache.log4j.Logger; +import org.jetbrains.annotations.NotNull; + +import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +/** + * Local process level lock. This {@link LockProvider} implementation is to + * guard table from concurrent operations happening in the local JVM process. + * <p> + * Note: This Lock provider implementation doesn't allow lock reentrancy. + * Attempting to reacquire the lock from the same thread will throw + * HoodieLockException. Threads other than the current lock owner, will + * block on lock() and return false on tryLock(). + */ +public class LocalProcessLockProvider implements LockProvider<ReentrantReadWriteLock> { + + private static final Logger LOG = LogManager.getLogger(ZookeeperBasedLockProvider.class); + private static final ReentrantReadWriteLock LOCK = new ReentrantReadWriteLock(); + private final long maxWaitTimeMillis; + + public LocalProcessLockProvider(final LockConfiguration lockConfiguration, final Configuration conf) { + TypedProperties typedProperties = lockConfiguration.getConfig(); + maxWaitTimeMillis = (typedProperties.containsKey(LockConfiguration.LOCK_ACQUIRE_WAIT_TIMEOUT_MS_PROP_KEY) + ? lockConfiguration.getConfig().getLong(LockConfiguration.LOCK_ACQUIRE_WAIT_TIMEOUT_MS_PROP_KEY) : 0); + } + + @Override + public void lock() { Review comment: I guess we call only tryLock and not lock(). will leave it to you. but other lock providers are not implementing lock() ########## File path: hudi-client/hudi-client-common/src/main/java/org/apache/hudi/client/transaction/lock/LocalProcessLockProvider.java ########## @@ -0,0 +1,127 @@ +/* + * 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.hudi.common.config.LockConfiguration; +import org.apache.hudi.common.config.TypedProperties; +import org.apache.hudi.common.lock.LockProvider; +import org.apache.hudi.common.lock.LockState; +import org.apache.hudi.common.util.StringUtils; +import org.apache.hudi.exception.HoodieLockException; +import org.apache.log4j.LogManager; +import org.apache.log4j.Logger; +import org.jetbrains.annotations.NotNull; + +import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +/** + * Local process level lock. This {@link LockProvider} implementation is to + * guard table from concurrent operations happening in the local JVM process. + * <p> + * Note: This Lock provider implementation doesn't allow lock reentrancy. + * Attempting to reacquire the lock from the same thread will throw + * HoodieLockException. Threads other than the current lock owner, will + * block on lock() and return false on tryLock(). + */ +public class LocalProcessLockProvider implements LockProvider<ReentrantReadWriteLock> { + + private static final Logger LOG = LogManager.getLogger(ZookeeperBasedLockProvider.class); + private static final ReentrantReadWriteLock LOCK = new ReentrantReadWriteLock(); + private final long maxWaitTimeMillis; + + public LocalProcessLockProvider(final LockConfiguration lockConfiguration, final Configuration conf) { + TypedProperties typedProperties = lockConfiguration.getConfig(); + maxWaitTimeMillis = (typedProperties.containsKey(LockConfiguration.LOCK_ACQUIRE_WAIT_TIMEOUT_MS_PROP_KEY) + ? lockConfiguration.getConfig().getLong(LockConfiguration.LOCK_ACQUIRE_WAIT_TIMEOUT_MS_PROP_KEY) : 0); + } + + @Override + public void lock() { + LOG.info(getLogMessage(LockState.ACQUIRING)); + if (LOCK.isWriteLockedByCurrentThread()) { + throw new HoodieLockException(getLogMessage(LockState.ALREADY_ACQUIRED)); + } + LOCK.writeLock().lock(); + LOG.info(getLogMessage(LockState.ACQUIRED)); + } + + @Override + public boolean tryLock() { + LOG.info(getLogMessage(LockState.ACQUIRING)); + if (LOCK.writeLock().isHeldByCurrentThread()) { + throw new HoodieLockException(getLogMessage(LockState.ALREADY_ACQUIRED)); + } + final boolean isLockAcquired; + try { + isLockAcquired = LOCK.writeLock().tryLock(maxWaitTimeMillis, TimeUnit.MILLISECONDS); + } catch (InterruptedException e) { + throw new HoodieLockException(getLogMessage(LockState.FAILED_TO_ACQUIRE)); + } + LOG.info(getLogMessage(isLockAcquired ? LockState.ACQUIRED : LockState.FAILED_TO_ACQUIRE)); + return isLockAcquired; + } + + @Override + public boolean tryLock(long time, @NotNull TimeUnit unit) { + LOG.info(getLogMessage(LockState.ACQUIRING)); Review comment: can we re-use code across this method and the other tryLock() (L68 to L80) ########## File path: hudi-client/hudi-client-common/src/main/java/org/apache/hudi/client/transaction/lock/LocalProcessLockProvider.java ########## @@ -0,0 +1,127 @@ +/* + * 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.hudi.common.config.LockConfiguration; +import org.apache.hudi.common.config.TypedProperties; +import org.apache.hudi.common.lock.LockProvider; +import org.apache.hudi.common.lock.LockState; +import org.apache.hudi.common.util.StringUtils; +import org.apache.hudi.exception.HoodieLockException; +import org.apache.log4j.LogManager; +import org.apache.log4j.Logger; +import org.jetbrains.annotations.NotNull; + +import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +/** + * Local process level lock. This {@link LockProvider} implementation is to + * guard table from concurrent operations happening in the local JVM process. + * <p> + * Note: This Lock provider implementation doesn't allow lock reentrancy. + * Attempting to reacquire the lock from the same thread will throw + * HoodieLockException. Threads other than the current lock owner, will + * block on lock() and return false on tryLock(). + */ +public class LocalProcessLockProvider implements LockProvider<ReentrantReadWriteLock> { + + private static final Logger LOG = LogManager.getLogger(ZookeeperBasedLockProvider.class); + private static final ReentrantReadWriteLock LOCK = new ReentrantReadWriteLock(); Review comment: may I know why do we need a reentrant lock? I was thinking for our use-case, we don't need a reentrant lock. can you help me understand. ########## File path: hudi-client/hudi-client-common/src/test/java/org/apache/hudi/client/transaction/TestLocalProcessLockProvider.java ########## @@ -0,0 +1,185 @@ +/* + * 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; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hudi.client.transaction.lock.LocalProcessLockProvider; +import org.apache.hudi.common.config.LockConfiguration; +import org.apache.hudi.common.config.TypedProperties; +import org.apache.hudi.exception.HoodieLockException; +import org.apache.log4j.LogManager; +import org.apache.log4j.Logger; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.concurrent.TimeUnit; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class TestLocalProcessLockProvider { + + private static final Logger LOG = LogManager.getLogger(TestLocalProcessLockProvider.class); + private final Configuration hadoopConfiguration = new Configuration(); + private final LockConfiguration lockConfiguration = new LockConfiguration(new TypedProperties()); + + @Test + public void testLockAcquisition() { + LocalProcessLockProvider localProcessLockProvider = new LocalProcessLockProvider(lockConfiguration, hadoopConfiguration); + assertDoesNotThrow(() -> { + localProcessLockProvider.lock(); + }); + assertDoesNotThrow(() -> { + localProcessLockProvider.unlock(); + }); + } + + @Test + public void testLockReAcquisitionBySameThread() { + LocalProcessLockProvider localProcessLockProvider = new LocalProcessLockProvider(lockConfiguration, hadoopConfiguration); + assertDoesNotThrow(() -> { + localProcessLockProvider.lock(); + }); + assertThrows(HoodieLockException.class, () -> { + localProcessLockProvider.lock(); + }); + assertDoesNotThrow(() -> { + localProcessLockProvider.unlock(); + }); + } + + @Test + public void testLockReAcquisitionByDifferentThread() { + LocalProcessLockProvider localProcessLockProvider = new LocalProcessLockProvider(lockConfiguration, hadoopConfiguration); + + // Main test thread + assertDoesNotThrow(() -> { + localProcessLockProvider.lock(); + }); + + // Another writer thread + Thread writer2 = new Thread(new Runnable() { + @Override + public void run() { + assertThrows(HoodieLockException.class, () -> { + localProcessLockProvider.lock(); + }); + } + }); + + try { + writer2.join(); + } catch (InterruptedException e) { + // + } + + assertDoesNotThrow(() -> { + localProcessLockProvider.unlock(); + }); + } + + @Test + public void testTryLockAcquisition() { + LocalProcessLockProvider localProcessLockProvider = new LocalProcessLockProvider(lockConfiguration, hadoopConfiguration); + Assertions.assertTrue(localProcessLockProvider.tryLock()); + assertDoesNotThrow(() -> { + localProcessLockProvider.unlock(); + }); + } + + @Test + public void testTryLockAcquisitionWithTimeout() { + LocalProcessLockProvider localProcessLockProvider = new LocalProcessLockProvider(lockConfiguration, hadoopConfiguration); + Assertions.assertTrue(localProcessLockProvider.tryLock(1, TimeUnit.MILLISECONDS)); + assertDoesNotThrow(() -> { + localProcessLockProvider.unlock(); + }); + } + + @Test + public void testTryLockReAcquisitionBySameThread() { Review comment: can we add a test where in, write1 acquires the lock. write2 try to acquire with 1 sec(or 5 secs). and within 1sec, writer1 releases the lock and writer2 was able to successfully acquire the lock. ########## File path: hudi-common/src/main/java/org/apache/hudi/common/config/HoodieMetadataConfig.java ########## @@ -44,7 +43,7 @@ // Enable the internal Metadata Table which saves file listings public static final ConfigProperty<Boolean> ENABLE = ConfigProperty .key(METADATA_PREFIX + ".enable") - .defaultValue(false) + .defaultValue(true) Review comment: Can you make this a separate PR. logically does not go with this patch. ########## File path: hudi-client/hudi-client-common/src/main/java/org/apache/hudi/client/transaction/lock/LocalProcessLockProvider.java ########## @@ -0,0 +1,127 @@ +/* + * 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.hudi.common.config.LockConfiguration; +import org.apache.hudi.common.config.TypedProperties; +import org.apache.hudi.common.lock.LockProvider; +import org.apache.hudi.common.lock.LockState; +import org.apache.hudi.common.util.StringUtils; +import org.apache.hudi.exception.HoodieLockException; +import org.apache.log4j.LogManager; +import org.apache.log4j.Logger; +import org.jetbrains.annotations.NotNull; + +import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +/** + * Local process level lock. This {@link LockProvider} implementation is to + * guard table from concurrent operations happening in the local JVM process. + * <p> + * Note: This Lock provider implementation doesn't allow lock reentrancy. + * Attempting to reacquire the lock from the same thread will throw + * HoodieLockException. Threads other than the current lock owner, will + * block on lock() and return false on tryLock(). + */ +public class LocalProcessLockProvider implements LockProvider<ReentrantReadWriteLock> { + + private static final Logger LOG = LogManager.getLogger(ZookeeperBasedLockProvider.class); + private static final ReentrantReadWriteLock LOCK = new ReentrantReadWriteLock(); + private final long maxWaitTimeMillis; + + public LocalProcessLockProvider(final LockConfiguration lockConfiguration, final Configuration conf) { + TypedProperties typedProperties = lockConfiguration.getConfig(); + maxWaitTimeMillis = (typedProperties.containsKey(LockConfiguration.LOCK_ACQUIRE_WAIT_TIMEOUT_MS_PROP_KEY) + ? lockConfiguration.getConfig().getLong(LockConfiguration.LOCK_ACQUIRE_WAIT_TIMEOUT_MS_PROP_KEY) : 0); + } + + @Override + public void lock() { + LOG.info(getLogMessage(LockState.ACQUIRING)); + if (LOCK.isWriteLockedByCurrentThread()) { + throw new HoodieLockException(getLogMessage(LockState.ALREADY_ACQUIRED)); + } + LOCK.writeLock().lock(); + LOG.info(getLogMessage(LockState.ACQUIRED)); + } + + @Override + public boolean tryLock() { + LOG.info(getLogMessage(LockState.ACQUIRING)); + if (LOCK.writeLock().isHeldByCurrentThread()) { + throw new HoodieLockException(getLogMessage(LockState.ALREADY_ACQUIRED)); + } + final boolean isLockAcquired; + try { + isLockAcquired = LOCK.writeLock().tryLock(maxWaitTimeMillis, TimeUnit.MILLISECONDS); Review comment: synced up directly. I am good here. ########## File path: hudi-client/hudi-client-common/src/main/java/org/apache/hudi/client/transaction/lock/LocalProcessLockProvider.java ########## @@ -0,0 +1,127 @@ +/* + * 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.hudi.common.config.LockConfiguration; +import org.apache.hudi.common.config.TypedProperties; +import org.apache.hudi.common.lock.LockProvider; +import org.apache.hudi.common.lock.LockState; +import org.apache.hudi.common.util.StringUtils; +import org.apache.hudi.exception.HoodieLockException; +import org.apache.log4j.LogManager; +import org.apache.log4j.Logger; +import org.jetbrains.annotations.NotNull; + +import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.ReentrantReadWriteLock; + +/** + * Local process level lock. This {@link LockProvider} implementation is to + * guard table from concurrent operations happening in the local JVM process. + * <p> + * Note: This Lock provider implementation doesn't allow lock reentrancy. + * Attempting to reacquire the lock from the same thread will throw + * HoodieLockException. Threads other than the current lock owner, will + * block on lock() and return false on tryLock(). + */ +public class LocalProcessLockProvider implements LockProvider<ReentrantReadWriteLock> { + + private static final Logger LOG = LogManager.getLogger(ZookeeperBasedLockProvider.class); + private static final ReentrantReadWriteLock LOCK = new ReentrantReadWriteLock(); Review comment: synced up directly. I am good here. -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
