vinothchandar commented on code in PR #13577:
URL: https://github.com/apache/hudi/pull/13577#discussion_r2252846602


##########
hudi-client/hudi-client-common/src/main/java/org/apache/hudi/client/transaction/TransactionManager.java:
##########
@@ -38,40 +43,131 @@ public class TransactionManager implements Serializable, 
AutoCloseable {
   protected static final Logger LOG = 
LoggerFactory.getLogger(TransactionManager.class);
   protected final LockManager lockManager;
   protected final boolean isLockRequired;
+  private final transient TimeGenerator timeGenerator;
+  protected boolean hasLock;
   protected Option<HoodieInstant> changeActionInstant = Option.empty();
   private Option<HoodieInstant> lastCompletedActionInstant = Option.empty();
 
   public TransactionManager(HoodieWriteConfig config, HoodieStorage storage) {
-    this(new LockManager(config, storage), config.isLockRequired());
+    this(new LockManager(config, storage), config);
   }
 
-  protected TransactionManager(LockManager lockManager, boolean 
isLockRequired) {
+  public TransactionManager(HoodieWriteConfig config, boolean isLockRequired, 
HoodieStorage storage) {
+    this(new LockManager(config, storage), isLockRequired, 
TimeGenerators.getTimeGenerator(config.getTimeGeneratorConfig()));
+  }
+
+  protected TransactionManager(LockManager lockManager, HoodieWriteConfig 
writeConfig) {
+    this(lockManager, writeConfig.isLockRequired(), 
TimeGenerators.getTimeGenerator(writeConfig.getTimeGeneratorConfig()));
+  }
+
+  public TransactionManager(LockManager lockManager, boolean isLockRequired, 
TimeGenerator timeGenerator) {
     this.lockManager = lockManager;
     this.isLockRequired = isLockRequired;
+    this.timeGenerator = timeGenerator;
+  }
+
+  public String generateInstantTime() {
+    if (!hasLock && isLockRequired) {
+      throw new HoodieLockException("Cannot create instant without acquiring a 
lock first.");
+    }
+    return HoodieInstantTimeGenerator.createNewInstantTime(timeGenerator, 0L);
   }
 
-  public void beginStateChange(Option<HoodieInstant> changeActionInstant,
-                               Option<HoodieInstant> 
lastCompletedActionInstant) {
+  /**
+   * Generates an instant time and executes an action that requires that 
instant time within a lock.
+   * @param instantTimeConsumingAction a function that takes the generated 
instant time and performs some action
+   * @return the result of the action
+   * @param <T> type of the result
+   */
+  public <T> T executeStateChangeWithInstant(Function<String, T> 
instantTimeConsumingAction) {
+    return executeStateChangeWithInstant(Option.empty(), Option.empty(), 
instantTimeConsumingAction);
+  }
+
+  /**
+   * Uses the provided instant if present or else generates an instant time 
and executes an action that requires that instant time within a lock.
+   * @param providedInstantTime an optional instant time provided by the 
caller. If not provided, a new instant time will be generated.
+   * @param instantTimeConsumingAction a function that takes the generated 
instant time and performs some action
+   * @return the result of the action
+   * @param <T> type of the result
+   */
+  public <T> T executeStateChangeWithInstant(Option<String> 
providedInstantTime, Function<String, T> instantTimeConsumingAction) {
+    return executeStateChangeWithInstant(providedInstantTime, Option.empty(), 
instantTimeConsumingAction);
+  }
+
+  /**
+   * Uses the provided instant if present or else generates an instant time 
and executes an action that requires that instant time within a lock.
+   * @param providedInstantTime an optional instant time provided by the 
caller. If not provided, a new instant time will be generated.
+   * @param lastCompletedActionInstant optional input representing the last 
completed instant, used for logging purposes.
+   * @param instantTimeConsumingAction a function that takes the generated 
instant time and performs some action
+   * @return the result of the action
+   * @param <T> type of the result
+   */
+  public synchronized <T> T executeStateChangeWithInstant(Option<String> 
providedInstantTime, Option<HoodieInstant> lastCompletedActionInstant, 
Function<String, T> instantTimeConsumingAction) {
+    if (isLockRequired()) {
+      acquireLock();
+    }
+    String requestedInstant = providedInstantTime.orElseGet(() -> 
HoodieInstantTimeGenerator.createNewInstantTime(timeGenerator, 0L));
+    try {
+      if (lastCompletedActionInstant.isEmpty()) {
+        LOG.info("State change starting for {}", changeActionInstant);
+      } else {
+        LOG.info("State change starting for {} with latest completed action 
instant {}", changeActionInstant, lastCompletedActionInstant.get());
+      }
+      return instantTimeConsumingAction.apply(requestedInstant);
+    } finally {
+      if (isLockRequired()) {
+        releaseLock();
+        LOG.info("State change ended for {}", requestedInstant);
+      }
+    }
+  }
+
+  public void beginStateChange() {
+    beginStateChange(Option.empty(), Option.empty());
+  }
+
+  public synchronized void beginStateChange(Option<HoodieInstant> 
changeActionInstant,
+                                            Option<HoodieInstant> 
lastCompletedActionInstant) {
     if (isLockRequired) {
       LOG.info("State change starting for {} with latest completed action 
instant {}",
           changeActionInstant, lastCompletedActionInstant);
-      lockManager.lock();
+      acquireLock();
       reset(this.changeActionInstant, changeActionInstant, 
lastCompletedActionInstant);
       LOG.info("State change started for {} with latest completed action 
instant {}",
           changeActionInstant, lastCompletedActionInstant);
     }
   }
 
-  public void endStateChange(Option<HoodieInstant> changeActionInstant) {
+  public void endStateChange() {
+    endStateChange(Option.empty());
+  }
+
+  public synchronized void endStateChange(Option<HoodieInstant> 
changeActionInstant) {
     if (isLockRequired) {
       LOG.info("State change ending for action instant {}", 
changeActionInstant);
       if (reset(changeActionInstant, Option.empty(), Option.empty())) {
-        lockManager.unlock();
+        releaseLock();
         LOG.info("State change ended for action instant {}", 
changeActionInstant);
       }
     }
   }
 
+  private void acquireLock() {
+    if (hasLock) {
+      LOG.debug("Lock already acquired, skipping lock acquisition.");

Review Comment:
   Short story:  existing test is kind of broken. but making 
`beginStateChange`/ `endStateChange` will cause a deadlock.. second writer will 
wait for the lock within beginStateChange.. and the first writer cant release 
it. 
   
   So we need to keep these methods simple.. and no shared state.



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