Skip to content

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

[#2795] fix(kafka-catalog): Fix Kafka catalog cannot be dropped (#2865)

mchadesabcf5f7923c8
diff --git a/catalogs/catalog-kafka/src/main/java/com/datastrato/gravitino/catalog/kafka/KafkaCatalogOperations.java b/catalogs/catalog-kafka/src/main/java/com/datastrato/gravitino/catalog/kafka/KafkaCatalogOperations.java
index db088dee5e..938c73e581 100644
--- a/catalogs/catalog-kafka/src/main/java/com/datastrato/gravitino/catalog/kafka/KafkaCatalogOperations.java
+++ b/catalogs/catalog-kafka/src/main/java/com/datastrato/gravitino/catalog/kafka/KafkaCatalogOperations.java
@@ -133,6 +133,7 @@ public class KafkaCatalogOperations implements CatalogOperations, SupportsSchema
AdminClientConfig.CLIENT_ID_CONFIG,
String.format(CLIENT_ID_TEMPLATE, config.get(ID_KEY), info.namespace(), info.name()));
+ createDefaultSchemaIfNecessary();
adminClient = AdminClient.create(adminClientConfig);
}
@@ -328,7 +329,6 @@ public class KafkaCatalogOperations implements CatalogOperations, SupportsSchema
@Override
public NameIdentifier[] listSchemas(Namespace namespace) throws NoSuchCatalogException {
- createDefaultSchemaIfNecessary();
try {
List<SchemaEntity> schemas =
store.list(namespace, SchemaEntity.class, Entity.EntityType.SCHEMA);
@@ -352,7 +352,6 @@ public class KafkaCatalogOperations implements CatalogOperations, SupportsSchema
@Override
public Schema loadSchema(NameIdentifier ident) throws NoSuchSchemaException {
- createDefaultSchemaIfNecessary();
try {
SchemaEntity schema = store.get(ident, Entity.EntityType.SCHEMA, SchemaEntity.class);
@@ -430,11 +429,6 @@ public class KafkaCatalogOperations implements CatalogOperations, SupportsSchema
* @throws NoSuchSchemaException If the schema does not exist.
*/
private void checkSchemaExists(NameIdentifier ident) throws NoSuchSchemaException {
- if (ident.equals(defaultSchemaIdent)) {
- createDefaultSchemaIfNecessary();
- return;
- }
-
if (!schemaExists(ident)) {
LOG.warn("Kafka catalog schema {} does not exist", ident);
throw new NoSuchSchemaException("Schema %s does not exist", ident);
@@ -543,7 +537,7 @@ public class KafkaCatalogOperations implements CatalogOperations, SupportsSchema
return topicConfigs;
}
- private synchronized void createDefaultSchemaIfNecessary() {
+ private void createDefaultSchemaIfNecessary() {
// If the default schema already exists, do nothing
try {
if (store.exists(defaultSchemaIdent, Entity.EntityType.SCHEMA)) {
diff --git a/catalogs/catalog-kafka/src/test/java/com/datastrato/gravitino/catalog/kafka/integration/test/CatalogKafkaIT.java b/catalogs/catalog-kafka/src/test/java/com/datastrato/gravitino/catalog/kafka/integration/test/CatalogKafkaIT.java
index 765d3364b0..5c518b7fec 100644
--- a/catalogs/catalog-kafka/src/test/java/com/datastrato/gravitino/catalog/kafka/integration/test/CatalogKafkaIT.java
+++ b/catalogs/catalog-kafka/src/test/java/com/datastrato/gravitino/catalog/kafka/integration/test/CatalogKafkaIT.java
@@ -10,9 +10,11 @@ import static com.datastrato.gravitino.catalog.kafka.KafkaTopicPropertiesMetadat
import static com.datastrato.gravitino.integration.test.container.KafkaContainer.DEFAULT_BROKER_PORT;
import com.datastrato.gravitino.Catalog;
+import com.datastrato.gravitino.CatalogChange;
import com.datastrato.gravitino.NameIdentifier;
import com.datastrato.gravitino.Namespace;
import com.datastrato.gravitino.client.GravitinoMetalake;
+import com.datastrato.gravitino.exceptions.NoSuchCatalogException;
import com.datastrato.gravitino.integration.test.container.ContainerSuite;
import com.datastrato.gravitino.integration.test.util.AbstractIT;
import com.datastrato.gravitino.integration.test.util.GravitinoITUtils;
@@ -29,6 +31,7 @@ import java.util.Map;
import java.util.concurrent.ExecutionException;
import org.apache.kafka.clients.admin.AdminClient;
import org.apache.kafka.clients.admin.Config;
+import org.apache.kafka.clients.admin.NewTopic;
import org.apache.kafka.clients.admin.TopicDescription;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRecord;
@@ -67,7 +70,7 @@ public class CatalogKafkaIT extends AbstractIT {
private static AdminClient adminClient;
@BeforeAll
- public static void startUp() {
+ public static void startUp() throws ExecutionException, InterruptedException {
CONTAINER_SUITE.startKafkaContainer();
kafkaBootstrapServers =
String.format(
@@ -75,13 +78,24 @@ public class CatalogKafkaIT extends AbstractIT {
CONTAINER_SUITE.getKafkaContainer().getContainerIpAddress(), DEFAULT_BROKER_PORT);
adminClient = AdminClient.create(ImmutableMap.of(BOOTSTRAP_SERVERS, kafkaBootstrapServers));
+ // create topics for testing
+ adminClient
+ .createTopics(
+ ImmutableList.of(
+ new NewTopic("topic1", 1, (short) 1),
+ new NewTopic("topic2", 1, (short) 1),
+ new NewTopic("topic3", 1, (short) 1)))
+ .all()
+ .get();
+
createMetalake();
- createCatalog();
+ Map<String, String> properties = Maps.newHashMap();
+ properties.put(BOOTSTRAP_SERVERS, kafkaBootstrapServers);
+ catalog = createCatalog(CATALOG_NAME, "Kafka catalog for IT", properties);
}
@AfterAll
public static void shutdown() {
- // todo: add drop catalog after it's supported
client.dropMetalake(NameIdentifier.of(METALAKE_NAME));
if (adminClient != null) {
adminClient.close();
@@ -94,6 +108,43 @@ public class CatalogKafkaIT extends AbstractIT {
}
}
+ @Test
+ public void testCatalog() throws ExecutionException, InterruptedException {
+ // test create catalog
+ String catalogName = GravitinoITUtils.genRandomName("test-catalog");
+ String comment = "test catalog";
+ Map<String, String> properties =
+ ImmutableMap.of(BOOTSTRAP_SERVERS, kafkaBootstrapServers, "key1", "value1");
+ Catalog createdCatalog = createCatalog(catalogName, comment, properties);
+ Assertions.assertEquals(catalogName, createdCatalog.name());
+ Assertions.assertEquals(comment, createdCatalog.comment());
+ Assertions.assertEquals(properties, createdCatalog.properties());
+
+ // test load catalog
+ Catalog loadedCatalog = metalake.loadCatalog(NameIdentifier.of(METALAKE_NAME, catalogName));
+ Assertions.assertEquals(createdCatalog, loadedCatalog);
+
+ // test alter catalog
+ Catalog alteredCatalog =
+ metalake.alterCatalog(
+ NameIdentifier.of(METALAKE_NAME, catalogName),
+ CatalogChange.updateComment("new comment"),
+ CatalogChange.removeProperty("key1"));
+ Assertions.assertEquals("new comment", alteredCatalog.comment());
+ Assertions.assertFalse(alteredCatalog.properties().containsKey("key1"));
+
+ // test drop catalog
+ boolean dropped = metalake.dropCatalog(NameIdentifier.of(METALAKE_NAME, catalogName));
+ Assertions.assertTrue(dropped);
+ Exception exception =
+ Assertions.assertThrows(
+ NoSuchCatalogException.class,
+ () -> metalake.loadCatalog(NameIdentifier.of(METALAKE_NAME, catalogName)));
+ Assertions.assertTrue(exception.getMessage().contains(catalogName));
+ // assert topic exists in Kafka after catalog dropped
+ Assertions.assertFalse(adminClient.listTopics().names().get().isEmpty());
+ }
+
@Test
public void testDefaultSchema() {
NameIdentifier[] schemas =
@@ -181,8 +232,9 @@ public class CatalogKafkaIT extends AbstractIT {
catalog
.asTopicCatalog()
.listTopics(Namespace.ofTopic(METALAKE_NAME, CATALOG_NAME, DEFAULT_SCHEMA_NAME));
- Assertions.assertEquals(1, topics.length);
- Assertions.assertEquals(topicName, topics[0].name());
+ Assertions.assertTrue(topics.length > 0);
+ Assertions.assertTrue(
+ ImmutableList.copyOf(topics).stream().anyMatch(topic -> topic.name().equals(topicName)));
}
@Test
@@ -353,15 +405,14 @@ public class CatalogKafkaIT extends AbstractIT {
metalake = loadMetalake;
}
- private static void createCatalog() {
- Map<String, String> properties = Maps.newHashMap();
- properties.put(BOOTSTRAP_SERVERS, kafkaBootstrapServers);
+ private static Catalog createCatalog(
+ String catalogName, String comment, Map<String, String> properties) {
metalake.createCatalog(
- NameIdentifier.of(METALAKE_NAME, CATALOG_NAME),
+ NameIdentifier.of(METALAKE_NAME, catalogName),
Catalog.Type.MESSAGING,
PROVIDER,
- "comment",
+ comment,
properties);
- catalog = metalake.loadCatalog(NameIdentifier.of(METALAKE_NAME, CATALOG_NAME));
+ return metalake.loadCatalog(NameIdentifier.of(METALAKE_NAME, catalogName));
}
}
diff --git a/core/src/main/java/com/datastrato/gravitino/catalog/CatalogManager.java b/core/src/main/java/com/datastrato/gravitino/catalog/CatalogManager.java
index f70d1f8d17..f374ce6e20 100644
--- a/core/src/main/java/com/datastrato/gravitino/catalog/CatalogManager.java
+++ b/core/src/main/java/com/datastrato/gravitino/catalog/CatalogManager.java
@@ -33,6 +33,7 @@ import com.datastrato.gravitino.file.FilesetCatalog;
import com.datastrato.gravitino.messaging.TopicCatalog;
import com.datastrato.gravitino.meta.AuditInfo;
import com.datastrato.gravitino.meta.CatalogEntity;
+import com.datastrato.gravitino.meta.SchemaEntity;
import com.datastrato.gravitino.rel.SupportsSchemas;
import com.datastrato.gravitino.rel.TableCatalog;
import com.datastrato.gravitino.storage.IdGenerator;
@@ -318,6 +319,7 @@ public class CatalogManager implements SupportsCatalogs, Closeable {
.build())
.build();
+ boolean createSuccess = false;
try {
NameIdentifier metalakeIdent = NameIdentifier.of(ident.namespace().levels());
if (!store.exists(metalakeIdent, EntityType.METALAKE)) {
@@ -325,9 +327,9 @@ public class CatalogManager implements SupportsCatalogs, Closeable {
throw new NoSuchMetalakeException(METALAKE_DOES_NOT_EXIST_MSG, metalakeIdent);
}
- // TODO: should avoid a race condition here
- CatalogWrapper wrapper = catalogCache.get(ident, id -> createCatalogWrapper(e));
store.put(e, false /* overwrite */);
+ CatalogWrapper wrapper = catalogCache.get(ident, id -> createCatalogWrapper(e));
+ createSuccess = true;
return wrapper.catalog;
} catch (EntityAlreadyExistsException e1) {
LOG.warn("Catalog {} already exists", ident, e1);
@@ -338,6 +340,14 @@ public class CatalogManager implements SupportsCatalogs, Closeable {
catalogCache.invalidate(ident);
LOG.error("Failed to create catalog {}", ident, e3);
throw new RuntimeException(e3);
+ } finally {
+ if (!createSuccess) {
+ try {
+ store.delete(ident, EntityType.CATALOG);
+ } catch (IOException e4) {
+ LOG.error("Failed to clean up catalog {}", ident, e4);
+ }
+ }
}
}
@@ -461,7 +471,23 @@ public class CatalogManager implements SupportsCatalogs, Closeable {
catalogCache.invalidate(ident);
try {
+ CatalogEntity catalogEntity = store.get(ident, EntityType.CATALOG, CatalogEntity.class);
+ if (catalogEntity.getProvider().equals("kafka")) {
+ // Kafka catalog needs to cascade drop the default schema
+ List<SchemaEntity> schemas =
+ store.list(
+ Namespace.ofSchema(ident.namespace().level(0), ident.name()),
+ SchemaEntity.class,
+ EntityType.SCHEMA);
+ // If there is only one schema, it must be the default schema, because we don't allow to
+ // drop the default schema.
+ if (schemas.size() == 1) {
+ return store.delete(ident, EntityType.CATALOG, true);
+ }
+ }
return store.delete(ident, EntityType.CATALOG);
+ } catch (NoSuchEntityException e) {
+ return false;
} catch (IOException ioe) {
LOG.error("Failed to drop catalog {}", ident, ioe);
throw new RuntimeException(ioe);