Skip to content

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

[#1527] feat(hadoop-catalog): Add fileset support for hadoop catalog (#1647)

Jerry Shao58883283d6d2
diff --git a/api/src/main/java/com/datastrato/gravitino/file/Fileset.java b/api/src/main/java/com/datastrato/gravitino/file/Fileset.java
index 88ffe9d677..64a8f4d1da 100644
--- a/api/src/main/java/com/datastrato/gravitino/file/Fileset.java
+++ b/api/src/main/java/com/datastrato/gravitino/file/Fileset.java
@@ -46,6 +46,31 @@ public interface Fileset extends Auditable {
/**
* Get the storage location of the file or directory path that is managed by this fileset object.
*
+ * <p>The returned storageLocation can either be the one specified when creating the fileset
+ * object, or the one specified in the catalog / schema level if the fileset object is created
+ * under this catalog / schema.
+ *
+ * <p>For managed fileset, the storageLocation can be:
+ *
+ * <p>1) The one specified when creating the fileset object.
+ *
+ * <p>2) When catalog property "location" is specified but schema property "location" is not
+ * specified, then the storageLocation will be "{catalog location}/schemaName/filesetName".
+ *
+ * <p>3) When catalog property "location" is not specified but schema property "location" is
+ * specified, then the storageLocation will be "{schema location}/filesetName".
+ *
+ * <p>4) When both catalog property "location" and schema property "location" are specified, then
+ * the storageLocation will be "{schema location}/filesetName".
+ *
+ * <p>5) When both catalog property "location" and schema property "location" are not specified,
+ * and storageLocation specified when creating the fileset object is null, this situation is
+ * illegal.
+ *
+ * <p>For external fileset, the storageLocation can be:
+ *
+ * <p>1) The one specified when creating the fileset object.
+ *
* @return The storage location of the fileset object.
*/
String storageLocation();
diff --git a/api/src/main/java/com/datastrato/gravitino/file/FilesetCatalog.java b/api/src/main/java/com/datastrato/gravitino/file/FilesetCatalog.java
index 3054ea1451..603bd51d18 100644
--- a/api/src/main/java/com/datastrato/gravitino/file/FilesetCatalog.java
+++ b/api/src/main/java/com/datastrato/gravitino/file/FilesetCatalog.java
@@ -9,6 +9,7 @@ import com.datastrato.gravitino.Namespace;
import com.datastrato.gravitino.exceptions.FilesetAlreadyExistsException;
import com.datastrato.gravitino.exceptions.NoSuchFilesetException;
import com.datastrato.gravitino.exceptions.NoSuchSchemaException;
+import com.datastrato.gravitino.file.FilesetChange.RenameFileset;
import java.util.Map;
/**
@@ -81,6 +82,9 @@ public interface FilesetCatalog {
* <p>Implementation may reject the change. If any change is rejected, no changes should be
* applied to the fileset.
*
+ * <p>The {@link RenameFileset} change will only update the fileset name, the underlying storage
+ * location for managed fileset will not be renamed.
+ *
* @param ident A fileset identifier.
* @param changes The changes to apply to the fileset.
* @return The altered fileset metadata.
diff --git a/catalogs/catalog-hadoop/build.gradle.kts b/catalogs/catalog-hadoop/build.gradle.kts
index 9ddde02d14..0a632635a9 100644
--- a/catalogs/catalog-hadoop/build.gradle.kts
+++ b/catalogs/catalog-hadoop/build.gradle.kts
@@ -21,6 +21,7 @@ dependencies {
implementation(libs.hadoop2.hdfs)
testImplementation(libs.junit.jupiter.api)
+ testImplementation(libs.junit.jupiter.params)
testRuntimeOnly(libs.junit.jupiter.engine)
testImplementation(libs.mockito.core)
}
diff --git a/catalogs/catalog-hadoop/src/main/java/com/datastrato/gravitino/catalog/hadoop/HadoopCatalogOperations.java b/catalogs/catalog-hadoop/src/main/java/com/datastrato/gravitino/catalog/hadoop/HadoopCatalogOperations.java
index 011ec879c1..8ea294697c 100644
--- a/catalogs/catalog-hadoop/src/main/java/com/datastrato/gravitino/catalog/hadoop/HadoopCatalogOperations.java
+++ b/catalogs/catalog-hadoop/src/main/java/com/datastrato/gravitino/catalog/hadoop/HadoopCatalogOperations.java
@@ -27,11 +27,11 @@ import com.datastrato.gravitino.file.FilesetCatalog;
import com.datastrato.gravitino.file.FilesetChange;
import com.datastrato.gravitino.meta.AuditInfo;
import com.datastrato.gravitino.meta.CatalogEntity;
+import com.datastrato.gravitino.meta.FilesetEntity;
import com.datastrato.gravitino.meta.SchemaEntity;
import com.datastrato.gravitino.rel.Schema;
import com.datastrato.gravitino.rel.SchemaChange;
import com.datastrato.gravitino.rel.SupportsSchemas;
-import com.datastrato.gravitino.storage.IdGenerator;
import com.datastrato.gravitino.utils.PrincipalUtils;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
@@ -44,6 +44,7 @@ import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
+import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
@@ -67,22 +68,18 @@ public class HadoopCatalogOperations implements CatalogOperations, SupportsSchem
private final EntityStore store;
- private final IdGenerator idGenerator;
-
@VisibleForTesting Configuration hadoopConf;
@VisibleForTesting Optional<Path> catalogStorageLocation;
// For testing only.
- HadoopCatalogOperations(CatalogEntity entity, EntityStore store, IdGenerator idGenerator) {
+ HadoopCatalogOperations(CatalogEntity entity, EntityStore store) {
this.entity = entity;
this.store = store;
- this.idGenerator = idGenerator;
}
public HadoopCatalogOperations(CatalogEntity entity) {
- this(
- entity, GravitinoEnv.getInstance().entityStore(), GravitinoEnv.getInstance().idGenerator());
+ this(entity, GravitinoEnv.getInstance().entityStore());
}
@Override
@@ -107,12 +104,37 @@ public class HadoopCatalogOperations implements CatalogOperations, SupportsSchem
@Override
public NameIdentifier[] listFilesets(Namespace namespace) throws NoSuchSchemaException {
- throw new UnsupportedOperationException();
+ try {
+ List<FilesetEntity> filesets =
+ store.list(namespace, FilesetEntity.class, Entity.EntityType.FILESET);
+ return filesets.stream()
+ .map(f -> NameIdentifier.of(namespace, f.name()))
+ .toArray(NameIdentifier[]::new);
+ } catch (IOException e) {
+ throw new RuntimeException("Failed to list filesets under namespace " + namespace, e);
+ }
}
@Override
public Fileset loadFileset(NameIdentifier ident) throws NoSuchFilesetException {
- throw new UnsupportedOperationException();
+ try {
+ FilesetEntity filesetEntity =
+ store.get(ident, Entity.EntityType.FILESET, FilesetEntity.class);
+
+ return new HadoopFileset.Builder()
+ .withName(ident.name())
+ .withType(filesetEntity.filesetType())
+ .withComment(filesetEntity.comment())
+ .withStorageLocation(filesetEntity.storageLocation())
+ .withProperties(filesetEntity.properties())
+ .withAuditInfo(filesetEntity.auditInfo())
+ .build();
+
+ } catch (NoSuchEntityException exception) {
+ throw new NoSuchFilesetException("Fileset " + ident + " does not exist", exception);
+ } catch (IOException ioe) {
+ throw new RuntimeException("Failed to load fileset " + ident, ioe);
+ }
}
@Override
@@ -123,18 +145,168 @@ public class HadoopCatalogOperations implements CatalogOperations, SupportsSchem
String storageLocation,
Map<String, String> properties)
throws NoSuchSchemaException, FilesetAlreadyExistsException {
- throw new UnsupportedOperationException();
+ try {
+ if (store.exists(ident, Entity.EntityType.FILESET)) {
+ throw new FilesetAlreadyExistsException("Fileset " + ident + " already exists");
+ }
+ } catch (IOException ioe) {
+ throw new RuntimeException("Failed to check if fileset " + ident + " exists", ioe);
+ }
+
+ SchemaEntity schemaEntity;
+ NameIdentifier schemaIdent = NameIdentifier.of(ident.namespace().levels());
+ try {
+ schemaEntity = store.get(schemaIdent, Entity.EntityType.SCHEMA, SchemaEntity.class);
+ } catch (NoSuchEntityException exception) {
+ throw new NoSuchSchemaException("Schema " + schemaIdent + " does not exist", exception);
+ } catch (IOException ioe) {
+ throw new RuntimeException("Failed to load schema " + schemaIdent, ioe);
+ }
+
+ // For external fileset, the storageLocation must be set.
+ if (type == Fileset.Type.EXTERNAL && StringUtils.isBlank(storageLocation)) {
+ throw new IllegalArgumentException(
+ "Storage location must be set for external fileset " + ident);
+ }
+
+ // Either catalog property "location", or schema property "location", or storageLocation must be
+ // set for managed fileset.
+ Path schemaPath = getSchemaPath(schemaIdent.name(), schemaEntity.properties());
+ if (schemaPath == null && StringUtils.isBlank(storageLocation)) {
+ throw new IllegalArgumentException(
+ "Storage location must be set for fileset "
+ + ident
+ + " when it's catalog and schema location are not set");
+ }
+
+ // The specified storageLocation will take precedence over the calculated one.
+ Path filesetPath =
+ StringUtils.isNotBlank(storageLocation)
+ ? new Path(storageLocation)
+ : new Path(schemaPath, ident.name());
+ try {
+ FileSystem fs = filesetPath.getFileSystem(hadoopConf);
+ if (!fs.exists(filesetPath)) {
+ if (!fs.mkdirs(filesetPath)) {
+ throw new RuntimeException(
+ "Failed to create fileset " + ident + " location " + filesetPath);
+ }
+
+ LOG.info("Created fileset {} location {}", ident, filesetPath);
+ } else {
+ LOG.info("Fileset {} manages the existing location {}", ident, filesetPath);
+ }
+
+ } catch (IOException ioe) {
+ throw new RuntimeException(
+ "Failed to create fileset " + ident + " location " + filesetPath, ioe);
+ }
+
+ StringIdentifier stringId = StringIdentifier.fromProperties(properties);
+ Preconditions.checkArgument(stringId != null, "Property String identifier should not be null");
+
+ FilesetEntity filesetEntity =
+ new FilesetEntity.Builder()
+ .withName(ident.name())
+ .withId(stringId.id())
+ .withNamespace(ident.namespace())
+ .withComment(comment)
+ .withFilesetType(type)
+ // Store the storageLocation to the store. If the "storageLocation" is null for
+ // managed fileset, Gravitino will get and store the location based on the
+ // catalog/schema's location and store it to the store.
+ .withStorageLocation(filesetPath.toString())
+ .withProperties(addManagedFlagToProperties(properties))
+ .withAuditInfo(
+ new AuditInfo.Builder()
+ .withCreator(PrincipalUtils.getCurrentPrincipal().getName())
+ .withCreateTime(Instant.now())
+ .build())
+ .build();
+
+ try {
+ store.put(filesetEntity, true /* overwrite */);
+ } catch (IOException ioe) {
+ throw new RuntimeException("Failed to create fileset " + ident, ioe);
+ }
+
+ return new HadoopFileset.Builder()
+ .withName(ident.name())
+ .withComment(comment)
+ .withType(type)
+ .withStorageLocation(filesetPath.toString())
+ .withProperties(filesetEntity.properties())
+ .withAuditInfo(filesetEntity.auditInfo())
+ .build();
}
@Override
public Fileset alterFileset(NameIdentifier ident, FilesetChange... changes)
throws NoSuchFilesetException, IllegalArgumentException {
- throw new UnsupportedOperationException();
+ try {
+ if (!store.exists(ident, Entity.EntityType.FILESET)) {
+ throw new NoSuchFilesetException("Fileset " + ident + " does not exist");
+ }
+ } catch (IOException ioe) {
+ throw new RuntimeException("Failed to load fileset " + ident, ioe);
+ }
+
+ try {
+ FilesetEntity updatedFilesetEntity =
+ store.update(
+ ident,
+ FilesetEntity.class,
+ Entity.EntityType.FILESET,
+ e -> updateFilesetEntity(ident, e, changes));
+
+ return new HadoopFileset.Builder()
+ .withName(updatedFilesetEntity.name())
+ .withComment(updatedFilesetEntity.comment())
+ .withType(updatedFilesetEntity.filesetType())
+ .withStorageLocation(updatedFilesetEntity.storageLocation())
+ .withProperties(updatedFilesetEntity.properties())
+ .withAuditInfo(updatedFilesetEntity.auditInfo())
+ .build();
+
+ } catch (IOException ioe) {
+ throw new RuntimeException("Failed to update fileset " + ident, ioe);
+ } catch (NoSuchEntityException nsee) {
+ throw new NoSuchFilesetException("Fileset " + ident + " does not exist", nsee);
+ } catch (AlreadyExistsException aee) {
+ // This is happened when renaming a fileset to an existing fileset name.
+ throw new RuntimeException(
+ "Fileset with the same name " + ident.name() + " already exists", aee);
+ }
}
@Override
public boolean dropFileset(NameIdentifier ident) {
- throw new UnsupportedOperationException();
+ try {
+ FilesetEntity filesetEntity =
+ store.get(ident, Entity.EntityType.FILESET, FilesetEntity.class);
+ Path filesetPath = new Path(filesetEntity.storageLocation());
+
+ // For managed fileset, we should delete the related files.
+ if (filesetEntity.filesetType() == Fileset.Type.MANAGED) {
+ FileSystem fs = filesetPath.getFileSystem(hadoopConf);
+ if (fs.exists(filesetPath)) {
+ if (!fs.delete(filesetPath, true)) {
+ LOG.warn("Failed to delete fileset {} location {}", ident, filesetPath);
+ return false;
+ }
+
+ } else {
+ LOG.warn("Fileset {} location {} does not exist", ident, filesetPath);
+ }
+ }
+
+ return store.delete(ident, Entity.EntityType.FILESET);
+ } catch (NoSuchEntityException ne) {
+ LOG.warn("Fileset {} does not exist", ident);
+ return false;
+ } catch (IOException ioe) {
+ throw new RuntimeException("Failed to delete fileset " + ident, ioe);
+ }
}
@Override
@@ -155,40 +327,31 @@ public class HadoopCatalogOperations implements CatalogOperations, SupportsSchem
throws NoSuchCatalogException, SchemaAlreadyExistsException {
try {
if (store.exists(ident, Entity.EntityType.SCHEMA)) {
- throw new SchemaAlreadyExistsException("Schema " + ident.name() + " already exists");
+ throw new SchemaAlreadyExistsException("Schema " + ident + " already exists");
}
} catch (IOException ioe) {
- throw new RuntimeException("Failed to check if schema " + ident.name() + " exists", ioe);
+ throw new RuntimeException("Failed to check if schema " + ident + " exists", ioe);
}
- String schemaLocation =
- (String)
- SCHEMA_PROPERTIES_METADATA.getOrDefault(
- properties, HadoopSchemaPropertiesMetadata.LOCATION);
- Path schemaPath =
- Optional.ofNullable(schemaLocation)
- .map(Path::new)
- .orElse(catalogStorageLocation.map(p -> new Path(p, ident.name())).orElse(null));
-
+ Path schemaPath = getSchemaPath(ident.name(), properties);
if (schemaPath != null) {
try {
FileSystem fs = schemaPath.getFileSystem(hadoopConf);
- if (fs.exists(schemaPath)) {
- throw new SchemaAlreadyExistsException(
- "Schema " + ident.name() + " location " + schemaPath + " already exists");
+ if (!fs.exists(schemaPath)) {
+ if (!fs.mkdirs(schemaPath)) {
+ // Fail the operation when failed to create the schema path.
+ throw new RuntimeException(
+ "Failed to create schema " + ident + " location " + schemaPath);
+ }
+ LOG.info("Created schema {} location {}", ident, schemaPath);
+ } else {
+ LOG.info("Schema {} manages the existing location {}", ident, schemaPath);
}
- if (!fs.mkdirs(schemaPath)) {
- // Fail the operation when failed to create the schema path.
- throw new RuntimeException(
- "Failed to create schema " + ident.name() + " location " + schemaPath);
- }
} catch (IOException ioe) {
throw new RuntimeException(
- "Failed to create schema " + ident.name() + " location " + schemaPath, ioe);
+ "Failed to create schema " + ident + " location " + schemaPath, ioe);
}
-
- LOG.info("Created schema {} location {}", ident.name(), schemaPath);
}
StringIdentifier stringId = StringIdentifier.fromProperties(properties);
@@ -210,7 +373,7 @@ public class HadoopCatalogOperations implements CatalogOperations, SupportsSchem
try {
store.put(schemaEntity, true /* overwrite */);
} catch (IOException ioe) {
- throw new RuntimeException("Failed to create schema " + ident.name(), ioe);
+ throw new RuntimeException("Failed to create schema " + ident, ioe);
}
return new HadoopSchema.Builder()
@@ -234,9 +397,9 @@ public class HadoopCatalogOperations implements CatalogOperations, SupportsSchem
.build();
} catch (NoSuchEntityException exception) {
- throw new NoSuchSchemaException("Schema " + ident.name() + " does not exist", exception);
+ throw new NoSuchSchemaException("Schema " + ident + " does not exist", exception);
} catch (IOException ioe) {
- throw new RuntimeException("Failed to load schema " + ident.name(), ioe);
+ throw new RuntimeException("Failed to load schema " + ident, ioe);
}
}
@@ -245,10 +408,10 @@ public class HadoopCatalogOperations implements CatalogOperations, SupportsSchem
throws NoSuchSchemaException {
try {
if (!store.exists(ident, Entity.EntityType.SCHEMA)) {
- throw new NoSuchSchemaException("Schema " + ident.name() + " does not exist");
+ throw new NoSuchSchemaException("Schema " + ident + " does not exist");
}
} catch (IOException ioe) {
- throw new RuntimeException("Failed to check if schema " + ident.name() + " exists", ioe);
+ throw new RuntimeException("Failed to check if schema " + ident + " exists", ioe);
}
try {
@@ -267,12 +430,12 @@ public class HadoopCatalogOperations implements CatalogOperations, SupportsSchem
.build();
} catch (IOException ioe) {
- throw new RuntimeException("Failed to update schema " + ident.name(), ioe);
+ throw new RuntimeException("Failed to update schema " + ident, ioe);
} catch (NoSuchEntityException nsee) {
- throw new NoSuchSchemaException("Schema " + ident.name() + " does not exist", nsee);
+ throw new NoSuchSchemaException("Schema " + ident + " does not exist", nsee);
} catch (AlreadyExistsException aee) {
throw new RuntimeException(
- "Schema "
+ "Schema with the same name "
+ ident.name()
+ " already exists, this is unexpected because schema doesn't support rename",
aee);
@@ -286,15 +449,7 @@ public class HadoopCatalogOperations implements CatalogOperations, SupportsSchem
Map<String, String> properties =
Optional.ofNullable(schemaEntity.properties()).orElse(Collections.emptyMap());
- String schemaLocation =
- (String)
- SCHEMA_PROPERTIES_METADATA.getOrDefault(
- properties, HadoopSchemaPropertiesMetadata.LOCATION);
- Path schemaPath =
- Optional.ofNullable(schemaLocation)
- .map(Path::new)
- .orElse(catalogStorageLocation.map(p -> new Path(p, ident.name())).orElse(null));
-
+ Path schemaPath = getSchemaPath(ident.name(), properties);
// Nothing to delete if the schema path is not set.
if (schemaPath == null) {
return true;
@@ -308,16 +463,16 @@ public class HadoopCatalogOperations implements CatalogOperations, SupportsSchem
if (fs.listStatus(schemaPath).length > 0 && !cascade) {
throw new NonEmptySchemaException(
- "Schema " + ident.name() + " with location " + schemaPath + " is not empty");
+ "Schema " + ident + " with location " + schemaPath + " is not empty");
} else {
fs.delete(schemaPath, true);
}
- LOG.info("Deleted schema {} location {}", ident.name(), schemaPath);
+ LOG.info("Deleted schema {} location {}", ident, schemaPath);
return true;
} catch (IOException ioe) {
- throw new RuntimeException("Failed to delete schema " + ident.name() + " location", ioe);
+ throw new RuntimeException("Failed to delete schema " + ident + " location", ioe);
}
}
@@ -354,7 +509,10 @@ public class HadoopCatalogOperations implements CatalogOperations, SupportsSchem
private SchemaEntity updateSchemaEntity(
NameIdentifier ident, SchemaEntity schemaEntity, SchemaChange... changes) {
- Map<String, String> props = Maps.newHashMap(schemaEntity.properties());
+ Map<String, String> props =
+ schemaEntity.properties() == null
+ ? Maps.newHashMap()
+ : Maps.newHashMap(schemaEntity.properties());
for (SchemaChange change : changes) {
if (change instanceof SchemaChange.SetProperty) {
@@ -380,8 +538,60 @@ public class HadoopCatalogOperations implements CatalogOperations, SupportsSchem
.withCreator(schemaEntity.auditInfo().creator())
.withCreateTime(schemaEntity.auditInfo().createTime())
.withLastModifier(PrincipalUtils.getCurrentPrincipal().getName())
- .withCreateTime(Instant.now())
+ .withLastModifiedTime(Instant.now())
+ .build())
+ .build();
+ }
+
+ private FilesetEntity updateFilesetEntity(
+ NameIdentifier ident, FilesetEntity filesetEntity, FilesetChange... changes) {
+ Map<String, String> props =
+ filesetEntity.properties() == null
+ ? Maps.newHashMap()
+ : Maps.newHashMap(filesetEntity.properties());
+ String newName = ident.name();
+
+ for (FilesetChange change : changes) {
+ if (change instanceof FilesetChange.SetProperty) {
+ FilesetChange.SetProperty setProperty = (FilesetChange.SetProperty) change;
+ props.put(setProperty.getProperty(), setProperty.getValue());
+ } else if (change instanceof FilesetChange.RemoveProperty) {
+ FilesetChange.RemoveProperty removeProperty = (FilesetChange.RemoveProperty) change;
+ props.remove(removeProperty.getProperty());
+ } else if (change instanceof FilesetChange.RenameFileset) {
+ newName = ((FilesetChange.RenameFileset) change).getNewName();
+ } else {
+ throw new IllegalArgumentException(
+ "Unsupported fileset change: " + change.getClass().getSimpleName());
+ }
+ }
+
+ return new FilesetEntity.Builder()
+ .withName(newName)
+ .withNamespace(ident.namespace())
+ .withId(filesetEntity.id())
+ .withComment(filesetEntity.comment())
+ .withFilesetType(filesetEntity.filesetType())
+ .withStorageLocation(filesetEntity.storageLocation())
+ .withProperties(props)
+ .withAuditInfo(
+ new AuditInfo.Builder()
+ .withCreator(filesetEntity.auditInfo().creator())
+ .withCreateTime(filesetEntity.auditInfo().createTime())
+ .withLastModifier(PrincipalUtils.getCurrentPrincipal().getName())
+ .withLastModifiedTime(Instant.now())
.build())
.build();
}
+
+ private Path getSchemaPath(String name, Map<String, String> properties) {
+ String schemaLocation =
+ (String)
+ SCHEMA_PROPERTIES_METADATA.getOrDefault(
+ properties, HadoopSchemaPropertiesMetadata.LOCATION);
+
+ return Optional.ofNullable(schemaLocation)
+ .map(Path::new)
+ .orElse(catalogStorageLocation.map(p -> new Path(p, name)).orElse(null));
+ }
}
diff --git a/catalogs/catalog-hadoop/src/main/java/com/datastrato/gravitino/catalog/hadoop/HadoopFileset.java b/catalogs/catalog-hadoop/src/main/java/com/datastrato/gravitino/catalog/hadoop/HadoopFileset.java
new file mode 100644
index 0000000000..b1140e72ce
--- /dev/null
+++ b/catalogs/catalog-hadoop/src/main/java/com/datastrato/gravitino/catalog/hadoop/HadoopFileset.java
@@ -0,0 +1,25 @@
+/*
+ * Copyright 2024 Datastrato Pvt Ltd.
+ * This software is licensed under the Apache License version 2.
+ */
+package com.datastrato.gravitino.catalog.hadoop;
+
+import com.datastrato.gravitino.catalog.file.BaseFileset;
+
+public class HadoopFileset extends BaseFileset {
+
+ public static class Builder extends BaseFilesetBuilder<Builder, HadoopFileset> {
+
+ @Override
+ protected HadoopFileset internalBuild() {
+ HadoopFileset fileset = new HadoopFileset();
+ fileset.name = name;
+ fileset.comment = comment;
+ fileset.storageLocation = storageLocation;
+ fileset.type = type;
+ fileset.properties = properties;
+ fileset.auditInfo = auditInfo;
+ return fileset;
+ }
+ }
+}
diff --git a/catalogs/catalog-hadoop/src/test/java/com/datastrato/gravitino/catalog/hadoop/TestHadoopCatalogOperations.java b/catalogs/catalog-hadoop/src/test/java/com/datastrato/gravitino/catalog/hadoop/TestHadoopCatalogOperations.java
index 8017062e28..d9e1f77d79 100644
--- a/catalogs/catalog-hadoop/src/test/java/com/datastrato/gravitino/catalog/hadoop/TestHadoopCatalogOperations.java
+++ b/catalogs/catalog-hadoop/src/test/java/com/datastrato/gravitino/catalog/hadoop/TestHadoopCatalogOperations.java
@@ -21,9 +21,12 @@ import com.datastrato.gravitino.NameIdentifier;
import com.datastrato.gravitino.Namespace;
import com.datastrato.gravitino.StringIdentifier;
import com.datastrato.gravitino.catalog.BaseCatalogPropertiesMetadata;
+import com.datastrato.gravitino.exceptions.NoSuchFilesetException;
import com.datastrato.gravitino.exceptions.NoSuchSchemaException;
import com.datastrato.gravitino.exceptions.NonEmptySchemaException;
import com.datastrato.gravitino.exceptions.SchemaAlreadyExistsException;
+import com.datastrato.gravitino.file.Fileset;
+import com.datastrato.gravitino.file.FilesetChange;
import com.datastrato.gravitino.rel.Schema;
import com.datastrato.gravitino.rel.SchemaChange;
import com.datastrato.gravitino.storage.IdGenerator;
@@ -36,6 +39,7 @@ import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
+import java.util.stream.Stream;
import org.apache.commons.io.FileUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
@@ -44,6 +48,9 @@ import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.Mockito;
public class TestHadoopCatalogOperations {
@@ -52,7 +59,7 @@ public class TestHadoopCatalogOperations {
"/tmp/gravitino_test_entityStore_" + UUID.randomUUID().toString().replace("-", "");
private static final String TEST_ROOT_PATH =
- "file:///tmp/gravitino_test_catalog_" + UUID.randomUUID().toString().replace("-", "");
+ "file:/tmp/gravitino_test_catalog_" + UUID.randomUUID().toString().replace("-", "");
private static EntityStore store;
@@ -88,7 +95,7 @@ public class TestHadoopCatalogOperations {
@Test
public void testHadoopCatalogConfiguration() {
Map<String, String> emptyProps = Maps.newHashMap();
- HadoopCatalogOperations ops = new HadoopCatalogOperations(null, store, idGenerator);
+ HadoopCatalogOperations ops = new HadoopCatalogOperations(null, store);
ops.initialize(emptyProps);
Configuration conf = ops.hadoopConf;
String value = conf.get("fs.defaultFS");
@@ -125,7 +132,7 @@ public class TestHadoopCatalogOperations {
Throwable exception =
Assertions.assertThrows(
SchemaAlreadyExistsException.class, () -> createSchema(name, comment, null, null));
- Assertions.assertEquals("Schema schema11 already exists", exception.getMessage());
+ Assertions.assertEquals("Schema m1.c1.schema11 already exists", exception.getMessage());
}
@Test
@@ -157,15 +164,6 @@ public class TestHadoopCatalogOperations {
Assertions.assertTrue(fs.exists(schemaPath1));
Assertions.assertTrue(fs.isDirectory(schemaPath1));
Assertions.assertTrue(fs.listStatus(schemaPath1).length == 0);
-
- // Test create schema on the same location
- String name1 = "schema13_1";
- Throwable exception =
- Assertions.assertThrows(
- SchemaAlreadyExistsException.class,
- () -> createSchema(name1, comment, null, schemaPath));
- Assertions.assertEquals(
- "Schema schema13_1 location " + schemaPath1 + " already exists", exception.getMessage());
}
@Test
@@ -195,7 +193,7 @@ public class TestHadoopCatalogOperations {
Schema schema = createSchema(name, comment, catalogPath, null);
Assertions.assertEquals(name, schema.name());
- try (HadoopCatalogOperations ops = new HadoopCatalogOperations(null, store, idGenerator)) {
+ try (HadoopCatalogOperations ops = new HadoopCatalogOperations(null, store)) {
ops.initialize(Maps.newHashMap());
Schema schema1 = ops.loadSchema(NameIdentifier.ofSchema("m1", "c1", name));
Assertions.assertEquals(name, schema1.name());
@@ -212,7 +210,7 @@ public class TestHadoopCatalogOperations {
Assertions.assertThrows(
NoSuchSchemaException.class,
() -> ops.loadSchema(NameIdentifier.ofSchema("m1", "c1", "schema16")));
- Assertions.assertEquals("Schema schema16 does not exist", exception.getMessage());
+ Assertions.assertEquals("Schema m1.c1.schema16 does not exist", exception.getMessage());
}
}
@@ -225,7 +223,7 @@ public class TestHadoopCatalogOperations {
createSchema(name, comment, null, null);
createSchema(name1, comment1, null, null);
- try (HadoopCatalogOperations ops = new HadoopCatalogOperations(null, store, idGenerator)) {
+ try (HadoopCatalogOperations ops = new HadoopCatalogOperations(null, store)) {
ops.initialize(Maps.newHashMap());
Set<NameIdentifier> idents =
Arrays.stream(ops.listSchemas(Namespace.of("m1", "c1"))).collect(Collectors.toSet());
@@ -243,7 +241,7 @@ public class TestHadoopCatalogOperations {
Schema schema = createSchema(name, comment, catalogPath, null);
Assertions.assertEquals(name, schema.name());
- try (HadoopCatalogOperations ops = new HadoopCatalogOperations(null, store, idGenerator)) {
+ try (HadoopCatalogOperations ops = new HadoopCatalogOperations(null, store)) {
ops.initialize(Maps.newHashMap());
Schema schema1 = ops.loadSchema(NameIdentifier.ofSchema("m1", "c1", name));
Assertions.assertEquals(name, schema1.name());
@@ -292,7 +290,7 @@ public class TestHadoopCatalogOperations {
Schema schema = createSchema(name, comment, catalogPath, null);
Assertions.assertEquals(name, schema.name());
- try (HadoopCatalogOperations ops = new HadoopCatalogOperations(null, store, idGenerator)) {
+ try (HadoopCatalogOperations ops = new HadoopCatalogOperations(null, store)) {
ops.initialize(ImmutableMap.of(HadoopCatalogPropertiesMetadata.LOCATION, catalogPath));
Schema schema1 = ops.loadSchema(NameIdentifier.ofSchema("m1", "c1", name));
Assertions.assertEquals(name, schema1.name());
@@ -321,7 +319,8 @@ public class TestHadoopCatalogOperations {
NonEmptySchemaException.class,
() -> ops.dropSchema(NameIdentifier.ofSchema("m1", "c1", name), false));
Assertions.assertEquals(
- "Schema schema20 with location " + schemaPath + " is not empty", exception1.getMessage());
+ "Schema m1.c1.schema20 with location " + schemaPath + " is not empty",
+ exception1.getMessage());
// Test drop non-empty schema with cascade = true
ops.dropSchema(NameIdentifier.ofSchema("m1", "c1", name), true);
@@ -329,6 +328,373 @@ public class TestHadoopCatalogOperations {
}
}
+ @ParameterizedTest
+ @MethodSource("locationArguments")
+ public void testCreateLoadAndDeleteFilesetWithLocations(
+ String name,
+ Fileset.Type type,
+ String catalogPath,
+ String schemaPath,
+ String storageLocation,
+ String expect)
+ throws IOException {
+ String schemaName = "s1_" + name;
+ String comment = "comment_s1";
+ Map<String, String> catalogProps = Maps.newHashMap();
+ if (catalogPath != null) {
+ catalogProps.put(HadoopCatalogPropertiesMetadata.LOCATION, catalogPath);
+ }
+
+ NameIdentifier schemaIdent = NameIdentifier.ofSchema("m1", "c1", schemaName);
+ try (HadoopCatalogOperations ops = new HadoopCatalogOperations(null, store)) {
+ ops.initialize(catalogProps);
+ if (!ops.schemaExists(schemaIdent)) {
+ createSchema(schemaName, comment, catalogPath, schemaPath);
+ }
+ Fileset fileset =
+ createFileset(name, schemaName, "comment", type, catalogPath, storageLocation);
+
+ Assertions.assertEquals(name, fileset.name());
+ Assertions.assertEquals(type, fileset.type());
+ Assertions.assertEquals("comment", fileset.comment());
+ Assertions.assertEquals(expect, fileset.storageLocation());
+
+ // Test load
+ NameIdentifier filesetIdent = NameIdentifier.of("m1", "c1", schemaName, name);
+ Fileset loadedFileset = ops.loadFileset(filesetIdent);
+ Assertions.assertEquals(name, loadedFileset.name());
+ Assertions.assertEquals(type, loadedFileset.type());
+ Assertions.assertEquals("comment", loadedFileset.comment());
+ Assertions.assertEquals(expect, loadedFileset.storageLocation());
+
+ // Test drop
+ ops.dropFileset(filesetIdent);
+ Path expectedPath = new Path(expect);
+ FileSystem fs = expectedPath.getFileSystem(new Configuration());
+ if (type == Fileset.Type.MANAGED) {
+ Assertions.assertFalse(fs.exists(expectedPath));
+ } else {
+ Assertions.assertTrue(fs.exists(expectedPath));
+ }
+ }
+ }
+
+ @Test
+ public void testCreateFilesetWithExceptions() throws IOException {
+ String schemaName = "schema22";
+ String comment = "comment22";
+ createSchema(schemaName, comment, null, null);
+ String name = "fileset22";
+ NameIdentifier filesetIdent = NameIdentifier.of("m1", "c1", schemaName, name);
+
+ // If neither catalog location, nor schema location and storageLocation is specified.
+ Throwable exception =
+ Assertions.assertThrows(
+ IllegalArgumentException.class,
+ () -> createFileset(name, schemaName, comment, Fileset.Type.MANAGED, null, null));
+ Assertions.assertEquals(
+ "Storage location must be set for fileset "
+ + filesetIdent
+ + " when it's catalog and schema "
+ + "location are not set",
+ exception.getMessage());
+ try (HadoopCatalogOperations ops = new HadoopCatalogOperations(null, store)) {
+ ops.initialize(Maps.newHashMap());
+ Throwable e =
+ Assertions.assertThrows(
+ NoSuchFilesetException.class, () -> ops.loadFileset(filesetIdent));
+ Assertions.assertEquals("Fileset m1.c1.schema22.fileset22 does not exist", e.getMessage());
+ }
+
+ // For external fileset, if storageLocation is not specified.
+ Throwable exception1 =
+ Assertions.assertThrows(
+ IllegalArgumentException.class,
+ () -> createFileset(name, schemaName, comment, Fileset.Type.EXTERNAL, null, null));
+ Assertions.assertEquals(
+ "Storage location must be set for external fileset " + filesetIdent,
+ exception1.getMessage());
+ try (HadoopCatalogOperations ops = new HadoopCatalogOperations(null, store)) {
+ ops.initialize(Maps.newHashMap());
+ Throwable e =
+ Assertions.assertThrows(
+ NoSuchFilesetException.class, () -> ops.loadFileset(filesetIdent));
+ Assertions.assertEquals("Fileset " + filesetIdent + " does not exist", e.getMessage());
+ }
+ }
+
+ @Test
+ public void testListFilesets() throws IOException {
+ String schemaName = "schema23";
+ String comment = "comment23";
+ String schemaPath = TEST_ROOT_PATH + "/" + schemaName;
+ createSchema(schemaName, comment, null, schemaPath);
+ String[] filesets = new String[] {"fileset23_1", "fileset23_2", "fileset23_3"};
+ for (String fileset : filesets) {
+ createFileset(fileset, schemaName, comment, Fileset.Type.MANAGED, null, null);
+ }
+
+ try (HadoopCatalogOperations ops = new HadoopCatalogOperations(null, store)) {
+ ops.initialize(Maps.newHashMap());
+ Set<NameIdentifier> idents =
+ Arrays.stream(ops.listFilesets(Namespace.of("m1", "c1", schemaName)))
+ .collect(Collectors.toSet());
+ Assertions.assertTrue(idents.size() >= 3);
+ for (String fileset : filesets) {
+ Assertions.assertTrue(idents.contains(NameIdentifier.of("m1", "c1", schemaName, fileset)));
+ }
+ }
+ }
+
+ @ParameterizedTest
+ @MethodSource("testRenameArguments")
+ public void testRenameFileset(
+ String name,
+ String newName,
+ Fileset.Type type,
+ String catalogPath,
+ String schemaPath,
+ String storageLocation,
+ String expect)
+ throws IOException {
+ String schemaName = "s24_" + name;
+ String comment = "comment_s24";
+ Map<String, String> catalogProps = Maps.newHashMap();
+ if (catalogPath != null) {
+ catalogProps.put(HadoopCatalogPropertiesMetadata.LOCATION, catalogPath);
+ }
+
+ NameIdentifier schemaIdent = NameIdentifier.ofSchema("m1", "c1", schemaName);
+ try (HadoopCatalogOperations ops = new HadoopCatalogOperations(null, store)) {
+ ops.initialize(catalogProps);
+ if (!ops.schemaExists(schemaIdent)) {
+ createSchema(schemaName, comment, catalogPath, schemaPath);
+ }
+ Fileset fileset =
+ createFileset(name, schemaName, "comment", type, catalogPath, storageLocation);
+
+ Assertions.assertEquals(name, fileset.name());
+ Assertions.assertEquals(type, fileset.type());
+ Assertions.assertEquals("comment", fileset.comment());
+
+ NameIdentifier filesetIdent = NameIdentifier.of("m1", "c1", schemaName, name);
+ Fileset loadedFileset = ops.loadFileset(filesetIdent);
+ Assertions.assertEquals(name, loadedFileset.name());
+ Assertions.assertEquals(type, loadedFileset.type());
+ Assertions.assertEquals("comment", loadedFileset.comment());
+
+ Fileset renamedFileset = ops.alterFileset(filesetIdent, FilesetChange.rename(newName));
+ Assertions.assertEquals(newName, renamedFileset.name());
+ Assertions.assertEquals(type, renamedFileset.type());
+ Assertions.assertEquals("comment", renamedFileset.comment());
+ Assertions.assertEquals(expect, renamedFileset.storageLocation());
+
+ Fileset loadedRenamedFileset =
+ ops.loadFileset(NameIdentifier.of("m1", "c1", schemaName, newName));
+ Assertions.assertEquals(newName, loadedRenamedFileset.name());
+ Assertions.assertEquals(type, loadedRenamedFileset.type());
+ Assertions.assertEquals("comment", loadedRenamedFileset.comment());
+ Assertions.assertEquals(expect, loadedRenamedFileset.storageLocation());
+ }
+ }
+
+ @Test
+ public void testAlterFilesetProperties() throws IOException {
+ String schemaName = "schema25";
+ String comment = "comment25";
+ String schemaPath = TEST_ROOT_PATH + "/" + schemaName;
+ createSchema(schemaName, comment, null, schemaPath);
+
+ String name = "fileset25";
+ Fileset fileset = createFileset(name, schemaName, comment, Fileset.Type.MANAGED, null, null);
+
+ FilesetChange change1 = FilesetChange.setProperty("k1", "v1");
+ FilesetChange change2 = FilesetChange.removeProperty("k1");
+
+ try (HadoopCatalogOperations ops = new HadoopCatalogOperations(null, store)) {
+ ops.initialize(Maps.newHashMap());
+ NameIdentifier filesetIdent = NameIdentifier.of("m1", "c1", schemaName, name);
+
+ Fileset fileset1 = ops.alterFileset(filesetIdent, change1);
+ Assertions.assertEquals(name, fileset1.name());
+ Assertions.assertEquals(Fileset.Type.MANAGED, fileset1.type());
+ Assertions.assertEquals("comment25", fileset1.comment());
+ Assertions.assertEquals(fileset.storageLocation(), fileset1.storageLocation());
+ Map<String, String> props1 = fileset1.properties();
+ Assertions.assertTrue(props1.containsKey("k1"));
+ Assertions.assertEquals("v1", props1.get("k1"));
+
+ Fileset fileset2 = ops.alterFileset(filesetIdent, change2);
+ Assertions.assertEquals(name, fileset2.name());
+ Assertions.assertEquals(Fileset.Type.MANAGED, fileset2.type());
+ Assertions.assertEquals("comment25", fileset2.comment());
+ Assertions.assertEquals(fileset.storageLocation(), fileset2.storageLocation());
+ Map<String, String> props2 = fileset2.properties();
+ Assertions.assertFalse(props2.containsKey("k1"));
+ }
+ }
+
+ private static Stream<Arguments> locationArguments() {
+ return Stream.of(
+ // Honor the catalog location
+ Arguments.of(
+ "fileset11",
+ Fileset.Type.MANAGED,
+ TEST_ROOT_PATH + "/catalog21",
+ null,
+ null,
+ TEST_ROOT_PATH + "/catalog21/s1_fileset11/fileset11"),
+ Arguments.of(
+ // honor the schema location
+ "fileset12",
+ Fileset.Type.MANAGED,
+ null,
+ TEST_ROOT_PATH + "/s1_fileset12",
+ null,
+ TEST_ROOT_PATH + "/s1_fileset12/fileset12"),
+ Arguments.of(
+ // honor the schema location
+ "fileset13",
+ Fileset.Type.MANAGED,
+ TEST_ROOT_PATH + "/catalog22",
+ TEST_ROOT_PATH + "/s1_fileset13",
+ null,
+ TEST_ROOT_PATH + "/s1_fileset13/fileset13"),
+ Arguments.of(
+ // honor the storage location
+ "fileset14",
+ Fileset.Type.MANAGED,
+ TEST_ROOT_PATH + "/catalog23",
+ TEST_ROOT_PATH + "/s1_fileset14",
+ TEST_ROOT_PATH + "/fileset14",
+ TEST_ROOT_PATH + "/fileset14"),
+ Arguments.of(
+ // honor the storage location
+ "fileset15",
+ Fileset.Type.MANAGED,
+ null,
+ null,
+ TEST_ROOT_PATH + "/fileset15",
+ TEST_ROOT_PATH + "/fileset15"),
+ Arguments.of(
+ // honor the storage location
+ "fileset16",
+ Fileset.Type.MANAGED,
+ TEST_ROOT_PATH + "/catalog24",
+ null,
+ TEST_ROOT_PATH + "/fileset16",
+ TEST_ROOT_PATH + "/fileset16"),
+ Arguments.of(
+ // honor the storage location
+ "fileset17",
+ Fileset.Type.EXTERNAL,
+ TEST_ROOT_PATH + "/catalog25",
+ TEST_ROOT_PATH + "/s1_fileset17",
+ TEST_ROOT_PATH + "/fileset17",
+ TEST_ROOT_PATH + "/fileset17"),
+ Arguments.of(
+ // honor the storage location
+ "fileset18",
+ Fileset.Type.EXTERNAL,
+ null,
+ TEST_ROOT_PATH + "/s1_fileset18",
+ TEST_ROOT_PATH + "/fileset18",
+ TEST_ROOT_PATH + "/fileset18"),
+ Arguments.of(
+ // honor the storage location
+ "fileset19",
+ Fileset.Type.EXTERNAL,
+ null,
+ null,
+ TEST_ROOT_PATH + "/fileset19",
+ TEST_ROOT_PATH + "/fileset19"));
+ }
+
+ private static Stream<Arguments> testRenameArguments() {
+ return Stream.of(
+ // Honor the catalog location
+ Arguments.of(
+ "fileset31",
+ "fileset31_new",
+ Fileset.Type.MANAGED,
+ TEST_ROOT_PATH + "/catalog21",
+ null,
+ null,
+ TEST_ROOT_PATH + "/catalog21/s24_fileset31/fileset31"),
+ Arguments.of(
+ // honor the schema location
+ "fileset32",
+ "fileset32_new",
+ Fileset.Type.MANAGED,
+ null,
+ TEST_ROOT_PATH + "/s24_fileset32",
+ null,
+ TEST_ROOT_PATH + "/s24_fileset32/fileset32"),
+ Arguments.of(
+ // honor the schema location
+ "fileset33",
+ "fileset33_new",
+ Fileset.Type.MANAGED,
+ TEST_ROOT_PATH + "/catalog22",
+ TEST_ROOT_PATH + "/s24_fileset33",
+ null,
+ TEST_ROOT_PATH + "/s24_fileset33/fileset33"),
+ Arguments.of(
+ // honor the storage location
+ "fileset34",
+ "fileset34_new",
+ Fileset.Type.MANAGED,
+ TEST_ROOT_PATH + "/catalog23",
+ TEST_ROOT_PATH + "/s24_fileset34",
+ TEST_ROOT_PATH + "/fileset34",
+ TEST_ROOT_PATH + "/fileset34"),
+ Arguments.of(
+ // honor the storage location
+ "fileset35",
+ "fileset35_new",
+ Fileset.Type.MANAGED,
+ null,
+ null,
+ TEST_ROOT_PATH + "/fileset35",
+ TEST_ROOT_PATH + "/fileset35"),
+ Arguments.of(
+ // honor the storage location
+ "fileset36",
+ "fileset36_new",
+ Fileset.Type.MANAGED,
+ TEST_ROOT_PATH + "/catalog24",
+ null,
+ TEST_ROOT_PATH + "/fileset36",
+ TEST_ROOT_PATH + "/fileset36"),
+ Arguments.of(
+ // honor the storage location
+ "fileset37",
+ "fileset37_new",
+ Fileset.Type.EXTERNAL,
+ TEST_ROOT_PATH + "/catalog25",
+ TEST_ROOT_PATH + "/s24_fileset37",
+ TEST_ROOT_PATH + "/fileset37",
+ TEST_ROOT_PATH + "/fileset37"),
+ Arguments.of(
+ // honor the storage location
+ "fileset38",
+ "fileset38_new",
+ Fileset.Type.EXTERNAL,
+ null,
+ TEST_ROOT_PATH + "/s24_fileset38",
+ TEST_ROOT_PATH + "/fileset38",
+ TEST_ROOT_PATH + "/fileset38"),
+ Arguments.of(
+ // honor the storage location
+ "fileset39",
+ "fileset39_new",
+ Fileset.Type.EXTERNAL,
+ null,
+ null,
+ TEST_ROOT_PATH + "/fileset39",
+ TEST_ROOT_PATH + "/fileset39"));
+ }
+
private Schema createSchema(String name, String comment, String catalogPath, String schemaPath)
throws IOException {
Map<String, String> props = Maps.newHashMap();
@@ -336,7 +702,7 @@ public class TestHadoopCatalogOperations {
props.put(HadoopCatalogPropertiesMetadata.LOCATION, catalogPath);
}
- try (HadoopCatalogOperations ops = new HadoopCatalogOperations(null, store, idGenerator)) {
+ try (HadoopCatalogOperations ops = new HadoopCatalogOperations(null, store)) {
ops.initialize(props);
NameIdentifier schemaIdent = NameIdentifier.ofSchema("m1", "c1", name);
@@ -351,4 +717,29 @@ public class TestHadoopCatalogOperations {
return ops.createSchema(schemaIdent, comment, schemaProps);
}
}
+
+ private Fileset createFileset(
+ String name,
+ String schemaName,
+ String comment,
+ Fileset.Type type,
+ String catalogPath,
+ String storageLocation)
+ throws IOException {
+ Map<String, String> props = Maps.newHashMap();
+ if (catalogPath != null) {
+ props.put(HadoopCatalogPropertiesMetadata.LOCATION, catalogPath);
+ }
+
+ try (HadoopCatalogOperations ops = new HadoopCatalogOperations(null, store)) {
+ ops.initialize(props);
+
+ NameIdentifier filesetIdent = NameIdentifier.of("m1", "c1", schemaName, name);
+ Map<String, String> filesetProps = Maps.newHashMap();
+ StringIdentifier stringId = StringIdentifier.fromId(idGenerator.nextId());
+ filesetProps = Maps.newHashMap(StringIdentifier.newPropertiesWithId(stringId, filesetProps));
+
+ return ops.createFileset(filesetIdent, comment, type, storageLocation, filesetProps);
+ }
+ }
}
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 5c75d57aa5..9b3097c065 100644
--- a/core/src/main/java/com/datastrato/gravitino/catalog/CatalogManager.java
+++ b/core/src/main/java/com/datastrato/gravitino/catalog/CatalogManager.java
@@ -24,6 +24,7 @@ import com.datastrato.gravitino.exceptions.CatalogAlreadyExistsException;
import com.datastrato.gravitino.exceptions.NoSuchCatalogException;
import com.datastrato.gravitino.exceptions.NoSuchEntityException;
import com.datastrato.gravitino.exceptions.NoSuchMetalakeException;
+import com.datastrato.gravitino.file.FilesetCatalog;
import com.datastrato.gravitino.meta.AuditInfo;
import com.datastrato.gravitino.meta.CatalogEntity;
import com.datastrato.gravitino.rel.SupportsSchemas;
@@ -98,6 +99,17 @@ public class CatalogManager implements SupportsCatalogs, Closeable {
});
}
+ public <R> R doWithFilesetOps(ThrowableFunction<FilesetCatalog, R> fn) throws Exception {
+ return classLoader.withClassLoader(
+ cl -> {
+ if (asFilesets() == null) {
+ throw new UnsupportedOperationException(
+ "Catalog does not support fileset operations");
+ }
+ return fn.apply(asFilesets());
+ });
+ }
+
public <R> R doWithPropertiesMeta(ThrowableFunction<HasPropertyMetadata, R> fn)
throws Exception {
return classLoader.withClassLoader(cl -> fn.apply(catalog.ops()));
@@ -127,6 +139,10 @@ public class CatalogManager implements SupportsCatalogs, Closeable {
private TableCatalog asTables() {
return catalog.ops() instanceof TableCatalog ? (TableCatalog) catalog.ops() : null;
}
+
+ private FilesetCatalog asFilesets() {
+ return catalog.ops() instanceof FilesetCatalog ? (FilesetCatalog) catalog.ops() : null;
+ }
}
private final Config config;
diff --git a/core/src/main/java/com/datastrato/gravitino/catalog/file/BaseFileset.java b/core/src/main/java/com/datastrato/gravitino/catalog/file/BaseFileset.java
new file mode 100644
index 0000000000..cd5f4b2b99
--- /dev/null
+++ b/core/src/main/java/com/datastrato/gravitino/catalog/file/BaseFileset.java
@@ -0,0 +1,133 @@
+/*
+ * Copyright 2024 Datastrato Pvt Ltd.
+ * This software is licensed under the Apache License version 2.
+ */
+package com.datastrato.gravitino.catalog.file;
+
+import com.datastrato.gravitino.file.Fileset;
+import com.datastrato.gravitino.meta.AuditInfo;
+import java.util.Map;
+import javax.annotation.Nullable;
+
+public abstract class BaseFileset implements Fileset {
+
+ protected String name;
+
+ @Nullable protected String comment;
+
+ protected Type type;
+
+ protected String storageLocation;
+
+ @Nullable protected Map<String, String> properties;
+
+ protected AuditInfo auditInfo;
+
+ @Override
+ public String name() {
+ return name;
+ }
+
+ @Nullable
+ @Override
+ public String comment() {
+ return comment;
+ }
+
+ @Override
+ public Type type() {
+ return type;
+ }
+
+ @Override
+ public String storageLocation() {
+ return storageLocation;
+ }
+
+ @Override
+ public AuditInfo auditInfo() {
+ return auditInfo;
+ }
+
+ @Nullable
+ @Override
+ public Map<String, String> properties() {
+ return properties;
+ }
+
+ interface Builder<SELF extends Builder<SELF, T>, T extends BaseFileset> {
+
+ SELF withName(String name);
+
+ SELF withComment(String comment);
+
+ SELF withType(Type type);
+
+ SELF withStorageLocation(String storageLocation);
+
+ SELF withProperties(Map<String, String> properties);
+
+ SELF withAuditInfo(AuditInfo auditInfo);
+
+ T build();
+ }
+
+ public abstract static class BaseFilesetBuilder<
+ SELF extends Builder<SELF, T>, T extends BaseFileset>
+ implements Builder<SELF, T> {
+ protected String name;
+ protected String comment;
+ protected Type type;
+ protected String storageLocation;
+ protected Map<String, String> properties;
+ protected AuditInfo auditInfo;
+
+ @Override
+ public SELF withName(String name) {
+ this.name = name;
+ return self();
+ }
+
+ @Override
+ public SELF withComment(String comment) {
+ this.comment = comment;
+ return self();
+ }
+
+ @Override
+ public SELF withType(Type type) {
+ this.type = type;
+ return self();
+ }
+
+ @Override
+ public SELF withStorageLocation(String storageLocation) {
+ this.storageLocation = storageLocation;
+ return self();
+ }
+
+ @Override
+ public SELF withProperties(Map<String, String> properties) {
+ this.properties = properties;
+ return self();
+ }
+
+ @Override
+ public SELF withAuditInfo(AuditInfo auditInfo) {
+ this.auditInfo = auditInfo;
+ return self();
+ }
+
+ @Override
+ public T build() {
+ T t = internalBuild();
+ return t;
+ }
+
+ private SELF self() {
+ return (SELF) this;
+ }
+
+ protected abstract T internalBuild();
+ }
+}
diff --git a/core/src/main/java/com/datastrato/gravitino/meta/FilesetEntity.java b/core/src/main/java/com/datastrato/gravitino/meta/FilesetEntity.java
index 7f0ccdfd88..839612d793 100644
--- a/core/src/main/java/com/datastrato/gravitino/meta/FilesetEntity.java
+++ b/core/src/main/java/com/datastrato/gravitino/meta/FilesetEntity.java
@@ -8,12 +8,12 @@ import com.datastrato.gravitino.Auditable;
import com.datastrato.gravitino.Entity;
import com.datastrato.gravitino.Field;
import com.datastrato.gravitino.HasIdentifier;
+import com.datastrato.gravitino.Namespace;
import com.datastrato.gravitino.file.Fileset;
import com.google.common.collect.Maps;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
-import javax.annotation.Nullable;
import lombok.ToString;
@ToString
@@ -28,7 +28,7 @@ public class FilesetEntity implements Entity, Auditable, HasIdentifier {
public static final Field TYPE =
Field.required("type", Fileset.Type.class, "The type of the fileset entity.");
public static final Field STORAGE_LOCATION =
- Field.optional(
+ Field.required(
"storage_location", String.class, "The storage location of the fileset entity.");
public static final Field AUDIT_INFO =
Field.required("audit_info", AuditInfo.class, "The audit details of the fileset entity.");
@@ -39,11 +39,13 @@ public class FilesetEntity implements Entity, Auditable, HasIdentifier {
private String name;
+ private Namespace namespace;
+
private String comment;
private Fileset.Type type;
- @Nullable private String storageLocation;
+ private String storageLocation;
private AuditInfo auditInfo;
@@ -80,6 +82,16 @@ public class FilesetEntity implements Entity, Auditable, HasIdentifier {
return name;
}
+ /**
+ * Returns the namespace of the fileset entity.
+ *
+ * @return The namespace of the fileset entity.
+ */
+ @Override
+ public Namespace namespace() {
+ return namespace;
+ }
+
/**
* Returns the unique id of the fileset entity.
*
@@ -196,6 +208,17 @@ public class FilesetEntity implements Entity, Auditable, HasIdentifier {
return this;
}
+ /**
+ * Sets the namespace of the fileset entity.
+ *
+ * @param namespace The namespace of the fileset entity.
+ * @return The builder instance.
+ */
+ public Builder withNamespace(Namespace namespace) {
+ fileset.namespace = namespace;
+ return this;
+ }
+
/**
* Sets the comment of the fileset entity.
*
@@ -260,11 +283,6 @@ public class FilesetEntity implements Entity, Auditable, HasIdentifier {
*/
public FilesetEntity build() {
fileset.validate();
-
- if (fileset.type == Fileset.Type.EXTERNAL && fileset.storageLocation == null) {
- throw new IllegalArgumentException("Storage location is required for EXTERNAL fileset.");
- }
-
return fileset;
}
}
diff --git a/core/src/main/java/com/datastrato/gravitino/proto/FilesetEntitySerDe.java b/core/src/main/java/com/datastrato/gravitino/proto/FilesetEntitySerDe.java
index f66272ab2a..84165ebe59 100644
--- a/core/src/main/java/com/datastrato/gravitino/proto/FilesetEntitySerDe.java
+++ b/core/src/main/java/com/datastrato/gravitino/proto/FilesetEntitySerDe.java
@@ -13,6 +13,7 @@ public class FilesetEntitySerDe implements ProtoSerDe<FilesetEntity, Fileset> {
Fileset.newBuilder()
.setId(filesetEntity.id())
.setName(filesetEntity.name())
+ .setStorageLocation(filesetEntity.storageLocation())
.setAuditInfo(new AuditInfoSerDe().serialize(filesetEntity.auditInfo()));
if (filesetEntity.comment() != null) {
@@ -26,10 +27,6 @@ public class FilesetEntitySerDe implements ProtoSerDe<FilesetEntity, Fileset> {
Fileset.Type type = Fileset.Type.valueOf(filesetEntity.filesetType().name());
builder.setType(type);
- if (filesetEntity.storageLocation() != null) {
- builder.setStorageLocation(filesetEntity.storageLocation());
- }
-
return builder.build();
}
@@ -39,6 +36,7 @@ public class FilesetEntitySerDe implements ProtoSerDe<FilesetEntity, Fileset> {
new FilesetEntity.Builder()
.withId(p.getId())
.withName(p.getName())
+ .withStorageLocation(p.getStorageLocation())
.withAuditInfo(new AuditInfoSerDe().deserialize(p.getAuditInfo()))
.withFilesetType(
com.datastrato.gravitino.file.Fileset.Type.valueOf(p.getType().name()));
@@ -47,10 +45,6 @@ public class FilesetEntitySerDe implements ProtoSerDe<FilesetEntity, Fileset> {
builder.withComment(p.getComment());
}
- if (p.hasStorageLocation()) {
- builder.withStorageLocation(p.getStorageLocation());
- }
-
if (p.getPropertiesCount() > 0) {
builder.withProperties(p.getPropertiesMap());
}
diff --git a/core/src/main/java/com/datastrato/gravitino/storage/kv/BinaryEntityKeyEncoder.java b/core/src/main/java/com/datastrato/gravitino/storage/kv/BinaryEntityKeyEncoder.java
index 3de6930ef6..0e89f17522 100644
--- a/core/src/main/java/com/datastrato/gravitino/storage/kv/BinaryEntityKeyEncoder.java
+++ b/core/src/main/java/com/datastrato/gravitino/storage/kv/BinaryEntityKeyEncoder.java
@@ -5,6 +5,7 @@
package com.datastrato.gravitino.storage.kv;
import static com.datastrato.gravitino.Entity.EntityType.CATALOG;
+import static com.datastrato.gravitino.Entity.EntityType.FILESET;
import static com.datastrato.gravitino.Entity.EntityType.METALAKE;
import static com.datastrato.gravitino.Entity.EntityType.SCHEMA;
import static com.datastrato.gravitino.Entity.EntityType.TABLE;
@@ -68,7 +69,8 @@ public class BinaryEntityKeyEncoder implements EntityKeyEncoder<byte[]> {
METALAKE, new String[] {METALAKE.getShortName() + "/"},
CATALOG, new String[] {CATALOG.getShortName() + "/", "/"},
SCHEMA, new String[] {SCHEMA.getShortName() + "/", "/", "/"},
- TABLE, new String[] {TABLE.getShortName() + "/", "/", "/", "/"});
+ TABLE, new String[] {TABLE.getShortName() + "/", "/", "/", "/"},
+ FILESET, new String[] {FILESET.getShortName() + "/", "/", "/", "/"});
@VisibleForTesting final NameMappingService nameMappingService;
diff --git a/core/src/test/java/com/datastrato/gravitino/meta/TestEntity.java b/core/src/test/java/com/datastrato/gravitino/meta/TestEntity.java
index 4924205271..ec921c357e 100644
--- a/core/src/test/java/com/datastrato/gravitino/meta/TestEntity.java
+++ b/core/src/test/java/com/datastrato/gravitino/meta/TestEntity.java
@@ -136,6 +136,7 @@ public class TestEntity {
.withName(fileName)
.withAuditInfo(auditInfo)
.withFilesetType(Fileset.Type.MANAGED)
+ .withStorageLocation("testLocation")
.withProperties(map)
.build();
@@ -146,6 +147,7 @@ public class TestEntity {
Assertions.assertEquals(Fileset.Type.MANAGED, fields.get(FilesetEntity.TYPE));
Assertions.assertEquals(map, fields.get(FilesetEntity.PROPERTIES));
Assertions.assertNull(fields.get(FilesetEntity.COMMENT));
+ Assertions.assertEquals("testLocation", fields.get(FilesetEntity.STORAGE_LOCATION));
FilesetEntity testFile1 =
new FilesetEntity.Builder()
@@ -153,12 +155,11 @@ public class TestEntity {
.withName(fileName)
.withAuditInfo(auditInfo)
.withFilesetType(Fileset.Type.MANAGED)
- .withProperties(map)
.withComment("testComment")
.withStorageLocation("testLocation")
.build();
Assertions.assertEquals("testComment", testFile1.comment());
- Assertions.assertEquals("testLocation", testFile1.storageLocation());
+ Assertions.assertNull(testFile1.properties());
Throwable exception =
Assertions.assertThrows(
@@ -173,7 +174,6 @@ public class TestEntity {
.withComment("testComment")
.build();
});
- Assertions.assertEquals(
- "Storage location is required for EXTERNAL fileset.", exception.getMessage());
+ Assertions.assertEquals("Field storage_location is required", exception.getMessage());
}
}
diff --git a/core/src/test/java/com/datastrato/gravitino/proto/TestEntityProtoSerDe.java b/core/src/test/java/com/datastrato/gravitino/proto/TestEntityProtoSerDe.java
index 65b4ca3b57..756b2472e2 100644
--- a/core/src/test/java/com/datastrato/gravitino/proto/TestEntityProtoSerDe.java
+++ b/core/src/test/java/com/datastrato/gravitino/proto/TestEntityProtoSerDe.java
@@ -188,6 +188,7 @@ public class TestEntityProtoSerDe {
.withName(fileName)
.withAuditInfo(auditInfo)
.withFilesetType(com.datastrato.gravitino.file.Fileset.Type.MANAGED)
+ .withStorageLocation("testLocation")
.withProperties(props)
.withComment(comment)
.build();
@@ -202,6 +203,7 @@ public class TestEntityProtoSerDe {
.withName(fileName)
.withAuditInfo(auditInfo)
.withFilesetType(com.datastrato.gravitino.file.Fileset.Type.MANAGED)
+ .withStorageLocation("testLocation")
.build();
byte[] fileBytes1 = protoEntitySerDe.serialize(fileEntity1);
com.datastrato.gravitino.meta.FilesetEntity fileEntityFromBytes1 =
@@ -209,7 +211,6 @@ public class TestEntityProtoSerDe {
Assertions.assertEquals(fileEntity1, fileEntityFromBytes1);
Assertions.assertNull(fileEntityFromBytes1.comment());
Assertions.assertNull(fileEntityFromBytes1.properties());
- Assertions.assertNull(fileEntityFromBytes1.storageLocation());
com.datastrato.gravitino.meta.FilesetEntity fileEntity2 =
new com.datastrato.gravitino.meta.FilesetEntity.Builder()
diff --git a/meta/src/main/proto/gravitino_meta.proto b/meta/src/main/proto/gravitino_meta.proto
index 53639b5333..96dff0f418 100644
--- a/meta/src/main/proto/gravitino_meta.proto
+++ b/meta/src/main/proto/gravitino_meta.proto
@@ -89,7 +89,7 @@ message Fileset {
string name = 2;
optional string comment = 3;
Type type = 4;
- optional string storage_location = 5;
+ string storage_location = 5;
map<string, string> properties = 6;
AuditInfo audit_info = 7;
}