FANNG1 commented on code in PR #5868:
URL: https://github.com/apache/gravitino/pull/5868#discussion_r1889524277
##########
flink-connector/flink/src/main/java/org/apache/gravitino/flink/connector/store/GravitinoCatalogStore.java:
##########
@@ -96,43 +105,56 @@ public boolean contains(String catalogName) throws
CatalogException {
return gravitinoCatalogManager.contains(catalogName);
}
- private String getGravitinoCatalogProvider(Configuration configuration) {
+ private BaseCatalogFactory catalogFactory(Map<String, String> configuration)
{
String catalogType =
Preconditions.checkNotNull(
- configuration.get(CommonCatalogOptions.CATALOG_TYPE),
+ configuration.get(CommonCatalogOptions.CATALOG_TYPE.key()),
"%s should not be null.",
CommonCatalogOptions.CATALOG_TYPE);
- switch (catalogType) {
- case GravitinoHiveCatalogFactoryOptions.IDENTIFIER:
- return "hive";
- default:
- throw new IllegalArgumentException(
- String.format("The catalog type is not supported:%s",
catalogType));
- }
+ return discoverFactories(
+ catalogFactory ->
(catalogFactory.factoryIdentifier().equalsIgnoreCase(catalogType)),
+ String.format(
+ "Flink catalog type [%s] matched multiple flink catalog factories,
it should only match one.",
+ catalogType));
}
- private Catalog.Type getGravitinoCatalogType(Configuration configuration) {
- String catalogType =
- Preconditions.checkNotNull(
- configuration.get(CommonCatalogOptions.CATALOG_TYPE),
- "%s should not be null.",
- CommonCatalogOptions.CATALOG_TYPE);
-
- switch (catalogType) {
- case GravitinoHiveCatalogFactoryOptions.IDENTIFIER:
- return Catalog.Type.RELATIONAL;
- default:
- throw new IllegalArgumentException(
- String.format("The catalog type is not supported:%s",
catalogType));
- }
+ private BaseCatalogFactory catalogFactory(String provider) {
+ return discoverFactories(
+ catalogFactory ->
+ ((BaseCatalogFactory) catalogFactory)
+ .gravitinoCatalogProvider()
+ .equalsIgnoreCase(provider),
+ String.format(
+ "Gravitino catalog provider [%s] matched multiple flink catalog
factories, it should only match one.",
+ provider));
}
- private PropertiesConverter getPropertiesConverter(String provider) {
- switch (provider) {
- case "hive":
- return HivePropertiesConverter.INSTANCE;
+ private BaseCatalogFactory discoverFactories(Predicate<Factory> predicate,
String errorMessage) {
+ Iterator<Factory> serviceLoaderIterator =
ServiceLoader.load(Factory.class).iterator();
+ final List<Factory> factories = new ArrayList<>();
+ while (true) {
+ try {
+ if (!serviceLoaderIterator.hasNext()) {
+ break;
+ }
+ Factory catalogFactory = serviceLoaderIterator.next();
+ if (catalogFactory instanceof BaseCatalogFactory &&
predicate.test(catalogFactory)) {
+ factories.add(catalogFactory);
+ }
+ } catch (Throwable t) {
+ if (t instanceof NoClassDefFoundError) {
+ LOG.debug(
Review Comment:
why using debug log?
##########
flink-connector/flink/src/main/java/org/apache/gravitino/flink/connector/store/GravitinoCatalogStore.java:
##########
@@ -96,43 +105,56 @@ public boolean contains(String catalogName) throws
CatalogException {
return gravitinoCatalogManager.contains(catalogName);
}
- private String getGravitinoCatalogProvider(Configuration configuration) {
+ private BaseCatalogFactory catalogFactory(Map<String, String> configuration)
{
String catalogType =
Preconditions.checkNotNull(
- configuration.get(CommonCatalogOptions.CATALOG_TYPE),
+ configuration.get(CommonCatalogOptions.CATALOG_TYPE.key()),
"%s should not be null.",
CommonCatalogOptions.CATALOG_TYPE);
- switch (catalogType) {
- case GravitinoHiveCatalogFactoryOptions.IDENTIFIER:
- return "hive";
- default:
- throw new IllegalArgumentException(
- String.format("The catalog type is not supported:%s",
catalogType));
- }
+ return discoverFactories(
+ catalogFactory ->
(catalogFactory.factoryIdentifier().equalsIgnoreCase(catalogType)),
+ String.format(
+ "Flink catalog type [%s] matched multiple flink catalog factories,
it should only match one.",
+ catalogType));
}
- private Catalog.Type getGravitinoCatalogType(Configuration configuration) {
- String catalogType =
- Preconditions.checkNotNull(
- configuration.get(CommonCatalogOptions.CATALOG_TYPE),
- "%s should not be null.",
- CommonCatalogOptions.CATALOG_TYPE);
-
- switch (catalogType) {
- case GravitinoHiveCatalogFactoryOptions.IDENTIFIER:
- return Catalog.Type.RELATIONAL;
- default:
- throw new IllegalArgumentException(
- String.format("The catalog type is not supported:%s",
catalogType));
- }
+ private BaseCatalogFactory catalogFactory(String provider) {
+ return discoverFactories(
+ catalogFactory ->
+ ((BaseCatalogFactory) catalogFactory)
+ .gravitinoCatalogProvider()
+ .equalsIgnoreCase(provider),
+ String.format(
+ "Gravitino catalog provider [%s] matched multiple flink catalog
factories, it should only match one.",
+ provider));
}
- private PropertiesConverter getPropertiesConverter(String provider) {
- switch (provider) {
- case "hive":
- return HivePropertiesConverter.INSTANCE;
+ private BaseCatalogFactory discoverFactories(Predicate<Factory> predicate,
String errorMessage) {
+ Iterator<Factory> serviceLoaderIterator =
ServiceLoader.load(Factory.class).iterator();
+ final List<Factory> factories = new ArrayList<>();
+ while (true) {
+ try {
+ if (!serviceLoaderIterator.hasNext()) {
+ break;
+ }
+ Factory catalogFactory = serviceLoaderIterator.next();
+ if (catalogFactory instanceof BaseCatalogFactory &&
predicate.test(catalogFactory)) {
+ factories.add(catalogFactory);
+ }
+ } catch (Throwable t) {
+ if (t instanceof NoClassDefFoundError) {
+ LOG.debug(
+ "NoClassDefFoundError when loading a " +
Factory.class.getCanonicalName() + ".", t);
+ } else {
+ throw new RuntimeException("Unexpected error when trying to load
service provider.", t);
+ }
+ }
+ }
+ // It should only match one.
+ if (factories.size() > 1) {
+ throw new RuntimeException(errorMessage);
}
- throw new IllegalArgumentException("The provider is not supported:" +
provider);
+ return (BaseCatalogFactory) factories.get(0);
Review Comment:
we'd better check whether factories is empty.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]