use of org.apache.geode.cache.client.NoAvailableServersException in project geode by apache.
the class CacheServerMaxConnectionsJUnitTest method testMaxCnxLimit.
/**
* This test performs the following:<br>
* 1)create server<br>
* 2)initialize proxy object and create region for client<br>
* 3)perform a PUT on client by acquiring Connection through proxy<br>
* 4)stop server monitor threads in client to ensure that server treats this as dead client <br>
* 5)wait for some time to allow server to clean up the dead client artifacts<br>
* 6)again perform a PUT on client through same Connection and verify after the put that the
* Connection object used was new one.
*/
@Test
public void testMaxCnxLimit() throws Exception {
PORT = createServer();
createProxyAndRegionForClient();
StatisticsType st = this.system.findType("CacheServerStats");
final Statistics s = this.system.findStatisticsByType(st)[0];
assertEquals(0, s.getInt("currentClients"));
assertEquals(0, s.getInt("currentClientConnections"));
Connection[] cnxs = new Connection[MAX_CNXS];
for (int i = 0; i < MAX_CNXS; i++) {
cnxs[i] = proxy.acquireConnection();
this.system.getLogWriter().info("acquired connection[" + i + "]=" + cnxs[i]);
}
WaitCriterion ev = new WaitCriterion() {
public boolean done() {
return s.getInt("currentClientConnections") == MAX_CNXS;
}
public String description() {
return null;
}
};
Wait.waitForCriterion(ev, 1000, 200, true);
assertEquals(MAX_CNXS, s.getInt("currentClientConnections"));
assertEquals(1, s.getInt("currentClients"));
this.system.getLogWriter().info("<ExpectedException action=add>" + "exceeded max-connections" + "</ExpectedException>");
try {
Connection cnx = proxy.acquireConnection();
if (cnx != null) {
fail("should not have been able to connect more than " + MAX_CNXS + " times but was able to connect " + s.getInt("currentClientConnections") + " times. Last connection=" + cnx);
}
this.system.getLogWriter().info("acquire connection returned null which is ok");
} catch (NoAvailableServersException expected) {
// This is expected but due to race conditions in server handshake
// we may get null back from acquireConnection instead.
this.system.getLogWriter().info("received expected " + expected.getMessage());
} catch (Exception ex) {
fail("expected acquireConnection to throw NoAvailableServersException but instead it threw " + ex);
} finally {
this.system.getLogWriter().info("<ExpectedException action=remove>" + "exceeded max-connections" + "</ExpectedException>");
}
// now lets see what happens we we close our connections
for (int i = 0; i < MAX_CNXS; i++) {
cnxs[i].close(false);
}
ev = new WaitCriterion() {
public boolean done() {
return s.getInt("currentClients") == 0;
}
public String description() {
return null;
}
};
Wait.waitForCriterion(ev, 3 * 1000, 200, true);
this.system.getLogWriter().info("currentClients=" + s.getInt("currentClients") + " currentClientConnections=" + s.getInt("currentClientConnections"));
assertEquals(0, s.getInt("currentClientConnections"));
assertEquals(0, s.getInt("currentClients"));
}
use of org.apache.geode.cache.client.NoAvailableServersException in project geode by apache.
the class ConnectionPoolDUnitTest method basicTestBridgeServerFailover.
private void basicTestBridgeServerFailover(final int cnxCount) throws CacheException {
final String name = this.getName();
final Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
VM vm1 = host.getVM(1);
VM vm2 = host.getVM(2);
// Create two bridge servers
SerializableRunnable createCacheServer = new CacheSerializableRunnable("Create Cache Server") {
public void run2() throws CacheException {
AttributesFactory factory = getBridgeServerRegionAttributes(null, null);
createRegion(name, factory.create());
// pause(1000);
try {
startBridgeServer(0);
} catch (Exception ex) {
org.apache.geode.test.dunit.Assert.fail("While starting CacheServer", ex);
}
}
};
vm0.invoke(createCacheServer);
vm1.invoke(createCacheServer);
final int port0 = vm0.invoke(() -> ConnectionPoolDUnitTest.getCacheServerPort());
final String host0 = NetworkUtils.getServerHostName(vm0.getHost());
final int port1 = vm1.invoke(() -> ConnectionPoolDUnitTest.getCacheServerPort());
// final String host1 = getServerHostName(vm1.getHost());
// Create one bridge client in this VM
SerializableRunnable create = new CacheSerializableRunnable("Create region") {
public void run2() throws CacheException {
getLonerSystem();
getCache();
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.LOCAL);
factory.setConcurrencyChecksEnabled(false);
ClientServerTestCase.configureConnectionPool(factory, host0, port0, port1, true, -1, cnxCount, null, 100);
Region region = createRegion(name, factory.create());
// force connections to form
region.put("keyInit", new Integer(0));
region.put("keyInit2", new Integer(0));
}
};
vm2.invoke(create);
// Launch async thread that puts objects into cache. This thread will execute until
// the test has ended (which is why the RegionDestroyedException and CacheClosedException
// are caught and ignored. If any other exception occurs, the test will fail. See
// the putAI.exceptionOccurred() assertion below.
AsyncInvocation putAI = vm2.invokeAsync(new CacheSerializableRunnable("Put objects") {
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
try {
for (int i = 0; i < 100000; i++) {
region.put("keyAI", new Integer(i));
try {
Thread.sleep(100);
} catch (InterruptedException ie) {
fail("interrupted");
}
}
} catch (NoAvailableServersException ignore) {
/* ignore */
} catch (RegionDestroyedException e) {
// will be thrown when the test ends
/* ignore */
} catch (CancelException e) {
// will be thrown when the test ends
/* ignore */
}
}
});
SerializableRunnable verify1Server = new CacheSerializableRunnable("verify1Server") {
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
PoolImpl pool = getPool(region);
verifyServerCount(pool, 1);
}
};
SerializableRunnable verify2Servers = new CacheSerializableRunnable("verify2Servers") {
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
PoolImpl pool = getPool(region);
verifyServerCount(pool, 2);
}
};
vm2.invoke(verify2Servers);
SerializableRunnable stopCacheServer = new SerializableRunnable("Stop CacheServer") {
public void run() {
stopBridgeServer(getCache());
}
};
final String expected = "java.io.IOException";
final String addExpected = "<ExpectedException action=add>" + expected + "</ExpectedException>";
final String removeExpected = "<ExpectedException action=remove>" + expected + "</ExpectedException>";
vm2.invoke(new SerializableRunnable() {
public void run() {
LogWriter bgexecLogger = new LocalLogWriter(InternalLogWriter.ALL_LEVEL, System.out);
bgexecLogger.info(addExpected);
}
});
try {
// make sure we removeExpected
// Bounce the non-current server (I know that VM1 contains the non-current server
// because ...
vm1.invoke(stopCacheServer);
vm2.invoke(verify1Server);
final int restartPort = port1;
vm1.invoke(new SerializableRunnable("Restart CacheServer") {
public void run() {
try {
Region region = getRootRegion().getSubregion(name);
assertNotNull(region);
startBridgeServer(restartPort);
} catch (Exception e) {
getSystem().getLogWriter().fine(new Exception(e));
org.apache.geode.test.dunit.Assert.fail("Failed to start CacheServer", e);
}
}
});
// Pause long enough for the monitor to realize the server has been bounced
// and reconnect to it.
vm2.invoke(verify2Servers);
} finally {
vm2.invoke(new SerializableRunnable() {
public void run() {
LogWriter bgexecLogger = new LocalLogWriter(InternalLogWriter.ALL_LEVEL, System.out);
bgexecLogger.info(removeExpected);
}
});
}
// Stop the other cache server
vm0.invoke(stopCacheServer);
// Run awhile
vm2.invoke(verify1Server);
org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info(// FIXME
"FIXME: this thread does not terminate");
// // Verify that no exception has occurred in the putter thread
// join(putAI, 5 * 60 * 1000, getLogWriter());
// //assertTrue("Exception occurred while invoking " + putAI, !putAI.exceptionOccurred());
// if (putAI.exceptionOccurred()) {
// fail("While putting entries: ", putAI.getException());
// }
// Close Pool
vm2.invoke(new CacheSerializableRunnable("Close Pool") {
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
region.localDestroyRegion();
}
});
// Stop the last cache server
vm1.invoke(stopCacheServer);
}
use of org.apache.geode.cache.client.NoAvailableServersException in project geode by apache.
the class ConnectionPoolDUnitTest method basicTestLifetimeExpire.
private void basicTestLifetimeExpire(final boolean threadLocal) throws CacheException {
final String name = this.getName();
final Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
VM vm1 = host.getVM(1);
VM vm2 = host.getVM(2);
AsyncInvocation putAI = null;
AsyncInvocation putAI2 = null;
try {
// Create two bridge servers
SerializableRunnable createCacheServer = new CacheSerializableRunnable("Create Cache Server") {
public void run2() throws CacheException {
AttributesFactory factory = getBridgeServerRegionAttributes(null, null);
factory.setCacheListener(new DelayListener(25));
createRegion(name, factory.create());
try {
startBridgeServer(0);
} catch (Exception ex) {
org.apache.geode.test.dunit.Assert.fail("While starting CacheServer", ex);
}
}
};
vm0.invoke(createCacheServer);
final int port0 = vm0.invoke(() -> ConnectionPoolDUnitTest.getCacheServerPort());
final String host0 = NetworkUtils.getServerHostName(vm0.getHost());
vm1.invoke(createCacheServer);
final int port1 = vm1.invoke(() -> ConnectionPoolDUnitTest.getCacheServerPort());
SerializableRunnable stopCacheServer = new SerializableRunnable("Stop CacheServer") {
public void run() {
stopBridgeServer(getCache());
}
};
// we only had to stop it to reserve a port
vm1.invoke(stopCacheServer);
// Create one bridge client in this VM
SerializableRunnable create = new CacheSerializableRunnable("Create region") {
public void run2() throws CacheException {
getLonerSystem();
getCache();
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.LOCAL);
factory.setConcurrencyChecksEnabled(false);
ClientServerTestCase.configureConnectionPool(factory, host0, port0, port1, false, /* queue */
-1, 0, null, 100, 500, threadLocal, 500);
Region region = createRegion(name, factory.create());
// force connections to form
region.put("keyInit", new Integer(0));
region.put("keyInit2", new Integer(0));
}
};
vm2.invoke(create);
// Launch async thread that puts objects into cache. This thread will execute until
// the test has ended.
SerializableRunnable putter1 = new CacheSerializableRunnable("Put objects") {
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
PoolImpl pool = getPool(region);
PoolStats stats = pool.getStats();
baselineLifetimeCheck = stats.getLoadConditioningCheck();
baselineLifetimeExtensions = stats.getLoadConditioningExtensions();
baselineLifetimeConnect = stats.getLoadConditioningConnect();
baselineLifetimeDisconnect = stats.getLoadConditioningDisconnect();
try {
int count = 0;
while (!stopTestLifetimeExpire) {
count++;
region.put("keyAI1", new Integer(count));
}
} catch (NoAvailableServersException ex) {
if (stopTestLifetimeExpire) {
return;
} else {
throw ex;
}
// } catch (RegionDestroyedException e) { //will be thrown when the test ends
// /*ignore*/
// } catch (CancelException e) { //will be thrown when the test ends
// /*ignore*/
}
}
};
SerializableRunnable putter2 = new CacheSerializableRunnable("Put objects") {
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
try {
int count = 0;
while (!stopTestLifetimeExpire) {
count++;
region.put("keyAI2", new Integer(count));
}
} catch (NoAvailableServersException ex) {
if (stopTestLifetimeExpire) {
return;
} else {
throw ex;
}
// } catch (RegionDestroyedException e) { //will be thrown when the test ends
// /*ignore*/
// } catch (CancelException e) { //will be thrown when the test ends
// /*ignore*/
}
}
};
putAI = vm2.invokeAsync(putter1);
putAI2 = vm2.invokeAsync(putter2);
SerializableRunnable verify1Server = new CacheSerializableRunnable("verify1Server") {
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
PoolImpl pool = getPool(region);
final PoolStats stats = pool.getStats();
verifyServerCount(pool, 1);
WaitCriterion ev = new WaitCriterion() {
public boolean done() {
return stats.getLoadConditioningCheck() >= (10 + baselineLifetimeCheck);
}
public String description() {
return null;
}
};
Wait.waitForCriterion(ev, 30 * 1000, 200, true);
// make sure no replacements are happening.
// since we have 2 threads and 2 cnxs and 1 server
// when lifetimes are up we should only want to connect back to the
// server we are already connected to and thus just extend our lifetime
assertTrue("baselineLifetimeCheck=" + baselineLifetimeCheck + " but stats.getLoadConditioningCheck()=" + stats.getLoadConditioningCheck(), stats.getLoadConditioningCheck() >= (10 + baselineLifetimeCheck));
baselineLifetimeCheck = stats.getLoadConditioningCheck();
assertTrue(stats.getLoadConditioningExtensions() > baselineLifetimeExtensions);
assertTrue(stats.getLoadConditioningConnect() == baselineLifetimeConnect);
assertTrue(stats.getLoadConditioningDisconnect() == baselineLifetimeDisconnect);
}
};
SerializableRunnable verify2Servers = new CacheSerializableRunnable("verify2Servers") {
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
PoolImpl pool = getPool(region);
final PoolStats stats = pool.getStats();
verifyServerCount(pool, 2);
// make sure some replacements are happening.
// since we have 2 threads and 2 cnxs and 2 servers
// when lifetimes are up we should connect to the other server sometimes.
// int retry = 300;
// while ((retry-- > 0)
// && (stats.getLoadConditioningCheck() < (10+baselineLifetimeCheck))) {
// pause(100);
// }
// assertTrue("Bug 39209 expected "
// + stats.getLoadConditioningCheck()
// + " to be >= "
// + (10+baselineLifetimeCheck),
// stats.getLoadConditioningCheck() >= (10+baselineLifetimeCheck));
// TODO: does this WaitCriterion actually help?
WaitCriterion wc = new WaitCriterion() {
String excuse;
public boolean done() {
int actual = stats.getLoadConditioningCheck();
int expected = 10 + baselineLifetimeCheck;
if (actual >= expected) {
return true;
}
excuse = "Bug 39209 expected " + actual + " to be >= " + expected;
return false;
}
public String description() {
return excuse;
}
};
try {
Wait.waitForCriterion(wc, 60 * 1000, 1000, true);
} catch (AssertionError e) {
// dumpStack();
throw e;
}
assertTrue(stats.getLoadConditioningConnect() > baselineLifetimeConnect);
assertTrue(stats.getLoadConditioningDisconnect() > baselineLifetimeDisconnect);
}
};
vm2.invoke(verify1Server);
assertEquals(true, putAI.isAlive());
assertEquals(true, putAI2.isAlive());
} finally {
vm2.invoke(new SerializableRunnable("Stop Putters") {
public void run() {
stopTestLifetimeExpire = true;
}
});
try {
if (putAI != null) {
// Verify that no exception has occurred in the putter thread
ThreadUtils.join(putAI, 30 * 1000);
if (putAI.exceptionOccurred()) {
org.apache.geode.test.dunit.Assert.fail("While putting entries: ", putAI.getException());
}
}
if (putAI2 != null) {
// Verify that no exception has occurred in the putter thread
ThreadUtils.join(putAI, 30 * 1000);
// FIXME this thread does not terminate
// if (putAI2.exceptionOccurred()) {
// fail("While putting entries: ", putAI.getException());
// }
}
} finally {
vm2.invoke(new SerializableRunnable("Stop Putters") {
public void run() {
stopTestLifetimeExpire = false;
}
});
// Close Pool
vm2.invoke(new CacheSerializableRunnable("Close Pool") {
public void run2() throws CacheException {
Region region = getRootRegion().getSubregion(name);
String poolName = region.getAttributes().getPoolName();
region.localDestroyRegion();
PoolManager.find(poolName).destroy();
}
});
SerializableRunnable stopCacheServer = new SerializableRunnable("Stop CacheServer") {
public void run() {
stopBridgeServer(getCache());
}
};
vm1.invoke(stopCacheServer);
vm0.invoke(stopCacheServer);
}
}
}
use of org.apache.geode.cache.client.NoAvailableServersException in project geode by apache.
the class SecurityTestUtils method doQueriesP.
private static void doQueriesP(final int multiUserIndex, final int expectedResult, final int expectedValue) {
Region region = null;
try {
if (multiUserAuthMode) {
region = proxyCaches[multiUserIndex].getRegion(REGION_NAME);
} else {
region = getCache().getRegion(REGION_NAME);
}
assertNotNull(region);
} catch (Exception ex) {
if (expectedResult == OTHER_EXCEPTION) {
getLogWriter().info("Got expected exception when doing queries: " + ex);
} else {
fail("Got unexpected exception when doing queries", ex);
}
}
String queryStr = "SELECT DISTINCT * FROM " + region.getFullPath();
try {
SelectResults queryResults = region.query(queryStr);
Set resultSet = queryResults.asSet();
assertEquals(expectedValue, resultSet.size());
if (expectedResult != NO_EXCEPTION) {
fail("Expected a NotAuthorizedException while doing queries");
}
} catch (NoAvailableServersException ex) {
if (expectedResult == NO_AVAILABLE_SERVERS) {
getLogWriter().info("Got expected NoAvailableServers when doing queries: " + ex.getCause());
} else {
fail("Got unexpected exception when doing queries", ex);
}
} catch (ServerConnectivityException ex) {
if ((expectedResult == NOTAUTHZ_EXCEPTION) && (ex.getCause() instanceof NotAuthorizedException)) {
getLogWriter().info("Got expected NotAuthorizedException when doing queries: " + ex.getCause());
} else if (expectedResult == OTHER_EXCEPTION) {
getLogWriter().info("Got expected exception when doing queries: " + ex);
} else {
fail("Got unexpected exception when doing queries", ex);
}
} catch (QueryInvocationTargetException qite) {
if ((expectedResult == NOTAUTHZ_EXCEPTION) && (qite.getCause() instanceof NotAuthorizedException)) {
getLogWriter().info("Got expected NotAuthorizedException when doing queries: " + qite.getCause());
} else if (expectedResult == OTHER_EXCEPTION) {
getLogWriter().info("Got expected exception when doing queries: " + qite);
} else {
fail("Got unexpected exception when doing queries", qite);
}
} catch (Exception ex) {
if (expectedResult == OTHER_EXCEPTION) {
getLogWriter().info("Got expected exception when doing queries: " + ex);
} else {
fail("Got unexpected exception when doing queries", ex);
}
}
}
use of org.apache.geode.cache.client.NoAvailableServersException in project geode by apache.
the class SecurityTestUtils method doGetsP.
private static void doGetsP(final int num, final int multiUserIndex, final int expectedResult, final boolean newVals) {
assertTrue(num <= KEYS.length);
Region region = null;
try {
if (multiUserAuthMode) {
region = proxyCaches[multiUserIndex].getRegion(REGION_NAME);
} else {
region = getCache().getRegion(REGION_NAME);
}
assertNotNull(region);
} catch (Exception ex) {
if (expectedResult == OTHER_EXCEPTION) {
getLogWriter().info("Got expected exception when doing gets: " + ex);
} else {
fail("Got unexpected exception when doing gets", ex);
}
}
for (int index = 0; index < num; ++index) {
Object value = null;
try {
try {
region.localInvalidate(KEYS[index]);
} catch (Exception ex) {
}
value = region.get(KEYS[index]);
if (expectedResult != NO_EXCEPTION) {
fail("Expected a NotAuthorizedException while doing gets");
}
} catch (NoAvailableServersException ex) {
if (expectedResult == NO_AVAILABLE_SERVERS) {
getLogWriter().info("Got expected NoAvailableServers when doing gets: " + ex.getCause());
continue;
} else {
fail("Got unexpected exception when doing gets", ex);
}
} catch (ServerConnectivityException ex) {
if ((expectedResult == NOTAUTHZ_EXCEPTION) && (ex.getCause() instanceof NotAuthorizedException)) {
getLogWriter().info("Got expected NotAuthorizedException when doing gets: " + ex.getCause());
continue;
} else if (expectedResult == OTHER_EXCEPTION) {
getLogWriter().info("Got expected exception when doing gets: " + ex);
} else {
fail("Got unexpected exception when doing gets", ex);
}
} catch (Exception ex) {
if (expectedResult == OTHER_EXCEPTION) {
getLogWriter().info("Got expected exception when doing gets: " + ex);
} else {
fail("Got unexpected exception when doing gets", ex);
}
}
assertNotNull(value);
if (newVals) {
assertEquals(NVALUES[index], value);
} else {
assertEquals(VALUES[index], value);
}
}
}
Aggregations