tstreamFileState.c 16.7 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 "query.h"
dengyihao's avatar
dengyihao 已提交
19
#include "storageapi.h"
dengyihao's avatar
dengyihao 已提交
20
#include "streamBackendRocksdb.h"
5
54liuyao 已提交
21
#include "taos.h"
dengyihao's avatar
dengyihao 已提交
22
#include "tcommon.h"
5
54liuyao 已提交
23 24 25
#include "thash.h"
#include "tsimplehash.h"

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

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

typedef SRowBuffPos SRowBuffInfo;

dengyihao's avatar
dengyihao 已提交
51
SStreamFileState* streamFileStateInit(int64_t memSize, uint32_t keySize, uint32_t rowSize, uint32_t selectRowSize,
52
                                      GetTsFun fp, void* pFile, TSKEY delMark, const char* idstr) {
5
54liuyao 已提交
53 54 55 56 57 58 59 60 61 62 63
  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 已提交
64
  rowSize += selectRowSize;
dengyihao's avatar
dengyihao 已提交
65
  pFileState->maxRowCount = TMAX((uint64_t)memSize / rowSize, FLUSH_NUM * 2);
5
54liuyao 已提交
66 67 68
  pFileState->usedBuffs = tdListNew(POINTER_BYTES);
  pFileState->freeBuffs = tdListNew(POINTER_BYTES);
  _hash_fn_t hashFn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY);
dengyihao's avatar
dengyihao 已提交
69
  int32_t    cap = TMIN(10240, pFileState->maxRowCount);
L
liuyao 已提交
70
  pFileState->rowBuffMap = tSimpleHashInit(cap, hashFn);
5
54liuyao 已提交
71 72 73
  if (!pFileState->usedBuffs || !pFileState->freeBuffs || !pFileState->rowBuffMap) {
    goto _error;
  }
74

L
liuyao 已提交
75
  pFileState->keyLen = keySize;
5
54liuyao 已提交
76
  pFileState->rowSize = rowSize;
L
liuyao 已提交
77
  pFileState->selectivityRowSize = selectRowSize;
5
54liuyao 已提交
78 79 80
  pFileState->preCheckPointVersion = 0;
  pFileState->checkPointVersion = 1;
  pFileState->pFileStore = pFile;
5
54liuyao 已提交
81
  pFileState->getTs = fp;
5
54liuyao 已提交
82
  pFileState->curRowCount = 0;
5
54liuyao 已提交
83
  pFileState->deleteMark = delMark;
L
add ini  
liuyao 已提交
84 85
  pFileState->flushMark = INT64_MIN;
  pFileState->maxTs = INT64_MIN;
86 87
  pFileState->id = taosStrdup(idstr);

L
liuyao 已提交
88
  recoverSnapshot(pFileState);
5
54liuyao 已提交
89 90 91
  return pFileState;

_error:
5
54liuyao 已提交
92
  streamFileStateDestroy(pFileState);
5
54liuyao 已提交
93 94 95 96
  return NULL;
}

void destroyRowBuffPos(SRowBuffPos* pPos) {
5
54liuyao 已提交
97
  taosMemoryFreeClear(pPos->pKey);
5
54liuyao 已提交
98 99 100 101 102 103 104 105
  taosMemoryFreeClear(pPos->pRowBuff);
  taosMemoryFree(pPos);
}

void destroyRowBuffPosPtr(void* ptr) {
  if (!ptr) {
    return;
  }
5
54liuyao 已提交
106
  SRowBuffPos* pPos = *(SRowBuffPos**)ptr;
L
liuyao 已提交
107 108 109
  if (!pPos->beUsed) {
    destroyRowBuffPos(pPos);
  }
5
54liuyao 已提交
110 111
}

L
liuyao 已提交
112 113 114 115 116 117 118 119
void destroyRowBuffAllPosPtr(void* ptr) {
  if (!ptr) {
    return;
  }
  SRowBuffPos* pPos = *(SRowBuffPos**)ptr;
  destroyRowBuffPos(pPos);
}

