Search in sources :

Example 36 with ServerLocation

use of org.apache.geode.distributed.internal.ServerLocation 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 37 with ServerLocation

use of org.apache.geode.distributed.internal.ServerLocation in project geode by apache.

the class ConnectionFactoryImpl method findBestServer.

public ServerLocation findBestServer(ServerLocation currentServer, Set excludedServers) {
    if (currentServer != null && source.isBalanced()) {
        return currentServer;
    }
    final Set origExcludedServers = excludedServers;
    excludedServers = new HashSet(excludedServers);
    Set blackListedServers = blackList.getBadServers();
    excludedServers.addAll(blackListedServers);
    ServerLocation server = source.findReplacementServer(currentServer, excludedServers);
    if (server == null) {
        // Nothing worked! Let's try without the blacklist.
        if (excludedServers.size() > origExcludedServers.size()) {
            // We had some guys black listed so lets give this another whirl.
            server = source.findReplacementServer(currentServer, origExcludedServers);
        }
    }
    if (server == null && logger.isDebugEnabled()) {
        logger.debug("Source was unable to findForReplacement any servers");
    }
    return server;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) ServerLocation(org.apache.geode.distributed.internal.ServerLocation) HashSet(java.util.HashSet)

Example 38 with ServerLocation

use of org.apache.geode.distributed.internal.ServerLocation in project geode by apache.

the class OpExecutorImpl method getNextOpServerLocation.

public ServerLocation getNextOpServerLocation() {
    ServerLocation retVal = null;
    Connection conn = (Connection) (threadLocalConnections ? localConnection.get() : null);
    if (conn == null || conn.isDestroyed()) {
        conn = connectionManager.borrowConnection(serverTimeout);
        retVal = conn.getServer();
        this.connectionManager.returnConnection(conn);
    } else {
        retVal = conn.getServer();
    }
    return retVal;
}
Also used : ServerLocation(org.apache.geode.distributed.internal.ServerLocation)

Example 39 with ServerLocation

use of org.apache.geode.distributed.internal.ServerLocation in project geode by apache.

the class ConnectionPoolImplJUnitTest method testCreatePool.

@Test
public void testCreatePool() throws Exception {
    CacheServer server1 = cache.addCacheServer();
    int port1 = port;
    server1.setPort(port1);
    server1.start();
    PoolFactory cpf = PoolManager.createFactory();
    cpf.addServer("localhost", port1);
    cpf.setSubscriptionEnabled(true);
    cpf.setSubscriptionRedundancy(0);
    PoolImpl pool = (PoolImpl) cpf.create("pool1");
    ServerLocation location1 = new ServerLocation("localhost", port1);
    Op testOp = new Op() {

        public Object attempt(Connection cnx) throws Exception {
            return cnx.getServer();
        }

        @Override
        public boolean useThreadLocalConnection() {
            return true;
        }
    };
    assertEquals(location1, pool.executeOnPrimary(testOp));
    assertEquals(location1, pool.executeOnQueuesAndReturnPrimaryResult(testOp));
}
Also used : PoolFactory(org.apache.geode.cache.client.PoolFactory) ServerLocation(org.apache.geode.distributed.internal.ServerLocation) CacheServer(org.apache.geode.cache.server.CacheServer) ClientServerTest(org.apache.geode.test.junit.categories.ClientServerTest) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 40 with ServerLocation

use of org.apache.geode.distributed.internal.ServerLocation in project geode by apache.

the class ConnectionPoolImplJUnitTest method testExecuteOp.

@Test
public void testExecuteOp() throws Exception {
    CacheServer server1 = cache.addCacheServer();
    CacheServer server2 = cache.addCacheServer();
    int[] ports = AvailablePortHelper.getRandomAvailableTCPPorts(2);
    int port1 = ports[0];
    int port2 = ports[1];
    server1.setPort(port1);
    server2.setPort(port2);
    server1.start();
    server2.start();
    PoolFactory cpf = PoolManager.createFactory();
    cpf.addServer("localhost", port2);
    cpf.addServer("localhost", port1);
    PoolImpl pool = (PoolImpl) cpf.create("pool1");
    ServerLocation location1 = new ServerLocation("localhost", port1);
    ServerLocation location2 = new ServerLocation("localhost", port2);
    Op testOp = new Op() {

        int attempts = 0;

        public Object attempt(Connection cnx) throws Exception {
            if (attempts == 0) {
                attempts++;
                throw new SocketTimeoutException();
            } else {
                return cnx.getServer();
            }
        }

        @Override
        public boolean useThreadLocalConnection() {
            return true;
        }
    };
    // TODO - set retry attempts, and throw in some assertions
    // about how many times we retry
    ServerLocation usedServer = (ServerLocation) pool.execute(testOp);
    assertTrue("expected " + location1 + " or " + location2 + ", got " + usedServer, location1.equals(usedServer) || location2.equals(usedServer));
    testOp = new Op() {

        public Object attempt(Connection cnx) throws Exception {
            throw new SocketTimeoutException();
        }

        @Override
        public boolean useThreadLocalConnection() {
            return true;
        }
    };
    try {
        usedServer = (ServerLocation) pool.execute(testOp);
        fail("Should have failed");
    } catch (ServerConnectivityException expected) {
    // do nothing
    }
}
Also used : ServerLocation(org.apache.geode.distributed.internal.ServerLocation) ServerConnectivityException(org.apache.geode.cache.client.ServerConnectivityException) SocketTimeoutException(java.net.SocketTimeoutException) PoolFactory(org.apache.geode.cache.client.PoolFactory) ServerConnectivityException(org.apache.geode.cache.client.ServerConnectivityException) SocketTimeoutException(java.net.SocketTimeoutException) CacheServer(org.apache.geode.cache.server.CacheServer) ClientServerTest(org.apache.geode.test.junit.categories.ClientServerTest) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Aggregations

ServerLocation (org.apache.geode.distributed.internal.ServerLocation)95 ArrayList (java.util.ArrayList)26 Test (org.junit.Test)21 HashSet (java.util.HashSet)19 List (java.util.List)18 HashMap (java.util.HashMap)17 ClientServerTest (org.apache.geode.test.junit.categories.ClientServerTest)17 ServerConnectivityException (org.apache.geode.cache.client.ServerConnectivityException)13 Map (java.util.Map)12 Iterator (java.util.Iterator)10 ServerOperationException (org.apache.geode.cache.client.ServerOperationException)9 GemFireSecurityException (org.apache.geode.security.GemFireSecurityException)9 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)9 Set (java.util.Set)8 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)8 LinkedHashSet (java.util.LinkedHashSet)7 Host (org.apache.geode.test.dunit.Host)7 VersionedObjectList (org.apache.geode.internal.cache.tier.sockets.VersionedObjectList)6 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)5