Skip to content

Commit

Permalink
⚡ Remove per-thread caching of context objects, as it provides no ben…
Browse files Browse the repository at this point in the history
…efit with the use of virtual threads
  • Loading branch information
ujibang committed Sep 27, 2024
1 parent 683b4a6 commit a737d7e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,16 @@
import java.util.Map;
import java.util.Optional;

import com.google.common.collect.Maps;
import com.mongodb.client.MongoClient;

import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Engine;
import org.graalvm.polyglot.HostAccess;
import org.graalvm.polyglot.Source;
import org.graalvm.polyglot.Value;
import org.graalvm.polyglot.io.IOAccess;
import org.restheart.configuration.Configuration;
import org.restheart.plugins.InterceptPoint;
import org.restheart.plugins.RegisterPlugin.MATCH_POLICY;
import org.restheart.utils.CleanerUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -75,9 +72,6 @@ protected AbstractJSPlugin() {
this.conf = null;
this.isService = true;
this.isInterceptor = false;

// register cleaner
CleanerUtils.get().cleaner().register(this, new State(this.ctxs));
}

protected AbstractJSPlugin(String name,
Expand All @@ -100,9 +94,6 @@ protected AbstractJSPlugin(String name,
this.conf = conf;
this.isService = isService;
this.isInterceptor = isInterceptor;

// register cleaner
CleanerUtils.get().cleaner().register(this, new State(this.ctxs));
}

public static Context context(Engine engine, Map<String, String> OPTS) {
Expand Down Expand Up @@ -162,66 +153,13 @@ public static void addBindings(Context ctx,
ctx.getBindings("js").putMember("pluginArgs", args);
}

// cache Context and handles for performace
// each working thread is associates with one context and one handle
// because js Context does not allow multithreaded access
protected Map<String, Context> ctxs = Maps.newHashMap();
protected Map<String, Value> handles = Maps.newHashMap();

/**
*
* @return the Context associated with this thread. If not existing, it instanitates it.
*/
protected Context ctx() {
var workingThreadName = Thread.currentThread().getName();

if (this.ctxs.get(workingThreadName) == null) {
var ctx = context(engine, contextOptions);
this.ctxs.put(workingThreadName, ctx);

addBindings(ctx, this.name, this.conf, LOGGER, this.mclient);
}

return this.ctxs.get(workingThreadName);
}

/**
*
* @return the handle Value associated with this thread. If not existing, it instanitates it.
*/
protected Value _handle() {
var workingThreadName = Thread.currentThread().getName();

if (this.handles.containsKey(workingThreadName)) {
return this.handles.get(workingThreadName);
} else {
var handle = ctx().eval(this.handleSource);
this.handles.put(workingThreadName, handle);
return handle;
}
}

// for cleaning
protected static class State implements Runnable {
private Map<String, Context> ctxs;

State(Map<String, Context> ctxs) {
// initialize State needed for cleaning action
this.ctxs = ctxs;
}

public void run() {
if (this.ctxs != null) {
this.ctxs.entrySet().stream().map(e -> e.getValue())
.filter(ctx -> ctx != null)
.forEach(ctx -> {
try {
ctx.close();
} catch(Throwable t) {
// nothing to do
}
});
}
}
var ret = context(engine, contextOptions);
addBindings(ret, pluginClass, conf, LOGGER, mclient);
return ret;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@
import java.nio.file.Path;
import java.util.Optional;

import com.mongodb.client.MongoClient;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Source;
import org.graalvm.polyglot.Value;
import org.restheart.configuration.Configuration;
import org.restheart.exchange.StringRequest;
import org.restheart.exchange.StringResponse;
import org.restheart.plugins.StringService;
import org.restheart.plugins.RegisterPlugin.MATCH_POLICY;
import org.restheart.utils.CleanerUtils;
import org.restheart.plugins.StringService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mongodb.client.MongoClient;

/**
*
* @author Andrea Di Cesare {@literal <[email protected]>}
Expand Down Expand Up @@ -72,9 +72,6 @@ export function handle(request, response) {
""";

JavaScriptService(Path pluginPath, Optional<MongoClient> mclient, Configuration conf) throws IOException {
// register cleaner
CleanerUtils.get().cleaner().register(this, new State(this.ctxs));

this.mclient = mclient;
this.conf = conf;
this.isService = true;
Expand Down Expand Up @@ -181,8 +178,9 @@ export function handle(request, response) {
/**
*
*/
@Override
public void handle(StringRequest request, StringResponse response) {
_handle().executeVoid(request, response);
ctx().eval(this.handleSource).executeVoid(request, response);
}

/**
Expand All @@ -198,16 +196,10 @@ protected Context ctx() {
contextOptions.remove("js.commonjs-core-modules-replacements");
}

var workingThreadName = Thread.currentThread().getName();

if (this.ctxs.get(workingThreadName) == null) {
var ctx = context(engine, contextOptions);
this.ctxs.put(workingThreadName, ctx);

addBindings(ctx, this.name, conf, LOGGER, this.mclient);
}
var ctx = context(engine, contextOptions);
addBindings(ctx, this.name, conf, LOGGER, this.mclient);

return this.ctxs.get(workingThreadName);
return ctx;
}

public String getModulesReplacements() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
import java.util.Map;
import java.util.Optional;

import com.google.common.collect.Maps;
import com.mongodb.client.MongoClient;

import org.graalvm.polyglot.Source;
import org.graalvm.polyglot.Value;
import org.restheart.configuration.Configuration;
Expand All @@ -37,6 +34,9 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.Maps;
import com.mongodb.client.MongoClient;

public class AbstractJSInterceptor<R extends Request<?>, S extends Response<?>> extends AbstractJSPlugin implements Interceptor<R, S> {
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractJSInterceptor.class);

Expand Down Expand Up @@ -69,9 +69,11 @@ public AbstractJSInterceptor(String name,

/**
*
*/
* @param request
* @param response */
@Override
public void handle(R request, S response) {
_handle().executeVoid(request, response);
ctx().eval(this.handleSource).executeVoid(request, response);
}

@Override
Expand Down

0 comments on commit a737d7e

Please sign in to comment.