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 f174782124 [rest] Add explicit commit lease release (#11146)
f174782124 is described below
commit f1747821245bb1e844a28e5afc1dd4f38410f5df
Author: YeJunHao <[email protected]>
AuthorDate: Mon Nov 16 14:23:49 2026 +0800
[rest] Add explicit commit lease release (#11146)
---
docs/static/rest-catalog-open-api.yaml | 49 ++++++++-
.../main/java/org/apache/paimon/rest/RESTApi.java | 8 ++
.../apache/paimon/rest/auth/DLFOpenApiActions.java | 5 +
.../paimon/rest/requests/CommitLockRequest.java | 2 +-
.../paimon/rest/auth/DLFOpenApiActionsTest.java | 3 +-
.../org/apache/paimon/rest/RESTCatalogLock.java | 43 ++++++--
.../apache/paimon/rest/CommitLockProtocolTest.java | 44 ++++++++-
.../apache/paimon/rest/RESTCatalogLockTest.java | 109 +++++++++++++++++++++
.../paimon/rest/RESTCommitLockIntegrationTest.java | 21 +++-
.../pypaimon/api/auth/dlf_openapi_actions.py | 2 +
.../pypaimon/tests/rest/dlf_signer_test.py | 2 +-
11 files changed, 274 insertions(+), 14 deletions(-)
diff --git a/docs/static/rest-catalog-open-api.yaml
b/docs/static/rest-catalog-open-api.yaml
index b33566a5e3..40d97b3c1c 100644
--- a/docs/static/rest-catalog-open-api.yaml
+++ b/docs/static/rest-catalog-open-api.yaml
@@ -760,7 +760,7 @@ paths:
Renewal never revives an expired lease. Publication retains snapshot
CAS validation
and atomically releases the lease on a new successful commit. A
rejected publication
does not release the lease. A replay of an already-published snapshot
must not release
- a newer lease. The lease duration is fixed for each scope. There is no
unlock RPC.
+ a newer lease. The lease duration is fixed for each scope.
requestBody:
required: true
content:
@@ -835,6 +835,53 @@ paths:
$ref: '#/components/responses/TableNotExistErrorResponse'
"500":
$ref: '#/components/responses/ServerErrorResponse'
+ /v1/{prefix}/databases/{database}/tables/{table}/commit-lock/release:
+ post:
+ tags:
+ - table
+ summary: Release a table commit lease
+ operationId: releaseCommitLock
+ parameters:
+ - name: prefix
+ in: path
+ required: true
+ schema:
+ type: string
+ - name: database
+ in: path
+ required: true
+ schema:
+ type: string
+ - name: table
+ in: path
+ required: true
+ schema:
+ type: string
+ description: >-
+ Requires the same permission as committing snapshots. Validate the
table incarnation
+ and branch and match the exact commitUser against the unexpired lease.
If there is no
+ active lease for this commitUser, return success without changing the
lease. Clients
+ attempt release after both successful and failed protected operations.
Expiry remains the
+ fallback for process termination, network partitions and failed
cleanup requests.
+ requestBody:
+ required: true
+ content:
+ application/json:
+ schema:
+ $ref: '#/components/schemas/CommitLockRequest'
+ responses:
+ "200":
+ description: Released or no active lease for this commitUser.
+ "400":
+ $ref: '#/components/responses/BadRequestErrorResponse'
+ "401":
+ $ref: '#/components/responses/UnauthorizedErrorResponse'
+ "403":
+ $ref: '#/components/responses/ForbiddenErrorResponse'
+ "404":
+ $ref: '#/components/responses/TableNotExistErrorResponse'
+ "500":
+ $ref: '#/components/responses/ServerErrorResponse'
/v1/{prefix}/databases/{database}/tables/{table}/rollback:
post:
tags:
diff --git a/paimon-api/src/main/java/org/apache/paimon/rest/RESTApi.java
b/paimon-api/src/main/java/org/apache/paimon/rest/RESTApi.java
index 77b3cf060b..d017d15448 100644
--- a/paimon-api/src/main/java/org/apache/paimon/rest/RESTApi.java
+++ b/paimon-api/src/main/java/org/apache/paimon/rest/RESTApi.java
@@ -762,6 +762,14 @@ public class RESTApi {
restAuthFunction);
}
+ public void releaseCommitLock(Identifier identifier, String tableUuid,
String commitUser) {
+ client.post(
+ resourcePaths.commitLock(identifier.getDatabaseName(),
identifier.getObjectName())
+ + "/release",
+ new CommitLockRequest(tableUuid, commitUser),
+ restAuthFunction);
+ }
+
/**
* Rollback instant for table.
*
diff --git
a/paimon-api/src/main/java/org/apache/paimon/rest/auth/DLFOpenApiActions.java
b/paimon-api/src/main/java/org/apache/paimon/rest/auth/DLFOpenApiActions.java
index 276e141b48..109dcbd5b2 100644
---
a/paimon-api/src/main/java/org/apache/paimon/rest/auth/DLFOpenApiActions.java
+++
b/paimon-api/src/main/java/org/apache/paimon/rest/auth/DLFOpenApiActions.java
@@ -62,6 +62,11 @@ final class DLFOpenApiActions {
"/v1/{prefix}/databases/{database}/tables/{table}/commit-lock/renew",
"RenewCommitLock"
},
+ {
+ "POST",
+
"/v1/{prefix}/databases/{database}/tables/{table}/commit-lock/release",
+ "ReleaseCommitLock"
+ },
{"POST", "/v1/{prefix}/databases/{database}/tables/{table}/replace",
"ReplaceTable"},
{"GET", "/v1/{prefix}/databases/{database}/tables/{table}/token",
"GetTableToken"},
{"POST", "/v1/{prefix}/databases/{database}/tables/{table}/rollback",
"RollbackToSnapshot"},
diff --git
a/paimon-api/src/main/java/org/apache/paimon/rest/requests/CommitLockRequest.java
b/paimon-api/src/main/java/org/apache/paimon/rest/requests/CommitLockRequest.java
index 9a12f8272c..c890c67030 100644
---
a/paimon-api/src/main/java/org/apache/paimon/rest/requests/CommitLockRequest.java
+++
b/paimon-api/src/main/java/org/apache/paimon/rest/requests/CommitLockRequest.java
@@ -26,7 +26,7 @@ import
org.apache.paimon.shade.jackson2.com.fasterxml.jackson.annotation.JsonPro
import java.beans.ConstructorProperties;
-/** Request to acquire or renew a table commit lease owned by a writer. */
+/** Request to acquire, renew or release a table commit lease owned by a
writer. */
@JsonIgnoreProperties(ignoreUnknown = true)
public class CommitLockRequest implements RESTRequest {
diff --git
a/paimon-api/src/test/java/org/apache/paimon/rest/auth/DLFOpenApiActionsTest.java
b/paimon-api/src/test/java/org/apache/paimon/rest/auth/DLFOpenApiActionsTest.java
index b73ea82eb3..9e4a4f1c36 100644
---
a/paimon-api/src/test/java/org/apache/paimon/rest/auth/DLFOpenApiActionsTest.java
+++
b/paimon-api/src/test/java/org/apache/paimon/rest/auth/DLFOpenApiActionsTest.java
@@ -49,7 +49,7 @@ public class DLFOpenApiActionsTest {
assertEquals(operation[2], DLFOpenApiActions.resolve(operation[0],
path), path);
actions.add(operation[2]);
}
- assertEquals(78, actions.size());
+ assertEquals(79, actions.size());
}
/** The table and docs/static/rest-*-open-api.yaml must list the same
operations. */
@@ -102,6 +102,7 @@ public class DLFOpenApiActionsTest {
assertAction("CommitTable", "POST", paths.commitTable("db", "t"));
assertAction("AcquireCommitLock", "POST", paths.commitLock("db", "t"));
assertAction("RenewCommitLock", "POST", paths.commitLock("db", "t") +
"/renew");
+ assertAction("ReleaseCommitLock", "POST", paths.commitLock("db", "t")
+ "/release");
assertAction("ReplaceTable", "POST", paths.replaceTable("db", "t"));
assertAction("GetTableToken", "GET", paths.tableToken("db", "t"));
assertAction("RollbackToSnapshot", "POST", paths.rollbackTable("db",
"t"));
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 b243d96dbf..dff9308221 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
@@ -119,10 +119,22 @@ public class RESTCatalogLock implements Lock {
long startedAt = nanoTime.getAsLong();
while (true) {
long requestedAt = nanoTime.getAsLong();
- CommitLockResponse response = acquireLease();
+ CommitLockResponse response;
+ try {
+ response = acquireLease();
+ } catch (Exception | Error e) {
+ Lock.release(this::releaseLease, e);
+ throw e;
+ }
if (response.isAcquired()) {
- validateGrant(response);
- Lease lease = new Lease(response.getLeaseMillis(),
requestedAt);
+ Lease lease;
+ try {
+ validateGrant(response);
+ lease = new Lease(response.getLeaseMillis(), requestedAt);
+ } catch (Exception | Error e) {
+ Lock.release(this::releaseLease, e);
+ throw e;
+ }
try {
lease.checkValid();
currentLease.set(lease);
@@ -158,6 +170,17 @@ public class RESTCatalogLock implements Lock {
}
}
+ private void releaseLease() {
+ boolean interrupted = Thread.interrupted();
+ try {
+ api.releaseCommitLock(identifier, tableUuid, commitUser);
+ } finally {
+ if (interrupted) {
+ Thread.currentThread().interrupt();
+ }
+ }
+ }
+
private void validateGrant(CommitLockResponse response) {
if (!commitUser.equals(response.getCommitUser()) ||
response.getLeaseMillis() <= 0) {
throw new IllegalStateException("The server returned an invalid
commit lease.");
@@ -281,12 +304,16 @@ public class RESTCatalogLock implements Lock {
public void close() {
if (closed.compareAndSet(false, true)) {
try {
- renewal.cancel(false);
- } finally {
- FutureTask<Void> request = renewalRequest;
- if (request != null) {
- request.cancel(true);
+ try {
+ renewal.cancel(false);
+ } finally {
+ FutureTask<Void> request = renewalRequest;
+ if (request != null) {
+ request.cancel(true);
+ }
}
+ } finally {
+ releaseLease();
}
}
}
diff --git
a/paimon-core/src/test/java/org/apache/paimon/rest/CommitLockProtocolTest.java
b/paimon-core/src/test/java/org/apache/paimon/rest/CommitLockProtocolTest.java
index b14d7187c2..efdfafddb0 100644
---
a/paimon-core/src/test/java/org/apache/paimon/rest/CommitLockProtocolTest.java
+++
b/paimon-core/src/test/java/org/apache/paimon/rest/CommitLockProtocolTest.java
@@ -58,8 +58,43 @@ import java.util.concurrent.TimeUnit;
import static org.assertj.core.api.Assertions.assertThat;
-/** Tests the owner lease wire protocol without changing legacy snapshot
requests. */
+/** Tests the owner lease and snapshot wire protocols. */
class CommitLockProtocolTest {
+ @Test
+ void acquireRenewAndReleaseCarryTheSameOwner() throws Exception {
+ try (MockWebServer server = new MockWebServer()) {
+ server.start();
+ Options options = new Options();
+ options.set(RESTCatalogOptions.URI, server.url("/").toString());
+ options.set(RESTCatalogOptions.TOKEN_PROVIDER, "bear");
+ options.set(RESTCatalogOptions.TOKEN, "token");
+ options.set(RESTCatalogInternalOptions.PREFIX, "catalog");
+ RESTApi api = new RESTApi(options, false);
+ Identifier identifier = new Identifier("db", "table", "dev");
+ CommitLockResponse grant =
+ new CommitLockResponse(true, "morax-job", 100000, 60000,
null);
+ server.enqueue(json(RESTApi.toJson(grant)));
+ api.acquireCommitLock(identifier, "table-id", "morax-job");
+ server.enqueue(json(RESTApi.toJson(grant)));
+ api.renewCommitLock(identifier, "table-id", "morax-job");
+ server.enqueue(new MockResponse().setResponseCode(200));
+ api.releaseCommitLock(identifier, "table-id", "morax-job");
+ ObjectNode expectedBody =
+ RESTApi.fromJson(
+
"{\"tableId\":\"table-id\",\"commitUser\":\"morax-job\"}",
+ ObjectNode.class);
+ for (String suffix :
+ new String[] {"/commit-lock", "/commit-lock/renew",
"/commit-lock/release"}) {
+ RecordedRequest recorded = server.takeRequest(10,
TimeUnit.SECONDS);
+ assertThat(recorded.getMethod()).isEqualTo("POST");
+ assertThat(recorded.getPath()).endsWith(suffix);
+
assertThat(recorded.getRequestUrl().pathSegments()).contains("table$branch_dev");
+ assertThat(RESTApi.fromJson(recorded.getBody().readUtf8(),
ObjectNode.class))
+ .isEqualTo(expectedBody);
+ }
+ }
+ }
+
@Test
void acquireRenewAndCommitUseTheSameOwnerWithoutToken() throws Exception {
try (MockWebServer server = new MockWebServer()) {
@@ -207,6 +242,7 @@ class CommitLockProtocolTest {
RESTApi.toJson(
new CommitLockResponse(
true, "morax-job", 100000, 60000,
snapshot(1)))));
+ server.enqueue(new MockResponse().setResponseCode(200));
Options tableOptions = Options.fromMap(table.options());
tableOptions.set(CoreOptions.REST_COMMIT_LOCK_ENABLED, true);
tableOptions.set(CoreOptions.REST_COMMIT_RETRY_ACQUIRE_LOCK, true);
@@ -228,6 +264,12 @@ class CommitLockProtocolTest {
RESTApi.fromJson(acquire.getBody().readUtf8(),
CommitLockRequest.class);
assertThat(request.getTableId()).isEqualTo("table-id");
assertThat(request.getCommitUser()).isEqualTo("morax-job");
+ RecordedRequest release = server.takeRequest(10,
TimeUnit.SECONDS);
+ assertThat(release.getPath()).endsWith("/commit-lock/release");
+ CommitLockRequest releaseRequest =
+ RESTApi.fromJson(release.getBody().readUtf8(),
CommitLockRequest.class);
+
assertThat(releaseRequest.getTableId()).isEqualTo(request.getTableId());
+
assertThat(releaseRequest.getCommitUser()).isEqualTo(request.getCommitUser());
}
}
}
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 b1104cf103..08aa19474f 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
@@ -792,8 +792,10 @@ class RESTCatalogLockTest {
try (AutoCloseable second = lock.acquire()) {
first.close();
lock.ensureValid();
+ verify(api).releaseCommitLock(identifier, "table-id", "morax-job");
}
verify(api, times(2)).acquireCommitLock(identifier, "table-id",
"morax-job");
+ verify(api, times(2)).releaseCommitLock(identifier, "table-id",
"morax-job");
verify(future, times(2)).cancel(false);
}
@@ -832,6 +834,113 @@ class RESTCatalogLockTest {
return lock.runWithLock(callable);
}
+ @Test
+ void emptyOperationReleasesWithoutPublishingASnapshot() throws Exception {
+ grant("morax-job", 60000);
+ Object result = run(() -> null);
+ assertThat(result).isNull();
+ verify(api).acquireCommitLock(identifier, "table-id", "morax-job");
+ verify(api).releaseCommitLock(identifier, "table-id", "morax-job");
+ }
+
+ @ParameterizedTest
+ @ValueSource(booleans = {false, true})
+ void everyCompletedScopeReleasesTheRemoteLease(boolean failOperation)
throws Exception {
+ grant("morax-job", 60000);
+ RuntimeException failure = new RuntimeException("Compaction
preparation failed");
+ if (failOperation) {
+ assertThatThrownBy(
+ () ->
+ run(
+ () -> {
+ throw failure;
+ }))
+ .isSameAs(failure);
+ } else {
+ assertThat(run(() -> "published")).isEqualTo("published");
+ }
+ verify(api).acquireCommitLock(identifier, "table-id", "morax-job");
+ verify(api).releaseCommitLock(identifier, "table-id", "morax-job");
+ }
+
+ @Test
+ void operationErrorStillReleasesRemoteLease() {
+ grant("morax-job", 60000);
+ AssertionError failure = new AssertionError("Preparation error");
+ assertThatThrownBy(
+ () ->
+ run(
+ () -> {
+ throw failure;
+ }))
+ .isSameAs(failure);
+ verify(api).releaseCommitLock(identifier, "table-id", "morax-job");
+ }
+
+ @ParameterizedTest
+ @ValueSource(booleans = {false, true})
+ void remoteReleaseFailurePreservesOperationOutcome(boolean failOperation)
throws Exception {
+ grant("morax-job", 60000);
+ RuntimeException releaseFailure = new RuntimeException("Release
unavailable");
+ RuntimeException operationFailure = new RuntimeException("Preparation
failed");
+ doThrow(releaseFailure).when(api).releaseCommitLock(identifier,
"table-id", "morax-job");
+ if (failOperation) {
+ assertThatThrownBy(
+ () ->
+ run(
+ () -> {
+ throw operationFailure;
+ }))
+ .isSameAs(operationFailure)
+ .satisfies(
+ error ->
+ assertThat(error.getSuppressed())
+ .containsExactly(releaseFailure));
+ } else {
+ assertThat(run(() -> "published")).isEqualTo("published");
+ }
+ }
+
+ @Test
+ void schedulerRejectionAfterGrantStillReleasesRemoteLease() {
+ grant("morax-job", 60000);
+ RejectedExecutionException failure = new
RejectedExecutionException("Scheduler stopped");
+ when(renewer.scheduleWithFixedDelay(any(Runnable.class), anyLong(),
anyLong(), any()))
+ .thenThrow(failure);
+ assertThatThrownBy(() -> run(() -> "unused")).hasCause(failure);
+ verify(api).releaseCommitLock(identifier, "table-id", "morax-job");
+ }
+
+ @Test
+ void uncertainAcquisitionStillAttemptsRemoteRelease() {
+ RuntimeException failure = new RuntimeException("Grant response lost");
+ when(api.acquireCommitLock(identifier, "table-id",
"morax-job")).thenThrow(failure);
+ assertThatThrownBy(() -> run(() -> "unused")).hasCause(failure);
+ verify(api).releaseCommitLock(identifier, "table-id", "morax-job");
+ verifyNoInteractions(renewer);
+ }
+
+ @Test
+ void interruptedOwnerAttemptsReleaseAndRetainsInterruptStatus() throws
Exception {
+ grant("morax-job", 60000);
+ doAnswer(
+ invocation -> {
+
assertThat(Thread.currentThread().isInterrupted()).isFalse();
+ return null;
+ })
+ .when(api)
+ .releaseCommitLock(identifier, "table-id", "morax-job");
+ AutoCloseable scope = lock.acquire();
+ Thread.currentThread().interrupt();
+ try {
+ scope.close();
+ assertThat(Thread.currentThread().isInterrupted()).isTrue();
+ verify(api).releaseCommitLock(identifier, "table-id", "morax-job");
+ } finally {
+ Thread.interrupted();
+ }
+ }
+
private void grant(String owner, long leaseMillis) {
doAnswer(
invocation -> {
diff --git
a/paimon-core/src/test/java/org/apache/paimon/rest/RESTCommitLockIntegrationTest.java
b/paimon-core/src/test/java/org/apache/paimon/rest/RESTCommitLockIntegrationTest.java
index 5857c1a8fc..aadf2ca2ba 100644
---
a/paimon-core/src/test/java/org/apache/paimon/rest/RESTCommitLockIntegrationTest.java
+++
b/paimon-core/src/test/java/org/apache/paimon/rest/RESTCommitLockIntegrationTest.java
@@ -187,6 +187,7 @@ class RESTCommitLockIntegrationTest {
.contains("table$branch_dev");
if (path.endsWith("/commit-lock")) {
assertThat(locking).isTrue();
+ assertThat(lease.get()).isFalse();
CommitLockRequest acquire =
fromJson(request,
CommitLockRequest.class);
assertThat(acquire.getTableId()).isEqualTo("table-id");
@@ -237,6 +238,16 @@ class RESTCommitLockIntegrationTest {
if ("GET".equals(request.getMethod()) &&
!path.contains("/schemas/")) {
return json(tableResponse);
}
+ if (path.endsWith("/commit-lock/release")) {
+ assertThat(locking).isTrue();
+ CommitLockRequest release =
+ fromJson(request,
CommitLockRequest.class);
+
assertThat(release.getTableId()).isEqualTo("table-id");
+
assertThat(release.getCommitUser()).isEqualTo("morax-job");
+ events.add("release");
+ lease.set(false);
+ return new MockResponse().setResponseCode(200);
+ }
return new MockResponse().setResponseCode(400);
}
});
@@ -518,6 +529,10 @@ class RESTCommitLockIntegrationTest {
if ("GET".equals(request.getMethod()) &&
!path.contains("/schemas/")) {
return json(tableResponse);
}
+ if (path.endsWith("/commit-lock/release")) {
+ owner.set(null);
+ return new MockResponse().setResponseCode(200);
+ }
return new MockResponse().setResponseCode(400);
}
});
@@ -767,11 +782,15 @@ class RESTCommitLockIntegrationTest {
private static List<String> expected(boolean locking, boolean retry) {
List<String> events = new ArrayList<>();
for (int attempt = 0; attempt < (retry ? 2 : 1); attempt++) {
- if (locking && attempt > 0) {
+ boolean acquiresLease = locking && attempt > 0;
+ if (acquiresLease) {
events.add("acquire");
}
events.add("head");
events.add("publish");
+ if (acquiresLease) {
+ events.add("release");
+ }
}
return events;
}
diff --git a/paimon-python/pypaimon/api/auth/dlf_openapi_actions.py
b/paimon-python/pypaimon/api/auth/dlf_openapi_actions.py
index 0268e8378b..9491c9b32d 100644
--- a/paimon-python/pypaimon/api/auth/dlf_openapi_actions.py
+++ b/paimon-python/pypaimon/api/auth/dlf_openapi_actions.py
@@ -42,6 +42,8 @@ OPERATIONS: Tuple[Tuple[str, str, str], ...] = (
("POST", "/v1/{prefix}/databases/{database}/tables/{table}/commit-lock",
"AcquireCommitLock"),
("POST",
"/v1/{prefix}/databases/{database}/tables/{table}/commit-lock/renew",
"RenewCommitLock"),
+ ("POST",
"/v1/{prefix}/databases/{database}/tables/{table}/commit-lock/release",
+ "ReleaseCommitLock"),
("POST", "/v1/{prefix}/databases/{database}/tables/{table}/replace",
"ReplaceTable"),
("GET", "/v1/{prefix}/databases/{database}/tables/{table}/token",
"GetTableToken"),
("POST", "/v1/{prefix}/databases/{database}/tables/{table}/rollback",
"RollbackToSnapshot"),
diff --git a/paimon-python/pypaimon/tests/rest/dlf_signer_test.py
b/paimon-python/pypaimon/tests/rest/dlf_signer_test.py
index fa5de9609b..fa761909ad 100644
--- a/paimon-python/pypaimon/tests/rest/dlf_signer_test.py
+++ b/paimon-python/pypaimon/tests/rest/dlf_signer_test.py
@@ -502,7 +502,7 @@ class DLFSignerTest(unittest.TestCase):
path = re.sub(r"\{[^}]+\}", "x1", template)
self.assertEqual(action, resolve_action(method, path), path)
actions.add(action)
- self.assertEqual(78, len(actions))
+ self.assertEqual(79, len(actions))
def test_openapi_v4_operations_match_java(self):
"""pypaimon registers Java's operations, in Java's order, under the
same actions."""