tsdbCacheRead.c 13.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * 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 "taoserror.h"
#include "tarray.h"
#include "tcommon.h"
#include "tsdb.h"
20
#include "tsdbDataFileRW.h"
21
#include "tsdbReadUtil.h"
22

23
#define HASTYPE(_type, _t) (((_type) & (_t)) == (_t))
24

H
Haojun Liao 已提交
25
static int32_t saveOneRow(SArray* pRow, SSDataBlock* pBlock, SCacheRowsReader* pReader, const int32_t* slotIds,
26
                          const int32_t* dstSlotIds, void** pRes, const char* idStr) {
27
  int32_t numOfRows = pBlock->info.rows;
M
Minglei Jin 已提交
28
  bool    allNullRow = true;
29

30
  if (HASTYPE(pReader->type, CACHESCAN_RETRIEVE_LAST)) {
31
    for (int32_t i = 0; i < pReader->numOfCols; ++i) {
32
      SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, dstSlotIds[i]);
33
      SFirstLastRes*   p = (SFirstLastRes*)varDataVal(pRes[i]);
M
Minglei Jin 已提交
34 35
      int32_t          slotId = slotIds[i];
      SLastCol*        pColVal = (SLastCol*)taosArrayGet(pRow, i);
36

37 38 39
      p->ts = pColVal->ts;
      p->isNull = !COL_VAL_IS_VALUE(&pColVal->colVal);
      allNullRow = p->isNull & allNullRow;
40

41 42 43
      if (!p->isNull) {
        if (IS_VAR_DATA_TYPE(pColVal->colVal.type)) {
          varDataSetLen(p->buf, pColVal->colVal.value.nData);
44

45 46 47 48 49
          memcpy(varDataVal(p->buf), pColVal->colVal.value.pData, pColVal->colVal.value.nData);
          p->bytes = pColVal->colVal.value.nData + VARSTR_HEADER_SIZE;  // binary needs to plus the header size
        } else {
          memcpy(p->buf, &pColVal->colVal.value, pReader->pSchema->columns[slotId].bytes);
          p->bytes = pReader->pSchema->columns[slotId].bytes;
50 51
        }
      }
52

53
      // pColInfoData->info.bytes includes the VARSTR_HEADER_SIZE, need to substruct it
54
      p->hasResult = true;
55
      varDataSetLen(pRes[i], pColInfoData->info.bytes - VARSTR_HEADER_SIZE);
56
      colDataSetVal(pColInfoData, numOfRows, (const char*)pRes[i], false);
57
    }
58

59
    pBlock->info.rows += allNullRow ? 0 : 1;
H
Haojun Liao 已提交
60
  } else if (HASTYPE(pReader->type, CACHESCAN_RETRIEVE_LAST_ROW)) {
61
    for (int32_t i = 0; i < pReader->numOfCols; ++i) {
M
Minglei Jin 已提交
62
      SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, dstSlotIds[i]);
63

M
Minglei Jin 已提交
64 65 66
      int32_t   slotId = slotIds[i];
      SLastCol* pColVal = (SLastCol*)taosArrayGet(pRow, i);
      SColVal*  pVal = &pColVal->colVal;
67

M
Minglei Jin 已提交
68
      allNullRow = false;
M
Minglei Jin 已提交
69 70 71
      if (IS_VAR_DATA_TYPE(pColVal->colVal.type)) {
        if (!COL_VAL_IS_VALUE(&pColVal->colVal)) {
          colDataSetNULL(pColInfoData, numOfRows);
72
        } else {
M
Minglei Jin 已提交
73
          varDataSetLen(pReader->transferBuf[slotId], pVal->value.nData);
74

M
Minglei Jin 已提交
75 76
          memcpy(varDataVal(pReader->transferBuf[slotId]), pVal->value.pData, pVal->value.nData);
          colDataSetVal(pColInfoData, numOfRows, pReader->transferBuf[slotId], false);
77
        }
M
Minglei Jin 已提交
78 79
      } else {
        colDataSetVal(pColInfoData, numOfRows, (const char*)&pVal->value.val, !COL_VAL_IS_VALUE(pVal));
80 81
      }
    }
82

