From 8b92d3fccdc2237fadd838b659f58706c23df463 Mon Sep 17 00:00:00 2001 From: "A.J. Beamon" Date: Sun, 6 Mar 2022 21:09:20 -0800 Subject: [PATCH] Use special keys to create/delete tenants --- bindings/python/fdb/impl.py | 36 +++++++++++++------ .../python/tests/tenant_tuple_name_tests.py | 4 +-- bindings/python/tests/tester.py | 6 ++-- 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/bindings/python/fdb/impl.py b/bindings/python/fdb/impl.py index 47e22f043d..5fcefb73f3 100644 --- a/bindings/python/fdb/impl.py +++ b/bindings/python/fdb/impl.py @@ -1179,12 +1179,34 @@ class Database(_TransactionCreator): return Transaction(pointer.value, self) def allocate_tenant(self, name): - tname = process_tenant_name(name) - return FutureVoid(self.capi.fdb_database_allocate_tenant(self.dpointer, tname, len(tname))) + Database.__database_allocate_tenant(self, process_tenant_name(name), []) def delete_tenant(self, name): - tname = process_tenant_name(name) - return FutureVoid(self.capi.fdb_database_remove_tenant(self.dpointer, tname, len(tname))) + Database.__database_delete_tenant(self, process_tenant_name(name), []) + + @staticmethod + @transactional + def __database_allocate_tenant(tr, name, existence_check_marker): + tr.options.set_special_key_space_enable_writes() + key = b'\xff\xff/management/tenant_map/%s' % name + if not existence_check_marker: + existing_tenant = tr[key].wait() + existence_check_marker.append(None) + if existing_tenant != None: + raise fdb.FDBError(2132) # tenant_already_exists + tr[key] = b'' + + @staticmethod + @transactional + def __database_delete_tenant(tr, name, existence_check_marker): + tr.options.set_special_key_space_enable_writes() + key = b'\xff\xff/management/tenant_map/%s' % name + if not existence_check_marker: + existing_tenant = tr[key].wait() + existence_check_marker.append(None) + if existing_tenant == None: + raise fdb.FDBError(2131) # tenant_not_found + del tr[key] class Tenant(_TransactionCreator): @@ -1510,12 +1532,6 @@ def init_c_api(): _capi.fdb_database_set_option.restype = ctypes.c_int _capi.fdb_database_set_option.errcheck = check_error_code - _capi.fdb_database_allocate_tenant.argtypes = [ctypes.c_void_p, ctypes.c_void_p, ctypes.c_int] - _capi.fdb_database_allocate_tenant.restype = ctypes.c_void_p - - _capi.fdb_database_remove_tenant.argtypes = [ctypes.c_void_p, ctypes.c_void_p, ctypes.c_int] - _capi.fdb_database_remove_tenant.restype = ctypes.c_void_p - _capi.fdb_tenant_destroy.argtypes = [ctypes.c_void_p] _capi.fdb_tenant_destroy.restype = None diff --git a/bindings/python/tests/tenant_tuple_name_tests.py b/bindings/python/tests/tenant_tuple_name_tests.py index 3b64675c68..13af513953 100644 --- a/bindings/python/tests/tenant_tuple_name_tests.py +++ b/bindings/python/tests/tenant_tuple_name_tests.py @@ -27,7 +27,7 @@ if __name__ == '__main__': def test_tenant_tuple_name(db): tuplename=(b'test', b'level', b'hierarchy', 3, 1.24, 'str') - db.allocate_tenant(tuplename).wait() + db.allocate_tenant(tuplename) tenant=db.open_tenant(tuplename) tenant[b'foo'] = b'bar' @@ -35,7 +35,7 @@ def test_tenant_tuple_name(db): assert tenant[b'foo'] == b'bar' del tenant[b'foo'] - db.delete_tenant(tuplename).wait() + db.delete_tenant(tuplename) # Expect a cluster file as input. This test will write to the FDB cluster, so # be aware of potential side effects. diff --git a/bindings/python/tests/tester.py b/bindings/python/tests/tester.py index 3f1f3f10d9..e1a559b396 100644 --- a/bindings/python/tests/tester.py +++ b/bindings/python/tests/tester.py @@ -593,10 +593,12 @@ class Tester: inst.push(b"WAITED_FOR_EMPTY") elif inst.op == six.u("TENANT_CREATE"): name = inst.pop() - inst.push(self.db.allocate_tenant(name)) + self.db.allocate_tenant(name) + inst.push(b"RESULT_NOT_PRESENT") elif inst.op == six.u("TENANT_DELETE"): name = inst.pop() - inst.push(self.db.delete_tenant(name)) + self.db.delete_tenant(name) + inst.push(b"RESULT_NOT_PRESENT") elif inst.op == six.u("TENANT_SET_ACTIVE"): name = inst.pop() self.tenant = self.db.open_tenant(name)