crash_gen.py 110.5 KB
Newer Older
S
Shuduo Sang 已提交
1
# -----!/usr/bin/python3.7
S
Steven Li 已提交
2 3 4 5 6 7 8 9 10 11 12 13
###################################################################
#           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 -*-
S
Shuduo Sang 已提交
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
# For type hinting before definition, ref:
# https://stackoverflow.com/questions/33533148/how-do-i-specify-that-the-return-type-of-a-method-is-the-same-as-the-class-itsel
from __future__ import annotations
import taos
import crash_gen
from util.sql import *
from util.cases import *
from util.dnodes import *
from util.log import *
from queue import Queue, Empty
from typing import IO
from typing import Set
from typing import Dict
from typing import List
from requests.auth import HTTPBasicAuth
import textwrap
import datetime
import logging
import time
import random
import threading
import requests
import copy
import argparse
import getopt
39

S
Steven Li 已提交
40
import sys
41
import os
42 43
import io
import signal
44
import traceback
45 46 47 48 49 50 51

try:
    import psutil
except:
    print("Psutil module needed, please install: sudo pip3 install psutil")
    sys.exit(-1)

52 53 54 55
# Require Python 3
if sys.version_info[0] < 3:
    raise Exception("Must be using Python 3")

S
Steven Li 已提交
56

S
Shuduo Sang 已提交
57
# Global variables, tried to keep a small number.
58 59 60

# Command-line/Environment Configurations, will set a bit later
# ConfigNameSpace = argparse.Namespace
S
Shuduo Sang 已提交
61
gConfig = argparse.Namespace()  # Dummy value, will be replaced later
62
gSvcMgr = None # TODO: refactor this hack, use dep injection
63
logger = None
S
Steven Li 已提交
64

S
Shuduo Sang 已提交
65
def runThread(wt: WorkerThread):
66
    wt.run()
67

68 69
class CrashGenError(Exception):
    def __init__(self, msg=None, errno=None):
S
Shuduo Sang 已提交
70
        self.msg = msg
71
        self.errno = errno
S
Shuduo Sang 已提交
72

73 74 75
    def __str__(self):
        return self.msg

S
Shuduo Sang 已提交
76

S
Steven Li 已提交
77
class WorkerThread:
78
    def __init__(self, pool: ThreadPool, tid, tc: ThreadCoordinator,
S
Shuduo Sang 已提交
79 80 81
                 # te: TaskExecutor,
                 ):  # note: main thread context!
        # self._curStep = -1
82
        self._pool = pool
S
Shuduo Sang 已提交
83 84
        self._tid = tid
        self._tc = tc  # type: ThreadCoordinator
S
Steven Li 已提交
85
        # self.threadIdent = threading.get_ident()
86 87
        self._thread = threading.Thread(target=runThread, args=(self,))
        self._stepGate = threading.Event()
S
Steven Li 已提交
88

89
        # Let us have a DB connection of our own
S
Shuduo Sang 已提交
90
        if (gConfig.per_thread_db_connection):  # type: ignore
91
            # print("connector_type = {}".format(gConfig.connector_type))
92 93 94 95 96 97 98 99 100 101 102
            if gConfig.connector_type == 'native':
                self._dbConn = DbConn.createNative() 
            elif gConfig.connector_type == 'rest':
                self._dbConn = DbConn.createRest() 
            elif gConfig.connector_type == 'mixed':
                if Dice.throw(2) == 0: # 1/2 chance
                    self._dbConn = DbConn.createNative() 
                else:
                    self._dbConn = DbConn.createRest() 
            else:
                raise RuntimeError("Unexpected connector type: {}".format(gConfig.connector_type))
103

S
Shuduo Sang 已提交
104
        self._dbInUse = False  # if "use db" was executed already
105

106
    def logDebug(self, msg):
S
Steven Li 已提交
107
        logger.debug("    TRD[{}] {}".format(self._tid, msg))
108 109

    def logInfo(self, msg):
S
Steven Li 已提交
110
        logger.info("    TRD[{}] {}".format(self._tid, msg))
111

112 113 114 115
    def dbInUse(self):
        return self._dbInUse

    def useDb(self):
S
Shuduo Sang 已提交
116
        if (not self._dbInUse):
117 118 119
            self.execSql("use db")
        self._dbInUse = True

120
    def getTaskExecutor(self):
S
Shuduo Sang 已提交
121
        return self._tc.getTaskExecutor()
122

S
Steven Li 已提交
123
    def start(self):
124
        self._thread.start()  # AFTER the thread is recorded
S
Steven Li 已提交
125

S
Shuduo Sang 已提交
126
    def run(self):
S
Steven Li 已提交
127
        # initialization after thread starts, in the thread context
128
        # self.isSleeping = False
129 130
        logger.info("Starting to run thread: {}".format(self._tid))

S
Shuduo Sang 已提交
131
        if (gConfig.per_thread_db_connection):  # type: ignore
132
            logger.debug("Worker thread openning database connection")
133
            self._dbConn.open()
S
Steven Li 已提交
134

S
Shuduo Sang 已提交
135 136
        self._doTaskLoop()

137
        # clean up
S
Shuduo Sang 已提交
138
        if (gConfig.per_thread_db_connection):  # type: ignore
139 140 141 142
            if self._dbConn.isOpen: #sometimes it is not open
                self._dbConn.close()
            else:
                logger.warning("Cleaning up worker thread, dbConn already closed")
143

S
Shuduo Sang 已提交
144
    def _doTaskLoop(self):
145 146
        # while self._curStep < self._pool.maxSteps:
        # tc = ThreadCoordinator(None)
S
Shuduo Sang 已提交
147 148
        while True:
            tc = self._tc  # Thread Coordinator, the overall master
149 150 151
            try:
                tc.crossStepBarrier()  # shared barrier first, INCLUDING the last one
            except threading.BrokenBarrierError as err: # main thread timed out
152
                print("_bto", end="")
153 154 155
                logger.debug("[TRD] Worker thread exiting due to main thread barrier time-out")
                break

156
            logger.debug("[TRD] Worker thread [{}] exited barrier...".format(self._tid))
157
            self.crossStepGate()   # then per-thread gate, after being tapped
158
            logger.debug("[TRD] Worker thread [{}] exited step gate...".format(self._tid))
159
            if not self._tc.isRunning():
160
                print("_wts", end="")
161
                logger.debug("[TRD] Thread Coordinator not running any more, worker thread now stopping...")
162 163
                break

164 165
            # Before we fetch the task and run it, let's ensure we properly "use" the database
            try:
166 167 168
                if (gConfig.per_thread_db_connection):  # most likely TRUE
                    if not self._dbConn.isOpen:  # might have been closed during server auto-restart
                        self._dbConn.open()
169 170 171
                self.useDb() # might encounter exceptions. TODO: catch
            except taos.error.ProgrammingError as err:
                errno = Helper.convertErrno(err.errno)
172
                if errno in [0x383, 0x386, 0x00B, 0x014]  : # invalid database, dropping, Unable to establish connection, Database not ready
173 174 175 176 177 178
                    # ignore
                    dummy = 0
                else:
                    print("\nCaught programming error. errno=0x{:X}, msg={} ".format(errno, err.msg))
                    raise

179
            # Fetch a task from the Thread Coordinator
180
            logger.debug( "[TRD] Worker thread [{}] about to fetch task".format(self._tid))
181
            task = tc.fetchTask()
182 183

            # Execute such a task
S
Shuduo Sang 已提交
184 185 186
            logger.debug(
                "[TRD] Worker thread [{}] about to execute task: {}".format(
                    self._tid, task.__class__.__name__))
187
            task.execute(self)
188
            tc.saveExecutedTask(task)
189
            logger.debug("[TRD] Worker thread [{}] finished executing task".format(self._tid))
S
Shuduo Sang 已提交
190 191

            self._dbInUse = False  # there may be changes between steps
192
        # print("_wtd", end=None) # worker thread died
193

S
Shuduo Sang 已提交
194 195
    def verifyThreadSelf(self):  # ensure we are called by this own thread
        if (threading.get_ident() != self._thread.ident):
S
Steven Li 已提交
196 197
            raise RuntimeError("Unexpectly called from other threads")

S
Shuduo Sang 已提交
198 199
    def verifyThreadMain(self):  # ensure we are called by the main thread
        if (threading.get_ident() != threading.main_thread().ident):
S
Steven Li 已提交
200 201 202
            raise RuntimeError("Unexpectly called from other threads")

    def verifyThreadAlive(self):
S
Shuduo Sang 已提交
203
        if (not self._thread.is_alive()):
S
Steven Li 已提交
204 205
            raise RuntimeError("Unexpected dead thread")

206
    # A gate is different from a barrier in that a thread needs to be "tapped"
S
Steven Li 已提交
207 208
    def crossStepGate(self):
        self.verifyThreadAlive()
S
Shuduo Sang 已提交
209 210
        self.verifyThreadSelf()  # only allowed by ourselves

211
        # Wait again at the "gate", waiting to be "tapped"
S
Shuduo Sang 已提交
212 213 214 215
        logger.debug(
            "[TRD] Worker thread {} about to cross the step gate".format(
                self._tid))
        self._stepGate.wait()
216
        self._stepGate.clear()
S
Shuduo Sang 已提交
217

218
        # self._curStep += 1  # off to a new step...
S
Steven Li 已提交
219

S
Shuduo Sang 已提交
220
    def tapStepGate(self):  # give it a tap, release the thread waiting there
221
        # self.verifyThreadAlive()
S
Shuduo Sang 已提交
222 223
        self.verifyThreadMain()  # only allowed for main thread

224 225 226 227 228 229
        if self._thread.is_alive():
            logger.debug("[TRD] Tapping worker thread {}".format(self._tid))
            self._stepGate.set()  # wake up!
            time.sleep(0)  # let the released thread run a bit
        else:
            print("_tad", end="") # Thread already dead
230

S
Shuduo Sang 已提交
231
    def execSql(self, sql):  # TODO: expose DbConn directly
232
        return self.getDbConn().execute(sql)
233

S
Shuduo Sang 已提交
234
    def querySql(self, sql):  # TODO: expose DbConn directly
235
        return self.getDbConn().query(sql)
236 237

    def getQueryResult(self):
238
        return self.getDbConn().getQueryResult()
239

240
    def getDbConn(self):
S
Shuduo Sang 已提交
241 242
        if (gConfig.per_thread_db_connection):
            return self._dbConn
243
        else:
244
            return self._tc.getDbManager().getDbConn()
245

246 247
    # def querySql(self, sql): # not "execute", since we are out side the DB context
    #     if ( gConfig.per_thread_db_connection ):
S
Shuduo Sang 已提交
248
    #         return self._dbConn.query(sql)
249 250
    #     else:
    #         return self._tc.getDbState().getDbConn().query(sql)
251

252
# The coordinator of all worker threads, mostly running in main thread
S
Shuduo Sang 已提交
253 254


255
class ThreadCoordinator:
S
Steven Li 已提交
256
    WORKER_THREAD_TIMEOUT = 60 # one minute
257

258
    def __init__(self, pool: ThreadPool, dbManager):
S
Shuduo Sang 已提交
259
        self._curStep = -1  # first step is 0
260
        self._pool = pool
261
        # self._wd = wd
S
Shuduo Sang 已提交
262
        self._te = None  # prepare for every new step
263
        self._dbManager = dbManager
S
Shuduo Sang 已提交
264 265
        self._executedTasks: List[Task] = []  # in a given step
        self._lock = threading.RLock()  # sync access for a few things
S
Steven Li 已提交
266

S
Shuduo Sang 已提交
267 268
        self._stepBarrier = threading.Barrier(
            self._pool.numThreads + 1)  # one barrier for all threads
269
        self._execStats = ExecutionStats()
270
        self._runStatus = MainExec.STATUS_RUNNING
S
Steven Li 已提交
271

272 273 274
    def getTaskExecutor(self):
        return self._te

S
Shuduo Sang 已提交
275
    def getDbManager(self) -> DbManager:
276
        return self._dbManager
277

278 279
    def crossStepBarrier(self, timeout=None):
        self._stepBarrier.wait(timeout) 
280

281 282 283 284
    def requestToStop(self):
        self._runStatus = MainExec.STATUS_STOPPING
        self._execStats.registerFailure("User Interruption")

285
    def _runShouldEnd(self, transitionFailed, hasAbortedTask, workerTimeout):
286 287 288 289 290 291 292 293 294
        maxSteps = gConfig.max_steps  # type: ignore
        if self._curStep >= (maxSteps - 1): # maxStep==10, last curStep should be 9
            return True
        if self._runStatus != MainExec.STATUS_RUNNING:
            return True
        if transitionFailed:
            return True
        if hasAbortedTask:
            return True
295 296
        if workerTimeout:
            return True
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
        return False

    def _hasAbortedTask(self): # from execution of previous step
        for task in self._executedTasks:
            if task.isAborted():
                # print("Task aborted: {}".format(task))
                # hasAbortedTask = True
                return True
        return False

    def _releaseAllWorkerThreads(self, transitionFailed):
        self._curStep += 1  # we are about to get into next step. TODO: race condition here!
        # Now not all threads had time to go to sleep
        logger.debug(
            "--\r\n\n--> Step {} starts with main thread waking up".format(self._curStep))

        # A new TE for the new step
        self._te = None # set to empty first, to signal worker thread to stop
        if not transitionFailed:  # only if not failed
            self._te = TaskExecutor(self._curStep)

        logger.debug("[TRD] Main thread waking up at step {}, tapping worker threads".format(
                self._curStep))  # Now not all threads had time to go to sleep
        # Worker threads will wake up at this point, and each execute it's own task
        self.tapAllThreads() # release all worker thread from their "gate"

    def _syncAtBarrier(self):
         # Now main thread (that's us) is ready to enter a step
        # let other threads go past the pool barrier, but wait at the
        # thread gate
        logger.debug("[TRD] Main thread about to cross the barrier")
328
        self.crossStepBarrier(timeout=self.WORKER_THREAD_TIMEOUT)
329 330 331 332 333 334 335 336 337 338 339 340 341
        self._stepBarrier.reset()  # Other worker threads should now be at the "gate"
        logger.debug("[TRD] Main thread finished crossing the barrier")

    def _doTransition(self):
        transitionFailed = False
        try:
            sm = self._dbManager.getStateMachine()
            logger.debug("[STT] starting transitions")
            # at end of step, transiton the DB state
            sm.transition(self._executedTasks)
            logger.debug("[STT] transition ended")
            # Due to limitation (or maybe not) of the Python library,
            # we cannot share connections across threads
342 343 344 345 346 347
            # Here we are in main thread, we cannot operate the connections created in workers
            # Moving below to task loop
            # if sm.hasDatabase():
            #     for t in self._pool.threadList:
            #         logger.debug("[DB] use db for all worker threads")
            #         t.useDb()
348 349 350 351 352 353 354 355 356 357 358 359 360
                    # t.execSql("use db") # main thread executing "use
                    # db" on behalf of every worker thread
        except taos.error.ProgrammingError as err:
            if (err.msg == 'network unavailable'):  # broken DB connection
                logger.info("DB connection broken, execution failed")
                traceback.print_stack()
                transitionFailed = True
                self._te = None  # Not running any more
                self._execStats.registerFailure("Broken DB Connection")
                # continue # don't do that, need to tap all threads at
                # end, and maybe signal them to stop
            else:
                raise
361
        return transitionFailed
362 363 364 365 366 367

        self.resetExecutedTasks()  # clear the tasks after we are done
        # Get ready for next step
        logger.debug("<-- Step {} finished, trasition failed = {}".format(self._curStep, transitionFailed))
        return transitionFailed

S
Shuduo Sang 已提交
368
    def run(self):
369
        self._pool.createAndStartThreads(self)
S
Steven Li 已提交
370 371

        # Coordinate all threads step by step
S
Shuduo Sang 已提交
372
        self._curStep = -1  # not started yet
373
        
S
Shuduo Sang 已提交
374
        self._execStats.startExec()  # start the stop watch
375 376
        transitionFailed = False
        hasAbortedTask = False
377 378
        workerTimeout = False
        while not self._runShouldEnd(transitionFailed, hasAbortedTask, workerTimeout):
379
            if not gConfig.debug: # print this only if we are not in debug mode                
S
Shuduo Sang 已提交
380
                print(".", end="", flush=True)
381
                        
