From 96b9bfb3ab7ed22d74837b9d31e07bc896508a4d Mon Sep 17 00:00:00 2001 From: joeyleeeeeee97 Date: Sun, 7 Feb 2021 14:10:15 +0800 Subject: [PATCH] [Wisp] ResourceContainerMXBean Summary: Add ResourceContainerMXBean, currently only support CPU resource amount. Test Plan: jtreg test/com/alibaba/ Reviewed-by: yuleil, zhengxiaolinX Issue: https://github.com/alibaba/dragonwell8/issues/206 --- .../alibaba/wisp/engine/WispControlGroup.java | 9 +++ .../management/ResourceContainerMXBean.java | 10 ++++ .../com/alibaba/rcm/ResourceContainer.java | 4 ++ .../rcm/ResourceContainerMXBeanImpl.java | 34 +++++++++++ .../alibaba/rcm/ResourceContainerMonitor.java | 40 +++++++++++++ .../internal/AbstractResourceContainer.java | 22 +++++++ .../tenant/TenantResourceContainer.java | 5 ++ .../lang/management/PlatformComponent.java | 18 +++++- .../management/ManagementFactoryHelper.java | 12 +++- test/com/alibaba/rcm/RcmCpuTest.java | 41 ++++++------- test/com/alibaba/rcm/RcmMXBeanTest.java | 59 +++++++++++++++++++ test/com/alibaba/rcm/RcmRootTest.java | 22 +++---- test/com/alibaba/rcm/RcmUpdateTest.java | 26 +++----- test/com/alibaba/rcm/RcmUtils.java | 49 +++++++++++++++ test/com/alibaba/rcm/TestConfiguration.java | 28 +-------- test/com/alibaba/rcm/TestRootNotNull.java | 24 +------- test/com/alibaba/rcm/TestSkeletalAttach.java | 29 ++------- test/com/alibaba/rcm/TestState.java | 28 +-------- .../rcm/TestSwitchBetweenContainers.java | 28 +-------- .../alibaba/rcm/demo/MyResourceContainer.java | 28 ++------- .../alibaba/rcm/demo/MyResourceFactory.java | 22 ------- test/com/alibaba/rcm/demo/MyResourceType.java | 22 ------- .../rcm/demo/RCInheritedThreadFactory.java | 22 ------- 23 files changed, 312 insertions(+), 270 deletions(-) create mode 100644 src/share/classes/com/alibaba/management/ResourceContainerMXBean.java create mode 100644 src/share/classes/com/alibaba/rcm/ResourceContainerMXBeanImpl.java create mode 100644 src/share/classes/com/alibaba/rcm/ResourceContainerMonitor.java create mode 100644 test/com/alibaba/rcm/RcmMXBeanTest.java create mode 100644 test/com/alibaba/rcm/RcmUtils.java diff --git a/src/linux/classes/com/alibaba/wisp/engine/WispControlGroup.java b/src/linux/classes/com/alibaba/wisp/engine/WispControlGroup.java index 127239746f..8b0d2767d6 100644 --- a/src/linux/classes/com/alibaba/wisp/engine/WispControlGroup.java +++ b/src/linux/classes/com/alibaba/wisp/engine/WispControlGroup.java @@ -85,6 +85,7 @@ private WispControlGroup(int cfsQuotaUs, int cfsPeriodUs) { private final AtomicLong currentPeriodStart; private final AtomicLong remainQuota; volatile Boolean destroyed = false; + private long totalConsume = 0; CountDownLatch destroyLatch = new CountDownLatch(1); private static class CpuLimit { @@ -142,6 +143,7 @@ long calcCpuTicks(WispTask task) { long usage = System.nanoTime() - task.enterTs; remainQuota.addAndGet(-usage); task.enterTs = 0; + totalConsume += usage; return usage; } @@ -276,6 +278,13 @@ public void destroy() { } } + @Override + public Long getConsumedAmount(ResourceType resourceType) { + if (resourceType != ResourceType.CPU_PERCENT) + return 0L; + return totalConsume; + } + @Override protected void attach() { super.attach(); diff --git a/src/share/classes/com/alibaba/management/ResourceContainerMXBean.java b/src/share/classes/com/alibaba/management/ResourceContainerMXBean.java new file mode 100644 index 0000000000..489a1941ea --- /dev/null +++ b/src/share/classes/com/alibaba/management/ResourceContainerMXBean.java @@ -0,0 +1,10 @@ +package com.alibaba.management; + +import java.lang.management.PlatformManagedObject; +import java.util.List; + +public interface ResourceContainerMXBean extends PlatformManagedObject { + List getAllContainerIds(); + List getConstraintsById(long id); + long getCPUResourceConsumedAmount(long id); +} diff --git a/src/share/classes/com/alibaba/rcm/ResourceContainer.java b/src/share/classes/com/alibaba/rcm/ResourceContainer.java index d8f639b2f5..ab0f1b53cb 100644 --- a/src/share/classes/com/alibaba/rcm/ResourceContainer.java +++ b/src/share/classes/com/alibaba/rcm/ResourceContainer.java @@ -207,4 +207,8 @@ static ResourceContainer current() { * Then the container state will become {@link State#DEAD}. */ void destroy(); + + Long getId(); + + Long getConsumedAmount(ResourceType resourceType); } diff --git a/src/share/classes/com/alibaba/rcm/ResourceContainerMXBeanImpl.java b/src/share/classes/com/alibaba/rcm/ResourceContainerMXBeanImpl.java new file mode 100644 index 0000000000..b5b3d3aa05 --- /dev/null +++ b/src/share/classes/com/alibaba/rcm/ResourceContainerMXBeanImpl.java @@ -0,0 +1,34 @@ +package com.alibaba.rcm; + +import com.alibaba.management.ResourceContainerMXBean; +import sun.management.Util; + +import javax.management.ObjectName; +import java.util.List; +import java.util.stream.Collectors; + +public class ResourceContainerMXBeanImpl implements ResourceContainerMXBean { + private final static String TENANT_CONTAINER_MXBEAN_NAME = "com.alibaba.management:type=ResourceContainer"; + + @Override + public List getAllContainerIds() { + return ResourceContainerMonitor.getAllContainerIds(); + } + + @Override + public List getConstraintsById(long id) { + return ResourceContainerMonitor.getConstraintsById(id).stream().map(c -> c.getValues()[0]).collect(Collectors.toList()); + } + + + @Override + public long getCPUResourceConsumedAmount(long id) { + ResourceContainer container = ResourceContainerMonitor.getContainerById(id); + return container.getConsumedAmount(ResourceType.CPU_PERCENT); + } + + @Override + public ObjectName getObjectName() { + return Util.newObjectName(TENANT_CONTAINER_MXBEAN_NAME); + } +} diff --git a/src/share/classes/com/alibaba/rcm/ResourceContainerMonitor.java b/src/share/classes/com/alibaba/rcm/ResourceContainerMonitor.java new file mode 100644 index 0000000000..9cab2af924 --- /dev/null +++ b/src/share/classes/com/alibaba/rcm/ResourceContainerMonitor.java @@ -0,0 +1,40 @@ +package com.alibaba.rcm; + +import com.alibaba.rcm.Constraint; +import com.alibaba.rcm.ResourceContainer; +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; +import java.util.concurrent.atomic.AtomicLong; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; + +public class ResourceContainerMonitor { + private static Map tenantContainerMap = new HashMap<>(); + private static long idGen = 0; + + public synchronized static long register(ResourceContainer resourceContainer) { + long id = idGen++; + tenantContainerMap.put(id, resourceContainer); + return id; + } + + public synchronized static ResourceContainer getContainerById(long id) { + return tenantContainerMap.get(id); + } + + public synchronized static List getAllContainerIds() { + return new ArrayList<>(tenantContainerMap.keySet()); + } + + public synchronized static List getConstraintsById(long id) { + AbstractResourceContainer resourceContainer = (AbstractResourceContainer) tenantContainerMap.get(id); + return StreamSupport + .stream(resourceContainer.getConstraints().spliterator(), false) + .collect(Collectors.toList()); + } +} diff --git a/src/share/classes/com/alibaba/rcm/internal/AbstractResourceContainer.java b/src/share/classes/com/alibaba/rcm/internal/AbstractResourceContainer.java index abb0039e05..440a9d63b2 100644 --- a/src/share/classes/com/alibaba/rcm/internal/AbstractResourceContainer.java +++ b/src/share/classes/com/alibaba/rcm/internal/AbstractResourceContainer.java @@ -24,6 +24,8 @@ import com.alibaba.rcm.Constraint; import com.alibaba.rcm.ResourceContainer; +import com.alibaba.rcm.ResourceContainerMonitor; +import com.alibaba.rcm.ResourceType; import sun.misc.SharedSecrets; import java.util.Collections; @@ -40,6 +42,11 @@ public abstract class AbstractResourceContainer implements ResourceContainer { protected final static AbstractResourceContainer ROOT = new RootContainer(); + final long id; + + protected AbstractResourceContainer() { + id = ResourceContainerMonitor.register(this); + } public static AbstractResourceContainer root() { return ROOT; @@ -86,6 +93,11 @@ protected void attach() { SharedSecrets.getJavaLangAccess().setResourceContainer(Thread.currentThread(), this); } + @Override + public Long getId() { + return id; + } + /** * Detach from this resource container and return to root container. *

