Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
6af088ad
TDengine
项目概览
taosdata
/
TDengine
大约 2 年 前同步成功
通知
1192
Star
22018
Fork
4786
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
TDengine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
6af088ad
编写于
11月 19, 2021
作者:
A
Alex Duan
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
combine multi-table write to one sql
上级
194e4f0f
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
130 addition
and
35 deletion
+130
-35
src/client/inc/tsclient.h
src/client/inc/tsclient.h
+2
-0
src/client/src/tscSql.c
src/client/src/tscSql.c
+20
-0
src/client/src/tscStream.c
src/client/src/tscStream.c
+86
-34
src/inc/trpc.h
src/inc/trpc.h
+1
-0
src/rpc/src/rpcMain.c
src/rpc/src/rpcMain.c
+6
-0
src/util/inc/tidpool.h
src/util/inc/tidpool.h
+2
-0
src/util/src/tidpool.c
src/util/src/tidpool.c
+13
-1
未找到文件。
src/client/inc/tsclient.h
浏览文件 @
6af088ad
...
...
@@ -486,6 +486,8 @@ TAOS *taos_connect_a(char *ip, char *user, char *pass, char *db, uint16_t port,
void
*
param
,
TAOS
**
taos
);
TAOS_RES
*
taos_query_h
(
TAOS
*
taos
,
const
char
*
sqlstr
,
int64_t
*
res
);
TAOS_RES
*
taos_query_ra
(
TAOS
*
taos
,
const
char
*
sqlstr
,
__async_cb_func_t
fp
,
void
*
param
);
// get taos connection unused session number
int32_t
taos_unused_session
(
TAOS
*
taos
);
void
waitForQueryRsp
(
void
*
param
,
TAOS_RES
*
tres
,
int
code
);
...
...
src/client/src/tscSql.c
浏览文件 @
6af088ad
...
...
@@ -28,6 +28,7 @@
#include "tutil.h"
#include "ttimer.h"
#include "tscProfile.h"
#include "tidpool.h"
static
bool
validImpl
(
const
char
*
str
,
size_t
maxsize
)
{
if
(
str
==
NULL
)
{
...
...
@@ -306,6 +307,25 @@ void taos_close(TAOS *taos) {
taosRemoveRef
(
tscRefId
,
pObj
->
rid
);
}
// get taos connection unused session number
int32_t
taos_unused_session
(
TAOS
*
taos
)
{
// param valid check
STscObj
*
pObj
=
(
STscObj
*
)
taos
;
if
(
pObj
==
NULL
||
pObj
->
signature
!=
pObj
)
{
tscError
(
"pObj:%p is NULL or freed"
,
pObj
);
terrno
=
TSDB_CODE_TSC_DISCONNECTED
;
return
0
;
}
if
(
pObj
->
pRpcObj
==
NULL
)
{
tscError
(
"pObj:%p pRpcObj is NULL."
,
pObj
);
terrno
=
TSDB_CODE_TSC_DISCONNECTED
;
return
0
;
}
// get number
return
rpcUnusedSession
(
pObj
->
pRpcObj
->
pDnodeConn
,
false
);
}
void
waitForQueryRsp
(
void
*
param
,
TAOS_RES
*
tres
,
int
code
)
{
assert
(
tres
!=
NULL
);
...
...
src/client/src/tscStream.c
浏览文件 @
6af088ad
...
...
@@ -266,9 +266,15 @@ static void tscStreamFillTimeGap(SSqlStream* pStream, TSKEY ts) {
}
// callback send values
int32_t
ok_cnt
=
0
;
int32_t
err_cnt
=
0
;
void
cbSendValues
(
void
*
param
,
TAOS_RES
*
res
,
int
code
)
{
if
(
code
<
0
)
{
tscError
(
"CQ=%p insert another table error 0x%x"
,
param
,
code
);
err_cnt
++
;
tscError
(
"CQ Send Failed. code=0x%x ok_cnt=%d err_cnt=%d"
,
code
,
ok_cnt
,
err_cnt
);
}
else
{
ok_cnt
++
;
tscInfo
(
"CQ Send OK. row=%d ok_cnt=%d err_cnt=%d"
,
code
,
ok_cnt
,
err_cnt
);
}
}
...
...
@@ -303,36 +309,62 @@ size_t appendValues(TAOS_FIELD* fields, int32_t numCols, TAOS_ROW row, char* pBu
return
rowLen
;
}
bool
sqlBufSend
(
TAOS
*
taos
,
char
*
sqlBuf
)
{
// if no enough free session, wait max 10s
int32_t
sleepCnt
=
0
;
do
{
int32_t
session
=
taos_unused_session
(
taos
);
if
(
session
>
1000
)
{
break
;
}
taosMsleep
(
500
);
tscInfo
(
"CQ session < 1000. session=%d Wait 0.5s cnt=%d"
,
session
,
sleepCnt
);
}
while
(
++
sleepCnt
<
20
);
strcat
(
sqlBuf
,
";"
);
taos_query_ra
(
taos
,
sqlBuf
,
cbSendValues
,
NULL
);
return
true
;
}
#define STR_SQL_INSERT "insert into "
// send one table all rows for once
bool
sendChildTalbe
(
STscObj
*
pTscObj
,
char
*
superName
,
char
*
tableName
,
TAOS_FIELD
*
fields
,
int32_t
numCols
,
SArray
*
arr
)
{
int32_t
bufLen
=
TSDB_MAX_SQL_LEN
;
bool
sendChildTalbe
(
TAOS
*
taos
,
char
*
superName
,
char
*
tableName
,
TAOS_FIELD
*
fields
,
int32_t
numCols
,
SArray
*
arr
,
char
*
sqlBuf
,
int32_t
bufLen
)
{
char
dbName
[
TSDB_DB_NAME_LEN
]
=
""
;
char
dbTable
[
TSDB_TABLE_FNAME_LEN
];
size_t
numRows
=
taosArrayGetSize
(
arr
);
if
(
numRows
==
0
)
return
false
;
if
(
numRows
<
50
)
bufLen
/=
20
;
else
if
(
numRows
<
500
)
bufLen
/=
5
;
else
bufLen
/=
2
;
char
dbName
[
TSDB_DB_NAME_LEN
]
=
""
;
char
fullTable
[
TSDB_TABLE_FNAME_LEN
];
// obtain dbname
char
*
p
=
strstr
(
superName
,
"."
);
if
(
p
)
{
// if have db prefix , under this db create table
int32_t
len
=
p
-
superName
;
strncpy
(
dbName
,
superName
,
len
);
dbName
[
len
]
=
0
;
// append str end
sprintf
(
full
Table
,
"%s.%s"
,
dbName
,
tableName
);
sprintf
(
db
Table
,
"%s.%s"
,
dbName
,
tableName
);
}
else
{
// no db prefix
strcpy
(
full
Table
,
tableName
);
strcpy
(
db
Table
,
tableName
);
}
char
*
pBuf
=
(
char
*
)
tmalloc
(
bufLen
);
sprintf
(
pBuf
,
"insert into %s using %s tags(0) values "
,
fullTable
,
superName
);
size_t
curLen
=
strlen
(
pBuf
);
// first enter
if
(
sqlBuf
[
0
]
==
0
)
{
strcpy
(
sqlBuf
,
STR_SQL_INSERT
);
}
else
{
// check need send
if
(
bufLen
-
strlen
(
sqlBuf
)
<
300
)
{
sqlBufSend
(
taos
,
sqlBuf
);
strcpy
(
sqlBuf
,
STR_SQL_INSERT
);
}
}
// init
int32_t
preLen
=
strlen
(
sqlBuf
);
char
*
subBuf
=
sqlBuf
+
preLen
;
int32_t
subLen
=
bufLen
-
preLen
;
sprintf
(
subBuf
,
" %s using %s tags(0) values "
,
dbTable
,
superName
);
size_t
curLen
=
strlen
(
subBuf
);
TAOS_ROW
row
;
bool
full
=
false
;
...
...
@@ -340,30 +372,42 @@ bool sendChildTalbe(STscObj *pTscObj, char *superName, char *tableName, TAOS_FIE
row
=
(
TAOS_ROW
)
taosArrayGetP
(
arr
,
i
);
if
(
row
==
NULL
)
continue
;
curLen
+=
appendValues
(
fields
,
numCols
,
row
,
pBuf
,
bufLen
-
100
,
curLen
,
&
full
);
if
(
full
||
i
==
numRows
-
1
)
{
// need send
if
(
subLen
>
200
)
curLen
+=
appendValues
(
fields
,
numCols
,
row
,
subBuf
,
subLen
-
100
,
curLen
,
&
full
);
else
full
=
true
;
if
(
full
)
{
// need send
// send current
strcat
(
pBuf
,
";"
);
taos_query_a
(
pTscObj
,
pBuf
,
cbSendValues
,
NULL
);
// reset for next
if
(
full
)
{
sprintf
(
pBuf
,
"insert into %s.%s using %s tags(0) values "
,
dbName
,
tableName
,
superName
);
curLen
=
strlen
(
pBuf
);
// retry append. if full is true again, ignore this row
curLen
+=
appendValues
(
fields
,
numCols
,
row
,
pBuf
,
bufLen
-
100
,
curLen
,
&
full
);
full
=
false
;
// reset to false
}
sqlBufSend
(
taos
,
sqlBuf
);
// init reset
strcpy
(
sqlBuf
,
STR_SQL_INSERT
);
preLen
=
strlen
(
sqlBuf
);
subBuf
=
sqlBuf
+
preLen
;
subLen
=
bufLen
-
preLen
;
sprintf
(
subBuf
,
" %s using %s tags(0) values "
,
dbTable
,
superName
);
curLen
=
strlen
(
subBuf
);
// retry append. if full is true again, ignore this row
curLen
+=
appendValues
(
fields
,
numCols
,
row
,
subBuf
,
subLen
-
100
,
curLen
,
&
full
);
full
=
false
;
// reset to false
}
tfree
(
row
);
}
tfree
(
pBuf
);
return
true
;
}
// write cq result to another table
bool
toAnotherTable
(
STscObj
*
pTscObj
,
char
*
superName
,
TAOS_FIELD
*
fields
,
int32_t
numCols
,
SHashObj
*
tbHash
)
{
bool
toAnotherTable
(
STscObj
*
pTscObj
,
char
*
superName
,
TAOS_FIELD
*
fields
,
int32_t
numCols
,
SHashObj
*
tbHash
,
int32_t
numRows
)
{
int32_t
bufLen
=
TSDB_MAX_SQL_LEN
/
2
-
128
;
char
*
sqlBuf
=
tmalloc
(
bufLen
);
sqlBuf
[
0
]
=
0
;
// init
ok_cnt
=
0
;
err_cnt
=
0
;
int
cnt_table
=
0
;
void
*
pIter
=
taosHashIterate
(
tbHash
,
NULL
);
while
(
pIter
)
{
SArray
*
arr
=
*
(
SArray
**
)
pIter
;
...
...
@@ -377,14 +421,22 @@ bool toAnotherTable(STscObj *pTscObj, char *superName, TAOS_FIELD *fields, int32
key
[
len
]
=
0
;
// string end '\0'
// send all this table rows
sendChildTalbe
(
pTscObj
,
superName
,
key
,
fields
,
numCols
,
arr
);
sendChildTalbe
(
pTscObj
,
superName
,
key
,
fields
,
numCols
,
arr
,
sqlBuf
,
bufLen
);
// release SArray
taosArrayDestroy
(
arr
);
tfree
(
key
);
cnt_table
++
;
}
pIter
=
taosHashIterate
(
tbHash
,
pIter
);
}
if
(
sqlBuf
[
0
])
{
sqlBufSend
(
pTscObj
,
sqlBuf
);
}
tscInfo
(
"CQ ===== stream %d rows write to %d tables =====
\n
"
,
numRows
,
cnt_table
);
tfree
(
sqlBuf
);
return
true
;
}
...
...
@@ -483,7 +535,7 @@ static void tscProcessStreamRetrieveResult(void *param, TAOS_RES *res, int numOf
// write Another
if
(
toAnother
)
{
toAnotherTable
(
pSql
->
pTscObj
,
pStream
->
to
,
fields
,
dstColsNum
,
tbHash
);
toAnotherTable
(
pSql
->
pTscObj
,
pStream
->
to
,
fields
,
dstColsNum
,
tbHash
,
numOfRows
);
taosHashCleanup
(
tbHash
);
}
...
...
src/inc/trpc.h
浏览文件 @
6af088ad
...
...
@@ -92,6 +92,7 @@ int rpcGetConnInfo(void *thandle, SRpcConnInfo *pInfo);
void
rpcSendRecv
(
void
*
shandle
,
SRpcEpSet
*
pEpSet
,
SRpcMsg
*
pReq
,
SRpcMsg
*
pRsp
);
int
rpcReportProgress
(
void
*
pConn
,
char
*
pCont
,
int
contLen
);
void
rpcCancelRequest
(
int64_t
rid
);
int32_t
rpcUnusedSession
(
void
*
rpcInfo
,
bool
bLock
);
#ifdef __cplusplus
}
...
...
src/rpc/src/rpcMain.c
浏览文件 @
6af088ad
...
...
@@ -1659,3 +1659,9 @@ static void rpcDecRef(SRpcInfo *pRpc)
}
}
int32_t
rpcUnusedSession
(
void
*
rpcInfo
,
bool
bLock
)
{
SRpcInfo
*
info
=
(
SRpcInfo
*
)
rpcInfo
;
if
(
info
==
NULL
)
return
0
;
return
taosIdPoolNumOfFree
(
info
->
idPool
,
bLock
);
}
\ No newline at end of file
src/util/inc/tidpool.h
浏览文件 @
6af088ad
...
...
@@ -36,6 +36,8 @@ int taosIdPoolNumOfUsed(void *handle);
bool
taosIdPoolMarkStatus
(
void
*
handle
,
int
id
);
// get free count from pool , if bLock is true, locked pool than get free count, accuracy but slowly
int
taosIdPoolNumOfFree
(
void
*
handle
,
bool
bLock
);
#ifdef __cplusplus
}
#endif
...
...
src/util/src/tidpool.c
浏览文件 @
6af088ad
...
...
@@ -163,4 +163,16 @@ int taosIdPoolMaxSize(void *handle) {
pthread_mutex_unlock
(
&
pIdPool
->
mutex
);
return
ret
;
}
\ No newline at end of file
}
// get free count from pool , if bLock is true, locked pool than get free count, accuracy but slowly
int
taosIdPoolNumOfFree
(
void
*
handle
,
bool
bLock
)
{
id_pool_t
*
pIdPool
=
handle
;
if
(
bLock
)
pthread_mutex_lock
(
&
pIdPool
->
mutex
);
int
ret
=
pIdPool
->
numOfFree
;
if
(
bLock
)
pthread_mutex_unlock
(
&
pIdPool
->
mutex
);
return
ret
;
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录