Search in sources :

Example 26 with DescribeInstancesRequest

use of software.amazon.awssdk.services.ec2.model.DescribeInstancesRequest in project druid by druid-io.

the class EC2AutoScalerTest method testIptoIdLookup.

@Test
public void testIptoIdLookup() throws Exception {
    EC2AutoScaler autoScaler = new EC2AutoScaler(0, 1, ENV_CONFIG, amazonEC2Client, managementConfig);
    final int n = 150;
    Assert.assertTrue(n <= 2 * EC2AutoScaler.MAX_AWS_FILTER_VALUES);
    List<String> ips = Lists.transform(ContiguousSet.create(Range.closedOpen(0, n), DiscreteDomain.integers()).asList(), Functions.toStringFunction());
    EasyMock.expect(amazonEC2Client.describeInstances(new DescribeInstancesRequest().withFilters(new Filter("private-ip-address", ips.subList(0, EC2AutoScaler.MAX_AWS_FILTER_VALUES))))).andReturn(describeInstancesResult);
    EasyMock.expect(amazonEC2Client.describeInstances(new DescribeInstancesRequest().withFilters(new Filter("private-ip-address", ips.subList(EC2AutoScaler.MAX_AWS_FILTER_VALUES, n))))).andReturn(describeInstancesResult);
    EasyMock.replay(amazonEC2Client);
    final Reservation[] chunk1 = new Reservation[EC2AutoScaler.MAX_AWS_FILTER_VALUES];
    Arrays.fill(chunk1, reservation);
    final Reservation[] chunk2 = new Reservation[n - EC2AutoScaler.MAX_AWS_FILTER_VALUES];
    Arrays.fill(chunk2, reservation);
    EasyMock.expect(describeInstancesResult.getReservations()).andReturn(Lists.newArrayList(chunk1));
    EasyMock.expect(describeInstancesResult.getReservations()).andReturn(Lists.newArrayList(chunk2));
    EasyMock.replay(describeInstancesResult);
    EasyMock.expect(reservation.getInstances()).andReturn(Arrays.asList(instance)).times(n);
    EasyMock.replay(reservation);
    List<String> ids = autoScaler.ipToIdLookup(ips);
    Assert.assertEquals(n, ids.size());
}
Also used : Reservation(com.amazonaws.services.ec2.model.Reservation) Filter(com.amazonaws.services.ec2.model.Filter) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) EC2AutoScaler(io.druid.indexing.overlord.autoscaling.ec2.EC2AutoScaler) Test(org.junit.Test)

Example 27 with DescribeInstancesRequest

use of software.amazon.awssdk.services.ec2.model.DescribeInstancesRequest in project druid by druid-io.

the class EC2AutoScalerTest method testIdToIpLookup.

@Test
public void testIdToIpLookup() throws Exception {
    EC2AutoScaler autoScaler = new EC2AutoScaler(0, 1, ENV_CONFIG, amazonEC2Client, managementConfig);
    final int n = 150;
    Assert.assertTrue(n <= 2 * EC2AutoScaler.MAX_AWS_FILTER_VALUES);
    List<String> ids = Lists.transform(ContiguousSet.create(Range.closedOpen(0, n), DiscreteDomain.integers()).asList(), Functions.toStringFunction());
    EasyMock.expect(amazonEC2Client.describeInstances(new DescribeInstancesRequest().withFilters(new Filter("instance-id", ids.subList(0, EC2AutoScaler.MAX_AWS_FILTER_VALUES))))).andReturn(describeInstancesResult);
    EasyMock.expect(amazonEC2Client.describeInstances(new DescribeInstancesRequest().withFilters(new Filter("instance-id", ids.subList(EC2AutoScaler.MAX_AWS_FILTER_VALUES, n))))).andReturn(describeInstancesResult);
    EasyMock.replay(amazonEC2Client);
    final Reservation[] chunk1 = new Reservation[EC2AutoScaler.MAX_AWS_FILTER_VALUES];
    Arrays.fill(chunk1, reservation);
    final Reservation[] chunk2 = new Reservation[n - EC2AutoScaler.MAX_AWS_FILTER_VALUES];
    Arrays.fill(chunk2, reservation);
    EasyMock.expect(describeInstancesResult.getReservations()).andReturn(Lists.newArrayList(chunk1));
    EasyMock.expect(describeInstancesResult.getReservations()).andReturn(Lists.newArrayList(chunk2));
    EasyMock.replay(describeInstancesResult);
    EasyMock.expect(reservation.getInstances()).andReturn(Collections.singletonList(instance)).times(n);
    EasyMock.replay(reservation);
    List<String> ips = autoScaler.idToIpLookup(ids);
    Assert.assertEquals(n, ips.size());
}
Also used : Reservation(com.amazonaws.services.ec2.model.Reservation) Filter(com.amazonaws.services.ec2.model.Filter) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest) EC2AutoScaler(io.druid.indexing.overlord.autoscaling.ec2.EC2AutoScaler) Test(org.junit.Test)

Example 28 with DescribeInstancesRequest

use of software.amazon.awssdk.services.ec2.model.DescribeInstancesRequest in project druid by druid-io.

the class EC2AutoScaler method idToIpLookup.

