use of com.twitter.heron.common.basics.Pair in project heron by twitter.
the class PackingPlanBuilderTest method testAddToPackingPlan.
@Test
public void testAddToPackingPlan() throws ResourceExceededException {
PackingPlan plan = doCreatePackingPlanTest(testContainerInstances);
@SuppressWarnings({ "unchecked", "rawtypes" }) Pair<Integer, InstanceId>[] added = new Pair[] { new Pair<>(1, new InstanceId("componentB", 4, 1)), new Pair<>(4, new InstanceId("componentA", 5, 2)) };
PackingPlan updatedPlan = PackingTestHelper.addToTestPackingPlan(TOPOLOGY_ID, plan, PackingTestHelper.toContainerIdComponentNames(added), 0);
Pair<Integer, InstanceId>[] expected = concat(testContainerInstances, added);
AssertPacking.assertPackingPlan(TOPOLOGY_ID, expected, updatedPlan);
}
use of com.twitter.heron.common.basics.Pair in project heron by twitter.
the class PackingPlanBuilderTest method testInvalidComponentRemoveFromPackingPlan.
@Test(expected = PackingException.class)
public void testInvalidComponentRemoveFromPackingPlan() throws ResourceExceededException {
PackingPlan plan = doCreatePackingPlanTest(testContainerInstances);
@SuppressWarnings({ "unchecked", "rawtypes" }) Pair<Integer, String>[] removed = new Pair[] { new Pair<>(1, "componentC") };
PackingTestHelper.removeFromTestPackingPlan(TOPOLOGY_ID, plan, removed, 0);
}
use of com.twitter.heron.common.basics.Pair in project heron by twitter.
the class CommonPackingTests method testScaleDownTwoComponentsRemoveContainer.
@Test
public void testScaleDownTwoComponentsRemoveContainer() throws Exception {
@SuppressWarnings({ "unchecked", "rawtypes" }) Pair<Integer, InstanceId>[] initialComponentInstances = new Pair[] { new Pair<>(1, new InstanceId(A, 1, 0)), new Pair<>(1, new InstanceId(A, 2, 1)), new Pair<>(1, new InstanceId(B, 3, 0)), new Pair<>(1, new InstanceId(B, 4, 1)), new Pair<>(3, new InstanceId(A, 5, 2)), new Pair<>(3, new InstanceId(A, 6, 3)), new Pair<>(3, new InstanceId(B, 7, 2)), new Pair<>(3, new InstanceId(B, 8, 3)) };
Map<String, Integer> componentChanges = new HashMap<>();
componentChanges.put(A, -2);
componentChanges.put(B, -2);
@SuppressWarnings({ "unchecked", "rawtypes" }) Pair<Integer, InstanceId>[] expectedComponentInstances = new Pair[] { new Pair<>(1, new InstanceId(A, 1, 0)), new Pair<>(1, new InstanceId(A, 2, 1)), new Pair<>(1, new InstanceId(B, 3, 0)), new Pair<>(1, new InstanceId(B, 4, 1)) };
doScaleDownTest(initialComponentInstances, componentChanges, expectedComponentInstances);
}
use of com.twitter.heron.common.basics.Pair in project heron by twitter.
the class ZkUtils method setupZkTunnel.
/**
* Setup the tunnel if needed
*
* @param config basing on which we setup the tunnel process
* @return Pair of (zk_format_connectionString, List of tunneled processes)
*/
public static Pair<String, List<Process>> setupZkTunnel(Config config, NetworkUtils.TunnelConfig tunnelConfig) {
// Remove all spaces
String connectionString = Context.stateManagerConnectionString(config).replaceAll("\\s+", "");
List<Pair<InetSocketAddress, Process>> ret = new ArrayList<>();
// For zookeeper, connection String can be a list of host:port, separated by comma
String[] endpoints = connectionString.split(",");
for (String endpoint : endpoints) {
InetSocketAddress address = NetworkUtils.getInetSocketAddress(endpoint);
// Get the tunnel process if needed
Pair<InetSocketAddress, Process> pair = NetworkUtils.establishSSHTunnelIfNeeded(address, tunnelConfig, NetworkUtils.TunnelType.PORT_FORWARD);
ret.add(pair);
}
// Construct the new ConnectionString and tunnel processes
StringBuilder connectionStringBuilder = new StringBuilder();
List<Process> tunnelProcesses = new ArrayList<>();
String delim = "";
for (Pair<InetSocketAddress, Process> pair : ret) {
// Join the list of String with comma as delim
if (pair.first != null) {
connectionStringBuilder.append(delim).append(pair.first.getHostName()).append(":").append(pair.first.getPort());
delim = ",";
// If tunneled
if (pair.second != null) {
tunnelProcesses.add(pair.second);
}
}
}
String newConnectionString = connectionStringBuilder.toString();
return new Pair<String, List<Process>>(newConnectionString, tunnelProcesses);
}
use of com.twitter.heron.common.basics.Pair in project heron by twitter.
the class NetworkUtils method establishSSHTunnelIfNeeded.
/**
* Tests if a network location is reachable. This is best effort and may give false
* not reachable.
*
* @param endpoint the endpoint to connect to
* @param tunnelHost the host used to tunnel
* @param tunnelType what type of tunnel should be established
* @param timeoutMs Open connection will wait for this timeoutMs in ms.
* @param retryCount In case of connection timeoutMs try retryCount times.
* @param retryIntervalMs the interval in ms to retryCount
* @param verifyCount In case of longer tunnel setup, try verify times to wait
* @return a <new_reachable_endpoint, tunnelProcess> pair.
* If the endpoint already reachable, then new_reachable_endpoint equals to original endpoint, and
* tunnelProcess is null.
* If no way to reach even through ssh tunneling,
* then both new_reachable_endpoint and tunnelProcess are null.
*/
private static Pair<InetSocketAddress, Process> establishSSHTunnelIfNeeded(InetSocketAddress endpoint, String tunnelHost, TunnelType tunnelType, int timeoutMs, int retryCount, int retryIntervalMs, int verifyCount) {
if (NetworkUtils.isLocationReachable(endpoint, timeoutMs, retryCount, retryIntervalMs)) {
// Already reachable, return original endpoint directly
return new Pair<InetSocketAddress, Process>(endpoint, null);
} else {
// Can not reach directly, trying to do ssh tunnel
int localFreePort = SysUtils.getFreePort();
InetSocketAddress newEndpoint = new InetSocketAddress(LOCAL_HOST, localFreePort);
LOG.log(Level.FINE, "Trying to opening up tunnel to {0} from {1}", new Object[] { endpoint.toString(), newEndpoint.toString() });
// Set up the tunnel process
final Process tunnelProcess;
switch(tunnelType) {
case PORT_FORWARD:
tunnelProcess = ShellUtils.establishSSHTunnelProcess(tunnelHost, localFreePort, endpoint.getHostString(), endpoint.getPort());
break;
case SOCKS_PROXY:
tunnelProcess = ShellUtils.establishSocksProxyProcess(tunnelHost, localFreePort);
break;
default:
throw new IllegalArgumentException("Unrecognized TunnelType passed: " + tunnelType);
}
// Verify whether the tunnel process is working fine.
if (tunnelProcess != null && tunnelProcess.isAlive() && NetworkUtils.isLocationReachable(newEndpoint, timeoutMs, verifyCount, retryIntervalMs)) {
java.lang.Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
tunnelProcess.destroy();
}
});
// Can reach the destination via ssh tunnel
return new Pair<InetSocketAddress, Process>(newEndpoint, tunnelProcess);
}
LOG.log(Level.FINE, "Failed to opening up tunnel to {0} from {1}. Releasing process..", new Object[] { endpoint, newEndpoint });
tunnelProcess.destroy();
}
// No way to reach the destination. Return null.
return new Pair<InetSocketAddress, Process>(null, null);
}
Aggregations