clusterCommonCheck.py 10.8 KB
Newer Older
haoranc's avatar
haoranc 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
###################################################################
#           Copyright (c) 2016 by TAOS Technologies, Inc.
#                     All rights reserved.
#
#  This file is proprietary and confidential to TAOS Technologies.
#  No part of this file may be reproduced, stored, transmitted,
#  disclosed or used in any form or by any means other than as
#  expressly provided by the written permission from Jianhui Tao
#
###################################################################

# -*- coding: utf-8 -*-

from collections import defaultdict
import random
import string
import threading
import requests
import time
# import socketfrom

import taos
from util.log import *
from util.sql import *
from util.cases import *
from util.dnodes import *
from util.common import *

# class actionType(Enum):
#     CREATE_DATABASE = 0
#     CREATE_STABLE   = 1
#     CREATE_CTABLE   = 2
#     INSERT_DATA     = 3

class ClusterComCheck:
haoranc's avatar
haoranc 已提交
36
    def init(self, conn, logSql=False):
haoranc's avatar
haoranc 已提交
37 38 39
        tdSql.init(conn.cursor())
        # tdSql.init(conn.cursor(), logSql)  # output sql.txt file

40
    def checkDnodes(self,dnodeNumbers, timeout=30):
haoranc's avatar
haoranc 已提交
41
        count=0
haoranc's avatar
haoranc 已提交
42
        # print(tdSql)
43
        while count < timeout:
X
Xiaoyu Wang 已提交
44
            tdSql.query("select * from information_schema.ins_dnodes")
haoranc's avatar
haoranc 已提交
45 46 47 48 49 50
            # tdLog.debug(tdSql.queryResult)
            status=0
            for i in range(dnodeNumbers):
                if tdSql.queryResult[i][4] == "ready":
                    status+=1
            tdLog.info(status)
G
Ganlin Zhao 已提交
51

haoranc's avatar
haoranc 已提交
52
            if status == dnodeNumbers:
53
                tdLog.success("it find cluster with %d dnodes and check that all cluster dnodes are ready within %ds! " % (dnodeNumbers, count))
G
Ganlin Zhao 已提交
54
                return True
haoranc's avatar
haoranc 已提交
55 56 57
            count+=1
            time.sleep(1)
        else:
X
Xiaoyu Wang 已提交
58
            tdSql.query("select * from information_schema.ins_dnodes")
haoranc's avatar
haoranc 已提交
59
            tdLog.debug(tdSql.queryResult)
60
            tdLog.exit("it find cluster with %d dnodes but  check that there dnodes are not ready within %ds ! "% (dnodeNumbers, timeout))
haoranc's avatar
haoranc 已提交
61 62 63 64 65

    def checkDbRows(self,dbNumbers):
        dbNumbers=int(dbNumbers)
        count=0
        while count < 5:
X
Xiaoyu Wang 已提交
66
            tdSql.query("select * from information_schema.ins_databases;")
haoranc's avatar
haoranc 已提交
67
            count+=1
haoranc's avatar
haoranc 已提交
68 69 70 71 72 73 74 75 76
            if tdSql.checkRows(dbNumbers+2):
                tdLog.success("we find %d databases and expect %d in clusters! " %(tdSql.queryRows,dbNumbers+2))
                return True
            else:
                continue
        else :
            tdLog.debug(tdSql.queryResult)
            tdLog.exit("we find %d databases but expect %d in clusters! " %(tdSql.queryRows,dbNumbers))

haoranc's avatar
haoranc 已提交
77
    def checkDb(self,dbNumbers,restartNumber,dbNameIndex):
haoranc's avatar
haoranc 已提交
78
        count=0
haoranc's avatar
haoranc 已提交
79
        alldbNumbers=(dbNumbers*restartNumber)+2
G
Ganlin Zhao 已提交
80
        while count < 5:
haoranc's avatar
haoranc 已提交
81
            query_status=0
haoranc's avatar
haoranc 已提交
82 83
            for j in range(dbNumbers):
                for i in range(alldbNumbers):
X
Xiaoyu Wang 已提交
84
                    tdSql.query("select * from information_schema.ins_databases;")
G
Ganlin Zhao 已提交
85
                    if "%s_%d"%(dbNameIndex,j) == tdSql.queryResult[i][0] :
86
                        if tdSql.queryResult[i][15] == "ready":
haoranc's avatar
haoranc 已提交
87
                            query_status+=1
G
Ganlin Zhao 已提交
88
                            tdLog.debug("check %s_%d that status is ready "%(dbNameIndex,j))
haoranc's avatar
haoranc 已提交
89 90
                        else:
                            continue
haoranc's avatar
haoranc 已提交
91
            # print(query_status)