@Override
public List<String> idToIpLookup(List<String> nodeIds) {
    final List<String> retVal = FluentIterable.from(Lists.partition(nodeIds, MAX_AWS_FILTER_VALUES)).transformAndConcat(new Function<List<String>, Iterable<Reservation>>() {

        @Override
        public Iterable<Reservation> apply(List<String> input) {
            return amazonEC2Client.describeInstances(new DescribeInstancesRequest().withFilters(new Filter("instance-id", input))).getReservations();
        }
    }).transformAndConcat(new Function<Reservation, Iterable<Instance>>() {

        @Override
        public Iterable<Instance> apply(Reservation reservation) {
            return reservation.getInstances();
        }
    }).transform(new Function<Instance, String>() {

        @Override
        public String apply(Instance instance) {
            return instance.getPrivateIpAddress();
        }
    }).toList();
    log.debug("Performing lookup: %s --> %s", nodeIds, retVal);
    return retVal;
}
Also used : Function(com.google.common.base.Function) Reservation(com.amazonaws.services.ec2.model.Reservation) FluentIterable(com.google.common.collect.FluentIterable) Filter(com.amazonaws.services.ec2.model.Filter) Instance(com.amazonaws.services.ec2.model.Instance) List(java.util.List) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest)

Example 29 with DescribeInstancesRequest

use of software.amazon.awssdk.services.ec2.model.DescribeInstancesRequest in project druid by druid-io.

the class EC2AutoScaler method ipToIdLookup.

@Override
public List<String> ipToIdLookup(List<String> ips) {
    final List<String> retVal = FluentIterable.from(Lists.partition(ips, MAX_AWS_FILTER_VALUES)).transformAndConcat(new Function<List<String>, Iterable<Reservation>>() {

        @Override
        public Iterable<Reservation> apply(List<String> input) {
            return amazonEC2Client.describeInstances(new DescribeInstancesRequest().withFilters(new Filter("private-ip-address", input))).getReservations();
        }
    }).transformAndConcat(new Function<Reservation, Iterable<Instance>>() {

        @Override
        public Iterable<Instance> apply(Reservation reservation) {
            return reservation.getInstances();
        }
    }).transform(new Function<Instance, String>() {

        @Override
        public String apply(Instance instance) {
            return instance.getInstanceId();
        }
    }).toList();
    log.debug("Performing lookup: %s --> %s", ips, retVal);
    return retVal;
}
Also used : Function(com.google.common.base.Function) Reservation(com.amazonaws.services.ec2.model.Reservation) FluentIterable(com.google.common.collect.FluentIterable) Filter(com.amazonaws.services.ec2.model.Filter) Instance(com.amazonaws.services.ec2.model.Instance) List(java.util.List) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest)

Example 30 with DescribeInstancesRequest

use of software.amazon.awssdk.services.ec2.model.DescribeInstancesRequest in project camel by apache.

the class EC2Producer method describeInstances.

private void describeInstances(AmazonEC2Client ec2Client, Exchange exchange) {
    Collection instanceIds;
    DescribeInstancesRequest request = new DescribeInstancesRequest();
    if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(EC2Constants.INSTANCES_IDS))) {
        instanceIds = exchange.getIn().getHeader(EC2Constants.INSTANCES_IDS, Collection.class);
        request.withInstanceIds(instanceIds);
    }
    DescribeInstancesResult result;
    try {
        result = ec2Client.describeInstances(request);
    } catch (AmazonServiceException ase) {
        LOG.trace("Describe Instances command returned the error code {}", ase.getErrorCode());
        throw ase;
    }
    Message message = getMessageForResponse(exchange);
    message.setBody(result);
}
Also used : DescribeInstancesResult(com.amazonaws.services.ec2.model.DescribeInstancesResult) Message(org.apache.camel.Message) AmazonServiceException(com.amazonaws.AmazonServiceException) Collection(java.util.Collection) DescribeInstancesRequest(com.amazonaws.services.ec2.model.DescribeInstancesRequest)

Aggregations

DescribeInstancesRequest (com.amazonaws.services.ec2.model.DescribeInstancesRequest)52 DescribeInstancesResult (com.amazonaws.services.ec2.model.DescribeInstancesResult)35 Instance (com.amazonaws.services.ec2.model.Instance)30 Reservation (com.amazonaws.services.ec2.model.Reservation)29 ArrayList (java.util.ArrayList)26 List (java.util.List)23 Filter (com.amazonaws.services.ec2.model.Filter)18 Map (java.util.Map)15 Collectors (java.util.stream.Collectors)15 AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)12 Tag (com.amazonaws.services.ec2.model.Tag)11 Collection (java.util.Collection)11 Set (java.util.Set)11 Inject (javax.inject.Inject)11 DescribeVolumesResult (com.amazonaws.services.ec2.model.DescribeVolumesResult)10 TerminateInstancesRequest (com.amazonaws.services.ec2.model.TerminateInstancesRequest)10 TerminateInstancesResult (com.amazonaws.services.ec2.model.TerminateInstancesResult)10 Logger (org.slf4j.Logger)10 LoggerFactory (org.slf4j.LoggerFactory)10 StopInstancesRequest (com.amazonaws.services.ec2.model.StopInstancesRequest)9