TDengine/source/dnode/snode/src/snode.c

216 lines
8.2 KiB
C
Raw Normal View History

2021-09-22 09:27:48 +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-03-16 10:29:31 +00:00
#include "executor.h"
#include "rsync.h"
2021-12-27 10:43:27 +00:00
#include "sndInt.h"
#include "tqCommon.h"
2022-03-15 07:54:32 +00:00
#include "tuuid.h"
2022-10-28 09:03:17 +00:00
2023-12-21 10:52:25 +00:00
// clang-format off
#define sndError(...) do { if (sndDebugFlag & DEBUG_ERROR) {taosPrintLog("SND ERROR ", DEBUG_ERROR, sndDebugFlag, __VA_ARGS__);}} while (0)
#define sndInfo(...) do { if (sndDebugFlag & DEBUG_INFO) { taosPrintLog("SND INFO ", DEBUG_INFO, sndDebugFlag, __VA_ARGS__);}} while (0)
#define sndDebug(...) do { if (sndDebugFlag & DEBUG_DEBUG) { taosPrintLog("SND ", DEBUG_DEBUG, sndDebugFlag, __VA_ARGS__);}} while (0)
// clang-format on
static STaskId replaceStreamTaskId(SStreamTask *pTask) {
ASSERT(pTask->info.fillHistory);
STaskId id = {.streamId = pTask->id.streamId, .taskId = pTask->id.taskId};
pTask->id.streamId = pTask->streamTaskId.streamId;
pTask->id.taskId = pTask->streamTaskId.taskId;
return id;
}
static void restoreStreamTaskId(SStreamTask *pTask, STaskId *pId) {
ASSERT(pTask->info.fillHistory);
pTask->id.taskId = pId->taskId;
pTask->id.streamId = pId->streamId;
}
2023-11-15 03:50:14 +00:00
2023-10-01 14:27:29 +00:00
int32_t sndExpandTask(SSnode *pSnode, SStreamTask *pTask, int64_t nextProcessVer) {
2023-10-07 11:16:55 +00:00
ASSERT(pTask->info.taskLevel == TASK_LEVEL__AGG && taosArrayGetSize(pTask->upstreamInfo.pList) != 0);
2023-10-01 14:27:29 +00:00
int32_t code = streamTaskInit(pTask, pSnode->pMeta, &pSnode->msgCb, nextProcessVer);
if (code != TSDB_CODE_SUCCESS) {
return code;
2022-10-28 09:03:17 +00:00
}
2023-09-27 06:44:09 +00:00
pTask->pBackend = NULL;
2022-10-28 09:03:17 +00:00
2023-08-13 12:02:15 +00:00
streamTaskOpenAllUpstreamInput(pTask);
2023-12-21 10:52:25 +00:00
STaskId taskId = {0};
if (pTask->info.fillHistory) {
2023-12-21 10:52:25 +00:00
taskId = replaceStreamTaskId(pTask);
}
2023-12-21 10:52:25 +00:00
pTask->pState = streamStateOpen(pSnode->path, pTask, false, -1, -1);
2022-10-28 09:03:17 +00:00
if (pTask->pState == NULL) {
2023-11-15 03:50:14 +00:00
sndError("s-task:%s failed to open state for task", pTask->id.idStr);
2022-10-28 09:03:17 +00:00
return -1;
2023-08-18 02:53:58 +00:00
} else {
2023-11-15 03:50:14 +00:00
sndDebug("s-task:%s state:%p", pTask->id.idStr, pTask->pState);
2022-10-28 09:03:17 +00:00
}
2023-12-21 10:52:25 +00:00
if (pTask->info.fillHistory) {
restoreStreamTaskId(pTask, &taskId);
}
2022-10-28 09:03:17 +00:00
int32_t numOfVgroups = (int32_t)taosArrayGetSize(pTask->upstreamInfo.pList);
SReadHandle handle = {
.checkpointId = pTask->chkInfo.checkpointId,
.vnode = NULL,
.numOfVgroups = numOfVgroups,
.pStateBackend = pTask->pState,
.fillHistory = pTask->info.fillHistory,
.winRange = pTask->dataRange.window,
};
2023-05-25 09:51:03 +00:00
initStreamStateAPI(&handle.api);
2023-03-22 08:08:16 +00:00
pTask->exec.pExecutor = qCreateStreamExecTaskInfo(pTask->exec.qmsg, &handle, SNODE_HANDLE, pTask->id.taskId);
2023-04-08 17:39:09 +00:00
ASSERT(pTask->exec.pExecutor);
qSetTaskId(pTask->exec.pExecutor, pTask->id.taskId, pTask->id.streamId);
2022-10-28 09:03:17 +00:00
streamTaskResetUpstreamStageInfo(pTask);
2023-06-28 05:13:13 +00:00
streamSetupScheduleTrigger(pTask);
2023-08-29 07:11:01 +00:00
2023-09-27 06:44:09 +00:00
SCheckpointInfo *pChkInfo = &pTask->chkInfo;
2023-08-29 07:11:01 +00:00
// checkpoint ver is the kept version, handled data should be the next version.
if (pChkInfo->checkpointId != 0) {
pChkInfo->nextProcessVer = pChkInfo->checkpointVer + 1;
pChkInfo->processedVer = pChkInfo->checkpointVer;
2023-12-21 10:52:25 +00:00
sndInfo("s-task:%s restore from the checkpointId:%" PRId64 " ver:%" PRId64 " nextProcessVer:%" PRId64,
pTask->id.idStr, pChkInfo->checkpointId, pChkInfo->checkpointVer, pChkInfo->nextProcessVer);
2023-08-29 07:11:01 +00:00
}
2024-01-04 10:47:49 +00:00
char* p = streamTaskGetStatus(pTask)->name;
if (pTask->info.fillHistory) {
sndInfo("vgId:%d expand stream task, s-task:%s, checkpointId:%" PRId64 " checkpointVer:%" PRId64
2023-12-21 10:52:25 +00:00
" nextProcessVer:%" PRId64
" child id:%d, level:%d, status:%s fill-history:%d, related stream task:0x%x trigger:%" PRId64 " ms",
SNODE_HANDLE, pTask->id.idStr, pChkInfo->checkpointId, pChkInfo->checkpointVer, pChkInfo->nextProcessVer,
pTask->info.selfChildId, pTask->info.taskLevel, p, pTask->info.fillHistory,
(int32_t)pTask->streamTaskId.taskId, pTask->info.triggerParam);
} else {
sndInfo("vgId:%d expand stream task, s-task:%s, checkpointId:%" PRId64 " checkpointVer:%" PRId64
2023-12-21 10:52:25 +00:00
" nextProcessVer:%" PRId64
" child id:%d, level:%d, status:%s fill-history:%d, related fill-task:0x%x trigger:%" PRId64 " ms",
SNODE_HANDLE, pTask->id.idStr, pChkInfo->checkpointId, pChkInfo->checkpointVer, pChkInfo->nextProcessVer,
pTask->info.selfChildId, pTask->info.taskLevel, p, pTask->info.fillHistory,
(int32_t)pTask->hTaskInfo.id.taskId, pTask->info.triggerParam);
}
return 0;
}
2021-12-28 08:20:48 +00:00
SSnode *sndOpen(const char *path, const SSnodeOpt *pOption) {
2022-03-25 16:29:53 +00:00
SSnode *pSnode = taosMemoryCalloc(1, sizeof(SSnode));
2022-03-16 10:29:31 +00:00
if (pSnode == NULL) {
2022-10-28 09:03:17 +00:00
terrno = TSDB_CODE_OUT_OF_MEMORY;
2022-03-16 10:29:31 +00:00
return NULL;
}
pSnode->path = taosStrdup(path);
2022-10-28 09:03:17 +00:00
if (pSnode->path == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
goto FAIL;
}
pSnode->msgCb = pOption->msgCb;
pSnode->pMeta = streamMetaOpen(path, pSnode, (FTaskExpand *)sndExpandTask, SNODE_HANDLE, taosGetTimestampMs(), tqStartTaskCompleteCallback);
2022-03-16 10:29:31 +00:00
if (pSnode->pMeta == NULL) {
2022-10-28 09:03:17 +00:00
terrno = TSDB_CODE_OUT_OF_MEMORY;
goto FAIL;
2022-03-16 10:29:31 +00:00
}
2022-10-28 09:03:17 +00:00
2023-11-16 13:19:58 +00:00
if (streamMetaLoadAllTasks(pSnode->pMeta) < 0) {
goto FAIL;
}
2023-11-02 12:13:29 +00:00
stopRsync();
startRsync();
2021-12-27 10:43:27 +00:00
return pSnode;
2022-10-28 09:03:17 +00:00
FAIL:
taosMemoryFree(pSnode->path);
taosMemoryFree(pSnode);
return NULL;
2021-09-22 09:27:48 +00:00
}
2023-12-21 10:52:25 +00:00
int32_t sndInit(SSnode *pSnode) {
streamMetaResetTaskStatus(pSnode->pMeta);
streamMetaStartAllTasks(pSnode->pMeta);
2023-11-16 13:19:58 +00:00
return 0;
}
2022-03-16 10:29:31 +00:00
void sndClose(SSnode *pSnode) {
2023-11-21 06:35:41 +00:00
stopRsync();
2023-08-28 01:53:08 +00:00
streamMetaNotifyClose(pSnode->pMeta);
streamMetaCommit(pSnode->pMeta);
2022-10-28 09:03:17 +00:00
streamMetaClose(pSnode->pMeta);
taosMemoryFree(pSnode->path);
2022-03-25 16:29:53 +00:00
taosMemoryFree(pSnode);
2022-03-16 10:29:31 +00:00
}
2021-12-27 10:43:27 +00:00
2022-10-28 09:03:17 +00:00
int32_t sndProcessStreamMsg(SSnode *pSnode, SRpcMsg *pMsg) {
2022-06-20 06:29:18 +00:00
switch (pMsg->msgType) {
case TDMT_STREAM_TASK_RUN:
return tqStreamTaskProcessRunReq(pSnode->pMeta, pMsg, true);
2022-06-20 06:29:18 +00:00
case TDMT_STREAM_TASK_DISPATCH:
return tqStreamTaskProcessDispatchReq(pSnode->pMeta, pMsg);
2022-06-20 06:29:18 +00:00
case TDMT_STREAM_TASK_DISPATCH_RSP:
return tqStreamTaskProcessDispatchRsp(pSnode->pMeta, pMsg);
2022-10-31 10:37:28 +00:00
case TDMT_STREAM_RETRIEVE:
return tqStreamTaskProcessRetrieveReq(pSnode->pMeta, pMsg);
2023-11-22 12:23:58 +00:00
case TDMT_STREAM_RETRIEVE_RSP: // 1036
break;
case TDMT_VND_STREAM_TASK_CHECK:
return tqStreamTaskProcessCheckReq(pSnode->pMeta, pMsg);
case TDMT_VND_STREAM_TASK_CHECK_RSP:
return tqStreamTaskProcessCheckRsp(pSnode->pMeta, pMsg, true);
2023-11-10 06:14:22 +00:00
case TDMT_STREAM_TASK_CHECKPOINT_READY:
return tqStreamTaskProcessCheckpointReadyMsg(pSnode->pMeta, pMsg);
case TDMT_MND_STREAM_HEARTBEAT_RSP:
return tqStreamProcessStreamHbRsp(pSnode->pMeta, pMsg);
case TDMT_MND_STREAM_REQ_CHKPT_RSP:
return tqStreamProcessReqCheckpointRsp(pSnode->pMeta, pMsg);
case TDMT_STREAM_TASK_CHECKPOINT_READY_RSP:
return tqStreamProcessCheckpointReadyRsp(pSnode->pMeta, pMsg);
2022-06-20 06:29:18 +00:00
default:
2023-11-22 12:23:58 +00:00
sndError("invalid snode msg:%d", pMsg->msgType);
2022-06-20 06:29:18 +00:00
ASSERT(0);
}
return 0;
2022-03-14 12:32:19 +00:00
}
int32_t sndProcessWriteMsg(SSnode *pSnode, SRpcMsg *pMsg, SRpcMsg *pRsp) {
switch (pMsg->msgType) {
case TDMT_STREAM_TASK_DEPLOY: {
void * pReq = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
int32_t len = pMsg->contLen - sizeof(SMsgHead);
2024-01-11 01:53:19 +00:00
return tqStreamTaskProcessDeployReq(pSnode->pMeta, &pSnode->msgCb,pMsg->info.conn.applyIndex, pReq, len, true, true);
}
case TDMT_STREAM_TASK_DROP:
return tqStreamTaskProcessDropReq(pSnode->pMeta, pMsg->pCont, pMsg->contLen);
case TDMT_VND_STREAM_TASK_UPDATE:
return tqStreamTaskProcessUpdateReq(pSnode->pMeta, &pSnode->msgCb, pMsg, true);
case TDMT_VND_STREAM_TASK_RESET:
return tqStreamTaskProcessTaskResetReq(pSnode->pMeta, pMsg);
case TDMT_STREAM_TASK_PAUSE:
return tqStreamTaskProcessTaskPauseReq(pSnode->pMeta, pMsg->pCont);
case TDMT_STREAM_TASK_RESUME:
return tqStreamTaskProcessTaskResumeReq(pSnode->pMeta, pMsg->info.conn.applyIndex, pMsg->pCont, false);
2022-06-20 06:29:18 +00:00
default:
ASSERT(0);
}
return 0;
2023-11-01 01:37:54 +00:00
}