dnodes.py 18.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
###################################################################
#           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 -*-

import sys
import os
import os.path
17
import subprocess
18
from time import sleep
19 20 21 22
from util.log import *


class TDSimClient:
23 24 25
    def __init__(self):
        self.testCluster = False

S
Shuduo Sang 已提交
26 27 28 29 30 31
        self.cfgDict = {
            "numOfLogLines": "100000000",
            "numOfThreadsPerCore": "2.0",
            "locale": "en_US.UTF-8",
            "charset": "UTF-8",
            "asyncLog": "0",
32 33 34
            "minTablesPerVnode": "4",
            "maxTablesPerVnode": "1000",
            "tableIncStepPerVnode": "10000",
35 36
            "maxVgroupsPerDb": "1000",
            "sdbDebugFlag": "143",
S
Shuduo Sang 已提交
37 38 39 40 41 42
            "rpcDebugFlag": "135",
            "tmrDebugFlag": "131",
            "cDebugFlag": "135",
            "udebugFlag": "135",
            "jnidebugFlag": "135",
            "qdebugFlag": "135",
43
            "telemetryReporting": "0",
S
Shuduo Sang 已提交
44
            }
45
    def init(self, path):
46
        self.__init__()
47 48
        self.path = path

S
Shuduo Sang 已提交
49 50 51 52
    def getLogDir(self):
        self.logDir = "%s/sim/psim/log" % (self.path)
        return self.logDir

53
    def getCfgDir(self):
S
Shuduo Sang 已提交
54
        self.cfgDir = "%s/sim/psim/cfg" % (self.path)
55 56
        return self.cfgDir

57 58 59
    def setTestCluster(self, value):
        self.testCluster = value

S
Shuduo Sang 已提交
60 61 62
    def addExtraCfg(self, option, value):
        self.cfgDict.update({option: value})

63 64 65 66 67 68
    def cfg(self, option, value):
        cmd = "echo '%s %s' >> %s" % (option, value, self.cfgPath)
        if os.system(cmd) != 0:
            tdLog.exit(cmd)

    def deploy(self):
S
Shuduo Sang 已提交
69
        self.logDir = "%s/sim/psim/log" % (self.path)
70 71
        self.cfgDir = "%s/sim/psim/cfg" % (self.path)
        self.cfgPath = "%s/sim/psim/cfg/taos.cfg" % (self.path)
72 73 74 75

        cmd = "rm -rf " + self.logDir
        if os.system(cmd) != 0:
            tdLog.exit(cmd)
S
Shuduo Sang 已提交
76 77
    
        cmd = "mkdir -p " + self.logDir
78 79 80
        if os.system(cmd) != 0:
            tdLog.exit(cmd)

S
Shuduo Sang 已提交
81
        cmd = "rm -rf " + self.cfgDir
82 83 84 85 86 87 88 89 90 91 92
        if os.system(cmd) != 0:
            tdLog.exit(cmd)

        cmd = "mkdir -p " + self.cfgDir
        if os.system(cmd) != 0:
            tdLog.exit(cmd)

        cmd = "touch " + self.cfgPath
        if os.system(cmd) != 0:
            tdLog.exit(cmd)

93 94 95
        if self.testCluster:
            self.cfg("masterIp", "192.168.0.1")
            self.cfg("secondIp", "192.168.0.2")
96
        self.cfg("logDir", self.logDir)
S
Shuduo Sang 已提交
97 98 99 100

        for key, value in self.cfgDict.items():
            self.cfg(key, value)

101 102 103 104 105 106 107 108
        tdLog.debug("psim is deployed and configured by %s" % (self.cfgPath))


class TDDnode:
    def __init__(self, index):
        self.index = index
        self.running = 0
        self.deployed = 0
109
        self.testCluster = False
110
        self.valgrind = 0
L
liuyq-617 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
        self.cfgDict = {
            "numOfLogLines":"100000000",
            "mnodeEqualVnodeNum":"0",
            "walLevel":"2",
            "fsync":"1000",
            "statusInterval":"1",
            "numOfMnodes":"3",
            "numOfThreadsPerCore":"2.0",
            "monitor":"0",
            "maxVnodeConnections":"30000",
            "maxMgmtConnections":"30000",
            "maxMeterConnections":"30000",
            "maxShellConns":"30000",
            "locale":"en_US.UTF-8",
            "charset":"UTF-8",
            "asyncLog":"0",
            "anyIp":"0",
            "tsEnableTelemetryReporting":"0",
            "dDebugFlag":"135",
            "mDebugFlag":"135",
            "sdbDebugFlag":"135",
            "rpcDebugFlag":"135",
            "tmrDebugFlag":"131",
            "cDebugFlag":"135",
            "httpDebugFlag":"135",
            "monitorDebugFlag":"135",
            "udebugFlag":"135",
            "jnidebugFlag":"135",
            "qdebugFlag":"135"
        }
