voonhous commented on code in PR #19370: URL: https://github.com/apache/hudi/pull/19370#discussion_r3644799669
########## hudi-sync/hudi-hive-sync/src/test/java/org/apache/hudi/hive/transaction/lock/TestHiveMetastoreBasedLockProviderLockLoss.java: ########## @@ -0,0 +1,333 @@ +/* + * 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.hive.transaction.lock; + +import org.apache.hudi.exception.HoodieLockException; + +import org.apache.hadoop.hive.metastore.IMetaStoreClient; +import org.apache.hadoop.hive.metastore.api.NoSuchLockException; +import org.apache.thrift.TException; +import org.junit.jupiter.api.Test; + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.TimeUnit; +import java.util.function.BooleanSupplier; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +/** + * Unit tests for how {@link HiveMetastoreBasedLockProvider} reacts to the metastore reporting its + * lock as gone, with a mocked {@link IMetaStoreClient} and no live metastore or ZooKeeper. + */ +class TestHiveMetastoreBasedLockProviderLockLoss extends HiveMetastoreBasedLockProviderTestBase { + + private static final long LOCK_ID = 42L; + private static final long OTHER_LOCK_ID = 43L; + private static final long HEARTBEAT_INTERVAL_MS = 100L; + private static final long AWAIT_TIMEOUT_MS = 30_000L; + + @Override + protected long heartbeatIntervalMs() { + // Keep the ticks short so the scheduled heartbeat fires within the test. + return HEARTBEAT_INTERVAL_MS; + } + + @Test + void terminalHeartbeatFailureStopsRenewalAndDropsTheLock() throws Exception { + IMetaStoreClient client = mock(IMetaStoreClient.class); + when(client.lock(any())).thenReturn(acquiredLock(LOCK_ID)); + doThrow(new NoSuchLockException("lock " + LOCK_ID + " does not exist")) + .when(client).heartbeat(anyLong(), anyLong()); + + HiveMetastoreBasedLockProvider provider = new HiveMetastoreBasedLockProvider(lockConfiguration, client); + try { + assertTrue(provider.acquireLock(1000L, TimeUnit.MILLISECONDS, lockComponent)); + + // The metastore has expired the lock, so the provider must stop claiming to hold it. + awaitUntil(() -> provider.getLock() == null, "the lost lock must be dropped by the heartbeat"); + + // The heartbeat task latches the failure on its own, so the count below would stay at one + // even if the schedule were left running. Assert the cancellation itself as well. + assertTrue(heartbeatFutureOf(provider).isCancelled(), "the heartbeat schedule must be cancelled"); + + // Give the scheduler several more intervals: no further heartbeat may be attempted, since + // both the schedule and the heartbeat task itself are stopped after a terminal failure. + Thread.sleep(HEARTBEAT_INTERVAL_MS * 5); + verify(client, times(1)).heartbeat(0L, LOCK_ID); + + // The writer must learn that it no longer holds exclusivity, and the provider must not send + // a doomed unlock for a lock the metastore has already dropped. + assertThrows(HoodieLockException.class, provider::unlock); + verify(client, never()).unlock(anyLong()); + } finally { + provider.close(); + } + } + + @Test + void lockLostAfterTryLockIsReportedOnUnlock() throws Exception { + IMetaStoreClient client = mock(IMetaStoreClient.class); + when(client.lock(any())).thenReturn(acquiredLock(LOCK_ID)); + doThrow(new NoSuchLockException("lock " + LOCK_ID + " does not exist")) + .when(client).heartbeat(anyLong(), anyLong()); + + HiveMetastoreBasedLockProvider provider = new HiveMetastoreBasedLockProvider(lockConfiguration, client); + try { + // Same loss, but driven through the entry point the LockManager actually calls rather than + // the test-only acquireLock overload. + assertTrue(provider.tryLock(1000L, TimeUnit.MILLISECONDS)); + awaitUntil(() -> provider.getLock() == null, "the lost lock must be dropped by the heartbeat"); + + assertThrows(HoodieLockException.class, provider::unlock); + verify(client, never()).unlock(anyLong()); + } finally { + provider.close(); + } + } + + @Test + void closeAfterALostLockSendsNoUnlockAndShutsDownTheExecutor() throws Exception { + IMetaStoreClient client = mock(IMetaStoreClient.class); + when(client.lock(any())).thenReturn(acquiredLock(LOCK_ID)); + doThrow(new NoSuchLockException("lock " + LOCK_ID + " does not exist")) + .when(client).heartbeat(anyLong(), anyLong()); + + HiveMetastoreBasedLockProvider provider = new HiveMetastoreBasedLockProvider(lockConfiguration, client); + assertTrue(provider.acquireLock(1000L, TimeUnit.MILLISECONDS, lockComponent)); + awaitUntil(() -> provider.getLock() == null, "the lost lock must be dropped by the heartbeat"); + + provider.close(); Review Comment: Nit: this is the one test in the class where `close()` is not reached through a `finally`. If `awaitUntil` ever times out, `close()` is skipped and the provider's two non-daemon pool threads outlive the test. The assertions below still read the same after the block: ```suggestion try { assertTrue(provider.acquireLock(1000L, TimeUnit.MILLISECONDS, lockComponent)); awaitUntil(() -> provider.getLock() == null, "the lost lock must be dropped by the heartbeat"); } finally { provider.close(); } ``` ########## hudi-sync/hudi-hive-sync/src/main/java/org/apache/hudi/hive/transaction/lock/HiveMetastoreBasedLockProvider.java: ########## @@ -225,12 +234,33 @@ private void acquireLockInternal(long time, TimeUnit unit, LockComponent lockCom * takes a long time. Must be called only after {@link #lock} has been set. */ private void scheduleHeartbeat() { - Heartbeat heartbeat = new Heartbeat(hiveClient, lock.getLockid()); + Heartbeat heartbeat = new Heartbeat(hiveClient, lock.getLockid(), this::onLockLost); Review Comment: Coming back on this once more after reading the final state: let's take the one-line gate after all. The id guard removed the harmful path, but a WAITING response still gets a heartbeat scheduled at line 228 only for the `finally` to cancel it moments later -- wasted work, and the last theoretical window where `lockLostRemotely` can latch for a lock that was never held (a tick firing between the schedule and the `finally`, while the id still matches). An early return in `scheduleHeartbeat()` right after the null check closes it for good and covers both call sites: ```java if (lockResponseLocal.getState() != LockState.ACQUIRED) { return; } ``` -- 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]
