UnifiedMetaCacheMgr.java
// 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.doris.datasource.metacache;
import org.apache.doris.datasource.ExternalCatalog;
import org.apache.doris.datasource.ExternalTable;
import com.google.common.collect.ImmutableSet;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
/**
* Global metadata cache manager that provides one unified entrance for all engines.
*/
public class UnifiedMetaCacheMgr {
private final Map<Long, CatalogMetaCache> catalogMetaCaches = new ConcurrentHashMap<>();
private final Map<String, Function<ExternalCatalog, ? extends EngineMetaCache>> engineFactories =
new ConcurrentHashMap<>();
public void registerEngineFactory(String engineType,
Function<ExternalCatalog, ? extends EngineMetaCache> engineFactory) {
String normalizedEngineType = normalizeEngineType(engineType);
engineFactories.put(normalizedEngineType, Objects.requireNonNull(engineFactory, "engineFactory is null"));
}
public Function<ExternalCatalog, ? extends EngineMetaCache> removeEngineFactory(String engineType) {
return engineFactories.remove(normalizeEngineType(engineType));
}
public Set<String> getRegisteredEngineTypes() {
return ImmutableSet.copyOf(engineFactories.keySet());
}
public EngineMetaCache getOrCreateEngineMetaCache(ExternalCatalog catalog, String engineType) {
Objects.requireNonNull(catalog, "catalog cannot be null");
String normalizedEngineType = normalizeEngineType(engineType);
Function<ExternalCatalog, ? extends EngineMetaCache> engineFactory = engineFactories.get(normalizedEngineType);
if (engineFactory == null) {
throw new IllegalArgumentException("Engine cache factory is not registered: " + normalizedEngineType);
}
CatalogMetaCache catalogMetaCache = catalogMetaCaches.computeIfAbsent(catalog.getId(), CatalogMetaCache::new);
return catalogMetaCache.getOrCreateEngineMetaCache(normalizedEngineType,
() -> engineFactory.apply(catalog), catalog);
}
public EngineMetaCache getEngineMetaCache(long catalogId, String engineType) {
CatalogMetaCache catalogMetaCache = catalogMetaCaches.get(catalogId);
if (catalogMetaCache == null) {
return null;
}
return catalogMetaCache.getEngineMetaCache(engineType);
}
public CatalogMetaCache getCatalogMetaCache(long catalogId) {
return catalogMetaCaches.get(catalogId);
}
public CatalogMetaCache removeCatalogMetaCache(long catalogId) {
CatalogMetaCache catalogMetaCache = catalogMetaCaches.remove(catalogId);
if (catalogMetaCache != null) {
catalogMetaCache.invalidateCatalog();
}
return catalogMetaCache;
}
public void invalidateCatalogCache(long catalogId) {
CatalogMetaCache catalogMetaCache = catalogMetaCaches.get(catalogId);
if (catalogMetaCache != null) {
catalogMetaCache.invalidateCatalog();
}
}
public void invalidateDbCache(long catalogId, String dbName) {
CatalogMetaCache catalogMetaCache = catalogMetaCaches.get(catalogId);
if (catalogMetaCache != null) {
catalogMetaCache.invalidateDb(dbName);
}
}
public void invalidateTableCache(ExternalTable table) {
Objects.requireNonNull(table, "table cannot be null");
Objects.requireNonNull(table.getCatalog(), "table catalog cannot be null");
CatalogMetaCache catalogMetaCache = catalogMetaCaches.get(table.getCatalog().getId());
if (catalogMetaCache != null) {
catalogMetaCache.invalidateTable(table);
}
}
private static String normalizeEngineType(String engineType) {
String trimmedEngineType = Objects.requireNonNull(engineType, "engineType cannot be null").trim();
if (trimmedEngineType.isEmpty()) {
throw new IllegalArgumentException("engineType cannot be empty");
}
return trimmedEngineType.toLowerCase(Locale.ROOT);
}
}