5dnode3mnodeStop.py 9.5 KB
Newer Older
haoranc's avatar
haoranc 已提交
1 2 3 4 5 6 7 8 9
from ssl import ALERT_DESCRIPTION_CERTIFICATE_UNOBTAINABLE
import taos
import sys
import time
import os 

from util.log import *
from util.sql import *
from util.cases import *
haoranc's avatar
haoranc 已提交
10
from util.dnodes import *
haoranc's avatar
haoranc 已提交
11 12
from util.dnodes import TDDnodes
from util.dnodes import TDDnode
haoranc's avatar
haoranc 已提交
13 14 15
from util.cluster import *
from test import tdDnodes

haoranc's avatar
haoranc 已提交
16 17 18 19
import time
import socket
import subprocess
from multiprocessing import Process
haoranc's avatar
haoranc 已提交
20

haoranc's avatar
haoranc 已提交
21 22 23 24 25
        
class TDTestCase:

    def init(self,conn ,logSql):
        tdLog.debug(f"start to excute {__file__}")
haoranc's avatar
haoranc 已提交
26 27 28
        tdSql.init(conn.cursor())
        self.host = socket.gethostname()

haoranc's avatar
haoranc 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

    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 insert_data(self,count):
        # fisrt add data : db\stable\childtable\general table
        for couti in count:
            tdSql.execute("drop database if exists db%d" %couti)
50
            tdSql.execute("create database if not exists db%d replica 1 duration 300" %couti)