M
Minglei Jin 已提交
83
    pBlock->info.rows += allNullRow ? 0 : 1;
H
Haojun Liao 已提交
84 85 86
  } else {
    tsdbError("invalid retrieve type:%d, %s", pReader->type, idStr);
    return TSDB_CODE_INVALID_PARA;
87
  }
H
Haojun Liao 已提交
88 89

  return TSDB_CODE_SUCCESS;
90 91
}

92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
static int32_t setTableSchema(SCacheRowsReader* p, uint64_t suid, const char* idstr) {
  int32_t numOfTables = p->numOfTables;

  if (suid != 0) {
    p->pSchema = metaGetTbTSchema(p->pVnode->pMeta, suid, -1, 1);
    if (p->pSchema == NULL) {
      tsdbWarn("stable:%" PRIu64 " has been dropped, failed to retrieve cached rows, %s", suid, idstr);
      return TSDB_CODE_PAR_TABLE_NOT_EXIST;
    }
  } else {
    for (int32_t i = 0; i < numOfTables; ++i) {
      uint64_t uid = p->pTableList[i].uid;
      p->pSchema = metaGetTbTSchema(p->pVnode->pMeta, uid, -1, 1);
      if (p->pSchema != NULL) {
        break;
      }

      tsdbWarn("table:%" PRIu64 " has been dropped, failed to retrieve cached rows, %s", uid, idstr);
    }

    // all queried tables have been dropped already, return immediately.
    if (p->pSchema == NULL) {
      tsdbWarn("all queried tables has been dropped, try next group, %s", idstr);
      return TSDB_CODE_PAR_TABLE_NOT_EXIST;
    }
  }

  return TSDB_CODE_SUCCESS;
}

D
dapan1121 已提交
122 123 124 125 126 127 128
int32_t tsdbReuseCacherowsReader(void* reader, void* pTableIdList, int32_t numOfTables) {
  SCacheRowsReader* pReader = (SCacheRowsReader*)reader;

  pReader->pTableList = pTableIdList;
  pReader->numOfTables = numOfTables;
  pReader->lastTs = INT64_MIN;

M
Minglei Jin 已提交
129 130 131 132
  int64_t blocks;
  double  elapse;
  pReader->pLDataIterArray = destroySttBlockReader(pReader->pLDataIterArray, &blocks, &elapse);
  pReader->pLDataIterArray = taosArrayInit(4, POINTER_BYTES);
D
dapan1121 已提交
133 134 135 136

  return TSDB_CODE_SUCCESS;
}

137
int32_t tsdbCacherowsReaderOpen(void* pVnode, int32_t type, void* pTableIdList, int32_t numOfTables, int32_t numOfCols,
M
Minglei Jin 已提交
138
                                SArray* pCidList, int32_t* pSlotIds, uint64_t suid, void** pReader, const char* idstr) {
139 140
  *pReader = NULL;
  SCacheRowsReader* p = taosMemoryCalloc(1, sizeof(SCacheRowsReader));
141 142 143 144
  if (p == NULL) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

145 146
  p->type = type;
  p->pVnode = pVnode;
147
  p->pTsdb = p->pVnode->pTsdb;
148
  p->info.verRange = (SVersionRange){.minVer = 0, .maxVer = UINT64_MAX};
149
  p->info.suid = suid;
150
  p->numOfCols = numOfCols;
151
  p->pCidList = pCidList;
M
Minglei Jin 已提交
152
  p->pSlotIds = pSlotIds;
153

154
  if (numOfTables == 0) {
155 156 157 158
    *pReader = p;
    return TSDB_CODE_SUCCESS;
  }

159
  p->pTableList = pTableIdList;
160
  p->numOfTables = numOfTables;
161

162 163 164 165 166 167
  int32_t code = setTableSchema(p, suid, idstr);
  if (code != TSDB_CODE_SUCCESS) {
    tsdbCacherowsReaderClose(p);
    return code;
  }

168
  p->transferBuf = taosMemoryCalloc(p->pSchema->numOfCols, POINTER_BYTES);
169
  if (p->transferBuf == NULL) {
H
Haojun Liao 已提交
170
    tsdbCacherowsReaderClose(p);
H
Haojun Liao 已提交
171
    return TSDB_CODE_OUT_OF_MEMORY;
172 173
  }

174
  for (int32_t i = 0; i < p->pSchema->numOfCols; ++i) {
M
Minglei Jin 已提交
175
    if (IS_VAR_DATA_TYPE(p->pSchema->columns[i].type)) {
176
      p->transferBuf[i] = taosMemoryMalloc(p->pSchema->columns[i].bytes);
177 178 179 180
      if (p->transferBuf[i] == NULL) {
        tsdbCacherowsReaderClose(p);
        return TSDB_CODE_OUT_OF_MEMORY;
      }
181 182
    }
  }
183

184
  p->idstr = taosStrdup(idstr);
185 186
  taosThreadMutexInit(&p->readerMutex, NULL);

187 188
  p->lastTs = INT64_MIN;

189 190 191 192
  *pReader = p;
  return TSDB_CODE_SUCCESS;
}

