dnodes.py 15.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

25
    def init(self, path):
26
        self.__init__()
27 28 29 30 31
        self.path = path

    def getCfgDir(self):
        return self.cfgDir

32 33 34
    def setTestCluster(self, value):
        self.testCluster = value

35 36 37 38 39 40
    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):
41 42 43
        self.logDir = "%s/sim/psim/log" % (self.path,)
        self.cfgDir = "%s/sim/psim/cfg" % (self.path)
        self.cfgPath = "%s/sim/psim/cfg/taos.cfg" % (self.path)
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

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

65 66 67
        if self.testCluster:
            self.cfg("masterIp", "192.168.0.1")
            self.cfg("secondIp", "192.168.0.2")
68 69 70 71
        self.cfg("logDir", self.logDir)
        self.cfg("numOfLogLines", "100000000")
        self.cfg("numOfThreadsPerCore", "2.0")
        self.cfg("locale", "en_US.UTF-8")
72
        self.cfg("charset", "UTF-8")
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
        self.cfg("asyncLog", "0")
        self.cfg("anyIp", "0")
        self.cfg("sdbDebugFlag", "135")
        self.cfg("rpcDebugFlag", "135")
        self.cfg("tmrDebugFlag", "131")
        self.cfg("cDebugFlag", "135")
        self.cfg("udebugFlag", "135")
        self.cfg("jnidebugFlag", "135")
        self.cfg("qdebugFlag", "135")
        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
90
        self.testCluster = False
91
        self.valgrind = 0
92 93 94 95

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

96 97 98
    def setTestCluster(self, value):
        self.testCluster = value

99 100 101
    def setValgrind(self, value):
        self.valgrind = value

102 103 104 105 106 107 108 109 110 111 112 113 114
    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

115
    def deploy(self):
116 117 118 119
        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 已提交
120
            self.path, self.index)
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149

        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)

150 151 152
        if self.testCluster:
            self.startIP()

153 154 155 156 157 158
        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))
159 160 161
        self.cfg("dataDir", self.dataDir)
        self.cfg("logDir", self.logDir)
        self.cfg("numOfLogLines", "100000000")
S
Shengliang Guan 已提交
162
        self.cfg("mnodeEqualVnodeNum", "0")
163
        self.cfg("walLevel", "1")
164 165
        self.cfg("statusInterval", "1")
        self.cfg("numOfTotalVnodes", "64")
166
        self.cfg("numOfMnodes", "3")
167 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
        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")
        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))

    def start(self):
194 195 196
        selfPath = os.path.dirname(os.path.realpath(__file__))
        binPath = ""

197
        if ("community" in selfPath):
198 199 200 201 202
            projPath = selfPath + "/../../../../"

            for root, dirs, files in os.walk(projPath):
                if ("taosd" in files):
                    rootRealPath = os.path.dirname(os.path.realpath(root))
203
                    if ("packaging" not in rootRealPath):
204
                        binPath = os.path.join(root, "taosd")
S
Shuduo Sang 已提交
205
                        break
206 207 208 209 210 211 212
        else:
            projPath = selfPath + "/../../../"
            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):
                        binPath = os.path.join(root, "taosd")
S
Shuduo Sang 已提交
213
                        break
214 215

        if (binPath == ""):
216
            tdLog.exit("taosd not found!")
217
        else:
S
Shuduo Sang 已提交
218
            tdLog.info("taosd found in %s" % rootRealPath)
219 220 221

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

        if self.valgrind == 0:
224
            cmd = "nohup %s -c %s > /dev/null 2>&1 & " % (
225 226 227 228
                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"

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

            print(cmd)

234 235 236 237 238
        if os.system(cmd) != 0:
            tdLog.exit(cmd)
        self.running = 1
        tdLog.debug("dnode:%d is running with %s " % (self.index, cmd))

239 240
        tdLog.debug("wait 5 seconds for the dnode:%d to start." % (self.index))
        time.sleep(5)
241 242

    def stop(self):
243 244 245 246 247
        if self.valgrind == 0:
            toBeKilled = "taosd"
        else:
            toBeKilled = "valgrind.bin"

248
        if self.running != 0:
249
            psCmd = "ps -ef|grep -w %s| grep -v grep | awk '{print $2}'" % toBeKilled
S
Shuduo Sang 已提交
250 251
            processID = subprocess.check_output(
                psCmd, shell=True).decode("utf-8")
252 253

            while(processID):
S
Shuduo Sang 已提交
254
                killCmd = "kill -INT %s" % processID
255 256
                os.system(killCmd)
                time.sleep(1)
S
Shuduo Sang 已提交
257 258
                processID = subprocess.check_output(
                    psCmd, shell=True).decode("utf-8")
259

S
Shuduo Sang 已提交
260
            self.running = 0
261
            tdLog.debug("dnode:%d is stopped by kill -INT" % (self.index))
262 263

    def forcestop(self):
264 265 266 267 268
        if self.valgrind == 0:
            toBeKilled = "taosd"
        else:
            toBeKilled = "valgrind.bin"

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

            while(processID):
S
Shuduo Sang 已提交
275
                killCmd = "kill -KILL %s" % processID
276 277
                os.system(killCmd)
                time.sleep(1)
S
Shuduo Sang 已提交
278 279
                processID = subprocess.check_output(
                    psCmd, shell=True).decode("utf-8")
280

S
Shuduo Sang 已提交
281
            self.running = 0
282
            tdLog.debug("dnode:%d is stopped by kill -KILL" % (self.index))
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300

    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):
