tstreamFileState.c 15.3 KB
Newer Older
5
54liuyao 已提交
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 "tstreamFileState.h"

dengyihao's avatar
dengyihao 已提交
18
#include "streamBackendRocksdb.h"
5
54liuyao 已提交
19
#include "taos.h"
dengyihao's avatar
dengyihao 已提交
20
#include "tcommon.h"
5
54liuyao 已提交
21 22 23
#include "thash.h"
#include "tsimplehash.h"

L
add ini  
liuyao 已提交
24
#define FLUSH_RATIO                    0.5
dengyihao's avatar
dengyihao 已提交
25
#define FLUSH_NUM                      4
5
54liuyao 已提交
26 27 28 29 30 31 32 33
#define DEFAULT_MAX_STREAM_BUFFER_SIZE (128 * 1024 * 1024);

struct SStreamFileState {
  SList*     usedBuffs;
  SList*     freeBuffs;
  SSHashObj* rowBuffMap;
  void*      pFileStore;
  int32_t    rowSize;
L
liuyao 已提交
34
  int32_t    selectivityRowSize;
5
54liuyao 已提交
35 36 37 38 39
  int32_t    keyLen;
  uint64_t   preCheckPointVersion;
  uint64_t   checkPointVersion;
  TSKEY      maxTs;
  TSKEY      deleteMark;
5
54liuyao 已提交
40
  TSKEY      flushMark;
5
54liuyao 已提交
41 42
  uint64_t   maxRowCount;
  uint64_t   curRowCount;
5
54liuyao 已提交
43
  GetTsFun   getTs;
5
54liuyao 已提交
44 45 46 47
};

typedef SRowBuffPos SRowBuffInfo;

L
liuyao 已提交
48
SStreamFileState* streamFileStateInit(int64_t memSize, uint32_t keySize, uint32_t rowSize, uint32_t selectRowSize, GetTsFun fp, void* pFile,
dengyihao's avatar
dengyihao 已提交
49
                                      TSKEY delMark) {
5
54liuyao 已提交
50 51 52 53 54 55 56 57 58 59 60
  if (memSize <= 0) {
    memSize = DEFAULT_MAX_STREAM_BUFFER_SIZE;
  }
  if (rowSize == 0) {
    goto _error;
  }

  SStreamFileState* pFileState = taosMemoryCalloc(1, sizeof(SStreamFileState));
  if (!pFileState) {
    goto _error;
  }
L
liuyao 已提交
61
  rowSize += selectRowSize;
dengyihao's avatar
dengyihao 已提交
62
  pFileState->maxRowCount = TMAX((uint64_t)memSize / rowSize, FLUSH_NUM * 2);
5
54liuyao 已提交
63 64 65
  pFileState->usedBuffs = tdListNew(POINTER_BYTES);
  pFileState->freeBuffs = tdListNew(POINTER_BYTES);
  _hash_fn_t hashFn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY);
dengyihao's avatar
dengyihao 已提交
66
  int32_t    cap = TMIN(10240, pFileState->maxRowCount);
L
liuyao 已提交
67
  pFileState->rowBuffMap = tSimpleHashInit(cap, hashFn);
5
54liuyao 已提交
68 69 70
  if (!pFileState->usedBuffs || !pFileState->freeBuffs || !pFileState->rowBuffMap) {
    goto _error;
  }
L
liuyao 已提交
71
  pFileState->keyLen = keySize;
5
54liuyao 已提交
72
  pFileState->rowSize = rowSize;
L
liuyao 已提交
73
  pFileState->selectivityRowSize = selectRowSize;
5
54liuyao 已提交
74 75 76
  pFileState->preCheckPointVersion = 0;
  pFileState->checkPointVersion = 1;
  pFileState->pFileStore = pFile;
5
54liuyao 已提交
77
  pFileState->getTs = fp;
5
54liuyao 已提交
78
  pFileState->curRowCount = 0;
5
54liuyao 已提交
79
  pFileState->deleteMark = delMark;
L
add ini  
liuyao 已提交
80 81
  pFileState->flushMark = INT64_MIN;
  pFileState->maxTs = INT64_MIN;
L
liuyao 已提交
82
  recoverSnapshot(pFileState);
