Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
5fca9015
T
TDengine
项目概览
taosdata
/
TDengine
1 年多 前同步成功
通知
1185
Star
22016
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看板
提交
5fca9015
编写于
1月 25, 2022
作者:
S
Shengliang Guan
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refact worker util
上级
e777ee72
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
144 addition
and
121 deletion
+144
-121
include/util/tworker.h
include/util/tworker.h
+47
-30
source/dnode/mgmt/impl/inc/dndEnv.h
source/dnode/mgmt/impl/inc/dndEnv.h
+6
-6
source/dnode/mgmt/impl/src/dndVnodes.c
source/dnode/mgmt/impl/src/dndVnodes.c
+20
-20
source/dnode/mgmt/impl/src/dndWorker.c
source/dnode/mgmt/impl/src/dndWorker.c
+10
-10
source/util/src/tworker.c
source/util/src/tworker.c
+61
-55
未找到文件。
include/util/tworker.h
浏览文件 @
5fca9015
...
...
@@ -15,57 +15,74 @@
#ifndef _TD_UTIL_WORKER_H
#define _TD_UTIL_WORKER_H
#include "os.h"
#include "tqueue.h"
#ifdef __cplusplus
extern
"C"
{
#endif
typedef
struct
SWorkerPool
SWorkerPool
;
typedef
struct
SMWorkerPool
SMWorkerPool
;
typedef
struct
SQWorkerPool
SQWorkerPool
;
typedef
struct
SFWorkerPool
SFWorkerPool
;
typedef
struct
SWWorkerPool
SWWorkerPool
;
typedef
struct
SQWorker
{
int32_t
id
;
// worker ID
pthread_t
thread
;
// thread
SQWorkerPool
*
pool
;
}
SQWorker
;
typedef
struct
SWorker
{
int32_t
id
;
// worker ID
pthread_t
thread
;
// thread
SWorkerPool
*
pool
;
}
SWorker
;
typedef
struct
SQWorkerPool
{
int32_t
max
;
// max number of workers
int32_t
min
;
// min number of workers
int32_t
num
;
// current number of workers
STaosQset
*
qset
;
const
char
*
name
;
SQWorker
*
workers
;
pthread_mutex_t
mutex
;
}
SQWorkerPool
;
typedef
struct
SFWorker
{
int32_t
id
;
// worker ID
pthread_t
thread
;
// thread
SFWorkerPool
*
pool
;
}
SFWorker
;
typedef
struct
SWorkerPool
{
typedef
struct
S
F
WorkerPool
{
int32_t
max
;
// max number of workers
int32_t
min
;
// min number of workers
int32_t
num
;
// current number of workers
STaosQset
*
qset
;
const
char
*
name
;
S
Worker
*
workers
;
STaosQset
*
qset
;
const
char
*
name
;
S
FWorker
*
workers
;
pthread_mutex_t
mutex
;
}
SWorkerPool
;
}
S
F
WorkerPool
;
typedef
struct
S
M
Worker
{
typedef
struct
S
W
Worker
{
int32_t
id
;
// worker id
pthread_t
thread
;
// thread
STaosQall
*
qall
;
STaosQset
*
qset
;
// queue set
S
M
WorkerPool
*
pool
;
}
S
M
Worker
;
STaosQall
*
qall
;
STaosQset
*
qset
;
// queue set
S
W
WorkerPool
*
pool
;
}
S
W
Worker
;
typedef
struct
S
M
WorkerPool
{
typedef
struct
S
W
WorkerPool
{
int32_t
max
;
// max number of workers
int32_t
nextId
;
// from 0 to max-1, cyclic
const
char
*
name
;
S
MWorker
*
workers
;
const
char
*
name
;
S
WWorker
*
workers
;
pthread_mutex_t
mutex
;
}
S
M
WorkerPool
;
}
S
W
WorkerPool
;
int32_t
t
WorkerInit
(
S
WorkerPool
*
pool
);
void
t
WorkerCleanup
(
S
WorkerPool
*
pool
);
STaosQueue
*
t
WorkerAllocQueue
(
S
WorkerPool
*
pool
,
void
*
ahandle
,
FProcessItem
fp
);
void
t
WorkerFreeQueue
(
S
WorkerPool
*
pool
,
STaosQueue
*
queue
);
int32_t
t
QWorkerInit
(
SQ
WorkerPool
*
pool
);
void
t
QWorkerCleanup
(
SQ
WorkerPool
*
pool
);
STaosQueue
*
t
QWorkerAllocQueue
(
SQ
WorkerPool
*
pool
,
void
*
ahandle
,
FProcessItem
fp
);
void
t
QWorkerFreeQueue
(
SQ
WorkerPool
*
pool
,
STaosQueue
*
queue
);
int32_t
t
MWorkerInit
(
SM
WorkerPool
*
pool
);
void
t
MWorkerCleanup
(
SM
WorkerPool
*
pool
);
STaosQueue
*
t
MWorkerAllocQueue
(
SM
WorkerPool
*
pool
,
void
*
ahandle
,
FProcessItems
fp
);
void
t
MWorkerFreeQueue
(
SM
WorkerPool
*
pool
,
STaosQueue
*
queue
);
int32_t
t
WWorkerInit
(
SW
WorkerPool
*
pool
);
void
t
WWorkerCleanup
(
SW
WorkerPool
*
pool
);
STaosQueue
*
t
WWorkerAllocQueue
(
SW
WorkerPool
*
pool
,
void
*
ahandle
,
FProcessItems
fp
);
void
t
WWorkerFreeQueue
(
SW
WorkerPool
*
pool
,
STaosQueue
*
queue
);
#ifdef __cplusplus
}
...
...
source/dnode/mgmt/impl/inc/dndEnv.h
浏览文件 @
5fca9015
...
...
@@ -31,8 +31,8 @@ typedef struct {
SDnode
*
pDnode
;
STaosQueue
*
queue
;
union
{
SWorkerPool
pool
;
S
M
WorkerPool
mpool
;
S
Q
WorkerPool
pool
;
S
W
WorkerPool
mpool
;
};
}
SDnodeWorker
;
...
...
@@ -109,10 +109,10 @@ typedef struct {
int32_t
openVnodes
;
int32_t
totalVnodes
;
SRWLatch
latch
;
S
WorkerPool
queryPool
;
S
WorkerPool
fetchPool
;
S
M
WorkerPool
syncPool
;
S
M
WorkerPool
writePool
;
S
QWorkerPool
queryPool
;
S
QWorkerPool
fetchPool
;
S
W
WorkerPool
syncPool
;
S
W
WorkerPool
writePool
;
}
SVnodesMgmt
;
typedef
struct
{
...
...
source/dnode/mgmt/impl/src/dndVnodes.c
浏览文件 @
5fca9015
...
...
@@ -910,27 +910,27 @@ static int32_t dndInitVnodeWorkers(SDnode *pDnode) {
int32_t
maxWriteThreads
=
TMAX
(
pDnode
->
env
.
numOfCores
,
1
);
int32_t
maxSyncThreads
=
TMAX
(
pDnode
->
env
.
numOfCores
/
2
,
1
);
SWorkerPool
*
pPool
=
&
pMgmt
->
queryPool
;
S
Q
WorkerPool
*
pPool
=
&
pMgmt
->
queryPool
;
pPool
->
name
=
"vnode-query"
;
pPool
->
min
=
minQueryThreads
;
pPool
->
max
=
maxQueryThreads
;
if
(
tWorkerInit
(
pPool
)
!=
0
)
return
-
1
;
if
(
t
Q
WorkerInit
(
pPool
)
!=
0
)
return
-
1
;
pPool
=
&
pMgmt
->
fetchPool
;
pPool
->
name
=
"vnode-fetch"
;
pPool
->
min
=
minFetchThreads
;
pPool
->
max
=
maxFetchThreads
;
if
(
tWorkerInit
(
pPool
)
!=
0
)
return
-
1
;
if
(
t
Q
WorkerInit
(
pPool
)
!=
0
)
return
-
1
;
S
M
WorkerPool
*
pMPool
=
&
pMgmt
->
writePool
;
S
W
WorkerPool
*
pMPool
=
&
pMgmt
->
writePool
;
pMPool
->
name
=
"vnode-write"
;
pMPool
->
max
=
maxWriteThreads
;
if
(
t
M
WorkerInit
(
pMPool
)
!=
0
)
return
-
1
;
if
(
t
W
WorkerInit
(
pMPool
)
!=
0
)
return
-
1
;
pMPool
=
&
pMgmt
->
syncPool
;
pMPool
->
name
=
"vnode-sync"
;
pMPool
->
max
=
maxSyncThreads
;
if
(
t
M
WorkerInit
(
pMPool
)
!=
0
)
return
-
1
;
if
(
t
W
WorkerInit
(
pMPool
)
!=
0
)
return
-
1
;
dDebug
(
"vnode workers is initialized"
);
return
0
;
...
...
@@ -938,21 +938,21 @@ static int32_t dndInitVnodeWorkers(SDnode *pDnode) {
static
void
dndCleanupVnodeWorkers
(
SDnode
*
pDnode
)
{
SVnodesMgmt
*
pMgmt
=
&
pDnode
->
vmgmt
;
tWorkerCleanup
(
&
pMgmt
->
fetchPool
);
tWorkerCleanup
(
&
pMgmt
->
queryPool
);
t
M
WorkerCleanup
(
&
pMgmt
->
writePool
);
t
M
WorkerCleanup
(
&
pMgmt
->
syncPool
);
t
Q
WorkerCleanup
(
&
pMgmt
->
fetchPool
);
t
Q
WorkerCleanup
(
&
pMgmt
->
queryPool
);
t
W
WorkerCleanup
(
&
pMgmt
->
writePool
);
t
W
WorkerCleanup
(
&
pMgmt
->
syncPool
);
dDebug
(
"vnode workers is closed"
);
}
static
int32_t
dndAllocVnodeQueue
(
SDnode
*
pDnode
,
SVnodeObj
*
pVnode
)
{
SVnodesMgmt
*
pMgmt
=
&
pDnode
->
vmgmt
;
pVnode
->
pWriteQ
=
t
M
WorkerAllocQueue
(
&
pMgmt
->
writePool
,
pVnode
,
(
FProcessItems
)
dndProcessVnodeWriteQueue
);
pVnode
->
pApplyQ
=
t
M
WorkerAllocQueue
(
&
pMgmt
->
writePool
,
pVnode
,
(
FProcessItems
)
dndProcessVnodeApplyQueue
);
pVnode
->
pSyncQ
=
t
M
WorkerAllocQueue
(
&
pMgmt
->
syncPool
,
pVnode
,
(
FProcessItems
)
dndProcessVnodeSyncQueue
);
pVnode
->
pFetchQ
=
tWorkerAllocQueue
(
&
pMgmt
->
fetchPool
,
pVnode
,
(
FProcessItem
)
dndProcessVnodeFetchQueue
);
pVnode
->
pQueryQ
=
tWorkerAllocQueue
(
&
pMgmt
->
queryPool
,
pVnode
,
(
FProcessItem
)
dndProcessVnodeQueryQueue
);
pVnode
->
pWriteQ
=
t
W
WorkerAllocQueue
(
&
pMgmt
->
writePool
,
pVnode
,
(
FProcessItems
)
dndProcessVnodeWriteQueue
);
pVnode
->
pApplyQ
=
t
W
WorkerAllocQueue
(
&
pMgmt
->
writePool
,
pVnode
,
(
FProcessItems
)
dndProcessVnodeApplyQueue
);
pVnode
->
pSyncQ
=
t
W
WorkerAllocQueue
(
&
pMgmt
->
syncPool
,
pVnode
,
(
FProcessItems
)
dndProcessVnodeSyncQueue
);
pVnode
->
pFetchQ
=
t
Q
WorkerAllocQueue
(
&
pMgmt
->
fetchPool
,
pVnode
,
(
FProcessItem
)
dndProcessVnodeFetchQueue
);
pVnode
->
pQueryQ
=
t
Q
WorkerAllocQueue
(
&
pMgmt
->
queryPool
,
pVnode
,
(
FProcessItem
)
dndProcessVnodeQueryQueue
);
if
(
pVnode
->
pApplyQ
==
NULL
||
pVnode
->
pWriteQ
==
NULL
||
pVnode
->
pSyncQ
==
NULL
||
pVnode
->
pFetchQ
==
NULL
||
pVnode
->
pQueryQ
==
NULL
)
{
...
...
@@ -965,11 +965,11 @@ static int32_t dndAllocVnodeQueue(SDnode *pDnode, SVnodeObj *pVnode) {
static
void
dndFreeVnodeQueue
(
SDnode
*
pDnode
,
SVnodeObj
*
pVnode
)
{
SVnodesMgmt
*
pMgmt
=
&
pDnode
->
vmgmt
;
tWorkerFreeQueue
(
&
pMgmt
->
queryPool
,
pVnode
->
pQueryQ
);
tWorkerFreeQueue
(
&
pMgmt
->
fetchPool
,
pVnode
->
pFetchQ
);
t
M
WorkerFreeQueue
(
&
pMgmt
->
writePool
,
pVnode
->
pWriteQ
);
t
M
WorkerFreeQueue
(
&
pMgmt
->
writePool
,
pVnode
->
pApplyQ
);
t
M
WorkerFreeQueue
(
&
pMgmt
->
syncPool
,
pVnode
->
pSyncQ
);
t
Q
WorkerFreeQueue
(
&
pMgmt
->
queryPool
,
pVnode
->
pQueryQ
);
t
Q
WorkerFreeQueue
(
&
pMgmt
->
fetchPool
,
pVnode
->
pFetchQ
);
t
W
WorkerFreeQueue
(
&
pMgmt
->
writePool
,
pVnode
->
pWriteQ
);
t
W
WorkerFreeQueue
(
&
pMgmt
->
writePool
,
pVnode
->
pApplyQ
);
t
W
WorkerFreeQueue
(
&
pMgmt
->
syncPool
,
pVnode
->
pSyncQ
);
pVnode
->
pWriteQ
=
NULL
;
pVnode
->
pApplyQ
=
NULL
;
pVnode
->
pSyncQ
=
NULL
;
...
...
source/dnode/mgmt/impl/src/dndWorker.c
浏览文件 @
5fca9015
...
...
@@ -31,28 +31,28 @@ int32_t dndInitWorker(SDnode *pDnode, SDnodeWorker *pWorker, EWorkerType type, c
pWorker
->
pDnode
=
pDnode
;
if
(
pWorker
->
type
==
DND_WORKER_SINGLE
)
{
SWorkerPool
*
pPool
=
&
pWorker
->
pool
;
S
Q
WorkerPool
*
pPool
=
&
pWorker
->
pool
;
pPool
->
name
=
name
;
pPool
->
min
=
minNum
;
pPool
->
max
=
maxNum
;
if
(
tWorkerInit
(
pPool
)
!=
0
)
{
if
(
t
Q
WorkerInit
(
pPool
)
!=
0
)
{
terrno
=
TSDB_CODE_OUT_OF_MEMORY
;
return
-
1
;
}
pWorker
->
queue
=
tWorkerAllocQueue
(
pPool
,
pDnode
,
(
FProcessItem
)
queueFp
);
pWorker
->
queue
=
t
Q
WorkerAllocQueue
(
pPool
,
pDnode
,
(
FProcessItem
)
queueFp
);
if
(
pWorker
->
queue
==
NULL
)
{
terrno
=
TSDB_CODE_OUT_OF_MEMORY
;
return
-
1
;
}
}
else
if
(
pWorker
->
type
==
DND_WORKER_MULTI
)
{
S
M
WorkerPool
*
pPool
=
&
pWorker
->
mpool
;
S
W
WorkerPool
*
pPool
=
&
pWorker
->
mpool
;
pPool
->
name
=
name
;
pPool
->
max
=
maxNum
;
if
(
t
M
WorkerInit
(
pPool
)
!=
0
)
{
if
(
t
W
WorkerInit
(
pPool
)
!=
0
)
{
terrno
=
TSDB_CODE_OUT_OF_MEMORY
;
return
-
1
;
}
pWorker
->
queue
=
t
M
WorkerAllocQueue
(
pPool
,
pDnode
,
(
FProcessItems
)
queueFp
);
pWorker
->
queue
=
t
W
WorkerAllocQueue
(
pPool
,
pDnode
,
(
FProcessItems
)
queueFp
);
if
(
pWorker
->
queue
==
NULL
)
{
terrno
=
TSDB_CODE_OUT_OF_MEMORY
;
return
-
1
;
...
...
@@ -70,11 +70,11 @@ void dndCleanupWorker(SDnodeWorker *pWorker) {
}
if
(
pWorker
->
type
==
DND_WORKER_SINGLE
)
{
tWorkerCleanup
(
&
pWorker
->
pool
);
tWorkerFreeQueue
(
&
pWorker
->
pool
,
pWorker
->
queue
);
t
Q
WorkerCleanup
(
&
pWorker
->
pool
);
t
Q
WorkerFreeQueue
(
&
pWorker
->
pool
,
pWorker
->
queue
);
}
else
if
(
pWorker
->
type
==
DND_WORKER_MULTI
)
{
t
M
WorkerCleanup
(
&
pWorker
->
mpool
);
t
M
WorkerFreeQueue
(
&
pWorker
->
mpool
,
pWorker
->
queue
);
t
W
WorkerCleanup
(
&
pWorker
->
mpool
);
t
W
WorkerFreeQueue
(
&
pWorker
->
mpool
,
pWorker
->
queue
);
}
else
{
}
}
...
...
source/util/src/tworker.c
浏览文件 @
5fca9015
...
...
@@ -14,38 +14,39 @@
*/
#define _DEFAULT_SOURCE
#include "os.h"
#include "ulog.h"
#include "tqueue.h"
#include "tworker.h"
#include "ulog.h"
typedef
void
*
(
*
ThreadFp
)(
void
*
param
);
typedef
void
*
(
*
ThreadFp
)(
void
*
param
);
int32_t
t
WorkerInit
(
S
WorkerPool
*
pool
)
{
int32_t
t
QWorkerInit
(
SQ
WorkerPool
*
pool
)
{
pool
->
qset
=
taosOpenQset
();
pool
->
workers
=
calloc
(
sizeof
(
SWorker
),
pool
->
max
);
pthread_mutex_init
(
&
pool
->
mutex
,
NULL
);
for
(
int
i
=
0
;
i
<
pool
->
max
;
++
i
)
{
SWorker
*
worker
=
pool
->
workers
+
i
;
pool
->
workers
=
calloc
(
sizeof
(
SQWorker
),
pool
->
max
);
if
(
pthread_mutex_init
(
&
pool
->
mutex
,
NULL
))
{
return
-
1
;
}
for
(
int32_t
i
=
0
;
i
<
pool
->
max
;
++
i
)
{
SQWorker
*
worker
=
pool
->
workers
+
i
;
worker
->
id
=
i
;
worker
->
pool
=
pool
;
}
u
Info
(
"
worker:%s is initialized, min:%d max:%d"
,
pool
->
name
,
pool
->
min
,
pool
->
max
);
u
Debug
(
"q
worker:%s is initialized, min:%d max:%d"
,
pool
->
name
,
pool
->
min
,
pool
->
max
);
return
0
;
}
void
t
WorkerCleanup
(
S
WorkerPool
*
pool
)
{
for
(
int
i
=
0
;
i
<
pool
->
max
;
++
i
)
{
SWorker
*
worker
=
pool
->
workers
+
i
;
void
t
QWorkerCleanup
(
SQ
WorkerPool
*
pool
)
{
for
(
int
32_t
i
=
0
;
i
<
pool
->
max
;
++
i
)
{
S
Q
Worker
*
worker
=
pool
->
workers
+
i
;
if
(
worker
==
NULL
)
continue
;
if
(
taosCheckPthreadValid
(
worker
->
thread
))
{
taosQsetThreadResume
(
pool
->
qset
);
}
}
for
(
int
i
=
0
;
i
<
pool
->
max
;
++
i
)
{
SWorker
*
worker
=
pool
->
workers
+
i
;
for
(
int
32_t
i
=
0
;
i
<
pool
->
max
;
++
i
)
{
S
Q
Worker
*
worker
=
pool
->
workers
+
i
;
if
(
worker
==
NULL
)
continue
;
if
(
taosCheckPthreadValid
(
worker
->
thread
))
{
pthread_join
(
worker
->
thread
,
NULL
);
...
...
@@ -56,28 +57,28 @@ void tWorkerCleanup(SWorkerPool *pool) {
taosCloseQset
(
pool
->
qset
);
pthread_mutex_destroy
(
&
pool
->
mutex
);
u
Info
(
"
worker:%s is closed"
,
pool
->
name
);
u
Debug
(
"q
worker:%s is closed"
,
pool
->
name
);
}
static
void
*
t
WorkerThreadFp
(
S
Worker
*
worker
)
{
SWorkerPool
*
pool
=
worker
->
pool
;
FProcessItem
fp
=
NULL
;
static
void
*
t
QWorkerThreadFp
(
SQ
Worker
*
worker
)
{
S
Q
WorkerPool
*
pool
=
worker
->
pool
;
FProcessItem
fp
=
NULL
;
void
*
msg
=
NULL
;
void
*
ahandle
=
NULL
;
void
*
msg
=
NULL
;
void
*
ahandle
=
NULL
;
int32_t
code
=
0
;
taosBlockSIGPIPE
();
setThreadName
(
pool
->
name
);
uDebug
(
"worker:%s:%d is running"
,
pool
->
name
,
worker
->
id
);
uDebug
(
"
q
worker:%s:%d is running"
,
pool
->
name
,
worker
->
id
);
while
(
1
)
{
if
(
taosReadQitemFromQset
(
pool
->
qset
,
(
void
**
)
&
msg
,
&
ahandle
,
&
fp
)
==
0
)
{
uDebug
(
"worker:%s:%d qset:%p, got no message and exiting"
,
pool
->
name
,
worker
->
id
,
pool
->
qset
);
uDebug
(
"
q
worker:%s:%d qset:%p, got no message and exiting"
,
pool
->
name
,
worker
->
id
,
pool
->
qset
);
break
;
}
if
(
fp
)
{
if
(
fp
!=
NULL
)
{
(
*
fp
)(
ahandle
,
msg
);
}
}
...
...
@@ -85,7 +86,7 @@ static void *tWorkerThreadFp(SWorker *worker) {
return
NULL
;
}
STaosQueue
*
t
WorkerAllocQueue
(
S
WorkerPool
*
pool
,
void
*
ahandle
,
FProcessItem
fp
)
{
STaosQueue
*
t
QWorkerAllocQueue
(
SQ
WorkerPool
*
pool
,
void
*
ahandle
,
FProcessItem
fp
)
{
pthread_mutex_lock
(
&
pool
->
mutex
);
STaosQueue
*
queue
=
taosOpenQueue
();
if
(
queue
==
NULL
)
{
...
...
@@ -99,61 +100,66 @@ STaosQueue *tWorkerAllocQueue(SWorkerPool *pool, void *ahandle, FProcessItem fp)
// spawn a thread to process queue
if
(
pool
->
num
<
pool
->
max
)
{
do
{
SWorker
*
worker
=
pool
->
workers
+
pool
->
num
;
S
Q
Worker
*
worker
=
pool
->
workers
+
pool
->
num
;
pthread_attr_t
thAttr
;
pthread_attr_init
(
&
thAttr
);
pthread_attr_setdetachstate
(
&
thAttr
,
PTHREAD_CREATE_JOINABLE
);
if
(
pthread_create
(
&
worker
->
thread
,
&
thAttr
,
(
ThreadFp
)
tWorkerThreadFp
,
worker
)
!=
0
)
{
uError
(
"worker:%s:%d failed to create thread to process since %s"
,
pool
->
name
,
worker
->
id
,
strerror
(
errno
));
if
(
pthread_create
(
&
worker
->
thread
,
&
thAttr
,
(
ThreadFp
)
tQWorkerThreadFp
,
worker
)
!=
0
)
{
uError
(
"qworker:%s:%d failed to create thread to process since %s"
,
pool
->
name
,
worker
->
id
,
strerror
(
errno
));
taosCloseQueue
(
queue
);
queue
=
NULL
;
break
;
}
pthread_attr_destroy
(
&
thAttr
);
pool
->
num
++
;
uDebug
(
"worker:%s:%d is launched, total:%d"
,
pool
->
name
,
worker
->
id
,
pool
->
num
);
uDebug
(
"
q
worker:%s:%d is launched, total:%d"
,
pool
->
name
,
worker
->
id
,
pool
->
num
);
}
while
(
pool
->
num
<
pool
->
min
);
}
pthread_mutex_unlock
(
&
pool
->
mutex
);
uDebug
(
"worker:%s, queue:%p is allocated, ahandle:%p"
,
pool
->
name
,
queue
,
ahandle
);
uDebug
(
"
q
worker:%s, queue:%p is allocated, ahandle:%p"
,
pool
->
name
,
queue
,
ahandle
);
return
queue
;
}
void
t
WorkerFreeQueue
(
S
WorkerPool
*
pool
,
STaosQueue
*
queue
)
{
void
t
QWorkerFreeQueue
(
SQ
WorkerPool
*
pool
,
STaosQueue
*
queue
)
{
taosCloseQueue
(
queue
);
uDebug
(
"worker:%s, queue:%p is freed"
,
pool
->
name
,
queue
);
uDebug
(
"
q
worker:%s, queue:%p is freed"
,
pool
->
name
,
queue
);
}
int32_t
t
MWorkerInit
(
SM
WorkerPool
*
pool
)
{
int32_t
t
WWorkerInit
(
SW
WorkerPool
*
pool
)
{
pool
->
nextId
=
0
;
pool
->
workers
=
calloc
(
sizeof
(
S
M
Worker
),
pool
->
max
);
pool
->
workers
=
calloc
(
sizeof
(
S
W
Worker
),
pool
->
max
);
if
(
pool
->
workers
==
NULL
)
return
-
1
;
pthread_mutex_init
(
&
pool
->
mutex
,
NULL
);
for
(
int32_t
i
=
0
;
i
<
pool
->
max
;
++
i
)
{
S
M
Worker
*
worker
=
pool
->
workers
+
i
;
S
W
Worker
*
worker
=
pool
->
workers
+
i
;
worker
->
id
=
i
;
worker
->
qall
=
NULL
;
worker
->
qset
=
NULL
;
worker
->
pool
=
pool
;
}
uInfo
(
"worker:%s is initialized, max:%d"
,
pool
->
name
,
pool
->
max
);
uInfo
(
"w
w
orker:%s is initialized, max:%d"
,
pool
->
name
,
pool
->
max
);
return
0
;
}
void
t
MWorkerCleanup
(
SM
WorkerPool
*
pool
)
{
void
t
WWorkerCleanup
(
SW
WorkerPool
*
pool
)
{
for
(
int32_t
i
=
0
;
i
<
pool
->
max
;
++
i
)
{
S
M
Worker
*
worker
=
pool
->
workers
+
i
;
S
W
Worker
*
worker
=
pool
->
workers
+
i
;
if
(
taosCheckPthreadValid
(
worker
->
thread
))
{
if
(
worker
->
qset
)
taosQsetThreadResume
(
worker
->
qset
);
if
(
worker
->
qset
)
{
taosQsetThreadResume
(
worker
->
qset
);
}
}
}
for
(
int32_t
i
=
0
;
i
<
pool
->
max
;
++
i
)
{
S
M
Worker
*
worker
=
pool
->
workers
+
i
;
S
W
Worker
*
worker
=
pool
->
workers
+
i
;
if
(
taosCheckPthreadValid
(
worker
->
thread
))
{
pthread_join
(
worker
->
thread
,
NULL
);
taosFreeQall
(
worker
->
qall
);
...
...
@@ -164,30 +170,30 @@ void tMWorkerCleanup(SMWorkerPool *pool) {
tfree
(
pool
->
workers
);
pthread_mutex_destroy
(
&
pool
->
mutex
);
uInfo
(
"worker:%s is closed"
,
pool
->
name
);
uInfo
(
"w
w
orker:%s is closed"
,
pool
->
name
);
}
static
void
*
tWriteWorkerThreadFp
(
S
M
Worker
*
worker
)
{
S
M
WorkerPool
*
pool
=
worker
->
pool
;
static
void
*
tWriteWorkerThreadFp
(
S
W
Worker
*
worker
)
{
S
W
WorkerPool
*
pool
=
worker
->
pool
;
FProcessItems
fp
=
NULL
;
void
*
msg
=
NULL
;
void
*
ahandle
=
NULL
;
void
*
msg
=
NULL
;
void
*
ahandle
=
NULL
;
int32_t
numOfMsgs
=
0
;
int32_t
qtype
=
0
;
taosBlockSIGPIPE
();
setThreadName
(
pool
->
name
);
uDebug
(
"worker:%s:%d is running"
,
pool
->
name
,
worker
->
id
);
uDebug
(
"w
w
orker:%s:%d is running"
,
pool
->
name
,
worker
->
id
);
while
(
1
)
{
numOfMsgs
=
taosReadAllQitemsFromQset
(
worker
->
qset
,
worker
->
qall
,
&
ahandle
,
&
fp
);
if
(
numOfMsgs
==
0
)
{
uDebug
(
"worker:%s:%d qset:%p, got no message and exiting"
,
pool
->
name
,
worker
->
id
,
worker
->
qset
);
uDebug
(
"w
w
orker:%s:%d qset:%p, got no message and exiting"
,
pool
->
name
,
worker
->
id
,
worker
->
qset
);
break
;
}
if
(
fp
)
{
if
(
fp
!=
NULL
)
{
(
*
fp
)(
ahandle
,
worker
->
qall
,
numOfMsgs
);
}
}
...
...
@@ -195,9 +201,9 @@ static void *tWriteWorkerThreadFp(SMWorker *worker) {
return
NULL
;
}
STaosQueue
*
t
MWorkerAllocQueue
(
SM
WorkerPool
*
pool
,
void
*
ahandle
,
FProcessItems
fp
)
{
STaosQueue
*
t
WWorkerAllocQueue
(
SW
WorkerPool
*
pool
,
void
*
ahandle
,
FProcessItems
fp
)
{
pthread_mutex_lock
(
&
pool
->
mutex
);
S
M
Worker
*
worker
=
pool
->
workers
+
pool
->
nextId
;
S
W
Worker
*
worker
=
pool
->
workers
+
pool
->
nextId
;
STaosQueue
*
queue
=
taosOpenQueue
();
if
(
queue
==
NULL
)
{
...
...
@@ -228,13 +234,13 @@ STaosQueue *tMWorkerAllocQueue(SMWorkerPool *pool, void *ahandle, FProcessItems
pthread_attr_setdetachstate
(
&
thAttr
,
PTHREAD_CREATE_JOINABLE
);
if
(
pthread_create
(
&
worker
->
thread
,
&
thAttr
,
(
ThreadFp
)
tWriteWorkerThreadFp
,
worker
)
!=
0
)
{
uError
(
"worker:%s:%d failed to create thread to process since %s"
,
pool
->
name
,
worker
->
id
,
strerror
(
errno
));
uError
(
"w
w
orker:%s:%d failed to create thread to process since %s"
,
pool
->
name
,
worker
->
id
,
strerror
(
errno
));
taosFreeQall
(
worker
->
qall
);
taosCloseQset
(
worker
->
qset
);
taosCloseQueue
(
queue
);
queue
=
NULL
;
}
else
{
uDebug
(
"worker:%s:%d is launched, max:%d"
,
pool
->
name
,
worker
->
id
,
pool
->
max
);
uDebug
(
"w
w
orker:%s:%d is launched, max:%d"
,
pool
->
name
,
worker
->
id
,
pool
->
max
);
pool
->
nextId
=
(
pool
->
nextId
+
1
)
%
pool
->
max
;
}
...
...
@@ -245,12 +251,12 @@ STaosQueue *tMWorkerAllocQueue(SMWorkerPool *pool, void *ahandle, FProcessItems
}
pthread_mutex_unlock
(
&
pool
->
mutex
);
uDebug
(
"worker:%s, queue:%p is allocated, ahandle:%p"
,
pool
->
name
,
queue
,
ahandle
);
uDebug
(
"w
w
orker:%s, queue:%p is allocated, ahandle:%p"
,
pool
->
name
,
queue
,
ahandle
);
return
queue
;
}
void
t
MWorkerFreeQueue
(
SM
WorkerPool
*
pool
,
STaosQueue
*
queue
)
{
void
t
WWorkerFreeQueue
(
SW
WorkerPool
*
pool
,
STaosQueue
*
queue
)
{
taosCloseQueue
(
queue
);
uDebug
(
"worker:%s, queue:%p is freed"
,
pool
->
name
,
queue
);
uDebug
(
"w
w
orker:%s, queue:%p is freed"
,
pool
->
name
,
queue
);
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录