4dnode1mnode_basic_replica3_insertdatas.py 6.6 KB
Newer Older
1 2 3 4 5
# author : wenzhouwww
from ssl import ALERT_DESCRIPTION_CERTIFICATE_UNOBTAINABLE
import taos
import sys
import time
G
Ganlin Zhao 已提交
6
import os
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

from util.log import *
from util.sql import *
from util.cases import *
from util.dnodes import TDDnodes
from util.dnodes import TDDnode
from util.cluster import *

import time
import socket
import subprocess
sys.path.append(os.path.dirname(__file__))

class TDTestCase:
    def init(self,conn ,logSql):
        tdLog.debug(f"start to excute {__file__}")
        tdSql.init(conn.cursor())
        self.host = socket.gethostname()
        self.mnode_list = {}
        self.dnode_list = {}
        self.ts = 1483200000000
        self.db_name ='testdb'
G
Ganlin Zhao 已提交
29
        self.replica = 3
30
        self.vgroups = 2
G
Ganlin Zhao 已提交
31
        self.tb_nums = 10
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
        self.row_nums = 100

    def getBuildPath(self):
        selfPath = os.path.dirname(os.path.realpath(__file__))
        if ("community" in selfPath):
            projPath = selfPath[:selfPath.find("community")]
        else:
            projPath = selfPath[:selfPath.find("tests")]

        for root, dirs, files in os.walk(projPath):
            if ("taosd" in files):
                rootRealPath = os.path.dirname(os.path.realpath(root))
                if ("packaging" not in rootRealPath):
                    buildPath = root[:len(root) - len("/build/bin")]
                    break
        return buildPath

    def check_setup_cluster_status(self):
X
Xiaoyu Wang 已提交
50
        tdSql.query("select * from information_schema.ins_mnodes")
51 52 53 54 55
        for mnode in tdSql.queryResult:
            name = mnode[1]
            info = mnode
            self.mnode_list[name] = info

X
Xiaoyu Wang 已提交
56
        tdSql.query("select * from information_schema.ins_dnodes")
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
        for dnode in tdSql.queryResult:
            name = dnode[1]
            info = dnode
            self.dnode_list[name] = info

        count = 0
        is_leader = False
        mnode_name = ''
        for k,v in self.mnode_list.items():
            count +=1
            # only for 1 mnode
            mnode_name = k

            if v[2] =='leader':
                is_leader=True

        if count==1 and is_leader:
74
            tdLog.notice("===== depoly cluster success with 1 mnode as leader =====")
75 76 77 78 79 80
        else:
            tdLog.exit("===== depoly cluster fail with 1 mnode as leader =====")

        for k ,v in self.dnode_list.items():
            if k == mnode_name:
                if v[3]==0:
81
                    tdLog.notice("===== depoly cluster mnode only success at {} , support_vnodes is {} ".format(mnode_name,v[3]))
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
                else:
                    tdLog.exit("===== depoly cluster mnode only fail at {} , support_vnodes is {} ".format(mnode_name,v[3]))
            else:
                continue

    def create_db_check_vgroups(self):

        tdSql.execute("drop database if exists test")
        tdSql.execute("create database if not exists test replica 1 duration 300")
        tdSql.execute("use test")
        tdSql.execute(
        '''create table stb1
        (ts timestamp, c1 int, c2 bigint, c3 smallint, c4 tinyint, c5 float, c6 double, c7 bool, c8 binary(16),c9 nchar(32), c10 timestamp)
        tags (t1 int)
        '''
        )
        tdSql.execute(
            '''
            create table t1
            (ts timestamp, c1 int, c2 bigint, c3 smallint, c4 tinyint, c5 float, c6 double, c7 bool, c8 binary(16),c9 nchar(32), c10 timestamp)
            '''
        )
G
Ganlin Zhao 已提交
104

