snode.c 8.1 KB
Newer Older
H
refact  
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

L
Liu Jicong 已提交
16
#include "executor.h"
S
Shengliang Guan 已提交
17
#include "sndInt.h"
L
Liu Jicong 已提交
18
#include "tuuid.h"
H
refact  
Hongze Cheng 已提交
19

S
Shengliang Guan 已提交
20
SSnode *sndOpen(const char *path, const SSnodeOpt *pOption) {
wafwerar's avatar
wafwerar 已提交
21
  SSnode *pSnode = taosMemoryCalloc(1, sizeof(SSnode));
L
Liu Jicong 已提交
22 23 24
  if (pSnode == NULL) {
    return NULL;
  }
S
Shengliang Guan 已提交
25
  pSnode->msgCb = pOption->msgCb;
L
Liu Jicong 已提交
26 27
  pSnode->pMeta = sndMetaNew();
  if (pSnode->pMeta == NULL) {
wafwerar's avatar
wafwerar 已提交
28
    taosMemoryFree(pSnode);
L
Liu Jicong 已提交
29 30
    return NULL;
  }
S
Shengliang Guan 已提交
31
  return pSnode;
H
refact  
Hongze Cheng 已提交
32 33
}

L
Liu Jicong 已提交
34 35
void sndClose(SSnode *pSnode) {
  sndMetaDelete(pSnode->pMeta);
wafwerar's avatar
wafwerar 已提交
36
  taosMemoryFree(pSnode);
L
Liu Jicong 已提交
37
}
S
Shengliang Guan 已提交
38 39 40

int32_t sndGetLoad(SSnode *pSnode, SSnodeLoad *pLoad) { return 0; }

L
Liu Jicong 已提交
41
SStreamMeta *sndMetaNew() {
wafwerar's avatar
wafwerar 已提交
42
  SStreamMeta *pMeta = taosMemoryCalloc(1, sizeof(SStreamMeta));
L
Liu Jicong 已提交
43 44 45 46 47
  if (pMeta == NULL) {
    return NULL;
  }
  pMeta->pHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);
  if (pMeta->pHash == NULL) {
wafwerar's avatar
wafwerar 已提交
48
    taosMemoryFree(pMeta);
L
Liu Jicong 已提交
49 50 51 52 53 54 55
    return NULL;
  }
  return pMeta;
}

void sndMetaDelete(SStreamMeta *pMeta) {
  taosHashCleanup(pMeta->pHash);
wafwerar's avatar
wafwerar 已提交
56
  taosMemoryFree(pMeta);
L
Liu Jicong 已提交
57 58 59
}

int32_t sndMetaDeployTask(SStreamMeta *pMeta, SStreamTask *pTask) {
L
Liu Jicong 已提交
60
  pTask->exec.executor = qCreateStreamExecTaskInfo(pTask->exec.qmsg, NULL);
L
Liu Jicong 已提交
61 62 63
  return taosHashPut(pMeta->pHash, &pTask->taskId, sizeof(int32_t), pTask, sizeof(void *));
}

L
Liu Jicong 已提交
64 65 66 67
SStreamTask *sndMetaGetTask(SStreamMeta *pMeta, int32_t taskId) {
  return taosHashGet(pMeta->pHash, &taskId, sizeof(int32_t));
}

L
Liu Jicong 已提交
68 69 70
int32_t sndMetaRemoveTask(SStreamMeta *pMeta, int32_t taskId) {
  SStreamTask *pTask = taosHashGet(pMeta->pHash, &taskId, sizeof(int32_t));
  if (pTask == NULL) {
L
Liu Jicong 已提交
71 72
    return -1;
  }
wafwerar's avatar
wafwerar 已提交
73
  taosMemoryFree(pTask->exec.qmsg);
L
Liu Jicong 已提交
74
  // TODO:free executor
wafwerar's avatar
wafwerar 已提交
75
  taosMemoryFree(pTask);
L
Liu Jicong 已提交
76
  return taosHashRemove(pMeta->pHash, &taskId, sizeof(int32_t));
L
Liu Jicong 已提交
77 78
}

