tqExec.c 6.9 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;
}

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

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

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

    if (pDataBlock != NULL) {
      if (pRsp->withTbName) {
wmmhello's avatar
wmmhello 已提交
94
        int64_t uid = 0;
L
Liu Jicong 已提交
95
        if (pOffset->type == TMQ_OFFSET__LOG) {
wmmhello's avatar
wmmhello 已提交
96
          uid = pExec->pExecReader->msgIter.uid;
wmmhello's avatar
wmmhello 已提交
97 98 99
          if (tqAddTbNameToRsp(pTq, uid, pRsp) < 0) {
            continue;
          }
L
Liu Jicong 已提交
100
        } else {
wmmhello's avatar
wmmhello 已提交
101 102
          char* tbName = strdup(qExtractTbnameFromTask(task));
          taosArrayPush(pRsp->blockTbName, &tbName);
L
Liu Jicong 已提交
103 104
        }
      }
wmmhello's avatar
wmmhello 已提交
105
      if(pRsp->withSchema){
wmmhello's avatar
wmmhello 已提交
106 107 108 109 110 111
        if (pOffset->type == TMQ_OFFSET__LOG) {
          tqAddBlockSchemaToRsp(pExec, pRsp);
        }else{
          SSchemaWrapper* pSW = tCloneSSchemaWrapper(qExtractSchemaFromTask(task));
          taosArrayPush(pRsp->blockSchema, &pSW);
        }
wmmhello's avatar
wmmhello 已提交
112 113
      }
      tqAddBlockDataToRsp(pDataBlock, pRsp, taosArrayGetSize(pDataBlock->pDataBlock));
114
      pRsp->blockNum++;
L
Liu Jicong 已提交
115
      if (pOffset->type == TMQ_OFFSET__LOG) {
L
Liu Jicong 已提交
116 117
        continue;
      } else {
L
Liu Jicong 已提交
118
        rowCnt += pDataBlock->info.rows;
L
Liu Jicong 已提交
119
        if (rowCnt <= 4096) continue;
L
Liu Jicong 已提交
120 121 122
      }
    }

wmmhello's avatar
wmmhello 已提交
123
    if (pDataBlock == NULL && pOffset->type == TMQ_OFFSET__SNAPSHOT_DATA){
wmmhello's avatar
wmmhello 已提交
124 125 126 127
      if(pHandle->execHandle.subType != TOPIC_SUB_TYPE__COLUMN && qStreamExtractPrepareUid(task) != 0){
        continue;
      }
      tqDebug("tmqsnap vgId: %d, tsdb consume over, switch to wal, ver %" PRId64, TD_VID(pTq->pVnode),
S
Shengliang Guan 已提交
128
              pHandle->snapshotVer + 1);
wmmhello's avatar
wmmhello 已提交
129 130
//      tqOffsetResetToLog(pOffset, pHandle->snapshotVer);
//      qStreamPrepareScan(task, pOffset, pHandle->execHandle.subType);
131 132
    }

wmmhello's avatar
wmmhello 已提交
133 134
    if (pRsp->blockNum > 0){
      qStreamExtractOffset(task, &pRsp->rspOffset);
wmmhello's avatar
wmmhello 已提交
135
      tqDebug("tmqsnap task exec exited, get data");
wmmhello's avatar
wmmhello 已提交
136
      break;
L
Liu Jicong 已提交
137 138
    }

wmmhello's avatar
wmmhello 已提交
139 140
    SMqMetaRsp* tmp = qStreamExtractMetaMsg(task);
    if(tmp->rspOffset.type == TMQ_OFFSET__SNAPSHOT_DATA){
wmmhello's avatar
wmmhello 已提交
141 142
      tqOffsetResetToData(pOffset, tmp->rspOffset.uid, tmp->rspOffset.ts);
      qStreamPrepareScan(task, pOffset, pHandle->execHandle.subType);
wmmhello's avatar
wmmhello 已提交
143
      tmp->rspOffset.type = TMQ_OFFSET__SNAPSHOT_META;
wmmhello's avatar
wmmhello 已提交
144
      tqDebug("tmqsnap task exec change to get data");
wmmhello's avatar
wmmhello 已提交
145
      continue;
L
Liu Jicong 已提交
146
    }