H
Haojun Liao 已提交
193
void* tsdbCacherowsReaderClose(void* pReader) {
194
  SCacheRowsReader* p = pReader;
H
Haojun Liao 已提交
195 196 197
  if (p == NULL) {
    return NULL;
  }
198

199 200 201 202 203 204 205
  if (p->pSchema != NULL) {
    for (int32_t i = 0; i < p->pSchema->numOfCols; ++i) {
      taosMemoryFreeClear(p->transferBuf[i]);
    }

    taosMemoryFree(p->transferBuf);
    taosMemoryFree(p->pSchema);
206 207
  }

208 209
  taosMemoryFree(p->pCurrSchema);

M
Minglei Jin 已提交
210 211 212 213 214
  if (p->pLDataIterArray) {
    int64_t loadBlocks = 0;
    double  elapse = 0;
    destroySttBlockReader(p->pLDataIterArray, &loadBlocks, &elapse);
  }
215

216 217 218 219 220
  if (p->pFileReader) {
    tsdbDataFileReaderClose(&p->pFileReader);
    p->pFileReader = NULL;
  }

221
  taosMemoryFree((void*)p->idstr);
222 223
  taosThreadMutexDestroy(&p->readerMutex);

224 225 226 227 228 229 230 231 232 233 234 235 236 237
  if (p->pTableMap) {
    void*   pe = NULL;
    int32_t iter = 0;
    while ((pe = tSimpleHashIterate(p->pTableMap, pe, &iter)) != NULL) {
      STableLoadInfo* pInfo = *(STableLoadInfo**)pe;
      pInfo->pTombData = taosArrayDestroy(pInfo->pTombData);
    }

    tSimpleHashCleanup(p->pTableMap);
  }
  if (p->uidList) {
    taosMemoryFree(p->uidList);
  }

238
  taosMemoryFree(pReader);
H
Haojun Liao 已提交
239
  return NULL;
240 241
}

242
static void freeItem(void* pItem) {
243
  SLastCol* pCol = (SLastCol*)pItem;
244
  if (IS_VAR_DATA_TYPE(pCol->colVal.type) && pCol->colVal.value.pData) {
245 246 247 248
    taosMemoryFree(pCol->colVal.value.pData);
  }
}

249
static int32_t tsdbCacheQueryReseek(void* pQHandle) {
250 251 252
  int32_t           code = 0;
  SCacheRowsReader* pReader = pQHandle;

253 254 255 256
  code = taosThreadMutexTryLock(&pReader->readerMutex);
  if (code == 0) {
    // pause current reader's state if not paused, save ts & version for resuming
    // just wait for the big all tables' snapshot untaking for now
257

258
    code = TSDB_CODE_VND_QUERY_BUSY;
259

260 261 262 263 264 265 266 267
    taosThreadMutexUnlock(&pReader->readerMutex);

    return code;
  } else if (code == EBUSY) {
    return TSDB_CODE_VND_QUERY_BUSY;
  } else {
    return -1;
  }
268 269
}

