dnodes.py 16.0 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 19 20 21
from util.log import *


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

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

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

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

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

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

62 63 64 65 66 67
    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 已提交
68
        self.logDir = "%s/sim/psim/log" % (self.path)
69 70
        self.cfgDir = "%s/sim/psim/cfg" % (self.path)
        self.cfgPath = "%s/sim/psim/cfg/taos.cfg" % (self.path)
71 72 73 74

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

S
Shuduo Sang 已提交
80
        cmd = "rm -rf " + self.cfgDir
81 82 83 84 85 86 87 88 89 90 91
        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)

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

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

100 101 102 103 104 105 106 107
        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
108
        self.testCluster = False
109
        self.valgrind = 0
110 111 112 113

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

114 115 116
    def setTestCluster(self, value):
        self.testCluster = value

117 118 119
    def setValgrind(self, value):
        self.valgrind = value

120 121 122 123 124 125 126 127 128 129 130 131 132
    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

133
    def deploy(self):
134 135 136 137
        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 已提交
138
            self.path, self.index)
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167

        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)

168 169 170
        if self.testCluster:
            self.startIP()

171 172 173 174 175 176
        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))
177 178 179
        self.cfg("dataDir", self.dataDir)
        self.cfg("logDir", self.logDir)
        self.cfg("numOfLogLines", "100000000")
S
Shengliang Guan 已提交
180
        self.cfg("mnodeEqualVnodeNum", "0")
181 182
        self.cfg("walLevel", "2")
        self.cfg("fsync", "1000")
183
        self.cfg("statusInterval", "1")
184
        self.cfg("numOfMnodes", "3")
185 186 187 188 189 190 191 192 193 194
        self.cfg("numOfThreadsPerCore", "2.0")
        self.cfg("monitor", "0")
        self.cfg("maxVnodeConnections", "30000")
        self.cfg("maxMgmtConnections", "30000")
        self.cfg("maxMeterConnections", "30000")
        self.cfg("maxShellConns", "30000")
        self.cfg("locale", "en_US.UTF-8")
        self.cfg("charset", "UTF-8")
        self.cfg("asyncLog", "0")
        self.cfg("anyIp", "0")
195
        self.cfg("tsEnableTelemetryReporting", "0")
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
        self.cfg("dDebugFlag", "135")
        self.cfg("mDebugFlag", "135")
        self.cfg("sdbDebugFlag", "135")
        self.cfg("rpcDebugFlag", "135")
        self.cfg("tmrDebugFlag", "131")
        self.cfg("cDebugFlag", "135")
        self.cfg("httpDebugFlag", "135")
        self.cfg("monitorDebugFlag", "135")
        self.cfg("udebugFlag", "135")
        self.cfg("jnidebugFlag", "135")
        self.cfg("qdebugFlag", "135")
        self.deployed = 1
        tdLog.debug(
            "dnode:%d is deployed and configured by %s" %
            (self.index, self.cfgPath))

212
    def getBuildPath(self):
213 214
        selfPath = os.path.dirname(os.path.realpath(__file__))

215
        if ("community" in selfPath):
216
            projPath = selfPath[:selfPath.find("community")]
217
        else:
218 219 220 221 222 223
            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 已提交
224
                    buildPath = root[:len(root)-len("/build/bin")]
225 226 227 228 229 230 231
                    break
        return buildPath

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

        if (buildPath == ""):
232
            tdLog.exit("taosd not found!")
233
        else:
234 235 236
            tdLog.info("taosd found in %s" % buildPath)

        binPath = buildPath + "/build/bin/taosd"
237 238 239

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

        if self.valgrind == 0:
242
            cmd = "nohup %s -c %s > /dev/null 2>&1 & " % (
243 244 245 246
                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"

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

            print(cmd)

252 253 254 255 256
        if os.system(cmd) != 0:
            tdLog.exit(cmd)
        self.running = 1
        tdLog.debug("dnode:%d is running with %s " % (self.index, cmd))

257 258
        tdLog.debug("wait 5 seconds for the dnode:%d to start." % (self.index))
        time.sleep(5)
259 260

    def stop(self):
261 262 263 264 265
        if self.valgrind == 0:
            toBeKilled = "taosd"
        else:
            toBeKilled = "valgrind.bin"

266
        if self.running != 0:
267
            psCmd = "ps -ef|grep -w %s| grep -v grep | awk '{print $2}'" % toBeKilled
S
Shuduo Sang 已提交
268 269
            processID = subprocess.check_output(
                psCmd, shell=True).decode("utf-8")
270 271

            while(processID):
272
                killCmd = "kill -INT %s > /dev/null 2>&1" % processID
273 274
                os.system(killCmd)
                time.sleep(1)
S
Shuduo Sang 已提交
275 276
                processID = subprocess.check_output(
                    psCmd, shell=True).decode("utf-8")
277 278 279 280 281
            for port in range(6030, 6041):
                fuserCmd = "fuser -k -n tcp %d" % port
                os.system(fuserCmd)
            if self.valgrind:
                time.sleep(2)
282

S
Shuduo Sang 已提交
283
            self.running = 0
284
            tdLog.debug("dnode:%d is stopped by kill -INT" % (self.index))
285 286

    def forcestop(self):
287 288 289 290 291
        if self.valgrind == 0:
            toBeKilled = "taosd"
        else:
            toBeKilled = "valgrind.bin"

292
        if self.running != 0:
293
            psCmd = "ps -ef|grep -w %s| grep -v grep | awk '{print $2}'" % toBeKilled
S
Shuduo Sang 已提交
294 295
            processID = subprocess.check_output(
                psCmd, shell=True).decode("utf-8")
296 297

            while(processID):
298
                killCmd = "kill -KILL %s > /dev/null 2>&1" % processID
299 300
                os.system(killCmd)
                time.sleep(1)
S
Shuduo Sang 已提交
301 302
                processID = subprocess.check_output(
                    psCmd, shell=True).decode("utf-8")
303 304 305 306 307
            for port in range(6030, 6041):
                fuserCmd = "fuser -k -n tcp %d" % port
                os.system(fuserCmd)
            if self.valgrind:
                time.sleep(2)
308

S
Shuduo Sang 已提交
309
            self.running = 0
310
            tdLog.debug("dnode:%d is stopped by kill -KILL" % (self.index))
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328

    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):
