Beispiel #1
0
 async def create_database(self) -> None:
     self._database_status = ServiceStatus.IN_PROGRESS
     try:
         self._database = await self._database_module.create_database(
             self._db_config.config, self._model_settings.collection,
             self._model, self._model_settings.create_schema)
     except DataAccessError:
         raise
     except Exception as e:
         raise DataAccessError(f"Unexpected message happened: {e}")
     else:
         self._database_status = ServiceStatus.READY
     finally:
         if not self._database_status == ServiceStatus.READY:
             # attempt to create database was unsuccessful. Setup initial state
             self._database_status = ServiceStatus.NOT_CREATED
         self._event.set()  # weak up other waiting coroutines
Beispiel #2
0
    async def create_database(cls,
                              config,
                              collection: str,
                              model: Type[BaseModel],
                              create_schema: bool = True) -> IDataBase:
        """
        Creates new instance of Consul KV DB and performs necessary initializations.

        :param DBSettings config: configuration for consul kv server
        :param str collection: collection for storing model onto db
        :param Type[BaseModel] model: model which instances will be stored in DB
        :param bool create_schema: if the flag is true, the collection will be created.
        :return:
        """
        # NOTE: please, be sure that you avoid using this method twice (or more times) for the same
        # model
        if not all((cls.consul_client, cls.thread_pool, cls.loop)):
            cls.loop = asyncio.get_event_loop()
            try:
                cls.consul_client = Consul(host=config.hosts[0],
                                           port=config.port,
                                           loop=cls.loop)
            except ConnectionRefusedError as e:
                raise DataAccessExternalError(f"{e}")
            # needed to perform tree traversal in non-blocking mode
            cls.thread_pool = ThreadPoolExecutor(
                max_workers=multiprocessing.cpu_count())

        consul_db = cls(cls.consul_client, model, collection, cls.thread_pool,
                        cls.loop)

        try:
            if create_schema:
                await consul_db.create_object_root()
        except ClientConnectorError as e:
            raise DataAccessExternalError(f"{e}")
        except Exception as e:
            raise DataAccessError(
                f"Some unknown exception occurred in Consul module: {e}")

        return consul_db
Beispiel #3
0
 def __init__(self, model: Type[BaseModel], model_config: DBModelConfig,
              db_config: GeneralConfig):
     self._event = ThreadSafeEvent()
     self._model = model
     self._model_settings = model_config.config.get(model_config.database)
     self._db_config = db_config.databases.get(model_config.database)
     self._database_status = ServiceStatus.NOT_CREATED
     # TODO: Generate this list automatically by listing directories
     db_modules = {
         'ConsulDB': 'consul_db',
         'ElasticSearchDB': 'elasticsearch_db',
         'OpenLdap': 'openldap'
     }
     db_type = self._db_config.import_path
     if db_type not in db_modules.keys():
         raise DataAccessError("No valid db found")
     db_module = db_modules[db_type]
     self._database_module = getattr(
         __import__(f'cortx.utils.data.db.{db_module}', fromlist=[db_type]),
         db_type)
     self._database = None