예제 #1
0
def prepare_data():
    config = Config()
    config.max_connection_pool_size = 1
    # init connection pool
    connection_pool = ConnectionPool()
    # the graphd server's address
    assert connection_pool.init([('127.0.0.1', 9671)], config)
    client = connection_pool.get_session('root', 'nebula')
    client.execute(
        'CREATE SPACE IF NOT EXISTS ScanSpace('
        'PARTITION_NUM=10,'
        'vid_type=FIXED_STRING(20));'
        'USE ScanSpace;'
        'CREATE TAG IF NOT EXISTS person(name string, age int);'
        'CREATE EDGE IF NOT EXISTS friend(start int, end int);'
    )
    time.sleep(5)

    for id in range(20):
        vid = 'person' + str(id)
        cmd = 'INSERT VERTEX person(name, age) ' 'VALUES \"{}\":(\"{}\", {})'.format(
            vid, vid, id
        )
        client.execute(cmd)
    for id in range(20):
        src_id = 'person' + str(id)
        dst_id = 'person' + str(20 - id)
        start = random.randint(2000, 2010)
        end = random.randint(2010, 2020)
        cmd = 'INSERT EDGE friend(start, end) ' 'VALUES \"{}\"->\"{}\":({}, {})'.format(
            src_id, dst_id, start, end
        )
        client.execute(cmd)
    client.release()
    connection_pool.close()
예제 #2
0
    def test_init_failed(self):
        # init succeeded
        pool1 = ConnectionPool()
        addresses = list()
        addresses.append(('127.0.0.1', 9669))
        addresses.append(('127.0.0.1', 9670))
        assert pool1.init(addresses, Config())

        # init failed, connected failed
        pool2 = ConnectionPool()
        addresses = list()
        addresses.append(('127.0.0.1', 3800))
        try:
            pool2.init(addresses, Config())
            assert False
        except Exception:
            assert True

        # init failed, hostname not existed
        try:
            pool3 = ConnectionPool()
            addresses = list()
            addresses.append(('not_exist_hostname', 3800))
            assert not pool3.init(addresses, Config())
        except InValidHostname:
            assert True, "We expected get the exception"
예제 #3
0
def test_multi_thread():
    # Test multi thread
    addresses = [('127.0.0.1', 9669), ('127.0.0.1', 9670)]
    configs = Config()
    configs.max_connection_pool_size = 4
    pool = ConnectionPool()
    assert pool.init(addresses, configs)

    global success_flag
    success_flag = True

    def main_test():
        session = None
        global success_flag
        try:
            session = pool.get_session('root', 'nebula')
            if session is None:
                success_flag = False
                return
            space_name = 'space_' + threading.current_thread().getName()

            session.execute('DROP SPACE %s' % space_name)
            resp = session.execute(
                'CREATE SPACE IF NOT EXISTS %s(vid_type=FIXED_STRING(8))' % space_name
            )
            if not resp.is_succeeded():
                raise RuntimeError('CREATE SPACE failed: {}'.format(resp.error_msg()))

            time.sleep(3)
            resp = session.execute('USE %s' % space_name)
            if not resp.is_succeeded():
                raise RuntimeError('USE SPACE failed:{}'.format(resp.error_msg()))

        except Exception as x:
            print(x)
            success_flag = False
            return
        finally:
            if session is not None:
                session.release()

    thread1 = threading.Thread(target=main_test, name='thread1')
    thread2 = threading.Thread(target=main_test, name='thread2')
    thread3 = threading.Thread(target=main_test, name='thread3')
    thread4 = threading.Thread(target=main_test, name='thread4')

    thread1.start()
    thread2.start()
    thread3.start()
    thread4.start()

    thread1.join()
    thread2.join()
    thread3.join()
    thread4.join()

    pool.close()
    assert success_flag
예제 #4
0
def get_conn_pool(host: str, port: int, ssl_config: SSL_config):
    config = Config()
    config.max_connection_pool_size = 20
    config.timeout = 180000
    # init connection pool
    pool = ConnectionPool()
    if not pool.init([(host, port)], config, ssl_config):
        raise Exception("Fail to init connection pool.")
    return pool