329
        dnodeRootDir = "%s/sim/psim/dnode%d" % (self.path, index)
330 331 332
        return dnodeRootDir

    def getDnodesRootDir(self):
333
        dnodesRootDir = "%s/sim/psim" % (self.path)
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
        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))
350
        self.simDeployed = False
351 352

    def init(self, path):
353
        psCmd = "ps -ef|grep -w taosd| grep -v grep | awk '{print $2}'"
S
Shuduo Sang 已提交
354
        processID = subprocess.check_output(psCmd, shell=True).decode("utf-8")
355
        while(processID):
356
            killCmd = "kill -TERM %s > /dev/null 2>&1" % processID
357 358
            os.system(killCmd)
            time.sleep(1)
S
Shuduo Sang 已提交
359 360
            processID = subprocess.check_output(
                psCmd, shell=True).decode("utf-8")
361 362

        psCmd = "ps -ef|grep -w valgrind.bin| grep -v grep | awk '{print $2}'"
S
Shuduo Sang 已提交
363
        processID = subprocess.check_output(psCmd, shell=True).decode("utf-8")
364
        while(processID):
365
            killCmd = "kill -TERM %s > /dev/null 2>&1" % processID
366 367
            os.system(killCmd)
            time.sleep(1)
S
Shuduo Sang 已提交
368 369
            processID = subprocess.check_output(
                psCmd, shell=True).decode("utf-8")
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399

        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 已提交
400 401 402
        self.sim = TDSimClient()
        self.sim.init(self.path)

403 404 405
    def setTestCluster(self, value):
        self.testCluster = value

406 407 408
    def setValgrind(self, value):
        self.valgrind = value

409
    def deploy(self, index):
410
        self.sim.setTestCluster(self.testCluster)
411 412 413 414

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

416
        self.check(index)
417
        self.dnodes[index - 1].setTestCluster(self.testCluster)
418
        self.dnodes[index - 1].setValgrind(self.valgrind)
419 420 421 422 423 424 425 426 427 428 429 430 431 432
        self.dnodes[index - 1].deploy()

    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()

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

433 434 435 436
    def getDataSize(self, index):
        self.check(index)
        return self.dnodes[index - 1].getDataSize()

437 438 439 440 441 442
    def forcestop(self, index):
        self.check(index)
        self.dnodes[index - 1].forcestop()

    def startIP(self, index):
        self.check(index)
443 444 445

        if self.testCluster:
            self.dnodes[index - 1].startIP()
446 447 448

    def stopIP(self, index):
        self.check(index)
449 450 451

        if self.dnodes[index - 1].testCluster:
            self.dnodes[index - 1].stopIP()
452 453 454 455 456 457

    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 已提交
458
        tdLog.info("stop all dnodes")
459 460 461
        for i in range(len(self.dnodes)):
            self.dnodes[i].stop()

462
        psCmd = "ps -ef | grep -w taosd | grep 'root' | grep -v grep | awk '{print $2}'"
S
Shuduo Sang 已提交
463
        processID = subprocess.check_output(psCmd, shell=True).decode("utf-8")
464 465 466
        if processID:
            cmd = "sudo systemctl stop taosd"
            os.system(cmd)
467 468
        # if os.system(cmd) != 0 :
        # tdLog.exit(cmd)
469
        psCmd = "ps -ef|grep -w taosd| grep -v grep | awk '{print $2}'"
S
Shuduo Sang 已提交
470
        processID = subprocess.check_output(psCmd, shell=True).decode("utf-8")
471
        while(processID):
472
            killCmd = "kill -TERM %s > /dev/null 2>&1" % processID
473 474
            os.system(killCmd)
            time.sleep(1)
S
Shuduo Sang 已提交
475 476
            processID = subprocess.check_output(
                psCmd, shell=True).decode("utf-8")
477 478

        psCmd = "ps -ef|grep -w valgrind.bin| grep -v grep | awk '{print $2}'"
S
Shuduo Sang 已提交
479
        processID = subprocess.check_output(psCmd, shell=True).decode("utf-8")
480
        while(processID):
481
            killCmd = "kill -TERM %s > /dev/null 2>&1" % processID
482 483
            os.system(killCmd)
            time.sleep(1)
S
Shuduo Sang 已提交
484 485
            processID = subprocess.check_output(
                psCmd, shell=True).decode("utf-8")
486

487 488 489 490
        # if os.system(cmd) != 0 :
        # tdLog.exit(cmd)

    def getDnodesRootDir(self):
491
        dnodesRootDir = "%s/sim" % (self.path)
492 493 494 495 496
        return dnodesRootDir

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

S
Shuduo Sang 已提交
497 498 499 500 501 502
    def getSimLogPath(self):
        return self.sim.getLogDir()

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

503 504

tdDnodes = TDDnodes()