Search in sources :

Example 11 with HealthcheckOptions

use of com.hubspot.deploy.HealthcheckOptions in project Singularity by HubSpot.

the class SingularityHealthchecksTest method testMaxHealthcheckRetries.

@Test
public void testMaxHealthcheckRetries() {
    initRequest();
    final String deployId = "retry_test";
    HealthcheckOptions options = new HealthcheckOptionsBuilder("http://uri").setMaxRetries(Optional.of(2)).build();
    SingularityDeployBuilder db = new SingularityDeployBuilder(requestId, deployId).setHealthcheck(Optional.of(options));
    SingularityDeploy deploy = initDeploy(db, System.currentTimeMillis());
    deployChecker.checkDeploys();
    Assert.assertTrue(!deployManager.getDeployResult(requestId, deployId).isPresent());
    SingularityTask task = launchTask(request, deploy, System.currentTimeMillis(), 1, TaskState.TASK_RUNNING);
    deployChecker.checkDeploys();
    Assert.assertTrue(!deployManager.getDeployResult(requestId, deployId).isPresent());
    taskManager.saveHealthcheckResult(new SingularityTaskHealthcheckResult(Optional.of(503), Optional.of(1000L), System.currentTimeMillis(), Optional.<String>absent(), Optional.<String>absent(), task.getTaskId(), Optional.<Boolean>absent()));
    taskManager.saveHealthcheckResult(new SingularityTaskHealthcheckResult(Optional.of(503), Optional.of(1000L), System.currentTimeMillis() + 1, Optional.<String>absent(), Optional.<String>absent(), task.getTaskId(), Optional.<Boolean>absent()));
    deployChecker.checkDeploys();
    Assert.assertTrue(!deployManager.getDeployResult(requestId, deployId).isPresent());
    taskManager.saveHealthcheckResult(new SingularityTaskHealthcheckResult(Optional.of(503), Optional.of(1000L), System.currentTimeMillis() + 1, Optional.<String>absent(), Optional.<String>absent(), task.getTaskId(), Optional.<Boolean>absent()));
    deployChecker.checkDeploys();
    Assert.assertEquals(DeployState.FAILED, deployManager.getDeployResult(requestId, deployId).get().getDeployState());
}
Also used : SingularityTaskHealthcheckResult(com.hubspot.singularity.SingularityTaskHealthcheckResult) SingularityTask(com.hubspot.singularity.SingularityTask) HealthcheckOptions(com.hubspot.deploy.HealthcheckOptions) SingularityDeployBuilder(com.hubspot.singularity.SingularityDeployBuilder) HealthcheckOptionsBuilder(com.hubspot.deploy.HealthcheckOptionsBuilder) SingularityDeploy(com.hubspot.singularity.SingularityDeploy) Test(org.junit.Test)

Example 12 with HealthcheckOptions

use of com.hubspot.deploy.HealthcheckOptions in project Singularity by HubSpot.

the class SingularityHealthchecksTest method testHealthchecksSuccess.

@Test
public void testHealthchecksSuccess() {
    initRequest();
    final String deployId = "hc_test";
    HealthcheckOptions options = new HealthcheckOptionsBuilder("http://uri").setMaxRetries(Optional.of(2)).build();
    SingularityDeployBuilder db = new SingularityDeployBuilder(requestId, deployId).setHealthcheck(Optional.of(options));
    SingularityDeploy deploy = initDeploy(db, System.currentTimeMillis());
    deployChecker.checkDeploys();
    Assert.assertTrue(!deployManager.getDeployResult(requestId, deployId).isPresent());
    SingularityTask task = launchTask(request, deploy, System.currentTimeMillis(), 1, TaskState.TASK_RUNNING);
    deployChecker.checkDeploys();
    Assert.assertTrue(!deployManager.getDeployResult(requestId, deployId).isPresent());
    taskManager.saveHealthcheckResult(new SingularityTaskHealthcheckResult(Optional.of(503), Optional.of(1000L), System.currentTimeMillis(), Optional.<String>absent(), Optional.<String>absent(), task.getTaskId(), Optional.<Boolean>absent()));
    deployChecker.checkDeploys();
    Assert.assertTrue(!deployManager.getDeployResult(requestId, deployId).isPresent());
    taskManager.saveHealthcheckResult(new SingularityTaskHealthcheckResult(Optional.of(200), Optional.of(1000L), System.currentTimeMillis() + 1, Optional.<String>absent(), Optional.<String>absent(), task.getTaskId(), Optional.<Boolean>absent()));
    deployChecker.checkDeploys();
    Assert.assertEquals(DeployState.SUCCEEDED, deployManager.getDeployResult(requestId, deployId).get().getDeployState());
}
Also used : SingularityTaskHealthcheckResult(com.hubspot.singularity.SingularityTaskHealthcheckResult) SingularityTask(com.hubspot.singularity.SingularityTask) HealthcheckOptions(com.hubspot.deploy.HealthcheckOptions) SingularityDeployBuilder(com.hubspot.singularity.SingularityDeployBuilder) HealthcheckOptionsBuilder(com.hubspot.deploy.HealthcheckOptionsBuilder) SingularityDeploy(com.hubspot.singularity.SingularityDeploy) Test(org.junit.Test)

