2424import org .apache .doris .connector .spi .ConnectorProvider ;
2525import org .apache .doris .connector .spi .ConnectorSecuritySurface ;
2626import org .apache .doris .connector .spi .ConnectorSession ;
27+ import org .apache .doris .connectorconf .testplugins .BackendDiscoveringConnectorProvider ;
2728import 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
2933import org .junit .jupiter .api .Assertions ;
3034import 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 );
0 commit comments