Search in sources :

Example 1 with Zone

use of com.google.api.services.compute.model.Zone in project pipeline5 by hartwigmedical.

the class InstanceLifecycleManagerTest method shouldReturnEmptyOptionalIfNamedInstanceDoesNotExist.

@Test
public void shouldReturnEmptyOptionalIfNamedInstanceDoesNotExist() throws Exception {
    Compute.Instances.List zoneOneInstances = mock(Compute.Instances.List.class);
    Compute.Instances.List zoneTwoInstances = mock(Compute.Instances.List.class);
    InstanceList zoneOneInstanceList = mock(InstanceList.class);
    InstanceList zoneTwoInstanceList = mock(InstanceList.class);
    when(instances.list(ARGUMENTS.project(), zoneOne)).thenReturn(zoneOneInstances);
    when(instances.list(ARGUMENTS.project(), zoneTwo)).thenReturn(zoneTwoInstances);
    when(zoneOneInstances.execute()).thenReturn(zoneOneInstanceList);
    when(zoneTwoInstances.execute()).thenReturn(zoneTwoInstanceList);
    List<Instance> zoneOneInstanceItems = singletonList(namedInstance());
    List<Instance> zoneTwoInstanceItems = asList(namedInstance(), namedInstance());
    when(zoneOneInstanceList.getItems()).thenReturn(zoneOneInstanceItems);
    when(zoneTwoInstanceList.getItems()).thenReturn(zoneTwoInstanceItems);
    Compute.Zones zones = mock(Compute.Zones.class);
    Compute.Zones.List zonesList = mock(Compute.Zones.List.class);
    List<Zone> stubbedZones = asList(zone(zoneOne), zone(zoneTwo));
    when(zonesList.execute()).thenReturn(new ZoneList().setItems(stubbedZones));
    when(compute.zones()).thenReturn(zones);
    when(zones.list(ARGUMENTS.project())).thenReturn(zonesList);
    Optional<Instance> found = victim.findExistingInstance(vmName);
    assertThat(found).isEmpty();
}
Also used : Instance(com.google.api.services.compute.model.Instance) Zone(com.google.api.services.compute.model.Zone) ZoneList(com.google.api.services.compute.model.ZoneList) Compute(com.google.api.services.compute.Compute) InstanceList(com.google.api.services.compute.model.InstanceList) Test(org.junit.Test)

Example 2 with Zone

use of com.google.api.services.compute.model.Zone in project pipeline5 by hartwigmedical.

the class InstanceLifecycleManagerTest method zone.

private Zone zone(final String name) {
    Zone zone = mock(Zone.class);
    when(zone.getName()).thenReturn(name);
    when(zone.getRegion()).thenReturn("someregion-" + ARGUMENTS.region());
    return zone;
}
Also used : Zone(com.google.api.services.compute.model.Zone)

Example 3 with Zone

use of com.google.api.services.compute.model.Zone in project google-cloud-java by GoogleCloudPlatform.

the class HttpComputeRpc method listZones.

@Override
public Tuple<String, Iterable<Zone>> listZones(Map<Option, ?> options) {
    try {
        ZoneList zonesList = compute.zones().list(this.options.getProjectId()).setFilter(Option.FILTER.getString(options)).setMaxResults(Option.MAX_RESULTS.getLong(options)).setPageToken(Option.PAGE_TOKEN.getString(options)).setFields(Option.FIELDS.getString(options)).execute();
        Iterable<Zone> zones = zonesList.getItems();
        return Tuple.of(zonesList.getNextPageToken(), zones);
    } catch (IOException ex) {
        throw translate(ex);
    }
}
Also used : Zone(com.google.api.services.compute.model.Zone) IOException(java.io.IOException) ZoneList(com.google.api.services.compute.model.ZoneList)

Example 4 with Zone

use of com.google.api.services.compute.model.Zone in project pipeline5 by hartwigmedical.

the class GoogleComputeEngine method submit.