Example 13 with HealthcheckOptions

use of com.hubspot.deploy.HealthcheckOptions in project Singularity by HubSpot.

the class SingularityHealthchecksTest method testStartupDoesNotCountTowardsRetries.

@Test
public void testStartupDoesNotCountTowardsRetries() {
    initRequest();
    final String deployId = "retry_test";
    HealthcheckOptions options = new HealthcheckOptionsBuilder("http://uri").setMaxRetries(Optional.of(1)).build();
    SingularityDeployBuilder db = new SingularityDeployBuilder(requestId, deployId).setHealthcheck(Optional.of(options));
    SingularityDeploy deploy = initDeploy(db, System.currentTimeMillis());
    deployChecker.checkDeploys();
    Assert.assertTrue(!deployManager.getDeployResult(requestId, deployId).isPresent());
    SingularityTask task = launchTask(request, deploy, System.currentTimeMillis(), 1, TaskState.TASK_RUNNING);
    deployChecker.checkDeploys();
    Assert.assertTrue(!deployManager.getDeployResult(requestId, deployId).isPresent());
    taskManager.saveHealthcheckResult(new SingularityTaskHealthcheckResult(Optional.<Integer>absent(), Optional.of(1000L), System.currentTimeMillis(), Optional.<String>absent(), Optional.of("ConnectionRefused"), task.getTaskId(), Optional.of(true)));
    taskManager.saveHealthcheckResult(new SingularityTaskHealthcheckResult(Optional.<Integer>absent(), Optional.of(1000L), System.currentTimeMillis() + 1, Optional.<String>absent(), Optional.of("ConnectionRefused"), task.getTaskId(), Optional.of(true)));
    deployChecker.checkDeploys();
    Assert.assertTrue(!deployManager.getDeployResult(requestId, deployId).isPresent());
    taskManager.saveHealthcheckResult(new SingularityTaskHealthcheckResult(Optional.of(503), Optional.of(1000L), System.currentTimeMillis() + 1, Optional.<String>absent(), Optional.<String>absent(), task.getTaskId(), Optional.<Boolean>absent()));
    deployChecker.checkDeploys();
    Assert.assertTrue(!deployManager.getDeployResult(requestId, deployId).isPresent());
    taskManager.saveHealthcheckResult(new SingularityTaskHealthcheckResult(Optional.of(200), Optional.of(1000L), System.currentTimeMillis() + 1, Optional.<String>absent(), Optional.<String>absent(), task.getTaskId(), Optional.<Boolean>absent()));
    deployChecker.checkDeploys();
    Assert.assertEquals(DeployState.SUCCEEDED, deployManager.getDeployResult(requestId, deployId).get().getDeployState());
}
Also used : SingularityTaskHealthcheckResult(com.hubspot.singularity.SingularityTaskHealthcheckResult) SingularityTask(com.hubspot.singularity.SingularityTask) HealthcheckOptions(com.hubspot.deploy.HealthcheckOptions) SingularityDeployBuilder(com.hubspot.singularity.SingularityDeployBuilder) HealthcheckOptionsBuilder(com.hubspot.deploy.HealthcheckOptionsBuilder) SingularityDeploy(com.hubspot.singularity.SingularityDeploy) Test(org.junit.Test)

Example 14 with HealthcheckOptions

use of com.hubspot.deploy.HealthcheckOptions in project Singularity by HubSpot.

the class SingularityValidator method checkDeploy.

