Skip to content

Commit

Permalink
Merge pull request #42488 from HindujaB/runtime-mgt
Browse files Browse the repository at this point in the history
[Master] Move `remoteManagement` flag check to runtime
  • Loading branch information
HindujaB authored Apr 5, 2024
2 parents ccef872 + 148286d commit 66ab6b6
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class RepositoryImpl implements Repository {
private static final String nodeId = generateNodeId();
private static String balHome;
private static String balVersion;
private static boolean isRemoteEnabled = false;

@Override
public List<Artifact> getArtifacts() {
Expand Down Expand Up @@ -86,15 +87,19 @@ private Artifact createArtifact(ObjectValue service, ObjectValue listener) {
}

public static void addServiceListener(BObject listener, BObject service, Object attachPoint) {
if (!isRemoteEnabled) {
return;
}
BServiceType serviceType = (BServiceType) service.getType();
serviceType.attachPoint = attachPoint;
serviceListenerMap.put((ObjectValue) service, (ObjectValue) listener);
listenerServiceMap.put((ObjectValue) listener, (ObjectValue) service);
}

public static void addBallerinaInformation(String balHome, String balVersion) {
public static void addBallerinaRuntimeInformation(String balHome, String balVersion, boolean isRemoteEnabled) {
RepositoryImpl.balHome = balHome;
RepositoryImpl.balVersion = balVersion;
RepositoryImpl.isRemoteEnabled = isRemoteEnabled;
}

private static String generateNodeId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,8 @@ private void generateModuleClasses(BIRPackage module, Map<String, byte[]> jarEnt
}
}

MainMethodGen mainMethodGen = new MainMethodGen(symbolTable, jvmTypeGen, asyncDataCollector);
MainMethodGen mainMethodGen = new MainMethodGen(symbolTable, jvmTypeGen, asyncDataCollector,
isRemoteMgtEnabled);
mainMethodGen.generateMainMethod(mainFunc, cw, module, moduleClass, serviceEPAvailable, isTestable);
initMethodGen.generateLambdaForModuleExecuteFunction(cw, moduleClass, jvmCastGen, mainFunc,
testExecuteFunc);
Expand Down Expand Up @@ -760,8 +761,7 @@ CompiledJarFile generate(BIRPackage module, boolean isEntry) {
rewriteRecordInits(module.typeDefs);

// generate object/record value classes
JvmValueGen valueGen = new JvmValueGen(module, this, methodGen, typeHashVisitor, types,
isRemoteMgtEnabled);
JvmValueGen valueGen = new JvmValueGen(module, this, methodGen, typeHashVisitor, types);
JvmCastGen jvmCastGen = new JvmCastGen(symbolTable, jvmTypeGen, types);
valueGen.generateValueClasses(jarEntries, jvmConstantsGen, jvmTypeGen);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ public class JvmSignatures {
public static final String ALT_RECEIVE_CALL = "(L" + STRAND_CLASS + ";[L" + STRING_VALUE + ";)L" + OBJECT + ";";
public static final String MULTIPLE_RECEIVE_CALL = "(L" + STRAND_CLASS + ";[L" + RECEIVE_FIELD + ";L" + TYPE +
";)L" + OBJECT + ";";
public static final String ADD_BALLERINA_INFO = "(L" + STRING_VALUE + ";L" + STRING_VALUE + ";)V";
public static final String ADD_BALLERINA_INFO = "(L" + STRING_VALUE + ";L" + STRING_VALUE + ";Z)V";

private JvmSignatures() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,13 @@ public class JvmValueGen {
private final Types types;

JvmValueGen(BIRNode.BIRPackage module, JvmPackageGen jvmPackageGen, MethodGen methodGen,
TypeHashVisitor typeHashVisitor, Types types, boolean isRemoteMgtEnabled) {
TypeHashVisitor typeHashVisitor, Types types) {
this.module = module;
this.jvmPackageGen = jvmPackageGen;
this.methodGen = methodGen;
this.booleanType = jvmPackageGen.symbolTable.booleanType;
this.jvmRecordGen = new JvmRecordGen(jvmPackageGen.symbolTable);
this.jvmObjectGen = new JvmObjectGen(isRemoteMgtEnabled);
this.jvmObjectGen = new JvmObjectGen();
this.typeHashVisitor = typeHashVisitor;
this.types = types;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,16 @@ public class MainMethodGen {
private final BIRVarToJVMIndexMap indexMap;
private final JvmTypeGen jvmTypeGen;
private final AsyncDataCollector asyncDataCollector;
private final boolean isRemoteMgtEnabled;

public MainMethodGen(SymbolTable symbolTable, JvmTypeGen jvmTypeGen, AsyncDataCollector asyncDataCollector) {
public MainMethodGen(SymbolTable symbolTable, JvmTypeGen jvmTypeGen, AsyncDataCollector asyncDataCollector,
boolean isRemoteMgtEnabled) {
this.symbolTable = symbolTable;
// add main string[] args param first
indexMap = new BIRVarToJVMIndexMap(1);
this.jvmTypeGen = jvmTypeGen;
this.asyncDataCollector = asyncDataCollector;
this.isRemoteMgtEnabled = isRemoteMgtEnabled;
}

public void generateMainMethod(BIRNode.BIRFunction userMainFunc, ClassWriter cw, BIRNode.BIRPackage pkg,
Expand All @@ -161,7 +164,7 @@ public void generateMainMethod(BIRNode.BIRFunction userMainFunc, ClassWriter cw,

// check for java compatibility
generateJavaCompatibilityCheck(mv);
generateBallerinaNodeInformation(mv);
generateBallerinaRuntimeInformation(mv);
invokeConfigInit(mv, pkg.packageID);
// start all listeners and TRAP signal handler
startListenersAndSignalHandler(mv, serviceEPAvailable);
Expand Down Expand Up @@ -195,12 +198,17 @@ public void generateMainMethod(BIRNode.BIRFunction userMainFunc, ClassWriter cw,
mv.visitEnd();
}

private void generateBallerinaNodeInformation(MethodVisitor mv) {
private void generateBallerinaRuntimeInformation(MethodVisitor mv) {
String property = System.getProperty(BALLERINA_HOME);
mv.visitLdcInsn(property == null ? "" : property);
property = System.getProperty(BALLERINA_VERSION);
mv.visitLdcInsn(property == null ? "" : property);
mv.visitMethodInsn(Opcodes.INVOKESTATIC, REPOSITORY_IMPL, "addBallerinaInformation", ADD_BALLERINA_INFO,
if (isRemoteMgtEnabled) {
mv.visitInsn(ICONST_1);
} else {
mv.visitInsn(ICONST_0);
}
mv.visitMethodInsn(Opcodes.INVOKESTATIC, REPOSITORY_IMPL, "addBallerinaRuntimeInformation", ADD_BALLERINA_INFO,
false);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,6 @@
*/
public class JvmObjectGen {

private final boolean isRemoteMgtEnabled;

public JvmObjectGen(boolean isRemoteMgtEnabled) {
this.isRemoteMgtEnabled = isRemoteMgtEnabled;
}

static final FieldNameHashComparator FIELD_NAME_HASH_COMPARATOR = new FieldNameHashComparator();

public void createAndSplitCallMethod(ClassWriter cw, List<BIRNode.BIRFunction> functions, String objClassName,
Expand Down Expand Up @@ -203,8 +197,7 @@ public void createAndSplitCallMethod(ClassWriter cw, List<BIRNode.BIRFunction> f
}

private boolean isListenerAttach(BIRNode.BIRFunction func) {
return isRemoteMgtEnabled && func.name.value.equals("attach")
&& Symbols.isFlagOn(func.parameters.get(0).type.flags, Flags.SERVICE);
return func.name.value.equals("attach") && Symbols.isFlagOn(func.parameters.get(0).type.flags, Flags.SERVICE);
}

public void createAndSplitGetMethod(ClassWriter cw, Map<String, BField> fields, String className,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,16 @@ public void testRecordNoStrandDefaultValue() {

@Test
public void testRuntimeManagementAPI() {
CompileResult strandResult = BCompileUtil.compile("test-src/runtime/api/runtime_mgt");
AtomicReference<Throwable> exceptionRef = new AtomicReference<>();
final Scheduler scheduler = new Scheduler(false);
Thread thread1 = new Thread(() -> {
BRunUtil.runOnSchedule(strandResult, "main", scheduler);
try {
CompileResult strandResult = BCompileUtil.compile("test-src/runtime/api/runtime_mgt");
Thread.sleep(5000);
BRunUtil.runOnSchedule(strandResult, "main", scheduler);
} catch (Throwable e) {
exceptionRef.set(e);
}
});
Thread thread2 = new Thread(() -> {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
org= "testorg"
name="runtime_mgt"
version= "1.0.0"

[build-options]
remoteManagement=true

0 comments on commit 66ab6b6

Please sign in to comment.