Search in sources :

Example 1 with AvailabilityZone

use of com.amazonaws.services.ec2.model.AvailabilityZone in project incubator-gobblin by apache.

the class AWSSdkClient method getAvailabilityZones.

/**
 * Get availability zones in an Amazon AWS region
 *
 * @return List of availability zones
 */
public List<AvailabilityZone> getAvailabilityZones() {
    final AmazonEC2 amazonEC2 = getEc2Client();
    final DescribeAvailabilityZonesResult describeAvailabilityZonesResult = amazonEC2.describeAvailabilityZones();
    final List<AvailabilityZone> availabilityZones = describeAvailabilityZonesResult.getAvailabilityZones();
    LOGGER.info("Found: " + availabilityZones.size() + " availability zone");
    return availabilityZones;
}
Also used : AvailabilityZone(com.amazonaws.services.ec2.model.AvailabilityZone) AmazonEC2(com.amazonaws.services.ec2.AmazonEC2) DescribeAvailabilityZonesResult(com.amazonaws.services.ec2.model.DescribeAvailabilityZonesResult)

Example 2 with AvailabilityZone

use of com.amazonaws.services.ec2.model.AvailabilityZone in project incubator-gobblin by apache.

the class GobblinAWSClusterLauncher method setupGobblinCluster.

/**
 * Setup the Gobblin AWS cluster.
 *
 * @throws IOException If there's anything wrong setting up the AWS cluster
 */
@VisibleForTesting
String setupGobblinCluster() throws IOException {
    final String uuid = UUID.randomUUID().toString();
    // Create security group
    // TODO: Make security group restrictive
    final String securityGroupName = "GobblinSecurityGroup_" + uuid;
    this.awsSdkClient.createSecurityGroup(securityGroupName, "Gobblin cluster security group");
    this.awsSdkClient.addPermissionsToSecurityGroup(securityGroupName, "0.0.0.0/0", "tcp", 0, 65535);
    // Create key value pair
    final String keyName = "GobblinKey_" + uuid;
    final String material = this.awsSdkClient.createKeyValuePair(keyName);
    LOGGER.debug("Material is: " + material);
    FileUtils.writeStringToFile(new File(keyName + ".pem"), material);
    // Get all availability zones in the region. Currently, we will only use first
    final List<AvailabilityZone> availabilityZones = this.awsSdkClient.getAvailabilityZones();
    // Launch Cluster Master
    final String clusterId = launchClusterMaster(uuid, keyName, securityGroupName, availabilityZones.get(0));
    // Launch WorkUnit runners
    launchWorkUnitRunners(uuid, keyName, securityGroupName, availabilityZones.get(0));
    return clusterId;
}
Also used : AvailabilityZone(com.amazonaws.services.ec2.model.AvailabilityZone) File(java.io.File) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 3 with AvailabilityZone

use of com.amazonaws.services.ec2.model.AvailabilityZone in project herd by FINRAOS.

the class MockEc2OperationsImpl method describeAvailabilityZones.

@Override
public DescribeAvailabilityZonesResult describeAvailabilityZones(AmazonEC2Client ec2Client, DescribeAvailabilityZonesRequest describeAvailabilityZonesRequest) {
    List<AvailabilityZone> availabilityZones = new ArrayList<>();
    List<String> requestedZoneNames = describeAvailabilityZonesRequest.getZoneNames();
    // add all AZ if request is empty (this is AWS behavior)
    if (requestedZoneNames.isEmpty()) {
        requestedZoneNames.addAll(mockAvailabilityZones.keySet());
    }
    for (String requestedZoneName : requestedZoneNames) {
        // ignore AZ name which does not exist (this is AWS behavior)
        MockAvailabilityZone mockAvailabilityZone = mockAvailabilityZones.get(requestedZoneName);
        if (mockAvailabilityZone != null) {
            availabilityZones.add(mockAvailabilityZone.toAwsObject());
        }
    }
    DescribeAvailabilityZonesResult describeAvailabilityZonesResult = new DescribeAvailabilityZonesResult();
    describeAvailabilityZonesResult.setAvailabilityZones(availabilityZones);
    return describeAvailabilityZonesResult;
}
Also used : ArrayList(java.util.ArrayList) AvailabilityZone(com.amazonaws.services.ec2.model.AvailabilityZone) DescribeAvailabilityZonesResult(com.amazonaws.services.ec2.model.DescribeAvailabilityZonesResult)

