Package ganeti :: Package config :: Module temporary_reservations
[hide private]
[frames] | no frames]

Source Code for Module ganeti.config.temporary_reservations

  1  # 
  2  # 
  3   
  4  # Copyright (C) 2014 Google Inc. 
  5  # All rights reserved. 
  6  # 
  7  # Redistribution and use in source and binary forms, with or without 
  8  # modification, are permitted provided that the following conditions are 
  9  # met: 
 10  # 
 11  # 1. Redistributions of source code must retain the above copyright notice, 
 12  # this list of conditions and the following disclaimer. 
 13  # 
 14  # 2. Redistributions in binary form must reproduce the above copyright 
 15  # notice, this list of conditions and the following disclaimer in the 
 16  # documentation and/or other materials provided with the distribution. 
 17  # 
 18  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 
 19  # IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 
 20  # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
 21  # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 
 22  # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
 23  # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
 24  # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
 25  # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
 26  # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
 27  # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 28  # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 29   
 30   
 31  """Reserve resources, so that jobs can't take them. 
 32   
 33  """ 
 34   
 35  from ganeti import errors 
 36   
 37   
38 -class TemporaryReservationManager(object):
39 """A temporary resource reservation manager. 40 41 This is used to reserve resources in a job, before using them, making sure 42 other jobs cannot get them in the meantime. 43 44 """
45 - def __init__(self):
46 self._ec_reserved = {}
47
48 - def Reserved(self, resource):
49 for holder_reserved in self._ec_reserved.values(): 50 if resource in holder_reserved: 51 return True 52 return False
53
54 - def Reserve(self, ec_id, resource):
55 if self.Reserved(resource): 56 raise errors.ReservationError("Duplicate reservation for resource '%s'" 57 % str(resource)) 58 if ec_id not in self._ec_reserved: 59 self._ec_reserved[ec_id] = set([resource]) 60 else: 61 self._ec_reserved[ec_id].add(resource)
62
63 - def DropECReservations(self, ec_id):
64 if ec_id in self._ec_reserved: 65 del self._ec_reserved[ec_id]
66
67 - def GetReserved(self):
68 all_reserved = set() 69 for holder_reserved in self._ec_reserved.values(): 70 all_reserved.update(holder_reserved) 71 return all_reserved
72
73 - def GetECReserved(self, ec_id):
74 """ Used when you want to retrieve all reservations for a specific 75 execution context. E.g when commiting reserved IPs for a specific 76 network. 77 78 """ 79 ec_reserved = set() 80 if ec_id in self._ec_reserved: 81 ec_reserved.update(self._ec_reserved[ec_id]) 82 return ec_reserved
83
84 - def Generate(self, existing, generate_one_fn, ec_id):
85 """Generate a new resource of this type 86 87 """ 88 assert callable(generate_one_fn) 89 90 all_elems = self.GetReserved() 91 all_elems.update(existing) 92 retries = 64 93 while retries > 0: 94 new_resource = generate_one_fn() 95 if new_resource is not None and new_resource not in all_elems: 96 break 97 else: 98 raise errors.ConfigurationError("Not able generate new resource" 99 " (last tried: %s)" % new_resource) 100 self.Reserve(ec_id, new_resource) 101 return new_resource
102