예제 #5
0
    def start_standalone(self):
        os.chdir(self.work_dir)
        start_time = time.time()
        for p in self.all_processes:
            print('start stand alone process')
            p.start()

        config = Config()
        config.max_connection_pool_size = 20
        config.timeout = 60000
        # init connection pool
        client_pool = ConnectionPool()
        # assert client_pool.init([("127.0.0.1", int(self.graphd_port))], config)
        ssl_config = get_ssl_config(self.is_graph_ssl, self.ca_signed)
        print("begin to add hosts")
        ok = False
        # wait graph is ready, and then add hosts
        for _ in range(20):
            try:
                ok = client_pool.init(
                    [("127.0.0.1", self.graphd_processes[0].tcp_port)],
                    config,
                    ssl_config,
                )
                if ok:
                    break
            except:
                pass
            time.sleep(1)

        assert ok, "graph is not ready"
        # get session from the pool
        client = client_pool.get_session('root', 'nebula')

        hosts = ",".join([
            "127.0.0.1:{}".format(str(storaged.storage_port))
            for storaged in self.graphd_processes
        ])
        cmd = "ADD HOSTS {}".format(hosts)
        print("add hosts cmd is {}".format(cmd))
        resp = client.execute(cmd)
        assert resp.is_succeeded(), resp.error_msg()
        client.release()

        # wait nebula start
        server_ports = [p.tcp_port for p in self.all_processes]
        if not self._check_servers_status(server_ports):
            self._collect_pids()
            self.kill_all(signal.SIGKILL)
            elapse = time.time() - start_time
            raise Exception(f'nebula servers not ready in {elapse}s')

        self._collect_pids()

        return [p.tcp_port for p in self.graphd_processes]
예제 #6
0
 def test_4_timeout(self):
     try:
         configs = Config()
         configs.timeout = 100
         configs.max_connection_pool_size = 1
         pool = ConnectionPool()
         assert pool.init([('127.0.0.1', 9669)], configs)
         session = pool.get_session(self.user_name, self.password)
         ngql = ''
         for n in range(0, 500):
             ngql = ngql + 'show hosts;'
         session.execute(ngql)
         assert False, 'expect to get exception'
     except Exception as ex:
         assert str(ex).find('timed out') > 0
         assert True, ex
예제 #7
0
    def setup_class(self):
        self.addresses = list()
        self.addresses.append(('127.0.0.1', 9669))
        self.configs = Config()
        self.configs.min_connection_pool_size = 2
        self.configs.max_connection_pool_size = 4
        self.configs.idle_time = 2000
        self.configs.interval_check = 2

        # set SSL config
        self.ssl_config = SSL_config()
        self.ssl_config.cert_reqs = ssl.CERT_OPTIONAL
        self.ssl_config.ca_certs = os.path.join(current_dir,
                                                'secrets/test.ca.pem')
        self.ssl_config.keyfile = os.path.join(current_dir,
                                               'secrets/test.client.key')
        self.ssl_config.certfile = os.path.join(current_dir,
                                                'secrets/test.client.crt')
        # self signed SSL config
        self.ssl_selfs_signed_config = SSL_config()
        self.ssl_selfs_signed_config.cert_reqs = ssl.CERT_OPTIONAL
        self.ssl_selfs_signed_config.ca_certs = os.path.join(
            current_dir, 'secrets/test.self-signed.pem')
        self.ssl_selfs_signed_config.keyfile = os.path.join(
            current_dir, 'secrets/test.self-signed.key')
        self.ssl_selfs_signed_config.certfile = os.path.join(
            current_dir, 'secrets/test.self-signed.pem')
예제 #8
0
 def test_wrong_hostname(self):
     pool = ConnectionPool()
     try:
         pool.init([('wrong_host', 9669)], Config())
         assert False
     except InValidHostname:
         assert True