382 383 384 385 386 387 388 389 390 391
            try:
                self._syncAtBarrier() # For now just cross the barrier
            except threading.BrokenBarrierError as err:
                logger.info("Main loop aborted, caused by worker thread time-out")
                self._execStats.registerFailure("Aborted due to worker thread timeout")
                print("\n\nWorker Thread time-out detected, important thread info:")
                ts = ThreadStacks()
                ts.print(filterInternal=True)
                workerTimeout = True
                break
392 393

            # At this point, all threads should be pass the overall "barrier" and before the per-thread "gate"
S
Shuduo Sang 已提交
394 395
            # We use this period to do house keeping work, when all worker
            # threads are QUIET.
396 397 398
            hasAbortedTask = self._hasAbortedTask() # from previous step
            if hasAbortedTask: 
                logger.info("Aborted task encountered, exiting test program")
399
                self._execStats.registerFailure("Aborted Task Encountered")
400
                break # do transition only if tasks are error free
S
Shuduo Sang 已提交
401

402
            # Ending previous step
403 404 405 406
            try:
                transitionFailed = self._doTransition() # To start, we end step -1 first
            except taos.error.ProgrammingError as err:
                transitionFailed = True
407
                errno2 = Helper.convertErrno(err.errno)  # correct error scheme
S
Steven Li 已提交
408 409 410
                errMsg = "Transition failed: errno=0x{:X}, msg: {}".format(errno2, err)
                logger.info(errMsg)
                self._execStats.registerFailure(errMsg)
411

412 413
            # Then we move on to the next step
            self._releaseAllWorkerThreads(transitionFailed)                    
414

415 416
        if hasAbortedTask or transitionFailed : # abnormal ending, workers waiting at "gate"
            logger.debug("Abnormal ending of main thraed")
417 418
        elif workerTimeout:
            logger.debug("Abnormal ending of main thread, due to worker timeout")
419 420 421
        else: # regular ending, workers waiting at "barrier"
            logger.debug("Regular ending, main thread waiting for all worker threads to stop...")
            self._syncAtBarrier()
422

423 424 425
        self._te = None  # No more executor, time to end
        logger.debug("Main thread tapping all threads one last time...")
        self.tapAllThreads()  # Let the threads run one last time
426

427
        logger.debug("\r\n\n--> Main thread ready to finish up...")
428
        logger.debug("Main thread joining all threads")
S
Shuduo Sang 已提交
429
        self._pool.joinAll()  # Get all threads to finish
430
        logger.info("\nAll worker threads finished")
431 432
        self._execStats.endExec()

433 434
    def printStats(self):
        self._execStats.printStats()
S
Steven Li 已提交
435

S
Steven Li 已提交
436 437 438 439 440 441
    def isFailed(self):
        return self._execStats.isFailed()

    def getExecStats(self):
        return self._execStats

S
Shuduo Sang 已提交
442
    def tapAllThreads(self):  # in a deterministic manner
S
Steven Li 已提交
443
        wakeSeq = []
S
Shuduo Sang 已提交
444 445
        for i in range(self._pool.numThreads):  # generate a random sequence
            if Dice.throw(2) == 1:
S
Steven Li 已提交
446 447 448
                wakeSeq.append(i)
            else:
                wakeSeq.insert(0, i)
S
Shuduo Sang 已提交
449 450 451
        logger.debug(
            "[TRD] Main thread waking up worker threads: {}".format(
                str(wakeSeq)))
452
        # TODO: set dice seed to a deterministic value
S
Steven Li 已提交
453
        for i in wakeSeq:
S
Shuduo Sang 已提交
454 455 456
            # TODO: maybe a bit too deep?!
            self._pool.threadList[i].tapStepGate()
            time.sleep(0)  # yield
S
Steven Li 已提交
457

458
    def isRunning(self):
S
Shuduo Sang 已提交
459
        return self._te is not None
460

S
Shuduo Sang 已提交
461 462
    def fetchTask(self) -> Task:
        if (not self.isRunning()):  # no task
463
            raise RuntimeError("Cannot fetch task when not running")
464 465
        # return self._wd.pickTask()
        # Alternatively, let's ask the DbState for the appropriate task
466 467 468 469 470 471 472
        # dbState = self.getDbState()
        # tasks = dbState.getTasksAtState() # TODO: create every time?
        # nTasks = len(tasks)
        # i = Dice.throw(nTasks)
        # logger.debug(" (dice:{}/{}) ".format(i, nTasks))
        # # return copy.copy(tasks[i]) # Needs a fresh copy, to save execution results, etc.
        # return tasks[i].clone() # TODO: still necessary?
S
Shuduo Sang 已提交
473 474 475 476 477
        # pick a task type for current state
        taskType = self.getDbManager().getStateMachine().pickTaskType()
        return taskType(
            self.getDbManager(),
            self._execStats)  # create a task from it
478 479

    def resetExecutedTasks(self):
S
Shuduo Sang 已提交
480
        self._executedTasks = []  # should be under single thread
481 482 483 484

    def saveExecutedTask(self, task):
        with self._lock:
            self._executedTasks.append(task)
485 486

# We define a class to run a number of threads in locking steps.
S
Shuduo Sang 已提交
487

488 489 490 491
class Helper:
    @classmethod
    def convertErrno(cls, errno):
        return errno if (errno > 0) else 0x80000000 + errno
S
Shuduo Sang 已提交
492

493
class ThreadPool:
494
    def __init__(self, numThreads, maxSteps):
495 496 497 498
        self.numThreads = numThreads
        self.maxSteps = maxSteps
        # Internal class variables
        self.curStep = 0
S
Shuduo Sang 已提交
499 500
        self.threadList = []  # type: List[WorkerThread]

501
    # starting to run all the threads, in locking steps
502
    def createAndStartThreads(self, tc: ThreadCoordinator):
S
Shuduo Sang 已提交
503 504
        for tid in range(0, self.numThreads):  # Create the threads
            workerThread = WorkerThread(self, tid, tc)
505
            self.threadList.append(workerThread)
S
Shuduo Sang 已提交
506
            workerThread.start()  # start, but should block immediately before step 0
507 508 509 510 511 512

    def joinAll(self):
        for workerThread in self.threadList:
            logger.debug("Joining thread...")
            workerThread._thread.join()

513 514
# A queue of continguous POSITIVE integers, used by DbManager to generate continuous numbers
# for new table names
S
Shuduo Sang 已提交
515 516


S
Steven Li 已提交
517 518
class LinearQueue():
    def __init__(self):
519
        self.firstIndex = 1  # 1st ever element
S
Steven Li 已提交
520
        self.lastIndex = 0
S
Shuduo Sang 已提交
521 522
        self._lock = threading.RLock()  # our functions may call each other
        self.inUse = set()  # the indexes that are in use right now
S
Steven Li 已提交
523

524
    def toText(self):
S
Shuduo Sang 已提交
525 526
        return "[{}..{}], in use: {}".format(
            self.firstIndex, self.lastIndex, self.inUse)
527 528

    # Push (add new element, largest) to the tail, and mark it in use
S
Shuduo Sang 已提交
529
    def push(self):
530
        with self._lock:
S
Shuduo Sang 已提交
531 532
            # if ( self.isEmpty() ):
            #     self.lastIndex = self.firstIndex
533
            #     return self.firstIndex
534 535
            # Otherwise we have something
            self.lastIndex += 1
536 537
            self.allocate(self.lastIndex)
            # self.inUse.add(self.lastIndex) # mark it in use immediately
538
            return self.lastIndex
S
Steven Li 已提交
539 540

    def pop(self):
541
        with self._lock:
S
Shuduo Sang 已提交
542 543 544 545
            if (self.isEmpty()):
                # raise RuntimeError("Cannot pop an empty queue")
                return False  # TODO: None?

546
            index = self.firstIndex
S
Shuduo Sang 已提交
547
            if (index in self.inUse):
548 549
                return False

550 551 552 553 554 555 556
            self.firstIndex += 1
            return index

    def isEmpty(self):
        return self.firstIndex > self.lastIndex

    def popIfNotEmpty(self):
557
        with self._lock:
558 559 560 561
            if (self.isEmpty()):
                return 0
            return self.pop()

S
Steven Li 已提交
562
    def allocate(self, i):
563
        with self._lock:
564
            # logger.debug("LQ allocating item {}".format(i))
S
Shuduo Sang 已提交
565 566 567
            if (i in self.inUse):
                raise RuntimeError(
                    "Cannot re-use same index in queue: {}".format(i))
568 569
            self.inUse.add(i)

S
Steven Li 已提交
570
    def release(self, i):
571
        with self._lock:
572
            # logger.debug("LQ releasing item {}".format(i))
S
Shuduo Sang 已提交
573
            self.inUse.remove(i)  # KeyError possible, TODO: why?
574 575 576 577

    def size(self):
        return self.lastIndex + 1 - self.firstIndex

S
Steven Li 已提交
578
    def pickAndAllocate(self):
S
Shuduo Sang 已提交
579
        if (self.isEmpty()):
580 581
            return None
        with self._lock:
S
Shuduo Sang 已提交
582
            cnt = 0  # counting the interations
583 584
            while True:
                cnt += 1
S
Shuduo Sang 已提交
585
                if (cnt > self.size() * 10):  # 10x iteration already
586 587
                    # raise RuntimeError("Failed to allocate LinearQueue element")
                    return None
S
Shuduo Sang 已提交
588 589
                ret = Dice.throwRange(self.firstIndex, self.lastIndex + 1)
                if (ret not in self.inUse):
590 591 592
                    self.allocate(ret)
                    return ret

S
Shuduo Sang 已提交
593

594
class DbConn:
595
    TYPE_NATIVE = "native-c"
596
    TYPE_REST =   "rest-api"
597 598 599 600 601 602 603 604 605
    TYPE_INVALID = "invalid"

    @classmethod
    def create(cls, connType):
        if connType == cls.TYPE_NATIVE:
            return DbConnNative()
        elif connType == cls.TYPE_REST:
            return DbConnRest()
        else:
S
Shuduo Sang 已提交
606 607
            raise RuntimeError(
                "Unexpected connection type: {}".format(connType))
608 609 610 611 612 613 614 615 616

    @classmethod
    def createNative(cls):
        return cls.create(cls.TYPE_NATIVE)

    @classmethod
    def createRest(cls):
        return cls.create(cls.TYPE_REST)

617 618
    def __init__(self):
        self.isOpen = False
619
        self._type = self.TYPE_INVALID
620 621 622 623
        self._lastSql = None

    def getLastSql(self):
        return self._lastSql
624 625

    def open(self):
S
Shuduo Sang 已提交
626
        if (self.isOpen):
627 628
            raise RuntimeError("Cannot re-open an existing DB connection")

629 630
        # below implemented by child classes
        self.openByType()
631

632
        logger.debug("[DB] data connection opened, type = {}".format(self._type))
633 634
        self.isOpen = True

S
Shuduo Sang 已提交
635 636
    def resetDb(self):  # reset the whole database, etc.
        if (not self.isOpen):
637
            raise RuntimeError("Cannot reset database until connection is open")
638 639
        # self._tdSql.prepare() # Recreate database, etc.

640
        self.execute('drop database if exists db')
641 642
        logger.debug("Resetting DB, dropped database")
        # self._cursor.execute('create database db')
643
        # self._cursor.execute('use db')
644 645
        # tdSql.execute('show databases')

S
Shuduo Sang 已提交
646
    def queryScalar(self, sql) -> int:
647 648
        return self._queryAny(sql)

S
Shuduo Sang 已提交
649
    def queryString(self, sql) -> str:
650 651
        return self._queryAny(sql)

S
Shuduo Sang 已提交
652 653
    def _queryAny(self, sql):  # actual query result as an int
        if (not self.isOpen):
654
            raise RuntimeError("Cannot query database until connection is open")
655
        nRows = self.query(sql)
S
Shuduo Sang 已提交
656
        if nRows != 1:
657
            raise RuntimeError("Unexpected result for query: {}, rows = {}".format(sql, nRows))
658
        if self.getResultRows() != 1 or self.getResultCols() != 1:
659
            raise RuntimeError("Unexpected result set for query: {}".format(sql))
660 661
        return self.getQueryResult()[0][0]

662 663 664 665
    def use(self, dbName):
        self.execute("use {}".format(dbName))

    def hasDatabases(self):
666
        return self.query("show databases") > 1 # We now have a "log" database by default
667 668 669 670

    def hasTables(self):
        return self.query("show tables") > 0

671 672
    def execute(self, sql):
        raise RuntimeError("Unexpected execution, should be overriden")
S
Shuduo Sang 已提交
673

674 675 676
    def query(self, sql) -> int: # return num rows returned
        raise RuntimeError("Unexpected execution, should be overriden")

677 678
    def openByType(self):
        raise RuntimeError("Unexpected execution, should be overriden")
S
Shuduo Sang 已提交
679

680 681
    def getQueryResult(self):
        raise RuntimeError("Unexpected execution, should be overriden")
S
Shuduo Sang 已提交
682

683 684
    def getResultRows(self):
        raise RuntimeError("Unexpected execution, should be overriden")
S
Shuduo Sang 已提交
685

686 687 688 689
    def getResultCols(self):
        raise RuntimeError("Unexpected execution, should be overriden")

# Sample: curl -u root:taosdata -d "show databases" localhost:6020/rest/sql
S
Shuduo Sang 已提交
690 691


692 693 694 695
class DbConnRest(DbConn):
    def __init__(self):
        super().__init__()
        self._type = self.TYPE_REST
S
Shuduo Sang 已提交
696
        self._url = "http://localhost:6020/rest/sql"  # fixed for now
697 698
        self._result = None

S
Shuduo Sang 已提交
699 700 701
    def openByType(self):  # Open connection
        pass  # do nothing, always open

702
    def close(self):
S
Shuduo Sang 已提交
703
        if (not self.isOpen):
704
            raise RuntimeError("Cannot clean up database until connection is open")
705 706 707 708 709
        # Do nothing for REST
        logger.debug("[DB] REST Database connection closed")
        self.isOpen = False

    def _doSql(self, sql):
710
        self._lastSql = sql # remember this, last SQL attempted
711 712 713
        try:
            r = requests.post(self._url, 
                data = sql,
714
                auth = HTTPBasicAuth('root', 'taosdata'))         
715 716 717
        except:
            print("REST API Failure (TODO: more info here)")
            raise
718 719
        rj = r.json()
        # Sanity check for the "Json Result"
S
Shuduo Sang 已提交
720
        if ('status' not in rj):
721 722
            raise RuntimeError("No status in REST response")

S
Shuduo Sang 已提交
723 724 725 726
        if rj['status'] == 'error':  # clearly reported error
            if ('code' not in rj):  # error without code
                raise RuntimeError("REST error return without code")
            errno = rj['code']  # May need to massage this in the future
727
            # print("Raising programming error with REST return: {}".format(rj))
S
Shuduo Sang 已提交
728 729
            raise taos.error.ProgrammingError(
                rj['desc'], errno)  # todo: check existance of 'desc'
730

S
Shuduo Sang 已提交
731 732 733 734
        if rj['status'] != 'succ':  # better be this
            raise RuntimeError(
                "Unexpected REST return status: {}".format(
                    rj['status']))
735 736

        nRows = rj['rows'] if ('rows' in rj) else 0
S
Shuduo Sang 已提交
737
        self._result = rj
738 739
        return nRows

S
Shuduo Sang 已提交
740 741 742 743
    def execute(self, sql):
        if (not self.isOpen):
            raise RuntimeError(
                "Cannot execute database commands until connection is open")
744 745
        logger.debug("[SQL-REST] Executing SQL: {}".format(sql))
        nRows = self._doSql(sql)
S
Shuduo Sang 已提交
746 747
        logger.debug(
            "[SQL-REST] Execution Result, nRows = {}, SQL = {}".format(nRows, sql))
748 749
        return nRows

S
Shuduo Sang 已提交
750
    def query(self, sql):  # return rows affected
751 752 753 754 755 756 757 758 759 760 761 762 763
        return self.execute(sql)

    def getQueryResult(self):
        return self._result['data']

    def getResultRows(self):
        print(self._result)
        raise RuntimeError("TBD")
        # return self._tdSql.queryRows

    def getResultCols(self):
        print(self._result)
        raise RuntimeError("TBD")
S
Shuduo Sang 已提交
764

765
    # Duplicate code from TDMySQL, TODO: merge all this into DbConnNative
766 767


