Search in sources :

Example 6 with RunInstance

use of com.epam.pipeline.entity.pipeline.RunInstance in project cloud-pipeline by epam.

the class PipelineRunLoaderTest method shouldLoadPipelineRunTest.

@Test
void shouldLoadPipelineRunTest() throws EntityNotFoundException {
    RunInstance instance = new RunInstance();
    instance.setNodeType("type");
    instance.setAwsRegionId(TEST_REGION);
    instance.setSpot(true);
    instance.setNodeDisk(NODE_DISK);
    instance.setNodeId("id");
    instance.setNodeImage(TEST_PATH);
    instance.setNodeName(TEST_NAME);
    RunStatus runStatus = new RunStatus();
    runStatus.setRunId(1L);
    runStatus.setStatus(TaskStatus.SUCCESS);
    PipelineRunParameter parameter = new PipelineRunParameter();
    parameter.setName(TEST_NAME);
    parameter.setValue(TEST_VALUE);
    PipelineRun expectedPipelineRun = new PipelineRun();
    expectedPipelineRun.setId(1L);
    expectedPipelineRun.setName(TEST_NAME);
    expectedPipelineRun.setPipelineName(TEST_NAME);
    expectedPipelineRun.setPipelineId(1L);
    expectedPipelineRun.setInstance(instance);
    expectedPipelineRun.setStatus(TaskStatus.SUCCESS);
    expectedPipelineRun.setVersion(TEST_VERSION);
    expectedPipelineRun.setRunStatuses(Collections.singletonList(runStatus));
    expectedPipelineRun.setPricePerHour(PRICE);
    expectedPipelineRun.setOwner(TEST_NAME);
    expectedPipelineRun.setPipelineRunParameters(Collections.singletonList(parameter));
    RunLog runLog = new RunLog();
    runLog.setLogText(TEST_DESCRIPTION);
    runLog.setStatus(TaskStatus.SUCCESS);
    runLog.setTask(new PipelineTask(TEST_NAME));
    List<RunLog> runLogs = Collections.singletonList(runLog);
    PipelineRunWithLog expectedPipelineRunWithLog = new PipelineRunWithLog();
    expectedPipelineRunWithLog.setPipelineRun(expectedPipelineRun);
    expectedPipelineRunWithLog.setRunOwner(USER);
    expectedPipelineRunWithLog.setRunLogs(runLogs);
    PipelineRunLoader pipelineRunLoader = new PipelineRunLoader(apiClient);
    when(apiClient.loadPipelineRunWithLogs(anyLong())).thenReturn(expectedPipelineRunWithLog);
    when(apiClient.loadPipelineRun(anyLong())).thenReturn(expectedPipelineRun);
    Optional<EntityContainer<PipelineRunWithLog>> container = pipelineRunLoader.loadEntity(1L);
    EntityContainer<PipelineRunWithLog> pipelineRunEntityContainer = container.orElseThrow(AssertionError::new);
    PipelineRunWithLog actualPipelineRunWithLog = pipelineRunEntityContainer.getEntity();
    assertNotNull(actualPipelineRunWithLog);
    PipelineRun actualPipelineRun = actualPipelineRunWithLog.getPipelineRun();
    assertNotNull(actualPipelineRun);
    List<RunLog> actualRunLogs = actualPipelineRunWithLog.getRunLogs();
    assertNotNull(actualRunLogs);
    verifyPipelineRun(expectedPipelineRun, actualPipelineRun);
    verifyRunInstance(expectedPipelineRun.getInstance(), actualPipelineRun.getInstance());
    verifyRunStatuses(expectedPipelineRun.getRunStatuses(), actualPipelineRun.getRunStatuses());
    verifyRunParameters(expectedPipelineRun.getPipelineRunParameters(), actualPipelineRun.getPipelineRunParameters());
    verifyRunLogs(runLogs, actualRunLogs);
    verifyPipelineUser(pipelineRunEntityContainer.getOwner());
    verifyPermissions(PERMISSIONS_CONTAINER_WITH_OWNER, pipelineRunEntityContainer.getPermissions());
}
Also used : PipelineRun(com.epam.pipeline.entity.pipeline.PipelineRun) LoaderVerificationUtils.verifyPipelineRun(com.epam.pipeline.elasticsearchagent.LoaderVerificationUtils.verifyPipelineRun) RunStatus(com.epam.pipeline.entity.pipeline.run.RunStatus) RunLog(com.epam.pipeline.entity.pipeline.RunLog) EntityContainer(com.epam.pipeline.elasticsearchagent.model.EntityContainer) LoaderVerificationUtils.verifyRunInstance(com.epam.pipeline.elasticsearchagent.LoaderVerificationUtils.verifyRunInstance) RunInstance(com.epam.pipeline.entity.pipeline.RunInstance) PipelineTask(com.epam.pipeline.entity.pipeline.PipelineTask) PipelineRunWithLog(com.epam.pipeline.elasticsearchagent.model.PipelineRunWithLog) PipelineRunParameter(com.epam.pipeline.entity.pipeline.run.parameter.PipelineRunParameter) Test(org.junit.jupiter.api.Test)

