tqSink.c 7.6 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"

C
Cary Xu 已提交
18
SSubmitReq* tdBlockToSubmit(const SArray* pBlocks, const STSchema* pTSchema, bool createTb, int64_t suid,
19
                            const char* stbFullName, int32_t vgId, SBatchDeleteReq* deleteReq) {
20
  SSubmitReq* ret = NULL;
21 22
  SArray*     schemaReqs = NULL;
  SArray*     schemaReqSz = NULL;
23 24 25 26 27 28 29 30
  SArray*     tagArray = taosArrayInit(1, sizeof(STagVal));
  if (!tagArray) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }

  int32_t sz = taosArrayGetSize(pBlocks);

31 32 33 34 35
  if (createTb) {
    schemaReqs = taosArrayInit(sz, sizeof(void*));
    schemaReqSz = taosArrayInit(sz, sizeof(int32_t));
    for (int32_t i = 0; i < sz; i++) {
      SSDataBlock* pDataBlock = taosArrayGet(pBlocks, i);
36 37 38 39 40 41 42
      if (pDataBlock->info.type == STREAM_DELETE_DATA) {
        //
      }
      STagVal tagVal = {
          .cid = taosArrayGetSize(pDataBlock->pDataBlock) + 1,
          .type = TSDB_DATA_TYPE_UBIGINT,
          .i64 = (int64_t)pDataBlock->info.groupId,
L
Liu Jicong 已提交
43 44
      };
      STag* pTag = NULL;
45 46 47 48
      taosArrayClear(tagArray);
      taosArrayPush(tagArray, &tagVal);
      tTagNew(tagArray, 1, false, &pTag);
      if (pTag == NULL) {
L
Liu Jicong 已提交
49
        terrno = TSDB_CODE_OUT_OF_MEMORY;
50 51 52
        taosArrayDestroy(tagArray);
        return NULL;
      }
53 54

      SVCreateTbReq createTbReq = {0};
55 56 57
      SName         name = {0};
      tNameFromString(&name, stbFullName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);

58
      createTbReq.name = buildCtbNameByGroupId(stbFullName, pDataBlock->info.groupId);
59
      createTbReq.ctb.name = strdup((char*)tNameGetTableName(&name));  // strdup(stbFullName);
60 61 62
      createTbReq.flags = 0;
      createTbReq.type = TSDB_CHILD_TABLE;
      createTbReq.ctb.suid = suid;
63 64 65
      createTbReq.ctb.pTag = (uint8_t*)pTag;

      int32_t code;
66
      int32_t schemaLen;
67 68
      tEncodeSize(tEncodeSVCreateTbReq, &createTbReq, schemaLen, code);
      if (code < 0) {
69
        tdDestroySVCreateTbReq(&createTbReq);
70
        taosArrayDestroy(tagArray);
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
        taosMemoryFreeClear(ret);
        return NULL;
      }

      void* schemaStr = taosMemoryMalloc(schemaLen);
      if (schemaStr == NULL) {
        terrno = TSDB_CODE_OUT_OF_MEMORY;
        return NULL;
      }
      taosArrayPush(schemaReqs, &schemaStr);
      taosArrayPush(schemaReqSz, &schemaLen);

      SEncoder encoder = {0};
      tEncoderInit(&encoder, schemaStr, schemaLen);
      code = tEncodeSVCreateTbReq(&encoder, &createTbReq);
      if (code < 0) {
        terrno = TSDB_CODE_OUT_OF_MEMORY;
88 89
        return NULL;
      }
90 91
      tEncoderClear(&encoder);
      tdDestroySVCreateTbReq(&createTbReq);
92
    }
93 94
  }
  taosArrayDestroy(tagArray);
95

96 97 98 99 100 101 102 103 104 105 106 107 108
  // cal size
  int32_t cap = sizeof(SSubmitReq);
  for (int32_t i = 0; i < sz; i++) {
    SSDataBlock* pDataBlock = taosArrayGet(pBlocks, i);
    int32_t      rows = pDataBlock->info.rows;
    // TODO min
    int32_t rowSize = pDataBlock->info.rowSize;
    int32_t maxLen = TD_ROW_MAX_BYTES_FROM_SCHEMA(pTSchema);

    int32_t schemaLen = 0;
    if (createTb) {
      schemaLen = *(int32_t*)taosArrayGet(schemaReqSz, i);
    }
109 110 111 112 113 114 115 116 117 118
    cap += sizeof(SSubmitBlk) + schemaLen + rows * maxLen;
  }

  // assign data
  // TODO
  ret = rpcMallocCont(cap);
  ret->header.vgId = vgId;
  ret->length = sizeof(SSubmitReq);
  ret->numOfBlocks = htonl(sz);

119
  SSubmitBlk* blkHead = POINTER_SHIFT(ret, sizeof(SSubmitReq));
