tsdbCacheRead.c 14.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

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

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

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

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

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

44 45 46 47 48
          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;
49 50
        }
      }
51

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

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

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

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

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

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

  return TSDB_CODE_SUCCESS;
89 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
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 已提交
121 122 123 124 125 126 127
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 已提交
128 129 130 131
  int64_t blocks;
  double  elapse;
  pReader->pLDataIterArray = destroySttBlockReader(pReader->pLDataIterArray, &blocks, &elapse);
  pReader->pLDataIterArray = taosArrayInit(4, POINTER_BYTES);
D
dapan1121 已提交
132 133 134 135

  return TSDB_CODE_SUCCESS;
}

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

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

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

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

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

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

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

183
  SVnodeCfg* pCfg = &((SVnode*)pVnode)->config;
184
  int32_t    numOfStt = pCfg->sttTrigger;
M
Minglei Jin 已提交
185 186
  p->pLDataIterArray = taosArrayInit(4, POINTER_BYTES);
  if (p->pLDataIterArray == NULL) {
187 188 189 190
    tsdbCacherowsReaderClose(p);
    return TSDB_CODE_OUT_OF_MEMORY;
  }

191
  p->idstr = taosStrdup(idstr);
192 193
  taosThreadMutexInit(&p->readerMutex, NULL);

194 195
  p->lastTs = INT64_MIN;

196 197 198 199
  *pReader = p;
  return TSDB_CODE_SUCCESS;
}

H
Haojun Liao 已提交
200
void* tsdbCacherowsReaderClose(void* pReader) {
201
  SCacheRowsReader* p = pReader;
H
Haojun Liao 已提交
202 203 204
  if (p == NULL) {
    return NULL;
  }
205

206 207 208 209 210 211 212
  if (p->pSchema != NULL) {
    for (int32_t i = 0; i < p->pSchema->numOfCols; ++i) {
      taosMemoryFreeClear(p->transferBuf[i]);
    }

    taosMemoryFree(p->transferBuf);
    taosMemoryFree(p->pSchema);
213 214
  }

215 216
  taosMemoryFree(p->pCurrSchema);

M
Minglei Jin 已提交
217 218 219
  int64_t loadBlocks = 0;
  double  elapse = 0;
  destroySttBlockReader(p->pLDataIterArray, &loadBlocks, &elapse);
220

221 222 223 224 225
  if (p->pFileReader) {
    tsdbDataFileReaderClose(&p->pFileReader);
    p->pFileReader = NULL;
  }

226
  taosMemoryFree((void*)p->idstr);
227 228
  taosThreadMutexDestroy(&p->readerMutex);

229 230 231 232 233 234 235 236 237 238 239 240 241 242
  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);
  }

243
  taosMemoryFree(pReader);
H
Haojun Liao 已提交
244
  return NULL;
245 246
}

247
static void freeItem(void* pItem) {
248
  SLastCol* pCol = (SLastCol*)pItem;
249
  if (IS_VAR_DATA_TYPE(pCol->colVal.type) && pCol->colVal.value.pData) {
250 251 252 253
    taosMemoryFree(pCol->colVal.value.pData);
  }
}

254
static int32_t tsdbCacheQueryReseek(void* pQHandle) {
255 256 257
  int32_t           code = 0;
  SCacheRowsReader* pReader = pQHandle;

258 259 260 261
  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
262

263
    code = TSDB_CODE_VND_QUERY_BUSY;
264

265 266 267 268 269 270 271 272
    taosThreadMutexUnlock(&pReader->readerMutex);

    return code;
  } else if (code == EBUSY) {
    return TSDB_CODE_VND_QUERY_BUSY;
  } else {
    return -1;
  }
273 274
}

