Search in sources :

Example 56 with PipelineRun

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

the class InstanceOfferManager method getPipelineRunEstimatedPrice.

public PipelineRunPrice getPipelineRunEstimatedPrice(Long runId, Long regionId) {
    PipelineRun pipelineRun = pipelineRunManager.loadPipelineRun(runId);
    RunInstance runInstance = pipelineRun.getInstance();
    AwsRegion region = awsRegionManager.loadRegionOrGetDefault(regionId);
    double pricePerHourForInstance = getPricePerHourForInstance(runInstance.getNodeType(), isSpotRequest(runInstance.getSpot()), region.getAwsRegionName());
    double pricePerDisk = getPriceForDisk(runInstance.getNodeDisk(), region.getAwsRegionName());
    double pricePerHour = pricePerDisk + pricePerHourForInstance;
    PipelineRunPrice price = new PipelineRunPrice();
    price.setInstanceDisk(runInstance.getNodeDisk());
    price.setInstanceType(runInstance.getNodeType());
    price.setPricePerHour(pricePerHour);
    if (pipelineRun.getStatus().isFinal()) {
        long duration = pipelineRun.getEndDate().getTime() - pipelineRun.getStartDate().getTime();
        price.setTotalPrice(duration / ONE_HOUR * pricePerHour);
    } else {
        price.setTotalPrice(0);
    }
    return price;
}
Also used : PipelineRun(com.epam.pipeline.entity.pipeline.PipelineRun) AwsRegion(com.epam.pipeline.entity.region.AwsRegion) PipelineRunPrice(com.epam.pipeline.entity.cluster.PipelineRunPrice) RunInstance(com.epam.pipeline.entity.pipeline.RunInstance)

Example 57 with PipelineRun

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

the class InstanceOfferManager method getInstanceEstimatedPrice.

