2025-07-16 06:42:16 +00:00
import time
import math
import random
2025-09-01 11:07:39 +00:00
from new_test_framework . utils import (
tdLog ,
tdSql ,
tdStream ,
streamUtil ,
StreamTableType ,
StreamTable ,
cluster ,
)
2025-07-16 06:42:16 +00:00
from random import randint
import os
import subprocess
2025-09-01 11:07:39 +00:00
class TestStreamSameName :
2025-07-16 06:42:16 +00:00
caseName = " "
currentDir = os . path . dirname ( os . path . abspath ( __file__ ) )
runAll = False
dbname = " test1 "
trigTbname = " "
calcTbname = " "
outTbname = " "
streamName = " 123456 "
resultIdx = " "
sliding = 1
subTblNum = 3
tblRowNum = 10
tableList = [ ]
2025-09-01 11:07:39 +00:00
2025-07-16 06:42:16 +00:00
def setup_class ( cls ) :
tdLog . debug ( f " start to execute { __file__ } " )
2025-09-01 11:07:39 +00:00
def test_stream_same_name ( self ) :
""" Stream: check same name
Test stream with duplicate names .
2025-07-16 06:42:16 +00:00
Catalog :
2025-09-01 11:07:39 +00:00
- Streams : Stream
2025-07-16 06:42:16 +00:00
Since : v3 .3 .3 .7
Labels : common , ci
Jira : None
History :
- 2025 - 7 - 8 lvze Created
"""
tdStream . dropAllStreamsAndDbs ( )
self . prepareData ( )
self . createSnodeTest ( )
2025-09-01 11:07:39 +00:00
2025-07-16 06:42:16 +00:00
self . createOneStream ( " testst " )
2025-09-01 11:07:39 +00:00
sql = " create stream `testst` sliding(1s) from st1 partition by tbname stream_options(fill_history( ' 2025-01-01 00:00:00 ' )) into `testout` as select cts, cint, %% tbname from st1 where cint > 5 and tint > 0 and %% tbname like ' %% 2 ' order by cts; "
2025-07-16 06:42:16 +00:00
tdLog . info ( f " create same name stream: { sql } " )
try :
tdSql . execute ( sql )
except Exception as e :
2025-09-01 11:07:39 +00:00
if " Stream already exists " in str ( e ) :
tdLog . info ( f " cant create same name stream " )
else :
raise Exception ( f " cant create same name stream ,but create success " )
2025-07-16 06:42:16 +00:00
# error case
stream_name = " ashdjfklhgt49hg84g89j4hjq904j9m9vm94jg9j4gj94jg90qj490j2390hr823h8bnbuhu4h8gh48gj834g894j0g4j30gj0g4jg2ij9t0j2498gn498gn894ng9843ng894gk9j4e9gj49gh9jg90qj490j2390hr823hfj38jg84gh84h89gh48h8 "
# create stream
self . createOneStream ( stream_name )
# recreate
sql = (
" create stream ` %s ` sliding(1s) from st1 partition by tbname "
" stream_options(fill_history( ' 2025-01-01 00:00:00 ' )) into ` %s out` "
2025-09-01 11:07:39 +00:00
" as select cts, cint, %% %% tbname from st1 where cint > 5 and tint > 0 and %% %% tbname like ' %% 2 ' order by cts; "
2025-07-16 06:42:16 +00:00
% ( stream_name , stream_name )
)
tdLog . info ( " create same name stream: %s " % sql )
try :
tdSql . execute ( sql )
2025-09-01 11:07:39 +00:00
raise Exception (
" ERROR:recrate same name stream success, cant be recreate same name stream! "
)
2025-07-16 06:42:16 +00:00
except Exception as e :
if " Stream already exists " in str ( e ) :
tdLog . info ( " stream already exists!, test passed " )
else :
raise Exception ( " cant be recreate same name stream: %s " % str ( e ) )
def prepareData ( self ) :
tdLog . info ( f " prepare data " )
tdStream . dropAllStreamsAndDbs ( )
2025-09-01 11:07:39 +00:00
# wait all dnode ready
2025-07-16 06:42:16 +00:00
time . sleep ( 5 )
tdStream . init_database ( self . dbname )
2025-09-01 11:07:39 +00:00
2025-07-16 06:42:16 +00:00
st1 = StreamTable ( self . dbname , " st1 " , StreamTableType . TYPE_SUP_TABLE )
st1 . createTable ( 3 )
st1 . append_data ( 0 , self . tblRowNum )
2025-09-01 11:07:39 +00:00
2025-07-16 06:42:16 +00:00
self . tableList . append ( " st1 " )
for i in range ( 0 , self . subTblNum + 1 ) :
self . tableList . append ( f " st1_ { i } " )
2025-09-01 11:07:39 +00:00
2025-07-16 06:42:16 +00:00
ntb = StreamTable ( self . dbname , " ntb1 " , StreamTableType . TYPE_NORMAL_TABLE )
ntb . createTable ( )
ntb . append_data ( 0 , self . tblRowNum )
self . tableList . append ( f " ntb1 " )
2025-09-01 11:07:39 +00:00
def createOneStream ( self , stname ) :
2025-07-16 06:42:16 +00:00
tdLog . info ( f " create stream: " )
sql = (
2025-09-01 11:07:39 +00:00
f " create stream ` { stname } ` sliding(1s) from st1 partition by tbname "
" stream_options(fill_history( ' 2025-01-01 00:00:00 ' )) "
f " into ` { stname } out` as "
" select cts, cint, %% tbname from st1 "
" where cint > 5 and tint > 0 and %% tbname like ' %% 2 ' "
" order by cts; "
2025-07-16 06:42:16 +00:00
)
tdLog . info ( f " create stream: { sql } " )
try :
tdSql . execute ( sql )
except Exception as e :
2025-09-01 11:07:39 +00:00
if " No stream available snode now " not in str ( e ) :
raise Exception (
f " user cant create stream no snode ,but create success "
)
2025-07-16 06:42:16 +00:00
def checkResultRows ( self , expectedRows ) :
tdSql . checkResultsByFunc (
f " select * from information_schema.ins_snodes order by id; " ,
lambda : tdSql . getRows ( ) == expectedRows ,
2025-09-01 11:07:39 +00:00
delay = 0.5 ,
retry = 2 ,
2025-07-16 06:42:16 +00:00
)
2025-09-01 11:07:39 +00:00
2025-07-16 06:42:16 +00:00
def createSnodeTest ( self ) :
tdLog . info ( f " create snode test " )
tdSql . query ( " select * from information_schema.ins_dnodes order by id; " )
2025-09-01 11:07:39 +00:00
numOfNodes = tdSql . getRows ( )
2025-07-16 06:42:16 +00:00
tdLog . info ( f " numOfNodes: { numOfNodes } " )
2025-09-01 11:07:39 +00:00
2025-07-16 06:42:16 +00:00
for i in range ( 1 , numOfNodes + 1 ) :
tdSql . execute ( f " create snode on dnode { i } " )
tdLog . info ( f " create snode on dnode { i } success " )
self . checkResultRows ( numOfNodes )
2025-09-01 11:07:39 +00:00
2025-07-16 06:42:16 +00:00
tdSql . checkResultsByFunc (
2025-09-01 11:07:39 +00:00
f " show snodes; " , lambda : tdSql . getRows ( ) == numOfNodes , delay = 0.5 , retry = 2
2025-07-16 06:42:16 +00:00
)
def dropAllSnodeTest ( self ) :
tdLog . info ( f " drop all snode test " )
tdSql . query ( " select * from information_schema.ins_snodes order by id; " )
numOfSnodes = tdSql . getRows ( )
tdLog . info ( f " numOfSnodes: { numOfSnodes } " )
2025-09-01 11:07:39 +00:00
for i in range ( 1 , numOfSnodes ) :
2025-07-16 06:42:16 +00:00
tdSql . execute ( f " drop snode on dnode { i } " )
tdLog . info ( f " drop snode { i } success " )
2025-09-01 11:07:39 +00:00
2025-07-16 06:42:16 +00:00
self . checkResultRows ( 1 )
2025-09-01 11:07:39 +00:00
2025-07-16 06:42:16 +00:00
tdSql . checkResultsByFunc (
2025-09-01 11:07:39 +00:00
f " show snodes; " , lambda : tdSql . getRows ( ) == 1 , delay = 0.5 , retry = 2
2025-07-16 06:42:16 +00:00
)
2025-09-01 11:07:39 +00:00
numOfRows = tdSql . execute ( f " drop snode on dnode { numOfSnodes } " )
2025-07-16 06:42:16 +00:00
if numOfRows != 0 :
raise Exception ( f " drop all snodes failed! " )
tdSql . query ( " select * from information_schema.ins_snodes order by id; " )
numOfSnodes = tdSql . getRows ( )
tdLog . info ( f " After drop all snodes numOfSnodes: { numOfSnodes } " )
def dropOneSnodeTest ( self ) :
tdSql . query ( " select * from information_schema.ins_snodes order by id; " )
2025-09-01 11:07:39 +00:00
numOfSnodes = tdSql . getRows ( )
# 只有一个 snode 的时候不再执行删除
if numOfSnodes > 1 :
2025-07-16 06:42:16 +00:00
tdLog . info ( f " drop one snode test " )
2025-09-01 11:07:39 +00:00
tdSql . query (
" select * from information_schema.ins_streams order by stream_name; "
)
snodeid = tdSql . getData ( 0 , 6 )
2025-07-16 06:42:16 +00:00
tdSql . execute ( f " drop snode on dnode { snodeid } " )
tdLog . info ( f " drop snode { snodeid } success " )
2025-09-01 11:07:39 +00:00
# drop snode后流状态有延迟, 需要等待才能看到 failed 状态出现
2025-07-16 06:42:16 +00:00
time . sleep ( 15 )
2025-09-01 11:07:39 +00:00
2025-07-16 06:42:16 +00:00
def createStream ( self ) :
tdLog . info ( f " create stream " )
tdSql . query ( " select * from information_schema.ins_dnodes order by id; " )
2025-09-01 11:07:39 +00:00
numOfNodes = tdSql . getRows ( )
for i in range ( 1 , numOfNodes + 1 ) :
tdSql . execute (
f " create stream `s { i } ` sliding(1s) from st1 stream_options(fill_history( ' 2025-01-01 00:00:00 ' )) into `s { i } out` as select cts, cint from st1 where _tcurrent_ts % 2 = 0 order by cts; "
)
2025-07-16 06:42:16 +00:00
tdLog . info ( f " create stream s { i } success! " )
# tdSql.execute("create stream `s2` sliding(1s) from st1 partition by tint, tbname options(fill_history('2025-01-01 00:00:00')) into `s2out` as select cts, cint from st1 order by cts limit 3;")
# tdSql.execute("create stream `s3` sliding(1s) from st1 partition by tbname options(pre_filter(cint>2)|fill_history('2025-01-01 00:00:00')) into `s3out` as select cts, cint, %%tbname from %%trows where cint >15 and tint >0 and %%tbname like '%2' order by cts;")
# tdSql.execute("create stream `s4` sliding(1s) from st1 options(fill_history('2025-01-01 00:00:00')) into `s4out` as select _tcurrent_ts, cint from st1 order by cts limit 4;")
def dropOneStream ( self ) :
tdLog . info ( f " drop one stream: " )
2025-09-01 11:07:39 +00:00
tdSql . query (
" select * from information_schema.ins_streams order by stream_name; "
)
2025-07-16 06:42:16 +00:00
numOfStreams = tdSql . getRows ( )
tdLog . info ( f " Total streams: { numOfStreams } " )
2025-09-01 11:07:39 +00:00
streamid = tdSql . getData ( 0 , 0 )
2025-07-16 06:42:16 +00:00
tdSql . execute ( f " drop stream { streamid } " )
tdLog . info ( f " drop stream { streamid } success " )
2025-09-01 11:07:39 +00:00
tdSql . query (
" select * from information_schema.ins_streams order by stream_name; "
)
2025-07-16 06:42:16 +00:00
numOfStreams = tdSql . getRows ( )
tdLog . info ( f " Total streams: { numOfStreams } " )
2025-09-01 11:07:39 +00:00
2025-07-16 06:42:16 +00:00
def dropOneDnode ( self ) :
tdSql . query ( " select * from information_schema.ins_dnodes order by id; " )
numOfDnodes = tdSql . getRows ( )
tdLog . info ( f " Total dnodes: { numOfDnodes } " )
2025-09-01 11:07:39 +00:00
tdSql . query (
f " select `replica` from information_schema.ins_databases where name= ' { self . dbname } ' "
)
numOfReplica = tdSql . getData ( 0 , 0 )
if numOfDnodes == 3 and numOfReplica == 3 :
2025-07-16 06:42:16 +00:00
tdLog . info ( f " Total dndoes: 3,replica:3, can not drop dnode. " )
return
2025-09-01 11:07:39 +00:00
if numOfDnodes > 2 :
2025-07-16 06:42:16 +00:00
tdLog . info ( f " drop one dnode: " )
tdSql . query ( " select * from information_schema.ins_dnodes order by id; " )
2025-09-01 11:07:39 +00:00
dnodeid = tdSql . getData ( 2 , 0 )
2025-07-16 06:42:16 +00:00
tdSql . execute ( f " drop dnode { dnodeid } " )
tdLog . info ( f " drop dnode { dnodeid } success " )
2025-09-01 11:07:39 +00:00
2025-07-16 06:42:16 +00:00
tdSql . query ( " select * from information_schema.ins_dnodes order by id; " )
numOfDnodes = tdSql . getRows ( )
tdLog . info ( f " Total dnodes: { numOfDnodes } " )
# time.sleep(3)
2025-09-01 11:07:39 +00:00
2025-07-16 06:42:16 +00:00
def killOneDnode ( self ) :
tdSql . query ( " select * from information_schema.ins_dnodes order by id; " )
numOfDnodes = tdSql . getRows ( )
2025-09-01 11:07:39 +00:00
if numOfDnodes > 2 :
2025-07-16 06:42:16 +00:00
tdLog . info ( f " kill one dnode: " )
cmd = (
2025-09-01 11:07:39 +00:00
f " ps -ef | grep -wi taosd | grep ' dnode { numOfDnodes } /cfg ' "
" | grep -v grep | awk ' { print $2} ' | xargs kill -9 > /dev/null 2>&1 "
2025-07-16 06:42:16 +00:00
)
subprocess . run ( cmd , shell = True )
tdLog . info ( f " kill dndoe { numOfDnodes } success " )
2025-09-01 11:07:39 +00:00
# kill dnode后流状态有延迟, 需要等待才能看到 failed 状态出现
2025-07-16 06:42:16 +00:00
time . sleep ( 15 )
2025-09-01 11:07:39 +00:00
2025-07-16 06:42:16 +00:00
def killOneDnode2 ( self ) :
tdSql . query ( " select * from information_schema.ins_dnodes order by id; " )
numOfDnodes = tdSql . getRows ( )
2025-09-01 11:07:39 +00:00
if numOfDnodes > 2 :
2025-07-16 06:42:16 +00:00
tdLog . info ( f " kill one dnode: " )
2025-09-01 11:07:39 +00:00
tdDnodes = cluster . dnodes
2025-07-16 06:42:16 +00:00
tdDnodes [ numOfDnodes ] . stoptaosd ( )
# tdDnodes[numOfDnodes].starttaosd()
2025-09-01 11:07:39 +00:00
2025-07-16 06:42:16 +00:00
def checkStreamRunning ( self ) :
tdLog . info ( f " check stream running status: " )
2025-09-01 11:07:39 +00:00
timeout = 60
2025-07-16 06:42:16 +00:00
start_time = time . time ( )
while True :
if time . time ( ) - start_time > timeout :
tdLog . error ( " Timeout waiting for all streams to be running. " )
tdLog . error ( f " Final stream running status: { streamRunning } " )
2025-09-01 11:07:39 +00:00
raise TimeoutError (
f " Stream status did not reach ' Running ' within { timeout } s timeout. "
)
tdSql . query (
f " select status from information_schema.ins_streams order by stream_name; "
)
streamRunning = tdSql . getColData ( 0 )
2025-07-16 06:42:16 +00:00
if all ( status == " Running " for status in streamRunning ) :
tdLog . info ( " All Stream running! " )
tdLog . info ( f " stream running status: { streamRunning } " )
return
else :
tdLog . info ( " Stream not running! Wait stream running ... " )
tdLog . info ( f " stream running status: { streamRunning } " )
time . sleep ( 1 )