streamTask.c 8.3 KB
Newer Older
L
Liu Jicong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * 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/>.
 */

#include "executor.h"
#include "tstream.h"

L
Liu Jicong 已提交
19
SStreamTask* tNewSStreamTask(int64_t streamId) {
L
Liu Jicong 已提交
20 21 22 23
  SStreamTask* pTask = (SStreamTask*)taosMemoryCalloc(1, sizeof(SStreamTask));
  if (pTask == NULL) {
    return NULL;
  }
24 25 26 27 28 29 30 31

  pTask->id.taskId = tGenIdPI32();
  pTask->id.streamId = streamId;

  char buf[128] = {0};
  sprintf(buf, "0x%"PRIx64"-%d", pTask->id.streamId, pTask->id.taskId);

  pTask->id.idStr = taosStrdup(buf);
L
Liu Jicong 已提交
32
  pTask->schedStatus = TASK_SCHED_STATUS__INACTIVE;
L
Liu Jicong 已提交
33 34 35 36 37 38
  pTask->inputStatus = TASK_INPUT_STATUS__NORMAL;
  pTask->outputStatus = TASK_OUTPUT_STATUS__NORMAL;

  return pTask;
}

L
Liu Jicong 已提交
39 40 41 42
int32_t tEncodeStreamEpInfo(SEncoder* pEncoder, const SStreamChildEpInfo* pInfo) {
  if (tEncodeI32(pEncoder, pInfo->taskId) < 0) return -1;
  if (tEncodeI32(pEncoder, pInfo->nodeId) < 0) return -1;
  if (tEncodeI32(pEncoder, pInfo->childId) < 0) return -1;
L
Liu Jicong 已提交
43
  /*if (tEncodeI64(pEncoder, pInfo->processedVer) < 0) return -1;*/
L
Liu Jicong 已提交
44 45 46 47 48 49 50 51
  if (tEncodeSEpSet(pEncoder, &pInfo->epSet) < 0) return -1;
  return 0;
}

int32_t tDecodeStreamEpInfo(SDecoder* pDecoder, SStreamChildEpInfo* pInfo) {
  if (tDecodeI32(pDecoder, &pInfo->taskId) < 0) return -1;
  if (tDecodeI32(pDecoder, &pInfo->nodeId) < 0) return -1;
  if (tDecodeI32(pDecoder, &pInfo->childId) < 0) return -1;
L
Liu Jicong 已提交
52
  /*if (tDecodeI64(pDecoder, &pInfo->processedVer) < 0) return -1;*/
L
Liu Jicong 已提交
53 54 55 56
  if (tDecodeSEpSet(pDecoder, &pInfo->epSet) < 0) return -1;
  return 0;
}

