tq.c 27.4 KB
Newer Older
H
refact  
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * 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/>.
S
Shengliang Guan 已提交
14 15
 */

L
Liu Jicong 已提交
16
#include "tcompare.h"
L
Liu Jicong 已提交
17
#include "tqInt.h"
L
Liu Jicong 已提交
18
#include "tqMetaStore.h"
S
Shengliang Guan 已提交
19

L
Liu Jicong 已提交
20 21
// static
// read next version data
L
Liu Jicong 已提交
22
//
L
Liu Jicong 已提交
23
// send to fetch queue
L
Liu Jicong 已提交
24
//
L
Liu Jicong 已提交
25
// handle management message
L
Liu Jicong 已提交
26
//
L
Liu Jicong 已提交
27

L
Liu Jicong 已提交
28 29 30
int tqGroupSSize(const STqGroup* pGroup);
int tqTopicSSize();
int tqItemSSize();
L
Liu Jicong 已提交
31

L
Liu Jicong 已提交
32 33 34
void* tqSerializeListHandle(STqList* listHandle, void* ptr);
void* tqSerializeTopic(STqTopic* pTopic, void* ptr);
void* tqSerializeItem(STqMsgItem* pItem, void* ptr);
L
Liu Jicong 已提交
35

L
Liu Jicong 已提交
36 37
const void* tqDeserializeTopic(const void* pBytes, STqTopic* pTopic);
const void* tqDeserializeItem(const void* pBytes, STqMsgItem* pItem);
L
Liu Jicong 已提交
38

L
Liu Jicong 已提交
39 40
int tqInit() {
  int8_t old = atomic_val_compare_exchange_8(&tqMgmt.inited, 0, 1);
L
Liu Jicong 已提交
41
  if (old == 1) return 0;
L
Liu Jicong 已提交
42 43 44 45 46 47 48

  tqMgmt.timer = taosTmrInit(0, 0, 0, "TQ");
  return 0;
}

void tqCleanUp() {
  int8_t old = atomic_val_compare_exchange_8(&tqMgmt.inited, 1, 0);
L
Liu Jicong 已提交
49
  if (old == 0) return;
L
Liu Jicong 已提交
50 51 52 53
  taosTmrStop(tqMgmt.timer);
  taosTmrCleanUp(tqMgmt.timer);
}

L
Liu Jicong 已提交
54
STQ* tqOpen(const char* path, SWal* pWal, SMeta* pMeta, STqCfg* tqConfig, SMemAllocatorFactory* allocFac) {
L
Liu Jicong 已提交
55
  STQ* pTq = malloc(sizeof(STQ));
L
Liu Jicong 已提交
56
  if (pTq == NULL) {
L
Liu Jicong 已提交
57
    terrno = TSDB_CODE_TQ_OUT_OF_MEMORY;
L
Liu Jicong 已提交
58 59
    return NULL;
  }
H
Hongze Cheng 已提交
60
  pTq->path = strdup(path);
L
Liu Jicong 已提交
61
  pTq->tqConfig = tqConfig;
L
Liu Jicong 已提交
62 63
  pTq->pWal = pWal;
  pTq->pMeta = pMeta;
L
Liu Jicong 已提交
64
#if 0
L
Liu Jicong 已提交
65
  pTq->tqMemRef.pAllocatorFactory = allocFac;
L
Liu Jicong 已提交
66 67
  pTq->tqMemRef.pAllocator = allocFac->create(allocFac);
  if (pTq->tqMemRef.pAllocator == NULL) {
L
Liu Jicong 已提交
68
    // TODO: error code of buffer pool
L
Liu Jicong 已提交
69
  }
L
Liu Jicong 已提交
70
#endif
L
Liu Jicong 已提交
71
  pTq->tqMeta = tqStoreOpen(path, (FTqSerialize)tqSerializeConsumer, (FTqDeserialize)tqDeserializeConsumer, free, 0);
L
Liu Jicong 已提交
72
  if (pTq->tqMeta == NULL) {
L
Liu Jicong 已提交
73
    free(pTq);
L
Liu Jicong 已提交
74 75 76
#if 0
    allocFac->destroy(allocFac, pTq->tqMemRef.pAllocator);
#endif
L
Liu Jicong 已提交
77 78
    return NULL;
  }
L
Liu Jicong 已提交
79

L
Liu Jicong 已提交
80 81
  return pTq;
}
L
Liu Jicong 已提交
82

L
Liu Jicong 已提交
83
void tqClose(STQ* pTq) {
H
Hongze Cheng 已提交
84
  if (pTq) {
H
Hongze Cheng 已提交
85
    tfree(pTq->path);
H
Hongze Cheng 已提交
86 87
    free(pTq);
  }
L
Liu Jicong 已提交
88 89
  // TODO
}
L
Liu Jicong 已提交
90

L
Liu Jicong 已提交
91 92 93 94
static int tqProtoCheck(STqMsgHead* pMsg) {
  // TODO
  return pMsg->protoVer == 0;
}
L
Liu Jicong 已提交
95

L
Liu Jicong 已提交
96
static int tqAckOneTopic(STqTopic* pTopic, STqOneAck* pAck, STqQueryMsg** ppQuery) {
L
Liu Jicong 已提交
97
  // clean old item and move forward
L
Liu Jicong 已提交
98
  int32_t consumeOffset = pAck->consumeOffset;
L
Liu Jicong 已提交
99
  int     idx = consumeOffset % TQ_BUFFER_SIZE;
L
Liu Jicong 已提交
100 101
  ASSERT(pTopic->buffer[idx].content && pTopic->buffer[idx].executor);
  tfree(pTopic->buffer[idx].content);
L
Liu Jicong 已提交
102 103 104
  if (1 /* TODO: need to launch new query */) {
    STqQueryMsg* pNewQuery = malloc(sizeof(STqQueryMsg));
    if (pNewQuery == NULL) {
L
Liu Jicong 已提交
105
      terrno = TSDB_CODE_TQ_OUT_OF_MEMORY;
L
Liu Jicong 已提交
106 107
      return -1;
    }
L
Liu Jicong 已提交
108 109
    // TODO: lock executor
    // TODO: read from wal and assign to src
L
Liu Jicong 已提交
110 111 112 113 114
    /*pNewQuery->exec->executor = pTopic->buffer[idx].executor;*/
    /*pNewQuery->exec->src = 0;*/
    /*pNewQuery->exec->dest = &pTopic->buffer[idx];*/
    /*pNewQuery->next = *ppQuery;*/
    /**ppQuery = pNewQuery;*/
L
Liu Jicong 已提交
115
  }
L
Liu Jicong 已提交
116 117 118
  return 0;
}

