Search in sources :

Example 11 with Pair

use of com.twitter.heron.common.basics.Pair in project incubator-heron by apache.

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 timeout Open connection will wait for this timeout in ms.
 * @param retryCount In case of connection timeout try retryCount times.
 * @param retryInterval 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, Duration timeout, int retryCount, Duration retryInterval, int verifyCount) {
    if (NetworkUtils.isLocationReachable(endpoint, timeout, retryCount, retryInterval)) {
        // 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, timeout, verifyCount, retryInterval)) {
            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);
}
Also used : InetSocketAddress(java.net.InetSocketAddress) Pair(com.twitter.heron.common.basics.Pair)

Example 12 with Pair

use of com.twitter.heron.common.basics.Pair in project incubator-heron by apache.

the class ZkUtilsTest method testSetupZkTunnel.

/**
 * Test setupZkTunnel
 */
@Test
public void testSetupZkTunnel() throws Exception {
    String host0 = "host0";
    int port0 = 12;
    InetSocketAddress address0 = NetworkUtils.getInetSocketAddress(String.format("%s:%d", host0, port0));
    String host1 = "host1";
    int port1 = 13;
    InetSocketAddress address1 = NetworkUtils.getInetSocketAddress(String.format("%s:%d", host1, port1));
    String host2 = "host2";
    int port2 = 9049;
    InetSocketAddress address2 = NetworkUtils.getInetSocketAddress(String.format("%s:%d", host2, port2));
    String tunnelHost = "tunnelHost";
    int tunnelPort = 9519;
    InetSocketAddress tunnelAddress = NetworkUtils.getInetSocketAddress(String.format("%s:%d", tunnelHost, tunnelPort));
    // Original connection String
    String connectionString = String.format("%s:%d, %s:%d,  %s:%d   ", host0, port0, host1, port1, host2, port2);
    Config config = mock(Config.class);
    when(config.getStringValue(Key.STATEMGR_CONNECTION_STRING)).thenReturn(connectionString);
    NetworkUtils.TunnelConfig tunnelConfig = NetworkUtils.TunnelConfig.build(config, NetworkUtils.HeronSystem.STATE_MANAGER);
    Process process = mock(Process.class);
    // Mock the invocation of establishSSHTunnelIfNeeded
    // address0 and address1 are directly reachable
    // address2 are reachable after tunneling
    PowerMockito.spy(NetworkUtils.class);
    PowerMockito.doReturn(new Pair<>(address0, process)).when(NetworkUtils.class, "establishSSHTunnelIfNeeded", eq(address0), anyString(), any(NetworkUtils.TunnelType.class), any(Duration.class), anyInt(), any(Duration.class), anyInt());
    PowerMockito.doReturn(new Pair<>(address1, process)).when(NetworkUtils.class, "establishSSHTunnelIfNeeded", eq(address1), anyString(), any(NetworkUtils.TunnelType.class), any(Duration.class), anyInt(), any(Duration.class), anyInt());
    PowerMockito.doReturn(new Pair<>(tunnelAddress, process)).when(NetworkUtils.class, "establishSSHTunnelIfNeeded", eq(address2), anyString(), any(NetworkUtils.TunnelType.class), any(Duration.class), anyInt(), any(Duration.class), anyInt());
    Pair<String, List<Process>> ret = ZkUtils.setupZkTunnel(config, tunnelConfig);
    // Assert with expected results
    String expectedConnectionString = String.format("%s,%s,%s", address0.toString(), address1.toString(), tunnelAddress.toString());
    assertEquals(expectedConnectionString, ret.first);
    assertEquals(3, ret.second.size());
    for (Process p : ret.second) {
        assertEquals(process, p);
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) Config(com.twitter.heron.spi.common.Config) NetworkUtils(com.twitter.heron.spi.utils.NetworkUtils) Duration(java.time.Duration) List(java.util.List) Mockito.anyString(org.mockito.Mockito.anyString) Pair(com.twitter.heron.common.basics.Pair) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 13 with Pair

use of com.twitter.heron.common.basics.Pair in project heron by twitter.

the class CommonPackingTests method testScaleDownOneComponentRemoveContainer.

/**
   * Test the scenario where scaling down removes instances from containers that are most imbalanced
   * (i.e., tending towards homogeneity) first. If there is a tie (e.g. AABB, AB), chooses from the
   * container with the fewest instances, to favor ultimately removing  containers. If there is
   * still a tie, favor removing from higher numbered containers
   */
@Test
public void testScaleDownOneComponentRemoveContainer() 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<>(3, new InstanceId(B, 4, 1)), new Pair<>(3, new InstanceId(B, 5, 2)), new Pair<>(4, new InstanceId(B, 6, 3)), new Pair<>(4, new InstanceId(B, 7, 4)) };
    Map<String, Integer> componentChanges = new HashMap<>();
    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<>(3, new InstanceId(B, 4, 1)), new Pair<>(3, new InstanceId(B, 5, 2)) };
    doScaleDownTest(initialComponentInstances, componentChanges, expectedComponentInstances);
}
Also used : InstanceId(com.twitter.heron.spi.packing.InstanceId) HashMap(java.util.HashMap) Pair(com.twitter.heron.common.basics.Pair) Test(org.junit.Test)

