cluster.py 3.7 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
from ssl import ALERT_DESCRIPTION_CERTIFICATE_UNOBTAINABLE
import taos
import sys
import time
import os 
import  socket

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

class ClusterDnodes(TDDnodes):
    """rewrite TDDnodes and make MyDdnodes as TDDnodes child class"""
    def __init__(self ,dnodes_lists):

        super(ClusterDnodes,self).__init__()
        self.dnodes = dnodes_lists  # dnode must be TDDnode instance
        self.simDeployed = False
        self.testCluster = False
        self.valgrind = 0
        self.killValgrind = 1


class ConfigureyCluster:
haoranc's avatar
haoranc 已提交
27
    """This will create defined number of dnodes and create a cluster.
haoranc's avatar
haoranc 已提交
28
       at the same time, it will return TDDnodes list:  dnodes, """
haoranc's avatar
haoranc 已提交
29
    hostname = socket.gethostname()
haoranc's avatar
haoranc 已提交
30 31

    def __init__(self):
haoranc's avatar
haoranc 已提交
32 33
        self.dnodes = []      
        self.dnodeNums = 5
haoranc's avatar
haoranc 已提交
34
        self.independent = True
haoranc's avatar
haoranc 已提交
35
        self.startPort = 6030
haoranc's avatar
haoranc 已提交
36
        self.portStep = 100
haoranc's avatar
haoranc 已提交
37
        self.mnodeNums = 0
haoranc's avatar
haoranc 已提交
38

39
    def configure_cluster(self ,dnodeNums=5,mnodeNums=0,independentMnode=True,startPort=6030,portStep=100,hostname="%s"%hostname): 
haoranc's avatar
haoranc 已提交
40
        self.startPort=int(startPort)
haoranc's avatar
haoranc 已提交
41 42
        self.portStep=int(portStep)
        self.hostname=hostname
haoranc's avatar
haoranc 已提交
43 44
        self.dnodeNums = int(dnodeNums)
        self.mnodeNums = int(mnodeNums)
haoranc's avatar
haoranc 已提交
45
        self.dnodes = []
haoranc's avatar
haoranc 已提交
46 47
        startPort_sec = int(startPort+portStep)
        for num in range(1, (self.dnodeNums+1)):
haoranc's avatar
haoranc 已提交
48
            dnode = TDDnode(num)
haoranc's avatar
haoranc 已提交
49
            dnode.addExtraCfg("firstEp", f"{hostname}:{self.startPort}")
haoranc's avatar
haoranc 已提交
50
            dnode.addExtraCfg("fqdn", f"{hostname}")
haoranc's avatar
haoranc 已提交
51 52 53 54
            dnode.addExtraCfg("serverPort", f"{self.startPort + (num-1)*self.portStep}")
            dnode.addExtraCfg("secondEp", f"{hostname}:{startPort_sec}")

            # configure  dnoe of independent mnodes
55
            if num <= self.mnodeNums and self.mnodeNums != 0 and independentMnode == True :
56
                dnode.addExtraCfg("supportVnodes", 1024)
haoranc's avatar
haoranc 已提交
57 58 59 60
            # print(dnode)
            self.dnodes.append(dnode)
        return self.dnodes
        
haoranc's avatar
haoranc 已提交
61
    def create_dnode(self,conn,dnodeNum):
haoranc's avatar
haoranc 已提交
62
        tdSql.init(conn.cursor())
haoranc's avatar
haoranc 已提交
63 64
        dnodeNum=int(dnodeNum)
        for dnode in self.dnodes[1:dnodeNum]:
haoranc's avatar
haoranc 已提交
65 66 67 68 69
            # print(dnode.cfgDict)
            dnode_id = dnode.cfgDict["fqdn"] +  ":" +dnode.cfgDict["serverPort"]
            tdSql.execute(" create dnode '%s';"%dnode_id)

        
70 71 72 73 74 75 76
    def create_mnode(self,conn,mnodeNums):
        tdSql.init(conn.cursor())
        mnodeNums=int(mnodeNums)
        for i in range(2,mnodeNums+1):
            tdSql.execute(" create mnode on  dnode %d;"%i)


haoranc's avatar
haoranc 已提交
77 78 79 80 81
        
    def check_dnode(self,conn):
        tdSql.init(conn.cursor())
        count=0
        while count < 5:
X
Xiaoyu Wang 已提交
82
            tdSql.query("select * from information_schema.ins_dnodes")
haoranc's avatar
haoranc 已提交
83 84
            # tdLog.debug(tdSql.queryResult)
            status=0
haoranc's avatar
haoranc 已提交
85
            for i in range(self.dnodeNums):
haoranc's avatar
haoranc 已提交
86 87
                if tdSql.queryResult[i][4] == "ready":
                    status+=1
haoranc's avatar
haoranc 已提交
88
            # tdLog.debug(status)
haoranc's avatar
haoranc 已提交
89
            
haoranc's avatar
haoranc 已提交
90 91
            if status == self.dnodeNums:
                tdLog.debug(" create cluster with %d dnode and check cluster dnode all ready within 5s! " %self.dnodeNums)
haoranc's avatar
haoranc 已提交
92 93 94 95
                break 
            count+=1
            time.sleep(1)
        else:
haoranc's avatar
haoranc 已提交
96
            tdLog.exit("create cluster with %d dnode but  check dnode not ready within 5s ! "%self.dnodeNums)
haoranc's avatar
haoranc 已提交
97

haoranc's avatar
haoranc 已提交
98 99 100 101 102 103 104
    def checkConnectStatus(self,dnodeNo,hostname=hostname):
        dnodeNo = int(dnodeNo)
        tdLog.info("check dnode-%d connection"%(dnodeNo+1))
        hostname = socket.gethostname()
        port = 6030 + dnodeNo*100
        connectToDnode = tdCom.newcon(host=hostname,port=port)
        return connectToDnode
haoranc's avatar
haoranc 已提交
105

haoranc's avatar
haoranc 已提交
106
cluster = ConfigureyCluster()