Search in sources :

Example 1 with InstancePrice

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

the class PipelineRunManagerTest method setUp.

@Before
public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
    notScannedTool = new Tool();
    notScannedTool.setId(1L);
    notScannedTool.setImage(TEST_IMAGE);
    notScannedTool.setDefaultCommand("sleep");
    configuration = new PipelineConfiguration();
    configuration.setDockerImage(TEST_IMAGE);
    configuration.setInstanceDisk("1");
    configuration.setIsSpot(true);
    price = new InstancePrice(configuration.getInstanceType(), Integer.valueOf(configuration.getInstanceDisk()), PRICE_PER_HOUR);
    when(toolManager.loadByNameOrId(TEST_IMAGE)).thenReturn(notScannedTool);
    when(instanceOfferManager.isInstanceAllowed(anyString())).thenReturn(true);
    when(instanceOfferManager.isToolInstanceAllowed(anyString(), any())).thenReturn(true);
    when(instanceOfferManager.isPriceTypeAllowed(anyString(), any())).thenReturn(true);
    when(instanceOfferManager.getAllInstanceTypesObservable()).thenReturn(BehaviorSubject.create());
    when(instanceOfferManager.getInstanceEstimatedPrice(anyString(), anyInt(), anyBoolean(), anyString())).thenReturn(price);
    when(pipelineLauncher.launch(any(PipelineRun.class), any(), any(), anyString(), anyString())).thenReturn("sleep");
    when(toolManager.loadToolVersionScan(notScannedTool.getId(), null)).thenReturn(Optional.empty());
    when(toolVersionManager.loadToolVersion(anyLong(), anyString())).thenReturn(ToolVersion.builder().size(1L).build());
    doReturn(configuration).when(pipelineConfigurationManager).getPipelineConfiguration(any());
    doReturn(configuration).when(pipelineConfigurationManager).getPipelineConfiguration(any(), any());
    AwsRegion region = new AwsRegion();
    region.setAwsRegionName("us-east-1");
    doReturn(region).when(awsRegionManager).loadDefaultRegion();
    doNothing().when(entityManager).setManagers(any());
    doNothing().when(resourceMonitoringManager).monitorResourceUsage();
}
Also used : PipelineRun(com.epam.pipeline.entity.pipeline.PipelineRun) AwsRegion(com.epam.pipeline.entity.region.AwsRegion) InstancePrice(com.epam.pipeline.entity.cluster.InstancePrice) PipelineConfiguration(com.epam.pipeline.entity.configuration.PipelineConfiguration) Tool(com.epam.pipeline.entity.pipeline.Tool) Before(org.junit.Before)

Example 2 with InstancePrice

use of com.epam.pipeline.entity.cluster.InstancePrice 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 3 with InstancePrice

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

the class PipelineRunManager method setRunPrice.

private void setRunPrice(final RunInstance instance, final PipelineRun run) {
    if (!instance.isEmpty()) {
        run.setInstance(instance);
        InstancePrice runPrice = instanceOfferManager.getInstanceEstimatedPrice(instance.getNodeType(), instance.getNodeDisk(), instance.getSpot(), instance.getAwsRegionId());
        LOGGER.debug("Expected price per hour: {}", runPrice.getPricePerHour());
        run.setPricePerHour(BigDecimal.valueOf(runPrice.getPricePerHour()).setScale(2, RoundingMode.HALF_EVEN));
    }
}
Also used : InstancePrice(com.epam.pipeline.entity.cluster.InstancePrice)

Aggregations

InstancePrice (com.epam.pipeline.entity.cluster.InstancePrice)3 PipelineConfiguration (com.epam.pipeline.entity.configuration.PipelineConfiguration)2 PipelineRun (com.epam.pipeline.entity.pipeline.PipelineRun)2 AwsRegion (com.epam.pipeline.entity.region.AwsRegion)2 MessageConstants (com.epam.pipeline.common.MessageConstants)1 MessageHelper (com.epam.pipeline.common.MessageHelper)1 InstanceOfferRequestVO (com.epam.pipeline.controller.vo.InstanceOfferRequestVO)1 InstanceOfferDao (com.epam.pipeline.dao.cluster.InstanceOfferDao)1 AllowedInstanceAndPriceTypes (com.epam.pipeline.entity.cluster.AllowedInstanceAndPriceTypes)1 InstanceOffer (com.epam.pipeline.entity.cluster.InstanceOffer)1 InstanceType (com.epam.pipeline.entity.cluster.InstanceType)1 PipelineRunPrice (com.epam.pipeline.entity.cluster.PipelineRunPrice)1 ContextualPreferenceExternalResource (com.epam.pipeline.entity.contextual.ContextualPreferenceExternalResource)1 ContextualPreferenceLevel (com.epam.pipeline.entity.contextual.ContextualPreferenceLevel)1 RunInstance (com.epam.pipeline.entity.pipeline.RunInstance)1 Tool (com.epam.pipeline.entity.pipeline.Tool)1 GitClientException (com.epam.pipeline.exception.git.GitClientException)1 ContextualPreferenceManager (com.epam.pipeline.manager.contextual.ContextualPreferenceManager)1 PipelineRunManager (com.epam.pipeline.manager.pipeline.PipelineRunManager)1 PipelineVersionManager (com.epam.pipeline.manager.pipeline.PipelineVersionManager)1