Skip to content

Commit

Permalink
Fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelbranco80 committed Jun 24, 2024
1 parent 0a27176 commit 528d66a
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ class PythonCompilerService(engineDefinition: (Engine, Boolean))(implicit protec

private def buildTruffleContext(
environment: ProgramEnvironment,
maybeOutputStream: Option[OutputStream] = None
maybeOutputStream: Option[OutputStream]
): Context = {
// Add environment settings as hardcoded environment variables.
val ctxBuilder = Context
Expand Down
1 change: 0 additions & 1 deletion snapi-frontend/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,4 @@

opens raw.inferrer.api to
com.fasterxml.jackson.databind;

}
196 changes: 94 additions & 102 deletions snapi-truffle/src/main/java/raw/runtime/truffle/RawLanguageCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@
package raw.runtime.truffle;

import com.oracle.truffle.api.CompilerDirectives;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import raw.compiler.base.CompilerContext;
import raw.creds.api.CredentialsService;
import raw.creds.api.CredentialsServiceProvider;
Expand All @@ -27,122 +25,116 @@
import raw.utils.AuthenticatedUser;
import raw.utils.RawSettings;
import raw.utils.RawUtils;
import scala.Some;
import scala.runtime.BoxedUnit;

public class RawLanguageCache {

private final ClassLoader classLoader = RawLanguage.class.getClassLoader();

private final Object activeContextsLock = new Object();
private final Set<RawContext> activeContexts = new HashSet<RawContext>();

private final ConcurrentHashMap<RawSettings, CredentialsService> credentialsCache =
new ConcurrentHashMap<>();

private final ConcurrentHashMap<AuthenticatedUser, Value> map = new ConcurrentHashMap<>();

private static class Value {
private final CompilerContext compilerContext;
private final SourceContext sourceContext;
private final InferrerService inferrer;
private final ClassLoader classLoader = RawLanguage.class.getClassLoader();

Value(CompilerContext compilerContext, SourceContext sourceContext, InferrerService inferrer) {
this.compilerContext = compilerContext;
this.sourceContext = sourceContext;
this.inferrer = inferrer;
}
private final Object activeContextsLock = new Object();
private final Set<RawContext> activeContexts = new HashSet<RawContext>();

public CompilerContext getCompilerContext() {
return compilerContext;
}
private final ConcurrentHashMap<RawSettings, CredentialsService> credentialsCache =
new ConcurrentHashMap<>();

public SourceContext getSourceContext() {
return sourceContext;
}
private final ConcurrentHashMap<AuthenticatedUser, Value> map = new ConcurrentHashMap<>();

public InferrerService getInferrer() {
return inferrer;
}
}
private static class Value {
private final CompilerContext compilerContext;
private final SourceContext sourceContext;
private final InferrerService inferrer;

@CompilerDirectives.TruffleBoundary
private Value get(AuthenticatedUser user, RawSettings rawSettings) {
// Create services on-demand.
CredentialsService credentialsService =
credentialsCache.computeIfAbsent(
rawSettings, k -> CredentialsServiceProvider.apply(rawSettings));
return map.computeIfAbsent(
user,
k -> {
SourceContext sourceContext = new SourceContext(user, credentialsService, rawSettings);
InferrerService inferrer = InferrerServiceProvider.apply(sourceContext);
CompilerContext compilerContext =
new CompilerContext(
"rql2-truffle",
user,
inferrer,
sourceContext,
rawSettings);
return new Value(compilerContext, sourceContext, inferrer);
});
Value(CompilerContext compilerContext, SourceContext sourceContext, InferrerService inferrer) {
this.compilerContext = compilerContext;
this.sourceContext = sourceContext;
this.inferrer = inferrer;
}

public SourceContext getSourceContext(AuthenticatedUser user, RawSettings rawSettings) {
return get(user, rawSettings).getSourceContext();
public CompilerContext getCompilerContext() {
return compilerContext;
}

public CompilerContext getCompilerContext(AuthenticatedUser user, RawSettings rawSettings) {
return get(user, rawSettings).getCompilerContext();
public SourceContext getSourceContext() {
return sourceContext;
}

public InferrerService getInferrer(AuthenticatedUser user, RawSettings rawSettings) {
return get(user, rawSettings).getInferrer();
public InferrerService getInferrer() {
return inferrer;
}

@CompilerDirectives.TruffleBoundary
public void incrementContext(RawContext context) {
synchronized (activeContextsLock) {
activeContexts.add(context);
}
}

@CompilerDirectives.TruffleBoundary
private Value get(AuthenticatedUser user, RawSettings rawSettings) {
// Create services on-demand.
CredentialsService credentialsService =
credentialsCache.computeIfAbsent(
rawSettings, k -> CredentialsServiceProvider.apply(rawSettings));
return map.computeIfAbsent(
user,
k -> {
SourceContext sourceContext = new SourceContext(user, credentialsService, rawSettings);
InferrerService inferrer = InferrerServiceProvider.apply(sourceContext);
CompilerContext compilerContext =
new CompilerContext("rql2-truffle", user, inferrer, sourceContext, rawSettings);
return new Value(compilerContext, sourceContext, inferrer);
});
}

public SourceContext getSourceContext(AuthenticatedUser user, RawSettings rawSettings) {
return get(user, rawSettings).getSourceContext();
}

public CompilerContext getCompilerContext(AuthenticatedUser user, RawSettings rawSettings) {
return get(user, rawSettings).getCompilerContext();
}

public InferrerService getInferrer(AuthenticatedUser user, RawSettings rawSettings) {
return get(user, rawSettings).getInferrer();
}

@CompilerDirectives.TruffleBoundary
public void incrementContext(RawContext context) {
synchronized (activeContextsLock) {
activeContexts.add(context);
}

@CompilerDirectives.TruffleBoundary
public void releaseContext(RawContext context) {
synchronized (activeContextsLock) {
activeContexts.remove(context);
if (activeContexts.isEmpty()) {
// Close all inferrer services and credential services.
map.values()
.forEach(
v -> {
RawUtils.withSuppressNonFatalException(
() -> {
v.getInferrer().stop();
return BoxedUnit.UNIT;
},
true);
RawUtils.withSuppressNonFatalException(
() -> {
v.getSourceContext().credentialsService().stop();
return BoxedUnit.UNIT;
},
true);
});
map.clear();
credentialsCache
.values()
.forEach(
v -> {
RawUtils.withSuppressNonFatalException(
() -> {
v.stop();
return BoxedUnit.UNIT;
},
true);
});
credentialsCache.clear();
}
}
}

@CompilerDirectives.TruffleBoundary
public void releaseContext(RawContext context) {
synchronized (activeContextsLock) {
activeContexts.remove(context);
if (activeContexts.isEmpty()) {
// Close all inferrer services and credential services.
map.values()
.forEach(
v -> {
RawUtils.withSuppressNonFatalException(
() -> {
v.getInferrer().stop();
return BoxedUnit.UNIT;
},
true);
RawUtils.withSuppressNonFatalException(
() -> {
v.getSourceContext().credentialsService().stop();
return BoxedUnit.UNIT;
},
true);
});
map.clear();
credentialsCache
.values()
.forEach(
v -> {
RawUtils.withSuppressNonFatalException(
() -> {
v.stop();
return BoxedUnit.UNIT;
},
true);
});
credentialsCache.clear();
}
}
}
}

0 comments on commit 528d66a

Please sign in to comment.