예제 #9
0
 def test_timeout(self):
     config = Config()
     config.timeout = 1000
     config.max_connection_pool_size = 1
     pool = ConnectionPool()
     assert pool.init([('127.0.0.1', 9669)], config)
     session = pool.get_session('root', 'nebula')
     try:
         resp = session.execute(
             'USE nba;GO 1000 STEPS FROM \"Tim Duncan\" OVER like'
         )
         assert False
     except IOErrorException as e:
         assert True
         assert str(e).find("Read timed out")
     session.release()
     try:
         session = pool.get_session('root', 'nebula')
     except IOErrorException as e:
         assert False
예제 #10
0
 def setup_class(self):
     self.user_name = 'root'
     self.password = '******'
     self.configs = Config()
     self.configs.max_connection_pool_size = 6
     self.pool = ConnectionPool()
     assert self.pool.init(
         [('127.0.0.1', 9669), ('127.0.0.1', 9670), ('127.0.0.1', 9671)],
         self.configs,
     )
     assert self.pool.connects() == 0
     assert self.pool.in_used_connects() == 0
예제 #11
0
 def setup_class(self):
     self.addresses = list()
     self.addresses.append(('127.0.0.1', 9669))
     self.addresses.append(('127.0.0.1', 9670))
     self.configs = Config()
     self.configs.min_connection_pool_size = 2
     self.configs.max_connection_pool_size = 4
     self.configs.idle_time = 2000
     self.configs.interval_check = 2
     self.pool = ConnectionPool()
     assert self.pool.init(self.addresses, self.configs)
     assert self.pool.connects() == 2
예제 #12
0
    def setUp(self) -> None:
        super().setUpClass()
        self.user_name = 'root'
        self.password = '******'
        self.configs = Config()
        self.configs.max_connection_pool_size = 6
        self.pool = ConnectionPool()
        self.pool.init([('127.0.0.1', 9671)], self.configs)

        # get session from the pool
        client = self.pool.get_session('root', 'nebula')
        assert client is not None

        # prepare space and insert data
        resp = client.execute(
            'CREATE SPACE IF NOT EXISTS parameter_test(vid_type=FIXED_STRING(30));USE parameter_test'
        )
        assert resp.is_succeeded(), resp.error_msg()
        resp = client.execute(
            'CREATE TAG IF NOT EXISTS person(name string, age int);'
            'CREATE EDGE like (likeness double);')

        time.sleep(6)
        # insert data need to sleep after create schema
        resp = client.execute(
            'CREATE TAG INDEX person_age_index on person(age)')
        time.sleep(6)
        # insert vertex
        resp = client.execute(
            'INSERT VERTEX person(name, age) VALUES "Bob":("Bob", 10), "Lily":("Lily", 9)'
        )
        assert resp.is_succeeded(), resp.error_msg()
        # insert edges
        resp = client.execute(
            'INSERT EDGE like(likeness) VALUES "Bob"->"Lily":(80.0);')
        assert resp.is_succeeded(), resp.error_msg()
        resp = client.execute('REBUILD TAG INDEX person_age_index')
        assert resp.is_succeeded(), resp.error_msg()

        # prepare parameters
        bval = ttypes.Value()
        bval.set_bVal(True)
        ival = ttypes.Value()
        ival.set_iVal(3)
        sval = ttypes.Value()
        sval.set_sVal("Bob")
        self.params = {"p1": ival, "p2": bval, "p3": sval}

        assert self.pool.connects() == 1
        assert self.pool.in_used_connects() == 1
예제 #13
0
        print(' \n====== The query result of thread[%s]======\n ' %
              threading.current_thread().getName())
        print_resp(query_resp)

    except Exception as x:
        print(x)
        import traceback

        print(traceback.format_exc())
    finally:
        if client is not None:
            client.release()


if __name__ == '__main__':
    config = Config()
    config.max_connection_pool_size = 4

    # init connection pool
    connection_pool = ConnectionPool()
    assert connection_pool.init([('127.0.0.1', 9669), ('127.0.0.1', 9670)],
                                config)

    # Use multi thread and reuse the session three times
    for count in range(0, 3):
        threads = list()
        for i in range(0, 4):
            threads.append(
                threading.Thread(target=main_test, name='thread{}'.format(i)))

        for thread in threads:
예제 #14
0
    def setUpClass(cls) -> None:
        super().setUpClass()
        configs = Config()
        configs.max_connection_pool_size = 1
        cls.pool = ConnectionPool()
        cls.pool.init([('127.0.0.1', 9671)], configs)
        cls.session = cls.pool.get_session('root', 'nebula')
        resp = cls.session.execute('''
            CREATE SPACE IF NOT EXISTS test_data(vid_type=FIXED_STRING(8));
            USE test_data;
            CREATE TAG IF NOT EXISTS person(name string, age int8, grade int16, 
            friends int32, book_num int64, birthday datetime, 
            start_school date, morning time, property double, 
            is_girl bool, child_name fixed_string(10), expend float, 
            first_out_city timestamp, hobby string);
            CREATE TAG IF NOT EXISTS student(name string, interval duration);
            CREATE EDGE IF NOT EXISTS like(likeness double);
            CREATE EDGE IF NOT EXISTS friend(start_year int, end_year int);
            CREATE TAG INDEX IF NOT EXISTS person_name_index ON person(name(8));
            ''')
        assert resp.is_succeeded(), resp.error_msg()

        time.sleep(5)
        resp = cls.session.execute(
            "INSERT VERTEX person(name, age, grade,friends, book_num,"
            "birthday, start_school, morning, property,"
            "is_girl, child_name, expend, first_out_city) VALUES"
            "'Bob':('Bob', 10, 3, 10, 100, datetime('2010-09-10T10:08:02'),"
            "date('2017-09-10'), time('07:10:00'), "
            "1000.0, false, 'Hello World!', 100.0, 1111),"
            "'Lily':('Lily', 9, 3, 10, 100, datetime('2010-09-10T10:08:02'), "
            "date('2017-09-10'), time('07:10:00'), "
            "1000.0, false, 'Hello World!', 100.0, 1111),"
            "'Tom':('Tom', 10, 3, 10, 100, datetime('2010-09-10T10:08:02'), "
            "date('2017-09-10'), time('07:10:00'), "
            "1000.0, false, 'Hello World!', 100.0, 1111),"
            "'Jerry':('Jerry', 9, 3, 10, 100, datetime('2010-09-10T10:08:02'),"
            "date('2017-09-10'), time('07:10:00'), "
            "1000.0, false, 'Hello World!', 100.0, 1111), "
            "'John':('John', 10, 3, 10, 100, datetime('2010-09-10T10:08:02'), "
            "date('2017-09-10'), time('07:10:00'), "
            "1000.0, false, 'Hello World!', 100.0, 1111)")
        assert resp.is_succeeded(), resp.error_msg()
        resp = cls.session.execute(
            "INSERT VERTEX student(name, interval) VALUES "
            "'Bob':('Bob', duration({months:1, seconds:100, microseconds:20})),"
            "'Lily':('Lily', duration({years: 1, seconds: 0})),"
            "'Tom':('Tom', duration({years: 1, seconds: 0})),"
            "'Jerry':('Jerry', duration({years: 1, seconds: 0})),"
            "'John':('John', duration({years: 1, seconds: 0}))")
        assert resp.is_succeeded(), resp.error_msg()

        resp = cls.session.execute("INSERT EDGE like(likeness) VALUES "
                                   "'Bob'->'Lily':(80.0), "
                                   "'Bob'->'Tom':(70.0), "
                                   "'Jerry'->'Lily':(84.0),"
                                   "'Tom'->'Jerry':(68.3), "
                                   "'Bob'->'John':(97.2)")
        assert resp.is_succeeded(), resp.error_msg()
        resp = cls.session.execute(
            "INSERT EDGE friend(start_year, end_year) VALUES "
            "'Bob'->'Lily':(2018, 2020), "
            "'Bob'->'Tom':(2018, 2020), "
            "'Jerry'->'Lily':(2018, 2020),"
            "'Tom'->'Jerry':(2018, 2020), "
            "'Bob'->'John':(2018, 2020)")
        assert resp.is_succeeded(), resp.error_msg()
예제 #15
0
 def test_right_hostname(self):
     pool = ConnectionPool()
     assert pool.init([('localhost', 9669)], Config())