public InstancePrice getInstanceEstimatedPrice(Long id, String version, String configName, String instanceType, int instanceDisk, Boolean spot, Long regionId) throws GitClientException {
    Boolean useSpot = spot;
    if (StringUtils.isEmpty(instanceType) || instanceDisk <= 0 || useSpot == null) {
        PipelineConfiguration pipelineConfiguration = versionManager.loadParametersFromScript(id, version, configName);
        if (StringUtils.isEmpty(instanceType)) {
            instanceType = pipelineConfiguration.getInstanceType();
        }
        if (instanceDisk <= 0) {
            instanceDisk = Integer.parseInt(pipelineConfiguration.getInstanceDisk());
        }
        if (useSpot == null) {
            useSpot = pipelineConfiguration.getIsSpot();
        }
    }
    Assert.isTrue(isInstanceAllowed(instanceType), messageHelper.getMessage(MessageConstants.ERROR_INSTANCE_TYPE_IS_NOT_ALLOWED, instanceType));
    AwsRegion region = awsRegionManager.loadRegionOrGetDefault(regionId);
    double pricePerHourForInstance = getPricePerHourForInstance(instanceType, isSpotRequest(useSpot), region.getAwsRegionName());
    double pricePerDisk = getPriceForDisk(instanceDisk, region.getAwsRegionName());
    double pricePerHour = pricePerDisk + pricePerHourForInstance;
    InstancePrice instancePrice = new InstancePrice(instanceType, instanceDisk, pricePerHour);
    List<PipelineRun> runs = pipelineRunManager.loadAllRunsByPipeline(id, version).stream().filter(run -> run.getStatus().isFinal()).collect(Collectors.toList());
    if (!runs.isEmpty()) {
        long minimumDuration = -1;
        long maximumDuration = -1;
        long totalDurations = 0;
        for (PipelineRun run : runs) {
            long duration = run.getEndDate().getTime() - run.getStartDate().getTime();
            if (minimumDuration == -1 || minimumDuration > duration) {
                minimumDuration = duration;
            }
            if (maximumDuration == -1 || maximumDuration < duration) {
                maximumDuration = duration;
            }
            totalDurations += duration;
        }
        double averageDuration = (double) totalDurations / runs.size();
        instancePrice.setAverageTimePrice(pricePerHour * averageDuration / ONE_HOUR);
        instancePrice.setMinimumTimePrice(pricePerHour * minimumDuration / ONE_HOUR);
        instancePrice.setMaximumTimePrice(pricePerHour * maximumDuration / ONE_HOUR);
    }
    return instancePrice;
}
Also used : PipelineRun(com.epam.pipeline.entity.pipeline.PipelineRun) Arrays(java.util.Arrays) InstanceType(com.epam.pipeline.entity.cluster.InstanceType) URL(java.net.URL) Date(java.util.Date) BehaviorSubject(io.reactivex.subjects.BehaviorSubject) LoggerFactory(org.slf4j.LoggerFactory) SystemPreferences(com.epam.pipeline.manager.preference.SystemPreferences) Autowired(org.springframework.beans.factory.annotation.Autowired) StringUtils(org.apache.commons.lang3.StringUtils) PipelineRun(com.epam.pipeline.entity.pipeline.PipelineRun) MessageHelper(com.epam.pipeline.common.MessageHelper) ContextualPreferenceManager(com.epam.pipeline.manager.contextual.ContextualPreferenceManager) ListUtils(org.apache.commons.collections4.ListUtils) AntPathMatcher(org.springframework.util.AntPathMatcher) AllowedInstanceAndPriceTypes(com.epam.pipeline.entity.cluster.AllowedInstanceAndPriceTypes) InstancePrice(com.epam.pipeline.entity.cluster.InstancePrice) PipelineConfiguration(com.epam.pipeline.entity.configuration.PipelineConfiguration) Set(java.util.Set) AwsRegion(com.epam.pipeline.entity.region.AwsRegion) Collectors(java.util.stream.Collectors) ContextualPreferenceLevel(com.epam.pipeline.entity.contextual.ContextualPreferenceLevel) List(java.util.List) PostConstruct(javax.annotation.PostConstruct) MessageConstants(com.epam.pipeline.common.MessageConstants) GitClientException(com.epam.pipeline.exception.git.GitClientException) PipelineRunManager(com.epam.pipeline.manager.pipeline.PipelineRunManager) PipelineRunPrice(com.epam.pipeline.entity.cluster.PipelineRunPrice) AtomicReference(java.util.concurrent.atomic.AtomicReference) PipelineVersionManager(com.epam.pipeline.manager.pipeline.PipelineVersionManager) ContextualPreferenceExternalResource(com.epam.pipeline.entity.contextual.ContextualPreferenceExternalResource) InstanceOffer(com.epam.pipeline.entity.cluster.InstanceOffer) Propagation(org.springframework.transaction.annotation.Propagation) Service(org.springframework.stereotype.Service) Observable(io.reactivex.Observable) Subject(io.reactivex.subjects.Subject) AbstractSystemPreference(com.epam.pipeline.manager.preference.AbstractSystemPreference) InstanceOfferRequestVO(com.epam.pipeline.controller.vo.InstanceOfferRequestVO) PreferenceManager(com.epam.pipeline.manager.preference.PreferenceManager) Logger(org.slf4j.Logger) RunInstance(com.epam.pipeline.entity.pipeline.RunInstance) IOException(java.io.IOException) InputStreamReader(java.io.InputStreamReader) InstanceOfferDao(com.epam.pipeline.dao.cluster.InstanceOfferDao) BufferedReader(java.io.BufferedReader) AllArgsConstructor(lombok.AllArgsConstructor) Collections(java.util.Collections) AwsRegionManager(com.epam.pipeline.manager.region.AwsRegionManager) NoArgsConstructor(lombok.NoArgsConstructor) Transactional(org.springframework.transaction.annotation.Transactional) Assert(org.springframework.util.Assert) InputStream(java.io.InputStream) AwsRegion(com.epam.pipeline.entity.region.AwsRegion) InstancePrice(com.epam.pipeline.entity.cluster.InstancePrice) PipelineConfiguration(com.epam.pipeline.entity.configuration.PipelineConfiguration)

Example 58 with PipelineRun

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

the class ResourceMonitoringManager method monitorResourceUsage.

