Skip to content

Commit ae2cebe

Browse files
morningmanclaude
andcommitted
[fix](catalog) Skip a connector plugin whose security surface fails in its own ServiceLoader
### Background FE reads every connector provider's security surface at startup, before it publishes the provider, and skips a provider whose surface throws, so that one broken plugin directory does not stop FE. The Hive, Iceberg and Paimon providers build theirs from their metastore backends' secret keys, which `MetaStoreProviders` collects in a static initializer that runs a `ServiceLoader` over the backends the plugin's jars register. ### What problem does this PR solve? Issue Number: None Related PR: None PR Type: Bugfix Problem Summary: A `ServiceLoader` that cannot load a backend its descriptors name throws `ServiceConfigurationError`, an `Error` that is neither a `RuntimeException` nor a `LinkageError`, and a static initializer rethrows an `Error` as it is. The plugin directory path caught only those two and the classpath path only `RuntimeException`, so a descriptor naming a backend that is not there, such as one in a jar an earlier release left in a connector's `lib/`, stopped FE at startup; before the previous commit, which reads the surfaces at startup, it failed only that connector's catalogs. Both paths now skip the provider on a `ServiceConfigurationError` as well, and the plugins next to it still load. ### Release note None Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent b0bfd3c commit ae2cebe

3 files changed

Lines changed: 150 additions & 11 deletions

File tree