5
54liuyao 已提交
83 84 85
  return pFileState;

_error:
5
54liuyao 已提交
86
  streamFileStateDestroy(pFileState);
5
54liuyao 已提交
87 88 89 90
  return NULL;
}

void destroyRowBuffPos(SRowBuffPos* pPos) {
5
54liuyao 已提交
91
  taosMemoryFreeClear(pPos->pKey);
5
54liuyao 已提交
92 93 94 95 96 97 98 99
  taosMemoryFreeClear(pPos->pRowBuff);
  taosMemoryFree(pPos);
}

void destroyRowBuffPosPtr(void* ptr) {
  if (!ptr) {
    return;
  }
5
54liuyao 已提交
100
  SRowBuffPos* pPos = *(SRowBuffPos**)ptr;
L
liuyao 已提交
101 102 103
  if (!pPos->beUsed) {
    destroyRowBuffPos(pPos);
  }
5
54liuyao 已提交
104 105
}

L
liuyao 已提交
106 107 108 109 110 111 112 113
void destroyRowBuffAllPosPtr(void* ptr) {
  if (!ptr) {
    return;
  }
  SRowBuffPos* pPos = *(SRowBuffPos**)ptr;
  destroyRowBuffPos(pPos);
}

5
54liuyao 已提交
114 115 116 117 118 119 120 121 122 123 124
void destroyRowBuff(void* ptr) {
  if (!ptr) {
    return;
  }
  taosMemoryFree(*(void**)ptr);
}

void streamFileStateDestroy(SStreamFileState* pFileState) {
  if (!pFileState) {
    return;
  }
L
liuyao 已提交
125
  tdListFreeP(pFileState->usedBuffs, destroyRowBuffAllPosPtr);
5
54liuyao 已提交
126
  tdListFreeP(pFileState->freeBuffs, destroyRowBuff);
5
54liuyao 已提交
127
  tSimpleHashCleanup(pFileState->rowBuffMap);
5
54liuyao 已提交
128
  taosMemoryFree(pFileState);
5
54liuyao 已提交
129 130
}

5
54liuyao 已提交
131
void clearExpiredRowBuff(SStreamFileState* pFileState, TSKEY ts, bool all) {
5
54liuyao 已提交
132 133 134 135 136
  SListIter iter = {0};
  tdListInitIter(pFileState->usedBuffs, &iter, TD_LIST_FORWARD);

  SListNode* pNode = NULL;
  while ((pNode = tdListNext(&iter)) != NULL) {
5
54liuyao 已提交
137
    SRowBuffPos* pPos = *(SRowBuffPos**)(pNode->data);
dengyihao's avatar
dengyihao 已提交
138
    if (all || (pFileState->getTs(pPos->pKey) < ts)) {
L
liuyao 已提交
139
      ASSERT(pPos->pRowBuff != NULL);
5
54liuyao 已提交
140
      tdListAppend(pFileState->freeBuffs, &(pPos->pRowBuff));
5
54liuyao 已提交
141
      pPos->pRowBuff = NULL;
L
liuyao 已提交
142 143 144
      if (!all) {
        tSimpleHashRemove(pFileState->rowBuffMap, pPos->pKey, pFileState->keyLen);
      }
5
54liuyao 已提交
145
      destroyRowBuffPos(pPos);
L
liuyao 已提交
146 147
      tdListPopNode(pFileState->usedBuffs, pNode);
      taosMemoryFreeClear(pNode);
5
54liuyao 已提交
148 149 150 151
    }
  }
}

5
54liuyao 已提交
152
void streamFileStateClear(SStreamFileState* pFileState) {
L
add ini  
liuyao 已提交
153 154
  pFileState->flushMark = INT64_MIN;
  pFileState->maxTs = INT64_MIN;
5
54liuyao 已提交
155 156 157 158
  tSimpleHashClear(pFileState->rowBuffMap);
  clearExpiredRowBuff(pFileState, 0, true);
}

L
liuyao 已提交
159 160 161 162
bool needClearDiskBuff(SStreamFileState* pFileState) {
  return pFileState->flushMark > 0;
}

