Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ Integer updateCatalogMeta(
@UpdateProvider(
type = CatalogMetaSQLProviderFactory.class,
method = "softDeleteCatalogMetasByCatalogId")
Integer softDeleteCatalogMetasByCatalogId(@Param("catalogId") Long catalogId);
Integer softDeleteCatalogMetasByCatalogId(
@Param("catalogId") Long catalogId, @Param("currentVersion") Long currentVersion);

@UpdateProvider(
type = CatalogMetaSQLProviderFactory.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,9 @@ public static String updateCatalogMeta(
return getProvider().updateCatalogMeta(newCatalogPO, oldCatalogPO);
}

public static String softDeleteCatalogMetasByCatalogId(@Param("catalogId") Long catalogId) {
return getProvider().softDeleteCatalogMetasByCatalogId(catalogId);
public static String softDeleteCatalogMetasByCatalogId(
@Param("catalogId") Long catalogId, @Param("currentVersion") Long currentVersion) {
return getProvider().softDeleteCatalogMetasByCatalogId(catalogId, currentVersion);
}

public static String softDeleteCatalogMetasByMetalakeId(@Param("metalakeId") Long metalakeId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ Integer updateMetalakeMeta(
@UpdateProvider(
type = MetalakeMetaSQLProviderFactory.class,
method = "softDeleteMetalakeMetaByMetalakeId")
Integer softDeleteMetalakeMetaByMetalakeId(@Param("metalakeId") Long metalakeId);
Integer softDeleteMetalakeMetaByMetalakeId(
@Param("metalakeId") Long metalakeId, @Param("currentVersion") Long currentVersion);

@DeleteProvider(
type = MetalakeMetaSQLProviderFactory.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ public static String updateMetalakeMeta(
return getProvider().updateMetalakeMeta(newMetalakePO, oldMetalakePO);
}

public static String softDeleteMetalakeMetaByMetalakeId(@Param("metalakeId") Long metalakeId) {
return getProvider().softDeleteMetalakeMetaByMetalakeId(metalakeId);
public static String softDeleteMetalakeMetaByMetalakeId(
@Param("metalakeId") Long metalakeId, @Param("currentVersion") Long currentVersion) {
return getProvider().softDeleteMetalakeMetaByMetalakeId(metalakeId, currentVersion);
}

public static String deleteMetalakeMetasByLegacyTimeline(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ Integer updateSchemaMeta(
method = "softDeleteSchemaMetasBySchemaIds")
Integer softDeleteSchemaMetasBySchemaIds(@Param("schemaIds") List<Long> schemaIds);

@UpdateProvider(
type = SchemaMetaSQLProviderFactory.class,
method = "softDeleteSchemaMetaBySchemaIdAndVersion")
Integer softDeleteSchemaMetaBySchemaIdAndVersion(
@Param("schemaId") Long schemaId, @Param("currentVersion") Long currentVersion);

@UpdateProvider(
type = SchemaMetaSQLProviderFactory.class,
method = "softDeleteSchemaMetasByMetalakeId")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ public static String softDeleteSchemaMetasBySchemaIds(@Param("schemaIds") List<L
return getProvider().softDeleteSchemaMetasBySchemaIds(schemaIds);
}

public static String softDeleteSchemaMetaBySchemaIdAndVersion(
@Param("schemaId") Long schemaId, @Param("currentVersion") Long currentVersion) {
return getProvider().softDeleteSchemaMetaBySchemaIdAndVersion(schemaId, currentVersion);
}

public static String softDeleteSchemaMetasByMetalakeId(@Param("metalakeId") Long metalakeId) {
return getProvider().softDeleteSchemaMetasByMetalakeId(metalakeId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,25 +206,18 @@ public String updateCatalogMeta(
+ " last_version = #{newCatalogMeta.lastVersion},"
+ " deleted_at = #{newCatalogMeta.deletedAt}"
+ " WHERE catalog_id = #{oldCatalogMeta.catalogId}"
+ " AND catalog_name = #{oldCatalogMeta.catalogName}"
+ " AND metalake_id = #{oldCatalogMeta.metalakeId}"
+ " AND type = #{oldCatalogMeta.type}"
+ " AND provider = #{oldCatalogMeta.provider}"
+ " AND (catalog_comment = #{oldCatalogMeta.catalogComment} "
+ " OR (catalog_comment IS NULL and #{oldCatalogMeta.catalogComment} IS NULL))"
+ " AND properties = #{oldCatalogMeta.properties}"
+ " AND audit_info = #{oldCatalogMeta.auditInfo}"
+ " AND current_version = #{oldCatalogMeta.currentVersion}"
+ " AND last_version = #{oldCatalogMeta.lastVersion}"
+ " AND deleted_at = 0";
}

public String softDeleteCatalogMetasByCatalogId(@Param("catalogId") Long catalogId) {
public String softDeleteCatalogMetasByCatalogId(
@Param("catalogId") Long catalogId, @Param("currentVersion") Long currentVersion) {
return "UPDATE "
+ TABLE_NAME
+ " SET deleted_at = (UNIX_TIMESTAMP() * 1000.0)"
+ " + EXTRACT(MICROSECOND FROM CURRENT_TIMESTAMP(3)) / 1000"
+ " WHERE catalog_id = #{catalogId} AND deleted_at = 0";
+ " WHERE catalog_id = #{catalogId}"
+ " AND current_version = #{currentVersion} AND deleted_at = 0";
}

public String softDeleteCatalogMetasByMetalakeId(@Param("metalakeId") Long metalakeId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,23 +143,18 @@ public String updateMetalakeMeta(
+ " current_version = #{newMetalakeMeta.currentVersion},"
+ " last_version = #{newMetalakeMeta.lastVersion}"
+ " WHERE metalake_id = #{oldMetalakeMeta.metalakeId}"
+ " AND metalake_name = #{oldMetalakeMeta.metalakeName}"
+ " AND (metalake_comment = #{oldMetalakeMeta.metalakeComment} "
+ " OR (metalake_comment IS NULL and #{oldMetalakeMeta.metalakeComment} IS NULL))"
+ " AND properties = #{oldMetalakeMeta.properties}"
+ " AND audit_info = #{oldMetalakeMeta.auditInfo}"
+ " AND schema_version = #{oldMetalakeMeta.schemaVersion}"
+ " AND current_version = #{oldMetalakeMeta.currentVersion}"
+ " AND last_version = #{oldMetalakeMeta.lastVersion}"
+ " AND deleted_at = 0";
}

public String softDeleteMetalakeMetaByMetalakeId(@Param("metalakeId") Long metalakeId) {
public String softDeleteMetalakeMetaByMetalakeId(
@Param("metalakeId") Long metalakeId, @Param("currentVersion") Long currentVersion) {
return "UPDATE "
+ TABLE_NAME
+ " SET deleted_at = (UNIX_TIMESTAMP() * 1000.0)"
+ " + EXTRACT(MICROSECOND FROM CURRENT_TIMESTAMP(3)) / 1000"
+ " WHERE metalake_id = #{metalakeId} AND deleted_at = 0";
+ " WHERE metalake_id = #{metalakeId}"
+ " AND current_version = #{currentVersion} AND deleted_at = 0";
}

public String deleteMetalakeMetasByLegacyTimeline(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,15 +273,7 @@ public String updateSchemaMeta(
+ " last_version = #{newSchemaMeta.lastVersion},"
+ " deleted_at = #{newSchemaMeta.deletedAt}"
+ " WHERE schema_id = #{oldSchemaMeta.schemaId}"
+ " AND schema_name = #{oldSchemaMeta.schemaName}"
+ " AND metalake_id = #{oldSchemaMeta.metalakeId}"
+ " AND catalog_id = #{oldSchemaMeta.catalogId}"
+ " AND (schema_comment = #{oldSchemaMeta.schemaComment}"
+ " OR (schema_comment IS NULL and #{oldSchemaMeta.schemaComment} IS NULL))"
+ " AND properties = #{oldSchemaMeta.properties}"
+ " AND audit_info = #{oldSchemaMeta.auditInfo}"
+ " AND current_version = #{oldSchemaMeta.currentVersion}"
+ " AND last_version = #{oldSchemaMeta.lastVersion}"
+ " AND deleted_at = 0";
}

Expand All @@ -299,6 +291,16 @@ public String softDeleteSchemaMetasBySchemaIds(@Param("schemaIds") List<Long> sc
+ "</script>";
}

public String softDeleteSchemaMetaBySchemaIdAndVersion(
@Param("schemaId") Long schemaId, @Param("currentVersion") Long currentVersion) {
return "UPDATE "
+ TABLE_NAME
+ " SET deleted_at = (UNIX_TIMESTAMP() * 1000.0)"
+ " + EXTRACT(MICROSECOND FROM CURRENT_TIMESTAMP(3)) / 1000"
+ " WHERE schema_id = #{schemaId}"
+ " AND current_version = #{currentVersion} AND deleted_at = 0";
}

public String softDeleteSchemaMetasByMetalakeId(@Param("metalakeId") Long metalakeId) {
return "UPDATE "
+ TABLE_NAME
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@

public class CatalogMetaPostgreSQLProvider extends CatalogMetaBaseSQLProvider {
@Override
public String softDeleteCatalogMetasByCatalogId(Long catalogId) {
public String softDeleteCatalogMetasByCatalogId(Long catalogId, Long currentVersion) {
return "UPDATE "
+ TABLE_NAME
+ " SET deleted_at = CAST(EXTRACT(EPOCH FROM CURRENT_TIMESTAMP) * 1000 AS BIGINT)"
+ " WHERE catalog_id = #{catalogId} AND deleted_at = 0";
+ " WHERE catalog_id = #{catalogId}"
+ " AND current_version = #{currentVersion} AND deleted_at = 0";
}

@Override
Expand Down Expand Up @@ -101,17 +102,7 @@ public String updateCatalogMeta(
+ " last_version = #{newCatalogMeta.lastVersion},"
+ " deleted_at = #{newCatalogMeta.deletedAt}"
+ " WHERE catalog_id = #{oldCatalogMeta.catalogId}"
+ " AND catalog_name = #{oldCatalogMeta.catalogName}"
+ " AND metalake_id = #{oldCatalogMeta.metalakeId}"
+ " AND type = #{oldCatalogMeta.type}"
+ " AND provider = #{oldCatalogMeta.provider}"
+ " AND (catalog_comment = #{oldCatalogMeta.catalogComment} "
+ " OR (CAST(catalog_comment AS VARCHAR) IS NULL AND "
+ " CAST(#{oldCatalogMeta.catalogComment} AS VARCHAR) IS NULL))"
+ " AND properties = #{oldCatalogMeta.properties}"
+ " AND audit_info = #{oldCatalogMeta.auditInfo}"
+ " AND current_version = #{oldCatalogMeta.currentVersion}"
+ " AND last_version = #{oldCatalogMeta.lastVersion}"
+ " AND deleted_at = 0";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@

public class MetalakeMetaPostgreSQLProvider extends MetalakeMetaBaseSQLProvider {
@Override
public String softDeleteMetalakeMetaByMetalakeId(Long metalakeId) {
public String softDeleteMetalakeMetaByMetalakeId(Long metalakeId, Long currentVersion) {
return "UPDATE "
+ TABLE_NAME
+ " SET deleted_at = CAST(EXTRACT(EPOCH FROM CURRENT_TIMESTAMP) * 1000 AS BIGINT)"
+ " WHERE metalake_id = #{metalakeId} AND deleted_at = 0";
+ " WHERE metalake_id = #{metalakeId}"
+ " AND current_version = #{currentVersion} AND deleted_at = 0";
}

@Override
Expand Down Expand Up @@ -75,15 +76,7 @@ public String updateMetalakeMeta(
+ " current_version = #{newMetalakeMeta.currentVersion},"
+ " last_version = #{newMetalakeMeta.lastVersion}"
+ " WHERE metalake_id = #{oldMetalakeMeta.metalakeId}"
+ " AND metalake_name = #{oldMetalakeMeta.metalakeName}"
+ " AND (metalake_comment = #{oldMetalakeMeta.metalakeComment} "
+ " OR (CAST(metalake_comment AS VARCHAR) IS NULL AND "
+ " CAST(#{oldMetalakeMeta.metalakeComment} AS VARCHAR) IS NULL))"
+ " AND properties = #{oldMetalakeMeta.properties}"
+ " AND audit_info = #{oldMetalakeMeta.auditInfo}"
+ " AND schema_version = #{oldMetalakeMeta.schemaVersion}"
+ " AND current_version = #{oldMetalakeMeta.currentVersion}"
+ " AND last_version = #{oldMetalakeMeta.lastVersion}"
+ " AND deleted_at = 0";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,7 @@ public String updateSchemaMeta(
+ " last_version = #{newSchemaMeta.lastVersion},"
+ " deleted_at = #{newSchemaMeta.deletedAt}"
+ " WHERE schema_id = #{oldSchemaMeta.schemaId}"
+ " AND schema_name = #{oldSchemaMeta.schemaName}"
+ " AND metalake_id = #{oldSchemaMeta.metalakeId}"
+ " AND catalog_id = #{oldSchemaMeta.catalogId}"
+ " AND (schema_comment = #{oldSchemaMeta.schemaComment}"
+ " OR (CAST(schema_comment AS VARCHAR) IS NULL"
+ " AND CAST(#{oldSchemaMeta.schemaComment} AS VARCHAR) IS NULL))"
+ " AND properties = #{oldSchemaMeta.properties}"
+ " AND audit_info = #{oldSchemaMeta.auditInfo}"
+ " AND current_version = #{oldSchemaMeta.currentVersion}"
+ " AND last_version = #{oldSchemaMeta.lastVersion}"
+ " AND deleted_at = 0";
}

Expand All @@ -125,6 +116,15 @@ public String softDeleteSchemaMetasBySchemaIds(List<Long> schemaIds) {
+ "</script>";
}

@Override
public String softDeleteSchemaMetaBySchemaIdAndVersion(Long schemaId, Long currentVersion) {
return "UPDATE "
+ TABLE_NAME
+ " SET deleted_at = CAST(EXTRACT(EPOCH FROM CURRENT_TIMESTAMP) * 1000 AS BIGINT)"
+ " WHERE schema_id = #{schemaId}"
+ " AND current_version = #{currentVersion} AND deleted_at = 0";
}

@Override
public String softDeleteSchemaMetasByMetalakeId(Long metalakeId) {
return "UPDATE "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.gravitino.Namespace;
import org.apache.gravitino.exceptions.NoSuchEntityException;
import org.apache.gravitino.exceptions.NonEmptyEntityException;
import org.apache.gravitino.exceptions.OptimisticLockException;
import org.apache.gravitino.meta.CatalogEntity;
import org.apache.gravitino.meta.SchemaEntity;
import org.apache.gravitino.metrics.Monitored;
Expand Down Expand Up @@ -229,15 +230,19 @@ public <E extends Entity & HasIdentifier> CatalogEntity updateCatalog(
AtomicInteger updateResult = new AtomicInteger(0);
try {
SessionUtils.doMultipleWithCommit(
() ->
updateResult.set(
SessionUtils.getWithoutCommit(
CatalogMetaMapper.class,
mapper ->
mapper.updateCatalogMeta(
POConverters.updateCatalogPOWithVersion(
oldCatalogPO, newEntity, oldCatalogPO.getMetalakeId()),
oldCatalogPO))),
() -> {
updateResult.set(
SessionUtils.getWithoutCommit(
CatalogMetaMapper.class,
mapper ->
mapper.updateCatalogMeta(
POConverters.updateCatalogPOWithVersion(
oldCatalogPO, newEntity, oldCatalogPO.getMetalakeId()),
oldCatalogPO)));
if (updateResult.get() == 0) {
throw optimisticLockException(identifier);
}
},
Comment thread
yuqi1129 marked this conversation as resolved.
Outdated
() -> {
if (updateResult.get() > 0) {
SessionUtils.doWithoutCommit(
Expand All @@ -256,11 +261,7 @@ public <E extends Entity & HasIdentifier> CatalogEntity updateCatalog(
throw re;
}

if (updateResult.get() > 0) {
return newEntity;
} else {
throw new IOException("Failed to update the entity: " + identifier);
}
return newEntity;
}

@Monitored(
Expand All @@ -270,15 +271,14 @@ public boolean deleteCatalog(NameIdentifier identifier, boolean cascade) {
NameIdentifierUtil.checkCatalog(identifier);

String catalogName = identifier.name();
long catalogId = EntityIdService.getEntityId(identifier, Entity.EntityType.CATALOG);
CatalogPO catalogPO = getCatalogPOByName(identifier.namespace().level(0), catalogName);
long catalogId = catalogPO.getCatalogId();
long currentVersion = catalogPO.getCurrentVersion();
String metalakeName = identifier.namespace().level(0);

if (cascade) {
SessionUtils.doMultipleWithCommit(
() ->
SessionUtils.doWithoutCommit(
CatalogMetaMapper.class,
mapper -> mapper.softDeleteCatalogMetasByCatalogId(catalogId)),
() -> deleteCatalogWithVersion(identifier, catalogId, currentVersion),
() ->
SessionUtils.doWithoutCommit(
SchemaMetaMapper.class,
Expand Down Expand Up @@ -368,10 +368,7 @@ public boolean deleteCatalog(NameIdentifier identifier, boolean cascade) {
"Entity %s has sub-entities, you should remove sub-entities first", identifier);
}
SessionUtils.doMultipleWithCommit(
() ->
SessionUtils.doWithoutCommit(
CatalogMetaMapper.class,
mapper -> mapper.softDeleteCatalogMetasByCatalogId(catalogId)),
() -> deleteCatalogWithVersion(identifier, catalogId, currentVersion),
() ->
SessionUtils.doWithoutCommit(
OwnerMetaMapper.class,
Expand Down Expand Up @@ -415,6 +412,22 @@ public boolean deleteCatalog(NameIdentifier identifier, boolean cascade) {
return true;
}

private void deleteCatalogWithVersion(
NameIdentifier identifier, Long catalogId, Long currentVersion) {
int deleted =
SessionUtils.getWithoutCommit(
CatalogMetaMapper.class,
mapper -> mapper.softDeleteCatalogMetasByCatalogId(catalogId, currentVersion));
if (deleted == 0) {
throw optimisticLockException(identifier);
}
}

private OptimisticLockException optimisticLockException(NameIdentifier identifier) {
return new OptimisticLockException(
"The catalog %s was modified concurrently; retry the operation", identifier);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can define this method in the helper method.


@Monitored(
metricsSource = GRAVITINO_RELATIONAL_STORE_METRIC_NAME,
baseMetricName = "deleteCatalogMetasByLegacyTimeline")
Expand Down
Loading
Loading