• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #3572

02 Jan 2025 08:57AM UTC coverage: 63.077% (-0.2%) from 63.276%
#3572

push

travis-ci

web-flow
Merge pull request #29450 from taosdata/fix/TS-5651-skip-sync-heartbeat

fix:[TS-5651]skip-sync-heartbeat

139525 of 284348 branches covered (49.07%)

Branch coverage included in aggregate %.

217427 of 281548 relevant lines covered (77.23%)

18571459.85 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

63.05
/source/libs/stream/src/tstreamFileState.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "tstreamFileState.h"
17

18
#include "query.h"
19
#include "streamBackendRocksdb.h"
20
#include "taos.h"
21
#include "tcommon.h"
22
#include "thash.h"
23
#include "tsimplehash.h"
24

25
#define FLUSH_RATIO                    0.5
26
#define FLUSH_NUM                      4
27
#define DEFAULT_MAX_STREAM_BUFFER_SIZE (128 * 1024 * 1024)
28
#define MIN_NUM_OF_ROW_BUFF            10240
29
#define MIN_NUM_OF_RECOVER_ROW_BUFF    128
30
#define MIN_NUM_SEARCH_BUCKET          128
31
#define MAX_ARRAY_SIZE                 1024
32
#define MAX_GROUP_ID_NUM               200000
33
#define NUM_OF_CACHE_WIN               64
34
#define MAX_NUM_OF_CACHE_WIN           128
35

36
#define TASK_KEY               "streamFileState"
37
#define STREAM_STATE_INFO_NAME "StreamStateCheckPoint"
38

39
struct SStreamFileState {
40
  SList*     usedBuffs;
41
  SList*     freeBuffs;
42
  void*      rowStateBuff;
43
  void*      pFileStore;
44
  int32_t    rowSize;
45
  int32_t    selectivityRowSize;
46
  int32_t    keyLen;
47
  uint64_t   preCheckPointVersion;
48
  uint64_t   checkPointVersion;
49
  TSKEY      maxTs;
50
  TSKEY      deleteMark;
51
  TSKEY      flushMark;
52
  uint64_t   maxRowCount;
53
  uint64_t   curRowCount;
54
  GetTsFun   getTs;
55
  char*      id;
56
  char*      cfName;
57
  void*      searchBuff;
58
  SSHashObj* pGroupIdMap;
59
  bool       hasFillCatch;
60

61
  _state_buff_cleanup_fn         stateBuffCleanupFn;
62
  _state_buff_remove_fn          stateBuffRemoveFn;
63
  _state_buff_remove_by_pos_fn   stateBuffRemoveByPosFn;
64
  _state_buff_create_statekey_fn stateBuffCreateStateKeyFn;
65

66
  _state_file_remove_fn stateFileRemoveFn;
67
  _state_file_get_fn    stateFileGetFn;
68

69
  _state_fun_get_fn stateFunctionGetFn;
70
};
71

72
typedef SRowBuffPos SRowBuffInfo;
73

74
int fillStateKeyCompare(const void* pWin1, const void* pDatas, int pos) {
9,648✔
75
  SWinKey* pWin2 = taosArrayGet(pDatas, pos);
9,648✔
76
  return winKeyCmprImpl((SWinKey*)pWin1, pWin2);
9,648✔
77
}
78

79
int32_t stateHashBuffRemoveFn(void* pBuff, const void* pKey, size_t keyLen) {
4,037✔
80
  SRowBuffPos** pos = tSimpleHashGet(pBuff, pKey, keyLen);
4,037✔
81
  if (pos) {
4,037✔
82
    (*pos)->beFlushed = true;
2,212✔
83
  }
84
  return tSimpleHashRemove(pBuff, pKey, keyLen);
4,037✔
85
}
86

87
void stateHashBuffRemoveByPosFn(SStreamFileState* pFileState, SRowBuffPos* pPos) {
6,260,315✔
88
  size_t        keyLen = pFileState->keyLen;
6,260,315✔
89
  SRowBuffPos** ppPos = tSimpleHashGet(pFileState->rowStateBuff, pPos->pKey, keyLen);
6,260,315✔
90
  if (ppPos) {
6,262,464✔
91
    if ((*ppPos) == pPos) {
6,262,416!
92
      int32_t tmpRes = tSimpleHashRemove(pFileState->rowStateBuff, pPos->pKey, keyLen);
6,262,535✔
93
      qTrace("%s at line %d res:%d", __func__, __LINE__, tmpRes);
6,268,610✔
94
    }
95
  }
96
}
6,268,649✔
97

98
void stateHashBuffClearFn(void* pBuff) { tSimpleHashClear(pBuff); }
×
99

100
void stateHashBuffCleanupFn(void* pBuff) { tSimpleHashCleanup(pBuff); }
4,337✔
101

102
int32_t intervalFileRemoveFn(SStreamFileState* pFileState, const void* pKey) {
3,689✔
103
  return streamStateDel_rocksdb(pFileState->pFileStore, pKey);
3,689✔
104
}
105

106
int32_t intervalFileGetFn(SStreamFileState* pFileState, void* pKey, void** data, int32_t* pDataLen) {
671✔
107
  return streamStateGet_rocksdb(pFileState->pFileStore, pKey, data, pDataLen);
671✔
108
}
109

110
void* intervalCreateStateKey(SRowBuffPos* pPos, int64_t num) {
1,602,837✔
111
  SStateKey* pStateKey = taosMemoryCalloc(1, sizeof(SStateKey));
1,602,837!
112
  if (pStateKey == NULL) {
1,602,752!
113
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
114
    return NULL;
×
115
  }
116
  SWinKey* pWinKey = pPos->pKey;
1,602,752✔
117
  pStateKey->key = *pWinKey;
1,602,752✔
118
  pStateKey->opNum = num;
1,602,752✔
119
  return pStateKey;
1,602,752✔
120
}
121

122
void* defaultCreateStateKey(SRowBuffPos* pPos, int64_t num) {
705✔
123
  SWinKey* pStateKey = taosMemoryCalloc(1, sizeof(SWinKey));
705!
124
  if (pStateKey == NULL) {
705!
125
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
126
    return NULL;
×
127
  }
128
  SWinKey* pWinKey = pPos->pKey;
705✔
129
  *pStateKey = *pWinKey;
705✔
130
  return pStateKey;
705✔
131
}
132

133
int32_t sessionFileRemoveFn(SStreamFileState* pFileState, const void* pKey) {
2,050✔
134
  return streamStateSessionDel_rocksdb(pFileState->pFileStore, pKey);
2,050✔
135
}
136

137
int32_t sessionFileGetFn(SStreamFileState* pFileState, void* pKey, void** data, int32_t* pDataLen) {
300✔
138
  return streamStateSessionGet_rocksdb(pFileState->pFileStore, pKey, data, pDataLen);
300✔
139
}
140

141
void* sessionCreateStateKey(SRowBuffPos* pPos, int64_t num) {
1,004✔
142
  SStateSessionKey* pStateKey = taosMemoryCalloc(1, sizeof(SStateSessionKey));
1,004!
143
  if (pStateKey == NULL) {
1,004!
144
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
145
    return NULL;
×
146
  }
147
  SSessionKey* pWinKey = pPos->pKey;
1,004✔
148
  pStateKey->key = *pWinKey;
1,004✔
149
  pStateKey->opNum = num;
1,004✔
150
  return pStateKey;
1,004✔
151
}
152

153
static void streamFileStateDecode(TSKEY* pKey, void* pBuff, int32_t len) { pBuff = taosDecodeFixedI64(pBuff, pKey); }
13!
154

155
static int32_t streamFileStateEncode(TSKEY* pKey, void** pVal, int32_t* pLen) {
1,154✔
156
  *pLen = sizeof(TSKEY);
1,154✔
157
  (*pVal) = taosMemoryCalloc(1, *pLen);
1,154!
158
  if ((*pVal) == NULL) {
1,154!
159
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
160
    return terrno;
×
161
  }
162
  void*   buff = *pVal;
1,154✔
163
  int32_t tmp = taosEncodeFixedI64(&buff, *pKey);
1,154!
164
  return TSDB_CODE_SUCCESS;
1,154✔
165
}
166

167
int32_t streamFileStateInit(int64_t memSize, uint32_t keySize, uint32_t rowSize, uint32_t selectRowSize, GetTsFun fp,
6,206✔
168
                            void* pFile, TSKEY delMark, const char* taskId, int64_t checkpointId, int8_t type,
169
                            SStreamFileState** ppFileState) {
170
  int32_t code = TSDB_CODE_SUCCESS;
6,206✔
171
  int32_t lino = 0;
6,206✔
172
  if (memSize <= 0) {
6,206!
173
    memSize = DEFAULT_MAX_STREAM_BUFFER_SIZE;
×
174
  }
175
  if (rowSize == 0) {
6,206!
176
    code = TSDB_CODE_INVALID_PARA;
×
177
    QUERY_CHECK_CODE(code, lino, _end);
×
178
  }
179

180
  SStreamFileState* pFileState = taosMemoryCalloc(1, sizeof(SStreamFileState));
6,206!
181
  QUERY_CHECK_NULL(pFileState, code, lino, _end, terrno);
6,215!
182

183
  rowSize += selectRowSize;
6,215✔
184
  pFileState->maxRowCount = TMAX((uint64_t)memSize / rowSize, FLUSH_NUM * 2);
6,215✔
185
  pFileState->usedBuffs = tdListNew(POINTER_BYTES);
6,215✔
186
  QUERY_CHECK_NULL(pFileState->usedBuffs, code, lino, _end, terrno);
6,215!
187

188
  pFileState->freeBuffs = tdListNew(POINTER_BYTES);
6,215✔
189
  QUERY_CHECK_NULL(pFileState->freeBuffs, code, lino, _end, terrno);
6,214!
190

191
  _hash_fn_t hashFn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY);
6,214✔
192
  int32_t    cap = TMIN(MIN_NUM_OF_ROW_BUFF, pFileState->maxRowCount);
