use of org.apache.geode.distributed.internal.ServerLocation in project geode by apache.
the class ConnectionPoolImplJUnitTest method testExecuteOp.
@Test
public void testExecuteOp() throws Exception {
CacheServer server1 = cache.addCacheServer();
CacheServer server2 = cache.addCacheServer();
int[] ports = AvailablePortHelper.getRandomAvailableTCPPorts(2);
int port1 = ports[0];
int port2 = ports[1];
server1.setPort(port1);
server2.setPort(port2);
server1.start();
server2.start();
PoolFactory cpf = PoolManager.createFactory();
cpf.addServer("localhost", port2);
cpf.addServer("localhost", port1);
PoolImpl pool = (PoolImpl) cpf.create("pool1");
ServerLocation location1 = new ServerLocation("localhost", port1);
ServerLocation location2 = new ServerLocation("localhost", port2);
Op testOp = new Op() {
int attempts = 0;
public Object attempt(Connection cnx) throws Exception {
if (attempts == 0) {
attempts++;
throw new SocketTimeoutException();
} else {
return cnx.getServer();
}
}
@Override
public boolean useThreadLocalConnection() {
return true;
}
};
// TODO - set retry attempts, and throw in some assertions
// about how many times we retry
ServerLocation usedServer = (ServerLocation) pool.execute(testOp);
assertTrue("expected " + location1 + " or " + location2 + ", got " + usedServer, location1.equals(usedServer) || location2.equals(usedServer));
testOp = new Op() {
public Object attempt(Connection cnx) throws Exception {
throw new SocketTimeoutException();
}
@Override
public boolean useThreadLocalConnection() {
return true;
}
};
try {
usedServer = (ServerLocation) pool.execute(testOp);
fail("Should have failed");
} catch (ServerConnectivityException expected) {
// do nothing
}
}
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 CacheServerImpl method start.
@Override
public synchronized void start() throws IOException {
Assert.assertTrue(this.cache != null);
this.serialNumber = createSerialNumber();
if (DynamicRegionFactory.get().isOpen()) {
// from servers to clients instead of invalidates.
if (!this.notifyBySubscription) {
logger.info(LocalizedMessage.create(LocalizedStrings.CacheServerImpl_FORCING_NOTIFYBYSUBSCRIPTION_TO_SUPPORT_DYNAMIC_REGIONS));
this.notifyBySubscription = true;
}
}
this.advisor = CacheServerAdvisor.createCacheServerAdvisor(this);
this.loadMonitor = new LoadMonitor(loadProbe, maxConnections, loadPollInterval, FORCE_LOAD_UPDATE_FREQUENCY, advisor);
List overflowAttributesList = new LinkedList();
ClientSubscriptionConfig csc = this.getClientSubscriptionConfig();
overflowAttributesList.add(0, csc.getEvictionPolicy());
overflowAttributesList.add(1, valueOf(csc.getCapacity()));
overflowAttributesList.add(2, valueOf(this.port));
String diskStoreName = csc.getDiskStoreName();
if (diskStoreName != null) {
overflowAttributesList.add(3, diskStoreName);
// indicator to use diskstore
overflowAttributesList.add(4, true);
} else {
overflowAttributesList.add(3, csc.getOverflowDirectory());
overflowAttributesList.add(4, false);
}
this.acceptor = new AcceptorImpl(getPort(), getBindAddress(), getNotifyBySubscription(), getSocketBufferSize(), getMaximumTimeBetweenPings(), this.cache, getMaxConnections(), getMaxThreads(), getMaximumMessageCount(), getMessageTimeToLive(), this.loadMonitor, overflowAttributesList, this.isGatewayReceiver, this.gatewayTransportFilters, this.tcpNoDelay);
this.acceptor.start();
this.advisor.handshake();
this.loadMonitor.start(new ServerLocation(getExternalAddress(), getPort()), acceptor.getStats());
// TODO : Need to provide facility to enable/disable client health monitoring.
// Creating ClientHealthMonitoring region.
// Force initialization on current cache
ClientHealthMonitoringRegion.getInstance(this.cache);
this.cache.getLoggerI18n().config(LocalizedStrings.CacheServerImpl_CACHESERVER_CONFIGURATION___0, getConfig());
/*
* If the stopped bridge server is restarted, we'll need to re-register the client membership
* listener. If the listener is already registered it won't be registered as would the case when
* start() is invoked for the first time.
*/
ClientMembershipListener[] membershipListeners = ClientMembership.getClientMembershipListeners();
boolean membershipListenerRegistered = false;
for (ClientMembershipListener membershipListener : membershipListeners) {
// just checking by reference as the listener instance is final
if (listener == membershipListener) {
membershipListenerRegistered = true;
break;
}
}
if (!membershipListenerRegistered) {
ClientMembership.registerClientMembershipListener(listener);
}
if (!isGatewayReceiver) {
InternalDistributedSystem system = this.cache.getInternalDistributedSystem();
system.handleResourceEvent(ResourceEvent.CACHE_SERVER_START, this);
}
}
Aggregations