Skip to content

Commit b14f209

Browse files
yuyantingzerocopybara-github
authored andcommitted
Fix stockout detection in GCP vm async creation.
PiperOrigin-RevId: 886292409
1 parent 2c8f430 commit b14f209

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

perfkitbenchmarker/providers/gcp/gce_virtual_machine.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,7 @@ def __init__(self, vm_spec):
678678
self.create_disk_strategy = gce_disk_strategies.GetCreateDiskStrategy(
679679
self, None, 0
680680
)
681+
self.skip_existence_check = False
681682

682683
def _GetNetwork(self):
683684
"""Returns the GceNetwork to use."""
@@ -963,6 +964,7 @@ def _Create(self):
963964
if 'name' in stdout:
964965
response = json.loads(stdout)
965966
self.create_operation_name = response[0]['name']
967+
self.skip_existence_check = True
966968

967969
self._ParseCreateErrors(self.create_cmd.rate_limited, stderr, retcode)
968970
if not self.create_return_time:
@@ -1162,6 +1164,7 @@ def _PostCreate(self):
11621164

11631165
def _Delete(self):
11641166
"""Delete a GCE VM instance."""
1167+
self.skip_existence_check = False
11651168
delete_cmd = util.GcloudCommand(
11661169
self, 'compute', 'instances', 'delete', self.name
11671170
)
@@ -1192,6 +1195,13 @@ def _Resume(self):
11921195
)
11931196
def _Exists(self):
11941197
"""Returns true if the VM exists."""
1198+
if self.skip_existence_check:
1199+
# We use async creation for VMs.
1200+
# If the VM creation process is not yet complete,
1201+
# but the create operation exists, then we
1202+
# should skip the VM existence check and move on to checking the
1203+
# create operation status.
1204+
return True
11951205
getinstance_cmd = util.GcloudCommand(
11961206
self, 'compute', 'instances', 'describe', self.name
11971207
)

tests/gce_virtual_machine_test.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,19 @@ def testCreateRhel9CustomImage(self):
658658
self.assertEqual(vm_metadata, {**vm_metadata, **expected})
659659
self.assertNotIn('image_family', vm_metadata)
660660

661+
def testSkipExistenceCheck(self):
662+
vm_class = gce_virtual_machine.Debian11BasedGceVirtualMachine
663+
spec = gce_virtual_machine.GceVmSpec(
664+
_COMPONENT, machine_type='fake-machine-type'
665+
)
666+
with PatchCriticalObjects([('[{"name": "fake-operation"}]', '', 0)] * 10):
667+
vm = vm_class(spec) # pytype: disable=not-instantiable
668+
vm._CreateDependencies()
669+
vm._Create()
670+
self.assertTrue(vm.skip_existence_check)
671+
vm._Delete()
672+
self.assertFalse(vm.skip_existence_check)
673+
661674
def testCosVm(self):
662675
vm_class = virtual_machine.GetVmClass(provider_info.GCP, os_types.COS)
663676
spec = gce_virtual_machine.GceVmSpec(

0 commit comments

Comments
 (0)