105 106 107 108 109 110 111 112 113 114 115 116 117
        for i in range(5):
            tdSql.execute("create table sub_tb_{} using stb1 tags({})".format(i,i))
        tdSql.query("show stables")
        tdSql.checkRows(1)
        tdSql.query("show tables")
        tdSql.checkRows(6)

        tdSql.query("show test.vgroups;")
        vgroups_infos = {}  # key is id: value is info list
        for vgroup_info in tdSql.queryResult:
            vgroup_id = vgroup_info[0]
            tmp_list = []
            for role in vgroup_info[3:-4]:
S
Shengliang Guan 已提交
118
                if role in ['leader','leader*','follower']:
119 120 121 122
                    tmp_list.append(role)
            vgroups_infos[vgroup_id]=tmp_list

        for k , v in vgroups_infos.items():
S
Shengliang Guan 已提交
123
            if len(v) ==1 and v[0] in ['leader', 'leader*']:
124
                tdLog.notice(" === create database replica only 1 role leader  check success of vgroup_id {} ======".format(k))
125 126 127 128 129 130 131
            else:
                tdLog.exit(" === create database replica only 1 role leader  check fail of vgroup_id {} ======".format(k))

    def create_db_replica_3_insertdatas(self, dbname, replica_num ,vgroup_nums ,tb_nums , row_nums ):
        drop_db_sql = "drop database if exists {}".format(dbname)
        create_db_sql = "create database {} replica {} vgroups {}".format(dbname,replica_num,vgroup_nums)

132
        tdLog.notice(" ==== create database {} and insert rows begin =====".format(dbname))
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
        tdSql.execute(drop_db_sql)
        tdSql.execute(create_db_sql)
        tdSql.execute("use {}".format(dbname))
        tdSql.execute(
        '''create table stb1
        (ts timestamp, c1 int, c2 bigint, c3 smallint, c4 tinyint, c5 float, c6 double, c7 bool, c8 binary(32),c9 nchar(32), c10 timestamp)
        tags (t1 int)
        '''
        )
        tdSql.execute(
            '''
            create table t1
            (ts timestamp, c1 int, c2 bigint, c3 smallint, c4 tinyint, c5 float, c6 double, c7 bool, c8 binary(32),c9 nchar(32), c10 timestamp)
            '''
        )
G
Ganlin Zhao 已提交
148

149 150 151 152 153 154 155 156 157
        for i in range(tb_nums):
            sub_tbname = "sub_tb_{}".format(i)
            tdSql.execute("create table {} using stb1 tags({})".format(sub_tbname,i))
            # insert datas about new database

            for row_num in range(row_nums):
                ts = self.ts + 1000*row_num
                tdSql.execute(f"insert into {sub_tbname} values ({ts}, {row_num} ,{row_num}, 10 ,1 ,{row_num} ,{row_num},true,'bin_{row_num}','nchar_{row_num}',now) ")

158
        tdLog.notice(" ==== create database {} and insert rows execute end =====".format(dbname))
159 160 161 162 163 164 165 166

    def check_insert_status(self, dbname, tb_nums , row_nums):
        tdSql.execute("use {}".format(dbname))
        tdSql.query("select count(*) from {}.{}".format(dbname,'stb1'))
        tdSql.checkData(0 , 0 , tb_nums*row_nums)
        tdSql.query("select distinct tbname from {}.{}".format(dbname,'stb1'))
        tdSql.checkRows(tb_nums)

G
Ganlin Zhao 已提交
167
    def run(self):
168 169 170 171 172 173 174 175 176 177 178
        self.check_setup_cluster_status()
        self.create_db_check_vgroups()
        self.create_db_replica_3_insertdatas(self.db_name , self.replica , self.vgroups , self.tb_nums , self.row_nums)
        self.check_insert_status(self.db_name , self.tb_nums , self.row_nums)


    def stop(self):
        tdSql.close()
        tdLog.success(f"{__file__} successfully executed")

tdCases.addLinux(__file__, TDTestCase())
G
Ganlin Zhao 已提交
179
tdCases.addWindows(__file__, TDTestCase())