275 276
int32_t tsdbRetrieveCacheRows(void* pReader, SSDataBlock* pResBlock, const int32_t* slotIds, const int32_t* dstSlotIds,
                              SArray* pTableUidList) {
277
  if (pReader == NULL || pResBlock == NULL) {
278 279 280
    return TSDB_CODE_INVALID_PARA;
  }

281
  SCacheRowsReader* pr = pReader;
M
Minglei Jin 已提交
282 283 284 285
  int32_t           code = TSDB_CODE_SUCCESS;
  SArray*           pRow = taosArrayInit(TARRAY_SIZE(pr->pCidList), sizeof(SLastCol));
  bool              hasRes = false;
  SArray*           pLastCols = NULL;
286

287
  void** pRes = taosMemoryCalloc(pr->numOfCols, POINTER_BYTES);
288 289 290 291 292
  if (pRes == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _end;
  }

293
  for (int32_t j = 0; j < pr->numOfCols; ++j) {
M
Minglei Jin 已提交
294 295 296
    pRes[j] =
        taosMemoryCalloc(1, sizeof(SFirstLastRes) + pr->pSchema->columns[/*-1 == slotIds[j] ? 0 : */ slotIds[j]].bytes +
                                VARSTR_HEADER_SIZE);
297 298 299 300
    SFirstLastRes* p = (SFirstLastRes*)varDataVal(pRes[j]);
    p->ts = INT64_MIN;
  }

301
  pLastCols = taosArrayInit(pr->numOfCols, sizeof(SLastCol));
302 303 304 305 306
  if (pLastCols == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _end;
  }

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

    if (IS_VAR_DATA_TYPE(pCol->type)) {
      p.colVal.value.pData = taosMemoryCalloc(pCol->bytes, sizeof(char));
    }
    taosArrayPush(pLastCols, &p);
H
Haojun Liao 已提交
316
  }
317

318
  taosThreadMutexLock(&pr->readerMutex);
M
Minglei Jin 已提交
319
  code = tsdbTakeReadSnap2((STsdbReader*)pr, tsdbCacheQueryReseek, &pr->pReadSnap);
K
kailixu 已提交
320 321 322
  if (code != TSDB_CODE_SUCCESS) {
    goto _end;
  }
323

324
  int8_t ltype = (pr->type & CACHESCAN_RETRIEVE_LAST) >> 3;
325

326
  // retrieve the only one last row of all tables in the uid list.
H
Haojun Liao 已提交
327
  if (HASTYPE(pr->type, CACHESCAN_RETRIEVE_TYPE_SINGLE)) {
328
    int64_t st = taosGetTimestampUs();
X
Xiaoyu Wang 已提交
329
    int64_t totalLastTs = INT64_MAX;
330

331 332
    for (int32_t i = 0; i < pr->numOfTables; ++i) {
      STableKeyInfo* pKeyInfo = &pr->pTableList[i];
333

334 335
      tsdbCacheGetBatch(pr->pTsdb, pKeyInfo->uid, pRow, pr, ltype);
      // tsdbCacheGet(pr->pTsdb, pKeyInfo->uid, pRow, pr, ltype);
336
      if (TARRAY_SIZE(pRow) <= 0) {
M
Minglei Jin 已提交
337 338
        taosArrayClearEx(pRow, freeItem);
        // taosArrayClear(pRow);
339 340
        continue;
      }
341
      SLastCol* pColVal = taosArrayGet(pRow, 0);
M
Minglei Jin 已提交
342
      if (COL_VAL_IS_NONE(&pColVal->colVal)) {
M
Minglei Jin 已提交
343 344
        taosArrayClearEx(pRow, freeItem);
        // taosArrayClear(pRow);
345 346 347
        continue;
      }

H
Haojun Liao 已提交
348
      {
349
        bool    hasNotNullRow = true;
X
Xiaoyu Wang 已提交
350
        int64_t singleTableLastTs = INT64_MAX;
H
Haojun Liao 已提交
351
        for (int32_t k = 0; k < pr->numOfCols; ++k) {
352 353
          SLastCol* p = taosArrayGet(pLastCols, k);
          SLastCol* pColVal = (SLastCol*)taosArrayGet(pRow, k);
354

355 356 357 358
          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 已提交
359
              }
360
              continue;
H
Haojun Liao 已提交
361
            }
362

363 364
            hasRes = true;
            p->ts = pColVal->ts;
365 366 367 368 369
            if (k == 0) {
              if (TARRAY_SIZE(pTableUidList) == 0) {
                taosArrayPush(pTableUidList, &pKeyInfo->uid);
              } else {
                taosArraySet(pTableUidList, 0, &pKeyInfo->uid);
370
              }
371
            }
H
Haojun Liao 已提交
372

373 374 375
            if (pColVal->ts < singleTableLastTs && HASTYPE(pr->type, CACHESCAN_RETRIEVE_LAST)) {
              singleTableLastTs = pColVal->ts;
            }
H
Haojun Liao 已提交
376

377 378 379 380 381
            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 已提交
382
              }
383 384 385 386 387

              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 已提交
388 389
            }
          }
