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



##########
File path: 
hudi-client/hudi-client-common/src/main/java/org/apache/hudi/client/transaction/TransactionManager.java
##########
@@ -35,44 +35,39 @@
 public class TransactionManager implements Serializable {
 
   private static final Logger LOG = 
LogManager.getLogger(TransactionManager.class);
-
   private final LockManager lockManager;
+  private final boolean supportsOptimisticConcurrency;
   private Option<HoodieInstant> currentTxnOwnerInstant;
   private Option<HoodieInstant> lastCompletedTxnOwnerInstant;
-  private boolean supportsOptimisticConcurrency;
 
   public TransactionManager(HoodieWriteConfig config, FileSystem fs) {
     this.lockManager = new LockManager(config, fs);
     this.supportsOptimisticConcurrency = 
config.getWriteConcurrencyMode().supportsOptimisticConcurrencyControl();
   }
 
-  public synchronized void beginTransaction() {
+  public void beginTransaction() {
     if (supportsOptimisticConcurrency) {
-      LOG.info("Transaction starting without a transaction owner");

Review comment:
       Can we have the first info logging. Will help us debug issues where a 
writer is waiting to acquire a lock. 
   same for next method too. 

##########
File path: 
hudi-client/hudi-client-common/src/test/java/org/apache/hudi/client/transaction/TestTransactionManager.java
##########
@@ -0,0 +1,197 @@
+/*
+ * 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();
+      assertDoesNotThrow(() -> {
+        transactionManager.beginTransaction();
+      });
+      assertDoesNotThrow(() -> {
+        transactionManager.endTransaction();
+      });
+      writer2Completed.set(true);
+    });
+    writer2.start();
+
+    // Let writer1 and writer2 wait at the sync up
+    // point to make sure they run in parallel and
+    // one get blocked by the other.
+    latch.countDown();
+    try {
+      writer1.join();
+      writer2.join();
+    } catch (InterruptedException e) {
+      //
+    }
+
+    // Make sure both writers actually completed good
+    Assertions.assertTrue(writer1Completed.get());
+    Assertions.assertTrue(writer2Completed.get());
+  }
+
+  @Test
+  public void testMultiWriterMultipleTransactions() {
+    final int threadCount = 3;
+    final CountDownLatch latch = new CountDownLatch(threadCount);
+    final AtomicBoolean writer1Completed = new AtomicBoolean(false);
+    final AtomicBoolean writer2Completed = new AtomicBoolean(false);
+
+    Thread writer1 = new Thread(() -> {
+      latch.countDown();
+      for (int i = 0; i < 32; i++) {
+        transactionManager.beginTransaction();
+        transactionManager.endTransaction();
+      }
+      writer1Completed.set(true);
+    });
+    writer1.start();
+
+    Thread writer2 = new Thread(() -> {
+      latch.countDown();
+      for (int i = 0; i < 32; i++) {
+        transactionManager.beginTransaction();
+        transactionManager.endTransaction();
+      }
+      writer2Completed.set(true);
+    });
+    writer2.start();
+
+    // Let writer1 and writer2 wait at the sync up

Review comment:
       did you miss to add the latch.await above? I see only countdowns. 




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