L
liuyao 已提交
163
void popUsedBuffs(SStreamFileState* pFileState, SStreamSnapshot* pFlushList, uint64_t max, bool used) {
dengyihao's avatar
dengyihao 已提交
164
  uint64_t  i = 0;
5
54liuyao 已提交
165 166 167 168
  SListIter iter = {0};
  tdListInitIter(pFileState->usedBuffs, &iter, TD_LIST_FORWARD);

  SListNode* pNode = NULL;
L
liuyao 已提交
169
  while ((pNode = tdListNext(&iter)) != NULL && i < max) {
5
54liuyao 已提交
170
    SRowBuffPos* pPos = *(SRowBuffPos**)pNode->data;
L
liuyao 已提交
171
    if (pPos->beUsed == used) {
5
54liuyao 已提交
172
      tdListAppend(pFlushList, &pPos);
5
54liuyao 已提交
173 174
      pFileState->flushMark = TMAX(pFileState->flushMark, pFileState->getTs(pPos->pKey));
      tSimpleHashRemove(pFileState->rowBuffMap, pPos->pKey, pFileState->keyLen);
L
liuyao 已提交
175 176
      tdListPopNode(pFileState->usedBuffs, pNode);
      taosMemoryFreeClear(pNode);
5
54liuyao 已提交
177 178 179
      i++;
    }
  }
L
liuyao 已提交
180
  qInfo("do stream state flush %d rows to disck. is used: %d", listNEles(pFlushList), used);
L
liuyao 已提交
181 182 183 184 185 186 187 188 189 190 191 192 193
}

int32_t flushRowBuff(SStreamFileState* pFileState) {
  SStreamSnapshot* pFlushList = tdListNew(POINTER_BYTES);
  if (!pFlushList) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
  uint64_t num = (uint64_t)(pFileState->curRowCount * FLUSH_RATIO);
  num = TMAX(num, FLUSH_NUM);
  popUsedBuffs(pFileState, pFlushList, num, false);
  if (isListEmpty(pFlushList)) {
    popUsedBuffs(pFileState, pFlushList, num, true);
  }
L
liuyao 已提交
194
  flushSnapshot(pFileState, pFlushList, false);
L
liuyao 已提交
195 196 197 198 199 200 201 202 203 204
  SListIter fIter = {0};
  tdListInitIter(pFlushList, &fIter, TD_LIST_FORWARD);
  SListNode* pNode = NULL;
  while ((pNode = tdListNext(&fIter)) != NULL) {
    SRowBuffPos* pPos = *(SRowBuffPos**)pNode->data;
    ASSERT(pPos->pRowBuff != NULL);
    tdListAppend(pFileState->freeBuffs, &pPos->pRowBuff);
    pPos->pRowBuff = NULL;
  }
  tdListFreeP(pFlushList, destroyRowBuffPosPtr);
5
54liuyao 已提交
205 206 207 208
  return TSDB_CODE_SUCCESS;
}

int32_t clearRowBuff(SStreamFileState* pFileState) {
5
54liuyao 已提交
209
  clearExpiredRowBuff(pFileState, pFileState->maxTs - pFileState->deleteMark, false);
5
54liuyao 已提交
210 211 212 213 214 215
  if (isListEmpty(pFileState->freeBuffs)) {
    return flushRowBuff(pFileState);
  }
  return TSDB_CODE_SUCCESS;
}

5
54liuyao 已提交
216
void* getFreeBuff(SList* lists, int32_t buffSize) {
5
54liuyao 已提交
217 218 219 220 221
  SListNode* pNode = tdListPopHead(lists);
  if (!pNode) {
    return NULL;
  }
  void* ptr = *(void**)pNode->data;
5
54liuyao 已提交
222
  memset(ptr, 0, buffSize);
5
54liuyao 已提交
223 224 225 226 227 228
  taosMemoryFree(pNode);
  return ptr;
}