390
        }
391 392

        if (hasNotNullRow) {
X
Xiaoyu Wang 已提交
393
          if (INT64_MAX == totalLastTs || (INT64_MAX != singleTableLastTs && totalLastTs < singleTableLastTs)) {
X
Xiaoyu Wang 已提交
394
            totalLastTs = singleTableLastTs;
X
Xiaoyu Wang 已提交
395
          }
396 397
          double cost = (taosGetTimestampUs() - st) / 1000.0;
          if (cost > tsCacheLazyLoadThreshold) {
X
Xiaoyu Wang 已提交
398
            pr->lastTs = totalLastTs;
399
          }
400
        }
401
      }
402

M
Minglei Jin 已提交
403 404
      taosArrayClearEx(pRow, freeItem);
      // taosArrayClear(pRow);
405
    }
406 407

    if (hasRes) {
408
      saveOneRow(pLastCols, pResBlock, pr, slotIds, dstSlotIds, pRes, pr->idstr);
409
    }
H
Haojun Liao 已提交
410
  } else if (HASTYPE(pr->type, CACHESCAN_RETRIEVE_TYPE_ALL)) {
411
    for (int32_t i = pr->tableIndex; i < pr->numOfTables; ++i) {
412
      tb_uid_t uid = pr->pTableList[i].uid;
413

414
      tsdbCacheGetBatch(pr->pTsdb, uid, pRow, pr, ltype);
415
      if (TARRAY_SIZE(pRow) <= 0) {
M
Minglei Jin 已提交
416 417
        taosArrayClearEx(pRow, freeItem);
        // taosArrayClear(pRow);
418 419
        continue;
      }
M
Minglei Jin 已提交
420 421
      SLastCol* pColVal = (SLastCol*)taosArrayGet(pRow, 0);
      if (COL_VAL_IS_NONE(&pColVal->colVal)) {
M
Minglei Jin 已提交
422 423
        taosArrayClearEx(pRow, freeItem);
        // taosArrayClear(pRow);
424 425 426
        continue;
      }

427
      saveOneRow(pRow, pResBlock, pr, slotIds, dstSlotIds, pRes, pr->idstr);
M
Minglei Jin 已提交
428 429
      taosArrayClearEx(pRow, freeItem);
      // taosArrayClear(pRow);
430

431
      taosArrayPush(pTableUidList, &uid);
432

433
      ++pr->tableIndex;
434
      if (pResBlock->info.rows >= pResBlock->info.capacity) {
435
        goto _end;
436
      }
437 438
    }
  } else {
439
    code = TSDB_CODE_INVALID_PARA;
440 441
  }

442
_end:
443
  tsdbUntakeReadSnap2((STsdbReader*)pr, pr->pReadSnap, true);
444

M
Minglei Jin 已提交
445 446 447 448 449
  int64_t loadBlocks = 0;
  double  elapse = 0;
  pr->pLDataIterArray = destroySttBlockReader(pr->pLDataIterArray, &loadBlocks, &elapse);
  pr->pLDataIterArray = taosArrayInit(4, POINTER_BYTES);

450
  taosThreadMutexUnlock(&pr->readerMutex);
451

H
Haojun Liao 已提交
452 453 454 455
  if (pRes != NULL) {
    for (int32_t j = 0; j < pr->numOfCols; ++j) {
      taosMemoryFree(pRes[j]);
    }
456 457 458
  }

  taosMemoryFree(pRes);
459 460
  // taosArrayDestroyEx(pRow, freeItem);
  taosArrayDestroy(pRow);
461
  taosArrayDestroyEx(pLastCols, freeItem);
462

463
  return code;
464
}