Skip to content

Commit

Permalink
Merge branch 'main' into issue_2780
Browse files Browse the repository at this point in the history
  • Loading branch information
jerryshao authored Dec 10, 2024
2 parents e2a94d3 + 239e78a commit 4d1af74
Show file tree
Hide file tree
Showing 87 changed files with 2,686 additions and 224 deletions.
33 changes: 28 additions & 5 deletions api/src/main/java/org/apache/gravitino/Catalog.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,42 @@ public interface Catalog extends Auditable {
/** The type of the catalog. */
enum Type {
/** Catalog Type for Relational Data Structure, like db.table, catalog.db.table. */
RELATIONAL,
RELATIONAL(false),

/** Catalog Type for Fileset System (including HDFS, S3, etc.), like path/to/file */
FILESET,
FILESET(false),

/** Catalog Type for Message Queue, like Kafka://topic */
MESSAGING,
MESSAGING(false),

/** Catalog Type for ML model */
MODEL,
MODEL(true),

/** Catalog Type for test only. */
UNSUPPORTED;
UNSUPPORTED(false);

private final boolean supportsManagedCatalog;

Type(boolean supportsManagedCatalog) {
this.supportsManagedCatalog = supportsManagedCatalog;
}

/**
* A flag to indicate whether the catalog type supports managed catalog. Managed catalog is a
* concept in Gravitino, for the details of managed catalog, please refer to the class comment
* of {@link CatalogProvider}. If the catalog type supports managed catalog, users can create
* managed catalog of this type without specifying the provider, Gravitino will use the type as
* the provider to create the managed catalog. If the catalog type does not support managed
* catalog, users need to specify the provider when creating the catalog.
*
* <p>Currently, only the model catalog supports managed catalog.
*
* @return Whether the catalog type supports managed catalog. Returns true if the catalog type
* supports managed catalog.
*/
public boolean supportsManagedCatalog() {
return supportsManagedCatalog;
}

/**
* Convert the string (case-insensitive) to the catalog type.
Expand Down
26 changes: 26 additions & 0 deletions api/src/main/java/org/apache/gravitino/CatalogProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,41 @@
*/
package org.apache.gravitino;

import java.util.Locale;
import org.apache.gravitino.annotation.Evolving;

/**
* A Catalog provider is a class that provides a short name for a catalog. This short name is used
* when creating a catalog.
*
* <p>There are two kinds of catalogs in Gravitino, one is managed catalog and another is external
* catalog.
*
* <p>Managed catalog: A catalog and its subsidiary objects are all managed by Gravitino. Gravitino
* takes care of the lifecycle of the catalog and its objects. For those catalogs, Gravitino uses
* the type of the catalog as the provider short name. Note that for each catalog type, there is
* only one implementation of managed catalog for that type. Currently, Gravitino only has model
* catalog that is a managed catalog.
*
* <p>External catalog: A catalog its subsidiary objects are stored by an external sources, such as
* Hive catalog, the DB and tables are stored in HMS. For those catalogs, Gravitino uses a unique
* name as the provider short name to load the catalog. For example, Hive catalog uses "hive" as the
* provider short name.
*/
@Evolving
public interface CatalogProvider {

/**
* Form the provider short name for a managed catalog. The provider short name for a managed
* catalog is the catalog type in lowercase.
*
* @param type The catalog type.
* @return The provider short name for the managed catalog.
*/
static String shortNameForManagedCatalog(Catalog.Type type) {
return type.name().toLowerCase(Locale.ROOT);
}

/**
* The string that represents the catalog that this provider uses. This is overridden by children
* to provide a nice alias for the catalog.
Expand Down
26 changes: 23 additions & 3 deletions api/src/main/java/org/apache/gravitino/SupportsCatalogs.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,15 @@ default boolean catalogExists(String catalogName) {
* Create a catalog with specified catalog name, type, provider, comment, and properties.
*
* <p>The parameter "provider" is a short name of the catalog, used to tell Gravitino which
* catalog should be created. The short name should be the same as the {@link CatalogProvider}
* interface provided.
* catalog should be created. The short name:
*
* <p>1) should be the same as the {@link CatalogProvider} interface provided. 2) can be "null" if
* the created catalog is the managed catalog, like model catalog. For the details of the provider
* definition, see {@link CatalogProvider}.
*
* @param catalogName the name of the catalog.
* @param type the type of the catalog.
* @param provider the provider of the catalog.
* @param provider the provider of the catalog, or null if the catalog is a managed catalog.
* @param comment the comment of the catalog.
* @param properties the properties of the catalog.
* @return The created catalog.
Expand All @@ -98,6 +101,23 @@ Catalog createCatalog(
Map<String, String> properties)
throws NoSuchMetalakeException, CatalogAlreadyExistsException;

/**
* Create a managed catalog with specified catalog name, type, comment, and properties.
*
* @param catalogName the name of the catalog.
* @param type the type of the catalog.
* @param comment the comment of the catalog.
* @param properties the properties of the catalog.
* @return The created catalog.
* @throws NoSuchMetalakeException If the metalake does not exist.
* @throws CatalogAlreadyExistsException If the catalog already exists.
*/
default Catalog createCatalog(
String catalogName, Catalog.Type type, String comment, Map<String, String> properties)
throws NoSuchMetalakeException, CatalogAlreadyExistsException {
return createCatalog(catalogName, type, null, comment, properties);
}

/**
* Alter a catalog with specified catalog name and changes.
*
Expand Down
10 changes: 10 additions & 0 deletions api/src/main/java/org/apache/gravitino/credential/Credential.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ public interface Credential {
*/
Map<String, String> credentialInfo();

/**
* Initialize the credential with the credential information.
*
* <p>This method is invoked to deserialize the credential in client side.
*
* @param credentialInfo The credential information from {@link #credentialInfo}.
* @param expireTimeInMs The expire-time from {@link #expireTimeInMs()}.
*/
void initialize(Map<String, String> credentialInfo, long expireTimeInMs);

/**
* Converts the credential to properties to transfer the credential though API.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,35 +33,48 @@ public class GCSTokenCredential implements Credential {
/** GCS credential property, token name. */
public static final String GCS_TOKEN_NAME = "token";

private final String token;
private final long expireMs;
private String token;
private long expireTimeInMs;

/**
* @param token The GCS token.
* @param expireMs The GCS token expire time at ms.
* @param expireTimeInMs The GCS token expire time at ms.
*/
public GCSTokenCredential(String token, long expireMs) {
Preconditions.checkArgument(
StringUtils.isNotBlank(token), "GCS session token should not be null");
public GCSTokenCredential(String token, long expireTimeInMs) {
validate(token, expireTimeInMs);
this.token = token;
this.expireMs = expireMs;
this.expireTimeInMs = expireTimeInMs;
}

/**
* This is the constructor that is used by credential factory to create an instance of credential
* according to the credential information.
*/
public GCSTokenCredential() {}

@Override
public String credentialType() {
return GCS_TOKEN_CREDENTIAL_TYPE;
}

@Override
public long expireTimeInMs() {
return expireMs;
return expireTimeInMs;
}

@Override
public Map<String, String> credentialInfo() {
return (new ImmutableMap.Builder<String, String>()).put(GCS_TOKEN_NAME, token).build();
}

@Override
public void initialize(Map<String, String> credentialInfo, long expireTimeInMs) {
String token = credentialInfo.get(GCS_TOKEN_NAME);
validate(token, expireTimeInMs);
this.token = token;
this.expireTimeInMs = expireTimeInMs;
}

/**
* Get GCS token.
*
Expand All @@ -70,4 +83,11 @@ public Map<String, String> credentialInfo() {
public String token() {
return token;
}

private void validate(String token, long expireTimeInMs) {
Preconditions.checkArgument(
StringUtils.isNotBlank(token), "GCS session token should not be empty");
Preconditions.checkArgument(
expireTimeInMs > 0, "The expire time of GcsTokenCredential should great than 0");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ public class OSSTokenCredential implements Credential {
/** OSS security token. */
public static final String GRAVITINO_OSS_TOKEN = "oss-security-token";

private final String accessKeyId;
private final String secretAccessKey;
private final String securityToken;
private final long expireTimeInMS;
private String accessKeyId;
private String secretAccessKey;
private String securityToken;
private long expireTimeInMS;

/**
* Constructs an instance of {@link OSSTokenCredential} with secret key and token.
Expand All @@ -64,6 +64,12 @@ public OSSTokenCredential(
this.expireTimeInMS = expireTimeInMS;
}

/**
* This is the constructor that is used by credential factory to create an instance of credential
* according to the credential information.
*/
public OSSTokenCredential() {}

@Override
public String credentialType() {
return OSS_TOKEN_CREDENTIAL_TYPE;
Expand All @@ -83,6 +89,20 @@ public Map<String, String> credentialInfo() {
.build();
}

@Override
public void initialize(Map<String, String> credentialInfo, long expireTimeInMs) {
String accessKeyId = credentialInfo.get(GRAVITINO_OSS_SESSION_ACCESS_KEY_ID);
String secretAccessKey = credentialInfo.get(GRAVITINO_OSS_SESSION_SECRET_ACCESS_KEY);
String securityToken = credentialInfo.get(GRAVITINO_OSS_TOKEN);

validate(accessKeyId, secretAccessKey, securityToken, expireTimeInMs);

this.accessKeyId = accessKeyId;
this.secretAccessKey = secretAccessKey;
this.securityToken = securityToken;
this.expireTimeInMS = expireTimeInMs;
}

/**
* Get oss access key ID.
*
Expand All @@ -109,4 +129,16 @@ public String secretAccessKey() {
public String securityToken() {
return securityToken;
}

private void validate(
String accessKeyId, String secretAccessKey, String sessionToken, long expireTimeInMs) {
Preconditions.checkArgument(
StringUtils.isNotBlank(accessKeyId), "S3 access key Id should not be empty");
Preconditions.checkArgument(
StringUtils.isNotBlank(secretAccessKey), "S3 secret access key should not be empty");
Preconditions.checkArgument(
StringUtils.isNotBlank(sessionToken), "S3 session token should not be empty");
Preconditions.checkArgument(
expireTimeInMs > 0, "The expire time of S3TokenCredential should great than 0");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;

/** S3 secret key credential. */
public class S3SecretKeyCredential implements Credential {
Expand All @@ -33,8 +34,8 @@ public class S3SecretKeyCredential implements Credential {
/** The static secret access key used to access S3 data. */
public static final String GRAVITINO_S3_STATIC_SECRET_ACCESS_KEY = "s3-secret-access-key";

private final String accessKeyId;
private final String secretAccessKey;
private String accessKeyId;
private String secretAccessKey;

/**
* Constructs an instance of {@link S3SecretKeyCredential} with the static S3 access key ID and
Expand All @@ -44,13 +45,17 @@ public class S3SecretKeyCredential implements Credential {
* @param secretAccessKey The S3 static secret access key.
*/
public S3SecretKeyCredential(String accessKeyId, String secretAccessKey) {
Preconditions.checkNotNull(accessKeyId, "S3 access key Id should not null");
Preconditions.checkNotNull(secretAccessKey, "S3 secret access key should not null");

validate(accessKeyId, secretAccessKey, 0);
this.accessKeyId = accessKeyId;
this.secretAccessKey = secretAccessKey;
}

/**
* This is the constructor that is used by credential factory to create an instance of credential
* according to the credential information.
*/
public S3SecretKeyCredential() {}

@Override
public String credentialType() {
return S3_SECRET_KEY_CREDENTIAL_TYPE;
Expand All @@ -69,6 +74,15 @@ public Map<String, String> credentialInfo() {
.build();
}

@Override
public void initialize(Map<String, String> credentialInfo, long expireTimeInMs) {
String accessKeyId = credentialInfo.get(GRAVITINO_S3_STATIC_ACCESS_KEY_ID);
String secretAccessKey = credentialInfo.get(GRAVITINO_S3_STATIC_SECRET_ACCESS_KEY);
validate(accessKeyId, secretAccessKey, expireTimeInMs);
this.accessKeyId = accessKeyId;
this.secretAccessKey = secretAccessKey;
}

/**
* Get S3 static access key ID.
*
Expand All @@ -86,4 +100,13 @@ public String accessKeyId() {
public String secretAccessKey() {
return secretAccessKey;
}

private void validate(String accessKeyId, String secretAccessKey, long expireTimeInMs) {
Preconditions.checkArgument(
StringUtils.isNotBlank(accessKeyId), "S3 access key Id should not empty");
Preconditions.checkArgument(
StringUtils.isNotBlank(secretAccessKey), "S3 secret access key should not empty");
Preconditions.checkArgument(
expireTimeInMs == 0, "The expire time of S3SecretKeyCredential is not 0");
}
}
Loading

0 comments on commit 4d1af74

Please sign in to comment.