Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
59d7cb6d
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,发现更多精彩内容 >>
未验证
提交
59d7cb6d
编写于
1月 25, 2022
作者:
S
Shengliang Guan
提交者:
GitHub
1月 25, 2022
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #10017 from taosdata/feature/mnode
refact worker for fetch
上级
fdf97248
07978790
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
318 addition
and
165 deletion
+318
-165
include/util/tqueue.h
include/util/tqueue.h
+10
-6
include/util/tworker.h
include/util/tworker.h
+34
-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
+34
-34
source/dnode/mgmt/impl/src/dndWorker.c
source/dnode/mgmt/impl/src/dndWorker.c
+10
-10
source/util/src/tqueue.c
source/util/src/tqueue.c
+107
-34
source/util/src/tworker.c
source/util/src/tworker.c
+117
-45
未找到文件。
include/util/tqueue.h
浏览文件 @
59d7cb6d
...
...
@@ -15,6 +15,7 @@
#ifndef _TD_UTIL_QUEUE_H
#define _TD_UTIL_QUEUE_H
#include "os.h"
#ifdef __cplusplus
extern
"C"
{
...
...
@@ -40,13 +41,13 @@ shall be used to set up the protection.
typedef
struct
STaosQueue
STaosQueue
;
typedef
struct
STaosQset
STaosQset
;
typedef
struct
STaosQall
STaosQall
;
typedef
void
(
*
F
Process
Item
)(
void
*
ahandle
,
void
*
pItem
);
typedef
void
(
*
F
Process
Items
)(
void
*
ahandle
,
STaosQall
*
qall
,
int32_t
numOfItems
);
typedef
void
(
*
FItem
)(
void
*
ahandle
,
void
*
pItem
);
typedef
void
(
*
FItems
)(
void
*
ahandle
,
STaosQall
*
qall
,
int32_t
numOfItems
);
STaosQueue
*
taosOpenQueue
();
void
taosCloseQueue
(
STaosQueue
*
queue
);
void
taosSetQueueFp
(
STaosQueue
*
queue
,
F
ProcessItem
itemFp
,
FProcess
Items
itemsFp
);
void
*
taosAllocateQitem
(
int32_t
size
);
void
taosSetQueueFp
(
STaosQueue
*
queue
,
F
Item
itemFp
,
F
Items
itemsFp
);
void
*
taosAllocateQitem
(
int32_t
size
);
void
taosFreeQitem
(
void
*
pItem
);
int32_t
taosWriteQitem
(
STaosQueue
*
queue
,
void
*
pItem
);
int32_t
taosReadQitem
(
STaosQueue
*
queue
,
void
**
ppItem
);
...
...
@@ -66,8 +67,11 @@ int32_t taosAddIntoQset(STaosQset *qset, STaosQueue *queue, void *ahandle);
void
taosRemoveFromQset
(
STaosQset
*
qset
,
STaosQueue
*
queue
);
int32_t
taosGetQueueNumber
(
STaosQset
*
qset
);
int32_t
taosReadQitemFromQset
(
STaosQset
*
qset
,
void
**
ppItem
,
void
**
ahandle
,
FProcessItem
*
itemFp
);
int32_t
taosReadAllQitemsFromQset
(
STaosQset
*
qset
,
STaosQall
*
qall
,
void
**
ahandle
,
FProcessItems
*
itemsFp
);
int32_t
taosReadQitemFromQset
(
STaosQset
*
qset
,
void
**
ppItem
,
void
**
ahandle
,
FItem
*
itemFp
);
int32_t
taosReadAllQitemsFromQset
(
STaosQset
*
qset
,
STaosQall
*
qall
,
void
**
ahandle
,
FItems
*
itemsFp
);
int32_t
taosReadQitemFromQsetByThread
(
STaosQset
*
qset
,
void
**
ppItem
,
void
**
ahandle
,
FItem
*
itemFp
,
int32_t
threadId
);
void
taosResetQsetThread
(
STaosQset
*
qset
,
void
*
pItem
);
int32_t
taosGetQueueItemsNumber
(
STaosQueue
*
queue
);
int32_t
taosGetQsetItemsNumber
(
STaosQset
*
qset
);
...
...
include/util/tworker.h
浏览文件 @
59d7cb6d
...
...
@@ -15,57 +15,61 @@
#ifndef _TD_UTIL_WORKER_H
#define _TD_UTIL_WORKER_H
#include "tqueue.h"
#ifdef __cplusplus
extern
"C"
{
#endif
typedef
struct
S
WorkerPool
S
WorkerPool
;
typedef
struct
S
MWorkerPool
SM
WorkerPool
;
typedef
struct
S
QWorkerPool
SQ
WorkerPool
;
typedef
struct
S
WWorkerPool
SW
WorkerPool
;
typedef
struct
SWorker
{
int32_t
id
;
// worker ID
pthread_t
thread
;
// thread
SWorkerPool
*
pool
;
}
SWorker
;
typedef
struct
S
Q
Worker
{
int32_t
id
;
// worker ID
pthread_t
thread
;
// thread
S
Q
WorkerPool
*
pool
;
}
S
QWorker
,
SF
Worker
;
typedef
struct
SWorkerPool
{
typedef
struct
S
Q
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
QWorker
*
workers
;
pthread_mutex_t
mutex
;
}
SWorkerPool
;
}
S
QWorkerPool
,
SF
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
;
}
SMWorkerPool
;
}
SWWorkerPool
;
int32_t
tQWorkerInit
(
SQWorkerPool
*
pool
);
void
tQWorkerCleanup
(
SQWorkerPool
*
pool
);
STaosQueue
*
tQWorkerAllocQueue
(
SQWorkerPool
*
pool
,
void
*
ahandle
,
FItem
fp
);
void
tQWorkerFreeQueue
(
SQWorkerPool
*
pool
,
STaosQueue
*
queue
);
int32_t
t
WorkerInit
(
S
WorkerPool
*
pool
);
void
t
WorkerCleanup
(
S
WorkerPool
*
pool
);
STaosQueue
*
t
WorkerAllocQueue
(
SWorkerPool
*
pool
,
void
*
ahandle
,
FProcess
Item
fp
);
void
t
WorkerFreeQueue
(
S
WorkerPool
*
pool
,
STaosQueue
*
queue
);
int32_t
t
FWorkerInit
(
SF
WorkerPool
*
pool
);
void
t
FWorkerCleanup
(
SF
WorkerPool
*
pool
);
STaosQueue
*
t
FWorkerAllocQueue
(
SFWorkerPool
*
pool
,
void
*
ahandle
,
F
Item
fp
);
void
t
FWorkerFreeQueue
(
SF
WorkerPool
*
pool
,
STaosQueue
*
queue
);
int32_t
t
MWorkerInit
(
SM
WorkerPool
*
pool
);
void
t
MWorkerCleanup
(
SM
WorkerPool
*
pool
);
STaosQueue
*
t
MWorkerAllocQueue
(
SMWorkerPool
*
pool
,
void
*
ahandle
,
FProcess
Items
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
(
SWWorkerPool
*
pool
,
void
*
ahandle
,
F
Items
fp
);
void
t
WWorkerFreeQueue
(
SW
WorkerPool
*
pool
,
STaosQueue
*
queue
);
#ifdef __cplusplus
}
...
...
source/dnode/mgmt/impl/inc/dndEnv.h
浏览文件 @
59d7cb6d
...
...
@@ -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
FWorkerPool
fetchPool
;
S
W
WorkerPool
syncPool
;
S
W
WorkerPool
writePool
;
}
SVnodesMgmt
;
typedef
struct
{
...
...
source/dnode/mgmt/impl/src/dndVnodes.c
浏览文件 @
59d7cb6d
...
...
@@ -253,7 +253,7 @@ static int32_t dndGetVnodesFromFile(SDnode *pDnode, SWrapperCfg **ppCfgs, int32_
}
for
(
int32_t
i
=
0
;
i
<
vnodesNum
;
++
i
)
{
cJSON
*
vnode
=
cJSON_GetArrayItem
(
vnodes
,
i
);
cJSON
*
vnode
=
cJSON_GetArrayItem
(
vnodes
,
i
);
SWrapperCfg
*
pCfg
=
&
pCfgs
[
i
];
cJSON
*
vgId
=
cJSON_GetObjectItem
(
vnode
,
"vgId"
);
...
...
@@ -382,7 +382,7 @@ static void *dnodeOpenVnodeFunc(void *param) {
dndReportStartup
(
pDnode
,
"open-vnodes"
,
stepDesc
);
SVnodeCfg
cfg
=
{.
pDnode
=
pDnode
,
.
pTfs
=
pDnode
->
pTfs
,
.
vgId
=
pCfg
->
vgId
};
SVnode
*
pImpl
=
vnodeOpen
(
pCfg
->
path
,
&
cfg
);
SVnode
*
pImpl
=
vnodeOpen
(
pCfg
->
path
,
&
cfg
);
if
(
pImpl
==
NULL
)
{
dError
(
"vgId:%d, failed to open vnode by thread:%d"
,
pCfg
->
vgId
,
pThread
->
threadIndex
);
pThread
->
failed
++
;
...
...
@@ -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
);
S
WorkerPool
*
p
Pool
=
&
pMgmt
->
queryPool
;
pPool
->
name
=
"vnode-query"
;
pPool
->
min
=
minQueryThreads
;
pPool
->
max
=
maxQueryThreads
;
if
(
t
WorkerInit
(
p
Pool
)
!=
0
)
return
-
1
;
S
QWorkerPool
*
pQ
Pool
=
&
pMgmt
->
queryPool
;
p
Q
Pool
->
name
=
"vnode-query"
;
p
Q
Pool
->
min
=
minQueryThreads
;
p
Q
Pool
->
max
=
maxQueryThreads
;
if
(
t
QWorkerInit
(
pQ
Pool
)
!=
0
)
return
-
1
;
p
Pool
=
&
pMgmt
->
fetchPool
;
pPool
->
name
=
"vnode-fetch"
;
pPool
->
min
=
minFetchThreads
;
pPool
->
max
=
maxFetchThreads
;
if
(
t
WorkerInit
(
p
Pool
)
!=
0
)
return
-
1
;
SFWorkerPool
*
pF
Pool
=
&
pMgmt
->
fetchPool
;
p
F
Pool
->
name
=
"vnode-fetch"
;
p
F
Pool
->
min
=
minFetchThreads
;
p
F
Pool
->
max
=
maxFetchThreads
;
if
(
t
FWorkerInit
(
pF
Pool
)
!=
0
)
return
-
1
;
S
MWorkerPool
*
pM
Pool
=
&
pMgmt
->
writePool
;
p
M
Pool
->
name
=
"vnode-write"
;
p
M
Pool
->
max
=
maxWriteThreads
;
if
(
t
MWorkerInit
(
pM
Pool
)
!=
0
)
return
-
1
;
S
WWorkerPool
*
pW
Pool
=
&
pMgmt
->
writePool
;
p
W
Pool
->
name
=
"vnode-write"
;
p
W
Pool
->
max
=
maxWriteThreads
;
if
(
t
WWorkerInit
(
pW
Pool
)
!=
0
)
return
-
1
;
p
M
Pool
=
&
pMgmt
->
syncPool
;
p
M
Pool
->
name
=
"vnode-sync"
;
p
M
Pool
->
max
=
maxSyncThreads
;
if
(
t
MWorkerInit
(
pM
Pool
)
!=
0
)
return
-
1
;
p
W
Pool
=
&
pMgmt
->
syncPool
;
p
W
Pool
->
name
=
"vnode-sync"
;
p
W
Pool
->
max
=
maxSyncThreads
;
if
(
t
WWorkerInit
(
pW
Pool
)
!=
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
F
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
MWorkerAllocQueue
(
&
pMgmt
->
writePool
,
pVnode
,
(
FProcess
Items
)
dndProcessVnodeWriteQueue
);
pVnode
->
pApplyQ
=
t
MWorkerAllocQueue
(
&
pMgmt
->
writePool
,
pVnode
,
(
FProcess
Items
)
dndProcessVnodeApplyQueue
);
pVnode
->
pSyncQ
=
t
MWorkerAllocQueue
(
&
pMgmt
->
syncPool
,
pVnode
,
(
FProcess
Items
)
dndProcessVnodeSyncQueue
);
pVnode
->
pFetchQ
=
t
WorkerAllocQueue
(
&
pMgmt
->
fetchPool
,
pVnode
,
(
FProcess
Item
)
dndProcessVnodeFetchQueue
);
pVnode
->
pQueryQ
=
t
WorkerAllocQueue
(
&
pMgmt
->
queryPool
,
pVnode
,
(
FProcess
Item
)
dndProcessVnodeQueryQueue
);
pVnode
->
pWriteQ
=
t
WWorkerAllocQueue
(
&
pMgmt
->
writePool
,
pVnode
,
(
F
Items
)
dndProcessVnodeWriteQueue
);
pVnode
->
pApplyQ
=
t
WWorkerAllocQueue
(
&
pMgmt
->
writePool
,
pVnode
,
(
F
Items
)
dndProcessVnodeApplyQueue
);
pVnode
->
pSyncQ
=
t
WWorkerAllocQueue
(
&
pMgmt
->
syncPool
,
pVnode
,
(
F
Items
)
dndProcessVnodeSyncQueue
);
pVnode
->
pFetchQ
=
t
FWorkerAllocQueue
(
&
pMgmt
->
fetchPool
,
pVnode
,
(
F
Item
)
dndProcessVnodeFetchQueue
);
pVnode
->
pQueryQ
=
t
QWorkerAllocQueue
(
&
pMgmt
->
queryPool
,
pVnode
,
(
F
Item
)
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
F
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
浏览文件 @
59d7cb6d
...
...
@@ -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
=
t
WorkerAllocQueue
(
pPool
,
pDnode
,
(
FProcess
Item
)
queueFp
);
pWorker
->
queue
=
t
QWorkerAllocQueue
(
pPool
,
pDnode
,
(
F
Item
)
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
MWorkerAllocQueue
(
pPool
,
pDnode
,
(
FProcess
Items
)
queueFp
);
pWorker
->
queue
=
t
WWorkerAllocQueue
(
pPool
,
pDnode
,
(
F
Items
)
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/tqueue.c
浏览文件 @
59d7cb6d
...
...
@@ -13,35 +13,36 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "os.h"
#include "taoserror.h"
#define _DEFAULT_SOURCE
#include "tqueue.h"
#include "taoserror.h"
#include "ulog.h"
typedef
struct
STaosQnode
STaosQnode
;
typedef
struct
STaosQnode
{
STaosQnode
*
next
;
STaosQueue
*
queue
;
char
item
[];
}
STaosQnode
;
typedef
struct
STaosQueue
{
int32_t
itemSize
;
int32_t
numOfItems
;
STaosQnode
*
head
;
STaosQnode
*
tail
;
STaosQueue
*
next
;
// for queue set
STaosQset
*
qset
;
// for queue set
void
*
ahandle
;
// for queue set
FProcessItem
itemFp
;
FProcessItems
itemsFp
;
int32_t
threadId
;
STaosQnode
*
head
;
STaosQnode
*
tail
;
STaosQueue
*
next
;
// for queue set
STaosQset
*
qset
;
// for queue set
void
*
ahandle
;
// for queue set
FItem
itemFp
;
FItems
itemsFp
;
pthread_mutex_t
mutex
;
}
STaosQueue
;
typedef
struct
STaosQset
{
STaosQueue
*
head
;
STaosQueue
*
current
;
STaosQueue
*
head
;
STaosQueue
*
current
;
pthread_mutex_t
mutex
;
int32_t
numOfQueues
;
int32_t
numOfItems
;
...
...
@@ -56,19 +57,23 @@ typedef struct STaosQall {
}
STaosQall
;
STaosQueue
*
taosOpenQueue
()
{
STaosQueue
*
queue
=
calloc
(
sizeof
(
STaosQueue
),
1
);
STaosQueue
*
queue
=
calloc
(
1
,
sizeof
(
STaosQueue
)
);
if
(
queue
==
NULL
)
{
terrno
=
TSDB_CODE_OUT_OF_MEMORY
;
return
NULL
;
}
pthread_mutex_init
(
&
queue
->
mutex
,
NULL
);
if
(
pthread_mutex_init
(
&
queue
->
mutex
,
NULL
)
!=
0
)
{
terrno
=
TSDB_CODE_OUT_OF_MEMORY
;
return
NULL
;
}
uTrace
(
"queue:%p is opened"
,
queue
);
queue
->
threadId
=
-
1
;
uDebug
(
"queue:%p is opened"
,
queue
);
return
queue
;
}
void
taosSetQueueFp
(
STaosQueue
*
queue
,
F
ProcessItem
itemFp
,
FProcess
Items
itemsFp
)
{
void
taosSetQueueFp
(
STaosQueue
*
queue
,
F
Item
itemFp
,
F
Items
itemsFp
)
{
if
(
queue
==
NULL
)
return
;
queue
->
itemFp
=
itemFp
;
queue
->
itemsFp
=
itemsFp
;
...
...
@@ -77,7 +82,7 @@ void taosSetQueueFp(STaosQueue *queue, FProcessItem itemFp, FProcessItems itemsF
void
taosCloseQueue
(
STaosQueue
*
queue
)
{
if
(
queue
==
NULL
)
return
;
STaosQnode
*
pTemp
;
STaosQset
*
qset
;
STaosQset
*
qset
;
pthread_mutex_lock
(
&
queue
->
mutex
);
STaosQnode
*
pNode
=
queue
->
head
;
...
...
@@ -85,7 +90,9 @@ void taosCloseQueue(STaosQueue *queue) {
qset
=
queue
->
qset
;
pthread_mutex_unlock
(
&
queue
->
mutex
);
if
(
queue
->
qset
)
taosRemoveFromQset
(
qset
,
queue
);
if
(
queue
->
qset
)
{
taosRemoveFromQset
(
qset
,
queue
);
}
while
(
pNode
)
{
pTemp
=
pNode
;
...
...
@@ -96,7 +103,7 @@ void taosCloseQueue(STaosQueue *queue) {
pthread_mutex_destroy
(
&
queue
->
mutex
);
free
(
queue
);
u
Trace
(
"queue:%p is closed"
,
queue
);
u
Debug
(
"queue:%p is closed"
,
queue
);
}
bool
taosQueueEmpty
(
STaosQueue
*
queue
)
{
...
...
@@ -120,19 +127,23 @@ int32_t taosQueueSize(STaosQueue *queue) {
}
void
*
taosAllocateQitem
(
int32_t
size
)
{
STaosQnode
*
pNode
=
(
STaosQnode
*
)
calloc
(
sizeof
(
STaosQnode
)
+
size
,
1
);
STaosQnode
*
pNode
=
calloc
(
1
,
sizeof
(
STaosQnode
)
+
size
);
if
(
pNode
==
NULL
)
{
terrno
=
TSDB_CODE_OUT_OF_MEMORY
;
return
NULL
;
}
if
(
pNode
==
NULL
)
return
NULL
;
uTrace
(
"item:%p, node:%p is allocated"
,
pNode
->
item
,
pNode
);
return
(
void
*
)
pNode
->
item
;
}
void
taosFreeQitem
(
void
*
p
ara
m
)
{
if
(
p
ara
m
==
NULL
)
return
;
void
taosFreeQitem
(
void
*
p
Ite
m
)
{
if
(
p
Ite
m
==
NULL
)
return
;
char
*
temp
=
(
char
*
)
para
m
;
char
*
temp
=
pIte
m
;
temp
-=
sizeof
(
STaosQnode
);
uTrace
(
"item:%p, node:%p is freed"
,
p
ara
m
,
temp
);
uTrace
(
"item:%p, node:%p is freed"
,
p
Ite
m
,
temp
);
free
(
temp
);
}
...
...
@@ -175,7 +186,7 @@ int32_t taosReadQitem(STaosQueue *queue, void **ppItem) {
queue
->
numOfItems
--
;
if
(
queue
->
qset
)
atomic_sub_fetch_32
(
&
queue
->
qset
->
numOfItems
,
1
);
code
=
1
;
u
Debug
(
"item:%p is read out from queue:%p, items:%d"
,
*
ppItem
,
queue
,
queue
->
numOfItems
);
u
Trace
(
"item:%p is read out from queue:%p, items:%d"
,
*
ppItem
,
queue
,
queue
->
numOfItems
);
}
pthread_mutex_unlock
(
&
queue
->
mutex
);
...
...
@@ -183,7 +194,7 @@ int32_t taosReadQitem(STaosQueue *queue, void **ppItem) {
return
code
;
}
STaosQall
*
taosAllocateQall
()
{
return
calloc
(
sizeof
(
STaosQall
),
1
);
}
STaosQall
*
taosAllocateQall
()
{
return
calloc
(
1
,
sizeof
(
STaosQall
)
);
}
void
taosFreeQall
(
STaosQall
*
qall
)
{
free
(
qall
);
}
...
...
@@ -238,7 +249,7 @@ int32_t taosGetQitem(STaosQall *qall, void **ppItem) {
void
taosResetQitems
(
STaosQall
*
qall
)
{
qall
->
current
=
qall
->
start
;
}
STaosQset
*
taosOpenQset
()
{
STaosQset
*
qset
=
(
STaosQset
*
)
calloc
(
sizeof
(
STaosQset
),
1
);
STaosQset
*
qset
=
calloc
(
sizeof
(
STaosQset
),
1
);
if
(
qset
==
NULL
)
{
terrno
=
TSDB_CODE_OUT_OF_MEMORY
;
return
NULL
;
...
...
@@ -247,7 +258,7 @@ STaosQset *taosOpenQset() {
pthread_mutex_init
(
&
qset
->
mutex
,
NULL
);
tsem_init
(
&
qset
->
sem
,
0
,
0
);
u
Trace
(
"qset:%p is opened"
,
qset
);
u
Debug
(
"qset:%p is opened"
,
qset
);
return
qset
;
}
...
...
@@ -268,7 +279,7 @@ void taosCloseQset(STaosQset *qset) {
pthread_mutex_destroy
(
&
qset
->
mutex
);
tsem_destroy
(
&
qset
->
sem
);
free
(
qset
);
u
Trace
(
"qset:%p is closed"
,
qset
);
u
Debug
(
"qset:%p is closed"
,
qset
);
}
// tsem_post 'qset->sem', so that reader threads waiting for it
...
...
@@ -338,12 +349,12 @@ void taosRemoveFromQset(STaosQset *qset, STaosQueue *queue) {
pthread_mutex_unlock
(
&
qset
->
mutex
);
u
Trace
(
"queue:%p is removed from qset:%p"
,
queue
,
qset
);
u
Debug
(
"queue:%p is removed from qset:%p"
,
queue
,
qset
);
}
int32_t
taosGetQueueNumber
(
STaosQset
*
qset
)
{
return
qset
->
numOfQueues
;
}
int32_t
taosReadQitemFromQset
(
STaosQset
*
qset
,
void
**
ppItem
,
void
**
ahandle
,
F
Process
Item
*
itemFp
)
{
int32_t
taosReadQitemFromQset
(
STaosQset
*
qset
,
void
**
ppItem
,
void
**
ahandle
,
FItem
*
itemFp
)
{
STaosQnode
*
pNode
=
NULL
;
int32_t
code
=
0
;
...
...
@@ -365,6 +376,7 @@ int32_t taosReadQitemFromQset(STaosQset *qset, void **ppItem, void **ahandle, FP
*
ppItem
=
pNode
->
item
;
if
(
ahandle
)
*
ahandle
=
queue
->
ahandle
;
if
(
itemFp
)
*
itemFp
=
queue
->
itemFp
;
queue
->
head
=
pNode
->
next
;
if
(
queue
->
head
==
NULL
)
queue
->
tail
=
NULL
;
queue
->
numOfItems
--
;
...
...
@@ -382,7 +394,7 @@ int32_t taosReadQitemFromQset(STaosQset *qset, void **ppItem, void **ahandle, FP
return
code
;
}
int32_t
taosReadAllQitemsFromQset
(
STaosQset
*
qset
,
STaosQall
*
qall
,
void
**
ahandle
,
F
Process
Items
*
itemsFp
)
{
int32_t
taosReadAllQitemsFromQset
(
STaosQset
*
qset
,
STaosQall
*
qall
,
void
**
ahandle
,
FItems
*
itemsFp
)
{
STaosQueue
*
queue
;
int32_t
code
=
0
;
...
...
@@ -411,7 +423,9 @@ int32_t taosReadAllQitemsFromQset(STaosQset *qset, STaosQall *qall, void **ahand
queue
->
tail
=
NULL
;
queue
->
numOfItems
=
0
;
atomic_sub_fetch_32
(
&
qset
->
numOfItems
,
qall
->
numOfItems
);
for
(
int32_t
j
=
1
;
j
<
qall
->
numOfItems
;
++
j
)
tsem_wait
(
&
qset
->
sem
);
for
(
int32_t
j
=
1
;
j
<
qall
->
numOfItems
;
++
j
)
{
tsem_wait
(
&
qset
->
sem
);
}
}
pthread_mutex_unlock
(
&
queue
->
mutex
);
...
...
@@ -423,6 +437,65 @@ int32_t taosReadAllQitemsFromQset(STaosQset *qset, STaosQall *qall, void **ahand
return
code
;
}
int32_t
taosReadQitemFromQsetByThread
(
STaosQset
*
qset
,
void
**
ppItem
,
void
**
ahandle
,
FItem
*
itemFp
,
int32_t
threadId
)
{
STaosQnode
*
pNode
=
NULL
;
int32_t
code
=
-
1
;
tsem_wait
(
&
qset
->
sem
);
pthread_mutex_lock
(
&
qset
->
mutex
);
for
(
int32_t
i
=
0
;
i
<
qset
->
numOfQueues
;
++
i
)
{
if
(
qset
->
current
==
NULL
)
qset
->
current
=
qset
->
head
;
STaosQueue
*
queue
=
qset
->
current
;
if
(
queue
)
qset
->
current
=
queue
->
next
;
if
(
queue
==
NULL
)
break
;
if
(
queue
->
head
==
NULL
)
continue
;
if
(
queue
->
threadId
!=
-
1
&&
queue
->
threadId
!=
threadId
)
{
code
=
0
;
continue
;
}
pthread_mutex_lock
(
&
queue
->
mutex
);
if
(
queue
->
head
)
{
pNode
=
queue
->
head
;
pNode
->
queue
=
queue
;
queue
->
threadId
=
threadId
;
*
ppItem
=
pNode
->
item
;
if
(
ahandle
)
*
ahandle
=
queue
->
ahandle
;
if
(
itemFp
)
*
itemFp
=
queue
->
itemFp
;
queue
->
head
=
pNode
->
next
;
if
(
queue
->
head
==
NULL
)
queue
->
tail
=
NULL
;
queue
->
numOfItems
--
;
atomic_sub_fetch_32
(
&
qset
->
numOfItems
,
1
);
code
=
1
;
uTrace
(
"item:%p is read out from queue:%p, items:%d"
,
*
ppItem
,
queue
,
queue
->
numOfItems
);
}
pthread_mutex_unlock
(
&
queue
->
mutex
);
if
(
pNode
)
break
;
}
pthread_mutex_unlock
(
&
qset
->
mutex
);
return
code
;
}
void
taosResetQsetThread
(
STaosQset
*
qset
,
void
*
pItem
)
{
if
(
pItem
==
NULL
)
return
;
STaosQnode
*
pNode
=
(
STaosQnode
*
)((
char
*
)
pItem
-
sizeof
(
STaosQnode
));
pthread_mutex_lock
(
&
qset
->
mutex
);
pNode
->
queue
->
threadId
=
-
1
;
for
(
int32_t
i
=
0
;
i
<
pNode
->
queue
->
numOfItems
;
++
i
)
{
tsem_post
(
&
qset
->
sem
);
}
pthread_mutex_unlock
(
&
qset
->
mutex
);
}
int32_t
taosGetQueueItemsNumber
(
STaosQueue
*
queue
)
{
if
(
!
queue
)
return
0
;
...
...
source/util/src/tworker.c
浏览文件 @
59d7cb6d
...
...
@@ -14,38 +14,46 @@
*/
#define _DEFAULT_SOURCE
#include "os.h"
#include "ulog.h"
#include "tqueue.h"
#include "tworker.h"
#include "taoserror.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
(
pool
->
workers
==
NULL
)
{
terrno
=
TSDB_CODE_OUT_OF_MEMORY
;
return
-
1
;
}
if
(
pthread_mutex_init
(
&
pool
->
mutex
,
NULL
))
{
terrno
=
TSDB_CODE_OUT_OF_MEMORY
;
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
(
"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,15 +64,15 @@ void tWorkerCleanup(SWorkerPool *pool) {
taosCloseQset
(
pool
->
qset
);
pthread_mutex_destroy
(
&
pool
->
mutex
);
u
Info
(
"worker:%s is closed"
,
pool
->
name
);
u
Debug
(
"worker:%s is closed"
,
pool
->
name
);
}
static
void
*
t
WorkerThreadFp
(
S
Worker
*
worker
)
{
SWorkerPool
*
pool
=
worker
->
pool
;
F
ProcessItem
fp
=
NULL
;
static
void
*
t
QWorkerThreadFp
(
SQ
Worker
*
worker
)
{
S
Q
WorkerPool
*
pool
=
worker
->
pool
;
F
Item
fp
=
NULL
;
void
*
msg
=
NULL
;
void
*
ahandle
=
NULL
;
void
*
msg
=
NULL
;
void
*
ahandle
=
NULL
;
int32_t
code
=
0
;
taosBlockSIGPIPE
();
...
...
@@ -77,7 +85,7 @@ static void *tWorkerThreadFp(SWorker *worker) {
break
;
}
if
(
fp
)
{
if
(
fp
!=
NULL
)
{
(
*
fp
)(
ahandle
,
msg
);
}
}
...
...
@@ -85,11 +93,12 @@ static void *tWorkerThreadFp(SWorker *worker) {
return
NULL
;
}
STaosQueue
*
tWorkerAllocQueue
(
S
WorkerPool
*
pool
,
void
*
ahandle
,
FProcessItem
f
p
)
{
STaosQueue
*
tWorkerAllocQueue
(
S
QWorkerPool
*
pool
,
void
*
ahandle
,
FItem
fp
,
ThreadFp
threadF
p
)
{
pthread_mutex_lock
(
&
pool
->
mutex
);
STaosQueue
*
queue
=
taosOpenQueue
();
if
(
queue
==
NULL
)
{
pthread_mutex_unlock
(
&
pool
->
mutex
);
terrno
=
TSDB_CODE_OUT_OF_MEMORY
;
return
NULL
;
}
...
...
@@ -99,14 +108,18 @@ 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
)
tWorkerT
hreadFp
,
worker
)
!=
0
)
{
if
(
pthread_create
(
&
worker
->
thread
,
&
thAttr
,
t
hreadFp
,
worker
)
!=
0
)
{
uError
(
"worker:%s:%d failed to create thread to process since %s"
,
pool
->
name
,
worker
->
id
,
strerror
(
errno
));
taosCloseQueue
(
queue
);
terrno
=
TSDB_CODE_OUT_OF_MEMORY
;
queue
=
NULL
;
break
;
}
pthread_attr_destroy
(
&
thAttr
);
...
...
@@ -121,19 +134,73 @@ STaosQueue *tWorkerAllocQueue(SWorkerPool *pool, void *ahandle, FProcessItem fp)
return
queue
;
}
void
tWorkerFreeQueue
(
SWorkerPool
*
pool
,
STaosQueue
*
queue
)
{
STaosQueue
*
tQWorkerAllocQueue
(
SQWorkerPool
*
pool
,
void
*
ahandle
,
FItem
fp
)
{
return
tWorkerAllocQueue
(
pool
,
ahandle
,
fp
,
(
ThreadFp
)
tQWorkerThreadFp
);
}
void
tQWorkerFreeQueue
(
SQWorkerPool
*
pool
,
STaosQueue
*
queue
)
{
taosCloseQueue
(
queue
);
uDebug
(
"worker:%s, queue:%p is freed"
,
pool
->
name
,
queue
);
}
int32_t
tMWorkerInit
(
SMWorkerPool
*
pool
)
{
int32_t
tFWorkerInit
(
SFWorkerPool
*
pool
)
{
return
tQWorkerInit
((
SQWorkerPool
*
)
pool
);
}
void
tFWorkerCleanup
(
SFWorkerPool
*
pool
)
{
tQWorkerCleanup
(
pool
);
}
static
void
*
tFWorkerThreadFp
(
SQWorker
*
worker
)
{
SQWorkerPool
*
pool
=
worker
->
pool
;
FItem
fp
=
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
);
while
(
1
)
{
code
=
taosReadQitemFromQsetByThread
(
pool
->
qset
,
(
void
**
)
&
msg
,
&
ahandle
,
&
fp
,
worker
->
id
);
if
(
code
<
0
)
{
uDebug
(
"worker:%s:%d qset:%p, got no message and exiting"
,
pool
->
name
,
worker
->
id
,
pool
->
qset
);
break
;
}
else
if
(
code
==
0
)
{
// uTrace("worker:%s:%d qset:%p, got no message and continue", pool->name, worker->id, pool->qset);
continue
;
}
if
(
fp
!=
NULL
)
{
(
*
fp
)(
ahandle
,
msg
);
}
taosResetQsetThread
(
pool
->
qset
,
msg
);
}
return
NULL
;
}
STaosQueue
*
tFWorkerAllocQueue
(
SQWorkerPool
*
pool
,
void
*
ahandle
,
FItem
fp
)
{
return
tWorkerAllocQueue
(
pool
,
ahandle
,
fp
,
(
ThreadFp
)
tFWorkerThreadFp
);
}
void
tFWorkerFreeQueue
(
SFWorkerPool
*
pool
,
STaosQueue
*
queue
)
{
tQWorkerFreeQueue
(
pool
,
queue
);
}
int32_t
tWWorkerInit
(
SWWorkerPool
*
pool
)
{
pool
->
nextId
=
0
;
pool
->
workers
=
calloc
(
sizeof
(
SMWorker
),
pool
->
max
);
if
(
pool
->
workers
==
NULL
)
return
-
1
;
pool
->
workers
=
calloc
(
sizeof
(
SWWorker
),
pool
->
max
);
if
(
pool
->
workers
==
NULL
)
{
terrno
=
TSDB_CODE_OUT_OF_MEMORY
;
return
-
1
;
}
if
(
pthread_mutex_init
(
&
pool
->
mutex
,
NULL
)
!=
0
)
{
terrno
=
TSDB_CODE_OUT_OF_MEMORY
;
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
;
...
...
@@ -144,16 +211,18 @@ int32_t tMWorkerInit(SMWorkerPool *pool) {
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
);
...
...
@@ -167,12 +236,12 @@ void tMWorkerCleanup(SMWorkerPool *pool) {
uInfo
(
"worker:%s is closed"
,
pool
->
name
);
}
static
void
*
tW
riteWorkerThreadFp
(
SM
Worker
*
worker
)
{
S
M
WorkerPool
*
pool
=
worker
->
pool
;
F
ProcessItems
fp
=
NULL
;
static
void
*
tW
WorkerThreadFp
(
SW
Worker
*
worker
)
{
S
W
WorkerPool
*
pool
=
worker
->
pool
;
F
Items
fp
=
NULL
;
void
*
msg
=
NULL
;
void
*
ahandle
=
NULL
;
void
*
msg
=
NULL
;
void
*
ahandle
=
NULL
;
int32_t
numOfMsgs
=
0
;
int32_t
qtype
=
0
;
...
...
@@ -187,7 +256,7 @@ static void *tWriteWorkerThreadFp(SMWorker *worker) {
break
;
}
if
(
fp
)
{
if
(
fp
!=
NULL
)
{
(
*
fp
)(
ahandle
,
worker
->
qall
,
numOfMsgs
);
}
}
...
...
@@ -195,13 +264,14 @@ static void *tWriteWorkerThreadFp(SMWorker *worker) {
return
NULL
;
}
STaosQueue
*
t
MWorkerAllocQueue
(
SMWorkerPool
*
pool
,
void
*
ahandle
,
FProcess
Items
fp
)
{
STaosQueue
*
t
WWorkerAllocQueue
(
SWWorkerPool
*
pool
,
void
*
ahandle
,
F
Items
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
)
{
pthread_mutex_unlock
(
&
pool
->
mutex
);
terrno
=
TSDB_CODE_OUT_OF_MEMORY
;
return
NULL
;
}
...
...
@@ -221,17 +291,19 @@ STaosQueue *tMWorkerAllocQueue(SMWorkerPool *pool, void *ahandle, FProcessItems
taosCloseQset
(
worker
->
qset
);
taosCloseQueue
(
queue
);
pthread_mutex_unlock
(
&
pool
->
mutex
);
terrno
=
TSDB_CODE_OUT_OF_MEMORY
;
return
NULL
;
}
pthread_attr_t
thAttr
;
pthread_attr_init
(
&
thAttr
);
pthread_attr_setdetachstate
(
&
thAttr
,
PTHREAD_CREATE_JOINABLE
);
if
(
pthread_create
(
&
worker
->
thread
,
&
thAttr
,
(
ThreadFp
)
tW
rite
WorkerThreadFp
,
worker
)
!=
0
)
{
if
(
pthread_create
(
&
worker
->
thread
,
&
thAttr
,
(
ThreadFp
)
tWWorkerThreadFp
,
worker
)
!=
0
)
{
uError
(
"worker:%s:%d failed to create thread to process since %s"
,
pool
->
name
,
worker
->
id
,
strerror
(
errno
));
taosFreeQall
(
worker
->
qall
);
taosCloseQset
(
worker
->
qset
);
taosCloseQueue
(
queue
);
terrno
=
TSDB_CODE_OUT_OF_MEMORY
;
queue
=
NULL
;
}
else
{
uDebug
(
"worker:%s:%d is launched, max:%d"
,
pool
->
name
,
worker
->
id
,
pool
->
max
);
...
...
@@ -250,7 +322,7 @@ STaosQueue *tMWorkerAllocQueue(SMWorkerPool *pool, void *ahandle, FProcessItems
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
);
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录