Search in sources :

Example 21 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 22 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)

Example 23 with ServerLocation

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

the class ClientMetadataService method groupByServerToBuckets.

/**
   * This function should make a map of server to buckets it is hosting. If for some bucket servers
   * are not available due to mismatch in metadata it should fill up a random server for it.
   */
private HashMap<ServerLocation, HashSet<Integer>> groupByServerToBuckets(ClientPartitionAdvisor prAdvisor, Set<Integer> bucketSet, boolean primaryOnly) {
    if (primaryOnly) {
        HashMap<ServerLocation, HashSet<Integer>> serverToBucketsMap = new HashMap<ServerLocation, HashSet<Integer>>();
        for (Integer bucketId : bucketSet) {
            ServerLocation server = prAdvisor.advisePrimaryServerLocation(bucketId);
            if (server == null) {
                // will cause us to use the non-single hop path.
                return null;
            }
            HashSet<Integer> buckets = serverToBucketsMap.get(server);
            if (buckets == null) {
                // faster if this was an ArrayList
                buckets = new HashSet<Integer>();
                serverToBucketsMap.put(server, buckets);
            }
            buckets.add(bucketId);
        }
        if (logger.isDebugEnabled()) {
            logger.debug("ClientMetadataService: The server to bucket map is : {}", serverToBucketsMap);
        }
        return serverToBucketsMap;
    } else {
        return pruneNodes(prAdvisor, bucketSet);
    }
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ServerLocation(org.apache.geode.distributed.internal.ServerLocation) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 24 with ServerLocation

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

the class CacheServerLoadMessage method fromData.

@Override
public void fromData(DataInput in) throws IOException, ClassNotFoundException {
    super.fromData(in);
    load = new ServerLoad();
    InternalDataSerializer.invokeFromData(load, in);
    location = new ServerLocation();
    InternalDataSerializer.invokeFromData(location, in);
    this.clientIds = DataSerializer.readArrayList(in);
}
Also used : ServerLoad(org.apache.geode.cache.server.ServerLoad) ServerLocation(org.apache.geode.distributed.internal.ServerLocation)

Example 25 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)

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