301
        dnodeRootDir = "%s/sim/psim/dnode%d" % (self.path, index)
302 303 304
        return dnodeRootDir

    def getDnodesRootDir(self):
305
        dnodesRootDir = "%s/sim/psim" % (self.path)
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
        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))
322
        self.simDeployed = False
323 324

    def init(self, path):
325
        psCmd = "ps -ef|grep -w taosd| grep -v grep | awk '{print $2}'"
S
Shuduo Sang 已提交
326
        processID = subprocess.check_output(psCmd, shell=True).decode("utf-8")
327
        while(processID):
S
Shuduo Sang 已提交
328
            killCmd = "kill -KILL %s" % processID
329 330
            os.system(killCmd)
            time.sleep(1)
S
Shuduo Sang 已提交
331 332
            processID = subprocess.check_output(
                psCmd, shell=True).decode("utf-8")
333 334

        psCmd = "ps -ef|grep -w valgrind.bin| grep -v grep | awk '{print $2}'"
S
Shuduo Sang 已提交
335
        processID = subprocess.check_output(psCmd, shell=True).decode("utf-8")
336
        while(processID):
S
Shuduo Sang 已提交
337
            killCmd = "kill -KILL %s" % processID
338 339
            os.system(killCmd)
            time.sleep(1)
S
Shuduo Sang 已提交
340 341
            processID = subprocess.check_output(
                psCmd, shell=True).decode("utf-8")
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371

        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)

372 373 374
    def setTestCluster(self, value):
        self.testCluster = value

375 376 377
    def setValgrind(self, value):
        self.valgrind = value

378
    def deploy(self, index):
379 380 381
        self.sim = TDSimClient()
        self.sim.init(self.path)
        self.sim.setTestCluster(self.testCluster)
382 383 384 385

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

387
        self.check(index)
388
        self.dnodes[index - 1].setTestCluster(self.testCluster)
389
        self.dnodes[index - 1].setValgrind(self.valgrind)
390 391 392 393 394 395 396 397 398 399 400 401 402 403
        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()

404 405 406 407
    def getDataSize(self, index):
        self.check(index)
        return self.dnodes[index - 1].getDataSize()

408 409 410 411 412 413
    def forcestop(self, index):
        self.check(index)
        self.dnodes[index - 1].forcestop()

    def startIP(self, index):
        self.check(index)
414 415 416

        if self.testCluster:
            self.dnodes[index - 1].startIP()
417 418 419

    def stopIP(self, index):
        self.check(index)
420 421 422

        if self.dnodes[index - 1].testCluster:
            self.dnodes[index - 1].stopIP()
423 424 425 426 427 428

    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 已提交
429
        tdLog.info("stop all dnodes")
430 431 432
        for i in range(len(self.dnodes)):
            self.dnodes[i].stop()

433
        psCmd = "ps -ef | grep -w taosd | grep 'root' | grep -v grep | awk '{print $2}'"
S
Shuduo Sang 已提交
434
        processID = subprocess.check_output(psCmd, shell=True).decode("utf-8")
435 436 437
        if processID:
            cmd = "sudo systemctl stop taosd"
            os.system(cmd)
438 439
        # if os.system(cmd) != 0 :
        # tdLog.exit(cmd)
440
        psCmd = "ps -ef|grep -w taosd| grep -v grep | awk '{print $2}'"
S
Shuduo Sang 已提交
441
        processID = subprocess.check_output(psCmd, shell=True).decode("utf-8")
442
        while(processID):
S
Shuduo Sang 已提交
443
            killCmd = "kill -KILL %s" % processID
444 445
            os.system(killCmd)
            time.sleep(1)
S
Shuduo Sang 已提交
446 447
            processID = subprocess.check_output(
                psCmd, shell=True).decode("utf-8")
448 449

        psCmd = "ps -ef|grep -w valgrind.bin| grep -v grep | awk '{print $2}'"
S
Shuduo Sang 已提交
450
        processID = subprocess.check_output(psCmd, shell=True).decode("utf-8")
451
        while(processID):
S
Shuduo Sang 已提交
452
            killCmd = "kill -KILL %s" % processID
453 454
            os.system(killCmd)
            time.sleep(1)
S
Shuduo Sang 已提交
455 456
            processID = subprocess.check_output(
                psCmd, shell=True).decode("utf-8")
457

458 459 460 461
        # if os.system(cmd) != 0 :
        # tdLog.exit(cmd)

    def getDnodesRootDir(self):
462
        dnodesRootDir = "%s/sim" % (self.path)
463 464 465 466 467 468 469
        return dnodesRootDir

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


tdDnodes = TDDnodes()