L
Liu Jicong 已提交
119
static int tqAck(STqGroup* pGroup, STqAcks* pAcks) {
L
Liu Jicong 已提交
120
  int32_t    ackNum = pAcks->ackNum;
L
Liu Jicong 已提交
121
  STqOneAck* acks = pAcks->acks;
L
Liu Jicong 已提交
122
  // double ptr for acks and list
L
Liu Jicong 已提交
123 124 125 126
  int          i = 0;
  STqList*     node = pGroup->head;
  int          ackCnt = 0;
  STqQueryMsg* pQuery = NULL;
L
Liu Jicong 已提交
127
  while (i < ackNum && node->next) {
L
Liu Jicong 已提交
128
    if (acks[i].topicId == node->next->topic.topicId) {
L
Liu Jicong 已提交
129
      ackCnt++;
L
Liu Jicong 已提交
130 131
      tqAckOneTopic(&node->next->topic, &acks[i], &pQuery);
    } else if (acks[i].topicId < node->next->topic.topicId) {
L
Liu Jicong 已提交
132 133 134
      i++;
    } else {
      node = node->next;
L
Liu Jicong 已提交
135 136
    }
  }
L
Liu Jicong 已提交
137 138
  if (pQuery) {
    // post message
L
Liu Jicong 已提交
139 140 141
  }
  return ackCnt;
}
L
Liu Jicong 已提交
142

L
Liu Jicong 已提交
143
static int tqCommitGroup(STqGroup* pGroup) {
L
Liu Jicong 已提交
144
  // persist modification into disk
L
Liu Jicong 已提交
145 146 147
  return 0;
}

L
Liu Jicong 已提交
148
int tqCreateGroup(STQ* pTq, int64_t topicId, int64_t cgId, int64_t cId, STqGroup** ppGroup) {
L
Liu Jicong 已提交
149
  // create in disk
L
Liu Jicong 已提交
150 151
  STqGroup* pGroup = (STqGroup*)malloc(sizeof(STqGroup));
  if (pGroup == NULL) {
L
Liu Jicong 已提交
152
    // TODO
L
Liu Jicong 已提交
153 154
    return -1;
  }
L
Liu Jicong 已提交
155 156
  *ppGroup = pGroup;
  memset(pGroup, 0, sizeof(STqGroup));
L
Liu Jicong 已提交
157

L
Liu Jicong 已提交
158
  pGroup->topicList = tdListNew(sizeof(STqTopic));
L
Liu Jicong 已提交
159
  if (pGroup->topicList == NULL) {
L
Liu Jicong 已提交
160 161 162 163 164
    free(pGroup);
    return -1;
  }
  *ppGroup = pGroup;

L
Liu Jicong 已提交
165 166 167
  return 0;
}

L
Liu Jicong 已提交
168 169 170 171 172
STqGroup* tqOpenGroup(STQ* pTq, int64_t topicId, int64_t cgId, int64_t cId) {
  STqGroup* pGroup = tqHandleGet(pTq->tqMeta, cId);
  if (pGroup == NULL) {
    int code = tqCreateGroup(pTq, topicId, cgId, cId, &pGroup);
    if (code < 0) {
L
Liu Jicong 已提交
173
      // TODO
L
Liu Jicong 已提交
174 175
      return NULL;
    }
L
Liu Jicong 已提交
176
    tqHandleMovePut(pTq->tqMeta, cId, pGroup);
L
Liu Jicong 已提交
177
  }
L
Liu Jicong 已提交
178
  ASSERT(pGroup);
L
Liu Jicong 已提交
179

L
Liu Jicong 已提交
180
  return pGroup;
L
Liu Jicong 已提交
181
}
L
Liu Jicong 已提交
182

L
Liu Jicong 已提交
183 184 185 186
int tqCloseGroup(STQ* pTq, int64_t topicId, int64_t cgId, int64_t cId) {
  // TODO
  return 0;
}
L
Liu Jicong 已提交
187

L
Liu Jicong 已提交
188
int tqDropGroup(STQ* pTq, int64_t topicId, int64_t cgId, int64_t cId) {
L
Liu Jicong 已提交
189
  // delete from disk
L
Liu Jicong 已提交
190 191 192
  return 0;
}

L
Liu Jicong 已提交
193 194 195
static int tqFetch(STqGroup* pGroup, STqConsumeRsp** pRsp) {
  STqList* pHead = pGroup->head;
  STqList* pNode = pHead;
L
Liu Jicong 已提交
196
  int      totSize = 0;
L
Liu Jicong 已提交
197
  int      numOfMsgs = 0;
L
Liu Jicong 已提交
198
  // TODO: make it a macro
L
Liu Jicong 已提交
199
  int sizeLimit = 4 * 1024;
L
Liu Jicong 已提交
200 201 202 203

  void* ptr = realloc(*pRsp, sizeof(STqConsumeRsp) + sizeLimit);
  if (ptr == NULL) {
    terrno = TSDB_CODE_TQ_OUT_OF_MEMORY;
L
Liu Jicong 已提交
204 205
    return -1;
  }
L
Liu Jicong 已提交
206 207 208
  *pRsp = ptr;
  STqMsgContent* buffer = (*pRsp)->msgs;

L
Liu Jicong 已提交
209 210
  // iterate the list to get msgs of all topics
  // until all topic iterated or msgs over sizeLimit
L
Liu Jicong 已提交
211 212 213 214 215 216
  while (pNode->next) {
    pNode = pNode->next;
    STqTopic* pTopic = &pNode->topic;
    int       idx = pTopic->nextConsumeOffset % TQ_BUFFER_SIZE;
    if (pTopic->buffer[idx].content != NULL && pTopic->buffer[idx].offset == pTopic->nextConsumeOffset) {
      totSize += pTopic->buffer[idx].size;
L
Liu Jicong 已提交
217
      if (totSize > sizeLimit) {
L
Liu Jicong 已提交
218
        void* ptr = realloc(*pRsp, sizeof(STqConsumeRsp) + totSize);
L
Liu Jicong 已提交
219
        if (ptr == NULL) {
L
Liu Jicong 已提交
220 221
          totSize -= pTopic->buffer[idx].size;
          terrno = TSDB_CODE_TQ_OUT_OF_MEMORY;
L
Liu Jicong 已提交
222
          // return msgs already copied
L
Liu Jicong 已提交
223 224
          break;
        }
L
Liu Jicong 已提交
225 226
        *pRsp = ptr;
        break;
L
Liu Jicong 已提交
227
      }
L
Liu Jicong 已提交
228
      *((int64_t*)buffer) = pTopic->topicId;
L
Liu Jicong 已提交
229
      buffer = POINTER_SHIFT(buffer, sizeof(int64_t));
L
Liu Jicong 已提交
230
      *((int64_t*)buffer) = pTopic->buffer[idx].size;
L
Liu Jicong 已提交
231
      buffer = POINTER_SHIFT(buffer, sizeof(int64_t));
L
Liu Jicong 已提交
232 233 234
      memcpy(buffer, pTopic->buffer[idx].content, pTopic->buffer[idx].size);
      buffer = POINTER_SHIFT(buffer, pTopic->buffer[idx].size);
      numOfMsgs++;
L
Liu Jicong 已提交
235
      if (totSize > sizeLimit) {
L
Liu Jicong 已提交
236 237 238 239
        break;
      }
    }
  }
L
Liu Jicong 已提交
240 241
  (*pRsp)->bodySize = totSize;
  return numOfMsgs;
L
Liu Jicong 已提交
242 243
}

