Skip to content

Commit

Permalink
Merge pull request #42470 from ballerina-platform/runtime-mgt
Browse files Browse the repository at this point in the history
Merge remote management APIs to master
  • Loading branch information
HindujaB authored Apr 4, 2024
2 parents ab8cf65 + c071f80 commit 582d5b3
Show file tree
Hide file tree
Showing 17 changed files with 514 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://wso2.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.ballerina.runtime.api;

import java.util.Map;

/**
* Represents a Ballerina artifact.
*
* @since 2201.9.0
*/
public abstract class Artifact {

public final String name;
public final ArtifactType type;

protected Artifact(String name, ArtifactType type) {
this.name = name;
this.type = type;
}

public enum ArtifactType {
SERVICE
}

/**
* Get a detail of the Ballerina artifact.
* @param detailKey key of the detail.
* @return the detail object.
*/
public abstract Object getDetail(String detailKey);

/**
* Get all details of the Ballerina artifact.
* @return a map of all details.
*/
public abstract Map<String, Object> getAllDetails();

}
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,6 @@ public abstract class Environment {
* @return value stored in the strand.
*/
public abstract Object getStrandLocal(String key);

public abstract Repository getRepository();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://wso2.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.ballerina.runtime.api;

import java.util.Map;

/**
* Represents a Ballerina Node.
*
* @since 2201.9.0
*/
public abstract class Node {
public String nodeId;

public Node(String nodeId) {
this.nodeId = nodeId;
}

/**
* Get a detail of the Ballerina node.
* @param detailKey key of the detail.
* @return the detail object.
*/
public abstract Object getDetail(String detailKey);

/**
* Get all details of the Ballerina node.
* @return a map of all details.
*/
public abstract Map<String, Object> getAllDetails();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://wso2.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.ballerina.runtime.api;

import java.util.List;

/**
* External API to be used by the remote-management module to provide Ballerina runtime artifacts.
*
* @since 2201.9.0
*/
public interface Repository {

/**
* Get the list of runtime artifacts.
* @return List of artifacts that contains information about the active services and listeners.
*/
public List<Artifact> getArtifacts();

/**
* Get the current Ballerina node.
* @return Ballerina node.
*/
public Node getNode();
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import io.ballerina.runtime.api.Environment;
import io.ballerina.runtime.api.Module;
import io.ballerina.runtime.api.Repository;
import io.ballerina.runtime.api.async.StrandMetadata;
import io.ballerina.runtime.api.types.Parameter;
import io.ballerina.runtime.internal.scheduling.State;
Expand All @@ -40,6 +41,7 @@ public class BalEnvironment extends Environment {
private Module currentModule;
private String funcName;
private Parameter[] funcPathParams;
private Repository repository;

public BalEnvironment(Strand strand) {
this.strand = strand;
Expand All @@ -49,12 +51,14 @@ public BalEnvironment(Strand strand, Module currentModule) {
this.strand = strand;
this.currentModule = currentModule;
future = new BalFuture(this.strand);
this.repository = new RepositoryImpl();
}

public BalEnvironment(Strand strand, Module currentModule, String funcName, Parameter[] funcPathParams) {
this(strand, currentModule);
this.funcName = funcName;
this.funcPathParams = funcPathParams;
this.repository = new RepositoryImpl();
}

/**
Expand Down Expand Up @@ -154,4 +158,8 @@ public void setStrandLocal(String key, Object value) {
public Object getStrandLocal(String key) {
return strand.getProperty(key);
}

public Repository getRepository() {
return this.repository;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
/*
* Copyright (c) 2024, WSO2 LLC. (http://wso2.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.ballerina.runtime.internal;

import io.ballerina.runtime.api.Artifact;
import io.ballerina.runtime.api.Node;
import io.ballerina.runtime.api.Repository;
import io.ballerina.runtime.api.utils.TypeUtils;
import io.ballerina.runtime.api.values.BObject;
import io.ballerina.runtime.internal.types.BServiceType;
import io.ballerina.runtime.internal.values.ObjectValue;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
* API implementation for the runtime-management module to provide Ballerina runtime artifacts.
*
* @since 2201.9.0
*/
public class RepositoryImpl implements Repository {

private static final Map<ObjectValue, ObjectValue> serviceListenerMap = new HashMap<>();
private static final Map<ObjectValue, ObjectValue> listenerServiceMap = new HashMap<>();
private static final String nodeId = generateNodeId();
private static String balHome;
private static String balVersion;

@Override
public List<Artifact> getArtifacts() {
List<Artifact> artifacts = new ArrayList<>();
for (Map.Entry<ObjectValue, ObjectValue> entry : serviceListenerMap.entrySet()) {
ObjectValue service = entry.getKey();
ObjectValue listener = entry.getValue();
Artifact artifact = createArtifact(service, listener);
artifacts.add(artifact);
}
for (Map.Entry<ObjectValue, ObjectValue> entry : listenerServiceMap.entrySet()) {
ObjectValue listener = entry.getKey();
ObjectValue service = entry.getValue();

if (!serviceListenerMap.containsKey(service)) {
Artifact artifact = createArtifact(service, listener);
artifacts.add(artifact);
}
}
return artifacts;
}

@Override
public Node getNode() {
return new NodeImpl(nodeId, balVersion, balHome, System.getProperty("os.name"),
System.getProperty("os.version"));
}

private Artifact createArtifact(ObjectValue service, ObjectValue listener) {
ArtifactImpl artifact = new ArtifactImpl(service.toString(), Artifact.ArtifactType.SERVICE);
List<ObjectValue> listeners = (List<ObjectValue>) artifact.getDetail("listeners");
if (listeners == null) {
listeners = new ArrayList<>();
artifact.addDetail("listeners", listeners);
}
listeners.add(listener);
BServiceType serviceType = (BServiceType) TypeUtils.getImpliedType(service.getOriginalType());
artifact.addDetail("attachPoint", serviceType.attachPoint);
artifact.addDetail("service", service);
return artifact;
}

public static void addServiceListener(BObject listener, BObject service, Object attachPoint) {
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) {
RepositoryImpl.balHome = balHome;
RepositoryImpl.balVersion = balVersion;
}

private static String generateNodeId() {
return UUID.randomUUID().toString();
}

/**
* The implementation of Ballerina runtime artifacts.
*/
private static class ArtifactImpl extends Artifact {

private final Map<String, Object> details;

public ArtifactImpl(String name, ArtifactType type) {
super(name, type);
this.details = new HashMap<>();
}

private void addDetail(String detailsKey, Object value) {
this.details.put(detailsKey, value);
}

@Override
public Object getDetail(String detailKey) {
return details.getOrDefault(detailKey, null);
}

@Override
public Map<String, Object> getAllDetails() {
return Collections.unmodifiableMap(details);
}
}

/**
* The implementation of Ballerina runtime node.
*/
private static class NodeImpl extends Node {

private final Map<String, Object> details;

public NodeImpl(String nodeId, String balVersion, String balHome, String osName, String osVersion) {
super(nodeId);
this.details = new HashMap<>();
this.details.put("balVersion", balVersion);
this.details.put("balHome", balHome);
this.details.put("osName", osName);
this.details.put("osVersion", osVersion);
}

@Override
public Object getDetail(String detailKey) {
return details.getOrDefault(detailKey, null);
}

@Override
public Map<String, Object> getAllDetails() {
return Collections.unmodifiableMap(details);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
* @since 0.995.0
*/
public class BServiceType extends BNetworkObjectType implements ServiceType {

public Object attachPoint = null;
public BServiceType(String typeName, Module pkg, long flags) {
super(typeName, pkg, flags);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ public class JvmConstants {
public static final String RUNTIME_REGISTRY_CLASS = "io/ballerina/runtime/internal/scheduling/RuntimeRegistry";
public static final String VALUE_COMPARISON_UTILS = "io/ballerina/runtime/internal/ValueComparisonUtils";
public static final String REG_EXP_FACTORY = "io/ballerina/runtime/internal/regexp/RegExpFactory";
public static final String REPOSITORY_IMPL = "io/ballerina/runtime/internal/RepositoryImpl";

// other java classes
public static final String OBJECT = "java/lang/Object";
Expand Down Expand Up @@ -432,6 +433,8 @@ public class JvmConstants {
public static final String OBSERVABLE_ANNOTATION = "ballerina/observe/Observable";
public static final String DISPLAY_ANNOTATION = "display";
public static final String RECORD_CHECKPOINT_METHOD = "recordCheckpoint";
public static final String BALLERINA_HOME = "ballerina.home";
public static final String BALLERINA_VERSION = "ballerina.version";
public static final String GET_ELEMENT_OR_NIL = "getElementOrNil";
public static final String GET_ELEMENT = "getElement";
public static final String FILL_AND_GET = "fillAndGet";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -512,9 +512,11 @@ public class JvmSignatures {
public static final String GET_TEST_CONFIG_PATH = "(L" + MODULE + ";L" + STRING_VALUE + ";L" + STRING_VALUE +
";)L" + CONFIG_DETAILS + ";";
public static final String SET_DEFAULT_VALUE_METHOD = "(L" + STRING_VALUE + ";L" + B_FUNCTION_POINTER + ";)V";
public static final String ADD_SERVICE_LISTENER = "(L" + B_OBJECT + ";L" + B_OBJECT + ";L" + OBJECT + ";)V";
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";

private JvmSignatures() {
}
Expand Down
Loading

0 comments on commit 582d5b3

Please sign in to comment.