Search in sources :

Example 1 with ServerRefusedConnectionException

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());
}
Also used : CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) CacheException(org.apache.geode.cache.CacheException) ServerRefusedConnectionException(org.apache.geode.cache.client.ServerRefusedConnectionException) Region(org.apache.geode.cache.Region) ServerRefusedConnectionException(org.apache.geode.cache.client.ServerRefusedConnectionException) CqExistsException(org.apache.geode.cache.query.CqExistsException) CqException(org.apache.geode.cache.query.CqException) IgnoredException(org.apache.geode.test.dunit.IgnoredException) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) CacheException(org.apache.geode.cache.CacheException) PoolFactoryImpl(org.apache.geode.internal.cache.PoolFactoryImpl) Ignore(org.junit.Ignore) ClientSubscriptionTest(org.apache.geode.test.junit.categories.ClientSubscriptionTest) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 2 with ServerRefusedConnectionException

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);
    }
}
Also used : PoolFactory(org.apache.geode.cache.client.PoolFactory) AttributesFactory(org.apache.geode.cache.AttributesFactory) RegionAttributes(org.apache.geode.cache.RegionAttributes) ServerRefusedConnectionException(org.apache.geode.cache.client.ServerRefusedConnectionException) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) PoolImpl(org.apache.geode.cache.client.internal.PoolImpl) ServerRefusedConnectionException(org.apache.geode.cache.client.ServerRefusedConnectionException) NoAvailableServersException(org.apache.geode.cache.client.NoAvailableServersException) QueryInvocationTargetException(org.apache.geode.cache.query.QueryInvocationTargetException) FunctionException(org.apache.geode.cache.execute.FunctionException) ServerConnectivityException(org.apache.geode.cache.client.ServerConnectivityException) ServerOperationException(org.apache.geode.cache.client.ServerOperationException) IOException(java.io.IOException)

Example 3 with ServerRefusedConnectionException

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);
    }
}
Also used : RegionAttributes(org.apache.geode.cache.RegionAttributes) ServerRefusedConnectionException(org.apache.geode.cache.client.ServerRefusedConnectionException) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) PoolImpl(org.apache.geode.cache.client.internal.PoolImpl) ServerRefusedConnectionException(org.apache.geode.cache.client.ServerRefusedConnectionException) NoAvailableServersException(org.apache.geode.cache.client.NoAvailableServersException) QueryInvocationTargetException(org.apache.geode.cache.query.QueryInvocationTargetException) FunctionException(org.apache.geode.cache.execute.FunctionException) ServerConnectivityException(org.apache.geode.cache.client.ServerConnectivityException) ServerOperationException(org.apache.geode.cache.client.ServerOperationException) IOException(java.io.IOException) PoolFactory(org.apache.geode.cache.client.PoolFactory) AttributesFactory(org.apache.geode.cache.AttributesFactory)

Example 4 with ServerRefusedConnectionException

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;
}
Also used : GemFireSecurityException(org.apache.geode.security.GemFireSecurityException) Set(java.util.Set) HashSet(java.util.HashSet) GatewayConfigurationException(org.apache.geode.cache.GatewayConfigurationException) ServerLocation(org.apache.geode.distributed.internal.ServerLocation) ServerRefusedConnectionException(org.apache.geode.cache.client.ServerRefusedConnectionException) CancelException(org.apache.geode.CancelException) ServerRefusedConnectionException(org.apache.geode.cache.client.ServerRefusedConnectionException) GatewayConfigurationException(org.apache.geode.cache.GatewayConfigurationException) GemFireConfigException(org.apache.geode.GemFireConfigException) CancelException(org.apache.geode.CancelException) GemFireSecurityException(org.apache.geode.security.GemFireSecurityException) HashSet(java.util.HashSet)

Example 5 with ServerRefusedConnectionException

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;
}
Also used : HandShake(org.apache.geode.internal.cache.tier.sockets.HandShake) GemFireSecurityException(org.apache.geode.security.GemFireSecurityException) FailureTracker(org.apache.geode.cache.client.internal.ServerBlackList.FailureTracker) GatewayConfigurationException(org.apache.geode.cache.GatewayConfigurationException) GemFireConfigException(org.apache.geode.GemFireConfigException) ServerRefusedConnectionException(org.apache.geode.cache.client.ServerRefusedConnectionException) CancelException(org.apache.geode.CancelException) ServerRefusedConnectionException(org.apache.geode.cache.client.ServerRefusedConnectionException) GatewayConfigurationException(org.apache.geode.cache.GatewayConfigurationException) GemFireConfigException(org.apache.geode.GemFireConfigException) CancelException(org.apache.geode.CancelException) GemFireSecurityException(org.apache.geode.security.GemFireSecurityException)

Aggregations

ServerRefusedConnectionException (org.apache.geode.cache.client.ServerRefusedConnectionException)5 IOException (java.io.IOException)2 Properties (java.util.Properties)2 CancelException (org.apache.geode.CancelException)2 GemFireConfigException (org.apache.geode.GemFireConfigException)2 AttributesFactory (org.apache.geode.cache.AttributesFactory)2 GatewayConfigurationException (org.apache.geode.cache.GatewayConfigurationException)2 RegionAttributes (org.apache.geode.cache.RegionAttributes)2 NoAvailableServersException (org.apache.geode.cache.client.NoAvailableServersException)2 PoolFactory (org.apache.geode.cache.client.PoolFactory)2 ServerConnectivityException (org.apache.geode.cache.client.ServerConnectivityException)2 ServerOperationException (org.apache.geode.cache.client.ServerOperationException)2 PoolImpl (org.apache.geode.cache.client.internal.PoolImpl)2 FunctionException (org.apache.geode.cache.execute.FunctionException)2 QueryInvocationTargetException (org.apache.geode.cache.query.QueryInvocationTargetException)2 ConfigurationProperties (org.apache.geode.distributed.ConfigurationProperties)2 GemFireSecurityException (org.apache.geode.security.GemFireSecurityException)2 HashSet (java.util.HashSet)1 Set (java.util.Set)1 CacheException (org.apache.geode.cache.CacheException)1