auto_crash_gen_script.py 9.9 KB
Newer Older
W
update  
wenzhouwww@live.cn 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
###################################################################
#           Copyright (c) 2020 by TAOS Technologies, Inc.
#                     All rights reserved.
#
#  This file is proprietary and confidential to TAOS Technologies.
#  No part of this file may be reproduced, stored, transmitted,
#  disclosed or used in any form or by any means other than as
#  expressly provided by the written permission from Jianhui Tao
#
###################################################################

# -*- coding: utf-8 -*-
import os ,sys
import random
import argparse
import subprocess
import time

# set path about run instance

W
update  
wenzhouwww@live.cn 已提交
21
core_path = '/home/coredump/'
W
update  
wenzhouwww@live.cn 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
base_dir = os.path.dirname(os.path.realpath(__file__))
if base_dir.find("community")>0:
    repo = "community"
elif base_dir.find("TDengine")>0:
    repo = "TDengine"
else:
    repo ="TDengine"
print("base_dir:",base_dir)
home_dir = base_dir[:base_dir.find(repo)]
print("home_dir:",home_dir)
run_dir = os.path.join(home_dir,'run_dir')
run_dir = os.path.abspath(run_dir)
print("run dir is set at :",run_dir)
if not os.path.exists(run_dir):
    os.mkdir(run_dir)
run_log_file = run_dir+'/crash_gen_run.log'
crash_gen_cmds_file = os.path.join(run_dir, 'crash_gen_cmds.sh')
exit_status_logs =  os.path.join(run_dir, 'crash_exit.log')

def get_path():
    buildPath=''
    selfPath = os.path.dirname(os.path.realpath(__file__))
    if ("community" in selfPath):
        projPath = selfPath[:selfPath.find("community")]
    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

57
  
W
update  
wenzhouwww@live.cn 已提交
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
def random_args(args_list):
    nums_args_list = ["--max-dbs","--num-replicas","--num-dnodes","--max-steps","--num-threads",] # record int type arguments
    bools_args_list = ["--auto-start-service" , "--debug","--run-tdengine","--ignore-errors","--track-memory-leaks","--larger-data","--mix-oos-data","--dynamic-db-table-names",
    "--per-thread-db-connection","--record-ops","--verify-data","--use-shadow-db","--continue-on-exception"
    ]  # record bool type arguments
    strs_args_list = ["--connector-type"]  # record str type arguments

    args_list["--auto-start-service"]= True
    args_list["--continue-on-exception"]=True
    # connect_types=['native','rest','mixed'] # restful interface has change ,we should trans dbnames to connection or change sql such as "db.test"
    connect_types=['native']
    # args_list["--connector-type"]=connect_types[random.randint(0,2)]
    args_list["--connector-type"]= connect_types[0]
    args_list["--max-dbs"]= random.randint(1,10)
    
    dnodes = [1,3] # set single dnodes;
    
    args_list["--num-dnodes"]= random.sample(dnodes,1)[0]
    args_list["--num-replicas"]= random.randint(1,args_list["--num-dnodes"])
    args_list["--debug"]=False
    args_list["--per-thread-db-connection"]=True
    args_list["--track-memory-leaks"]=False
80 81 82 83 84 85

    num = random.randint(1, 10)   # set --track-memory-leaks randomly
    if num > 8 :
        args_list["--track-memory-leaks"]=True
    

W
update  
wenzhouwww@live.cn 已提交
86
    args_list["--max-steps"]=random.randint(300,500)
W
update  
wenzhouwww@live.cn 已提交
87
    
88
    threads = [32,64,128]
W
update  
wenzhouwww@live.cn 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
    args_list["--num-threads"]=random.sample(threads,1)[0] #$ debug
    args_list["--ignore-errors"]=[]   ## can add error codes for detail

    
    args_list["--run-tdengine"]= False
    args_list["--use-shadow-db"]= False
    args_list["--dynamic-db-table-names"]= False
    args_list["--verify-data"]= False
    args_list["--record-ops"] = False

    for key in bools_args_list:
        set_bool_value = [True,False]
        if key == "--auto-start-service" :
            continue
        elif key =="--run-tdengine":
            continue
        elif key == "--ignore-errors":
            continue
        elif key == "--debug":
            continue
        elif key == "--per-thread-db-connection":
            continue
        elif key == "--continue-on-exception":
            continue
        elif key == "--use-shadow-db":
            continue
        elif key =="--track-memory-leaks":
            continue
        elif key == "--dynamic-db-table-names":
            continue
        elif key == "--verify-data":
            continue
        elif key == "--record-ops":
            continue
        else:
            args_list[key]=set_bool_value[random.randint(0,1)]
    return args_list

def limits(args_list):
    if args_list["--use-shadow-db"]==True:
        if args_list["--max-dbs"] > 1:
            print("Cannot combine use-shadow-db with max-dbs of more than 1 ,set max-dbs=1")
            args_list["--max-dbs"]=1
        else:
            pass

    elif args_list["--num-replicas"]==0:
        print(" make sure num-replicas is at least 1 ")
        args_list["--num-replicas"]=1
    elif args_list["--num-replicas"]==1:
        pass

    elif args_list["--num-replicas"]>1:
        if not args_list["--auto-start-service"]:
            print("it should be deployed by crash_gen auto-start-service for multi replicas")
            
    else:
        pass
         
    return  args_list