public SingularityDeploy checkDeploy(SingularityRequest request, SingularityDeploy deploy, List<SingularityTaskId> activeTasks, List<SingularityPendingTaskId> pendingTasks) {
    checkNotNull(request, "request is null");
    checkNotNull(deploy, "deploy is null");
    String deployId = deploy.getId();
    if (deployId == null) {
        checkBadRequest(createDeployIds, "Id must not be null");
        SingularityDeployBuilder builder = deploy.toBuilder();
        builder.setId(createUniqueDeployId());
        deploy = builder.build();
        deployId = deploy.getId();
    }
    checkBadRequest(deployId != null && !DEPLOY_ID_ILLEGAL_PATTERN.matcher(deployId).find(), "Id cannot be null or contain characters other than [a-zA-Z0-9_.]");
    checkBadRequest(deployId.length() <= maxDeployIdSize, "Deploy id must be %s characters or less, it is %s (%s)", maxDeployIdSize, deployId.length(), deployId);
    checkBadRequest(deploy.getRequestId() != null && deploy.getRequestId().equals(request.getId()), "Deploy id must match request id");
    if (request.isLoadBalanced()) {
        checkBadRequest(deploy.getServiceBasePath().isPresent(), "Deploy for loadBalanced request must include serviceBasePath");
        checkBadRequest(deploy.getLoadBalancerGroups().isPresent() && !deploy.getLoadBalancerGroups().get().isEmpty(), "Deploy for a loadBalanced request must include at least one load balacner group");
    }
    checkForIllegalResources(request, deploy);
    if (deploy.getResources().isPresent()) {
        if (deploy.getHealthcheck().isPresent()) {
            HealthcheckOptions healthcheck = deploy.getHealthcheck().get();
            checkBadRequest(!(healthcheck.getPortIndex().isPresent() && healthcheck.getPortNumber().isPresent()), "Can only specify one of portIndex or portNumber for healthchecks");
            if (healthcheck.getPortIndex().isPresent()) {
                checkBadRequest(healthcheck.getPortIndex().get() >= 0, "healthcheckPortIndex cannot be negative");
                checkBadRequest(deploy.getResources().get().getNumPorts() > healthcheck.getPortIndex().get(), String.format("Must request %s ports for healthcheckPortIndex %s, only requested %s", healthcheck.getPortIndex().get() + 1, healthcheck.getPortIndex().get(), deploy.getResources().get().getNumPorts()));
            }
        }
        if (deploy.getLoadBalancerPortIndex().isPresent()) {
            checkBadRequest(deploy.getLoadBalancerPortIndex().get() >= 0, "loadBalancerPortIndex must be greater than 0");
            checkBadRequest(deploy.getResources().get().getNumPorts() > deploy.getLoadBalancerPortIndex().get(), String.format("Must request %s ports for loadBalancerPortIndex %s, only requested %s", deploy.getLoadBalancerPortIndex().get() + 1, deploy.getLoadBalancerPortIndex().get(), deploy.getResources().get().getNumPorts()));
        }
    }
    if (deploy.getHealthcheck().isPresent()) {
        checkBadRequest(!Strings.isNullOrEmpty(deploy.getHealthcheck().get().getUri()), "Must specify a uri when specifying health check parameters");
        if (!deploy.getResources().isPresent() || deploy.getResources().get().getNumPorts() == 0) {
            checkBadRequest(deploy.getHealthcheck().get().getPortNumber().isPresent(), "Either an explicit port number, or port resources and port index must be specified to run healthchecks against a uri");
        }
        if (maxTotalHealthcheckTimeoutSeconds.isPresent()) {
            HealthcheckOptions options = deploy.getHealthcheck().get();
            int intervalSeconds = options.getIntervalSeconds().or(defaultHealthcheckIntervalSeconds);
            int httpTimeoutSeconds = options.getResponseTimeoutSeconds().or(defaultHealthcheckResponseTimeoutSeconds);
            int startupTime = options.getStartupTimeoutSeconds().or(defaultHealthcheckStartupTimeoutSeconds);
            int attempts = options.getMaxRetries().or(defaultHealthcehckMaxRetries) + 1;
            int totalHealthCheckTime = startupTime + ((httpTimeoutSeconds + intervalSeconds) * attempts);
            checkBadRequest(totalHealthCheckTime < maxTotalHealthcheckTimeoutSeconds.get(), String.format("Max healthcheck time cannot be greater than %s, (was startup timeout: %s, interval: %s, attempts: %s)", maxTotalHealthcheckTimeoutSeconds.get(), startupTime, intervalSeconds, attempts));
        }
        if (deploy.getHealthcheck().get().getStartupDelaySeconds().isPresent()) {
            int startUpDelay = deploy.getHealthcheck().get().getStartupDelaySeconds().get();
            checkBadRequest(startUpDelay < defaultKillHealthcheckAfterSeconds, String.format("Health check startup delay time must be less than max health check run time %s (was %s)", defaultKillHealthcheckAfterSeconds, startUpDelay));
        }
    }
    checkBadRequest(deploy.getCommand().isPresent() && !deploy.getExecutorData().isPresent() || deploy.getExecutorData().isPresent() && deploy.getCustomExecutorCmd().isPresent() && !deploy.getCommand().isPresent() || deploy.getContainerInfo().isPresent(), "If not using custom executor, specify a command or containerInfo. If using custom executor, specify executorData and customExecutorCmd and no command.");
    checkBadRequest(!deploy.getContainerInfo().isPresent() || deploy.getContainerInfo().get().getType() != null, "Container type must not be null");
    if (deploy.getLabels().isPresent() && deploy.getMesosTaskLabels().isPresent()) {
        List<SingularityMesosTaskLabel> deprecatedLabels = SingularityMesosTaskLabel.labelsFromMap(deploy.getLabels().get());
        checkBadRequest(deprecatedLabels.containsAll(deploy.getMesosLabels().get()) && deploy.getMesosLabels().get().containsAll(deprecatedLabels), "Can only specify one of 'labels' or 'mesosLabels");
    }
    if (deploy.getTaskLabels().isPresent() && deploy.getMesosTaskLabels().isPresent()) {
        for (Map.Entry<Integer, Map<String, String>> entry : deploy.getTaskLabels().get().entrySet()) {
            List<SingularityMesosTaskLabel> deprecatedLabels = SingularityMesosTaskLabel.labelsFromMap(entry.getValue());
            checkBadRequest(deploy.getMesosTaskLabels().get().containsKey(entry.getKey()) && deprecatedLabels.containsAll(deploy.getMesosTaskLabels().get().get(entry.getKey())) && deploy.getMesosTaskLabels().get().get(entry.getKey()).containsAll(deprecatedLabels), "Can only specify one of 'taskLabels' or 'mesosTaskLabels");
        }
    }
    if (deploy.getContainerInfo().isPresent()) {
        SingularityContainerInfo containerInfo = deploy.getContainerInfo().get();
        checkBadRequest(containerInfo.getType() != null, "container type may not be null");
        if (containerInfo.getVolumes().isPresent() && !containerInfo.getVolumes().get().isEmpty()) {
            for (SingularityVolume volume : containerInfo.getVolumes().get()) {
                checkBadRequest(volume.getContainerPath() != null, "volume containerPath may not be null");
            }
        }
        if (deploy.getContainerInfo().get().getType() == SingularityContainerType.DOCKER) {
            checkDocker(deploy);
        }
    }
    checkBadRequest(deployHistoryHelper.isDeployIdAvailable(request.getId(), deployId), "Can not deploy a deploy that has already been deployed");
    if (deploy.getRunImmediately().isPresent()) {
        deploy = checkImmediateRunDeploy(request, deploy, deploy.getRunImmediately().get(), activeTasks, pendingTasks);
    }
    if (request.isDeployable()) {
        checkRequestForPriorityFreeze(request);
    }
    return deploy;
}
Also used : SingularityContainerInfo(com.hubspot.mesos.SingularityContainerInfo) HealthcheckOptions(com.hubspot.deploy.HealthcheckOptions) SingularityVolume(com.hubspot.mesos.SingularityVolume) SingularityDeployBuilder(com.hubspot.singularity.SingularityDeployBuilder) SingularityMesosTaskLabel(com.hubspot.mesos.SingularityMesosTaskLabel) Map(java.util.Map)

Aggregations

HealthcheckOptions (com.hubspot.deploy.HealthcheckOptions)14 HealthcheckOptionsBuilder (com.hubspot.deploy.HealthcheckOptionsBuilder)11 Test (org.junit.Test)11 SingularityDeployBuilder (com.hubspot.singularity.SingularityDeployBuilder)10 SingularityDeploy (com.hubspot.singularity.SingularityDeploy)9 SingularityTask (com.hubspot.singularity.SingularityTask)9 SingularityTaskHealthcheckResult (com.hubspot.singularity.SingularityTaskHealthcheckResult)7 Resources (com.hubspot.mesos.Resources)2 SingularityRequest (com.hubspot.singularity.SingularityRequest)2 SingularityRequestBuilder (com.hubspot.singularity.SingularityRequestBuilder)2 SingularityTaskId (com.hubspot.singularity.SingularityTaskId)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 SingularityContainerInfo (com.hubspot.mesos.SingularityContainerInfo)1 SingularityMesosTaskLabel (com.hubspot.mesos.SingularityMesosTaskLabel)1 SingularityVolume (com.hubspot.mesos.SingularityVolume)1 HealthcheckProtocol (com.hubspot.singularity.HealthcheckProtocol)1 Map (java.util.Map)1