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