public PipelineStatus submit(final RuntimeBucket bucket, final VirtualMachineJobDefinition jobDefinition, final String discriminator) {
    String vmName = format("%s%s-%s", bucket.runId(), discriminator.isEmpty() ? "" : "-" + discriminator, jobDefinition.name());
    RuntimeFiles flags = RuntimeFiles.of(discriminator);
    PipelineStatus status = PipelineStatus.FAILED;
    try {
        BucketCompletionWatcher.State currentState = bucketWatcher.currentState(bucket, flags);
        if (currentState == BucketCompletionWatcher.State.SUCCESS) {
            LOGGER.info("Compute engine job [{}] already exists, and succeeded. Skipping job.", vmName);
            return PipelineStatus.SKIPPED;
        } else if (currentState == BucketCompletionWatcher.State.FAILURE) {
            LOGGER.info("Compute engine job [{}] already exists, but failed. Deleting state and restarting.", vmName);
            bucket.delete(flags.failure());
            bucket.delete(jobDefinition.namespacedResults().path());
        }
        String project = arguments.project();
        List<Zone> zones = fetchZones();
        zoneRandomizer.accept(zones);
        int index = 0;
        boolean keepTrying = !zones.isEmpty();
        while (keepTrying) {
            Zone currentZone = zones.get(index % zones.size());
            Instance instance = lifecycleManager.newInstance();
            instance.setName(vmName);
            instance.setZone(currentZone.getName());
            instance.setTags(new Tags().setItems(arguments.tags()));
            if (arguments.usePreemptibleVms()) {
                instance.setScheduling(new Scheduling().setPreemptible(true));
            }
            instance.setMachineType(machineType(currentZone.getName(), jobDefinition.performanceProfile().uri(), project));
            final Map<String, String> labelMap = labels.asMap(List.of(Map.entry("job_name", jobDefinition.name())));
            instance.setLabels(labelMap);
            addServiceAccount(instance);
            Image image = attachDisks(compute, instance, jobDefinition, project, vmName, currentZone.getName(), arguments.imageName().isPresent() ? compute.images().get(arguments.imageProject().orElse(VirtualMachineJobDefinition.HMF_IMAGE_PROJECT), arguments.imageName().get()).execute() : resolveLatestImage(compute, jobDefinition.imageFamily(), arguments.imageProject().orElse(project)), labelMap);
            LOGGER.info("Submitting compute engine job [{}] using image [{}] in zone [{}]", vmName, image.getName(), currentZone.getName());
            String startupScript = arguments.useLocalSsds() ? jobDefinition.startupCommand().asUnixString(new LocalSsdStorageStrategy(jobDefinition.localSsdCount())) : jobDefinition.startupCommand().asUnixString(new PersistentStorageStrategy());
            addStartupCommand(instance, bucket, flags, startupScript);
            addNetworkInterface(instance, project);
            Operation result = lifecycleManager.deleteOldInstancesAndStart(instance, currentZone.getName(), vmName);
            if (result.getError() == null) {
                LOGGER.debug("Successfully initialised [{}]", vmName);
                status = waitForCompletion(bucket, flags, currentZone, instance);
                if (status != PipelineStatus.PREEMPTED) {
                    if (arguments.useLocalSsds()) {
                        // Instances with local SSDs cannot be stopped or restarted
                        lifecycleManager.delete(currentZone.getName(), vmName);
                    } else {
                        lifecycleManager.stop(currentZone.getName(), vmName);
                        if (status == PipelineStatus.SUCCESS) {
                            lifecycleManager.delete(currentZone.getName(), vmName);
                        } else {
                            lifecycleManager.disableStartupScript(currentZone.getName(), instance.getName());
                        }
                    }
                    LOGGER.info("Compute engine job [{}] is complete with status [{}]", vmName, status);
                    keepTrying = false;
                } else {
                    LOGGER.info("Instance [{}] in [{}] was pre-empted", vmName, currentZone.getName());
                }
            } else if (anyErrorMatch(result, ZONE_EXHAUSTED_ERROR_CODE)) {
                LOGGER.warn("Zone [{}] has insufficient resources to fulfill the request for [{}]. Trying next zone", currentZone.getName(), vmName);
            } else if (anyErrorMatch(result, UNSUPPORTED_OPERATION_ERROR_CODE)) {
                LOGGER.warn("Received unsupported operation from GCE for [{}], this likely means the instance was pre-empted before it could " + "start, or another operation has yet to complete. Trying next zone.", vmName);
            } else if (anyErrorMatch(result, QUOTA_EXCEEDED)) {
                throw new RuntimeException(String.format("Quota exceeded for [%s], will keep trying until resources are available. Quota [%s]", vmName, result.getError().getErrors().get(0).getMessage()));
            } else {
                throw new RuntimeException(result.getError().toPrettyString());
            }
            index++;
        }
    } catch (IOException e) {
        String message = format("An error occurred running job on compute engine [%s]", vmName);
        LOGGER.error(message, e);
        return PipelineStatus.FAILED;
    }
    return status;
}
Also used : PipelineStatus(com.hartwig.pipeline.execution.PipelineStatus) LocalSsdStorageStrategy(com.hartwig.pipeline.execution.vm.storage.LocalSsdStorageStrategy) PersistentStorageStrategy(com.hartwig.pipeline.execution.vm.storage.PersistentStorageStrategy) Instance(com.google.api.services.compute.model.Instance) Zone(com.google.api.services.compute.model.Zone) Scheduling(com.google.api.services.compute.model.Scheduling) Operation(com.google.api.services.compute.model.Operation) IOException(java.io.IOException) Image(com.google.api.services.compute.model.Image) Tags(com.google.api.services.compute.model.Tags)