6,213✔
193
  if (type == STREAM_STATE_BUFF_HASH || type == STREAM_STATE_BUFF_HASH_SEARCH) {
6,213✔
194
    pFileState->rowStateBuff = tSimpleHashInit(cap, hashFn);
4,186✔
195
    pFileState->stateBuffCleanupFn = stateHashBuffCleanupFn;
4,187✔
196
    pFileState->stateBuffRemoveFn = stateHashBuffRemoveFn;
4,187✔
197
    pFileState->stateBuffRemoveByPosFn = stateHashBuffRemoveByPosFn;
4,187✔
198
    pFileState->stateBuffCreateStateKeyFn = intervalCreateStateKey;
4,187✔
199

200
    pFileState->stateFileRemoveFn = intervalFileRemoveFn;
4,187✔
201
    pFileState->stateFileGetFn = intervalFileGetFn;
4,187✔
202
    pFileState->cfName = taosStrdup("state");
4,187!
203
    pFileState->stateFunctionGetFn = addRowBuffIfNotExist;
4,187✔
204
  } else if (type == STREAM_STATE_BUFF_SORT) {
2,027✔
205
    pFileState->rowStateBuff = tSimpleHashInit(cap, hashFn);
1,762✔
206
    pFileState->stateBuffCleanupFn = sessionWinStateCleanup;
1,763✔
207
    pFileState->stateBuffRemoveFn = deleteSessionWinStateBuffFn;
1,763✔
208
    pFileState->stateBuffRemoveByPosFn = deleteSessionWinStateBuffByPosFn;
1,763✔
209
    pFileState->stateBuffCreateStateKeyFn = sessionCreateStateKey;
1,763✔
210

211
    pFileState->stateFileRemoveFn = sessionFileRemoveFn;
1,763✔
212
    pFileState->stateFileGetFn = sessionFileGetFn;
1,763✔
213
    pFileState->cfName = taosStrdup("sess");
1,763!
214
    pFileState->stateFunctionGetFn = getSessionRowBuff;
1,763✔
215
  } else if (type == STREAM_STATE_BUFF_HASH_SORT) {
265!
216
    pFileState->rowStateBuff = tSimpleHashInit(cap, hashFn);
265✔
217
    pFileState->searchBuff = tSimpleHashInit(MIN_NUM_SEARCH_BUCKET, hashFn);
265✔
218
    QUERY_CHECK_NULL(pFileState->searchBuff, code, lino, _end, terrno);
265!
219
    pFileState->stateBuffCleanupFn = stateHashBuffCleanupFn;
265✔
220
    pFileState->stateBuffRemoveFn = stateHashBuffRemoveFn;
265✔
221
    pFileState->stateBuffRemoveByPosFn = stateHashBuffRemoveByPosFn;
265✔
222
    pFileState->stateBuffCreateStateKeyFn = defaultCreateStateKey;
265✔
223

224
    pFileState->stateFileRemoveFn = hashSortFileRemoveFn;
265✔
225
    pFileState->stateFileGetFn = hashSortFileGetFn;
265✔
226
    pFileState->cfName = taosStrdup("fill");
265!
227
    pFileState->stateFunctionGetFn = NULL;
265✔
228
  }
229

230
  QUERY_CHECK_NULL(pFileState->usedBuffs, code, lino, _end, terrno);
6,215!
231
  QUERY_CHECK_NULL(pFileState->freeBuffs, code, lino, _end, terrno);
6,215!
232
  QUERY_CHECK_NULL(pFileState->rowStateBuff, code, lino, _end, terrno);
6,215!
233
  QUERY_CHECK_NULL(pFileState->cfName, code, lino, _end, terrno);
6,215!
234

235
  if (type == STREAM_STATE_BUFF_HASH_SEARCH) {
6,215✔
236
    pFileState->searchBuff = tSimpleHashInit(MIN_NUM_SEARCH_BUCKET, hashFn);
70✔
237
    QUERY_CHECK_NULL(pFileState->searchBuff, code, lino, _end, terrno);
70!
238
  }
239

240
  pFileState->keyLen = keySize;
6,215✔
241
  pFileState->rowSize = rowSize;
6,215✔
242
  pFileState->selectivityRowSize = selectRowSize;
6,215✔
243
  pFileState->preCheckPointVersion = 0;
6,215✔
244
  pFileState->checkPointVersion = 1;
6,215✔
245
  pFileState->pFileStore = pFile;
6,215✔
246
  pFileState->getTs = fp;
6,215✔
247
  pFileState->curRowCount = 0;
6,215✔
248
  pFileState->deleteMark = delMark;
6,215✔
249
  pFileState->flushMark = INT64_MIN;
6,215✔
250
  pFileState->maxTs = INT64_MIN;
6,215✔
251
  pFileState->id = taosStrdup(taskId);
6,215!
252
  QUERY_CHECK_NULL(pFileState->id, code, lino, _end, terrno);
6,215!
253

254
  pFileState->pGroupIdMap = tSimpleHashInit(1024, hashFn);
6,215✔
255
  QUERY_CHECK_NULL(pFileState->pGroupIdMap, code, lino, _end, terrno);
6,215!
256

257
  pFileState->hasFillCatch = true;
6,215✔
258

259
  if (type == STREAM_STATE_BUFF_HASH || type == STREAM_STATE_BUFF_HASH_SEARCH) {
6,215✔
260
    code = recoverSnapshot(pFileState, checkpointId);
4,187✔
261
  } else if (type == STREAM_STATE_BUFF_SORT) {
2,028✔
262
    code = recoverSession(pFileState, checkpointId);
1,763✔
263
  } else if (type == STREAM_STATE_BUFF_HASH_SORT) {
265!
264
    code = recoverFillSnapshot(pFileState, checkpointId);
265✔
265
  }
266
  QUERY_CHECK_CODE(code, lino, _end);
6,215!
267

268
  void*   valBuf = NULL;
6,215✔
269
  int32_t len = 0;
6,215✔
270
  int32_t tmpRes = streamDefaultGet_rocksdb(pFileState->pFileStore, STREAM_STATE_INFO_NAME, &valBuf, &len);
6,215✔
271
  if (tmpRes == TSDB_CODE_SUCCESS) {
6,215✔
272
    QUERY_CHECK_CONDITION((len == sizeof(TSKEY)), code, lino, _end, TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
13!
273
    streamFileStateDecode(&pFileState->flushMark, valBuf, len);
13✔
274
    qDebug("===stream===flushMark  read:%" PRId64, pFileState->flushMark);
13✔
275
  }
276
  taosMemoryFreeClear(valBuf);
6,215!
277
  (*ppFileState) = pFileState;
6,215✔
278

279
_end:
6,215✔
280
  if (code != TSDB_CODE_SUCCESS) {
6,215!
281
    streamFileStateDestroy(pFileState);
×
282
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
283
  }
284
  return code;
6,215✔
285
}
286

287
void destroyRowBuffPos(SRowBuffPos* pPos) {
12,278,388✔
288
  taosMemoryFreeClear(pPos->pKey);
12,278,388!
289
  taosMemoryFreeClear(pPos->pRowBuff);
12,301,165!
290
  taosMemoryFree(pPos);
12,294,936!
291
}
12,277,261✔
292

293
void destroyRowBuffPosPtr(void* ptr) {
484,381✔
294
  if (!ptr) {
484,381!
295
    return;
×
296
  }
297
  SRowBuffPos* pPos = *(SRowBuffPos**)ptr;
484,381✔
298
  if (!pPos->beUsed) {
484,381✔
299
    destroyRowBuffPos(pPos);
1,465✔
300
  }
301
}
302

303
void destroyRowBuffAllPosPtr(void* ptr) {
6,232,101✔
304
  if (!ptr) {
6,232,101!
305
    return;
×
306
  }
307
  SRowBuffPos* pPos = *(SRowBuffPos**)ptr;
6,232,101✔
308
  destroyRowBuffPos(pPos);
6,232,101✔
309
}
310

311
void destroyRowBuff(void* ptr) {
5,825,001✔
312
  if (!ptr) {
5,825,001!
313
    return;
×
314
  }
315
  taosMemoryFree(*(void**)ptr);
5,825,001!
316
}
317

318
void streamFileStateDestroy(SStreamFileState* pFileState) {
13,509✔
319
  if (!pFileState) {
13,509✔
320
    return;
7,430✔
321
  }
322

323
  taosMemoryFree(pFileState->id);
6,079!
324
  taosMemoryFree(pFileState->cfName);
6,082!
325
  tdListFreeP(pFileState->usedBuffs, destroyRowBuffAllPosPtr);
6,082✔
326
  tdListFreeP(pFileState->freeBuffs, destroyRowBuff);
6,082✔
327
  pFileState->stateBuffCleanupFn(pFileState->rowStateBuff);
6,082✔
328
  sessionWinStateCleanup(pFileState->searchBuff);
6,082✔
329
  tSimpleHashCleanup(pFileState->pGroupIdMap);
6,082✔
330
  taosMemoryFree(pFileState);
6,082!
331
}
332

333
int32_t putFreeBuff(SStreamFileState* pFileState, SRowBuffPos* pPos) {
6,524,955✔
334
  int32_t code = TSDB_CODE_SUCCESS;
6,524,955✔
335
  int32_t lino = 0;
6,524,955✔
336
  if (pPos->pRowBuff) {
6,524,955✔
337
    code = tdListAppend(pFileState->freeBuffs, &(pPos->pRowBuff));
6,524,910✔
338
    QUERY_CHECK_CODE(code, lino, _end);
6,524,883!
339
    pPos->pRowBuff = NULL;
6,524,883✔
340
  }
341

342
_end:
45✔
343
  if (code != TSDB_CODE_SUCCESS) {
6,524,928!
344
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
345
  }
346
  return code;
6,524,915✔
347
}
348

