morrySnow commented on code in PR #65644: URL: https://github.com/apache/doris/pull/65644#discussion_r3604588584
########## fe/fe-extension-loader/src/main/java/org/apache/doris/extension/loader/PluginRegistry.java: ########## @@ -0,0 +1,197 @@ +// 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.extension.loader; + +import org.apache.doris.extension.spi.PluginFactory; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.time.Instant; +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; +import java.util.Objects; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; + +/** + * Process-wide registry of loaded plugins across all plugin families + * (filesystem, connector, authentication, lineage, ...). + * + * <p>This is the single fact source behind {@code information_schema.plugins}. + * It only stores load-time snapshots (plain strings); listing the registry + * never executes plugin code. + * + * <p>Rules: + * <ul> + * <li>Primary key is {@code (type, name)}. The first registration wins; + * later registrations with the same key are rejected, never silently + * overridden. Family managers register built-ins before external plugins, + * so a directory jar can never displace a built-in row.</li> + * <li>Rows only exist for successfully loaded plugins. Load failures are + * reported via logs by the loading side and never enter the registry.</li> + * </ul> + */ +public final class PluginRegistry { + + /** Where a plugin was loaded from. */ + public enum PluginSource { + /** Bundled on the FE classpath, discovered via ServiceLoader. */ + BUILTIN, + /** Loaded from a plugin directory jar. */ + EXTERNAL + } + + /** Immutable load-time snapshot of one plugin. */ + public static final class PluginRecord { + private final String type; + private final String name; + private final String version; + private final String description; + private final PluginSource source; + private final Instant loadTime; + + public PluginRecord(String type, String name, String version, String description, + PluginSource source, Instant loadTime) { + this.type = Objects.requireNonNull(type, "type"); + this.name = Objects.requireNonNull(name, "name"); + this.version = version; + this.description = description == null ? "" : description; + this.source = Objects.requireNonNull(source, "source"); + this.loadTime = Objects.requireNonNull(loadTime, "loadTime"); + } + + public String getType() { + return type; + } + + public String getName() { + return name; + } + + /** Plugin release version from jar MANIFEST Implementation-Version, may be null. */ + public String getVersion() { + return version; + } + + public String getDescription() { + return description; + } + + public PluginSource getSource() { + return source; + } + + public Instant getLoadTime() { + return loadTime; + } + } + + private static final Logger LOG = LogManager.getLogger(PluginRegistry.class); + + private static final PluginRegistry INSTANCE = new PluginRegistry(); + + private final ConcurrentMap<String, PluginRecord> recordsByKey = new ConcurrentHashMap<>(); + + private PluginRegistry() { + } + + public static PluginRegistry getInstance() { + return INSTANCE; + } + + /** + * Registers a load-time snapshot for one plugin. + * + * <p>Rejects (returns false) when the name is invalid or the + * {@code (type, name)} key is already taken. Rejection never throws so a + * bad self-reported name cannot break FE startup paths that register + * built-ins; callers decide how to react. + * + * @param type plugin family label, e.g. "FILESYSTEM", "AUTHENTICATION" + * @param name plugin identity within the family + * @param version plugin release version, may be null when unknown + * @param description one-line description, may be null + * @param source BUILTIN or EXTERNAL + * @return true if the record was inserted, false if rejected + */ + public boolean register(String type, String name, String version, String description, PluginSource source) { + String validationError = PluginNames.validate(name); + if (validationError != null) { + LOG.warn("Reject plugin registration with invalid name: type={}, name={}, reason={}", + type, name, validationError); + return false; + } + PluginRecord record = new PluginRecord(type, name.trim(), version, description, source, Instant.now()); + PluginRecord existing = recordsByKey.putIfAbsent(key(record.getType(), record.getName()), record); + if (existing != null) { + // The same plugin may be legitimately loaded by more than one manager + // instance within a family (e.g. authentication); keep the first row. + LOG.info("Skip duplicated plugin registration: type={}, name={}, existingSource={}, newSource={}", Review Comment: warning ########## fe/fe-extension-loader/src/main/java/org/apache/doris/extension/loader/PluginNames.java: ########## @@ -0,0 +1,62 @@ +// 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.extension.loader; + +/** + * Validation rules for self-reported plugin names. + * + * <p>A plugin name is a configuration-facing identity key, so it is validated + * at load time: non-blank after trimming, bounded length, restricted charset. + * An invalid name is treated as a load failure by the loading side. + */ +final class PluginNames { + + static final int MAX_LENGTH = 64; + + private PluginNames() { + } + + /** + * Validates a self-reported plugin name. + * + * @param name raw value returned by {@code PluginFactory.name()} + * @return null if valid, otherwise a human-readable rejection reason + */ + static String validate(String name) { Review Comment: 使用 org.apache.doris.common.FeNameFormat 统一处理 -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
