Skip to content

Commit

Permalink
GEODE-2859: Fix race in ShowDeadlockDUnitTest
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredjstewart committed May 11, 2017
1 parent e98606d commit ab7e51f
Showing 1 changed file with 32 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,11 @@
*/
package org.apache.geode.management.internal.cli.commands;

import static org.apache.geode.distributed.ConfigurationProperties.*;

import org.apache.geode.cache.Cache;
import org.apache.commons.io.FileUtils;
import org.apache.geode.cache.execute.Function;
import org.apache.geode.cache.execute.FunctionContext;
import org.apache.geode.cache.execute.FunctionService;
import org.apache.geode.cache.execute.ResultCollector;
import org.apache.geode.distributed.DistributedLockService;
import org.apache.geode.distributed.internal.deadlock.GemFireDeadlockDetector;
import org.apache.geode.distributed.internal.membership.InternalDistributedMember;
import org.apache.geode.management.cli.Result;
Expand All @@ -36,6 +33,7 @@
import org.apache.geode.test.dunit.VM;
import org.apache.geode.test.dunit.cache.internal.JUnit4CacheTestCase;
import org.apache.geode.test.junit.categories.DistributedTest;
import org.awaitility.Awaitility;
import org.junit.Test;
import org.junit.experimental.categories.Category;

Expand All @@ -46,7 +44,6 @@
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import static org.apache.geode.distributed.ConfigurationProperties.MCAST_PORT;
import static org.apache.geode.test.dunit.Assert.*;
import static org.apache.geode.test.dunit.Invoke.invokeInEveryVM;
import static org.apache.geode.test.dunit.LogWriterUtils.getLogWriter;
Expand Down Expand Up @@ -98,15 +95,17 @@ public void testNoDeadlock() throws ClassNotFoundException, IOException {
createCache(vm1);
createCache(new Properties());

String fileName = "dependency.txt";
String filename = "dependency.txt";
GemFireDeadlockDetector detect = new GemFireDeadlockDetector();
assertEquals(null, detect.find().findCycle());

CommandProcessor commandProcessor = new CommandProcessor();
CommandStringBuilder csb = new CommandStringBuilder(CliStrings.SHOW_DEADLOCK);
csb.addOption(CliStrings.SHOW_DEADLOCK__DEPENDENCIES__FILE, fileName);
Result result = commandProcessor.createCommandStatement(csb.toString(), EMPTY_ENV).process();

String showDeadlockCommand = new CommandStringBuilder(CliStrings.SHOW_DEADLOCK)
.addOption(CliStrings.SHOW_DEADLOCK__DEPENDENCIES__FILE, filename).toString();

CommandProcessor commandProcessor = new CommandProcessor();
Result result =
commandProcessor.createCommandStatement(showDeadlockCommand, EMPTY_ENV).process();
String deadLockOutputFromCommand = getResultAsString(result);

getLogWriter().info("output = " + deadLockOutputFromCommand);
Expand All @@ -115,7 +114,7 @@ public void testNoDeadlock() throws ClassNotFoundException, IOException {
assertEquals(true,
deadLockOutputFromCommand.startsWith(CliStrings.SHOW_DEADLOCK__NO__DEADLOCK));
result.saveIncomingFiles(null);
File file = new File(fileName);
File file = new File(filename);
assertTrue(file.exists());
file.delete();

Expand All @@ -140,40 +139,34 @@ public void testDistributedDeadlockWithFunction()
// This thread locks the lock member2 first, then member1.
lockTheLocks(vm1, member1);

Thread.sleep(5000);
CommandProcessor commandProcessor = new CommandProcessor();
CommandStringBuilder csb = new CommandStringBuilder(CliStrings.SHOW_DEADLOCK);
csb.addOption(CliStrings.SHOW_DEADLOCK__DEPENDENCIES__FILE, filename);
Result result = commandProcessor.createCommandStatement(csb.toString(), EMPTY_ENV).process();
String showDeadlockCommand = new CommandStringBuilder(CliStrings.SHOW_DEADLOCK)
.addOption(CliStrings.SHOW_DEADLOCK__DEPENDENCIES__FILE, filename).toString();

String deadLockOutputFromCommand = getResultAsString(result);
getLogWriter().info("Deadlock = " + deadLockOutputFromCommand);
result.saveIncomingFiles(null);
assertEquals(true,
deadLockOutputFromCommand.startsWith(CliStrings.SHOW_DEADLOCK__DEADLOCK__DETECTED));
assertEquals(true, result.getStatus().equals(Status.OK));
File file = new File(filename);
assertTrue(file.exists());
file.delete();
CommandProcessor commandProcessor = new CommandProcessor();

Awaitility.await().atMost(1, TimeUnit.MINUTES).until(() -> {
Result result =
commandProcessor.createCommandStatement(showDeadlockCommand, EMPTY_ENV).process();
String deadLockOutputFromCommand = getResultAsString(result);
File fileResult = new File(filename);
FileUtils.deleteQuietly(fileResult);
try {
result.saveIncomingFiles(null);
} catch (IOException e) {
throw new RuntimeException(e);
}
assertEquals(true,
deadLockOutputFromCommand.startsWith(CliStrings.SHOW_DEADLOCK__DEADLOCK__DETECTED));
assertEquals(true, result.getStatus().equals(Status.OK));
assertTrue(fileResult.exists());
fileResult.delete();
});
}


private void createCache(Properties props) {
getSystem(props);
final Cache cache = getCache();
}

private Properties createProperties(Host host, int locatorPort) {
Properties props = new Properties();
props.setProperty(MCAST_PORT, "0");
// props.setProperty(DistributionConfig.LOCATORS_NAME, getServerHostName(host) + "[" +
// locatorPort + "]");
props.setProperty(LOG_LEVEL, "info");
props.setProperty(STATISTIC_SAMPLING_ENABLED, "true");
props.setProperty(ENABLE_TIME_STATISTICS, "true");
props.put(ENABLE_NETWORK_PARTITION_DETECTION, "true");
return props;
getCache();
}

private void lockTheLocks(VM vm0, final InternalDistributedMember member) {
Expand All @@ -183,11 +176,7 @@ private void lockTheLocks(VM vm0, final InternalDistributedMember member) {

public void run() {
lock.lock();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
fail("interrupted", e);
}

ResultCollector collector =
FunctionService.onMember(basicGetSystem(), member).execute(new TestFunction());
// wait the function to lock the lock on member.
Expand All @@ -197,26 +186,6 @@ public void run() {
});
}

private void lockTheDLocks(VM vm, final String first, final String second) {
vm.invokeAsync(new SerializableRunnable() {

private static final long serialVersionUID = 1L;

public void run() {
getCache();
DistributedLockService dls = DistributedLockService.create("deadlock_test", getSystem());
dls.lock(first, 10 * 1000, -1);

try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
dls.lock(second, 10 * 1000, -1);
}
});
}

private InternalDistributedMember createCache(VM vm) {
return (InternalDistributedMember) vm.invoke(new SerializableCallable() {
/**
Expand Down

0 comments on commit ab7e51f

Please sign in to comment.