5
54liuyao 已提交
120 121 122 123 124 125 126 127 128 129 130
void destroyRowBuff(void* ptr) {
  if (!ptr) {
    return;
  }
  taosMemoryFree(*(void**)ptr);
}

void streamFileStateDestroy(SStreamFileState* pFileState) {
  if (!pFileState) {
    return;
  }
131 132

  taosMemoryFree(pFileState->id);
L
liuyao 已提交
133
  tdListFreeP(pFileState->usedBuffs, destroyRowBuffAllPosPtr);
5
54liuyao 已提交
134
  tdListFreeP(pFileState->freeBuffs, destroyRowBuff);
5
54liuyao 已提交
135
  tSimpleHashCleanup(pFileState->rowBuffMap);
5
54liuyao 已提交
136
  taosMemoryFree(pFileState);
5
54liuyao 已提交
137 138
}

5
54liuyao 已提交
139
void clearExpiredRowBuff(SStreamFileState* pFileState, TSKEY ts, bool all) {
5
54liuyao 已提交
140 141 142 143 144
  SListIter iter = {0};
  tdListInitIter(pFileState->usedBuffs, &iter, TD_LIST_FORWARD);

  SListNode* pNode = NULL;
  while ((pNode = tdListNext(&iter)) != NULL) {
5
54liuyao 已提交
145
    SRowBuffPos* pPos = *(SRowBuffPos**)(pNode->data);
L
liuyao 已提交
146
    if (all || (pFileState->getTs(pPos->pKey) < ts && !pPos->beUsed)) {
L
liuyao 已提交
147
      ASSERT(pPos->pRowBuff != NULL);
5
54liuyao 已提交
148
      tdListAppend(pFileState->freeBuffs, &(pPos->pRowBuff));
5
54liuyao 已提交
149
      pPos->pRowBuff = NULL;
L
liuyao 已提交
150 151 152
      if (!all) {
        tSimpleHashRemove(pFileState->rowBuffMap, pPos->pKey, pFileState->keyLen);
      }
5
54liuyao 已提交
153
      destroyRowBuffPos(pPos);
L
liuyao 已提交
154 155
      tdListPopNode(pFileState->usedBuffs, pNode);
      taosMemoryFreeClear(pNode);
5
54liuyao 已提交
156 157 158 159
    }
  }
}

5
54liuyao 已提交
160
void streamFileStateClear(SStreamFileState* pFileState) {
L
add ini  
liuyao 已提交
161 162
  pFileState->flushMark = INT64_MIN;
  pFileState->maxTs = INT64_MIN;
5
54liuyao 已提交
163 164 165 166
  tSimpleHashClear(pFileState->rowBuffMap);
  clearExpiredRowBuff(pFileState, 0, true);
}

dengyihao's avatar
dengyihao 已提交
167
bool needClearDiskBuff(SStreamFileState* pFileState) { return pFileState->flushMark > 0; }
L
liuyao 已提交
168

L
liuyao 已提交
169
void popUsedBuffs(SStreamFileState* pFileState, SStreamSnapshot* pFlushList, uint64_t max, bool used) {
dengyihao's avatar
dengyihao 已提交
170
  uint64_t  i = 0;
5
54liuyao 已提交
171 172 173 174
  SListIter iter = {0};
  tdListInitIter(pFileState->usedBuffs, &iter, TD_LIST_FORWARD);

  SListNode* pNode = NULL;
L
liuyao 已提交
175
  while ((pNode = tdListNext(&iter)) != NULL && i < max) {
5
54liuyao 已提交
176
    SRowBuffPos* pPos = *(SRowBuffPos**)pNode->data;
L
liuyao 已提交
177
    if (pPos->beUsed == used) {
5
54liuyao 已提交
178
      tdListAppend(pFlushList, &pPos);
5
54liuyao 已提交
179 180
      pFileState->flushMark = TMAX(pFileState->flushMark, pFileState->getTs(pPos->pKey));
      tSimpleHashRemove(pFileState->rowBuffMap, pPos->pKey, pFileState->keyLen);
L
liuyao 已提交
181 182
      tdListPopNode(pFileState->usedBuffs, pNode);
      taosMemoryFreeClear(pNode);
5
54liuyao 已提交
183 184 185
      i++;
    }
  }
186 187

  qInfo("stream state flush %d rows to disk. is used:%d", listNEles(pFlushList), used);
L
liuyao 已提交
188 189 190 191 192 193 194
}