L
Liu Jicong 已提交
244
STqGroup* tqGetGroup(STQ* pTq, int64_t clientId) { return tqHandleGet(pTq->tqMeta, clientId); }
L
Liu Jicong 已提交
245

L
Liu Jicong 已提交
246 247 248 249 250 251 252 253 254 255 256
int tqSendLaunchQuery(STqMsgItem* bufItem, int64_t offset) {
  if (tqQueryExecuting(bufItem->status)) {
    return 0;
  }
  bufItem->status = 1;
  // load data from wal or buffer pool
  // put into exec
  // send exec into non blocking queue
  // when query finished, put into buffer pool
  return 0;
}
L
Liu Jicong 已提交
257

L
Liu Jicong 已提交
258
/*int tqMoveOffsetToNext(TqGroupHandle* gHandle) {*/
L
Liu Jicong 已提交
259
/*return 0;*/
L
Liu Jicong 已提交
260 261
/*}*/

L
Liu Jicong 已提交
262 263 264
int tqPushMsg(STQ* pTq, void* p, int64_t version) {
  // add reference
  // judge and launch new query
L
Liu Jicong 已提交
265 266 267
  return 0;
}

L
Liu Jicong 已提交
268
int tqCommit(STQ* pTq) {
L
Liu Jicong 已提交
269
  // do nothing
L
Liu Jicong 已提交
270 271 272
  return 0;
}

L
Liu Jicong 已提交
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
int tqBufferSetOffset(STqTopic* pTopic, int64_t offset) {
  int code;
  memset(pTopic->buffer, 0, sizeof(pTopic->buffer));
  // launch query
  for (int i = offset; i < offset + TQ_BUFFER_SIZE; i++) {
    int pos = i % TQ_BUFFER_SIZE;
    code = tqSendLaunchQuery(&pTopic->buffer[pos], offset);
    if (code < 0) {
      // TODO: error handling
    }
  }
  // set offset
  pTopic->nextConsumeOffset = offset;
  pTopic->floatingCursor = offset;
  return 0;
}

STqTopic* tqFindTopic(STqGroup* pGroup, int64_t topicId) {
  // TODO
  return NULL;
}

int tqSetCursor(STQ* pTq, STqSetCurReq* pMsg) {
  int       code;
  int64_t   clientId = pMsg->head.clientId;
  int64_t   topicId = pMsg->topicId;
  int64_t   offset = pMsg->offset;
  STqGroup* gHandle = tqGetGroup(pTq, clientId);
  if (gHandle == NULL) {
    // client not connect
    return -1;
  }
  STqTopic* topicHandle = tqFindTopic(gHandle, topicId);
  if (topicHandle == NULL) {
    return -1;
  }
  if (pMsg->offset == topicHandle->nextConsumeOffset) {
    return 0;
  }
  // TODO: check log last version

  code = tqBufferSetOffset(topicHandle, offset);
  if (code < 0) {
    // set error code
    return -1;
  }

L
Liu Jicong 已提交
320 321 322
  return 0;
}

L
Liu Jicong 已提交
323 324
// temporary
int tqProcessCMsg(STQ* pTq, STqConsumeReq* pMsg, STqRspHandle* pRsp) {
L
Liu Jicong 已提交
325 326 327 328 329 330
  int64_t   clientId = pMsg->head.clientId;
  STqGroup* pGroup = tqGetGroup(pTq, clientId);
  if (pGroup == NULL) {
    terrno = TSDB_CODE_TQ_GROUP_NOT_SET;
    return -1;
  }
L
Liu Jicong 已提交
331 332 333 334 335 336 337
  pGroup->rspHandle.handle = pRsp->handle;
  pGroup->rspHandle.ahandle = pRsp->ahandle;

  return 0;
}

