github-actions[bot] commented on code in PR #65644: URL: https://github.com/apache/doris/pull/65644#discussion_r3602424893
########## 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={}", + record.getType(), record.getName(), existing.getSource(), source); + return false; + } + LOG.info("Registered plugin: type={}, name={}, version={}, source={}", + record.getType(), record.getName(), version, source); + return true; + } + + /** + * Registers a classpath built-in factory. Snapshots {@code name()} and + * {@code description()} now; the version comes from the Implementation-Version + * of the jar that bundles the factory class, when available. + */ + public boolean registerBuiltin(String type, PluginFactory factory) { + return register(type, factory.name(), implementationVersionOf(factory.getClass()), + factory.description(), PluginSource.BUILTIN); + } + + /** Registers a directory-loaded plugin from its load-time handle snapshot. */ + public boolean registerExternal(String type, PluginHandle<?> handle) { + return register(type, handle.getPluginName(), handle.getImplementationVersion(), + handle.getDescription(), PluginSource.EXTERNAL); + } + + /** Returns a snapshot of all registered plugins sorted by (type, name). */ + public List<PluginRecord> list() { + List<PluginRecord> records = new ArrayList<>(recordsByKey.values()); + records.sort(Comparator.comparing(PluginRecord::getType).thenComparing(PluginRecord::getName)); + return records; + } + + /** Removes all records. Only for tests. */ + public void clearForTest() { + recordsByKey.clear(); + } + + /** + * Best-effort release version of a classpath (built-in) plugin, from the + * Implementation-Version of the jar that contains the given class. + * + * @return the version, or null when unavailable (e.g. classes directory) + */ + public static String implementationVersionOf(Class<?> clazz) { + Package pkg = clazz.getPackage(); Review Comment: [P2] Read each built-in version from its defining jar. Class.getPackage() returns the one Package object for a package name/classloader; with two ServiceLoader factories in the same package but different jars, the first class to define that package fixes its manifest metadata, so the later plugin row reports the first jar's Implementation-Version. That violates this method's defining-jar contract and differs from the external path, which already resolves CodeSource. Reuse the defining-jar manifest helper here and cover two same-package factory jars with different versions. -- 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]