SRowBuffPos* getNewRowPos(SStreamFileState* pFileState) {
  SRowBuffPos* pPos = taosMemoryCalloc(1, sizeof(SRowBuffPos));
L
liuyao 已提交
229
  pPos->pKey = taosMemoryCalloc(1, pFileState->keyLen);
5
54liuyao 已提交
230
  void* pBuff = getFreeBuff(pFileState->freeBuffs, pFileState->rowSize);
5
54liuyao 已提交
231 232
  if (pBuff) {
    pPos->pRowBuff = pBuff;
L
liuyao 已提交
233
    goto _end;
5
54liuyao 已提交
234 235 236 237 238 239 240
  }

  if (pFileState->curRowCount < pFileState->maxRowCount) {
    pBuff = taosMemoryCalloc(1, pFileState->rowSize);
    if (pBuff) {
      pPos->pRowBuff = pBuff;
      pFileState->curRowCount++;
L
liuyao 已提交
241
      goto _end;
5
54liuyao 已提交
242 243 244 245 246
    }
  }

  int32_t code = clearRowBuff(pFileState);
  ASSERT(code == 0);
5
54liuyao 已提交
247
  pPos->pRowBuff = getFreeBuff(pFileState->freeBuffs, pFileState->rowSize);
L
liuyao 已提交
248 249 250 251

_end:
  tdListAppend(pFileState->usedBuffs, &pPos);
  ASSERT(pPos->pRowBuff != NULL);
5
54liuyao 已提交
252 253 254 255
  return pPos;
}

int32_t getRowBuff(SStreamFileState* pFileState, void* pKey, int32_t keyLen, void** pVal, int32_t* pVLen) {
5
54liuyao 已提交
256
  pFileState->maxTs = TMAX(pFileState->maxTs, pFileState->getTs(pKey));
5
54liuyao 已提交
257 258
  SRowBuffPos** pos = tSimpleHashGet(pFileState->rowBuffMap, pKey, keyLen);
  if (pos) {
L
liuyao 已提交
259 260 261
    *pVLen = pFileState->rowSize;
    *pVal = *pos;
    (*pos)->beUsed = true;
5
54liuyao 已提交
262 263 264
    return TSDB_CODE_SUCCESS;
  }
  SRowBuffPos* pNewPos = getNewRowPos(pFileState);
L
liuyao 已提交
265 266
  pNewPos->beUsed = true;
  ASSERT(pNewPos->pRowBuff);
5
54liuyao 已提交
267 268 269 270 271
  memcpy(pNewPos->pKey, pKey, keyLen);

  TSKEY ts = pFileState->getTs(pKey);
  if (ts > pFileState->maxTs - pFileState->deleteMark && ts < pFileState->flushMark) {
    int32_t len = 0;
dengyihao's avatar
dengyihao 已提交
272
    void*   pVal = NULL;
L
liuyao 已提交
273
    int32_t code = streamStateGet_rocksdb(pFileState->pFileStore, pKey, &pVal, &len);
L
add ini  
liuyao 已提交
274
    qDebug("===stream===get %" PRId64 " from disc, res %d", ts, code);
L
liuyao 已提交
275 276 277
    if (code == TSDB_CODE_SUCCESS) {
      memcpy(pNewPos->pRowBuff, pVal, len);
    }
5
54liuyao 已提交
278 279 280
    taosMemoryFree(pVal);
  }

5
54liuyao 已提交
281
  tSimpleHashPut(pFileState->rowBuffMap, pKey, keyLen, &pNewPos, POINTER_BYTES);
5
54liuyao 已提交
282 283 284 285
  if (pVal) {
    *pVLen = pFileState->rowSize;
    *pVal = pNewPos;
  }
5
54liuyao 已提交
286 287 288
  return TSDB_CODE_SUCCESS;
}

5
54liuyao 已提交
289 290 291 292 293 294 295
int32_t deleteRowBuff(SStreamFileState* pFileState, const void* pKey, int32_t keyLen) {
  int32_t code_buff = tSimpleHashRemove(pFileState->rowBuffMap, pKey, keyLen);
  int32_t code_rocks = streamStateDel_rocksdb(pFileState->pFileStore, pKey);
  return code_buff == TSDB_CODE_SUCCESS ? code_buff : code_rocks;
}