768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
class MyTDSql:
    def __init__(self):
        self.queryRows = 0
        self.queryCols = 0
        self.affectedRows = 0

    def init(self, cursor, log=True):
        self.cursor = cursor
        # if (log):
        #     caller = inspect.getframeinfo(inspect.stack()[1][0])
        #     self.cursor.log(caller.filename + ".sql")

    def close(self):
        self.cursor.close()

    def query(self, sql):
        self.sql = sql
        try:
            self.cursor.execute(sql)
            self.queryResult = self.cursor.fetchall()
            self.queryRows = len(self.queryResult)
            self.queryCols = len(self.cursor.description)
        except Exception as e:
            # caller = inspect.getframeinfo(inspect.stack()[1][0])
            # args = (caller.filename, caller.lineno, sql, repr(e))
            # tdLog.exit("%s(%d) failed: sql:%s, %s" % args)
            raise
        return self.queryRows
796

797 798 799 800 801 802 803 804 805 806 807
    def execute(self, sql):
        self.sql = sql
        try:
            self.affectedRows = self.cursor.execute(sql)
        except Exception as e:
            # caller = inspect.getframeinfo(inspect.stack()[1][0])
            # args = (caller.filename, caller.lineno, sql, repr(e))
            # tdLog.exit("%s(%d) failed: sql:%s, %s" % args)
            raise
        return self.affectedRows

S
Shuduo Sang 已提交
808

809
class DbConnNative(DbConn):
810 811 812 813
    # Class variables
    _lock = threading.Lock()
    _connInfoDisplayed = False

814 815
    def __init__(self):
        super().__init__()
816
        self._type = self.TYPE_NATIVE
S
Shuduo Sang 已提交
817
        self._conn = None
818
        self._cursor = None        
S
Shuduo Sang 已提交
819

820 821 822 823 824 825 826
    def getBuildPath(self):
        selfPath = os.path.dirname(os.path.realpath(__file__))
        if ("community" in selfPath):
            projPath = selfPath[:selfPath.find("communit")]
        else:
            projPath = selfPath[:selfPath.find("tests")]

827
        buildPath = None
828 829 830 831
        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 已提交
832
                    buildPath = root[:len(root) - len("/build/bin")]
833
                    break
834
        if buildPath == None:
835
            raise RuntimeError("Failed to determine buildPath, selfPath={}, projPath={}".format(selfPath, projPath))
836 837
        return buildPath

838
    
S
Shuduo Sang 已提交
839
    def openByType(self):  # Open connection
840
        cfgPath = self.getBuildPath() + "/test/cfg"
841
        hostAddr = "127.0.0.1"
842

843 844 845 846 847
        with self._lock: # force single threading for opening DB connections
            if not self._connInfoDisplayed:
                self.__class__._connInfoDisplayed = True # updating CLASS variable
                logger.info("Initiating TAOS native connection to {}, using config at {}".format(hostAddr, cfgPath))
            
848
            self._conn = taos.connect(host=hostAddr, config=cfgPath)  # TODO: make configurable
849 850
            self._cursor = self._conn.cursor()
        
851
        self._cursor.execute('reset query cache')
S
Shuduo Sang 已提交
852
        # self._cursor.execute('use db') # do this at the beginning of every
853 854

        # Open connection
855
        self._tdSql = MyTDSql()
856
        self._tdSql.init(self._cursor)
S
Shuduo Sang 已提交
857

858
    def close(self):
S
Shuduo Sang 已提交
859
        if (not self.isOpen):
860
            raise RuntimeError("Cannot clean up database until connection is open")
861
        self._tdSql.close()
862
        logger.debug("[DB] Database connection closed")
863
        self.isOpen = False
S
Steven Li 已提交
864

S
Shuduo Sang 已提交
865 866
    def execute(self, sql):
        if (not self.isOpen):
867
            raise RuntimeError("Cannot execute database commands until connection is open")
868
        logger.debug("[SQL] Executing SQL: {}".format(sql))
869
        self._lastSql = sql
870
        nRows = self._tdSql.execute(sql)
S
Shuduo Sang 已提交
871 872 873
        logger.debug(
            "[SQL] Execution Result, nRows = {}, SQL = {}".format(
                nRows, sql))
874
        return nRows
S
Steven Li 已提交
875

S
Shuduo Sang 已提交
876 877 878 879
    def query(self, sql):  # return rows affected
        if (not self.isOpen):
            raise RuntimeError(
                "Cannot query database until connection is open")
880
        logger.debug("[SQL] Executing SQL: {}".format(sql))
881
        self._lastSql = sql
882
        nRows = self._tdSql.query(sql)
S
Shuduo Sang 已提交
883 884 885
        logger.debug(
            "[SQL] Query Result, nRows = {}, SQL = {}".format(
                nRows, sql))
886
        return nRows
887
        # results are in: return self._tdSql.queryResult
888

889 890 891
    def getQueryResult(self):
        return self._tdSql.queryResult

892 893
    def getResultRows(self):
        return self._tdSql.queryRows
894

895 896
    def getResultCols(self):
        return self._tdSql.queryCols
897

S
Shuduo Sang 已提交
898

899
class AnyState:
S
Shuduo Sang 已提交
900 901 902
    STATE_INVALID = -1
    STATE_EMPTY = 0  # nothing there, no even a DB
    STATE_DB_ONLY = 1  # we have a DB, but nothing else
903
    STATE_TABLE_ONLY = 2  # we have a table, but totally empty
S
Shuduo Sang 已提交
904
    STATE_HAS_DATA = 3  # we have some data in the table
905 906 907 908 909
    _stateNames = ["Invalid", "Empty", "DB_Only", "Table_Only", "Has_Data"]

    STATE_VAL_IDX = 0
    CAN_CREATE_DB = 1
    CAN_DROP_DB = 2
910 911
    CAN_CREATE_FIXED_SUPER_TABLE = 3
    CAN_DROP_FIXED_SUPER_TABLE = 4
912 913 914 915 916 917 918
    CAN_ADD_DATA = 5
    CAN_READ_DATA = 6

    def __init__(self):
        self._info = self.getInfo()

    def __str__(self):
S
Shuduo Sang 已提交
919 920
        # -1 hack to accomodate the STATE_INVALID case
        return self._stateNames[self._info[self.STATE_VAL_IDX] + 1]
921 922 923 924

    def getInfo(self):
        raise RuntimeError("Must be overriden by child classes")

S
Steven Li 已提交
925 926 927 928 929 930
    def equals(self, other):
        if isinstance(other, int):
            return self.getValIndex() == other
        elif isinstance(other, AnyState):
            return self.getValIndex() == other.getValIndex()
        else:
S
Shuduo Sang 已提交
931 932 933
            raise RuntimeError(
                "Unexpected comparison, type = {}".format(
                    type(other)))
S
Steven Li 已提交
934

935 936 937
    def verifyTasksToState(self, tasks, newState):
        raise RuntimeError("Must be overriden by child classes")

S
Steven Li 已提交
938 939 940
    def getValIndex(self):
        return self._info[self.STATE_VAL_IDX]

941 942
    def getValue(self):
        return self._info[self.STATE_VAL_IDX]
S
Shuduo Sang 已提交
943

944 945
    def canCreateDb(self):
        return self._info[self.CAN_CREATE_DB]
S
Shuduo Sang 已提交
946

947 948
    def canDropDb(self):
        return self._info[self.CAN_DROP_DB]
S
Shuduo Sang 已提交
949

950 951
    def canCreateFixedSuperTable(self):
        return self._info[self.CAN_CREATE_FIXED_SUPER_TABLE]
S
Shuduo Sang 已提交
952

953 954
    def canDropFixedSuperTable(self):
        return self._info[self.CAN_DROP_FIXED_SUPER_TABLE]
S
Shuduo Sang 已提交
955

956 957
    def canAddData(self):
        return self._info[self.CAN_ADD_DATA]
S
Shuduo Sang 已提交
958

959 960 961 962 963
    def canReadData(self):
        return self._info[self.CAN_READ_DATA]

    def assertAtMostOneSuccess(self, tasks, cls):
        sCnt = 0
S
Shuduo Sang 已提交
964
        for task in tasks:
965 966 967
            if not isinstance(task, cls):
                continue
            if task.isSuccess():
S
Steven Li 已提交
968
                # task.logDebug("Task success found")
969
                sCnt += 1
S
Shuduo Sang 已提交
970 971 972
                if (sCnt >= 2):
                    raise RuntimeError(
                        "Unexpected more than 1 success with task: {}".format(cls))
973 974 975 976

    def assertIfExistThenSuccess(self, tasks, cls):
        sCnt = 0
        exists = False
S
Shuduo Sang 已提交
977
        for task in tasks:
978 979
            if not isinstance(task, cls):
                continue
S
Shuduo Sang 已提交
980
            exists = True  # we have a valid instance
981 982
            if task.isSuccess():
                sCnt += 1
S
Shuduo Sang 已提交
983 984 985
        if (exists and sCnt <= 0):
            raise RuntimeError(
                "Unexpected zero success for task: {}".format(cls))
986 987

    def assertNoTask(self, tasks, cls):
S
Shuduo Sang 已提交
988
        for task in tasks:
989
            if isinstance(task, cls):
S
Shuduo Sang 已提交
990 991
                raise CrashGenError(
                    "This task: {}, is not expected to be present, given the success/failure of others".format(cls.__name__))
992 993

    def assertNoSuccess(self, tasks, cls):
S
Shuduo Sang 已提交
994
        for task in tasks:
995 996
            if isinstance(task, cls):
                if task.isSuccess():
S
Shuduo Sang 已提交
997 998
                    raise RuntimeError(
                        "Unexpected successful task: {}".format(cls))
999 1000

    def hasSuccess(self, tasks, cls):
S
Shuduo Sang 已提交
1001
        for task in tasks:
1002 1003 1004 1005 1006 1007
            if not isinstance(task, cls):
                continue
            if task.isSuccess():
                return True
        return False

S
Steven Li 已提交
1008
    def hasTask(self, tasks, cls):
S
Shuduo Sang 已提交
1009
        for task in tasks:
S
Steven Li 已提交
1010 1011 1012 1013
            if isinstance(task, cls):
                return True
        return False

S
Shuduo Sang 已提交
1014

1015 1016 1017 1018
class StateInvalid(AnyState):
    def getInfo(self):
        return [
            self.STATE_INVALID,
S
Shuduo Sang 已提交
1019 1020 1021
            False, False,  # can create/drop Db
            False, False,  # can create/drop fixed table
            False, False,  # can insert/read data with fixed table
1022 1023 1024 1025
        ]

    # def verifyTasksToState(self, tasks, newState):

S
Shuduo Sang 已提交
1026

1027 1028 1029 1030
class StateEmpty(AnyState):
    def getInfo(self):
        return [
            self.STATE_EMPTY,
S
Shuduo Sang 已提交
1031 1032 1033
            True, False,  # can create/drop Db
            False, False,  # can create/drop fixed table
            False, False,  # can insert/read data with fixed table
1034 1035
        ]

S
Shuduo Sang 已提交
1036 1037
    def verifyTasksToState(self, tasks, newState):
        if (self.hasSuccess(tasks, TaskCreateDb)
1038
                ):  # at EMPTY, if there's succes in creating DB
S
Shuduo Sang 已提交
1039 1040 1041 1042
            if (not self.hasTask(tasks, TaskDropDb)):  # and no drop_db tasks
                # we must have at most one. TODO: compare numbers
                self.assertAtMostOneSuccess(tasks, TaskCreateDb)

1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053

class StateDbOnly(AnyState):
    def getInfo(self):
        return [
            self.STATE_DB_ONLY,
            False, True,
            True, False,
            False, False,
        ]

    def verifyTasksToState(self, tasks, newState):
S
Shuduo Sang 已提交
1054 1055 1056
        if (not self.hasTask(tasks, TaskCreateDb)):
            # only if we don't create any more
            self.assertAtMostOneSuccess(tasks, TaskDropDb)
1057 1058 1059 1060 1061

        # TODO: restore the below, the problem exists, although unlikely in real-world
        # if (gSvcMgr!=None) and gSvcMgr.isRestarting():     
        # if (gSvcMgr == None) or (not gSvcMgr.isRestarting()) : 
        #     self.assertIfExistThenSuccess(tasks, TaskDropDb)       
1062

S
Shuduo Sang 已提交
1063

1064
class StateSuperTableOnly(AnyState):
1065 1066 1067 1068 1069 1070 1071 1072 1073
    def getInfo(self):
        return [
            self.STATE_TABLE_ONLY,
            False, True,
            False, True,
            True, True,
        ]

    def verifyTasksToState(self, tasks, newState):
S
Shuduo Sang 已提交
1074
        if (self.hasSuccess(tasks, TaskDropSuperTable)
1075
                ):  # we are able to drop the table
1076
            #self.assertAtMostOneSuccess(tasks, TaskDropSuperTable)
S
Shuduo Sang 已提交
1077 1078
            # we must have had recreted it
            self.hasSuccess(tasks, TaskCreateSuperTable)
1079

1080
            # self._state = self.STATE_DB_ONLY
S
Steven Li 已提交
1081 1082
        # elif ( self.hasSuccess(tasks, AddFixedDataTask) ): # no success dropping the table, but added data
        #     self.assertNoTask(tasks, DropFixedTableTask) # not true in massively parrallel cases
1083
            # self._state = self.STATE_HAS_DATA
S
Steven Li 已提交
1084 1085 1086
        # elif ( self.hasSuccess(tasks, ReadFixedDataTask) ): # no success in prev cases, but was able to read data
            # self.assertNoTask(tasks, DropFixedTableTask)
            # self.assertNoTask(tasks, AddFixedDataTask)
1087
            # self._state = self.STATE_TABLE_ONLY # no change
S
Steven Li 已提交
1088 1089 1090
        # else: # did not drop table, did not insert data, did not read successfully, that is impossible
        #     raise RuntimeError("Unexpected no-success scenarios")
        # TODO: need to revamp!!
1091

S
Shuduo Sang 已提交
1092

1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
class StateHasData(AnyState):
    def getInfo(self):
        return [
            self.STATE_HAS_DATA,
            False, True,
            False, True,
            True, True,
        ]

    def verifyTasksToState(self, tasks, newState):
S
Shuduo Sang 已提交
1103
        if (newState.equals(AnyState.STATE_EMPTY)):
1104
            self.hasSuccess(tasks, TaskDropDb)
S
Shuduo Sang 已提交
1105 1106 1107 1108
            if (not self.hasTask(tasks, TaskCreateDb)):
                self.assertAtMostOneSuccess(tasks, TaskDropDb)  # TODO: dicy
        elif (newState.equals(AnyState.STATE_DB_ONLY)):  # in DB only
            if (not self.hasTask(tasks, TaskCreateDb)
1109
                ):  # without a create_db task
S
Shuduo Sang 已提交
1110 1111
                # we must have drop_db task
                self.assertNoTask(tasks, TaskDropDb)
1112
            self.hasSuccess(tasks, TaskDropSuperTable)
1113
            # self.assertAtMostOneSuccess(tasks, DropFixedSuperTableTask) # TODO: dicy
1114 1115 1116 1117
        # elif ( newState.equals(AnyState.STATE_TABLE_ONLY) ): # data deleted
            # self.assertNoTask(tasks, TaskDropDb)
            # self.assertNoTask(tasks, TaskDropSuperTable)
            # self.assertNoTask(tasks, TaskAddData)
S
Steven Li 已提交
1118
            # self.hasSuccess(tasks, DeleteDataTasks)
S
Shuduo Sang 已提交
1119 1120
        else:  # should be STATE_HAS_DATA
            if (not self.hasTask(tasks, TaskCreateDb)
1121
                ):  # only if we didn't create one
S
Shuduo Sang 已提交
1122 1123 1124
                # we shouldn't have dropped it
                self.assertNoTask(tasks, TaskDropDb)
            if (not self.hasTask(tasks, TaskCreateSuperTable)
1125
                    ):  # if we didn't create the table
S
Shuduo Sang 已提交
1126 1127
                # we should not have a task that drops it
                self.assertNoTask(tasks, TaskDropSuperTable)
1128
            # self.assertIfExistThenSuccess(tasks, ReadFixedDataTask)
S
Steven Li 已提交
1129

S
Shuduo Sang 已提交
1130

1131
class StateMechine:
1132 1133
    def __init__(self, dbConn):
        self._dbConn = dbConn
