manojpec commented on a change in pull request #4363:
URL: https://github.com/apache/hudi/pull/4363#discussion_r771806164



##########
File path: 
hudi-client/hudi-client-common/src/test/java/org/apache/hudi/client/transaction/TestTransactionManager.java
##########
@@ -0,0 +1,208 @@
+/*
+ * 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.hudi.client.transaction.lock.InProcessLockProvider;
+import org.apache.hudi.common.model.HoodieFailedWritesCleaningPolicy;
+import org.apache.hudi.common.model.WriteConcurrencyMode;
+import org.apache.hudi.common.testutils.HoodieCommonTestHarness;
+import org.apache.hudi.config.HoodieCompactionConfig;
+import org.apache.hudi.config.HoodieLockConfig;
+import org.apache.hudi.config.HoodieWriteConfig;
+import org.apache.hudi.exception.HoodieLockException;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.io.IOException;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+public class TestTransactionManager extends HoodieCommonTestHarness {
+  HoodieWriteConfig writeConfig;
+  TransactionManager transactionManager;
+
+  @BeforeEach
+  private void init() throws IOException {
+    initPath();
+    initMetaClient();
+    this.writeConfig = getWriteConfig();
+    this.transactionManager = new TransactionManager(this.writeConfig, 
this.metaClient.getFs());
+  }
+
+  private HoodieWriteConfig getWriteConfig() {
+    return HoodieWriteConfig.newBuilder()
+        .withPath(basePath)
+        .withCompactionConfig(HoodieCompactionConfig.newBuilder()
+            
.withFailedWritesCleaningPolicy(HoodieFailedWritesCleaningPolicy.LAZY)
+            .build())
+        
.withWriteConcurrencyMode(WriteConcurrencyMode.OPTIMISTIC_CONCURRENCY_CONTROL)
+        .withLockConfig(HoodieLockConfig.newBuilder()
+            .withLockProvider(InProcessLockProvider.class)
+            .build())
+        .build();
+  }
+
+  @Test
+  public void testSingleWriterTransaction() {
+    transactionManager.beginTransaction();
+    transactionManager.endTransaction();
+  }
+
+  @Test
+  public void testSingleWriterNestedTransaction() {
+    transactionManager.beginTransaction();
+    assertThrows(HoodieLockException.class, () -> {
+      transactionManager.beginTransaction();
+    });
+
+    transactionManager.endTransaction();
+    assertThrows(HoodieLockException.class, () -> {
+      transactionManager.endTransaction();
+    });
+  }
+
+  @Test
+  public void testSingleWriterMultipleTransactions() {
+    for (int i = 0; i < 32; i++) {
+      transactionManager.beginTransaction();
+      transactionManager.endTransaction();
+    }
+  }
+
+  @Test
+  public void testMultiWriterTransactions() {
+    final int threadCount = 3;
+    final long awaitMaxTimeoutMs = 2000L;
+    final CountDownLatch latch = new CountDownLatch(threadCount);
+    final AtomicBoolean writer1Completed = new AtomicBoolean(false);
+    final AtomicBoolean writer2Completed = new AtomicBoolean(false);
+
+    // Let writer1 get the lock first, then wait for others
+    // to join the sync up point.
+    Thread writer1 = new Thread(() -> {
+      assertDoesNotThrow(() -> {
+        transactionManager.beginTransaction();
+      });
+      latch.countDown();
+      try {
+        latch.await(awaitMaxTimeoutMs, TimeUnit.MILLISECONDS);
+        // Following sleep is to make sure writer2 attempts
+        // to try lock and to get bocked on the lock which
+        // this thread is currently holding.
+        Thread.sleep(50);
+      } catch (InterruptedException e) {
+        //
+      }
+      assertDoesNotThrow(() -> {
+        transactionManager.endTransaction();
+      });
+      writer1Completed.set(true);
+    });
+    writer1.start();
+
+    // Writer2 will block on trying to acquire the lock
+    // and will eventually get the lock before the timeout.
+    Thread writer2 = new Thread(() -> {
+      latch.countDown();

Review comment:
       Made both the threads do countDown() and await() which will force the 
happens-before relationship between these two threads and the make the test 
deterministic. 




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