int32_t getRowBuffByPos(SStreamFileState* pFileState, SRowBuffPos* pPos, void** pVal) {
5
54liuyao 已提交
296
  if (pPos->pRowBuff) {
5
54liuyao 已提交
297 298
    (*pVal) = pPos->pRowBuff;
    return TSDB_CODE_SUCCESS;
5
54liuyao 已提交
299 300
  }

5
54liuyao 已提交
301
  pPos->pRowBuff = getFreeBuff(pFileState->freeBuffs, pFileState->rowSize);
L
liuyao 已提交
302 303 304 305 306 307
  if (!pPos->pRowBuff) {
    int32_t code = clearRowBuff(pFileState);
    ASSERT(code == 0);
    pPos->pRowBuff = getFreeBuff(pFileState->freeBuffs, pFileState->rowSize);
    ASSERT(pPos->pRowBuff);
  }
5
54liuyao 已提交
308 309

  int32_t len = 0;
dengyihao's avatar
dengyihao 已提交
310
  void*   pBuff = NULL;
L
liuyao 已提交
311 312 313
  streamStateGet_rocksdb(pFileState->pFileStore, pPos->pKey, &pBuff, &len);
  memcpy(pPos->pRowBuff, pBuff, len);
  taosMemoryFree(pBuff);
5
54liuyao 已提交
314
  (*pVal) = pPos->pRowBuff;
L
liuyao 已提交
315
  tdListPrepend(pFileState->usedBuffs, &pPos);
5
54liuyao 已提交
316 317 318 319 320 321 322 323 324
  return TSDB_CODE_SUCCESS;
}

bool hasRowBuff(SStreamFileState* pFileState, void* pKey, int32_t keyLen) {
  SRowBuffPos** pos = tSimpleHashGet(pFileState->rowBuffMap, pKey, keyLen);
  if (pos) {
    return true;
  }
  return false;
5
54liuyao 已提交
325 326
}

dengyihao's avatar
dengyihao 已提交
327
void releaseRowBuffPos(SRowBuffPos* pBuff) { pBuff->beUsed = false; }
5
54liuyao 已提交
328 329

SStreamSnapshot* getSnapshot(SStreamFileState* pFileState) {
5
54liuyao 已提交
330
  clearExpiredRowBuff(pFileState, pFileState->maxTs - pFileState->deleteMark, false);
5
54liuyao 已提交
331 332 333
  return pFileState->usedBuffs;
}

dengyihao's avatar
dengyihao 已提交
334 335 336
void streamFileStateDecode(TSKEY* key, void* pBuff, int32_t len) { pBuff = taosDecodeFixedI64(pBuff, key); }

void streamFileStateEncode(TSKEY* key, void** pVal, int32_t* pLen) {
L
liuyao 已提交
337
  *pLen = sizeof(TSKEY);
L
liuyao 已提交
338
  (*pVal) = taosMemoryCalloc(1, *pLen);
dengyihao's avatar
dengyihao 已提交
339
  void* buff = *pVal;
dengyihao's avatar
dengyihao 已提交
340
  taosEncodeFixedI64(&buff, *key);
L
liuyao 已提交
341 342 343
}