Example 4 with AvailabilityZone

use of com.amazonaws.services.ec2.model.AvailabilityZone in project herd by FINRAOS.

the class Ec2DaoImpl method getLatestSpotPrices.

/**
 * This implementation uses DescribeSpotPriceHistory API which returns the latest spot price history for the specified AZ and instance types. This method
 * then filters the returned list to only contain the latest spot price for each instance type.
 */
@Override
public List<SpotPrice> getLatestSpotPrices(String availabilityZone, Collection<String> instanceTypes, Collection<String> productDescriptions, AwsParamsDto awsParamsDto) {
    AmazonEC2Client ec2Client = getEc2Client(awsParamsDto);
    DescribeSpotPriceHistoryRequest describeSpotPriceHistoryRequest = new DescribeSpotPriceHistoryRequest();
    describeSpotPriceHistoryRequest.setAvailabilityZone(availabilityZone);
    describeSpotPriceHistoryRequest.setInstanceTypes(instanceTypes);
    describeSpotPriceHistoryRequest.setProductDescriptions(productDescriptions);
    DescribeSpotPriceHistoryResult describeSpotPriceHistoryResult = ec2Operations.describeSpotPriceHistory(ec2Client, describeSpotPriceHistoryRequest);
    List<SpotPrice> spotPrices = new ArrayList<>();
    Set<String> instanceTypesFound = new HashSet<>();
    for (SpotPrice spotPriceHistoryEntry : describeSpotPriceHistoryResult.getSpotPriceHistory()) {
        if (instanceTypesFound.add(spotPriceHistoryEntry.getInstanceType())) {
            spotPrices.add(spotPriceHistoryEntry);
        }
    }
    return spotPrices;
}
Also used : AmazonEC2Client(com.amazonaws.services.ec2.AmazonEC2Client) DescribeSpotPriceHistoryResult(com.amazonaws.services.ec2.model.DescribeSpotPriceHistoryResult) SpotPrice(com.amazonaws.services.ec2.model.SpotPrice) DescribeSpotPriceHistoryRequest(com.amazonaws.services.ec2.model.DescribeSpotPriceHistoryRequest) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 5 with AvailabilityZone

use of com.amazonaws.services.ec2.model.AvailabilityZone in project herd by FINRAOS.

the class EmrPricingHelper method updateEmrClusterDefinitionWithBestPrice.

/**
 * Finds the best price for each master and core instances based on the subnets and master and core instance search parameters given in the definition.
 * <p/>
 * The results of the findings are used to update the given definition.
 * <p/>
 * If the instance's instanceSpotPrice is set, the instance definition will keep that value. If the instance's instanceMaxSearchPrice is set, the best price
 * will be found. If the found price is spot, the instanceSpotPrice will be set to the value of instanceMaxSearchPrice. If the found price is on-demand, the
 * instanceSpotPrice will be removed. The definition's subnetId will be set to the particular subnet which the best price is found. The value will always be
 * replaced by a single subnet ID.
 * <p/>
 * The definition's instanceMaxSearchPrice and instanceOnDemandThreshold will be removed by this operation.
 *
 * @param emrClusterAlternateKeyDto EMR cluster alternate key
 * @param emrClusterDefinition The EMR cluster definition with search criteria, and the definition that will be updated
 * @param awsParamsDto the AWS related parameters for access/secret keys and proxy details
 */
