List of usage examples for com.amazonaws.services.ec2.model DescribeSpotPriceHistoryRequest setInstanceTypes
public void setInstanceTypes(java.util.Collection<String> instanceTypes)
Filters the results by the specified instance types.
From source file:imperial.modaclouds.monitoring.datacollectors.monitors.EC2SpotPriceMonitor.java
License:BSD License
@Override public void run() { String accessKeyId = null;/* w w w .j a v a2 s . com*/ String secretKey = null; long startTime = 0; while (!spmt.isInterrupted()) { if (System.currentTimeMillis() - startTime > 10000) { spotInstanceVec = new ArrayList<SpotInstance>(); for (String metric : getProvidedMetrics()) { try { VM resource = new VM(Config.getInstance().getVmType(), Config.getInstance().getVmId()); if (dcAgent.shouldMonitor(resource, metric)) { Map<String, String> parameters = dcAgent.getParameters(resource, metric); String endpoint = null; String productDes = null; String instanceType = null; accessKeyId = parameters.get("accessKey"); secretKey = parameters.get("secretKey"); endpoint = parameters.get("endPoint"); productDes = parameters.get("productDescription"); instanceType = parameters.get("productDescription"); period = Integer.valueOf(parameters.get("samplingTime")) * 1000; samplingProb = Double.valueOf(parameters.get("samplingProbability")); SpotInstance spotInstance = new SpotInstance(); spotInstance.productDes = new ArrayList<String>(); spotInstance.instanceType = new ArrayList<String>(); spotInstance.endpoint = endpoint; spotInstance.productDes.add(productDes); spotInstance.instanceType.add(instanceType); spotInstanceVec.add(spotInstance); } } catch (NumberFormatException | ConfigurationException e) { e.printStackTrace(); } } startTime = System.currentTimeMillis(); } AWSCredentials credentials = new BasicAWSCredentials(accessKeyId, secretKey); AmazonEC2 ec2 = new AmazonEC2Client(credentials); ////// boolean isSent = false; if (Math.random() < this.samplingProb) { isSent = true; } for (int i = 0; i < spotInstanceVec.size(); i++) { ec2.setEndpoint(spotInstanceVec.get(i).endpoint); DescribeSpotPriceHistoryRequest request = new DescribeSpotPriceHistoryRequest(); request.setProductDescriptions(spotInstanceVec.get(i).productDes); request.setInstanceTypes(spotInstanceVec.get(i).instanceType); //request.setStartTime(startTime); //request.setMaxResults(maxResult); String nextToken = ""; //do { request.withNextToken(nextToken); DescribeSpotPriceHistoryResult result = ec2.describeSpotPriceHistory(request); List<String> splitString = Arrays.asList(result.getSpotPriceHistory().get(0).toString().split(",")); if (isSent) { for (String temp : splitString) { if (temp.contains("SpotPrice")) { temp = temp.replace("SpotPrice: ", ""); System.out.println(temp); try { logger.info("Sending datum: {} {} {}", temp, "SpotPrice", monitoredTarget); dcAgent.send( new VM(Config.getInstance().getVmType(), Config.getInstance().getVmId()), "SpotPrice", temp); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } //for (int j = 0; j < result.getSpotPriceHistory().size(); j++) { //System.out.println(result.getSpotPriceHistory().get(0)); //break; //} //result = result.withNextToken(result.getNextToken()); //nextToken = result.getNextToken(); //} while(!nextToken.isEmpty()); } try { Thread.sleep(period); } catch (InterruptedException e) { Thread.currentThread().interrupt(); break; } } }
From source file:org.finra.dm.dao.impl.Ec2DaoImpl.java
License:Apache License
/** * 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. *//*from www .j a v a 2s . com*/ @Override public List<SpotPrice> getLatestSpotPrices(String availabilityZone, Collection<String> instanceTypes, AwsParamsDto awsParamsDto) { AmazonEC2Client ec2Client = getEc2Client(awsParamsDto); DescribeSpotPriceHistoryRequest describeSpotPriceHistoryRequest = new DescribeSpotPriceHistoryRequest(); describeSpotPriceHistoryRequest.setAvailabilityZone(availabilityZone); describeSpotPriceHistoryRequest.setInstanceTypes(instanceTypes); 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; }
From source file:org.finra.herd.dao.impl.Ec2DaoImpl.java
License:Apache License
/** * 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. *//*w w w . j a v a 2 s. co m*/ @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; }