Skip to content

Commit

Permalink
[Misc] fix multi-tenant testcase TestHierachicalTenants.java and Test…
Browse files Browse the repository at this point in the history
…JGroup.java

Summary: Since the TestJGroup.java test run requires a separate and clean environment, it is configured as a manual use case; Merge patch from ajdk/jdk8u/codereview/8500250 to fix testcade TestHierachicalTenants.java bug

Test Plan: CI pipeline

Reviewed-by: jeffery.wsj, lvfei.lv

Issue: #361
  • Loading branch information
sendaoYan committed Jul 29, 2022
1 parent de9f248 commit 0fc7441
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@

import jdk.testlibrary.TestUtils;
import java.util.concurrent.CountDownLatch;
import java.security.NoSuchAlgorithmException;
import java.security.MessageDigest;

import static java.security.MessageDigest.getInstance;

import static jdk.testlibrary.Asserts.*;

/*
Expand All @@ -36,21 +41,40 @@
*/

public class TestHierachicalTenants {
private static final int LOAD_NUM = 50;

private void workLoad(int load) throws NoSuchAlgorithmException {
MessageDigest md5 = getInstance("MD5");
int count = load;
while (--count > 0) {
md5.update("hello world!!!".getBytes());
if (count % 20 == 0) {
Thread.yield();
}
}
}

private void testParentLimit() {
private void testParentLimit() throws NoSuchAlgorithmException {
TenantConfiguration config = new TenantConfiguration()
.limitCpuSet("0") // forcefully bind to cpu-0
.limitCpuCfs(200000, 100000);
TenantContainer parent = TenantContainer.create(config);

long timeLimitMillis = 5_000;
workLoad(1_000_000); // warm up

long timeLimitMillis = 20_000;
long[] counts = new long[5];
try {
// counts[counts.length - 1] is the baseline number
parent.run(()->{
parent.run(() -> {
// the primary counter loop-1
long startTime = System.currentTimeMillis();
while (System.currentTimeMillis() - startTime < timeLimitMillis) {
try {
workLoad(LOAD_NUM);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
++counts[counts.length - 1];
}
});
Expand All @@ -62,11 +86,11 @@ private void testParentLimit() {

for (int i = 0; i < childrenTenants.length; ++i) {
final int idx = i;
parent.run(()->{
parent.run(() -> {
childrenTenants[idx] = TenantContainer.create(config);
});
childrenTenants[i].run(()->{
threads[idx] = new Thread(()->{
childrenTenants[i].run(() -> {
threads[idx] = new Thread(() -> {
try {
start.await();
} catch (InterruptedException e) {
Expand All @@ -76,6 +100,11 @@ private void testParentLimit() {
// below counter loop must be identical to parent's primary counter loop-1
long startTime = System.currentTimeMillis();
while (System.currentTimeMillis() - startTime < timeLimitMillis) {
try {
workLoad(LOAD_NUM);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
++counts[idx];
}
});
Expand All @@ -97,17 +126,19 @@ private void testParentLimit() {
}

System.out.println("counts:");
for (long cnt : counts) { System.out.println(cnt); }
for (long cnt : counts) {
System.out.println(cnt);
}

// check result
long sum = 0;
double acceptableDiffPercent = 0.4;
double acceptableDiffPercent = 0.2;
for (int i = 0; i < counts.length - 1; ++i) {
assertLessThan(counts[i], counts[counts.length - 1]);
sum += counts[i];
}

assertLessThan(Math.abs(sum - (double)counts[counts.length - 1]), counts[counts.length - 1] * acceptableDiffPercent);
assertLessThan(Math.abs(sum - (double) counts[counts.length - 1]), counts[counts.length - 1] * acceptableDiffPercent);

} catch (TenantException e) {
e.printStackTrace();
Expand All @@ -121,7 +152,7 @@ private void testParentBasic() {
assertNull(getParent(parent));
TenantContainer children[] = new TenantContainer[1];
try {
parent.run(()->{
parent.run(() -> {
children[0] = TenantContainer.create(config);
});
} catch (TenantException e) {
Expand Down
151 changes: 75 additions & 76 deletions jdk/test/multi-tenant/test/com/alibaba/tenant/TestJGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
* @requires os.arch == "amd64"
* @summary Test JGroup
* @library /lib/testlibrary
* @run main/othervm/bootclasspath -XX:+MultiTenant -XX:+TenantCpuAccounting -XX:+TenantCpuThrottling -XX:+UseG1GC -Xmx200m -Xms200m -Dcom.alibaba.tenant.DebugJGroup=true com.alibaba.tenant.TestJGroup
* @run main/othervm/bootclasspath/manual -XX:+MultiTenant -XX:+TenantCpuAccounting -XX:+TenantCpuThrottling -XX:+UseG1GC -Xmx200m -Xms200m -Dcom.alibaba.tenant.DebugJGroup=true com.alibaba.tenant.TestJGroup
*/

class JGroupWorker implements Runnable {
Expand Down Expand Up @@ -118,86 +118,85 @@ static void mySleep(int t) {
public static void main(String[] args) throws Exception {
setUp();

int iteration = 10;
int i = 0;
double usage1 = 0;
double usage2 = 0;
double usage3 = 0;
double diff = 0.8;
long usage1 = 0;
long usage2 = 0;
long usage3 = 0;
JGroupWorker jw1 = new JGroupWorker(0);
JGroupWorker jw2 = new JGroupWorker(1);
JGroupWorker jw3 = new JGroupWorker(2);
for (i = 0; i < iteration; i++) {
Thread t1 = new Thread(jw1);
Thread t2 = new Thread(jw2);
Thread t3 = new Thread(jw3);
t1.start();
t2.start();
t3.start();

mySleep(1000);

JGroupsWorker js = new JGroupsWorker(jw1.group,
jw2.group,
jw3.group);
Thread t4 = new Thread(js);
t4.start();
try {
t4.join();
} catch (InterruptedException e) {
System.out.println("Interreupted...");
}

try {
t1.join();
} catch (InterruptedException e) {
System.out.println("Interreupted...");
}
usage1 = jw1.group.getCpuTime();
jw1.group.destory();

if (usage1 <= 0) {
throw new Exception("Invalid cpu usage, usage1: " + usage1);
}
try {
t2.join();
} catch (InterruptedException e) {
System.out.println("Interreupted...");
}
usage2 = jw2.group.getCpuTime();
jw2.group.destory();

if (usage2 <= 0) {
throw new Exception("Invalid cpu usage, usage2: " + usage2);
}
try {
t3.join();
} catch (InterruptedException e) {
System.out.println("Interreupted...");
}
usage3 = jw3.group.getCpuTime();
jw3.group.destory();

if (usage3 <= 0) {
throw new Exception("Invalid cpu usage, usage3: " + usage3);
}

if ((js.count3 < js.count2 * diff) || (js.count2 < js.count1 * diff)) {
continue;
}

if ((jw3.count < jw2.count * diff) || (jw2.count < jw1.count * diff)) {
continue;
}

if ((usage3 < usage2 * diff) || (usage2 < usage1 * diff)) {
continue;
}
break;
Thread t1 = new Thread(jw1);
Thread t2 = new Thread(jw2);
Thread t3 = new Thread(jw3);
t1.start();
t2.start();
t3.start();

mySleep(1000);

JGroupsWorker js = new JGroupsWorker(jw1.group,
jw2.group,
jw3.group);
Thread t4 =new Thread(js);
t4.start();
try {
t4.join();
} catch (InterruptedException e) {
System.out.println("Interreupted...");
}

try {
t1.join();
} catch (InterruptedException e) {
System.out.println("Interreupted...");
}
usage1 = jw1.group.getCpuTime();
jw1.group.destory();

if(usage1 <= 0 ) {
throw new Exception("Invalid cpu usage, usage1: "+ usage1);
}
try {
t2.join();
} catch (InterruptedException e) {
System.out.println("Interreupted...");
}
usage2 = jw2.group.getCpuTime();
jw2.group.destory();

if(usage2 <= 0 ) {
throw new Exception("Invalid cpu usage, usage2: "+ usage2);
}
try {
t3.join();
} catch (InterruptedException e) {
System.out.println("Interreupted...");
}
usage3 = jw3.group.getCpuTime();
jw3.group.destory();

if(usage3 <= 0 ) {
throw new Exception("Invalid cpu usage, usage3: "+ usage3);
}

if ((js.count3 < js.count2) || (js.count2 < js.count1)) {
throw new Exception("com.alibaba.tenant.JGroupsWorker test failed!"
+ " count1 = " + js.count1
+ " count2 = " + js.count2
+ " count3 = " + js.count3);
}

if ((jw3.count < jw2.count) || (jw2.count < jw1.count)) {
throw new Exception("Three com.alibaba.tenant.JGroupWorker test failed!"
+ " jw1 count = " + jw1.count
+ " jw2 count = " + jw2.count
+ " jw3 count = " + jw3.count);
}

if (i == iteration) {
throw new Exception("com.alibaba.tenant.JGroupsWorker test failed!");
if ((usage3 < usage2) || (usage2 < usage1)) {
throw new Exception("Three com.alibaba.tenant.JGroupWorker test failed!"
+ " jw1 usage = " + usage1
+ " jw2 usage = " + usage2
+ " jw3 usage = " + usage3);
}
}
}

0 comments on commit 0fc7441

Please sign in to comment.