World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.
Java ai-catalogdata-catalogdatalakefederated-querylakehousemetadatametalakemodel-catalogopendatacatalogskycomputingstratosphere
[#95] feat(core): add `GravitonEnv` to manage all the modules in Graviton (#108)
Jerry Shaoba4bb0c2978a
diff --git a/client-java/src/main/java/com/datastrato/graviton/ErrorHandlers.java b/client-java/src/main/java/com/datastrato/graviton/ErrorHandlers.javaindex ddc6d7d58d..8eef64f211 100644--- a/client-java/src/main/java/com/datastrato/graviton/ErrorHandlers.java+++ b/client-java/src/main/java/com/datastrato/graviton/ErrorHandlers.java@@ -2,7 +2,6 @@* Copyright 2023 Datastrato.* This software is licensed under the Apache License version 2.*/-package com.datastrato.graviton;import com.datastrato.graviton.client.ErrorHandler;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.javaindex 986e21369d..176a6490f6 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@@ -31,13 +31,14 @@ public class CatalogCreateRequest implements RESTRequest {@Nullable@JsonProperty("properties")- private Map properties;+ private Map<String, String> properties;public CatalogCreateRequest() {this(null, null, null, null);}- public CatalogCreateRequest(String name, Catalog.Type type, String comment, Map properties) {+ public CatalogCreateRequest(+ String name, Catalog.Type type, String comment, Map<String, String> properties) {this.name = name;this.type = type;this.comment = comment;diff --git a/core/src/main/java/com/datastrato/graviton/EntityStoreFactory.java b/core/src/main/java/com/datastrato/graviton/EntityStoreFactory.javaindex ddeaa6a5b8..12027f19e9 100644--- a/core/src/main/java/com/datastrato/graviton/EntityStoreFactory.java+++ b/core/src/main/java/com/datastrato/graviton/EntityStoreFactory.java@@ -11,7 +11,7 @@ import org.slf4j.LoggerFactory;public class EntityStoreFactory {- private static final Logger LOG = LoggerFactory.getLogger(EntityStore.class);+ private static final Logger LOG = LoggerFactory.getLogger(EntityStoreFactory.class);// Register EntityStore's short name to its full qualified class name in the map. So that user// don't need to specify the full qualified class name when creating an EntityStore.diff --git a/core/src/main/java/com/datastrato/graviton/GravitonEnv.java b/core/src/main/java/com/datastrato/graviton/GravitonEnv.javanew file mode 100644index 0000000000..b2478821c1--- /dev/null+++ b/core/src/main/java/com/datastrato/graviton/GravitonEnv.java@@ -0,0 +1,104 @@+/*+ * Copyright 2023 Datastrato.+ * This software is licensed under the Apache License version 2.+ */+package com.datastrato.graviton;++import com.datastrato.graviton.catalog.CatalogManager;+import com.datastrato.graviton.catalog.CatalogOperationDispatcher;+import com.datastrato.graviton.meta.BaseMetalakesOperations;+import org.slf4j.Logger;+import org.slf4j.LoggerFactory;++public class GravitonEnv {++ private static final Logger LOG = LoggerFactory.getLogger(GravitonEnv.class);++ private Config config;++ private EntityStore entityStore;++ private EntitySerDe entitySerDe;++ private CatalogManager catalogManager;++ private CatalogOperationDispatcher catalogOperationDispatcher;++ // TODO. We should refactor the BaseMetalakesOperations to align with other class names. @jerry+ private BaseMetalakesOperations metalakesOperations;++ private GravitonEnv() {}++ private static class InstanceHolder {+ private static final GravitonEnv INSTANCE = new GravitonEnv();+ }++ public static GravitonEnv getInstance() {+ return InstanceHolder.INSTANCE;+ }++ public void initialize(Config config) {+ LOG.info("Initializing Graviton Environment...");++ this.config = config;++ // Initialize EntitySerDe+ this.entitySerDe = EntitySerDeFactory.createEntitySerDe(config);++ // Initialize EntityStore+ this.entityStore = EntityStoreFactory.createEntityStore(config);+ entityStore.initialize(config);+ entityStore.setSerDe(entitySerDe);++ // Create and initialize metalake related modules+ this.metalakesOperations = new BaseMetalakesOperations();++ // Create and initialize Catalog related modules+ this.catalogManager = new CatalogManager(config);+ this.catalogOperationDispatcher = new CatalogOperationDispatcher(catalogManager);++ LOG.info("Graviton Environment is initialized.");+ }++ public Config config() {+ return config;+ }++ public EntitySerDe entitySerDe() {+ return entitySerDe;+ }++ public EntityStore entityStore() {+ return entityStore;+ }++ public CatalogManager catalogManager() {+ return catalogManager;+ }++ public CatalogOperationDispatcher catalogOperationDispatcher() {+ return catalogOperationDispatcher;+ }++ public BaseMetalakesOperations metalakesOperations() {+ return metalakesOperations;+ }++ public void shutdown() {+ LOG.info("Shutting down Graviton Environment...");++ if (entityStore != null) {+ try {+ entityStore.close();+ } catch (Exception e) {+ LOG.warn("Failed to close EntityStore.", e);+ }+ }++ if (catalogManager != null) {+ catalogManager.close();+ }++ LOG.info("Graviton Environment is shut down.");+ }+}diff --git a/core/src/main/java/com/datastrato/graviton/catalog/CatalogManager.java b/core/src/main/java/com/datastrato/graviton/catalog/CatalogManager.javaindex d6bb36740b..44b05f1ab4 100644--- a/core/src/main/java/com/datastrato/graviton/catalog/CatalogManager.java+++ b/core/src/main/java/com/datastrato/graviton/catalog/CatalogManager.java@@ -114,6 +114,10 @@ public class CatalogManager implements SupportsCatalogs {.build();}+ public void close() {+ catalogCache.invalidateAll();+ }+@Overridepublic NameIdentifier[] listCatalogs(Namespace namespace) throws NoSuchMetalakeException {throw new UnsupportedOperationException("Not implemented yet");diff --git a/core/src/main/java/com/datastrato/graviton/meta/BaseCatalogsOperations.java b/core/src/main/java/com/datastrato/graviton/meta/BaseCatalogsOperations.javadeleted file mode 100644index 61debc7842..0000000000--- a/core/src/main/java/com/datastrato/graviton/meta/BaseCatalogsOperations.java+++ /dev/null@@ -1,42 +0,0 @@-/*- * Copyright 2023 Datastrato.- * This software is licensed under the Apache License version 2.- */-package com.datastrato.graviton.meta;--import com.datastrato.graviton.*;-import com.datastrato.graviton.exceptions.CatalogAlreadyExistsException;-import com.datastrato.graviton.exceptions.NoSuchCatalogException;-import com.datastrato.graviton.exceptions.NoSuchMetalakeException;-import java.util.Map;--public class BaseCatalogsOperations implements SupportsCatalogs {-- @Override- public NameIdentifier[] listCatalogs(Namespace namespace) throws NoSuchMetalakeException {- throw new UnsupportedOperationException("Not implemented yet");- }-- @Override- public Catalog loadCatalog(NameIdentifier ident) throws NoSuchCatalogException {- throw new UnsupportedOperationException("Not implemented yet");- }-- @Override- public Catalog createCatalog(- NameIdentifier ident, Catalog.Type type, String comment, Map<String, String> properties)- throws NoSuchMetalakeException, CatalogAlreadyExistsException {- throw new UnsupportedOperationException("Not implemented yet");- }-- @Override- public Catalog alterCatalog(NameIdentifier ident, CatalogChange... changes)- throws NoSuchCatalogException, IllegalArgumentException {- throw new UnsupportedOperationException("Not implemented yet");- }-- @Override- public boolean dropCatalog(NameIdentifier ident) {- throw new UnsupportedOperationException("Not implemented yet");- }-}diff --git a/server/src/main/java/com/datastrato/graviton/server/GravitonServer.java b/server/src/main/java/com/datastrato/graviton/server/GravitonServer.javaindex eb38bedd58..09a3e9b7d2 100644--- a/server/src/main/java/com/datastrato/graviton/server/GravitonServer.java+++ b/server/src/main/java/com/datastrato/graviton/server/GravitonServer.java@@ -4,7 +4,8 @@*/package com.datastrato.graviton.server;-import com.datastrato.graviton.meta.BaseCatalogsOperations;+import com.datastrato.graviton.GravitonEnv;+import com.datastrato.graviton.catalog.CatalogManager;import com.datastrato.graviton.meta.BaseMetalakesOperations;import com.datastrato.graviton.server.web.JettyServer;import com.datastrato.graviton.server.web.ObjectMapperProvider;@@ -27,9 +28,12 @@ public class GravitonServer extends ResourceConfig {private final JettyServer server;+ private final GravitonEnv gravitonEnv;+public GravitonServer() {serverConfig = new ServerConfig();server = new JettyServer();+ gravitonEnv = GravitonEnv.getInstance();}public void initialize() {@@ -42,6 +46,8 @@ public class GravitonServer extends ResourceConfig {server.initialize(serverConfig);+ gravitonEnv.initialize(serverConfig);+// initialize Jersey REST API resources.initializeRestApi();}@@ -52,8 +58,8 @@ public class GravitonServer extends ResourceConfig {new AbstractBinder() {@Overrideprotected void configure() {- bind(BaseMetalakesOperations.class).to(BaseMetalakesOperations.class).ranked(1);- bind(BaseCatalogsOperations.class).to(BaseCatalogsOperations.class).ranked(1);+ bind(gravitonEnv.metalakesOperations()).to(BaseMetalakesOperations.class).ranked(1);+ bind(gravitonEnv.catalogManager()).to(CatalogManager.class).ranked(1);}});register(ObjectMapperProvider.class).register(JacksonFeature.class);@@ -73,6 +79,7 @@ public class GravitonServer extends ResourceConfig {public void stop() {server.stop();+ gravitonEnv.shutdown();}public static void main(String[] args) throws Exception {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.javaindex c4dbd5e66c..ef6d8f6f19 100644--- 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@@ -8,6 +8,7 @@ import com.datastrato.graviton.Catalog;import com.datastrato.graviton.CatalogChange;import com.datastrato.graviton.NameIdentifier;import com.datastrato.graviton.Namespace;+import com.datastrato.graviton.catalog.CatalogManager;import com.datastrato.graviton.dto.requests.CatalogCreateRequest;import com.datastrato.graviton.dto.requests.CatalogUpdateRequest;import com.datastrato.graviton.dto.requests.CatalogUpdatesRequest;@@ -17,7 +18,6 @@ import com.datastrato.graviton.dto.responses.DropResponse;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 javax.inject.Inject;import javax.servlet.http.HttpServletRequest;@@ -35,13 +35,13 @@ public class CatalogOperations {private static final Logger LOG = LoggerFactory.getLogger(CatalogOperations.class);- private final BaseCatalogsOperations ops;+ private final CatalogManager manager;@Context private HttpServletRequest httpRequest;@Inject- public CatalogOperations(BaseCatalogsOperations ops) {- this.ops = ops;+ public CatalogOperations(CatalogManager manager) {+ this.manager = manager;}@GET@@ -54,7 +54,7 @@ public class CatalogOperations {try {Namespace metalakeNS = Namespace.of(metalake);- NameIdentifier[] idents = ops.listCatalogs(metalakeNS);+ NameIdentifier[] idents = manager.listCatalogs(metalakeNS);return Utils.ok(new CatalogListResponse(idents));} catch (NoSuchMetalakeException ex) {@@ -87,7 +87,7 @@ public class CatalogOperations {try {NameIdentifier ident = NameIdentifier.of(metalake, request.getName());Catalog catalog =- ops.createCatalog(+ manager.createCatalog(ident, request.getType(), request.getComment(), request.getProperties());return Utils.ok(new CatalogResponse(DTOConverters.toDTO(catalog)));@@ -124,7 +124,7 @@ public class CatalogOperations {try {NameIdentifier ident = NameIdentifier.of(metalakeName, catalogName);- Catalog catalog = ops.loadCatalog(ident);+ Catalog catalog = manager.loadCatalog(ident);return Utils.ok(new CatalogResponse(DTOConverters.toDTO(catalog)));} catch (NoSuchMetalakeException ex) {@@ -176,7 +176,7 @@ public class CatalogOperations {.map(CatalogUpdateRequest::catalogChange).toArray(CatalogChange[]::new);- Catalog catalog = ops.alterCatalog(ident, changes);+ Catalog catalog = manager.alterCatalog(ident, changes);return Utils.ok(new CatalogResponse(DTOConverters.toDTO(catalog)));} catch (NoSuchCatalogException ex) {@@ -222,7 +222,7 @@ public class CatalogOperations {try {NameIdentifier ident = NameIdentifier.of(metalakeName, catalogName);- boolean dropped = ops.dropCatalog(ident);+ boolean dropped = manager.dropCatalog(ident);if (!dropped) {LOG.warn("Failed to drop catalog {} under metalake {}", catalogName, metalakeName);}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.javaindex 68067f0a03..843620fabe 100644--- 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@@ -10,6 +10,7 @@ import static org.mockito.Mockito.*;import com.datastrato.graviton.Catalog;import com.datastrato.graviton.NameIdentifier;import com.datastrato.graviton.Namespace;+import com.datastrato.graviton.catalog.CatalogManager;import com.datastrato.graviton.dto.CatalogDTO;import com.datastrato.graviton.dto.requests.CatalogCreateRequest;import com.datastrato.graviton.dto.requests.CatalogUpdateRequest;@@ -19,7 +20,6 @@ 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.datastrato.graviton.meta.CatalogEntity;import com.google.common.collect.ImmutableList;import com.google.common.collect.ImmutableMap;@@ -47,7 +47,7 @@ public class TestCatalogOperations extends JerseyTest {}}- private BaseCatalogsOperations ops = mock(BaseCatalogsOperations.class);+ private CatalogManager manager = mock(CatalogManager.class);@Overrideprotected Application configure() {@@ -57,7 +57,7 @@ public class TestCatalogOperations extends JerseyTest {new AbstractBinder() {@Overrideprotected void configure() {- bind(ops).to(BaseCatalogsOperations.class).ranked(2);+ bind(manager).to(CatalogManager.class).ranked(2);bindFactory(MockServletRequestFactory.class).to(HttpServletRequest.class);}});@@ -70,7 +70,7 @@ public class TestCatalogOperations extends JerseyTest {NameIdentifier ident1 = NameIdentifier.of("metalake1", "catalog1");NameIdentifier ident2 = NameIdentifier.of("metalake1", "catalog2");- when(ops.listCatalogs(any())).thenReturn(new NameIdentifier[] {ident1, ident2});+ when(manager.listCatalogs(any())).thenReturn(new NameIdentifier[] {ident1, ident2});Response resp =target("/metalakes/metalake1/catalogs")@@ -89,7 +89,7 @@ public class TestCatalogOperations extends JerseyTest {Assertions.assertEquals(ident1, idents[0]);Assertions.assertEquals(ident2, idents[1]);- doThrow(new NoSuchMetalakeException("mock error")).when(ops).listCatalogs(any());+ doThrow(new NoSuchMetalakeException("mock error")).when(manager).listCatalogs(any());Response resp1 =target("/metalakes/metalake1/catalogs").request(MediaType.APPLICATION_JSON_TYPE)@@ -111,7 +111,7 @@ public class TestCatalogOperations extends JerseyTest {"catalog1", Catalog.Type.RELATIONAL, "comment", ImmutableMap.of("key", "value"));TestCatalog catalog = buildCatalog("metalake1", "catalog1");- when(ops.createCatalog(any(), any(), any(), any())).thenReturn(catalog);+ when(manager.createCatalog(any(), any(), any(), any())).thenReturn(catalog);Response resp =target("/metalakes/metalake1/catalogs")@@ -133,7 +133,7 @@ public class TestCatalogOperations extends JerseyTest {// Test throw NoSuchMetalakeExceptiondoThrow(new NoSuchMetalakeException("mock error"))- .when(ops)+ .when(manager).createCatalog(any(), any(), any(), any());Response resp1 =target("/metalakes/metalake1/catalogs")@@ -150,7 +150,7 @@ public class TestCatalogOperations extends JerseyTest {// Test throw CatalogAlreadyExistsExceptiondoThrow(new CatalogAlreadyExistsException("mock error"))- .when(ops)+ .when(manager).createCatalog(any(), any(), any(), any());Response resp2 =target("/metalakes/metalake1/catalogs")@@ -166,7 +166,9 @@ public class TestCatalogOperations extends JerseyTest {CatalogAlreadyExistsException.class.getSimpleName(), errorResponse1.getType());// Test throw internal RuntimeException- doThrow(new RuntimeException("mock error")).when(ops).createCatalog(any(), any(), any(), any());+ doThrow(new RuntimeException("mock error"))+ .when(manager)+ .createCatalog(any(), any(), any(), any());Response resp3 =target("/metalakes/metalake1/catalogs").request(MediaType.APPLICATION_JSON_TYPE)@@ -185,7 +187,7 @@ public class TestCatalogOperations extends JerseyTest {public void testLoadCatalog() {TestCatalog catalog = buildCatalog("metalake1", "catalog1");- when(ops.loadCatalog(any())).thenReturn(catalog);+ when(manager.loadCatalog(any())).thenReturn(catalog);Response resp =target("/metalakes/metalake1/catalogs/catalog1")@@ -205,7 +207,7 @@ public class TestCatalogOperations extends JerseyTest {Assertions.assertEquals(ImmutableMap.of("key", "value"), catalogDTO.properties());// Test throw NoSuchMetalakeException- doThrow(new NoSuchMetalakeException("mock error")).when(ops).loadCatalog(any());+ doThrow(new NoSuchMetalakeException("mock error")).when(manager).loadCatalog(any());Response resp1 =target("/metalakes/metalake1/catalogs/catalog1").request(MediaType.APPLICATION_JSON_TYPE)@@ -219,7 +221,7 @@ public class TestCatalogOperations extends JerseyTest {Assertions.assertEquals(NoSuchMetalakeException.class.getSimpleName(), errorResponse.getType());// Test throw NoSuchCatalogException- doThrow(new NoSuchCatalogException("mock error")).when(ops).loadCatalog(any());+ doThrow(new NoSuchCatalogException("mock error")).when(manager).loadCatalog(any());Response resp2 =target("/metalakes/metalake1/catalogs/catalog1").request(MediaType.APPLICATION_JSON_TYPE)@@ -233,7 +235,7 @@ public class TestCatalogOperations extends JerseyTest {Assertions.assertEquals(NoSuchCatalogException.class.getSimpleName(), errorResponse1.getType());// Test throw internal RuntimeException- doThrow(new RuntimeException("mock error")).when(ops).loadCatalog(any());+ doThrow(new RuntimeException("mock error")).when(manager).loadCatalog(any());Response resp3 =target("/metalakes/metalake1/catalogs/catalog1").request(MediaType.APPLICATION_JSON_TYPE)@@ -252,7 +254,7 @@ public class TestCatalogOperations extends JerseyTest {public void testAlterCatalog() {TestCatalog catalog = buildCatalog("metalake1", "catalog2");- when(ops.alterCatalog(any(), any())).thenReturn(catalog);+ when(manager.alterCatalog(any(), any())).thenReturn(catalog);CatalogUpdateRequest updateRequest = new CatalogUpdateRequest.RenameCatalogRequest("catalog2");CatalogUpdatesRequest req = new CatalogUpdatesRequest(ImmutableList.of(updateRequest));@@ -275,7 +277,7 @@ public class TestCatalogOperations extends JerseyTest {Assertions.assertEquals(ImmutableMap.of("key", "value"), catalogDTO.properties());// Test throw NoSuchCatalogException- doThrow(new NoSuchCatalogException("mock error")).when(ops).alterCatalog(any(), any());+ doThrow(new NoSuchCatalogException("mock error")).when(manager).alterCatalog(any(), any());Response resp2 =target("/metalakes/metalake1/catalogs/catalog1").request(MediaType.APPLICATION_JSON_TYPE)@@ -289,7 +291,7 @@ public class TestCatalogOperations extends JerseyTest {Assertions.assertEquals(NoSuchCatalogException.class.getSimpleName(), errorResponse.getType());// Test throw IllegalArgumentException- doThrow(new IllegalArgumentException("mock error")).when(ops).alterCatalog(any(), any());+ doThrow(new IllegalArgumentException("mock error")).when(manager).alterCatalog(any(), any());Response resp3 =target("/metalakes/metalake1/catalogs/catalog1").request(MediaType.APPLICATION_JSON_TYPE)@@ -304,7 +306,7 @@ public class TestCatalogOperations extends JerseyTest {IllegalArgumentException.class.getSimpleName(), errorResponse1.getType());// Test throw internal RuntimeException- doThrow(new RuntimeException("mock error")).when(ops).alterCatalog(any(), any());+ doThrow(new RuntimeException("mock error")).when(manager).alterCatalog(any(), any());Response resp4 =target("/metalakes/metalake1/catalogs/catalog1").request(MediaType.APPLICATION_JSON_TYPE)@@ -321,7 +323,7 @@ public class TestCatalogOperations extends JerseyTest {@Testpublic void testDropCatalog() {- when(ops.dropCatalog(any())).thenReturn(true);+ when(manager.dropCatalog(any())).thenReturn(true);Response resp =target("/metalakes/metalake1/catalogs/catalog1")@@ -335,7 +337,7 @@ public class TestCatalogOperations extends JerseyTest {Assertions.assertTrue(dropResponse.dropped());// Test when failed to drop catalog- when(ops.dropCatalog(any())).thenReturn(false);+ when(manager.dropCatalog(any())).thenReturn(false);Response resp2 =target("/metalakes/metalake1/catalogs/catalog1")@@ -349,7 +351,7 @@ public class TestCatalogOperations extends JerseyTest {Assertions.assertFalse(dropResponse2.dropped());// Test throw internal RuntimeException- doThrow(new RuntimeException("mock error")).when(ops).dropCatalog(any());+ doThrow(new RuntimeException("mock error")).when(manager).dropCatalog(any());Response resp3 =target("/metalakes/metalake1/catalogs/catalog1").request(MediaType.APPLICATION_JSON_TYPE)