L
Liu Jicong 已提交
57
int32_t tEncodeSStreamTask(SEncoder* pEncoder, const SStreamTask* pTask) {
L
Liu Jicong 已提交
58
  if (tStartEncode(pEncoder) < 0) return -1;
59 60
  if (tEncodeI64(pEncoder, pTask->id.streamId) < 0) return -1;
  if (tEncodeI32(pEncoder, pTask->id.taskId) < 0) return -1;
L
Liu Jicong 已提交
61
  if (tEncodeI32(pEncoder, pTask->totalLevel) < 0) return -1;
62 63
  if (tEncodeI8(pEncoder, pTask->taskLevel) < 0) return -1;
  if (tEncodeI8(pEncoder, pTask->outputType) < 0) return -1;
L
Liu Jicong 已提交
64
  if (tEncodeI16(pEncoder, pTask->dispatchMsgType) < 0) return -1;
65 66

  if (tEncodeI8(pEncoder, pTask->taskStatus) < 0) return -1;
L
Liu Jicong 已提交
67
  if (tEncodeI8(pEncoder, pTask->schedStatus) < 0) return -1;
L
Liu Jicong 已提交
68

L
Liu Jicong 已提交
69
  if (tEncodeI32(pEncoder, pTask->selfChildId) < 0) return -1;
L
Liu Jicong 已提交
70 71 72
  if (tEncodeI32(pEncoder, pTask->nodeId) < 0) return -1;
  if (tEncodeSEpSet(pEncoder, &pTask->epSet) < 0) return -1;

L
Liu Jicong 已提交
73 74 75 76
  if (tEncodeI64(pEncoder, pTask->recoverSnapVer) < 0) return -1;
  if (tEncodeI64(pEncoder, pTask->startVer) < 0) return -1;
  if (tEncodeI8(pEncoder, pTask->fillHistory) < 0) return -1;

L
Liu Jicong 已提交
77 78 79 80 81 82 83
  int32_t epSz = taosArrayGetSize(pTask->childEpInfo);
  if (tEncodeI32(pEncoder, epSz) < 0) return -1;
  for (int32_t i = 0; i < epSz; i++) {
    SStreamChildEpInfo* pInfo = taosArrayGetP(pTask->childEpInfo, i);
    if (tEncodeStreamEpInfo(pEncoder, pInfo) < 0) return -1;
  }

84
  if (pTask->taskLevel != TASK_LEVEL__SINK) {
L
Liu Jicong 已提交
85 86 87
    if (tEncodeCStr(pEncoder, pTask->exec.qmsg) < 0) return -1;
  }

88
  if (pTask->outputType == TASK_OUTPUT__TABLE) {
L
Liu Jicong 已提交
89 90 91
    if (tEncodeI64(pEncoder, pTask->tbSink.stbUid) < 0) return -1;
    if (tEncodeCStr(pEncoder, pTask->tbSink.stbFullName) < 0) return -1;
    if (tEncodeSSchemaWrapper(pEncoder, pTask->tbSink.pSchemaWrapper) < 0) return -1;
92
  } else if (pTask->outputType == TASK_OUTPUT__SMA) {
L
Liu Jicong 已提交
93
    if (tEncodeI64(pEncoder, pTask->smaSink.smaId) < 0) return -1;
94
  } else if (pTask->outputType == TASK_OUTPUT__FETCH) {
L
Liu Jicong 已提交
95
    if (tEncodeI8(pEncoder, pTask->fetchSink.reserved) < 0) return -1;
96
  } else if (pTask->outputType == TASK_OUTPUT__FIXED_DISPATCH) {
L
Liu Jicong 已提交
97 98 99
    if (tEncodeI32(pEncoder, pTask->fixedEpDispatcher.taskId) < 0) return -1;
    if (tEncodeI32(pEncoder, pTask->fixedEpDispatcher.nodeId) < 0) return -1;
    if (tEncodeSEpSet(pEncoder, &pTask->fixedEpDispatcher.epSet) < 0) return -1;
100
  } else if (pTask->outputType == TASK_OUTPUT__SHUFFLE_DISPATCH) {
L
Liu Jicong 已提交
101
    if (tSerializeSUseDbRspImp(pEncoder, &pTask->shuffleDispatcher.dbInfo) < 0) return -1;
L
Liu Jicong 已提交
102
    if (tEncodeCStr(pEncoder, pTask->shuffleDispatcher.stbFullName) < 0) return -1;
L
Liu Jicong 已提交
103
  }
104
  if (tEncodeI64(pEncoder, pTask->triggerParam) < 0) return -1;
L
Liu Jicong 已提交
105

L
Liu Jicong 已提交
106
  tEndEncode(pEncoder);
L
Liu Jicong 已提交
107 108 109 110
  return pEncoder->pos;
}

