Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
d138f77a
T
TDengine
项目概览
taosdata
/
TDengine
大约 1 年 前同步成功
通知
1184
Star
22015
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看板
体验新版 GitCode,发现更多精彩内容 >>
提交
d138f77a
编写于
9月 24, 2022
作者:
Z
zhihaop
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat: use sleepMutex to sleep the background thread
上级
e7fa3b0a
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
33 addition
and
47 deletion
+33
-47
src/client/inc/tscBulkWrite.h
src/client/inc/tscBulkWrite.h
+5
-2
src/client/src/tscBulkWrite.c
src/client/src/tscBulkWrite.c
+28
-45
未找到文件。
src/client/inc/tscBulkWrite.h
浏览文件 @
d138f77a
...
...
@@ -35,12 +35,15 @@ typedef struct SAsyncBulkWriteDispatcher {
SArray
*
buffer
;
// the mutex to protect the dispatcher.
pthread_mutex_t
mutex
;
pthread_mutex_t
bufferMutex
;
// the mutex to sleep the background thread.
pthread_mutex_t
sleepMutex
;
// the cond to signal to background thread.
pthread_cond_t
timeout
;
// the cond to signal
to background thread
.
// the cond to signal
when buffer not full
.
pthread_cond_t
notFull
;
// the background thread to manage batching timeout.
...
...
src/client/src/tscBulkWrite.c
浏览文件 @
d138f77a
...
...
@@ -183,10 +183,10 @@ inline static SArray* dispatcherPollAll(SAsyncBulkWriteDispatcher* dispatcher) {
* @return the items in the dispatcher, SArray<SSqlObj*>.
*/
inline
static
SArray
*
dispatcherLockPollAll
(
SAsyncBulkWriteDispatcher
*
dispatcher
)
{
pthread_mutex_lock
(
&
dispatcher
->
m
utex
);
pthread_mutex_lock
(
&
dispatcher
->
bufferM
utex
);
SArray
*
statements
=
dispatcherPollAll
(
dispatcher
);
pthread_cond_broadcast
(
&
dispatcher
->
notFull
);
pthread_mutex_unlock
(
&
dispatcher
->
m
utex
);
pthread_mutex_unlock
(
&
dispatcher
->
bufferM
utex
);
return
statements
;
}
...
...
@@ -198,19 +198,19 @@ inline static SArray* dispatcherLockPollAll(SAsyncBulkWriteDispatcher* dispatche
* @return return whether offer success.
*/
inline
static
bool
dispatcherTryOffer
(
SAsyncBulkWriteDispatcher
*
dispatcher
,
SSqlObj
*
pSql
)
{
pthread_mutex_lock
(
&
dispatcher
->
m
utex
);
pthread_mutex_lock
(
&
dispatcher
->
bufferM
utex
);
// if dispatcher is shutdown, must fail back to normal insertion.
// usually not happen, unless taos_query_a(...) after taos_close(...).
if
(
atomic_load_8
(
&
dispatcher
->
shutdown
))
{
pthread_mutex_unlock
(
&
dispatcher
->
m
utex
);
pthread_mutex_unlock
(
&
dispatcher
->
bufferM
utex
);
return
false
;
}
// the buffer is full.
while
(
dispatcher
->
currentSize
>=
dispatcher
->
batchSize
)
{
if
(
pthread_cond_wait
(
&
dispatcher
->
notFull
,
&
dispatcher
->
m
utex
))
{
pthread_mutex_unlock
(
&
dispatcher
->
m
utex
);
if
(
pthread_cond_wait
(
&
dispatcher
->
notFull
,
&
dispatcher
->
bufferM
utex
))
{
pthread_mutex_unlock
(
&
dispatcher
->
bufferM
utex
);
return
false
;
}
}
...
...
@@ -226,7 +226,7 @@ inline static bool dispatcherTryOffer(SAsyncBulkWriteDispatcher* dispatcher, SSq
taosArrayDestroy
(
&
statements
);
pthread_cond_broadcast
(
&
dispatcher
->
notFull
);
}
pthread_mutex_unlock
(
&
dispatcher
->
m
utex
);
pthread_mutex_unlock
(
&
dispatcher
->
bufferM
utex
);
return
true
;
}
...
...
@@ -264,23 +264,10 @@ _error:
* @param millis the duration in milliseconds.
* @return the timespec after `millis` ms.
*/
static
inline
struct
timespec
afterMillis
(
struct
timespec
t
,
int32_t
millis
)
{
t
.
tv_nsec
+=
millis
*
1000000L
;
t
.
tv_sec
+=
t
.
tv_nsec
/
1000000000L
;
t
.
tv_nsec
%=
1000000000L
;
return
t
;
}
/**
* Get the duration in milliseconds from timespec s to timespec t.
* @param s the start timespec.
* @param t the end timespec.
* @return the duration in milliseconds.
*/
static
inline
int64_t
durationMillis
(
struct
timespec
s
,
struct
timespec
t
)
{
int64_t
d
=
(
t
.
tv_sec
-
s
.
tv_sec
)
*
1000
;
d
+=
(
t
.
tv_nsec
-
s
.
tv_nsec
)
/
1000000L
;
return
d
;
static
inline
void
afterMillis
(
struct
timespec
*
t
,
int32_t
millis
)
{
t
->
tv_nsec
+=
millis
*
1000000L
;
t
->
tv_sec
+=
t
->
tv_nsec
/
1000000000L
;
t
->
tv_nsec
%=
1000000000L
;
}
/**
...
...
@@ -289,26 +276,19 @@ static inline int64_t durationMillis(struct timespec s, struct timespec t) {
* @param dispatcher the dispatcher thread to sleep.
* @param timeout the timeout in CLOCK_REALTIME.
*/
inline
static
void
dispatcherSleepUntil
(
SAsyncBulkWriteDispatcher
*
dispatcher
,
struct
timespec
timeout
)
{
struct
timespec
current
;
clock_gettime
(
CLOCK_REALTIME
,
&
current
);
// if current > timeout, no sleep required.
if
(
durationMillis
(
current
,
timeout
)
<=
0
)
{
return
;
}
pthread_mutex_lock
(
&
dispatcher
->
mutex
);
inline
static
void
dispatcherSleepUntil
(
SAsyncBulkWriteDispatcher
*
dispatcher
,
struct
timespec
*
timeout
)
{
pthread_mutex_lock
(
&
dispatcher
->
sleepMutex
);
while
(
true
)
{
// notified by dispatcherShutdown(...).
if
(
atomic_load_8
(
&
dispatcher
->
shutdown
))
{
break
;
}
if
(
pthread_cond_timedwait
(
&
dispatcher
->
timeout
,
&
dispatcher
->
mutex
,
&
timeout
))
{
if
(
pthread_cond_timedwait
(
&
dispatcher
->
timeout
,
&
dispatcher
->
sleepMutex
,
timeout
))
{
fflush
(
stdout
);
break
;
}
}
pthread_mutex_unlock
(
&
dispatcher
->
m
utex
);
pthread_mutex_unlock
(
&
dispatcher
->
sleepM
utex
);
}
/**
...
...
@@ -319,9 +299,9 @@ static void* dispatcherTimeoutCallback(void* arg) {
setThreadName
(
"tscAsyncBackground"
);
while
(
!
atomic_load_8
(
&
dispatcher
->
shutdown
))
{
struct
timespec
curren
t
;
clock_gettime
(
CLOCK_REALTIME
,
&
curren
t
);
struct
timespec
timeout
=
afterMillis
(
curren
t
,
dispatcher
->
timeoutMs
);
struct
timespec
timeou
t
;
clock_gettime
(
CLOCK_REALTIME
,
&
timeou
t
);
afterMillis
(
&
timeou
t
,
dispatcher
->
timeoutMs
);
SArray
*
statements
=
dispatcherLockPollAll
(
dispatcher
);
dispatcherExecute
(
statements
);
...
...
@@ -329,7 +309,7 @@ static void* dispatcherTimeoutCallback(void* arg) {
// Similar to scheduleAtFixedRate in Java, if the execution time exceed
// `timeoutMs` milliseconds, then there will be no sleep.
dispatcherSleepUntil
(
dispatcher
,
timeout
);
dispatcherSleepUntil
(
dispatcher
,
&
timeout
);
}
return
NULL
;
}
...
...
@@ -354,13 +334,15 @@ SAsyncBulkWriteDispatcher* createAsyncBulkWriteDispatcher(int32_t batchSize, int
}
// init the mutex and the cond.
pthread_mutex_init
(
&
dispatcher
->
mutex
,
NULL
);
pthread_mutex_init
(
&
dispatcher
->
bufferMutex
,
NULL
);
pthread_mutex_init
(
&
dispatcher
->
sleepMutex
,
NULL
);
pthread_cond_init
(
&
dispatcher
->
timeout
,
NULL
);
pthread_cond_init
(
&
dispatcher
->
notFull
,
NULL
);
// init background thread.
if
(
pthread_create
(
&
dispatcher
->
background
,
NULL
,
dispatcherTimeoutCallback
,
dispatcher
))
{
pthread_mutex_destroy
(
&
dispatcher
->
mutex
);
pthread_mutex_destroy
(
&
dispatcher
->
bufferMutex
);
pthread_mutex_destroy
(
&
dispatcher
->
sleepMutex
);
pthread_cond_destroy
(
&
dispatcher
->
timeout
);
pthread_cond_destroy
(
&
dispatcher
->
notFull
);
taosArrayDestroy
(
&
dispatcher
->
buffer
);
...
...
@@ -378,10 +360,10 @@ SAsyncBulkWriteDispatcher* createAsyncBulkWriteDispatcher(int32_t batchSize, int
*/
inline
static
void
dispatcherShutdown
(
SAsyncBulkWriteDispatcher
*
dispatcher
)
{
// mark shutdown, signal shutdown to timeout thread.
pthread_mutex_lock
(
&
dispatcher
->
m
utex
);
pthread_mutex_lock
(
&
dispatcher
->
sleepM
utex
);
atomic_store_8
(
&
dispatcher
->
shutdown
,
true
);
pthread_cond_broadcast
(
&
dispatcher
->
timeout
);
pthread_mutex_unlock
(
&
dispatcher
->
m
utex
);
pthread_mutex_unlock
(
&
dispatcher
->
sleepM
utex
);
// make sure the timeout thread exit.
pthread_join
(
dispatcher
->
background
,
NULL
);
...
...
@@ -409,7 +391,8 @@ void destroyAsyncDispatcher(SAsyncBulkWriteDispatcher* dispatcher) {
taosArrayDestroy
(
&
dispatcher
->
buffer
);
// destroy the mutex.
pthread_mutex_destroy
(
&
dispatcher
->
mutex
);
pthread_mutex_destroy
(
&
dispatcher
->
bufferMutex
);
pthread_mutex_destroy
(
&
dispatcher
->
sleepMutex
);
pthread_cond_destroy
(
&
dispatcher
->
timeout
);
pthread_cond_destroy
(
&
dispatcher
->
notFull
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录