DorisExternalMetaCache.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.doris;

import org.apache.doris.catalog.Env;
import org.apache.doris.common.Config;
import org.apache.doris.datasource.CacheException;
import org.apache.doris.datasource.CatalogIf;
import org.apache.doris.datasource.ExternalCatalog;
import org.apache.doris.datasource.ExternalTable;
import org.apache.doris.datasource.SchemaCacheKey;
import org.apache.doris.datasource.SchemaCacheValue;
import org.apache.doris.datasource.metacache.AbstractExternalMetaCache;
import org.apache.doris.datasource.metacache.CacheSpec;
import org.apache.doris.datasource.metacache.MetaCacheEntry;
import org.apache.doris.datasource.metacache.MetaCacheEntryDef;
import org.apache.doris.system.Backend;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.stream.Collectors;

/**
 * Remote Doris engine implementation of {@link AbstractExternalMetaCache}.
 *
 * <p>Registered entries:
 * <ul>
 *   <li>{@code backends}: remote backend topology keyed by catalog id</li>
 *   <li>{@code schema}: schema cache keyed by {@link SchemaCacheKey}</li>
 * </ul>
 *
 * <p>The backend cache is intentionally independent from table/db invalidation and can be
 * refreshed explicitly via {@link #invalidateBackendCache(long)}.
 *
 * <p>db/table/partition invalidation only targets schema entries.
 */
public class DorisExternalMetaCache extends AbstractExternalMetaCache {
    private static final Logger LOG = LogManager.getLogger(DorisExternalMetaCache.class);

    public static final String ENGINE = "doris";
    public static final String ENTRY_BACKENDS = "backends";
    public static final String ENTRY_SCHEMA = "schema";
    private static final CacheSpec BACKENDS_CACHE_SPEC = CacheSpec.fromTtlValue(
            null, Config.external_cache_expire_time_seconds_after_access, 20);
    private static final CacheSpec SCHEMA_CACHE_SPEC = CacheSpec.fromTtlValue(
            null, Config.external_cache_expire_time_seconds_after_access, Config.max_external_schema_cache_num);
    @SuppressWarnings("unchecked")
    private static final Class<ImmutableMap<Long, Backend>> BACKEND_MAP_CLASS =
            (Class<ImmutableMap<Long, Backend>>) (Class<?>) ImmutableMap.class;

    private final MetaCacheEntryDef<Long, ImmutableMap<Long, Backend>> backendsEntryDef;
    private final MetaCacheEntryDef<SchemaCacheKey, SchemaCacheValue> schemaEntryDef;

    public DorisExternalMetaCache(ExecutorService refreshExecutor) {
        super(ENGINE, refreshExecutor);
        backendsEntryDef = MetaCacheEntryDef.of(
                ENTRY_BACKENDS,
                Long.class,
                BACKEND_MAP_CLASS,
                this::loadBackends,
                BACKENDS_CACHE_SPEC);
        schemaEntryDef = MetaCacheEntryDef.of(
                ENTRY_SCHEMA,
                SchemaCacheKey.class,
                SchemaCacheValue.class,
                this::loadSchemaCacheValue,
                SCHEMA_CACHE_SPEC);
        registerMetaCacheEntryDef(backendsEntryDef);
        registerMetaCacheEntryDef(schemaEntryDef);
    }

    public ImmutableMap<Long, Backend> getBackends(long catalogId) {
        ImmutableMap<Long, Backend> backends = backendsEntry(catalogId).get(catalogId);
        return backends == null ? ImmutableMap.of() : backends;
    }

    public void invalidateBackendCache(long catalogId) {
        try {
            backendsEntry(catalogId).invalidateKey(catalogId);
        } catch (IllegalStateException e) {
            // The catalog may be removed before external cache initialization.
        }
    }

    @Override
    public void invalidateDb(long catalogId, String dbName) {
        schemaEntry(catalogId).invalidateIf(key -> key.getNameMapping().getLocalDbName().equals(dbName));
    }

    @Override
    public void invalidateTable(long catalogId, String dbName, String tableName) {
        schemaEntry(catalogId).invalidateIf(key -> key.getNameMapping().getLocalDbName().equals(dbName)
                && key.getNameMapping().getLocalTblName().equals(tableName));
    }

    @Override
    public void invalidatePartitions(long catalogId, String dbName, String tableName, List<String> partitions) {
        invalidateTable(catalogId, dbName, tableName);
    }

    private ImmutableMap<Long, Backend> loadBackends(Long catalogId) {
        RemoteDorisExternalCatalog catalog = (RemoteDorisExternalCatalog) Env.getCurrentEnv().getCatalogMgr()
                .getCatalog(catalogId);
        List<Backend> backends = catalog.getFeServiceClient().listBackends();
        if (LOG.isDebugEnabled()) {
            List<String> names = backends.stream().map(Backend::getAddress).collect(Collectors.toList());
            LOG.debug("load backends:{} from:{}", String.join(",", names), catalog.getName());
        }
        Map<Long, Backend> backendMap = Maps.newHashMap();
        backends.forEach(backend -> backendMap.put(backend.getId(), backend));
        return ImmutableMap.copyOf(backendMap);
    }

    private MetaCacheEntry<Long, ImmutableMap<Long, Backend>> backendsEntry(long catalogId) {
        return entry(catalogId, backendsEntryDef);
    }

    private MetaCacheEntry<SchemaCacheKey, SchemaCacheValue> schemaEntry(long catalogId) {
        return entry(catalogId, schemaEntryDef);
    }

    private SchemaCacheValue loadSchemaCacheValue(SchemaCacheKey key) {
        ExternalTable dorisTable = findExternalTable(key);
        return dorisTable.initSchemaAndUpdateTime(key).orElseThrow(() ->
                new CacheException("failed to load doris schema cache value for: %s.%s.%s",
                        null, key.getNameMapping().getCtlId(), key.getNameMapping().getLocalDbName(),
                        key.getNameMapping().getLocalTblName()));
    }

    @Override
    protected Map<String, String> catalogPropertyCompatibilityMap() {
        return java.util.Collections.singletonMap(
                ExternalCatalog.SCHEMA_CACHE_TTL_SECOND,
                "meta.cache." + ENGINE + "." + ENTRY_SCHEMA + ".ttl-second");
    }

    private ExternalTable findExternalTable(SchemaCacheKey key) {
        CatalogIf<?> catalog = Env.getCurrentEnv().getCatalogMgr().getCatalog(key.getNameMapping().getCtlId());
        if (!(catalog instanceof ExternalCatalog)) {
            throw new CacheException("catalog %s is not external when loading doris schema cache",
                    null, key.getNameMapping().getCtlId());
        }
        ExternalCatalog externalCatalog = (ExternalCatalog) catalog;
        return externalCatalog.getDb(key.getNameMapping().getLocalDbName())
                .flatMap(db -> db.getTable(key.getNameMapping().getLocalTblName()))
                .orElseThrow(() -> new CacheException(
                        "table %s.%s.%s not found when loading doris schema cache",
                        null, key.getNameMapping().getCtlId(), key.getNameMapping().getLocalDbName(),
                        key.getNameMapping().getLocalTblName()));
    }
}