public void monitorResourceUsage() {
    int idleTimeout = preferenceManager.getPreference(SYSTEM_MAX_IDLE_TIMEOUT_MINUTES);
    Map<String, PipelineRun> running = pipelineRunManager.loadRunningPipelineRuns().stream().filter(r -> DateUtils.nowUTC().isAfter(r.getProlongedAtTime().plus(idleTimeout, ChronoUnit.MINUTES))).collect(Collectors.toMap(PipelineRun::getPodId, r -> r));
    LOGGER.debug("Checking cpu stats for pipelines: " + running.keySet().stream().collect(Collectors.joining(", ")));
    LocalDateTime now = DateUtils.nowUTC();
    Map<String, Double> cpuMetrics = monitoringDao.loadCpuUsageRateMetrics(running.keySet(), now.minusMinutes(idleTimeout), now);
    LOGGER.debug("CPU Metrics received: " + cpuMetrics.entrySet().stream().map(e -> e.getKey() + ":" + e.getValue()).collect(Collectors.joining(", ")));
    double idleCpuLevel = preferenceManager.getPreference(SYSTEM_IDLE_CPU_THRESHOLD_PERCENT) / PERCENT;
    int actionTimeout = preferenceManager.getPreference(SYSTEM_IDLE_ACTION_TIMEOUT_MINUTES);
    IdleRunAction action = IdleRunAction.valueOf(preferenceManager.getPreference(SystemPreferences.SYSTEM_IDLE_ACTION));
    List<PipelineRun> runsToUpdate = processRuns(running, cpuMetrics, idleCpuLevel, actionTimeout, action);
    pipelineRunManager.updatePipelineRunsLastNotification(runsToUpdate);
}
Also used : PipelineRun(com.epam.pipeline.entity.pipeline.PipelineRun) SYSTEM_MAX_IDLE_TIMEOUT_MINUTES(com.epam.pipeline.manager.preference.SystemPreferences.SYSTEM_MAX_IDLE_TIMEOUT_MINUTES) SYSTEM_IDLE_CPU_THRESHOLD_PERCENT(com.epam.pipeline.manager.preference.SystemPreferences.SYSTEM_IDLE_CPU_THRESHOLD_PERCENT) Precision(org.apache.commons.math3.util.Precision) InstanceType(com.epam.pipeline.entity.cluster.InstanceType) MessageConstants(com.epam.pipeline.common.MessageConstants) PipelineRunManager(com.epam.pipeline.manager.pipeline.PipelineRunManager) LocalDateTime(java.time.LocalDateTime) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) SystemPreferences(com.epam.pipeline.manager.preference.SystemPreferences) HashMap(java.util.HashMap) Scheduled(org.springframework.scheduling.annotation.Scheduled) IdleRunAction(com.epam.pipeline.entity.monitoring.IdleRunAction) ArrayList(java.util.ArrayList) PipelineRun(com.epam.pipeline.entity.pipeline.PipelineRun) Pair(org.apache.commons.lang3.tuple.Pair) MessageHelper(com.epam.pipeline.common.MessageHelper) Service(org.springframework.stereotype.Service) Map(java.util.Map) NotificationType(com.epam.pipeline.entity.notification.NotificationSettings.NotificationType) Observable(io.reactivex.Observable) ConditionalOnProperty(org.springframework.boot.autoconfigure.condition.ConditionalOnProperty) DateUtils(com.epam.pipeline.entity.utils.DateUtils) Logger(org.slf4j.Logger) InstanceOfferManager(com.epam.pipeline.manager.cluster.InstanceOfferManager) PreferenceManager(com.epam.pipeline.manager.preference.PreferenceManager) AbstractSchedulingManager(com.epam.pipeline.manager.scheduling.AbstractSchedulingManager) TaskScheduler(org.springframework.scheduling.TaskScheduler) Collectors(java.util.stream.Collectors) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) List(java.util.List) ChronoUnit(java.time.temporal.ChronoUnit) SYSTEM_IDLE_ACTION_TIMEOUT_MINUTES(com.epam.pipeline.manager.preference.SystemPreferences.SYSTEM_IDLE_ACTION_TIMEOUT_MINUTES) PostConstruct(javax.annotation.PostConstruct) NotificationManager(com.epam.pipeline.manager.notification.NotificationManager) MonitoringESDao(com.epam.pipeline.dao.monitoring.MonitoringESDao) Collections(java.util.Collections) LocalDateTime(java.time.LocalDateTime) IdleRunAction(com.epam.pipeline.entity.monitoring.IdleRunAction)

Example 59 with PipelineRun

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

the class PodMonitor method clearWorkerNodes.

