Search in sources :

Example 91 with ServerLocation

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

the class ConnectionManagerJUnitTest method testInvalidateServer.

@Test
public void testInvalidateServer() throws InterruptedException, AllConnectionsInUseException, NoAvailableServersException {
    manager = new ConnectionManagerImpl("pool", factory, endpointManager, 10, 0, -1, -1, logger, 60 * 1000, cancelCriterion, poolStats);
    manager.start(background);
    ServerLocation server1 = new ServerLocation("localhost", 1);
    ServerLocation server2 = new ServerLocation("localhost", 2);
    factory.nextServer = server1;
    Connection conn1 = manager.borrowConnection(0);
    Connection conn2 = manager.borrowConnection(0);
    Connection conn3 = manager.borrowConnection(0);
    factory.nextServer = server2;
    Connection conn4 = manager.borrowConnection(0);
    Assert.assertEquals(4, factory.creates);
    Assert.assertEquals(0, factory.destroys);
    manager.returnConnection(conn2);
    endpointManager.serverCrashed(conn2.getEndpoint());
    Assert.assertEquals(3, factory.destroys);
    conn1.destroy();
    manager.returnConnection(conn1);
    Assert.assertEquals(3, factory.destroys);
    manager.returnConnection(conn3);
    manager.returnConnection(conn4);
    Assert.assertEquals(3, factory.destroys);
    manager.borrowConnection(0);
    Assert.assertEquals(4, factory.creates);
    Assert.assertEquals(3, factory.destroys);
}
Also used : ServerLocation(org.apache.geode.distributed.internal.ServerLocation) ClientServerTest(org.apache.geode.test.junit.categories.ClientServerTest) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 92 with ServerLocation

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

the class OpExecutorImplJUnitTest method testExecuteOn.

@Test
public void testExecuteOn() throws Exception {
    OpExecutorImpl exec = new OpExecutorImpl(manager, queueManager, endpointManager, riTracker, 3, 10, false, cancelCriterion, null);
    ServerLocation server = new ServerLocation("localhost", -1);
    Object result = exec.executeOn(server, new Op() {

        @Override
        public Object attempt(Connection cnx) throws Exception {
            return "hello";
        }

        @Override
        public boolean useThreadLocalConnection() {
            return true;
        }
    });
    assertEquals("hello", result);
    assertEquals(1, borrows);
    assertEquals(1, returns);
    assertEquals(0, invalidateConnections);
    assertEquals(0, serverCrashes);
    reset();
    try {
        result = exec.executeOn(server, new Op() {

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

            @Override
            public boolean useThreadLocalConnection() {
                return true;
            }
        });
        fail("Should have got an exception");
    } catch (ServerConnectivityException expected) {
    // do nothing
    }
    assertEquals(1, borrows);
    assertEquals(1, returns);
    assertEquals(1, invalidateConnections);
    assertEquals(0, serverCrashes);
    reset();
    try {
        result = exec.executeOn(server, new Op() {

            @Override
            public Object attempt(Connection cnx) throws Exception {
                throw new ServerOperationException("Something didn't work");
            }

            @Override
            public boolean useThreadLocalConnection() {
                return true;
            }
        });
        fail("Should have got an exception");
    } catch (ServerOperationException expected) {
    // do nothing
    }
    assertEquals(1, borrows);
    assertEquals(1, returns);
    assertEquals(0, invalidateConnections);
    assertEquals(0, serverCrashes);
    reset();
    {
        final String expectedEx = "java.lang.Exception";
        final String addExpected = "<ExpectedException action=add>" + expectedEx + "</ExpectedException>";
        final String removeExpected = "<ExpectedException action=remove>" + expectedEx + "</ExpectedException>";
        logger.info(addExpected);
        try {
            result = exec.executeOn(server, new Op() {

                @Override
                public Object attempt(Connection cnx) throws Exception {
                    throw new Exception("Something didn't work");
                }

                @Override
                public boolean useThreadLocalConnection() {
                    return true;
                }
            });
            fail("Should have got an exception");
        } catch (ServerConnectivityException expected) {
        // do nothing
        } finally {
            logger.info(removeExpected);
        }
    }
    assertEquals(1, borrows);
    assertEquals(1, returns);
    assertEquals(1, invalidateConnections);
    assertEquals(1, serverCrashes);
}
Also used : ServerConnectivityException(org.apache.geode.cache.client.ServerConnectivityException) SocketTimeoutException(java.net.SocketTimeoutException) ServerLocation(org.apache.geode.distributed.internal.ServerLocation) ServerOperationException(org.apache.geode.cache.client.ServerOperationException) NoAvailableServersException(org.apache.geode.cache.client.NoAvailableServersException) SocketTimeoutException(java.net.SocketTimeoutException) ServerConnectivityException(org.apache.geode.cache.client.ServerConnectivityException) ServerOperationException(org.apache.geode.cache.client.ServerOperationException) IOException(java.io.IOException) UnitTest(org.apache.geode.test.junit.categories.UnitTest) ClientServerTest(org.apache.geode.test.junit.categories.ClientServerTest) Test(org.junit.Test)

