fixed some backend init issues and error handling

Signed-off-by: Jürgen Eckel <juergen@riddleandcode.com>
This commit is contained in:
Jürgen Eckel 2022-04-19 14:07:19 +02:00
parent f85af010ea
commit b7c4acd830
3 changed files with 90 additions and 72 deletions

View File

@ -6,6 +6,7 @@
import logging import logging
from importlib import import_module from importlib import import_module
from planetmint.config import Config from planetmint.config import Config
from planetmint.common.exceptions import ConfigurationError
BACKENDS = { # This is path to MongoDBClass BACKENDS = { # This is path to MongoDBClass
'tarantool_db': 'planetmint.backend.tarantool.connection.TarantoolDB', 'tarantool_db': 'planetmint.backend.tarantool.connection.TarantoolDB',
@ -30,6 +31,7 @@ def Connection(host: str = None, port: int = None, login: str = None, password:
port = port or Config().get()['database']['port'] if not kwargs.get("port") else kwargs["port"] port = port or Config().get()['database']['port'] if not kwargs.get("port") else kwargs["port"]
login = login or Config().get()["database"]["login"] if not kwargs.get("login") else kwargs["login"] login = login or Config().get()["database"]["login"] if not kwargs.get("login") else kwargs["login"]
password = password or Config().get()["database"]["password"] password = password or Config().get()["database"]["password"]
try:
if backend == "tarantool_db": if backend == "tarantool_db":
modulepath, _, class_name = BACKENDS[backend].rpartition('.') modulepath, _, class_name = BACKENDS[backend].rpartition('.')
Class = getattr(import_module(modulepath), class_name) Class = getattr(import_module(modulepath), class_name)
@ -63,7 +65,9 @@ def Connection(host: str = None, port: int = None, login: str = None, password:
replicaset=replicaset, ssl=ssl, login=login, password=password, replicaset=replicaset, ssl=ssl, login=login, password=password,
ca_cert=ca_cert, certfile=certfile, keyfile=keyfile, ca_cert=ca_cert, certfile=certfile, keyfile=keyfile,
keyfile_passphrase=keyfile_passphrase, crlfile=crlfile) keyfile_passphrase=keyfile_passphrase, crlfile=crlfile)
except:
logger.info('Exception in _connect(): {}')
raise ConfigurationError
def _kwargs_parser(key, kwargs): def _kwargs_parser(key, kwargs):
if kwargs.get(key): if kwargs.get(key):

View File

@ -7,6 +7,7 @@ import logging
import tarantool import tarantool
from planetmint.config import Config from planetmint.config import Config
from planetmint.common.exceptions import ConfigurationError
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -14,13 +15,16 @@ logger = logging.getLogger(__name__)
class TarantoolDB: class TarantoolDB:
def __init__(self, host: str = "localhost", port: int = 3303, user: str = None, password: str = None, def __init__(self, host: str = "localhost", port: int = 3303, user: str = None, password: str = None,
reset_database: bool = False): reset_database: bool = False):
try:
self.host = host self.host = host
self.port = port self.port = port
# TODO add user support later on # TODO add user support later on
print(f"host : {host}") print(f"host : {host}")
print(f"port : {port}") print(f"port : {port}")
# self.db_connect = tarantool.connect(host=host, port=port, user=user, password=password) # self.db_connect = tarantool.connect(host=host, port=port, user=user, password=password)
#TODO : raise configuraiton error if the connection cannot be established
self.db_connect = tarantool.connect(host=self.host, port=self.port) self.db_connect = tarantool.connect(host=self.host, port=self.port)
print( f"connection : {self.db_connect}")
self.init_path = Config().get()["database"]["init_config"]["absolute_path"] self.init_path = Config().get()["database"]["init_config"]["absolute_path"]
self.drop_path = Config().get()["database"]["drop_config"]["absolute_path"] self.drop_path = Config().get()["database"]["drop_config"]["absolute_path"]
if reset_database: if reset_database:
@ -30,6 +34,10 @@ class TarantoolDB:
self.SPACE_NAMES = ["abci_chains", "assets", "blocks", "blocks_tx", self.SPACE_NAMES = ["abci_chains", "assets", "blocks", "blocks_tx",
"elections", "meta_data", "pre_commits", "validators", "elections", "meta_data", "pre_commits", "validators",
"transactions", "inputs", "outputs", "keys"] "transactions", "inputs", "outputs", "keys"]
except:
logger.info('Exception in _connect(): {}')
raise ConfigurationError
def _reconnect(self): def _reconnect(self):
self.db_connect = tarantool.connect(host=self.host, port=self.port) self.db_connect = tarantool.connect(host=self.host, port=self.port)

View File

@ -113,16 +113,23 @@ def _configure_planetmint(request):
@pytest.fixture(scope='session') @pytest.fixture(scope='session')
def _setup_database(_configure_planetmint): # TODO Here is located setup database def _setup_database(_configure_planetmint): # TODO Here is located setup database
# from planetmint.backend.connection_tarantool import init_tarantool, drop_tarantool from planetmint.backend.tarantool.connection import TarantoolDB
# print('Initializing test db')
# init_tarantool()
# print('Finishing init database') print('Deleting `{}` database')
db_conn = TarantoolDB("localhost", 3303)
db_conn.drop_database()
db_conn.init_database()
print('Finished deleting ``')
yield yield
# print('Deleting `{}` database')
# drop_tarantool() print('Initializing test db')
# print('Finished deleting ``') db_conn2 = TarantoolDB("localhost", 3303)
db_conn2.drop_database()
print('Finishing init database')
@pytest.fixture @pytest.fixture
@ -130,8 +137,7 @@ def _bdb(_setup_database ):
from planetmint.backend import Connection from planetmint.backend import Connection
from planetmint.common.memoize import to_dict, from_dict from planetmint.common.memoize import to_dict, from_dict
from planetmint.models import Transaction from planetmint.models import Transaction
#conn = Connection( backend='tarantool_db', port=3301, host='localhost', login='guest') conn = Connection()
#conn = Connection()
yield yield
to_dict.cache_clear() to_dict.cache_clear()
@ -510,14 +516,14 @@ def unspent_outputs(unspent_output_0, unspent_output_1, unspent_output_2):
return unspent_output_0, unspent_output_1, unspent_output_2 return unspent_output_0, unspent_output_1, unspent_output_2
@pytest.fixture #@pytest.fixture
def mongo_client(db_context): # TODO Here add TarantoolConnectionClass #def mongo_client(db_context): # TODO Here add TarantoolConnectionClass
return None # MongoClient(host=db_context.host, port=db_context.port) # return None # MongoClient(host=db_context.host, port=db_context.port)
#
#
@pytest.fixture #@pytest.fixture
def utxo_collection(db_context, mongo_client): #def utxo_collection(db_context, mongo_client):
return mongo_client[db_context.name].utxos # return mongo_client[db_context.name].utxos
@pytest.fixture @pytest.fixture
@ -529,12 +535,12 @@ def dummy_unspent_outputs():
] ]
@pytest.fixture #@pytest.fixture
def utxoset(dummy_unspent_outputs, utxo_collection): #def utxoset(dummy_unspent_outputs, utxo_collection):
res = utxo_collection.insert_many(copy.deepcopy(dummy_unspent_outputs)) # res = utxo_collection.insert_many(copy.deepcopy(dummy_unspent_outputs))
assert res.acknowledged # assert res.acknowledged
assert len(res.inserted_ids) == 3 # assert len(res.inserted_ids) == 3
return dummy_unspent_outputs, utxo_collection # return dummy_unspent_outputs, utxo_collection
@pytest.fixture @pytest.fixture