private void clearWorkerNodes(PipelineRun run, KubernetesClient client) {
    List<Pod> workers = getWorkerPods(String.valueOf(run.getId()), client);
    workers.forEach(worker -> {
        String runIdLabel = getRunIdLabel(worker);
        LOGGER.debug("Clearing worker {} node for parent run {}.", runIdLabel, run.getId());
        Long workerId = Long.parseLong(runIdLabel);
        PipelineRun workerRun = pipelineRunManager.loadPipelineRun(workerId);
        getPodLogs(workerRun, client, worker);
        workerRun.setTerminating(false);
        workerRun.setStatus(run.getStatus());
        workerRun.setEndDate(run.getEndDate());
        pipelineRunManager.updatePipelineStatus(workerRun);
        checkAndUpdateEc2State(workerRun, false);
        client.pods().inNamespace(kubeNamespace).withLabel(KubernetesConstants.RUN_ID_LABEL, runIdLabel).delete();
    });
}
Also used : PipelineRun(com.epam.pipeline.entity.pipeline.PipelineRun) Pod(io.fabric8.kubernetes.api.model.Pod)

Example 60 with PipelineRun

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

the class PipelineRunManager method commitRun.

/**
 * Commits docker image and push it to a docker registry from specified run
 * @param id {@link PipelineRun} id for pipeline run to be committed
 * @param registryId {@link DockerRegistry} id where new image will be pushed
 * @param deleteFiles if true files from pipeline working directory will be cleaned
 * @param stopPipeline if true pipeline will be stopped after commit
 * @param checkSize if true method will check if free disk space is enough for commit operation
 * @return  {@link PipelineRun} to be committed
 */
@Transactional(propagation = Propagation.REQUIRED)
public PipelineRun commitRun(Long id, Long registryId, String newImageName, boolean deleteFiles, boolean stopPipeline, boolean checkSize) {
    if (checkSize) {
        Assert.state(checkFreeSpaceAvailable(id), messageHelper.getMessage(MessageConstants.ERROR_INSTANCE_DISK_NOT_ENOUGH));
    }
    PipelineRun pipelineRun = pipelineRunDao.loadPipelineRun(id);
    DockerRegistry dockerRegistry = dockerRegistryManager.load(registryId);
    Assert.notNull(pipelineRun, messageHelper.getMessage(MessageConstants.ERROR_RUN_PIPELINES_NOT_FOUND, id));
    Assert.state(pipelineRun.getStatus() == TaskStatus.RUNNING, messageHelper.getMessage(MessageConstants.ERROR_PIPELINE_RUN_FINISHED, id));
    Assert.notNull(dockerRegistry, messageHelper.getMessage(MessageConstants.ERROR_REGISTRY_NOT_FOUND, registryId));
    String dockerImageFromRun = retrieveImageName(pipelineRun);
    String resolvedImageName = StringUtils.isEmpty(newImageName) ? dockerImageFromRun : newImageName;
    // check that there is no tool with this name in another registry
    toolManager.assertThatToolUniqueAcrossRegistries(resolvedImageName, dockerRegistry.getPath());
    return dockerContainerOperationManager.commitContainer(pipelineRun, dockerRegistry, resolvedImageName, deleteFiles, stopPipeline);
}
Also used : PipelineRun(com.epam.pipeline.entity.pipeline.PipelineRun) DockerRegistry(com.epam.pipeline.entity.pipeline.DockerRegistry) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

PipelineRun (com.epam.pipeline.entity.pipeline.PipelineRun)88 Test (org.junit.Test)31 Transactional (org.springframework.transaction.annotation.Transactional)29 AbstractSpringTest (com.epam.pipeline.AbstractSpringTest)23 EnvVarsBuilderTest (com.epam.pipeline.manager.execution.EnvVarsBuilderTest)22 ArrayList (java.util.ArrayList)18 RunInstance (com.epam.pipeline.entity.pipeline.RunInstance)17 Date (java.util.Date)15 Pipeline (com.epam.pipeline.entity.pipeline.Pipeline)14 List (java.util.List)14 PipelineConfiguration (com.epam.pipeline.entity.configuration.PipelineConfiguration)13 Map (java.util.Map)11 LocalDateTime (java.time.LocalDateTime)10 Arrays (java.util.Arrays)10 Collectors (java.util.stream.Collectors)10 PipelineUser (com.epam.pipeline.entity.user.PipelineUser)9 Collections (java.util.Collections)9 HashMap (java.util.HashMap)9 MessageHelper (com.epam.pipeline.common.MessageHelper)8 RunSid (com.epam.pipeline.entity.pipeline.run.parameter.RunSid)8