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

L
Liu Jicong 已提交
18
int32_t tqAddBlockDataToRsp(const SSDataBlock* pBlock, SMqDataRsp* pRsp, int32_t numOfCols, int8_t precision) {
L
Liu Jicong 已提交
19 20 21 22 23 24
  int32_t dataStrLen = sizeof(SRetrieveTableRsp) + blockGetEncodeSize(pBlock);
  void*   buf = taosMemoryCalloc(1, dataStrLen);
  if (buf == NULL) return -1;

  SRetrieveTableRsp* pRetrieve = (SRetrieveTableRsp*)buf;
  pRetrieve->useconds = 0;
L
Liu Jicong 已提交
25
  pRetrieve->precision = precision;
L
Liu Jicong 已提交
26 27 28 29
  pRetrieve->compressed = 0;
  pRetrieve->completed = 1;
  pRetrieve->numOfRows = htonl(pBlock->info.rows);

H
Haojun Liao 已提交
30
  int32_t actualLen = blockEncode(pBlock, pRetrieve->data, numOfCols);
L
Liu Jicong 已提交
31 32 33 34 35 36 37
  actualLen += sizeof(SRetrieveTableRsp);
  ASSERT(actualLen <= dataStrLen);
  taosArrayPush(pRsp->blockDataLen, &actualLen);
  taosArrayPush(pRsp->blockData, &buf);
  return 0;
}

L
Liu Jicong 已提交
38 39
static int32_t tqAddBlockSchemaToRsp(const STqExecHandle* pExec, SMqDataRsp* pRsp) {
  SSchemaWrapper* pSW = tCloneSSchemaWrapper(pExec->pExecReader->pSchemaWrapper);
L
Liu Jicong 已提交
40 41 42
  if (pSW == NULL) {
    return -1;
  }
L
Liu Jicong 已提交
43 44 45 46
  taosArrayPush(pRsp->blockSchema, &pSW);
  return 0;
}

47
static int32_t tqAddTbNameToRsp(const STQ* pTq, int64_t uid, SMqDataRsp* pRsp, int32_t n) {
L
Liu Jicong 已提交
48 49
  SMetaReader mr = {0};
  metaReaderInit(&mr, pTq->pVnode->pMeta, 0);
50
  // TODO add reference to gurantee success
L
Liu Jicong 已提交
51
  if (metaGetTableEntryByUid(&mr, uid) < 0) {
52
    metaReaderClear(&mr);
L
Liu Jicong 已提交
53 54
    return -1;
  }
55 56 57 58
  for (int32_t i = 0; i < n; i++) {
    char* tbName = strdup(mr.me.name);
    taosArrayPush(pRsp->blockTbName, &tbName);
  }
L
Liu Jicong 已提交
59 60 61 62
  metaReaderClear(&mr);
  return 0;
}

63
int32_t tqScanData(STQ* pTq, const STqHandle* pHandle, SMqDataRsp* pRsp, STqOffsetVal* pOffset) {
L
Liu Jicong 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
  const STqExecHandle* pExec = &pHandle->execHandle;
  ASSERT(pExec->subType == TOPIC_SUB_TYPE__COLUMN);

  qTaskInfo_t task = pExec->task;

  if (qStreamPrepareScan(task, pOffset, pHandle->execHandle.subType) < 0) {
    tqDebug("prepare scan failed, return");
    if (pOffset->type == TMQ_OFFSET__LOG) {
      pRsp->rspOffset = *pOffset;
      return 0;
    } else {
      tqOffsetResetToLog(pOffset, pHandle->snapshotVer);
      if (qStreamPrepareScan(task, pOffset, pHandle->execHandle.subType) < 0) {
        tqDebug("prepare scan failed, return");
        pRsp->rspOffset = *pOffset;
        return 0;
      }
    }
  }

  int32_t rowCnt = 0;
  while (1) {
    SSDataBlock* pDataBlock = NULL;
    uint64_t     ts = 0;
88
    tqDebug("vgId:%d, tmq task start to execute", pTq->pVnode->config.vgId);
L
Liu Jicong 已提交
89 90 91
    if (qExecTask(task, &pDataBlock, &ts) < 0) {
      ASSERT(0);
    }
92
    tqDebug("vgId:%d, tmq task executed, get %p", pTq->pVnode->config.vgId, pDataBlock);
L
Liu Jicong 已提交
93

94 95 96 97
    if (pDataBlock == NULL) {
      break;
    }

L
Liu Jicong 已提交
98
    tqAddBlockDataToRsp(pDataBlock, pRsp, pExec->numOfCols, pTq->pVnode->config.tsdbCfg.precision);
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
    pRsp->blockNum++;

    if (pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
      rowCnt += pDataBlock->info.rows;
      if (rowCnt >= 4096) break;
    }
  }

  if (qStreamExtractOffset(task, &pRsp->rspOffset) < 0) {
    ASSERT(0);
    return -1;
  }
  ASSERT(pRsp->rspOffset.type != 0);

  if (pRsp->withTbName) {
    if (pRsp->rspOffset.type == TMQ_OFFSET__LOG) {
      int64_t uid = pExec->pExecReader->msgIter.uid;
116
      tqAddTbNameToRsp(pTq, uid, pRsp, 1);
117 118
    } else {
      pRsp->withTbName = false;
L
Liu Jicong 已提交
119 120
    }
  }
121
  ASSERT(pRsp->withSchema == false);
L
Liu Jicong 已提交
122 123 124 125

  return 0;
}

L
Liu Jicong 已提交
126
int32_t tqScanTaosx(STQ* pTq, const STqHandle* pHandle, STaosxRsp* pRsp, SMqMetaRsp* pMetaRsp, STqOffsetVal* pOffset) {
L
Liu Jicong 已提交
127
  const STqExecHandle* pExec = &pHandle->execHandle;
128
  qTaskInfo_t          task = pExec->task;
L
Liu Jicong 已提交
129

130
  if (qStreamPrepareScan(task, pOffset, pHandle->execHandle.subType) < 0) {
131
    tqDebug("prepare scan failed, return");
L
Liu Jicong 已提交
132 133 134 135
    if (pOffset->type == TMQ_OFFSET__LOG) {
      pRsp->rspOffset = *pOffset;
      return 0;
    } else {
136
      tqOffsetResetToLog(pOffset, pHandle->snapshotVer);
137
      if (qStreamPrepareScan(task, pOffset, pHandle->execHandle.subType) < 0) {
138
        tqDebug("prepare scan failed, return");
L
Liu Jicong 已提交
139 140 141 142
        pRsp->rspOffset = *pOffset;
        return 0;
      }
    }
L
Liu Jicong 已提交
143 144
  }

L
Liu Jicong 已提交
145
  int32_t rowCnt = 0;
L
Liu Jicong 已提交
146 147 148
  while (1) {
    SSDataBlock* pDataBlock = NULL;
    uint64_t     ts = 0;
wmmhello's avatar
wmmhello 已提交
149
    tqDebug("tmqsnap task start to execute");
L
Liu Jicong 已提交
150 151 152
    if (qExecTask(task, &pDataBlock, &ts) < 0) {
      ASSERT(0);
    }
wmmhello's avatar
wmmhello 已提交
153
    tqDebug("tmqsnap task execute end, get %p", pDataBlock);
L
Liu Jicong 已提交
154 155 156

    if (pDataBlock != NULL) {
      if (pRsp->withTbName) {
wmmhello's avatar
wmmhello 已提交
157
        int64_t uid = 0;
L
Liu Jicong 已提交
158
        if (pOffset->type == TMQ_OFFSET__LOG) {
wmmhello's avatar
wmmhello 已提交
159
          uid = pExec->pExecReader->msgIter.uid;
160
          if (tqAddTbNameToRsp(pTq, uid, (SMqDataRsp*)pRsp, 1) < 0) {
wmmhello's avatar
wmmhello 已提交
161 162
            continue;
          }
L
Liu Jicong 已提交
163 164 165
        } else {
          char* tbName = strdup(qExtractTbnameFromTask(task));
          taosArrayPush(pRsp->blockTbName, &tbName);
L
Liu Jicong 已提交
166 167
        }
      }
L
Liu Jicong 已提交
168
      if (pRsp->withSchema) {
wmmhello's avatar
wmmhello 已提交
169
        if (pOffset->type == TMQ_OFFSET__LOG) {
170
          tqAddBlockSchemaToRsp(pExec, (SMqDataRsp*)pRsp);
L
Liu Jicong 已提交
171
        } else {
wmmhello's avatar
wmmhello 已提交
172 173 174
          SSchemaWrapper* pSW = tCloneSSchemaWrapper(qExtractSchemaFromTask(task));
          taosArrayPush(pRsp->blockSchema, &pSW);
        }
wmmhello's avatar
wmmhello 已提交
175
      }
wmmhello's avatar
wmmhello 已提交
176

L
Liu Jicong 已提交
177 178
      tqAddBlockDataToRsp(pDataBlock, (SMqDataRsp*)pRsp, taosArrayGetSize(pDataBlock->pDataBlock),
                          pTq->pVnode->config.tsdbCfg.precision);
179
      pRsp->blockNum++;
L
Liu Jicong 已提交
180
      if (pOffset->type == TMQ_OFFSET__LOG) {
L
Liu Jicong 已提交
181 182
        continue;
      } else {
L
Liu Jicong 已提交
183
        rowCnt += pDataBlock->info.rows;
L
Liu Jicong 已提交
184
        if (rowCnt <= 4096) continue;
L
Liu Jicong 已提交
185 186 187
      }
    }

188 189
    if (pDataBlock == NULL && pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
      if (qStreamExtractPrepareUid(task) != 0) {
190 191
        continue;
      }
192 193 194 195
      tqDebug("tmqsnap vgId: %d, tsdb consume over, switch to wal, ver %" PRId64, TD_VID(pTq->pVnode),
              pHandle->snapshotVer + 1);
      break;
    }
196

197 198 199
    if (pRsp->blockNum > 0) {
      tqDebug("tmqsnap task exec exited, get data");
      break;
L
Liu Jicong 已提交
200
    }
201

202 203 204 205 206 207 208 209 210 211 212 213
    SMqMetaRsp* tmp = qStreamExtractMetaMsg(task);
    if (tmp->rspOffset.type == TMQ_OFFSET__SNAPSHOT_DATA) {
      tqOffsetResetToData(pOffset, tmp->rspOffset.uid, tmp->rspOffset.ts);
      qStreamPrepareScan(task, pOffset, pHandle->execHandle.subType);
      tmp->rspOffset.type = TMQ_OFFSET__SNAPSHOT_META;
      tqDebug("tmqsnap task exec change to get data");
      continue;
    }

    *pMetaRsp = *tmp;
    tqDebug("tmqsnap task exec exited, get meta");

214
    tqDebug("task exec exited");
L
Liu Jicong 已提交
215 216 217
    break;
  }

218 219 220 221 222
  if (qStreamExtractOffset(task, &pRsp->rspOffset) < 0) {
    ASSERT(0);
  }

  ASSERT(pRsp->rspOffset.type != 0);
L
Liu Jicong 已提交
223 224 225
  return 0;
}

226
int32_t tqTaosxScanLog(STQ* pTq, STqHandle* pHandle, SSubmitReq* pReq, STaosxRsp* pRsp) {
L
Liu Jicong 已提交
227
  STqExecHandle* pExec = &pHandle->execHandle;
wmmhello's avatar
wmmhello 已提交
228 229
  ASSERT(pExec->subType != TOPIC_SUB_TYPE__COLUMN);

230 231 232
  SArray* pBlocks = taosArrayInit(0, sizeof(SSDataBlock));
  SArray* pSchemas = taosArrayInit(0, sizeof(SSchemaWrapper));

wmmhello's avatar
wmmhello 已提交
233 234 235
  if (pExec->subType == TOPIC_SUB_TYPE__TABLE) {
    STqReader* pReader = pExec->pExecReader;
    tqReaderSetDataMsg(pReader, pReq, 0);
L
Liu Jicong 已提交
236
    while (tqNextDataBlock(pReader)) {
237 238 239 240 241 242 243 244
      /*SSDataBlock block = {0};*/
      /*if (tqRetrieveDataBlock(&block, pReader) < 0) {*/
      /*if (terrno == TSDB_CODE_TQ_TABLE_SCHEMA_NOT_FOUND) continue;*/
      /*}*/

      taosArrayClear(pBlocks);
      taosArrayClear(pSchemas);
      if (tqRetrieveTaosxBlock(pReader, pBlocks, pSchemas) < 0) {
L
Liu Jicong 已提交
245 246
        if (terrno == TSDB_CODE_TQ_TABLE_SCHEMA_NOT_FOUND) continue;
      }
wmmhello's avatar
wmmhello 已提交
247 248
      if (pRsp->withTbName) {
        int64_t uid = pExec->pExecReader->msgIter.uid;
249 250
        if (tqAddTbNameToRsp(pTq, uid, (SMqDataRsp*)pRsp, taosArrayGetSize(pBlocks)) < 0) {
          taosArrayDestroyEx(pBlocks, (FDelete)blockDataFreeRes);
wmmhello's avatar
wmmhello 已提交
251 252 253
          continue;
        }
      }
L
Liu Jicong 已提交
254 255
      if (pHandle->fetchMeta) {
        SSubmitBlk* pBlk = pReader->pBlock;
L
Liu Jicong 已提交
256
        int32_t     schemaLen = htonl(pBlk->schemaLen);
257
        if (schemaLen > 0) {
L
Liu Jicong 已提交
258 259 260 261
          if (pRsp->createTableNum == 0) {
            pRsp->createTableLen = taosArrayInit(0, sizeof(int32_t));
            pRsp->createTableReq = taosArrayInit(0, sizeof(void*));
          }
262 263 264
          void* createReq = taosMemoryCalloc(1, schemaLen);
          memcpy(createReq, pBlk->data, schemaLen);
          taosArrayPush(pRsp->createTableLen, &schemaLen);
L
Liu Jicong 已提交
265 266 267 268
          taosArrayPush(pRsp->createTableReq, &createReq);
          pRsp->createTableNum++;
        }
      }
269 270 271 272 273 274 275 276 277
      for (int32_t i = 0; i < taosArrayGetSize(pBlocks); i++) {
        SSDataBlock* pBlock = taosArrayGet(pBlocks, i);
        tqAddBlockDataToRsp(pBlock, (SMqDataRsp*)pRsp, taosArrayGetSize(pBlock->pDataBlock),
                            pTq->pVnode->config.tsdbCfg.precision);
        blockDataFreeRes(pBlock);
        SSchemaWrapper* pSW = taosArrayGet(pSchemas, i);
        taosArrayPush(pRsp->blockSchema, &pSW);
        pRsp->blockNum++;
      }
L
Liu Jicong 已提交
278
    }
wmmhello's avatar
wmmhello 已提交
279 280 281 282
  } else if (pExec->subType == TOPIC_SUB_TYPE__DB) {
    STqReader* pReader = pExec->pExecReader;
    tqReaderSetDataMsg(pReader, pReq, 0);
    while (tqNextDataBlockFilterOut(pReader, pExec->execDb.pFilterOutTbUid)) {
283 284 285 286 287 288 289
      /*SSDataBlock block = {0};*/
      /*if (tqRetrieveDataBlock(&block, pReader) < 0) {*/
      /*if (terrno == TSDB_CODE_TQ_TABLE_SCHEMA_NOT_FOUND) continue;*/
      /*}*/
      taosArrayClear(pBlocks);
      taosArrayClear(pSchemas);
      if (tqRetrieveTaosxBlock(pReader, pBlocks, pSchemas) < 0) {
L
Liu Jicong 已提交
290 291
        if (terrno == TSDB_CODE_TQ_TABLE_SCHEMA_NOT_FOUND) continue;
      }
wmmhello's avatar
wmmhello 已提交
292 293
      if (pRsp->withTbName) {
        int64_t uid = pExec->pExecReader->msgIter.uid;
294 295 296
        if (tqAddTbNameToRsp(pTq, uid, (SMqDataRsp*)pRsp, taosArrayGetSize(pBlocks)) < 0) {
          taosArrayDestroyEx(pBlocks, (FDelete)blockDataFreeRes);
          /*blockDataFreeRes(&block);*/
wmmhello's avatar
wmmhello 已提交
297 298 299
          continue;
        }
      }
L
Liu Jicong 已提交
300 301
      if (pHandle->fetchMeta) {
        SSubmitBlk* pBlk = pReader->pBlock;
L
Liu Jicong 已提交
302
        int32_t     schemaLen = htonl(pBlk->schemaLen);
303
        if (schemaLen > 0) {
L
Liu Jicong 已提交
304 305 306
          if (pRsp->createTableNum == 0) {
            pRsp->createTableLen = taosArrayInit(0, sizeof(int32_t));
            pRsp->createTableReq = taosArrayInit(0, sizeof(void*));
L
Liu Jicong 已提交
307
          }
308 309 310
          void* createReq = taosMemoryCalloc(1, schemaLen);
          memcpy(createReq, pBlk->data, schemaLen);
          taosArrayPush(pRsp->createTableLen, &schemaLen);
L
Liu Jicong 已提交
311 312
          taosArrayPush(pRsp->createTableReq, &createReq);
          pRsp->createTableNum++;
L
Liu Jicong 已提交
313 314
        }
      }
315 316 317 318 319 320 321 322 323 324 325 326 327 328
      /*tqAddBlockDataToRsp(&block, (SMqDataRsp*)pRsp, taosArrayGetSize(block.pDataBlock),*/
      /*pTq->pVnode->config.tsdbCfg.precision);*/
      /*blockDataFreeRes(&block);*/
      /*tqAddBlockSchemaToRsp(pExec, (SMqDataRsp*)pRsp);*/
      /*pRsp->blockNum++;*/
      for (int32_t i = 0; i < taosArrayGetSize(pBlocks); i++) {
        SSDataBlock* pBlock = taosArrayGet(pBlocks, i);
        tqAddBlockDataToRsp(pBlock, (SMqDataRsp*)pRsp, taosArrayGetSize(pBlock->pDataBlock),
                            pTq->pVnode->config.tsdbCfg.precision);
        blockDataFreeRes(pBlock);
        SSchemaWrapper* pSW = taosArrayGet(pSchemas, i);
        taosArrayPush(pRsp->blockSchema, &pSW);
        pRsp->blockNum++;
      }
L
Liu Jicong 已提交
329
    }
L
Liu Jicong 已提交
330
  }
L
Liu Jicong 已提交
331

332 333 334
  taosArrayDestroy(pBlocks);
  taosArrayDestroy(pSchemas);

wmmhello's avatar
wmmhello 已提交
335 336 337 338 339
  if (pRsp->blockNum == 0) {
    return -1;
  }

  return 0;
L
Liu Jicong 已提交
340
}