int32_t tDecodeSStreamTask(SDecoder* pDecoder, SStreamTask* pTask) {
L
Liu Jicong 已提交
111
  if (tStartDecode(pDecoder) < 0) return -1;
112 113
  if (tDecodeI64(pDecoder, &pTask->id.streamId) < 0) return -1;
  if (tDecodeI32(pDecoder, &pTask->id.taskId) < 0) return -1;
L
Liu Jicong 已提交
114
  if (tDecodeI32(pDecoder, &pTask->totalLevel) < 0) return -1;
115 116
  if (tDecodeI8(pDecoder, &pTask->taskLevel) < 0) return -1;
  if (tDecodeI8(pDecoder, &pTask->outputType) < 0) return -1;
L
Liu Jicong 已提交
117
  if (tDecodeI16(pDecoder, &pTask->dispatchMsgType) < 0) return -1;
118 119

  if (tDecodeI8(pDecoder, &pTask->taskStatus) < 0) return -1;
L
Liu Jicong 已提交
120
  if (tDecodeI8(pDecoder, &pTask->schedStatus) < 0) return -1;
L
Liu Jicong 已提交
121

L
Liu Jicong 已提交
122
  if (tDecodeI32(pDecoder, &pTask->selfChildId) < 0) return -1;
L
Liu Jicong 已提交
123 124 125
  if (tDecodeI32(pDecoder, &pTask->nodeId) < 0) return -1;
  if (tDecodeSEpSet(pDecoder, &pTask->epSet) < 0) return -1;

L
Liu Jicong 已提交
126 127 128 129
  if (tDecodeI64(pDecoder, &pTask->recoverSnapVer) < 0) return -1;
  if (tDecodeI64(pDecoder, &pTask->startVer) < 0) return -1;
  if (tDecodeI8(pDecoder, &pTask->fillHistory) < 0) return -1;

L
Liu Jicong 已提交
130 131 132 133 134 135
  int32_t epSz;
  if (tDecodeI32(pDecoder, &epSz) < 0) return -1;
  pTask->childEpInfo = taosArrayInit(epSz, sizeof(void*));
  for (int32_t i = 0; i < epSz; i++) {
    SStreamChildEpInfo* pInfo = taosMemoryCalloc(1, sizeof(SStreamChildEpInfo));
    if (pInfo == NULL) return -1;
5
54liuyao 已提交
136 137 138 139
    if (tDecodeStreamEpInfo(pDecoder, pInfo) < 0) {
      taosMemoryFreeClear(pInfo);
      return -1;
    }
L
Liu Jicong 已提交
140 141 142
    taosArrayPush(pTask->childEpInfo, &pInfo);
  }

143
  if (pTask->taskLevel != TASK_LEVEL__SINK) {
L
Liu Jicong 已提交
144 145 146
    if (tDecodeCStrAlloc(pDecoder, &pTask->exec.qmsg) < 0) return -1;
  }

147
  if (pTask->outputType == TASK_OUTPUT__TABLE) {
L
Liu Jicong 已提交
148 149 150 151 152
    if (tDecodeI64(pDecoder, &pTask->tbSink.stbUid) < 0) return -1;
    if (tDecodeCStrTo(pDecoder, pTask->tbSink.stbFullName) < 0) return -1;
    pTask->tbSink.pSchemaWrapper = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
    if (pTask->tbSink.pSchemaWrapper == NULL) return -1;
    if (tDecodeSSchemaWrapper(pDecoder, pTask->tbSink.pSchemaWrapper) < 0) return -1;
153
  } else if (pTask->outputType == TASK_OUTPUT__SMA) {
L
Liu Jicong 已提交
154
    if (tDecodeI64(pDecoder, &pTask->smaSink.smaId) < 0) return -1;
155
  } else if (pTask->outputType == TASK_OUTPUT__FETCH) {
L
Liu Jicong 已提交
156
    if (tDecodeI8(pDecoder, &pTask->fetchSink.reserved) < 0) return -1;
157
  } else if (pTask->outputType == TASK_OUTPUT__FIXED_DISPATCH) {
L
Liu Jicong 已提交
158 159 160
    if (tDecodeI32(pDecoder, &pTask->fixedEpDispatcher.taskId) < 0) return -1;
    if (tDecodeI32(pDecoder, &pTask->fixedEpDispatcher.nodeId) < 0) return -1;
    if (tDecodeSEpSet(pDecoder, &pTask->fixedEpDispatcher.epSet) < 0) return -1;
161
  } else if (pTask->outputType == TASK_OUTPUT__SHUFFLE_DISPATCH) {
L
Liu Jicong 已提交
162
    if (tDeserializeSUseDbRspImp(pDecoder, &pTask->shuffleDispatcher.dbInfo) < 0) return -1;
L
Liu Jicong 已提交
163
    if (tDecodeCStrTo(pDecoder, pTask->shuffleDispatcher.stbFullName) < 0) return -1;
L
Liu Jicong 已提交
164
  }
165
  if (tDecodeI64(pDecoder, &pTask->triggerParam) < 0) return -1;
L
Liu Jicong 已提交
166

L
Liu Jicong 已提交
167
  tEndDecode(pDecoder);
L
Liu Jicong 已提交
168 169 170
  return 0;
}

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
void tFreeStreamTask(SStreamTask* pTask) {
  qDebug("free s-task:%s", pTask->id.idStr);

  if (pTask->inputQueue) {
    streamQueueClose(pTask->inputQueue);
  }
  if (pTask->outputQueue) {
    streamQueueClose(pTask->outputQueue);
  }
  if (pTask->exec.qmsg) {
    taosMemoryFree(pTask->exec.qmsg);
  }

  if (pTask->exec.pExecutor) {
    qDestroyTask(pTask->exec.pExecutor);
    pTask->exec.pExecutor = NULL;
  }

  if (pTask->exec.pTqReader != NULL) {
    pTask->exec.pTqReader = NULL;
  }

L
Liu Jicong 已提交
193
  taosArrayDestroyP(pTask->childEpInfo, taosMemoryFree);
L
Liu Jicong 已提交
194 195 196 197
  if (pTask->outputType == TASK_OUTPUT__TABLE) {
    tDeleteSSchemaWrapper(pTask->tbSink.pSchemaWrapper);
    taosMemoryFree(pTask->tbSink.pTSchema);
  }
198

L
Liu Jicong 已提交
199 200
  if (pTask->outputType == TASK_OUTPUT__SHUFFLE_DISPATCH) {
    taosArrayDestroy(pTask->shuffleDispatcher.dbInfo.pVgroupInfos);
L
Liu Jicong 已提交
201 202
    taosArrayDestroy(pTask->checkReqIds);
    pTask->checkReqIds = NULL;
L
Liu Jicong 已提交
203
  }
204

205 206 207
  if (pTask->pState) {
    streamStateClose(pTask->pState);
  }
208

L
Liu Jicong 已提交
209 210
  taosMemoryFree(pTask);
}