tsdbCacheRead.c 12.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * 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"

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

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

27
  if (HASTYPE(pReader->type, CACHESCAN_RETRIEVE_LAST)) {
28 29
    bool allNullRow = true;

30
    for (int32_t i = 0; i < pReader->numOfCols; ++i) {
31
      SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, dstSlotIds[i]);
M
Minglei Jin 已提交
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 43 44 45 46 47
      if (!p->isNull) {
        if (IS_VAR_DATA_TYPE(pColVal->colVal.type)) {
          varDataSetLen(p->buf, pColVal->colVal.value.nData);
          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;
48 49
        }
      }
50

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

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

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

M
Minglei Jin 已提交
66 67 68
      if (IS_VAR_DATA_TYPE(pColVal->colVal.type)) {
        if (!COL_VAL_IS_VALUE(&pColVal->colVal)) {
          colDataSetNULL(pColInfoData, numOfRows);
69
        } else {
M
Minglei Jin 已提交
70 71 72 73
          varDataSetLen(pReader->transferBuf[slotId], pVal->value.nData);
          // tsdbError("buf:%p len:%d index:%d", pReader->transferBuf[slotId], pVal->value.nData, slotId);
          memcpy(varDataVal(pReader->transferBuf[slotId]), pVal->value.pData, pVal->value.nData);
          colDataSetVal(pColInfoData, numOfRows, pReader->transferBuf[slotId], false);
74
        }
M
Minglei Jin 已提交
75 76
      } else {
        colDataSetVal(pColInfoData, numOfRows, (const char*)&pVal->value.val, !COL_VAL_IS_VALUE(pVal));
77 78
      }
    }
79

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

  return TSDB_CODE_SUCCESS;
87 88
}

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
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;
}

119
int32_t tsdbCacherowsReaderOpen(void* pVnode, int32_t type, void* pTableIdList, int32_t numOfTables, int32_t numOfCols,
120
                                SArray* pCidList, uint64_t suid, void** pReader, const char* idstr) {
121 122
  *pReader = NULL;
  SCacheRowsReader* p = taosMemoryCalloc(1, sizeof(SCacheRowsReader));
123 124 125 126
  if (p == NULL) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

127 128
  p->type = type;
  p->pVnode = pVnode;
129 130
  p->pTsdb = p->pVnode->pTsdb;
  p->verRange = (SVersionRange){.minVer = 0, .maxVer = UINT64_MAX};
131
  p->numOfCols = numOfCols;
132
  p->pCidList = pCidList;
133
  p->suid = suid;
134

135
  if (numOfTables == 0) {
136 137 138 139
    *pReader = p;
    return TSDB_CODE_SUCCESS;
  }

140
  p->pTableList = pTableIdList;
141
  p->numOfTables = numOfTables;
142

143 144 145 146 147 148
  int32_t code = setTableSchema(p, suid, idstr);
  if (code != TSDB_CODE_SUCCESS) {
    tsdbCacherowsReaderClose(p);
    return code;
  }

149
  p->transferBuf = taosMemoryCalloc(p->pSchema->numOfCols, POINTER_BYTES);
150
  if (p->transferBuf == NULL) {
H
Haojun Liao 已提交
151
    tsdbCacherowsReaderClose(p);
H
Haojun Liao 已提交
152
    return TSDB_CODE_OUT_OF_MEMORY;
153 154
  }

155
  for (int32_t i = 0; i < p->pSchema->numOfCols; ++i) {
M
Minglei Jin 已提交
156
    if (IS_VAR_DATA_TYPE(p->pSchema->columns[i].type)) {
157
      p->transferBuf[i] = taosMemoryMalloc(p->pSchema->columns[i].bytes);
M
Minglei Jin 已提交
158
      // tsdbError("buf:%p len:%d index:%d", p->transferBuf[i], p->pSchema->columns[i].bytes, i);
159 160 161 162
      if (p->transferBuf[i] == NULL) {
        tsdbCacherowsReaderClose(p);
        return TSDB_CODE_OUT_OF_MEMORY;
      }
163 164
    }
  }
165

166 167
  int32_t numOfStt = ((SVnode*)pVnode)->config.sttTrigger;
  p->pLoadInfo = tCreateLastBlockLoadInfo(p->pSchema, NULL, 0, numOfStt);
168 169 170 171 172
  if (p->pLoadInfo == NULL) {
    tsdbCacherowsReaderClose(p);
    return TSDB_CODE_OUT_OF_MEMORY;
  }

173
  p->idstr = taosStrdup(idstr);
174 175
  taosThreadMutexInit(&p->readerMutex, NULL);

176 177
  p->lastTs = INT64_MIN;

178 179 180 181
  *pReader = p;
  return TSDB_CODE_SUCCESS;
}

