Search in sources :

Example 1 with TopologyRuntimeManagementException

use of org.apache.heron.scheduler.TopologyRuntimeManagementException in project heron by twitter.

the class V1Controller method deleteService.

/**
 * Deletes the headless <code>Service</code> for a <code>topology</code>'s <code>Executors</code>
 * and <code>Manager</code> using the <code>topology</code>'s name.
 */
void deleteService() {
    try (Response response = coreClient.deleteNamespacedServiceCall(getTopologyName(), getNamespace(), null, null, 0, null, KubernetesConstants.DELETE_OPTIONS_PROPAGATION_POLICY, null, null).execute()) {
        if (!response.isSuccessful()) {
            if (response.code() == HTTP_NOT_FOUND) {
                LOG.log(Level.WARNING, "Deleting non-existent Kubernetes headless service for Topology: " + getTopologyName());
                return;
            }
            LOG.log(Level.SEVERE, "Error when deleting the Service of the job [" + getTopologyName() + "] in namespace [" + getNamespace() + "]");
            LOG.log(Level.SEVERE, "Error killing topology message:" + response.message());
            KubernetesUtils.logResponseBodyIfPresent(LOG, response);
            throw new TopologyRuntimeManagementException(KubernetesUtils.errorMessageFromResponse(response));
        }
    } catch (ApiException e) {
        if (e.getCode() == HTTP_NOT_FOUND) {
            LOG.log(Level.WARNING, "Tried to delete a non-existent Kubernetes service for Topology: " + getTopologyName());
            return;
        }
        throw new TopologyRuntimeManagementException("Error deleting topology [" + getTopologyName() + "] Kubernetes service", e);
    } catch (IOException e) {
        throw new TopologyRuntimeManagementException("Error deleting topology [" + getTopologyName() + "] Kubernetes service", e);
    }
    LOG.log(Level.INFO, "Headless Service for the Job [" + getTopologyName() + "] in namespace [" + getNamespace() + "] is deleted.");
}
Also used : Response(okhttp3.Response) TopologyRuntimeManagementException(org.apache.heron.scheduler.TopologyRuntimeManagementException) IOException(java.io.IOException) ApiException(io.kubernetes.client.openapi.ApiException)

Example 2 with TopologyRuntimeManagementException

use of org.apache.heron.scheduler.TopologyRuntimeManagementException in project heron by twitter.

the class V1Controller method addContainers.

/**
 * Adds a specified number of Pods to a <code>topology</code>'s <code>Executors</code>.
 * @param containersToAdd Set of containers to be added.
 * @return The passed in <code>Packing Plan</code>.
 */
@Override
public Set<PackingPlan.ContainerPlan> addContainers(Set<PackingPlan.ContainerPlan> containersToAdd) {
    final V1StatefulSet statefulSet;
    try {
        statefulSet = getStatefulSet();
    } catch (ApiException ae) {
        final String message = ae.getMessage() + "\ndetails:" + ae.getResponseBody();
        throw new TopologyRuntimeManagementException(message, ae);
    }
    final V1StatefulSetSpec v1StatefulSet = Objects.requireNonNull(statefulSet.getSpec());
    final int currentContainerCount = Objects.requireNonNull(v1StatefulSet.getReplicas());
    final int newContainerCount = currentContainerCount + containersToAdd.size();
    try {
        patchStatefulSetReplicas(newContainerCount);
    } catch (ApiException ae) {
        throw new TopologyRuntimeManagementException(ae.getMessage() + "\ndetails\n" + ae.getResponseBody());
    }
    return containersToAdd;
}
Also used : TopologyRuntimeManagementException(org.apache.heron.scheduler.TopologyRuntimeManagementException) V1StatefulSet(io.kubernetes.client.openapi.models.V1StatefulSet) V1StatefulSetSpec(io.kubernetes.client.openapi.models.V1StatefulSetSpec) ApiException(io.kubernetes.client.openapi.ApiException)

Example 3 with TopologyRuntimeManagementException

use of org.apache.heron.scheduler.TopologyRuntimeManagementException in project heron by twitter.

the class V1Controller method removePersistentVolumeClaims.

/**
 * Removes all Persistent Volume Claims associated with a specific topology, if they exist.
 * It looks for the following:
 * metadata:
 *   labels:
 *     topology: <code>topology-name</code>
 *     onDemand: <code>true</code>
 */