S
Shuduo Sang 已提交
1134 1135 1136
        self._curState = self._findCurrentState()  # starting state
        # transitition target probabilities, indexed with value of STATE_EMPTY,
        # STATE_DB_ONLY, etc.
1137
        self._stateWeights = [1, 2, 10, 40]
S
Shuduo Sang 已提交
1138

1139 1140 1141
    def getCurrentState(self):
        return self._curState

1142 1143 1144
    def hasDatabase(self):
        return self._curState.canDropDb()  # ha, can drop DB means it has one

1145
    # May be slow, use cautionsly...
S
Shuduo Sang 已提交
1146
    def getTaskTypes(self):  # those that can run (directly/indirectly) from the current state
1147 1148 1149 1150 1151 1152
        def typesToStrings(types):
            ss = []
            for t in types:
                ss.append(t.__name__)
            return ss

S
Shuduo Sang 已提交
1153
        allTaskClasses = StateTransitionTask.__subclasses__()  # all state transition tasks
1154 1155
        firstTaskTypes = []
        for tc in allTaskClasses:
S
Shuduo Sang 已提交
1156
            # t = tc(self) # create task object
1157 1158
            if tc.canBeginFrom(self._curState):
                firstTaskTypes.append(tc)
S
Shuduo Sang 已提交
1159 1160 1161 1162 1163 1164 1165 1166
        # now we have all the tasks that can begin directly from the current
        # state, let's figure out the INDIRECT ones
        taskTypes = firstTaskTypes.copy()  # have to have these
        for task1 in firstTaskTypes:  # each task type gathered so far
            endState = task1.getEndState()  # figure the end state
            if endState is None:  # does not change end state
                continue  # no use, do nothing
            for tc in allTaskClasses:  # what task can further begin from there?
1167
                if tc.canBeginFrom(endState) and (tc not in firstTaskTypes):
S
Shuduo Sang 已提交
1168
                    taskTypes.append(tc)  # gather it
1169 1170

        if len(taskTypes) <= 0:
S
Shuduo Sang 已提交
1171 1172 1173 1174 1175 1176 1177
            raise RuntimeError(
                "No suitable task types found for state: {}".format(
                    self._curState))
        logger.debug(
            "[OPS] Tasks found for state {}: {}".format(
                self._curState,
                typesToStrings(taskTypes)))
1178 1179 1180 1181
        return taskTypes

    def _findCurrentState(self):
        dbc = self._dbConn
S
Shuduo Sang 已提交
1182
        ts = time.time()  # we use this to debug how fast/slow it is to do the various queries to find the current DB state
1183 1184
        if not dbc.hasDatabases():  # no database?!
            logger.debug( "[STT] empty database found, between {} and {}".format(ts, time.time()))
1185
            return StateEmpty()
S
Shuduo Sang 已提交
1186 1187
        # did not do this when openning connection, and this is NOT the worker
        # thread, which does this on their own
1188 1189 1190
        dbc.use("db")
        if not dbc.hasTables():  # no tables
            logger.debug("[STT] DB_ONLY found, between {} and {}".format(ts, time.time()))
1191
            return StateDbOnly()
1192 1193 1194 1195

        sTable = DbManager.getFixedSuperTable()
        if sTable.hasRegTables(dbc):  # no regular tables
            logger.debug("[STT] SUPER_TABLE_ONLY found, between {} and {}".format(ts, time.time()))
1196
            return StateSuperTableOnly()
S
Shuduo Sang 已提交
1197
        else:  # has actual tables
1198
            logger.debug("[STT] HAS_DATA found, between {} and {}".format(ts, time.time()))
1199 1200 1201
            return StateHasData()

    def transition(self, tasks):
S
Shuduo Sang 已提交
1202
        if (len(tasks) == 0):  # before 1st step, or otherwise empty
1203
            logger.debug("[STT] Starting State: {}".format(self._curState))
S
Shuduo Sang 已提交
1204
            return  # do nothing
1205

S
Shuduo Sang 已提交
1206 1207
        # this should show up in the server log, separating steps
        self._dbConn.execute("show dnodes")
1208 1209 1210 1211

        # Generic Checks, first based on the start state
        if self._curState.canCreateDb():
            self._curState.assertIfExistThenSuccess(tasks, TaskCreateDb)
S
Shuduo Sang 已提交
1212 1213
            # self.assertAtMostOneSuccess(tasks, CreateDbTask) # not really, in
            # case of multiple creation and drops
1214 1215

        if self._curState.canDropDb():
1216
            if gSvcMgr == None: # only if we are running as client-only
S
Steven Li 已提交
1217
                self._curState.assertIfExistThenSuccess(tasks, TaskDropDb)
S
Shuduo Sang 已提交
1218 1219
            # self.assertAtMostOneSuccess(tasks, DropDbTask) # not really in
            # case of drop-create-drop
1220 1221 1222

        # if self._state.canCreateFixedTable():
            # self.assertIfExistThenSuccess(tasks, CreateFixedTableTask) # Not true, DB may be dropped
S
Shuduo Sang 已提交
1223 1224
            # self.assertAtMostOneSuccess(tasks, CreateFixedTableTask) # not
            # really, in case of create-drop-create
1225 1226 1227

        # if self._state.canDropFixedTable():
            # self.assertIfExistThenSuccess(tasks, DropFixedTableTask) # Not True, the whole DB may be dropped
S
Shuduo Sang 已提交
1228 1229
            # self.assertAtMostOneSuccess(tasks, DropFixedTableTask) # not
            # really in case of drop-create-drop
1230 1231

        # if self._state.canAddData():
S
Shuduo Sang 已提交
1232 1233
        # self.assertIfExistThenSuccess(tasks, AddFixedDataTask)  # not true
        # actually
1234 1235 1236 1237 1238 1239

        # if self._state.canReadData():
            # Nothing for sure

        newState = self._findCurrentState()
        logger.debug("[STT] New DB state determined: {}".format(newState))
S
Shuduo Sang 已提交
1240 1241
        # can old state move to new state through the tasks?
        self._curState.verifyTasksToState(tasks, newState)
1242 1243 1244
        self._curState = newState

    def pickTaskType(self):
S
Shuduo Sang 已提交
1245 1246
        # all the task types we can choose from at curent state
        taskTypes = self.getTaskTypes()
1247 1248 1249
        weights = []
        for tt in taskTypes:
            endState = tt.getEndState()
S
Shuduo Sang 已提交
1250 1251 1252
            if endState is not None:
                # TODO: change to a method
                weights.append(self._stateWeights[endState.getValIndex()])
1253
            else:
S
Shuduo Sang 已提交
1254 1255
                # read data task, default to 10: TODO: change to a constant
                weights.append(10)
1256
        i = self._weighted_choice_sub(weights)
S
Shuduo Sang 已提交
1257
        # logger.debug(" (weighted random:{}/{}) ".format(i, len(taskTypes)))
1258 1259
        return taskTypes[i]

S
Shuduo Sang 已提交
1260 1261 1262 1263 1264
    # ref:
    # https://eli.thegreenplace.net/2010/01/22/weighted-random-generation-in-python/
    def _weighted_choice_sub(self, weights):
        # TODO: use our dice to ensure it being determinstic?
        rnd = random.random() * sum(weights)
1265 1266 1267 1268
        for i, w in enumerate(weights):
            rnd -= w
            if rnd < 0:
                return i
1269

1270
# Manager of the Database Data/Connection
S
Shuduo Sang 已提交
1271 1272 1273 1274


class DbManager():
    def __init__(self, resetDb=True):
S
Steven Li 已提交
1275
        self.tableNumQueue = LinearQueue()
S
Shuduo Sang 已提交
1276 1277 1278
        # datetime.datetime(2019, 1, 1) # initial date time tick
        self._lastTick = self.setupLastTick()
        self._lastInt = 0  # next one is initial integer
1279
        self._lock = threading.RLock()
S
Shuduo Sang 已提交
1280

1281
        # self.openDbServerConnection()
S
Shuduo Sang 已提交
1282 1283
        self._dbConn = DbConn.createNative() if (
            gConfig.connector_type == 'native') else DbConn.createRest()
1284
        try:
S
Shuduo Sang 已提交
1285
            self._dbConn.open()  # may throw taos.error.ProgrammingError: disconnected
1286 1287
        except taos.error.ProgrammingError as err:
            # print("Error type: {}, msg: {}, value: {}".format(type(err), err.msg, err))
S
Shuduo Sang 已提交
1288 1289 1290
            if (err.msg == 'client disconnected'):  # cannot open DB connection
                print(
                    "Cannot establish DB connection, please re-run script without parameter, and follow the instructions.")
S
Steven Li 已提交
1291
                sys.exit(2)
1292
            else:
S
Shuduo Sang 已提交
1293 1294
                raise
        except BaseException:
S
Steven Li 已提交
1295
            print("[=] Unexpected exception")
S
Shuduo Sang 已提交
1296 1297 1298 1299
            raise

        if resetDb:
            self._dbConn.resetDb()  # drop and recreate DB
1300

S
Shuduo Sang 已提交
1301 1302
        # Do this after dbConn is in proper shape
        self._stateMachine = StateMechine(self._dbConn)
1303

1304 1305 1306
    def getDbConn(self):
        return self._dbConn

S
Shuduo Sang 已提交
1307
    def getStateMachine(self) -> StateMechine:
1308 1309 1310 1311
        return self._stateMachine

    # def getState(self):
    #     return self._stateMachine.getCurrentState()
1312 1313 1314 1315 1316 1317

    # We aim to create a starting time tick, such that, whenever we run our test here once
    # We should be able to safely create 100,000 records, which will not have any repeated time stamp
    # when we re-run the test in 3 minutes (180 seconds), basically we should expand time duration
    # by a factor of 500.
    # TODO: what if it goes beyond 10 years into the future
1318
    # TODO: fix the error as result of above: "tsdb timestamp is out of range"
1319
    def setupLastTick(self):
1320
        t1 = datetime.datetime(2020, 6, 1)
1321
        t2 = datetime.datetime.now()
S
Shuduo Sang 已提交
1322 1323 1324 1325
        # maybe a very large number, takes 69 years to exceed Python int range
        elSec = int(t2.timestamp() - t1.timestamp())
        elSec2 = (elSec % (8 * 12 * 30 * 24 * 60 * 60 / 500)) * \
            500  # a number representing seconds within 10 years
1326
        # print("elSec = {}".format(elSec))
S
Shuduo Sang 已提交
1327 1328 1329
        t3 = datetime.datetime(2012, 1, 1)  # default "keep" is 10 years
        t4 = datetime.datetime.fromtimestamp(
            t3.timestamp() + elSec2)  # see explanation above
1330 1331 1332
        logger.info("Setting up TICKS to start from: {}".format(t4))
        return t4

S
Shuduo Sang 已提交
1333
    def pickAndAllocateTable(self):  # pick any table, and "use" it
S
Steven Li 已提交
1334 1335
        return self.tableNumQueue.pickAndAllocate()

1336 1337 1338 1339 1340
    def addTable(self):
        with self._lock:
            tIndex = self.tableNumQueue.push()
        return tIndex

1341 1342
    @classmethod
    def getFixedSuperTableName(cls):
1343
        return "fs_table"
1344

1345 1346 1347 1348
    @classmethod
    def getFixedSuperTable(cls):
        return TdSuperTable(cls.getFixedSuperTableName())

S
Shuduo Sang 已提交
1349
    def releaseTable(self, i):  # return the table back, so others can use it
S
Steven Li 已提交
1350 1351
        self.tableNumQueue.release(i)

1352
    def getNextTick(self):
S
Shuduo Sang 已提交
1353
        with self._lock:  # prevent duplicate tick
1354 1355
            if Dice.throw(20) == 0:  # 1 in 20 chance
                return self._lastTick + datetime.timedelta(0, -100) # Go back in time 100 seconds
S
Shuduo Sang 已提交
1356 1357 1358
            else:  # regular
                # add one second to it
                self._lastTick += datetime.timedelta(0, 1)
S
Steven Li 已提交
1359
                return self._lastTick
1360 1361

    def getNextInt(self):
1362 1363 1364
        with self._lock:
            self._lastInt += 1
            return self._lastInt
1365 1366

    def getNextBinary(self):
S
Shuduo Sang 已提交
1367 1368
        return "Beijing_Shanghai_Los_Angeles_New_York_San_Francisco_Chicago_Beijing_Shanghai_Los_Angeles_New_York_San_Francisco_Chicago_{}".format(
            self.getNextInt())
1369 1370

    def getNextFloat(self):
1371 1372 1373
        ret = 0.9 + self.getNextInt()
        # print("Float obtained: {}".format(ret))
        return ret
S
Shuduo Sang 已提交
1374

S
Steven Li 已提交
1375
    def getTableNameToDelete(self):
S
Shuduo Sang 已提交
1376 1377
        tblNum = self.tableNumQueue.pop()  # TODO: race condition!
        if (not tblNum):  # maybe false
1378
            return False
S
Shuduo Sang 已提交
1379

S
Steven Li 已提交
1380 1381
        return "table_{}".format(tblNum)

1382
    def cleanUp(self):
S
Shuduo Sang 已提交
1383 1384
        self._dbConn.close()

1385

1386
class TaskExecutor():
1387
    class BoundedList:
S
Shuduo Sang 已提交
1388
        def __init__(self, size=10):
1389 1390
            self._size = size
            self._list = []
S
Steven Li 已提交
1391
            self._lock = threading.Lock()
1392

S
Shuduo Sang 已提交
1393
        def add(self, n: int):
S
Steven Li 已提交
1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419
            with self._lock:
                if not self._list:  # empty
                    self._list.append(n)
                    return
                # now we should insert
                nItems = len(self._list)
                insPos = 0
                for i in range(nItems):
                    insPos = i
                    if n <= self._list[i]:  # smaller than this item, time to insert
                        break  # found the insertion point
                    insPos += 1  # insert to the right

                if insPos == 0:  # except for the 1st item, # TODO: elimiate first item as gating item
                    return  # do nothing

                # print("Inserting at postion {}, value: {}".format(insPos, n))
                self._list.insert(insPos, n)  # insert

                newLen = len(self._list)
                if newLen <= self._size:
                    return  # do nothing
                elif newLen == (self._size + 1):
                    del self._list[0]  # remove the first item
                else:
                    raise RuntimeError("Corrupt Bounded List")
1420 1421 1422 1423 1424 1425

        def __str__(self):
            return repr(self._list)

    _boundedList = BoundedList()

1426 1427 1428
    def __init__(self, curStep):
        self._curStep = curStep

1429 1430 1431 1432
    @classmethod
    def getBoundedList(cls):
        return cls._boundedList

1433 1434 1435
    def getCurStep(self):
        return self._curStep

S
Shuduo Sang 已提交
1436
    def execute(self, task: Task, wt: WorkerThread):  # execute a task on a thread
1437
        task.execute(wt)
1438

1439 1440 1441 1442
    def recordDataMark(self, n: int):
        # print("[{}]".format(n), end="", flush=True)
        self._boundedList.add(n)

1443 1444
    # def logInfo(self, msg):
    #     logger.info("    T[{}.x]: ".format(self._curStep) + msg)
1445

1446 1447
    # def logDebug(self, msg):
    #     logger.debug("    T[{}.x]: ".format(self._curStep) + msg)
1448

S
Shuduo Sang 已提交
1449

S
Steven Li 已提交
1450
class Task():
1451 1452 1453 1454
    taskSn = 100

    @classmethod
    def allocTaskNum(cls):
S
Shuduo Sang 已提交
1455
        Task.taskSn += 1  # IMPORTANT: cannot use cls.taskSn, since each sub class will have a copy
S
Steven Li 已提交
1456 1457
        # logger.debug("Allocating taskSN: {}".format(Task.taskSn))
        return Task.taskSn
1458

S
Shuduo Sang 已提交
1459
    def __init__(self, dbManager: DbManager, execStats: ExecutionStats):
1460
        self._dbManager = dbManager
S
Shuduo Sang 已提交
1461
        self._workerThread = None
1462
        self._err = None
1463
        self._aborted = False
1464
        self._curStep = None
S
Shuduo Sang 已提交
1465
        self._numRows = None  # Number of rows affected
1466

S
Shuduo Sang 已提交
1467
        # Assign an incremental task serial number
1468
        self._taskNum = self.allocTaskNum()
S
Steven Li 已提交
1469
        # logger.debug("Creating new task {}...".format(self._taskNum))
1470