int tqConsume(STQ* pTq, SRpcMsg* pReq, SRpcMsg** pRsp) {
L
Liu Jicong 已提交
338 339 340
  STqConsumeReq* pMsg = pReq->pCont;
  int64_t        clientId = pMsg->head.clientId;
  STqGroup*      pGroup = tqGetGroup(pTq, clientId);
L
Liu Jicong 已提交
341 342 343 344 345 346 347 348 349 350 351
  if (pGroup == NULL) {
    terrno = TSDB_CODE_TQ_GROUP_NOT_SET;
    return -1;
  }

  SList* topicList = pGroup->topicList;

  int totSize = 0;
  int numOfMsgs = 0;
  int sizeLimit = 4096;

L
Liu Jicong 已提交
352 353
  STqConsumeRsp* pCsmRsp = (*pRsp)->pCont;
  void*          ptr = realloc((*pRsp)->pCont, sizeof(STqConsumeRsp) + sizeLimit);
L
Liu Jicong 已提交
354 355 356 357 358 359 360 361 362 363
  if (ptr == NULL) {
    terrno = TSDB_CODE_TQ_OUT_OF_MEMORY;
    return -1;
  }
  (*pRsp)->pCont = ptr;

  SListIter iter;
  tdListInitIter(topicList, &iter, TD_LIST_FORWARD);

  STqMsgContent* buffer = NULL;
L
Liu Jicong 已提交
364
  SArray*        pArray = taosArrayInit(0, sizeof(void*));
L
Liu Jicong 已提交
365

L
Liu Jicong 已提交
366 367 368 369
  SListNode* pn;
  while ((pn = tdListNext(&iter)) != NULL) {
    STqTopic*   pTopic = *(STqTopic**)pn->data;
    int         idx = pTopic->floatingCursor % TQ_BUFFER_SIZE;
L
Liu Jicong 已提交
370 371
    STqMsgItem* pItem = &pTopic->buffer[idx];
    if (pItem->content != NULL && pItem->offset == pTopic->floatingCursor) {
L
Liu Jicong 已提交
372 373
      if (pItem->status == TQ_ITEM_READY) {
        // if has data
L
Liu Jicong 已提交
374 375 376 377 378 379 380 381 382 383 384 385
        totSize += pTopic->buffer[idx].size;
        if (totSize > sizeLimit) {
          void* ptr = realloc((*pRsp)->pCont, sizeof(STqConsumeRsp) + totSize);
          if (ptr == NULL) {
            totSize -= pTopic->buffer[idx].size;
            terrno = TSDB_CODE_TQ_OUT_OF_MEMORY;
            // return msgs already copied
            break;
          }
          (*pRsp)->pCont = ptr;
          break;
        }
L
Liu Jicong 已提交
386
        *((int64_t*)buffer) = htobe64(pTopic->topicId);
L
Liu Jicong 已提交
387
        buffer = POINTER_SHIFT(buffer, sizeof(int64_t));
L
Liu Jicong 已提交
388
        *((int64_t*)buffer) = htobe64(pTopic->buffer[idx].size);
L
Liu Jicong 已提交
389 390 391 392 393 394 395
        buffer = POINTER_SHIFT(buffer, sizeof(int64_t));
        memcpy(buffer, pTopic->buffer[idx].content, pTopic->buffer[idx].size);
        buffer = POINTER_SHIFT(buffer, pTopic->buffer[idx].size);
        numOfMsgs++;
        if (totSize > sizeLimit) {
          break;
        }
L
Liu Jicong 已提交
396 397
      } else if (pItem->status == TQ_ITEM_PROCESS) {
        // if not have data but in process
L
Liu Jicong 已提交
398

L
Liu Jicong 已提交
399 400
      } else if (pItem->status == TQ_ITEM_EMPTY) {
        // if not have data and not in process
L
Liu Jicong 已提交
401
        int32_t old = atomic_val_compare_exchange_32(&pItem->status, TQ_ITEM_EMPTY, TQ_ITEM_PROCESS);
L
Liu Jicong 已提交
402
        if (old != TQ_ITEM_EMPTY) {
L
Liu Jicong 已提交
403 404 405 406 407 408 409 410 411 412
          continue;
        }
        pItem->offset = pTopic->floatingCursor;
        taosArrayPush(pArray, &pItem);
      } else {
        ASSERT(0);
      }
    }
  }

L
Liu Jicong 已提交
413 414 415 416 417 418 419 420 421 422 423
  if (numOfMsgs > 0) {
    // set code and other msg
    rpcSendResponse(*pRsp);
  } else {
    // most recent data has been fetched

    // enable timer for blocking wait
    // once new data written when waiting, launch query and rsp
  }

  // fetched a num of msgs, rpc response
L
Liu Jicong 已提交
424
  for (int i = 0; i < pArray->size; i++) {
L
Liu Jicong 已提交
425 426
    STqMsgItem* pItem = taosArrayGet(pArray, i);

L
Liu Jicong 已提交
427
    // read from wal
L
Liu Jicong 已提交
428 429
    void* raw = NULL;
    /*int code = pTq->tqLogReader->logRead(, &raw, pItem->offset);*/
L
Liu Jicong 已提交
430 431
    /*int code = pTq->tqLogHandle->logRead(pItem->pTopic->logReader, &raw, pItem->offset);*/
    /*if (code < 0) {*/
L
Liu Jicong 已提交
432
    // TODO: error
L
Liu Jicong 已提交
433
    /*}*/
L
Liu Jicong 已提交
434 435
    // get msgType
    // if submitblk
L
Liu Jicong 已提交
436 437 438
    pItem->executor->assign(pItem->executor->runtimeEnv, raw);
    SSDataBlock* content = pItem->executor->exec(pItem->executor->runtimeEnv);
    pItem->content = content;
L
Liu Jicong 已提交
439
    // if other type, send just put into buffer
L
Liu Jicong 已提交
440
    /*pItem->content = raw;*/
L
Liu Jicong 已提交
441 442 443 444

    int32_t old = atomic_val_compare_exchange_32(&pItem->status, TQ_ITEM_PROCESS, TQ_ITEM_READY);
    ASSERT(old == TQ_ITEM_PROCESS);
  }
L
Liu Jicong 已提交
445
  taosArrayDestroy(pArray);
L
Liu Jicong 已提交
446 447 448 449 450

  return 0;
}

#if 0
L
Liu Jicong 已提交
451
int tqConsume(STQ* pTq, STqConsumeReq* pMsg) {
L
Liu Jicong 已提交
452
  if (!tqProtoCheck((STqMsgHead*)pMsg)) {
L
Liu Jicong 已提交
453
    // proto version invalid
L
Liu Jicong 已提交
454 455
    return -1;
  }
L
Liu Jicong 已提交
456 457 458
  int64_t   clientId = pMsg->head.clientId;
  STqGroup* pGroup = tqGetGroup(pTq, clientId);
  if (pGroup == NULL) {
L
Liu Jicong 已提交
459
    // client not connect
L
Liu Jicong 已提交
460 461
    return -1;
  }
L
Liu Jicong 已提交
462
  if (pMsg->acks.ackNum != 0) {
L
Liu Jicong 已提交
463
    if (tqAck(pGroup, &pMsg->acks) != 0) {
L
Liu Jicong 已提交
464
      // ack not success
L
Liu Jicong 已提交
465 466 467 468
      return -1;
    }
  }

L
Liu Jicong 已提交
469
  STqConsumeRsp* pRsp = (STqConsumeRsp*)pMsg;
L
Liu Jicong 已提交
470

L
Liu Jicong 已提交
471
  if (tqFetch(pGroup, (void**)&pRsp->msgs) <= 0) {
L
Liu Jicong 已提交
472
    // fetch error
L
Liu Jicong 已提交
473 474 475
    return -1;
  }

L
Liu Jicong 已提交
476
  // judge and launch new query
L
Liu Jicong 已提交
477 478 479 480
  /*if (tqSendLaunchQuery(gHandle)) {*/
  // launch query error
  /*return -1;*/
  /*}*/
L
Liu Jicong 已提交
481 482
  return 0;
}
L
Liu Jicong 已提交
483
#endif
L
Liu Jicong 已提交
484

L
Liu Jicong 已提交
485 486
int tqSerializeConsumer(const STqConsumerHandle* pConsumer, STqSerializedHead** ppHead) {
  int32_t num = taosArrayGetSize(pConsumer->topics);
L
Liu Jicong 已提交
487 488
  int32_t sz = sizeof(STqSerializedHead) + sizeof(int64_t) * 2 + TSDB_TOPIC_FNAME_LEN +
               num * (sizeof(int64_t) + TSDB_TOPIC_FNAME_LEN);
L
Liu Jicong 已提交
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
  if (sz > (*ppHead)->ssize) {
    void* tmpPtr = realloc(*ppHead, sz);
    if (tmpPtr == NULL) {
      free(*ppHead);
      return -1;
    }
    *ppHead = tmpPtr;
    (*ppHead)->ssize = sz;
  }

  void* ptr = (*ppHead)->content;
  *(int64_t*)ptr = pConsumer->consumerId;
  ptr = POINTER_SHIFT(ptr, sizeof(int64_t));
  *(int64_t*)ptr = pConsumer->epoch;
  ptr = POINTER_SHIFT(ptr, sizeof(int64_t));
  memcpy(ptr, pConsumer->topics, TSDB_TOPIC_FNAME_LEN);
  ptr = POINTER_SHIFT(ptr, TSDB_TOPIC_FNAME_LEN);
  *(int32_t*)ptr = num;
  ptr = POINTER_SHIFT(ptr, sizeof(int32_t));
  for (int32_t i = 0; i < num; i++) {
    STqTopicHandle* pTopic = taosArrayGet(pConsumer->topics, i);
    memcpy(ptr, pTopic->topicName, TSDB_TOPIC_FNAME_LEN);
    ptr = POINTER_SHIFT(ptr, TSDB_TOPIC_FNAME_LEN);
    *(int64_t*)ptr = pTopic->committedOffset;
    POINTER_SHIFT(ptr, sizeof(int64_t));
  }
L
Liu Jicong 已提交
515

L
Liu Jicong 已提交
516 517 518 519 520
  return 0;
}

