4dnode1mnode_basic_replica3_vgroups_stopOne.py 13.3 KB
Newer Older
1 2 3 4 5 6
# author : wenzhouwww
from errno import ESOCKTNOSUPPORT
from ssl import ALERT_DESCRIPTION_CERTIFICATE_UNOBTAINABLE
import taos
import sys
import time
G
Ganlin Zhao 已提交
7
import os
8 9 10 11 12 13 14 15

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 *

G
Ganlin Zhao 已提交
16
import time
17 18 19 20 21 22 23 24 25 26 27 28 29
import random
import socket
import subprocess

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 已提交
30
        self.replica = 1
31
        self.vgroups = 2
G
Ganlin Zhao 已提交
32
        self.tb_nums = 10
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
        self.row_nums = 100
        self.max_vote_time_cost = 10  # seconds
        self.stop_dnode = None

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

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

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

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

    def _get_stop_dnode(self):
        only_dnode_list = self.dnode_list.keys() - self.mnode_list.keys()
        self.stop_dnode = random.sample(only_dnode_list , 1 )[0]
        return self.stop_dnode

G
Ganlin Zhao 已提交
136

137 138 139 140 141 142 143 144 145 146 147
    def check_vgroups_revote_leader(self,dbname):

        status = True
        stop_dnode_id = self.dnode_list[self.stop_dnode][0]

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

149 150
                if ind%2==0:
                    if role == stop_dnode_id and vgroups_leader_follower[ind+1]=="offline":
151
                        tdLog.notice("====== dnode {} has offline , endpoint is {}".format(stop_dnode_id , self.stop_dnode))
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
                    elif role == stop_dnode_id :
                        tdLog.exit("====== dnode {} has not offline , endpoint is {}".format(stop_dnode_id , self.stop_dnode))
                    else:
                        continue
                else:
                    vgroup_status.append(role)
            if vgroup_status.count("leader")!=1 or vgroup_status.count("follower")!=1 or vgroup_status.count("offline")!=1:
                status = False
                return status
        return status


    def wait_stop_dnode_OK(self):

        def _get_status():

            status =  ""
X
Xiaoyu Wang 已提交
169
            tdSql.query("select * from information_schema.ins_dnodes")
170 171 172 173 174 175 176
            dnode_infos = tdSql.queryResult
            for dnode_info in dnode_infos:
                endpoint = dnode_info[1]
                dnode_status = dnode_info[4]
                if endpoint == self.stop_dnode:
                    status = dnode_status
                    break
G
Ganlin Zhao 已提交
177
            return status
178 179 180 181 182

        status = _get_status()
        while status !="offline":
            time.sleep(0.1)
            status = _get_status()
183 184
            # tdLog.notice("==== stop dnode has not been stopped , endpoint is {}".format(self.stop_dnode))
        tdLog.notice("==== stop_dnode has stopped , endpoint is {}".format(self.stop_dnode))
185 186

    def wait_start_dnode_OK(self):
G
Ganlin Zhao 已提交
187

188 189 190
        def _get_status():

            status =  ""
X
Xiaoyu Wang 已提交
191
            tdSql.query("select * from information_schema.ins_dnodes")
192 193 194 195 196 197 198
            dnode_infos = tdSql.queryResult
            for dnode_info in dnode_infos:
                endpoint = dnode_info[1]
                dnode_status = dnode_info[4]
                if endpoint == self.stop_dnode:
                    status = dnode_status
                    break
G
Ganlin Zhao 已提交
199
            return status
200 201 202 203 204

        status = _get_status()
        while status !="ready":
            time.sleep(0.1)
            status = _get_status()
205 206
            # tdLog.notice("==== stop dnode has not been stopped , endpoint is {}".format(self.stop_dnode))
        tdLog.notice("==== stop_dnode has restart , endpoint is {}".format(self.stop_dnode))
207

G
Ganlin Zhao 已提交
208 209


210 211 212
    def random_stop_One_dnode(self):
        self.stop_dnode = self._get_stop_dnode()
        stop_dnode_id = self.dnode_list[self.stop_dnode][0]
213
        tdLog.notice(" ==== dnode {} will offline  ,endpoints is {} ====".format(stop_dnode_id , self.stop_dnode))
214 215 216
        tdDnodes=cluster.dnodes
        tdDnodes[stop_dnode_id-1].stoptaosd()
        self.wait_stop_dnode_OK()
X
Xiaoyu Wang 已提交
217
        # os.system("taos -s 'select * from information_schema.ins_dnodes;'")
218 219

    def Restart_stop_dnode(self):
G
Ganlin Zhao 已提交
220

221 222 223 224
        tdDnodes=cluster.dnodes
        stop_dnode_id = self.dnode_list[self.stop_dnode][0]
        tdDnodes[stop_dnode_id-1].starttaosd()
        self.wait_start_dnode_OK()
X
Xiaoyu Wang 已提交
225
        # os.system("taos -s 'select * from information_schema.ins_dnodes;'")
226 227

    def check_vgroups_init_done(self,dbname):
G
Ganlin Zhao 已提交
228