int32_t flushRowBuff(SStreamFileState* pFileState) {
  SStreamSnapshot* pFlushList = tdListNew(POINTER_BYTES);
  if (!pFlushList) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
195

L
liuyao 已提交
196 197 198
  uint64_t num = (uint64_t)(pFileState->curRowCount * FLUSH_RATIO);
  num = TMAX(num, FLUSH_NUM);
  popUsedBuffs(pFileState, pFlushList, num, false);
199

L
liuyao 已提交
200 201 202
  if (isListEmpty(pFlushList)) {
    popUsedBuffs(pFileState, pFlushList, num, true);
  }
203

L
liuyao 已提交
204
  flushSnapshot(pFileState, pFlushList, false);
205

L
liuyao 已提交
206 207 208 209 210 211 212 213 214
  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;
  }
215

L
liuyao 已提交
216
  tdListFreeP(pFlushList, destroyRowBuffPosPtr);
5
54liuyao 已提交
217 218 219 220
  return TSDB_CODE_SUCCESS;
}

int32_t clearRowBuff(SStreamFileState* pFileState) {
5
54liuyao 已提交
221
  clearExpiredRowBuff(pFileState, pFileState->maxTs - pFileState->deleteMark, false);
5
54liuyao 已提交
222 223 224 225 226 227
  if (isListEmpty(pFileState->freeBuffs)) {
    return flushRowBuff(pFileState);
  }
  return TSDB_CODE_SUCCESS;
}

5
54liuyao 已提交
228
void* getFreeBuff(SList* lists, int32_t buffSize) {
5
54liuyao 已提交
229 230 231 232 233
  SListNode* pNode = tdListPopHead(lists);
  if (!pNode) {
    return NULL;
  }
  void* ptr = *(void**)pNode->data;
5
54liuyao 已提交
234
  memset(ptr, 0, buffSize);
5
54liuyao 已提交
235 236 237 238 239 240
  taosMemoryFree(pNode);
  return ptr;
}

SRowBuffPos* getNewRowPos(SStreamFileState* pFileState) {
  SRowBuffPos* pPos = taosMemoryCalloc(1, sizeof(SRowBuffPos));
L
liuyao 已提交
241
  pPos->pKey = taosMemoryCalloc(1, pFileState->keyLen);
5
54liuyao 已提交
242
  void* pBuff = getFreeBuff(pFileState->freeBuffs, pFileState->rowSize);
5
54liuyao 已提交
243 244
  if (pBuff) {
    pPos->pRowBuff = pBuff;
L
liuyao 已提交
245
    goto _end;
5
54liuyao 已提交
246 247 248 249 250 251 252
  }

  if (pFileState->curRowCount < pFileState->maxRowCount) {
    pBuff = taosMemoryCalloc(1, pFileState->rowSize);
    if (pBuff) {
      pPos->pRowBuff = pBuff;
      pFileState->curRowCount++;
L
liuyao 已提交
253
      goto _end;
5
54liuyao 已提交
254 255 256 257 258
    }
  }

  int32_t code = clearRowBuff(pFileState);
  ASSERT(code == 0);
5
54liuyao 已提交
259
  pPos->pRowBuff = getFreeBuff(pFileState->freeBuffs, pFileState->rowSize);
L
liuyao 已提交
260 261 262 263

_end:
  tdListAppend(pFileState->usedBuffs, &pPos);
  ASSERT(pPos->pRowBuff != NULL);
5
54liuyao 已提交
264 265 266 267
  return pPos;
}