147

wmmhello's avatar
wmmhello 已提交
148
    *pMetaRsp = *tmp;
wmmhello's avatar
wmmhello 已提交
149
    tqDebug("tmqsnap task exec exited, get meta");
L
Liu Jicong 已提交
150 151 152 153 154 155
    break;
  }

  return 0;
}

L
Liu Jicong 已提交
156
#if 0
L
Liu Jicong 已提交
157
int32_t tqScanSnapshot(STQ* pTq, const STqExecHandle* pExec, SMqDataRsp* pRsp, STqOffsetVal offset, int32_t workerId) {
L
Liu Jicong 已提交
158 159
  ASSERT(pExec->subType == TOPIC_SUB_TYPE__COLUMN);
  qTaskInfo_t task = pExec->execCol.task[workerId];
L
Liu Jicong 已提交
160

L
Liu Jicong 已提交
161
  if (qStreamPrepareTsdbScan(task, offset.uid, offset.ts) < 0) {
L
Liu Jicong 已提交
162 163 164 165
    ASSERT(0);
  }

  int32_t rowCnt = 0;
L
Liu Jicong 已提交
166 167 168 169 170 171 172 173 174
  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 已提交
175
    ASSERT(taosArrayGetSize(pDataBlock->pDataBlock) != 0);
L
Liu Jicong 已提交
176 177

    tqAddBlockDataToRsp(pDataBlock, pRsp);
L
Liu Jicong 已提交
178 179

    if (pRsp->withTbName) {
L
Liu Jicong 已提交
180
      pRsp->withTbName = 0;
181
#if 0
182 183 184 185 186
      int64_t uid;
      int64_t ts;
      if (qGetStreamScanStatus(task, &uid, &ts) < 0) {
        ASSERT(0);
      }
L
Liu Jicong 已提交
187
      tqAddTbNameToRsp(pTq, uid, pRsp);
L
Liu Jicong 已提交
188
#endif
L
Liu Jicong 已提交
189
    }
L
Liu Jicong 已提交
190
    pRsp->blockNum++;
L
Liu Jicong 已提交
191 192 193 194 195 196 197 198

    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 已提交
199
  }
L
Liu Jicong 已提交
200
  tqOffsetResetToData(&pRsp->rspOffset, uid, ts);
L
Liu Jicong 已提交
201 202 203

  return 0;
}
L
Liu Jicong 已提交
204
#endif
L
Liu Jicong 已提交
205

wmmhello's avatar
wmmhello 已提交
206 207
SSDataBlock* tqLogScanExec(int8_t subType, STqReader* pReader, SHashObj* pFilterOutTbUid, SSDataBlock* block) {
  if (subType == TOPIC_SUB_TYPE__TABLE) {
L
Liu Jicong 已提交
208
    while (tqNextDataBlock(pReader)) {
wmmhello's avatar
wmmhello 已提交
209
      if (tqRetrieveDataBlock(block, pReader) < 0) {
L
Liu Jicong 已提交
210 211
        if (terrno == TSDB_CODE_TQ_TABLE_SCHEMA_NOT_FOUND) continue;
      }
wmmhello's avatar
wmmhello 已提交
212
      return block;
L
Liu Jicong 已提交
213
    }
wmmhello's avatar
wmmhello 已提交
214 215 216
  } else if (subType == TOPIC_SUB_TYPE__DB) {
    while (tqNextDataBlockFilterOut(pReader, pFilterOutTbUid)) {
      if (tqRetrieveDataBlock(block, pReader) < 0) {
L
Liu Jicong 已提交
217 218
        if (terrno == TSDB_CODE_TQ_TABLE_SCHEMA_NOT_FOUND) continue;
      }
wmmhello's avatar
wmmhello 已提交
219
      return block;
L
Liu Jicong 已提交
220 221
    }
  }
L
Liu Jicong 已提交
222

wmmhello's avatar
wmmhello 已提交
223
  return NULL;
L
Liu Jicong 已提交
224
}