add hack code

This commit is contained in:
Nico Schottelius 2020-01-15 10:02:37 +01:00
parent 12e8ccd01c
commit b877ab13b3
2 changed files with 23 additions and 6 deletions

View file

@ -86,10 +86,23 @@ class DB(object):
return self._db_clients[0].put(self.realkey(key), value, **kwargs) return self._db_clients[0].put(self.realkey(key), value, **kwargs)
@readable_errors @readable_errors
def increment(key, **kwargs): def increment(self, key, **kwargs):
with self._db_clients[0].lock(key) as lock: print(self.realkey(key))
value = int(self.get(self.realkey(key), **kwargs))
self.set(self.realkey(key), str(value + 1), **kwargs)
print("prelock")
lock = self._db_clients[0].lock('/nicohack/foo')
print("prelockacq")
lock.acquire()
print("prelockrelease")
lock.release()
with self._db_clients[0].lock("/nicohack/mac/last_used_index") as lock:
print("in lock")
pass
# with self._db_clients[0].lock(self.realkey(key)) as lock:# value = int(self.get(self.realkey(key), **kwargs))
# self.set(self.realkey(key), str(value + 1), **kwargs)
if __name__ == '__main__': if __name__ == '__main__':

View file

@ -48,7 +48,9 @@ class MAC(object):
def last_used_index(self): def last_used_index(self):
value = self.db.get("last_used_index") value = self.db.get("last_used_index")
if not value: if not value:
return 0 self.db.set("last_used_index", "0")
value = self.db.get("last_used_index")
return int(value) return int(value)
def last_used_mac(self): def last_used_mac(self):
@ -62,7 +64,7 @@ class MAC(object):
def get_next(self, vmuuid=None, as_int=False): def get_next(self, vmuuid=None, as_int=False):
last_number = self.last_used_index() last_number = self.last_used_index()
# FIXME: compare to 48bit minus prefix length # FIXME: compare to 48bit minus prefix length to the power of 2
if last_number == int('0xffffff', 16): if last_number == int('0xffffff', 16):
raise UncloudException("Exhausted all possible mac addresses - try to free some") raise UncloudException("Exhausted all possible mac addresses - try to free some")
@ -77,6 +79,8 @@ class MAC(object):
db_entry['index'] = next_number db_entry['index'] = next_number
db_entry['mac_address'] = next_mac db_entry['mac_address'] = next_mac
# should be one transaction
self.db.increment("last_used_index")
self.db.set("used/{}".format(next_mac), self.db.set("used/{}".format(next_mac),
db_entry, as_json=True) db_entry, as_json=True)