tqExec.c 7.0 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"

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

  SRetrieveTableRsp* pRetrieve = (SRetrieveTableRsp*)buf;
  pRetrieve->useconds = 0;
  pRetrieve->precision = TSDB_DEFAULT_PRECISION;
  pRetrieve->compressed = 0;
  pRetrieve->completed = 1;
  pRetrieve->numOfRows = htonl(pBlock->info.rows);

  // TODO enable compress
  int32_t actualLen = 0;
32
  blockEncode(pBlock, pRetrieve->data, &actualLen, numOfCols, false);
L
Liu Jicong 已提交
33 34 35 36 37 38 39
  actualLen += sizeof(SRetrieveTableRsp);
  ASSERT(actualLen <= dataStrLen);
  taosArrayPush(pRsp->blockDataLen, &actualLen);
  taosArrayPush(pRsp->blockData, &buf);
  return 0;
}

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

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

L
Liu Jicong 已提交
63 64
int64_t tqScan(STQ* pTq, const STqHandle* pHandle, SMqDataRsp* pRsp, STqOffsetVal* pOffset) {
  const STqExecHandle* pExec = &pHandle->execHandle;
L
Liu Jicong 已提交
65
  qTaskInfo_t          task = pExec->execCol.task;
L
Liu Jicong 已提交
66

L
Liu Jicong 已提交
67
  if (qStreamPrepareScan(task, pOffset) < 0) {
L
Liu Jicong 已提交
68 69 70 71
    if (pOffset->type == TMQ_OFFSET__LOG) {
      pRsp->rspOffset = *pOffset;
      return 0;
    } else {
72
      tqOffsetResetToLog(pOffset, pHandle->snapshotVer);
L
Liu Jicong 已提交
73 74 75 76 77
      if (qStreamPrepareScan(task, pOffset) < 0) {
        pRsp->rspOffset = *pOffset;
        return 0;
      }
    }
L
Liu Jicong 已提交
78 79
  }

L
Liu Jicong 已提交
80
  int32_t rowCnt = 0;
L
Liu Jicong 已提交
81 82 83
  while (1) {
    SSDataBlock* pDataBlock = NULL;
    uint64_t     ts = 0;
L
Liu Jicong 已提交
84
    tqDebug("task start to execute");
L
Liu Jicong 已提交
85 86 87
    if (qExecTask(task, &pDataBlock, &ts) < 0) {
      ASSERT(0);
    }
L
Liu Jicong 已提交
88
    tqDebug("task execute end, get %p", pDataBlock);
L
Liu Jicong 已提交
89 90 91

    if (pDataBlock != NULL) {
      if (pRsp->withTbName) {
L
Liu Jicong 已提交
92
        if (pOffset->type == TMQ_OFFSET__LOG) {
L
Liu Jicong 已提交
93
          int64_t uid = pExec->pExecReader->msgIter.uid;
94 95 96
          if (tqAddTbNameToRsp(pTq, uid, pRsp) < 0) {
            continue;
          }
L
Liu Jicong 已提交
97 98 99 100
        } else {
          pRsp->withTbName = 0;
        }
      }
101
      tqAddBlockDataToRsp(pDataBlock, pRsp, pExec->numOfCols);
102
      pRsp->blockNum++;
L
Liu Jicong 已提交
103
      if (pOffset->type == TMQ_OFFSET__LOG) {
L
Liu Jicong 已提交
104 105
        continue;
      } else {
L
Liu Jicong 已提交
106
        rowCnt += pDataBlock->info.rows;
L
Liu Jicong 已提交
107
        if (rowCnt <= 4096) continue;
L
Liu Jicong 已提交
108 109 110
      }
    }

111
    if (pRsp->blockNum == 0 && pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA) {
L
Liu Jicong 已提交
112
      tqDebug("vgId: %d, tsdb consume over, switch to wal, ver %ld", TD_VID(pTq->pVnode), pHandle->snapshotVer + 1);
113
      tqOffsetResetToLog(pOffset, pHandle->snapshotVer);
114 115 116 117
      qStreamPrepareScan(task, pOffset);
      continue;
    }

L
Liu Jicong 已提交
118 119 120 121 122 123 124 125
    void* meta = qStreamExtractMetaMsg(task);
    if (meta != NULL) {
      // tq add meta to rsp
    }

    if (qStreamExtractOffset(task, &pRsp->rspOffset) < 0) {
      ASSERT(0);
    }
L
Liu Jicong 已提交
126

L
Liu Jicong 已提交
127 128
    ASSERT(pRsp->rspOffset.type != 0);

129
    if (pRsp->reqOffset.type == TMQ_OFFSET__LOG) {
L
Liu Jicong 已提交
130 131
      ASSERT(pRsp->rspOffset.version + 1 >= pRsp->reqOffset.version);
    }
L
Liu Jicong 已提交
132
    tqDebug("task exec exited");
L
Liu Jicong 已提交
133 134 135 136 137 138
    break;
  }

  return 0;
}