def get_cmds(args_list):
    build_path = get_path()
    if repo == "community":
        crash_gen_path = build_path[:-5]+"community/tests/pytest/"
    elif repo == "TDengine":
        crash_gen_path = build_path[:-5]+"/tests/pytest/"
    else:
        pass

    bools_args_list = ["--auto-start-service" , "--debug","--run-tdengine","--ignore-errors","--track-memory-leaks","--larger-data","--mix-oos-data","--dynamic-db-table-names",
    "--per-thread-db-connection","--record-ops","--verify-data","--use-shadow-db","--continue-on-exception"]
    arguments = ""
    for k ,v in args_list.items():
        if k == "--ignore-errors":
            if v:
                arguments+=(k+"="+str(v)+" ")
            else:
                arguments+=""
        elif  k in bools_args_list and v==True:
            arguments+=(k+" ")
        elif k in bools_args_list and v==False:
            arguments+=""
        else:
            arguments+=(k+"="+str(v)+" ")
    crash_gen_cmd = 'cd %s && ./crash_gen.sh %s '%(crash_gen_path ,arguments)
    return crash_gen_cmd

def run_crash_gen(crash_cmds,result_file):
    os.system('echo "%s">>%s'%(crash_cmds,crash_gen_cmds_file))
W
update  
wenzhouwww@live.cn 已提交
179
    os.system("cp %s %s"%(crash_gen_cmds_file, core_path))
W
update  
wenzhouwww@live.cn 已提交
180 181 182 183 184
    os.system("%s>>%s"%(crash_cmds,result_file))

def check_status(result_file):
    run_code = subprocess.Popen("tail -n 50 %s"%result_file, shell=True, stdout=subprocess.PIPE,stderr=subprocess.STDOUT).stdout.read().decode("utf-8")
    os.system("tail -n 50 %s>>%s"%(result_file,exit_status_logs))
185 186 187
    mem_status = check_memory(run_dir)
    if mem_status >0:
        return mem_status
W
update  
wenzhouwww@live.cn 已提交
188 189 190 191 192 193 194
    if "Crash_Gen is now exiting with status code: 1" in run_code:
        return 1
    elif "Crash_Gen is now exiting with status code: 0" in run_code:
        return 0
    else:
        return 2 

195 196 197 198 199 200 201 202 203 204 205 206 207
def check_memory(run_dir):
    '''
    invalid read, invalid write
    '''
    mem_report_path = os.path.join(run_dir , "reporter")
    if not os.path.exists(mem_report_path):
        os.mkdir(mem_report_path)
    status = 0
    stderr_files = subprocess.Popen("find %s -name \"stderr.log\" "%run_dir , shell=True, stdout=subprocess.PIPE,stderr=subprocess.STDOUT).stdout.read().decode("utf-8")
    stderr_list = stderr_files.split("\n")[:-1]
    for stderr_file in stderr_list:
        print(stderr_file)
        grep_res = subprocess.Popen("grep -i 'Invalid read' %s "%stderr_file , shell=True, stdout=subprocess.PIPE,stderr=subprocess.STDOUT).stdout.read().decode("utf-8")
208 209 210
        dnode_name = stderr_file.split("/")[-3]
        back_path = os.path.join(core_path,"reporter",dnode_name)
        if not os.path.exists(back_path):
W
update  
wenzhouwww@live.cn 已提交
211
            os.system("mkdir -p %s"%back_path)
212

213
        if grep_res:
214
            os.system("cp %s %s"%(stderr_file , back_path))
215 216 217 218 219 220
            status = 4
            break
        
        grep_res = subprocess.Popen("grep -i 'Invalid write' %s "%stderr_file , shell=True, stdout=subprocess.PIPE,stderr=subprocess.STDOUT).stdout.read().decode("utf-8")
        if grep_res:
            status = 4
221
            os.system("cp %s %s"%(stderr_file , back_path))
222 223 224
            break
    return status

W
update  
wenzhouwww@live.cn 已提交
225 226 227 228 229 230 231
def main():
    args_list = {"--auto-start-service":False ,"--max-dbs":0,"--connector-type":"native","--debug":False,"--run-tdengine":False,"--ignore-errors":[],
        "--num-replicas":1 ,"--track-memory-leaks":False , "--larger-data":False, "--mix-oos-data":False, "--dynamic-db-table-names":False,"--num-dnodes":1,
        "--per-thread-db-connection":False , "--record-ops":False , "--max-steps":100, "--num-threads":10, "--verify-data":False,"--use-shadow-db":False , 
        "--continue-on-exception":False }

    build_path = get_path()
232
    os.system("pip3 install taospy")
W
update  
wenzhouwww@live.cn 已提交
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
    if repo =="community":
        crash_gen_path = build_path[:-5]+"community/tests/pytest/"
    elif repo =="TDengine":
        crash_gen_path = build_path[:-5]+"/tests/pytest/"
    else:
        pass
    print(crash_gen_path)
    if os.path.exists(crash_gen_path+"crash_gen.sh"):
        print(" make sure crash_gen.sh is ready")
    else:
        print( " crash_gen.sh is not exists ")
        sys.exit(1)
    
    # set value args

    args = random_args(args_list)
    args = limits(args)
    crash_cmds = get_cmds(args)
    crash_cmds= crash_cmds+"--set-path="+"%s"%run_dir
    # clean run_dir
    os.system('rm -rf %s'%run_dir )
    if not os.path.exists(run_dir):
        os.mkdir(run_dir)
    print(crash_cmds)
    run_crash_gen(crash_cmds,run_log_file)
    status = check_status(run_log_file)
259
    
W
update  
wenzhouwww@live.cn 已提交
260
    print("exit status : ", status)
261 262
    if status ==4:
        print('======== crash_gen found memory bugs at reporter ========')
W
update  
wenzhouwww@live.cn 已提交
263 264 265 266 267 268 269 270 271 272 273
    if status >0:
        print('======== crash_gen run failed and not exit as expected ========')
    else:
        print('======== crash_gen run sucess and exit as expected ========')
    exit(status)
    

if __name__ == '__main__':
    main()