int32_t getRowBuff(SStreamFileState* pFileState, void* pKey, int32_t keyLen, void** pVal, int32_t* pVLen) {
5
54liuyao 已提交
268
  pFileState->maxTs = TMAX(pFileState->maxTs, pFileState->getTs(pKey));
5
54liuyao 已提交
269 270
  SRowBuffPos** pos = tSimpleHashGet(pFileState->rowBuffMap, pKey, keyLen);
  if (pos) {
L
liuyao 已提交
271 272 273
    *pVLen = pFileState->rowSize;
    *pVal = *pos;
    (*pos)->beUsed = true;
5
54liuyao 已提交
274 275 276
    return TSDB_CODE_SUCCESS;
  }
  SRowBuffPos* pNewPos = getNewRowPos(pFileState);
L
liuyao 已提交
277 278
  pNewPos->beUsed = true;
  ASSERT(pNewPos->pRowBuff);
5
54liuyao 已提交
279 280 281 282 283
  memcpy(pNewPos->pKey, pKey, keyLen);

  TSKEY ts = pFileState->getTs(pKey);
  if (ts > pFileState->maxTs - pFileState->deleteMark && ts < pFileState->flushMark) {
    int32_t len = 0;
284 285
    void*   p = NULL;
    int32_t code = streamStateGet_rocksdb(pFileState->pFileStore, pKey, &p, &len);
L
add ini  
liuyao 已提交
286
    qDebug("===stream===get %" PRId64 " from disc, res %d", ts, code);
L
liuyao 已提交
287
    if (code == TSDB_CODE_SUCCESS) {
288
      memcpy(pNewPos->pRowBuff, p, len);
L
liuyao 已提交
289
    }
290
    taosMemoryFree(p);
5
54liuyao 已提交
291 292
  }

5
54liuyao 已提交
293
  tSimpleHashPut(pFileState->rowBuffMap, pKey, keyLen, &pNewPos, POINTER_BYTES);
5
54liuyao 已提交
294 295 296 297
  if (pVal) {
    *pVLen = pFileState->rowSize;
    *pVal = pNewPos;
  }
5
54liuyao 已提交
298 299 300
  return TSDB_CODE_SUCCESS;
}

5
54liuyao 已提交
301 302 303 304 305 306 307
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 已提交
308
  if (pPos->pRowBuff) {
5
54liuyao 已提交
309 310
    (*pVal) = pPos->pRowBuff;
    return TSDB_CODE_SUCCESS;
5
54liuyao 已提交
311 312
  }

5
54liuyao 已提交
313
  pPos->pRowBuff = getFreeBuff(pFileState->freeBuffs, pFileState->rowSize);
L
liuyao 已提交
314 315 316 317 318 319
  if (!pPos->pRowBuff) {
    int32_t code = clearRowBuff(pFileState);
    ASSERT(code == 0);
    pPos->pRowBuff = getFreeBuff(pFileState->freeBuffs, pFileState->rowSize);
    ASSERT(pPos->pRowBuff);
  }
5
54liuyao 已提交
320 321

  int32_t len = 0;
dengyihao's avatar
dengyihao 已提交
322
  void*   pBuff = NULL;
L
liuyao 已提交
323 324 325
  streamStateGet_rocksdb(pFileState->pFileStore, pPos->pKey, &pBuff, &len);
  memcpy(pPos->pRowBuff, pBuff, len);
  taosMemoryFree(pBuff);
5
54liuyao 已提交
326
  (*pVal) = pPos->pRowBuff;
L
liuyao 已提交
327
  tdListPrepend(pFileState->usedBuffs, &pPos);
5
54liuyao 已提交
328 329 330 331 332 333 334 335 336
  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 已提交
337 338
}

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

