query_after_reset-multi-threading.py 6.8 KB
Newer Older
S
Shuduo Sang 已提交
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 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 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 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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
###################################################################
#           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 random
import threading
import queue

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

current_tb = ""
last_tb = ""
written = 0


class Test (threading.Thread):
    def __init__(self, threadId, name, sleepTime, q):
        threading.Thread.__init__(self)
        self.threadId = threadId
        self.name = name
        self.sleepTime = sleepTime
        self.q = q

        self.threadLock = threading.Lock()

    def create_table(self):
        global current_tb
        global last_tb
        global written

        tdLog.info("create a table")
        current_tb = "tb%d" % int(round(time.time() * 1000))
        tdLog.info("current table %s" % current_tb)

        if (current_tb == last_tb):
            return
        else:
            tdSql.execute(
                'create table %s (ts timestamp, speed int)' %
                current_tb)
            last_tb = current_tb
            written = 0

    def insert_data(self):
        global current_tb
        global last_tb
        global written

        tdLog.info("will insert data to table")
        if (current_tb == ""):
            tdLog.info("no table, create first")
            self.create_table()

        tdLog.info("insert data to table")
        for i in range(0, 10):
            self.threadLock.acquire()
            insertRows = 1000
            tdLog.info("insert %d rows to %s" % (insertRows, current_tb))

            for j in range(0, insertRows):
                ret = tdSql.execute(
                    'insert into %s values (now + %dm, %d)' %
                    (current_tb, j, j))
                written = written + 1
            self.threadLock.release()

    def query_data(self):
        global current_tb
        global last_tb
        global written

        if (written > 0):
            tdLog.info("query data from table")
            tdSql.query("select * from %s" % last_tb)
            tdSql.checkRows(written)

    def query_stable(self):
        tdLog.sleep(10)
        self.threadLock.acquire()
        tdLog.info("query super table")

        try:
            tdSql.query("select * from st")
        except Exception as e:
            tdLog.info(repr(e))
            self.q.put(-1)
            tdLog.exit("failed")
        tdSql.checkRows(1)
        self.threadLock.release()

    def create_stable(self):
        global current_tb
        global last_tb
        global written

        self.threadLock.acquire()

        tdLog.info("create a super table and sub-table and insert data")

        try:
            tdSql.execute(
                "create table if not exists st (ts timestamp, tagtype int) tags(dev nchar(50))")
            tdSql.execute(
                'CREATE TABLE if not exists dev_001 using st tags("dev_01")')
            tdSql.execute(
                "INSERT INTO dev_001(ts, tagtype) VALUES('2020-05-13 10:00:00.000', 1)")
        except Exception as e:
            tdLog.info(repr(e))
            self.q.put(-1)
            tdLog.exit("failed")
        self.stop_database()
        self.delete_datafiles()
        self.restart_database()
        self.threadLock.release()

    def stop_database(self):
        tdLog.info("stop databae")
        tdDnodes.stop(1)

    def restart_database(self):
        global current_tb
        global last_tb
        global written

        tdLog.info("restart databae")
        tdDnodes.stop(1)
        tdDnodes.start(1)
        tdLog.sleep(5)

    def force_restart(self):
        global current_tb
        global last_tb
        global written

        tdLog.info("force restart database")
        tdDnodes.forcestop(1)
        tdDnodes.start(1)
        tdLog.sleep(5)

    def drop_table(self):
        global current_tb
        global last_tb
        global written

        for i in range(0, 10):
            self.threadLock.acquire()

            tdLog.info("current_tb %s" % current_tb)

            if (current_tb != ""):
                tdLog.info("drop current tb %s" % current_tb)
                tdSql.execute("drop table %s" % current_tb)
                current_tb = ""
                last_tb = ""
                written = 0
            tdLog.sleep(self.sleepTime)
            self.threadLock.release()

    def reset_query_cache(self):
        global current_tb
        global last_tb
        global written

        tdLog.info("reset query cache")
        tdSql.execute("reset query cache")
        tdLog.sleep(1)

    def reset_database(self):
        global current_tb
        global last_tb
        global written

        tdLog.info("reset database")
        tdDnodes.forcestop(1)
        tdDnodes.deploy(1)
        current_tb = ""
        last_tb = ""
        written = 0
        tdDnodes.start(1)
        tdSql.prepare()

    def delete_datafiles(self):
        global current_tb
        global last_tb
        global written

        tdLog.info("delete data files")
        dnodesDir = tdDnodes.getDnodesRootDir()
        dataDir = dnodesDir + '/dnode1/*'
        deleteCmd = 'rm -rf %s' % dataDir
        os.system(deleteCmd)

        current_tb = ""
        last_tb = ""
        written = 0
        tdDnodes.start(1)
        tdSql.prepare()

    def run(self):
        switch = {
            1: self.create_table,
            2: self.insert_data,
            3: self.query_data,
            4: self.create_stable,
            5: self.restart_database,
            6: self.force_restart,
            7: self.drop_table,
            8: self.reset_query_cache,
            9: self.reset_database,
            10: self.delete_datafiles,
            11: self.query_stable,
        }

        switch.get(self.threadId, lambda: "ERROR")()


class TDTestCase:
    def init(self, conn, logSql):
        tdLog.debug("start to execute %s" % __file__)
        tdSql.init(conn.cursor(), logSql)

    def run(self):
        tdSql.prepare()

        q = queue.Queue()
        test1 = Test(4, "create_stable", 1, q)
        test2 = Test(11, "query_stable", 2, q)

        test1.start()
        test2.start()
        test1.join()
        test2.join()

        while not q.empty():
            if (q.get() == -1):
                tdLog.exit("failed to end of test")

        tdLog.info("end of test")

    def stop(self):
        tdSql.close()
        tdLog.success("%s successfully executed" % __file__)


tdCases.addWindows(__file__, TDTestCase())
tdCases.addLinux(__file__, TDTestCase())