L
Liu Jicong 已提交
139
#if 0
L
Liu Jicong 已提交
140
int32_t tqScanSnapshot(STQ* pTq, const STqExecHandle* pExec, SMqDataRsp* pRsp, STqOffsetVal offset, int32_t workerId) {
L
Liu Jicong 已提交
141 142
  ASSERT(pExec->subType == TOPIC_SUB_TYPE__COLUMN);
  qTaskInfo_t task = pExec->execCol.task[workerId];
L
Liu Jicong 已提交
143

L
Liu Jicong 已提交
144
  if (qStreamPrepareTsdbScan(task, offset.uid, offset.ts) < 0) {
L
Liu Jicong 已提交
145 146 147 148
    ASSERT(0);
  }

  int32_t rowCnt = 0;
L
Liu Jicong 已提交
149 150 151 152 153 154 155 156 157
  while (1) {
    SSDataBlock* pDataBlock = NULL;
    uint64_t     ts = 0;
    if (qExecTask(task, &pDataBlock, &ts) < 0) {
      ASSERT(0);
    }
    if (pDataBlock == NULL) break;

    ASSERT(pDataBlock->info.rows != 0);
H
Haojun Liao 已提交
158
    ASSERT(taosArrayGetSize(pDataBlock->pDataBlock) != 0);
L
Liu Jicong 已提交
159 160

    tqAddBlockDataToRsp(pDataBlock, pRsp);
L
Liu Jicong 已提交
161 162

    if (pRsp->withTbName) {
L
Liu Jicong 已提交
163
      pRsp->withTbName = 0;
164
#if 0
165 166 167 168 169
      int64_t uid;
      int64_t ts;
      if (qGetStreamScanStatus(task, &uid, &ts) < 0) {
        ASSERT(0);
      }
L
Liu Jicong 已提交
170
      tqAddTbNameToRsp(pTq, uid, pRsp);
L
Liu Jicong 已提交
171
#endif
L
Liu Jicong 已提交
172
    }
L
Liu Jicong 已提交
173
    pRsp->blockNum++;
L
Liu Jicong 已提交
174 175 176 177 178 179 180 181

    rowCnt += pDataBlock->info.rows;
    if (rowCnt >= 4096) break;
  }
  int64_t uid;
  int64_t ts;
  if (qGetStreamScanStatus(task, &uid, &ts) < 0) {
    ASSERT(0);
L
Liu Jicong 已提交
182
  }
L
Liu Jicong 已提交
183
  tqOffsetResetToData(&pRsp->rspOffset, uid, ts);
L
Liu Jicong 已提交
184 185 186

  return 0;
}
L
Liu Jicong 已提交
187
#endif
L
Liu Jicong 已提交
188

L
Liu Jicong 已提交
189
int32_t tqLogScanExec(STQ* pTq, STqExecHandle* pExec, SSubmitReq* pReq, SMqDataRsp* pRsp) {
L
Liu Jicong 已提交
190
  ASSERT(pExec->subType != TOPIC_SUB_TYPE__COLUMN);
L
Liu Jicong 已提交
191

L
Liu Jicong 已提交
192
  if (pExec->subType == TOPIC_SUB_TYPE__TABLE) {
L
Liu Jicong 已提交
193
    pRsp->withSchema = 1;
L
Liu Jicong 已提交
194
    STqReader* pReader = pExec->pExecReader;
L
Liu Jicong 已提交
195
    tqReaderSetDataMsg(pReader, pReq, 0);
L
Liu Jicong 已提交
196 197
    while (tqNextDataBlock(pReader)) {
      SSDataBlock block = {0};
198
      if (tqRetrieveDataBlock(&block, pReader) < 0) {
L
Liu Jicong 已提交
199 200 201
        if (terrno == TSDB_CODE_TQ_TABLE_SCHEMA_NOT_FOUND) continue;
      }
      if (pRsp->withTbName) {
L
Liu Jicong 已提交
202
        int64_t uid = pExec->pExecReader->msgIter.uid;
203
        if (tqAddTbNameToRsp(pTq, uid, pRsp) < 0) {
L
Liu Jicong 已提交
204
          blockDataFreeRes(&block);
205 206
          continue;
        }
L
Liu Jicong 已提交
207
      }
208
      tqAddBlockDataToRsp(&block, pRsp, taosArrayGetSize(block.pDataBlock));
L
Liu Jicong 已提交
209
      blockDataFreeRes(&block);
L
Liu Jicong 已提交
210
      tqAddBlockSchemaToRsp(pExec, pRsp);
L
Liu Jicong 已提交
211 212 213 214
      pRsp->blockNum++;
    }
  } else if (pExec->subType == TOPIC_SUB_TYPE__DB) {
    pRsp->withSchema = 1;
L
Liu Jicong 已提交
215
    STqReader* pReader = pExec->pExecReader;
L
Liu Jicong 已提交
216
    tqReaderSetDataMsg(pReader, pReq, 0);
L
Liu Jicong 已提交
217
    while (tqNextDataBlockFilterOut(pReader, pExec->execDb.pFilterOutTbUid)) {
L
Liu Jicong 已提交
218
      SSDataBlock block = {0};
219
      if (tqRetrieveDataBlock(&block, pReader) < 0) {
L
Liu Jicong 已提交
220 221 222
        if (terrno == TSDB_CODE_TQ_TABLE_SCHEMA_NOT_FOUND) continue;
      }
      if (pRsp->withTbName) {
L
Liu Jicong 已提交
223
        int64_t uid = pExec->pExecReader->msgIter.uid;
224
        if (tqAddTbNameToRsp(pTq, uid, pRsp) < 0) {
L
Liu Jicong 已提交
225
          blockDataFreeRes(&block);
226 227
          continue;
        }
L
Liu Jicong 已提交
228
      }
229
      tqAddBlockDataToRsp(&block, pRsp, taosArrayGetSize(block.pDataBlock));
L
Liu Jicong 已提交
230
      blockDataFreeRes(&block);
L
Liu Jicong 已提交
231
      tqAddBlockSchemaToRsp(pExec, pRsp);
L
Liu Jicong 已提交
232 233 234
      pRsp->blockNum++;
    }
  }
L
Liu Jicong 已提交
235

L
Liu Jicong 已提交
236 237 238
  if (pRsp->blockNum == 0) {
    return -1;
  }
L
Liu Jicong 已提交
239

L
Liu Jicong 已提交
240 241
  return 0;
}