int32_t flushSnapshot(SStreamFileState* pFileState, SStreamSnapshot* pSnapshot, bool flushState) {
dengyihao's avatar
dengyihao 已提交
344
  int32_t   code = TSDB_CODE_SUCCESS;
5
54liuyao 已提交
345 346 347
  SListIter iter = {0};
  tdListInitIter(pSnapshot, &iter, TD_LIST_FORWARD);

dengyihao's avatar
dengyihao 已提交
348
  const int32_t BATCH_LIMIT = 256;
dengyihao's avatar
dengyihao 已提交
349 350 351
  SListNode*    pNode = NULL;

  void* batch = streamStateCreateBatch();
5
54liuyao 已提交
352 353
  while ((pNode = tdListNext(&iter)) != NULL && code == TSDB_CODE_SUCCESS) {
    SRowBuffPos* pPos = *(SRowBuffPos**)pNode->data;
L
liuyao 已提交
354
    ASSERT(pPos->pRowBuff && pFileState->rowSize > 0);
dengyihao's avatar
dengyihao 已提交
355 356 357 358
    if (streamStateGetBatchSize(batch) >= BATCH_LIMIT) {
      code = streamStatePutBatch_rocksdb(pFileState->pFileStore, batch);
      streamStateClearBatch(batch);
    }
dengyihao's avatar
dengyihao 已提交
359 360

    SStateKey sKey = {.key = *((SWinKey*)pPos->pKey), .opNum = ((SStreamState*)pFileState->pFileStore)->number};
dengyihao's avatar
dengyihao 已提交
361
    code = streamStatePutBatch(pFileState->pFileStore, "state", batch, &sKey, pPos->pRowBuff, pFileState->rowSize);
L
add ini  
liuyao 已提交
362
    qDebug("===stream===put %" PRId64 " to disc, res %d", sKey.key.ts, code);
L
liuyao 已提交
363
  }
dengyihao's avatar
dengyihao 已提交
364 365 366
  if (streamStateGetBatchSize(batch) > 0) {
    code = streamStatePutBatch_rocksdb(pFileState->pFileStore, batch);
  }
dengyihao's avatar
dengyihao 已提交
367
  streamStateClearBatch(batch);
dengyihao's avatar
dengyihao 已提交
368

L
liuyao 已提交
369
  if (flushState) {
dengyihao's avatar
dengyihao 已提交
370 371 372 373 374 375 376
    const char* taskKey = "streamFileState";
    {
      char    keyBuf[128] = {0};
      void*   valBuf = NULL;
      int32_t len = 0;
      sprintf(keyBuf, "%s:%" PRId64 "", taskKey, ((SStreamState*)pFileState->pFileStore)->checkPointId);
      streamFileStateEncode(&pFileState->flushMark, &valBuf, &len);
dengyihao's avatar
dengyihao 已提交
377
      code = streamStatePutBatch(pFileState->pFileStore, "default", batch, keyBuf, valBuf, len);
dengyihao's avatar
dengyihao 已提交
378 379 380 381 382 383
      taosMemoryFree(valBuf);
    }
    {
      char    keyBuf[128] = {0};
      char    valBuf[64] = {0};
      int32_t len = 0;
dengyihao's avatar
dengyihao 已提交
384
      memcpy(keyBuf, taskKey, strlen(taskKey));
dengyihao's avatar
dengyihao 已提交
385 386
      len = sprintf(valBuf, "%" PRId64 "", ((SStreamState*)pFileState->pFileStore)->checkPointId);
      code = streamStatePutBatch(pFileState->pFileStore, "default", batch, keyBuf, valBuf, len);
dengyihao's avatar
dengyihao 已提交
387 388
    }
    streamStatePutBatch_rocksdb(pFileState->pFileStore, batch);
5
54liuyao 已提交
389
  }
dengyihao's avatar
dengyihao 已提交
390
  streamStateDestroyBatch(batch);
dengyihao's avatar
dengyihao 已提交
391

5
54liuyao 已提交
392 393
  return code;
}
L
liuyao 已提交
394

dengyihao's avatar
dengyihao 已提交
395 396 397 398 399 400
int32_t forceRemoveCheckpoint(SStreamFileState* pFileState, int64_t checkpointId) {
  const char* taskKey = "streamFileState";
  char        keyBuf[128] = {0};
  sprintf(keyBuf, "%s:%" PRId64 "", taskKey, checkpointId);
  return streamDefaultDel_rocksdb(pFileState->pFileStore, keyBuf);
}
L
liuyao 已提交
401

dengyihao's avatar
dengyihao 已提交
402 403
int32_t getSnapshotIdList(SStreamFileState* pFileState, SArray* list) {
  const char* taskKey = "streamFileState";
dengyihao's avatar
dengyihao 已提交
404
  return streamDefaultIterGet_rocksdb(pFileState->pFileStore, taskKey, NULL, list);
dengyihao's avatar
dengyihao 已提交
405
}
L
liuyao 已提交
406 407