Example 93 with ServerLocation

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

the class FixedPRSinglehopDUnitTest method testMetadataInClientWithFixedPartitions.

/**
   * This test will check to see if all the partitionAttributes are sent to the client. In case one
   * partition comes late, we should fetch that when there is a network hop because of that
   * partitioned region. This test will create 3 servers with partition. Do some operations on them.
   * Validate that the metadata are fetched and then later up one more partition and do some
   * operations on them. It should fetch new fpa. Verify that the correct servers are known to the
   * client metadata service at the end.
   */
@Test
public void testMetadataInClientWithFixedPartitions() throws Exception {
    final Host host = Host.getHost(0);
    VM server1 = host.getVM(0);
    VM server2 = host.getVM(1);
    VM server3 = host.getVM(2);
    VM server4 = host.getVM(3);
    Boolean simpleFPR = false;
    final int portLocator = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
    final String hostLocator = NetworkUtils.getServerHostName(server1.getHost());
    final String locator = hostLocator + "[" + portLocator + "]";
    server3.invoke(() -> FixedPRSinglehopDUnitTest.startLocatorInVM(portLocator));
    try {
        List<FixedPartitionAttributes> fpaList = new ArrayList<FixedPartitionAttributes>();
        fpaList.add(FixedPartitionAttributes.createFixedPartition("Q1", true, 3));
        fpaList.add(FixedPartitionAttributes.createFixedPartition("Q2", false, 3));
        Integer port1 = (Integer) server1.invoke(() -> FixedPRSinglehopDUnitTest.createServerWithLocator(locator, false, fpaList, simpleFPR));
        fpaList.clear();
        fpaList.add(FixedPartitionAttributes.createFixedPartition("Q3", true, 3));
        Integer port2 = (Integer) server2.invoke(() -> FixedPRSinglehopDUnitTest.createServerWithLocator(locator, false, fpaList, simpleFPR));
        fpaList.clear();
        createClientWithLocator(hostLocator, portLocator);
        putIntoPartitionedRegionsThreeQs();
        getFromPartitionedRegionsFor3Qs();
        // Server 1 is actually primary for both Q1 and Q2, since there is no FPA server with
        // primary set to true.
        Awaitility.await().atMost(15, TimeUnit.SECONDS).until(() -> (server1.invoke(FixedPRSinglehopDUnitTest::primaryBucketsOnServer) == 6) && (server2.invoke(FixedPRSinglehopDUnitTest::primaryBucketsOnServer) == 3));
        // TODO: Verify that all the fpa's are in the map
        server1.invoke(FixedPRSinglehopDUnitTest::printView);
        server2.invoke(FixedPRSinglehopDUnitTest::printView);
        int totalBucketOnServer = 0;
        totalBucketOnServer += (Integer) server1.invoke(FixedPRSinglehopDUnitTest::primaryBucketsOnServer);
        totalBucketOnServer += (Integer) server2.invoke(FixedPRSinglehopDUnitTest::primaryBucketsOnServer);
        verifyMetadata(totalBucketOnServer, 1);
        updateIntoSinglePRFor3Qs();
        // now create one more partition
        fpaList.clear();
        fpaList.add(FixedPartitionAttributes.createFixedPartition("Q4", true, 3));
        fpaList.add(FixedPartitionAttributes.createFixedPartition("Q2", true, 3));
        fpaList.add(FixedPartitionAttributes.createFixedPartition("Q1", false, 3));
        fpaList.add(FixedPartitionAttributes.createFixedPartition("Q3", false, 3));
        Integer port4 = (Integer) server4.invoke(() -> FixedPRSinglehopDUnitTest.createServerWithLocator(locator, false, fpaList, simpleFPR));
        putIntoPartitionedRegions();
        // Wait to make sure that the buckets have actually moved.
        Awaitility.await().atMost(15, TimeUnit.SECONDS).until(() -> (server1.invoke(FixedPRSinglehopDUnitTest::primaryBucketsOnServer) == 3) && (server2.invoke(FixedPRSinglehopDUnitTest::primaryBucketsOnServer) == 3) && (server4.invoke(FixedPRSinglehopDUnitTest::primaryBucketsOnServer) == 6));
        getFromPartitionedRegions();
        server1.invoke(FixedPRSinglehopDUnitTest::printView);
        server2.invoke(FixedPRSinglehopDUnitTest::printView);
        server4.invoke(FixedPRSinglehopDUnitTest::printView);
        updateIntoSinglePR(false);
        ClientMetadataService cms = ((GemFireCacheImpl) cache).getClientMetadataService();
        ClientPartitionAdvisor advisor = cms.getClientPartitionAdvisor("/" + PR_NAME);
        int[] expected = new int[] { port1, port1, port1, port4, port4, port4, port2, port2, port2, port4, port4, port4 };
        for (int i = 0; i < expected.length; i++) {
            ServerLocation primary = advisor.advisePrimaryServerLocation(i);
            assertNotNull("bucket " + i + " had no primary server", primary);
            assertEquals("bucket " + i + " was incorrect", expected[i], primary.getPort());
        }
    } finally {
        server3.invoke(FixedPRSinglehopDUnitTest::stopLocator);
    }
}
Also used : FixedPartitionAttributes(org.apache.geode.cache.FixedPartitionAttributes) ServerLocation(org.apache.geode.distributed.internal.ServerLocation) ClientMetadataService(org.apache.geode.cache.client.internal.ClientMetadataService) ArrayList(java.util.ArrayList) Host(org.apache.geode.test.dunit.Host) VM(org.apache.geode.test.dunit.VM) ClientPartitionAdvisor(org.apache.geode.cache.client.internal.ClientPartitionAdvisor) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 94 with ServerLocation

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

