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();
}
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;
}
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));
}
}
Aggregations