Example 14 with Pair

use of com.twitter.heron.common.basics.Pair in project heron by twitter.

the class PackingPlanBuilderTest method testRemoveFromPackingPlan.

@Test
public void testRemoveFromPackingPlan() throws ResourceExceededException {
    PackingPlan plan = doCreatePackingPlanTest(testContainerInstances);
    @SuppressWarnings({ "unchecked", "rawtypes" }) Pair<Integer, String>[] removed = new Pair[] { new Pair<>(1, "componentA"), new Pair<>(3, "componentA") };
    PackingPlan updatedPlan = PackingTestHelper.removeFromTestPackingPlan(TOPOLOGY_ID, plan, removed, 0);
    @SuppressWarnings({ "unchecked", "rawtypes" }) Pair<Integer, InstanceId>[] expected = new Pair[] { new Pair<>(3, new InstanceId("componentB", 3, 0)) };
    AssertPacking.assertPackingPlan(TOPOLOGY_ID, expected, updatedPlan);
}
Also used : InstanceId(com.twitter.heron.spi.packing.InstanceId) PackingPlan(com.twitter.heron.spi.packing.PackingPlan) Pair(com.twitter.heron.common.basics.Pair) Test(org.junit.Test)

Example 15 with Pair

use of com.twitter.heron.common.basics.Pair in project heron by twitter.

the class PackingPlanBuilderTest method testExceededCapacityAddingToPackingPlan.

@Test(expected = ResourceExceededException.class)
public void testExceededCapacityAddingToPackingPlan() throws ResourceExceededException {
    PackingPlan plan = doCreatePackingPlanTest(testContainerInstances);
    @SuppressWarnings({ "unchecked", "rawtypes" }) Pair<Integer, InstanceId>[] added = new Pair[] { new Pair<>(3, new InstanceId("componentB", 4, 1)) };
    PackingTestHelper.addToTestPackingPlan(TOPOLOGY_ID, plan, PackingTestHelper.toContainerIdComponentNames(added), 0);
}
Also used : InstanceId(com.twitter.heron.spi.packing.InstanceId) PackingPlan(com.twitter.heron.spi.packing.PackingPlan) Pair(com.twitter.heron.common.basics.Pair) Test(org.junit.Test)

Aggregations

Pair (com.twitter.heron.common.basics.Pair)27 Test (org.junit.Test)18 InstanceId (com.twitter.heron.spi.packing.InstanceId)12 PackingPlan (com.twitter.heron.spi.packing.PackingPlan)12 HashMap (java.util.HashMap)7 InetSocketAddress (java.net.InetSocketAddress)6 Config (com.twitter.heron.spi.common.Config)5 ArrayList (java.util.ArrayList)4 ContainerPlan (com.twitter.heron.spi.packing.PackingPlan.ContainerPlan)2 NetworkUtils (com.twitter.heron.spi.utils.NetworkUtils)2 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 List (java.util.List)2 POST (javax.ws.rs.POST)2 Produces (javax.ws.rs.Produces)2 Before (org.junit.Before)2 Mockito.anyString (org.mockito.Mockito.anyString)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 SubmitDryRunResponse (com.twitter.heron.scheduler.dryrun.SubmitDryRunResponse)1 File (java.io.File)1