229 230 231 232 233 234 235
        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 已提交
236

237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
                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 已提交
252

253
            # tdLog.notice("=== database {} show vgroups vote the leader is in progress ===".format(dbname))
254 255
        end = time.time()
        cost_time = end - start
256
        tdLog.notice(" ==== database %s vote the leaders success , cost time is %.3f second ====="%(dbname,cost_time) )
257 258 259
        # os.system("taos -s 'show {}.vgroups;'".format(dbname))
        if cost_time >= self.max_vote_time_cost:
            tdLog.exit(" ==== database %s vote the leaders cost too large time , cost time is %.3f second ===="%(dbname,cost_time) )
G
Ganlin Zhao 已提交
260

261 262
        return cost_time

G
Ganlin Zhao 已提交
263

264 265 266 267 268 269 270
    def revote_leader_time_costs(self,dbname):
        start = time.time()

        status = self.check_vgroups_revote_leader(dbname)
        while not status:
            time.sleep(0.1)
            status = self.check_vgroups_revote_leader(dbname)
G
Ganlin Zhao 已提交
271

272
            # tdLog.notice("=== database {} show vgroups vote the leader is in progress ===".format(dbname))
273 274
        end = time.time()
        cost_time = end - start
275
        tdLog.notice(" ==== database %s revote the leaders success , cost time is %.3f second ====="%(dbname,cost_time) )
276 277 278
        # os.system("taos -s 'show {}.vgroups;'".format(dbname))
        if cost_time >= self.max_vote_time_cost:
            tdLog.exit(" ==== database %s revote the leaders cost too large time , cost time is %.3f second ===="%(dbname,cost_time) )
G
Ganlin Zhao 已提交
279 280


281
        return cost_time
G
Ganlin Zhao 已提交
282

283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
    def exec_revote_action(self,dbname):

        tdSql.query("show {}.vgroups".format(dbname))
        before_revote = tdSql.queryResult

        before_vgroups = set()
        for vgroup_info in before_revote:
            before_vgroups.add(vgroup_info[3:-4])

        self.random_stop_One_dnode()
        tdSql.query("show {}.vgroups".format(dbname))
        after_revote = tdSql.queryResult

        after_vgroups = set()
        for vgroup_info in after_revote:
            after_vgroups.add(vgroup_info[3:-4])
G
Ganlin Zhao 已提交
299

300 301 302 303 304
        vote_act = set(set(after_vgroups)-set(before_vgroups))
        if not vote_act:
            tdLog.exit(" ===maybe revote not occured , there is no dnode offline ====")
        else:
            for vgroup_info in vote_act:
G
Ganlin Zhao 已提交
305
                for ind , role in enumerate(vgroup_info):
306 307 308
                    if role==self.dnode_list[self.stop_dnode][0]:

                        if vgroup_info[ind+1] =="offline" and "leader" in vgroup_info:
309
                            tdLog.notice(" === revote leader ok , leader is {} now   ====".format(list(vgroup_info).index("leader")-1))
310 311 312 313 314 315 316 317 318 319 320 321
                        elif vgroup_info[ind+1] !="offline":
                            tdLog.exit(" === dnode {} should be offline ".format(self.stop_dnode))
                        else:
                            continue
                        break



        self.revote_leader_time_costs(dbname)
        self.Restart_stop_dnode()
    def test_init_vgroups_time_costs(self):

322 323
        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))
324

G
Ganlin Zhao 已提交
325
        # create database replica 3 vgroups 1
326 327 328

        db1 = 'db_1'
        create_db_replica_3_vgroups_1 = "create database {} replica 3 vgroups 1".format(db1)
329
        tdLog.notice('=======database {} replica 3 vgroups 1 ======'.format(db1))
330 331 332 333 334 335 336
        tdSql.execute(create_db_replica_3_vgroups_1)
        self.vote_leader_time_costs(db1)
        self.exec_revote_action(db1)

        # create database replica 3 vgroups 10
        db2 = 'db_2'
        create_db_replica_3_vgroups_10 = "create database {} replica 3 vgroups 10".format(db2)
337
        tdLog.notice('=======database {} replica 3 vgroups 10 ======'.format(db2))
338 339 340 341
        tdSql.execute(create_db_replica_3_vgroups_10)
        self.vote_leader_time_costs(db2)
        self.exec_revote_action(db2)

342 343 344
        # create database replica 3 vgroups 100
        db3 = 'db_3'
        create_db_replica_3_vgroups_100 = "create database {} replica 3 vgroups 100".format(db3)
345
        tdLog.notice('=======database {} replica 3 vgroups 100 ======'.format(db3))
346 347 348
        tdSql.execute(create_db_replica_3_vgroups_100)
        self.vote_leader_time_costs(db3)
        self.exec_revote_action(db3)
349

G
Ganlin Zhao 已提交
350 351 352


    def run(self):
353 354
        self.check_setup_cluster_status()
        self.test_init_vgroups_time_costs()
G
Ganlin Zhao 已提交
355

356 357 358 359 360 361 362 363



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

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