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

fix: new config to support #547 #548

Merged
merged 5 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -44,9 +44,11 @@ public class LocalGraalExecutionService implements ScriptExecutionService {

public static final int DEFAULT_MAX_CPU_TIME = 100;
public static final int DEFAULT_MAX_MEMORY = 10485760;
public static final boolean DEFAULT_REMOVE_COMMENTS = true;

private int maxCpuTime;
private int maxMemory;
private boolean removeComments;

// custom jackson configuration with typeReference
private final ObjectMapper mapper = new ObjectMapper()
Expand All @@ -58,6 +60,7 @@ public class LocalGraalExecutionService implements ScriptExecutionService {
public LocalGraalExecutionService() {
this.maxCpuTime = DEFAULT_MAX_CPU_TIME;
this.maxMemory = DEFAULT_MAX_MEMORY;
this.removeComments = DEFAULT_REMOVE_COMMENTS;
}

public int getMaxCpuTime() {
Expand All @@ -76,6 +79,14 @@ public void setMaxMemory(int maxMemory) {
this.maxMemory = maxMemory;
}

public boolean isRemoveComments() {
return removeComments;
}

public void setRemoveComments(boolean removeComments) {
this.removeComments = removeComments;
}

// TODO implement a threadPool for sandboxes to avoid bootstrap
@Override
public Map<String, Serializable> executeFunction(String name, String function, Map<String, Serializable> input)
Expand Down Expand Up @@ -107,7 +118,10 @@ public Map<String, Serializable> executeFunction(String name, String function, M
writer.append("result = JSON.stringify(" + name + "(data));");
writer.append("logs = JSON.stringify(_logs);");

String code = RemoveComments.perform(writer.toString());
String code = writer.toString();
if (removeComments) {
code = RemoveComments.perform(code);
}
sandbox.eval(code);

String output = (String) sandbox.get("result");
Expand Down Expand Up @@ -160,7 +174,10 @@ public <T> T executeFunction(String name, String function, Class<T> clazz, Seria
writer.append("result = JSON.stringify(" + name + "(" + String.join(",", vars) + "));");
writer.append("logs = JSON.stringify(_logs);");

String code = RemoveComments.perform(writer.toString());
String code = writer.toString();
if (removeComments) {
code = RemoveComments.perform(code);
}
sandbox.eval(code);

String output = (String) sandbox.get("result");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package it.smartcommunitylab.aac.config;

import it.smartcommunitylab.aac.claims.LocalGraalExecutionService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
Expand All @@ -25,8 +26,21 @@
@Order(9)
public class ExecutionConfig {

@Value("${engine.graal.max-cpu-time}")
private int graalMaxCpuTime;

@Value("${engine.graal.max-memory}")
private int graalMaxMemory;

@Value("${engine.graal.remove-comments}")
private boolean graalRemoveComments;

@Bean
public LocalGraalExecutionService localGraalExecutionService() {
return new LocalGraalExecutionService();
LocalGraalExecutionService executionService = new LocalGraalExecutionService();
executionService.setMaxMemory(graalMaxMemory);
executionService.setMaxCpuTime(graalMaxCpuTime);
executionService.setRemoveComments(graalRemoveComments);
return executionService;
}
}
6 changes: 6 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,12 @@ spring:
store-type: jdbc
jdbc.initialize-schema: always

engine:
graal:
max-cpu-time: 100
max-memory: 10485760
remove-comments: ${REMOVE_COMMENTS_IN_SCRIPTS:true}
thomaschiozzi-tndigit marked this conversation as resolved.
Show resolved Hide resolved

# ROLE PREFIX FOR AUTHORIZATION CHECK
authorization:
contextSpace:
Expand Down