use of org.apache.geode.test.dunit.SerializableRunnable in project geode by apache.
the class LocatorDUnitTest method testLeadFailureAndCoordShutdown.
/**
* test lead member failure and normal coordinator shutdown with network partition detection
* enabled.
* <p>
* Start two locators with admin distributed systems, then start two regular distributed members.
* <p>
* We kill the lead member and demonstrate that the other members continue to operate normally.
* <p>
* We then shut down the group coordinator and observe the second locator pick up the job and the
* remaining member continues to operate normally.
*/
@Test
public void testLeadFailureAndCoordShutdown() throws Exception {
disconnectAllFromDS();
Host host = Host.getHost(0);
VM vm1 = host.getVM(1);
VM vm2 = host.getVM(2);
VM locvm = host.getVM(3);
Locator locator = null;
final int[] ports = AvailablePortHelper.getRandomAvailableTCPPorts(2);
final int port1 = ports[0];
this.port1 = port1;
final int port2 = ports[1];
this.port2 = port2;
DistributedTestUtils.deleteLocatorStateFile(port1, port2);
final String host0 = NetworkUtils.getServerHostName(host);
final String locators = host0 + "[" + port1 + "]," + host0 + "[" + port2 + "]";
final Properties properties = new Properties();
properties.put(MCAST_PORT, "0");
properties.put(LOCATORS, locators);
properties.put(ENABLE_NETWORK_PARTITION_DETECTION, "true");
properties.put(DISABLE_AUTO_RECONNECT, "true");
properties.put(MEMBER_TIMEOUT, "2000");
properties.put(ENABLE_CLUSTER_CONFIGURATION, "false");
addDSProps(properties);
try {
final String uname = getUniqueName();
File logFile = new File("");
locator = Locator.startLocatorAndDS(port1, logFile, properties);
DistributedSystem sys = locator.getDistributedSystem();
locvm.invoke(new SerializableRunnable() {
public void run() {
File lf = new File("");
try {
Locator.startLocatorAndDS(port2, lf, properties);
MembershipManagerHelper.inhibitForcedDisconnectLogging(true);
} catch (IOException ios) {
org.apache.geode.test.dunit.Assert.fail("Unable to start locator2", ios);
}
}
});
Object[] connectArgs = new Object[] { properties };
SerializableRunnable crashSystem = new SerializableRunnable("Crash system") {
public void run() {
DistributedSystem msys = InternalDistributedSystem.getAnyInstance();
msys.getLogWriter().info("<ExpectedException action=add>service failure</ExpectedException>");
msys.getLogWriter().info("<ExpectedException action=add>org.apache.geode.ConnectException</ExpectedException>");
msys.getLogWriter().info("<ExpectedException action=add>org.apache.geode.ForcedDisconnectException</ExpectedException>");
MembershipManagerHelper.crashDistributedSystem(msys);
}
};
assertTrue(MembershipManagerHelper.getLeadMember(sys) == null);
DistributedMember mem1 = (DistributedMember) vm1.invoke(this.getClass(), "getDistributedMember", connectArgs);
DistributedMember mem2 = (DistributedMember) vm2.invoke(this.getClass(), "getDistributedMember", connectArgs);
assertEquals(mem1, MembershipManagerHelper.getLeadMember(sys));
assertEquals(sys.getDistributedMember(), MembershipManagerHelper.getCoordinator(sys));
MembershipManagerHelper.inhibitForcedDisconnectLogging(true);
// crash the lead vm. Should be okay
vm1.invoke(crashSystem);
Awaitility.waitAtMost(4 * 2000, TimeUnit.MILLISECONDS).pollInterval(200, TimeUnit.MILLISECONDS).until(() -> isSystemConnected());
assertTrue("Distributed system should not have disconnected", isSystemConnected());
assertTrue("Distributed system should not have disconnected", vm2.invoke(() -> LocatorDUnitTest.isSystemConnected()));
assertTrue("Distributed system should not have disconnected", locvm.invoke(() -> LocatorDUnitTest.isSystemConnected()));
// stop the locator normally. This should also be okay
locator.stop();
if (!Locator.getLocators().isEmpty()) {
// log this for debugging purposes before throwing assertion error
org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().warning("found locator " + Locator.getLocators().iterator().next());
}
assertTrue("locator is not stopped", Locator.getLocators().isEmpty());
assertTrue("Distributed system should not have disconnected", vm2.invoke(() -> LocatorDUnitTest.isSystemConnected()));
assertTrue("Distributed system should not have disconnected", locvm.invoke(() -> LocatorDUnitTest.isSystemConnected()));
// the remaining non-locator member should now be the lead member
assertEquals("This test sometimes fails. If the log contains " + "'failed to collect all ACKs' it is a false failure.", mem2, vm2.invoke(() -> LocatorDUnitTest.getLeadMember()));
SerializableRunnable disconnect = new SerializableRunnable("Disconnect from " + locators) {
public void run() {
DistributedSystem sys = InternalDistributedSystem.getAnyInstance();
if (sys != null && sys.isConnected()) {
sys.disconnect();
}
}
};
// disconnect the first vm and demonstrate that the third vm and the
// locator notice the failure and exit
vm2.invoke(() -> disconnectDistributedSystem());
locvm.invoke(() -> stopLocator());
} finally {
MembershipManagerHelper.inhibitForcedDisconnectLogging(false);
if (locator != null) {
locator.stop();
}
try {
locvm.invoke(() -> stopLocator());
} catch (Exception e) {
org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().severe("failed to stop locator in vm 3", e);
}
}
}
use of org.apache.geode.test.dunit.SerializableRunnable in project geode by apache.
the class DistTXDebugDUnitTest method postTearDownCacheTestCase.
@Override
public final void postTearDownCacheTestCase() throws Exception {
Invoke.invokeInEveryVM(new SerializableRunnable() {
@Override
public void run() {
InternalResourceManager.setResourceObserver(null);
}
});
InternalResourceManager.setResourceObserver(null);
}
use of org.apache.geode.test.dunit.SerializableRunnable in project geode by apache.
the class ClientServerInvalidAndDestroyedEntryDUnitTest method doTestClientGetsInvalidEntry.
/**
* Bug #43407 - when a client does a get(k) and the entry is invalid in the server we want the
* client to end up with an entry that is invalid.
*/
private void doTestClientGetsInvalidEntry(final String regionName, final boolean usePR, boolean useTX) throws Exception {
VM vm1 = Host.getHost(0).getVM(1);
VM vm2 = Host.getHost(0).getVM(2);
// here are the keys that will be used to validate behavior. Keys must be
// colocated if using both a partitioned region in the server and transactions
// in the client. All of these keys hash to bucket 0 in a two-bucket PR
// except Object11 and IDoNotExist1
final String notAffectedKey = "Object1";
final String nonexistantKey = (usePR && useTX) ? "IDoNotExist2" : "IDoNotExist1";
final String key1 = "Object10";
final String key2 = (usePR && useTX) ? "Object12" : "Object11";
SerializableCallableIF createServer = getCreateServerCallable(regionName, usePR);
int serverPort = (Integer) vm1.invoke(createServer);
vm2.invoke(createServer);
vm1.invoke(new SerializableRunnable("populate server and create invalid entry") {
public void run() {
Region myRegion = getCache().getRegion(regionName);
for (int i = 1; i <= 20; i++) {
myRegion.put("Object" + i, "Value" + i);
}
myRegion.invalidate(key1);
myRegion.invalidate(key2);
}
});
org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("creating client cache");
ClientCache c = new ClientCacheFactory().addPoolServer("localhost", serverPort).set(LOG_LEVEL, LogWriterUtils.getDUnitLogLevel()).create();
Region myRegion = c.createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY).create(regionName);
;
if (useTX) {
c.getCacheTransactionManager().begin();
}
// get of a valid entry should work
assertNotNull(myRegion.get(notAffectedKey));
// get of an invalid entry should return null and create the entry in an invalid state
org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("getting " + key1 + " - should reach this cache and be INVALID");
assertNull(myRegion.get(key1));
assertTrue(myRegion.containsKey(key1));
// since this might be a PR we also check the next key to force PR Get messaging
assertNull(myRegion.get(key2));
assertTrue(myRegion.containsKey(key2));
// now try a key that doesn't exist anywhere
assertNull(myRegion.get(nonexistantKey));
assertFalse(myRegion.containsKey(nonexistantKey));
if (useTX) {
c.getCacheTransactionManager().commit();
// test that the commit correctly created the entries in the region
assertNotNull(myRegion.get(notAffectedKey));
assertNull(myRegion.get(key1));
assertTrue(myRegion.containsKey(key1));
assertNull(myRegion.get(key2));
assertTrue(myRegion.containsKey(key2));
}
myRegion.localDestroy(notAffectedKey);
myRegion.localDestroy(key1);
myRegion.localDestroy(key2);
if (useTX) {
c.getCacheTransactionManager().begin();
}
// check that getAll returns invalidated entries
List keys = new LinkedList();
keys.add(notAffectedKey);
keys.add(key1);
keys.add(key2);
Map result = myRegion.getAll(keys);
assertNotNull(result.get(notAffectedKey));
assertNull(result.get(key1));
assertNull(result.get(key2));
assertTrue(result.containsKey(key1));
assertTrue(result.containsKey(key2));
assertTrue(myRegion.containsKey(key1));
assertTrue(myRegion.containsKey(key2));
if (useTX) {
c.getCacheTransactionManager().commit();
// test that the commit correctly created the entries in the region
assertNotNull(myRegion.get(notAffectedKey));
assertNull(myRegion.get(key1));
assertTrue(myRegion.containsKey(key1));
assertNull(myRegion.get(key2));
assertTrue(myRegion.containsKey(key2));
}
// test that a listener is not invoked when there is already an invalidated
// entry in the client cache
UpdateListener listener = new UpdateListener();
listener.log = org.apache.geode.test.dunit.LogWriterUtils.getLogWriter();
myRegion.getAttributesMutator().addCacheListener(listener);
myRegion.get(key1);
assertEquals("expected no cache listener invocations", 0, listener.updateCount, listener.updateCount);
myRegion.localDestroy(notAffectedKey);
myRegion.getAll(keys);
assertTrue("expected to find " + notAffectedKey, myRegion.containsKey(notAffectedKey));
assertEquals("expected only one listener invocation for " + notAffectedKey, 1, listener.updateCount);
}
use of org.apache.geode.test.dunit.SerializableRunnable in project geode by apache.
the class ClientServerInvalidAndDestroyedEntryDUnitTest method doTestClientGetsTombstone.
/**
* Similar to bug #43407 but not reported in a ticket, we want a client that does a get() on a
* destroyed entry to end up with a tombstone for that entry. This was already the case but there
* were no unit tests covering this for different server configurations and with/without
* transactions.
*/
private void doTestClientGetsTombstone(final String regionName, final boolean usePR, boolean useTX) throws Exception {
VM vm1 = Host.getHost(0).getVM(1);
VM vm2 = Host.getHost(0).getVM(2);
// here are the keys that will be used to validate behavior. Keys must be
// colocated if using both a partitioned region in the server and transactions
// in the client. All of these keys hash to bucket 0 in a two-bucket PR
// except Object11 and IDoNotExist1
final String notAffectedKey = "Object1";
final String nonexistantKey = (usePR && useTX) ? "IDoNotExist2" : "IDoNotExist1";
final String key1 = "Object10";
final String key2 = (usePR && useTX) ? "Object12" : "Object11";
SerializableCallableIF createServer = getCreateServerCallable(regionName, usePR);
int serverPort = (Integer) vm1.invoke(createServer);
vm2.invoke(createServer);
vm1.invoke(new SerializableRunnable("populate server and create invalid entry") {
public void run() {
Region myRegion = getCache().getRegion(regionName);
for (int i = 1; i <= 20; i++) {
myRegion.put("Object" + i, "Value" + i);
}
myRegion.destroy(key1);
myRegion.destroy(key2);
}
});
org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("creating client cache");
ClientCache c = new ClientCacheFactory().addPoolServer("localhost", serverPort).set(LOG_LEVEL, LogWriterUtils.getDUnitLogLevel()).create();
Region myRegion = c.createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY).create(regionName);
;
if (useTX) {
c.getCacheTransactionManager().begin();
}
// get of a valid entry should work
assertNotNull(myRegion.get(notAffectedKey));
// get of an invalid entry should return null and create the entry in an invalid state
org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("getting " + key1 + " - should reach this cache and be a TOMBSTONE");
assertNull(myRegion.get(key1));
assertFalse(myRegion.containsKey(key1));
RegionEntry entry;
if (!useTX) {
entry = ((LocalRegion) myRegion).getRegionEntry(key1);
// it should be there
assertNotNull(entry);
// it should be a destroyed entry with Token.TOMBSTONE
assertTrue(entry.isTombstone());
}
// since this might be a PR we also check the next key to force PR Get messaging
assertNull(myRegion.get(key2));
assertFalse(myRegion.containsKey(key2));
if (!useTX) {
entry = ((LocalRegion) myRegion).getRegionEntry(key2);
// it should be there
assertNotNull(entry);
// it should be a destroyed entry with Token.TOMBSTONE
assertTrue(entry.isTombstone());
}
// now try a key that doesn't exist anywhere
assertNull(myRegion.get(nonexistantKey));
assertFalse(myRegion.containsKey(nonexistantKey));
if (useTX) {
c.getCacheTransactionManager().commit();
// test that the commit correctly created the entries in the region
assertNotNull(myRegion.get(notAffectedKey));
assertNull(myRegion.get(key1));
assertFalse(myRegion.containsKey(key1));
entry = ((LocalRegion) myRegion).getRegionEntry(key1);
// it should be there
assertNotNull(entry);
// it should be a destroyed entry with Token.TOMBSTONE
assertTrue(entry.isTombstone());
assertNull(myRegion.get(key2));
assertFalse(myRegion.containsKey(key2));
entry = ((LocalRegion) myRegion).getRegionEntry(key2);
// it should be there
assertNotNull(entry);
// it should be a destroyed entry with Token.TOMBSTONE
assertTrue(entry.isTombstone());
}
myRegion.localDestroy(notAffectedKey);
if (useTX) {
c.getCacheTransactionManager().begin();
}
// check that getAll returns invalidated entries
List keys = new LinkedList();
keys.add(notAffectedKey);
keys.add(key1);
keys.add(key2);
Map result = myRegion.getAll(keys);
org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("result of getAll = " + result);
assertNotNull(result.get(notAffectedKey));
assertNull(result.get(key1));
assertNull(result.get(key2));
assertFalse(myRegion.containsKey(key1));
assertFalse(myRegion.containsKey(key2));
if (!useTX) {
entry = ((LocalRegion) myRegion).getRegionEntry(key1);
// it should be there
assertNotNull(entry);
// it should be a destroyed entry with Token.TOMBSTONE
assertTrue(entry.isTombstone());
entry = ((LocalRegion) myRegion).getRegionEntry(key2);
// it should be there
assertNotNull(entry);
// it should be a destroyed entry with Token.TOMBSTONE
assertTrue(entry.isTombstone());
} else {
// useTX
c.getCacheTransactionManager().commit();
// test that the commit correctly created the entries in the region
assertNotNull(myRegion.get(notAffectedKey));
assertNull(myRegion.get(key1));
assertFalse(myRegion.containsKey(key1));
entry = ((LocalRegion) myRegion).getRegionEntry(key1);
// it should be there
assertNotNull(entry);
// it should be a destroyed entry with Token.TOMBSTONE
assertTrue(entry.isTombstone());
assertNull(myRegion.get(key2));
assertFalse(myRegion.containsKey(key2));
entry = ((LocalRegion) myRegion).getRegionEntry(key2);
// it should be there
assertNotNull(entry);
// it should be a destroyed entry with Token.TOMBSTONE
assertTrue(entry.isTombstone());
}
}
use of org.apache.geode.test.dunit.SerializableRunnable in project geode by apache.
the class ConnectDisconnectDUnitTest method setLocatorPorts.
public void setLocatorPorts(int[] ports) {
DistributedTestUtils.deleteLocatorStateFile(ports);
String locators = "";
for (int i = 0; i < ports.length; i++) {
if (i > 0) {
locators += ",";
}
locators += "localhost[" + ports[i] + "]";
}
final String locators_string = locators;
for (int i = 0; i < ports.length; i++) {
final int port = ports[i];
Host.getHost(0).getVM(i).invoke(new SerializableRunnable("set locator port") {
public void run() {
LOCATOR_PORT = port;
LOCATORS_STRING = locators_string;
}
});
}
locatorPorts = ports;
}
Aggregations