349
void clearExpiredRowBuff(SStreamFileState* pFileState, TSKEY ts, bool all) {
6,386✔
350
  int32_t   code = TSDB_CODE_SUCCESS;
6,386✔
351
  int32_t   lino = 0;
6,386✔
352
  SListIter iter = {0};
6,386✔
353
  tdListInitIter(pFileState->usedBuffs, &iter, TD_LIST_FORWARD);
6,386✔
354

355
  SListNode* pNode = NULL;
6,391✔
356
  while ((pNode = tdListNext(&iter)) != NULL) {
7,168,828✔
357
    SRowBuffPos* pPos = *(SRowBuffPos**)(pNode->data);
7,162,408✔
358
    if (all || (pFileState->getTs(pPos->pKey) < ts && !pPos->beUsed)) {
7,162,408!
359
      code = putFreeBuff(pFileState, pPos);
6,039,895✔
360
      QUERY_CHECK_CODE(code, lino, _end);
6,039,847!
361

362
      if (!all) {
6,039,847✔
363
        pFileState->stateBuffRemoveByPosFn(pFileState, pPos);
5,786,532✔
364
      }
365
      destroyRowBuffPos(pPos);
6,039,967✔
366
      SListNode* tmp = tdListPopNode(pFileState->usedBuffs, pNode);
6,039,970✔
367
      taosMemoryFreeClear(tmp);
6,039,814!
368
    }
369
  }
370

371
_end:
6,387✔
372
  if (code != TSDB_CODE_SUCCESS) {
6,387!
373
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
374
  }
375
}
6,387✔
376

377
int32_t clearFlushedRowBuff(SStreamFileState* pFileState, SStreamSnapshot* pFlushList, uint64_t max, bool all) {
7,346✔
378
  int32_t   code = TSDB_CODE_SUCCESS;
7,346✔
379
  int32_t   lino = 0;
7,346✔
380
  uint64_t  i = 0;
7,346✔
381
  SListIter iter = {0};
7,346✔
382
  tdListInitIter(pFileState->usedBuffs, &iter, TD_LIST_FORWARD);
7,346✔
383

384
  SListNode* pNode = NULL;
7,346✔
385
  while ((pNode = tdListNext(&iter)) != NULL && i < max) {
977,377✔
386
    SRowBuffPos* pPos = *(SRowBuffPos**)pNode->data;
970,031✔
387
    if (isFlushedState(pFileState, pFileState->getTs(pPos->pKey), 0)) {
970,031✔
388
      if (all || !pPos->beUsed) {
6,318✔
389
        if (all && !pPos->pRowBuff) {
873!
390
          continue;
×
391
        }
392
        code = tdListAppend(pFlushList, &pPos);
873✔
393
        QUERY_CHECK_CODE(code, lino, _end);
873!
394

395
        pFileState->flushMark = TMAX(pFileState->flushMark, pFileState->getTs(pPos->pKey));
873✔
396
        pFileState->stateBuffRemoveByPosFn(pFileState, pPos);
873✔
397
        SListNode* tmp = tdListPopNode(pFileState->usedBuffs, pNode);
873✔
398
        taosMemoryFreeClear(tmp);
873!
399
        if (pPos->pRowBuff) {
873✔
400
          i++;
828✔
401
        }
402
      }
403
    }
404
  }
405

406
_end:
7,346✔
407
  if (code != TSDB_CODE_SUCCESS) {
7,346!
408
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
409
  }
410
  return code;
7,346✔
411
}
412

413
void streamFileStateClear(SStreamFileState* pFileState) {
1,836✔
414
  pFileState->flushMark = INT64_MIN;
1,836✔
415
  pFileState->maxTs = INT64_MIN;
1,836✔
416
  tSimpleHashClear(pFileState->rowStateBuff);
1,836✔
417
  clearExpiredRowBuff(pFileState, 0, true);
1,836✔
418
}
1,836✔
419

420
bool needClearDiskBuff(SStreamFileState* pFileState) { return pFileState->flushMark > 0; }
260✔
421

422
void streamFileStateReleaseBuff(SStreamFileState* pFileState, SRowBuffPos* pPos, bool used) { pPos->beUsed = used; }
11,688,909✔
423