haoranc's avatar
haoranc 已提交
92 93
            count+=1
            if query_status == dbNumbers:
haoranc's avatar
haoranc 已提交
94
                tdLog.success(" check %d database and  all databases  are ready within 5s! " %dbNumbers)
haoranc's avatar
haoranc 已提交
95 96 97
                return True
        else:
            tdLog.debug(tdSql.queryResult)
haoranc's avatar
haoranc 已提交
98
            tdLog.debug("query status is %d"%query_status)
haoranc's avatar
haoranc 已提交
99 100 101 102 103 104 105 106 107 108 109
            tdLog.exit("database is not ready within 5s")

    def checkData(self,dbname,stbname,stableCount,CtableCount,rowsPerSTable,):
        tdSql.execute("use %s"%dbname)
        tdSql.query("show stables")
        tdSql.checkRows(stableCount)
        tdSql.query("show tables")
        tdSql.checkRows(CtableCount)
        for i in range(stableCount):
            tdSql.query("select count(*) from %s%d"%(stbname,i))
            tdSql.checkData(0,0,rowsPerSTable)
G
Ganlin Zhao 已提交
110
        return
haoranc's avatar
haoranc 已提交
111 112 113 114

    def checkMnodeStatus(self,mnodeNums):
        self.mnodeNums=int(mnodeNums)
        # self.leaderDnode=int(leaderDnode)
haoranc's avatar
haoranc 已提交
115
        tdLog.debug("start to check status of mnodes")
haoranc's avatar
haoranc 已提交
116 117 118 119
        count=0

        while count < 10:
            time.sleep(1)
X
Xiaoyu Wang 已提交
120
            tdSql.query("select * from information_schema.ins_mnodes;")
G
Ganlin Zhao 已提交
121
            if tdSql.checkRows(self.mnodeNums) :
haoranc's avatar
haoranc 已提交
122 123 124 125 126
                tdLog.success("cluster has %d mnodes" %self.mnodeNums )

            if self.mnodeNums == 1:
                if  tdSql.queryResult[0][2]== 'leader' and  tdSql.queryResult[0][3]== 'ready'  :
                    tdLog.success("%d mnodes is ready in 10s"%self.mnodeNums)
G
Ganlin Zhao 已提交
127 128 129
                    return True
                count+=1
            elif self.mnodeNums == 3 :
haoranc's avatar
haoranc 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143
                if  tdSql.queryResult[0][2]=='leader'  and  tdSql.queryResult[0][3]== 'ready' :
                    if  tdSql.queryResult[1][2]=='follower' and  tdSql.queryResult[1][3]== 'ready' :
                        if  tdSql.queryResult[2][2]=='follower' and  tdSql.queryResult[2][3]== 'ready' :
                            tdLog.success("%d mnodes is ready in 10s"%self.mnodeNums)
                            return True
                elif  tdSql.queryResult[1][2]=='leader'  and  tdSql.queryResult[1][3]== 'ready' :
                    if  tdSql.queryResult[0][2]=='follower' and  tdSql.queryResult[0][3]== 'ready' :
                        if  tdSql.queryResult[2][2]=='follower' and  tdSql.queryResult[2][3]== 'ready' :
                            tdLog.success("%d mnodes is ready in 10s"%self.mnodeNums)
                            return True
                elif  tdSql.queryResult[2][2]=='leader'  and  tdSql.queryResult[2][3]== 'ready' :
                    if  tdSql.queryResult[0][2]=='follower' and  tdSql.queryResult[0][3]== 'ready' :
                        if  tdSql.queryResult[1][2]=='follower' and  tdSql.queryResult[1][3]== 'ready' :
                            tdLog.success("%d mnodes is ready in 10s"%self.mnodeNums)
G
Ganlin Zhao 已提交
144
                            return True
haoranc's avatar
haoranc 已提交
145
                count+=1
G
Ganlin Zhao 已提交
146
            elif self.mnodeNums == 2 :
haoranc's avatar
haoranc 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159
                if  tdSql.queryResult[0][2]=='leader' and  tdSql.queryResult[0][3]== 'ready' :
                    if  tdSql.queryResult[1][2]=='follower' and  tdSql.queryResult[1][3]== 'ready' :
                        tdLog.success("%d mnodes is ready in 10s"%self.mnodeNums)
                        return True
                elif tdSql.queryResult[1][2]=='leader' and  tdSql.queryResult[1][3]== 'ready' :
                    if  tdSql.queryResult[0][2]=='follower' and  tdSql.queryResult[0][3]== 'ready' :
                        tdLog.success("%d mnodes is ready in 10s"%self.mnodeNums)
                        return True
                count+=1
        else:
            tdLog.debug(tdSql.queryResult)
            tdLog.exit("cluster of %d  mnodes is not ready in 10s " %self.mnodeNums)

