Search in sources :

Example 1 with InstanceGroupManagersDeleteInstancesRequest

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

the class GceAutoScaler method terminateWithIds.

/**
 * Terminates the instances in the list of IDs provided by the caller
 */
@Override
public AutoScalingData terminateWithIds(List<String> ids) {
    log.info("Asked to terminate IDs: [%s]", String.join(",", ids));
    if (ids.isEmpty()) {
        return new AutoScalingData(new ArrayList<>());
    }
    try {
        final String project = envConfig.getProjectId();
        final String zone = envConfig.getZoneName();
        final String managedInstanceGroupName = envConfig.getManagedInstanceGroupName();
        List<String> before = getRunningInstances();
        InstanceGroupManagersDeleteInstancesRequest requestBody = new InstanceGroupManagersDeleteInstancesRequest();
        requestBody.setInstances(namesToInstances(ids));
        Compute computeService = createComputeService();
        Compute.InstanceGroupManagers.DeleteInstances request = computeService.instanceGroupManagers().deleteInstances(project, zone, managedInstanceGroupName, requestBody);
        Operation response = request.execute();
        Operation.Error err = waitForOperationEnd(computeService, response);
        if (err == null || err.isEmpty()) {
            List<String> after = null;
            // certain amount of retries in checking)
            for (int i = 0; i < RUNNING_INSTANCES_MAX_RETRIES; i++) {
                after = getRunningInstances();
                if (after.size() == (before.size() - ids.size())) {
                    break;
                }
                log.info("Machines not down yet, waiting");
                Thread.sleep(POLL_INTERVAL_MS);
            }
            // keep only the ones no more present
            before.removeAll(after);
            return new AutoScalingData(before);
        } else {
            log.error("Unable to terminate instances: %s", err.toPrettyString());
        }
    } catch (Exception e) {
        log.error(e, "Unable to terminate any instances.");
    }
    return new AutoScalingData(new ArrayList<>());
}
Also used : InstanceGroupManagersDeleteInstancesRequest(com.google.api.services.compute.model.InstanceGroupManagersDeleteInstancesRequest) AutoScalingData(org.apache.druid.indexing.overlord.autoscaling.AutoScalingData) Compute(com.google.api.services.compute.Compute) Operation(com.google.api.services.compute.model.Operation) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException)

Example 2 with InstanceGroupManagersDeleteInstancesRequest

use of com.google.api.services.compute.model.InstanceGroupManagersDeleteInstancesRequest 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 3 with InstanceGroupManagersDeleteInstancesRequest

use of com.google.api.services.compute.model.InstanceGroupManagersDeleteInstancesRequest 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 4 with InstanceGroupManagersDeleteInstancesRequest

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

the class GceAutoScalerTest method testTerminateWithIdsWithMissingRemoval.

@Test
public void testTerminateWithIdsWithMissingRemoval() 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 after1RunningInstance = createRunningInstances(Arrays.asList("http://xyz/foo", "http://xyz/bar", "http://xyz/baz"));
    // not changing anything, will trigger the loop around getRunningInstances
    InstanceGroupManagersListManagedInstancesResponse after2RunningInstance = createRunningInstances(Arrays.asList("http://xyz/foo", "http://xyz/bar"));
    // now the machine got dropped!
    // 1st call
    EasyMock.expect(mockInstancesRequest.execute()).andReturn(beforeRunningInstance);
    EasyMock.expect(mockInstancesRequest.setMaxResults(500L)).andReturn(mockInstancesRequest);
    // 2nd call, the next is needed
    EasyMock.expect(mockInstancesRequest.execute()).andReturn(after1RunningInstance);
    EasyMock.expect(mockInstancesRequest.setMaxResults(500L)).andReturn(mockInstancesRequest);
    // 3rd call, this unblocks
    EasyMock.expect(mockInstancesRequest.execute()).andReturn(after2RunningInstance);
    EasyMock.expect(mockInstancesRequest.setMaxResults(500L)).andReturn(mockInstancesRequest);
    EasyMock.replay(mockInstancesRequest);
    EasyMock.expect(mockInstanceGroupManagers.listManagedInstances("proj-x", "us-central-1", "druid-mig")).andReturn(mockInstancesRequest).times(3);
    // 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 three times in getRunningInstances...
    EasyMock.expect(mockCompute.instanceGroupManagers()).andReturn(mockInstanceGroupManagers);
    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)

Aggregations

InstanceGroupManagersDeleteInstancesRequest (com.google.api.services.compute.model.InstanceGroupManagersDeleteInstancesRequest)4 Operation (com.google.api.services.compute.model.Operation)4 AutoScalingData (org.apache.druid.indexing.overlord.autoscaling.AutoScalingData)4 InstanceGroupManagersListManagedInstancesResponse (com.google.api.services.compute.model.InstanceGroupManagersListManagedInstancesResponse)3 Test (org.junit.Test)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 ManagedInstance (com.google.api.services.compute.model.ManagedInstance)1 IOException (java.io.IOException)1 GeneralSecurityException (java.security.GeneralSecurityException)1