1471 1472
        self._execStats = execStats

1473
    def isSuccess(self):
S
Shuduo Sang 已提交
1474
        return self._err is None
1475

1476 1477 1478
    def isAborted(self):
        return self._aborted

S
Shuduo Sang 已提交
1479
    def clone(self):  # TODO: why do we need this again?
1480
        newTask = self.__class__(self._dbManager, self._execStats)
1481 1482 1483
        return newTask

    def logDebug(self, msg):
S
Shuduo Sang 已提交
1484 1485 1486
        self._workerThread.logDebug(
            "Step[{}.{}] {}".format(
                self._curStep, self._taskNum, msg))
1487 1488

    def logInfo(self, msg):
S
Shuduo Sang 已提交
1489 1490 1491
        self._workerThread.logInfo(
            "Step[{}.{}] {}".format(
                self._curStep, self._taskNum, msg))
1492

1493
    def _executeInternal(self, te: TaskExecutor, wt: WorkerThread):
S
Shuduo Sang 已提交
1494 1495 1496
        raise RuntimeError(
            "To be implemeted by child classes, class name: {}".format(
                self.__class__.__name__))
1497

1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510
    def _isErrAcceptable(self, errno, msg):
        if errno in [
                0x05,  # TSDB_CODE_RPC_NOT_READY
                # 0x200, # invalid SQL, TODO: re-examine with TD-934
                0x360, 0x362, 
                0x369, # tag already exists
                0x36A, 0x36B, 0x36D,
                0x381, 
                0x380, # "db not selected"
                0x383,
                0x386,  # DB is being dropped?!
                0x503,
                0x510,  # vnode not in ready state
1511
                0x14,   # db not ready, errno changed
1512 1513 1514 1515
                0x600,
                1000  # REST catch-all error
            ]: 
            return True # These are the ALWAYS-ACCEPTABLE ones
1516 1517
        elif (errno in [ 0x0B ]) and gConfig.auto_start_service:
            return True # We may get "network unavilable" when restarting service
1518 1519 1520
        elif errno == 0x200 : # invalid SQL, we need to div in a bit more
            if msg.find("invalid column name") != -1:
                return True 
1521 1522 1523 1524
            elif msg.find("tags number not matched") != -1: # mismatched tags after modification
                return True
            elif msg.find("duplicated column names") != -1: # also alter table tag issues
                return True
S
Steven Li 已提交
1525 1526 1527
        elif (gSvcMgr!=None) and gSvcMgr.isRestarting():
            logger.info("Ignoring error when service is restarting: errno = {}, msg = {}".format(errno, msg))
            return True
1528 1529 1530 1531
        
        return False # Not an acceptable error


1532 1533
    def execute(self, wt: WorkerThread):
        wt.verifyThreadSelf()
S
Shuduo Sang 已提交
1534
        self._workerThread = wt  # type: ignore
1535 1536

        te = wt.getTaskExecutor()
1537
        self._curStep = te.getCurStep()
S
Shuduo Sang 已提交
1538 1539
        self.logDebug(
            "[-] executing task {}...".format(self.__class__.__name__))
1540 1541

        self._err = None
1542 1543
        self._execStats.beginTaskType(self.__class__.__name__)  # mark beginning
        errno2 = None
1544
        try:
S
Shuduo Sang 已提交
1545
            self._executeInternal(te, wt)  # TODO: no return value?
1546
        except taos.error.ProgrammingError as err:
1547
            errno2 = Helper.convertErrno(err.errno)
1548
            if (gConfig.continue_on_exception):  # user choose to continue
1549
                self.logDebug("[=] Continue after TAOS exception: errno=0x{:X}, msg: {}, SQL: {}".format(
1550
                        errno2, err, wt.getDbConn().getLastSql()))
1551
                self._err = err
1552 1553
            elif self._isErrAcceptable(errno2, err.__str__()):
                self.logDebug("[=] Acceptable Taos library exception: errno=0x{:X}, msg: {}, SQL: {}".format(
1554
                        errno2, err, wt.getDbConn().getLastSql()))
1555
                print("_", end="", flush=True)
S
Shuduo Sang 已提交
1556
                self._err = err
1557
            else: # not an acceptable error
1558 1559 1560
                errMsg = "[=] Unexpected Taos library exception ({}): errno=0x{:X}, msg: {}, SQL: {}".format(
                    self.__class__.__name__,
                    errno2, err, wt.getDbConn().getLastSql())
1561
                self.logDebug(errMsg)
S
Shuduo Sang 已提交
1562
                if gConfig.debug:
1563 1564
                    # raise # so that we see full stack
                    traceback.print_exc()
1565 1566
                print(
                    "\n\n----------------------------\nProgram ABORTED Due to Unexpected TAOS Error: \n\n{}\n".format(errMsg) +
1567 1568 1569 1570
                    "----------------------------\n")
                # sys.exit(-1)
                self._err = err
                self._aborted = True
S
Shuduo Sang 已提交
1571
        except Exception as e:
S
Steven Li 已提交
1572
            self.logInfo("Non-TAOS exception encountered")
S
Shuduo Sang 已提交
1573
            self._err = e
S
Steven Li 已提交
1574
            self._aborted = True
1575
            traceback.print_exc()
1576
        except BaseException as e:
1577
            self.logInfo("Python base exception encountered")
1578
            self._err = e
1579
            self._aborted = True
S
Steven Li 已提交
1580
            traceback.print_exc()
S
Shuduo Sang 已提交
1581 1582 1583
        except BaseException:
            self.logDebug(
                "[=] Unexpected exception, SQL: {}".format(
1584
                    wt.getDbConn().getLastSql()))
1585
            raise
1586
        self._execStats.endTaskType(self.__class__.__name__, self.isSuccess())
S
Shuduo Sang 已提交
1587 1588 1589 1590

        self.logDebug("[X] task execution completed, {}, status: {}".format(
            self.__class__.__name__, "Success" if self.isSuccess() else "Failure"))
        # TODO: merge with above.
1591
        self._execStats.incExecCount(self.__class__.__name__, self.isSuccess(), errno2)
S
Steven Li 已提交
1592

1593
    def execSql(self, sql):
1594
        return self._dbManager.execute(sql)
1595

S
Shuduo Sang 已提交
1596
    def execWtSql(self, wt: WorkerThread, sql):  # execute an SQL on the worker thread
1597 1598
        return wt.execSql(sql)

S
Shuduo Sang 已提交
1599
    def queryWtSql(self, wt: WorkerThread, sql):  # execute an SQL on the worker thread
1600 1601
        return wt.querySql(sql)

S
Shuduo Sang 已提交
1602
    def getQueryResult(self, wt: WorkerThread):  # execute an SQL on the worker thread
1603 1604 1605
        return wt.getQueryResult()


1606
class ExecutionStats:
1607
    def __init__(self):
S
Shuduo Sang 已提交
1608 1609
        # total/success times for a task
        self._execTimes: Dict[str, [int, int]] = {}
1610 1611 1612
        self._tasksInProgress = 0
        self._lock = threading.Lock()
        self._firstTaskStartTime = None
1613
        self._execStartTime = None
1614
        self._errors = {}
S
Shuduo Sang 已提交
1615 1616
        self._elapsedTime = 0.0  # total elapsed time
        self._accRunTime = 0.0  # accumulated run time
1617

1618 1619 1620
        self._failed = False
        self._failureReason = None

S
Steven Li 已提交
1621
    def __str__(self):
S
Shuduo Sang 已提交
1622 1623
        return "[ExecStats: _failed={}, _failureReason={}".format(
            self._failed, self._failureReason)
S
Steven Li 已提交
1624 1625

    def isFailed(self):
S
Shuduo Sang 已提交
1626
        return self._failed
S
Steven Li 已提交
1627

1628 1629 1630 1631 1632 1633
    def startExec(self):
        self._execStartTime = time.time()

    def endExec(self):
        self._elapsedTime = time.time() - self._execStartTime

1634
    def incExecCount(self, klassName, isSuccess, eno=None):  # TODO: add a lock here
1635 1636
        if klassName not in self._execTimes:
            self._execTimes[klassName] = [0, 0]
S
Shuduo Sang 已提交
1637 1638
        t = self._execTimes[klassName]  # tuple for the data
        t[0] += 1  # index 0 has the "total" execution times
1639
        if isSuccess:
S
Shuduo Sang 已提交
1640
            t[1] += 1  # index 1 has the "success" execution times
1641 1642 1643 1644 1645
        if eno != None:             
            if klassName not in self._errors:
                self._errors[klassName] = {}
            errors = self._errors[klassName]
            errors[eno] = errors[eno]+1 if eno in errors else 1
1646 1647 1648

    def beginTaskType(self, klassName):
        with self._lock:
S
Shuduo Sang 已提交
1649 1650
            if self._tasksInProgress == 0:  # starting a new round
                self._firstTaskStartTime = time.time()  # I am now the first task
1651 1652 1653 1654 1655
            self._tasksInProgress += 1

    def endTaskType(self, klassName, isSuccess):
        with self._lock:
            self._tasksInProgress -= 1
S
Shuduo Sang 已提交
1656
            if self._tasksInProgress == 0:  # all tasks have stopped
1657 1658 1659
                self._accRunTime += (time.time() - self._firstTaskStartTime)
                self._firstTaskStartTime = None

1660 1661 1662 1663
    def registerFailure(self, reason):
        self._failed = True
        self._failureReason = reason

1664
    def printStats(self):
S
Shuduo Sang 已提交
1665 1666 1667 1668 1669 1670
        logger.info(
            "----------------------------------------------------------------------")
        logger.info(
            "| Crash_Gen test {}, with the following stats:". format(
                "FAILED (reason: {})".format(
                    self._failureReason) if self._failed else "SUCCEEDED"))
1671
        logger.info("| Task Execution Times (success/total):")
1672
        execTimesAny = 0
S
Shuduo Sang 已提交
1673
        for k, n in self._execTimes.items():
1674
            execTimesAny += n[0]
1675 1676 1677 1678 1679 1680 1681 1682
            errStr = None
            if k in self._errors:
                errors = self._errors[k]
                # print("errors = {}".format(errors))
                errStrs = ["0x{:X}:{}".format(eno, n) for (eno, n) in errors.items()]
                # print("error strings = {}".format(errStrs))
                errStr = ", ".join(errStrs) 
            logger.info("|    {0:<24}: {1}/{2} (Errors: {3})".format(k, n[1], n[0], errStr))
S
Shuduo Sang 已提交
1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701

        logger.info(
            "| Total Tasks Executed (success or not): {} ".format(execTimesAny))
        logger.info(
            "| Total Tasks In Progress at End: {}".format(
                self._tasksInProgress))
        logger.info(
            "| Total Task Busy Time (elapsed time when any task is in progress): {:.3f} seconds".format(
                self._accRunTime))
        logger.info(
            "| Average Per-Task Execution Time: {:.3f} seconds".format(self._accRunTime / execTimesAny))
        logger.info(
            "| Total Elapsed Time (from wall clock): {:.3f} seconds".format(
                self._elapsedTime))
        logger.info(
            "| Top numbers written: {}".format(
                TaskExecutor.getBoundedList()))
        logger.info(
            "----------------------------------------------------------------------")
1702 1703 1704


class StateTransitionTask(Task):
1705 1706 1707 1708 1709
    LARGE_NUMBER_OF_TABLES = 35
    SMALL_NUMBER_OF_TABLES = 3
    LARGE_NUMBER_OF_RECORDS = 50
    SMALL_NUMBER_OF_RECORDS = 3

1710
    @classmethod
S
Shuduo Sang 已提交
1711
    def getInfo(cls):  # each sub class should supply their own information
1712 1713
        raise RuntimeError("Overriding method expected")

S
Shuduo Sang 已提交
1714
    _endState = None
1715
    @classmethod
S
Shuduo Sang 已提交
1716
    def getEndState(cls):  # TODO: optimize by calling it fewer times
1717 1718
        raise RuntimeError("Overriding method expected")

1719 1720 1721
    # @classmethod
    # def getBeginStates(cls):
    #     return cls.getInfo()[0]
1722

1723 1724 1725
    # @classmethod
    # def getEndState(cls): # returning the class name
    #     return cls.getInfo()[0]
1726 1727

    @classmethod
1728 1729 1730
    def canBeginFrom(cls, state: AnyState):
        # return state.getValue() in cls.getBeginStates()
        raise RuntimeError("must be overriden")
1731

1732 1733
    @classmethod
    def getRegTableName(cls, i):
1734
        return "reg_table_{}".format(i)
1735

1736 1737
    def execute(self, wt: WorkerThread):
        super().execute(wt)
S
Shuduo Sang 已提交
1738 1739


1740
class TaskCreateDb(StateTransitionTask):
1741
    @classmethod
1742
    def getEndState(cls):
S
Shuduo Sang 已提交
1743
        return StateDbOnly()
1744

1745 1746 1747 1748
    @classmethod
    def canBeginFrom(cls, state: AnyState):
        return state.canCreateDb()

1749
    def _executeInternal(self, te: TaskExecutor, wt: WorkerThread):
S
Shuduo Sang 已提交
1750 1751
        self.execWtSql(wt, "create database db")

1752

1753
class TaskDropDb(StateTransitionTask):
1754
    @classmethod
1755 1756
    def getEndState(cls):
        return StateEmpty()
1757

1758 1759 1760 1761
    @classmethod
    def canBeginFrom(cls, state: AnyState):
        return state.canDropDb()

1762
    def _executeInternal(self, te: TaskExecutor, wt: WorkerThread):
1763
        self.execWtSql(wt, "drop database db")
S
Steven Li 已提交
1764
        logger.debug("[OPS] database dropped at {}".format(time.time()))
1765

S
Shuduo Sang 已提交
1766

1767
class TaskCreateSuperTable(StateTransitionTask):
1768
    @classmethod
1769 1770
    def getEndState(cls):
        return StateSuperTableOnly()
1771

1772 1773
    @classmethod
    def canBeginFrom(cls, state: AnyState):
1774
        return state.canCreateFixedSuperTable()
1775

1776
    def _executeInternal(self, te: TaskExecutor, wt: WorkerThread):
S
Shuduo Sang 已提交
1777
        if not wt.dbInUse():  # no DB yet, to the best of our knowledge
1778 1779 1780
            logger.debug("Skipping task, no DB yet")
            return

1781
        sTable = self._dbManager.getFixedSuperTable()
1782
        # wt.execSql("use db")    # should always be in place
1783 1784
        sTable.create(wt.getDbConn(), {'ts':'timestamp', 'speed':'int'}, {'b':'binary(200)', 'f':'float'})
        # self.execWtSql(wt,"create table db.{} (ts timestamp, speed int) tags (b binary(200), f float) ".format(tblName))
S
Shuduo Sang 已提交
1785 1786
        # No need to create the regular tables, INSERT will do that
        # automatically
1787

S
Steven Li 已提交
1788

1789 1790 1791 1792
class TdSuperTable:
    def __init__(self, stName):
        self._stName = stName

1793 1794 1795
    def getName(self):
        return self._stName

1796 1797 1798 1799 1800 1801 1802 1803
    def create(self, dbc, cols: dict, tags: dict):
        sql = "CREATE TABLE db.{} ({}) TAGS ({})".format(
            self._stName,
            ",".join(['%s %s'%(k,v) for (k,v) in cols.items()]),
            ",".join(['%s %s'%(k,v) for (k,v) in tags.items()])
            )
        dbc.execute(sql)        

1804 1805 1806 1807
    def getRegTables(self, dbc: DbConn):
        try:
            dbc.query("select TBNAME from db.{}".format(self._stName))  # TODO: analyze result set later            
        except taos.error.ProgrammingError as err:                    
1808
            errno2 = Helper.convertErrno(err.errno) 
1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821
            logger.debug("[=] Failed to get tables from super table: errno=0x{:X}, msg: {}".format(errno2, err))
            raise

        qr = dbc.getQueryResult()
        return [v[0] for v in qr] # list transformation, ref: https://stackoverflow.com/questions/643823/python-list-transformation

    def hasRegTables(self, dbc: DbConn):
        return dbc.query("SELECT * FROM db.{}".format(self._stName)) > 0

    def ensureTable(self, dbc: DbConn, regTableName: str):
        sql = "select tbname from {} where tbname in ('{}')".format(self._stName, regTableName)
        if dbc.query(sql) >= 1 : # reg table exists already
            return
