bowenli86 commented on a change in pull request #8404: [FLINK-11476][table] Create CatalogManager to manage multiple catalogs URL: https://github.com/apache/flink/pull/8404#discussion_r283063574
########## File path: flink-table/flink-table-planner/src/main/java/org/apache/flink/table/catalog/CatalogManager.java ########## @@ -0,0 +1,315 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.table.catalog; + +import org.apache.flink.annotation.Internal; +import org.apache.flink.table.api.CatalogAlreadyExistsException; +import org.apache.flink.table.api.CatalogNotExistException; +import org.apache.flink.table.api.ExternalCatalogAlreadyExistException; +import org.apache.flink.table.api.TableSchema; +import org.apache.flink.table.catalog.exceptions.CatalogException; +import org.apache.flink.table.catalog.exceptions.DatabaseNotExistException; +import org.apache.flink.table.catalog.exceptions.TableNotExistException; +import org.apache.flink.table.operations.CatalogTableOperation; +import org.apache.flink.util.StringUtils; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; + +import static org.apache.flink.util.Preconditions.checkArgument; +import static org.apache.flink.util.Preconditions.checkNotNull; + +/** + * A CatalogManager that encapsulates all available catalogs. It also implements the logic of + * table path resolution. Supports both new API ({@link ReadableCatalog} as well as {@link ExternalCatalog}. + */ +@Internal +public class CatalogManager { + private static final Logger LOG = LoggerFactory.getLogger(CatalogManager.class); + + // A map between names and catalogs. + private Map<String, Catalog> catalogs; + + // TO BE REMOVED along with ExternalCatalog API + private Map<String, ExternalCatalog> externalCatalogs; + + // The name of the default catalog and schema + private String currentCatalogName; + + private String currentDatabaseName; + + public CatalogManager(String defaultCatalogName, Catalog defaultCatalog) { + catalogs = new LinkedHashMap<>(); + externalCatalogs = new LinkedHashMap<>(); + catalogs.put(defaultCatalogName, defaultCatalog); + this.currentCatalogName = defaultCatalogName; + this.currentDatabaseName = defaultCatalog.getCurrentDatabase(); + } + + /** + * Registers a catalog under the given name. The catalog name must be unique across both + * {@link Catalog}s and {@link ExternalCatalog}s. + * + * @param catalogName name under which to register the given catalog + * @param catalog catalog to register + * @throws CatalogAlreadyExistsException thrown if the name is already taken + */ + public void registerCatalog(String catalogName, Catalog catalog) throws CatalogAlreadyExistsException { + checkArgument(!StringUtils.isNullOrWhitespaceOnly(catalogName), "Catalog name cannot be null or empty."); + checkNotNull(catalog, "Catalog cannot be null"); + + if (catalogs.containsKey(catalogName) || externalCatalogs.containsKey(catalogName)) { + throw new CatalogAlreadyExistsException(catalogName); + } + + catalogs.put(catalogName, catalog); + catalog.open(); + } + + /** + * Gets a catalog by name. + * + * @param catalogName name of the catalog to retrieve + * @return the requested catalog + * @throws CatalogNotExistException thrown if the catalog doesn't exist + * @see CatalogManager#getExternalCatalog(String) + */ + public Catalog getCatalog(String catalogName) throws CatalogNotExistException { + if (!catalogs.keySet().contains(catalogName)) { + throw new CatalogNotExistException(catalogName); + } + + return catalogs.get(catalogName); + } + + /** + * Registers an external catalog under the given name. The catalog name must be unique across both + * {@link Catalog}s and {@link ExternalCatalog}s. + * + * @param catalogName name under which to register the given catalog + * @param catalog catalog to register + * @throws ExternalCatalogAlreadyExistException thrown if the name is already taken + * @deprecated {@link ExternalCatalog} APIs will be dropped + */ + @Deprecated + public void registerExternalCatalog(String catalogName, ExternalCatalog catalog) { + checkArgument(!StringUtils.isNullOrWhitespaceOnly(catalogName), "catalogName cannot be null or empty"); + checkNotNull(catalog, "catalog cannot be null"); + + if (externalCatalogs.containsKey(catalogName) || catalogs.containsKey(catalogName)) { + throw new ExternalCatalogAlreadyExistException(catalogName); + } + + externalCatalogs.put(catalogName, catalog); + } + + /** + * Gets an external catalog by name. + * + * @param externalCatalogName name of the catalog to retrieve + * @return the requested external catalog + * @see CatalogManager#getCatalog(String) + * @deprecated {@link ExternalCatalog} APIs will be dropped + */ + @Deprecated + public Optional<ExternalCatalog> getExternalCatalog(String externalCatalogName) { + return Optional.ofNullable(externalCatalogs.get(externalCatalogName)); + } + + /** + * Retrieves names of all registered catalogs. It does not include {@link ExternalCatalog}s. + * + * @return a set of names of registered catalogs + * @see CatalogManager#getExternalCatalogNames() + */ + public Set<String> getCatalogNames() { + return catalogs.keySet(); + } + + /** + * Retrieves names of all registered external catalogs. It does not include {@link Catalog}s. + * + * @return a set of names of registered catalogs + * @see CatalogManager#getCatalogNames() + * @deprecated {@link ExternalCatalog} APIs will be dropped + */ + @Deprecated + public Set<String> getExternalCatalogNames() { + return externalCatalogs.keySet(); + } + + /** + * Gets the current default catalog that will be used when resolving table path. + * + * @return the current default catalog + * @see CatalogManager#resolveTable(String...) + */ + public String getCurrentCatalogName() { + return currentCatalogName; + } + + /** + * Sets the current default catalog name that will be used when resolving table path. + * + * @param catalogName catalog name to set as current default catalog + * @throws CatalogNotExistException thrown if the catalog doesn't exist + * @see CatalogManager#resolveTable(String...) + */ + public void setCurrentCatalog(String catalogName) throws CatalogNotExistException { + checkArgument(!StringUtils.isNullOrWhitespaceOnly(catalogName), "Catalog name cannot be null or empty."); + + if (externalCatalogs.keySet().contains(catalogName)) { + throw new CatalogException("An external catalog cannot be set as the default one."); + } + + Catalog potentialCurrentCatalog = catalogs.get(catalogName); + if (potentialCurrentCatalog == null) { + throw new CatalogNotExistException(catalogName); + } + + if (!currentCatalogName.equals(catalogName)) { + currentCatalogName = catalogName; + currentDatabaseName = potentialCurrentCatalog.getCurrentDatabase(); + + LOG.info( + "Sets the current default catalog as '{}' and the current default database as '{}'", + currentCatalogName, + currentDatabaseName); + } + } + + /** + * Gets the current default database name that will be used when resolving table path. + * + * @return the current default database + * @see CatalogManager#resolveTable(String...) + */ + public String getCurrentDatabaseName() { + return currentDatabaseName; + } + + /** + * Sets the current default catalog name that will be used when resolving table path. + * The database has to exist in the current catalog. + * + * @param databaseName database name to set as current default database name + * @throws DatabaseNotExistException thrown if the database doesn't exist in the current catalog + * @see CatalogManager#resolveTable(String...) + * @see CatalogManager#setCurrentCatalog(String) + */ + public void setCurrentDatabase(String databaseName) throws DatabaseNotExistException { + checkArgument(!StringUtils.isNullOrWhitespaceOnly(databaseName), "The database name cannot be null or empty"); + + if (!catalogs.get(currentCatalogName).databaseExists(databaseName)) { + throw new DatabaseNotExistException(currentCatalogName, databaseName); + } + + if (!currentDatabaseName.equals(databaseName)) { + currentDatabaseName = databaseName; + + LOG.info( + "Sets the current default catalog as '{}' and the current default database as '{}'", Review comment: shall we rephrase the logging msg to be different than that in "setCurrentCatalog"? because 1) it doesn't actually set the current catalog, 2) to differentiate from calling of "setCurrentCatalog" can be sth like "Sets the current default database as '{}' in the current default catalog '{}'"? ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services