Skip to content

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

[#779] feat(core): Use tree lock in Gravitino web server (#2260)

Qi Yub9313d826d4a
diff --git a/core/src/main/java/com/datastrato/gravitino/GravitinoEnv.java b/core/src/main/java/com/datastrato/gravitino/GravitinoEnv.java
index 9b27f561b0..43c851be9d 100644
--- a/core/src/main/java/com/datastrato/gravitino/GravitinoEnv.java
+++ b/core/src/main/java/com/datastrato/gravitino/GravitinoEnv.java
@@ -7,6 +7,7 @@ package com.datastrato.gravitino;
import com.datastrato.gravitino.auxiliary.AuxiliaryServiceManager;
import com.datastrato.gravitino.catalog.CatalogManager;
import com.datastrato.gravitino.catalog.CatalogOperationDispatcher;
+import com.datastrato.gravitino.lock.LockManager;
import com.datastrato.gravitino.meta.MetalakeManager;
import com.datastrato.gravitino.metrics.MetricsSystem;
import com.datastrato.gravitino.metrics.source.JVMMetricsSource;
@@ -39,6 +40,8 @@ public class GravitinoEnv {
private MetricsSystem metricsSystem;
+ private LockManager lockManager;
+
private GravitinoEnv() {}
private static class InstanceHolder {
@@ -85,6 +88,8 @@ public class GravitinoEnv {
this.auxServiceManager.serviceInit(
config.getConfigsWithPrefix(AuxiliaryServiceManager.GRAVITINO_AUX_SERVICE_PREFIX));
+ // Tree lock
+ this.lockManager = new LockManager(config);
LOG.info("Gravitino Environment is initialized.");
}
@@ -152,6 +157,10 @@ public class GravitinoEnv {
return metricsSystem;
}
+ public LockManager getLockManager() {
+ return lockManager;
+ }
+
public void start() {
auxServiceManager.serviceStart();
metricsSystem.start();
diff --git a/core/src/main/java/com/datastrato/gravitino/lock/TreeLockUtils.java b/core/src/main/java/com/datastrato/gravitino/lock/TreeLockUtils.java
new file mode 100644
index 0000000000..2826555def
--- /dev/null
+++ b/core/src/main/java/com/datastrato/gravitino/lock/TreeLockUtils.java
@@ -0,0 +1,55 @@
+/*
+ * Copyright 2024 Datastrato Pvt Ltd.
+ * This software is licensed under the Apache License version 2.
+ */
+
+package com.datastrato.gravitino.lock;
+
+import com.datastrato.gravitino.GravitinoEnv;
+import com.datastrato.gravitino.NameIdentifier;
+import com.datastrato.gravitino.utils.Executable;
+
+/** Utility class for tree locks. */
+public class TreeLockUtils {
+
+ private TreeLockUtils() {
+ // Prevent instantiation.
+ }
+
+ /**
+ * Execute the given executable with the given tree lock.
+ *
+ * @param identifier The identifier of resource path that the lock attempts to lock.
+ * @param lockType The type of lock to use.
+ * @param executable The executable to execute.
+ * @return The result of the executable.
+ * @param <R> The type of the result.
+ * @param <E> The type of the exception.
+ * @throws E If the executable throws an exception.
+ */
+ public static <R, E extends Exception> R doWithTreeLock(
+ NameIdentifier identifier, LockType lockType, Executable<R, E> executable) throws E {
+ TreeLock lock = GravitinoEnv.getInstance().getLockManager().createTreeLock(identifier);
+ try {
+ lock.lock(lockType);
+ return executable.execute();
+ } finally {
+ lock.unlock();
+ }
+ }
+
+ /**
+ * Execute the given executable with the root tree lock.
+ *
+ * @param lockType The type of lock to use.
+ * @param executable The executable to execute.
+ * @return The result of the executable.
+ * @param <R> The type of the result.
+ * @param <E> The type of the exception.
+ * @throws E If the executable throws an exception.
+ */
+ public static <R, E extends Exception> R doWithRootTreeLock(
+ LockType lockType, Executable<R, E> executable) throws E {
+ return doWithTreeLock(LockManager.ROOT, lockType, executable);
+ }
+}
diff --git a/server/src/main/java/com/datastrato/gravitino/server/web/rest/CatalogOperations.java b/server/src/main/java/com/datastrato/gravitino/server/web/rest/CatalogOperations.java
index 696a93b6e2..db6cd4e7c4 100644
--- a/server/src/main/java/com/datastrato/gravitino/server/web/rest/CatalogOperations.java
+++ b/server/src/main/java/com/datastrato/gravitino/server/web/rest/CatalogOperations.java
@@ -16,6 +16,8 @@ import com.datastrato.gravitino.dto.responses.CatalogResponse;
import com.datastrato.gravitino.dto.responses.DropResponse;
import com.datastrato.gravitino.dto.responses.EntityListResponse;
import com.datastrato.gravitino.dto.util.DTOConverters;
+import com.datastrato.gravitino.lock.LockType;
+import com.datastrato.gravitino.lock.TreeLockUtils;
import com.datastrato.gravitino.server.web.Utils;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
@@ -57,7 +59,12 @@ public class CatalogOperations {
httpRequest,
() -> {
Namespace catalogNS = Namespace.ofCatalog(metalake);
- NameIdentifier[] idents = manager.listCatalogs(catalogNS);
+ // Lock the root and the metalake with WRITE lock to ensure the consistency of the list.
+ NameIdentifier[] idents =
+ TreeLockUtils.doWithTreeLock(
+ NameIdentifier.of(metalake),
+ LockType.WRITE,
+ () -> manager.listCatalogs(catalogNS));
return Utils.ok(new EntityListResponse(idents));
});
} catch (Exception e) {
@@ -76,12 +83,16 @@ public class CatalogOperations {
request.validate();
NameIdentifier ident = NameIdentifier.ofCatalog(metalake, request.getName());
Catalog catalog =
- manager.createCatalog(
+ TreeLockUtils.doWithTreeLock(
ident,
- request.getType(),
- request.getProvider(),
- request.getComment(),
- request.getProperties());
+ LockType.WRITE,
+ () ->
+ manager.createCatalog(
+ ident,
+ request.getType(),
+ request.getProvider(),
+ request.getComment(),
+ request.getProperties()));
return Utils.ok(new CatalogResponse(DTOConverters.toDTO(catalog)));
});
@@ -98,7 +109,8 @@ public class CatalogOperations {
@PathParam("metalake") String metalakeName, @PathParam("catalog") String catalogName) {
try {
NameIdentifier ident = NameIdentifier.ofCatalog(metalakeName, catalogName);
- Catalog catalog = manager.loadCatalog(ident);
+ Catalog catalog =
+ TreeLockUtils.doWithTreeLock(ident, LockType.READ, () -> manager.loadCatalog(ident));
return Utils.ok(new CatalogResponse(DTOConverters.toDTO(catalog)));
} catch (Exception e) {
@@ -124,8 +136,9 @@ public class CatalogOperations {
request.getUpdates().stream()
.map(CatalogUpdateRequest::catalogChange)
.toArray(CatalogChange[]::new);
-
- Catalog catalog = manager.alterCatalog(ident, changes);
+ Catalog catalog =
+ TreeLockUtils.doWithTreeLock(
+ ident, LockType.WRITE, () -> manager.alterCatalog(ident, changes));
return Utils.ok(new CatalogResponse(DTOConverters.toDTO(catalog)));
});
@@ -145,7 +158,9 @@ public class CatalogOperations {
httpRequest,
() -> {
NameIdentifier ident = NameIdentifier.ofCatalog(metalakeName, catalogName);
- boolean dropped = manager.dropCatalog(ident);
+ boolean dropped =
+ TreeLockUtils.doWithTreeLock(
+ ident, LockType.WRITE, () -> manager.dropCatalog(ident));
if (!dropped) {
LOG.warn("Failed to drop catalog {} under metalake {}", catalogName, metalakeName);
}
diff --git a/server/src/main/java/com/datastrato/gravitino/server/web/rest/FilesetOperations.java b/server/src/main/java/com/datastrato/gravitino/server/web/rest/FilesetOperations.java
index e2c7746cd1..8e2b52e8cd 100644
--- a/server/src/main/java/com/datastrato/gravitino/server/web/rest/FilesetOperations.java
+++ b/server/src/main/java/com/datastrato/gravitino/server/web/rest/FilesetOperations.java
@@ -18,6 +18,8 @@ import com.datastrato.gravitino.dto.responses.FilesetResponse;
import com.datastrato.gravitino.dto.util.DTOConverters;
import com.datastrato.gravitino.file.Fileset;
import com.datastrato.gravitino.file.FilesetChange;
+import com.datastrato.gravitino.lock.LockType;
+import com.datastrato.gravitino.lock.TreeLockUtils;
import com.datastrato.gravitino.metrics.MetricNames;
import com.datastrato.gravitino.server.web.Utils;
import java.util.Optional;
@@ -62,7 +64,11 @@ public class FilesetOperations {
httpRequest,
() -> {
Namespace filesetNS = Namespace.ofFileset(metalake, catalog, schema);
- NameIdentifier[] idents = dispatcher.listFilesets(filesetNS);
+ NameIdentifier[] idents =
+ TreeLockUtils.doWithTreeLock(
+ NameIdentifier.of(metalake, catalog, schema),
+ LockType.WRITE,
+ () -> dispatcher.listFilesets(filesetNS));
return Utils.ok(new EntityListResponse(idents));
});
@@ -89,12 +95,16 @@ public class FilesetOperations {
NameIdentifier.ofFileset(metalake, catalog, schema, request.getName());
Fileset fileset =
- dispatcher.createFileset(
+ TreeLockUtils.doWithTreeLock(
ident,
- request.getComment(),
- Optional.ofNullable(request.getType()).orElse(Fileset.Type.MANAGED),
- request.getStorageLocation(),
- request.getProperties());
+ LockType.WRITE,
+ () ->
+ dispatcher.createFileset(
+ ident,
+ request.getComment(),
+ Optional.ofNullable(request.getType()).orElse(Fileset.Type.MANAGED),
+ request.getStorageLocation(),
+ request.getProperties()));
return Utils.ok(new FilesetResponse(DTOConverters.toDTO(fileset)));
});
@@ -119,7 +129,9 @@ public class FilesetOperations {
httpRequest,
() -> {
NameIdentifier ident = NameIdentifier.ofFileset(metalake, catalog, schema, fileset);
- Fileset t = dispatcher.loadFileset(ident);
+ Fileset t =
+ TreeLockUtils.doWithTreeLock(
+ ident, LockType.READ, () -> dispatcher.loadFileset(ident));
return Utils.ok(new FilesetResponse(DTOConverters.toDTO(t)));
});
} catch (Exception e) {
@@ -148,7 +160,9 @@ public class FilesetOperations {
request.getUpdates().stream()
.map(FilesetUpdateRequest::filesetChange)
.toArray(FilesetChange[]::new);
- Fileset t = dispatcher.alterFileset(ident, changes);
+ Fileset t =
+ TreeLockUtils.doWithTreeLock(
+ ident, LockType.WRITE, () -> dispatcher.alterFileset(ident, changes));
return Utils.ok(new FilesetResponse(DTOConverters.toDTO(t)));
});
@@ -172,7 +186,9 @@ public class FilesetOperations {
httpRequest,
() -> {
NameIdentifier ident = NameIdentifier.ofFileset(metalake, catalog, schema, fileset);
- boolean dropped = dispatcher.dropFileset(ident);
+ boolean dropped =
+ TreeLockUtils.doWithTreeLock(
+ ident, LockType.WRITE, () -> dispatcher.dropFileset(ident));
if (!dropped) {
LOG.warn("Failed to drop fileset {} under schema {}", fileset, schema);
}
diff --git a/server/src/main/java/com/datastrato/gravitino/server/web/rest/MetalakeOperations.java b/server/src/main/java/com/datastrato/gravitino/server/web/rest/MetalakeOperations.java
index db3dfd1cd6..f8eaea0e2a 100644
--- a/server/src/main/java/com/datastrato/gravitino/server/web/rest/MetalakeOperations.java
+++ b/server/src/main/java/com/datastrato/gravitino/server/web/rest/MetalakeOperations.java
@@ -17,6 +17,8 @@ import com.datastrato.gravitino.dto.responses.DropResponse;
import com.datastrato.gravitino.dto.responses.MetalakeListResponse;
import com.datastrato.gravitino.dto.responses.MetalakeResponse;
import com.datastrato.gravitino.dto.util.DTOConverters;
+import com.datastrato.gravitino.lock.LockType;
+import com.datastrato.gravitino.lock.TreeLockUtils;
import com.datastrato.gravitino.meta.BaseMetalake;
import com.datastrato.gravitino.meta.MetalakeManager;
import com.datastrato.gravitino.metrics.MetricNames;
@@ -63,7 +65,8 @@ public class MetalakeOperations {
return Utils.doAs(
httpRequest,
() -> {
- BaseMetalake[] metalakes = manager.listMetalakes();
+ BaseMetalake[] metalakes =
+ TreeLockUtils.doWithRootTreeLock(LockType.WRITE, manager::listMetalakes);
MetalakeDTO[] metalakeDTOS =
Arrays.stream(metalakes).map(DTOConverters::toDTO).toArray(MetalakeDTO[]::new);
return Utils.ok(new MetalakeListResponse(metalakeDTOS));
@@ -87,7 +90,12 @@ public class MetalakeOperations {
request.validate();
NameIdentifier ident = NameIdentifier.ofMetalake(request.getName());
BaseMetalake metalake =
- manager.createMetalake(ident, request.getComment(), request.getProperties());
+ TreeLockUtils.doWithTreeLock(
+ ident,
+ LockType.WRITE,
+ () ->
+ manager.createMetalake(
+ ident, request.getComment(), request.getProperties()));
return Utils.ok(new MetalakeResponse(DTOConverters.toDTO(metalake)));
});
@@ -107,7 +115,9 @@ public class MetalakeOperations {
httpRequest,
() -> {
NameIdentifier identifier = NameIdentifier.ofMetalake(metalakeName);
- BaseMetalake metalake = manager.loadMetalake(identifier);
+ BaseMetalake metalake =
+ TreeLockUtils.doWithTreeLock(
+ identifier, LockType.READ, () -> manager.loadMetalake(identifier));
return Utils.ok(new MetalakeResponse(DTOConverters.toDTO(metalake)));
});
@@ -133,8 +143,9 @@ public class MetalakeOperations {
updatesRequest.getUpdates().stream()
.map(MetalakeUpdateRequest::metalakeChange)
.toArray(MetalakeChange[]::new);
-
- BaseMetalake updatedMetalake = manager.alterMetalake(identifier, changes);
+ BaseMetalake updatedMetalake =
+ TreeLockUtils.doWithTreeLock(
+ identifier, LockType.WRITE, () -> manager.alterMetalake(identifier, changes));
return Utils.ok(new MetalakeResponse(DTOConverters.toDTO(updatedMetalake)));
});
@@ -154,7 +165,9 @@ public class MetalakeOperations {
httpRequest,
() -> {
NameIdentifier identifier = NameIdentifier.ofMetalake(metalakeName);
- boolean dropped = manager.dropMetalake(identifier);
+ boolean dropped =
+ TreeLockUtils.doWithTreeLock(
+ identifier, LockType.WRITE, () -> manager.dropMetalake(identifier));
if (!dropped) {
LOG.warn("Failed to drop metalake by name {}", metalakeName);
}
diff --git a/server/src/main/java/com/datastrato/gravitino/server/web/rest/PartitionOperations.java b/server/src/main/java/com/datastrato/gravitino/server/web/rest/PartitionOperations.java
index 62d3fee497..118d1e63a8 100644
--- a/server/src/main/java/com/datastrato/gravitino/server/web/rest/PartitionOperations.java
+++ b/server/src/main/java/com/datastrato/gravitino/server/web/rest/PartitionOperations.java
@@ -17,6 +17,8 @@ import com.datastrato.gravitino.dto.responses.PartitionListResponse;
import com.datastrato.gravitino.dto.responses.PartitionNameListResponse;
import com.datastrato.gravitino.dto.responses.PartitionResponse;
import com.datastrato.gravitino.dto.util.DTOConverters;
+import com.datastrato.gravitino.lock.LockType;
+import com.datastrato.gravitino.lock.TreeLockUtils;
import com.datastrato.gravitino.metrics.MetricNames;
import com.datastrato.gravitino.rel.Table;
import com.datastrato.gravitino.rel.partitions.Partition;
@@ -60,14 +62,19 @@ public class PartitionOperations {
httpRequest,
() -> {
NameIdentifier tableIdent = NameIdentifier.of(metalake, catalog, schema, table);
- Table loadTable = dispatcher.loadTable(tableIdent);
- if (verbose) {
- Partition[] partitions = loadTable.supportPartitions().listPartitions();
- return Utils.ok(new PartitionListResponse(toDTOs(partitions)));
- } else {
- String[] partitionNames = loadTable.supportPartitions().listPartitionNames();
- return Utils.ok(new PartitionNameListResponse((partitionNames)));
- }
+ return TreeLockUtils.doWithTreeLock(
+ tableIdent,
+ LockType.READ,
+ () -> {
+ Table loadTable = dispatcher.loadTable(tableIdent);
+ if (verbose) {
+ Partition[] partitions = loadTable.supportPartitions().listPartitions();
+ return Utils.ok(new PartitionListResponse(toDTOs(partitions)));
+ } else {
+ String[] partitionNames = loadTable.supportPartitions().listPartitionNames();
+ return Utils.ok(new PartitionNameListResponse((partitionNames)));
+ }
+ });
});
} catch (Exception e) {
return ExceptionHandlers.handlePartitionException(OperationType.LIST, "", table, e);
@@ -90,9 +97,14 @@ public class PartitionOperations {
httpRequest,
() -> {
NameIdentifier tableIdent = NameIdentifier.of(metalake, catalog, schema, table);
- Table loadTable = dispatcher.loadTable(tableIdent);
- Partition p = loadTable.supportPartitions().getPartition(partition);
- return Utils.ok(new PartitionResponse(DTOConverters.toDTO(p)));
+ return TreeLockUtils.doWithTreeLock(
+ tableIdent,
+ LockType.READ,
+ () -> {
+ Table loadTable = dispatcher.loadTable(tableIdent);
+ Partition p = loadTable.supportPartitions().getPartition(partition);
+ return Utils.ok(new PartitionResponse(DTOConverters.toDTO(p)));
+ });
});
} catch (Exception e) {
return ExceptionHandlers.handlePartitionException(OperationType.GET, "", table, e);
@@ -117,10 +129,18 @@ public class PartitionOperations {
httpRequest,
() -> {
NameIdentifier tableIdent = NameIdentifier.of(metalake, catalog, schema, table);
- Table loadTable = dispatcher.loadTable(tableIdent);
- Partition p =
- loadTable.supportPartitions().addPartition(fromDTO(request.getPartitions()[0]));
- return Utils.ok(new PartitionListResponse(new PartitionDTO[] {DTOConverters.toDTO(p)}));
+ return TreeLockUtils.doWithTreeLock(
+ tableIdent,
+ LockType.WRITE,
+ () -> {
+ Table loadTable = dispatcher.loadTable(tableIdent);
+ Partition p =
+ loadTable
+ .supportPartitions()
+ .addPartition(fromDTO(request.getPartitions()[0]));
+ return Utils.ok(
+ new PartitionListResponse(new PartitionDTO[] {DTOConverters.toDTO(p)}));
+ });
});
} catch (Exception e) {
return ExceptionHandlers.handlePartitionException(OperationType.CREATE, "", table, e);
diff --git a/server/src/main/java/com/datastrato/gravitino/server/web/rest/SchemaOperations.java b/server/src/main/java/com/datastrato/gravitino/server/web/rest/SchemaOperations.java
index 0342668d7e..399f6001a1 100644
--- a/server/src/main/java/com/datastrato/gravitino/server/web/rest/SchemaOperations.java
+++ b/server/src/main/java/com/datastrato/gravitino/server/web/rest/SchemaOperations.java
@@ -16,6 +16,8 @@ import com.datastrato.gravitino.dto.responses.DropResponse;
import com.datastrato.gravitino.dto.responses.EntityListResponse;
import com.datastrato.gravitino.dto.responses.SchemaResponse;
import com.datastrato.gravitino.dto.util.DTOConverters;
+import com.datastrato.gravitino.lock.LockType;
+import com.datastrato.gravitino.lock.TreeLockUtils;
import com.datastrato.gravitino.metrics.MetricNames;
import com.datastrato.gravitino.rel.Schema;
import com.datastrato.gravitino.rel.SchemaChange;
@@ -65,7 +67,11 @@ public class SchemaOperations {
httpRequest,
() -> {
Namespace schemaNS = Namespace.ofSchema(metalake, catalog);
- NameIdentifier[] idents = dispatcher.listSchemas(schemaNS);
+ NameIdentifier[] idents =
+ TreeLockUtils.doWithTreeLock(
+ NameIdentifier.of(metalake, catalog),
+ LockType.WRITE,
+ () -> dispatcher.listSchemas(schemaNS));
return Utils.ok(new EntityListResponse(idents));
});
} catch (Exception e) {
@@ -88,7 +94,12 @@ public class SchemaOperations {
request.validate();
NameIdentifier ident = NameIdentifier.ofSchema(metalake, catalog, request.getName());
Schema schema =
- dispatcher.createSchema(ident, request.getComment(), request.getProperties());
+ TreeLockUtils.doWithTreeLock(
+ ident,
+ LockType.WRITE,
+ () ->
+ dispatcher.createSchema(
+ ident, request.getComment(), request.getProperties()));
return Utils.ok(new SchemaResponse(DTOConverters.toDTO(schema)));
});
@@ -112,7 +123,9 @@ public class SchemaOperations {
httpRequest,
() -> {
NameIdentifier ident = NameIdentifier.ofSchema(metalake, catalog, schema);
- Schema s = dispatcher.loadSchema(ident);
+ Schema s =
+ TreeLockUtils.doWithTreeLock(
+ ident, LockType.READ, () -> dispatcher.loadSchema(ident));
return Utils.ok(new SchemaResponse(DTOConverters.toDTO(s)));
});
@@ -141,7 +154,9 @@ public class SchemaOperations {
request.getUpdates().stream()
.map(SchemaUpdateRequest::schemaChange)
.toArray(SchemaChange[]::new);
- Schema s = dispatcher.alterSchema(ident, changes);
+ Schema s =
+ TreeLockUtils.doWithTreeLock(
+ ident, LockType.WRITE, () -> dispatcher.alterSchema(ident, changes));
return Utils.ok(new SchemaResponse(DTOConverters.toDTO(s)));
});
@@ -165,7 +180,9 @@ public class SchemaOperations {
httpRequest,
() -> {
NameIdentifier ident = NameIdentifier.ofSchema(metalake, catalog, schema);
- boolean dropped = dispatcher.dropSchema(ident, cascade);
+ boolean dropped =
+ TreeLockUtils.doWithTreeLock(
+ ident, LockType.WRITE, () -> dispatcher.dropSchema(ident, cascade));
if (!dropped) {
LOG.warn("Fail to drop schema {} under namespace {}", schema, ident.namespace());
}
diff --git a/server/src/main/java/com/datastrato/gravitino/server/web/rest/TableOperations.java b/server/src/main/java/com/datastrato/gravitino/server/web/rest/TableOperations.java
index ee075d9474..f2bcd9026d 100644
--- a/server/src/main/java/com/datastrato/gravitino/server/web/rest/TableOperations.java
+++ b/server/src/main/java/com/datastrato/gravitino/server/web/rest/TableOperations.java
@@ -19,6 +19,8 @@ import com.datastrato.gravitino.dto.responses.DropResponse;
import com.datastrato.gravitino.dto.responses.EntityListResponse;
import com.datastrato.gravitino.dto.responses.TableResponse;
import com.datastrato.gravitino.dto.util.DTOConverters;
+import com.datastrato.gravitino.lock.LockType;
+import com.datastrato.gravitino.lock.TreeLockUtils;
import com.datastrato.gravitino.metrics.MetricNames;
import com.datastrato.gravitino.rel.Table;
import com.datastrato.gravitino.rel.TableChange;
@@ -66,7 +68,11 @@ public class TableOperations {
httpRequest,
() -> {
Namespace tableNS = Namespace.ofTable(metalake, catalog, schema);
- NameIdentifier[] idents = dispatcher.listTables(tableNS);
+ NameIdentifier[] idents =
+ TreeLockUtils.doWithTreeLock(
+ NameIdentifier.of(metalake, catalog, schema),
+ LockType.WRITE,
+ () -> dispatcher.listTables(tableNS));
return Utils.ok(new EntityListResponse(idents));
});
@@ -93,15 +99,19 @@ public class TableOperations {
NameIdentifier.ofTable(metalake, catalog, schema, request.getName());
Table table =
- dispatcher.createTable(
+ TreeLockUtils.doWithTreeLock(
ident,
- fromDTOs(request.getColumns()),
- request.getComment(),
- request.getProperties(),
- fromDTOs(request.getPartitioning()),
- fromDTO(request.getDistribution()),
- fromDTOs(request.getSortOrders()),
- fromDTOs(request.getIndexes()));
+ LockType.WRITE,
+ () ->
+ dispatcher.createTable(
+ ident,
+ fromDTOs(request.getColumns()),
+ request.getComment(),
+ request.getProperties(),
+ fromDTOs(request.getPartitioning()),
+ fromDTO(request.getDistribution()),
+ fromDTOs(request.getSortOrders()),
+ fromDTOs(request.getIndexes())));
return Utils.ok(new TableResponse(DTOConverters.toDTO(table)));
});
@@ -126,7 +136,9 @@ public class TableOperations {
httpRequest,
() -> {
NameIdentifier ident = NameIdentifier.ofTable(metalake, catalog, schema, table);
- Table t = dispatcher.loadTable(ident);
+ Table t =
+ TreeLockUtils.doWithTreeLock(
+ ident, LockType.READ, () -> dispatcher.loadTable(ident));
return Utils.ok(new TableResponse(DTOConverters.toDTO(t)));
});
} catch (Exception e) {
@@ -155,7 +167,9 @@ public class TableOperations {
request.getUpdates().stream()
.map(TableUpdateRequest::tableChange)
.toArray(TableChange[]::new);
- Table t = dispatcher.alterTable(ident, changes);
+ Table t =
+ TreeLockUtils.doWithTreeLock(
+ ident, LockType.WRITE, () -> dispatcher.alterTable(ident, changes));
return Utils.ok(new TableResponse(DTOConverters.toDTO(t)));
});
@@ -180,7 +194,11 @@ public class TableOperations {
httpRequest,
() -> {
NameIdentifier ident = NameIdentifier.ofTable(metalake, catalog, schema, table);
- boolean dropped = purge ? dispatcher.purgeTable(ident) : dispatcher.dropTable(ident);
+ boolean dropped =
+ TreeLockUtils.doWithTreeLock(
+ ident,
+ LockType.WRITE,
+ () -> purge ? dispatcher.purgeTable(ident) : dispatcher.dropTable(ident));
if (!dropped) {
LOG.warn("Failed to drop table {} under schema {}", table, schema);
}