@@ -141,5 +153,15 @@ public Iterable getConstraints() { public void destroy() { throw new UnsupportedOperationException("destroy() is not supported by root container"); } + + @Override + public Long getId() { + return null; + } + + @Override + public Long getConsumedAmount(ResourceType resourceType) { + return 0L; + } } } diff --git a/src/share/classes/com/alibaba/tenant/TenantResourceContainer.java b/src/share/classes/com/alibaba/tenant/TenantResourceContainer.java index 7d822c96bf..7f2a0a7b41 100644 --- a/src/share/classes/com/alibaba/tenant/TenantResourceContainer.java +++ b/src/share/classes/com/alibaba/tenant/TenantResourceContainer.java @@ -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(); diff --git a/src/share/classes/java/lang/management/PlatformComponent.java b/src/share/classes/java/lang/management/PlatformComponent.java index 93fe022a96..c51b412468 100644 --- a/src/share/classes/java/lang/management/PlatformComponent.java +++ b/src/share/classes/java/lang/management/PlatformComponent.java @@ -35,9 +35,7 @@ import javax.management.MBeanServerConnection; import javax.management.ObjectName; -import com.alibaba.management.TenantContainerMXBean; -import com.alibaba.management.ElasticHeapMXBean; -import com.alibaba.management.WispCounterMXBean; +import com.alibaba.management.*; import com.sun.management.HotSpotDiagnosticMXBean; import com.sun.management.UnixOperatingSystemMXBean; @@ -307,6 +305,20 @@ public List getMXBeans() { }), /** + * Resource Container. + */ + RESOURCE_CONTAINER( + "com.alibaba.management.ResourceContainerMXBean", + "com.alibaba.management", "ResourceContainerMXBean", defaultKeyProperties(), + true, + new MXBeanFetcher() { + public List getMXBeans() { + return Collections.singletonList(ManagementFactoryHelper.getResourceContainerMXBean()); + } + }), + + + /** * Wisp Counter. */ WISP_COUNTER( diff --git a/src/share/classes/sun/management/ManagementFactoryHelper.java b/src/share/classes/sun/management/ManagementFactoryHelper.java index 40a0d63257..ae5e8d0b21 100644 --- a/src/share/classes/sun/management/ManagementFactoryHelper.java +++ b/src/share/classes/sun/management/ManagementFactoryHelper.java @@ -39,9 +39,8 @@ import java.security.PrivilegedActionException; import java.security.PrivilegedExceptionAction; -import com.alibaba.management.TenantContainerMXBean; -import com.alibaba.management.ElasticHeapMXBean; -import com.alibaba.management.WispCounterMXBean; +import com.alibaba.management.*; +import com.alibaba.rcm.ResourceContainerMXBeanImpl; import com.alibaba.tenant.TenantContainerMXBeanImpl; import com.alibaba.jvm.gc.ElasticHeapMXBeanImpl; import com.alibaba.wisp.engine.WispCounterMXBeanImpl; @@ -74,6 +73,7 @@ public class ManagementFactoryHelper { private static TenantContainerMXBeanImpl tenantContainerMBean = null; private static ElasticHeapMXBeanImpl elasticHeapMXBean = null; private static WispCounterMXBeanImpl wispCounterMBean = null; + private static ResourceContainerMXBean resourceContainerMXBean = null; public static synchronized ClassLoadingMXBean getClassLoadingMXBean() { if (classMBean == null) { @@ -137,6 +137,12 @@ public static synchronized WispCounterMXBean getWispCounterMXBean() { } return wispCounterMBean; } + public static synchronized ResourceContainerMXBean getResourceContainerMXBean() { + if (resourceContainerMXBean == null) { + resourceContainerMXBean = new ResourceContainerMXBeanImpl(); + } + return resourceContainerMXBean; + } public static List getMemoryPoolMXBeans() { MemoryPoolMXBean[] pools = MemoryImpl.getMemoryPools(); diff --git a/test/com/alibaba/rcm/RcmCpuTest.java b/test/com/alibaba/rcm/RcmCpuTest.java index 5b15d2b210..78083f1368 100644 --- a/test/com/alibaba/rcm/RcmCpuTest.java +++ b/test/com/alibaba/rcm/RcmCpuTest.java @@ -1,25 +1,18 @@ -/* +package com.alibaba.rcm;/* * @test * @library /lib/testlibrary + * @build RcmCpuTest RcmUtils * @summary test RCM cpu resource control. * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+UseWisp2 -XX:ActiveProcessorCount=4 RcmCpuTest */ -import java.lang.Long; -import java.lang.reflect.Field; - -import com.alibaba.rcm.ResourceContainer; -import com.alibaba.rcm.ResourceType; - import java.security.MessageDigest; - -import java.util.Collections; -import java.util.concurrent.FutureTask; import java.util.concurrent.Callable; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.concurrent.FutureTask; -import static jdk.testlibrary.Asserts.*; +import static jdk.testlibrary.Asserts.assertLT; public class RcmCpuTest { @@ -42,28 +35,32 @@ public Long call() throws Exception { } public static void main(String[] args) throws Exception { - Field f = Class.forName("com.alibaba.wisp.engine.WispConfiguration").getDeclaredField("ENABLE_THREAD_AS_WISP"); - f.setAccessible(true); - boolean isWispEnabled = f.getBoolean(null); - ResourceContainer rc0 = isWispEnabled ? WispResourceContainerFactory.instance() - .createContainer(Collections.singletonList(ResourceType.CPU_PERCENT.newConstraint(40))) : null; - ResourceContainer rc1 = isWispEnabled ? WispResourceContainerFactory.instance() - .createContainer(Collections.singletonList(ResourceType.CPU_PERCENT.newConstraint(80))) : null; + ResourceContainer rc0 = RcmUtils.createContainer( + ResourceType.CPU_PERCENT.newConstraint(40)); + ResourceContainer rc1 = RcmUtils.createContainer( + ResourceType.CPU_PERCENT.newConstraint(80)); + taskFactory(1_000_000).call(); // warm up Callable task0 = taskFactory(2_000_000); Callable task1 = taskFactory(2_000_000); - FutureTask futureTask0 = new FutureTask(task0); - FutureTask futureTask1 = new FutureTask(task1); + FutureTask futureTask0 = new FutureTask<>(task0); + FutureTask futureTask1 = new FutureTask<>(task1); ExecutorService es = Executors.newFixedThreadPool(4); es.submit(() -> { + System.out.println("start-0"); rc0.run(futureTask0); + System.out.println("done-0"); }); es.submit(() -> { + System.out.println("start-1"); rc1.run(futureTask1); + System.out.println("done-1"); }); Long duration0 = futureTask0.get(); Long duration1 = futureTask1.get(); - double ratio = (double) duration1.longValue() / duration0.longValue(); - assertLT(Math.abs(ratio - 0.5), 0.1, "deviation is out of reasonable scope"); + es.shutdownNow(); + + double ratio = (double) duration1 / duration0; + assertLT(Math.abs(ratio - 0.5), 0.10, "deviation is out of reasonable scope"); } } \ No newline at end of file diff --git a/test/com/alibaba/rcm/RcmMXBeanTest.java b/test/com/alibaba/rcm/RcmMXBeanTest.java new file mode 100644 index 0000000000..626f0ed1af --- /dev/null +++ b/test/com/alibaba/rcm/RcmMXBeanTest.java @@ -0,0 +1,59 @@ +/* + * @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 com.alibaba.wisp.engine.WispResourceContainerFactory; + +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() == 4); + + + for (long id : resourceContainerMXBean.getAllContainerIds()) { + if (id != 0) { + assertTrue(resourceContainerMXBean.getConstraintsById(id).size() == 1); + assertEQ(resourceContainerMXBean.getConstraintsById(id).get(0), 80L); + assertGreaterThan(resourceContainerMXBean.getCPUResourceConsumedAmount(id), 0L); + } else { + assertTrue(resourceContainerMXBean.getConstraintsById(id).isEmpty()); + } + } + } +} diff --git a/test/com/alibaba/rcm/RcmRootTest.java b/test/com/alibaba/rcm/RcmRootTest.java index b0e7f93c39..ce88035c91 100644 --- a/test/com/alibaba/rcm/RcmRootTest.java +++ b/test/com/alibaba/rcm/RcmRootTest.java @@ -1,32 +1,26 @@ -/* +package com.alibaba.rcm;/* * @test * @library /lib/testlibrary + * @build RcmRootTest RcmUtils * @summary test RCM root API. * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+UseWisp2 -Dcom.alibaba.wisp.carrierEngines=4 RcmRootTest */ -import sun.misc.SharedSecrets; -import java.lang.reflect.Field; - -import com.alibaba.rcm.ResourceContainer; -import com.alibaba.rcm.ResourceType; import com.alibaba.rcm.internal.AbstractResourceContainer; +import sun.misc.SharedSecrets; -import java.util.Collections; -import java.util.concurrent.FutureTask; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.concurrent.FutureTask; -import static jdk.testlibrary.Asserts.*; +import static jdk.testlibrary.Asserts.assertFalse; +import static jdk.testlibrary.Asserts.assertTrue; public class RcmRootTest { public static void main(String[] args) throws Exception { - Field f = Class.forName("com.alibaba.wisp.engine.WispConfiguration").getDeclaredField("ENABLE_THREAD_AS_WISP"); - f.setAccessible(true); - boolean isWispEnabled = f.getBoolean(null); - ResourceContainer rc0 = isWispEnabled ? WispResourceContainerFactory.instance().createContainer(Collections.singletonList(ResourceType.CPU_PERCENT.newConstraint(80))) : null; - ResourceContainer rc1 = isWispEnabled ? WispResourceContainerFactory.instance().createContainer(Collections.singletonList(ResourceType.CPU_PERCENT.newConstraint(80))) : null; + ResourceContainer rc0 = RcmUtils.createContainer(); + ResourceContainer rc1 = RcmUtils.createContainer();; FutureTask future0 = new FutureTask<>(() -> { boolean isException = false; diff --git a/test/com/alibaba/rcm/RcmUpdateTest.java b/test/com/alibaba/rcm/RcmUpdateTest.java index ac71ed9583..b237de9c6d 100644 --- a/test/com/alibaba/rcm/RcmUpdateTest.java +++ b/test/com/alibaba/rcm/RcmUpdateTest.java @@ -1,26 +1,20 @@ -/* +package com.alibaba.rcm;/* * @test * @library /lib/testlibrary + * @build RcmUpdateTest RcmUtils * @summary test RCM updating API. * @run main/othervm -XX:+UnlockExperimentalVMOptions -XX:+UseWisp2 -XX:ActiveProcessorCount=4 RcmUpdateTest */ -import java.lang.Long; -import java.lang.reflect.Field; - -import com.alibaba.rcm.ResourceContainer; -import com.alibaba.rcm.ResourceType; import com.alibaba.rcm.internal.AbstractResourceContainer; -import java.util.Collections; +import java.security.MessageDigest; import java.util.concurrent.Callable; -import java.util.concurrent.FutureTask; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.concurrent.FutureTask; -import java.security.MessageDigest; - -import static jdk.testlibrary.Asserts.*; +import static jdk.testlibrary.Asserts.assertLT; public class RcmUpdateTest { @@ -43,11 +37,7 @@ public Long call() throws Exception { } public static void main(String[] args) throws Exception { - Field f = Class.forName("com.alibaba.wisp.engine.WispConfiguration").getDeclaredField("ENABLE_THREAD_AS_WISP"); - f.setAccessible(true); - boolean isWispEnabled = f.getBoolean(null); - ResourceContainer rc0 = isWispEnabled ? WispResourceContainerFactory.instance() - .createContainer(Collections.singletonList(ResourceType.CPU_PERCENT.newConstraint(40))) : null; + ResourceContainer rc0 = RcmUtils.createContainer(ResourceType.CPU_PERCENT.newConstraint(40)); Callable task0 = taskFactory(2_000_000); FutureTask futureTask0 = new FutureTask(task0); @@ -66,8 +56,10 @@ public static void main(String[] args) throws Exception { }); }); Long duration1 = futureTask1.get(); + es.shutdownNow(); double ratio = (double) duration1.longValue() / duration0.longValue(); - assertLT(Math.abs(ratio - 0.5), 0.1, "deviation is out of reasonable scope"); + assertLT(Math.abs(ratio - 0.5), 0.1, "deviation is out of reasonable scope:" + + duration0.longValue() + "/" + duration1.longValue()); } } \ No newline at end of file diff --git a/test/com/alibaba/rcm/RcmUtils.java b/test/com/alibaba/rcm/RcmUtils.java new file mode 100644 index 0000000000..89eb3909fb --- /dev/null +++ b/test/com/alibaba/rcm/RcmUtils.java @@ -0,0 +1,49 @@ +package com.alibaba.rcm; + +import com.alibaba.rcm.Constraint; +import com.alibaba.rcm.ResourceContainer; +import com.alibaba.tenant.TenantConfiguration; +import com.alibaba.tenant.TenantContainer; +import com.alibaba.tenant.TenantContainerFactory; +import com.alibaba.tenant.TenantGlobals; +import com.alibaba.wisp.engine.WispResourceContainerFactory; +import java.lang.reflect.Field; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import static jdk.testlibrary.Asserts.fail; + +public class RcmUtils { + public static final boolean IS_WISP_ENABLED; + public static final boolean IS_TENANT_ENABLED = TenantGlobals.isTenantEnabled(); + + static { + boolean isWispEnabled = false; + try { + Field f = Class.forName("com.alibaba.wisp.engine.WispConfiguration") + .getDeclaredField("ENABLE_THREAD_AS_WISP"); + f.setAccessible(true); + isWispEnabled = f.getBoolean(null); + } catch (Exception e) { + throw new RuntimeException(e); + } + IS_WISP_ENABLED = isWispEnabled; + } + + public static ResourceContainer createContainer(Iterable constraints) { + if (IS_WISP_ENABLED) { + return WispResourceContainerFactory.instance() + .createContainer(constraints); + } else if (IS_TENANT_ENABLED) { + return TenantContainerFactory.instance() + .createContainer(constraints); + } else { + fail("Unsupported scenarios"); + } + return null; + } + + public static ResourceContainer createContainer(Constraint... constraints) { + return createContainer(Stream.of(constraints).collect(Collectors.toList())); + } +} diff --git a/test/com/alibaba/rcm/TestConfiguration.java b/test/com/alibaba/rcm/TestConfiguration.java index bcaea5fa97..527ed3f130 100644 --- a/test/com/alibaba/rcm/TestConfiguration.java +++ b/test/com/alibaba/rcm/TestConfiguration.java @@ -1,25 +1,3 @@ -/* - * Copyright (c) 2020 Alibaba Group Holding Limited. All Rights Reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Alibaba designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - */ - /* * @test * @summary Test builder and update constraint @@ -48,14 +26,14 @@ public static void main(String[] args) { assertTrue(iterator2Stream(mc.operations.iterator()).collect(Collectors.toSet()) .equals(new HashSet<>(Arrays.asList("update " + MyResourceType.MY_RESOURCE1.toString(), - "update " + MyResourceType.MY_RESOURCE2.toString())))); + "update " + MyResourceType.MY_RESOURCE2.toString())))); mc.updateConstraint(MyResourceType.MY_RESOURCE2.newConstraint()); assertTrue(iterator2Stream(mc.operations.iterator()).collect(Collectors.toSet()) .equals(new HashSet<>(Arrays.asList("update " + MyResourceType.MY_RESOURCE1.toString(), - "update " + MyResourceType.MY_RESOURCE2.toString(), - "update " + MyResourceType.MY_RESOURCE2.toString())))); + "update " + MyResourceType.MY_RESOURCE2.toString(), + "update " + MyResourceType.MY_RESOURCE2.toString())))); } private static Stream iterator2Stream(Iterator iterator) { diff --git a/test/com/alibaba/rcm/TestRootNotNull.java b/test/com/alibaba/rcm/TestRootNotNull.java index be5c11790e..1f361a40cd 100644 --- a/test/com/alibaba/rcm/TestRootNotNull.java +++ b/test/com/alibaba/rcm/TestRootNotNull.java @@ -1,26 +1,4 @@ -/* - * Copyright (c) 2020 Alibaba Group Holding Limited. All Rights Reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Alibaba designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - */ - -/* +package com.alibaba.rcm;/* * @test * @summary Test default ResourceContainer is root() instead of null * @library /lib/testlibrary diff --git a/test/com/alibaba/rcm/TestSkeletalAttach.java b/test/com/alibaba/rcm/TestSkeletalAttach.java index 8ee14cffb7..5312124438 100644 --- a/test/com/alibaba/rcm/TestSkeletalAttach.java +++ b/test/com/alibaba/rcm/TestSkeletalAttach.java @@ -1,33 +1,11 @@ -/* - * Copyright (c) 2020 Alibaba Group Holding Limited. All Rights Reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Alibaba designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - */ - -/* +package com.alibaba.rcm;/* * @test * @summary Test skeletal implementation in AbstractResourceContainer * @library /lib/testlibrary + * @modules java.base/com.alibaba.rcm.internal:+open * @run main TestSkeletalAttach */ -import com.alibaba.rcm.ResourceContainer; import demo.MyResourceContainer; import demo.MyResourceFactory; @@ -40,7 +18,8 @@ public class TestSkeletalAttach { public static void main(String[] args) { - MyResourceContainer rc = (MyResourceContainer) MyResourceFactory.INSTANCE.createContainer(Collections.emptyList()); + MyResourceContainer rc = (MyResourceContainer) MyResourceFactory.INSTANCE + .createContainer(Collections.emptyList()); rc.run(() -> { assertListEQ(Collections.singletonList("attach"), rc.operations); }); diff --git a/test/com/alibaba/rcm/TestState.java b/test/com/alibaba/rcm/TestState.java index aa43c521d8..e3161eeafa 100644 --- a/test/com/alibaba/rcm/TestState.java +++ b/test/com/alibaba/rcm/TestState.java @@ -1,38 +1,16 @@ -/* - * Copyright (c) 2020 Alibaba Group Holding Limited. All Rights Reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Alibaba designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - */ - -/* +package com.alibaba.rcm;/* * @test * @summary Test get tenant state * @library /lib/testlibrary + * @modules java.base/com.alibaba.rcm.internal:+open * @run main TestState */ -import com.alibaba.rcm.ResourceContainer; import demo.MyResourceFactory; import java.util.Collections; -import static jdk.testlibrary.Asserts.*; +import static jdk.testlibrary.Asserts.assertEQ; public class TestState { diff --git a/test/com/alibaba/rcm/TestSwitchBetweenContainers.java b/test/com/alibaba/rcm/TestSwitchBetweenContainers.java index 7aa31a16bf..734c19eee5 100644 --- a/test/com/alibaba/rcm/TestSwitchBetweenContainers.java +++ b/test/com/alibaba/rcm/TestSwitchBetweenContainers.java @@ -1,38 +1,16 @@ -/* - * Copyright (c) 2020 Alibaba Group Holding Limited. All Rights Reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Alibaba designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - */ - -/* +package com.alibaba.rcm;/* * @test * @summary Test switch between containers * @library /lib/testlibrary + * @modules java.base/com.alibaba.rcm.internal:+open * @run main TestSkeletalAttach */ -import com.alibaba.rcm.ResourceContainer; import demo.MyResourceFactory; import java.util.Collections; -import static jdk.testlibrary.Asserts.*; +import static jdk.testlibrary.Asserts.assertTrue; public class TestSwitchBetweenContainers { diff --git a/test/com/alibaba/rcm/demo/MyResourceContainer.java b/test/com/alibaba/rcm/demo/MyResourceContainer.java index 1f7e4ae485..6daa5e3007 100644 --- a/test/com/alibaba/rcm/demo/MyResourceContainer.java +++ b/test/com/alibaba/rcm/demo/MyResourceContainer.java @@ -1,28 +1,7 @@ -/* - * Copyright (c) 2020 Alibaba Group Holding Limited. All Rights Reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Alibaba designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - */ - package demo; import com.alibaba.rcm.Constraint; +import com.alibaba.rcm.ResourceType; import com.alibaba.rcm.internal.AbstractResourceContainer; import java.util.ArrayList; @@ -65,4 +44,9 @@ public Iterable getConstraints() { public void destroy() { dead = true; } + + @Override + public Long getConsumedAmount(ResourceType resourceType) { + return null; + } } diff --git a/test/com/alibaba/rcm/demo/MyResourceFactory.java b/test/com/alibaba/rcm/demo/MyResourceFactory.java index 5f9db9c422..2f11b1fb94 100644 --- a/test/com/alibaba/rcm/demo/MyResourceFactory.java +++ b/test/com/alibaba/rcm/demo/MyResourceFactory.java @@ -1,25 +1,3 @@ -/* - * Copyright (c) 2020 Alibaba Group Holding Limited. All Rights Reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Alibaba designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - */ - package demo; import com.alibaba.rcm.Constraint; diff --git a/test/com/alibaba/rcm/demo/MyResourceType.java b/test/com/alibaba/rcm/demo/MyResourceType.java index a7ad4044c0..78b3ef743e 100644 --- a/test/com/alibaba/rcm/demo/MyResourceType.java +++ b/test/com/alibaba/rcm/demo/MyResourceType.java @@ -1,25 +1,3 @@ -/* - * Copyright (c) 2020 Alibaba Group Holding Limited. All Rights Reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Alibaba designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - */ - package demo; import com.alibaba.rcm.ResourceType; diff --git a/test/com/alibaba/rcm/demo/RCInheritedThreadFactory.java b/test/com/alibaba/rcm/demo/RCInheritedThreadFactory.java index 805cff8d02..952a7df688 100644 --- a/test/com/alibaba/rcm/demo/RCInheritedThreadFactory.java +++ b/test/com/alibaba/rcm/demo/RCInheritedThreadFactory.java @@ -1,25 +1,3 @@ -/* - * Copyright (c) 2020 Alibaba Group Holding Limited. All Rights Reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Alibaba designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - */ - package demo; import com.alibaba.rcm.internal.AbstractResourceContainer;