const void* tqDeserializeConsumer(const STqSerializedHead* pHead, STqConsumerHandle** ppConsumer) {
  STqConsumerHandle* pConsumer = *ppConsumer;
L
Liu Jicong 已提交
521
  const void*        ptr = pHead->content;
L
Liu Jicong 已提交
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
  pConsumer->consumerId = *(int64_t*)ptr;
  ptr = POINTER_SHIFT(ptr, sizeof(int64_t));
  pConsumer->epoch = *(int64_t*)ptr;
  ptr = POINTER_SHIFT(ptr, sizeof(int64_t));
  memcpy(pConsumer->cgroup, ptr, TSDB_TOPIC_FNAME_LEN);
  ptr = POINTER_SHIFT(ptr, TSDB_TOPIC_FNAME_LEN);
  int32_t sz = *(int32_t*)ptr;
  ptr = POINTER_SHIFT(ptr, sizeof(int32_t));
  pConsumer->topics = taosArrayInit(sz, sizeof(STqTopicHandle));
  for (int32_t i = 0; i < sz; i++) {
    /*STqTopicHandle* topicHandle = */
    /*taosArrayPush(pConsumer->topics, );*/
  }
  return NULL;
}

#if 0
L
Liu Jicong 已提交
539
int tqSerializeGroup(const STqGroup* pGroup, STqSerializedHead** ppHead) {
L
Liu Jicong 已提交
540
  // calculate size
L
Liu Jicong 已提交
541
  int sz = tqGroupSSize(pGroup) + sizeof(STqSerializedHead);
L
Liu Jicong 已提交
542
  if (sz > (*ppHead)->ssize) {
L
Liu Jicong 已提交
543
    void* tmpPtr = realloc(*ppHead, sz);
L
Liu Jicong 已提交
544
    if (tmpPtr == NULL) {
L
Liu Jicong 已提交
545
      free(*ppHead);
L
Liu Jicong 已提交
546
      // TODO: memory err
L
Liu Jicong 已提交
547 548 549 550
      return -1;
    }
    *ppHead = tmpPtr;
    (*ppHead)->ssize = sz;
L
Liu Jicong 已提交
551
  }
L
Liu Jicong 已提交
552
  void* ptr = (*ppHead)->content;
L
Liu Jicong 已提交
553
  // do serialization
L
Liu Jicong 已提交
554
  *(int64_t*)ptr = pGroup->clientId;
L
Liu Jicong 已提交
555
  ptr = POINTER_SHIFT(ptr, sizeof(int64_t));
L
Liu Jicong 已提交
556
  *(int64_t*)ptr = pGroup->cgId;
L
Liu Jicong 已提交
557
  ptr = POINTER_SHIFT(ptr, sizeof(int64_t));
L
Liu Jicong 已提交
558
  *(int32_t*)ptr = pGroup->topicNum;
559
  ptr = POINTER_SHIFT(ptr, sizeof(int32_t));
L
Liu Jicong 已提交
560 561
  if (pGroup->topicNum > 0) {
    tqSerializeListHandle(pGroup->head, ptr);
L
Liu Jicong 已提交
562 563 564 565
  }
  return 0;
}

L
Liu Jicong 已提交
566 567
void* tqSerializeListHandle(STqList* listHandle, void* ptr) {
  STqList* node = listHandle;
568
  ASSERT(node != NULL);
L
Liu Jicong 已提交
569
  while (node) {
L
Liu Jicong 已提交
570
    ptr = tqSerializeTopic(&node->topic, ptr);
L
Liu Jicong 已提交
571 572
    node = node->next;
  }
573
  return ptr;
L
Liu Jicong 已提交
574
}
575

L
Liu Jicong 已提交
576 577
void* tqSerializeTopic(STqTopic* pTopic, void* ptr) {
  *(int64_t*)ptr = pTopic->nextConsumeOffset;
L
Liu Jicong 已提交
578
  ptr = POINTER_SHIFT(ptr, sizeof(int64_t));
L
Liu Jicong 已提交
579
  *(int64_t*)ptr = pTopic->topicId;
L
Liu Jicong 已提交
580
  ptr = POINTER_SHIFT(ptr, sizeof(int64_t));
L
Liu Jicong 已提交
581 582 583 584
  /**(int32_t*)ptr = pTopic->head;*/
  /*ptr = POINTER_SHIFT(ptr, sizeof(int32_t));*/
  /**(int32_t*)ptr = pTopic->tail;*/
  /*ptr = POINTER_SHIFT(ptr, sizeof(int32_t));*/
L
Liu Jicong 已提交
585
  for (int i = 0; i < TQ_BUFFER_SIZE; i++) {
L
Liu Jicong 已提交
586
    ptr = tqSerializeItem(&pTopic->buffer[i], ptr);
L
Liu Jicong 已提交
587
  }
588
  return ptr;
L
Liu Jicong 已提交
589 590
}

L
Liu Jicong 已提交
591
void* tqSerializeItem(STqMsgItem* bufItem, void* ptr) {
L
Liu Jicong 已提交
592 593
  // TODO: do we need serialize this?
  // mainly for executor
594
  return ptr;
L
Liu Jicong 已提交
595 596
}

