Skip to content

Commit

Permalink
Set thread context class loader for all plugin factories
Browse files Browse the repository at this point in the history
  • Loading branch information
electrum committed Jul 17, 2021
1 parent 39fb227 commit be94b0c
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,12 @@ private EventListener createEventListener(File configFile)
String name = properties.remove(EVENT_LISTENER_NAME_PROPERTY);
checkArgument(!isNullOrEmpty(name), "EventListener plugin configuration for %s does not contain %s", configFile, EVENT_LISTENER_NAME_PROPERTY);

EventListenerFactory eventListenerFactory = eventListenerFactories.get(name);
checkArgument(eventListenerFactory != null, "Event listener factory '%s' is not registered. Available factories: %s", name, eventListenerFactories.keySet());
EventListenerFactory factory = eventListenerFactories.get(name);
checkArgument(factory != null, "Event listener factory '%s' is not registered. Available factories: %s", name, eventListenerFactories.keySet());

EventListener eventListener;
try (ThreadContextClassLoader ignored = new ThreadContextClassLoader(eventListenerFactory.getClass().getClassLoader())) {
eventListener = eventListenerFactory.create(properties);
try (ThreadContextClassLoader ignored = new ThreadContextClassLoader(factory.getClass().getClassLoader())) {
eventListener = factory.create(properties);
}

log.info("-- Loaded event listener %s --", configFile);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.trino.execution.ManagedQueryExecution;
import io.trino.server.ResourceGroupInfo;
import io.trino.spi.TrinoException;
import io.trino.spi.classloader.ThreadContextClassLoader;
import io.trino.spi.memory.ClusterMemoryPoolManager;
import io.trino.spi.resourcegroups.ResourceGroupConfigurationManager;
import io.trino.spi.resourcegroups.ResourceGroupConfigurationManagerContext;
Expand Down Expand Up @@ -154,10 +155,14 @@ public void setConfigurationManager(String name, Map<String, String> properties)

log.info("-- Loading resource group configuration manager --");

ResourceGroupConfigurationManagerFactory configurationManagerFactory = configurationManagerFactories.get(name);
checkState(configurationManagerFactory != null, "Resource group configuration manager '%s' is not registered", name);
ResourceGroupConfigurationManagerFactory factory = configurationManagerFactories.get(name);
checkState(factory != null, "Resource group configuration manager '%s' is not registered", name);

ResourceGroupConfigurationManager<C> configurationManager;
try (ThreadContextClassLoader ignored = new ThreadContextClassLoader(factory.getClass().getClassLoader())) {
configurationManager = cast(factory.create(ImmutableMap.copyOf(properties), configurationManagerContext));
}

ResourceGroupConfigurationManager<C> configurationManager = cast(configurationManagerFactory.create(ImmutableMap.copyOf(properties), configurationManagerContext));
checkState(this.configurationManager.compareAndSet(cast(legacyManager), configurationManager), "configurationManager already set");

log.info("-- Loaded resource group configuration manager %s --", name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import io.trino.plugin.base.security.ReadOnlySystemAccessControl;
import io.trino.spi.QueryId;
import io.trino.spi.TrinoException;
import io.trino.spi.classloader.ThreadContextClassLoader;
import io.trino.spi.connector.CatalogSchemaName;
import io.trino.spi.connector.CatalogSchemaTableName;
import io.trino.spi.connector.ConnectorAccessControl;
Expand Down Expand Up @@ -166,10 +167,14 @@ private SystemAccessControl createSystemAccessControl(File configFile)
String name = properties.remove(NAME_PROPERTY);
checkState(!isNullOrEmpty(name), "Access control configuration does not contain '%s' property: %s", NAME_PROPERTY, configFile);

SystemAccessControlFactory systemAccessControlFactory = systemAccessControlFactories.get(name);
checkState(systemAccessControlFactory != null, "Access control '%s' is not registered: %s", name, configFile);
SystemAccessControlFactory factory = systemAccessControlFactories.get(name);
checkState(factory != null, "Access control '%s' is not registered: %s", name, configFile);

SystemAccessControl systemAccessControl;
try (ThreadContextClassLoader ignored = new ThreadContextClassLoader(factory.getClass().getClassLoader())) {
systemAccessControl = factory.create(ImmutableMap.copyOf(properties));
}

SystemAccessControl systemAccessControl = systemAccessControlFactory.create(ImmutableMap.copyOf(properties));
log.info("-- Loaded system access control %s --", name);
return systemAccessControl;
}
Expand All @@ -180,10 +185,14 @@ protected void setSystemAccessControl(String name, Map<String, String> propertie
requireNonNull(name, "name is null");
requireNonNull(properties, "properties is null");

SystemAccessControlFactory systemAccessControlFactory = systemAccessControlFactories.get(name);
checkState(systemAccessControlFactory != null, "Access control '%s' is not registered", name);
SystemAccessControlFactory factory = systemAccessControlFactories.get(name);
checkState(factory != null, "Access control '%s' is not registered", name);

SystemAccessControl systemAccessControl;
try (ThreadContextClassLoader ignored = new ThreadContextClassLoader(factory.getClass().getClassLoader())) {
systemAccessControl = factory.create(ImmutableMap.copyOf(properties));
}

SystemAccessControl systemAccessControl = systemAccessControlFactory.create(ImmutableMap.copyOf(properties));
setSystemAccessControls(ImmutableList.of(systemAccessControl));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import io.airlift.log.Logger;
import io.trino.spi.classloader.ThreadContextClassLoader;
import io.trino.spi.security.GroupProvider;
import io.trino.spi.security.GroupProviderFactory;

Expand Down Expand Up @@ -83,10 +84,14 @@ private void setConfiguredGroupProvider(String name, Map<String, String> propert

log.info("-- Loading group provider %s --", name);

GroupProviderFactory groupProviderFactory = groupProviderFactories.get(name);
checkState(groupProviderFactory != null, "Group provider %s is not registered", name);
GroupProviderFactory factory = groupProviderFactories.get(name);
checkState(factory != null, "Group provider %s is not registered", name);

GroupProvider groupProvider;
try (ThreadContextClassLoader ignored = new ThreadContextClassLoader(factory.getClass().getClassLoader())) {
groupProvider = factory.create(ImmutableMap.copyOf(properties));
}

GroupProvider groupProvider = groupProviderFactory.create(ImmutableMap.copyOf(properties));
checkState(configuredGroupProvider.compareAndSet(Optional.empty(), Optional.of(groupProvider)), "groupProvider is already set");

log.info("-- Loaded group provider %s --", name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import io.airlift.log.Logger;
import io.airlift.node.NodeInfo;
import io.trino.Session;
import io.trino.spi.classloader.ThreadContextClassLoader;
import io.trino.spi.resourcegroups.ResourceGroupId;
import io.trino.spi.resourcegroups.SessionPropertyConfigurationManagerContext;
import io.trino.spi.session.SessionConfigurationContext;
Expand Down Expand Up @@ -86,7 +87,11 @@ public void setConfigurationManager(String name, Map<String, String> properties)
SessionPropertyConfigurationManagerFactory factory = factories.get(name);
checkState(factory != null, "Session property configuration manager '%s' is not registered", name);

SessionPropertyConfigurationManager manager = factory.create(properties, configurationManagerContext);
SessionPropertyConfigurationManager manager;
try (ThreadContextClassLoader ignored = new ThreadContextClassLoader(factory.getClass().getClassLoader())) {
manager = factory.create(properties, configurationManagerContext);
}

checkState(delegate.compareAndSet(null, manager), "sessionPropertyConfigurationManager is already set");

log.info("-- Loaded session property configuration manager %s --", name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import com.google.common.collect.ImmutableMap;
import io.airlift.log.Logger;
import io.trino.spi.classloader.ThreadContextClassLoader;
import io.trino.spi.security.CertificateAuthenticator;
import io.trino.spi.security.CertificateAuthenticatorFactory;

Expand Down Expand Up @@ -76,7 +77,11 @@ public void loadCertificateAuthenticator()
CertificateAuthenticatorFactory factory = factories.get(name);
checkState(factory != null, "Certificate authenticator '%s' is not registered", name);

CertificateAuthenticator authenticator = factory.create(ImmutableMap.copyOf(properties));
CertificateAuthenticator authenticator;
try (ThreadContextClassLoader ignored = new ThreadContextClassLoader(factory.getClass().getClassLoader())) {
authenticator = factory.create(ImmutableMap.copyOf(properties));
}

this.authenticator.set(requireNonNull(authenticator, "authenticator is null"));

log.info("-- Loaded certificate authenticator %s --", name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import com.google.common.collect.ImmutableMap;
import com.google.inject.Inject;
import io.airlift.log.Logger;
import io.trino.spi.classloader.ThreadContextClassLoader;
import io.trino.spi.security.PasswordAuthenticator;
import io.trino.spi.security.PasswordAuthenticatorFactory;

Expand Down Expand Up @@ -103,8 +104,13 @@ private PasswordAuthenticator loadAuthenticator(File configFile)
PasswordAuthenticatorFactory factory = factories.get(name);
checkState(factory != null, "Password authenticator '%s' is not registered", name);

PasswordAuthenticator authenticator;
try (ThreadContextClassLoader ignored = new ThreadContextClassLoader(factory.getClass().getClassLoader())) {
authenticator = factory.create(ImmutableMap.copyOf(properties));
}

log.info("-- Loaded password authenticator %s --", name);
return factory.create(ImmutableMap.copyOf(properties));
return authenticator;
}

public List<PasswordAuthenticator> getAuthenticators()
Expand Down

0 comments on commit be94b0c

Please sign in to comment.