Search in sources :

Example 11 with Pair

use of org.apache.heron.common.basics.Pair in project heron by twitter.

the class KubernetesContextTest method createVolumeHostPath.

/**
 * Create test cases for <code>Host Path</code>.
 * @param testCases Test case container.
 *                  Input: [0] Config, [1] Boolean to indicate Manager/Executor.
 *                  Output: <code>Map<String, Map<VolumeConfigKeys, String></code>
 * @param isExecutor Boolean to indicate Manager/Executor test case generation.
 */
private void createVolumeHostPath(List<TestTuple<Pair<Config, Boolean>, Map<String, Map<VolumeConfigKeys, String>>>> testCases, boolean isExecutor) {
    final String volumeNameValid = "volume-name-valid";
    final String passingValue = "should-pass";
    final String processName = isExecutor ? KubernetesConstants.EXECUTOR_NAME : KubernetesConstants.MANAGER_NAME;
    final String keyPattern = String.format(KubernetesContext.KUBERNETES_VOLUME_HOSTPATH_PREFIX + "%%s.%%s", processName);
    // With type.
    final Map<String, Map<VolumeConfigKeys, String>> expectedWithType = ImmutableMap.of(volumeNameValid, new HashMap<VolumeConfigKeys, String>() {

        {
            put(VolumeConfigKeys.type, "DirectoryOrCreate");
            put(VolumeConfigKeys.path, passingValue);
            put(VolumeConfigKeys.pathOnHost, passingValue);
            put(VolumeConfigKeys.subPath, passingValue);
            put(VolumeConfigKeys.readOnly, passingValue);
        }
    });
    final Config configWithType = Config.newBuilder().put(String.format(keyPattern, volumeNameValid, "type"), "DirectoryOrCreate").put(String.format(keyPattern, volumeNameValid, "pathOnHost"), passingValue).put(String.format(keyPattern, volumeNameValid, "path"), passingValue).put(String.format(keyPattern, volumeNameValid, "subPath"), passingValue).put(String.format(keyPattern, volumeNameValid, "readOnly"), passingValue).build();
    testCases.add(new TestTuple<>(processName + ": 'hostPath' with 'type'", new Pair<>(configWithType, isExecutor), expectedWithType));
    // Without type.
    final Map<String, Map<VolumeConfigKeys, String>> expectedWithoutType = ImmutableMap.of(volumeNameValid, new HashMap<VolumeConfigKeys, String>() {

        {
            put(VolumeConfigKeys.pathOnHost, passingValue);
            put(VolumeConfigKeys.path, passingValue);
            put(VolumeConfigKeys.subPath, passingValue);
            put(VolumeConfigKeys.readOnly, passingValue);
        }
    });
    final Config configWithoutType = Config.newBuilder().put(String.format(keyPattern, volumeNameValid, "pathOnHost"), passingValue).put(String.format(keyPattern, volumeNameValid, "path"), passingValue).put(String.format(keyPattern, volumeNameValid, "subPath"), passingValue).put(String.format(keyPattern, volumeNameValid, "readOnly"), passingValue).build();
    testCases.add(new TestTuple<>(processName + ": 'hostPath' without 'type'", new Pair<>(configWithoutType, isExecutor), expectedWithoutType));
    // Ignored.
    final Config configIgnored = Config.newBuilder().put(String.format(keyPattern, volumeNameValid, "type"), "BlockDevice").put(String.format(keyPattern, volumeNameValid, "pathOnHost"), passingValue).put(String.format(keyPattern, volumeNameValid, "path"), passingValue).put(String.format(keyPattern, volumeNameValid, "subPath"), passingValue).put(String.format(keyPattern, volumeNameValid, "readOnly"), passingValue).build();
    testCases.add(new TestTuple<>(processName + ": 'hostPath' ignored", new Pair<>(configIgnored, !isExecutor), new HashMap<>()));
}
Also used : VolumeConfigKeys(org.apache.heron.scheduler.kubernetes.KubernetesConstants.VolumeConfigKeys) HashMap(java.util.HashMap) Config(org.apache.heron.spi.common.Config) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) Map(java.util.Map) Pair(org.apache.heron.common.basics.Pair)

Example 12 with Pair

use of org.apache.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);
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) Pair(org.apache.heron.common.basics.Pair)

Example 13 with Pair

use of org.apache.heron.common.basics.Pair in project heron by twitter.

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(org.apache.heron.spi.common.Config) NetworkUtils(org.apache.heron.spi.utils.NetworkUtils) Duration(java.time.Duration) List(java.util.List) Mockito.anyString(org.mockito.Mockito.anyString) Pair(org.apache.heron.common.basics.Pair) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 14 with Pair