H
Haojun Liao 已提交
182
void* tsdbCacherowsReaderClose(void* pReader) {
183
  SCacheRowsReader* p = pReader;
184

185 186 187 188 189 190 191
  if (p->pSchema != NULL) {
    for (int32_t i = 0; i < p->pSchema->numOfCols; ++i) {
      taosMemoryFreeClear(p->transferBuf[i]);
    }

    taosMemoryFree(p->transferBuf);
    taosMemoryFree(p->pSchema);
192 193
  }

194 195
  taosMemoryFree(p->pCurrSchema);

196 197
  destroyLastBlockLoadInfo(p->pLoadInfo);

198
  taosMemoryFree((void*)p->idstr);
199 200
  taosThreadMutexDestroy(&p->readerMutex);

201
  taosMemoryFree(pReader);
H
Haojun Liao 已提交
202
  return NULL;
203
}
204

205
static void freeItem(void* pItem) {
206
  SLastCol* pCol = (SLastCol*)pItem;
207 208 209 210 211
  if (IS_VAR_DATA_TYPE(pCol->colVal.type)) {
    taosMemoryFree(pCol->colVal.value.pData);
  }
}

212
static int32_t tsdbCacheQueryReseek(void* pQHandle) {
213 214 215
  int32_t           code = 0;
  SCacheRowsReader* pReader = pQHandle;

216 217 218 219
  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
220

221
    code = TSDB_CODE_VND_QUERY_BUSY;
222

223 224 225 226 227 228 229 230
    taosThreadMutexUnlock(&pReader->readerMutex);

    return code;
  } else if (code == EBUSY) {
    return TSDB_CODE_VND_QUERY_BUSY;
  } else {
    return -1;
  }
231 232
}

233 234
int32_t tsdbRetrieveCacheRows(void* pReader, SSDataBlock* pResBlock, const int32_t* slotIds, const int32_t* dstSlotIds,
                              SArray* pTableUidList) {
235
  if (pReader == NULL || pResBlock == NULL) {
236 237 238
    return TSDB_CODE_INVALID_PARA;
  }

239
  SCacheRowsReader* pr = pReader;
M
Minglei Jin 已提交
240 241 242 243
  int32_t           code = TSDB_CODE_SUCCESS;
  SArray*           pRow = taosArrayInit(TARRAY_SIZE(pr->pCidList), sizeof(SLastCol));
  bool              hasRes = false;
  SArray*           pLastCols = NULL;
244

245
  void** pRes = taosMemoryCalloc(pr->numOfCols, POINTER_BYTES);
246 247 248 249 250
  if (pRes == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _end;
  }

251
  for (int32_t j = 0; j < pr->numOfCols; ++j) {
M
Minglei Jin 已提交
252 253 254
    pRes[j] =
        taosMemoryCalloc(1, sizeof(SFirstLastRes) + pr->pSchema->columns[/*-1 == slotIds[j] ? 0 : */ slotIds[j]].bytes +
                                VARSTR_HEADER_SIZE);
255 256 257 258
    SFirstLastRes* p = (SFirstLastRes*)varDataVal(pRes[j]);
    p->ts = INT64_MIN;
  }

259
  pLastCols = taosArrayInit(pr->numOfCols, sizeof(SLastCol));
260 261 262 263 264
  if (pLastCols == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _end;
  }

265
  for (int32_t i = 0; i < pr->pSchema->numOfCols; ++i) {
266
    struct STColumn* pCol = &pr->pSchema->columns[i];
267
    SLastCol         p = {.ts = INT64_MIN, .colVal.type = pCol->type, .colVal.flag = CV_FLAG_NULL};
268 269 270 271 272

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

275
  taosThreadMutexLock(&pr->readerMutex);
X
Xiaoyu Wang 已提交
276
  code = tsdbTakeReadSnap((STsdbReader*)pr, tsdbCacheQueryReseek, &pr->pReadSnap);
K
kailixu 已提交
277 278 279
  if (code != TSDB_CODE_SUCCESS) {
    goto _end;
  }
280
  pr->pDataFReader = NULL;
281
  pr->pDataFReaderLast = NULL;
282

283 284
  char const* lstring = pr->type & CACHESCAN_RETRIEVE_LAST ? "last" : "last_row";

285
  // retrieve the only one last row of all tables in the uid list.
H
Haojun Liao 已提交
286
  if (HASTYPE(pr->type, CACHESCAN_RETRIEVE_TYPE_SINGLE)) {
D
dapan1121 已提交
287 288
    int64_t st = taosGetTimestampUs();
    int64_t totalLastTs = INT64_MAX;
289 290
    for (int32_t i = 0; i < pr->numOfTables; ++i) {
      STableKeyInfo* pKeyInfo = &pr->pTableList[i];
291

M
Minglei Jin 已提交
292
      tsdbCacheGet(pr->pTsdb, pKeyInfo->uid, pRow, pr, lstring);
293
      if (TARRAY_SIZE(pRow) <= 0) {
294
        taosArrayClear(pRow);
295 296 297
        continue;
      }

H
Haojun Liao 已提交
298
      {
299
        bool    hasNotNullRow = true;
D
dapan1121 已提交
300
        int64_t singleTableLastTs = INT64_MAX;
H
Haojun Liao 已提交
301
        for (int32_t k = 0; k < pr->numOfCols; ++k) {
302 303
          SLastCol* p = taosArrayGet(pLastCols, k);
          SLastCol* pColVal = (SLastCol*)taosArrayGet(pRow, k);
304

305 306 307 308
          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;
309
              }
310 311 312 313 314 315 316 317
              continue;
            }

            hasRes = true;
            p->ts = pColVal->ts;
            if (pColVal->ts < singleTableLastTs && HASTYPE(pr->type, CACHESCAN_RETRIEVE_LAST)) {
              singleTableLastTs = pColVal->ts;
            }
H
Haojun Liao 已提交
318

319 320 321 322 323
            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 已提交
324
              }
325 326 327 328 329

              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 已提交
330 331
            }
          }
332
        }