1822 1823
        sql = "CREATE TABLE {} USING {} tags ({})".format(
            regTableName, self._stName, self._getTagStrForSql(dbc)
1824 1825 1826
        )
        dbc.execute(sql)

1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871
    def _getTagStrForSql(self, dbc) :
        tags = self._getTags(dbc)
        tagStrs = []
        for tagName in tags: 
            tagType = tags[tagName]
            if tagType == 'BINARY':
                tagStrs.append("'Beijing-Shanghai-LosAngeles'")
            elif tagType == 'FLOAT':
                tagStrs.append('9.9')
            elif tagType == 'INT':
                tagStrs.append('88')
            else:
                raise RuntimeError("Unexpected tag type: {}".format(tagType))
        return ", ".join(tagStrs)

    def _getTags(self, dbc) -> dict:
        dbc.query("DESCRIBE {}".format(self._stName))
        stCols = dbc.getQueryResult()
        # print(stCols)
        ret = {row[0]:row[1] for row in stCols if row[3]=='TAG'} # name:type
        # print("Tags retrieved: {}".format(ret))
        return ret

    def addTag(self, dbc, tagName, tagType):
        if tagName in self._getTags(dbc): # already 
            return
        # sTable.addTag("extraTag", "int")
        sql = "alter table db.{} add tag {} {}".format(self._stName, tagName, tagType)
        dbc.execute(sql)

    def dropTag(self, dbc, tagName):
        if not tagName in self._getTags(dbc): # don't have this tag
            return
        sql = "alter table db.{} drop tag {}".format(self._stName, tagName)
        dbc.execute(sql)

    def changeTag(self, dbc, oldTag, newTag):
        tags = self._getTags(dbc)
        if not oldTag in tags: # don't have this tag
            return
        if newTag in tags: # already have this tag
            return
        sql = "alter table db.{} change tag {} {}".format(self._stName, oldTag, newTag)
        dbc.execute(sql)

1872
class TaskReadData(StateTransitionTask):
1873
    @classmethod
1874
    def getEndState(cls):
S
Shuduo Sang 已提交
1875
        return None  # meaning doesn't affect state
1876

1877 1878 1879 1880
    @classmethod
    def canBeginFrom(cls, state: AnyState):
        return state.canReadData()

1881
    def _executeInternal(self, te: TaskExecutor, wt: WorkerThread):
1882
        sTable = self._dbManager.getFixedSuperTable()
1883

S
Shuduo Sang 已提交
1884 1885
        if random.randrange(
                5) == 0:  # 1 in 5 chance, simulate a broken connection. TODO: break connection in all situations
1886 1887
            wt.getDbConn().close()
            wt.getDbConn().open()
1888
        
1889 1890 1891 1892 1893 1894
        dbc = wt.getDbConn()
        for rTbName in sTable.getRegTables(dbc):  # regular tables
            aggExpr = Dice.choice([
                '*', 
                'count(*)', 
                'avg(speed)', 
1895
                # 'twa(speed)', # TODO: this one REQUIRES a where statement, not reasonable
1896 1897 1898 1899 1900 1901 1902 1903 1904
                'sum(speed)', 
                'stddev(speed)', 
                'min(speed)', 
                'max(speed)', 
                'first(speed)', 
                'last(speed)']) # TODO: add more from 'top'
            filterExpr = Dice.choice([ # TODO: add various kind of WHERE conditions
                None
            ])
1905
            try:
1906 1907 1908
                dbc.execute("select {} from db.{}".format(aggExpr, rTbName))
                if aggExpr not in ['stddev(speed)']: #TODO: STDDEV not valid for super tables?!
                    dbc.execute("select {} from db.{}".format(aggExpr, sTable.getName()))
1909
            except taos.error.ProgrammingError as err:                    
1910
                errno2 = Helper.convertErrno(err.errno)
1911
                logger.debug("[=] Read Failure: errno=0x{:X}, msg: {}, SQL: {}".format(errno2, err, dbc.getLastSql()))
1912
                raise
S
Shuduo Sang 已提交
1913

1914
class TaskDropSuperTable(StateTransitionTask):
1915
    @classmethod
1916
    def getEndState(cls):
S
Shuduo Sang 已提交
1917
        return StateDbOnly()
1918

1919 1920
    @classmethod
    def canBeginFrom(cls, state: AnyState):
1921
        return state.canDropFixedSuperTable()
1922

1923
    def _executeInternal(self, te: TaskExecutor, wt: WorkerThread):
S
Shuduo Sang 已提交
1924 1925 1926 1927 1928 1929 1930
        # 1/2 chance, we'll drop the regular tables one by one, in a randomized
        # sequence
        if Dice.throw(2) == 0:
            tblSeq = list(range(
                2 + (self.LARGE_NUMBER_OF_TABLES if gConfig.larger_data else self.SMALL_NUMBER_OF_TABLES)))
            random.shuffle(tblSeq)
            tickOutput = False  # if we have spitted out a "d" character for "drop regular table"
1931
            isSuccess = True
S
Shuduo Sang 已提交
1932 1933 1934
            for i in tblSeq:
                regTableName = self.getRegTableName(
                    i)  # "db.reg_table_{}".format(i)
1935
                try:
S
Shuduo Sang 已提交
1936 1937 1938
                    self.execWtSql(wt, "drop table {}".format(
                        regTableName))  # nRows always 0, like MySQL
                except taos.error.ProgrammingError as err:
1939 1940
                    # correcting for strange error number scheme                    
                    errno2 = Helper.convertErrno(err.errno)
S
Shuduo Sang 已提交
1941
                    if (errno2 in [0x362]):  # mnode invalid table name
1942
                        isSuccess = False
S
Shuduo Sang 已提交
1943 1944 1945
                        logger.debug(
                            "[DB] Acceptable error when dropping a table")
                    continue  # try to delete next regular table
1946 1947

                if (not tickOutput):
S
Shuduo Sang 已提交
1948 1949
                    tickOutput = True  # Print only one time
                    if isSuccess:
1950 1951
                        print("d", end="", flush=True)
                    else:
S
Shuduo Sang 已提交
1952
                        print("f", end="", flush=True)
1953 1954

        # Drop the super table itself
S
Shuduo Sang 已提交
1955
        tblName = self._dbManager.getFixedSuperTableName()
1956
        self.execWtSql(wt, "drop table db.{}".format(tblName))
1957

S
Shuduo Sang 已提交
1958

1959 1960 1961
class TaskAlterTags(StateTransitionTask):
    @classmethod
    def getEndState(cls):
S
Shuduo Sang 已提交
1962
        return None  # meaning doesn't affect state
1963 1964 1965

    @classmethod
    def canBeginFrom(cls, state: AnyState):
S
Shuduo Sang 已提交
1966
        return state.canDropFixedSuperTable()  # if we can drop it, we can alter tags
1967 1968

    def _executeInternal(self, te: TaskExecutor, wt: WorkerThread):
1969 1970 1971
        # tblName = self._dbManager.getFixedSuperTableName()
        dbc = wt.getDbConn()
        sTable = self._dbManager.getFixedSuperTable()
1972
        dice = Dice.throw(4)
S
Shuduo Sang 已提交
1973
        if dice == 0:
1974 1975
            sTable.addTag(dbc, "extraTag", "int")
            # sql = "alter table db.{} add tag extraTag int".format(tblName)
S
Shuduo Sang 已提交
1976
        elif dice == 1:
1977 1978
            sTable.dropTag(dbc, "extraTag")
            # sql = "alter table db.{} drop tag extraTag".format(tblName)
S
Shuduo Sang 已提交
1979
        elif dice == 2:
1980 1981
            sTable.dropTag(dbc, "newTag")
            # sql = "alter table db.{} drop tag newTag".format(tblName)
S
Shuduo Sang 已提交
1982
        else:  # dice == 3
1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999
            sTable.changeTag(dbc, "extraTag", "newTag")
            # sql = "alter table db.{} change tag extraTag newTag".format(tblName)

class TaskRestartService(StateTransitionTask):
    _isRunning = False
    _classLock = threading.Lock()

    @classmethod
    def getEndState(cls):
        return None  # meaning doesn't affect state

    @classmethod
    def canBeginFrom(cls, state: AnyState):
        if gConfig.auto_start_service:
            return state.canDropFixedSuperTable()  # Basicallly when we have the super table
        return False # don't run this otherwise

2000
    CHANCE_TO_RESTART_SERVICE = 100
2001 2002 2003 2004
    def _executeInternal(self, te: TaskExecutor, wt: WorkerThread):
        if not gConfig.auto_start_service: # only execute when we are in -a mode
            print("_a", end="", flush=True)
            return
2005

2006 2007 2008 2009 2010 2011
        with self._classLock:
            if self._isRunning:
                print("Skipping restart task, another running already")
                return
            self._isRunning = True

2012
        if Dice.throw(self.CHANCE_TO_RESTART_SERVICE) == 0: # 1 in N chance
2013 2014 2015
            dbc = wt.getDbConn()
            dbc.execute("show databases") # simple delay, align timing with other workers
            gSvcMgr.restart()
2016

2017
        self._isRunning = False
S
Shuduo Sang 已提交
2018

2019
class TaskAddData(StateTransitionTask):
S
Shuduo Sang 已提交
2020 2021
    # Track which table is being actively worked on
    activeTable: Set[int] = set()
2022

S
Shuduo Sang 已提交
2023 2024
    # We use these two files to record operations to DB, useful for power-off
    # tests
2025 2026 2027 2028 2029
    fAddLogReady = None
    fAddLogDone = None

    @classmethod
    def prepToRecordOps(cls):
S
Shuduo Sang 已提交
2030 2031 2032 2033
        if gConfig.record_ops:
            if (cls.fAddLogReady is None):
                logger.info(
                    "Recording in a file operations to be performed...")
2034
                cls.fAddLogReady = open("add_log_ready.txt", "w")
S
Shuduo Sang 已提交
2035
            if (cls.fAddLogDone is None):
2036 2037
                logger.info("Recording in a file operations completed...")
                cls.fAddLogDone = open("add_log_done.txt", "w")
2038

2039
    @classmethod
2040 2041
    def getEndState(cls):
        return StateHasData()
2042 2043 2044 2045

    @classmethod
    def canBeginFrom(cls, state: AnyState):
        return state.canAddData()
S
Shuduo Sang 已提交
2046

2047
    def _executeInternal(self, te: TaskExecutor, wt: WorkerThread):
2048
        ds = self._dbManager # Quite DANGEROUS here, may result in multi-thread client access
2049
        tblSeq = list(range(
S
Shuduo Sang 已提交
2050 2051 2052 2053
                self.LARGE_NUMBER_OF_TABLES if gConfig.larger_data else self.SMALL_NUMBER_OF_TABLES))
        random.shuffle(tblSeq)
        for i in tblSeq:
            if (i in self.activeTable):  # wow already active
2054
                print("x", end="", flush=True) # concurrent insertion
2055
            else:
S
Shuduo Sang 已提交
2056
                self.activeTable.add(i)  # marking it active
2057 2058 2059
            
            sTable = ds.getFixedSuperTable()
            regTableName = self.getRegTableName(i)  # "db.reg_table_{}".format(i)
2060
            sTable.ensureTable(wt.getDbConn(), regTableName)  # Ensure the table exists           
2061 2062
           
            for j in range(self.LARGE_NUMBER_OF_RECORDS if gConfig.larger_data else self.SMALL_NUMBER_OF_RECORDS):  # number of records per table
S
Shuduo Sang 已提交
2063
                nextInt = ds.getNextInt()
2064 2065
                if gConfig.record_ops:
                    self.prepToRecordOps()
2066
                    self.fAddLogReady.write("Ready to write {} to {}\n".format(nextInt, regTableName))
2067 2068
                    self.fAddLogReady.flush()
                    os.fsync(self.fAddLogReady)
2069
                sql = "insert into {} values ('{}', {});".format( # removed: tags ('{}', {})
S
Shuduo Sang 已提交
2070
                    regTableName,
2071 2072
                    # ds.getFixedSuperTableName(),
                    # ds.getNextBinary(), ds.getNextFloat(),
2073
                    ds.getNextTick(), nextInt)
S
Shuduo Sang 已提交
2074 2075 2076
                self.execWtSql(wt, sql)
                # Successfully wrote the data into the DB, let's record it
                # somehow
2077
                te.recordDataMark(nextInt)
2078
                if gConfig.record_ops:
S
Shuduo Sang 已提交
2079 2080 2081
                    self.fAddLogDone.write(
                        "Wrote {} to {}\n".format(
                            nextInt, regTableName))
2082 2083
                    self.fAddLogDone.flush()
                    os.fsync(self.fAddLogDone)
S
Shuduo Sang 已提交
2084
            self.activeTable.discard(i)  # not raising an error, unlike remove
2085 2086


S
Steven Li 已提交
2087 2088
# Deterministic random number generator
class Dice():
S
Shuduo Sang 已提交
2089
    seeded = False  # static, uninitialized
S
Steven Li 已提交
2090 2091

    @classmethod
S
Shuduo Sang 已提交
2092
    def seed(cls, s):  # static
S
Steven Li 已提交
2093
        if (cls.seeded):
S
Shuduo Sang 已提交
2094 2095
            raise RuntimeError(
                "Cannot seed the random generator more than once")
S
Steven Li 已提交
2096 2097 2098 2099 2100
        cls.verifyRNG()
        random.seed(s)
        cls.seeded = True  # TODO: protect against multi-threading

    @classmethod
S
Shuduo Sang 已提交
2101
    def verifyRNG(cls):  # Verify that the RNG is determinstic
S
Steven Li 已提交
2102 2103 2104 2105
        random.seed(0)
        x1 = random.randrange(0, 1000)
        x2 = random.randrange(0, 1000)
        x3 = random.randrange(0, 1000)
S
Shuduo Sang 已提交
2106
        if (x1 != 864 or x2 != 394 or x3 != 776):
S
Steven Li 已提交
2107 2108 2109
            raise RuntimeError("System RNG is not deterministic")

    @classmethod
S
Shuduo Sang 已提交
2110
    def throw(cls, stop):  # get 0 to stop-1
2111
        return cls.throwRange(0, stop)
S
Steven Li 已提交
2112 2113

    @classmethod
S
Shuduo Sang 已提交
2114 2115
    def throwRange(cls, start, stop):  # up to stop-1
        if (not cls.seeded):
S
Steven Li 已提交
2116
            raise RuntimeError("Cannot throw dice before seeding it")
2117
        return random.randrange(start, stop)
S
Steven Li 已提交
2118

2119 2120 2121 2122
    @classmethod
    def choice(cls, cList):
        return random.choice(cList)

S
Steven Li 已提交
2123

S
Steven Li 已提交
2124 2125
class LoggingFilter(logging.Filter):
    def filter(self, record: logging.LogRecord):
S
Shuduo Sang 已提交
2126 2127
        if (record.levelno >= logging.INFO):
            return True  # info or above always log
S
Steven Li 已提交
2128

S
Steven Li 已提交
2129 2130 2131 2132
        # Commenting out below to adjust...

        # if msg.startswith("[TRD]"):
        #     return False
S
Steven Li 已提交
2133 2134
        return True

S
Shuduo Sang 已提交
2135 2136

class MyLoggingAdapter(logging.LoggerAdapter):
2137 2138 2139 2140
    def process(self, msg, kwargs):
        return "[{}]{}".format(threading.get_ident() % 10000, msg), kwargs
        # return '[%s] %s' % (self.extra['connid'], msg), kwargs

S
Shuduo Sang 已提交
2141 2142

class SvcManager:
2143
    def __init__(self):
2144
        print("Starting TDengine Service Manager")
2145 2146 2147
        # signal.signal(signal.SIGTERM, self.sigIntHandler) # Moved to MainExec
        # signal.signal(signal.SIGINT, self.sigIntHandler)
        # signal.signal(signal.SIGUSR1, self.sigUsrHandler)  # different handler!
2148

2149
        self.inSigHandler = False
2150 2151
        # self._status = MainExec.STATUS_RUNNING # set inside
        # _startTaosService()
2152
        self.svcMgrThread = None
2153 2154
        self._lock = threading.Lock()
        self._isRestarting = False
2155

2156 2157 2158 2159 2160 2161 2162 2163
    def _doMenu(self):
        choice = ""
        while True:
            print("\nInterrupting Service Program, Choose an Action: ")
            print("1: Resume")
            print("2: Terminate")
            print("3: Restart")
            # Remember to update the if range below
