Skip to content

Commit

Permalink
[Wisp] ResourceContainerMXBean
Browse files Browse the repository at this point in the history
Summary: Add ResourceContainerMXBean, currently only
support CPU resource amount.

Test Plan: jtreg test/com/alibaba/

Reviewed-by: yuleil, zhengxiaolinX

Issue: dragonwell-project/dragonwell8#206
  • Loading branch information
joeyleeeeeee97 authored and joeylee.lz committed Feb 8, 2021
1 parent 174b3d7 commit 84a16b9
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
public interface ResourceContainerMXBean extends PlatformManagedObject {
List<Long> getAllContainerIds();
List<Long> getConstraintsById(long id);
long getContainerConsumedAmount(long id);
long getCPUResourceConsumedAmount(long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public List<Long> getConstraintsById(long id) {


@Override
public long getContainerConsumedAmount(long id) {
public long getCPUResourceConsumedAmount(long id) {
ResourceContainer container = ResourceContainerMonitor.getContainerById(id);
return container.getConsumedAmount(ResourceType.CPU_PERCENT);
}
Expand Down
20 changes: 8 additions & 12 deletions src/share/classes/com/alibaba/rcm/ResourceContainerMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.alibaba.rcm.internal.AbstractResourceContainer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
Expand All @@ -13,32 +14,27 @@
import java.util.stream.StreamSupport;

public class ResourceContainerMonitor {
private static Map<Long, ResourceContainer> tenantContainerMap = new ConcurrentHashMap<>();
private static AtomicLong idGen = new AtomicLong(0);
private static Map<Long, ResourceContainer> tenantContainerMap = new HashMap<>();
private static long idGen = 0;

public static long register(ResourceContainer resourceContainer) {
long id = idGen.getAndIncrement();
public synchronized static long register(ResourceContainer resourceContainer) {
long id = idGen++;
tenantContainerMap.put(id, resourceContainer);
return id;
}

public static ResourceContainer getContainerById(long id) {
public synchronized static ResourceContainer getContainerById(long id) {
return tenantContainerMap.get(id);
}

public static List<Long> getAllContainerIds() {
public synchronized static List<Long> getAllContainerIds() {
return new ArrayList<>(tenantContainerMap.keySet());
}

public static List<Constraint> getConstraintsById(long id) {
public synchronized static List<Constraint> getConstraintsById(long id) {
AbstractResourceContainer resourceContainer = (AbstractResourceContainer) tenantContainerMap.get(id);
return StreamSupport
.stream(resourceContainer.getConstraints().spliterator(), false)
.collect(Collectors.toList());
}

public long getContainerConsumedAmount(long id) {
return 0;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,11 @@ public void destroy() {
throw new UnsupportedOperationException("Should not call TenantResourceContainer::destroy() directly");
}

@Override
public Long getConsumedAmount(ResourceType resourceType) {
throw new UnsupportedOperationException();
}

void destroyImpl() {
if (jgroup != null) {
jgroup.destory();
Expand Down
54 changes: 54 additions & 0 deletions test/com/alibaba/rcm/RcmMXBeanTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* @test
* @library /lib/testlibrary
* @summary test RcmMXBeanTest
* @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+UseWisp2 -XX:ActiveProcessorCount=4 RcmMXBeanTest
*/

import com.alibaba.management.ResourceContainerMXBean;
import com.alibaba.rcm.ResourceContainer;
import com.alibaba.rcm.ResourceType;

import javax.management.MBeanServer;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.util.Collections;

import static jdk.testlibrary.Asserts.*;

public class RcmMXBeanTest {
static ResourceContainerMXBean resourceContainerMXBean;

public static void main(String[] args) throws Exception {
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
try {
resourceContainerMXBean = ManagementFactory.newPlatformMXBeanProxy(mbs,
"com.alibaba.management:type=ResourceContainer", ResourceContainerMXBean.class);
} catch (IOException e) {
e.printStackTrace();
}

for (int i = 0; i < 3; i++) {
ResourceContainer rc1 = WispResourceContainerFactory.instance()
.createContainer(Collections.singletonList(ResourceType.CPU_PERCENT.newConstraint(80)));

rc1.run(() -> {
new Thread(() -> {
while (true) {
Thread.yield();
}
}).start();
});
Thread.sleep(1000);
}

assertTrue(resourceContainerMXBean.getAllContainerIds().size() == 3);


for (long id : resourceContainerMXBean.getAllContainerIds()) {
assertEQ(resourceContainerMXBean.getConstraintsById(id).size(), 1L);
assertEQ(resourceContainerMXBean.getConstraintsById(id).get(0), 80L);
assertGreaterThan(resourceContainerMXBean.getCPUResourceConsumedAmount(id), 0L);
}
}
}

0 comments on commit 84a16b9

Please sign in to comment.