public void updateEmrClusterDefinitionWithBestPrice(EmrClusterAlternateKeyDto emrClusterAlternateKeyDto, EmrClusterDefinition emrClusterDefinition, AwsParamsDto awsParamsDto) {
    EmrVpcPricingState emrVpcPricingState = new EmrVpcPricingState();
    // Get total count of instances this definition will attempt to create
    int totalInstanceCount = getTotalInstanceCount(emrClusterDefinition);
    // Get the subnet information
    List<Subnet> subnets = getSubnets(emrClusterDefinition, awsParamsDto);
    for (Subnet subnet : subnets) {
        emrVpcPricingState.getSubnetAvailableIpAddressCounts().put(subnet.getSubnetId(), subnet.getAvailableIpAddressCount());
    }
    // Filter out subnets with not enough available IPs
    removeSubnetsWithAvailableIpsLessThan(subnets, totalInstanceCount);
    if (subnets.isEmpty()) {
        LOGGER.info(String.format("Insufficient IP availability. namespace=\"%s\" emrClusterDefinitionName=\"%s\" emrClusterName=\"%s\" " + "totalRequestedInstanceCount=%s emrVpcPricingState=%s", emrClusterAlternateKeyDto.getNamespace(), emrClusterAlternateKeyDto.getEmrClusterDefinitionName(), emrClusterAlternateKeyDto.getEmrClusterName(), totalInstanceCount, jsonHelper.objectToJson(emrVpcPricingState)));
        throw new ObjectNotFoundException(String.format("There are no subnets in the current VPC which have sufficient IP addresses available to run your " + "clusters. Try expanding the list of subnets or try again later. requestedInstanceCount=%s%n%s", totalInstanceCount, emrVpcPricingStateFormatter.format(emrVpcPricingState)));
    }
    // Best prices are accumulated in this list
    List<EmrClusterPriceDto> emrClusterPrices = new ArrayList<>();
    InstanceDefinition masterInstanceDefinition = getMasterInstanceDefinition(emrClusterDefinition);
    InstanceDefinition coreInstanceDefinition = getCoreInstanceDefinition(emrClusterDefinition);
    InstanceDefinition taskInstanceDefinition = getTaskInstanceDefinition(emrClusterDefinition);
    Set<String> requestedInstanceTypes = new HashSet<>();
    String masterInstanceType = masterInstanceDefinition.getInstanceType();
    requestedInstanceTypes.add(masterInstanceType);
    if (coreInstanceDefinition != null) {
        String coreInstanceType = coreInstanceDefinition.getInstanceType();
        requestedInstanceTypes.add(coreInstanceType);
    }
    if (taskInstanceDefinition != null) {
        String taskInstanceType = taskInstanceDefinition.getInstanceType();
        requestedInstanceTypes.add(taskInstanceType);
    }
    // Get AZs for the subnets
    for (AvailabilityZone availabilityZone : getAvailabilityZones(subnets, awsParamsDto)) {
        // Create a mapping of instance types to prices for more efficient, in-memory lookup
        // This method also validates that the given instance types are real instance types supported by AWS.
        Map<String, BigDecimal> instanceTypeOnDemandPrices = getInstanceTypeOnDemandPrices(availabilityZone, requestedInstanceTypes);
        // Create a mapping of instance types to prices for more efficient, in-memory lookup
        // When AWS does not return any spot price history for an instance type in an availability zone, the algorithm will not use that availability zone
        // when selecting the lowest price.
        Map<String, BigDecimal> instanceTypeSpotPrices = getInstanceTypeSpotPrices(availabilityZone, requestedInstanceTypes, awsParamsDto);
        emrVpcPricingState.getSpotPricesPerAvailabilityZone().put(availabilityZone.getZoneName(), instanceTypeSpotPrices);
        emrVpcPricingState.getOnDemandPricesPerAvailabilityZone().put(availabilityZone.getZoneName(), instanceTypeOnDemandPrices);
        // Get and compare master price
        BigDecimal masterSpotPrice = instanceTypeSpotPrices.get(masterInstanceType);
        BigDecimal masterOnDemandPrice = instanceTypeOnDemandPrices.get(masterInstanceType);
        Ec2PriceDto masterPrice = getBestInstancePrice(masterSpotPrice, masterOnDemandPrice, masterInstanceDefinition);
        // Get and compare core price
        Ec2PriceDto corePrice = null;
        if (coreInstanceDefinition != null) {
            String coreInstanceType = coreInstanceDefinition.getInstanceType();
            BigDecimal coreSpotPrice = instanceTypeSpotPrices.get(coreInstanceType);
            BigDecimal coreOnDemandPrice = instanceTypeOnDemandPrices.get(coreInstanceType);
            corePrice = getBestInstancePrice(coreSpotPrice, coreOnDemandPrice, coreInstanceDefinition);
        }
        // Get and compare task price
        Ec2PriceDto taskPrice = null;
        if (taskInstanceDefinition != null) {
            String taskInstanceType = taskInstanceDefinition.getInstanceType();
            BigDecimal taskSpotPrice = instanceTypeSpotPrices.get(taskInstanceType);
            BigDecimal taskOnDemandPrice = instanceTypeOnDemandPrices.get(taskInstanceType);
            taskPrice = getBestInstancePrice(taskSpotPrice, taskOnDemandPrice, taskInstanceDefinition);
        }
        // If prices were found
        if (masterPrice != null && (coreInstanceDefinition == null || corePrice != null) && (taskInstanceDefinition == null || taskPrice != null)) {
            // Add the pricing result to the result list
            emrClusterPrices.add(createEmrClusterPrice(availabilityZone, masterPrice, corePrice, taskPrice));
        }
    // If prices were not found for either master or core, this AZ cannot satisfy the search criteria. Ignore this AZ.
    }
    if (emrClusterPrices.isEmpty()) {
        LOGGER.info(String.format("No subnets which satisfied the best price search criteria. namespace=\"%s\" emrClusterDefinitionName=\"%s\" " + "emrClusterName=\"%s\" emrVpcPricingState=%s", emrClusterAlternateKeyDto.getNamespace(), emrClusterAlternateKeyDto.getEmrClusterDefinitionName(), emrClusterAlternateKeyDto.getEmrClusterName(), jsonHelper.objectToJson(emrVpcPricingState)));
        throw new ObjectNotFoundException(String.format("There were no subnets which satisfied your best price search criteria. If you explicitly opted to use spot EC2 instances, please confirm " + "that your instance types support spot pricing. Otherwise, try setting the max price or the on-demand threshold to a higher value.%n%s", emrVpcPricingStateFormatter.format(emrVpcPricingState)));
    }
    // Find the best prices from the result list
    EmrClusterPriceDto bestEmrClusterPrice = getEmrClusterPriceWithLowestCoreInstancePrice(emrClusterPrices);
    // Find the best subnet among the best AZ's
    Subnet bestEmrClusterSubnet = getBestSubnetForAvailabilityZone(bestEmrClusterPrice.getAvailabilityZone(), subnets);
    // Update the definition with the new calculated values
    updateInstanceDefinitionsWithBestPrice(emrClusterDefinition, bestEmrClusterSubnet, bestEmrClusterPrice);
}
Also used : InstanceDefinition(org.finra.herd.model.api.xml.InstanceDefinition) MasterInstanceDefinition(org.finra.herd.model.api.xml.MasterInstanceDefinition) EmrVpcPricingState(org.finra.herd.model.dto.EmrVpcPricingState) EmrClusterPriceDto(org.finra.herd.model.dto.EmrClusterPriceDto) ArrayList(java.util.ArrayList) AvailabilityZone(com.amazonaws.services.ec2.model.AvailabilityZone) BigDecimal(java.math.BigDecimal) Ec2PriceDto(org.finra.herd.model.dto.Ec2PriceDto) ObjectNotFoundException(org.finra.herd.model.ObjectNotFoundException) Subnet(com.amazonaws.services.ec2.model.Subnet) HashSet(java.util.HashSet)