SStreamSnapshot* getSnapshot(SStreamFileState* pFileState) {
dengyihao's avatar
dengyihao 已提交
342 343 344
  int64_t mark = (INT64_MIN + pFileState->deleteMark >= pFileState->maxTs) ? INT64_MIN
                                                                           : pFileState->maxTs - pFileState->deleteMark;
  clearExpiredRowBuff(pFileState, mark, false);
5
54liuyao 已提交
345 346 347
  return pFileState->usedBuffs;
}

dengyihao's avatar
dengyihao 已提交
348 349 350
void streamFileStateDecode(TSKEY* key, void* pBuff, int32_t len) { pBuff = taosDecodeFixedI64(pBuff, key); }

void streamFileStateEncode(TSKEY* key, void** pVal, int32_t* pLen) {
L
liuyao 已提交
351
  *pLen = sizeof(TSKEY);
L
liuyao 已提交
352
  (*pVal) = taosMemoryCalloc(1, *pLen);
dengyihao's avatar
dengyihao 已提交
353
  void* buff = *pVal;
dengyihao's avatar
dengyihao 已提交
354
  taosEncodeFixedI64(&buff, *key);
L
liuyao 已提交
355 356 357
}

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

dengyihao's avatar
dengyihao 已提交
362
  const int32_t BATCH_LIMIT = 256;
363 364 365 366

  int64_t    st = taosGetTimestampMs();
  int32_t    numOfElems = listNEles(pSnapshot);
  SListNode* pNode = NULL;
dengyihao's avatar
dengyihao 已提交
367

dengyihao's avatar
dengyihao 已提交
368 369 370 371 372
  int idx = streamStateGetCfIdx(pFileState->pFileStore, "state");

  int32_t len = pFileState->rowSize + sizeof(uint64_t) + sizeof(int32_t) + 1;
  char*   buf = taosMemoryCalloc(1, len);

dengyihao's avatar
dengyihao 已提交
373
  void* batch = streamStateCreateBatch();
5
54liuyao 已提交
374 375
  while ((pNode = tdListNext(&iter)) != NULL && code == TSDB_CODE_SUCCESS) {
    SRowBuffPos* pPos = *(SRowBuffPos**)pNode->data;
L
liuyao 已提交
376
    ASSERT(pPos->pRowBuff && pFileState->rowSize > 0);
377

dengyihao's avatar
dengyihao 已提交
378
    if (streamStateGetBatchSize(batch) >= BATCH_LIMIT) {
L
liuyao 已提交
379
      streamStatePutBatch_rocksdb(pFileState->pFileStore, batch);
dengyihao's avatar
dengyihao 已提交
380 381
      streamStateClearBatch(batch);
    }
dengyihao's avatar
dengyihao 已提交
382 383

    SStateKey sKey = {.key = *((SWinKey*)pPos->pKey), .opNum = ((SStreamState*)pFileState->pFileStore)->number};
dengyihao's avatar
dengyihao 已提交
384 385
    code = streamStatePutBatchOptimize(pFileState->pFileStore, idx, batch, &sKey, pPos->pRowBuff, pFileState->rowSize,
                                       0, buf);
386
    // todo handle failure
dengyihao's avatar
dengyihao 已提交
387
    memset(buf, 0, len);
H
Haojun Liao 已提交
388
//    qDebug("===stream===put %" PRId64 " to disc, res %d", sKey.key.ts, code);
L
liuyao 已提交
389
  }
dengyihao's avatar
dengyihao 已提交
390
  taosMemoryFree(buf);
H
Haojun Liao 已提交
391

dengyihao's avatar
dengyihao 已提交
392
  if (streamStateGetBatchSize(batch) > 0) {
L
liuyao 已提交
393
    streamStatePutBatch_rocksdb(pFileState->pFileStore, batch);
dengyihao's avatar
dengyihao 已提交
394
  }
H
Haojun Liao 已提交
395

dengyihao's avatar
dengyihao 已提交
396
  streamStateClearBatch(batch);
dengyihao's avatar
dengyihao 已提交
397

