Search in sources :

Example 1 with NoSubscriptionServersAvailableException

use of org.apache.geode.cache.NoSubscriptionServersAvailableException in project geode by apache.

the class DurableClientBug39997DUnitTest method testNoServerAvailableOnStartup.

@Test
public void testNoServerAvailableOnStartup() {
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    final String hostName = NetworkUtils.getServerHostName(host);
    final int port = AvailablePortHelper.getRandomAvailableTCPPort();
    vm0.invoke(new SerializableRunnable("create cache") {

        public void run() {
            getSystem(getClientProperties());
            PoolImpl p = (PoolImpl) PoolManager.createFactory().addServer(hostName, port).setSubscriptionEnabled(true).setSubscriptionRedundancy(0).create("DurableClientReconnectDUnitTestPool");
            AttributesFactory factory = new AttributesFactory();
            factory.setScope(Scope.LOCAL);
            factory.setPoolName(p.getName());
            Cache cache = getCache();
            Region region1 = cache.createRegion("region", factory.create());
            cache.readyForEvents();
            try {
                region1.registerInterest("ALL_KEYS");
                fail("Should have received an exception trying to register interest");
            } catch (NoSubscriptionServersAvailableException expected) {
            // this is expected
            }
        }
    });
    vm1.invoke(new SerializableRunnable() {

        public void run() {
            Cache cache = getCache();
            AttributesFactory factory = new AttributesFactory();
            factory.setScope(Scope.DISTRIBUTED_ACK);
            cache.createRegion("region", factory.create());
            CacheServer server = cache.addCacheServer();
            server.setPort(port);
            try {
                server.start();
            } catch (IOException e) {
                Assert.fail("couldn't start server", e);
            }
        }
    });
    vm0.invoke(new SerializableRunnable() {

        public void run() {
            Cache cache = getCache();
            final Region region = cache.getRegion("region");
            Wait.waitForCriterion(new WaitCriterion() {

                public String description() {
                    return "Wait for register interest to succeed";
                }

                public boolean done() {
                    try {
                        region.registerInterest("ALL_KEYS");
                    } catch (NoSubscriptionServersAvailableException e) {
                        return false;
                    }
                    return true;
                }
            }, 30000, 1000, true);
        }
    });
}
Also used : SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) Host(org.apache.geode.test.dunit.Host) IOException(java.io.IOException) PoolImpl(org.apache.geode.cache.client.internal.PoolImpl) AttributesFactory(org.apache.geode.cache.AttributesFactory) WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) VM(org.apache.geode.test.dunit.VM) NoSubscriptionServersAvailableException(org.apache.geode.cache.NoSubscriptionServersAvailableException) Region(org.apache.geode.cache.Region) CacheServer(org.apache.geode.cache.server.CacheServer) Cache(org.apache.geode.cache.Cache) ClientSubscriptionTest(org.apache.geode.test.junit.categories.ClientSubscriptionTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 2 with NoSubscriptionServersAvailableException

use of org.apache.geode.cache.NoSubscriptionServersAvailableException in project geode by apache.

the class QueueManagerImpl method getAllConnections.

public QueueConnections getAllConnections() {
    ConnectionList snapshot = queueConnections;
    if (snapshot.getPrimary() == null) {
        // wait for a new primary to become available.
        synchronized (lock) {
            snapshot = queueConnections;
            while (snapshot.getPrimary() == null && !snapshot.primaryDiscoveryFailed() && !shuttingDown && pool.getPoolOrCacheCancelInProgress() == null) {
                try {
                    lock.wait();
                } catch (InterruptedException ignore) {
                    Thread.currentThread().interrupt();
                    break;
                }
                snapshot = queueConnections;
            }
        }
    }
    if (snapshot.getPrimary() == null) {
        pool.getCancelCriterion().checkCancelInProgress(null);
        GemFireException exception = snapshot.getPrimaryDiscoveryException();
        if (exception == null || exception instanceof NoSubscriptionServersAvailableException) {
            exception = new NoSubscriptionServersAvailableException(exception);
        } else {
            exception = new ServerConnectivityException(exception.getMessage(), exception);
        }
        throw exception;
    }
    return snapshot;
}
Also used : ServerConnectivityException(org.apache.geode.cache.client.ServerConnectivityException) GemFireException(org.apache.geode.GemFireException) NoSubscriptionServersAvailableException(org.apache.geode.cache.NoSubscriptionServersAvailableException)

Example 3 with NoSubscriptionServersAvailableException

use of org.apache.geode.cache.NoSubscriptionServersAvailableException in project geode by apache.

the class QueueManagerImpl method initializeConnections.

private void initializeConnections() {
    final boolean isDebugEnabled = logger.isDebugEnabled();
    if (isDebugEnabled) {
        logger.debug("SubscriptionManager - intitializing connections");
    }
    int queuesNeeded = redundancyLevel == -1 ? -1 : redundancyLevel + 1;
    Set excludedServers = new HashSet(blackList.getBadServers());
    List servers = findQueueServers(excludedServers, queuesNeeded, true, false, null);
    if (servers == null || servers.isEmpty()) {
        logger.warn(LocalizedStrings.QueueManagerImpl_COULD_NOT_CREATE_A_QUEUE_NO_QUEUE_SERVERS_AVAILABLE);
        scheduleRedundancySatisfierIfNeeded(redundancyRetryInterval);
        synchronized (lock) {
            queueConnections = queueConnections.setPrimaryDiscoveryFailed(null);
            lock.notifyAll();
        }
        return;
    }
    if (isDebugEnabled) {
        logger.debug("SubscriptionManager - discovered subscription servers {}", servers);
    }
    SortedMap /* <ServerQueueStatus,Connection> */
    oldQueueServers = new TreeMap(QSIZE_COMPARATOR);
    List nonRedundantServers = new ArrayList();
    for (Iterator itr = servers.iterator(); itr.hasNext(); ) {
        ServerLocation server = (ServerLocation) itr.next();
        Connection connection = null;
        try {
            connection = factory.createClientToServerConnection(server, true);
        } catch (GemFireSecurityException e) {
            throw e;
        } catch (GemFireConfigException e) {
            throw e;
        } catch (Exception e) {
            if (isDebugEnabled) {
                logger.debug("SubscriptionManager - Error connected to server: {}", server, e);
            }
        }
        if (connection != null) {
            ServerQueueStatus status = connection.getQueueStatus();
            if (status.isRedundant() || status.isPrimary()) {
                oldQueueServers.put(status, connection);
            } else {
                nonRedundantServers.add(connection);
            }
        }
    }
    // This ordering was determined from the old ConnectionProxyImpl code
    //
    // initialization order of the new redundant and primary server is
    // old redundant w/ second largest queue
    // old redundant w/ third largest queue
    // ...
    // old primary
    // non redundants in no particular order
    //
    // The primary is then chosen as
    // redundant with the largest queue
    // primary if there are no redundants
    // a non redundant
    // if the redundant with the largest queue fails, then we go and
    // make a new server a primary.
    Connection newPrimary = null;
    if (!oldQueueServers.isEmpty()) {
        newPrimary = (Connection) oldQueueServers.remove(oldQueueServers.lastKey());
    } else if (!nonRedundantServers.isEmpty()) {
        newPrimary = (Connection) nonRedundantServers.remove(0);
    }
    nonRedundantServers.addAll(0, oldQueueServers.values());
    for (Iterator itr = nonRedundantServers.iterator(); itr.hasNext(); ) {
        Connection connection = (Connection) itr.next();
        QueueConnectionImpl queueConnection = initializeQueueConnection(connection, false, null);
        if (queueConnection != null) {
            addToConnectionList(queueConnection, false);
        }
    }
    QueueConnectionImpl primaryQueue = null;
    if (newPrimary != null) {
        primaryQueue = initializeQueueConnection(newPrimary, true, null);
        if (primaryQueue == null) {
            newPrimary.destroy();
        } else {
            if (!addToConnectionList(primaryQueue, true)) {
                primaryQueue = null;
            }
        }
    }
    excludedServers.addAll(servers);
    // above.
    if (redundancyLevel != -1 && getCurrentRedundancy() < redundancyLevel) {
        if (isDebugEnabled) {
            logger.debug("SubscriptionManager - Some initial connections failed. Trying to create redundant queues");
        }
        recoverRedundancy(excludedServers, false);
    }
    if (redundancyLevel != -1 && primaryQueue == null) {
        if (isDebugEnabled) {
            logger.debug("SubscriptionManager - Intial primary creation failed. Trying to create a new primary");
        }
        while (primaryQueue == null) {
            primaryQueue = createNewPrimary(excludedServers);
            if (primaryQueue == null) {
                // couldn't find a server to make primary
                break;
            }
            if (!addToConnectionList(primaryQueue, true)) {
                excludedServers.add(primaryQueue.getServer());
                primaryQueue = null;
            }
        }
    }
    if (primaryQueue == null) {
        if (isDebugEnabled) {
            logger.debug("SubscriptionManager - Unable to create a new primary queue, using one of the redundant queues");
        }
        while (primaryQueue == null) {
            primaryQueue = promoteBackupToPrimary(queueConnections.getBackups());
            if (primaryQueue == null) {
                // no backup servers available
                break;
            }
            if (!addToConnectionList(primaryQueue, true)) {
                synchronized (lock) {
                    // make sure we don't retry this same connection.
                    queueConnections = queueConnections.removeConnection(primaryQueue);
                }
                primaryQueue = null;
            }
        }
    }
    if (primaryQueue == null) {
        logger.error(LocalizedMessage.create(LocalizedStrings.QueueManagerImpl_COULD_NOT_INITIALIZE_A_PRIMARY_QUEUE_ON_STARTUP_NO_QUEUE_SERVERS_AVAILABLE));
        synchronized (lock) {
            queueConnections = queueConnections.setPrimaryDiscoveryFailed(new NoSubscriptionServersAvailableException(LocalizedStrings.QueueManagerImpl_COULD_NOT_INITIALIZE_A_PRIMARY_QUEUE_ON_STARTUP_NO_QUEUE_SERVERS_AVAILABLE.toLocalizedString()));
            lock.notifyAll();
        }
        cqsDisconnected();
    } else {
        cqsConnected();
    }
    if (getCurrentRedundancy() < redundancyLevel) {
        logger.warn(LocalizedMessage.create(LocalizedStrings.QueueManagerImpl_UNABLE_TO_INITIALIZE_ENOUGH_REDUNDANT_QUEUES_ON_STARTUP_THE_REDUNDANCY_COUNT_IS_CURRENTLY_0, getCurrentRedundancy()));
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) ServerLocation(org.apache.geode.distributed.internal.ServerLocation) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) CancelException(org.apache.geode.CancelException) GemFireSecurityException(org.apache.geode.security.GemFireSecurityException) GemFireException(org.apache.geode.GemFireException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ServerConnectivityException(org.apache.geode.cache.client.ServerConnectivityException) GemFireConfigException(org.apache.geode.GemFireConfigException) NoSubscriptionServersAvailableException(org.apache.geode.cache.NoSubscriptionServersAvailableException) GemFireSecurityException(org.apache.geode.security.GemFireSecurityException) GemFireConfigException(org.apache.geode.GemFireConfigException) SortedMap(java.util.SortedMap) NoSubscriptionServersAvailableException(org.apache.geode.cache.NoSubscriptionServersAvailableException) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) ServerQueueStatus(org.apache.geode.internal.cache.tier.sockets.ServerQueueStatus) HashSet(java.util.HashSet)

Example 4 with NoSubscriptionServersAvailableException

use of org.apache.geode.cache.NoSubscriptionServersAvailableException in project geode by apache.

the class QueueManagerJUnitTest method testWaitForPrimary.

@Test
public void testWaitForPrimary() throws Exception {
    factory.addConnection(0, 0, 1);
    manager = new QueueManagerImpl(pool, endpoints, source, factory, 2, 20, logger, ClientProxyMembershipID.getNewProxyMembership(ds));
    manager.start(background);
    manager.getAllConnections().getPrimary().destroy();
    try {
        manager.getAllConnections().getPrimary();
        fail("Should have received NoQueueServersAvailableException");
    } catch (NoSubscriptionServersAvailableException expected) {
    // do thing
    }
    factory.addConnection(0, 0, 2);
    factory.addConnection(0, 0, 3);
    boolean done = false;
    try {
        for (StopWatch time = new StopWatch(true); !done && time.elapsedTimeMillis() < 11 * 1000; ) {
            Thread.sleep(200);
            try {
                manager.getAllConnections();
                done = true;
            } catch (NoSubscriptionServersAvailableException e) {
            // done = false;
            }
        }
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }
    assertTrue("getAllConnections still throwing NoSubscriptionServersAvailableException", done);
    assertPortEquals(2, manager.getAllConnections().getPrimary());
}
Also used : NoSubscriptionServersAvailableException(org.apache.geode.cache.NoSubscriptionServersAvailableException) StopWatch(org.apache.geode.internal.util.StopWatch) ClientServerTest(org.apache.geode.test.junit.categories.ClientServerTest) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Aggregations

NoSubscriptionServersAvailableException (org.apache.geode.cache.NoSubscriptionServersAvailableException)4 GemFireException (org.apache.geode.GemFireException)2 ServerConnectivityException (org.apache.geode.cache.client.ServerConnectivityException)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Set (java.util.Set)1 SortedMap (java.util.SortedMap)1 TreeMap (java.util.TreeMap)1 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)1 CancelException (org.apache.geode.CancelException)1 GemFireConfigException (org.apache.geode.GemFireConfigException)1 AttributesFactory (org.apache.geode.cache.AttributesFactory)1 Cache (org.apache.geode.cache.Cache)1 Region (org.apache.geode.cache.Region)1 PoolImpl (org.apache.geode.cache.client.internal.PoolImpl)1