Search in sources :

Example 1 with InstanceGroupManagersListManagedInstancesResponse

use of com.google.api.services.compute.model.InstanceGroupManagersListManagedInstancesResponse in project druid by druid-io.

the class GceAutoScalerTest method testTerminateWithIds.

@Test
public void testTerminateWithIds() throws IOException, GeneralSecurityException, GceServiceException {
    GceAutoScaler autoScaler = EasyMock.createMockBuilder(GceAutoScaler.class).withConstructor(int.class, int.class, GceEnvironmentConfig.class).withArgs(2, 4, new GceEnvironmentConfig(1, "proj-x", "us-central-1", "druid-mig")).addMockedMethod("createComputeServiceImpl").createMock();
    EasyMock.expect(autoScaler.createComputeServiceImpl()).andReturn(null);
    EasyMock.expect(autoScaler.createComputeServiceImpl()).andReturn(mockCompute);
    EasyMock.replay(autoScaler);
    // set up getRunningInstances results
    InstanceGroupManagersListManagedInstancesResponse beforeRunningInstance = createRunningInstances(Arrays.asList("http://xyz/foo", "http://xyz/bar", "http://xyz/baz"));
    InstanceGroupManagersListManagedInstancesResponse afterRunningInstance = createRunningInstances(Arrays.asList("http://xyz/foo", "http://xyz/bar"));
    // 1st call
    EasyMock.expect(mockInstancesRequest.execute()).andReturn(beforeRunningInstance);
    EasyMock.expect(mockInstancesRequest.setMaxResults(500L)).andReturn(mockInstancesRequest);
    // 2nd call
    EasyMock.expect(mockInstancesRequest.execute()).andReturn(afterRunningInstance);
    EasyMock.expect(mockInstancesRequest.setMaxResults(500L)).andReturn(mockInstancesRequest);
    EasyMock.replay(mockInstancesRequest);
    EasyMock.expect(mockInstanceGroupManagers.listManagedInstances("proj-x", "us-central-1", "druid-mig")).andReturn(mockInstancesRequest).times(2);
    // set up the delete operation
    Operation mockResponse = new Operation();
    mockResponse.setStatus("DONE");
    mockResponse.setError(new Operation.Error());
    EasyMock.expect(mockDeleteRequest.execute()).andReturn(mockResponse);
    EasyMock.replay(mockDeleteRequest);
    InstanceGroupManagersDeleteInstancesRequest requestBody = new InstanceGroupManagersDeleteInstancesRequest();
    requestBody.setInstances(Collections.singletonList("zones/us-central-1/instances/baz"));
    EasyMock.expect(mockInstanceGroupManagers.deleteInstances("proj-x", "us-central-1", "druid-mig", requestBody)).andReturn(mockDeleteRequest);
    EasyMock.replay(mockInstanceGroupManagers);
    // called twice in getRunningInstances...
    EasyMock.expect(mockCompute.instanceGroupManagers()).andReturn(mockInstanceGroupManagers);
    EasyMock.expect(mockCompute.instanceGroupManagers()).andReturn(mockInstanceGroupManagers);
    // ...and once in terminateWithIds
    EasyMock.expect(mockCompute.instanceGroupManagers()).andReturn(mockInstanceGroupManagers);
    // and that's all folks!
    EasyMock.replay(mockCompute);
    AutoScalingData autoScalingData = autoScaler.terminateWithIds(Collections.singletonList("baz"));
    Assert.assertEquals(1, autoScalingData.getNodeIds().size());
    Assert.assertEquals("baz", autoScalingData.getNodeIds().get(0));
    EasyMock.verify(mockCompute);
    EasyMock.verify(mockInstanceGroupManagers);
    EasyMock.verify(mockDeleteRequest);
    EasyMock.verify(mockInstancesRequest);
}
Also used : InstanceGroupManagersDeleteInstancesRequest(com.google.api.services.compute.model.InstanceGroupManagersDeleteInstancesRequest) AutoScalingData(org.apache.druid.indexing.overlord.autoscaling.AutoScalingData) InstanceGroupManagersListManagedInstancesResponse(com.google.api.services.compute.model.InstanceGroupManagersListManagedInstancesResponse) Operation(com.google.api.services.compute.model.Operation) Test(org.junit.Test)

