Search in sources :

Example 41 with ServerLocation

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

the class LocatorLoadBalancingDUnitTest method testDiscovery.

/**
   * Test the locator discovers a bridge server and is initialized with the correct load for that
   * bridge server.
   */
@Test
public void testDiscovery() {
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    VM vm2 = host.getVM(2);
    int locatorPort = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
    String hostName = NetworkUtils.getServerHostName(vm0.getHost());
    vm0.invoke("Start Locator", () -> startLocator(hostName, locatorPort, ""));
    String locators = getLocatorString(host, locatorPort);
    int serverPort = vm1.invoke("Start BridgeServer", () -> startBridgeServer(new String[] { "a", "b" }, locators));
    ServerLoad expectedLoad = new ServerLoad(0f, 1 / 800.0f, 0f, 1f);
    ServerLocation expectedLocation = new ServerLocation(NetworkUtils.getServerHostName(vm0.getHost()), serverPort);
    Map expected = new HashMap();
    expected.put(expectedLocation, expectedLoad);
    vm0.invoke("check Locator Load", () -> checkLocatorLoad(expected));
    int serverPort2 = vm2.invoke("Start BridgeServer", () -> startBridgeServer(new String[] { "a", "b" }, locators));
    ServerLocation expectedLocation2 = new ServerLocation(NetworkUtils.getServerHostName(vm0.getHost()), serverPort2);
    expected.put(expectedLocation2, expectedLoad);
    vm0.invoke("check Locator Load", () -> checkLocatorLoad(expected));
}
Also used : ServerLoad(org.apache.geode.cache.server.ServerLoad) ServerLocation(org.apache.geode.distributed.internal.ServerLocation) HashMap(java.util.HashMap) VM(org.apache.geode.test.dunit.VM) Host(org.apache.geode.test.dunit.Host) HashMap(java.util.HashMap) Map(java.util.Map) ClientServerTest(org.apache.geode.test.junit.categories.ClientServerTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 42 with ServerLocation

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

the class LocatorLoadBalancingDUnitTest method testCustomLoadProbe.

@Test
public void testCustomLoadProbe() throws Exception {
    final Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    VM vm2 = host.getVM(2);
    // VM vm3 = host.getVM(3);
    int locatorPort = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
    String hostName = NetworkUtils.getServerHostName(vm0.getHost());
    vm0.invoke("Start Locator", () -> startLocator(hostName, locatorPort, ""));
    String locators = getLocatorString(host, locatorPort);
    final ServerLoad load1 = new ServerLoad(.3f, .01f, .44f, 4564f);
    final ServerLoad load2 = new ServerLoad(23.2f, 1.1f, 22.3f, .3f);
    int serverPort1 = vm1.invoke("Start BridgeServer", () -> startBridgeServer(null, locators, new String[] { REGION_NAME }, new MyLoadProbe(load1), false));
    int serverPort2 = vm2.invoke("Start BridgeServer", () -> startBridgeServer(null, locators, new String[] { REGION_NAME }, new MyLoadProbe(load2), false));
    HashMap expected = new HashMap();
    ServerLocation l1 = new ServerLocation(NetworkUtils.getServerHostName(host), serverPort1);
    ServerLocation l2 = new ServerLocation(NetworkUtils.getServerHostName(host), serverPort2);
    expected.put(l1, load1);
    expected.put(l2, load2);
    vm0.invoke("check Locator Load", () -> checkLocatorLoad(expected));
    load1.setConnectionLoad(25f);
    vm1.invoke("changeLoad", () -> changeLoad(load1));
    load2.setSubscriptionConnectionLoad(3.5f);
    vm2.invoke("changeLoad", () -> changeLoad(load2));
    vm0.invoke("check Locator Load", () -> checkLocatorLoad(expected));
    final ServerLoad load1Updated = new ServerLoad(1f, .1f, 0f, 1f);
    final ServerLoad load2Updated = new ServerLoad(2f, 5f, 0f, 2f);
    expected.put(l1, load1Updated);
    expected.put(l2, load2Updated);
    vm1.invoke("changeLoad", () -> changeLoad(load1Updated));
    vm2.invoke("changeLoad", () -> changeLoad(load2Updated));
    vm0.invoke("check Locator Load", () -> checkLocatorLoad(expected));
    PoolFactoryImpl pf = new PoolFactoryImpl(null);
    pf.addLocator(NetworkUtils.getServerHostName(host), locatorPort);
    pf.setMinConnections(20);
    pf.setSubscriptionEnabled(true);
    pf.setIdleTimeout(-1);
    startBridgeClient(pf.getPoolAttributes(), new String[] { REGION_NAME });
    waitForPrefilledConnections(20);
    // The first 10 connection should to go vm1, then 1 to vm2, then another 9 to vm1
    // because have unequal values for loadPerConnection
    vm1.invoke("Check Connection Count", () -> checkConnectionCount(19));
    vm2.invoke("Check Connection Count", () -> checkConnectionCount(1));
}
Also used : ServerLoad(org.apache.geode.cache.server.ServerLoad) HashMap(java.util.HashMap) ServerLocation(org.apache.geode.distributed.internal.ServerLocation) VM(org.apache.geode.test.dunit.VM) Host(org.apache.geode.test.dunit.Host) PoolFactoryImpl(org.apache.geode.internal.cache.PoolFactoryImpl) ClientServerTest(org.apache.geode.test.junit.categories.ClientServerTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 43 with ServerLocation

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

the class AutoConnectionSourceImplJUnitTest method testDiscoverLocators.

@Test
public void testDiscoverLocators() throws Exception {
    startFakeLocator();
    int secondPort = AvailablePortHelper.getRandomAvailableTCPPort();
    TcpServer server2 = new TcpServer(secondPort, InetAddress.getLocalHost(), null, null, handler, new FakeHelper(), Thread.currentThread().getThreadGroup(), "tcp server");
    server2.start();
    try {
        ArrayList locators = new ArrayList();
        locators.add(new ServerLocation(InetAddress.getLocalHost().getHostName(), secondPort));
        handler.nextLocatorListResponse = new LocatorListResponse(locators, false);
        Thread.sleep(500);
        try {
            new TcpClient().stop(InetAddress.getLocalHost(), port);
        } catch (ConnectException ignore) {
        // must not be running
        }
        server.join(1000);
        ServerLocation server1 = new ServerLocation("localhost", 10);
        handler.nextConnectionResponse = new ClientConnectionResponse(server1);
        assertEquals(server1, source.findServer(null));
    } finally {
        try {
            new TcpClient().stop(InetAddress.getLocalHost(), secondPort);
        } catch (ConnectException ignore) {
        // must not be running
        }
        server.join(60 * 1000);
    }
}
Also used : LocatorListResponse(org.apache.geode.cache.client.internal.locator.LocatorListResponse) ClientConnectionResponse(org.apache.geode.cache.client.internal.locator.ClientConnectionResponse) ServerLocation(org.apache.geode.distributed.internal.ServerLocation) TcpClient(org.apache.geode.distributed.internal.tcpserver.TcpClient) ArrayList(java.util.ArrayList) TcpServer(org.apache.geode.distributed.internal.tcpserver.TcpServer) ConnectException(java.net.ConnectException) ClientServerTest(org.apache.geode.test.junit.categories.ClientServerTest) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 44 with ServerLocation

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

the class ClientMetadataService method findNextServer.

private ServerLocation findNextServer(Set<Map.Entry<ServerLocation, HashSet<Integer>>> entrySet, HashSet<Integer> currentBucketSet) {
    ServerLocation server = null;
    int max = -1;
    ArrayList<ServerLocation> nodesOfEqualSize = new ArrayList<ServerLocation>();
    for (Map.Entry<ServerLocation, HashSet<Integer>> entry : entrySet) {
        HashSet<Integer> buckets = new HashSet<Integer>();
        buckets.addAll(entry.getValue());
        buckets.removeAll(currentBucketSet);
        if (max < buckets.size()) {
            max = buckets.size();
            server = entry.getKey();
            nodesOfEqualSize.clear();
            nodesOfEqualSize.add(server);
        } else if (max == buckets.size()) {
            nodesOfEqualSize.add(server);
        }
    }
    // return node;
    Random r = new Random();
    if (nodesOfEqualSize.size() > 0) {
        return nodesOfEqualSize.get(r.nextInt(nodesOfEqualSize.size()));
    }
    return null;
}
Also used : Random(java.util.Random) ServerLocation(org.apache.geode.distributed.internal.ServerLocation) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 45 with ServerLocation

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

the class ClientMetadataService method getServerToFilterMap.

public Map<ServerLocation, HashSet> getServerToFilterMap(final Collection routingKeys, final Region region, boolean primaryMembersNeeded, boolean bucketsAsFilter) {
    final String regionFullPath = region.getFullPath();
    ClientPartitionAdvisor prAdvisor = this.getClientPartitionAdvisor(regionFullPath);
    if (prAdvisor == null || prAdvisor.adviseRandomServerLocation() == null) {
        scheduleGetPRMetaData((LocalRegion) region, false);
        return null;
    }
    HashMap<Integer, HashSet> bucketToKeysMap = groupByBucketOnClientSide(region, prAdvisor, routingKeys, bucketsAsFilter);
    HashMap<ServerLocation, HashSet> serverToKeysMap = new HashMap<ServerLocation, HashSet>();
    HashMap<ServerLocation, HashSet<Integer>> serverToBuckets = groupByServerToBuckets(prAdvisor, bucketToKeysMap.keySet(), primaryMembersNeeded);
    if (serverToBuckets == null) {
        return null;
    }
    for (Map.Entry entry : serverToBuckets.entrySet()) {
        ServerLocation server = (ServerLocation) entry.getKey();
        HashSet<Integer> buckets = (HashSet) entry.getValue();
        for (Integer bucket : buckets) {
            // use LinkedHashSet to maintain the order of keys
            // the keys will be iterated several times
            LinkedHashSet keys = (LinkedHashSet) serverToKeysMap.get(server);
            if (keys == null) {
                keys = new LinkedHashSet();
            }
            keys.addAll(bucketToKeysMap.get(bucket));
            serverToKeysMap.put(server, keys);
        }
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Returning server to keys map : {}", serverToKeysMap);
    }
    return serverToKeysMap;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ServerLocation(org.apache.geode.distributed.internal.ServerLocation) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

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