398 399 400 401
  int64_t elapsed = taosGetTimestampMs() - st;
  qDebug("%s flush to disk in batch model completed, rows:%d, batch size:%d, elapsed time:%"PRId64"ms", pFileState->id, numOfElems,
         BATCH_LIMIT, elapsed);

L
liuyao 已提交
402
  if (flushState) {
dengyihao's avatar
dengyihao 已提交
403 404 405 406 407 408 409
    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);
L
liuyao 已提交
410
      streamStatePutBatch(pFileState->pFileStore, "default", batch, keyBuf, valBuf, len, 0);
dengyihao's avatar
dengyihao 已提交
411 412 413 414 415 416
      taosMemoryFree(valBuf);
    }
    {
      char    keyBuf[128] = {0};
      char    valBuf[64] = {0};
      int32_t len = 0;
dengyihao's avatar
dengyihao 已提交
417
      memcpy(keyBuf, taskKey, strlen(taskKey));
dengyihao's avatar
dengyihao 已提交
418
      len = sprintf(valBuf, "%" PRId64 "", ((SStreamState*)pFileState->pFileStore)->checkPointId);
dengyihao's avatar
dengyihao 已提交
419
      code = streamStatePutBatch(pFileState->pFileStore, "default", batch, keyBuf, valBuf, len, 0);
dengyihao's avatar
dengyihao 已提交
420 421
    }
    streamStatePutBatch_rocksdb(pFileState->pFileStore, batch);
5
54liuyao 已提交
422
  }
dengyihao's avatar
dengyihao 已提交
423

424
  streamStateDestroyBatch(batch);
5
54liuyao 已提交
425 426
  return code;
}
L
liuyao 已提交
427

dengyihao's avatar
dengyihao 已提交
428 429 430 431 432 433
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 已提交
434

dengyihao's avatar
dengyihao 已提交
435 436
int32_t getSnapshotIdList(SStreamFileState* pFileState, SArray* list) {
  const char* taskKey = "streamFileState";
dengyihao's avatar
dengyihao 已提交
437
  return streamDefaultIterGet_rocksdb(pFileState->pFileStore, taskKey, NULL, list);
dengyihao's avatar
dengyihao 已提交
438
}
L
liuyao 已提交
439 440

int32_t deleteExpiredCheckPoint(SStreamFileState* pFileState, TSKEY mark) {
dengyihao's avatar
dengyihao 已提交
441 442 443 444 445 446 447
  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 已提交
448
    memcpy(buf, taskKey, strlen(taskKey));
dengyihao's avatar
dengyihao 已提交
449
    code = streamDefaultGet_rocksdb(pFileState->pFileStore, buf, &val, &len);
dengyihao's avatar
dengyihao 已提交
450
    if (code != 0 || len == 0 || val == NULL) {
dengyihao's avatar
dengyihao 已提交
451 452
      return TSDB_CODE_FAILED;
    }
dengyihao's avatar
dengyihao 已提交
453
    memcpy(buf, val, len);
Y
yihaoDeng 已提交
454 455
    buf[len] = 0;
    maxCheckPointId = atol((char*)buf);
dengyihao's avatar
dengyihao 已提交
456
    taosMemoryFree(val);
dengyihao's avatar
dengyihao 已提交
457
  }
dengyihao's avatar
dengyihao 已提交
458
  for (int64_t i = maxCheckPointId; i > 0; i--) {
dengyihao's avatar
dengyihao 已提交
459 460 461 462 463 464 465 466
    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;
    }
dengyihao's avatar
dengyihao 已提交
467
    memcpy(buf, val, len);
Y
yihaoDeng 已提交
468 469 470
    buf[len] = 0;
    taosMemoryFree(val);

dengyihao's avatar
dengyihao 已提交
471
    TSKEY ts;
Y
yihaoDeng 已提交
472
    ts = atol((char*)buf);
