From b877ab13b34b058540fc10fabde67501be1b79f8 Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Wed, 15 Jan 2020 10:02:37 +0100 Subject: [PATCH] add hack code --- uncloud/hack/db.py | 21 +++++++++++++++++---- uncloud/hack/mac.py | 8 ++++++-- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/uncloud/hack/db.py b/uncloud/hack/db.py index ac643bd..cb5e490 100644 --- a/uncloud/hack/db.py +++ b/uncloud/hack/db.py @@ -86,10 +86,23 @@ class DB(object): return self._db_clients[0].put(self.realkey(key), value, **kwargs) @readable_errors - def increment(key, **kwargs): - with self._db_clients[0].lock(key) as lock: - value = int(self.get(self.realkey(key), **kwargs)) - self.set(self.realkey(key), str(value + 1), **kwargs) + def increment(self, key, **kwargs): + print(self.realkey(key)) + + + 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__': diff --git a/uncloud/hack/mac.py b/uncloud/hack/mac.py index a408103..e7f41a2 100755 --- a/uncloud/hack/mac.py +++ b/uncloud/hack/mac.py @@ -48,7 +48,9 @@ class MAC(object): def last_used_index(self): value = self.db.get("last_used_index") if not value: - return 0 + self.db.set("last_used_index", "0") + value = self.db.get("last_used_index") + return int(value) def last_used_mac(self): @@ -62,7 +64,7 @@ class MAC(object): def get_next(self, vmuuid=None, as_int=False): 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): 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['mac_address'] = next_mac + # should be one transaction + self.db.increment("last_used_index") self.db.set("used/{}".format(next_mac), db_entry, as_json=True)