Skip to content
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

Revert "Set Thread context classloder for entire middleware chain (#7… #778

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.microsoft.azure.functions.worker.broker;

import com.microsoft.azure.functions.worker.binding.*;

/**
* Used to executor of arbitrary Java method in any JAR using reflection.
* Thread-Safety: Multiple thread.
*/
public class EnhancedJavaMethodExecutorImpl implements JavaMethodExecutor {

private final ClassLoader classLoader;

public EnhancedJavaMethodExecutorImpl(ClassLoader classLoader) {
this.classLoader = classLoader;
}

public void execute(ExecutionContextDataSource executionContextDataSource) throws Exception {
try {
Thread.currentThread().setContextClassLoader(this.classLoader);
Object retValue = ParameterResolver.resolveArguments(executionContextDataSource)
.orElseThrow(() -> new NoSuchMethodException("Cannot locate the method signature with the given input"))
.invoke(executionContextDataSource::getFunctionInstance);
executionContextDataSource.updateReturnValue(retValue);
} finally {
Thread.currentThread().setContextClassLoader(ClassLoader.getSystemClassLoader());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,29 +113,20 @@ private void initializeFunctionInstanceInjector() {
}

private FunctionExecutionMiddleware getFunctionExecutionMiddleWare() {
FunctionExecutionMiddleware functionExecutionMiddleware = new FunctionExecutionMiddleware(JavaMethodExecutor.getInstance());
FunctionExecutionMiddleware functionExecutionMiddleware = new FunctionExecutionMiddleware(
JavaMethodExecutors.createJavaMethodExecutor(this.classLoaderProvider.createClassLoader()));
WorkerLogManager.getSystemLogger().info("Load last middleware: FunctionExecutionMiddleware");
return functionExecutionMiddleware;
}

public Optional<TypedData> invokeMethod(String id, InvocationRequest request, List<ParameterBinding> outputs)
throws Exception {
ExecutionContextDataSource executionContextDataSource = buildExecutionContext(id, request);
invoke(executionContextDataSource);
this.invocationChainFactory.create().doNext(executionContextDataSource);
outputs.addAll(executionContextDataSource.getDataStore().getOutputParameterBindings(true));
return executionContextDataSource.getDataStore().getDataTargetTypedValue(BindingDataStore.RETURN_NAME);
}

private void invoke(ExecutionContextDataSource executionContextDataSource) throws Exception {
ClassLoader prevContextClassLoader = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(classLoaderProvider.createClassLoader());
this.invocationChainFactory.create().doNext(executionContextDataSource);
} finally {
Thread.currentThread().setContextClassLoader(prevContextClassLoader);
}
}

private ExecutionContextDataSource buildExecutionContext(String id, InvocationRequest request)
throws NoSuchMethodException {
ImmutablePair<String, FunctionDefinition> methodEntry = this.methods.get(id);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,14 @@
package com.microsoft.azure.functions.worker.broker;


import java.util.*;
import com.microsoft.azure.functions.worker.binding.*;


/**
* Used to executor of arbitrary Java method in any JAR using reflection.
* Thread-Safety: Multiple thread.
*/
public class JavaMethodExecutor {

private static final JavaMethodExecutor INSTANCE = new JavaMethodExecutor();

public static JavaMethodExecutor getInstance(){
return INSTANCE;
}

private JavaMethodExecutor() {}

public void execute(ExecutionContextDataSource executionContextDataSource) throws Exception {
Object retValue = ParameterResolver.resolveArguments(executionContextDataSource)
.orElseThrow(() -> new NoSuchMethodException("Cannot locate the method signature with the given input"))
.invoke(executionContextDataSource::getFunctionInstance);
executionContextDataSource.updateReturnValue(retValue);
}
public interface JavaMethodExecutor {
void execute(ExecutionContextDataSource executionContextDataSource) throws Exception;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.microsoft.azure.functions.worker.broker;

import com.microsoft.azure.functions.worker.binding.*;

/**
* Used to executor of arbitrary Java method in any JAR using reflection.
* Thread-Safety: Multiple thread.
*/
public class JavaMethodExecutorImpl implements JavaMethodExecutor {

private static final JavaMethodExecutorImpl INSTANCE = new JavaMethodExecutorImpl();

public static JavaMethodExecutorImpl getInstance(){
return INSTANCE;
}

private JavaMethodExecutorImpl () {}

public void execute(ExecutionContextDataSource executionContextDataSource) throws Exception {
Object retValue = ParameterResolver.resolveArguments(executionContextDataSource)
.orElseThrow(() -> new NoSuchMethodException("Cannot locate the method signature with the given input"))
.invoke(executionContextDataSource::getFunctionInstance);
executionContextDataSource.updateReturnValue(retValue);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.microsoft.azure.functions.worker.broker;

import com.microsoft.azure.functions.worker.WorkerLogManager;
import org.apache.commons.lang3.SystemUtils;

public class JavaMethodExecutors {
public static JavaMethodExecutor createJavaMethodExecutor(ClassLoader classLoader) {
if(SystemUtils.IS_JAVA_1_8) {
WorkerLogManager.getSystemLogger().info("Loading JavaMethodExecutorImpl");
return JavaMethodExecutorImpl.getInstance();
} else {
WorkerLogManager.getSystemLogger().info("Loading EnhancedJavaMethodExecutorImpl");
return new EnhancedJavaMethodExecutorImpl(classLoader);
}
}
}