Example 5 with Zone

use of com.google.api.services.compute.model.Zone in project pipeline5 by hartwigmedical.

the class GoogleComputeEngineTest method setUp.

@Before
public void setUp() throws Exception {
    images = mock(Compute.Images.class);
    imagesFromFamily = mock(Compute.Images.GetFromFamily.class);
    when(imagesFromFamily.execute()).thenReturn(new Image());
    when(images.getFromFamily(ARGUMENTS.project(), VirtualMachineJobDefinition.STANDARD_IMAGE)).thenReturn(imagesFromFamily);
    final ArgumentCaptor<Instance> instanceArgumentCaptor = ArgumentCaptor.forClass(Instance.class);
    Operation insertOperation = mock(Operation.class);
    when(insertOperation.getName()).thenReturn("insert");
    final Compute.Instances instances = mock(Compute.Instances.class);
    lifecycleManager = mock(InstanceLifecycleManager.class);
    instance = mock(Instance.class);
    when(lifecycleManager.newInstance()).thenReturn(instance);
    when(lifecycleManager.deleteOldInstancesAndStart(instanceArgumentCaptor.capture(), any(), any())).thenReturn(insertOperation);
    when(instance.getName()).thenReturn(INSTANCE_NAME);
    Compute.Instances.Stop stop = mock(Compute.Instances.Stop.class);
    Operation stopOperation = mock(Operation.class);
    when(stopOperation.getName()).thenReturn("stop");
    when(stopOperation.getStatus()).thenReturn(DONE);
    when(stop.execute()).thenReturn(stopOperation);
    when(instances.stop(ARGUMENTS.project(), FIRST_ZONE_NAME, INSTANCE_NAME)).thenReturn(stop);
    Compute.Instances.Delete delete = mock(Compute.Instances.Delete.class);
    Operation deleteOperation = mock(Operation.class);
    when(deleteOperation.getName()).thenReturn("delete");
    when(deleteOperation.getStatus()).thenReturn(DONE);
    when(delete.execute()).thenReturn(stopOperation);
    when(instances.delete(ARGUMENTS.project(), FIRST_ZONE_NAME, INSTANCE_NAME)).thenReturn(delete);
    Compute.Instances.List list = mock(Compute.Instances.List.class);
    InstanceList instanceList = mock(InstanceList.class);
    Instance one = mock(Instance.class);
    Instance two = mock(Instance.class);
    Instance three = mock(Instance.class);
    when(one.getName()).thenReturn("vm-1");
    when(two.getName()).thenReturn("vm-2");
    when(three.getName()).thenReturn("vm-3");
    List<Instance> existingInstances = Arrays.asList(one, two, three);
    when(instances.list(any(), any())).thenReturn(list);
    when(list.execute()).thenReturn(instanceList);
    when(instanceList.getItems()).thenReturn(existingInstances);
    final Compute.ZoneOperations zoneOperations = mock(Compute.ZoneOperations.class);
    final Compute.ZoneOperations.Get zoneOpGet = mock(Compute.ZoneOperations.Get.class);
    Operation zoneOpGetOperation = mock(Operation.class);
    when(zoneOpGetOperation.getStatus()).thenReturn(DONE);
    when(zoneOpGet.execute()).thenReturn(zoneOpGetOperation);
    when(zoneOperations.get(ARGUMENTS.project(), FIRST_ZONE_NAME, "insert")).thenReturn(zoneOpGet);
    when(zoneOperations.get(ARGUMENTS.project(), FIRST_ZONE_NAME, "stop")).thenReturn(zoneOpGet);
    compute = mock(Compute.class);
    when(compute.images()).thenReturn(images);
    when(compute.instances()).thenReturn(instances);
    when(compute.zoneOperations()).thenReturn(zoneOperations);
    Compute.Zones zones = mock(Compute.Zones.class);
    Compute.Zones.List zonesList = mock(Compute.Zones.List.class);
    when(zonesList.execute()).thenReturn(new ZoneList().setItems(Lists.newArrayList(zone(FIRST_ZONE_NAME), zone(SECOND_ZONE_NAME))));
    when(zones.list(ARGUMENTS.project())).thenReturn(zonesList);
    when(compute.zones()).thenReturn(zones);
    bucketWatcher = mock(BucketCompletionWatcher.class);
    victim = new GoogleComputeEngine(ARGUMENTS, compute, z -> {
    }, lifecycleManager, bucketWatcher, Labels.of(Arguments.testDefaults(), TestInputs.defaultSomaticRunMetadata()));
    runtimeBucket = MockRuntimeBucket.test();
    jobDefinition = VirtualMachineJobDefinition.builder().name("test").namespacedResults(RESULTS_DIRECTORY).startupCommand(BashStartupScript.of(runtimeBucket.getRuntimeBucket().name())).build();
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Arrays(java.util.Arrays) Instance(com.google.api.services.compute.model.Instance) InstanceList(com.google.api.services.compute.model.InstanceList) Tags(com.google.api.services.compute.model.Tags) Arguments(com.hartwig.pipeline.Arguments) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) MockRuntimeBucket(com.hartwig.pipeline.testsupport.MockRuntimeBucket) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Zone(com.google.api.services.compute.model.Zone) AttachedDisk(com.google.api.services.compute.model.AttachedDisk) Lists(com.google.common.collect.Lists) ArgumentCaptor(org.mockito.ArgumentCaptor) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) TestInputs(com.hartwig.pipeline.testsupport.TestInputs) Map(java.util.Map) PipelineStatus(com.hartwig.pipeline.execution.PipelineStatus) Before(org.junit.Before) ZoneList(com.google.api.services.compute.model.ZoneList) State(com.hartwig.pipeline.execution.vm.BucketCompletionWatcher.State) ResultsDirectory(com.hartwig.pipeline.ResultsDirectory) Mockito.times(org.mockito.Mockito.times) IOException(java.io.IOException) NetworkInterface(com.google.api.services.compute.model.NetworkInterface) Scheduling(com.google.api.services.compute.model.Scheduling) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Operation(com.google.api.services.compute.model.Operation) Mockito.verify(org.mockito.Mockito.verify) Mockito.never(org.mockito.Mockito.never) List(java.util.List) Image(com.google.api.services.compute.model.Image) Labels(com.hartwig.pipeline.labels.Labels) Collections(java.util.Collections) Compute(com.google.api.services.compute.Compute) Mockito.mock(org.mockito.Mockito.mock) Instance(com.google.api.services.compute.model.Instance) Operation(com.google.api.services.compute.model.Operation) Image(com.google.api.services.compute.model.Image) ZoneList(com.google.api.services.compute.model.ZoneList) Compute(com.google.api.services.compute.Compute) InstanceList(com.google.api.services.compute.model.InstanceList) Before(org.junit.Before)