Aggregations

AvailabilityZone (com.amazonaws.services.ec2.model.AvailabilityZone)10 DescribeAvailabilityZonesResult (com.amazonaws.services.ec2.model.DescribeAvailabilityZonesResult)8 ArrayList (java.util.ArrayList)7 HashSet (java.util.HashSet)5 AmazonEC2Client (com.amazonaws.services.ec2.AmazonEC2Client)4 HashMap (java.util.HashMap)4 AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)3 DescribeRegionsResult (com.amazonaws.services.ec2.model.DescribeRegionsResult)3 SpotPrice (com.amazonaws.services.ec2.model.SpotPrice)3 Subnet (com.amazonaws.services.ec2.model.Subnet)3 List (java.util.List)3 CreateVolumeRequest (com.amazonaws.services.ec2.model.CreateVolumeRequest)2 DescribeAvailabilityZonesRequest (com.amazonaws.services.ec2.model.DescribeAvailabilityZonesRequest)2 DescribeSpotPriceHistoryResult (com.amazonaws.services.ec2.model.DescribeSpotPriceHistoryResult)2 Region (com.amazonaws.services.ec2.model.Region)2 AvailabilityZone (com.sequenceiq.cloudbreak.cloud.model.AvailabilityZone)2 CloudRegions (com.sequenceiq.cloudbreak.cloud.model.CloudRegions)2 Region (com.sequenceiq.cloudbreak.cloud.model.Region)2 Collections.singletonList (java.util.Collections.singletonList)2 Test (org.junit.Test)2