L
Liu Jicong 已提交
79 80
static int32_t sndProcessTaskDeployReq(SSnode *pNode, SRpcMsg *pMsg) {
  SStreamMeta *pMeta = pNode->pMeta;
L
Liu Jicong 已提交
81 82
  char        *msg = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
  int32_t      msgLen = pMsg->contLen - sizeof(SMsgHead);
L
Liu Jicong 已提交
83 84 85 86 87 88 89 90 91 92 93 94

  SStreamTask *pTask = taosMemoryCalloc(1, sizeof(SStreamTask));
  if (pTask == NULL) {
    return -1;
  }
  SDecoder decoder;
  tDecoderInit(&decoder, (uint8_t *)msg, msgLen);
  if (tDecodeSStreamTask(&decoder, pTask) < 0) {
    ASSERT(0);
  }
  tDecoderClear(&decoder);

L
Liu Jicong 已提交
95
  pTask->execStatus = TASK_EXEC_STATUS__IDLE;
L
Liu Jicong 已提交
96 97 98 99 100 101 102 103 104 105 106 107

  pTask->inputQueue = streamQueueOpen();
  pTask->outputQueue = streamQueueOpen();
  pTask->inputStatus = TASK_INPUT_STATUS__NORMAL;
  pTask->outputStatus = TASK_INPUT_STATUS__NORMAL;

  if (pTask->inputQueue == NULL || pTask->outputQueue == NULL) goto FAIL;

  pTask->pMsgCb = &pNode->msgCb;

  ASSERT(pTask->execType != TASK_EXEC__NONE);

L
Liu Jicong 已提交
108
  ASSERT(pTask->isDataScan == 0);
L
Liu Jicong 已提交
109
  pTask->exec.executor = qCreateStreamExecTaskInfo(pTask->exec.qmsg, NULL);
L
Liu Jicong 已提交
110 111 112 113
  ASSERT(pTask->exec.executor);

  streamSetupTrigger(pTask);

L
Liu Jicong 已提交
114 115
  qInfo("deploy stream: stream id %ld task id %d child id %d on snode", pTask->streamId, pTask->taskId,
        pTask->selfChildId);
L
Liu Jicong 已提交
116

L
Liu Jicong 已提交
117 118
  taosHashPut(pMeta->pHash, &pTask->taskId, sizeof(int32_t), &pTask, sizeof(void *));

L
Liu Jicong 已提交
119 120 121 122 123
  return 0;

FAIL:
  if (pTask->inputQueue) streamQueueClose(pTask->inputQueue);
  if (pTask->outputQueue) streamQueueClose(pTask->outputQueue);
L
Liu Jicong 已提交
124
  if (pTask) taosMemoryFree(pTask);
L
Liu Jicong 已提交
125 126 127 128 129 130 131 132
  return -1;
}

static int32_t sndProcessTaskRunReq(SSnode *pNode, SRpcMsg *pMsg) {
  SStreamMeta       *pMeta = pNode->pMeta;
  SStreamTaskRunReq *pReq = pMsg->pCont;
  int32_t            taskId = pReq->taskId;
  SStreamTask       *pTask = *(SStreamTask **)taosHashGet(pMeta->pHash, &taskId, sizeof(int32_t));
L
Liu Jicong 已提交
133
  streamProcessRunReq(pTask);
L
Liu Jicong 已提交
134 135 136
  return 0;
}

L
Liu Jicong 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
static int32_t sndProcessTaskDispatchReq(SSnode *pNode, SRpcMsg *pMsg) {
  SStreamMeta *pMeta = pNode->pMeta;

  char   *msgStr = pMsg->pCont;
  char   *msgBody = POINTER_SHIFT(msgStr, sizeof(SMsgHead));
  int32_t msgLen = pMsg->contLen - sizeof(SMsgHead);

  SStreamDispatchReq req;
  SDecoder           decoder;
  tDecoderInit(&decoder, msgBody, msgLen);
  tDecodeStreamDispatchReq(&decoder, &req);
  int32_t      taskId = req.taskId;
  SStreamTask *pTask = *(SStreamTask **)taosHashGet(pMeta->pHash, &taskId, sizeof(int32_t));
  SRpcMsg      rsp = {
           .info = pMsg->info,
           .code = 0,
  };
L
Liu Jicong 已提交
154
  streamProcessDispatchReq(pTask, &req, &rsp);
L
Liu Jicong 已提交
155 156 157 158 159 160 161 162 163
  return 0;
}

static int32_t sndProcessTaskRecoverReq(SSnode *pNode, SRpcMsg *pMsg) {
  SStreamMeta *pMeta = pNode->pMeta;

  SStreamTaskRecoverReq *pReq = pMsg->pCont;
  int32_t                taskId = pReq->taskId;
  SStreamTask           *pTask = *(SStreamTask **)taosHashGet(pMeta->pHash, &taskId, sizeof(int32_t));
L
Liu Jicong 已提交
164
  streamProcessRecoverReq(pTask, pReq, pMsg);
L
Liu Jicong 已提交
165 166 167 168 169 170 171 172 173
  return 0;
}

static int32_t sndProcessTaskDispatchRsp(SSnode *pNode, SRpcMsg *pMsg) {
  SStreamMeta *pMeta = pNode->pMeta;

  SStreamDispatchRsp *pRsp = POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead));
  int32_t             taskId = pRsp->taskId;
  SStreamTask        *pTask = *(SStreamTask **)taosHashGet(pMeta->pHash, &taskId, sizeof(int32_t));
L
Liu Jicong 已提交
174
  streamProcessDispatchRsp(pTask, pRsp);