120 121 122
  for (int32_t i = 0; i < sz; i++) {
    SSDataBlock* pDataBlock = taosArrayGet(pBlocks, i);

123
    blkHead->numOfRows = htonl(pDataBlock->info.rows);
124 125 126 127 128 129 130 131
    blkHead->sversion = htonl(pTSchema->version);
    // TODO
    blkHead->suid = htobe64(suid);
    // uid is assigned by vnode
    blkHead->uid = 0;

    int32_t rows = pDataBlock->info.rows;

L
Liu Jicong 已提交
132 133
    tqDebug("tq sink, convert block %d, rows: %d", i, rows);

134
    int32_t dataLen = 0;
135

136
    void* blkSchema = POINTER_SHIFT(blkHead, sizeof(SSubmitBlk));
137 138 139

    int32_t schemaLen = 0;
    if (createTb) {
140 141 142
      schemaLen = *(int32_t*)taosArrayGet(schemaReqSz, i);
      void* schemaStr = taosArrayGetP(schemaReqs, i);
      memcpy(blkSchema, schemaStr, schemaLen);
143 144 145
    }
    blkHead->schemaLen = htonl(schemaLen);

146
    STSRow* rowData = POINTER_SHIFT(blkSchema, schemaLen);
147 148 149 150 151 152 153 154 155 156
    for (int32_t j = 0; j < rows; j++) {
      SRowBuilder rb = {0};
      tdSRowInit(&rb, pTSchema->version);
      tdSRowSetTpInfo(&rb, pTSchema->numOfCols, pTSchema->flen);
      tdSRowResetBuf(&rb, rowData);

      for (int32_t k = 0; k < pTSchema->numOfCols; k++) {
        const STColumn*  pColumn = &pTSchema->columns[k];
        SColumnInfoData* pColData = taosArrayGet(pDataBlock->pDataBlock, k);
        if (colDataIsNull_s(pColData, j)) {
157
          tdAppendColValToRow(&rb, pColumn->colId, pColumn->type, TD_VTYPE_NULL, NULL, false, pColumn->offset, k);
158 159 160 161 162 163 164
        } else {
          void* data = colDataGetData(pColData, j);
          tdAppendColValToRow(&rb, pColumn->colId, pColumn->type, TD_VTYPE_NORM, data, true, pColumn->offset, k);
        }
      }
      int32_t rowLen = TD_ROW_LEN(rowData);
      rowData = POINTER_SHIFT(rowData, rowLen);
165
      dataLen += rowLen;
166 167 168 169
    }
    blkHead->dataLen = htonl(dataLen);

    ret->length += sizeof(SSubmitBlk) + schemaLen + dataLen;
170
    blkHead = POINTER_SHIFT(blkHead, sizeof(SSubmitBlk) + schemaLen + dataLen);
171 172 173
  }

  ret->length = htonl(ret->length);
174 175 176 177

  if (schemaReqs) taosArrayDestroyP(schemaReqs, taosMemoryFree);
  taosArrayDestroy(schemaReqSz);

178 179 180
  return ret;
}

L
Liu Jicong 已提交
181
void tqTableSink(SStreamTask* pTask, void* vnode, int64_t ver, void* data) {
182 183 184
  const SArray*   pRes = (const SArray*)data;
  SVnode*         pVnode = (SVnode*)vnode;
  SBatchDeleteReq deleteReq = {0};
L
Liu Jicong 已提交
185

L
Liu Jicong 已提交
186
  tqDebug("vgId:%d, task %d write into table, block num: %d", TD_VID(pVnode), pTask->taskId, (int32_t)pRes->size);
L
Liu Jicong 已提交
187

L
Liu Jicong 已提交
188
  ASSERT(pTask->tbSink.pTSchema);
189
  deleteReq.deleteReqs = taosArrayInit(0, sizeof(SSingleDeleteReq));
L
Liu Jicong 已提交
190
  SSubmitReq* pReq = tdBlockToSubmit(pRes, pTask->tbSink.pTSchema, true, pTask->tbSink.stbUid,
191
                                     pTask->tbSink.stbFullName, pVnode->config.vgId, &deleteReq);
L
Liu Jicong 已提交
192 193 194

  tqDebug("vgId:%d, task %d convert blocks over, put into write-queue", TD_VID(pVnode), pTask->taskId);

195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
  int32_t code;
  int32_t len;
  tEncodeSize(tEncodeSBatchDeleteReq, &deleteReq, len, code);
  if (code < 0) {
    //
    ASSERT(0);
  }
  SEncoder encoder;
  void*    buf = taosMemoryCalloc(1, len + sizeof(SMsgHead));
  void*    abuf = POINTER_SHIFT(buf, sizeof(SMsgHead));
  tEncoderInit(&encoder, abuf, len);
  tEncodeSBatchDeleteReq(&encoder, &deleteReq);
  tEncoderClear(&encoder);

  if (taosArrayGetSize(deleteReq.deleteReqs) != 0) {
    SRpcMsg msg = {
        .msgType = TDMT_VND_BATCH_DEL,
        .pCont = buf,
        .contLen = len + sizeof(SMsgHead),
    };
    if (tmsgPutToQueue(&pVnode->msgCb, WRITE_QUEUE, &msg) != 0) {
      tqDebug("failed to put into write-queue since %s", terrstr());
    }
  }
  taosArrayDestroy(deleteReq.deleteReqs);

L
Liu Jicong 已提交
221 222 223 224 225 226 227 228
  /*tPrintFixedSchemaSubmitReq(pReq, pTask->tbSink.pTSchema);*/
  // build write msg
  SRpcMsg msg = {
      .msgType = TDMT_VND_SUBMIT,
      .pCont = pReq,
      .contLen = ntohl(pReq->length),
  };

S
Shengliang Guan 已提交
229 230 231
  if (tmsgPutToQueue(&pVnode->msgCb, WRITE_QUEUE, &msg) != 0) {
    tqDebug("failed to put into write-queue since %s", terrstr());
  }
L
Liu Jicong 已提交
232
}