S
Shuduo Sang 已提交
2164
            # print("Enter Choice: ", end="", flush=True)
2165 2166 2167
            while choice == "":
                choice = input("Enter Choice: ")
                if choice != "":
S
Shuduo Sang 已提交
2168 2169 2170
                    break  # done with reading repeated input
            if choice in ["1", "2", "3"]:
                break  # we are done with whole method
2171
            print("Invalid choice, please try again.")
S
Shuduo Sang 已提交
2172
            choice = ""  # reset
2173 2174
        return choice

S
Shuduo Sang 已提交
2175
    def sigUsrHandler(self, signalNumber, frame):
2176
        print("Interrupting main thread execution upon SIGUSR1")
2177
        if self.inSigHandler:  # already
2178
            print("Ignoring repeated SIG...")
S
Shuduo Sang 已提交
2179
            return  # do nothing if it's already not running
2180
        self.inSigHandler = True
2181 2182

        choice = self._doMenu()
S
Shuduo Sang 已提交
2183 2184 2185 2186 2187
        if choice == "1":
            # TODO: can the sub-process be blocked due to us not reading from
            # queue?
            self.sigHandlerResume()
        elif choice == "2":
2188
            self.stopTaosService()
2189 2190
        elif choice == "3": # Restart
            self.restart()
2191 2192
        else:
            raise RuntimeError("Invalid menu choice: {}".format(choice))
2193

2194 2195
        self.inSigHandler = False

2196
    def sigIntHandler(self, signalNumber, frame):
2197
        print("SvcManager: INT Signal Handler starting...")
2198
        if self.inSigHandler:
2199 2200
            print("Ignoring repeated SIG_INT...")
            return
2201
        self.inSigHandler = True
2202

S
Shuduo Sang 已提交
2203
        self.stopTaosService()
2204
        print("SvcManager: INT Signal Handler returning...")
2205
        self.inSigHandler = False
2206

S
Shuduo Sang 已提交
2207
    def sigHandlerResume(self):
2208
        print("Resuming TDengine service manager thread (main thread)...\n\n")
2209

2210
    def _checkServiceManagerThread(self):
2211 2212 2213 2214
        if self.svcMgrThread:  # valid svc mgr thread
            if self.svcMgrThread.isStopped():  # done?
                self.svcMgrThread.procIpcBatch()  # one last time. TODO: appropriate?
                self.svcMgrThread = None  # no more
2215 2216

    def _procIpcAll(self):
2217 2218 2219 2220 2221 2222
        while self.isRunning() or self.isRestarting() :  # for as long as the svc mgr thread is still here
            if self.isRunning():
                self.svcMgrThread.procIpcBatch()  # regular processing,
                self._checkServiceManagerThread()
            elif self.isRetarting():
                print("Service restarting...")
2223 2224 2225 2226 2227
            time.sleep(0.5)  # pause, before next round
        print(
            "Service Manager Thread (with subprocess) has ended, main thread now exiting...")

    def startTaosService(self):
2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240
        with self._lock:
            if self.svcMgrThread:
                raise RuntimeError("Cannot start TAOS service when one may already be running")

            # Find if there's already a taosd service, and then kill it
            for proc in psutil.process_iter():
                if proc.name() == 'taosd':
                    print("Killing an existing TAOSD process in 2 seconds... press CTRL-C to interrupe")
                    time.sleep(2.0)
                    proc.kill()
                # print("Process: {}".format(proc.name()))
            
            self.svcMgrThread = ServiceManagerThread()  # create the object
S
Steven Li 已提交
2241
            print("Attempting to start TAOS service started, printing out output...")
2242
            self.svcMgrThread.start()            
2243 2244 2245 2246
            self.svcMgrThread.procIpcBatch(
                trimToTarget=10,
                forceOutput=True)  # for printing 10 lines
            print("TAOS service started")
2247 2248

    def stopTaosService(self, outputLines=20):
2249 2250 2251 2252
        with self._lock:
            if not self.isRunning():
                logger.warning("Cannot stop TAOS service, not running")
                return
2253

2254 2255 2256 2257 2258
            print("Terminating Service Manager Thread (SMT) execution...")
            self.svcMgrThread.stop()
            if self.svcMgrThread.isStopped():
                self.svcMgrThread.procIpcBatch(outputLines)  # one last time
                self.svcMgrThread = None
2259 2260
                print("End of TDengine Service Output")
                print("----- TDengine Service (managed by SMT) is now terminated -----\n")
2261 2262
            else:
                print("WARNING: SMT did not terminate as expected")
2263 2264 2265

    def run(self):
        self.startTaosService()
2266
        self._procIpcAll()  # pump/process all the messages, may encounter SIG + restart
2267
        if self.isRunning():  # if sig handler hasn't destroyed it by now
2268 2269
            self.stopTaosService()  # should have started already

2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283
    def restart(self):
        if self._isRestarting:
            logger.warning("Cannot restart service when it's already restarting")
            return

        self._isRestarting = True
        if self.isRunning():
            self.stopTaosService()
        else:
            logger.warning("Service not running when restart requested")

        self.startTaosService()
        self._isRestarting = False

2284 2285
    def isRunning(self):
        return self.svcMgrThread != None
2286

2287 2288 2289
    def isRestarting(self):
        return self._isRestarting

2290 2291 2292 2293 2294
class ServiceManagerThread:
    MAX_QUEUE_SIZE = 10000

    def __init__(self):
        self._tdeSubProcess = None
2295
        self._thread = None
2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312
        self._status = None

    def getStatus(self):
        return self._status

    def isRunning(self):
        # return self._thread and self._thread.is_alive()
        return self._status == MainExec.STATUS_RUNNING

    def isStopping(self):
        return self._status == MainExec.STATUS_STOPPING

    def isStopped(self):
        return self._status == MainExec.STATUS_STOPPED

    # Start the thread (with sub process), and wait for the sub service
    # to become fully operational
2313 2314
    def start(self):
        if self._thread:
2315
            raise RuntimeError("Unexpected _thread")
2316
        if self._tdeSubProcess:
2317 2318 2319 2320
            raise RuntimeError("TDengine sub process already created/running")

        self._status = MainExec.STATUS_STARTING

2321
        self._tdeSubProcess = TdeSubProcess()
2322 2323 2324 2325
        self._tdeSubProcess.start()

        self._ipcQueue = Queue()
        self._thread = threading.Thread(
2326
            target=self.svcOutputReader,
2327
            args=(self._tdeSubProcess.getStdOut(), self._ipcQueue))
2328
        self._thread.daemon = True  # thread dies with the program
2329 2330
        self._thread.start()

2331 2332 2333 2334 2335 2336
        self._thread2 = threading.Thread(
            target=self.svcErrorReader,
            args=(self._tdeSubProcess.getStdErr(), self._ipcQueue))
        self._thread2.daemon = True  # thread dies with the program
        self._thread2.start()

2337
        # wait for service to start
2338
        for i in range(0, 10):
2339 2340 2341
            time.sleep(1.0)
            # self.procIpcBatch() # don't pump message during start up
            print("_zz_", end="", flush=True)
2342
            if self._status == MainExec.STATUS_RUNNING:
2343
                logger.info("[] TDengine service READY to process requests")
2344 2345
                return  # now we've started
        # TODO: handle this better?
2346
        self.procIpcBatch(20, True) # display output before cronking out, trim to last 20 msgs, force output
2347
        raise RuntimeError("TDengine service did not start successfully")
2348 2349 2350 2351 2352 2353

    def stop(self):
        # can be called from both main thread or signal handler
        print("Terminating TDengine service running as the sub process...")
        if self.isStopped():
            print("Service already stopped")
2354
            return
2355 2356 2357
        if self.isStopping():
            print("Service is already being stopped")
            return
2358 2359 2360 2361
        # Linux will send Control-C generated SIGINT to the TDengine process
        # already, ref:
        # https://unix.stackexchange.com/questions/176235/fork-and-how-signals-are-delivered-to-processes
        if not self._tdeSubProcess:
2362
            raise RuntimeError("sub process object missing")
2363

2364
        self._status = MainExec.STATUS_STOPPING
2365 2366
        retCode = self._tdeSubProcess.stop()
        print("Attempted to stop sub process, got return code: {}".format(retCode))
2367 2368
        if (retCode==-11): # SGV
            logger.error("[[--ERROR--]]: TDengine service SEGV fault (check core file!)")
2369

2370
        if self._tdeSubProcess.isRunning():  # still running
2371 2372
            print("FAILED to stop sub process, it is still running... pid = {}".format(
                    self._tdeSubProcess.getPid()))
2373
        else:
2374 2375 2376
            self._tdeSubProcess = None  # not running any more
            self.join()  # stop the thread, change the status, etc.

2377 2378 2379
    def join(self):
        # TODO: sanity check
        if not self.isStopping():
2380 2381 2382
            raise RuntimeError(
                "Unexpected status when ending svc mgr thread: {}".format(
                    self._status))
2383

2384
        if self._thread:
2385
            self._thread.join()
2386
            self._thread = None
2387
            self._status = MainExec.STATUS_STOPPED
2388 2389 2390
            # STD ERR thread
            self._thread2.join()
            self._thread2 = None
S
Shuduo Sang 已提交
2391
        else:
2392
            print("Joining empty thread, doing nothing")
2393 2394 2395

    def _trimQueue(self, targetSize):
        if targetSize <= 0:
2396
            return  # do nothing
2397
        q = self._ipcQueue
2398
        if (q.qsize() <= targetSize):  # no need to trim
2399 2400 2401 2402
            return

        logger.debug("Triming IPC queue to target size: {}".format(targetSize))
        itemsToTrim = q.qsize() - targetSize
2403
        for i in range(0, itemsToTrim):
2404 2405 2406
            try:
                q.get_nowait()
            except Empty:
2407 2408
                break  # break out of for loop, no more trimming

2409
    TD_READY_MSG = "TDengine is initialized successfully"
S
Shuduo Sang 已提交
2410

2411 2412
    def procIpcBatch(self, trimToTarget=0, forceOutput=False):
        self._trimQueue(trimToTarget)  # trim if necessary
S
Shuduo Sang 已提交
2413 2414
        # Process all the output generated by the underlying sub process,
        # managed by IO thread
2415
        print("<", end="", flush=True)
S
Shuduo Sang 已提交
2416 2417
        while True:
            try:
2418
                line = self._ipcQueue.get_nowait()  # getting output at fast speed
2419
                self._printProgress("_o")
2420 2421 2422
            except Empty:
                # time.sleep(2.3) # wait only if there's no output
                # no more output
2423
                print(".>", end="", flush=True)
S
Shuduo Sang 已提交
2424
                return  # we are done with THIS BATCH
2425
            else:  # got line, printing out
2426 2427 2428 2429 2430 2431 2432
                if forceOutput:
                    logger.info(line)
                else:
                    logger.debug(line)
        print(">", end="", flush=True)

    _ProgressBars = ["--", "//", "||", "\\\\"]
2433

2434 2435
    def _printProgress(self, msg):  # TODO: assuming 2 chars
        print(msg, end="", flush=True)
2436 2437 2438
        pBar = self._ProgressBars[Dice.throw(4)]
        print(pBar, end="", flush=True)
        print('\b\b\b\b', end="", flush=True)
2439

2440 2441 2442
    def svcOutputReader(self, out: IO, queue):
        # Important Reference: https://stackoverflow.com/questions/375427/non-blocking-read-on-a-subprocess-pipe-in-python
        # print("This is the svcOutput Reader...")
2443
        # for line in out :
2444 2445 2446
        for line in iter(out.readline, b''):
            # print("Finished reading a line: {}".format(line))
            # print("Adding item to queue...")
2447 2448 2449 2450 2451
            try:
                line = line.decode("utf-8").rstrip()
            except UnicodeError:
                print("\nNon-UTF8 server output: {}\n".format(line))

2452 2453
            # This might block, and then causing "out" buffer to block
            queue.put(line)
2454 2455
            self._printProgress("_i")

2456 2457
            if self._status == MainExec.STATUS_STARTING:  # we are starting, let's see if we have started
                if line.find(self.TD_READY_MSG) != -1:  # found
S
Steven Li 已提交
2458
                    logger.info("Waiting for the service to become FULLY READY")
2459
                    time.sleep(1.0) # wait for the server to truly start. TODO: remove this
S
Steven Li 已提交
2460 2461
                    logger.info("Service is now FULLY READY")   
                    self._status = MainExec.STATUS_RUNNING                 
2462 2463

            # Trim the queue if necessary: TODO: try this 1 out of 10 times
2464
            self._trimQueue(self.MAX_QUEUE_SIZE * 9 // 10)  # trim to 90% size
2465

2466 2467 2468
            if self.isStopping():  # TODO: use thread status instead
                # WAITING for stopping sub process to finish its outptu
                print("_w", end="", flush=True)
2469 2470

            # queue.put(line)
2471 2472
        # meaning sub process must have died
        print("\nNo more output from IO thread managing TDengine service")
2473 2474
        out.close()

2475 2476 2477 2478
    def svcErrorReader(self, err: IO, queue):
        for line in iter(err.readline, b''):
            print("\nTD Svc STDERR: {}".format(line))

2479 2480

class TdeSubProcess:
2481 2482 2483 2484 2485
    def __init__(self):
        self.subProcess = None

    def getStdOut(self):
        return self.subProcess.stdout
2486

2487 2488 2489
    def getStdErr(self):
        return self.subProcess.stderr

2490
    def isRunning(self):
2491
        return self.subProcess is not None
2492

2493 2494 2495
    def getPid(self):
        return self.subProcess.pid

S
Shuduo Sang 已提交
2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509
    def getBuildPath(self):
        selfPath = os.path.dirname(os.path.realpath(__file__))
        if ("community" in selfPath):
            projPath = selfPath[:selfPath.find("communit")]
        else:
            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):
                    buildPath = root[:len(root) - len("/build/bin")]
                    break
        return buildPath
2510

2511
    def start(self):
2512
        ON_POSIX = 'posix' in sys.builtin_module_names
S
Shuduo Sang 已提交
2513

2514 2515 2516
        taosdPath = self.getBuildPath() + "/build/bin/taosd"
        cfgPath = self.getBuildPath() + "/test/cfg"

2517 2518 2519
        # Delete the log files
        logPath = self.getBuildPath() + "/test/log"
        # ref: https://stackoverflow.com/questions/1995373/deleting-all-files-in-a-directory-with-python/1995397
2520 2521 2522 2523
        # filelist = [ f for f in os.listdir(logPath) ] # if f.endswith(".bak") ]
        # for f in filelist:
        #     filePath = os.path.join(logPath, f)
        #     print("Removing log file: {}".format(filePath))
2524 2525 2526 2527 2528 2529
        #     os.remove(filePath)        
        if os.path.exists(logPath):
            logPathSaved = logPath + "_" + time.strftime('%Y-%m-%d-%H-%M-%S')
            logger.info("Saving old log files to: {}".format(logPathSaved))
            os.rename(logPath, logPathSaved)
        # os.mkdir(logPath) # recreate, no need actually, TDengine will auto-create with proper perms
2530
            
S
Shuduo Sang 已提交
2531
        svcCmd = [taosdPath, '-c', cfgPath]
2532
        # svcCmdSingle = "{} -c {}".format(taosdPath, cfgPath)
2533
        # svcCmd = ['vmstat', '1']
S
Shuduo Sang 已提交
2534
        if self.subProcess:  # already there
2535 2536
            raise RuntimeError("Corrupt process state")

S
Steven Li 已提交
2537
        # print("Starting service: {}".format(svcCmd))
2538
        self.subProcess = subprocess.Popen(
2539 2540
            svcCmd, shell=False,
            # svcCmdSingle, shell=True, # capture core dump?
S
Shuduo Sang 已提交
2541
            stdout=subprocess.PIPE,
2542
            stderr=subprocess.PIPE,
2543
            # bufsize=1, # not supported in binary mode
S
Steven Li 已提交
2544 2545
            close_fds=ON_POSIX
            )  # had text=True, which interferred with reading EOF
2546

2547 2548 2549
    def stop(self):
        if not self.subProcess:
            print("Sub process already stopped")
2550
            return -1
2551

2552
        retCode = self.subProcess.poll() # contains real sub process return code
S
Shuduo Sang 已提交
2553
        if retCode:  # valid return code, process ended
2554
            self.subProcess = None