L
Liu Jicong 已提交
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
  return 0;
}

static int32_t sndProcessTaskRecoverRsp(SSnode *pNode, SRpcMsg *pMsg) {
  SStreamMeta *pMeta = pNode->pMeta;

  SStreamTaskRecoverRsp *pRsp = pMsg->pCont;
  int32_t                taskId = pRsp->taskId;
  SStreamTask           *pTask = *(SStreamTask **)taosHashGet(pMeta->pHash, &taskId, sizeof(int32_t));
  streamProcessRecoverRsp(pTask, pRsp);
  return 0;
}

static int32_t sndProcessTaskDropReq(SSnode *pNode, SRpcMsg *pMsg) {
  SStreamMeta *pMeta = pNode->pMeta;

  char                *msg = pMsg->pCont;
  int32_t              msgLen = pMsg->contLen;
  SVDropStreamTaskReq *pReq = (SVDropStreamTaskReq *)msg;
  int32_t              code = taosHashRemove(pMeta->pHash, &pReq->taskId, sizeof(int32_t));
  ASSERT(code == 0);
  if (code == 0) {
    // sendrsp
  }
  return code;
}

L
Liu Jicong 已提交
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
static int32_t sndProcessTaskRetrieveReq(SSnode *pNode, SRpcMsg *pMsg) {
  SStreamMeta *pMeta = pNode->pMeta;

  char              *msgStr = pMsg->pCont;
  char              *msgBody = POINTER_SHIFT(msgStr, sizeof(SMsgHead));
  int32_t            msgLen = pMsg->contLen - sizeof(SMsgHead);
  SStreamRetrieveReq req;
  SDecoder           decoder;
  tDecoderInit(&decoder, msgBody, msgLen);
  tDecodeStreamRetrieveReq(&decoder, &req);
  int32_t      taskId = req.dstTaskId;
  SStreamTask *pTask = *(SStreamTask **)taosHashGet(pMeta->pHash, &taskId, sizeof(int32_t));
  if (atomic_load_8(&pTask->taskStatus) != TASK_STATUS__NORMAL) {
    return 0;
  }
  SRpcMsg rsp = {
      .info = pMsg->info,
      .code = 0,
  };
  streamProcessRetrieveReq(pTask, &req, &rsp);
  return 0;
}

static int32_t sndProcessTaskRetrieveRsp(SSnode *pNode, SRpcMsg *pMsg) {
  //
  return 0;
}

L
Liu Jicong 已提交
230
int32_t sndProcessUMsg(SSnode *pSnode, SRpcMsg *pMsg) {
L
Liu Jicong 已提交
231
  // stream deploy
L
Liu Jicong 已提交
232 233
  // stream stop/resume
  // operator exec
L
Liu Jicong 已提交
234 235 236
  switch (pMsg->msgType) {
    case TDMT_STREAM_TASK_DEPLOY:
      return sndProcessTaskDeployReq(pSnode, pMsg);
L
Liu Jicong 已提交
237
    case TDMT_STREAM_TASK_DROP:
L
Liu Jicong 已提交
238 239
      return sndProcessTaskDropReq(pSnode, pMsg);
    default:
S
shm  
Shengliang Guan 已提交
240
      ASSERT(0);
L
Liu Jicong 已提交
241
  }
L
Liu Jicong 已提交
242
  return 0;
L
Liu Jicong 已提交
243 244
}

L
Liu Jicong 已提交
245 246 247 248 249 250 251 252
int32_t sndProcessSMsg(SSnode *pSnode, SRpcMsg *pMsg) {
  switch (pMsg->msgType) {
    case TDMT_STREAM_TASK_RUN:
      return sndProcessTaskRunReq(pSnode, pMsg);
    case TDMT_STREAM_TASK_DISPATCH:
      return sndProcessTaskDispatchReq(pSnode, pMsg);
    case TDMT_STREAM_TASK_RECOVER:
      return sndProcessTaskRecoverReq(pSnode, pMsg);
L
Liu Jicong 已提交
253 254
    case TDMT_STREAM_RETRIEVE:
      return sndProcessTaskRecoverReq(pSnode, pMsg);
L
Liu Jicong 已提交
255 256 257 258
    case TDMT_STREAM_TASK_DISPATCH_RSP:
      return sndProcessTaskDispatchRsp(pSnode, pMsg);
    case TDMT_STREAM_TASK_RECOVER_RSP:
      return sndProcessTaskRecoverRsp(pSnode, pMsg);
L
Liu Jicong 已提交
259
    case TDMT_STREAM_RETRIEVE_RSP:
5
54liuyao 已提交
260
      return sndProcessTaskRetrieveRsp(pSnode, pMsg);
L
Liu Jicong 已提交
261 262 263 264
    default:
      ASSERT(0);
  }
  return 0;
L
Liu Jicong 已提交
265
}