-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Abstractify HadoopTask #1367
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Abstractify HadoopTask #1367
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
indexing-service/src/main/java/io/druid/indexing/common/task/HadoopTask.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| /* | ||
| * Licensed to Metamarkets Group Inc. (Metamarkets) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. Metamarkets 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 io.druid.indexing.common.task; | ||
|
|
||
| import com.google.api.client.repackaged.com.google.common.base.Throwables; | ||
| import com.google.common.base.Joiner; | ||
| import com.google.common.collect.ImmutableList; | ||
| import com.google.common.collect.Lists; | ||
| import com.google.inject.Injector; | ||
| import com.metamx.common.logger.Logger; | ||
| import io.druid.guice.ExtensionsConfig; | ||
| import io.druid.guice.GuiceInjectors; | ||
| import io.druid.indexing.common.TaskToolbox; | ||
| import io.druid.initialization.Initialization; | ||
| import io.tesla.aether.internal.DefaultTeslaAether; | ||
|
|
||
| import java.io.File; | ||
| import java.lang.reflect.InvocationTargetException; | ||
| import java.lang.reflect.Method; | ||
| import java.net.URL; | ||
| import java.net.URLClassLoader; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
|
|
||
| public abstract class HadoopTask extends AbstractTask | ||
| { | ||
| private static final Logger log = new Logger(HadoopTask.class); | ||
| private static final ExtensionsConfig extensionsConfig; | ||
|
|
||
| final static Injector injector = GuiceInjectors.makeStartupInjector(); | ||
|
|
||
| static { | ||
| extensionsConfig = injector.getInstance(ExtensionsConfig.class); | ||
| } | ||
|
|
||
| private final List<String> hadoopDependencyCoordinates; | ||
|
|
||
| protected HadoopTask(String id, String dataSource, List<String> hadoopDependencyCoordinates) | ||
| { | ||
| super(id, dataSource); | ||
| this.hadoopDependencyCoordinates = hadoopDependencyCoordinates; | ||
| } | ||
|
|
||
| public List<String> getHadoopDependencyCoordinates() | ||
| { | ||
| return hadoopDependencyCoordinates == null ? null : ImmutableList.copyOf(hadoopDependencyCoordinates); | ||
| } | ||
|
|
||
| protected ClassLoader buildClassLoader(final TaskToolbox toolbox) throws Exception | ||
| { | ||
| final List<String> finalHadoopDependencyCoordinates = hadoopDependencyCoordinates != null | ||
| ? hadoopDependencyCoordinates | ||
| : toolbox.getConfig().getDefaultHadoopCoordinates(); | ||
|
|
||
| final DefaultTeslaAether aetherClient = Initialization.getAetherClient(extensionsConfig); | ||
|
|
||
| final List<URL> extensionURLs = Lists.newArrayList(); | ||
| for (String coordinate : extensionsConfig.getCoordinates()) { | ||
| final ClassLoader coordinateLoader = Initialization.getClassLoaderForCoordinates( | ||
| aetherClient, coordinate, extensionsConfig.getDefaultVersion() | ||
| ); | ||
| extensionURLs.addAll(Arrays.asList(((URLClassLoader) coordinateLoader).getURLs())); | ||
| } | ||
|
|
||
| final List<URL> nonHadoopURLs = Lists.newArrayList(); | ||
| nonHadoopURLs.addAll(Arrays.asList(((URLClassLoader) HadoopIndexTask.class.getClassLoader()).getURLs())); | ||
|
|
||
| final List<URL> driverURLs = Lists.newArrayList(); | ||
| driverURLs.addAll(nonHadoopURLs); | ||
| // put hadoop dependencies last to avoid jets3t & apache.httpcore version conflicts | ||
| for (String hadoopDependencyCoordinate : finalHadoopDependencyCoordinates) { | ||
| final ClassLoader hadoopLoader = Initialization.getClassLoaderForCoordinates( | ||
| aetherClient, hadoopDependencyCoordinate, extensionsConfig.getDefaultVersion() | ||
| ); | ||
| driverURLs.addAll(Arrays.asList(((URLClassLoader) hadoopLoader).getURLs())); | ||
| } | ||
|
|
||
| final URLClassLoader loader = new URLClassLoader(driverURLs.toArray(new URL[driverURLs.size()]), null); | ||
|
|
||
| final List<URL> jobUrls = Lists.newArrayList(); | ||
| jobUrls.addAll(nonHadoopURLs); | ||
| jobUrls.addAll(extensionURLs); | ||
|
|
||
| System.setProperty("druid.hadoop.internal.classpath", Joiner.on(File.pathSeparator).join(jobUrls)); | ||
| return loader; | ||
| } | ||
|
|
||
| /** | ||
| * This method tries to isolate class loading during a Function call | ||
| * | ||
| * @param clazzName The Class which has a static method called `runTask` | ||
| * @param input The input for `runTask`, must have `input.getClass()` be the class of the input for runTask | ||
| * @param loader The loader to use as the context class loader during invocation | ||
| * @param <InputType> The input type of the method. | ||
| * @param <OutputType> The output type of the method. The result of runTask must be castable to this type. | ||
| * | ||
| * @return The result of the method invocation | ||
| */ | ||
| public static <InputType, OutputType> OutputType invokeForeignLoader( | ||
| final String clazzName, | ||
| final InputType input, | ||
| final ClassLoader loader | ||
| ) | ||
| { | ||
| log.debug("Launching [%s] on class loader [%s] with input class [%s]", clazzName, loader, input.getClass()); | ||
| final ClassLoader oldLoader = Thread.currentThread().getContextClassLoader(); | ||
| try { | ||
| Thread.currentThread().setContextClassLoader(loader); | ||
| final Class<?> clazz = loader.loadClass(clazzName); | ||
| final Method method = clazz.getMethod("runTask", input.getClass()); | ||
| return (OutputType) method.invoke(null, input); | ||
| } | ||
| catch (IllegalAccessException | InvocationTargetException | ClassNotFoundException | NoSuchMethodException e) { | ||
| throw Throwables.propagate(e); | ||
| } | ||
| finally { | ||
| Thread.currentThread().setContextClassLoader(oldLoader); | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
earlier it was HadoopIndexGeneratorInnerProcessing.class.getName() which is more compiler friendly. You can possibly change the invokeForeignLoader(Class clazz,...) instead of invokeForeignLoader(String clazz,..) too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did it this way on purpose. The prior way didn't work as expected because it was causing the clazz to get loaded in the current classloader instead of the foreign classloader. This way achieves complete isolation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do you have an example of what did not work as a consequence of this? This is something we should have a test for
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was in some modifications I did for the compression task, not in the hadoop task (though some OTHER hadoop problems may have been a result of this).
I tried to find a way to add unit tests but couldn't find a clean method (all of maybe 20 minutes). If anyone knows how to do classloading isolation unit tests I'm all ears.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Specifically, there were some classes in the target clazz (method parameters, etc) that were getting loaded in the current classloader, so when it came time to use them, they were failing because the class was not loaded properly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We also need to make sure this doesn't introduce new problems that the old way of loading somehow masked.