-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Warmup java worker before specialization (#672)
* add warm up logics * update warm up request * minor updates * update logs * add warm up jar * add more logs for env testing * remove test logs * refractor code * add warm up log * add fix for java8 systemclassloader - add warmup funciton * revert pipeline change - add logs * restrucer warmup handler * stop system classloader load java library in warmup process * update warmup function * update log messages * adjust logs * fix warmup jar * refactor code * update warm up jar * style refactoring * Rename to worker warmup and added capability * Updating tests --------- Co-authored-by: Shreyas Gopalakrishna <shreyasg@microsoft.com>
- Loading branch information
1 parent
1483b11
commit f66b2fe
Showing
16 changed files
with
276 additions
and
79 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
88 changes: 88 additions & 0 deletions
88
src/main/java/com/microsoft/azure/functions/worker/handler/WorkerWarmupHandler.java
This file contains 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,88 @@ | ||
package com.microsoft.azure.functions.worker.handler; | ||
|
||
import com.microsoft.azure.functions.rpc.messages.*; | ||
import com.microsoft.azure.functions.worker.WorkerLogManager; | ||
import com.microsoft.azure.functions.worker.broker.JavaFunctionBroker; | ||
import com.microsoft.azure.functions.worker.reflect.FactoryClassLoader; | ||
|
||
import java.util.*; | ||
|
||
import static com.microsoft.azure.functions.worker.Constants.JAVA_LIBRARY_DIRECTORY; | ||
|
||
|
||
public class WorkerWarmupHandler extends MessageHandler<WorkerWarmupRequest, WorkerWarmupResponse.Builder> { | ||
|
||
private static final String WARM_UP_FUNCTION_NAME = "WarmupFunc"; | ||
private static final String WARM_UP_FUNCTION_ENTRY_POINT = "com.microsoft.azure.functions.warmup.java.Function.run"; | ||
private static final String WARM_UP_FUNCTION_SCRIPT_FILE = JAVA_LIBRARY_DIRECTORY + "/warmup-httptrigger.jar"; | ||
private final JavaFunctionBroker javaFunctionBroker = new JavaFunctionBroker(new FactoryClassLoader().createClassLoaderProvider()); | ||
|
||
public WorkerWarmupHandler() { | ||
super(StreamingMessage::getWorkerWarmupRequest, | ||
WorkerWarmupResponse::newBuilder, | ||
WorkerWarmupResponse.Builder::setResult, | ||
StreamingMessage.Builder::setWorkerWarmupResponse); | ||
} | ||
|
||
@Override | ||
String execute(WorkerWarmupRequest workerWarmupRequest, WorkerWarmupResponse.Builder builder) { | ||
try { | ||
WorkerLogManager.getSystemLogger().info("azure function java worker warm up start."); | ||
this.javaFunctionBroker.setWorkerDirectory(workerWarmupRequest.getWorkerDirectory()); | ||
warmupFunctionEnvironmentReload(); | ||
UUID functionId = warmupFunctionLoad(workerWarmupRequest); | ||
warmupInvocation(functionId); | ||
WorkerLogManager.getSystemLogger().info("azure function java worker warm up completed successfully."); | ||
} catch (Exception e) { | ||
WorkerLogManager.getSystemLogger().severe("warm up process failed with exception: " + e.getMessage()); | ||
throw new RuntimeException(e); | ||
} | ||
return "azure function java worker warm up completed"; | ||
} | ||
|
||
private void warmupFunctionEnvironmentReload() throws Exception { | ||
FunctionEnvironmentReloadRequest functionEnvironmentReloadRequest = FunctionEnvironmentReloadRequest.newBuilder() | ||
.putAllEnvironmentVariables(System.getenv()) | ||
.build(); | ||
new FunctionEnvironmentReloadRequestHandler(this.javaFunctionBroker).execute(functionEnvironmentReloadRequest, null); | ||
WorkerLogManager.getSystemLogger().info("finish warm up FunctionEnvironmentReloadRequestHandler"); | ||
} | ||
|
||
private UUID warmupFunctionLoad(WorkerWarmupRequest workerWarmupRequest) throws Exception { | ||
Map<String, BindingInfo> map = new HashMap<>(); | ||
BindingInfo httpTrigger = BindingInfo.newBuilder().setDirection(BindingInfo.Direction.in).setDataType(BindingInfo.DataType.undefined).setType("httpTrigger").build(); | ||
map.put("req", httpTrigger); | ||
BindingInfo http = BindingInfo.newBuilder().setDirection(BindingInfo.Direction.out).setDataType(BindingInfo.DataType.undefined).setType("http").build(); | ||
map.put("$return", http); | ||
RpcFunctionMetadata rpcFunctionMetadata = RpcFunctionMetadata.newBuilder() | ||
.setName(WARM_UP_FUNCTION_NAME) | ||
.setEntryPoint(WARM_UP_FUNCTION_ENTRY_POINT) | ||
.setScriptFile(workerWarmupRequest.getWorkerDirectory() + WARM_UP_FUNCTION_SCRIPT_FILE) | ||
.putAllBindings(map) | ||
.build(); | ||
final UUID functionId = UUID.randomUUID(); | ||
FunctionLoadRequest functionLoadRequest = FunctionLoadRequest.newBuilder() | ||
.setFunctionId(functionId.toString()) | ||
.setMetadata(rpcFunctionMetadata) | ||
.build(); | ||
String loadRequestResult = new FunctionLoadRequestHandler(this.javaFunctionBroker, true).execute(functionLoadRequest, FunctionLoadResponse.newBuilder()); | ||
WorkerLogManager.getSystemLogger().info("finish warm up FunctionLoadRequestHandler with result: " + loadRequestResult); | ||
return functionId; | ||
} | ||
|
||
private void warmupInvocation(UUID functionId) throws Exception { | ||
List<ParameterBinding> inputDataList = new ArrayList<>(); | ||
ParameterBinding parameterBinding = ParameterBinding.newBuilder() | ||
.setName("req") | ||
.setData(TypedData.newBuilder().setHttp(RpcHttp.newBuilder().setMethod("GET"))) | ||
.build(); | ||
inputDataList.add(parameterBinding); | ||
InvocationRequest invocationRequest = InvocationRequest.newBuilder() | ||
.setFunctionId(functionId.toString()) | ||
.setInvocationId(UUID.randomUUID().toString()) | ||
.addAllInputData(inputDataList) | ||
.build(); | ||
String invocationResult = new InvocationRequestHandler(this.javaFunctionBroker).execute(invocationRequest, InvocationResponse.newBuilder()); | ||
WorkerLogManager.getSystemLogger().info("finish warm up InvocationRequestHandler with result: " + invocationResult); | ||
} | ||
} |
This file contains 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
Oops, something went wrong.