L
Liu Jicong 已提交
597 598 599
const void* tqDeserializeGroup(const STqSerializedHead* pHead, STqGroup** ppGroup) {
  STqGroup*   gHandle = *ppGroup;
  const void* ptr = pHead->content;
L
Liu Jicong 已提交
600
  gHandle->clientId = *(int64_t*)ptr;
601 602 603 604 605 606 607
  ptr = POINTER_SHIFT(ptr, sizeof(int64_t));
  gHandle->cgId = *(int64_t*)ptr;
  ptr = POINTER_SHIFT(ptr, sizeof(int64_t));
  gHandle->ahandle = NULL;
  gHandle->topicNum = *(int32_t*)ptr;
  ptr = POINTER_SHIFT(ptr, sizeof(int32_t));
  gHandle->head = NULL;
L
Liu Jicong 已提交
608
  STqList* node = gHandle->head;
L
Liu Jicong 已提交
609 610
  for (int i = 0; i < gHandle->topicNum; i++) {
    if (gHandle->head == NULL) {
L
Liu Jicong 已提交
611
      if ((node = malloc(sizeof(STqList))) == NULL) {
L
Liu Jicong 已提交
612
        // TODO: error
613 614
        return NULL;
      }
L
Liu Jicong 已提交
615
      node->next = NULL;
L
Liu Jicong 已提交
616
      ptr = tqDeserializeTopic(ptr, &node->topic);
617 618
      gHandle->head = node;
    } else {
L
Liu Jicong 已提交
619
      node->next = malloc(sizeof(STqList));
L
Liu Jicong 已提交
620 621
      if (node->next == NULL) {
        // TODO: error
622 623 624
        return NULL;
      }
      node->next->next = NULL;
L
Liu Jicong 已提交
625
      ptr = tqDeserializeTopic(ptr, &node->next->topic);
626 627 628 629
      node = node->next;
    }
  }
  return ptr;
L
Liu Jicong 已提交
630
}
631

L
Liu Jicong 已提交
632
const void* tqDeserializeTopic(const void* pBytes, STqTopic* topic) {
633
  const void* ptr = pBytes;
L
Liu Jicong 已提交
634
  topic->nextConsumeOffset = *(int64_t*)ptr;
635
  ptr = POINTER_SHIFT(ptr, sizeof(int64_t));
L
Liu Jicong 已提交
636
  topic->topicId = *(int64_t*)ptr;
637
  ptr = POINTER_SHIFT(ptr, sizeof(int64_t));
L
Liu Jicong 已提交
638 639 640 641
  /*topic->head = *(int32_t*)ptr;*/
  /*ptr = POINTER_SHIFT(ptr, sizeof(int32_t));*/
  /*topic->tail = *(int32_t*)ptr;*/
  /*ptr = POINTER_SHIFT(ptr, sizeof(int32_t));*/
L
Liu Jicong 已提交
642
  for (int i = 0; i < TQ_BUFFER_SIZE; i++) {
L
Liu Jicong 已提交
643
    ptr = tqDeserializeItem(ptr, &topic->buffer[i]);
644 645
  }
  return ptr;
L
Liu Jicong 已提交
646 647
}

L
Liu Jicong 已提交
648
const void* tqDeserializeItem(const void* pBytes, STqMsgItem* bufItem) { return pBytes; }
L
Liu Jicong 已提交
649

L
Liu Jicong 已提交
650
// TODO: make this a macro
L
Liu Jicong 已提交
651
int tqGroupSSize(const STqGroup* gHandle) {
L
Liu Jicong 已提交
652 653
  return sizeof(int64_t) * 2  // cId + cgId
         + sizeof(int32_t)    // topicNum
L
Liu Jicong 已提交
654
         + gHandle->topicNum * tqTopicSSize();
L
Liu Jicong 已提交
655
}
656

L
Liu Jicong 已提交
657
// TODO: make this a macro
L
Liu Jicong 已提交
658
int tqTopicSSize() {
L
Liu Jicong 已提交
659 660
  return sizeof(int64_t) * 2    // nextConsumeOffset + topicId
         + sizeof(int32_t) * 2  // head + tail
L
Liu Jicong 已提交
661
         + TQ_BUFFER_SIZE * tqItemSSize();
L
Liu Jicong 已提交
662
}
663

L
Liu Jicong 已提交
664
int tqItemSSize() {
L
Liu Jicong 已提交
665 666
  // TODO: do this need serialization?
  // mainly for executor
L
Liu Jicong 已提交
667 668
  return 0;
}
L
Liu Jicong 已提交
669
#endif
670

