voonhous commented on code in PR #19371:
URL: https://github.com/apache/hudi/pull/19371#discussion_r3656255028


##########
hudi-sync/hudi-hive-sync/src/test/java/org/apache/hudi/hive/transaction/lock/TestHiveMetastoreBasedLockProviderAcquireTimeout.java:
##########
@@ -0,0 +1,75 @@
+/*
+ * 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.junit.jupiter.api.Test;
+
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyLong;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+/**
+ * Unit tests for what {@link HiveMetastoreBasedLockProvider} reports when the 
metastore does not
+ * answer a lock request in time, with a mocked {@link IMetaStoreClient} and 
no live metastore or
+ * ZooKeeper.
+ */
+class TestHiveMetastoreBasedLockProviderAcquireTimeout extends 
HiveMetastoreBasedLockProviderTestBase {
+
+  private static final long LOCK_ID = 42L;
+  private static final long ACQUIRE_TIMEOUT_MS = 200L;
+
+  @Test
+  void acquireTimeoutIsReportedAsATimeout() throws Exception {
+    IMetaStoreClient client = mock(IMetaStoreClient.class);
+    CountDownLatch metastoreAnswers = new CountDownLatch(1);
+    when(client.lock(any())).thenAnswer(invocation -> {
+      metastoreAnswers.await();
+      return acquiredLock(LOCK_ID);
+    });

Review Comment:
   The guard here does not fail pre-fix for the reason it is meant to. 
`checkLock` is left unstubbed, so on master the mock returns `null` and the 
provider dies at `lockResponse.getState()` with a bare NPE before any assertion 
runs:
   
   ```
   expected: <org.apache.hudi.exception.HoodieLockException> but was: 
<java.lang.NullPointerException>
   Caused by: NPE: Cannot invoke "LockResponse.getState()" because 
"lockResponse" is null
     at HiveMetastoreBasedLockProvider.acquireLockInternal(...:232)
   ```
   
   (reproduced in a worktree at `48fe2c6bd0a4` with this file copied in)
   
   So `assertInstanceOf(TimeoutException.class, ...)` on L67 never actually 
discriminates against the production symptom, which is 
`HoodieLockException(cause=NoSuchLockException)` -- `TxnHandler.checkLock` 
throws that for lock id 0. Only the `verify(never()).checkLock(...)` on L68 
pins real behaviour.
   
   Stub it the way a real metastore answers and the pre-fix failure becomes the 
intended one, `expected: <TimeoutException> but was: <NoSuchLockException>`. 
Verified in both directions.
   
   ```suggestion
       when(client.lock(any())).thenAnswer(invocation -> {
         metastoreAnswers.await();
         return acquiredLock(LOCK_ID);
       });
       // What a real metastore answers for the txn id 0 that the timed-out 
request carried.
       when(client.checkLock(anyLong())).thenThrow(new NoSuchLockException("No 
such lock 0"));
   ```
   
   Needs `import org.apache.hadoop.hive.metastore.api.NoSuchLockException;`, 
already imported by `TestHiveMetastoreBasedLockProviderLockLoss`.



##########
hudi-sync/hudi-hive-sync/src/test/java/org/apache/hudi/hive/transaction/lock/TestHiveMetastoreBasedLockProviderAcquireTimeout.java:
##########
@@ -0,0 +1,75 @@
+/*
+ * 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.junit.jupiter.api.Test;
+
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
+
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyLong;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+/**
+ * Unit tests for what {@link HiveMetastoreBasedLockProvider} reports when the 
metastore does not
+ * answer a lock request in time, with a mocked {@link IMetaStoreClient} and 
no live metastore or
+ * ZooKeeper.
+ */
+class TestHiveMetastoreBasedLockProviderAcquireTimeout extends 
HiveMetastoreBasedLockProviderTestBase {
+
+  private static final long LOCK_ID = 42L;
+  private static final long ACQUIRE_TIMEOUT_MS = 200L;
+
+  @Test
+  void acquireTimeoutIsReportedAsATimeout() throws Exception {
+    IMetaStoreClient client = mock(IMetaStoreClient.class);
+    CountDownLatch metastoreAnswers = new CountDownLatch(1);
+    when(client.lock(any())).thenAnswer(invocation -> {
+      metastoreAnswers.await();
+      return acquiredLock(LOCK_ID);
+    });
+
+    HiveMetastoreBasedLockProvider provider = new 
HiveMetastoreBasedLockProvider(lockConfiguration, client);
+    try {
+      HoodieLockException thrown = assertThrows(HoodieLockException.class,
+          () -> provider.tryLock(ACQUIRE_TIMEOUT_MS, TimeUnit.MILLISECONDS));
+
+      // The metastore never answered, and that is what the writer has to be 
told. Looking the lock
+      // up afterwards cannot help: the request that timed out never returned 
a lock id.
+      assertInstanceOf(TimeoutException.class, thrown.getCause());
+      verify(client, never()).checkLock(anyLong());
+      assertNull(provider.getLock());
+    } finally {
+      metastoreAnswers.countDown();

Review Comment:
   This `countDown()` unblocks the mock, so the test already builds the exact 
scenario the deleted branch was aimed at: the metastore grants the lock after 
the client gave up. Nothing asserts what becomes of it.
   
   Worth pinning, because the answer is "nothing". `acquireLockInternal`'s 
`finally` sees `lock == null` (the `Future.get` threw before the assignment) 
and so does `close()`, so no `unlock` and no `heartbeat` is ever sent for lock 
42 -- it stays held server side until `hive.txn.timeout` (default 300s) reaps 
it. That is a deliberate tradeoff of this PR and should be written down in the 
test rather than left implicit.
   
   Add a second latch so the assertion is deterministic:
   
   ```java
       CountDownLatch lockReturned = new CountDownLatch(1);
       when(client.lock(any())).thenAnswer(invocation -> {
         metastoreAnswers.await();
         lockReturned.countDown();
         return acquiredLock(LOCK_ID);
       });
       ...
       } finally {
         metastoreAnswers.countDown();
         provider.close();
       }
       // A lock granted after the client gave up is abandoned, not released: 
it stays held at the
       // metastore until hive.txn.timeout reaps it.
       assertTrue(lockReturned.await(30, TimeUnit.SECONDS));
       verify(client, never()).unlock(anyLong());
       verify(client, never()).heartbeat(anyLong(), anyLong());
   ```
   
   Verified passing on this branch.



-- 
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]

Reply via email to