Example 7 with RunInstance

use of com.epam.pipeline.entity.pipeline.RunInstance in project cloud-pipeline by epam.

the class AutoscaleManager method checkFreeNodes.

private void checkFreeNodes(Set<String> scheduledRuns, KubernetesClient client, Set<String> pods) {
    NodeList nodeList = getAvailableNodes(client);
    RunInstance defaultInstance = clusterManager.getDefaultInstance();
    List<RunInstance> requiredInstances = getRequiredInstances(scheduledRuns, client);
    nodeList.getItems().forEach(node -> {
        String runId = node.getMetadata().getLabels().get(KubernetesConstants.RUN_ID_LABEL);
        if (node.getMetadata().getLabels().get(KubernetesConstants.PAUSED_NODE_LABEL) != null) {
            LOGGER.debug("Node {} is paused.", runId);
            return;
        }
        RunInstance previousConfiguration = getPreviousRunInstance(runId);
        if (!isNodeAvailable(node)) {
            if (previousConfiguration != null) {
                Long id = Long.parseLong(runId);
                LOGGER.debug("Trying to set failure status for run {}.", runId);
                pipelineRunManager.updatePipelineStatusIfNotFinal(id, TaskStatus.FAILURE, new Date());
                updatePodStatus(node, id);
            }
            LOGGER.debug("Scaling down unavailable {} node.", runId);
            clusterManager.scaleDown(runId);
            return;
        }
        if (previousConfiguration == null) {
            LOGGER.debug("Scaling down {} node for deleted pipeline.", runId);
            clusterManager.scaleDown(runId);
            return;
        }
        if (scheduledRuns.contains(runId) || pods.contains(runId)) {
            LOGGER.debug("Node is already assigned to run {}.", runId);
            return;
        }
        int currentClusterSize = getCurrentClusterSize(client);
        Optional<RunInstance> matchingPipeline = requiredInstances.stream().filter(instance -> clusterManager.requirementsMatch(previousConfiguration, instance)).findFirst();
        if (matchingPipeline.isPresent()) {
            requiredInstances.remove(matchingPipeline.get());
            LOGGER.debug("Leaving node {} free since it possibly matches a pending run.", runId);
        } else {
            Integer minClusterSize = preferenceManager.getPreference(SystemPreferences.CLUSTER_MIN_SIZE);
            if (clusterManager.requirementsMatch(defaultInstance, previousConfiguration) && currentClusterSize <= minClusterSize) {
                LOGGER.debug("Minimum cluster size achieved. Leaving {} nodes.", currentClusterSize);
            } else {
                if (clusterManager.isNodeExpired(runId)) {
                    LOGGER.debug("Scaling down expired node {}.", runId);
                    clusterManager.scaleDown(runId);
                } else {
                    LOGGER.debug("Leaving node {} free.", runId);
                }
            }
        }
    });
    Integer minClusterSize = preferenceManager.getPreference(SystemPreferences.CLUSTER_MIN_SIZE);
    int currentClusterSize = getCurrentClusterSize(client);
    if (minClusterSize > 0 && currentClusterSize < minClusterSize) {
        createFreeNodes(nodeList);
    }
}
Also used : GitClientException(com.epam.pipeline.exception.git.GitClientException) Date(java.util.Date) PipelineRunManager(com.epam.pipeline.manager.pipeline.PipelineRunManager) LoggerFactory(org.slf4j.LoggerFactory) SystemPreferences(com.epam.pipeline.manager.preference.SystemPreferences) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) Value(org.springframework.beans.factory.annotation.Value) HashSet(java.util.HashSet) PipelineRun(com.epam.pipeline.entity.pipeline.PipelineRun) ParallelExecutorService(com.epam.pipeline.manager.parallel.ParallelExecutorService) Service(org.springframework.stereotype.Service) Duration(java.time.Duration) Map(java.util.Map) ConditionalOnProperty(org.springframework.boot.autoconfigure.condition.ConditionalOnProperty) PipelineConfiguration(com.epam.pipeline.entity.configuration.PipelineConfiguration) Node(io.fabric8.kubernetes.api.model.Node) PodCondition(io.fabric8.kubernetes.api.model.PodCondition) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) NodeCondition(io.fabric8.kubernetes.api.model.NodeCondition) PreferenceManager(com.epam.pipeline.manager.preference.PreferenceManager) Logger(org.slf4j.Logger) PipelineRunParameter(com.epam.pipeline.entity.pipeline.run.parameter.PipelineRunParameter) RunInstance(com.epam.pipeline.entity.pipeline.RunInstance) TaskStatus(com.epam.pipeline.entity.pipeline.TaskStatus) AbstractSchedulingManager(com.epam.pipeline.manager.scheduling.AbstractSchedulingManager) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Pod(io.fabric8.kubernetes.api.model.Pod) Set(java.util.Set) Instant(java.time.Instant) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Collectors(java.util.stream.Collectors) NodeList(io.fabric8.kubernetes.api.model.NodeList) Objects(java.util.Objects) Config(io.fabric8.kubernetes.client.Config) List(java.util.List) CmdExecutionException(com.epam.pipeline.exception.CmdExecutionException) PodList(io.fabric8.kubernetes.api.model.PodList) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) NumberUtils(org.apache.commons.lang3.math.NumberUtils) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) Collections(java.util.Collections) NodeList(io.fabric8.kubernetes.api.model.NodeList) RunInstance(com.epam.pipeline.entity.pipeline.RunInstance) Date(java.util.Date)