G
Ganlin Zhao 已提交
160

haoranc's avatar
haoranc 已提交
161 162 163 164 165 166


    def check3mnodeoff(self,offlineDnodeNo,mnodeNums=3):
        count=0
        while count < 10:
            time.sleep(1)
X
Xiaoyu Wang 已提交
167
            tdSql.query("select * from information_schema.ins_mnodes;")
haoranc's avatar
haoranc 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
            if tdSql.checkRows(mnodeNums) :
                tdLog.success("cluster has %d mnodes" %self.mnodeNums )
            else:
                tdLog.exit("mnode number is correct")
            if offlineDnodeNo == 1:
                if  tdSql.queryResult[0][2]=='offline' :
                    if  tdSql.queryResult[1][2]=='leader'  and  tdSql.queryResult[1][3]== 'ready'  :
                        if  tdSql.queryResult[2][2]=='follower' and  tdSql.queryResult[2][3]== 'ready'  :
                            tdLog.success("stop mnodes  on dnode %d  successfully in 10s" %offlineDnodeNo)
                            return True
                    elif tdSql.queryResult[1][2]=='follower' and  tdSql.queryResult[1][3]== 'ready' :
                        if  tdSql.queryResult[2][2]=='leader' and  tdSql.queryResult[2][3]== 'ready' :
                            tdLog.debug("stop mnodes  on dnode %d  successfully in 10s" %offlineDnodeNo)
                            return True
                count+=1
            elif offlineDnodeNo == 2:
                if  tdSql.queryResult[1][2]=='offline' :
                    if  tdSql.queryResult[0][2]=='leader' and  tdSql.queryResult[0][3]== 'ready' :
                        if  tdSql.queryResult[2][2]=='follower' and  tdSql.queryResult[2][3]== 'ready' :
                            tdLog.debug("stop mnodes  on dnode %d  successfully in 10s" %offlineDnodeNo)
                            return True
                    elif tdSql.queryResult[0][2]=='follower' and  tdSql.queryResult[0][3]== 'ready' :
                        if  tdSql.queryResult[2][2]=='leader' and  tdSql.queryResult[2][3]== 'ready' :
                            tdLog.debug("stop mnodes  on dnode %d  successfully in 10s" %offlineDnodeNo)
                            return True
                count+=1
            elif offlineDnodeNo == 3:
                if  tdSql.queryResult[2][2]=='offline' :
                    if  tdSql.queryResult[0][2]=='leader' and  tdSql.queryResult[0][3]== 'ready' :
                        if  tdSql.queryResult[1][2]=='follower' and  tdSql.queryResult[1][3]== 'ready' :
                            tdLog.debug("stop mnodes  on dnode %d  successfully in 10s" %offlineDnodeNo)
                            return True
                    elif tdSql.queryResult[0][2]=='follower' and  tdSql.queryResult[0][3]== 'ready' :
                        if  tdSql.queryResult[1][2]=='leader' and  tdSql.queryResult[1][3]== 'ready' :
                            tdLog.debug("stop mnodes  on dnode %d  successfully in 10s" %offlineDnodeNo)
                            return True
                count+=1
        else:
            tdLog.debug(tdSql.queryResult)
            tdLog.exit("stop mnodes  on dnode %d  failed in 10s ")

haoranc's avatar
haoranc 已提交
209 210 211 212
    def check3mnode2off(self,mnodeNums=3):
        count=0
        while count < 10:
            time.sleep(1)
X
Xiaoyu Wang 已提交
213
            tdSql.query("select * from information_schema.ins_mnodes;")
haoranc's avatar
haoranc 已提交
214 215 216 217 218 219 220 221 222 223 224 225 226
            if tdSql.checkRows(mnodeNums) :
                tdLog.success("cluster has %d mnodes" %self.mnodeNums )
            else:
                tdLog.exit("mnode number is correct")
            if  tdSql.queryResult[0][2]=='leader' :
                if  tdSql.queryResult[1][2]=='offline'  and  tdSql.queryResult[1][3]== 'ready'  :
                    if  tdSql.queryResult[2][2]=='offline' and  tdSql.queryResult[2][3]== 'ready'  :
                        tdLog.success("stop mnodes of follower  on dnode successfully in 10s")
                        return True
            count+=1
        else:
            tdLog.debug(tdSql.queryResult)
            tdLog.exit("stop mnodes  on dnode %d  failed in 10s ")
G
Ganlin Zhao 已提交
227

haoranc's avatar
haoranc 已提交
228 229 230 231 232 233 234



    def close(self):
        self.cursor.close()

clusterComCheck = ClusterComCheck()