int32_t deleteExpiredCheckPoint(SStreamFileState* pFileState, TSKEY mark) {
dengyihao's avatar
dengyihao 已提交
408 409 410 411 412 413 414
  int32_t     code = TSDB_CODE_SUCCESS;
  const char* taskKey = "streamFileState";
  int64_t     maxCheckPointId = 0;
  {
    char    buf[128] = {0};
    void*   val = NULL;
    int32_t len = 0;
dengyihao's avatar
dengyihao 已提交
415
    memcpy(buf, taskKey, strlen(taskKey));
dengyihao's avatar
dengyihao 已提交
416 417 418 419 420 421
    code = streamDefaultGet_rocksdb(pFileState->pFileStore, buf, &val, &len);
    if (code != 0) {
      return TSDB_CODE_FAILED;
    }
    sscanf(val, "%" PRId64 "", &maxCheckPointId);
  }
dengyihao's avatar
dengyihao 已提交
422
  for (int64_t i = maxCheckPointId; i > 0; i--) {
dengyihao's avatar
dengyihao 已提交
423 424 425 426 427 428 429 430 431 432
    char    buf[128] = {0};
    void*   val = 0;
    int32_t len = 0;
    sprintf(buf, "%s:%" PRId64 "", taskKey, i);
    code = streamDefaultGet_rocksdb(pFileState->pFileStore, buf, &val, &len);
    if (code != 0) {
      return TSDB_CODE_FAILED;
    }
    TSKEY ts;
    sscanf(val, "%" PRId64 "", &ts);
L
liuyao 已提交
433
    if (ts < mark) {
dengyihao's avatar
dengyihao 已提交
434
      // statekey winkey.ts < mark
dengyihao's avatar
dengyihao 已提交
435
      forceRemoveCheckpoint(pFileState, i);
dengyihao's avatar
dengyihao 已提交
436
      break;
dengyihao's avatar
dengyihao 已提交
437
    } else {
dengyihao's avatar
dengyihao 已提交
438 439
    }
  }
L
liuyao 已提交
440 441
  return code;
}
dengyihao's avatar
dengyihao 已提交
442

L
liuyao 已提交
443 444
int32_t recoverSnapshot(SStreamFileState* pFileState) {
  int32_t code = TSDB_CODE_SUCCESS;
L
liuyao 已提交
445 446 447
  if (pFileState->maxTs != INT64_MIN) {
    deleteExpiredCheckPoint(pFileState, pFileState->maxTs - pFileState->deleteMark);
  }
dengyihao's avatar
dengyihao 已提交
448
  void*   pStVal = NULL;
L
liuyao 已提交
449 450
  int32_t len = 0;

dengyihao's avatar
dengyihao 已提交
451 452
  SWinKey          key = {.groupId = 0, .ts = 0};
  SStreamStateCur* pCur = streamStateSeekToLast_rocksdb(pFileState->pFileStore, &key);
dengyihao's avatar
dengyihao 已提交
453 454
  if (pCur == NULL) {
    return -1;
L
liuyao 已提交
455
  }
dengyihao's avatar
dengyihao 已提交
456

L
liuyao 已提交
457 458 459 460
  while (code == TSDB_CODE_SUCCESS) {
    if (pFileState->curRowCount == pFileState->maxRowCount) {
      break;
    }
dengyihao's avatar
dengyihao 已提交
461 462
    void*        pVal = NULL;
    int32_t      pVLen = 0;
L
liuyao 已提交
463
    SRowBuffPos* pNewPos = getNewRowPos(pFileState);
dengyihao's avatar
dengyihao 已提交
464
    code = streamStateGetKVByCur_rocksdb(pCur, pNewPos->pKey, (const void**)&pVal, &pVLen);
L
liuyao 已提交
465 466
    if (code != TSDB_CODE_SUCCESS || pFileState->getTs(pNewPos->pKey) < pFileState->flushMark) {
      destroyRowBuffPos(pNewPos);
L
liuyao 已提交
467 468
      SListNode* pNode = tdListPopTail(pFileState->usedBuffs);
      taosMemoryFreeClear(pNode);
L
liuyao 已提交
469 470 471 472 473 474 475 476 477 478
      break;
    }
    memcpy(pNewPos->pRowBuff, pVal, pVLen);
    code = tSimpleHashPut(pFileState->rowBuffMap, pNewPos->pKey, pFileState->rowSize, &pNewPos, POINTER_BYTES);
    if (code != TSDB_CODE_SUCCESS) {
      destroyRowBuffPos(pNewPos);
      break;
    }
    code = streamStateCurPrev_rocksdb(pFileState->pFileStore, pCur);
  }
dengyihao's avatar
dengyihao 已提交
479
  streamStateFreeCur(pCur);
L
liuyao 已提交
480

5
54liuyao 已提交
481
  return TSDB_CODE_SUCCESS;
L
liuyao 已提交
482 483 484 485 486
}

int32_t streamFileStateGeSelectRowSize(SStreamFileState* pFileState) {
  return pFileState->selectivityRowSize;
}