the class RedundancyLevelTestBase method createClientCache.

public static void createClientCache(String host, int port1, int port2, int port3, int port4, int redundancy, int socketReadTimeout, int retryInterval) throws Exception {
    if (!FailOverDetectionByCCU) {
        oldBo = ClientServerObserverHolder.setInstance(new ClientServerObserverAdapter() {

            public void beforeFailoverByCacheClientUpdater(ServerLocation epFailed) {
                try {
                    Thread.sleep(300000);
                } catch (InterruptedException ie) {
                    // expected - test will shut down the cache which will interrupt
                    // the CacheClientUpdater thread that invoked this method
                    Thread.currentThread().interrupt();
                }
            }
        });
    }
    Properties props = new Properties();
    props.setProperty(MCAST_PORT, "0");
    props.setProperty(LOCATORS, "");
    new RedundancyLevelTestBase().createCache(props);
    PoolImpl p = (PoolImpl) PoolManager.createFactory().addServer(host, PORT1).addServer(host, PORT2).addServer(host, PORT3).addServer(host, PORT4).setSubscriptionEnabled(true).setReadTimeout(socketReadTimeout).setSocketBufferSize(32768).setMinConnections(8).setSubscriptionRedundancy(redundancy).setRetryAttempts(5).setPingInterval(retryInterval).create("DurableClientReconnectDUnitTestPool");
    AttributesFactory factory = new AttributesFactory();
    factory.setScope(Scope.DISTRIBUTED_ACK);
    factory.setPoolName(p.getName());
    RegionAttributes attrs = factory.createRegionAttributes();
    cache.createRegion(REGION_NAME, attrs);
    pool = p;
    createEntriesK1andK2();
    registerK1AndK2();
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) ServerLocation(org.apache.geode.distributed.internal.ServerLocation) RegionAttributes(org.apache.geode.cache.RegionAttributes) ClientServerObserverAdapter(org.apache.geode.internal.cache.ClientServerObserverAdapter) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) PoolImpl(org.apache.geode.cache.client.internal.PoolImpl)

Example 95 with ServerLocation

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

the class Bug36457DUnitTest method setClientServerObserver.

public static void setClientServerObserver() {
    PoolImpl.AFTER_PRIMARY_IDENTIFICATION_FROM_BACKUP_CALLBACK_FLAG = true;
    ClientServerObserverHolder.setInstance(new ClientServerObserverAdapter() {

        public void afterPrimaryIdentificationFromBackup(ServerLocation primaryEndpoint) {
            LogWriterUtils.getLogWriter().fine("TEST FAILED HERE YOGI ");
            isFaileoverHappened = true;
        }
    });
}
Also used : ServerLocation(org.apache.geode.distributed.internal.ServerLocation) ClientServerObserverAdapter(org.apache.geode.internal.cache.ClientServerObserverAdapter)

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