Skip to content

Use a single classloader for java 11 if environment variable is set #461

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 1 commit into from
Sep 29, 2021
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
Expand Up @@ -59,5 +59,19 @@ public async Task HttpTriggerJavaClassLoader()
Assert.True(await Utilities.InvokeHttpTrigger("HttpTriggerJavaClassLoader", "?&name=Test", HttpStatusCode.InternalServerError, ""));
}
}

[Fact]
public async void HttpTriggerJavaStatic()
{
String value = Environment.GetEnvironmentVariable("FUNCTIONS_WORKER_JAVA_V3_SINGLE_CLASSLOADER");
String java_home = Environment.GetEnvironmentVariable("JAVA_HOME");
if (java_home.Contains("1.8") || (value != null && (value.ToLower().Equals("true") || value.ToLower().Equals("1"))))
{
await HttpTriggerTests("HttpTriggerJavaStatic1", "", HttpStatusCode.OK, "1");
await HttpTriggerTests("HttpTriggerJavaStatic2", "", HttpStatusCode.OK, "2");
await HttpTriggerTests("HttpTriggerJavaStatic1", "", HttpStatusCode.OK, "3");
await HttpTriggerTests("HttpTriggerJavaStatic2", "", HttpStatusCode.OK, "4");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{
"profiles": {
"Azure.Functions.Java.Tests.E2E": {
"commandName": "Project"
"commandName": "Project",
"environmentVariables": {
"FUNCTIONS_WORKER_JAVA_V3_SINGLE_CLASSLOADER": "1"
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -237,4 +237,32 @@ public HttpResponseMessage runRetryExponentialBackoffRetryFail(
return request.createResponseBuilder(HttpStatus.OK).body("Hello, " + name).build();
}
}

private static int flag = 0;

@FunctionName("HttpTriggerJavaStatic1")
public HttpResponseMessage HttpTriggerJavaStatic1(
@HttpTrigger(
name = "req",
methods = {HttpMethod.GET, HttpMethod.POST},
authLevel = AuthorizationLevel.ANONYMOUS)
HttpRequestMessage<Optional<String>> request,
final ExecutionContext context) {
context.getLogger().info("Java HTTP trigger - static processed a request.");
flag++;
return request.createResponseBuilder(HttpStatus.OK).body(String.valueOf(flag)).build();
}

@FunctionName("HttpTriggerJavaStatic2")
public HttpResponseMessage HttpTriggerJavaStatic2(
@HttpTrigger(
name = "req",
methods = {HttpMethod.GET, HttpMethod.POST},
authLevel = AuthorizationLevel.ANONYMOUS)
HttpRequestMessage<Optional<String>> request,
final ExecutionContext context) {
context.getLogger().info("Java HTTP trigger - static processed a request.");
flag++;
return request.createResponseBuilder(HttpStatus.OK).body(String.valueOf(flag)).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ private Constants(){}
public final static String TRIGGER_METADATA_DOLLAR_REQUEST_KEY = "$request";
public final static String FUNCTIONS_WORKER_DIRECTORY = "FUNCTIONS_WORKER_DIRECTORY";
public final static String FUNCTIONS_WORKER_JAVA_LOAD_APP_LIBS = "FUNCTIONS_WORKER_JAVA_LOAD_APP_LIBS";
public final static String FUNCTIONS_WORKER_JAVA_V3_SINGLE_CLASSLOADER = "FUNCTIONS_WORKER_JAVA_V3_SINGLE_CLASSLOADER";
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,8 @@ public static boolean isLoadAppLibsFirst() {
String javaReverseLibLoading = System.getenv(Constants.FUNCTIONS_WORKER_JAVA_LOAD_APP_LIBS);
return Util.isTrue(javaReverseLibLoading);
}
public static boolean isCustomURLClassLoader() {
String customURLClassLoader = System.getenv(Constants.FUNCTIONS_WORKER_JAVA_V3_SINGLE_CLASSLOADER);
return Util.isTrue(customURLClassLoader);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,31 @@ public EnhancedClassLoaderProvider() {
*/
@Override
public ClassLoader createClassLoader() {
if(Helper.isCustomURLClassLoader()) {
return getURLClassLoaderInstance();
}else {
return createURLClassLoaderInstance();
}
}

/**
* Create and return a singleton URL classloader
* @return instance of URLClassLoader
*/
private URLClassLoader getURLClassLoaderInstance() {
if (classLoaderInstance == null) {
synchronized (lock) {
if (classLoaderInstance == null) {
classLoaderInstance = createURLClassLoaderInstance();
}
}
}
return classLoaderInstance;
}

private URLClassLoader createURLClassLoaderInstance(){
URL[] urlsForClassLoader = new URL[urls.size()];
urls.toArray(urlsForClassLoader);

URLClassLoader classLoader = new URLClassLoader(urlsForClassLoader);
loadDrivers(classLoader);
return classLoader;
Expand Down Expand Up @@ -73,4 +95,6 @@ public void addUrl(URL url) throws IOException {
urls.add(url);
}
private final Set<URL> urls;
private final Object lock = new Object();
private static volatile URLClassLoader classLoaderInstance;
}