Example 2 with InstanceGroupManagersListManagedInstancesResponse

use of com.google.api.services.compute.model.InstanceGroupManagersListManagedInstancesResponse in project druid by druid-io.

the class GceAutoScalerTest method testTerminate.

@Test
public void testTerminate() throws IOException, GeneralSecurityException, GceServiceException {
    GceAutoScaler autoScaler = EasyMock.createMockBuilder(GceAutoScaler.class).withConstructor(int.class, int.class, GceEnvironmentConfig.class).withArgs(2, 4, new GceEnvironmentConfig(1, "proj-x", "us-central-1", "druid-mig")).addMockedMethod("createComputeServiceImpl").createMock();
    EasyMock.expect(autoScaler.createComputeServiceImpl()).andReturn(null);
    EasyMock.expect(autoScaler.createComputeServiceImpl()).andReturn(mockCompute);
    EasyMock.replay(autoScaler);
    // testing the ip --> id part
    Instance i0 = makeInstance("baz", "1.2.3.6");
    InstanceList mockInstanceListResponse = new InstanceList();
    mockInstanceListResponse.setNextPageToken(null);
    mockInstanceListResponse.setItems(Collections.singletonList(i0));
    EasyMock.expect(mockIpToIdRequest.execute()).andReturn(mockInstanceListResponse);
    EasyMock.expect(mockIpToIdRequest.setPageToken(EasyMock.anyString())).andReturn(// the method needs to return something, what is actually irrelevant here
    mockIpToIdRequest);
    EasyMock.replay(mockIpToIdRequest);
    EasyMock.expect(mockInstances.list("proj-x", "us-central-1")).andReturn(mockIpToIdRequest);
    EasyMock.expect(mockCompute.instances()).andReturn(mockInstances);
    EasyMock.replay(mockInstances);
    // testing the delete part
    InstanceGroupManagersListManagedInstancesResponse beforeRunningInstance = createRunningInstances(Arrays.asList("http://xyz/foo", "http://xyz/bar", "http://xyz/baz"));
    InstanceGroupManagersListManagedInstancesResponse afterRunningInstance = createRunningInstances(Arrays.asList("http://xyz/foo", "http://xyz/bar"));
    // 1st call
    EasyMock.expect(mockInstancesRequest.execute()).andReturn(beforeRunningInstance);
    EasyMock.expect(mockInstancesRequest.setMaxResults(500L)).andReturn(mockInstancesRequest);
    // 2nd call
    EasyMock.expect(mockInstancesRequest.execute()).andReturn(afterRunningInstance);
    EasyMock.expect(mockInstancesRequest.setMaxResults(500L)).andReturn(mockInstancesRequest);
    EasyMock.replay(mockInstancesRequest);
    EasyMock.expect(mockInstanceGroupManagers.listManagedInstances("proj-x", "us-central-1", "druid-mig")).andReturn(mockInstancesRequest).times(2);
    // set up the delete operation
    Operation mockResponse = new Operation();
    mockResponse.setStatus("DONE");
    mockResponse.setError(new Operation.Error());
    EasyMock.expect(mockDeleteRequest.execute()).andReturn(mockResponse);
    EasyMock.replay(mockDeleteRequest);
    InstanceGroupManagersDeleteInstancesRequest requestBody = new InstanceGroupManagersDeleteInstancesRequest();
    requestBody.setInstances(Collections.singletonList("zones/us-central-1/instances/baz"));
    EasyMock.expect(mockInstanceGroupManagers.deleteInstances("proj-x", "us-central-1", "druid-mig", requestBody)).andReturn(mockDeleteRequest);
    EasyMock.replay(mockInstanceGroupManagers);
    // called twice in getRunningInstances...
    EasyMock.expect(mockCompute.instanceGroupManagers()).andReturn(mockInstanceGroupManagers);
    EasyMock.expect(mockCompute.instanceGroupManagers()).andReturn(mockInstanceGroupManagers);
    // ...and once in terminateWithIds
    EasyMock.expect(mockCompute.instanceGroupManagers()).andReturn(mockInstanceGroupManagers);
    // and that's all folks!
    EasyMock.replay(mockCompute);
    AutoScalingData autoScalingData = autoScaler.terminate(Collections.singletonList("1.2.3.6"));
    Assert.assertEquals(1, autoScalingData.getNodeIds().size());
    Assert.assertEquals("baz", autoScalingData.getNodeIds().get(0));
    EasyMock.verify(mockCompute);
    EasyMock.verify(mockIpToIdRequest);
    EasyMock.verify(mockInstanceGroupManagers);
    EasyMock.verify(mockDeleteRequest);
    EasyMock.verify(mockInstancesRequest);
}
Also used : InstanceGroupManagersDeleteInstancesRequest(com.google.api.services.compute.model.InstanceGroupManagersDeleteInstancesRequest) AutoScalingData(org.apache.druid.indexing.overlord.autoscaling.AutoScalingData) Instance(com.google.api.services.compute.model.Instance) ManagedInstance(com.google.api.services.compute.model.ManagedInstance) InstanceGroupManagersListManagedInstancesResponse(com.google.api.services.compute.model.InstanceGroupManagersListManagedInstancesResponse) Operation(com.google.api.services.compute.model.Operation) InstanceList(com.google.api.services.compute.model.InstanceList) Test(org.junit.Test)