141 142 143 144

    def init(self, path):
        self.path = path

145 146 147
    def setTestCluster(self, value):
        self.testCluster = value

148 149 150
    def setValgrind(self, value):
        self.valgrind = value

151 152 153 154 155 156 157 158 159 160 161 162 163
    def getDataSize(self):
        totalSize = 0

        if (self.deployed == 1):
            for dirpath, dirnames, filenames in os.walk(self.dataDir):
                for f in filenames:
                    fp = os.path.join(dirpath, f)

                    if not os.path.islink(fp):
                        totalSize = totalSize + os.path.getsize(fp)

        return totalSize

L
liuyq-617 已提交
164 165 166 167
    def addExtraCfg(self, option, value):
        self.cfgDict.update({option: value})

    def deploy(self, *updatecfgDict):
168 169 170 171
        self.logDir = "%s/sim/dnode%d/log" % (self.path, self.index)
        self.dataDir = "%s/sim/dnode%d/data" % (self.path, self.index)
        self.cfgDir = "%s/sim/dnode%d/cfg" % (self.path, self.index)
        self.cfgPath = "%s/sim/dnode%d/cfg/taos.cfg" % (
S
Shuduo Sang 已提交
172
            self.path, self.index)
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

        cmd = "rm -rf " + self.dataDir
        if os.system(cmd) != 0:
            tdLog.exit(cmd)

        cmd = "rm -rf " + self.logDir
        if os.system(cmd) != 0:
            tdLog.exit(cmd)

        cmd = "rm -rf " + self.cfgDir
        if os.system(cmd) != 0:
            tdLog.exit(cmd)

        cmd = "mkdir -p " + self.dataDir
        if os.system(cmd) != 0:
            tdLog.exit(cmd)

        cmd = "mkdir -p " + self.logDir
        if os.system(cmd) != 0:
            tdLog.exit(cmd)

        cmd = "mkdir -p " + self.cfgDir
        if os.system(cmd) != 0:
            tdLog.exit(cmd)

        cmd = "touch " + self.cfgPath
        if os.system(cmd) != 0:
            tdLog.exit(cmd)

202 203 204
        if self.testCluster:
            self.startIP()

205 206 207 208 209 210
        if self.testCluster:
            self.cfg("masterIp", "192.168.0.1")
            self.cfg("secondIp", "192.168.0.2")
            self.cfg("publicIp", "192.168.0.%d" % (self.index))
            self.cfg("internalIp", "192.168.0.%d" % (self.index))
            self.cfg("privateIp", "192.168.0.%d" % (self.index))
L
liuyq-617 已提交
211 212 213 214
        self.cfgDict["dataDir"] = self.dataDir
        self.cfgDict["logDir"] = self.logDir
        # self.cfg("dataDir",self.dataDir)
        # self.cfg("logDir",self.logDir)
L
liuyq-617 已提交
215
        print(updatecfgDict)
L
liuyq-617 已提交
216
        isFirstDir = 1
L
liuyq-617 已提交
217 218 219
        if updatecfgDict[0] and updatecfgDict[0][0]:
            print(updatecfgDict[0][0])
            for key,value in updatecfgDict[0][0].items():
L
liuyq-617 已提交
220 221 222 223 224 225 226 227 228
                if value == 'dataDir' :
                    if isFirstDir:
                        self.cfgDict.pop('dataDir')
                        self.cfg(value,key)
                        isFirstDir = 0
                    else:
                        self.cfg(value,key)
                else:
                    self.addExtraCfg(key,value)
L
liuyq-617 已提交
229 230 231
        for key, value in self.cfgDict.items():
            self.cfg(key, value)

232 233 234 235 236
        self.deployed = 1
        tdLog.debug(
            "dnode:%d is deployed and configured by %s" %
            (self.index, self.cfgPath))

237
    def getBuildPath(self):
238
        buildPath = ""
239 240
        selfPath = os.path.dirname(os.path.realpath(__file__))

241
        if ("community" in selfPath):
242
            projPath = selfPath[:selfPath.find("community")]
243
        else:
244 245 246 247 248 249
            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):
S
Shuduo Sang 已提交
250
                    buildPath = root[:len(root)-len("/build/bin")]
251 252 253 254 255 256 257
                    break
        return buildPath

    def start(self):
        buildPath = self.getBuildPath()

        if (buildPath == ""):
