Exemple #1
0
    def start(self,
              is_block=False) -> None:
        """
        Start the AI flow master.

        :param is_block: AI flow master will run non-stop if True.
        """
        if self.config_file is not None:
            self.master_config.load_from_file(self.config_file)
        else:
            self.master_config.set_master_port(str(_PORT))
        global GLOBAL_MASTER_CONFIG
        GLOBAL_MASTER_CONFIG = self.master_config
        logging.info("AI Flow Master Config {}".format(GLOBAL_MASTER_CONFIG))
        if not self.master_config.get_enable_ha():
            self.server = AIFlowServer(
                store_uri=self.master_config.get_db_uri(),
                port=str(self.master_config.get_master_port()),
                start_default_notification=self.master_config.start_default_notification(),
                notification_uri=self.master_config.get_notification_uri())
        else:
            self.server = HighAvailableAIFlowServer(
                store_uri=self.master_config.get_db_uri(),
                port=str(self.master_config.get_master_port()),
                start_default_notification=self.master_config.start_default_notification(),
                notification_uri=self.master_config.get_notification_uri(),
                server_uri=self.master_config.get_master_ip() + ":" + self.master_config.get_master_port(),
                ttl_ms=self.master_config.get_ha_ttl_ms())
        self.server.run(is_block=is_block)
Exemple #2
0
 def start_aiflow_server(host, port):
     port = str(port)
     server_uri = host + ":" + port
     server = HighAvailableAIFlowServer(
         store_uri=_SQLITE_DB_URI, port=port,
         server_uri=server_uri)
     server.run()
     return server
Exemple #3
0
 def setUp(self) -> None:
     SqlAlchemyStore(_SQLITE_DB_URI)
     self.server1 = HighAvailableAIFlowServer(
         store_uri=_SQLITE_DB_URI, port=50051,
         server_uri='localhost:50051')
     self.server1.run()
     self.server2 = None
     self.server3 = None
     self.config = ProjectConfig()
     self.config.set_enable_ha(True)
     self.client = AIFlowClient(server_uri='localhost:50052,localhost:50051', project_config=self.config)
Exemple #4
0
class AIFlowMaster(object):
    """
    AI flow master.
    """
    def __init__(self, config_file: Text = None, enable_ha=False, server_uri: str = None, ttl_ms=10000) -> None:
        """
        Set the master attribute according to the master config file.

        :param config_file: master configuration file.
        """
        super().__init__()
        self.config_file = config_file
        self.server = None
        self.master_config = MasterConfig()
        self.enable_ha = enable_ha
        self.server_uri = server_uri
        self.ttl_ms = ttl_ms

    def start(self,
              is_block=False) -> None:
        """
        Start the AI flow master.

        :param is_block: AI flow master will run non-stop if True.
        """
        if self.config_file is not None:
            self.master_config.load_from_file(self.config_file)
        else:
            self.master_config.set_master_port(str(_PORT))
        global GLOBAL_MASTER_CONFIG
        GLOBAL_MASTER_CONFIG = self.master_config
        logging.info("AI Flow Master Config {}".format(GLOBAL_MASTER_CONFIG))
        if not self.master_config.get_enable_ha():
            self.server = AIFlowServer(
                store_uri=self.master_config.get_db_uri(),
                port=str(self.master_config.get_master_port()),
                start_default_notification=self.master_config.start_default_notification(),
                notification_uri=self.master_config.get_notification_uri())
        else:
            self.server = HighAvailableAIFlowServer(
                store_uri=self.master_config.get_db_uri(),
                port=str(self.master_config.get_master_port()),
                start_default_notification=self.master_config.start_default_notification(),
                notification_uri=self.master_config.get_notification_uri(),
                server_uri=self.master_config.get_master_ip() + ":" + self.master_config.get_master_port(),
                ttl_ms=self.master_config.get_ha_ttl_ms())
        self.server.run(is_block=is_block)

    def stop(self, clear_sql_lite_db_file=True) -> None:
        """
        Stop the AI flow master.

        :param clear_sql_lite_db_file: If True, the sqlite database files will be deleted When the server stops working.
        """
        self.server.stop()
        if self.master_config.get_db_type() == DBType.SQLITE and clear_sql_lite_db_file:
            store = SqlAlchemyStore(self.master_config.get_db_uri())
            base.metadata.drop_all(store.db_engine)
            os.remove(self.master_config.get_sql_lite_db_file())

    def _clear_db(self):
        if self.master_config.get_db_type() == DBType.SQLITE:
            store = SqlAlchemyStore(self.master_config.get_db_uri())
            base.metadata.drop_all(store.db_engine)
            base.metadata.create_all(store.db_engine)
Exemple #5
0
class TestHighAvailableAIFlowServer(unittest.TestCase):

    @staticmethod
    def start_aiflow_server(host, port):
        port = str(port)
        server_uri = host + ":" + port
        server = HighAvailableAIFlowServer(
            store_uri=_SQLITE_DB_URI, port=port,
            server_uri=server_uri)
        server.run()
        return server

    def wait_for_new_members_detected(self, new_member_uri):
        for i in range(100):
            living_member = self.client.living_aiflow_members
            if new_member_uri in living_member:
                break
            else:
                time.sleep(0.1)

    def setUp(self) -> None:
        SqlAlchemyStore(_SQLITE_DB_URI)
        self.server1 = HighAvailableAIFlowServer(
            store_uri=_SQLITE_DB_URI, port=50051,
            server_uri='localhost:50051')
        self.server1.run()
        self.server2 = None
        self.server3 = None
        self.config = ProjectConfig()
        self.config.set_enable_ha(True)
        self.client = AIFlowClient(server_uri='localhost:50052,localhost:50051', project_config=self.config)

    def tearDown(self) -> None:
        self.client.stop_listen_event()
        self.client.disable_high_availability()
        if self.server1 is not None:
            self.server1.stop()
        if self.server2 is not None:
            self.server2.stop()
        if self.server3 is not None:
            self.server3.stop()
        store = SqlAlchemyStore(_SQLITE_DB_URI)
        base.metadata.drop_all(store.db_engine)

    def test_server_change(self) -> None:
        self.client.register_project("test_project")
        projects = self.client.list_project(10, 0)
        self.assertEqual(self.client.current_aiflow_uri, "localhost:50051")
        self.assertEqual(projects[0].name, "test_project")

        self.server2 = self.start_aiflow_server("localhost", 50052)
        self.wait_for_new_members_detected("localhost:50052")
        self.server1.stop()
        projects = self.client.list_project(10, 0)
        self.assertEqual(self.client.current_aiflow_uri, "localhost:50052")
        self.assertEqual(projects[0].name, "test_project")

        self.server3 = self.start_aiflow_server("localhost", 50053)
        self.wait_for_new_members_detected("localhost:50053")
        self.server2.stop()
        projects = self.client.list_project(10, 0)
        self.assertEqual(self.client.current_aiflow_uri, "localhost:50053")
        self.assertEqual(projects[0].name, "test_project")