S
Shengliang 已提交
671
int32_t tqProcessConsumeReq(STQ* pTq, SRpcMsg* pMsg) {
L
Liu Jicong 已提交
672 673 674 675 676 677 678
  SMqConsumeReq* pReq = pMsg->pCont;
  SRpcMsg        rpcMsg;
  int64_t        reqId = pReq->reqId;
  int64_t        consumerId = pReq->consumerId;
  int64_t        reqOffset = pReq->offset;
  int64_t        fetchOffset = reqOffset;
  int64_t        blockingTime = pReq->blockingTime;
L
Liu Jicong 已提交
679

L
Liu Jicong 已提交
680 681
  int           rspLen = 0;
  SMqConsumeRsp rsp = {.consumerId = consumerId, .numOfTopics = 1, .pBlockData = NULL};
L
Liu Jicong 已提交
682

L
Liu Jicong 已提交
683
  STqConsumerHandle* pConsumer = tqHandleGet(pTq->tqMeta, consumerId);
L
Liu Jicong 已提交
684
  ASSERT(pConsumer);
L
Liu Jicong 已提交
685
  int sz = taosArrayGetSize(pConsumer->topics);
L
Liu Jicong 已提交
686

L
Liu Jicong 已提交
687 688
  for (int i = 0; i < sz; i++) {
    STqTopicHandle* pTopic = taosArrayGet(pConsumer->topics, i);
L
Liu Jicong 已提交
689
    // TODO: support multiple topic in one req
L
Liu Jicong 已提交
690 691 692
    if (strcmp(pTopic->topicName, pReq->topic) != 0) {
      continue;
    }
L
Liu Jicong 已提交
693

L
Liu Jicong 已提交
694 695 696 697 698
    if (fetchOffset == -1) {
      fetchOffset = pTopic->committedOffset + 1;
    }
    int8_t    pos;
    int8_t    skip = 0;
L
Liu Jicong 已提交
699
    SWalHead* pHead;
L
Liu Jicong 已提交
700
    while (1) {
L
Liu Jicong 已提交
701 702 703 704 705 706 707 708 709 710 711 712
      pos = fetchOffset % TQ_BUFFER_SIZE;
      skip = atomic_val_compare_exchange_8(&pTopic->buffer.output[pos].status, 0, 1);
      if (skip == 1) {
        // do nothing
        break;
      }
      if (walReadWithHandle(pTopic->pReadhandle, fetchOffset) < 0) {
        // check err
        atomic_store_8(&pTopic->buffer.output[pos].status, 0);
        skip = 1;
        break;
      }
L
Liu Jicong 已提交
713
      // read until find TDMT_VND_SUBMIT
L
Liu Jicong 已提交
714 715 716 717
      pHead = pTopic->pReadhandle->pHead;
      if (pHead->head.msgType == TDMT_VND_SUBMIT) {
        break;
      }
L
Liu Jicong 已提交
718
      if (walReadWithHandle(pTopic->pReadhandle, fetchOffset) < 0) {
L
Liu Jicong 已提交
719 720 721
        atomic_store_8(&pTopic->buffer.output[pos].status, 0);
        skip = 1;
        break;
L
Liu Jicong 已提交
722
      }
L
Liu Jicong 已提交
723 724
      atomic_store_8(&pTopic->buffer.output[pos].status, 0);
      fetchOffset++;
L
Liu Jicong 已提交
725
    }
L
Liu Jicong 已提交
726
    if (skip == 1) continue;
L
Liu Jicong 已提交
727
    SSubmitMsg* pCont = (SSubmitMsg*)&pHead->head.body;
L
Liu Jicong 已提交
728
    qTaskInfo_t task = pTopic->buffer.output[pos].task;
L
Liu Jicong 已提交
729

L
Liu Jicong 已提交
730
    qSetStreamInput(task, pCont);
L
Liu Jicong 已提交
731

L
Liu Jicong 已提交
732
    // SArray<SSDataBlock>
L
Liu Jicong 已提交
733 734 735 736 737 738 739 740 741 742 743 744 745
    SArray* pRes = taosArrayInit(0, sizeof(SSDataBlock));
    while (1) {
      SSDataBlock* pDataBlock;
      uint64_t     ts;
      if (qExecTask(task, &pDataBlock, &ts) < 0) {
        break;
      }
      if (pDataBlock != NULL) {
        taosArrayPush(pRes, pDataBlock);
      } else {
        break;
      }
    }
L
Liu Jicong 已提交
746 747
    //TODO copy
    rsp.schemas = pTopic->buffer.output[pos].pReadHandle->pSchemaWrapper;
L
Liu Jicong 已提交
748 749 750 751 752 753 754

    atomic_store_8(&pTopic->buffer.output[pos].status, 0);

    if (taosArrayGetSize(pRes) == 0) {
      taosArrayDestroy(pRes);
      fetchOffset++;
      continue;
755
    }
L
Liu Jicong 已提交
756

L
Liu Jicong 已提交
757 758 759
    rsp.pBlockData = pRes;

#if 0
L
Liu Jicong 已提交
760 761
    pTopic->buffer.output[pos].dst = pRes;
    if (pTopic->buffer.firstOffset == -1 || pReq->offset < pTopic->buffer.firstOffset) {
L
Liu Jicong 已提交
762
      pTopic->buffer.firstOffset = pReq->offset;
L
Liu Jicong 已提交
763
    }
L
Liu Jicong 已提交
764
    if (pTopic->buffer.lastOffset == -1 || pReq->offset > pTopic->buffer.lastOffset) {
L
Liu Jicong 已提交
765
      pTopic->buffer.lastOffset = pReq->offset;
L
Liu Jicong 已提交
766
    }
L
Liu Jicong 已提交
767
#endif
L
Liu Jicong 已提交
768
  }
L
Liu Jicong 已提交
769 770 771 772 773 774 775 776 777 778 779 780
  int32_t tlen = tEncodeSMqConsumeRsp(NULL, &rsp);
  void*   buf = rpcMallocCont(tlen);
  if (buf == NULL) {
    pMsg->code = -1;
    return -1;
  }
  void* abuf = buf;
  tEncodeSMqConsumeRsp(&abuf, &rsp);
  pMsg->pCont = buf;
  pMsg->contLen = tlen;
  pMsg->code = 0;
  rpcSendResponse(pMsg);
L
Liu Jicong 已提交
781 782 783
  return 0;
}

L
Liu Jicong 已提交
784 785 786
int32_t tqProcessSetConnReq(STQ* pTq, char* msg) {
  SMqSetCVgReq req;
  tDecodeSMqSetCVgReq(msg, &req);
L
Liu Jicong 已提交
787 788 789 790
  STqConsumerHandle* pConsumer = calloc(sizeof(STqConsumerHandle), 1);
  if (pConsumer == NULL) {
    return -1;
  }
L
Liu Jicong 已提交
791 792
  strcpy(pConsumer->cgroup, req.cgroup);
  pConsumer->topics = taosArrayInit(0, sizeof(STqTopicHandle));
L
Liu Jicong 已提交
793 794
  pConsumer->consumerId = req.newConsumerId;
  pConsumer->epoch = 0;
L
Liu Jicong 已提交
795

L
Liu Jicong 已提交
796 797 798 799 800
  STqTopicHandle* pTopic = calloc(sizeof(STqTopicHandle), 1);
  if (pTopic == NULL) {
    free(pConsumer);
    return -1;
  }
L
Liu Jicong 已提交
801
  strcpy(pTopic->topicName, req.topicName);
L
Liu Jicong 已提交
802 803 804
  pTopic->sql = strdup(req.sql);
  pTopic->logicalPlan = strdup(req.logicalPlan);
  pTopic->physicalPlan = strdup(req.physicalPlan);
L
Liu Jicong 已提交
805 806
  pTopic->committedOffset = -1;
  pTopic->currentOffset = -1;
807

L
Liu Jicong 已提交
808 809
  pTopic->buffer.firstOffset = -1;
  pTopic->buffer.lastOffset = -1;
L
Liu Jicong 已提交
810 811 812
  pTopic->pReadhandle = walOpenReadHandle(pTq->pWal);
  if (pTopic->pReadhandle == NULL) {
  }
L
Liu Jicong 已提交
813 814
  for (int i = 0; i < TQ_BUFFER_SIZE; i++) {
    pTopic->buffer.output[i].status = 0;
L
Liu Jicong 已提交
815
    STqReadHandle* pReadHandle = tqInitSubmitMsgScanner(pTq->pMeta);
L
Liu Jicong 已提交
816
    pTopic->buffer.output[i].pReadHandle = pReadHandle;
L
fix tq  
Liu Jicong 已提交
817
    pTopic->buffer.output[i].task = qCreateStreamExecTaskInfo(req.qmsg, pReadHandle);
L
Liu Jicong 已提交
818
  }
L
Liu Jicong 已提交
819
  taosArrayPush(pConsumer->topics, pTopic);
L
Liu Jicong 已提交
820 821
  tqHandleMovePut(pTq->tqMeta, req.newConsumerId, pConsumer);
  tqHandleCommit(pTq->tqMeta, req.newConsumerId);
L
Liu Jicong 已提交
822
  terrno = TSDB_CODE_SUCCESS;
L
Liu Jicong 已提交
823 824 825
  return 0;
}

