use of org.apache.geode.cache.client.ServerRefusedConnectionException in project geode by apache.
the class DurableClientSimpleDUnitTest method testMultipleVMsWithSameDurableId.
/**
* Test that a second VM with the same durable id cannot connect to the server while the first VM
* is connected. Also, verify that the first client is not affected by the second one attempting
* to connect.
*/
@Ignore("TODO: test is disabled")
@Test
public void testMultipleVMsWithSameDurableId() {
// Start a server
final int serverPort = ((Integer) this.server1VM.invoke(() -> CacheServerTestUtil.createCacheServer(regionName, new Boolean(true)))).intValue();
// Start a durable client that is not kept alive on the server when it
// stops normally
final String durableClientId = getName() + "_client";
this.durableClientVM.invoke(() -> CacheServerTestUtil.createCacheClient(getClientPool(getServerHostName(durableClientVM.getHost()), serverPort, true), regionName, getClientDistributedSystemProperties(durableClientId), Boolean.TRUE));
// Send clientReady message
this.durableClientVM.invoke(new CacheSerializableRunnable("Send clientReady") {
@Override
public void run2() throws CacheException {
CacheServerTestUtil.getCache().readyForEvents();
}
});
// Have the durable client register interest in all keys
this.durableClientVM.invoke(new CacheSerializableRunnable("Register interest") {
@Override
public void run2() throws CacheException {
// Get the region
Region region = CacheServerTestUtil.getCache().getRegion(regionName);
assertNotNull(region);
// Register interest in all keys
region.registerInterestRegex(".*", InterestResultPolicy.NONE);
}
});
// Attempt to start another durable client VM with the same id.
this.publisherClientVM.invoke(new CacheSerializableRunnable("Create another durable client") {
@Override
public void run2() throws CacheException {
getSystem(getClientDistributedSystemProperties(durableClientId));
PoolFactoryImpl pf = (PoolFactoryImpl) PoolManager.createFactory();
pf.init(getClientPool(getServerHostName(publisherClientVM.getHost()), serverPort, true));
try {
pf.create("uncreatablePool");
fail("Should not have been able to create the pool");
} catch (ServerRefusedConnectionException expected) {
// expected exception
disconnectFromDS();
} catch (Exception e) {
Assert.fail("Should not have gotten here", e);
}
}
});
// Verify durable client on server
this.server1VM.invoke(new CacheSerializableRunnable("Verify durable client") {
@Override
public void run2() throws CacheException {
// Find the proxy
checkNumberOfClientProxies(1);
CacheClientProxy proxy = getClientProxy();
assertNotNull(proxy);
// Verify that it is durable and its properties are correct
assertTrue(proxy.isDurable());
assertEquals(durableClientId, proxy.getDurableId());
assertEquals(DistributionConfig.DEFAULT_DURABLE_CLIENT_TIMEOUT, proxy.getDurableTimeout());
}
});
// Start normal publisher client
this.publisherClientVM.invoke(() -> CacheServerTestUtil.createCacheClient(getClientPool(getServerHostName(publisherClientVM.getHost()), serverPort, false), regionName));
// Publish some entries
final int numberOfEntries = 10;
publishEntries(numberOfEntries);
// Verify the durable client received the updates
verifyDurableClientEvents(this.durableClientVM, numberOfEntries);
// Stop the publisher client
this.publisherClientVM.invoke(() -> CacheServerTestUtil.closeCache());
// Stop the durable client
this.durableClientVM.invoke(() -> CacheServerTestUtil.closeCache());
// Stop the server
this.server1VM.invoke(() -> CacheServerTestUtil.closeCache());
}
use of org.apache.geode.cache.client.ServerRefusedConnectionException in project geode by apache.
the class SecurityTestUtils method createCacheClientForMultiUserMode.
protected static void createCacheClientForMultiUserMode(final int numOfUsers, final String authInitModule, final Properties[] authProps, final Properties javaProps, final int[] ports, final int numConnections, final boolean setupDynamicRegionFactory, final String durableClientId, final int expectedResult) {
if (numOfUsers < 1) {
fail("Number of users cannot be less than one");
}
multiUserAuthMode = true;
if (numOfUsers != authProps.length) {
fail("Number of authProps provided does not match with numOfUsers specified, " + authProps.length);
}
if (authProps[0] == null) {
authProps[0] = new Properties();
}
authProps[0].setProperty(MCAST_PORT, "0");
authProps[0].setProperty(LOCATORS, "");
authProps[0].setProperty(SECURITY_LOG_LEVEL, "finest");
Properties props = new Properties();
if (authInitModule != null) {
authProps[0].setProperty(SECURITY_CLIENT_AUTH_INIT, authInitModule);
props.setProperty(SECURITY_CLIENT_AUTH_INIT, authInitModule);
}
if (durableClientId != null) {
props.setProperty(DURABLE_CLIENT_ID, durableClientId);
props.setProperty(DURABLE_CLIENT_TIMEOUT, String.valueOf(DistributionConfig.DEFAULT_DURABLE_CLIENT_TIMEOUT));
}
SecurityTestUtils tmpInstance = new SecurityTestUtils("temp");
tmpInstance.createSystem(props, javaProps);
AttributesFactory factory = new AttributesFactory();
int[] portsI = new int[ports.length];
for (int z = 0; z < ports.length; z++) {
portsI[z] = ports[z];
}
try {
tmpInstance.openCache();
PoolFactory poolFactory = PoolManager.createFactory();
poolFactory.setRetryAttempts(200);
poolFactory.setMultiuserAuthentication(multiUserAuthMode);
poolFactory.setSubscriptionEnabled(true);
pool = configureConnectionPoolWithNameAndFactory(factory, getIPLiteral(), portsI, true, 1, numConnections, null, null, poolFactory);
if (setupDynamicRegionFactory) {
initClientDynamicRegionFactory(pool.getName());
}
proxyCaches = new ProxyCache[numOfUsers];
for (int i = 0; i < numOfUsers; i++) {
proxyCaches[i] = (ProxyCache) ((PoolImpl) pool).createAuthenticatedCacheView(authProps[i]);
}
factory.setScope(Scope.LOCAL);
factory.setDataPolicy(DataPolicy.EMPTY);
RegionAttributes attrs = factory.create();
cache.createRegion(REGION_NAME, attrs);
if (expectedResult != NO_EXCEPTION && expectedResult != NOFORCE_AUTHREQ_EXCEPTION) {
if (!multiUserAuthMode) {
fail("Expected an exception when starting client");
}
}
} catch (AuthenticationRequiredException ex) {
if (expectedResult == AUTHREQ_EXCEPTION || expectedResult == NOFORCE_AUTHREQ_EXCEPTION) {
getLogWriter().info("Got expected exception when starting client: " + ex);
} else {
fail("Got unexpected exception when starting client", ex);
}
} catch (AuthenticationFailedException ex) {
if (expectedResult == AUTHFAIL_EXCEPTION) {
getLogWriter().info("Got expected exception when starting client: " + ex);
} else {
fail("Got unexpected exception when starting client", ex);
}
} catch (ServerRefusedConnectionException ex) {
if (expectedResult == CONNREFUSED_EXCEPTION) {
getLogWriter().info("Got expected exception when starting client: " + ex);
} else {
fail("Got unexpected exception when starting client", ex);
}
} catch (Exception ex) {
fail("Got unexpected exception when starting client", ex);
}
}
use of org.apache.geode.cache.client.ServerRefusedConnectionException in project geode by apache.
the class SecurityTestUtils method createCacheClient.
// 5
protected static void createCacheClient(final String authInitModule, Properties authProps, final Properties javaProps, int[] ports, final int numConnections, final boolean setupDynamicRegionFactory, final boolean multiUserMode, final boolean subscriptionEnabled, final int expectedResult) {
multiUserAuthMode = multiUserMode;
if (authProps == null) {
authProps = new Properties();
}
authProps.setProperty(MCAST_PORT, "0");
authProps.setProperty(LOCATORS, "");
authProps.setProperty(SECURITY_LOG_LEVEL, "finest");
// TODO (ashetkar) Add " && (!multiUserAuthMode)" below.
if (authInitModule != null) {
authProps.setProperty(SECURITY_CLIENT_AUTH_INIT, authInitModule);
}
SecurityTestUtils tmpInstance = new SecurityTestUtils("temp");
tmpInstance.createSystem(authProps, javaProps);
AttributesFactory factory = new AttributesFactory();
int[] portsI = new int[ports.length];
for (int z = 0; z < ports.length; z++) {
portsI[z] = ports[z];
}
try {
PoolFactory poolFactory = PoolManager.createFactory();
poolFactory.setRetryAttempts(200);
if (multiUserAuthMode) {
poolFactory.setMultiuserAuthentication(multiUserAuthMode);
// [sumedh] Why is this false here only to be overridden in
// ClientServerTestCase.configureConnectionPoolWithNameAndFactory below?
// Actually setting it to false causes MultiUserAPIDUnitTest to fail.
// poolFactory.setSubscriptionEnabled(false);
}
pool = configureConnectionPoolWithNameAndFactory(factory, getIPLiteral(), portsI, subscriptionEnabled, 0, numConnections, null, null, poolFactory);
if (setupDynamicRegionFactory) {
initClientDynamicRegionFactory(pool.getName());
}
tmpInstance.openCache();
try {
getLogWriter().info("multi-user mode " + multiUserAuthMode);
proxyCaches[0] = (ProxyCache) ((PoolImpl) pool).createAuthenticatedCacheView(authProps);
if (!multiUserAuthMode) {
fail("Expected a UnsupportedOperationException but got none in single-user mode");
}
} catch (UnsupportedOperationException uoe) {
if (!multiUserAuthMode) {
getLogWriter().info("Got expected UnsupportedOperationException in single-user mode");
} else {
fail("Got unexpected exception in multi-user mode ", uoe);
}
}
factory.setScope(Scope.LOCAL);
if (multiUserAuthMode) {
factory.setDataPolicy(DataPolicy.EMPTY);
}
RegionAttributes attrs = factory.create();
cache.createRegionFactory(attrs).create(REGION_NAME);
// if (expectedResult != NO_EXCEPTION && expectedResult != NOFORCE_AUTHREQ_EXCEPTION) {
// if (!multiUserAuthMode) {
// fail("Expected an exception when starting client");
// }
// }
} catch (AuthenticationRequiredException ex) {
if (expectedResult == AUTHREQ_EXCEPTION || expectedResult == NOFORCE_AUTHREQ_EXCEPTION) {
getLogWriter().info("Got expected exception when starting client: " + ex);
} else {
fail("Got unexpected exception when starting client", ex);
}
} catch (AuthenticationFailedException ex) {
if (expectedResult == AUTHFAIL_EXCEPTION) {
getLogWriter().info("Got expected exception when starting client: " + ex);
} else {
fail("Got unexpected exception when starting client", ex);
}
} catch (ServerRefusedConnectionException ex) {
if (expectedResult == CONNREFUSED_EXCEPTION) {
getLogWriter().info("Got expected exception when starting client: " + ex);
} else {
fail("Got unexpected exception when starting client", ex);
}
} catch (GemFireSecurityException ex) {
if (expectedResult == SECURITY_EXCEPTION) {
getLogWriter().info("Got expected exception when starting client: " + ex);
} else {
fail("Got unexpected exception when starting client", ex);
}
} catch (Exception ex) {
fail("Got unexpected exception when starting client", ex);
}
}
use of org.apache.geode.cache.client.ServerRefusedConnectionException in project geode by apache.
the class ConnectionFactoryImpl method createClientToServerConnection.
public Connection createClientToServerConnection(Set excludedServers) throws GemFireSecurityException {
final Set origExcludedServers = excludedServers;
excludedServers = new HashSet(excludedServers);
Set blackListedServers = blackList.getBadServers();
excludedServers.addAll(blackListedServers);
Connection conn = null;
// long startTime = System.currentTimeMillis();
RuntimeException fatalException = null;
boolean tryBlackList = true;
do {
ServerLocation server = source.findServer(excludedServers);
if (server == null) {
if (tryBlackList) {
// Nothing worked! Let's try without the blacklist.
tryBlackList = false;
int size = excludedServers.size();
excludedServers.removeAll(blackListedServers);
// make sure we didn't remove any of the ones that the caller set not to use
excludedServers.addAll(origExcludedServers);
if (excludedServers.size() < size) {
// We are able to remove some exclusions, so lets give this another whirl.
continue;
}
}
if (logger.isDebugEnabled()) {
logger.debug("Source was unable to locate any servers");
}
if (fatalException != null) {
throw fatalException;
}
return null;
}
try {
conn = createClientToServerConnection(server, false);
} catch (CancelException e) {
// propagate this up immediately
throw e;
} catch (GemFireSecurityException e) {
// propagate this up immediately
throw e;
} catch (GatewayConfigurationException e) {
// propagate this up immediately
throw e;
} catch (ServerRefusedConnectionException srce) {
fatalException = srce;
if (logger.isDebugEnabled()) {
logger.debug("ServerRefusedConnectionException attempting to connect to {}", server, srce);
}
} catch (Exception e) {
logger.warn(LocalizedMessage.create(LocalizedStrings.ConnectException_COULD_NOT_CONNECT_TO_0, server), e);
}
excludedServers.add(server);
} while (conn == null);
// }
return conn;
}
use of org.apache.geode.cache.client.ServerRefusedConnectionException in project geode by apache.
the class ConnectionFactoryImpl method createClientToServerConnection.
public Connection createClientToServerConnection(ServerLocation location, boolean forQueue) throws GemFireSecurityException {
ConnectionImpl connection = new ConnectionImpl(this.ds, this.cancelCriterion);
FailureTracker failureTracker = blackList.getFailureTracker(location);
boolean initialized = false;
try {
HandShake connHandShake = new HandShake(handshake);
connection.connect(endpointManager, location, connHandShake, socketBufferSize, handShakeTimeout, readTimeout, getCommMode(forQueue), this.gatewaySender, this.socketCreator);
failureTracker.reset();
connection.setHandShake(connHandShake);
authenticateIfRequired(connection);
initialized = true;
} catch (GemFireConfigException e) {
throw e;
} catch (CancelException e) {
// propagate this up, don't retry
throw e;
} catch (GemFireSecurityException e) {
// propagate this up, don't retry
throw e;
} catch (GatewayConfigurationException e) {
// propagate this up, don't retry
throw e;
} catch (ServerRefusedConnectionException src) {
// propagate this up, don't retry
logger.warn(LocalizedMessage.create(LocalizedStrings.AutoConnectionSourceImpl_COULD_NOT_CREATE_A_NEW_CONNECTION_TO_SERVER_0, src.getMessage()));
testFailedConnectionToServer = true;
throw src;
} catch (Exception e) {
if (e.getMessage() != null && (e.getMessage().equals("Connection refused") || e.getMessage().equals("Connection reset"))) {
// print an exception
if (logger.isDebugEnabled()) {
logger.debug("Unable to connect to {}: connection refused", location);
}
} else {
// print a warning with the exception stack trace
logger.warn(LocalizedMessage.create(LocalizedStrings.ConnectException_COULD_NOT_CONNECT_TO_0, location), e);
}
testFailedConnectionToServer = true;
} finally {
if (!initialized) {
connection.destroy();
failureTracker.addFailure();
connection = null;
}
}
return connection;
}
Aggregations