S
Shuduo Sang 已提交
2555 2556
        else:  # process still alive, let's interrupt it
            print(
2557
                "Sub process is running, sending SIG_INT and waiting for it to terminate...")
S
Shuduo Sang 已提交
2558 2559 2560 2561
            # sub process should end, then IPC queue should end, causing IO
            # thread to end
            self.subProcess.send_signal(signal.SIGINT)
            try:
2562
                self.subProcess.wait(10)
2563
                retCode = self.subProcess.returncode
2564 2565
            except subprocess.TimeoutExpired as err:
                print("Time out waiting for TDengine service process to exit")
2566
                retCode = -3
2567
            else:
2568
                print("TDengine service process terminated successfully from SIG_INT")
2569
                retCode = -4
2570
                self.subProcess = None
2571
        return retCode
2572

2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600
class ThreadStacks: # stack info for all threads
    def __init__(self):
        self._allStacks = {}
        allFrames = sys._current_frames()
        for th in threading.enumerate():                        
            stack = traceback.extract_stack(allFrames[th.ident])     
            self._allStacks[th.native_id] = stack

    def print(self, filteredEndName = None, filterInternal = False):
        for thNid, stack in self._allStacks.items(): # for each thread            
            lastFrame = stack[-1]
            if filteredEndName: # we need to filter out stacks that match this name                
                if lastFrame.name == filteredEndName : # end did not match
                    continue
            if filterInternal:
                if lastFrame.name in ['wait', 'invoke_excepthook', 
                    '_wait', # The Barrier exception
                    'svcOutputReader', # the svcMgr thread
                    '__init__']: # the thread that extracted the stack
                    continue # ignore
            # Now print
            print("\n<----- Thread Info for ID: {}".format(thNid))
            for frame in stack:
                # print(frame)
                print("File {filename}, line {lineno}, in {name}".format(
                    filename=frame.filename, lineno=frame.lineno, name=frame.name))
                print("    {}".format(frame.line))
            print("-----> End of Thread Info\n")
S
Shuduo Sang 已提交
2601

2602 2603 2604
class ClientManager:
    def __init__(self):
        print("Starting service manager")
2605 2606
        # signal.signal(signal.SIGTERM, self.sigIntHandler)
        # signal.signal(signal.SIGINT, self.sigIntHandler)
2607

2608
        self._status = MainExec.STATUS_RUNNING
2609 2610
        self.tc = None

2611 2612
        self.inSigHandler = False

2613
    def sigIntHandler(self, signalNumber, frame):
2614
        if self._status != MainExec.STATUS_RUNNING:
2615 2616 2617
            print("Repeated SIGINT received, forced exit...")
            # return  # do nothing if it's already not running
            sys.exit(-1)
2618
        self._status = MainExec.STATUS_STOPPING  # immediately set our status
2619

2620
        print("ClientManager: Terminating program...")
2621 2622
        self.tc.requestToStop()

2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663
    def _doMenu(self):
        choice = ""
        while True:
            print("\nInterrupting Client Program, Choose an Action: ")
            print("1: Resume")
            print("2: Terminate")
            print("3: Show Threads")
            # Remember to update the if range below
            # print("Enter Choice: ", end="", flush=True)
            while choice == "":
                choice = input("Enter Choice: ")
                if choice != "":
                    break  # done with reading repeated input
            if choice in ["1", "2", "3"]:
                break  # we are done with whole method
            print("Invalid choice, please try again.")
            choice = ""  # reset
        return choice

    def sigUsrHandler(self, signalNumber, frame):
        print("Interrupting main thread execution upon SIGUSR1")
        if self.inSigHandler:  # already
            print("Ignoring repeated SIG_USR1...")
            return  # do nothing if it's already not running
        self.inSigHandler = True

        choice = self._doMenu()
        if choice == "1":
            print("Resuming execution...")
            time.sleep(1.0)
        elif choice == "2":
            print("Not implemented yet")
            time.sleep(1.0)
        elif choice == "3":
            ts = ThreadStacks()
            ts.print()
        else:
            raise RuntimeError("Invalid menu choice: {}".format(choice))

        self.inSigHandler = False

S
Shuduo Sang 已提交
2664
    def _printLastNumbers(self):  # to verify data durability
2665 2666
        dbManager = DbManager(resetDb=False)
        dbc = dbManager.getDbConn()
2667
        if dbc.query("show databases") <= 1:  # no database (we have a default called "log")
2668
            return
2669
        dbc.execute("use db")
S
Shuduo Sang 已提交
2670
        if dbc.query("show tables") == 0:  # no tables
S
Steven Li 已提交
2671
            return
2672

S
Shuduo Sang 已提交
2673
        sTbName = dbManager.getFixedSuperTableName()
2674 2675

        # get all regular tables
S
Shuduo Sang 已提交
2676 2677
        # TODO: analyze result set later
        dbc.query("select TBNAME from db.{}".format(sTbName))
2678 2679 2680
        rTables = dbc.getQueryResult()

        bList = TaskExecutor.BoundedList()
S
Shuduo Sang 已提交
2681
        for rTbName in rTables:  # regular tables
2682 2683
            dbc.query("select speed from db.{}".format(rTbName[0]))
            numbers = dbc.getQueryResult()
S
Shuduo Sang 已提交
2684
            for row in numbers:
2685 2686 2687 2688 2689 2690
                # print("<{}>".format(n), end="", flush=True)
                bList.add(row[0])

        print("Top numbers in DB right now: {}".format(bList))
        print("TDengine client execution is about to start in 2 seconds...")
        time.sleep(2.0)
S
Shuduo Sang 已提交
2691
        dbManager = None  # release?
2692 2693 2694 2695

    def prepare(self):
        self._printLastNumbers()

2696
    def run(self, svcMgr):    
2697 2698
        self._printLastNumbers()

S
Shuduo Sang 已提交
2699
        dbManager = DbManager()  # Regular function
2700
        thPool = ThreadPool(gConfig.num_threads, gConfig.max_steps)
2701
        self.tc = ThreadCoordinator(thPool, dbManager)
2702
        
2703
        self.tc.run()
S
Steven Li 已提交
2704 2705
        # print("exec stats: {}".format(self.tc.getExecStats()))
        # print("TC failed = {}".format(self.tc.isFailed()))
2706
        if svcMgr: # gConfig.auto_start_service:
2707
            svcMgr.stopTaosService()
2708 2709
        # Print exec status, etc., AFTER showing messages from the server
        self.conclude()
S
Steven Li 已提交
2710
        # print("TC failed (2) = {}".format(self.tc.isFailed()))
S
Shuduo Sang 已提交
2711 2712
        # Linux return code: ref https://shapeshed.com/unix-exit-codes/
        return 1 if self.tc.isFailed() else 0
2713 2714 2715

    def conclude(self):
        self.tc.printStats()
S
Shuduo Sang 已提交
2716
        self.tc.getDbManager().cleanUp()
2717 2718

class MainExec:
2719 2720 2721
    STATUS_STARTING = 1
    STATUS_RUNNING = 2
    STATUS_STOPPING = 3
2722
    STATUS_STOPPED = 4
2723

2724 2725 2726
    def __init__(self):        
        self._clientMgr = None
        self._svcMgr = None
2727

2728 2729 2730
        signal.signal(signal.SIGTERM, self.sigIntHandler)
        signal.signal(signal.SIGINT,  self.sigIntHandler)
        signal.signal(signal.SIGUSR1, self.sigUsrHandler)  # different handler!
2731

2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744
    def sigUsrHandler(self, signalNumber, frame):
        if self._clientMgr:
            self._clientMgr.sigUsrHandler(signalNumber, frame)
        elif self._svcMgr: # Only if no client mgr, we are running alone
            self._svcMgr.sigUsrHandler(signalNumber, frame)
        
    def sigIntHandler(self, signalNumber, frame):
        if self._svcMgr:
            self._svcMgr.sigIntHandler(signalNumber, frame)
        if self._clientMgr:
            self._clientMgr.sigIntHandler(signalNumber, frame)

    def runClient(self):
2745
        global gSvcMgr
2746 2747
        if gConfig.auto_start_service:
            self._svcMgr = SvcManager()
2748
            gSvcMgr = self._svcMgr # hack alert
2749 2750 2751
            self._svcMgr.startTaosService() # we start, don't run
        
        self._clientMgr = ClientManager()
2752 2753 2754 2755 2756 2757 2758
        ret = None
        try: 
            ret = self._clientMgr.run(self._svcMgr) # stop TAOS service inside
        except requests.exceptions.ConnectionError as err:
            logger.warning("Failed to open REST connection to DB")
            # don't raise
        return ret
2759 2760

    def runService(self):
2761
        global gSvcMgr
2762
        self._svcMgr = SvcManager()
2763 2764
        gSvcMgr = self._svcMgr # save it in a global variable TODO: hack alert

2765
        self._svcMgr.run() # run to some end state
2766 2767
        self._svcMgr = None 
        gSvcMgr = None        
2768 2769

    def runTemp(self):  # for debugging purposes
2770 2771
        # # Hack to exercise reading from disk, imcreasing coverage. TODO: fix
        # dbc = dbState.getDbConn()
S
Shuduo Sang 已提交
2772
        # sTbName = dbState.getFixedSuperTableName()
2773 2774
        # dbc.execute("create database if not exists db")
        # if not dbState.getState().equals(StateEmpty()):
S
Shuduo Sang 已提交
2775
        #     dbc.execute("use db")
2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786

        # rTables = None
        # try: # the super table may not exist
        #     sql = "select TBNAME from db.{}".format(sTbName)
        #     logger.info("Finding out tables in super table: {}".format(sql))
        #     dbc.query(sql) # TODO: analyze result set later
        #     logger.info("Fetching result")
        #     rTables = dbc.getQueryResult()
        #     logger.info("Result: {}".format(rTables))
        # except taos.error.ProgrammingError as err:
        #     logger.info("Initial Super table OPS error: {}".format(err))
S
Shuduo Sang 已提交
2787

2788 2789 2790 2791 2792 2793 2794 2795
        # # sys.exit()
        # if ( not rTables == None):
        #     # print("rTables[0] = {}, type = {}".format(rTables[0], type(rTables[0])))
        #     try:
        #         for rTbName in rTables : # regular tables
        #             ds = dbState
        #             logger.info("Inserting into table: {}".format(rTbName[0]))
        #             sql = "insert into db.{} values ('{}', {});".format(
S
Shuduo Sang 已提交
2796
        #                 rTbName[0],
2797 2798
        #                 ds.getNextTick(), ds.getNextInt())
        #             dbc.execute(sql)
S
Shuduo Sang 已提交
2799
        #         for rTbName in rTables : # regular tables
2800
        #             dbc.query("select * from db.{}".format(rTbName[0])) # TODO: check success failure
S
Shuduo Sang 已提交
2801
        #         logger.info("Initial READING operation is successful")
2802
        #     except taos.error.ProgrammingError as err:
S
Shuduo Sang 已提交
2803 2804
        #         logger.info("Initial WRITE/READ error: {}".format(err))

2805 2806 2807
        # Sandbox testing code
        # dbc = dbState.getDbConn()
        # while True:
S
Shuduo Sang 已提交
2808
        #     rows = dbc.query("show databases")
2809
        #     print("Rows: {}, time={}".format(rows, time.time()))
S
Shuduo Sang 已提交
2810 2811
        return

S
Steven Li 已提交
2812

2813
def main():
S
Shuduo Sang 已提交
2814 2815
    # Super cool Python argument library:
    # https://docs.python.org/3/library/argparse.html
2816 2817 2818 2819 2820 2821 2822 2823 2824
    parser = argparse.ArgumentParser(
        formatter_class=argparse.RawDescriptionHelpFormatter,
        description=textwrap.dedent('''\
            TDengine Auto Crash Generator (PLEASE NOTICE the Prerequisites Below)
            ---------------------------------------------------------------------
            1. You build TDengine in the top level ./build directory, as described in offical docs
            2. You run the server there before this script: ./build/bin/taosd -c test/cfg

            '''))
2825

2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846
    # parser.add_argument('-a', '--auto-start-service', action='store_true',                        
    #                     help='Automatically start/stop the TDengine service (default: false)')
    # parser.add_argument('-c', '--connector-type', action='store', default='native', type=str,
    #                     help='Connector type to use: native, rest, or mixed (default: 10)')
    # parser.add_argument('-d', '--debug', action='store_true',                        
    #                     help='Turn on DEBUG mode for more logging (default: false)')
    # parser.add_argument('-e', '--run-tdengine', action='store_true',                        
    #                     help='Run TDengine service in foreground (default: false)')
    # parser.add_argument('-l', '--larger-data', action='store_true',                        
    #                     help='Write larger amount of data during write operations (default: false)')
    # parser.add_argument('-p', '--per-thread-db-connection', action='store_true',                        
    #                     help='Use a single shared db connection (default: false)')
    # parser.add_argument('-r', '--record-ops', action='store_true',                        
    #                     help='Use a pair of always-fsynced fils to record operations performing + performed, for power-off tests (default: false)')                    
    # parser.add_argument('-s', '--max-steps', action='store', default=1000, type=int,
    #                     help='Maximum number of steps to run (default: 100)')
    # parser.add_argument('-t', '--num-threads', action='store', default=5, type=int,
    #                     help='Number of threads to run (default: 10)')
    # parser.add_argument('-x', '--continue-on-exception', action='store_true',                        
    #                     help='Continue execution after encountering unexpected/disallowed errors/exceptions (default: false)')                        

S
Shuduo Sang 已提交
2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897
    parser.add_argument(
        '-a',
        '--auto-start-service',
        action='store_true',
        help='Automatically start/stop the TDengine service (default: false)')
    parser.add_argument(
        '-c',
        '--connector-type',
        action='store',
        default='native',
        type=str,
        help='Connector type to use: native, rest, or mixed (default: 10)')
    parser.add_argument(
        '-d',
        '--debug',
        action='store_true',
        help='Turn on DEBUG mode for more logging (default: false)')
    parser.add_argument(
        '-e',
        '--run-tdengine',
        action='store_true',
        help='Run TDengine service in foreground (default: false)')
    parser.add_argument(
        '-l',
        '--larger-data',
        action='store_true',
        help='Write larger amount of data during write operations (default: false)')
    parser.add_argument(
        '-p',
        '--per-thread-db-connection',
        action='store_true',
        help='Use a single shared db connection (default: false)')
    parser.add_argument(
        '-r',
        '--record-ops',
        action='store_true',
        help='Use a pair of always-fsynced fils to record operations performing + performed, for power-off tests (default: false)')
    parser.add_argument(
        '-s',
        '--max-steps',
        action='store',
        default=1000,
        type=int,
        help='Maximum number of steps to run (default: 100)')
    parser.add_argument(
        '-t',
        '--num-threads',
        action='store',
        default=5,
        type=int,
        help='Number of threads to run (default: 10)')
2898 2899 2900 2901 2902
    parser.add_argument(
        '-x',
        '--continue-on-exception',
        action='store_true',
        help='Continue execution after encountering unexpected/disallowed errors/exceptions (default: false)')
2903

2904
    global gConfig
2905
    gConfig = parser.parse_args()
2906

2907
    # Logging Stuff
2908
    global logger
S
Shuduo Sang 已提交
2909 2910
    _logger = logging.getLogger('CrashGen')  # real logger
    _logger.addFilter(LoggingFilter())
2911 2912 2913
    ch = logging.StreamHandler()
    _logger.addHandler(ch)

S
Shuduo Sang 已提交
2914 2915
    # Logging adapter, to be used as a logger
    logger = MyLoggingAdapter(_logger, [])
2916

S
Shuduo Sang 已提交
2917 2918
    if (gConfig.debug):
        logger.setLevel(logging.DEBUG)  # default seems to be INFO
S
Steven Li 已提交
2919 2920
    else:
        logger.setLevel(logging.INFO)
S
Shuduo Sang 已提交
2921

2922 2923
    Dice.seed(0)  # initial seeding of dice

2924
    # Run server or client
2925
    mExec = MainExec()
S
Shuduo Sang 已提交
2926
    if gConfig.run_tdengine:  # run server
2927
        mExec.runService()
S
Shuduo Sang 已提交
2928
    else:
2929
        return mExec.runClient()
2930

S
Shuduo Sang 已提交
2931

2932
if __name__ == "__main__":
S
Steven Li 已提交
2933 2934 2935
    exitCode = main()
    # print("Exiting with code: {}".format(exitCode))
    sys.exit(exitCode)