‎fe/fe-core/src/main/java/org/apache/doris/connector/ConnectorPluginManager.java‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,11 @@ void loadBuiltins(ClassLoader classLoader) {
195195
try {
196196
// Snapshot self-reported metadata (the security surface included) before publishing the
197197
// provider so one throwing implementation is rejected cleanly instead of
198-
// aborting startup or being active without an inventory row.
198+
// aborting startup or being active without an inventory row. A surface built through the
199+
// provider's own ServiceLoader fails with ServiceConfigurationError, as construction can.
199200
securitySurface = provider.securitySurface();
200201
PluginRegistry.getInstance().registerBuiltin(PLUGIN_FAMILY, provider);
201-
} catch (RuntimeException e) {
202+
} catch (ServiceConfigurationError | RuntimeException e) {
202203
LOG.warn("Skip built-in connector provider {}: self-reported metadata failed",
203204
providerClass.getName(), e);
204205
continue;
@@ -333,12 +334,14 @@ public void loadPlugins(List<Path> pluginRoots) {
333334
continue;
334335
}
335336
ConnectorProvider provider = handle.getFactory();
336-
// Snapshot the security surface before publishing anything: it is plugin code, a connector may
337-
// build it through its own ServiceLoader, and one bad plugin dir must not stop FE.
337+
// Snapshot the security surface before publishing anything: it is plugin code, and one bad plugin
338+
// dir must not stop FE. A connector may build it through its own ServiceLoader, as Hive, Iceberg and
339+
// Paimon do for their metastore backends, and a descriptor naming a backend that cannot be loaded
340+
// fails that with ServiceConfigurationError, an Error.
338341
ConnectorSecuritySurface securitySurface;
339342
try {
340343
securitySurface = provider.securitySurface();
341-
} catch (RuntimeException | LinkageError e) {
344+
} catch (RuntimeException | LinkageError | ServiceConfigurationError e) {
342345
runtimeManager.discard(handle.getPluginName());
343346
LOG.warn("Skip connector plugin '{}' from {}: securitySurface() failed",
344347
handle.getPluginName(), handle.getPluginDir(), e);

‎fe/fe-core/src/test/java/org/apache/doris/connector/ConnectorPluginManagerTest.java‎

Lines changed: 73 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@
2424
import org.apache.doris.connector.spi.ConnectorProvider;
2525
import org.apache.doris.connector.spi.ConnectorSecuritySurface;
2626
import org.apache.doris.connector.spi.ConnectorSession;
27+
import org.apache.doris.connectorconf.testplugins.BackendDiscoveringConnectorProvider;
2728
import org.apache.doris.datasource.CatalogFactory;
29+
import org.apache.doris.extension.loader.PluginRegistry;
30+
import org.apache.doris.pluginapiversion.testplugins.VersionProbeConnectorProvider;
31+
import org.apache.doris.utframe.PluginJarWriter;
2832

2933
import org.junit.jupiter.api.Assertions;
3034
import org.junit.jupiter.api.BeforeEach;
@@ -340,8 +344,8 @@ private static int kernelApiMajor() throws IOException {
340344

341345
@Test
342346
void classpathProviderWhoseSecuritySurfaceThrowsIsSkippedNotFatal(@TempDir Path tempDir) throws Exception {
343-
// A connector may build its security surface through its own ServiceLoader. When that throws, FE skips
344-
// the provider, as it does a filesystem provider whose sensitivePropertyKeys() throws, and starts.
347+
// A connector's security surface is plugin code. When it throws, FE skips the provider, as it does a
348+
// filesystem provider whose sensitivePropertyKeys() throws, and starts.
345349
Path jar = createClasspathProviderJar(tempDir.resolve("throwing-surface.jar"), kernelApiMajor() + ".0",
346350
ThrowingSurfaceClasspathProvider.class);
347351
ConnectorPluginManager loading = new ConnectorPluginManager();
@@ -351,6 +355,49 @@ void classpathProviderWhoseSecuritySurfaceThrowsIsSkippedNotFatal(@TempDir Path
351355
Assertions.assertFalse(loading.getRegisteredTypes().contains("throwing-surface-test"));
352356
}
353357

358+
@Test
359+
void classpathProviderWhoseSecuritySurfaceFailsInItsOwnServiceLoaderIsSkippedNotFatal(@TempDir Path tempDir)
360+
throws Exception {
361+
// Hive, Iceberg and Paimon read their metastore backends through a ServiceLoader to build their security
362+
// surfaces. A service descriptor naming a backend that is not there fails that lookup with
363+
// ServiceConfigurationError, an Error: FE must skip the provider and start all the same.
364+
Path jar = createClasspathProviderJar(tempDir.resolve("backend-discovering.jar"), kernelApiMajor() + ".0",
365+
BackendDiscoveringConnectorProvider.class);
366+
Path leftBehind = writeBackendDescriptorJar(tempDir.resolve("left-behind.jar"));
367+
ConnectorPluginManager loading = new ConnectorPluginManager();
368+
try (URLClassLoader loader =
369+
providerClassLoader(jar, BackendDiscoveringConnectorProvider.class, leftBehind)) {
370+
Assertions.assertDoesNotThrow(() -> loading.loadBuiltins(loader));
371+
}
372+
Assertions.assertFalse(loading.getRegisteredTypes().contains(BackendDiscoveringConnectorProvider.TYPE));
373+
}
374+
375+
@Test
376+
void pluginWhoseSecuritySurfaceFailsInItsOwnServiceLoaderIsSkippedNotFatal(@TempDir Path tempDir)
377+
throws Exception {
378+
// The same failure from a plugin directory, as a jar left in lib/ by an earlier release causes when its
379+
// descriptor names a backend the plugin no longer ships. loadPlugins promises partial success: the plugin
380+
// is skipped, and the plugin next to it still loads.
381+
String apiVersion = kernelApiMajor() + ".0";
382+
Path root = tempDir.resolve("connector-plugins");
383+
PluginJarWriter.writePluginRoot(root, "backend-discovering", BackendDiscoveringConnectorProvider.class,
384+
ConnectorProvider.class, "Doris-Connector-Plugin-Api-Version", apiVersion,
385+
Collections.singletonList(BackendDiscoveringConnectorProvider.Backend.class));
386+
writeBackendDescriptorJar(root.resolve("backend-discovering").resolve("lib").resolve("left-behind.jar"));
387+
PluginJarWriter.writePluginRoot(root, "version-probe", VersionProbeConnectorProvider.class,
388+
ConnectorProvider.class, "Doris-Connector-Plugin-Api-Version", apiVersion, Collections.emptyList());
389+
ConnectorPluginManager loading = new ConnectorPluginManager();
390+
try {
391+
Assertions.assertDoesNotThrow(() -> loading.loadPlugins(Collections.singletonList(root)));
392+
Assertions.assertFalse(loading.getRegisteredTypes().contains(BackendDiscoveringConnectorProvider.TYPE));
393+
Assertions.assertTrue(loading.getRegisteredTypes().contains("version_probe"),
394+
loading.getRegisteredTypes().toString());
395+
} finally {
396+
// loadPlugins writes inventory rows into a process-wide singleton.
397+
PluginRegistry.getInstance().clearForTest();
398+
}
399+
}
400+
354401
@Test
355402
void incompatibleClasspathProviderIsRejectedBeforeConstruction(@TempDir Path tempDir) throws Exception {
356403
System.clearProperty(CLASSPATH_PROVIDER_CONSTRUCTED);
@@ -462,7 +509,7 @@ public Connector create(Map<String, String> properties, ConnectorContext context
462509

463510
@Override
464511
public ConnectorSecuritySurface securitySurface() {
465-
throw new IllegalStateException("the connector's metastore providers failed to load");
512+
throw new IllegalStateException("the connector could not build its security surface");
466513
}
467514
}
468515

@@ -498,9 +545,14 @@ private static URLClassLoader providerClassLoader(Path jarPath) throws IOExcepti
498545
return providerClassLoader(jarPath, ClasspathProvider.class);
499546
}
500547

501-
private static URLClassLoader providerClassLoader(Path jarPath, Class<? extends ConnectorProvider> providerClass)
502-
throws IOException {
503-
return new URLClassLoader(new URL[] {jarPath.toUri().toURL()}, ConnectorProvider.class.getClassLoader()) {
548+
private static URLClassLoader providerClassLoader(Path jarPath, Class<? extends ConnectorProvider> providerClass,
549+
Path... moreJars) throws IOException {
550+
URL[] urls = new URL[moreJars.length + 1];
551+
urls[0] = jarPath.toUri().toURL();
552+
for (int i = 0; i < moreJars.length; i++) {
553+
urls[i + 1] = moreJars[i].toUri().toURL();
554+
}
555+
return new URLClassLoader(urls, ConnectorProvider.class.getClassLoader()) {
504556
@Override
505557
protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
506558
if (name.equals(providerClass.getName())) {
@@ -520,6 +572,21 @@ protected Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundE
520572
};
521573
}
522574

575+
/**
576+
* Writes a jar holding only a service descriptor for {@link BackendDiscoveringConnectorProvider.Backend} that
577+
* names a class no jar has, as a jar left behind by an earlier release would.
578+
*/
579+
private static Path writeBackendDescriptorJar(Path jarPath) throws IOException {
580+
Files.createDirectories(jarPath.getParent());
581+
try (JarOutputStream jar = new JarOutputStream(Files.newOutputStream(jarPath))) {
582+
jar.putNextEntry(new JarEntry(
583+
"META-INF/services/" + BackendDiscoveringConnectorProvider.Backend.class.getName()));
584+
jar.write("org.apache.doris.connectorconf.testplugins.RemovedBackend\n".getBytes(StandardCharsets.UTF_8));
585+
jar.closeEntry();
586+
}
587+
return jarPath;
588+
}
589+
523590
/** Admits {@code provider} as both loading paths do: with its security surface read up front. */
524591
private boolean admit(ConnectorProvider provider, boolean failFast) {
525592
return manager.registerDiscovered(provider, provider.securitySurface(), failFast);
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.apache.doris.connectorconf.testplugins;
19+
20+
import org.apache.doris.connector.spi.Connector;
21+
import org.apache.doris.connector.spi.ConnectorContext;
22+
import org.apache.doris.connector.spi.ConnectorProvider;
23+
import org.apache.doris.connector.spi.ConnectorSecuritySurface;
24+
25+
import java.util.ArrayList;
26+
import java.util.List;
27+
import java.util.Map;
28+
import java.util.ServiceLoader;
29+
30+
/**
31+
* A connector provider that builds its security surface from backends it discovers through its own
32+
* {@link ServiceLoader}, the way the Hive, Iceberg and Paimon providers declare their metastore backends' secret
33+
* keys through {@code MetaStoreProviders}. Its class bytes are copied into a temporary jar, so a test can load it
34+
* the way FE loads a shipped connector.
35+
*
36+
* <p>When a service descriptor in the provider's jars names a backend that cannot be loaded, the lookup throws
37+
* {@link java.util.ServiceConfigurationError}, which is an {@link Error} and neither a {@link RuntimeException}
38+
* nor a {@link LinkageError}.
39+
*
40+
* <p>It lives outside {@code org.apache.doris.connector.} for the reason {@link ConfProbeConnectorProviderA} does.
41+
*/
42+
public class BackendDiscoveringConnectorProvider implements ConnectorProvider {
43+
44+
public static final String TYPE = "backend_discovering";
45+
46+
/** What the provider's backends implement; its jars register them under this interface's name. */
47+
public interface Backend {
48+
String sensitiveKey();
49+
}
50+
51+
@Override
52+
public String getType() {
53+
return TYPE;
54+
}
55+
56+
@Override
57+
public Connector create(Map<String, String> properties, ConnectorContext context) {
58+
throw new UnsupportedOperationException(TYPE + " exists to be admitted or skipped at load time");
59+
}
60+
61+
@Override
62+
public ConnectorSecuritySurface securitySurface() {
63+
List<String> keys = new ArrayList<>();
64+
for (Backend backend : ServiceLoader.load(Backend.class, getClass().getClassLoader())) {
65+
keys.add(backend.sensitiveKey());
66+
}
67+
return ConnectorSecuritySurface.EMPTY.withSensitiveKeys(keys);
68+
}
69+
}

0 commit comments

Comments
 (0)