Commit 013da361 authored by Guido Trotter's avatar Guido Trotter
Browse files

TemporaryReservationManager


Signed-off-by: default avatarGuido Trotter <ultrotter@google.com>
Reviewed-by: default avatarIustin Pop <iustin@google.com>
parent 0debfb35
......@@ -64,6 +64,61 @@ def _ValidateConfig(data):
constants.CONFIG_VERSION))
class TemporaryReservationManager:
"""A temporary resource reservation manager.
This is used to reserve resources in a job, before using them, making sure
other jobs cannot get them in the meantime.
"""
def __init__(self):
self._ec_reserved = {}
def Reserved(self, resource):
for holder_reserved in self._ec_reserved.items():
if resource in holder_reserved:
return True
return False
def Reserve(self, ec_id, resource):
if self.Reserved(resource):
raise errors.ReservationError("Duplicate reservation for resource: %s." %
(resource))
if ec_id not in self._ec_reserved:
self._ec_reserved[ec_id] = set([resource])
else:
self._ec_reserved[ec_id].add(resource)
def DropECReservations(self, ec_id):
if ec_id in self._ec_reserved:
del self._ec_reserved[ec_id]
def GetReserved(self):
all_reserved = set()
for holder_reserved in self._ec_reserved.values():
all_reserved.update(holder_reserved)
return all_reserved
def Generate(self, existing, generate_one_fn, ec_id):
"""Generate a new resource of this type
"""
assert callable(generate_one_fn)
all_elems = self.GetReserved()
all_elems.update(existing)
retries = 64
while retries > 0:
new_resource = generate_one_fn()
if new_resource is not None and new_resource not in all_elems:
break
else:
raise errors.ConfigurationError("Not able generate new resource"
" (last tried: %s)" % new_resource)
self.Reserve(ec_id, new_resource)
return new_resource
class ConfigWriter:
"""The interface to the cluster configuration.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment