tqExec.c 12.2 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
  pRetrieve->compressed = 0;
  pRetrieve->completed = 1;
28
  pRetrieve->numOfRows = htobe64((int64_t)pBlock->info.rows);
L
Liu Jicong 已提交
29

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

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

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

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

  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;
86
    tqDebug("vgId:%d, tmq task start to execute", pTq->pVnode->config.vgId);
L
Liu Jicong 已提交
87
    if (qExecTask(task, &pDataBlock, &ts) < 0) {
L
Liu Jicong 已提交
88 89
      tqError("vgId:%d task exec error since %s", pTq->pVnode->config.vgId, terrstr());
      return -1;
L
Liu Jicong 已提交
90
    }
91
    tqDebug("vgId:%d, tmq task executed, get %p", pTq->pVnode->config.vgId, pDataBlock);
L
Liu Jicong 已提交
92

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

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

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

  if (qStreamExtractOffset(task, &pRsp->rspOffset) < 0) {
    return -1;
  }
L
Liu Jicong 已提交
109 110 111 112 113 114

  if (pRsp->rspOffset.type == 0) {
    tqError("expected rsp offset: type %d %" PRId64 " %" PRId64 " %" PRId64, pRsp->rspOffset.type, pRsp->rspOffset.ts,
            pRsp->rspOffset.uid, pRsp->rspOffset.version);
    return -1;
  }
115 116 117 118

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

  return 0;
}

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

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

L
Liu Jicong 已提交
147
  int32_t rowCnt = 0;
L
Liu Jicong 已提交
148 149 150
  while (1) {
    SSDataBlock* pDataBlock = NULL;
    uint64_t     ts = 0;
wmmhello's avatar
wmmhello 已提交
151
    tqDebug("tmqsnap task start to execute");
L
Liu Jicong 已提交
152
    if (qExecTask(task, &pDataBlock, &ts) < 0) {
L
Liu Jicong 已提交
153 154
      tqError("vgId:%d task exec error since %s", pTq->pVnode->config.vgId, terrstr());
      return -1;
L
Liu Jicong 已提交
155
    }
wmmhello's avatar
wmmhello 已提交
156
    tqDebug("tmqsnap task execute end, get %p", pDataBlock);
L
Liu Jicong 已提交
157 158 159

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

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

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

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

205 206 207 208 209 210 211 212 213 214 215 216
    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");

217
    tqDebug("task exec exited");
L
Liu Jicong 已提交
218 219 220
    break;
  }

L
Liu Jicong 已提交
221 222 223 224 225 226
  qStreamExtractOffset(task, &pRsp->rspOffset);

  if (pRsp->rspOffset.type == 0) {
    tqError("expected rsp offset: type %d %" PRId64 " %" PRId64 " %" PRId64, pRsp->rspOffset.type, pRsp->rspOffset.ts,
            pRsp->rspOffset.uid, pRsp->rspOffset.version);
    return -1;
227 228
  }

L
Liu Jicong 已提交
229 230 231
  return 0;
}

232
int32_t tqTaosxScanLog(STQ* pTq, STqHandle* pHandle, SSubmitReq* pReq, STaosxRsp* pRsp) {
L
Liu Jicong 已提交
233
  STqExecHandle* pExec = &pHandle->execHandle;
L
Liu Jicong 已提交
234
  /*A(pExec->subType != TOPIC_SUB_TYPE__COLUMN);*/
wmmhello's avatar
wmmhello 已提交
235

236
  SArray* pBlocks = taosArrayInit(0, sizeof(SSDataBlock));
237
  SArray* pSchemas = taosArrayInit(0, sizeof(void*));
238

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

343 344 345
  taosArrayDestroy(pBlocks);
  taosArrayDestroy(pSchemas);

wmmhello's avatar
wmmhello 已提交
346 347 348 349 350
  if (pRsp->blockNum == 0) {
    return -1;
  }

  return 0;
L
Liu Jicong 已提交
351
}