retentionpolicy.py 4.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
###################################################################
#           Copyright (c) 2016 by TAOS Technologies, Inc.
#                     All rights reserved.
#
#  This file is proprietary and confidential to TAOS Technologies.
#  No part of this file may be reproduced, stored, transmitted,
#  disclosed or used in any form or by any means other than as
#  expressly provided by the written permission from Jianhui Tao
#
###################################################################

# -*- coding: utf-8 -*-

import sys
import os
import datetime
sys.path.insert(0, os.getcwd())
import taos
from util.log import *
from util.cases import *
from util.sql import *
from util.dnodes import *


class TDTestRetetion:
    def init(self):
        self.queryRows=0
        tdLog.debug("start to execute %s" % __file__)
        tdLog.info("prepare cluster")
        tdDnodes.init("")
        tdDnodes.setTestCluster(False)
        tdDnodes.setValgrind(False)
        tdDnodes.stopAll()
        tdDnodes.deploy(1)
        tdDnodes.start(1)
        print(tdDnodes.getDnodesRootDir())
        self.conn = taos.connect(config=tdDnodes.getSimCfgPath())
        tdSql.init(self.conn.cursor())
        tdSql.execute('reset query cache')
    def checkRows(self, expectRows,sql):
        if self.queryRows == expectRows:
            tdLog.info("sql:%s, queryRows:%d == expect:%d" % (sql, self.queryRows, expectRows))
        else:
            caller = inspect.getframeinfo(inspect.stack()[1][0])
            args = (caller.filename, caller.lineno, sql, self.queryRows, expectRows)
L
liuyq-617 已提交
46
            os.system("sudo timedatectl set-ntp true")
L
liuyq-617 已提交
47 48
            os.system("date -s '%s'"%(datetime.datetime.now()+datetime.timedelta(hours=1)))
            time.sleep(5)
49 50 51 52 53 54 55 56 57
            tdLog.exit("%s(%d) failed: sql:%s, queryRows:%d != expect:%d" % args)

    def run(self):
        
        tdLog.info("=============== step1")
        tdSql.execute('create database test keep 3 days 1;')
        tdSql.execute('use test;')
        tdSql.execute('create table test(ts timestamp,i int);')

L
liuyq-617 已提交
58
        cmd = 'insert into test values(now-2d,1)(now-1d,2)(now,3)(now+1d,4);'
59 60 61 62 63 64 65
        tdLog.info(cmd)
        tdSql.execute(cmd)
        tdSql.query('select * from test')
        tdSql.checkRows(4)

        tdLog.info("=============== step2")
        tdDnodes.stop(1)
L
liuyq-617 已提交
66
        os.system("sudo timedatectl set-ntp false")
L
liuyq-617 已提交
67
        os.system("date -s '%s'"%(datetime.datetime.now()+datetime.timedelta(hours=48)))
68
        tdDnodes.start(1)
L
liuyq-617 已提交
69 70 71 72
        cmd = 'insert into test values(now,5);'
        tdDnodes.stop(1)
        tdDnodes.start(1)
        
73
        tdLog.info(cmd)
74
        ttime = datetime.datetime.now()
75
        tdSql.execute(cmd)
L
liuyq-617 已提交
76
        self.queryRows=tdSql.query('select * from test')
77
        self.checkRows(3,cmd)
78 79
        tdLog.info("=============== step3")
        tdDnodes.stop(1)
L
liuyq-617 已提交
80
        os.system("date -s '%s'"%(datetime.datetime.now()+datetime.timedelta(hours=48)))
81 82 83
        tdDnodes.start(1)
        tdLog.info(cmd)
        tdSql.execute(cmd)
L
liuyq-617 已提交
84 85 86 87 88 89 90 91
        self.queryRows=tdSql.query('select * from test')
        if self.queryRows==4:
            self.checkRows(4,cmd)
            return 0
        cmd = 'insert into test values(now-1d,6);'
        tdLog.info(cmd)
        tdSql.execute(cmd)
        self.queryRows=tdSql.query('select * from test')
92
        self.checkRows(3,cmd)
93 94 95
        tdLog.info("=============== step4")
        tdDnodes.stop(1)
        tdDnodes.start(1)
L
liuyq-617 已提交
96
        cmd = 'insert into test values(now,7);'
97 98
        tdLog.info(cmd)
        tdSql.execute(cmd)
L
liuyq-617 已提交
99
        self.queryRows=tdSql.query('select * from test')
100
        self.checkRows(4,cmd)
101 102 103 104 105

        tdLog.info("=============== step5")
        tdDnodes.stop(1)
        tdDnodes.start(1)
        cmd='select * from test where ts > now-1d'
L
liuyq-617 已提交
106
        self.queryRows=tdSql.query('select * from test where ts > now-1d')
L
liuyq-617 已提交
107
        self.checkRows(2,cmd)
108

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
        tdLog.info("=============== step6")
        tdDnodes.stop(1)
        os.system("date -s '%s'"%(ttime + datetime.timedelta(seconds=(72*60*60-7))))
        tdDnodes.start(1)
        while datetime.datetime.now() < (ttime + datetime.timedelta(seconds=(72*60*60-1))):
            time.sleep(0.001)
        cmd = 'select * from test'
        self.queryRows=tdSql.query(cmd)
        self.checkRows(4,cmd)
        while datetime.datetime.now() < (ttime + datetime.timedelta(hours=72)):
            time.sleep(0.001)
        cmd = 'select * from test'
        self.queryRows=tdSql.query(cmd)
        self.checkRows(3,cmd)

124
    def stop(self):
L
liuyq-617 已提交
125
        os.system("sudo timedatectl set-ntp true")
L
liuyq-617 已提交
126 127
        os.system("date -s '%s'"%(datetime.datetime.now()+datetime.timedelta(hours=1)))
        time.sleep(5)
128 129 130 131 132 133 134 135 136
        tdSql.close()
        tdLog.success("%s successfully executed" % __file__)


clients = TDTestRetetion()
clients.init()
clients.run()
clients.stop()