use of org.apache.heron.common.basics.Pair in project heron by twitter.

the class TopologyResource method restart.

@POST
@Path("/{cluster}/{role}/{environment}/{name}/restart")
@Produces(MediaType.APPLICATION_JSON)
@SuppressWarnings("IllegalCatch")
public Response restart(@PathParam("cluster") final String cluster, @PathParam("role") final String role, @PathParam("environment") final String environment, @PathParam("name") final String name, @DefaultValue("-1") @FormParam("container_id") final int containerId) {
    try {
        final List<Pair<String, Object>> keyValues = new ArrayList<>(Arrays.asList(Pair.create(Key.CLUSTER.value(), cluster), Pair.create(Key.ROLE.value(), role), Pair.create(Key.ENVIRON.value(), environment), Pair.create(Key.TOPOLOGY_NAME.value(), name), Pair.create(Key.TOPOLOGY_CONTAINER_ID.value(), containerId)));
        final Config config = createConfig(keyValues);
        getActionFactory().createRuntimeAction(config, ActionType.RESTART).execute();
        return Response.ok().type(MediaType.APPLICATION_JSON).entity(Utils.createMessage(String.format("%s restarted", name))).build();
    } catch (Exception ex) {
        LOG.error("error restarting topology {}", name, ex);
        return Response.serverError().type(MediaType.APPLICATION_JSON).entity(Utils.createMessage(ex.getMessage())).build();
    }
}
Also used : Config(org.apache.heron.spi.common.Config) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Pair(org.apache.heron.common.basics.Pair) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces)

Example 15 with Pair

use of org.apache.heron.common.basics.Pair in project heron by twitter.

the class TopologyResource method updateContainerNumber.

protected Response updateContainerNumber(String cluster, String role, String environment, String name, MultivaluedMap<String, String> params, String containerNumber) {
    final List<Pair<String, Object>> keyValues = new ArrayList<>(Arrays.asList(Pair.create(Key.CLUSTER.value(), cluster), Pair.create(Key.ROLE.value(), role), Pair.create(Key.ENVIRON.value(), environment), Pair.create(Key.TOPOLOGY_NAME.value(), name), Pair.create(Keys.PARAM_CONTAINER_NUMBER, containerNumber)));
    // has a dry run been requested?
    if (params.containsKey(PARAM_DRY_RUN)) {
        keyValues.add(Pair.create(Key.DRY_RUN.value(), Boolean.TRUE));
    }
    final Set<Pair<String, Object>> overrides = getUpdateOverrides(params);
    // apply overrides if they exists
    if (!overrides.isEmpty()) {
        keyValues.addAll(overrides);
    }
    final Config config = createConfig(keyValues);
    getActionFactory().createRuntimeAction(config, ActionType.UPDATE).execute();
    return Response.ok().type(MediaType.APPLICATION_JSON).entity(Utils.createMessage(String.format("%s updated", name))).build();
}
Also used : Config(org.apache.heron.spi.common.Config) ArrayList(java.util.ArrayList) Pair(org.apache.heron.common.basics.Pair)

Aggregations

Pair (org.apache.heron.common.basics.Pair)37 Test (org.junit.Test)19 Config (org.apache.heron.spi.common.Config)15 HashMap (java.util.HashMap)13 InstanceId (org.apache.heron.spi.packing.InstanceId)10 LinkedList (java.util.LinkedList)8 Map (java.util.Map)7 VolumeConfigKeys (org.apache.heron.scheduler.kubernetes.KubernetesConstants.VolumeConfigKeys)7 Matchers.anyString (org.mockito.Matchers.anyString)7 ImmutableMap (com.google.common.collect.ImmutableMap)6 TestTuple (org.apache.heron.scheduler.kubernetes.KubernetesUtils.TestTuple)6 PackingPlan (org.apache.heron.spi.packing.PackingPlan)6 V1ConfigMap (io.kubernetes.client.openapi.models.V1ConfigMap)5 ArrayList (java.util.ArrayList)4 V1ConfigMapBuilder (io.kubernetes.client.openapi.models.V1ConfigMapBuilder)3 V1VolumeMount (io.kubernetes.client.openapi.models.V1VolumeMount)3 V1VolumeMountBuilder (io.kubernetes.client.openapi.models.V1VolumeMountBuilder)3 InetSocketAddress (java.net.InetSocketAddress)3 TopologySubmissionException (org.apache.heron.scheduler.TopologySubmissionException)3 V1Volume (io.kubernetes.client.openapi.models.V1Volume)2