private void removePersistentVolumeClaims() {
    final String name = getTopologyName();
    final StringBuilder selectorLabel = new StringBuilder();
    // Generate selector label.
    for (Map.Entry<String, String> label : getPersistentVolumeClaimLabels(name).entrySet()) {
        if (selectorLabel.length() != 0) {
            selectorLabel.append(",");
        }
        selectorLabel.append(label.getKey()).append("=").append(label.getValue());
    }
    // Remove all dynamically backed Persistent Volume Claims.
    try {
        V1Status status = coreClient.deleteCollectionNamespacedPersistentVolumeClaim(getNamespace(), null, null, null, null, null, selectorLabel.toString(), null, null, null, null, null, null, null);
        LOG.log(Level.INFO, String.format("Removing automatically generated Persistent Volume Claims for `%s`:%n%s", name, status.getMessage()));
    } catch (ApiException e) {
        final String message = String.format("Failed to connect to K8s cluster to delete Persistent " + "Volume Claims for topology `%s`. A manual clean-up is required.%n%s", name, e.getMessage());
        LOG.log(Level.WARNING, message);
        throw new TopologyRuntimeManagementException(message);
    }
}
Also used : TopologyRuntimeManagementException(org.apache.heron.scheduler.TopologyRuntimeManagementException) V1Status(io.kubernetes.client.openapi.models.V1Status) Map(java.util.Map) HashMap(java.util.HashMap) V1ConfigMap(io.kubernetes.client.openapi.models.V1ConfigMap) ApiException(io.kubernetes.client.openapi.ApiException)

Example 4 with TopologyRuntimeManagementException

use of org.apache.heron.scheduler.TopologyRuntimeManagementException in project heron by twitter.

the class KubernetesSchedulerTest method testAddContainers.

@Test
public void testAddContainers() throws Exception {
    KubernetesController controller = Mockito.mock(KubernetesController.class);
    Mockito.doReturn(controller).when(scheduler).getController();
    scheduler.initialize(config, mockRuntime);
    Set<PackingPlan.ContainerPlan> containers = new HashSet<>();
    // Failure to deploy a container
    Mockito.doThrow(new TopologyRuntimeManagementException("")).when(controller).addContainers(Mockito.anySetOf(PackingPlan.ContainerPlan.class));
    expectedException.expect(TopologyRuntimeManagementException.class);
    scheduler.addContainers(containers);
    // Successful deployment
    Mockito.doNothing().when(controller).addContainers(Mockito.anySetOf(PackingPlan.ContainerPlan.class));
    scheduler.addContainers(containers);
}
Also used : TopologyRuntimeManagementException(org.apache.heron.scheduler.TopologyRuntimeManagementException) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 5 with TopologyRuntimeManagementException

use of org.apache.heron.scheduler.TopologyRuntimeManagementException in project heron by twitter.

the class KubernetesSchedulerTest method testRemoveContainers.

@Test
public void testRemoveContainers() throws Exception {
    KubernetesController controller = Mockito.mock(KubernetesController.class);
    Mockito.doReturn(controller).when(scheduler).getController();
    scheduler.initialize(config, mockRuntime);
    Set<PackingPlan.ContainerPlan> containers = new HashSet<>();
    containers.add(PackingTestUtils.testContainerPlan(0));
    // Failure to remove container
    Mockito.doThrow(new TopologyRuntimeManagementException("")).when(controller).removeContainers(Mockito.anySetOf(PackingPlan.ContainerPlan.class));
    expectedException.expect(TopologyRuntimeManagementException.class);
    scheduler.removeContainers(containers);
    // Successful removal
    Mockito.doNothing().when(controller).removeContainers(Mockito.anySetOf(PackingPlan.ContainerPlan.class));
    scheduler.removeContainers(containers);
}
Also used : TopologyRuntimeManagementException(org.apache.heron.scheduler.TopologyRuntimeManagementException) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

TopologyRuntimeManagementException (org.apache.heron.scheduler.TopologyRuntimeManagementException)8 ApiException (io.kubernetes.client.openapi.ApiException)5 V1ConfigMap (io.kubernetes.client.openapi.models.V1ConfigMap)2 V1StatefulSet (io.kubernetes.client.openapi.models.V1StatefulSet)2 V1StatefulSetSpec (io.kubernetes.client.openapi.models.V1StatefulSetSpec)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 Response (okhttp3.Response)2 Test (org.junit.Test)2 V1EnvVar (io.kubernetes.client.openapi.models.V1EnvVar)1 V1EnvVarSource (io.kubernetes.client.openapi.models.V1EnvVarSource)1 V1SecretKeySelector (io.kubernetes.client.openapi.models.V1SecretKeySelector)1 V1Status (io.kubernetes.client.openapi.models.V1Status)1 Config (org.apache.heron.spi.common.Config)1