Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
010dea24
T
TDengine
项目概览
taosdata
/
TDengine
大约 2 年 前同步成功
通知
1192
Star
22018
Fork
4786
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
TDengine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
010dea24
编写于
9月 15, 2022
作者:
Z
zhihaop
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat: using thread local buffer to improve performance at scale
上级
f6de716d
变更
5
显示空白变更内容
内联
并排
Showing
5 changed file
with
125 addition
and
28 deletion
+125
-28
src/client/inc/tscBulkWrite.h
src/client/inc/tscBulkWrite.h
+48
-3
src/client/inc/tsclient.h
src/client/inc/tsclient.h
+2
-2
src/client/src/tscAsync.c
src/client/src/tscAsync.c
+7
-4
src/client/src/tscBulkWrite.c
src/client/src/tscBulkWrite.c
+63
-14
src/client/src/tscSystem.c
src/client/src/tscSystem.c
+5
-5
未找到文件。
src/client/inc/tscBulkWrite.h
浏览文件 @
010dea24
...
...
@@ -16,23 +16,29 @@
#ifndef TDENGINE_TSCBULKWRITE_H
#define TDENGINE_TSCBULKWRITE_H
#include <pthread.h>
#include <stdlib.h>
#ifdef __cplusplus
extern
"C"
{
#endif
#include "tlist.h"
#include "tarray.h"
#include "tlist.h"
#include "tthread.h"
/**
* SAsyncBulkWriteDispatcher is an async batching dispatcher(for writing), it can buffer insertion statements, batch
* and merge them into single statement.
*/
typedef
struct
SAsyncBulkWriteDispatcher
{
// the
mpmc queue
to store the insertion statements. equivalent to SList<SSqlObj*>.
// the
buffer
to store the insertion statements. equivalent to SList<SSqlObj*>.
SList
*
buffer
;
// the mutex to protect the buffer.
pthread_mutex_t
mutex
;
// the background thread to manage batching timeout.
pthread_t
*
background
;
pthread_t
background
;
// the maximum number of insertion rows in a batch.
int32_t
batchSize
;
...
...
@@ -55,6 +61,7 @@ typedef struct SAsyncBulkWriteDispatcher {
// forward declaration.
typedef
struct
SSqlObj
SSqlObj
;
/**
* Merge the statements into single SSqlObj.
*
...
...
@@ -125,6 +132,44 @@ bool tscSupportBulkInsertion(SSqlObj* pSql);
*/
bool
dispatcherTryBatching
(
SAsyncBulkWriteDispatcher
*
dispatcher
,
SSqlObj
*
pSql
);
/**
* A thread local version of SAsyncBulkWriteDispatcher.
*/
typedef
struct
SThreadLocalDispatcher
{
pthread_key_t
key
;
// the maximum number of insertion rows in a batch.
int32_t
batchSize
;
// the batching timeout in milliseconds.
int32_t
timeoutMs
;
}
SThreadLocalDispatcher
;
/**
* Create a thread local SAsyncBulkWriteDispatcher variable.
*
* @param batchSize the batchSize of SAsyncBulkWriteDispatcher.
* @param timeoutMs the timeoutMs of SAsyncBulkWriteDispatcher.
* @return the thread local SAsyncBulkWriteDispatcher.
*/
SThreadLocalDispatcher
*
createThreadLocalDispatcher
(
int32_t
batchSize
,
int32_t
timeoutMs
);
/**
* Destroy the thread local SAsyncBulkWriteDispatcher variable.
* (will destroy all the instances of SAsyncBulkWriteDispatcher in the thread local variable)
*
* @param dispatcher the thread local SAsyncBulkWriteDispatcher variable.
*/
void
destroyThreadLocalDispatcher
(
SThreadLocalDispatcher
*
dispatcher
);
/**
* Get the thread local instance of SAsyncBulkWriteDispatcher.
* @param dispatcher the thread local SAsyncBulkWriteDispatcher variable.
* @return the thread local SAsyncBulkWriteDispatcher.
*/
SAsyncBulkWriteDispatcher
*
dispatcherThreadLocal
(
SThreadLocalDispatcher
*
dispatcher
);
#ifdef __cplusplus
}
#endif
...
...
src/client/inc/tsclient.h
浏览文件 @
010dea24
...
...
@@ -546,8 +546,8 @@ extern SHashObj *tscTableMetaMap;
extern
SCacheObj
*
tscVgroupListBuf
;
// forward declaration.
typedef
struct
S
AsyncBulkWriteDispatcher
SAsyncBulkWrite
Dispatcher
;
extern
S
AsyncBulkWriteDispatcher
*
tscDispatcher
;
typedef
struct
S
ThreadLocalDispatcher
SThreadLocal
Dispatcher
;
extern
S
ThreadLocalDispatcher
*
tscDispatcher
;
extern
int
tscObjRef
;
extern
void
*
tscTmr
;
extern
void
*
tscQhandle
;
...
...
src/client/src/tscAsync.c
浏览文件 @
010dea24
...
...
@@ -397,11 +397,14 @@ void doAsyncQuery(STscObj* pObj, SSqlObj* pSql, __async_cb_func_t fp, void* para
return
;
}
if
(
tscDispatcher
!=
NULL
&&
dispatcherTryBatching
(
tscDispatcher
,
pSql
))
{
if
(
tscDispatcher
!=
NULL
)
{
SAsyncBulkWriteDispatcher
*
dispatcher
=
dispatcherThreadLocal
(
tscDispatcher
);
if
(
dispatcherTryBatching
(
dispatcher
,
pSql
))
{
taosReleaseRef
(
tscObjRef
,
pSql
->
self
);
tscDebug
(
"sql obj %p has been buffer in insert buffer"
,
pSql
);
return
;
}
}
SQueryInfo
*
pQueryInfo
=
tscGetQueryInfo
(
pCmd
);
executeQuery
(
pSql
,
pQueryInfo
);
...
...
src/client/src/tscBulkWrite.c
浏览文件 @
010dea24
...
...
@@ -151,8 +151,7 @@ SArray* dispatcherPollAll(SAsyncBulkWriteDispatcher* dispatcher) {
}
pthread_mutex_lock
(
&
dispatcher
->
mutex
);
SArray
*
statements
=
taosArrayInit
(
atomic_load_32
(
&
dispatcher
->
bufferSize
),
sizeof
(
SSqlObj
*
));
SArray
*
statements
=
taosArrayInit
(
0
,
sizeof
(
SSqlObj
*
));
if
(
statements
==
NULL
)
{
pthread_mutex_unlock
(
&
dispatcher
->
mutex
);
tscError
(
"failed to poll all items: out of memory"
);
...
...
@@ -160,7 +159,7 @@ SArray* dispatcherPollAll(SAsyncBulkWriteDispatcher* dispatcher) {
}
// get all the sql statements from the buffer.
while
(
atomic_load_32
(
&
dispatcher
->
bufferSize
)
)
{
while
(
true
)
{
SListNode
*
node
=
tdListPopHead
(
dispatcher
->
buffer
);
if
(
!
node
)
{
break
;
...
...
@@ -172,7 +171,6 @@ SArray* dispatcherPollAll(SAsyncBulkWriteDispatcher* dispatcher) {
listNodeFree
(
node
);
atomic_fetch_sub_32
(
&
dispatcher
->
bufferSize
,
1
);
atomic_fetch_sub_32
(
&
dispatcher
->
currentSize
,
statementGetInsertionRows
(
item
));
taosArrayPush
(
statements
,
&
item
);
}
...
...
@@ -285,8 +283,7 @@ SAsyncBulkWriteDispatcher* createAsyncBulkWriteDispatcher(int32_t batchSize, int
pthread_mutex_init
(
&
dispatcher
->
mutex
,
NULL
);
// init background thread.
dispatcher
->
background
=
taosCreateThread
(
dispatcherTimeoutCallback
,
dispatcher
);
if
(
!
dispatcher
->
background
)
{
if
(
pthread_create
(
&
dispatcher
->
background
,
NULL
,
dispatcherTimeoutCallback
,
dispatcher
))
{
tdListFree
(
dispatcher
->
buffer
);
tfree
(
dispatcher
);
return
NULL
;
...
...
@@ -300,17 +297,18 @@ void destroyAsyncDispatcher(SAsyncBulkWriteDispatcher* dispatcher) {
return
;
}
// mark shutdown.
atomic_store_8
(
&
dispatcher
->
shutdown
,
true
);
// make sure the timeout thread exit.
pthread_join
(
dispatcher
->
background
,
NULL
);
// poll and send all the statements in the buffer.
while
(
atomic_load_32
(
&
dispatcher
->
bufferSize
))
{
SArray
*
statements
=
dispatcherPollAll
(
dispatcher
);
dispatcherExecute
(
statements
);
}
// make sure the thread exit.
taosDestroyThread
(
dispatcher
->
background
);
// destroy the buffer.
tdListFree
(
dispatcher
->
buffer
);
...
...
@@ -375,3 +373,54 @@ bool dispatcherTryBatching(SAsyncBulkWriteDispatcher* dispatcher, SSqlObj* pSql)
}
return
true
;
}
/**
* Destroy the SAsyncBulkWriteDispatcher create by SThreadLocalDispatcher.
* @param arg
*/
static
void
destroyDispatcher
(
void
*
arg
)
{
SAsyncBulkWriteDispatcher
*
dispatcher
=
arg
;
if
(
!
dispatcher
)
{
return
;
}
destroyAsyncDispatcher
(
dispatcher
);
}
SThreadLocalDispatcher
*
createThreadLocalDispatcher
(
int32_t
batchSize
,
int32_t
timeoutMs
)
{
SThreadLocalDispatcher
*
dispatcher
=
calloc
(
1
,
sizeof
(
SThreadLocalDispatcher
));
if
(
!
dispatcher
)
{
return
NULL
;
}
dispatcher
->
batchSize
=
batchSize
;
dispatcher
->
timeoutMs
=
timeoutMs
;
if
(
pthread_key_create
(
&
dispatcher
->
key
,
destroyDispatcher
))
{
free
(
dispatcher
);
return
NULL
;
}
return
dispatcher
;
}
SAsyncBulkWriteDispatcher
*
dispatcherThreadLocal
(
SThreadLocalDispatcher
*
dispatcher
)
{
SAsyncBulkWriteDispatcher
*
value
=
pthread_getspecific
(
dispatcher
->
key
);
if
(
value
)
{
return
value
;
}
value
=
createAsyncBulkWriteDispatcher
(
dispatcher
->
batchSize
,
dispatcher
->
timeoutMs
);
if
(
value
)
{
pthread_setspecific
(
dispatcher
->
key
,
value
);
return
value
;
}
return
NULL
;
}
void
destroyThreadLocalDispatcher
(
SThreadLocalDispatcher
*
dispatcher
)
{
if
(
dispatcher
)
{
pthread_key_delete
(
dispatcher
->
key
);
free
(
dispatcher
);
}
}
src/client/src/tscSystem.c
浏览文件 @
010dea24
...
...
@@ -50,7 +50,7 @@ void *tscRpcCache; // cache to keep rpc obj
int32_t
tscNumOfThreads
=
1
;
// num of rpc threads
char
tscLogFileName
[]
=
"taoslog"
;
int
tscLogFileNum
=
10
;
S
AsyncBulkWriteDispatcher
*
tscDispatcher
=
NULL
;
S
ThreadLocalDispatcher
*
tscDispatcher
=
NULL
;
static
pthread_mutex_t
rpcObjMutex
;
// mutex to protect open the rpc obj concurrently
static
pthread_once_t
tscinit
=
PTHREAD_ONCE_INIT
;
...
...
@@ -60,20 +60,20 @@ static pthread_mutex_t setConfMutex = PTHREAD_MUTEX_INITIALIZER;
static
volatile
int
tscInitRes
=
0
;
/**
* Init the t
aosc
async bulk write dispatcher.
* Init the t
hread local
async bulk write dispatcher.
*
* @param batchSize the batchSize of async bulk write dispatcher.
* @param timeoutMs the timeout of batching in milliseconds.
*/
void
tscInitAsyncDispatcher
(
int32_t
batchSize
,
int32_t
timeoutMs
)
{
tscDispatcher
=
create
AsyncBulkWrite
Dispatcher
(
batchSize
,
timeoutMs
);
tscDispatcher
=
create
ThreadLocal
Dispatcher
(
batchSize
,
timeoutMs
);
}
/**
* Destroy the t
aosc
async bulk write dispatcher.
* Destroy the t
hread local
async bulk write dispatcher.
*/
void
tscDestroyAsyncDispatcher
()
{
destroy
Async
Dispatcher
(
tscDispatcher
);
destroy
ThreadLocal
Dispatcher
(
tscDispatcher
);
tscDispatcher
=
NULL
;
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录