Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#10602] improvemet:check update count in FilesetMetaService.updateFileset (#10613)

Victory Tuduobb419f8d092d
diff --git a/core/src/main/java/org/apache/gravitino/storage/relational/service/FilesetMetaService.java b/core/src/main/java/org/apache/gravitino/storage/relational/service/FilesetMetaService.java
index 97e7304ae8..118b9f0cbf 100644
--- a/core/src/main/java/org/apache/gravitino/storage/relational/service/FilesetMetaService.java
+++ b/core/src/main/java/org/apache/gravitino/storage/relational/service/FilesetMetaService.java
@@ -215,22 +215,38 @@ public class FilesetMetaService {
FilesetPO newFilesetPO =
POConverters.updateFilesetPOWithVersion(oldFilesetPO, newEntity, checkNeedUpdateVersion);
if (checkNeedUpdateVersion) {
- // These operations are guaranteed to be atomic by the transaction. If version info is
- // inserted successfully and the uniqueness is guaranteed by `fileset_id + version +
- // deleted_at`, it means that no other transaction has been inserted (if a uniqueness
- // conflict occurs, the transaction will be rolled back), then we can consider that the
- // fileset meta update is successful
- SessionUtils.doMultipleWithCommit(
- () ->
- SessionUtils.doWithoutCommit(
- FilesetVersionMapper.class,
- mapper -> mapper.insertFilesetVersions(newFilesetPO.getFilesetVersionPOs())),
- () ->
- SessionUtils.doWithoutCommit(
- FilesetMetaMapper.class,
- mapper -> mapper.updateFilesetMeta(newFilesetPO, oldFilesetPO)));
- // we set the updateResult to 1 to indicate that the update is successful
- updateResult = 1;
+ // These operations are performed atomically within a single transaction. The version
+ // insert is protected by a unique constraint on `fileset_id + version + deleted_at`. If
+ // the meta update affects 0 rows (concurrent modification), the transaction is rolled
+ // back — including the version insert — and the update is treated as a conflict.
+ int[] metaUpdateCountRef = new int[1];
+ try {
+ SessionUtils.doMultipleWithCommit(
+ () ->
+ SessionUtils.doWithoutCommit(
+ FilesetVersionMapper.class,
+ mapper -> mapper.insertFilesetVersions(newFilesetPO.getFilesetVersionPOs())),
+ () -> {
+ metaUpdateCountRef[0] =
+ SessionUtils.getWithoutCommit(
+ FilesetMetaMapper.class,
+ mapper -> mapper.updateFilesetMeta(newFilesetPO, oldFilesetPO));
+ if (metaUpdateCountRef[0] == 0) {
+ throw new RuntimeException("Failed to update the entity: " + identifier);
+ }
+ });
+ updateResult = 1;
+ } catch (RuntimeException re) {
+ if (metaUpdateCountRef[0] == 0) {
+ // The meta update matched no rows; the transaction was rolled back,
+ // including the version insert above.
+ throw new IOException("Failed to update the entity: " + identifier);
+ } else {
+ ExceptionUtils.checkSQLException(
+ re, Entity.EntityType.FILESET, newEntity.nameIdentifier().toString());
+ throw re;
+ }
+ }
} else {
updateResult =
SessionUtils.doWithCommitAndFetchResult(
diff --git a/core/src/test/java/org/apache/gravitino/storage/relational/service/TestFilesetMetaService.java b/core/src/test/java/org/apache/gravitino/storage/relational/service/TestFilesetMetaService.java
index c292418c1e..24faf760e5 100644
--- a/core/src/test/java/org/apache/gravitino/storage/relational/service/TestFilesetMetaService.java
+++ b/core/src/test/java/org/apache/gravitino/storage/relational/service/TestFilesetMetaService.java
@@ -376,4 +376,80 @@ public class TestFilesetMetaService extends TestJDBCBackend {
.withAuditInfo(auditInfo)
.build();
}
+
+ @TestTemplate
+ public void testUpdateFilesetReturnsSuccessWhenVersionedMetaUpdateAffectsNoRows()
+ throws IOException {
+ String filesetName = GravitinoITUtils.genRandomName("tst_fs_conflict");
+ NameIdentifier filesetIdent =
+ NameIdentifier.of(metalakeName, catalogName, schemaName, filesetName);
+ FilesetEntity filesetEntity =
+ createFilesetEntity(
+ RandomIdGenerator.INSTANCE.nextId(),
+ NamespaceUtil.ofFileset(metalakeName, catalogName, schemaName),
+ filesetName,
+ AUDIT_INFO,
+ "/tmp");
+ FilesetMetaService.getInstance().insertFileset(filesetEntity, true);
+
+ AuditInfo conflictingAuditInfo =
+ AuditInfo.builder()
+ .withCreator("conflicting-updater")
+ .withCreateTime(Instant.now())
+ .build();
+ FilesetEntity updatedFilesetEntity =
+ FilesetEntity.builder()
+ .withId(filesetEntity.id())
+ .withName(filesetEntity.name())
+ .withNamespace(filesetEntity.namespace())
+ .withFilesetType(filesetEntity.filesetType())
+ .withStorageLocations(ImmutableMap.of(LOCATION_NAME_UNKNOWN, "/tmp-v2"))
+ .withComment("comment-v2")
+ .withProperties(ImmutableMap.of("version", "2"))
+ .withAuditInfo(
+ AuditInfo.builder()
+ .withCreator("expected-updater")
+ .withCreateTime(Instant.now())
+ .build())
+ .build();
+
+ Exception exception =
+ Assertions.assertThrows(
+ IOException.class,
+ () ->
+ FilesetMetaService.getInstance()
+ .updateFileset(
+ filesetIdent,
+ e -> {
+ // Simulate an optimistic locking conflict
+ try {
+ backend.update(
+ filesetIdent,
+ Entity.EntityType.FILESET,
+ entity -> {
+ FilesetEntity cloned =
+ createFilesetEntity(
+ entity.id(),
+ entity.namespace(),
+ entity.name(),
+ conflictingAuditInfo,
+ "/tmp");
+ return cloned;
+ });
+ } catch (Exception ex) {
+ throw new RuntimeException(ex);
+ }
+ return updatedFilesetEntity;
+ }));
+ Assertions.assertTrue(
+ exception.getMessage().contains("Failed to update the entity: " + filesetIdent));
+
+ FilesetEntity persistedEntity =
+ FilesetMetaService.getInstance().getFilesetByIdentifier(filesetIdent);
+ Assertions.assertEquals(conflictingAuditInfo, persistedEntity.auditInfo());
+ Assertions.assertEquals("", persistedEntity.comment());
+ Assertions.assertNull(persistedEntity.properties());
+ Assertions.assertEquals("/tmp", persistedEntity.storageLocations().get(LOCATION_NAME_UNKNOWN));
+ Assertions.assertNotEquals(updatedFilesetEntity, persistedEntity);
+ }
}