258
            tdLog.exit("taosd not found!")
259
        else:
260 261 262
            tdLog.info("taosd found in %s" % buildPath)

        binPath = buildPath + "/build/bin/taosd"
263 264 265

        if self.deployed == 0:
            tdLog.exit("dnode:%d is not deployed" % (self.index))
266 267

        if self.valgrind == 0:
268
            cmd = "nohup %s -c %s > /dev/null 2>&1 & " % (
269 270 271 272
                binPath, self.cfgDir)
        else:
            valgrindCmdline = "valgrind --tool=memcheck --leak-check=full --show-reachable=no --track-origins=yes --show-leak-kinds=all -v --workaround-gcc296-bugs=yes"

273
            cmd = "nohup %s %s -c %s 2>&1 & " % (
274 275 276 277
                valgrindCmdline, binPath, self.cfgDir)

            print(cmd)

278 279 280 281
        if os.system(cmd) != 0:
            tdLog.exit(cmd)
        self.running = 1
        tdLog.debug("dnode:%d is running with %s " % (self.index, cmd))
L
liuyq-617 已提交
282 283 284 285 286
        if self.valgrind == 0:
            time.sleep(0.1)
            key = 'from offline to online'
            bkey = bytes(key,encoding="utf8")
            logFile = self.logDir + "/taosdlog.0"
L
liuyq-617 已提交
287 288 289 290 291 292
            i = 0
            while not os.path.exists(logFile):
                sleep(0.1)
                i += 1
                if i>50:
                    break
