4dnode1mnode_basic_replica3_vgroups.py 7.3 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

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

class TDTestCase:
20
    def init(self, conn, logSql, replicaVar=1):
21
        self.replicaVar = int(replicaVar)
22 23 24 25 26 27 28
        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 = 1
30
        self.vgroups = 2
G
Ganlin Zhao 已提交
31
        self.tb_nums = 10
32
        self.row_nums = 10
W
update  
wenzhouwww 已提交
33
        self.max_vote_time_cost = 100  # seconds
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50

    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 已提交
51
        tdSql.query("select * from information_schema.ins_mnodes")
52 53 54 55 56
        for mnode in tdSql.queryResult:
            name = mnode[1]
            info = mnode
            self.mnode_list[name] = info

X
Xiaoyu Wang 已提交
57
        tdSql.query("select * from information_schema.ins_dnodes")
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
        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:
75
            tdLog.notice("===== depoly cluster success with 1 mnode as leader =====")
76
        else:
W
update  
wenzhouwww 已提交
77
            tdLog.notice("===== depoly cluster fail with 1 mnode as leader =====")
78 79 80 81

        for k ,v in self.dnode_list.items():
            if k == mnode_name:
                if v[3]==0:
82
                    tdLog.notice("===== depoly cluster mnode only success at {} , support_vnodes is {} ".format(mnode_name,v[3]))
83
                else:
W
update  
wenzhouwww 已提交
84
                    tdLog.notice("===== depoly cluster mnode only fail at {} , support_vnodes is {} ".format(mnode_name,v[3]))
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
            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 已提交
105

106 107 108 109 110 111 112 113 114 115 116 117 118
        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]:
119
                if role in ['leader', 'leader*', 'leader**', 'follower']:
120 121 122 123
                    tmp_list.append(role)
            vgroups_infos[vgroup_id]=tmp_list

        for k , v in vgroups_infos.items():
124
            if len(v) == 1 and v[0] in ['leader', 'leader*', 'leader**']:
125
                tdLog.notice(" === create database replica only 1 role leader  check success of vgroup_id {} ======".format(k))
126
            else:
W
update  
wenzhouwww 已提交
127
                tdLog.notice(" === create database replica only 1 role leader  check fail of vgroup_id {} ======".format(k))
128 129 130 131 132 133 134 135 136 137

    def check_vgroups_init_done(self,dbname):

        status = True

        tdSql.query("show {}.vgroups".format(dbname))
        for vgroup_info in tdSql.queryResult:
            vgroup_id = vgroup_info[0]
            vgroup_status = []
            for ind , role in enumerate(vgroup_info[3:-4]):
G
Ganlin Zhao 已提交
138

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
                if ind%2==0:
                    continue
                else:
                    vgroup_status.append(role)
            if vgroup_status.count("leader")!=1 or vgroup_status.count("follower")!=2:
                status = False
                return status
        return status


    def vote_leader_time_costs(self,dbname):
        start = time.time()
        status = self.check_vgroups_init_done(dbname)
        while not status:
            time.sleep(0.1)
            status = self.check_vgroups_init_done(dbname)
G
Ganlin Zhao 已提交
155

156
            # tdLog.notice("=== database {} show vgroups vote the leader is in progress ===".format(dbname))
157 158
        end = time.time()
        cost_time = end - start
159
        tdLog.notice(" ==== database %s vote the leaders success , cost time is %.3f second ====="%(dbname,cost_time) )
160 161
        # os.system("taos -s 'show {}.vgroups;'".format(dbname))
        if cost_time >= self.max_vote_time_cost:
W
update  
wenzhouwww 已提交
162
            tdLog.notice(" ==== database %s vote the leaders cost too large time , cost time is %.3f second ===="%(dbname,cost_time) )
G
Ganlin Zhao 已提交
163 164


165
        return cost_time
G
Ganlin Zhao 已提交
166

167 168
    def test_init_vgroups_time_costs(self):

169 170
        tdLog.notice(" ====start check time cost about vgroups vote leaders ==== ")
        tdLog.notice(" ==== current max time cost is set value : {} =======".format(self.max_vote_time_cost))
171

G
Ganlin Zhao 已提交
172
        # create database replica 3 vgroups 1
173 174 175

        db1 = 'db_1'
        create_db_replica_3_vgroups_1 = "create database {} replica 3 vgroups 1".format(db1)
176
        tdLog.notice('=======database {} replica 3 vgroups 1 ======'.format(db1))
177 178 179 180 181 182
        tdSql.execute(create_db_replica_3_vgroups_1)
        self.vote_leader_time_costs(db1)

        # create database replica 3 vgroups 10
        db2 = 'db_2'
        create_db_replica_3_vgroups_10 = "create database {} replica 3 vgroups 10".format(db2)
183
        tdLog.notice('=======database {} replica 3 vgroups 10 ======'.format(db2))
184 185 186
        tdSql.execute(create_db_replica_3_vgroups_10)
        self.vote_leader_time_costs(db2)

W
update  
wenzhouwww 已提交
187
        # create database replica 3 vgroups 30
188
        db3 = 'db_3'
189
        create_db_replica_3_vgroups_100 = "create database {} replica 3 vgroups 20".format(db3)
W
update  
wenzhouwww 已提交
190
        tdLog.notice('=======database {} replica 3 vgroups 30 ======'.format(db3))
191 192 193
        tdSql.execute(create_db_replica_3_vgroups_100)
        self.vote_leader_time_costs(db3)

G
Ganlin Zhao 已提交
194 195 196


    def run(self):
197 198 199 200 201 202 203 204 205 206
        self.check_setup_cluster_status()
        self.test_init_vgroups_time_costs()



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

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