From 0425d804f1fcc05d904623a23d6891b72548e148 Mon Sep 17 00:00:00 2001 From: hindujaB Date: Fri, 5 Apr 2024 13:58:06 +0530 Subject: [PATCH 1/4] Move remoteEnabled filtering to runtime (cherry picked from commit fa403b2c14ea59415155118eb3495ce07bfc8e37) --- .../runtime/internal/RepositoryImpl.java | 7 ++++++- .../compiler/bir/codegen/JvmPackageGen.java | 6 +++--- .../compiler/bir/codegen/JvmSignatures.java | 2 +- .../compiler/bir/codegen/JvmValueGen.java | 4 ++-- .../bir/codegen/methodgen/MainMethodGen.java | 15 +++++++++++---- .../bir/codegen/split/values/JvmObjectGen.java | 9 +-------- 6 files changed, 24 insertions(+), 19 deletions(-) diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/RepositoryImpl.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/RepositoryImpl.java index 0e5c697f8cb4..520b233fc622 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/RepositoryImpl.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/RepositoryImpl.java @@ -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 getArtifacts() { @@ -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() { diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmPackageGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmPackageGen.java index 69a7f5f2154c..a82af2f48f9e 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmPackageGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmPackageGen.java @@ -391,7 +391,8 @@ private void generateModuleClasses(BIRPackage module, Map 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); @@ -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); diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmSignatures.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmSignatures.java index 5c8126aef8f1..356149014add 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmSignatures.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmSignatures.java @@ -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() { } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmValueGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmValueGen.java index f3ab9ffb1af7..ad1a99d6ea3b 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmValueGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/JvmValueGen.java @@ -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; } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/methodgen/MainMethodGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/methodgen/MainMethodGen.java index 352cd3ccff3f..a94029e296dc 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/methodgen/MainMethodGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/methodgen/MainMethodGen.java @@ -138,13 +138,15 @@ 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, @@ -161,7 +163,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); @@ -195,12 +197,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); } diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/split/values/JvmObjectGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/split/values/JvmObjectGen.java index a1c9f7976836..3dc77fc9a261 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/split/values/JvmObjectGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/split/values/JvmObjectGen.java @@ -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 functions, String objClassName, @@ -203,8 +197,7 @@ public void createAndSplitCallMethod(ClassWriter cw, List 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 fields, String className, From 0153cb84cb0740d6474b00cf8cf5bc11e35c137e Mon Sep 17 00:00:00 2001 From: hindujaB Date: Fri, 5 Apr 2024 14:03:55 +0530 Subject: [PATCH 2/4] Fix checkstyle errors (cherry picked from commit dbdd1e706215b197f6d2b58c20c5ab0de799e082) --- .../java/io/ballerina/runtime/internal/RepositoryImpl.java | 2 +- .../compiler/bir/codegen/methodgen/MainMethodGen.java | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/RepositoryImpl.java b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/RepositoryImpl.java index 520b233fc622..85b73c8b793a 100644 --- a/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/RepositoryImpl.java +++ b/bvm/ballerina-runtime/src/main/java/io/ballerina/runtime/internal/RepositoryImpl.java @@ -87,7 +87,7 @@ private Artifact createArtifact(ObjectValue service, ObjectValue listener) { } public static void addServiceListener(BObject listener, BObject service, Object attachPoint) { - if (isRemoteEnabled) { + if (!isRemoteEnabled) { return; } BServiceType serviceType = (BServiceType) service.getType(); diff --git a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/methodgen/MainMethodGen.java b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/methodgen/MainMethodGen.java index a94029e296dc..2ede946b3a1c 100644 --- a/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/methodgen/MainMethodGen.java +++ b/compiler/ballerina-lang/src/main/java/org/wso2/ballerinalang/compiler/bir/codegen/methodgen/MainMethodGen.java @@ -140,7 +140,8 @@ public class MainMethodGen { private final AsyncDataCollector asyncDataCollector; private final boolean isRemoteMgtEnabled; - public MainMethodGen(SymbolTable symbolTable, JvmTypeGen jvmTypeGen, AsyncDataCollector asyncDataCollector, boolean isRemoteMgtEnabled) { + public MainMethodGen(SymbolTable symbolTable, JvmTypeGen jvmTypeGen, AsyncDataCollector asyncDataCollector, + boolean isRemoteMgtEnabled) { this.symbolTable = symbolTable; // add main string[] args param first indexMap = new BIRVarToJVMIndexMap(1); From 43474b491d24df4c3564f4855264bf723ec1f577 Mon Sep 17 00:00:00 2001 From: hindujaB Date: Fri, 5 Apr 2024 14:28:57 +0530 Subject: [PATCH 3/4] Fix runtime api test --- .../ballerinalang/test/runtime/api/RuntimeAPITest.java | 8 ++++++-- .../test-src/runtime/api/runtime_mgt/Ballerina.toml | 3 +++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/runtime/api/RuntimeAPITest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/runtime/api/RuntimeAPITest.java index 04a1a92ef1c3..3017935e814b 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/runtime/api/RuntimeAPITest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/runtime/api/RuntimeAPITest.java @@ -98,11 +98,15 @@ public void testRecordNoStrandDefaultValue() { @Test public void testRuntimeManagementAPI() { - CompileResult strandResult = BCompileUtil.compile("test-src/runtime/api/runtime_mgt"); AtomicReference 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"); + BRunUtil.runOnSchedule(strandResult, "main", scheduler); + } catch (Throwable e) { + exceptionRef.set(e); + } }); Thread thread2 = new Thread(() -> { try { diff --git a/tests/jballerina-unit-test/src/test/resources/test-src/runtime/api/runtime_mgt/Ballerina.toml b/tests/jballerina-unit-test/src/test/resources/test-src/runtime/api/runtime_mgt/Ballerina.toml index 331a8155316c..8d2efc931e0a 100644 --- a/tests/jballerina-unit-test/src/test/resources/test-src/runtime/api/runtime_mgt/Ballerina.toml +++ b/tests/jballerina-unit-test/src/test/resources/test-src/runtime/api/runtime_mgt/Ballerina.toml @@ -2,3 +2,6 @@ org= "testorg" name="runtime_mgt" version= "1.0.0" + +[build-options] +remoteManagement=true From 148286d8c30b4fba6f4799662373b8c9caa4d3c0 Mon Sep 17 00:00:00 2001 From: hindujaB Date: Fri, 5 Apr 2024 16:09:52 +0530 Subject: [PATCH 4/4] Fix runtime api test --- .../java/org/ballerinalang/test/runtime/api/RuntimeAPITest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/runtime/api/RuntimeAPITest.java b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/runtime/api/RuntimeAPITest.java index 3017935e814b..75d0c597bb5a 100644 --- a/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/runtime/api/RuntimeAPITest.java +++ b/tests/jballerina-unit-test/src/test/java/org/ballerinalang/test/runtime/api/RuntimeAPITest.java @@ -103,6 +103,7 @@ public void testRuntimeManagementAPI() { Thread thread1 = new Thread(() -> { 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);