haoranc's avatar
haoranc 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
            tdSql.execute("use db%d" %couti)
            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)
                '''
            )
            for i in range(4):
                tdSql.execute(f'create table ct{i+1} using stb1 tags ( {i+1} )')

haoranc's avatar
haoranc 已提交
67
    
haoranc's avatar
haoranc 已提交
68 69 70 71 72
    def check3mnode(self):
        count=0
        while count < 10:
            time.sleep(1)
            tdSql.query("show mnodes;")
haoranc's avatar
haoranc 已提交
73
            if tdSql.checkRows(3) :     
haoranc's avatar
haoranc 已提交
74 75 76
                tdLog.debug("mnode is  three nodes")
            else:
                tdLog.exit("mnode number is correct")
haoranc's avatar
haoranc 已提交
77 78 79
            if  tdSql.queryResult[0][2]=='leader' :
                if  tdSql.queryResult[1][2]=='follower':
                    if  tdSql.queryResult[2][2]=='follower':
haoranc's avatar
haoranc 已提交
80
                        tdLog.debug("three mnodes is ready in 10s")
haoranc's avatar
haoranc 已提交
81
                        break
haoranc's avatar
haoranc 已提交
82 83
            elif tdSql.queryResult[1][2]=='leader' :
                if  tdSql.queryResult[0][2]=='follower':
haoranc's avatar
haoranc 已提交
84
                    if  tdSql.queryResult[2][2]=='follower':
haoranc's avatar
haoranc 已提交
85
                        tdLog.debug("three mnodes is ready in 10s")
haoranc's avatar
haoranc 已提交
86
                        break      
haoranc's avatar
haoranc 已提交
87
            elif tdSql.queryResult[2][2]=='leader' :
haoranc's avatar
haoranc 已提交
88
                if  tdSql.queryResult[1][2]=='follower':
haoranc's avatar
haoranc 已提交
89 90
                    if  tdSql.queryResult[0][2]=='follower':
                        tdLog.debug("three mnodes is ready in 10s")
haoranc's avatar
haoranc 已提交
91
                        break                   
haoranc's avatar
haoranc 已提交
92 93
            count+=1
        else:
haoranc's avatar
haoranc 已提交
94
            tdLog.exit("three mnodes is not ready in 10s ")
haoranc's avatar
haoranc 已提交
95 96 97

        tdSql.query("show mnodes;")       
        tdSql.checkRows(3) 
haoranc's avatar
haoranc 已提交
98
        tdSql.checkData(0,1,'%s:6030'%self.host)
haoranc's avatar
haoranc 已提交
99
        tdSql.checkData(0,3,'ready')
haoranc's avatar
haoranc 已提交
100
        tdSql.checkData(1,1,'%s:6130'%self.host)
haoranc's avatar
haoranc 已提交
101
        tdSql.checkData(1,3,'ready')
haoranc's avatar
haoranc 已提交
102
        tdSql.checkData(2,1,'%s:6230'%self.host)
haoranc's avatar
haoranc 已提交
103 104 105 106 107 108 109 110
        tdSql.checkData(2,3,'ready')

    def check3mnode1off(self):
        count=0
        while count < 10:
            time.sleep(1)
            tdSql.query("show mnodes;")
            if tdSql.checkRows(3) :
haoranc's avatar
haoranc 已提交
111 112 113
                tdLog.debug("mnode is  three nodes")
            else:
                tdLog.exit("mnode number is correct")
haoranc's avatar
haoranc 已提交
114 115 116
            if  tdSql.queryResult[0][2]=='offline' :
                if  tdSql.queryResult[1][2]=='leader':
                    if  tdSql.queryResult[2][2]=='follower':
haoranc's avatar
haoranc 已提交
117
                        tdLog.debug("stop mnodes  on dnode 2 successfully in 10s")
haoranc's avatar
haoranc 已提交
118 119 120
                        break
                elif tdSql.queryResult[1][2]=='follower':
                    if  tdSql.queryResult[2][2]=='leader':
haoranc's avatar
haoranc 已提交
121
                        tdLog.debug("stop mnodes  on dnode 2 successfully in 10s")
haoranc's avatar
haoranc 已提交
122
                        break
haoranc's avatar
haoranc 已提交
123 124
            count+=1
        else:
haoranc's avatar
haoranc 已提交
125 126
            tdLog.exit("stop mnodes  on dnode 2 failed in 10s ")
            
haoranc's avatar
haoranc 已提交
127
        tdSql.error("drop mnode on dnode 1;")
haoranc's avatar
haoranc 已提交
128 129 130

        tdSql.query("show mnodes;")       
        tdSql.checkRows(3) 
haoranc's avatar
haoranc 已提交
131
        tdSql.checkData(0,1,'%s:6030'%self.host)
haoranc's avatar
haoranc 已提交
132 133
        tdSql.checkData(0,2,'offline')
        tdSql.checkData(0,3,'ready')
haoranc's avatar
haoranc 已提交
134
        tdSql.checkData(1,1,'%s:6130'%self.host)
haoranc's avatar
haoranc 已提交
135
        tdSql.checkData(1,3,'ready')
haoranc's avatar
haoranc 已提交
136
        tdSql.checkData(2,1,'%s:6230'%self.host)
haoranc's avatar
haoranc 已提交
137 138 139 140
        tdSql.checkData(2,3,'ready')

    def check3mnode2off(self):
        count=0
haoranc's avatar
haoranc 已提交
141
        while count < 40:
haoranc's avatar
haoranc 已提交
142 143 144
            time.sleep(1)
            tdSql.query("show mnodes;")
            if tdSql.checkRows(3) :
haoranc's avatar
haoranc 已提交
145 146 147
                tdLog.debug("mnode is  three nodes")
            else:
                tdLog.exit("mnode number is correct")
haoranc's avatar
haoranc 已提交
148 149 150
            if  tdSql.queryResult[0][2]=='leader' :
                if  tdSql.queryResult[1][2]=='offline':
                    if  tdSql.queryResult[2][2]=='follower':
haoranc's avatar
haoranc 已提交
151
                        tdLog.debug("stop mnodes  on dnode 2 successfully in 10s")
haoranc's avatar
haoranc 已提交
152
                        break
haoranc's avatar
haoranc 已提交
153 154
            count+=1
        else:
haoranc's avatar
haoranc 已提交
155 156
            tdLog.exit("stop mnodes  on dnode 2 failed in 10s ")

haoranc's avatar
haoranc 已提交
157
        tdSql.error("drop mnode on dnode 2;")
haoranc's avatar
haoranc 已提交
158 159 160

        tdSql.query("show mnodes;")       
        tdSql.checkRows(3) 
haoranc's avatar
haoranc 已提交
161
        tdSql.checkData(0,1,'%s:6030'%self.host)
haoranc's avatar
haoranc 已提交
162
        tdSql.checkData(0,3,'ready')
haoranc's avatar
haoranc 已提交
163
        tdSql.checkData(1,1,'%s:6130'%self.host)
haoranc's avatar
haoranc 已提交
164
        tdSql.checkData(1,3,'ready')
haoranc's avatar
haoranc 已提交
165
        tdSql.checkData(2,1,'%s:6230'%self.host)
haoranc's avatar
haoranc 已提交
166 167 168 169 170 171 172 173
        tdSql.checkData(2,3,'ready')

    def check3mnode3off(self):
        count=0
        while count < 10:
            time.sleep(1)
            tdSql.query("show mnodes;")
            if tdSql.checkRows(3) :
haoranc's avatar
haoranc 已提交
174 175 176
                tdLog.debug("mnode is  three nodes")
            else:
                tdLog.exit("mnode number is correct")
haoranc's avatar
haoranc 已提交
177 178 179
            if  tdSql.queryResult[0][2]=='leader' :
                if  tdSql.queryResult[2][2]=='offline':
                    if  tdSql.queryResult[1][2]=='follower':
haoranc's avatar
haoranc 已提交
180
                        tdLog.debug("stop mnodes  on dnode 3 successfully in 10s")
haoranc's avatar
haoranc 已提交
181
                        break
haoranc's avatar
haoranc 已提交
182 183
            count+=1
        else:
haoranc's avatar
haoranc 已提交
184 185
            tdLog.exit("stop mnodes  on dnode 3 failed in 10s")

haoranc's avatar
haoranc 已提交
186
        tdSql.error("drop mnode on dnode 3;")
haoranc's avatar
haoranc 已提交
187 188
        tdSql.query("show mnodes;")       
        tdSql.checkRows(3) 
haoranc's avatar
haoranc 已提交
189
        tdSql.checkData(0,1,'%s:6030'%self.host)
haoranc's avatar
haoranc 已提交
190 191
        tdSql.checkData(0,2,'leader')
        tdSql.checkData(0,3,'ready')
haoranc's avatar
haoranc 已提交
192
        tdSql.checkData(1,1,'%s:6130'%self.host)
haoranc's avatar
haoranc 已提交
193 194
        tdSql.checkData(1,2,'follower')
        tdSql.checkData(1,3,'ready')
haoranc's avatar
haoranc 已提交
195
        tdSql.checkData(2,1,'%s:6230'%self.host)
haoranc's avatar
haoranc 已提交
196 197 198
        tdSql.checkData(2,2,'offline')
        tdSql.checkData(2,3,'ready')

haoranc's avatar
haoranc 已提交
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
    
    def check_dnodes_status(self,dnodeNumbers):
        count=0
        while count < 5:
            tdSql.query("show dnodes")
            # tdLog.debug(tdSql.queryResult)
            status=0
            for i in range(dnodeNumbers):
                if tdSql.queryResult[i][4] == "ready":
                    status+=1
            tdLog.debug(status)
            
            if status == dnodeNumbers:
                tdLog.debug(" create cluster with %d dnode and check cluster dnode all ready within 5s! " %dnodeNumbers)
                break 
            count+=1
            time.sleep(1)
        else:
            tdLog.exit("create cluster with %d dnode but  check dnode not ready within 5s ! "%dnodeNumbers)
haoranc's avatar
haoranc 已提交
218 219

    def five_dnode_three_mnode(self,dnodenumber):
haoranc's avatar
haoranc 已提交
220
        self.check_dnodes_status(5)
haoranc's avatar
haoranc 已提交
221
        tdSql.query("show mnodes;")   
haoranc's avatar
haoranc 已提交
222
        tdLog.debug(self.host)
haoranc's avatar
haoranc 已提交
223
        tdSql.checkRows(1)    
haoranc's avatar
haoranc 已提交
224
        tdSql.checkData(0,1,'%s:6030'%self.host)
haoranc's avatar
haoranc 已提交
225 226 227 228 229
        tdSql.checkData(0,2,'leader')
        tdSql.checkData(0,3,'ready')

        # fisr add three mnodes;
        tdSql.execute("create mnode on dnode 2")
P
plum-lihui 已提交
230
        time.sleep(10)
haoranc's avatar
haoranc 已提交
231 232 233 234 235 236 237 238 239
        tdSql.execute("create mnode on dnode 3")

        # fisrt check statut ready
        self.check3mnode()


        tdSql.error("create mnode on dnode 2")

        tdSql.query("show dnodes;")
haoranc's avatar
haoranc 已提交
240
        # tdLog.debug(tdSql.queryResult)
haoranc's avatar
haoranc 已提交
241 242

        tdLog.debug("stop and follower of mnode") 
haoranc's avatar
haoranc 已提交
243 244 245 246
        tdDnodes=cluster.dnodes
        # tdLog.debug(tdDnodes[0])

        tdDnodes[1].stoptaosd()
haoranc's avatar
haoranc 已提交
247
        self.check3mnode2off()
haoranc's avatar
haoranc 已提交
248 249
        tdDnodes[1].starttaosd()
        self.check3mnode()
haoranc's avatar
haoranc 已提交
250

haoranc's avatar
haoranc 已提交
251
        tdDnodes[2].stoptaosd()
haoranc's avatar
haoranc 已提交
252
        self.check3mnode3off()
haoranc's avatar
haoranc 已提交
253 254
        tdDnodes[2].starttaosd()
        self.check3mnode()
haoranc's avatar
haoranc 已提交
255

haoranc's avatar
haoranc 已提交
256
        tdDnodes[0].stoptaosd()
haoranc's avatar
haoranc 已提交
257
        self.check3mnode1off()
haoranc's avatar
haoranc 已提交
258 259
        tdDnodes[0].starttaosd()
        self.check3mnode()
haoranc's avatar
haoranc 已提交
260

haoranc's avatar
haoranc 已提交
261
        self.check3mnode()
haoranc's avatar
haoranc 已提交
262 263
        stopcount =0 
        while stopcount <= 2:
haoranc's avatar
haoranc 已提交
264
            for i in range(dnodenumber):
haoranc's avatar
haoranc 已提交
265 266
                tdDnodes[i].stoptaosd()
                tdDnodes[i].starttaosd()
haoranc's avatar
haoranc 已提交
267
                # self.check3mnode()
haoranc's avatar
haoranc 已提交
268
            stopcount+=1
haoranc's avatar
haoranc 已提交
269 270 271 272
        self.check3mnode()

    def run(self): 
        self.five_dnode_three_mnode(5)
haoranc's avatar
haoranc 已提交
273
 
haoranc's avatar
haoranc 已提交
274 275 276 277 278 279
    def stop(self):
        tdSql.close()
        tdLog.success(f"{__file__} successfully executed")

tdCases.addLinux(__file__, TDTestCase())
tdCases.addWindows(__file__, TDTestCase())