Aggregations

Zone (com.google.api.services.compute.model.Zone)6 Instance (com.google.api.services.compute.model.Instance)4 ZoneList (com.google.api.services.compute.model.ZoneList)4 Compute (com.google.api.services.compute.Compute)3 InstanceList (com.google.api.services.compute.model.InstanceList)3 IOException (java.io.IOException)3 Test (org.junit.Test)3 Image (com.google.api.services.compute.model.Image)2 Operation (com.google.api.services.compute.model.Operation)2 Scheduling (com.google.api.services.compute.model.Scheduling)2 Tags (com.google.api.services.compute.model.Tags)2 PipelineStatus (com.hartwig.pipeline.execution.PipelineStatus)2 AttachedDisk (com.google.api.services.compute.model.AttachedDisk)1 NetworkInterface (com.google.api.services.compute.model.NetworkInterface)1 Lists (com.google.common.collect.Lists)1 Arguments (com.hartwig.pipeline.Arguments)1 ResultsDirectory (com.hartwig.pipeline.ResultsDirectory)1 State (com.hartwig.pipeline.execution.vm.BucketCompletionWatcher.State)1 LocalSsdStorageStrategy (com.hartwig.pipeline.execution.vm.storage.LocalSsdStorageStrategy)1 PersistentStorageStrategy (com.hartwig.pipeline.execution.vm.storage.PersistentStorageStrategy)1