Example 3 with InstanceGroupManagersListManagedInstancesResponse

use of com.google.api.services.compute.model.InstanceGroupManagersListManagedInstancesResponse in project druid by druid-io.

the class GceAutoScaler method getRunningInstances.

// Returns the list of the IDs of the machines running in the MIG
private List<String> getRunningInstances() {
    // 500 is sadly the max, see below
    final long maxResults = 500L;
    ArrayList<String> ids = new ArrayList<>();
    try {
        final String project = envConfig.getProjectId();
        final String zone = envConfig.getZoneName();
        final String managedInstanceGroupName = envConfig.getManagedInstanceGroupName();
        Compute computeService = createComputeService();
        Compute.InstanceGroupManagers.ListManagedInstances request = computeService.instanceGroupManagers().listManagedInstances(project, zone, managedInstanceGroupName);
        // Notice that while the doc says otherwise, there is not nextPageToken to page
        // through results and so everything needs to be in the same page
        request.setMaxResults(maxResults);
        InstanceGroupManagersListManagedInstancesResponse response = request.execute();
        for (ManagedInstance mi : response.getManagedInstances()) {
            ids.add(GceUtils.extractNameFromInstance(mi.getInstance()));
        }
        log.debug("Found running instances [%s]", String.join(",", ids));
    } catch (Exception e) {
        log.error(e, "Unable to get instances.");
    }
    return ids;
}
Also used : Compute(com.google.api.services.compute.Compute) ArrayList(java.util.ArrayList) ManagedInstance(com.google.api.services.compute.model.ManagedInstance) InstanceGroupManagersListManagedInstancesResponse(com.google.api.services.compute.model.InstanceGroupManagersListManagedInstancesResponse) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException)

Example 4 with InstanceGroupManagersListManagedInstancesResponse

use of com.google.api.services.compute.model.InstanceGroupManagersListManagedInstancesResponse in project druid by druid-io.

the class GceAutoScalerTest method createRunningInstances.

private InstanceGroupManagersListManagedInstancesResponse createRunningInstances(List<String> instances) {
    InstanceGroupManagersListManagedInstancesResponse mockResponse = new InstanceGroupManagersListManagedInstancesResponse();
    mockResponse.setManagedInstances(new ArrayList<>());
    for (String x : instances) {
        ManagedInstance mi = new ManagedInstance();
        mi.setInstance(x);
        mockResponse.getManagedInstances().add(mi);
    }
    return mockResponse;
}
Also used : ManagedInstance(com.google.api.services.compute.model.ManagedInstance) InstanceGroupManagersListManagedInstancesResponse(com.google.api.services.compute.model.InstanceGroupManagersListManagedInstancesResponse)

Example 5 with InstanceGroupManagersListManagedInstancesResponse

use of com.google.api.services.compute.model.InstanceGroupManagersListManagedInstancesResponse in project druid by druid-io.

the class GceAutoScalerTest method testProvision.