L
Liu Jicong 已提交
826
STqReadHandle* tqInitSubmitMsgScanner(SMeta* pMeta) {
827 828 829 830
  STqReadHandle* pReadHandle = malloc(sizeof(STqReadHandle));
  if (pReadHandle == NULL) {
    return NULL;
  }
L
Liu Jicong 已提交
831
  pReadHandle->pVnodeMeta = pMeta;
L
Liu Jicong 已提交
832
  pReadHandle->pMsg = NULL;
833
  pReadHandle->ver = -1;
L
Liu Jicong 已提交
834
  pReadHandle->pColIdList = NULL;
L
Liu Jicong 已提交
835 836 837
  pReadHandle->sver = -1;
  pReadHandle->pSchema = NULL;
  pReadHandle->pSchemaWrapper = NULL;
L
Liu Jicong 已提交
838
  return pReadHandle;
839 840
}

L
Liu Jicong 已提交
841 842
void tqReadHandleSetMsg(STqReadHandle* pReadHandle, SSubmitMsg* pMsg, int64_t ver) {
  pReadHandle->pMsg = pMsg;
L
Liu Jicong 已提交
843 844
  pMsg->length = htonl(pMsg->length);
  pMsg->numOfBlocks = htonl(pMsg->numOfBlocks);
L
Liu Jicong 已提交
845 846 847 848 849
  tInitSubmitMsgIter(pMsg, &pReadHandle->msgIter);
  pReadHandle->ver = ver;
  memset(&pReadHandle->blkIter, 0, sizeof(SSubmitBlkIter));
}

850
bool tqNextDataBlock(STqReadHandle* pHandle) {
L
Liu Jicong 已提交
851 852 853 854 855 856 857
  while (1) {
    if (tGetSubmitMsgNext(&pHandle->msgIter, &pHandle->pBlock) < 0) {
      return false;
    }
    if (pHandle->pBlock == NULL) return false;

    pHandle->pBlock->uid = htobe64(pHandle->pBlock->uid);
L
Liu Jicong 已提交
858
    if (pHandle->tbUid == pHandle->pBlock->uid) {
L
Liu Jicong 已提交
859 860 861 862 863
      pHandle->pBlock->tid = htonl(pHandle->pBlock->tid);
      pHandle->pBlock->sversion = htonl(pHandle->pBlock->sversion);
      pHandle->pBlock->dataLen = htonl(pHandle->pBlock->dataLen);
      pHandle->pBlock->schemaLen = htonl(pHandle->pBlock->schemaLen);
      pHandle->pBlock->numOfRows = htons(pHandle->pBlock->numOfRows);
L
Liu Jicong 已提交
864
      return true;
L
Liu Jicong 已提交
865
    }
866
  }
L
Liu Jicong 已提交
867
  return false;
868 869 870
}

int tqRetrieveDataBlockInfo(STqReadHandle* pHandle, SDataBlockInfo* pBlockInfo) {
L
Liu Jicong 已提交
871 872 873
  /*int32_t         sversion = pHandle->pBlock->sversion;*/
  /*SSchemaWrapper* pSchema = metaGetTableSchema(pHandle->pMeta, pHandle->pBlock->uid, sversion, false);*/
  pBlockInfo->numOfCols = taosArrayGetSize(pHandle->pColIdList);
874 875 876 877
  pBlockInfo->rows = pHandle->pBlock->numOfRows;
  pBlockInfo->uid = pHandle->pBlock->uid;
  return 0;
}
L
Liu Jicong 已提交
878

L
Liu Jicong 已提交
879
SArray* tqRetrieveDataBlock(STqReadHandle* pHandle) {
L
Liu Jicong 已提交
880 881 882 883 884 885 886 887 888 889 890 891 892 893 894
  /*int32_t         sversion = pHandle->pBlock->sversion;*/
  // TODO set to real sversion
  int32_t sversion = 0;
  if (pHandle->sver != sversion) {
    pHandle->pSchema = metaGetTbTSchema(pHandle->pVnodeMeta, pHandle->pBlock->uid, sversion);

    tb_uid_t quid;
    STbCfg*  pTbCfg = metaGetTbInfoByUid(pHandle->pVnodeMeta, pHandle->pBlock->uid);
    if (pTbCfg->type == META_CHILD_TABLE) {
      quid = pTbCfg->ctbCfg.suid;
    } else {
      quid = pHandle->pBlock->uid;
    }
    pHandle->pSchemaWrapper = metaGetTableSchema(pHandle->pVnodeMeta, quid, sversion, true);
    pHandle->sver = sversion;
L
Liu Jicong 已提交
895 896
  }

L
Liu Jicong 已提交
897 898 899 900 901 902 903 904
  STSchema*       pTschema = pHandle->pSchema;
  SSchemaWrapper* pSchemaWrapper = pHandle->pSchemaWrapper;

  int32_t numOfRows = pHandle->pBlock->numOfRows;
  int32_t numOfCols = pHandle->pSchema->numOfCols;
  int32_t colNumNeed = taosArrayGetSize(pHandle->pColIdList);

  SArray* pArray = taosArrayInit(colNumNeed, sizeof(SColumnInfoData));
905 906 907
  if (pArray == NULL) {
    return NULL;
  }
L
Liu Jicong 已提交
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929

  int j = 0;
  for (int32_t i = 0; i < colNumNeed; i++) {
    int32_t colId = *(int32_t*)taosArrayGet(pHandle->pColIdList, i);
    while (j < pSchemaWrapper->nCols && pSchemaWrapper->pSchema[j].colId < colId) {
      j++;
    }
    SSchema* pColSchema = &pSchemaWrapper->pSchema[j];
    ASSERT(pColSchema->colId == colId);
    SColumnInfoData colInfo = {0};
    int             sz = numOfRows * pColSchema->bytes;
    colInfo.info.bytes = pColSchema->bytes;
    colInfo.info.colId = colId;
    colInfo.info.type = pColSchema->type;

    colInfo.pData = calloc(1, sz);
    if (colInfo.pData == NULL) {
      // TODO free
      taosArrayDestroy(pArray);
      return NULL;
    }
    taosArrayPush(pArray, &colInfo);
930 931 932
  }

  SMemRow row;
L
Liu Jicong 已提交
933 934
  int32_t kvIdx = 0;
  tInitSubmitBlkIter(pHandle->pBlock, &pHandle->blkIter);
935
  while ((row = tGetSubmitBlkNext(&pHandle->blkIter)) != NULL) {
L
Liu Jicong 已提交
936 937 938 939 940 941
    // get all wanted col of that block
    for (int32_t i = 0; i < colNumNeed; i++) {
      SColumnInfoData* pColData = taosArrayGet(pArray, i);
      STColumn*        pCol = schemaColAt(pTschema, i);
      // TODO
      ASSERT(pCol->colId == pColData->info.colId);
942
      void* val = tdGetMemRowDataOfColEx(row, pCol->colId, pCol->type, TD_DATA_ROW_HEAD_SIZE + pCol->offset, &kvIdx);
L
Liu Jicong 已提交
943
      memcpy(pColData->pData, val, pCol->bytes);
944 945 946 947
    }
  }
  return pArray;
}