This is an automated email from the ASF dual-hosted git repository.

JingsongLi pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/paimon.git


The following commit(s) were added to refs/heads/master by this push:
     new f1ffd1369a [core] Replace firstCommit lock flag with explicit 
acquisition modes (#11177)
f1ffd1369a is described below

commit f1ffd1369ae6db2438a4c9a98fe4216566e88ccc
Author: Jingsong Lee <[email protected]>
AuthorDate: Mon Nov 16 10:29:44 2026 +0800

    [core] Replace firstCommit lock flag with explicit acquisition modes 
(#11177)
---
 .../DataEvolutionRowIdReassigner.java              |  5 ++-
 .../org/apache/paimon/catalog/SnapshotCommit.java  |  9 +++--
 .../paimon/operation/FileStoreCommitImpl.java      | 27 +++++++-------
 .../java/org/apache/paimon/operation/Lock.java     | 36 ++++++++++++-------
 .../operation/RemoveUnexistingManifests.java       |  3 +-
 .../org/apache/paimon/rest/RESTCatalogLock.java    |  4 +--
 .../apache/paimon/rest/RESTCatalogLockTest.java    | 42 ++++++++++++++++++++--
 7 files changed, 94 insertions(+), 32 deletions(-)

diff --git 
a/paimon-core/src/main/java/org/apache/paimon/append/dataevolution/DataEvolutionRowIdReassigner.java
 
b/paimon-core/src/main/java/org/apache/paimon/append/dataevolution/DataEvolutionRowIdReassigner.java
index 390e6ac560..fdef3f1f20 100644
--- 
a/paimon-core/src/main/java/org/apache/paimon/append/dataevolution/DataEvolutionRowIdReassigner.java
+++ 
b/paimon-core/src/main/java/org/apache/paimon/append/dataevolution/DataEvolutionRowIdReassigner.java
@@ -37,6 +37,7 @@ import org.apache.paimon.manifest.ManifestFileMeta;
 import org.apache.paimon.manifest.ManifestList;
 import org.apache.paimon.manifest.ProjectedManifestEntry;
 import org.apache.paimon.operation.FileStoreCommitImpl;
+import org.apache.paimon.operation.Lock.AcquireMode;
 import org.apache.paimon.options.Options;
 import org.apache.paimon.partition.PartitionPredicate;
 import org.apache.paimon.stats.SimpleStats;
@@ -376,7 +377,9 @@ public class DataEvolutionRowIdReassigner {
                                 () ->
                                         commitAssignment(
                                                 currentPlan, manifestFile, 
manifestList, commit),
-                                retryCount == 0);
+                                retryCount == 0
+                                        ? AcquireMode.OPTIMISTIC_ALLOWED
+                                        : AcquireMode.REQUIRED);
             }
             if (attempt.success) {
                 return attempt;
diff --git 
a/paimon-core/src/main/java/org/apache/paimon/catalog/SnapshotCommit.java 
b/paimon-core/src/main/java/org/apache/paimon/catalog/SnapshotCommit.java
index 6f6d4059f3..2033cf0c70 100644
--- a/paimon-core/src/main/java/org/apache/paimon/catalog/SnapshotCommit.java
+++ b/paimon-core/src/main/java/org/apache/paimon/catalog/SnapshotCommit.java
@@ -55,8 +55,13 @@ public abstract class SnapshotCommit implements Lock {
     }
 
     @Override
-    public final <T> T runWithLock(Callable<T> callable, boolean firstCommit) 
throws Exception {
-        return lock.runWithLock(callable, firstCommit);
+    public final AutoCloseable acquire(AcquireMode mode) throws Exception {
+        return lock.acquire(mode);
+    }
+
+    @Override
+    public final <T> T runWithLock(Callable<T> callable, AcquireMode mode) 
throws Exception {
+        return lock.runWithLock(callable, mode);
     }
 
     @Override
diff --git 
a/paimon-core/src/main/java/org/apache/paimon/operation/FileStoreCommitImpl.java
 
b/paimon-core/src/main/java/org/apache/paimon/operation/FileStoreCommitImpl.java
index 9cf8b882a2..b2ab968f86 100644
--- 
a/paimon-core/src/main/java/org/apache/paimon/operation/FileStoreCommitImpl.java
+++ 
b/paimon-core/src/main/java/org/apache/paimon/operation/FileStoreCommitImpl.java
@@ -43,6 +43,7 @@ import org.apache.paimon.manifest.ManifestFileMeta;
 import org.apache.paimon.manifest.ManifestList;
 import org.apache.paimon.manifest.PartitionEntry;
 import org.apache.paimon.manifest.SimpleFileEntry;
+import org.apache.paimon.operation.Lock.AcquireMode;
 import org.apache.paimon.operation.commit.CommitChanges;
 import org.apache.paimon.operation.commit.CommitChangesProvider;
 import org.apache.paimon.operation.commit.CommitCleaner;
@@ -245,13 +246,10 @@ public class FileStoreCommitImpl implements 
FileStoreCommit {
         this.commitCleaner = new CommitCleaner(manifestList, manifestFile, 
indexManifestFile);
     }
 
-    /**
-     * Run metadata preparation and publication with this commit's lock. Pass 
{@code false} when the
-     * operation requires the configured lock even on its first attempt.
-     */
-    public <T> T runWithLock(Callable<T> callable, boolean firstCommit) {
+    /** Run metadata preparation and publication with the given lock 
acquisition policy. */
+    public <T> T runWithLock(Callable<T> callable, AcquireMode mode) {
         try {
-            return snapshotCommit.runWithLock(callable, firstCommit);
+            return snapshotCommit.runWithLock(callable, mode);
         } catch (RuntimeException e) {
             throw e;
         } catch (Exception e) {
@@ -1195,7 +1193,7 @@ public class FileStoreCommitImpl implements 
FileStoreCommit {
                                     latestSnapshotSupplier.get(),
                                     detectConflicts,
                                     newStatsFileName),
-                    retryResult == null);
+                    retryResult == null ? AcquireMode.OPTIMISTIC_ALLOWED : 
AcquireMode.REQUIRED);
         } catch (Lock.AcquisitionException | RuntimeException e) {
             throw e;
         } catch (Exception e) {
@@ -1690,7 +1688,7 @@ public class FileStoreCommitImpl implements 
FileStoreCommit {
                                 indexManifest,
                                 nextRowId,
                                 properties),
-                false);
+                AcquireMode.REQUIRED);
     }
 
     private boolean replaceManifestListOnceWithoutLock(
@@ -1744,7 +1742,8 @@ public class FileStoreCommitImpl implements 
FileStoreCommit {
     }
 
     private Pair<Boolean, CommitCallback.Context> 
rollbackToAsLatestOnce(Snapshot targetSnapshot) {
-        return runWithLock(() -> 
rollbackToAsLatestOnceWithoutLock(targetSnapshot), false);
+        return runWithLock(
+                () -> rollbackToAsLatestOnceWithoutLock(targetSnapshot), 
AcquireMode.REQUIRED);
     }
 
     private Pair<Boolean, CommitCallback.Context> 
rollbackToAsLatestOnceWithoutLock(
@@ -1908,7 +1907,11 @@ public class FileStoreCommitImpl implements 
FileStoreCommit {
         int retryCount = 0;
         long startMillis = System.currentTimeMillis();
         while (true) {
-            boolean success = compactManifestOnce(retryCount == 0);
+            boolean success =
+                    compactManifestOnce(
+                            retryCount == 0
+                                    ? AcquireMode.OPTIMISTIC_ALLOWED
+                                    : AcquireMode.REQUIRED);
             if (success) {
                 break;
             }
@@ -1926,8 +1929,8 @@ public class FileStoreCommitImpl implements 
FileStoreCommit {
         }
     }
 
-    private boolean compactManifestOnce(boolean firstCommit) {
-        return runWithLock(this::compactManifestOnceWithoutLock, firstCommit);
+    private boolean compactManifestOnce(AcquireMode mode) {
+        return runWithLock(this::compactManifestOnceWithoutLock, mode);
     }
 
     private boolean compactManifestOnceWithoutLock() {
diff --git a/paimon-core/src/main/java/org/apache/paimon/operation/Lock.java 
b/paimon-core/src/main/java/org/apache/paimon/operation/Lock.java
index 623aa3eafa..68ac5f653e 100644
--- a/paimon-core/src/main/java/org/apache/paimon/operation/Lock.java
+++ b/paimon-core/src/main/java/org/apache/paimon/operation/Lock.java
@@ -31,13 +31,25 @@ import java.util.concurrent.Callable;
 @Public
 public interface Lock extends AutoCloseable {
 
+    /** Acquisition policy for the outermost operation scope. Nested 
operations reuse that scope. */
+    enum AcquireMode {
+        /** Acquire the configured lock. */
+        REQUIRED,
+
+        /**
+         * Allow the backend to use optimistic concurrency instead of 
acquiring the lock. The
+         * operation must support retrying after a publication conflict.
+         */
+        OPTIMISTIC_ALLOWED
+    }
+
     /**
      * Acquire a scope to release on the acquiring thread. Clean up partial 
acquisition on failure.
      */
     AutoCloseable acquire() throws Exception;
 
-    /** Acquire the scope for the given commit attempt. */
-    default AutoCloseable acquire(boolean firstCommit) throws Exception {
+    /** Acquire a scope using the given policy. Backends may always acquire 
the configured lock. */
+    default AutoCloseable acquire(AcquireMode mode) throws Exception {
         return acquire();
     }
 
@@ -47,14 +59,14 @@ public interface Lock extends AutoCloseable {
      * result or exception.
      */
     default <T> T runWithLock(Callable<T> callable) throws Exception {
-        return runWithLock(callable, false);
+        return runWithLock(callable, AcquireMode.REQUIRED);
     }
 
-    /** Run a commit attempt with the scope required by this lock. */
-    default <T> T runWithLock(Callable<T> callable, boolean firstCommit) 
throws Exception {
+    /** Run an operation with the given acquisition policy. */
+    default <T> T runWithLock(Callable<T> callable, AcquireMode mode) throws 
Exception {
         AutoCloseable scope;
         try {
-            scope = acquire(firstCommit);
+            scope = acquire(mode);
         } catch (Exception e) {
             if (ExceptionUtils.findThrowable(e, 
InterruptedException.class).isPresent()) {
                 Thread.currentThread().interrupt();
@@ -111,7 +123,7 @@ public interface Lock extends AutoCloseable {
         }
 
         @Override
-        public <T> T runWithLock(Callable<T> callable, boolean firstCommit) 
throws Exception {
+        public <T> T runWithLock(Callable<T> callable, AcquireMode mode) 
throws Exception {
             return callable.call();
         }
 
@@ -137,16 +149,16 @@ public interface Lock extends AutoCloseable {
 
         @Override
         public AutoCloseable acquire() throws Exception {
-            return acquire(false);
+            return acquire(AcquireMode.REQUIRED);
         }
 
         @Override
-        public AutoCloseable acquire(boolean firstCommit) throws Exception {
+        public AutoCloseable acquire(AcquireMode mode) throws Exception {
             if (held.get()) {
                 delegate.ensureValid();
                 return () -> {};
             }
-            AutoCloseable scope = delegate.acquire(firstCommit);
+            AutoCloseable scope = delegate.acquire(mode);
             try {
                 delegate.ensureValid();
                 held.set(true);
@@ -161,12 +173,12 @@ public interface Lock extends AutoCloseable {
         }
 
         @Override
-        public <T> T runWithLock(Callable<T> callable, boolean firstCommit) 
throws Exception {
+        public <T> T runWithLock(Callable<T> callable, AcquireMode mode) 
throws Exception {
             if (held.get()) {
                 delegate.ensureValid();
                 return callable.call();
             }
-            return Lock.super.runWithLock(callable, firstCommit);
+            return Lock.super.runWithLock(callable, mode);
         }
 
         @Override
diff --git 
a/paimon-core/src/main/java/org/apache/paimon/operation/RemoveUnexistingManifests.java
 
b/paimon-core/src/main/java/org/apache/paimon/operation/RemoveUnexistingManifests.java
index 659900d557..001374d638 100644
--- 
a/paimon-core/src/main/java/org/apache/paimon/operation/RemoveUnexistingManifests.java
+++ 
b/paimon-core/src/main/java/org/apache/paimon/operation/RemoveUnexistingManifests.java
@@ -26,6 +26,7 @@ import org.apache.paimon.manifest.FileKind;
 import org.apache.paimon.manifest.ManifestEntry;
 import org.apache.paimon.manifest.ManifestFileMeta;
 import org.apache.paimon.manifest.ManifestList;
+import org.apache.paimon.operation.Lock.AcquireMode;
 import org.apache.paimon.table.FileStoreTable;
 import org.apache.paimon.table.source.ScanMode;
 import org.apache.paimon.utils.FileStorePathFactory;
@@ -66,7 +67,7 @@ public class RemoveUnexistingManifests {
         try (FileStoreCommitImpl commit =
                 (FileStoreCommitImpl)
                         table.store().newCommit("Repair-table-" + 
UUID.randomUUID(), table)) {
-            return commit.runWithLock(() -> repairManifests(commit), false);
+            return commit.runWithLock(() -> repairManifests(commit), 
AcquireMode.REQUIRED);
         }
     }
 
diff --git 
a/paimon-core/src/main/java/org/apache/paimon/rest/RESTCatalogLock.java 
b/paimon-core/src/main/java/org/apache/paimon/rest/RESTCatalogLock.java
index a4c2585f15..b243d96dbf 100644
--- a/paimon-core/src/main/java/org/apache/paimon/rest/RESTCatalogLock.java
+++ b/paimon-core/src/main/java/org/apache/paimon/rest/RESTCatalogLock.java
@@ -165,7 +165,7 @@ public class RESTCatalogLock implements Lock {
     }
 
     @Override
-    public AutoCloseable acquire(boolean firstCommit) throws Exception {
+    public AutoCloseable acquire(AcquireMode mode) throws Exception {
         if (optimistic.get()) {
             return () -> {};
         }
@@ -173,7 +173,7 @@ public class RESTCatalogLock implements Lock {
             ensureValid();
             return () -> {};
         }
-        if (!firstCommit) {
+        if (mode != AcquireMode.OPTIMISTIC_ALLOWED) {
             return acquire();
         }
         optimistic.set(true);
diff --git 
a/paimon-core/src/test/java/org/apache/paimon/rest/RESTCatalogLockTest.java 
b/paimon-core/src/test/java/org/apache/paimon/rest/RESTCatalogLockTest.java
index 91856529bb..b1104cf103 100644
--- a/paimon-core/src/test/java/org/apache/paimon/rest/RESTCatalogLockTest.java
+++ b/paimon-core/src/test/java/org/apache/paimon/rest/RESTCatalogLockTest.java
@@ -19,8 +19,10 @@
 package org.apache.paimon.rest;
 
 import org.apache.paimon.catalog.Catalog;
+import org.apache.paimon.catalog.CatalogSnapshotCommit;
 import org.apache.paimon.catalog.Identifier;
 import org.apache.paimon.operation.Lock;
+import org.apache.paimon.operation.Lock.AcquireMode;
 import org.apache.paimon.rest.exceptions.ForbiddenException;
 import org.apache.paimon.rest.exceptions.NoSuchResourceException;
 import org.apache.paimon.rest.responses.CommitLockResponse;
@@ -723,11 +725,46 @@ class RESTCatalogLockTest {
         }
     }
 
+    @ParameterizedTest
+    @ValueSource(booleans = {false, true})
+    void optimisticScopeIsInheritedAndClearedBeforeRequiredAcquisition(boolean 
acquireDirectly)
+            throws Exception {
+        Lock publication =
+                new CatalogSnapshotCommit(mock(Catalog.class), identifier, 
"table-id", lock);
+        Callable<String> operation =
+                () ->
+                        publication.runWithLock(
+                                () -> {
+                                    publication.ensureValid();
+                                    return "published";
+                                });
+        if (acquireDirectly) {
+            try (AutoCloseable ignored = 
publication.acquire(AcquireMode.OPTIMISTIC_ALLOWED)) {
+                assertThat(operation.call()).isEqualTo("published");
+            }
+        } else {
+            assertThat(publication.runWithLock(operation, 
AcquireMode.OPTIMISTIC_ALLOWED))
+                    .isEqualTo("published");
+        }
+        verifyNoInteractions(api, renewer, workers);
+        assertThatThrownBy(publication::ensureValid).hasMessageContaining("No 
commit lease");
+
+        grant("morax-job", 60000);
+        assertThat(publication.runWithLock(operation)).isEqualTo("published");
+        verify(api).acquireCommitLock(identifier, "table-id", "morax-job");
+        verify(future).cancel(false);
+    }
+
     @Test
     void nestedPublicationReusesLeaseAndRechecksValidity() throws Exception {
         grant("morax-job", 60000);
         Lock publication = Lock.reentrant(lock);
-        assertThat(publication.runWithLock(() -> publication.runWithLock(() -> 
"published")))
+        assertThat(
+                        publication.runWithLock(
+                                () ->
+                                        publication.runWithLock(
+                                                () -> "published", 
AcquireMode.OPTIMISTIC_ALLOWED),
+                                AcquireMode.REQUIRED))
                 .isEqualTo("published");
         verify(api).acquireCommitLock(identifier, "table-id", "morax-job");
         verify(future).cancel(false);
@@ -739,7 +776,8 @@ class RESTCatalogLockTest {
                                             publication.runWithLock(
                                                     () -> {
                                                         throw new 
AssertionError();
-                                                    }))
+                                                    },
+                                                    
AcquireMode.OPTIMISTIC_ALLOWED))
                             .hasMessageContaining("no longer usable");
                     return null;
                 });

Reply via email to