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([('172.28.3.1', 3699)], 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 start(self):
        os.chdir(self.work_dir)
        start_time = time.time()
        for p in self.all_processes:
            p.start()

        time.sleep(3)
        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)
        assert client_pool.init([("127.0.0.1", self.graphd_processes[0].tcp_port)], config)

        cmd = "ADD HOSTS 127.0.0.1:" + str(self.storaged_processes[0].tcp_port) + " INTO NEW ZONE \"default_zone\""
        print(cmd)

        # get session from the pool
        client = client_pool.get_session('root', 'nebula')
        resp = client.execute(cmd)
        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]
示例#4
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
示例#5
0
def get_conn_pool(host: str, port: int):
    config = Config()
    config.max_connection_pool_size = 20
    config.timeout = 180000
    # init connection pool
    pool = ConnectionPool()
    if not pool.init([(host, port)], config):
        raise Exception("Fail to init connection pool.")
    return pool
示例#6
0
    def create_nebula_clients(self):
        config = Config()
        config.max_connection_pool_size = 20
        config.timeout = 60000
        # init connection pool
        self.client_pool = ConnectionPool()
        assert self.client_pool.init([(self.host, self.port)], config)

        # get session from the pool
        self.client = self.client_pool.get_session(self.user, self.password)
示例#7
0
    def start(self):
        os.chdir(self.work_dir)
        start_time = time.time()
        for p in self.all_processes:
            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.tcp_port))
            for storaged in self.storaged_processes
        ])
        cmd = "ADD HOSTS {} INTO NEW ZONE \"default_zone\"".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]
    def __enter__(self):
        config = Config()
        config.max_connection_pool_size = 2
        config.timeout = 0
        # init connection pool
        self.client_pool = ConnectionPool()
        assert self.client_pool.init([(self.host, self.port)], config)

        # get session from the pool
        self.client = self.client_pool.get_session(self.user, self.password)
        return self
    def __init__(self):
        # define a config
        config = Config()
        config.max_connection_pool_size = 10
        # init connection pool
        connection_pool = ConnectionPool()
        # if the given servers are ok, return true, else return false
        ok = connection_pool.init([('10.141.186.105', 3699)], config)

        # option 1 control the connection release yourself
        # get session from the pool
        self.session = connection_pool.get_session('root', 'nebula')

        # select space
        self.session.execute('USE adeci_test')
示例#10
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
示例#11
0
 def test_wrong_hostname(self):
     pool = ConnectionPool()
     try:
         pool.init([('wrong_host', 9669)], Config())
         assert False
     except InValidHostname:
         assert True
示例#12
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
示例#13
0
 def setup_class(self):
     self.addresses = list()
     self.addresses.append(('127.0.0.1', 3699))
     self.addresses.append(('127.0.0.1', 3700))
     self.configs = Config()
     self.configs.min_connection_pool_size = 2
     self.configs.max_connection_pool_size = 4
     self.pool = ConnectionPool()
     assert self.pool.init(self.addresses, self.configs)
     assert self.pool.connnects() == 2
示例#14
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.connnects() == 0
     assert self.pool.in_used_connects() == 0
示例#15
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.connnects() == 2
示例#16
0
 def setup_class(self):
     self.addresses = list()
     self.addresses.append(('127.0.0.1', 3699))
     self.addresses.append(('127.0.0.1', 3700))
     self.user_name = 'root'
     self.password = '******'
     self.configs = Config()
     self.configs.min_connection_pool_size = 2
     self.configs.max_connection_pool_size = 4
     self.pool = ConnectionPool()
     self.pool._check_delay = 2
     assert self.pool.init(self.addresses, self.configs)
     assert self.pool.connnects() == 2
示例#17
0
                for secret_id in (user_secret_id, password_secret_id))

    return DEFAULT_NG_CREDENTIAL


def parse_nebula_graphd_endpoint():
    ng_endpoints_str = os.environ.get('NG_ENDPOINTS',
                                      '127.0.0.1:9669,').split(",")
    ng_endpoints = []
    for endpoint in ng_endpoints_str:
        if endpoint:
            parts = endpoint.split(":")  # we dont consider IPv6 now
            ng_endpoints.append((parts[0], int(parts[1])))
    return ng_endpoints


ng_config = Config()
ng_config.max_connection_pool_size = int(
    os.environ.get('NG_MAX_CONN_POOL_SIZE', 10))
ng_endpoints = parse_nebula_graphd_endpoint()
connection_pool = ConnectionPool()

if __name__ == "__main__":
    connection_pool.init(ng_endpoints, ng_config)
    try:
        app.run(host="0.0.0.0", port=5000)
    finally:
        connection_pool.close()
else:
    connection_pool.init(ng_endpoints, ng_config)
示例#18
0
        # Print the result of query
        print(' \n====== The query result of thread[%s]======\n ' %
              threading.current_thread().getName())
        print_resp(query_resp)
        client.release()

    except Exception as x:
        print(x)
        if client is not None:
            client.release()
        exit(1)


if __name__ == '__main__':
    config = Config()
    config.timeout = 1000
    config.max_connection_pool_size = 4
    config.max_retry_time = 3

    addresses = list()
    addresses.append(('127.0.0.1', 3699))
    # init connection pool
    connection_pool = ConnectionPool()
    assert connection_pool.init(addresses, 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(
示例#19
0
# This source code is licensed under Apache 2.0 License,
# attached with Common Clause Condition 1.0, found in the LICENSES directory.

import sys
import time

sys.path.insert(0, '../')

from nebula2.gclient.net import ConnectionPool
from nebula2.Config import Config
from FormatResp import print_resp

if __name__ == '__main__':
    client = None
    try:
        config = Config()
        config.max_connection_pool_size = 2
        # init connection pool
        connection_pool = ConnectionPool()
        assert connection_pool.init([('127.0.0.1', 3700), ('127.0.0.1', 3699)],
                                    config)

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

        client.execute(
            'CREATE SPACE IF NOT EXISTS test; USE test;'
            'CREATE TAG IF NOT EXISTS person(name string, age int);')

        # insert data need to sleep after create schema
    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);
            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) VALUES "
            "'Bob':('Bob'), 'Lily':('Lily'), "
            "'Tom':('Tom'), 'Jerry':('Jerry'), 'John':('John')")
        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()
示例#21
0
 def test_right_hostname(self):
     pool = ConnectionPool()
     assert pool.init([('localhost', 9669)], Config())