333 334

        if (hasNotNullRow) {
D
dapan1121 已提交
335 336 337 338 339 340 341
          if (INT64_MAX == totalLastTs || (INT64_MAX != singleTableLastTs && totalLastTs < singleTableLastTs)) {
            totalLastTs = singleTableLastTs;
          }
          double cost = (taosGetTimestampUs() - st) / 1000.0;
          if (cost > tsCacheLazyLoadThreshold) {
            pr->lastTs = totalLastTs;
          }
342
        }
343
      }
344

345 346 347 348
      if (taosArrayGetSize(pTableUidList) == 0) {
        taosArrayPush(pTableUidList, &pKeyInfo->uid);
      } else {
        taosArraySet(pTableUidList, 0, &pKeyInfo->uid);
349 350
      }

M
Minglei Jin 已提交
351
      taosArrayClear(pRow);
352
    }
353 354

    if (hasRes) {
355
      saveOneRow(pLastCols, pResBlock, pr, slotIds, dstSlotIds, pRes, pr->idstr);
356
    }
H
Haojun Liao 已提交
357
  } else if (HASTYPE(pr->type, CACHESCAN_RETRIEVE_TYPE_ALL)) {
358 359
    for (int32_t i = pr->tableIndex; i < pr->numOfTables; ++i) {
      STableKeyInfo* pKeyInfo = &pr->pTableList[i];
360

M
Minglei Jin 已提交
361
      tsdbCacheGet(pr->pTsdb, pKeyInfo->uid, pRow, pr, lstring);
362
      if (TARRAY_SIZE(pRow) <= 0) {
363
        taosArrayClear(pRow);
364 365
        continue;
      }
366 367

      saveOneRow(pRow, pResBlock, pr, slotIds, dstSlotIds, pRes, pr->idstr);
M
Minglei Jin 已提交
368
      taosArrayClear(pRow);
369

370
      taosArrayPush(pTableUidList, &pKeyInfo->uid);
371

372 373
      pr->tableIndex += 1;
      if (pResBlock->info.rows >= pResBlock->info.capacity) {
374
        goto _end;
375
      }
376 377
    }
  } else {
378
    code = TSDB_CODE_INVALID_PARA;
379 380
  }

381
_end:
382
  tsdbDataFReaderClose(&pr->pDataFReaderLast);
383 384
  tsdbDataFReaderClose(&pr->pDataFReader);

385
  resetLastBlockLoadInfo(pr->pLoadInfo);
386
  tsdbUntakeReadSnap((STsdbReader*)pr, pr->pReadSnap, true);
387
  taosThreadMutexUnlock(&pr->readerMutex);
388

H
Haojun Liao 已提交
389 390 391 392
  if (pRes != NULL) {
    for (int32_t j = 0; j < pr->numOfCols; ++j) {
      taosMemoryFree(pRes[j]);
    }
393 394 395
  }

  taosMemoryFree(pRes);
M
Minglei Jin 已提交
396
  taosArrayDestroy(pRow);
397
  taosArrayDestroyEx(pLastCols, freeItem);
398
  return code;
399
}