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);
}
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);
}
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;
}
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;
}
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);
}
Aggregations