Skip to content

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

[#72] feat(server): add Catalog rest implementation for Graviton (#75)

Jerry Shao302752cc91a1
diff --git a/common/src/main/java/com/datastrato/graviton/dto/requests/CatalogCreateRequest.java b/common/src/main/java/com/datastrato/graviton/dto/requests/CatalogCreateRequest.java
index 983b01a6ae..8490017b27 100644
--- a/common/src/main/java/com/datastrato/graviton/dto/requests/CatalogCreateRequest.java
+++ b/common/src/main/java/com/datastrato/graviton/dto/requests/CatalogCreateRequest.java
@@ -8,9 +8,11 @@ import java.util.Map;
import javax.annotation.Nullable;
import lombok.EqualsAndHashCode;
import lombok.Getter;
+import lombok.ToString;
@Getter
@EqualsAndHashCode
+@ToString
public class CatalogCreateRequest implements RESTRequest {
@JsonProperty("name")
diff --git a/common/src/main/java/com/datastrato/graviton/dto/requests/CatalogUpdateRequest.java b/common/src/main/java/com/datastrato/graviton/dto/requests/CatalogUpdateRequest.java
index 3c875c9a3e..b4beb73413 100644
--- a/common/src/main/java/com/datastrato/graviton/dto/requests/CatalogUpdateRequest.java
+++ b/common/src/main/java/com/datastrato/graviton/dto/requests/CatalogUpdateRequest.java
@@ -6,6 +6,7 @@ import com.fasterxml.jackson.annotation.*;
import com.google.common.base.Preconditions;
import lombok.EqualsAndHashCode;
import lombok.Getter;
+import lombok.ToString;
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY)
@@ -26,6 +27,7 @@ public interface CatalogUpdateRequest extends RESTRequest {
CatalogChange catalogChange();
@EqualsAndHashCode
+ @ToString
class RenameCatalogRequest implements CatalogUpdateRequest {
@Getter
@@ -54,6 +56,7 @@ public interface CatalogUpdateRequest extends RESTRequest {
}
@EqualsAndHashCode
+ @ToString
class UpdateCatalogCommentRequest implements CatalogUpdateRequest {
@Getter
@@ -82,6 +85,7 @@ public interface CatalogUpdateRequest extends RESTRequest {
}
@EqualsAndHashCode
+ @ToString
class SetCatalogPropertyRequest implements CatalogUpdateRequest {
@Getter
@@ -117,6 +121,7 @@ public interface CatalogUpdateRequest extends RESTRequest {
}
@EqualsAndHashCode
+ @ToString
class RemoveCatalogPropertyRequest implements CatalogUpdateRequest {
@Getter
diff --git a/common/src/main/java/com/datastrato/graviton/dto/requests/CatalogUpdatesRequest.java b/common/src/main/java/com/datastrato/graviton/dto/requests/CatalogUpdatesRequest.java
new file mode 100644
index 0000000000..43bc118257
--- /dev/null
+++ b/common/src/main/java/com/datastrato/graviton/dto/requests/CatalogUpdatesRequest.java
@@ -0,0 +1,30 @@
+package com.datastrato.graviton.dto.requests;
+
+import com.datastrato.graviton.rest.RESTRequest;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import java.util.List;
+import lombok.EqualsAndHashCode;
+import lombok.Getter;
+import lombok.ToString;
+
+@Getter
+@EqualsAndHashCode
+@ToString
+public class CatalogUpdatesRequest implements RESTRequest {
+
+ @JsonProperty("requests")
+ private final List<CatalogUpdateRequest> requests;
+
+ public CatalogUpdatesRequest(List<CatalogUpdateRequest> requests) {
+ this.requests = requests;
+ }
+
+ public CatalogUpdatesRequest() {
+ this(null);
+ }
+
+ @Override
+ public void validate() throws IllegalArgumentException {
+ requests.forEach(RESTRequest::validate);
+ }
+}
diff --git a/common/src/main/java/com/datastrato/graviton/dto/requests/MetalakeUpdateRequest.java b/common/src/main/java/com/datastrato/graviton/dto/requests/MetalakeUpdateRequest.java
index f2cf32ef34..3bf3f4f846 100644
--- a/common/src/main/java/com/datastrato/graviton/dto/requests/MetalakeUpdateRequest.java
+++ b/common/src/main/java/com/datastrato/graviton/dto/requests/MetalakeUpdateRequest.java
@@ -6,6 +6,7 @@ import com.fasterxml.jackson.annotation.*;
import com.google.common.base.Preconditions;
import lombok.EqualsAndHashCode;
import lombok.Getter;
+import lombok.ToString;
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY)
@@ -26,6 +27,7 @@ public interface MetalakeUpdateRequest extends RESTRequest {
MetalakeChange metalakeChange();
@EqualsAndHashCode
+ @ToString
class RenameMetalakeRequest implements MetalakeUpdateRequest {
@Getter
@@ -54,6 +56,7 @@ public interface MetalakeUpdateRequest extends RESTRequest {
}
@EqualsAndHashCode
+ @ToString
class UpdateMetalakeCommentRequest implements MetalakeUpdateRequest {
@Getter
@@ -116,6 +119,7 @@ public interface MetalakeUpdateRequest extends RESTRequest {
}
@EqualsAndHashCode
+ @ToString
class RemoveMetalakePropertyRequest implements MetalakeUpdateRequest {
@Getter
diff --git a/common/src/main/java/com/datastrato/graviton/dto/requests/MetalakeUpdatesRequest.java b/common/src/main/java/com/datastrato/graviton/dto/requests/MetalakeUpdatesRequest.java
index b51c63aeb7..363a17156c 100644
--- a/common/src/main/java/com/datastrato/graviton/dto/requests/MetalakeUpdatesRequest.java
+++ b/common/src/main/java/com/datastrato/graviton/dto/requests/MetalakeUpdatesRequest.java
@@ -5,9 +5,11 @@ import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;
import lombok.EqualsAndHashCode;
import lombok.Getter;
+import lombok.ToString;
@Getter
@EqualsAndHashCode
+@ToString
public class MetalakeUpdatesRequest implements RESTRequest {
@JsonProperty("requests")
diff --git a/common/src/main/java/com/datastrato/graviton/dto/responses/CatalogListResponse.java b/common/src/main/java/com/datastrato/graviton/dto/responses/CatalogListResponse.java
new file mode 100644
index 0000000000..0239177635
--- /dev/null
+++ b/common/src/main/java/com/datastrato/graviton/dto/responses/CatalogListResponse.java
@@ -0,0 +1,49 @@
+package com.datastrato.graviton.dto.responses;
+
+import com.datastrato.graviton.dto.CatalogDTO;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.google.common.base.Preconditions;
+import java.util.Arrays;
+import lombok.EqualsAndHashCode;
+import lombok.Getter;
+import lombok.ToString;
+
+@Getter
+@EqualsAndHashCode(callSuper = true)
+@ToString
+public class CatalogListResponse extends BaseResponse {
+
+ @JsonProperty("catalogs")
+ private final CatalogDTO[] catalogs;
+
+ public CatalogListResponse(CatalogDTO[] catalogs) {
+ super(0, null, null);
+ this.catalogs = catalogs;
+ }
+
+ // This is the constructor that is used by Jackson deserializer
+ public CatalogListResponse() {
+ super();
+ this.catalogs = null;
+ }
+
+ @Override
+ public void validate() throws IllegalArgumentException {
+ Preconditions.checkArgument(catalogs != null, "catalogs must be non-null");
+ Arrays.stream(catalogs)
+ .forEach(
+ catalog -> {
+ Preconditions.checkArgument(
+ catalog.name() != null && !catalog.name().isEmpty(),
+ "catalog 'name' must be non-null and non-empty");
+ Preconditions.checkArgument(
+ catalog.type() != null, "catalog 'type' must be non-null and non-empty");
+ Preconditions.checkArgument(
+ catalog.auditInfo().creator() != null && !catalog.auditInfo().creator().isEmpty(),
+ "catalog 'audit.creator' must be non-null and non-empty");
+ Preconditions.checkArgument(
+ catalog.auditInfo().createTime() != null,
+ "catalog 'audit.createTime' must be non-null");
+ });
+ }
+}
diff --git a/common/src/main/java/com/datastrato/graviton/dto/responses/CatalogResponse.java b/common/src/main/java/com/datastrato/graviton/dto/responses/CatalogResponse.java
new file mode 100644
index 0000000000..6f41f98d07
--- /dev/null
+++ b/common/src/main/java/com/datastrato/graviton/dto/responses/CatalogResponse.java
@@ -0,0 +1,43 @@
+package com.datastrato.graviton.dto.responses;
+
+import com.datastrato.graviton.dto.CatalogDTO;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.google.common.base.Preconditions;
+import lombok.EqualsAndHashCode;
+import lombok.Getter;
+import lombok.ToString;
+
+@Getter
+@ToString
+@EqualsAndHashCode(callSuper = true)
+public class CatalogResponse extends BaseResponse {
+
+ @JsonProperty("catalog")
+ private final CatalogDTO catalog;
+
+ public CatalogResponse(CatalogDTO catalog) {
+ super(0, null, null);
+ this.catalog = catalog;
+ }
+
+ // This is the constructor that is used by Jackson deserializer
+ public CatalogResponse() {
+ super();
+ this.catalog = null;
+ }
+
+ @Override
+ public void validate() throws IllegalArgumentException {
+ Preconditions.checkArgument(catalog != null, "catalog must be non-null");
+ Preconditions.checkArgument(
+ catalog.name() != null && !catalog.name().isEmpty(),
+ "catalog 'name' must be non-null and non-empty");
+ Preconditions.checkArgument(
+ catalog.type() != null, "catalog 'type' must be non-null and non-empty");
+ Preconditions.checkArgument(
+ catalog.auditInfo().creator() != null && !catalog.auditInfo().creator().isEmpty(),
+ "catalog 'audit.creator' must be non-null and non-empty");
+ Preconditions.checkArgument(
+ catalog.auditInfo().createTime() != null, "catalog 'audit.createTime' must be non-null");
+ }
+}
diff --git a/common/src/test/java/com/datastrato/graviton/json/TestRequestJsonSerDe.java b/common/src/test/java/com/datastrato/graviton/json/TestRequestJsonSerDe.java
index 70109c6f05..02e58108a7 100644
--- a/common/src/test/java/com/datastrato/graviton/json/TestRequestJsonSerDe.java
+++ b/common/src/test/java/com/datastrato/graviton/json/TestRequestJsonSerDe.java
@@ -40,7 +40,7 @@ public class TestRequestJsonSerDe {
}
@Test
- public void testmetalakeUpdateRequestSerDe() throws JsonProcessingException {
+ public void testMetalakeUpdateRequestSerDe() throws JsonProcessingException {
MetalakeUpdateRequest req = new MetalakeUpdateRequest.RenameMetalakeRequest("newMetalake");
String serJson = JsonUtils.objectMapper().writeValueAsString(req);
MetalakeUpdateRequest deserReq =
diff --git a/server/src/main/java/com/datastrato/graviton/server/GravitonServer.java b/server/src/main/java/com/datastrato/graviton/server/GravitonServer.java
index e10939c7c1..4b082950f1 100644
--- a/server/src/main/java/com/datastrato/graviton/server/GravitonServer.java
+++ b/server/src/main/java/com/datastrato/graviton/server/GravitonServer.java
@@ -1,5 +1,6 @@
package com.datastrato.graviton.server;
+import com.datastrato.graviton.meta.BaseCatalogsOperations;
import com.datastrato.graviton.meta.BaseMetalakesOperations;
import com.datastrato.graviton.server.web.JettyServer;
import com.datastrato.graviton.server.web.ObjectMapperProvider;
@@ -48,6 +49,7 @@ public class GravitonServer extends ResourceConfig {
@Override
protected void configure() {
bind(BaseMetalakesOperations.class).to(BaseMetalakesOperations.class).ranked(1);
+ bind(BaseCatalogsOperations.class).to(BaseCatalogsOperations.class).ranked(1);
}
});
register(ObjectMapperProvider.class).register(JacksonFeature.class);
diff --git a/server/src/main/java/com/datastrato/graviton/server/web/rest/CatalogOperations.java b/server/src/main/java/com/datastrato/graviton/server/web/rest/CatalogOperations.java
new file mode 100644
index 0000000000..58877500ab
--- /dev/null
+++ b/server/src/main/java/com/datastrato/graviton/server/web/rest/CatalogOperations.java
@@ -0,0 +1,226 @@
+package com.datastrato.graviton.server.web.rest;
+
+import com.datastrato.graviton.Catalog;
+import com.datastrato.graviton.CatalogChange;
+import com.datastrato.graviton.NameIdentifier;
+import com.datastrato.graviton.Namespace;
+import com.datastrato.graviton.dto.CatalogDTO;
+import com.datastrato.graviton.dto.requests.CatalogCreateRequest;
+import com.datastrato.graviton.dto.requests.CatalogUpdateRequest;
+import com.datastrato.graviton.dto.requests.CatalogUpdatesRequest;
+import com.datastrato.graviton.dto.responses.CatalogListResponse;
+import com.datastrato.graviton.dto.responses.CatalogResponse;
+import com.datastrato.graviton.exceptions.CatalogAlreadyExistsException;
+import com.datastrato.graviton.exceptions.NoSuchCatalogException;
+import com.datastrato.graviton.exceptions.NoSuchMetalakeException;
+import com.datastrato.graviton.meta.BaseCatalogsOperations;
+import com.datastrato.graviton.server.web.Utils;
+import java.util.Arrays;
+import javax.inject.Inject;
+import javax.servlet.http.HttpServletRequest;
+import javax.ws.rs.*;
+import javax.ws.rs.core.Context;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+@Path("/metalakes/{metalake}/catalogs")
+@Consumes(MediaType.APPLICATION_JSON)
+@Produces(MediaType.APPLICATION_JSON)
+public class CatalogOperations {
+
+ private static final Logger LOG = LoggerFactory.getLogger(CatalogOperations.class);
+
+ private final BaseCatalogsOperations ops;
+
+ @Context private HttpServletRequest httpRequest;
+
+ @Inject
+ public CatalogOperations(BaseCatalogsOperations ops) {
+ this.ops = ops;
+ }
+
+ @GET
+ @Produces("application/vnd.graviton.v1+json")
+ public Response listCatalogs(@PathParam("metalake") String metalake) {
+ if (metalake == null || metalake.isEmpty()) {
+ LOG.error("Metalake name is null or empty");
+ return Utils.illegalArguments("Metalake name is null or empty");
+ }
+
+ try {
+ Namespace metalakeNS = Namespace.of(metalake);
+ Catalog[] catalogs = ops.listCatalogs(metalakeNS);
+
+ CatalogDTO[] catalogDTOS =
+ Arrays.stream(catalogs).map(DTOConverters::toDTO).toArray(CatalogDTO[]::new);
+ return Utils.ok(new CatalogListResponse(catalogDTOS));
+
+ } catch (NoSuchMetalakeException ex) {
+ LOG.error("Metalake {} does not exist, fail to list catalogs", metalake);
+ return Utils.notFound("Metalake " + metalake + " does not exist");
+
+ } catch (Exception e) {
+ LOG.error("Failed to list catalogs under metalake {}", metalake, e);
+ return Utils.internalError(e.getMessage());
+ }
+ }
+
+ @POST
+ @Produces("application/vnd.graviton.v1+json")
+ public Response createCatalog(
+ @PathParam("metalake") String metalake, CatalogCreateRequest request) {
+ if (metalake == null || metalake.isEmpty()) {
+ LOG.error("Metalake name is null or empty");
+ return Utils.illegalArguments("Metalake name is null or empty");
+ }
+
+ try {
+ request.validate();
+ } catch (IllegalArgumentException e) {
+ LOG.error("Failed to validate create Catalog arguments {}", request, e);
+ return Utils.illegalArguments(e.getMessage());
+ }
+
+ try {
+ NameIdentifier ident = NameIdentifier.of(metalake, request.getName());
+ Catalog catalog =
+ ops.createCatalog(
+ ident, request.getType(), request.getComment(), request.getProperties());
+ return Utils.ok(new CatalogResponse(DTOConverters.toDTO(catalog)));
+
+ } catch (NoSuchMetalakeException ex) {
+ LOG.error("Metalake {} does not exist, fail to create catalog", metalake);
+ return Utils.notFound("Metalake " + metalake + " does not exist");
+
+ } catch (CatalogAlreadyExistsException ex) {
+ LOG.error("Catalog {} already exists under metalake {}", request.getName(), metalake);
+ return Utils.alreadyExists(
+ String.format(
+ "Catalog %s already exists under metalake %s", request.getName(), metalake));
+
+ } catch (Exception e) {
+ LOG.error("Failed to create catalog under metalake {}", metalake, e);
+ return Utils.internalError(e.getMessage());
+ }
+ }
+
+ @GET
+ @Path("{catalog}")
+ @Produces("application/vnd.graviton.v1+json")
+ public Response loadCatalog(
+ @PathParam("metalake") String metalakeName, @PathParam("catalog") String catalogName) {
+ if (metalakeName == null || metalakeName.isEmpty()) {
+ LOG.error("Metalake name is null or empty");
+ return Utils.illegalArguments("Metalake name is null or empty");
+ }
+
+ if (catalogName == null || catalogName.isEmpty()) {
+ LOG.error("Catalog name is null or empty");
+ return Utils.illegalArguments("Catalog name is null or empty");
+ }
+
+ try {
+ NameIdentifier ident = NameIdentifier.of(metalakeName, catalogName);
+ Catalog catalog = ops.loadCatalog(ident);
+ return Utils.ok(new CatalogResponse(DTOConverters.toDTO(catalog)));
+
+ } catch (NoSuchMetalakeException ex) {
+ LOG.error("Metalake {} does not exist, fail to load catalog {}", metalakeName, catalogName);
+ return Utils.notFound("Metalake " + metalakeName + " does not exist");
+
+ } catch (NoSuchCatalogException ex) {
+ LOG.error("Catalog {} does not exist under metalake {}", catalogName, metalakeName);
+ return Utils.notFound(
+ String.format("Catalog %s does not exist under metalake %s", catalogName, metalakeName));
+
+ } catch (Exception e) {
+ LOG.error("Failed to load catalog {} under metalake {}", catalogName, metalakeName, e);
+ return Utils.internalError(e.getMessage());
+ }
+ }
+
+ @PUT
+ @Path("{catalog}")
+ @Produces("application/vnd.graviton.v1+json")
+ public Response alterCatalog(
+ @PathParam("metalake") String metalakeName,
+ @PathParam("catalog") String catalogName,
+ CatalogUpdatesRequest request) {
+ if (metalakeName == null || metalakeName.isEmpty()) {
+ LOG.error("Metalake name is null or empty");
+ return Utils.illegalArguments("Metalake name is null or empty");
+ }
+
+ if (catalogName == null || catalogName.isEmpty()) {
+ LOG.error("Catalog name is null or empty");
+ return Utils.illegalArguments("Catalog name is null or empty");
+ }
+
+ try {
+ request.validate();
+ } catch (IllegalArgumentException e) {
+ LOG.error("Failed to validate alter Catalog arguments {}", request, e);
+ return Utils.illegalArguments(e.getMessage());
+ }
+
+ try {
+ NameIdentifier ident = NameIdentifier.of(metalakeName, catalogName);
+ CatalogChange[] changes =
+ request.getRequests().stream()
+ .map(CatalogUpdateRequest::catalogChange)
+ .toArray(CatalogChange[]::new);
+
+ Catalog catalog = ops.alterCatalog(ident, changes);
+ return Utils.ok(new CatalogResponse(DTOConverters.toDTO(catalog)));
+
+ } catch (NoSuchCatalogException ex) {
+ LOG.error("Catalog {} does not exist under metalake {}", catalogName, metalakeName);
+ return Utils.notFound(ex.getMessage());
+
+ } catch (IllegalArgumentException ex) {
+ LOG.error(
+ "Failed to alter catalog {} under metalake {} with unsupported changes",
+ catalogName,
+ metalakeName,
+ ex);
+ return Utils.illegalArguments(ex.getMessage());
+
+ } catch (Exception e) {
+ LOG.error("Failed to alter catalog {} under metalake {}", catalogName, metalakeName, e);
+ return Utils.internalError(e.getMessage());
+ }
+ }
+
+ @DELETE
+ @Path("{catalog}")
+ @Produces("application/vnd.graviton.v1+json")
+ public Response dropCatalog(
+ @PathParam("metalake") String metalakeName, @PathParam("catalog") String catalogName) {
+ if (metalakeName == null || metalakeName.isEmpty()) {
+ LOG.error("Metalake name is null or empty");
+ return Utils.illegalArguments("Metalake name is null or empty");
+ }
+
+ if (catalogName == null || catalogName.isEmpty()) {
+ LOG.error("Catalog name is null or empty");
+ return Utils.illegalArguments("Catalog name is null or empty");
+ }
+
+ try {
+ NameIdentifier ident = NameIdentifier.of(metalakeName, catalogName);
+ boolean dropped = ops.dropCatalog(ident);
+ if (dropped) {
+ return Utils.ok();
+ } else {
+ LOG.warn("Failed to drop catalog {} under metalake {}", catalogName, metalakeName);
+ return Utils.internalError("Failed to drop catalog " + catalogName);
+ }
+
+ } catch (Exception e) {
+ LOG.error("Failed to drop catalog {} under metalake {}", catalogName, metalakeName, e);
+ return Utils.internalError(e.getMessage());
+ }
+ }
+}
diff --git a/server/src/main/java/com/datastrato/graviton/server/web/rest/DTOConverters.java b/server/src/main/java/com/datastrato/graviton/server/web/rest/DTOConverters.java
index fde1d5a1e2..98ee5b46b0 100644
--- a/server/src/main/java/com/datastrato/graviton/server/web/rest/DTOConverters.java
+++ b/server/src/main/java/com/datastrato/graviton/server/web/rest/DTOConverters.java
@@ -1,8 +1,10 @@
package com.datastrato.graviton.server.web.rest;
import com.datastrato.graviton.Audit;
+import com.datastrato.graviton.Catalog;
import com.datastrato.graviton.Metalake;
import com.datastrato.graviton.dto.AuditDTO;
+import com.datastrato.graviton.dto.CatalogDTO;
import com.datastrato.graviton.dto.MetalakeDTO;
public class DTOConverters {
@@ -26,4 +28,14 @@ public class DTOConverters {
.withAudit(toDTO(metalake.auditInfo()))
.build();
}
+
+ public static CatalogDTO toDTO(Catalog catalog) {
+ return new CatalogDTO.Builder()
+ .withName(catalog.name())
+ .withType(catalog.type())
+ .withComment(catalog.comment())
+ .withProperties(catalog.properties())
+ .withAudit(toDTO(catalog.auditInfo()))
+ .build();
+ }
}
diff --git a/server/src/main/java/com/datastrato/graviton/server/web/rest/MetalakeOperations.java b/server/src/main/java/com/datastrato/graviton/server/web/rest/MetalakeOperations.java
index 3df40c1d0f..0227115af1 100644
--- a/server/src/main/java/com/datastrato/graviton/server/web/rest/MetalakeOperations.java
+++ b/server/src/main/java/com/datastrato/graviton/server/web/rest/MetalakeOperations.java
@@ -72,8 +72,8 @@ public class MetalakeOperations {
return Utils.ok(new MetalakeResponse(DTOConverters.toDTO(metalake)));
} catch (MetalakeAlreadyExistsException exception) {
- LOG.warn("Metalake with name {} already exists", request.getName(), exception);
- return Utils.alreadyExists("Metalake with name " + request.getName() + " already exists");
+ LOG.error("Metalake with name {} already exists", request.getName(), exception);
+ return Utils.alreadyExists(exception.getMessage());
} catch (Exception e) {
LOG.error("Failed to create metalake", e);
@@ -86,6 +86,7 @@ public class MetalakeOperations {
@Produces("application/vnd.graviton.v1+json")
public Response loadMetalake(@PathParam("name") String metalakeName) {
if (metalakeName == null || metalakeName.isEmpty()) {
+ LOG.error("Metalake name is null or empty");
return Utils.illegalArguments("Metalake name is required");
}
@@ -95,8 +96,8 @@ public class MetalakeOperations {
return Utils.ok(new MetalakeResponse(DTOConverters.toDTO(metalake)));
} catch (NoSuchMetalakeException e) {
- LOG.warn("Failed to find metalake by name {}", metalakeName);
- return Utils.notFound("Failed to find metalake by name " + metalakeName);
+ LOG.error("Failed to find metalake by name {}", metalakeName);
+ return Utils.notFound(e.getMessage());
} catch (Exception e) {
LOG.error("Failed to get metalake by name {}", metalakeName, e);
@@ -110,13 +111,14 @@ public class MetalakeOperations {
public Response alterMetalake(
@PathParam("name") String metalakeName, MetalakeUpdatesRequest updatesRequest) {
if (metalakeName == null || metalakeName.isEmpty()) {
+ LOG.error("Metalake name is null or empty");
return Utils.illegalArguments("Metalake name is required");
}
try {
updatesRequest.validate();
} catch (Exception e) {
- LOG.error("Failed to validate update metalake arguments {}", updatesRequest, e);
+ LOG.error("Failed to validate alter metalake arguments {}", updatesRequest, e);
return Utils.illegalArguments(e.getMessage());
}
@@ -131,8 +133,12 @@ public class MetalakeOperations {
return Utils.ok(new MetalakeResponse(DTOConverters.toDTO(updatedMetalake)));
} catch (NoSuchMetalakeException e) {
- LOG.warn("Failed to find metalake by name {}", metalakeName);
- return Utils.notFound("Failed to find metalake by name " + metalakeName);
+ LOG.error("Failed to find metalake by name {}", metalakeName);
+ return Utils.notFound(e.getMessage());
+
+ } catch (IllegalArgumentException ex) {
+ LOG.error("Failed to alter metalake by name {} by unsupported change", metalakeName, ex);
+ return Utils.illegalArguments(ex.getMessage());
} catch (Exception e) {
LOG.error("Failed to update metalake by name {}", metalakeName, e);
@@ -145,6 +151,7 @@ public class MetalakeOperations {
@Produces("application/vnd.graviton.v1+json")
public Response dropMetalake(@PathParam("name") String metalakeName) {
if (metalakeName == null || metalakeName.isEmpty()) {
+ LOG.error("Metalake name is null or empty");
return Utils.illegalArguments("metalake name is required");
}
diff --git a/server/src/test/java/com/datastrato/graviton/server/web/rest/TestCatalog.java b/server/src/test/java/com/datastrato/graviton/server/web/rest/TestCatalog.java
new file mode 100644
index 0000000000..76ee7f6c44
--- /dev/null
+++ b/server/src/test/java/com/datastrato/graviton/server/web/rest/TestCatalog.java
@@ -0,0 +1,31 @@
+package com.datastrato.graviton.server.web.rest;
+
+import com.datastrato.graviton.Config;
+import com.datastrato.graviton.meta.BaseCatalog;
+import java.io.IOException;
+
+public class TestCatalog extends BaseCatalog {
+ @Override
+ public void initialize(Config config) throws RuntimeException {}
+
+ @Override
+ public void close() throws IOException {}
+
+ public static class Builder extends BaseCatalogBuilder<Builder, TestCatalog> {
+
+ @Override
+ protected TestCatalog internalBuild() {
+ TestCatalog catalog = new TestCatalog();
+ catalog.id = id;
+ catalog.metalakeId = metalakeId;
+ catalog.name = name;
+ catalog.namespace = namespace;
+ catalog.type = type;
+ catalog.comment = comment;
+ catalog.properties = properties;
+ catalog.auditInfo = auditInfo;
+
+ return catalog;
+ }
+ }
+}
diff --git a/server/src/test/java/com/datastrato/graviton/server/web/rest/TestCatalogOperations.java b/server/src/test/java/com/datastrato/graviton/server/web/rest/TestCatalogOperations.java
new file mode 100644
index 0000000000..0dc8ace345
--- /dev/null
+++ b/server/src/test/java/com/datastrato/graviton/server/web/rest/TestCatalogOperations.java
@@ -0,0 +1,381 @@
+package com.datastrato.graviton.server.web.rest;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.*;
+
+import com.datastrato.graviton.Catalog;
+import com.datastrato.graviton.Namespace;
+import com.datastrato.graviton.dto.CatalogDTO;
+import com.datastrato.graviton.dto.requests.CatalogCreateRequest;
+import com.datastrato.graviton.dto.requests.CatalogUpdateRequest;
+import com.datastrato.graviton.dto.requests.CatalogUpdatesRequest;
+import com.datastrato.graviton.dto.responses.BaseResponse;
+import com.datastrato.graviton.dto.responses.CatalogListResponse;
+import com.datastrato.graviton.dto.responses.CatalogResponse;
+import com.datastrato.graviton.dto.responses.ErrorType;
+import com.datastrato.graviton.exceptions.CatalogAlreadyExistsException;
+import com.datastrato.graviton.exceptions.NoSuchCatalogException;
+import com.datastrato.graviton.exceptions.NoSuchMetalakeException;
+import com.datastrato.graviton.meta.AuditInfo;
+import com.datastrato.graviton.meta.BaseCatalogsOperations;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+import java.time.Instant;
+import javax.servlet.http.HttpServletRequest;
+import javax.ws.rs.client.Entity;
+import javax.ws.rs.core.Application;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import org.glassfish.hk2.utilities.binding.AbstractBinder;
+import org.glassfish.jersey.server.ResourceConfig;
+import org.glassfish.jersey.test.JerseyTest;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class TestCatalogOperations extends JerseyTest {
+
+ private static class MockServletRequestFactory extends ServletRequestFactoryBase {
+ @Override
+ public HttpServletRequest get() {
+ HttpServletRequest request = mock(HttpServletRequest.class);
+ when(request.getRemoteUser()).thenReturn(null);
+ return request;
+ }
+ }
+
+ private BaseCatalogsOperations ops = mock(BaseCatalogsOperations.class);
+
+ @Override
+ protected Application configure() {
+ ResourceConfig resourceConfig = new ResourceConfig();
+ resourceConfig.register(CatalogOperations.class);
+ resourceConfig.register(
+ new AbstractBinder() {
+ @Override
+ protected void configure() {
+ bind(ops).to(BaseCatalogsOperations.class).ranked(2);
+ bindFactory(MockServletRequestFactory.class).to(HttpServletRequest.class);
+ }
+ });
+
+ return resourceConfig;
+ }
+
+ @Test
+ public void testListCatalogs() {
+ TestCatalog catalog1 = buildCatalog("metalake1", "catalog1");
+ TestCatalog catalog2 = buildCatalog("metalake1", "catalog2");
+
+ when(ops.listCatalogs(any())).thenReturn(new Catalog[] {catalog1, catalog2});
+
+ Response resp =
+ target("/metalakes/metalake1/catalogs")
+ .request(MediaType.APPLICATION_JSON_TYPE)
+ .accept("application/vnd.graviton.v1+json")
+ .get();
+
+ Assertions.assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
+ Assertions.assertEquals(MediaType.APPLICATION_JSON_TYPE, resp.getMediaType());
+
+ CatalogListResponse listResponse = resp.readEntity(CatalogListResponse.class);
+ Assertions.assertEquals(0, listResponse.getCode());
+ Assertions.assertNull(listResponse.getType());
+ Assertions.assertNull(listResponse.getMessage());
+
+ CatalogDTO[] catalogs = listResponse.getCatalogs();
+ Assertions.assertEquals(2, catalogs.length);
+ Assertions.assertEquals("catalog1", catalogs[0].name());
+ Assertions.assertEquals("catalog2", catalogs[1].name());
+
+ doThrow(new NoSuchMetalakeException("mock error")).when(ops).listCatalogs(any());
+ Response resp1 =
+ target("/metalakes/metalake1/catalogs")
+ .request(MediaType.APPLICATION_JSON_TYPE)
+ .accept("application/vnd.graviton.v1+json")
+ .get();
+
+ Assertions.assertEquals(Response.Status.NOT_FOUND.getStatusCode(), resp1.getStatus());
+ Assertions.assertEquals(MediaType.APPLICATION_JSON_TYPE, resp1.getMediaType());
+
+ BaseResponse baseResponse = resp1.readEntity(BaseResponse.class);
+ Assertions.assertEquals(ErrorType.NOT_FOUND.errorCode(), baseResponse.getCode());
+ Assertions.assertEquals(ErrorType.NOT_FOUND.errorType(), baseResponse.getType());
+ }
+
+ @Test
+ public void testCreateCatalog() {
+ CatalogCreateRequest req =
+ new CatalogCreateRequest(
+ "catalog1", Catalog.Type.RELATIONAL, "comment", ImmutableMap.of("key", "value"));
+ TestCatalog catalog = buildCatalog("metalake1", "catalog1");
+
+ when(ops.createCatalog(any(), any(), any(), any())).thenReturn(catalog);
+
+ Response resp =
+ target("/metalakes/metalake1/catalogs")
+ .request(MediaType.APPLICATION_JSON_TYPE)
+ .accept("application/vnd.graviton.v1+json")
+ .post(Entity.entity(req, MediaType.APPLICATION_JSON_TYPE));
+
+ Assertions.assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
+ Assertions.assertEquals(MediaType.APPLICATION_JSON_TYPE, resp.getMediaType());
+
+ CatalogResponse catalogResponse = resp.readEntity(CatalogResponse.class);
+ Assertions.assertEquals(0, catalogResponse.getCode());
+ Assertions.assertNull(catalogResponse.getType());
+ Assertions.assertNull(catalogResponse.getMessage());
+
+ CatalogDTO catalogDTO = catalogResponse.getCatalog();
+ Assertions.assertEquals("catalog1", catalogDTO.name());
+ Assertions.assertEquals(Catalog.Type.RELATIONAL, catalogDTO.type());
+ Assertions.assertEquals("comment", catalogDTO.comment());
+ Assertions.assertEquals(ImmutableMap.of("key", "value"), catalogDTO.properties());
+
+ // Test throw NoSuchMetalakeException
+ doThrow(new NoSuchMetalakeException("mock error"))
+ .when(ops)
+ .createCatalog(any(), any(), any(), any());
+ Response resp1 =
+ target("/metalakes/metalake1/catalogs")
+ .request(MediaType.APPLICATION_JSON_TYPE)
+ .accept("application/vnd.graviton.v1+json")
+ .post(Entity.entity(req, MediaType.APPLICATION_JSON_TYPE));
+
+ Assertions.assertEquals(Response.Status.NOT_FOUND.getStatusCode(), resp1.getStatus());
+ Assertions.assertEquals(MediaType.APPLICATION_JSON_TYPE, resp1.getMediaType());
+
+ BaseResponse baseResponse = resp1.readEntity(BaseResponse.class);
+ Assertions.assertEquals(ErrorType.NOT_FOUND.errorCode(), baseResponse.getCode());
+ Assertions.assertEquals(ErrorType.NOT_FOUND.errorType(), baseResponse.getType());
+
+ // Test throw CatalogAlreadyExistsException
+ doThrow(new CatalogAlreadyExistsException("mock error"))
+ .when(ops)
+ .createCatalog(any(), any(), any(), any());
+ Response resp2 =
+ target("/metalakes/metalake1/catalogs")
+ .request(MediaType.APPLICATION_JSON_TYPE)
+ .accept("application/vnd.graviton.v1+json")
+ .post(Entity.entity(req, MediaType.APPLICATION_JSON_TYPE));
+
+ Assertions.assertEquals(Response.Status.CONFLICT.getStatusCode(), resp2.getStatus());
+
+ BaseResponse baseResponse2 = resp2.readEntity(BaseResponse.class);
+ Assertions.assertEquals(ErrorType.ALREADY_EXISTS.errorCode(), baseResponse2.getCode());
+ Assertions.assertEquals(ErrorType.ALREADY_EXISTS.errorType(), baseResponse2.getType());
+
+ // Test throw internal RuntimeException
+ doThrow(new RuntimeException("mock error")).when(ops).createCatalog(any(), any(), any(), any());
+ Response resp3 =
+ target("/metalakes/metalake1/catalogs")
+ .request(MediaType.APPLICATION_JSON_TYPE)
+ .accept("application/vnd.graviton.v1+json")
+ .post(Entity.entity(req, MediaType.APPLICATION_JSON_TYPE));
+
+ Assertions.assertEquals(
+ Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), resp3.getStatus());
+
+ BaseResponse baseResponse3 = resp3.readEntity(BaseResponse.class);
+ Assertions.assertEquals(ErrorType.INTERNAL_ERROR.errorCode(), baseResponse3.getCode());
+ Assertions.assertEquals(ErrorType.INTERNAL_ERROR.errorType(), baseResponse3.getType());
+ }
+
+ @Test
+ public void testLoadCatalog() {
+ TestCatalog catalog = buildCatalog("metalake1", "catalog1");
+
+ when(ops.loadCatalog(any())).thenReturn(catalog);
+
+ Response resp =
+ target("/metalakes/metalake1/catalogs/catalog1")
+ .request(MediaType.APPLICATION_JSON_TYPE)
+ .accept("application/vnd.graviton.v1+json")
+ .get();
+
+ Assertions.assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
+
+ CatalogResponse catalogResponse = resp.readEntity(CatalogResponse.class);
+ Assertions.assertEquals(0, catalogResponse.getCode());
+ Assertions.assertNull(catalogResponse.getType());
+ Assertions.assertNull(catalogResponse.getMessage());
+
+ CatalogDTO catalogDTO = catalogResponse.getCatalog();
+ Assertions.assertEquals("catalog1", catalogDTO.name());
+ Assertions.assertEquals(Catalog.Type.RELATIONAL, catalogDTO.type());
+ Assertions.assertEquals("comment", catalogDTO.comment());
+ Assertions.assertEquals(ImmutableMap.of("key", "value"), catalogDTO.properties());
+
+ // Test throw NoSuchMetalakeException
+ doThrow(new NoSuchMetalakeException("mock error")).when(ops).loadCatalog(any());
+ Response resp1 =
+ target("/metalakes/metalake1/catalogs/catalog1")
+ .request(MediaType.APPLICATION_JSON_TYPE)
+ .accept("application/vnd.graviton.v1+json")
+ .get();
+
+ Assertions.assertEquals(Response.Status.NOT_FOUND.getStatusCode(), resp1.getStatus());
+
+ BaseResponse baseResponse = resp1.readEntity(BaseResponse.class);
+ Assertions.assertEquals(ErrorType.NOT_FOUND.errorCode(), baseResponse.getCode());
+ Assertions.assertEquals(ErrorType.NOT_FOUND.errorType(), baseResponse.getType());
+
+ // Test throw NoSuchCatalogException
+ doThrow(new NoSuchCatalogException("mock error")).when(ops).loadCatalog(any());
+ Response resp2 =
+ target("/metalakes/metalake1/catalogs/catalog1")
+ .request(MediaType.APPLICATION_JSON_TYPE)
+ .accept("application/vnd.graviton.v1+json")
+ .get();
+
+ Assertions.assertEquals(Response.Status.NOT_FOUND.getStatusCode(), resp2.getStatus());
+
+ BaseResponse baseResponse2 = resp2.readEntity(BaseResponse.class);
+ Assertions.assertEquals(ErrorType.NOT_FOUND.errorCode(), baseResponse2.getCode());
+ Assertions.assertEquals(ErrorType.NOT_FOUND.errorType(), baseResponse2.getType());
+
+ // Test throw internal RuntimeException
+ doThrow(new RuntimeException("mock error")).when(ops).loadCatalog(any());
+ Response resp3 =
+ target("/metalakes/metalake1/catalogs/catalog1")
+ .request(MediaType.APPLICATION_JSON_TYPE)
+ .accept("application/vnd.graviton.v1+json")
+ .get();
+
+ Assertions.assertEquals(
+ Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), resp3.getStatus());
+
+ BaseResponse baseResponse3 = resp3.readEntity(BaseResponse.class);
+ Assertions.assertEquals(ErrorType.INTERNAL_ERROR.errorCode(), baseResponse3.getCode());
+ Assertions.assertEquals(ErrorType.INTERNAL_ERROR.errorType(), baseResponse3.getType());
+ }
+
+ @Test
+ public void testAlterCatalog() {
+ TestCatalog catalog = buildCatalog("metalake1", "catalog2");
+
+ when(ops.alterCatalog(any(), any())).thenReturn(catalog);
+
+ CatalogUpdateRequest updateRequest = new CatalogUpdateRequest.RenameCatalogRequest("catalog2");
+ CatalogUpdatesRequest req = new CatalogUpdatesRequest(ImmutableList.of(updateRequest));
+
+ Response resp =
+ target("/metalakes/metalake1/catalogs/catalog1")
+ .request(MediaType.APPLICATION_JSON_TYPE)
+ .accept("application/vnd.graviton.v1+json")
+ .put(Entity.entity(req, MediaType.APPLICATION_JSON_TYPE));
+
+ Assertions.assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
+
+ CatalogResponse catalogResponse = resp.readEntity(CatalogResponse.class);
+ Assertions.assertEquals(0, catalogResponse.getCode());
+ Assertions.assertNull(catalogResponse.getType());
+ Assertions.assertNull(catalogResponse.getMessage());
+
+ CatalogDTO catalogDTO = catalogResponse.getCatalog();
+ Assertions.assertEquals("catalog2", catalogDTO.name());
+ Assertions.assertEquals(Catalog.Type.RELATIONAL, catalogDTO.type());
+ Assertions.assertEquals("comment", catalogDTO.comment());
+ Assertions.assertEquals(ImmutableMap.of("key", "value"), catalogDTO.properties());
+
+ // Test throw NoSuchCatalogException
+ doThrow(new NoSuchCatalogException("mock error")).when(ops).alterCatalog(any(), any());
+ Response resp2 =
+ target("/metalakes/metalake1/catalogs/catalog1")
+ .request(MediaType.APPLICATION_JSON_TYPE)
+ .accept("application/vnd.graviton.v1+json")
+ .put(Entity.entity(req, MediaType.APPLICATION_JSON_TYPE));
+
+ Assertions.assertEquals(Response.Status.NOT_FOUND.getStatusCode(), resp2.getStatus());
+
+ BaseResponse baseResponse = resp2.readEntity(BaseResponse.class);
+ Assertions.assertEquals(ErrorType.NOT_FOUND.errorCode(), baseResponse.getCode());
+ Assertions.assertEquals(ErrorType.NOT_FOUND.errorType(), baseResponse.getType());
+
+ // Test throw IllegalArgumentException
+ doThrow(new IllegalArgumentException("mock error")).when(ops).alterCatalog(any(), any());
+ Response resp3 =
+ target("/metalakes/metalake1/catalogs/catalog1")
+ .request(MediaType.APPLICATION_JSON_TYPE)
+ .accept("application/vnd.graviton.v1+json")
+ .put(Entity.entity(req, MediaType.APPLICATION_JSON_TYPE));
+
+ Assertions.assertEquals(Response.Status.BAD_REQUEST.getStatusCode(), resp3.getStatus());
+
+ BaseResponse baseResponse1 = resp3.readEntity(BaseResponse.class);
+ Assertions.assertEquals(ErrorType.INVALID_ARGUMENTS.errorCode(), baseResponse1.getCode());
+ Assertions.assertEquals(ErrorType.INVALID_ARGUMENTS.errorType(), baseResponse1.getType());
+
+ // Test throw internal RuntimeException
+ doThrow(new RuntimeException("mock error")).when(ops).alterCatalog(any(), any());
+ Response resp4 =
+ target("/metalakes/metalake1/catalogs/catalog1")
+ .request(MediaType.APPLICATION_JSON_TYPE)
+ .accept("application/vnd.graviton.v1+json")
+ .put(Entity.entity(req, MediaType.APPLICATION_JSON_TYPE));
+
+ Assertions.assertEquals(
+ Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), resp4.getStatus());
+
+ BaseResponse baseResponse2 = resp4.readEntity(BaseResponse.class);
+ Assertions.assertEquals(ErrorType.INTERNAL_ERROR.errorCode(), baseResponse2.getCode());
+ Assertions.assertEquals(ErrorType.INTERNAL_ERROR.errorType(), baseResponse2.getType());
+ }
+
+ @Test
+ public void testDropCatalog() {
+ when(ops.dropCatalog(any())).thenReturn(true);
+
+ Response resp =
+ target("/metalakes/metalake1/catalogs/catalog1")
+ .request(MediaType.APPLICATION_JSON_TYPE)
+ .accept("application/vnd.graviton.v1+json")
+ .delete();
+
+ Assertions.assertEquals(Response.Status.NO_CONTENT.getStatusCode(), resp.getStatus());
+
+ // Test when failed to drop catalog
+ when(ops.dropCatalog(any())).thenReturn(false);
+
+ Response resp2 =
+ target("/metalakes/metalake1/catalogs/catalog1")
+ .request(MediaType.APPLICATION_JSON_TYPE)
+ .accept("application/vnd.graviton.v1+json")
+ .delete();
+
+ Assertions.assertEquals(
+ Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), resp2.getStatus());
+
+ BaseResponse baseResponse = resp2.readEntity(BaseResponse.class);
+ Assertions.assertEquals(ErrorType.INTERNAL_ERROR.errorCode(), baseResponse.getCode());
+ Assertions.assertEquals(ErrorType.INTERNAL_ERROR.errorType(), baseResponse.getType());
+
+ // Test throw internal RuntimeException
+ doThrow(new RuntimeException("mock error")).when(ops).dropCatalog(any());
+ Response resp3 =
+ target("/metalakes/metalake1/catalogs/catalog1")
+ .request(MediaType.APPLICATION_JSON_TYPE)
+ .accept("application/vnd.graviton.v1+json")
+ .delete();
+
+ Assertions.assertEquals(
+ Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), resp3.getStatus());
+
+ BaseResponse baseResponse1 = resp3.readEntity(BaseResponse.class);
+ Assertions.assertEquals(ErrorType.INTERNAL_ERROR.errorCode(), baseResponse1.getCode());
+ Assertions.assertEquals(ErrorType.INTERNAL_ERROR.errorType(), baseResponse1.getType());
+ }
+
+ private static TestCatalog buildCatalog(String metalake, String catalogName) {
+ return new TestCatalog.Builder()
+ .withId(1L)
+ .withMetalakeId(1L)
+ .withName(catalogName)
+ .withComment("comment")
+ .withNamespace(Namespace.of(metalake))
+ .withProperties(ImmutableMap.of("key", "value"))
+ .withType(Catalog.Type.RELATIONAL)
+ .withAuditInfo(
+ new AuditInfo.Builder().withCreator("creator").withCreateTime(Instant.now()).build())
+ .build();
+ }
+}
diff --git a/server/src/test/java/com/datastrato/graviton/server/web/rest/TestMetalakesOperations.java b/server/src/test/java/com/datastrato/graviton/server/web/rest/TestMetalakeOperations.java
similarity index 99%
rename from server/src/test/java/com/datastrato/graviton/server/web/rest/TestMetalakesOperations.java
rename to server/src/test/java/com/datastrato/graviton/server/web/rest/TestMetalakeOperations.java
index 2349872113..f201f2055b 100644
--- a/server/src/test/java/com/datastrato/graviton/server/web/rest/TestMetalakesOperations.java
+++ b/server/src/test/java/com/datastrato/graviton/server/web/rest/TestMetalakeOperations.java
@@ -32,7 +32,7 @@ import org.glassfish.jersey.test.JerseyTest;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
-public class TestMetalakesOperations extends JerseyTest {
+public class TestMetalakeOperations extends JerseyTest {
private static class MockServletRequestFactory extends ServletRequestFactoryBase {
@Override