424
int32_t popUsedBuffs(SStreamFileState* pFileState, SStreamSnapshot* pFlushList, uint64_t max, bool used) {
6,070✔
425
  int32_t   code = TSDB_CODE_SUCCESS;
6,070✔
426
  int32_t   lino = 0;
6,070✔
427
  uint64_t  i = 0;
6,070✔
428
  SListIter iter = {0};
6,070✔
429
  tdListInitIter(pFileState->usedBuffs, &iter, TD_LIST_FORWARD);
6,070✔
430

431
  SListNode* pNode = NULL;
6,069✔
432
  while ((pNode = tdListNext(&iter)) != NULL && i < max) {
1,450,605!
433
    SRowBuffPos* pPos = *(SRowBuffPos**)pNode->data;
1,441,602✔
434
    if (pPos->beUsed == used) {
1,441,602✔
435
      if (used && !pPos->pRowBuff) {
476,727!
436
        QUERY_CHECK_CONDITION((pPos->needFree == true), code, lino, _end, TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
×
437
        continue;
×
438
      }
439
      code = tdListAppend(pFlushList, &pPos);
476,727✔
440
      QUERY_CHECK_CODE(code, lino, _end);
480,734!
441

442
      pFileState->flushMark = TMAX(pFileState->flushMark, pFileState->getTs(pPos->pKey));
480,734✔
443
      pFileState->stateBuffRemoveByPosFn(pFileState, pPos);
473,465✔
444
      SListNode* tmp = tdListPopNode(pFileState->usedBuffs, pNode);
481,516✔
445
      taosMemoryFreeClear(tmp);
471,195!
446
      if (pPos->pRowBuff) {
479,661!
447
        i++;
479,887✔
448
      }
449
    }
450
  }
451

452
  qInfo("stream state flush %d rows to disk. is used:%d", listNEles(pFlushList), used);
5,588!
453

454
_end:
×
455
  if (code != TSDB_CODE_SUCCESS) {
5,590!
456
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
457
  }
458
  return code;
6,070✔
459
}
460

461
int32_t flushRowBuff(SStreamFileState* pFileState) {
3,790✔
462
  int32_t          code = TSDB_CODE_SUCCESS;
3,790✔
463
  int32_t          lino = 0;
3,790✔
464
  SStreamSnapshot* pFlushList = tdListNew(POINTER_BYTES);
3,790✔
465
  if (!pFlushList) {
3,790!
466
    code = TSDB_CODE_OUT_OF_MEMORY;
×
467
    QUERY_CHECK_CODE(code, lino, _end);
×
468
  }
469

470
  uint64_t num = (uint64_t)(pFileState->curRowCount * FLUSH_RATIO);
3,790✔
471
  num = TMAX(num, FLUSH_NUM);
3,790✔
472
  code = clearFlushedRowBuff(pFileState, pFlushList, num, false);
3,790✔
473
  QUERY_CHECK_CODE(code, lino, _end);
3,790!
474

475
  if (isListEmpty(pFlushList)) {
3,790✔
476
    code = popUsedBuffs(pFileState, pFlushList, num, false);
3,209✔
477
    QUERY_CHECK_CODE(code, lino, _end);
3,209!
478

479
    if (isListEmpty(pFlushList)) {
3,209✔
480
      code = popUsedBuffs(pFileState, pFlushList, num, true);
2,861✔
481
      QUERY_CHECK_CODE(code, lino, _end);
2,861!
482
    }
483
  }
484

485
  if (pFileState->searchBuff) {
3,790✔
486
    code = clearFlushedRowBuff(pFileState, pFlushList, pFileState->curRowCount, true);
3,556✔
487
    QUERY_CHECK_CODE(code, lino, _end);
3,556!
488
  }
489

490
  flushSnapshot(pFileState, pFlushList, false);
3,790✔
491

492
  SListIter fIter = {0};
3,790✔
493
  tdListInitIter(pFlushList, &fIter, TD_LIST_FORWARD);
3,790✔
494
  SListNode* pNode = NULL;
3,790✔
495
  while ((pNode = tdListNext(&fIter)) != NULL) {
488,171✔
496
    SRowBuffPos* pPos = *(SRowBuffPos**)pNode->data;
484,381✔
497
    code = putFreeBuff(pFileState, pPos);
484,381✔
498
    QUERY_CHECK_CODE(code, lino, _end);
484,381!
499
  }
500

501
  tdListFreeP(pFlushList, destroyRowBuffPosPtr);
3,790✔
502

503
_end:
3,790✔
504
  if (code != TSDB_CODE_SUCCESS) {
3,790!
505
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
506
  }
507
  return code;
3,790✔
508
}
509

510
int32_t clearRowBuff(SStreamFileState* pFileState) {
4,402✔
511
  if (pFileState->deleteMark != INT64_MAX) {
4,402!
512
    clearExpiredRowBuff(pFileState, pFileState->maxTs - pFileState->deleteMark, false);
×
513
  }
514
  if (isListEmpty(pFileState->freeBuffs)) {
4,402✔
515
    return flushRowBuff(pFileState);
3,790✔
516
  }
517
  return TSDB_CODE_SUCCESS;
612✔
518
}
519

520
void* getFreeBuff(SStreamFileState* pFileState) {
12,783,021✔
521
  SList*     lists = pFileState->freeBuffs;
12,783,021✔
522
  int32_t    buffSize = pFileState->rowSize;
12,783,021✔
523
  SListNode* pNode = tdListPopHead(lists);
12,783,021✔
524
  if (!pNode) {
12,777,563✔
525
    return NULL;
12,486,958✔
526
  }
527
  void* ptr = *(void**)pNode->data;
290,605✔
528
  memset(ptr, 0, buffSize);
290,605✔
529
  taosMemoryFree(pNode);
290,605!
530
  return ptr;
290,262✔
531
}
532

533
void streamFileStateClearBuff(SStreamFileState* pFileState, SRowBuffPos* pPos) {
×
534
  if (pPos->pRowBuff) {
×
535
    memset(pPos->pRowBuff, 0, pFileState->rowSize);
×
536
  }
537
}
×
538

539
SRowBuffPos* getNewRowPos(SStreamFileState* pFileState) {
12,776,497✔
540
  int32_t      code = TSDB_CODE_SUCCESS;
12,776,497✔
541
  int32_t      lino = 0;
12,776,497✔
542
  SRowBuffPos* pPos = taosMemoryCalloc(1, sizeof(SRowBuffPos));
12,776,497!
543
  if (!pPos) {
12,786,242!
544
    code = terrno;
×
545
    QUERY_CHECK_CODE(code, lino, _error);
×
546
  }
547

548
  pPos->pKey = taosMemoryCalloc(1, pFileState->keyLen);
12,786,242!
549
  if (!pPos->pKey) {
12,785,453!
550
    code = terrno;
×
551
    QUERY_CHECK_CODE(code, lino, _error);
×
552
  }
553

554
  void* pBuff = getFreeBuff(pFileState);
12,785,453✔
555
  if (pBuff) {
12,774,436✔
556
    pPos->pRowBuff = pBuff;
289,703✔
557
    goto _end;
289,703✔
558
  }
559

560
  if (pFileState->curRowCount < pFileState->maxRowCount) {
12,484,733✔
561
    pBuff = taosMemoryCalloc(1, pFileState->rowSize);
12,483,967!
562
    QUERY_CHECK_NULL(pBuff, code, lino, _error, terrno);
12,489,655!
563
    pPos->pRowBuff = pBuff;
12,489,655✔
564
    pFileState->curRowCount++;
12,489,655✔
565
    goto _end;
12,489,655✔
566
  }
567

568
  code = clearRowBuff(pFileState);
766✔
569
  QUERY_CHECK_CODE(code, lino, _error);
152!
570

571
  pPos->pRowBuff = getFreeBuff(pFileState);
152✔
572

573
_end:
12,779,510✔
574
  code = tdListAppend(pFileState->usedBuffs, &pPos);
12,779,510✔
575
  QUERY_CHECK_CODE(code, lino, _error);
12,786,821!
576

577
  QUERY_CHECK_CONDITION((pPos->pRowBuff != NULL), code, lino, _error, TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
12,786,821!
578
_error:
12,786,821✔
579
  if (code != TSDB_CODE_SUCCESS) {
12,786,821!
580
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
581
    return NULL;
×
582
  }
583

584
  return pPos;
12,786,821✔
585
}
586

587
SRowBuffPos* getNewRowPosForWrite(SStreamFileState* pFileState) {
12,778,698✔
588
  int32_t      code = TSDB_CODE_SUCCESS;
12,778,698✔
589
  int32_t      lino = 0;
12,778,698✔
590
  SRowBuffPos* newPos = getNewRowPos(pFileState);
12,778,698✔
591
  if (!newPos) {
12,782,516!
592
    code = TSDB_CODE_OUT_OF_MEMORY;
×
593
    QUERY_CHECK_CODE(code, lino, _error);
×
594
  }
595
  newPos->beUsed = true;
12,782,516✔
596
  newPos->beFlushed = false;
12,782,516✔
597
  newPos->needFree = false;
12,782,516✔
598
  newPos->beUpdated = true;
12,782,516✔
599
  return newPos;
12,782,516✔
600

601
_error:
×
602
  if (code != TSDB_CODE_SUCCESS) {
×
603
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
604
  }
605
  return NULL;
×
606
}
607

608
int32_t addRowBuffIfNotExist(SStreamFileState* pFileState, void* pKey, int32_t keyLen, void** pVal, int32_t* pVLen,
13,611,956✔
609
                             int32_t* pWinCode) {
610
  int32_t code = TSDB_CODE_SUCCESS;
13,611,956✔
611
  int32_t lino = 0;
13,611,956✔
612
  (*pWinCode) = TSDB_CODE_SUCCESS;
13,611,956✔
613
  pFileState->maxTs = TMAX(pFileState->maxTs, pFileState->getTs(pKey));
13,611,956✔
614
  SRowBuffPos** pos = tSimpleHashGet(pFileState->rowStateBuff, pKey, keyLen);
13,609,543✔
615
  if (pos) {
13,628,650✔
616
    if (pVal != NULL) {
858,735!
617
      *pVLen = pFileState->rowSize;
858,751✔
618
      *pVal = *pos;
858,751✔
619
      (*pos)->beUsed = true;
858,751✔
620
      (*pos)->beFlushed = false;
858,751✔
621
    }
622
    goto _end;
858,735✔
623
  }
624
  SRowBuffPos* pNewPos = getNewRowPosForWrite(pFileState);
12,769,915✔
625
  if (!pNewPos || !pNewPos->pRowBuff) {
12,769,947!
626
    code = TSDB_CODE_OUT_OF_MEMORY;
29✔
627
    QUERY_CHECK_CODE(code, lino, _end);
29!
628
  }
629

630
  memcpy(pNewPos->pKey, pKey, keyLen);
12,769,947✔
631
  (*pWinCode) = TSDB_CODE_FAILED;
12,769,947✔
632

633
  TSKEY ts = pFileState->getTs(pKey);
12,769,947✔
634
  if (!isDeteled(pFileState, ts) && isFlushedState(pFileState, ts, 0)) {
12,762,715✔
635
    int32_t len = 0;
1,076✔
636
    void*   p = NULL;
1,076✔
637
    (*pWinCode) = pFileState->stateFileGetFn(pFileState, pKey, &p, &len);
1,076✔
638
    qDebug("===stream===get %" PRId64 " from disc, res %d", ts, (*pWinCode));
1,076✔
639
    if ((*pWinCode) == TSDB_CODE_SUCCESS) {
1,076✔
640
      memcpy(pNewPos->pRowBuff, p, len);
543✔
641
    }
642
    taosMemoryFree(p);
1,076!
643
  }
644

645
  code = tSimpleHashPut(pFileState->rowStateBuff, pKey, keyLen, &pNewPos, POINTER_BYTES);
12,757,910✔
646
  QUERY_CHECK_CODE(code, lino, _end);
12,749,341!
647

648
  if (pVal) {
12,749,341!
649
    *pVLen = pFileState->rowSize;
12,749,488✔
650
    *pVal = pNewPos;
12,749,488✔
651
  }
652

653
_end:
×
654
  if (code != TSDB_CODE_SUCCESS) {
13,608,076!
655
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
656
  }
657
  return code;
13,604,468✔
658
}
659

660
void deleteRowBuff(SStreamFileState* pFileState, const void* pKey, int32_t keyLen) {
5,668✔
661
  int32_t code_buff = pFileState->stateBuffRemoveFn(pFileState->rowStateBuff, pKey, keyLen);
5,668✔
662
  qTrace("%s at line %d res:%d", __func__, __LINE__, code_buff);
5,668✔
663
  int32_t code_file = pFileState->stateFileRemoveFn(pFileState, pKey);
5,668✔
664
  qTrace("%s at line %d res:%d", __func__, __LINE__, code_file);
5,666✔
665
  if (pFileState->searchBuff != NULL) {
5,666✔
666
    deleteHashSortRowBuff(pFileState, pKey);
41✔
667
  }
668
}
5,666✔
669

670
static int32_t recoverSessionRowBuff(SStreamFileState* pFileState, SRowBuffPos* pPos) {
360✔
671
  int32_t code = TSDB_CODE_SUCCESS;
360✔
672
  int32_t lino = 0;
360✔
673
  int32_t len = 0;
360✔
674
  void*   pBuff = NULL;
360✔
675
  code = pFileState->stateFileGetFn(pFileState, pPos->pKey, &pBuff, &len);
360✔
676
  QUERY_CHECK_CODE(code, lino, _end);
360!
677
  memcpy(pPos->pRowBuff, pBuff, len);
360✔
678
  taosMemoryFree(pBuff);
360!
679

680
_end:
360✔
681
  if (code != TSDB_CODE_SUCCESS) {
360!
682
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
683
  }
684
  return code;
360✔
685
}
686

687
static int32_t recoverStateRowBuff(SStreamFileState* pFileState, SRowBuffPos* pPos) {
360✔
688
  int32_t code = TSDB_CODE_SUCCESS;
360✔
689
  int32_t lino = 0;
360✔
690
  pPos->pRowBuff = getFreeBuff(pFileState);
360✔
691
  if (!pPos->pRowBuff) {
360✔
692
    if (pFileState->curRowCount < pFileState->maxRowCount) {
82!
693
      pPos->pRowBuff = taosMemoryCalloc(1, pFileState->rowSize);
×
694
      if (!pPos->pRowBuff) {
×
695
        code = terrno;
×
696
        QUERY_CHECK_CODE(code, lino, _end);
×
697
      }
698
      pFileState->curRowCount++;
×
699
    } else {
700
      code = clearRowBuff(pFileState);
82✔
701
      QUERY_CHECK_CODE(code, lino, _end);
82!
702
      pPos->pRowBuff = getFreeBuff(pFileState);
82✔
703
    }
704
    QUERY_CHECK_CONDITION((pPos->pRowBuff != NULL), code, lino, _end, TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
82!
705
  }
706

707
  code = recoverSessionRowBuff(pFileState, pPos);
360✔
708
  QUERY_CHECK_CODE(code, lino, _end);
360!
709

710
_end:
360✔
711
  if (code != TSDB_CODE_SUCCESS) {
360!
712
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
713
  }
714
  return code;
360✔
715
}
716

717
int32_t getRowBuffByPos(SStreamFileState* pFileState, SRowBuffPos* pPos, void** pVal) {
11,623,716✔
718
  int32_t code = TSDB_CODE_SUCCESS;
11,623,716✔
719
  int32_t lino = 0;
11,623,716✔
720
  if (pPos->pRowBuff) {
11,623,716✔
721
    if (pPos->needFree) {
11,623,514!
722
      code = recoverSessionRowBuff(pFileState, pPos);
×
723
      QUERY_CHECK_CODE(code, lino, _end);
×
724
    }
725
    (*pVal) = pPos->pRowBuff;
11,623,514✔
726
    goto _end;
11,623,514✔
727
  }
728

729
  code = recoverStateRowBuff(pFileState, pPos);
202✔
730
  QUERY_CHECK_CODE(code, lino, _end);
360!
731

732
  (*pVal) = pPos->pRowBuff;
360✔
733
  if (!pPos->needFree) {
360✔
734
    code = tdListPrepend(pFileState->usedBuffs, &pPos);
120✔
735
    QUERY_CHECK_CODE(code, lino, _end);
120!
736
  }
737

738
_end:
360✔
739
  if (code != TSDB_CODE_SUCCESS) {
11,623,874!
740
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
741
  }
742
  return code;
11,623,600✔
743
}
744

745
bool hasRowBuff(SStreamFileState* pFileState, void* pKey, int32_t keyLen) {
28✔
746
  SRowBuffPos** pos = tSimpleHashGet(pFileState->rowStateBuff, pKey, keyLen);
28✔
747
  if (pos) {
28✔
748
    return true;
13✔
749
  }
750
  return false;
15✔
751
}
752

753
SStreamSnapshot* getSnapshot(SStreamFileState* pFileState) {
4,551✔
754
  int64_t mark = (pFileState->deleteMark == INT64_MAX || pFileState->maxTs == INT64_MIN)
1,951✔
755
                     ? INT64_MIN
756
                     : pFileState->maxTs - pFileState->deleteMark;
6,502✔
757
  clearExpiredRowBuff(pFileState, mark, false);
4,551✔
758
  return pFileState->usedBuffs;
4,555✔
759
}
760

761
void flushSnapshot(SStreamFileState* pFileState, SStreamSnapshot* pSnapshot, bool flushState) {
8,342✔
762
  int32_t   code = TSDB_CODE_SUCCESS;
8,342✔
763
  int32_t   lino = 0;
8,342✔
764
  SListIter iter = {0};
8,342✔
765
  tdListInitIter(pSnapshot, &iter, TD_LIST_FORWARD);
8,342✔
766

767
  const int32_t BATCH_LIMIT = 256;
8,345✔
768

769
  int64_t    st = taosGetTimestampMs();
8,346✔
770
  SListNode* pNode = NULL;
8,346✔
771

772
  int idx = streamStateGetCfIdx(pFileState->pFileStore, pFileState->cfName);
8,346✔
773

774
  int32_t len = (pFileState->rowSize + sizeof(uint64_t) + sizeof(int32_t) + 64) * 2;
8,347✔
775
  char*   buf = taosMemoryCalloc(1, len);
8,347!
776
  if (!buf) {
8,346!
777
    code = terrno;
×
778
    QUERY_CHECK_CODE(code, lino, _end);
×
779
  }
780

781
  void* batch = streamStateCreateBatch();
8,346✔
782
  if (!batch) {
8,346!
783
    code = TSDB_CODE_OUT_OF_MEMORY;
×
784
    QUERY_CHECK_CODE(code, lino, _end);
×
785
  }
786

787
  while ((pNode = tdListNext(&iter)) != NULL && code == TSDB_CODE_SUCCESS) {
1,615,061✔
788
    SRowBuffPos* pPos = *(SRowBuffPos**)pNode->data;
1,606,601✔
789
    if (pPos->beFlushed || !pPos->pRowBuff) {
1,606,601!
790
      continue;
2,224✔
791
    }
792
    pPos->beFlushed = true;
1,604,377✔
793
    pFileState->flushMark = TMAX(pFileState->flushMark, pFileState->getTs(pPos->pKey));
1,604,377✔
794

795
    qDebug("===stream===flushed start:%" PRId64, pFileState->getTs(pPos->pKey));
1,604,280✔
796
    if (streamStateGetBatchSize(batch) >= BATCH_LIMIT) {
1,604,280✔
797
      code = streamStatePutBatch_rocksdb(pFileState->pFileStore, batch);
6,190✔
798
      streamStateClearBatch(batch);
6,190✔
799
      QUERY_CHECK_CODE(code, lino, _end);
6,190!
800
    }
801

802
    void* pSKey = pFileState->stateBuffCreateStateKeyFn(pPos, ((SStreamState*)pFileState->pFileStore)->number);
1,604,570✔
803
    QUERY_CHECK_NULL(pSKey, code, lino, _end, terrno);
1,604,330!
804

805
    code = streamStatePutBatchOptimize(pFileState->pFileStore, idx, batch, pSKey, pPos->pRowBuff, pFileState->rowSize,
1,604,330✔
806
                                       0, buf);
807
    taosMemoryFreeClear(pSKey);
1,604,421!
808
    QUERY_CHECK_CODE(code, lino, _end);
1,604,491!
809
    // todo handle failure
810
    memset(buf, 0, len);
1,604,491✔
811
  }
812
  taosMemoryFreeClear(buf);
8,440!
813

814
  int32_t numOfElems = streamStateGetBatchSize(batch);
8,445✔
815
  if (numOfElems > 0) {
8,348✔
816
    code = streamStatePutBatch_rocksdb(pFileState->pFileStore, batch);
1,834✔
817
    QUERY_CHECK_CODE(code, lino, _end);
1,835!
818
  } else {
819
    goto _end;
6,514✔
820
  }
821

822
  streamStateClearBatch(batch);
1,835✔
823

824
  clearSearchBuff(pFileState);
1,835✔
825

826
  int64_t elapsed = taosGetTimestampMs() - st;
1,834✔
827
  qDebug("%s flush to disk in batch model completed, rows:%d, batch size:%d, elapsed time:%" PRId64 "ms",
1,834✔
828
         pFileState->id, numOfElems, BATCH_LIMIT, elapsed);
829

830
  if (flushState) {
1,835✔
831
    void*   valBuf = NULL;
1,154✔
832
    int32_t len = 0;
1,154✔
833
    code = streamFileStateEncode(&pFileState->flushMark, &valBuf, &len);
1,154✔
834
    QUERY_CHECK_CODE(code, lino, _end);
1,154!
835

836
    qDebug("===stream===flushMark write:%" PRId64, pFileState->flushMark);
1,154✔
837
    code = streamStatePutBatch(pFileState->pFileStore, "default", batch, STREAM_STATE_INFO_NAME, valBuf, len, 0);
1,154✔
838
    taosMemoryFree(valBuf);
1,154!
839
    QUERY_CHECK_CODE(code, lino, _end);
1,154!
840

841
    code = streamStatePutBatch_rocksdb(pFileState->pFileStore, batch);
1,154✔
842
    QUERY_CHECK_CODE(code, lino, _end);
1,154!
843
  }
844

845
_end:
681✔
846
  if (code != TSDB_CODE_SUCCESS) {
8,349!
847
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
848
  }
849
  taosMemoryFree(buf);
8,349!
850
  streamStateDestroyBatch(batch);
8,332✔
851
}
8,347✔
852

853
int32_t forceRemoveCheckpoint(SStreamFileState* pFileState, int64_t checkpointId) {
×
854
  char keyBuf[128] = {0};
×
855
  TAOS_UNUSED(tsnprintf(keyBuf, sizeof(keyBuf), "%s:%" PRId64 "", TASK_KEY, checkpointId));
×
856
  return streamDefaultDel_rocksdb(pFileState->pFileStore, keyBuf);
×
857
}
858

859
int32_t deleteExpiredCheckPoint(SStreamFileState* pFileState, TSKEY mark) {
×
860
  int32_t code = TSDB_CODE_SUCCESS;
×
861
  int64_t maxCheckPointId = 0;
×
862
  {
863
    char    buf[128] = {0};
×
864
    void*   val = NULL;
×
865
    int32_t len = 0;
×
866
    memcpy(buf, TASK_KEY, strlen(TASK_KEY));
×
867
    code = streamDefaultGet_rocksdb(pFileState->pFileStore, buf, &val, &len);
×
868
    if (code != 0 || len == 0 || val == NULL) {
×
869
      return TSDB_CODE_FAILED;
×
870
    }
871
    memcpy(buf, val, len);
×
872
    buf[len] = 0;
×
873
    maxCheckPointId = taosStr2Int64((char*)buf, NULL, 10);
×
874
    taosMemoryFree(val);
×
875
  }
876
  for (int64_t i = maxCheckPointId; i > 0; i--) {
×
877
    char    buf[128] = {0};
×
878
    void*   val = 0;
×
879
    int32_t len = 0;
×
880
    TAOS_UNUSED(tsnprintf(buf, sizeof(buf), "%s:%" PRId64 "", TASK_KEY, i));
×
881
    code = streamDefaultGet_rocksdb(pFileState->pFileStore, buf, &val, &len);
×
882
    if (code != 0) {
×
883
      return TSDB_CODE_FAILED;
×
884
    }
885
    memcpy(buf, val, len);
×
886
    buf[len] = 0;
×
887
    taosMemoryFree(val);
×
888

889
    TSKEY ts;
890
    ts = taosStr2Int64((char*)buf, NULL, 10);
×
891
    if (ts < mark) {
×
892
      // statekey winkey.ts < mark
893
      int32_t tmpRes = forceRemoveCheckpoint(pFileState, i);
×
894
      qTrace("%s at line %d res:%d", __func__, __LINE__, tmpRes);
×
895
      break;
×
896
    }
897
  }
898
  return code;
×
899
}
900

901
int32_t recoverSession(SStreamFileState* pFileState, int64_t ckId) {
1,762✔
902
  int32_t code = TSDB_CODE_SUCCESS;
1,762✔
903
  int32_t lino = 0;
1,762✔
904
  int32_t winRes = TSDB_CODE_SUCCESS;
1,762✔
905
  if (pFileState->maxTs != INT64_MIN) {
1,762!
906
    int64_t mark = (INT64_MIN + pFileState->deleteMark >= pFileState->maxTs)
×
907
                       ? INT64_MIN
908
                       : pFileState->maxTs - pFileState->deleteMark;
×
909
    int32_t tmpRes = deleteExpiredCheckPoint(pFileState, mark);
×
910
    qTrace("%s at line %d res:%d", __func__, __LINE__, tmpRes);
×
911
  }
912

913
  SStreamStateCur* pCur = streamStateSessionSeekToLast_rocksdb(pFileState->pFileStore, INT64_MAX);
1,762✔
914
  int32_t          recoverNum = TMIN(MIN_NUM_OF_RECOVER_ROW_BUFF, pFileState->maxRowCount);
1,763✔
915
  while (winRes == TSDB_CODE_SUCCESS) {
1,763!
916
    if (pFileState->curRowCount >= recoverNum) {
1,763!
917
      break;
1,763✔
918
    }
919

920
    void*       pVal = NULL;
1,763✔
921
    int32_t     vlen = 0;
1,763✔
922
    SSessionKey key = {0};
1,763✔
923
    winRes = streamStateSessionGetKVByCur_rocksdb(getStateFileStore(pFileState), pCur, &key, &pVal, &vlen);
1,763✔
924
    if (winRes != TSDB_CODE_SUCCESS) {
1,763!
925
      break;
1,763✔
926
    }
927

928
    if (vlen != pFileState->rowSize) {
×
929
      code = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
930
      QUERY_CHECK_CODE(code, lino, _end);
×
931
    }
932

933
    SRowBuffPos* pPos = createSessionWinBuff(pFileState, &key, pVal, &vlen);
×
934
    pPos->beUsed = false;
×
935
    winRes = putSessionWinResultBuff(pFileState, pPos);
×
936
    if (winRes != TSDB_CODE_SUCCESS) {
×
937
      break;
×
938
    }
939

940
    winRes = streamStateSessionCurPrev_rocksdb(pCur);
×
941
  }
942

943
_end:
×
944
  if (code != TSDB_CODE_SUCCESS) {
1,763!
945
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
946
  }
947
  streamStateFreeCur(pCur);
1,763✔
948
  return code;
1,763✔
949
}
950

951
int32_t recoverSnapshot(SStreamFileState* pFileState, int64_t ckId) {
4,183✔
952
  int32_t code = TSDB_CODE_SUCCESS;
4,183✔
953
  int32_t lino = 0;
4,183✔
954
  int32_t winCode = TSDB_CODE_SUCCESS;
4,183✔
955
  if (pFileState->maxTs != INT64_MIN) {
4,183!
956
    int64_t mark = (INT64_MIN + pFileState->deleteMark >= pFileState->maxTs)
×
957
                       ? INT64_MIN
958
                       : pFileState->maxTs - pFileState->deleteMark;
×
959
    int32_t tmpRes = deleteExpiredCheckPoint(pFileState, mark);
×
960
    qTrace("%s at line %d res:%d", __func__, __LINE__, tmpRes);
×
961
  }
962

963
  SStreamStateCur* pCur = streamStateSeekToLast_rocksdb(pFileState->pFileStore);
4,183✔
964
  int32_t          recoverNum = TMIN(MIN_NUM_OF_RECOVER_ROW_BUFF, pFileState->maxRowCount);
4,187✔
965
  while (winCode == TSDB_CODE_SUCCESS) {
4,187!
966
    if (pFileState->curRowCount >= recoverNum) {
4,187!
967
      break;
4,187✔
968
    }
969

970
    void*        pVal = NULL;
4,187✔
971
    int32_t      vlen = 0;
4,187✔
972
    SRowBuffPos* pNewPos = getNewRowPosForWrite(pFileState);
4,187✔
973
    if (!pNewPos || !pNewPos->pRowBuff) {
4,187!
974
      code = TSDB_CODE_OUT_OF_MEMORY;
×
975
      QUERY_CHECK_CODE(code, lino, _end);
×
976
    }
977

978
    winCode = streamStateGetKVByCur_rocksdb(getStateFileStore(pFileState), pCur, pNewPos->pKey, (const void**)&pVal, &vlen);
4,187✔
979
    qDebug("===stream=== get state by cur winres:%d. %s", winCode, __func__);
4,187✔
980
    if (winCode != TSDB_CODE_SUCCESS || pFileState->getTs(pNewPos->pKey) < pFileState->flushMark) {
4,187!
981
      destroyRowBuffPos(pNewPos);
4,187✔
982
      SListNode* pNode = tdListPopTail(pFileState->usedBuffs);
4,187✔
983
      taosMemoryFreeClear(pNode);
4,187!
984
      taosMemoryFreeClear(pVal);
4,187!
985
      break;
4,187✔
986
    }
987
    if (vlen != pFileState->rowSize) {
×
988
      qError("row size mismatch, expect:%d, actual:%d", pFileState->rowSize, vlen);
×
989
      code = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
990
      taosMemoryFreeClear(pVal);
×
991
      QUERY_CHECK_CODE(code, lino, _end);
×
992
    }
993
    memcpy(pNewPos->pRowBuff, pVal, vlen);
×
994
    taosMemoryFreeClear(pVal);
×
995
    pNewPos->beFlushed = true;
×
996
    pNewPos->beUsed = false;
×
997
    qDebug("===stream=== read checkpoint state from disc. %s", __func__);
×
998
    code = tSimpleHashPut(pFileState->rowStateBuff, pNewPos->pKey, pFileState->keyLen, &pNewPos, POINTER_BYTES);
×
999
    if (code != TSDB_CODE_SUCCESS) {
×
1000
      destroyRowBuffPos(pNewPos);
×
1001
      break;
×
1002
    }
1003
    streamStateCurPrev_rocksdb(pCur);
×
1004
  }
1005

1006
_end:
×
1007
  if (code != TSDB_CODE_SUCCESS) {
4,187!
1008
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1009
  }
1010
  streamStateFreeCur(pCur);
4,187✔
1011
  return code;
4,187✔
1012
}
1013

1014
int32_t streamFileStateGetSelectRowSize(SStreamFileState* pFileState) { return pFileState->selectivityRowSize; }
228,837✔
1015

1016
void streamFileStateReloadInfo(SStreamFileState* pFileState, TSKEY ts) {
1,506✔
1017
  pFileState->flushMark = TMAX(pFileState->flushMark, ts);
1,506✔
1018
  pFileState->maxTs = TMAX(pFileState->maxTs, ts);
1,506✔
1019
}
1,506✔
1020

1021
void* getRowStateBuff(SStreamFileState* pFileState) { return pFileState->rowStateBuff; }
41,483✔
1022
void* getSearchBuff(SStreamFileState* pFileState) { return pFileState->searchBuff; }
13,380,529✔
1023

1024
void* getStateFileStore(SStreamFileState* pFileState) { return pFileState->pFileStore; }
21,342✔
1025

1026
bool isDeteled(SStreamFileState* pFileState, TSKEY ts) {
12,763,566✔
1027
  return pFileState->deleteMark != INT64_MAX && pFileState->maxTs > 0 &&
18,553,583!
1028
         ts < (pFileState->maxTs - pFileState->deleteMark);
5,790,017✔
1029
}
1030

1031
bool isFlushedState(SStreamFileState* pFileState, TSKEY ts, TSKEY gap) { return ts <= (pFileState->flushMark + gap); }
10,269,011✔
1032

1033
TSKEY getFlushMark(SStreamFileState* pFileState) { return pFileState->flushMark; };
631✔
1034

1035
int32_t getRowStateRowSize(SStreamFileState* pFileState) { return pFileState->rowSize; }
229,863✔
1036

1037
int32_t getFunctionRowBuff(SStreamFileState* pFileState, void* pKey, int32_t keyLen, void** pVal, int32_t* pVLen) {
228,842✔
1038
  int32_t winCode = TSDB_CODE_SUCCESS;
228,842✔
1039
  return pFileState->stateFunctionGetFn(pFileState, pKey, keyLen, pVal, pVLen, &winCode);
228,842✔
1040
}
1041

1042
int32_t recoverFillSnapshot(SStreamFileState* pFileState, int64_t ckId) {
265✔
1043
  int32_t code = TSDB_CODE_SUCCESS;
265✔
1044
  int32_t lino = 0;
265✔
1045
  if (pFileState->maxTs != INT64_MIN) {
265!
1046
    int64_t mark = (INT64_MIN + pFileState->deleteMark >= pFileState->maxTs)
×
1047
                       ? INT64_MIN
1048
                       : pFileState->maxTs - pFileState->deleteMark;
×
1049
    code = deleteExpiredCheckPoint(pFileState, mark);
×
1050
    QUERY_CHECK_CODE(code, lino, _end);
×
1051
  }
1052

1053
  SStreamStateCur* pCur = streamStateFillSeekToLast_rocksdb(pFileState->pFileStore);
265✔
1054
  if (pCur == NULL) {
265✔
1055
    return code;
263✔
1056
  }
1057
  int32_t recoverNum = TMIN(MIN_NUM_OF_RECOVER_ROW_BUFF, pFileState->maxRowCount);
2✔
1058
  int32_t winRes = TSDB_CODE_SUCCESS;
2✔
1059
  while (winRes == TSDB_CODE_SUCCESS) {
4!
1060
    if (pFileState->curRowCount >= recoverNum) {
4!
1061
      break;
2✔
1062
    }
1063

1064
    void*        pVal = NULL;
4✔
1065
    int32_t      vlen = 0;
4✔
1066
    SRowBuffPos* pNewPos = getNewRowPosForWrite(pFileState);
4✔
1067
    winRes = streamStateFillGetKVByCur_rocksdb(pCur, pNewPos->pKey, (const void**)&pVal, &vlen);
4✔
1068
    qDebug("===stream=== get state by cur winres:%d. %s", winRes, __func__);
4!
1069
    if (winRes != TSDB_CODE_SUCCESS || isFlushedState(pFileState, pFileState->getTs(pNewPos->pKey), 0)) {
4!
1070
      destroyRowBuffPos(pNewPos);
2✔
1071
      SListNode* pNode = tdListPopTail(pFileState->usedBuffs);
2✔
1072
      taosMemoryFreeClear(pNode);
2!
1073
      taosMemoryFreeClear(pVal);
2!
1074
      break;
2✔
1075
    }
1076

1077
    if (vlen != pFileState->rowSize) {
2!
1078
      qError("row size mismatch, expect:%d, actual:%d", pFileState->rowSize, vlen);
×
1079
      destroyRowBuffPos(pNewPos);
×
1080
      code = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
1081
      taosMemoryFreeClear(pVal);
×
1082
      QUERY_CHECK_CODE(code, lino, _end);
×
1083
    }
1084

1085
    memcpy(pNewPos->pRowBuff, pVal, vlen);
2✔
1086
    taosMemoryFreeClear(pVal);
2!
1087
    pNewPos->beFlushed = true;
2✔
1088
    pNewPos->beUsed = false;
2✔
1089
    qDebug("===stream=== read checkpoint state from disc. %s", __func__);
2!
1090
    winRes = tSimpleHashPut(pFileState->rowStateBuff, pNewPos->pKey, pFileState->keyLen, &pNewPos, POINTER_BYTES);
2✔
1091
    if (winRes != TSDB_CODE_SUCCESS) {
2!
1092
      destroyRowBuffPos(pNewPos);
×
1093
      break;
×
1094
    }
1095
    streamStateCurPrev_rocksdb(pCur);
2✔
1096
  }
1097
  streamStateFreeCur(pCur);
2✔
1098

1099
_end:
2✔
1100
  if (code != TSDB_CODE_SUCCESS) {
2!
1101
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1102
  }
1103
  return code;
2✔
1104
}
1105

1106
int32_t getRowBuff(SStreamFileState* pFileState, void* pKey, int32_t keyLen, void** pVal, int32_t* pVLen,
1,738✔
1107
                   int32_t* pWinCode) {
1108
  int32_t code = TSDB_CODE_SUCCESS;
1,738✔
1109
  int32_t lino = 0;
1,738✔
1110
  (*pWinCode) = TSDB_CODE_FAILED;
1,738✔
1111
  pFileState->maxTs = TMAX(pFileState->maxTs, pFileState->getTs(pKey));
1,738✔
1112
  SRowBuffPos** ppPos = tSimpleHashGet(pFileState->rowStateBuff, pKey, keyLen);
1,738✔
1113
  if (ppPos) {
1,738✔
1114
    *pVLen = pFileState->rowSize;
832✔
1115
    *pVal = *ppPos;
832✔
1116
    (*ppPos)->beUsed = true;
832✔
1117
    (*ppPos)->beFlushed = false;
832✔
1118
    (*pWinCode) = TSDB_CODE_SUCCESS;
832✔
1119
    if ((*ppPos)->pRowBuff == NULL) {
832!
1120
      code = recoverStateRowBuff(pFileState, *ppPos);
×
1121
      QUERY_CHECK_CODE(code, lino, _end);
×
1122
    }
1123
    goto _end;
832✔
1124
  }
1125
  TSKEY ts = pFileState->getTs(pKey);
906✔
1126
  if (!isDeteled(pFileState, ts) && isFlushedState(pFileState, ts, 0)) {
906!
1127
    int32_t len = 0;
24✔
1128
    void*   p = NULL;
24✔
1129
    (*pWinCode) = pFileState->stateFileGetFn(pFileState, pKey, &p, &len);
24✔
1130
    qDebug("===stream===get %" PRId64 " from disc, res %d", ts, (*pWinCode));
24!
1131
    if ((*pWinCode) == TSDB_CODE_SUCCESS) {
24✔
1132
      SRowBuffPos* pNewPos = getNewRowPosForWrite(pFileState);
20✔
1133
      if (!pNewPos || !pNewPos->pRowBuff) {
20!
1134
        code = TSDB_CODE_OUT_OF_MEMORY;
×
1135
        QUERY_CHECK_CODE(code, lino, _end);
×
1136
      }
1137

1138
      memcpy(pNewPos->pKey, pKey, keyLen);
20✔
1139
      memcpy(pNewPos->pRowBuff, p, len);
20✔
1140
      code = tSimpleHashPut(pFileState->rowStateBuff, pKey, keyLen, &pNewPos, POINTER_BYTES);
20✔
1141
      QUERY_CHECK_CODE(code, lino, _end);
20!
1142

1143
      if (pVal) {
20!
1144
        *pVLen = pFileState->rowSize;
20✔
1145
        *pVal = pNewPos;
20✔
1146
      }
1147
    }
1148
    taosMemoryFree(p);
24!
1149
  }
1150

1151
_end:
882✔
1152
  if (code != TSDB_CODE_SUCCESS) {
1,738!
1153
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1154
  }
1155
  return code;
1,738✔
1156
}
1157

1158
int32_t streamFileStateGroupPut(SStreamFileState* pFileState, int64_t groupId, void* value, int32_t vLen) {
462✔
1159
  int32_t code = TSDB_CODE_SUCCESS;
462✔
1160
  int32_t lino = 0;
462✔
1161
  if (value != NULL) {
462!
1162
    code = TSDB_CODE_INVALID_PARA;
×
1163
    QUERY_CHECK_CODE(code, lino, _end);
×
1164
  }
1165

1166
  if (tSimpleHashGet(pFileState->pGroupIdMap, &groupId, sizeof(int64_t)) == NULL) {
462✔
1167
    if (tSimpleHashGetSize(pFileState->pGroupIdMap) <= MAX_GROUP_ID_NUM) {
169!
1168
      code = tSimpleHashPut(pFileState->pGroupIdMap, &groupId, sizeof(int64_t), NULL, 0);
169✔
1169
      QUERY_CHECK_CODE(code, lino, _end);
169!
1170
    }
1171
    code = streamStatePutParTag_rocksdb(pFileState->pFileStore, groupId, value, vLen);
169✔
1172
    QUERY_CHECK_CODE(code, lino, _end);
169!
1173
  }
1174

1175
_end:
462✔
1176
  if (code != TSDB_CODE_SUCCESS) {
462!
1177
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1178
  }
1179
  return code;
462✔
1180
}
1181

1182
void streamFileStateGroupCurNext(SStreamStateCur* pCur) {
1,287✔
1183
  SStreamFileState* pFileState = (SStreamFileState*)pCur->pStreamFileState;
1,287✔
1184
  if (pCur->hashIter == -1) {
1,287✔
1185
    streamStateCurNext(pFileState->pFileStore, pCur);
68✔
1186
    return;
68✔
1187
  }
1188

1189
  int64_t gpId = *(int64_t*)tSimpleHashGetKey(pCur->pHashData, NULL);
1,219!
1190
  pCur->minGpId = TMAX(pCur->minGpId, gpId);
1,219✔
1191

1192
  SSHashObj* pHash = pFileState->pGroupIdMap;
1,219✔
1193
  pCur->pHashData = tSimpleHashIterate(pHash, pCur->pHashData, &pCur->hashIter);
1,219✔
1194
  if (!pCur->pHashData) {
1,219✔
1195
    pCur->hashIter = -1;
751✔
1196
    streamStateParTagSeekKeyNext_rocksdb(pFileState->pFileStore, pCur->minGpId, pCur);
751✔
1197
    return;
751✔
1198
  }
1199
}
1200

1201
int32_t streamFileStateGroupGetKVByCur(SStreamStateCur* pCur, int64_t* pKey, void** pVal, int32_t* pVLen) {
3,888✔
1202
  int32_t code = TSDB_CODE_SUCCESS;
3,888✔
1203
  if (pCur->pHashData) {
3,888✔
1204
    *pKey = *(int64_t*)tSimpleHashGetKey(pCur->pHashData, NULL);
1,219!
1205
    return code;
1,219✔
1206
  }
1207
  return streamStateParTagGetKVByCur_rocksdb(pCur, pKey, NULL, NULL);
2,669✔
1208
}
1209

1210
SSHashObj* getGroupIdCache(SStreamFileState* pFileState) {
2,601✔
1211
  return pFileState->pGroupIdMap;
2,601✔
1212
}
1213

1214
void clearExpiredState(SStreamFileState* pFileState) {
4,166✔
1215
  int32_t    code = TSDB_CODE_SUCCESS;
4,166✔
1216
  int32_t    lino = 0;
4,166✔
1217
  SSHashObj* pSearchBuff = pFileState->searchBuff;
4,166✔
1218
  void*      pIte = NULL;
4,166✔
1219
  int32_t    iter = 0;
4,166✔
1220
  while ((pIte = tSimpleHashIterate(pSearchBuff, pIte, &iter)) != NULL) {
6,194✔
1221
    SArray* pWinStates = *((void**)pIte);
2,028✔
1222
    int32_t size = taosArrayGetSize(pWinStates);
2,028✔
1223
    for (int32_t i = 0; i < size - 1; i++) {
2,447✔
1224
      SWinKey* pKey = taosArrayGet(pWinStates, i);
419✔
1225
      int32_t  code_buff = pFileState->stateBuffRemoveFn(pFileState->rowStateBuff, pKey, sizeof(SWinKey));
419✔
1226
      qTrace("clear expired buff, ts:%" PRId64 ". %s at line %d res:%d", pKey->ts, __func__, __LINE__, code_buff);
419!
1227

1228
      if (isFlushedState(pFileState, pKey->ts, 0)) {
419✔
1229
        int32_t code_file = pFileState->stateFileRemoveFn(pFileState, pKey);
308✔
1230
        qTrace("clear expired file, ts:%" PRId64 ". %s at line %d res:%d", pKey->ts, __func__, __LINE__, code_file);
308!
1231
      }
1232
    }
1233
    taosArrayRemoveBatch(pWinStates, 0, size - 1, NULL);
2,028✔
1234
  }
1235
  code = clearRowBuff(pFileState);
4,168✔
1236
  QUERY_CHECK_CODE(code, lino, _end);
4,168!
1237

1238
_end:
4,168✔
1239
  if (code != TSDB_CODE_SUCCESS) {
4,168!
1240
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1241
  }
1242
}
4,168✔
1243

1244
#ifdef BUILD_NO_CALL
1245
int32_t getStateSearchRowBuff(SStreamFileState* pFileState, const SWinKey* pKey, void** pVal, int32_t* pVLen,
1246
                           int32_t* pWinCode) {
1247
  int32_t code = TSDB_CODE_SUCCESS;
1248
  int32_t lino = 0;
1249

1250
  code = addRowBuffIfNotExist(pFileState, (void*)pKey, sizeof(SWinKey), pVal, pVLen, pWinCode);
1251
  QUERY_CHECK_CODE(code, lino, _end);
1252

1253
  SArray*    pWinStates = NULL;
1254
  SSHashObj* pSearchBuff = getSearchBuff(pFileState);
1255
  void**     ppBuff = tSimpleHashGet(pSearchBuff, &pKey->groupId, sizeof(uint64_t));
1256
  if (ppBuff) {
1257
    pWinStates = (SArray*)(*ppBuff);
1258
  } else {
1259
    pWinStates = taosArrayInit(16, sizeof(SWinKey));
1260
    QUERY_CHECK_NULL(pWinStates, code, lino, _end, terrno);
1261

1262
    code = tSimpleHashPut(pSearchBuff, &pKey->groupId, sizeof(uint64_t), &pWinStates, POINTER_BYTES);
1263
    QUERY_CHECK_CODE(code, lino, _end);
1264
  }
1265

1266
  // recover
1267
  if (taosArrayGetSize(pWinStates) == 0 && needClearDiskBuff(pFileState)) {
1268
    TSKEY            ts = getFlushMark(pFileState);
1269
    SWinKey          start = {.groupId = pKey->groupId, .ts = INT64_MAX};
1270
    void*            pState = getStateFileStore(pFileState);
1271
    SStreamStateCur* pCur = streamStateSeekKeyPrev_rocksdb(pState, &start);
1272
    for (int32_t i = 0; i < NUM_OF_CACHE_WIN; i++) {
1273
      SWinKey tmpKey = {.groupId = pKey->groupId};
1274
      int32_t tmpRes = streamStateGetGroupKVByCur_rocksdb(pState, pCur, &tmpKey, NULL, 0);
1275
      if (tmpRes != TSDB_CODE_SUCCESS) {
1276
        break;
1277
      }
1278
      void* tmp = taosArrayPush(pWinStates, &tmpKey);
1279
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
1280
      streamStateCurPrev_rocksdb(pCur);
1281
    }
1282
    taosArraySort(pWinStates, winKeyCmprImpl);
1283
    streamStateFreeCur(pCur);
1284
  }
1285

1286
  int32_t size = taosArrayGetSize(pWinStates);
1287
  int32_t index = binarySearch(pWinStates, size, pKey, fillStateKeyCompare);
1288
  if (!isFlushedState(pFileState, pKey->ts, 0)|| index >= 0) {
1289
    // find the first position which is smaller than the pKey
1290
    if (index >= 0) {
1291
      SWinKey* pTmpKey = taosArrayGet(pWinStates, index);
1292
      if (winKeyCmprImpl(pTmpKey, pKey) == 0) {
1293
        goto _end;
1294
      }
1295
    }
1296
    index++;
1297
    void* tmp = taosArrayInsert(pWinStates, index, pKey);
1298
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
1299
  }
1300

1301
  if (size >= MAX_NUM_OF_CACHE_WIN) {
1302
    int32_t num = size - NUM_OF_CACHE_WIN;
1303
    taosArrayRemoveBatch(pWinStates, 0, num, NULL);
1304
  }
1305

1306
_end:
1307
  if (code != TSDB_CODE_SUCCESS) {
1308
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
1309
  }
1310
  return code;
1311
}
1312
#endif
1313

1314
int32_t getRowStatePrevRow(SStreamFileState* pFileState, const SWinKey* pKey, SWinKey* pResKey, void** ppVal,
103✔
1315
                           int32_t* pVLen, int32_t* pWinCode) {
1316
  int32_t    code = TSDB_CODE_SUCCESS;
103✔
1317
  int32_t    lino = 0;
103✔
1318
  SArray*    pWinStates = NULL;
103✔
1319
  SSHashObj* pSearchBuff = getSearchBuff(pFileState);
103✔
1320
  void*      pState = getStateFileStore(pFileState);
103✔
1321
  void**     ppBuff = (void**) tSimpleHashGet(pSearchBuff, &pKey->groupId, sizeof(uint64_t));
103✔
1322
  if (ppBuff) {
103!
1323
    pWinStates = (SArray*)(*ppBuff);
103✔
1324
  } else {
1325
    qDebug("===stream=== search buff is empty.group id:%" PRId64, pKey->groupId);
×
1326
    SStreamStateCur* pCur = streamStateSeekKeyPrev_rocksdb(pState, pKey);
×
1327
    void*            tmpVal = NULL;
×
1328
    int32_t          len = 0;
×
1329
    (*pWinCode) = streamStateGetGroupKVByCur_rocksdb(pState, pCur, pResKey, (const void**)&tmpVal, &len);
×
1330
    if ((*pWinCode) == TSDB_CODE_SUCCESS) {
×
1331
      SRowBuffPos* pNewPos = getNewRowPosForWrite(pFileState);
×
1332
      if (!pNewPos || !pNewPos->pRowBuff) {
×
1333
        code = TSDB_CODE_OUT_OF_MEMORY;
×
1334
        QUERY_CHECK_CODE(code, lino, _end);
×
1335
      }
1336
      memcpy(pNewPos->pRowBuff, tmpVal, len);
×
1337
      taosMemoryFreeClear(tmpVal);
×
1338
      *pVLen = getRowStateRowSize(pFileState);
×
1339
      (*ppVal) = pNewPos;
×
1340
    }
1341
    streamStateFreeCur(pCur);
×
1342
    return code;
×
1343
  }
1344
  int32_t size = taosArrayGetSize(pWinStates);
103✔
1345
  int32_t index = binarySearch(pWinStates, size, pKey, fillStateKeyCompare);
103✔
1346
  if (index >= 0) {
103!
1347
    SWinKey* pCurKey = taosArrayGet(pWinStates, index);
103✔
1348
    if (winKeyCmprImpl(pCurKey, pKey) == 0) {
103!
1349
      index--;
103✔
1350
    } else {
1351
      qDebug("%s failed at line %d since do not find cur SWinKey. trigger may be force window close", __func__, __LINE__);
×
1352
    }
1353
  }
1354
  if (index == -1) {
103✔
1355
    SStreamStateCur* pCur = streamStateSeekKeyPrev_rocksdb(pState, pKey);
28✔
1356
    void*            tmpVal = NULL;
28✔
1357
    int32_t          len = 0;
28✔
1358
    (*pWinCode) = streamStateGetGroupKVByCur_rocksdb(pState, pCur, pResKey, (const void**)&tmpVal, &len);
28✔
1359
    if ((*pWinCode) == TSDB_CODE_SUCCESS) {
28!
1360
      SRowBuffPos* pNewPos = getNewRowPosForWrite(pFileState);
×
1361
      if (!pNewPos || !pNewPos->pRowBuff) {
×
1362
        code = TSDB_CODE_OUT_OF_MEMORY;
×
1363
        QUERY_CHECK_CODE(code, lino, _end);
×
1364
      }
1365
      memcpy(pNewPos->pRowBuff, tmpVal, len);
×
1366
      taosMemoryFreeClear(tmpVal);
×
1367
      *pVLen = getRowStateRowSize(pFileState);
×
1368
      (*ppVal) = pNewPos;
×
1369
    }
1370
    streamStateFreeCur(pCur);
28✔
1371
    return code;
28✔
1372
  } else {
1373
    SWinKey* pPrevKey = taosArrayGet(pWinStates, index);
75✔
1374
    *pResKey = *pPrevKey;
75✔
1375
    return addRowBuffIfNotExist(pFileState, (void*)pPrevKey, sizeof(SWinKey), ppVal, pVLen, pWinCode);
75✔
1376
  }
1377
  (*pWinCode) = TSDB_CODE_FAILED;
1378

1379
_end:
×
1380
  if (code != TSDB_CODE_SUCCESS) {
×
1381
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1382
  }
1383
  return code;
×
1384
}
1385

1386
int32_t addSearchItem(SStreamFileState* pFileState, SArray* pWinStates, const SWinKey* pKey) {
2,475✔
1387
  int32_t code = TSDB_CODE_SUCCESS;
2,475✔
1388
  int32_t lino = 0;
2,475✔
1389
  int32_t size = taosArrayGetSize(pWinStates);
2,475✔
1390
  int32_t index = binarySearch(pWinStates, size, pKey, fillStateKeyCompare);
2,475✔
1391
  if (!isFlushedState(pFileState, pKey->ts, 0) || index >= 0 || size == 0) {
2,475!
1392
    if (index >= 0) {
2,475✔
1393
      SWinKey* pTmpKey = taosArrayGet(pWinStates, index);
2,161✔
1394
      if (winKeyCmprImpl(pTmpKey, pKey) == 0) {
2,161✔
1395
        goto _end;
1,547✔
1396
      }
1397
    }
1398
    index++;
928✔
1399
    void* tmp = taosArrayInsert(pWinStates, index, pKey);
928✔
1400
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
927!
1401
  }
1402

1403
  if (size >= MAX_NUM_OF_CACHE_WIN) {
927!
1404
    int32_t num = size - NUM_OF_CACHE_WIN;
×
1405
    taosArrayRemoveBatch(pWinStates, 0, num, NULL);
×
1406
  }
1407
_end:
927✔
1408
  if (code != TSDB_CODE_SUCCESS) {
2,474!
1409
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1410
  }
1411
  return code;
2,474✔
1412
}
1413

1414
int32_t addArrayBuffIfNotExist(SSHashObj* pSearchBuff, uint64_t groupId, SArray** ppResStates) {
3,824✔
1415
  int32_t code = TSDB_CODE_SUCCESS;
3,824✔
1416
  int32_t lino = 0; 
3,824✔
1417
  SArray*    pWinStates = NULL;
3,824✔
1418
  void**     ppBuff = tSimpleHashGet(pSearchBuff, &groupId, sizeof(uint64_t));
3,824✔
1419
  if (ppBuff) {
3,824✔
1420
    pWinStates = (SArray*)(*ppBuff);
3,526✔
1421
  } else {
1422
    pWinStates = taosArrayInit(16, sizeof(SWinKey));
298✔
1423
    QUERY_CHECK_NULL(pWinStates, code, lino, _end, terrno);
298!
1424

1425
    code = tSimpleHashPut(pSearchBuff, &groupId, sizeof(uint64_t), &pWinStates, POINTER_BYTES);
298✔
1426
    QUERY_CHECK_CODE(code, lino, _end);
298!
1427
  }
1428

1429
  (*ppResStates) = pWinStates;
3,824✔
1430

1431
_end:
3,824✔
1432
  if (code != TSDB_CODE_SUCCESS) {
3,824!
1433
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1434
  }
1435
  return code;
3,824✔
1436
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc