TDengine/include/util/tworker.h

184 lines
5.6 KiB
C
Raw Normal View History

2020-12-09 06:12:50 +00:00
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2022-02-28 06:31:57 +00:00
#ifndef _TD_UTIL_WORKER_H_
#define _TD_UTIL_WORKER_H_
2022-02-28 07:19:00 +00:00
2024-06-24 10:28:56 +00:00
#include "tlist.h"
2021-10-20 01:06:08 +00:00
#include "tqueue.h"
#include "tarray.h"
2021-10-20 01:06:08 +00:00
2020-12-09 06:12:50 +00:00
#ifdef __cplusplus
extern "C" {
#endif
2022-01-25 08:23:10 +00:00
typedef struct SWWorkerPool SWWorkerPool;
typedef struct SQueueWorker {
int32_t id; // worker id
int64_t pid; // thread pid
TdThread thread; // thread id
void *pool;
} SQueueWorker;
2021-11-27 07:57:54 +00:00
2022-01-25 08:23:10 +00:00
typedef struct SQWorkerPool {
2022-03-22 02:46:16 +00:00
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;
SQueueWorker *workers;
2022-03-19 16:47:45 +00:00
TdThreadMutex mutex;
2022-03-22 02:46:16 +00:00
} SQWorkerPool;
2020-12-09 06:12:50 +00:00
typedef struct SAutoQWorkerPool {
float ratio;
STaosQset *qset;
const char *name;
SArray *workers;
TdThreadMutex mutex;
} SAutoQWorkerPool;
2022-01-25 08:23:10 +00:00
typedef struct SWWorker {
2021-11-27 07:57:54 +00:00
int32_t id; // worker id
int64_t pid; // thread pid
TdThread thread; // thread id
2022-02-28 06:31:57 +00:00
STaosQall *qall;
STaosQset *qset;
2022-01-25 08:23:10 +00:00
SWWorkerPool *pool;
} SWWorker;
2024-01-11 03:50:52 +00:00
struct SWWorkerPool {
2022-03-22 08:10:26 +00:00
int32_t max; // max number of workers
int32_t num;
2022-03-22 02:46:16 +00:00
int32_t nextId; // from 0 to max-1, cyclic
const char *name;
SWWorker *workers;
2022-03-19 16:47:45 +00:00
TdThreadMutex mutex;
2024-01-11 03:50:52 +00:00
};
2022-01-25 08:23:10 +00:00
int32_t tQWorkerInit(SQWorkerPool *pool);
void tQWorkerCleanup(SQWorkerPool *pool);
2022-01-25 09:14:24 +00:00
STaosQueue *tQWorkerAllocQueue(SQWorkerPool *pool, void *ahandle, FItem fp);
2022-01-25 08:23:10 +00:00
void tQWorkerFreeQueue(SQWorkerPool *pool, STaosQueue *queue);
int32_t tAutoQWorkerInit(SAutoQWorkerPool *pool);
void tAutoQWorkerCleanup(SAutoQWorkerPool *pool);
STaosQueue *tAutoQWorkerAllocQueue(SAutoQWorkerPool *pool, void *ahandle, FItem fp);
void tAutoQWorkerFreeQueue(SAutoQWorkerPool *pool, STaosQueue *queue);
2022-01-25 08:23:10 +00:00
int32_t tWWorkerInit(SWWorkerPool *pool);
void tWWorkerCleanup(SWWorkerPool *pool);
2022-01-25 09:14:24 +00:00
STaosQueue *tWWorkerAllocQueue(SWWorkerPool *pool, void *ahandle, FItems fp);
2022-01-25 08:23:10 +00:00
void tWWorkerFreeQueue(SWWorkerPool *pool, STaosQueue *queue);
2020-12-09 06:12:50 +00:00
typedef enum SQWorkerPoolType {
QWORKER_POOL = 0,
QUERY_AUTO_QWORKER_POOL,
} SQWorkerPoolType;
2022-03-22 06:38:26 +00:00
typedef struct {
const char *name;
int32_t min;
int32_t max;
FItem fp;
void *param;
SQWorkerPoolType poolType;
2022-03-22 08:51:30 +00:00
} SSingleWorkerCfg;
2022-03-22 06:38:26 +00:00
typedef struct {
const char *name;
STaosQueue *queue;
SQWorkerPoolType poolType; // default to QWORKER_POOL
void *pool;
2022-03-22 08:51:30 +00:00
} SSingleWorker;
2022-03-22 06:38:26 +00:00
typedef struct {
const char *name;
2022-03-28 08:43:52 +00:00
int32_t max;
2022-03-22 06:38:26 +00:00
FItems fp;
void *param;
2022-03-22 08:51:30 +00:00
} SMultiWorkerCfg;
2022-03-22 06:38:26 +00:00
typedef struct {
const char *name;
STaosQueue *queue;
SWWorkerPool pool;
2022-03-22 08:51:30 +00:00
} SMultiWorker;
2022-03-22 06:38:26 +00:00
2022-03-22 08:51:30 +00:00
int32_t tSingleWorkerInit(SSingleWorker *pWorker, const SSingleWorkerCfg *pCfg);
void tSingleWorkerCleanup(SSingleWorker *pWorker);
int32_t tMultiWorkerInit(SMultiWorker *pWorker, const SMultiWorkerCfg *pCfg);
void tMultiWorkerCleanup(SMultiWorker *pWorker);
2022-03-22 06:38:26 +00:00
2024-06-24 10:28:56 +00:00
struct SQueryAutoQWorkerPoolCB;
typedef struct SQueryAutoQWorker {
int32_t id; // worker id
int32_t backupIdx;// the idx when put into backup pool
int64_t pid; // thread pid
TdThread thread; // thread id
void *pool;
} SQueryAutoQWorker;
typedef struct SQueryAutoQWorkerPool {
int32_t num;
int32_t max;
int32_t min;
2024-06-26 08:46:37 +00:00
int32_t maxInUse;
2024-06-24 10:28:56 +00:00
2024-07-01 07:41:19 +00:00
int64_t activeRunningN; // 4 bytes for activeN, 4 bytes for runningN
// activeN are running workers and workers waiting at reading new queue msgs
// runningN are workers processing queue msgs, not include blocking/waitingAfterBlock/waitingBeforeProcessMsg workers.
2024-06-24 10:28:56 +00:00
int32_t waitingAfterBlockN; // workers that recovered from blocking but waiting for too many running workers
TdThreadMutex waitingAfterBlockLock;
TdThreadCond waitingAfterBlockCond;
int32_t waitingBeforeProcessMsgN; // workers that get msg from queue, but waiting for too many running workers
TdThreadMutex waitingBeforeProcessMsgLock;
TdThreadCond waitingBeforeProcessMsgCond;
int32_t backupNum; // workers that are in backup pool, not reading msg from queue
TdThreadMutex backupLock;
TdThreadCond backupCond;
const char *name;
TdThreadMutex poolLock;
SList *workers;
SList *backupWorkers;
SList *exitedWorkers;
STaosQset *qset;
struct SQueryAutoQWorkerPoolCB *pCb;
bool exit;
} SQueryAutoQWorkerPool;
int32_t tQueryAutoQWorkerInit(SQueryAutoQWorkerPool *pPool);
void tQueryAutoQWorkerCleanup(SQueryAutoQWorkerPool *pPool);
STaosQueue *tQueryAutoQWorkerAllocQueue(SQueryAutoQWorkerPool *pPool, void *ahandle, FItem fp);
void tQueryAutoQWorkerFreeQueue(SQueryAutoQWorkerPool* pPool, STaosQueue* pQ);
typedef struct SQueryAutoQWorkerPoolCB {
2024-06-26 08:46:37 +00:00
void *pPool;
int32_t (*beforeBlocking)(void *pPool);
int32_t (*afterRecoverFromBlocking)(void *pPool);
2024-06-24 10:28:56 +00:00
} SQueryAutoQWorkerPoolCB;
2020-12-09 06:12:50 +00:00
#ifdef __cplusplus
}
#endif
2022-02-28 06:31:57 +00:00
#endif /*_TD_UTIL_WORKER_H_*/