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;
}
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;
}
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);
}
}
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);
}
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));
}
Aggregations