提交 f09d14b6 编写于 作者: C Chuansheng Lu 提交者: Jonathan Lu

[MultiTenant] MultiTenant framework

Summary: ported MultiTenant framework to Dragonwell8

Test Plan: jdk/test/multi-tenant

Reviewed-by: yuleil, sanhong

Issue: https://github.com/alibaba/dragonwell8/issues/84
上级 76ecee17
......@@ -43,7 +43,8 @@ H_TARGET_FILES = $(INCLUDEDIR)/jdwpTransport.h \
$(INCLUDEDIR)/classfile_constants.h \
$(INCLUDEDIR)/jawt.h \
$(OPENJDK_TARGET_OS_INCLUDE)/jni_md.h \
$(OPENJDK_TARGET_OS_INCLUDE)/jawt_md.h
$(OPENJDK_TARGET_OS_INCLUDE)/jawt_md.h \
$(INCLUDEDIR)/tenantenv.h \
$(INCLUDEDIR)/%.h: $(JDK_TOPDIR)/src/share/javavm/export/%.h
$(call install-file)
......
......@@ -560,7 +560,8 @@ EXPORTED_PRIVATE_PKGS = com.oracle.net \
com.alibaba.jwarmup \
com.alibaba.management \
com.alibaba.jvm.gc \
com.alibaba.rcm
com.alibaba.rcm \
com.alibaba.tenant
$(IMAGES_OUTPUTDIR)/symbols/_the.symbols: $(IMAGES_OUTPUTDIR)/lib/rt.jar
$(RM) -r $(IMAGES_OUTPUTDIR)/symbols/META-INF/sym
......
......@@ -2559,4 +2559,10 @@ sun/awt/X11/XErrorEvent
com/alibaba/jwarmup/JWarmUp
com/alibaba/management
com/alibaba/jvm/gc
# eea35d9d56e0006e
com/alibaba/tenant/TenantContainer
com/alibaba/tenant/TenantConfiguration
com/alibaba/tenant/TenantException
com/alibaba/tenant/TenantState
com/alibaba/tenant/TenantGlobals
com/alibaba/tenant/TenantContainerFactory
# eea35d9d56e0006e
\ No newline at end of file
......@@ -143,6 +143,7 @@ LIBJAVA_SRC_DIRS += $(JDK_TOPDIR)/src/$(OPENJDK_TARGET_OS_API_DIR)/native/java/l
$(JDK_TOPDIR)/src/share/native/common \
$(JDK_TOPDIR)/src/share/native/com/alibaba/jwarmup \
$(JDK_TOPDIR)/src/share/native/com/alibaba/jvm/gc \
$(JDK_TOPDIR)/src/share/native/com/alibaba/tenant \
$(JDK_TOPDIR)/src/share/native/sun/misc \
$(JDK_TOPDIR)/src/share/native/sun/reflect \
$(JDK_TOPDIR)/src/share/native/java/util \
......
......@@ -215,7 +215,10 @@ SUNWprivate_1.1 {
Java_java_lang_System_setIn0;
Java_java_lang_System_setOut0;
Java_java_lang_Thread_registerNatives;
Java_com_alibaba_tenant_NativeDispatcher_registerNatives0;
Java_com_alibaba_tenant_NativeDispatcher_getThreadsAllocatedMemory;
Java_com_alibaba_jwarmup_JWarmUp_registerNatives;
Java_com_alibaba_tenant_TenantGlobals_getTenantFlags;
Java_com_alibaba_jvm_gc_ElasticHeapMXBeanImpl_registerNatives;
Java_java_lang_Throwable_fillInStackTrace;
Java_java_lang_Throwable_getStackTraceDepth;
......
......@@ -10,6 +10,7 @@ text: .text%collapse: OUTPUTDIR/canonicalize_md.o;
text: .text%Java_java_lang_Object_registerNatives;
text: .text%Java_java_lang_System_registerNatives;
text: .text%Java_java_lang_Thread_registerNatives;
text: .text%Java_com_alibaba_tenant_TenantGlobals_getTenantFlags;
text: .text%Java_com_alibaba_jwarmup_JWarmUp_registerNatives;
text: .text%Java_com_alibaba_jvm_gc_ElasticHeapMXBeanImpl_registerNatives;
text: .text%Java_java_security_AccessController_getStackAccessControlContext;
......
package com.alibaba.management;
import java.lang.management.PlatformManagedObject;
import java.util.List;
/**
* MXBean interface to interact with TenantContainer object
*/
public interface TenantContainerMXBean extends PlatformManagedObject {
/**
* Retrieve all the IDs of all created {@code TenantContainer}'s
* @return List of TenantContainer IDs
*/
public List<Long> getAllTenantIds();
/**
* Retrieve the accumulated allocated memory size in bytes
* of a {@code TenantContainer} represented by {@code id}
* @param id ID of the {@code TenantContainer} object to be queried
* @return Accumulated allocated memory size in bytes
* @throws IllegalArgumentException If cannot find valid {@code TenantContainer}
* object corresponding to the given ID
*/
public long getTenantAllocatedMemoryById(long id);
/**
* Retrieve name of a {@code TenantContainer} object represent by 'id'
* @param id ID of the {@code TenantContainer} object to be queried
* @return Name of found ID
* @throws IllegalArgumentException If cannot find valid {@code TenantContainer}
* object corresponding to the given ID
*/
public String getTenantNameById(long id);
}
/*
* 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.tenant;
import java.security.AccessController;
/**
* All cgroup operations are in this class
*
*/
class NativeDispatcher {
// Attach the current thread to given {@code tenant}
native void attach(TenantContainer tenant);
// Gets an array containing the amount of memory allocated on the Java heap for a set of threads (in bytes)
native void getThreadsAllocatedMemory(long[] ids, long[] memSizes);
private static native void registerNatives0();
static {
AccessController.doPrivileged(
new java.security.PrivilegedAction<Void>() {
public Void run() {
System.loadLibrary("java");
return null;
}
});
registerNatives0();
}
}
\ No newline at end of file
/*
* 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.tenant;
import sun.misc.SharedSecrets;
import sun.misc.VM;
import sun.security.action.GetPropertyAction;
import java.security.AccessController;
import java.util.HashMap;
import java.util.Map;
import java.util.Collection;
import com.alibaba.rcm.ResourceType;
import com.alibaba.rcm.Constraint;
/**
*
* The configuration used by tenant
*/
public class TenantConfiguration {
/*
* Resource throttling configurations
*/
private Map<ResourceType, Constraint> constraints = new HashMap<>();
/**
* Create an empty TenantConfiguration, no limitations on any resource
*/
public TenantConfiguration() {
}
/**
* Build TenantConfiguration with given constraints
* @param constraints
*/
public TenantConfiguration(Iterable<Constraint> constraints) {
if (constraints != null) {
for (Constraint c : constraints) {
this.constraints.put(c.getResourceType(), c);
}
}
}
/*
* @return all resource limits specified by this configuration
*/
Collection<Constraint> getAllConstraints() {
return constraints.values();
}
void setConstraint(Constraint constraint) {
constraints.put(constraint.getResourceType(), constraint);
}
}
/*
* 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.tenant;
import com.alibaba.rcm.Constraint;
import com.alibaba.rcm.ResourceContainer;
import com.alibaba.rcm.ResourceContainerFactory;
/**
* Singleton factory specialized for multi-tenant {@code ResourceContainer}
* With support of new RCM API (com.alibaba.rcm)
*
*/
public class TenantContainerFactory implements ResourceContainerFactory {
/**
* Create a {@code ResourceContainer} which is capable of throttling resource
* using MuliTenant facitlities.
* A {@code TenantContainer} object will be created implicitly for each successful
* call to {@code TenantContainerFactory.createContainer}
* @param constraints the target {@code Constraint}s
* @return
*/
@Override
public TenantResourceContainer createContainer(Iterable<Constraint> constraints) {
TenantContainer tenant = TenantContainer.create(new TenantConfiguration(constraints));
return tenant.resourceContainer;
}
/**
* Retrieve the {@code TenantContainer} object associated with given {@code ResourceContainer}
*
* @param resourceContainer
* @return
*/
public static TenantContainer tenantContainerOf(ResourceContainer resourceContainer) {
if (!(resourceContainer instanceof TenantResourceContainer)) {
throw new IllegalArgumentException("Incoming ResourceContainer is not for MultiTenant");
}
return ((TenantResourceContainer) resourceContainer).getTenant();
}
private TenantContainerFactory() { }
private static final class Holder {
private static final TenantContainerFactory INSTANCE = new TenantContainerFactory();
}
/**
*
* @return Singleton instance of TenantContainerFactory
*/
public static TenantContainerFactory instance() {
return Holder.INSTANCE;
}
}
\ No newline at end of file
package com.alibaba.tenant;
import com.alibaba.management.TenantContainerMXBean;
import sun.management.Util;
import javax.management.ObjectName;
import java.util.List;
/**
* Implementation class for TenantContainerMXBean.
*/
public class TenantContainerMXBeanImpl implements TenantContainerMXBean {
private final static String TENANT_CONTAINER_MXBEAN_NAME = "com.alibaba.management:type=TenantContainer";
@Override
public List<Long> getAllTenantIds() {
return TenantContainer.getAllTenantIds();
}
public long getTenantAllocatedMemoryById(long id) {
TenantContainer container = TenantContainer.getTenantContainerById(id);
if (null == container) {
throw new IllegalArgumentException("The id of tenant is invalid !");
}
return container.getAllocatedMemory();
}
@Override
public ObjectName getObjectName() {
return Util.newObjectName(TENANT_CONTAINER_MXBEAN_NAME);
}
@Override
public String getTenantNameById(long id) {
TenantContainer container = TenantContainer.getTenantContainerById(id);
if (null == container) {
throw new IllegalArgumentException("The id of tenant is invalid !");
}
return container.getName();
}
}
/*
* 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.tenant;
/**
* Will be thrown when problems found with running a given code snippet
* in a {@code TenantContainer} with method {@code TenantContainer.run()},
* or from newly created thread in a TenantContainer.
*
*/
public class TenantException extends Exception {
private static final long serialVersionUID = 436814125546098458L;
/**
* Construct a {@code TenantException} object with given message
*/
public TenantException(String msg) {
super(msg);
}
}
/*
* 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.tenant;
/**
* This class defines the constants used by multi-tenant JDK
*/
public class TenantGlobals {
/**
* Retrieves the flags used by multi-tenant module.
* @return the flags
*/
private native static int getTenantFlags();
private final static int flags = getTenantFlags();
/**** Be careful: the following bit definitions must be consistent with
**** the ones defined in prims/tenantenv.cpp **/
/**
* Bit to indicate that if the multi-tenant feature is enabled.
*/
public static final int TENANT_FLAG_MULTI_TENANT_ENABLED = 0x1;
/**
* Bit to indicate that if heap throttling feature is enabled
*/
public static final int TENANT_FLAG_HEAP_THROTTLING_ENABLED = 0x2;
/**
* Bit to indicate that if cpu throttling feature is enabled
*/
public static final int TENANT_FLAG_CPU_THROTTLING_ENABLED = 0x4;
/**
* Bit to indicate that if cpu accounting feature is enabled
*/
public static final int TENANT_FLAG_CPU_ACCOUNTING_ENABLED = 0x40;
/**
* Bit to indicate that if heap isolation feature is enabled
*/
public static final int TENANT_FLAG_HEAP_ISOLATION_ENABLED = 0x80;
private TenantGlobals() { }
/**
* Test if multi-tenant feature is enabled.
* @return true if enabled otherwise false
*/
public static boolean isTenantEnabled() {
return 0 != (flags & TENANT_FLAG_MULTI_TENANT_ENABLED);
}
/**
* Test if heap throttling feature is enabled.
* @return true if enabled otherwise false
*/
public static boolean isHeapThrottlingEnabled() {
return 0 != (flags & TENANT_FLAG_HEAP_THROTTLING_ENABLED);
}
/**
* Test if heap isolation feature is enabled.
* @return true if enabled otherwise false
*/
public static boolean isHeapIsolationEnabled() {
return 0 != (flags & TENANT_FLAG_HEAP_ISOLATION_ENABLED);
}
/**
* Test if cpu throttling feature is enabled.
* @return true if enabled otherwise false
*/
public static boolean isCpuThrottlingEnabled() {
return 0 != (flags & TENANT_FLAG_CPU_THROTTLING_ENABLED);
}
/**
* Test if cpu accounting feature is enabled.
* @return true if enabled otherwise false
*/
public static boolean isCpuAccountingEnabled() {
return 0 != (flags & TENANT_FLAG_CPU_ACCOUNTING_ENABLED);
}
}
/*
* 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.tenant;
import com.alibaba.rcm.Constraint;
import com.alibaba.rcm.ResourceType;
import com.alibaba.rcm.internal.AbstractResourceContainer;
import java.util.HashMap;
import java.util.Map;
import static com.alibaba.tenant.TenantState.*;
class TenantResourceContainer extends AbstractResourceContainer {
TenantResourceContainer(TenantResourceContainer parent,
TenantContainer tenant,
Iterable<Constraint> constraints) {
super();
this.parent = parent;
this.constraints = new HashMap<>();
this.tenant = tenant;
if (constraints != null) {
constraints.forEach(c -> this.constraints.put(c.getResourceType(), c));
}
init();
}
private void init() {
if (TenantGlobals.isCpuThrottlingEnabled() || TenantGlobals.isCpuAccountingEnabled()) {
if (constraints.containsKey(ResourceType.CPU_PERCENT)) {
Constraint c = translate(constraints.remove(ResourceType.CPU_PERCENT));
constraints.put(c.getResourceType(), c);
}
}
}
private static Constraint translate(Constraint constraint) {
return constraint;
}
// cached constraints
private Map<ResourceType, Constraint> constraints;
/*
* The parent container.
*/
private TenantResourceContainer parent;
/*
* Associated TenantContainer object
*/
private TenantContainer tenant;
TenantResourceContainer getParent() {
return parent;
}
TenantContainer getTenant() {
return this.tenant;
}
@Override
protected void attach() {
super.attach();
}
@Override
protected void detach() {
super.detach();
}
@Override
public void run(Runnable command) {
// This is the first thread which runs in this tenant container
if (tenant.getState() == STARTING) {
// move the tenant state to RUNNING
tenant.setState(RUNNING);
}
super.run(command);
}
@Override
public State getState() {
return TenantState.translate(tenant.getState());
}
@Override
public void updateConstraint(Constraint constraint) {
Constraint c = translate(constraint);
constraints.put(c.getResourceType(), c);
}
@Override
public Iterable<Constraint> getConstraints() {
return constraints.values();
}
@Override
public void destroy() {
throw new UnsupportedOperationException("Should not call TenantResourceContainer::destroy() directly");
}
void destroyImpl() {
parent = null;
tenant = null;
}
// exposed to TenantContainer implementation
long getProcessCpuTime() {
return 0;
}
}
/*
* 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.tenant;
import com.alibaba.rcm.ResourceType;
/*
* Type of resource that can be throttled when MultiTenant feature enabled
*/
class TenantResourceType extends ResourceType {
private TenantResourceType(String name, boolean isJGroup) {
super(name);
}
}
/*
* 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.tenant;
import java.util.Collection;
import java.util.IdentityHashMap;
/*
* Class to track and run tenant level shutdown hooks registered through
* <tt>{@link Runtime#addShutdownHook Runtime.addShutdownHook}</tt> or
* <tt>{@link TenantContainer#addShutdownHook TenantContainer.addShutdownHook}</tt>
*
*/
class TenantShutdownHooks {
private IdentityHashMap<Thread, Thread> hooks = new IdentityHashMap<>();
private Collection<Thread> threads = null;
TenantShutdownHooks() {
}
//Add a new shutdown hook.
synchronized void add(Thread hook) {
if(hooks == null) {
throw new IllegalStateException("Shutdown in progress");
}
if (hook.isAlive()) {
throw new IllegalArgumentException("Hook already running");
}
if (hooks.containsKey(hook)) {
throw new IllegalArgumentException("Hook previously registered");
}
hooks.put(hook, hook);
}
//Remove a previously-registered hook.
synchronized boolean remove(Thread hook) {
if(hooks == null) {
throw new IllegalStateException("Shutdown in progress");
}
if (hook == null) {
throw new NullPointerException();
}
return hooks.remove(hook) != null;
}
/* Iterates over all hooks creating a new thread for each
* to run in. Hooks are running concurrently and this method waits for
* them to finish. This function will be called by the tenant destroying
* thread at the beginning of {@code TenantContainer.destroy}.
*/
void runHooks() {
synchronized(this) {
threads = hooks.keySet();
hooks = null;
}
for (Thread hook : threads) {
hook.start();
}
for (Thread hook : threads) {
try {
hook.join();
} catch (InterruptedException x) { }
}
}
}
/*
* 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.tenant;
import com.alibaba.rcm.ResourceContainer.State;
/**
* Defines the state used by TenantContainer
*/
public enum TenantState {
/**
* Just created
*/
STARTING,
/**
* At least one task has been submitted after creation
*/
RUNNING,
/**
* {@code TenantContainer.destroy()} has been called, no new thread
* can be created in this container.
*/
STOPPING,
/**
* All resource has been released fromm this tenant.
* No new calls to {@code TenantContainer.run()} can happen.
*/
DEAD;
static TenantState translate(State state) {
return TenantState.values()[state.ordinal()];
}
static State translate(TenantState tenantState) {
return State.values()[tenantState.ordinal()];
}
}
......@@ -37,10 +37,13 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.locks.LockSupport;
import com.alibaba.rcm.internal.AbstractResourceContainer;
import sun.misc.VM;
import sun.nio.ch.Interruptible;
import sun.reflect.CallerSensitive;
import sun.reflect.Reflection;
import sun.security.util.SecurityConstants;
import com.alibaba.tenant.TenantContainer;
import com.alibaba.tenant.TenantGlobals;
/**
......@@ -214,6 +217,11 @@ class Thread implements Runnable {
private volatile int threadStatus = 0;
/**
* The tenant container which creates this thread object
*/
TenantContainer inheritedTenantContainer;
/**
* The thread attached {@code ResourceContainer}
*/
......@@ -430,8 +438,12 @@ class Thread implements Runnable {
tid = nextThreadID();
/* com.alibaba.rcm API */
this.resourceContainer = parent.resourceContainer != null ?
parent.resourceContainer : AbstractResourceContainer.root();
this.resourceContainer = AbstractResourceContainer.root();
/* Set the tenant container */
if (VM.isBooted() && TenantGlobals.isTenantEnabled()) {
inheritedTenantContainer = TenantContainer.current();
}
}
/**
......@@ -775,6 +787,7 @@ class Thread implements Runnable {
inheritedAccessControlContext = null;
blocker = null;
uncaughtExceptionHandler = null;
inheritedTenantContainer = null;
}
/**
......
......@@ -35,6 +35,7 @@ import java.util.Set;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import com.alibaba.management.TenantContainerMXBean;
import com.alibaba.management.ElasticHeapMXBean;
import com.sun.management.HotSpotDiagnosticMXBean;
import com.sun.management.UnixOperatingSystemMXBean;
......@@ -276,6 +277,24 @@ enum PlatformComponent {
}
}),
/**
* Tenant Container.
*/
TENANT_CONTAINER(
"com.alibaba.management.TenantContainerMXBean",
"com.alibaba.management", "TenantContainer", defaultKeyProperties(),
true,
new MXBeanFetcher<TenantContainerMXBean>() {
public List<TenantContainerMXBean> getMXBeans() {
TenantContainerMXBean m = ManagementFactoryHelper.getTenantContainerMXBean();
if (null == m) {
return Collections.emptyList();
} else {
return Collections.singletonList(m);
}
}
}),
/**
* Flight Recorder.
*/
......
......@@ -39,7 +39,9 @@ import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import com.alibaba.management.TenantContainerMXBean;
import com.alibaba.management.ElasticHeapMXBean;
import com.alibaba.tenant.TenantContainerMXBeanImpl;
import com.alibaba.jvm.gc.ElasticHeapMXBeanImpl;
import sun.util.logging.LoggingSupport;
......@@ -70,6 +72,7 @@ public class ManagementFactoryHelper {
private static RuntimeImpl runtimeMBean = null;
private static CompilationImpl compileMBean = null;
private static OperatingSystemImpl osMBean = null;
private static TenantContainerMXBeanImpl tenantContainerMBean = null;
private static FlightRecorderMXBeanImpl flightRecorderMBean = null;
private static ElasticHeapMXBeanImpl elasticHeapMXBean = null;
......@@ -115,6 +118,13 @@ public class ManagementFactoryHelper {
return osMBean;
}
public static synchronized TenantContainerMXBean getTenantContainerMXBean() {
if (tenantContainerMBean == null) {
tenantContainerMBean = new TenantContainerMXBeanImpl();
}
return tenantContainerMBean;
}
public static synchronized FlightRecorderMXBean getFlightRecorderMXBean() {
if (flightRecorderMBean == null) {
flightRecorderMBean = new FlightRecorderMXBeanImpl();
......
......@@ -361,6 +361,12 @@ JVM_ElasticHeapGetSoftmxPercent(JNIEnv *env, jclass clazz);
JNIEXPORT jlong JNICALL
JVM_ElasticHeapGetTotalUncommittedBytes(JNIEnv *env, jclass clazz);
/*
* com.alibaba.tenant.TenantContainer
*/
JNIEXPORT void JNICALL
JVM_AttachToTenant(JNIEnv *env, jobject tenant);
/*
* java.lang.reflect.Array
*/
......
/*
* 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.
*/
#ifndef _JAVASOFT_TENANT_ENV_H
#define _JAVASOFT_TENANT_ENV_H
#include "jni.h"
// 0x00200000 represents tenant module and the last 10 represents version 1.0
#define TENANT_ENV_VERSION_1_0 0x00200010
/*
* Tenant Native Method Interface.
*/
struct TenantNativeInterface_;
struct TenantEnv_;
#ifdef __cplusplus
typedef TenantEnv_ TenantEnv;
#else
typedef const struct TenantNativeInterface_* TenantEnv;
#endif
/*
* We use inlined functions for C++ so that programmers can write:
* tenantEnv->GetTenantFlags(cls);
* in C++ rather than:
* (*tenantEnv)->GetTenantFlags(tenantEnv, cls);
* in C.
*/
struct TenantNativeInterface_ {
jint (JNICALL *GetTenantFlags)(TenantEnv *env, jclass cls);
};
struct TenantEnv_ {
const struct TenantNativeInterface_ *functions;
#ifdef __cplusplus
jint GetTenantFlags(jclass cls) {
return functions->GetTenantFlags(this, cls);
}
#endif
};
#endif // _JAVASOFT_TENANT_ENV_H_
#include "jni.h"
#include "jni_util.h"
#include "jvm.h"
#include "jmm.h"
#include "com_alibaba_tenant_NativeDispatcher.h"
#define TENANT "Lcom/alibaba/tenant/TenantContainer;"
#define THREAD "Ljava/lang/Thread;"
#define ARRAY_LENGTH(a) (sizeof(a)/sizeof(a[0]))
static const JmmInterface* jmm_interface = NULL;
static JNINativeMethod methods[] = {
{"attach", "(" TENANT ")V", (void *)&JVM_AttachToTenant},
};
JNIEXPORT void JNICALL
Java_com_alibaba_tenant_NativeDispatcher_registerNatives0(JNIEnv *env, jclass cls)
{
(*env)->RegisterNatives(env, cls, methods, ARRAY_LENGTH(methods));
}
JNIEXPORT void JNICALL
Java_com_alibaba_tenant_NativeDispatcher_getThreadsAllocatedMemory(JNIEnv *env,
jobject obj,
jlongArray ids,
jlongArray sizeArray)
{
if (NULL == jmm_interface) {
jmm_interface = (JmmInterface*) JVM_GetManagement(JMM_VERSION_1_0);
}
jmm_interface->GetThreadAllocatedMemory(env, ids, sizeArray);
}
/*
* 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.
*/
#include "jni.h"
#include "tenantenv.h"
#include "jni_util.h"
#include "jvm.h"
#include "com_alibaba_tenant_TenantGlobals.h"
static TenantEnv*
getTenantEnv(JNIEnv *env)
{
jint rc = JNI_ERR;
JavaVM *jvm = NULL;
TenantEnv *tenantEnv = NULL;
(*env)->GetJavaVM(env, &jvm);
if(NULL != jvm) {
/* Get tenant environment */
rc = (*jvm)->GetEnv(jvm, (void **)&tenantEnv, TENANT_ENV_VERSION_1_0);
if (JNI_OK != rc) {
tenantEnv = NULL;
}
}
return tenantEnv;
}
JNIEXPORT jint JNICALL
Java_com_alibaba_tenant_TenantGlobals_getTenantFlags(JNIEnv *env, jclass cls)
{
jint rc = JNI_ERR;
TenantEnv* tenantEnv = getTenantEnv(env);
if(NULL != tenantEnv) {
rc = (*tenantEnv)->GetTenantFlags(tenantEnv, cls);
} else {
//throw exception
JNU_ThrowByName(env, "java/lang/InternalError", "Can not get tenant environment.");
}
return rc;
}
/*
* 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 jdk.testlibrary;
import java.lang.reflect.Method;
import java.util.Arrays;
import static jdk.testlibrary.Asserts.fail;
public class TestUtils {
/**
* Run all methods of {@code clazz} whose names start with {@code prefix}
* @param prefix
* @param clazz
* @param object
*/
public static void runWithPrefix(String prefix, Class clazz, Object object) {
if (prefix == null || clazz == null || object == null) {
throw new IllegalArgumentException("Bad arguments");
}
long totalTests = Arrays.stream(clazz.getDeclaredMethods())
.filter(m -> m.getName().startsWith(prefix))
.count();
long passed = 0;
long failed = 0;
for (Method method : clazz.getDeclaredMethods()) {
if (method.getName().startsWith(prefix)) {
method.setAccessible(true);
String name = clazz.getName() + "." + method.getName();
println("=== Begin test " + name + " ===");
try {
method.invoke(object);
++passed;
println("=== PASSED (" + passed + " passed, " + failed +" failed, "
+ totalTests + " total) ===");
} catch (Throwable e) {
e.printStackTrace();
++failed;
println("=== FAILED ( " + passed + " passed, " + failed +" failed"
+ totalTests + " total) ===");
}
}
}
if (failed != 0) {
fail("Total " + failed + "/" + totalTests + " testcases failed, class " + clazz.getName());
} else {
println("All " + totalTests + " testcases passed, class " + clazz.getName());
}
}
private static void println(String msg) {
System.err.println(msg);
System.out.println(msg);
}
}
/*
* 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 test.com.alibaba.tenant;
import com.alibaba.tenant.*;
import org.junit.Test;
import static org.junit.Assert.*;
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadMXBean;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.IntStream;
/* @test
* @summary unit tests for com.alibaba.tenant.TenantContainer
* @library /lib/testlibrary
* @compile TestTenantContainer.java
* @run junit/othervm/timeout=300 -XX:+MultiTenant -XX:+UseG1GC -Xmx600m -Xms200m
* -Dcom.alibaba.tenant.test.prop=root test.com.alibaba.tenant.TestTenantContainer
*/
public class TestTenantContainer {
static private final int MAP_SIZE = 128;
static private final int MAP_ARRAY_LENGTH = 40;
private Map<Integer, String> populateMap(int size) {
Map<Integer, String> map = new HashMap<Integer, String>();
for (int i = 0; i < size; i += 1) {
String valStr = "value is [" + i + "]";
map.put(i, valStr);
}
return map;
}
@Test
public void testRunInRootTenant() throws TenantException {
TenantConfiguration tconfig = new TenantConfiguration();
final TenantContainer tenant = TenantContainer.create(tconfig);
tenant.run(() -> {
assertSame(TenantContainer.current(), tenant);
TenantContainer.primitiveRunInRoot(() -> {
//should be in root tenant.
assertNull(TenantContainer.current());
});
});
}
@Test
public void testTenantSystemProperty() {
String value = System.getProperty("com.alibaba.tenant.enableMultiTenant");
assertTrue(value != null && "true".equalsIgnoreCase(value));
}
@Test
public void testCurrent() throws TenantException {
// should be in the root tenant at the begging
assertNull(TenantContainer.current());
TenantConfiguration tconfig = new TenantConfiguration();
final TenantContainer tenant = TenantContainer.create(tconfig);
tenant.run(() -> {
// run in 'current' tenant
assertSame(TenantContainer.current(), tenant);
System.out.println("testCurrent: thread [" + Thread.currentThread().getName() + "] is running in tenant: "
+ TenantContainer.current().getTenantId());
});
// switch back to root tenant.
assertNull(TenantContainer.current());
tenant.destroy();
}
@Test
public void testCurrentWithGC() throws TenantException {
// should be in the root tenant at the begging
assertNull(TenantContainer.current());
TenantConfiguration tconfig = new TenantConfiguration();
final TenantContainer tenant = TenantContainer.create(tconfig);
tenant.run(() -> {
int arrayIndex = 0;
Object[] array = new Object[MAP_ARRAY_LENGTH];
while (arrayIndex < MAP_ARRAY_LENGTH * 20) {
// run in 'current' tenant
assertSame(TenantContainer.current(), tenant);
Map<Integer, String> map = populateMap(MAP_SIZE);
array[arrayIndex % MAP_ARRAY_LENGTH] = map;
arrayIndex++;
Thread.yield();
System.gc();
}
System.out.println("testCurrent: thread [" + Thread.currentThread().getName() + "] is running in tenant: "
+ TenantContainer.current().getTenantId());
});
// switch back to root tenant.
assertNull(TenantContainer.current());
tenant.destroy();
}
@Test
public void testGetTenantID() throws TenantException {
TenantConfiguration tconfig = new TenantConfiguration();
final TenantContainer tenant = TenantContainer.create(tconfig);
tenant.run(() -> {
assertEquals(TenantContainer.current().getTenantId(), tenant.getTenantId());
System.out.println("testGetTenantID: thread [" + Thread.currentThread().getName()
+ "] is running in tenant: " + TenantContainer.current().getTenantId());
});
tenant.destroy();
}
@Test
public void testGetState() throws TenantException {
TenantConfiguration tconfig = new TenantConfiguration();
final TenantContainer tenant = TenantContainer.create(tconfig);
assertTrue(TenantState.STARTING == tenant.getState());
tenant.run(() -> {
assertTrue(TenantState.RUNNING == tenant.getState());
System.out.println("testGetState: thread [" + Thread.currentThread().getName() + "] is running in tenant: "
+ TenantContainer.current().getTenantId());
});
tenant.destroy();
}
@Test
public void testRun() throws TenantException {
TenantConfiguration tconfig = new TenantConfiguration();
final TenantContainer tenant = TenantContainer.create(tconfig);
final TenantContainer tenant2 = TenantContainer.create(tconfig);
try {
tenant.run(() -> {
// it is not allowed to run into tenant 2 from tenant
try {
tenant2.run(() -> {
// should not reach here.
fail();
});
} catch (TenantException e) {
// Expected
}
// it is allowed to run into same tenant
try {
tenant.run(() -> {
assertEquals(TenantContainer.current().getTenantId(), tenant.getTenantId());
System.out.println("testRun: thread [" + Thread.currentThread().getName()
+ "] is running in tenant: " + TenantContainer.current().getTenantId());
});
} catch (TenantException e) {
fail();
}
});
} catch (TenantException e) {
fail();
}
tenant.destroy();
tenant2.destroy();
}
@Test
public void testGetAttachedThreads() throws TenantException {
TenantConfiguration tconfig = new TenantConfiguration();
final TenantContainer tenant = TenantContainer.create(tconfig);
tenant.run(() -> {
Thread[] threads = tenant.getAttachedThreads();
assertEquals(threads.length, 1);
assertEquals(threads[0].getId(), Thread.currentThread().getId());
System.out.println("testGetAttachedThreads: thread [" + Thread.currentThread().getName()
+ "] is running in tenant: " + TenantContainer.current().getTenantId());
});
tenant.destroy();
}
@Test
public void testTenantInheritance() throws TenantException {
TenantConfiguration tconfig = new TenantConfiguration();
final TenantContainer tenant = TenantContainer.create(tconfig);
tenant.run(() -> {
assertSame(TenantContainer.current(), tenant);
Thread thread = new Thread(() -> {
TenantContainer tc = TenantContainer.current();
assertSame(tc, tenant);
Thread[] threads = tc.getAttachedThreads();
assertEquals(threads.length, 2);
assertTrue(Thread.currentThread().getId() == threads[0].getId() ||
Thread.currentThread().getId() == threads[1].getId());
System.out.println("testTenantInheritance: thread [" + Thread.currentThread().getName()
+ "] is running in tenant: " + TenantContainer.current().getTenantId());
});
thread.start();
try {
thread.join();
} catch (Exception e) {
e.printStackTrace();
fail();
}
Thread[] threads = tenant.getAttachedThreads();
assertEquals(threads.length, 1);
assertEquals(threads[0].getId(), Thread.currentThread().getId());
});
tenant.destroy();
}
@Test
public void testTenantGetAllocatedMemory() throws TenantException, InterruptedException {
TenantConfiguration tconfig = new TenantConfiguration();
final TenantContainer tenant = TenantContainer.create(tconfig);
CountDownLatch p1 = new CountDownLatch(1);
CountDownLatch p2 = new CountDownLatch(1);
CountDownLatch p3 = new CountDownLatch(1);
CountDownLatch p4 = new CountDownLatch(1);
CountDownLatch startTest = new CountDownLatch(2);
CountDownLatch endTest = new CountDownLatch(1);
long allocatedMem0 = 0;
long allocatedMem1 = 0;
long allocatedMem2 = 0;
long allocatedMem3 = 0;
assertTrue(0 == tenant.getAllocatedMemory());
tenant.run(() -> {
Thread thread1 = new Thread(() -> {
assertTrue(TenantContainer.current() == tenant);
System.out.println("thread1 started");
try {
byte[] byteArray0 = new byte[10240];
startTest.countDown();
p1.await();
byte[] byteArray1 = new byte[10240];
endTest.await();
} catch (InterruptedException ee) {
ee.printStackTrace();
fail();
} finally {
System.out.println("thread1 ended");
}
});
thread1.start();
});
Thread thread2 = new Thread(() -> {
System.out.println("thread2 started");
try {
startTest.countDown();
p2.await();
tenant.run(()->{
byte[] byteArray0 = new byte[10240];
});
p3.await();
byte[] byteArray1 = new byte[10240];
tenant.run(()->{
byte[] byteArray0 = new byte[10240];
});
byte[] byteArray2 = new byte[10240];
endTest.await();
} catch (InterruptedException | TenantException ee) {
ee.printStackTrace();
fail();
} finally {
System.out.println("thread2 ended");
}
});
thread2.start();
startTest.await();
allocatedMem0 = tenant.getAllocatedMemory();
System.out.println("allocatedMem0 = " + allocatedMem0);
assertTrue(allocatedMem0 >= 10240);
p1.countDown();
Thread.sleep(1000);
allocatedMem1 = tenant.getAllocatedMemory();
System.out.println("allocatedMem1 = " + allocatedMem1);
assertTrue((allocatedMem1 - allocatedMem0) >= 10240 && (allocatedMem1 - allocatedMem0) < 11240);
p2.countDown();
Thread.sleep(1000);
allocatedMem2 = tenant.getAllocatedMemory();
System.out.println("allocatedMem2 = " + allocatedMem2);
assertTrue((allocatedMem2 - allocatedMem1) >= 10240 && (allocatedMem2 - allocatedMem1) < 11240);
p3.countDown();
Thread.sleep(1000);
allocatedMem3 = tenant.getAllocatedMemory();
System.out.println("allocatedMem3 = " + allocatedMem3);
assertTrue((allocatedMem3 - allocatedMem2) >= 10240 && (allocatedMem3 - allocatedMem2) < 11240);
p4.countDown();
Thread.sleep(1000);
tenant.destroy();
endTest.countDown();
System.out.println("testTenantGetAllocatedMemory passed!");
}
public static void main(String[] args) throws Exception {
TestTenantContainer test = new TestTenantContainer();
test.testCurrent();
test.testGetTenantID();
test.testGetState();
test.testGetAttachedThreads();
test.testRun();
test.testCurrentWithGC();
test.testTenantInheritance();
test.testRunInRootTenant();
test.testTenantGetAllocatedMemory();
}
}
/*
* 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.tenant;
import com.alibaba.rcm.Constraint;
import com.alibaba.rcm.ResourceContainer;
import com.alibaba.rcm.ResourceContainerFactory;
import com.alibaba.rcm.internal.AbstractResourceContainer;
import java.util.Collections;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static com.alibaba.rcm.ResourceType.*;
import static jdk.testlibrary.Asserts.*;
/*
* @test
* @library /lib/testlibrary
* @summary test RCM API based TenantContainerFactory
* @run main/othervm/bootclasspath -XX:+MultiTenant com.alibaba.tenant.TestTenantContainerFactory
*/
public class TestTenantContainerFactory {
private void testSingleton() {
ResourceContainerFactory factory = TenantContainerFactory.instance();
assertTrue(factory == TenantContainerFactory.instance());
ResourceContainerFactory factory2 = TenantContainerFactory.instance();
assertSame(factory, factory2);
}
private void testCreation() {
try {
ResourceContainer container = TenantContainerFactory.instance()
.createContainer(Collections.emptySet());
assertNotNull(container);
assertTrue(container instanceof TenantResourceContainer);
assertNull(TenantContainer.current());
assertNotNull(TenantResourceContainer.root());
} catch (Throwable t) {
fail();
}
}
private void testCreationWithHeapLimit() {
try {
Iterable<Constraint> constraints = Stream.of(HEAP_RETAINED.newConstraint(32 * 1024 * 1024))
.collect(Collectors.toSet());
ResourceContainer container = TenantContainerFactory.instance()
.createContainer(constraints);
assertNotNull(container);
assertTrue(container instanceof TenantResourceContainer);
assertNull(TenantContainer.current());
assertNotNull(TenantResourceContainer.root());
} catch (Throwable t) {
fail();
}
}
private void testDestroy() {
ResourceContainer container = TenantContainerFactory.instance()
.createContainer(null);
try {
container.destroy();
fail("Should throw UnsupportedException");
} catch (UnsupportedOperationException e) {
// expected
}
try {
TenantContainer tenant = TenantContainerFactory.tenantContainerOf(container);
tenant.destroy();
} catch (Throwable t) {
fail();
}
}
private void testImplicitTenantContainer() {
try {
Iterable<Constraint> constraints = Stream.of(CPU_PERCENT.newConstraint(10))
.collect(Collectors.toList());
ResourceContainer rc = TenantContainerFactory.instance().createContainer(constraints);
TenantContainer tenant = TenantContainerFactory.tenantContainerOf(rc);
assertNotNull(tenant);
assertNull(TenantContainer.current());
assertSame(AbstractResourceContainer.root(), AbstractResourceContainer.current());
tenant.run(()->{
System.out.println("Hello");
assertSame(TenantContainer.current(), tenant);
assertSame(AbstractResourceContainer.current(), rc);
});
tenant.destroy();
} catch (Throwable e) {
fail();
}
}
public static void main(String[] args) {
TestTenantContainerFactory test = new TestTenantContainerFactory();
test.testSingleton();
test.testCreation();
test.testDestroy();
test.testImplicitTenantContainer();
if (TenantGlobals.isHeapThrottlingEnabled()) {
test.testCreationWithHeapLimit();
}
}
}
/*
* 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 test.com.alibaba.tenant;
import org.junit.Test;
import com.alibaba.tenant.TenantConfiguration;
import com.alibaba.tenant.TenantContainer;
import com.alibaba.tenant.TenantGlobals;
import static org.junit.Assert.*;
/* @test
* @summary unit tests for com.alibaba.tenant.TenantGlobals
* @library /lib/testlibrary
* @compile TestTenantGlobals.java
* @run junit/othervm test.com.alibaba.tenant.TestTenantGlobals
*/
public class TestTenantGlobals {
@Test
public void testIfTenantIsDisabled() {
assertFalse(TenantGlobals.isTenantEnabled());
String value = System.getProperty("com.alibaba.tenant.enableMultiTenant");
boolean bTenantIsEnabled = false;
if(value != null && "true".equalsIgnoreCase(value)) {
bTenantIsEnabled = true;
}
assertFalse(bTenantIsEnabled);
TenantConfiguration tconfig = new TenantConfiguration();
try {
TenantContainer.create(tconfig);
fail(); // should not reach here.
} catch (UnsupportedOperationException exception) {
// Expected
System.out.println("Can not create tenant without -XX:+MultiTenant enabled.");
exception.printStackTrace();
}
try {
TenantContainer.create(tconfig);
fail(); // should not reach here.
} catch (UnsupportedOperationException exception) {
// Expected
System.out.println("Can not create tenant without -XX:+MultiTenant enabled.");
exception.printStackTrace();
}
try {
TenantContainer.current();
fail(); // should not reach here.
} catch (UnsupportedOperationException exception) {
// Expected
System.out.println("Can not get current tenant without -XX:+MultiTenant enabled.");
exception.printStackTrace();
}
}
public static void main(String[] args) {
TestTenantGlobals test = new TestTenantGlobals();
test.testIfTenantIsDisabled();
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册