Skip to content

Commit

Permalink
Set Thread context classloder for entire middleware chain (#717)
Browse files Browse the repository at this point in the history
* Set Thread context classloder for entire middleware chain

* clean up code

* clean up
  • Loading branch information
kaibocai committed Sep 27, 2023
1 parent 029414f commit 5315874
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 77 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -113,20 +113,29 @@ private void initializeFunctionInstanceInjector() {
}

private FunctionExecutionMiddleware getFunctionExecutionMiddleWare() {
FunctionExecutionMiddleware functionExecutionMiddleware = new FunctionExecutionMiddleware(
JavaMethodExecutors.createJavaMethodExecutor(this.classLoaderProvider.createClassLoader()));
FunctionExecutionMiddleware functionExecutionMiddleware = new FunctionExecutionMiddleware(JavaMethodExecutor.getInstance());
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);
this.invocationChainFactory.create().doNext(executionContextDataSource);
invoke(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,14 +1,25 @@
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 interface JavaMethodExecutor {
void execute(ExecutionContextDataSource executionContextDataSource) throws Exception;
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);
}
}

This file was deleted.

This file was deleted.

0 comments on commit 5315874

Please sign in to comment.