270 271
int32_t tsdbRetrieveCacheRows(void* pReader, SSDataBlock* pResBlock, const int32_t* slotIds, const int32_t* dstSlotIds,
                              SArray* pTableUidList) {
272
  if (pReader == NULL || pResBlock == NULL) {
273 274 275
    return TSDB_CODE_INVALID_PARA;
  }

276
  SCacheRowsReader* pr = pReader;
M
Minglei Jin 已提交
277 278 279
  int32_t           code = TSDB_CODE_SUCCESS;
  SArray*           pRow = taosArrayInit(TARRAY_SIZE(pr->pCidList), sizeof(SLastCol));
  bool              hasRes = false;
280

281
  void** pRes = taosMemoryCalloc(pr->numOfCols, POINTER_BYTES);
282 283 284 285 286
  if (pRes == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _end;
  }

287
  for (int32_t j = 0; j < pr->numOfCols; ++j) {
288
    pRes[j] = taosMemoryCalloc(1, sizeof(SFirstLastRes) + pr->pSchema->columns[slotIds[j]].bytes + VARSTR_HEADER_SIZE);
289 290 291 292
    SFirstLastRes* p = (SFirstLastRes*)varDataVal(pRes[j]);
    p->ts = INT64_MIN;
  }

293
  taosThreadMutexLock(&pr->readerMutex);
M
Minglei Jin 已提交
294
  code = tsdbTakeReadSnap2((STsdbReader*)pr, tsdbCacheQueryReseek, &pr->pReadSnap);
K
kailixu 已提交
295 296 297
  if (code != TSDB_CODE_SUCCESS) {
    goto _end;
  }
298

299 300
  int8_t         ltype = (pr->type & CACHESCAN_RETRIEVE_LAST) >> 3;
  STableKeyInfo* pTableList = pr->pTableList;
301

302
  // retrieve the only one last row of all tables in the uid list.
H
Haojun Liao 已提交
303
  if (HASTYPE(pr->type, CACHESCAN_RETRIEVE_TYPE_SINGLE)) {
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
    SArray* pLastCols = taosArrayInit(pr->numOfCols, sizeof(SLastCol));
    if (pLastCols == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _end;
    }

    for (int32_t i = 0; i < pr->numOfCols; ++i) {
      int32_t          slotId = slotIds[i];
      struct STColumn* pCol = &pr->pSchema->columns[slotId];
      SLastCol         p = {.ts = INT64_MIN, .colVal.type = pCol->type, .colVal.flag = CV_FLAG_NULL};

      if (IS_VAR_DATA_TYPE(pCol->type)) {
        p.colVal.value.pData = taosMemoryCalloc(pCol->bytes, sizeof(char));
      }
      taosArrayPush(pLastCols, &p);
    }

321 322
    int64_t st = taosGetTimestampUs();
    int64_t totalLastTs = INT64_MAX;
323
    for (int32_t i = 0; i < pr->numOfTables; ++i) {
324
      tb_uid_t uid = pTableList[i].uid;
325

326
      tsdbCacheGetBatch(pr->pTsdb, uid, pRow, pr, ltype);
327
      if (TARRAY_SIZE(pRow) <= 0 || COL_VAL_IS_NONE(&((SLastCol*)TARRAY_DATA(pRow))[0].colVal)) {
M
Minglei Jin 已提交
328
        taosArrayClearEx(pRow, freeItem);
329 330 331
        continue;
      }

H
Haojun Liao 已提交
332
      {
333
        bool    hasNotNullRow = true;
X
Xiaoyu Wang 已提交
334
        int64_t singleTableLastTs = INT64_MAX;
H
Haojun Liao 已提交
335
        for (int32_t k = 0; k < pr->numOfCols; ++k) {
336 337
          SLastCol* p = taosArrayGet(pLastCols, k);
          SLastCol* pColVal = (SLastCol*)taosArrayGet(pRow, k);
338

339 340 341 342
          if (pColVal->ts > p->ts) {
            if (!COL_VAL_IS_VALUE(&pColVal->colVal) && HASTYPE(pr->type, CACHESCAN_RETRIEVE_LAST)) {
              if (!COL_VAL_IS_VALUE(&p->colVal)) {
                hasNotNullRow = false;
H
Haojun Liao 已提交
343
              }
344
              continue;
H
Haojun Liao 已提交
345
            }
346

347 348
            hasRes = true;
            p->ts = pColVal->ts;
349 350
            if (k == 0) {
              if (TARRAY_SIZE(pTableUidList) == 0) {
351
                taosArrayPush(pTableUidList, &uid);
352
              } else {
353
                taosArraySet(pTableUidList, 0, &uid);
354
              }
355
            }
H
Haojun Liao 已提交
356

357 358 359
            if (pColVal->ts < singleTableLastTs && HASTYPE(pr->type, CACHESCAN_RETRIEVE_LAST)) {
              singleTableLastTs = pColVal->ts;
            }
H
Haojun Liao 已提交
360

361 362 363 364 365
            if (!IS_VAR_DATA_TYPE(pColVal->colVal.type)) {
              p->colVal = pColVal->colVal;
            } else {
              if (COL_VAL_IS_VALUE(&pColVal->colVal)) {
                memcpy(p->colVal.value.pData, pColVal->colVal.value.pData, pColVal->colVal.value.nData);
H
Haojun Liao 已提交
366
              }
367 368 369 370 371

              p->colVal.value.nData = pColVal->colVal.value.nData;
              p->colVal.type = pColVal->colVal.type;
              p->colVal.flag = pColVal->colVal.flag;
              p->colVal.cid = pColVal->colVal.cid;
H
Haojun Liao 已提交
372 373
            }
          }
374
        }
375 376

        if (hasNotNullRow) {
X
Xiaoyu Wang 已提交
377
          if (INT64_MAX == totalLastTs || (INT64_MAX != singleTableLastTs && totalLastTs < singleTableLastTs)) {
X
Xiaoyu Wang 已提交
378
            totalLastTs = singleTableLastTs;
X
Xiaoyu Wang 已提交
379
          }
380 381
          double cost = (taosGetTimestampUs() - st) / 1000.0;
          if (cost > tsCacheLazyLoadThreshold) {
X
Xiaoyu Wang 已提交
382
            pr->lastTs = totalLastTs;
383
          }
384
        }
385
      }
386

M
Minglei Jin 已提交
387
      taosArrayClearEx(pRow, freeItem);
388
    }
389 390

    if (hasRes) {
391
      saveOneRow(pLastCols, pResBlock, pr, slotIds, dstSlotIds, pRes, pr->idstr);
392
    }
393 394

    taosArrayDestroyEx(pLastCols, freeItem);
H
Haojun Liao 已提交
395
  } else if (HASTYPE(pr->type, CACHESCAN_RETRIEVE_TYPE_ALL)) {
396
    for (int32_t i = pr->tableIndex; i < pr->numOfTables; ++i) {
397
      tb_uid_t uid = pTableList[i].uid;
398

399
      tsdbCacheGetBatch(pr->pTsdb, uid, pRow, pr, ltype);
400
      if (TARRAY_SIZE(pRow) <= 0 || COL_VAL_IS_NONE(&((SLastCol*)TARRAY_DATA(pRow))[0].colVal)) {
M
Minglei Jin 已提交
401
        taosArrayClearEx(pRow, freeItem);
402 403 404
        continue;
      }

405
      saveOneRow(pRow, pResBlock, pr, slotIds, dstSlotIds, pRes, pr->idstr);
M
Minglei Jin 已提交
406
      taosArrayClearEx(pRow, freeItem);
407

408
      taosArrayPush(pTableUidList, &uid);
409

410
      ++pr->tableIndex;
411
      if (pResBlock->info.rows >= pResBlock->info.capacity) {
412
        goto _end;
413
      }
414 415
    }
  } else {
416
    code = TSDB_CODE_INVALID_PARA;
417 418
  }

419
_end:
420
  tsdbUntakeReadSnap2((STsdbReader*)pr, pr->pReadSnap, true);
421

422
  taosThreadMutexUnlock(&pr->readerMutex);
423

H
Haojun Liao 已提交
424 425 426 427
  if (pRes != NULL) {
    for (int32_t j = 0; j < pr->numOfCols; ++j) {
      taosMemoryFree(pRes[j]);
    }
428 429 430
  }

  taosMemoryFree(pRes);
431
  taosArrayDestroy(pRow);
432

433
  return code;
434
}