Example 8 with RunInstance

use of com.epam.pipeline.entity.pipeline.RunInstance in project cloud-pipeline by epam.

the class AutoscaleManager method createNodeForRun.

private void createNodeForRun(List<CompletableFuture<Void>> tasks, String runId, RunInstance requiredInstance) {
    long longId = Long.parseLong(runId);
    addNodeUpTask(longId);
    tasks.add(CompletableFuture.runAsync(() -> {
        Instant start = Instant.now();
        // save required instance
        pipelineRunManager.updateRunInstance(longId, requiredInstance);
        RunInstance instance = clusterManager.scaleUp(runId, requiredInstance);
        // save instance ID and IP
        pipelineRunManager.updateRunInstance(longId, instance);
        Instant end = Instant.now();
        removeNodeUpTask(longId);
        LOGGER.debug("Time to create a node for run {} : {} s.", runId, Duration.between(start, end).getSeconds());
    }, executorService.getExecutorService()).exceptionally(e -> {
        LOGGER.error(e.getMessage(), e);
        if (e.getCause() instanceof CmdExecutionException && Objects.equals(NODEUP_SPOT_FAILED_EXIT_CODE, ((CmdExecutionException) e.getCause()).getExitCode())) {
            spotNodeUpAttempts.merge(longId, 1, (oldVal, newVal) -> oldVal + 1);
        }
        removeNodeUpTask(longId, false);
        return null;
    }));
}
Also used : GitClientException(com.epam.pipeline.exception.git.GitClientException) Date(java.util.Date) PipelineRunManager(com.epam.pipeline.manager.pipeline.PipelineRunManager) LoggerFactory(org.slf4j.LoggerFactory) SystemPreferences(com.epam.pipeline.manager.preference.SystemPreferences) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) Value(org.springframework.beans.factory.annotation.Value) HashSet(java.util.HashSet) PipelineRun(com.epam.pipeline.entity.pipeline.PipelineRun) ParallelExecutorService(com.epam.pipeline.manager.parallel.ParallelExecutorService) Service(org.springframework.stereotype.Service) Duration(java.time.Duration) Map(java.util.Map) ConditionalOnProperty(org.springframework.boot.autoconfigure.condition.ConditionalOnProperty) PipelineConfiguration(com.epam.pipeline.entity.configuration.PipelineConfiguration) Node(io.fabric8.kubernetes.api.model.Node) PodCondition(io.fabric8.kubernetes.api.model.PodCondition) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) NodeCondition(io.fabric8.kubernetes.api.model.NodeCondition) PreferenceManager(com.epam.pipeline.manager.preference.PreferenceManager) Logger(org.slf4j.Logger) PipelineRunParameter(com.epam.pipeline.entity.pipeline.run.parameter.PipelineRunParameter) RunInstance(com.epam.pipeline.entity.pipeline.RunInstance) TaskStatus(com.epam.pipeline.entity.pipeline.TaskStatus) AbstractSchedulingManager(com.epam.pipeline.manager.scheduling.AbstractSchedulingManager) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Pod(io.fabric8.kubernetes.api.model.Pod) Set(java.util.Set) Instant(java.time.Instant) HasMetadata(io.fabric8.kubernetes.api.model.HasMetadata) Collectors(java.util.stream.Collectors) NodeList(io.fabric8.kubernetes.api.model.NodeList) Objects(java.util.Objects) Config(io.fabric8.kubernetes.client.Config) List(java.util.List) CmdExecutionException(com.epam.pipeline.exception.CmdExecutionException) PodList(io.fabric8.kubernetes.api.model.PodList) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) NumberUtils(org.apache.commons.lang3.math.NumberUtils) PostConstruct(javax.annotation.PostConstruct) Optional(java.util.Optional) Collections(java.util.Collections) Instant(java.time.Instant) RunInstance(com.epam.pipeline.entity.pipeline.RunInstance) CmdExecutionException(com.epam.pipeline.exception.CmdExecutionException)