@Test
public void testProvision() throws IOException, GeneralSecurityException, GceServiceException {
    GceAutoScaler autoScaler = EasyMock.createMockBuilder(GceAutoScaler.class).withConstructor(int.class, int.class, GceEnvironmentConfig.class).withArgs(2, 4, new GceEnvironmentConfig(1, "proj-x", "us-central-1", "druid-mig")).addMockedMethod("createComputeServiceImpl").createMock();
    EasyMock.expect(autoScaler.createComputeServiceImpl()).andReturn(null);
    EasyMock.expect(autoScaler.createComputeServiceImpl()).andReturn(mockCompute);
    EasyMock.replay(autoScaler);
    // set up getRunningInstances results
    InstanceGroupManagersListManagedInstancesResponse beforeRunningInstance = createRunningInstances(Arrays.asList("http://xyz/foo", "http://xyz/bar"));
    InstanceGroupManagersListManagedInstancesResponse afterRunningInstance = createRunningInstances(Arrays.asList("http://xyz/foo", "http://xyz/bar", "http://xyz/baz"));
    // 1st call
    EasyMock.expect(mockInstancesRequest.execute()).andReturn(beforeRunningInstance);
    EasyMock.expect(mockInstancesRequest.setMaxResults(500L)).andReturn(mockInstancesRequest);
    // 2nd call
    EasyMock.expect(mockInstancesRequest.execute()).andReturn(afterRunningInstance);
    EasyMock.expect(mockInstancesRequest.setMaxResults(500L)).andReturn(mockInstancesRequest);
    EasyMock.replay(mockInstancesRequest);
    EasyMock.expect(mockInstanceGroupManagers.listManagedInstances("proj-x", "us-central-1", "druid-mig")).andReturn(mockInstancesRequest).times(2);
    // set up the resize operation
    Operation mockResponse = new Operation();
    mockResponse.setStatus("DONE");
    mockResponse.setError(new Operation.Error());
    EasyMock.expect(mockResizeRequest.execute()).andReturn(mockResponse);
    EasyMock.replay(mockResizeRequest);
    EasyMock.expect(mockInstanceGroupManagers.resize("proj-x", "us-central-1", "druid-mig", 3)).andReturn(mockResizeRequest);
    EasyMock.replay(mockInstanceGroupManagers);
    // called twice in getRunningInstances...
    EasyMock.expect(mockCompute.instanceGroupManagers()).andReturn(mockInstanceGroupManagers);
    EasyMock.expect(mockCompute.instanceGroupManagers()).andReturn(mockInstanceGroupManagers);
    // ...and once in provision
    EasyMock.expect(mockCompute.instanceGroupManagers()).andReturn(mockInstanceGroupManagers);
    // and that's all folks!
    EasyMock.replay(mockCompute);
    AutoScalingData autoScalingData = autoScaler.provision();
    Assert.assertEquals(1, autoScalingData.getNodeIds().size());
    Assert.assertEquals("baz", autoScalingData.getNodeIds().get(0));
    EasyMock.verify(mockCompute);
    EasyMock.verify(mockInstanceGroupManagers);
    EasyMock.verify(mockResizeRequest);
    EasyMock.verify(mockInstancesRequest);
}
Also used : AutoScalingData(org.apache.druid.indexing.overlord.autoscaling.AutoScalingData) InstanceGroupManagersListManagedInstancesResponse(com.google.api.services.compute.model.InstanceGroupManagersListManagedInstancesResponse) Operation(com.google.api.services.compute.model.Operation) Test(org.junit.Test)

Aggregations

InstanceGroupManagersListManagedInstancesResponse (com.google.api.services.compute.model.InstanceGroupManagersListManagedInstancesResponse)8 AutoScalingData (org.apache.druid.indexing.overlord.autoscaling.AutoScalingData)6 Test (org.junit.Test)6 Operation (com.google.api.services.compute.model.Operation)5 InstanceGroupManagersDeleteInstancesRequest (com.google.api.services.compute.model.InstanceGroupManagersDeleteInstancesRequest)3 ManagedInstance (com.google.api.services.compute.model.ManagedInstance)3 Compute (com.google.api.services.compute.Compute)1 Instance (com.google.api.services.compute.model.Instance)1 InstanceList (com.google.api.services.compute.model.InstanceList)1 IOException (java.io.IOException)1 GeneralSecurityException (java.security.GeneralSecurityException)1 ArrayList (java.util.ArrayList)1