L
liuyao 已提交
473
    if (ts < mark) {
dengyihao's avatar
dengyihao 已提交
474
      // statekey winkey.ts < mark
dengyihao's avatar
dengyihao 已提交
475
      forceRemoveCheckpoint(pFileState, i);
dengyihao's avatar
dengyihao 已提交
476 477 478
      break;
    }
  }
L
liuyao 已提交
479 480
  return code;
}
dengyihao's avatar
dengyihao 已提交
481

L
liuyao 已提交
482 483
int32_t recoverSnapshot(SStreamFileState* pFileState) {
  int32_t code = TSDB_CODE_SUCCESS;
L
liuyao 已提交
484
  if (pFileState->maxTs != INT64_MIN) {
dengyihao's avatar
dengyihao 已提交
485 486 487 488
    int64_t mark = (INT64_MIN + pFileState->deleteMark >= pFileState->maxTs)
                       ? INT64_MIN
                       : pFileState->maxTs - pFileState->deleteMark;
    deleteExpiredCheckPoint(pFileState, mark);
L
liuyao 已提交
489
  }
dengyihao's avatar
dengyihao 已提交
490
  void*   pStVal = NULL;
L
liuyao 已提交
491 492
  int32_t len = 0;

dengyihao's avatar
dengyihao 已提交
493 494
  SWinKey          key = {.groupId = 0, .ts = 0};
  SStreamStateCur* pCur = streamStateSeekToLast_rocksdb(pFileState->pFileStore, &key);
dengyihao's avatar
dengyihao 已提交
495 496
  if (pCur == NULL) {
    return -1;
L
liuyao 已提交
497
  }
dengyihao's avatar
dengyihao 已提交
498

L
liuyao 已提交
499 500 501 502
  while (code == TSDB_CODE_SUCCESS) {
    if (pFileState->curRowCount == pFileState->maxRowCount) {
      break;
    }
dengyihao's avatar
dengyihao 已提交
503 504
    void*        pVal = NULL;
    int32_t      pVLen = 0;
L
liuyao 已提交
505
    SRowBuffPos* pNewPos = getNewRowPos(pFileState);
dengyihao's avatar
dengyihao 已提交
506
    code = streamStateGetKVByCur_rocksdb(pCur, pNewPos->pKey, (const void**)&pVal, &pVLen);
L
liuyao 已提交
507 508
    if (code != TSDB_CODE_SUCCESS || pFileState->getTs(pNewPos->pKey) < pFileState->flushMark) {
      destroyRowBuffPos(pNewPos);
L
liuyao 已提交
509 510
      SListNode* pNode = tdListPopTail(pFileState->usedBuffs);
      taosMemoryFreeClear(pNode);
L
liuyao 已提交
511 512 513
      break;
    }
    memcpy(pNewPos->pRowBuff, pVal, pVLen);
L
liuyao 已提交
514
    code = tSimpleHashPut(pFileState->rowBuffMap, pNewPos->pKey, pFileState->keyLen, &pNewPos, POINTER_BYTES);
L
liuyao 已提交
515 516 517 518 519 520
    if (code != TSDB_CODE_SUCCESS) {
      destroyRowBuffPos(pNewPos);
      break;
    }
    code = streamStateCurPrev_rocksdb(pFileState->pFileStore, pCur);
  }
dengyihao's avatar
dengyihao 已提交
521
  streamStateFreeCur(pCur);
L
liuyao 已提交
522

5
54liuyao 已提交
523
  return TSDB_CODE_SUCCESS;
L
liuyao 已提交
524 525
}

dengyihao's avatar
dengyihao 已提交
526
int32_t streamFileStateGeSelectRowSize(SStreamFileState* pFileState) { return pFileState->selectivityRowSize; }
L
liuyao 已提交
527 528 529 530 531

void streamFileStateReloadInfo(SStreamFileState* pFileState, TSKEY ts) {
  pFileState->flushMark = TMAX(pFileState->flushMark, ts);
  pFileState->maxTs = TMAX(pFileState->maxTs, ts);
}