Example 9 with RunInstance

use of com.epam.pipeline.entity.pipeline.RunInstance in project cloud-pipeline by epam.

the class AutoscaleManager method getNewRunInstance.

private RunInstance getNewRunInstance(String runId) throws GitClientException {
    Long longRunId = Long.parseLong(runId);
    PipelineRun run = pipelineRunManager.loadPipelineRun(longRunId);
    RunInstance instance;
    if (run.getInstance() == null || run.getInstance().isEmpty()) {
        PipelineConfiguration configuration = pipelineRunManager.loadRunConfiguration(longRunId);
        instance = clusterManager.configurationToInstance(configuration);
    } else {
        instance = clusterManager.fillInstance(run.getInstance());
    }
    if (instance.getSpot() != null && instance.getSpot() && spotNodeUpAttempts.getOrDefault(longRunId, 0) >= preferenceManager.getPreference(SystemPreferences.CLUSTER_SPOT_MAX_ATTEMPTS)) {
        instance.setSpot(false);
        pipelineRunManager.updateRunInstance(longRunId, instance);
    }
    return instance;
}
Also used : PipelineRun(com.epam.pipeline.entity.pipeline.PipelineRun) RunInstance(com.epam.pipeline.entity.pipeline.RunInstance) PipelineConfiguration(com.epam.pipeline.entity.configuration.PipelineConfiguration)

Example 10 with RunInstance

use of com.epam.pipeline.entity.pipeline.RunInstance in project cloud-pipeline by epam.

the class ClusterManagerImpl method getDefaultInstance.

@Override
public RunInstance getDefaultInstance() {
    RunInstance instance = new RunInstance();
    instance.setNodeDisk(preferenceManager.getPreference(SystemPreferences.CLUSTER_INSTANCE_HDD));
    instance.setEffectiveNodeDisk(preferenceManager.getPreference(SystemPreferences.CLUSTER_INSTANCE_HDD));
    instance.setNodeImage(preferenceManager.getPreference(SystemPreferences.CLUSTER_INSTANCE_IMAGE));
    instance.setNodeType(preferenceManager.getPreference(SystemPreferences.CLUSTER_INSTANCE_TYPE));
    instance.setAwsRegionId(awsRegionManager.loadDefaultRegion().getAwsRegionName());
    return instance;
}
Also used : RunInstance(com.epam.pipeline.entity.pipeline.RunInstance)

Aggregations

RunInstance (com.epam.pipeline.entity.pipeline.RunInstance)20 PipelineRun (com.epam.pipeline.entity.pipeline.PipelineRun)14 PipelineConfiguration (com.epam.pipeline.entity.configuration.PipelineConfiguration)6 Date (java.util.Date)6 PipelineRunParameter (com.epam.pipeline.entity.pipeline.run.parameter.PipelineRunParameter)5 CmdExecutionException (com.epam.pipeline.exception.CmdExecutionException)5 List (java.util.List)5 GitClientException (com.epam.pipeline.exception.git.GitClientException)4 PipelineRunManager (com.epam.pipeline.manager.pipeline.PipelineRunManager)4 PreferenceManager (com.epam.pipeline.manager.preference.PreferenceManager)4 SystemPreferences (com.epam.pipeline.manager.preference.SystemPreferences)4 Duration (java.time.Duration)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 Optional (java.util.Optional)4 Collectors (java.util.stream.Collectors)4 PostConstruct (javax.annotation.PostConstruct)4 TaskStatus (com.epam.pipeline.entity.pipeline.TaskStatus)3 ParallelExecutorService (com.epam.pipeline.manager.parallel.ParallelExecutorService)3