L
liuyq-617 已提交
293 294 295 296 297 298 299 300 301 302 303
            popen = subprocess.Popen('tail -f ' + logFile, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
            pid = popen.pid
            print('Popen.pid:' + str(pid))
            while True:
                line = popen.stdout.readline().strip()
                if bkey in line:
                    print(line)
                    popen.kill()
                    break
            tdLog.debug("the dnode:%d has been started." % (self.index))
        else:
L
liuyq-617 已提交
304 305
            tdLog.debug("wait 10 seconds for the dnode:%d to start." % (self.index))
            time.sleep(10)
L
liuyq-617 已提交
306

L
liuyq-617 已提交
307 308
        
        # time.sleep(5)
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
    
    def startWithoutSleep(self):
        buildPath = self.getBuildPath()

        if (buildPath == ""):
            tdLog.exit("taosd not found!")
        else:
            tdLog.info("taosd found in %s" % buildPath)

        binPath = buildPath + "/build/bin/taosd"

        if self.deployed == 0:
            tdLog.exit("dnode:%d is not deployed" % (self.index))

        if self.valgrind == 0:
            cmd = "nohup %s -c %s > /dev/null 2>&1 & " % (
                binPath, self.cfgDir)
        else:
            valgrindCmdline = "valgrind --tool=memcheck --leak-check=full --show-reachable=no --track-origins=yes --show-leak-kinds=all -v --workaround-gcc296-bugs=yes"

            cmd = "nohup %s %s -c %s 2>&1 & " % (
                valgrindCmdline, binPath, self.cfgDir)

            print(cmd)

        if os.system(cmd) != 0:
            tdLog.exit(cmd)
        self.running = 1
        tdLog.debug("dnode:%d is running with %s " % (self.index, cmd))
338 339

    def stop(self):
340 341 342 343 344
        if self.valgrind == 0:
            toBeKilled = "taosd"
        else:
            toBeKilled = "valgrind.bin"

345
        if self.running != 0:
346
            psCmd = "ps -ef|grep -w %s| grep -v grep | awk '{print $2}'" % toBeKilled
S
Shuduo Sang 已提交
347 348
            processID = subprocess.check_output(
                psCmd, shell=True).decode("utf-8")
349 350

            while(processID):
351
                killCmd = "kill -INT %s > /dev/null 2>&1" % processID
352 353
                os.system(killCmd)
                time.sleep(1)
S
Shuduo Sang 已提交
354 355
                processID = subprocess.check_output(
                    psCmd, shell=True).decode("utf-8")
356 357 358 359 360
            for port in range(6030, 6041):
                fuserCmd = "fuser -k -n tcp %d" % port
                os.system(fuserCmd)
            if self.valgrind:
                time.sleep(2)
361

S
Shuduo Sang 已提交
362
            self.running = 0
363
            tdLog.debug("dnode:%d is stopped by kill -INT" % (self.index))
364 365

    def forcestop(self):
366 367 368 369 370
        if self.valgrind == 0:
            toBeKilled = "taosd"
        else:
            toBeKilled = "valgrind.bin"

371
        if self.running != 0:
372
            psCmd = "ps -ef|grep -w %s| grep -v grep | awk '{print $2}'" % toBeKilled
S
Shuduo Sang 已提交
373 374
            processID = subprocess.check_output(
                psCmd, shell=True).decode("utf-8")
375 376

            while(processID):
377
                killCmd = "kill -KILL %s > /dev/null 2>&1" % processID
378 379
                os.system(killCmd)
                time.sleep(1)
S
Shuduo Sang 已提交
380 381
                processID = subprocess.check_output(
                    psCmd, shell=True).decode("utf-8")
382 383 384 385 386
            for port in range(6030, 6041):
                fuserCmd = "fuser -k -n tcp %d" % port
                os.system(fuserCmd)
            if self.valgrind:
                time.sleep(2)
387

S
Shuduo Sang 已提交
388
            self.running = 0
389
            tdLog.debug("dnode:%d is stopped by kill -KILL" % (self.index))
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407

    def startIP(self):
        cmd = "sudo ifconfig lo:%d 192.168.0.%d up" % (self.index, self.index)
        if os.system(cmd) != 0:
            tdLog.exit(cmd)

    def stopIP(self):
        cmd = "sudo ifconfig lo:%d 192.168.0.%d down" % (
            self.index, self.index)
        if os.system(cmd) != 0:
            tdLog.exit(cmd)

    def cfg(self, option, value):
        cmd = "echo '%s %s' >> %s" % (option, value, self.cfgPath)
        if os.system(cmd) != 0:
            tdLog.exit(cmd)

    def getDnodeRootDir(self, index):
408
        dnodeRootDir = "%s/sim/psim/dnode%d" % (self.path, index)
409 410 411
        return dnodeRootDir

    def getDnodesRootDir(self):
412
        dnodesRootDir = "%s/sim/psim" % (self.path)
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
        return dnodesRootDir


class TDDnodes:
    def __init__(self):
        self.dnodes = []
        self.dnodes.append(TDDnode(1))
        self.dnodes.append(TDDnode(2))
        self.dnodes.append(TDDnode(3))
        self.dnodes.append(TDDnode(4))
        self.dnodes.append(TDDnode(5))
        self.dnodes.append(TDDnode(6))
        self.dnodes.append(TDDnode(7))
        self.dnodes.append(TDDnode(8))
        self.dnodes.append(TDDnode(9))
        self.dnodes.append(TDDnode(10))
429
        self.simDeployed = False
430 431

    def init(self, path):
432
        psCmd = "ps -ef|grep -w taosd| grep -v grep | awk '{print $2}'"
S
Shuduo Sang 已提交
433
        processID = subprocess.check_output(psCmd, shell=True).decode("utf-8")
434
        while(processID):
435
            killCmd = "kill -TERM %s > /dev/null 2>&1" % processID
436 437
            os.system(killCmd)
            time.sleep(1)
S
Shuduo Sang 已提交
438 439
            processID = subprocess.check_output(
                psCmd, shell=True).decode("utf-8")
440 441

        psCmd = "ps -ef|grep -w valgrind.bin| grep -v grep | awk '{print $2}'"
S
Shuduo Sang 已提交
442
        processID = subprocess.check_output(psCmd, shell=True).decode("utf-8")
443
        while(processID):
444
            killCmd = "kill -TERM %s > /dev/null 2>&1" % processID
445 446
            os.system(killCmd)
            time.sleep(1)
S
Shuduo Sang 已提交
447 448
            processID = subprocess.check_output(
                psCmd, shell=True).decode("utf-8")
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478

        binPath = os.path.dirname(os.path.realpath(__file__))
        binPath = binPath + "/../../../debug/"
        tdLog.debug("binPath %s" % (binPath))
        binPath = os.path.realpath(binPath)
        tdLog.debug("binPath real path %s" % (binPath))

        # cmd = "sudo cp %s/build/lib/libtaos.so /usr/local/lib/taos/" % (binPath)
        # tdLog.debug(cmd)
        # os.system(cmd)

        # cmd = "sudo cp %s/build/bin/taos /usr/local/bin/taos/" % (binPath)
        # if os.system(cmd) != 0 :
        #  tdLog.exit(cmd)
        # tdLog.debug("execute %s" % (cmd))

        # cmd = "sudo cp %s/build/bin/taosd /usr/local/bin/taos/" % (binPath)
        # if os.system(cmd) != 0 :
        # tdLog.exit(cmd)
        # tdLog.debug("execute %s" % (cmd))

        if path == "":
            # self.path = os.path.expanduser('~')
            self.path = os.path.abspath(binPath + "../../")
        else:
            self.path = os.path.realpath(path)

        for i in range(len(self.dnodes)):
            self.dnodes[i].init(self.path)

S
Shuduo Sang 已提交
479 480 481
        self.sim = TDSimClient()
        self.sim.init(self.path)

482 483 484
    def setTestCluster(self, value):
        self.testCluster = value

485 486 487
    def setValgrind(self, value):
        self.valgrind = value

L
liuyq-617 已提交
488
    def deploy(self, index, *updatecfgDict):
489
        self.sim.setTestCluster(self.testCluster)
490 491 492 493

        if (self.simDeployed == False):
            self.sim.deploy()
            self.simDeployed = True
494

495
        self.check(index)
496
        self.dnodes[index - 1].setTestCluster(self.testCluster)
497
        self.dnodes[index - 1].setValgrind(self.valgrind)
L
liuyq-617 已提交
498
        self.dnodes[index - 1].deploy(updatecfgDict)
499 500 501 502 503 504 505 506

    def cfg(self, index, option, value):
        self.check(index)
        self.dnodes[index - 1].cfg(option, value)

    def start(self, index):
        self.check(index)
        self.dnodes[index - 1].start()
507 508 509 510
    
    def startWithoutSleep(self, index):
        self.check(index)
        self.dnodes[index - 1].startWithoutSleep()
511 512 513 514 515

    def stop(self, index):
        self.check(index)
        self.dnodes[index - 1].stop()

516 517 518 519
    def getDataSize(self, index):
        self.check(index)
        return self.dnodes[index - 1].getDataSize()

520 521 522 523 524 525
    def forcestop(self, index):
        self.check(index)
        self.dnodes[index - 1].forcestop()

    def startIP(self, index):
        self.check(index)
526 527 528

        if self.testCluster:
            self.dnodes[index - 1].startIP()
529 530 531

    def stopIP(self, index):
        self.check(index)
532 533 534

        if self.dnodes[index - 1].testCluster:
            self.dnodes[index - 1].stopIP()
535 536 537 538 539 540

    def check(self, index):
        if index < 1 or index > 10:
            tdLog.exit("index:%d should on a scale of [1, 10]" % (index))

    def stopAll(self):
S
Shuduo Sang 已提交
541
        tdLog.info("stop all dnodes")
542 543 544
        for i in range(len(self.dnodes)):
            self.dnodes[i].stop()

545
        psCmd = "ps -ef | grep -w taosd | grep 'root' | grep -v grep | awk '{print $2}'"
S
Shuduo Sang 已提交
546
        processID = subprocess.check_output(psCmd, shell=True).decode("utf-8")
547 548 549
        if processID:
            cmd = "sudo systemctl stop taosd"
            os.system(cmd)
550 551
        # if os.system(cmd) != 0 :
        # tdLog.exit(cmd)
552
        psCmd = "ps -ef|grep -w taosd| grep -v grep | awk '{print $2}'"
S
Shuduo Sang 已提交
553
        processID = subprocess.check_output(psCmd, shell=True).decode("utf-8")
554
        while(processID):
555
            killCmd = "kill -TERM %s > /dev/null 2>&1" % processID
556 557
            os.system(killCmd)
            time.sleep(1)
S
Shuduo Sang 已提交
558 559
            processID = subprocess.check_output(
                psCmd, shell=True).decode("utf-8")
560 561

        psCmd = "ps -ef|grep -w valgrind.bin| grep -v grep | awk '{print $2}'"
S
Shuduo Sang 已提交
562
        processID = subprocess.check_output(psCmd, shell=True).decode("utf-8")
563
        while(processID):
564
            killCmd = "kill -TERM %s > /dev/null 2>&1" % processID
565 566
            os.system(killCmd)
            time.sleep(1)
S
Shuduo Sang 已提交
567 568
            processID = subprocess.check_output(
                psCmd, shell=True).decode("utf-8")
569

570 571 572 573
        # if os.system(cmd) != 0 :
        # tdLog.exit(cmd)

    def getDnodesRootDir(self):
574
        dnodesRootDir = "%s/sim" % (self.path)
575 576 577 578 579
        return dnodesRootDir

    def getSimCfgPath(self):
        return self.sim.getCfgDir()

S
Shuduo Sang 已提交
580 581 582 583 584 585
    def getSimLogPath(self):
        return self.sim.getLogDir()

    def addSimExtraCfg(self, option, value):
        self.sim.addExtraCfg(option, value)

586 587

tdDnodes = TDDnodes()