Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

offers.getNextToken() has been nul #275

Open
blair319 opened this issue Nov 3, 2017 · 2 comments
Open

offers.getNextToken() has been nul #275

blair319 opened this issue Nov 3, 2017 · 2 comments

Comments

@blair319
Copy link

blair319 commented Nov 3, 2017

`private void pollAPI() throws Exception {
long currentTime = new DateMidnight().getMillis();

    DescribeReservedInstancesOfferingsRequest req =  new DescribeReservedInstancesOfferingsRequest()
            .withFilters(new com.amazonaws.services.ec2.model.Filter().withName("marketplace").withValues("false"));
    String token = null;
    boolean hasNewPrice = false;
    AmazonEC2Client ec2Client = new AmazonEC2Client(AwsUtils.awsCredentialsProvider, AwsUtils.clientConfig);

    for (Region region: Region.getAllRegions()) {
        // GovCloud uses different credentials than standard AWS, so you would need two separate
        // sets of credentials if you wanted to poll for RIs in both environments. For now, we
        // just ignore GovCloud here in order to prevent AuthFailure errors.
        if (region == Region.US_GOV_WEST_1) {
            continue;
        }
        ec2Client.setEndpoint("ec2." + region.name + ".amazonaws.com");
        do {
            if (!StringUtils.isEmpty(token))
                req.setNextToken(token);
            DescribeReservedInstancesOfferingsResult offers = ec2Client.describeReservedInstancesOfferings(req);
            token = offers.getNextToken();

            for (ReservedInstancesOffering offer: offers.getReservedInstancesOfferings()) {
                logger.info("offers list size,"+ offers.getReservedInstancesOfferings().size());
                if (offer.getProductDescription().indexOf("Amazon VPC") >= 0)
                    continue;

                // Ignore Region-Wide RIs
                if (offer.getAvailabilityZone() == null)
                    continue;

                ReservationUtilization utilization = ReservationUtilization.get(offer.getOfferingType());
                Ec2InstanceReservationPrice.ReservationPeriod term = offer.getDuration() / 24 / 3600 > 366 ?
                        Ec2InstanceReservationPrice.ReservationPeriod.threeyear : Ec2InstanceReservationPrice.ReservationPeriod.oneyear;
                if (term != this.term)
                    continue;

                double hourly = offer.getUsagePrice();
                if (hourly <= 0) {
                    for (RecurringCharge recurringCharge: offer.getRecurringCharges()) {
                        if (recurringCharge.getFrequency().equals("Hourly")) {
                            hourly = recurringCharge.getAmount();
                            break;
                        }
                    }
                }
                UsageType usageType = getUsageType(offer.getInstanceType(), offer.getProductDescription());
                // Unknown Zone
                if (Zone.getZone(offer.getAvailabilityZone()) == null) {
                    logger.error("No Zone for " + offer.getAvailabilityZone());
                } else {
                    hasNewPrice = setPrice(utilization, currentTime, Zone.getZone(offer.getAvailabilityZone()).region, usageType,
                            offer.getFixedPrice(), hourly) || hasNewPrice;

                    logger.info("Setting RI price for " + Zone.getZone(offer.getAvailabilityZone()).region + " " + utilization + " " + usageType + " " + offer.getFixedPrice() + " " + hourly);
                }
            }
        } while (!StringUtils.isEmpty(token));
    }

    ec2Client.shutdown();
    if (hasNewPrice) {
        for (ReservationUtilization utilization: files.keySet()) {
            File file = files.get(utilization);
            DataOutputStream out = new DataOutputStream(new FileOutputStream(file));
            try {
                Serializer.serialize(out, this.ec2InstanceReservationPrices.get(utilization));
                AwsUtils.upload(config.workS3BucketName, config.workS3BucketPrefix, file);
            }
            finally {
                out.close();
            }
        }
    }
}`

token = offers.getNextToken(); has been null, and then the cycle of death
Why not take token

@nfonrose
Copy link
Member

nfonrose commented Nov 3, 2017 via email

@blair319
Copy link
Author

blair319 commented Nov 3, 2017

@nfonrose A new example and i use the latest code installed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants