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

taosdata / TDengine / #3625

26 Feb 2025 10:19AM UTC coverage: 63.633% (+0.1%) from 63.485%
#3625

push

travis-ci

web-flow
Merge pull request #29914 from taosdata/feat/TS-5613-3.0

feat:[TS-5613]support bool in cast

148738 of 299799 branches covered (49.61%)

Branch coverage included in aggregate %.

233124 of 300297 relevant lines covered (77.63%)

17654074.26 hits per line

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

63.43
/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,802✔
75
  SWinKey* pWin2 = taosArrayGet(pDatas, pos);
9,802✔
76
  return winKeyCmprImpl((SWinKey*)pWin1, pWin2);
9,802✔
77
}
78

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

87
void stateHashBuffRemoveByPosFn(SStreamFileState* pFileState, SRowBuffPos* pPos) {
6,263,171✔
88
  size_t        keyLen = pFileState->keyLen;
6,263,171✔
89
  SRowBuffPos** ppPos = tSimpleHashGet(pFileState->rowStateBuff, pPos->pKey, keyLen);
6,263,171✔
90
  if (ppPos) {
6,266,076✔
91
    if ((*ppPos) == pPos) {
6,265,812!
92
      int32_t tmpRes = tSimpleHashRemove(pFileState->rowStateBuff, pPos->pKey, keyLen);
6,265,822✔
93
      qTrace("%s at line %d res:%d", __func__, __LINE__, tmpRes);
6,268,697✔
94
    }
95
  }
96
}
6,269,372✔
97

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

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

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

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

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

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

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

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

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

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

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

167
int32_t streamFileStateInit(int64_t memSize, uint32_t keySize, uint32_t rowSize, uint32_t selectRowSize, GetTsFun fp,
6,161✔
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,161✔
171
  int32_t lino = 0;
6,161✔
172
  if (memSize <= 0) {
6,161!
173
    memSize = DEFAULT_MAX_STREAM_BUFFER_SIZE;
×
174
  }
175
  if (rowSize == 0) {
6,161!
176
    code = TSDB_CODE_INVALID_PARA;
×
177
    QUERY_CHECK_CODE(code, lino, _end);
×
178
  }
179

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

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

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

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

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

211
    pFileState->stateFileRemoveFn = sessionFileRemoveFn;
1,787✔
212
    pFileState->stateFileGetFn = sessionFileGetFn;
1,787✔
213
    pFileState->cfName = taosStrdup("sess");
1,787!
214
    pFileState->stateFunctionGetFn = getSessionRowBuff;
1,787✔
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,166!
231
  QUERY_CHECK_NULL(pFileState->freeBuffs, code, lino, _end, terrno);
6,166!
232
  QUERY_CHECK_NULL(pFileState->rowStateBuff, code, lino, _end, terrno);
6,166!
233
  QUERY_CHECK_NULL(pFileState->cfName, code, lino, _end, terrno);
6,166!
234

235
  if (type == STREAM_STATE_BUFF_HASH_SEARCH) {
6,166✔
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,166✔
241
  pFileState->rowSize = rowSize;
6,166✔
242
  pFileState->selectivityRowSize = selectRowSize;
6,166✔
243
  pFileState->preCheckPointVersion = 0;
6,166✔
244
  pFileState->checkPointVersion = 1;
6,166✔
245
  pFileState->pFileStore = pFile;
6,166✔
246
  pFileState->getTs = fp;
6,166✔
247
  pFileState->curRowCount = 0;
6,166✔
248
  pFileState->deleteMark = delMark;
6,166✔
249
  pFileState->flushMark = INT64_MIN;
6,166✔
250
  pFileState->maxTs = INT64_MIN;
6,166✔
251
  pFileState->id = taosStrdup(taskId);
6,166!
252
  QUERY_CHECK_NULL(pFileState->id, code, lino, _end, terrno);
6,165!
253

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

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

259
  if (type == STREAM_STATE_BUFF_HASH || type == STREAM_STATE_BUFF_HASH_SEARCH) {
6,166✔
260
    code = recoverSnapshot(pFileState, checkpointId);
4,114✔
261
  } else if (type == STREAM_STATE_BUFF_SORT) {
2,052✔
262
    code = recoverSession(pFileState, checkpointId);
1,787✔
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,166!
267

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

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

287
void destroyRowBuffPos(SRowBuffPos* pPos) {
8,635,424✔
288
  taosMemoryFreeClear(pPos->pKey);
8,635,424!
289
  taosMemoryFreeClear(pPos->pRowBuff);
8,635,440!
290
  taosMemoryFree(pPos);
8,635,443!
291
}
8,635,391✔
292

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

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

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

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

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

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

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

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

355
  SListNode* pNode = NULL;
6,783✔
356
  while ((pNode = tdListNext(&iter)) != NULL) {
8,167,939✔
357
    SRowBuffPos* pPos = *(SRowBuffPos**)(pNode->data);
8,160,867✔
358
    if (all || (pFileState->getTs(pPos->pKey) < ts && !pPos->beUsed)) {
8,160,867!
359
      code = putFreeBuff(pFileState, pPos);
6,045,351✔
360
      QUERY_CHECK_CODE(code, lino, _end);
6,044,969!
361

362
      if (!all) {
6,044,969✔
363
        pFileState->stateBuffRemoveByPosFn(pFileState, pPos);
5,785,750✔
364
      }
365
      destroyRowBuffPos(pPos);
6,045,329✔
366
      SListNode* tmp = tdListPopNode(pFileState->usedBuffs, pNode);
6,045,860✔
367
      taosMemoryFreeClear(tmp);
6,045,439!
368
    }
369
  }
370

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

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

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

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

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

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

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

422
void streamFileStateReleaseBuff(SStreamFileState* pFileState, SRowBuffPos* pPos, bool used) { pPos->beUsed = used; }
8,461,290✔
423

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

431
  SListNode* pNode = NULL;
6,367✔
432
  while ((pNode = tdListNext(&iter)) != NULL && i < max) {
1,449,467!
433
    SRowBuffPos* pPos = *(SRowBuffPos**)pNode->data;
1,436,903✔
434
    if (pPos->beUsed == used) {
1,436,903✔
435
      if (used && !pPos->pRowBuff) {
474,747!
436
        QUERY_CHECK_CONDITION((pPos->needFree == true), code, lino, _end, TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
×
437
        continue;
×
438
      }
439
      code = tdListAppend(pFlushList, &pPos);
474,747✔
440
      QUERY_CHECK_CODE(code, lino, _end);
482,147!
441

442
      pFileState->flushMark = TMAX(pFileState->flushMark, pFileState->getTs(pPos->pKey));
482,147✔
443
      pFileState->stateBuffRemoveByPosFn(pFileState, pPos);
476,965✔
444
      SListNode* tmp = tdListPopNode(pFileState->usedBuffs, pNode);
481,796✔
445
      taosMemoryFreeClear(tmp);
477,109!
446
      if (pPos->pRowBuff) {
480,944✔
447
        i++;
480,872✔
448
      }
449
    }
450
  }
451

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

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

461
int32_t flushRowBuff(SStreamFileState* pFileState) {
3,960✔
462
  int32_t          code = TSDB_CODE_SUCCESS;
3,960✔
463
  int32_t          lino = 0;
3,960✔
464
  SStreamSnapshot* pFlushList = tdListNew(POINTER_BYTES);
3,960✔
465
  if (!pFlushList) {
3,959!
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,959✔
471
  num = TMAX(num, FLUSH_NUM);
3,959✔
472
  code = clearFlushedRowBuff(pFileState, pFlushList, num, false);
3,959✔
473
  QUERY_CHECK_CODE(code, lino, _end);
3,959!
474

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

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

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

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

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

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

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

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

520
void* getFreeBuff(SStreamFileState* pFileState) {
9,103,727✔
521
  SList*     lists = pFileState->freeBuffs;
9,103,727✔
522
  int32_t    buffSize = pFileState->rowSize;
9,103,727✔
523
  SListNode* pNode = tdListPopHead(lists);
9,103,727✔
524
  if (!pNode) {
9,097,919✔
525
    return NULL;
8,801,087✔
526
  }
527
  void* ptr = *(void**)pNode->data;
296,832✔
528
  memset(ptr, 0, buffSize);
296,832✔
529
  taosMemoryFree(pNode);
296,832!
530
  return ptr;
296,471✔
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) {
9,098,649✔
540
  int32_t      code = TSDB_CODE_SUCCESS;
9,098,649✔
541
  int32_t      lino = 0;
9,098,649✔
542
  SRowBuffPos* pPos = taosMemoryCalloc(1, sizeof(SRowBuffPos));
9,098,649!
543
  if (!pPos) {
9,112,860!
544
    code = terrno;
×
545
    QUERY_CHECK_CODE(code, lino, _error);
×
546
  }
547

548
  pPos->pKey = taosMemoryCalloc(1, pFileState->keyLen);
9,112,860!
549
  if (!pPos->pKey) {
9,109,534!
550
    code = terrno;
×
551
    QUERY_CHECK_CODE(code, lino, _error);
×
552
  }
553

554
  void* pBuff = getFreeBuff(pFileState);
9,109,534✔
555
  if (pBuff) {
9,094,488✔
556
    pPos->pRowBuff = pBuff;
295,890✔
557
    goto _end;
295,890✔
558
  }
559

560
  if (pFileState->curRowCount < pFileState->maxRowCount) {
8,798,598✔
561
    pBuff = taosMemoryCalloc(1, pFileState->rowSize);
8,797,814!
562
    QUERY_CHECK_NULL(pBuff, code, lino, _error, terrno);
8,807,677!
563
    pPos->pRowBuff = pBuff;
8,807,677✔
564
    pFileState->curRowCount++;
8,807,677✔
565
    goto _end;
8,807,677✔
566
  }
567

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

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

573
_end:
9,103,720✔
574
  code = tdListAppend(pFileState->usedBuffs, &pPos);
9,103,720✔
575
  QUERY_CHECK_CODE(code, lino, _error);
9,109,759!
576

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

584
  return pPos;
9,109,759✔
585
}
586

587
SRowBuffPos* getNewRowPosForWrite(SStreamFileState* pFileState) {
9,099,477✔
588
  int32_t      code = TSDB_CODE_SUCCESS;
9,099,477✔
589
  int32_t      lino = 0;
9,099,477✔
590
  SRowBuffPos* newPos = getNewRowPos(pFileState);
9,099,477✔
591
  if (!newPos) {
9,101,787!
592
    code = TSDB_CODE_OUT_OF_MEMORY;
×
593
    QUERY_CHECK_CODE(code, lino, _error);
×
594
  }
595
  newPos->beUsed = true;
9,101,787✔
596
  newPos->beFlushed = false;
9,101,787✔
597
  newPos->needFree = false;
9,101,787✔
598
  newPos->beUpdated = true;
9,101,787✔
599
  return newPos;
9,101,787✔
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,
9,976,295✔
609
                             int32_t* pWinCode) {
610
  int32_t code = TSDB_CODE_SUCCESS;
9,976,295✔
611
  int32_t lino = 0;
9,976,295✔
612
  (*pWinCode) = TSDB_CODE_SUCCESS;
9,976,295✔
613
  pFileState->maxTs = TMAX(pFileState->maxTs, pFileState->getTs(pKey));
9,976,295✔
614
  SRowBuffPos** pos = tSimpleHashGet(pFileState->rowStateBuff, pKey, keyLen);
9,973,546✔
615
  if (pos) {
9,986,671✔
616
    if (pVal != NULL) {
896,167!
617
      *pVLen = pFileState->rowSize;
896,176✔
618
      *pVal = *pos;
896,176✔
619
      (*pos)->beUsed = true;
896,176✔
620
      (*pos)->beFlushed = false;
896,176✔
621
    }
622
    goto _end;
896,167✔
623
  }
624
  SRowBuffPos* pNewPos = getNewRowPosForWrite(pFileState);
9,090,504✔
625
  if (!pNewPos || !pNewPos->pRowBuff) {
9,090,401!
626
    code = TSDB_CODE_OUT_OF_MEMORY;
×
627
    QUERY_CHECK_CODE(code, lino, _end);
×
628
  }
629

630
  memcpy(pNewPos->pKey, pKey, keyLen);
9,090,401✔
631
  (*pWinCode) = TSDB_CODE_FAILED;
9,090,401✔
632

633
  TSKEY ts = pFileState->getTs(pKey);
9,090,401✔
634
  if (!isDeteled(pFileState, ts) && isFlushedState(pFileState, ts, 0)) {
9,083,749✔
635
    int32_t len = 0;
296,957✔
636
    void*   p = NULL;
296,957✔
637
    (*pWinCode) = pFileState->stateFileGetFn(pFileState, pKey, &p, &len);
296,957✔
638
    qDebug("===stream===get %" PRId64 " from disc, res %d", ts, (*pWinCode));
296,957✔
639
    if ((*pWinCode) == TSDB_CODE_SUCCESS) {
296,957✔
640
      memcpy(pNewPos->pRowBuff, p, len);
574✔
641
    }
642
    taosMemoryFree(p);
296,957!
643
  }
644

645
  code = tSimpleHashPut(pFileState->rowStateBuff, pKey, keyLen, &pNewPos, POINTER_BYTES);
9,080,718✔
646
  QUERY_CHECK_CODE(code, lino, _end);
9,081,047!
647

648
  if (pVal) {
9,081,047!
649
    *pVLen = pFileState->rowSize;
9,081,175✔
650
    *pVal = pNewPos;
9,081,175✔
651
  }
652

653
_end:
×
654
  if (code != TSDB_CODE_SUCCESS) {
9,977,214!
655
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
656
  }
657
  return code;
9,973,190✔
658
}
659

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

670
void deleteRowBuffByGroupId(SStreamFileState* pFileState, uint64_t groupId) {
55✔
671
  SSHashObj* pRowMap = pFileState->rowStateBuff;
55✔
672
  void*   pIte = NULL;
55✔
673
  int32_t iter = 0;
55✔
674
  while ((pIte = tSimpleHashIterate(pRowMap, pIte, &iter)) != NULL) {
132✔
675
    size_t keyLen = 0;
77✔
676
    SWinKey* pKey = tSimpleHashGetKey(pIte, &keyLen);
77!
677
    if (pKey->groupId == groupId) {
77!
678
      int32_t tmpRes = tSimpleHashIterateRemove(pRowMap, pKey, keyLen, &pIte, &iter);
77✔
679
      qTrace("%s at line %d res:%d", __func__, __LINE__, tmpRes);
77!
680
    }
681
  }
682

683
  while (1) {
333✔
684
    SWinKey tmp = {.ts = INT64_MIN, .groupId = groupId};
388✔
685
    SStreamStateCur* pCur = streamStateSeekKeyNext_rocksdb(pFileState->pFileStore, &tmp);
388✔
686
    SWinKey delKey = {.groupId = groupId};
388✔
687
    int32_t code = streamStateGetGroupKVByCur_rocksdb(pFileState->pFileStore, pCur, &delKey, NULL, 0);
388✔
688
    if (code != TSDB_CODE_SUCCESS) {
388✔
689
      break;
55✔
690
    }
691
    code = streamStateDel_rocksdb(pFileState->pFileStore, &delKey);
333✔
692
    qTrace("%s at line %d res:%d", __func__, __LINE__, code);
333!
693
  }
694
}
55✔
695

696
static int32_t recoverSessionRowBuff(SStreamFileState* pFileState, SRowBuffPos* pPos) {
371✔
697
  int32_t code = TSDB_CODE_SUCCESS;
371✔
698
  int32_t lino = 0;
371✔
699
  int32_t len = 0;
371✔
700
  void*   pBuff = NULL;
371✔
701
  code = pFileState->stateFileGetFn(pFileState, pPos->pKey, &pBuff, &len);
371✔
702
  QUERY_CHECK_CODE(code, lino, _end);
371!
703
  memcpy(pPos->pRowBuff, pBuff, len);
371✔
704
  taosMemoryFree(pBuff);
371!
705

706
_end:
371✔
707
  if (code != TSDB_CODE_SUCCESS) {
371!
708
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
709
  }
710
  return code;
371✔
711
}
712

713
static int32_t recoverStateRowBuff(SStreamFileState* pFileState, SRowBuffPos* pPos) {
371✔
714
  int32_t code = TSDB_CODE_SUCCESS;
371✔
715
  int32_t lino = 0;
371✔
716
  pPos->pRowBuff = getFreeBuff(pFileState);
371✔
717
  if (!pPos->pRowBuff) {
371✔
718
    if (pFileState->curRowCount < pFileState->maxRowCount) {
81!
719
      pPos->pRowBuff = taosMemoryCalloc(1, pFileState->rowSize);
×
720
      if (!pPos->pRowBuff) {
×
721
        code = terrno;
×
722
        QUERY_CHECK_CODE(code, lino, _end);
×
723
      }
724
      pFileState->curRowCount++;
×
725
    } else {
726
      code = clearRowBuff(pFileState);
81✔
727
      QUERY_CHECK_CODE(code, lino, _end);
81!
728
      pPos->pRowBuff = getFreeBuff(pFileState);
81✔
729
    }
730
    QUERY_CHECK_CONDITION((pPos->pRowBuff != NULL), code, lino, _end, TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
81!
731
  }
732

733
  code = recoverSessionRowBuff(pFileState, pPos);
371✔
734
  QUERY_CHECK_CODE(code, lino, _end);
371!
735

736
_end:
371✔
737
  if (code != TSDB_CODE_SUCCESS) {
371!
738
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
739
  }
740
  return code;
371✔
741
}
742

743
int32_t getRowBuffByPos(SStreamFileState* pFileState, SRowBuffPos* pPos, void** pVal) {
8,389,036✔
744
  int32_t code = TSDB_CODE_SUCCESS;
8,389,036✔
745
  int32_t lino = 0;
8,389,036✔
746
  if (pPos->pRowBuff) {
8,389,036✔
747
    if (pPos->needFree) {
8,388,706!
748
      code = recoverSessionRowBuff(pFileState, pPos);
×
749
      QUERY_CHECK_CODE(code, lino, _end);
×
750
    }
751
    (*pVal) = pPos->pRowBuff;
8,388,706✔
752
    goto _end;
8,388,706✔
753
  }
754

755
  code = recoverStateRowBuff(pFileState, pPos);
330✔
756
  QUERY_CHECK_CODE(code, lino, _end);
371!
757

758
  (*pVal) = pPos->pRowBuff;
371✔
759
  if (!pPos->needFree) {
371✔
760
    code = tdListPrepend(pFileState->usedBuffs, &pPos);
116✔
761
    QUERY_CHECK_CODE(code, lino, _end);
116!
762
  }
763

764
_end:
371✔
765
  if (code != TSDB_CODE_SUCCESS) {
8,389,077!
766
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
767
  }
768
  return code;
8,389,005✔
769
}
770

771
bool hasRowBuff(SStreamFileState* pFileState, void* pKey, int32_t keyLen) {
28✔
772
  SRowBuffPos** pos = tSimpleHashGet(pFileState->rowStateBuff, pKey, keyLen);
28✔
773
  if (pos) {
28✔
774
    return true;
13✔
775
  }
776
  return false;
15✔
777
}
778

779
SStreamSnapshot* getSnapshot(SStreamFileState* pFileState) {
4,659✔
780
  int64_t mark = (pFileState->deleteMark == INT64_MAX || pFileState->maxTs == INT64_MIN)
1,936✔
781
                     ? INT64_MIN
782
                     : pFileState->maxTs - pFileState->deleteMark;
6,595✔
783
  clearExpiredRowBuff(pFileState, mark, false);
4,659✔
784
  return pFileState->usedBuffs;
4,666✔
785
}
786

787
void flushSnapshot(SStreamFileState* pFileState, SStreamSnapshot* pSnapshot, bool flushState) {
8,623✔
788
  int32_t   code = TSDB_CODE_SUCCESS;
8,623✔
789
  int32_t   lino = 0;
8,623✔
790
  SListIter iter = {0};
8,623✔
791
  tdListInitIter(pSnapshot, &iter, TD_LIST_FORWARD);
8,623✔
792

793
  const int32_t BATCH_LIMIT = 256;
8,624✔
794

795
  int64_t    st = taosGetTimestampMs();
8,624✔
796
  SListNode* pNode = NULL;
8,624✔
797

798
  int idx = streamStateGetCfIdx(pFileState->pFileStore, pFileState->cfName);
8,624✔
799

800
  int32_t len = (pFileState->rowSize + sizeof(uint64_t) + sizeof(int32_t) + 64) * 2;
8,629✔
801
  char*   buf = taosMemoryCalloc(1, len);
8,629!
802
  if (!buf) {
8,624!
803
    code = terrno;
×
804
    QUERY_CHECK_CODE(code, lino, _end);
×
805
  }
806

807
  void* batch = streamStateCreateBatch();
8,624✔
808
  if (!batch) {
8,625!
809
    code = TSDB_CODE_OUT_OF_MEMORY;
×
810
    QUERY_CHECK_CODE(code, lino, _end);
×
811
  }
812

813
  while ((pNode = tdListNext(&iter)) != NULL && code == TSDB_CODE_SUCCESS) {
2,608,416✔
814
    SRowBuffPos* pPos = *(SRowBuffPos**)pNode->data;
2,599,678✔
815
    if (pPos->beFlushed || !pPos->pRowBuff) {
2,599,678!
816
      continue;
688,153✔
817
    }
818
    pPos->beFlushed = true;
1,911,525✔
819
    pFileState->flushMark = TMAX(pFileState->flushMark, pFileState->getTs(pPos->pKey));
1,911,525✔
820

821
    qDebug("===stream===flushed start:%" PRId64, pFileState->getTs(pPos->pKey));
1,911,467✔
822
    if (streamStateGetBatchSize(batch) >= BATCH_LIMIT) {
1,911,469✔
823
      code = streamStatePutBatch_rocksdb(pFileState->pFileStore, batch);
7,383✔
824
      streamStateClearBatch(batch);
7,383✔
825
      QUERY_CHECK_CODE(code, lino, _end);
7,383!
826
    }
827

828
    void* pSKey = pFileState->stateBuffCreateStateKeyFn(pPos, ((SStreamState*)pFileState->pFileStore)->number);
1,911,695✔
829
    QUERY_CHECK_NULL(pSKey, code, lino, _end, terrno);
1,911,503!
830

831
    code = streamStatePutBatchOptimize(pFileState->pFileStore, idx, batch, pSKey, pPos->pRowBuff, pFileState->rowSize,
1,911,503✔
832
                                       0, buf);
833
    taosMemoryFreeClear(pSKey);
1,911,626!
834
    QUERY_CHECK_CODE(code, lino, _end);
1,911,638!
835
    // todo handle failure
836
    memset(buf, 0, len);
1,911,638✔
837
  }
838
  taosMemoryFreeClear(buf);
8,667!
839

840
  int32_t numOfElems = streamStateGetBatchSize(batch);
8,675✔
841
  if (numOfElems > 0) {
8,624✔
842
    code = streamStatePutBatch_rocksdb(pFileState->pFileStore, batch);
1,862✔
843
    QUERY_CHECK_CODE(code, lino, _end);
1,862!
844
  } else {
845
    goto _end;
6,762✔
846
  }
847

848
  streamStateClearBatch(batch);
1,862✔
849

850
  clearSearchBuff(pFileState);
1,862✔
851

852
  int64_t elapsed = taosGetTimestampMs() - st;
1,862✔
853
  qDebug("%s flush to disk in batch model completed, rows:%d, batch size:%d, elapsed time:%" PRId64 "ms",
1,862✔
854
         pFileState->id, numOfElems, BATCH_LIMIT, elapsed);
855

856
  if (flushState) {
1,862✔
857
    void*   valBuf = NULL;
1,162✔
858
    int32_t len = 0;
1,162✔
859
    code = streamFileStateEncode(&pFileState->flushMark, &valBuf, &len);
1,162✔
860
    QUERY_CHECK_CODE(code, lino, _end);
1,162!
861

862
    qDebug("===stream===flushMark write:%" PRId64, pFileState->flushMark);
1,162✔
863
    code = streamStatePutBatch(pFileState->pFileStore, "default", batch, STREAM_STATE_INFO_NAME, valBuf, len, 0);
1,162✔
864
    taosMemoryFree(valBuf);
1,162!
865
    QUERY_CHECK_CODE(code, lino, _end);
1,162!
866

867
    code = streamStatePutBatch_rocksdb(pFileState->pFileStore, batch);
1,162✔
868
    QUERY_CHECK_CODE(code, lino, _end);
1,162!
869
  }
870

871
_end:
700✔
872
  if (code != TSDB_CODE_SUCCESS) {
8,624!
873
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
874
  }
875
  taosMemoryFree(buf);
8,624!
876
  streamStateDestroyBatch(batch);
8,610✔
877
}
8,625✔
878

879
int32_t forceRemoveCheckpoint(SStreamFileState* pFileState, int64_t checkpointId) {
×
880
  char keyBuf[128] = {0};
×
881
  TAOS_UNUSED(tsnprintf(keyBuf, sizeof(keyBuf), "%s:%" PRId64 "", TASK_KEY, checkpointId));
×
882
  return streamDefaultDel_rocksdb(pFileState->pFileStore, keyBuf);
×
883
}
884

885
int32_t deleteExpiredCheckPoint(SStreamFileState* pFileState, TSKEY mark) {
×
886
  int32_t code = TSDB_CODE_SUCCESS;
×
887
  int64_t maxCheckPointId = 0;
×
888
  {
889
    char    buf[128] = {0};
×
890
    void*   val = NULL;
×
891
    int32_t len = 0;
×
892
    memcpy(buf, TASK_KEY, strlen(TASK_KEY));
×
893
    code = streamDefaultGet_rocksdb(pFileState->pFileStore, buf, &val, &len);
×
894
    if (code != 0 || len == 0 || val == NULL) {
×
895
      return TSDB_CODE_FAILED;
×
896
    }
897
    memcpy(buf, val, len);
×
898
    buf[len] = 0;
×
899
    maxCheckPointId = taosStr2Int64((char*)buf, NULL, 10);
×
900
    taosMemoryFree(val);
×
901
  }
902
  for (int64_t i = maxCheckPointId; i > 0; i--) {
×
903
    char    buf[128] = {0};
×
904
    void*   val = 0;
×
905
    int32_t len = 0;
×
906
    TAOS_UNUSED(tsnprintf(buf, sizeof(buf), "%s:%" PRId64 "", TASK_KEY, i));
×
907
    code = streamDefaultGet_rocksdb(pFileState->pFileStore, buf, &val, &len);
×
908
    if (code != 0) {
×
909
      return TSDB_CODE_FAILED;
×
910
    }
911
    memcpy(buf, val, len);
×
912
    buf[len] = 0;
×
913
    taosMemoryFree(val);
×
914

915
    TSKEY ts;
916
    ts = taosStr2Int64((char*)buf, NULL, 10);
×
917
    if (ts < mark) {
×
918
      // statekey winkey.ts < mark
919
      int32_t tmpRes = forceRemoveCheckpoint(pFileState, i);
×
920
      qTrace("%s at line %d res:%d", __func__, __LINE__, tmpRes);
×
921
      break;
×
922
    }
923
  }
924
  return code;
×
925
}
926

927
int32_t recoverSession(SStreamFileState* pFileState, int64_t ckId) {
1,787✔
928
  int32_t code = TSDB_CODE_SUCCESS;
1,787✔
929
  int32_t lino = 0;
1,787✔
930
  int32_t winRes = TSDB_CODE_SUCCESS;
1,787✔
931
  if (pFileState->maxTs != INT64_MIN) {
1,787!
932
    int64_t mark = (INT64_MIN + pFileState->deleteMark >= pFileState->maxTs)
×
933
                       ? INT64_MIN
934
                       : pFileState->maxTs - pFileState->deleteMark;
×
935
    int32_t tmpRes = deleteExpiredCheckPoint(pFileState, mark);
×
936
    qTrace("%s at line %d res:%d", __func__, __LINE__, tmpRes);
×
937
  }
938

939
  SStreamStateCur* pCur = streamStateSessionSeekToLast_rocksdb(pFileState->pFileStore, INT64_MAX);
1,787✔
940
  int32_t          recoverNum = TMIN(MIN_NUM_OF_RECOVER_ROW_BUFF, pFileState->maxRowCount);
1,787✔
941
  while (winRes == TSDB_CODE_SUCCESS) {
1,787!
942
    if (pFileState->curRowCount >= recoverNum) {
1,787!
943
      break;
1,787✔
944
    }
945

946
    void*       pVal = NULL;
1,787✔
947
    int32_t     vlen = 0;
1,787✔
948
    SSessionKey key = {0};
1,787✔
949
    winRes = streamStateSessionGetKVByCur_rocksdb(getStateFileStore(pFileState), pCur, &key, &pVal, &vlen);
1,787✔
950
    if (winRes != TSDB_CODE_SUCCESS) {
1,787!
951
      break;
1,787✔
952
    }
953

954
    if (vlen != pFileState->rowSize) {
×
955
      code = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
956
      QUERY_CHECK_CODE(code, lino, _end);
×
957
    }
958

959
    SRowBuffPos* pPos = createSessionWinBuff(pFileState, &key, pVal, &vlen);
×
960
    pPos->beUsed = false;
×
961
    winRes = putSessionWinResultBuff(pFileState, pPos);
×
962
    if (winRes != TSDB_CODE_SUCCESS) {
×
963
      break;
×
964
    }
965

966
    winRes = streamStateSessionCurPrev_rocksdb(pCur);
×
967
  }
968

969
_end:
×
970
  if (code != TSDB_CODE_SUCCESS) {
1,787!
971
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
972
  }
973
  streamStateFreeCur(pCur);
1,787✔
974
  return code;
1,787✔
975
}
976

977
int32_t recoverSnapshot(SStreamFileState* pFileState, int64_t ckId) {
4,112✔
978
  int32_t code = TSDB_CODE_SUCCESS;
4,112✔
979
  int32_t lino = 0;
4,112✔
980
  int32_t winCode = TSDB_CODE_SUCCESS;
4,112✔
981
  if (pFileState->maxTs != INT64_MIN) {
4,112!
982
    int64_t mark = (INT64_MIN + pFileState->deleteMark >= pFileState->maxTs)
×
983
                       ? INT64_MIN
984
                       : pFileState->maxTs - pFileState->deleteMark;
×
985
    int32_t tmpRes = deleteExpiredCheckPoint(pFileState, mark);
×
986
    qTrace("%s at line %d res:%d", __func__, __LINE__, tmpRes);
×
987
  }
988

989
  SStreamStateCur* pCur = streamStateSeekToLast_rocksdb(pFileState->pFileStore);
4,112✔
990
  int32_t          recoverNum = TMIN(MIN_NUM_OF_RECOVER_ROW_BUFF, pFileState->maxRowCount);
4,114✔
991
  while (winCode == TSDB_CODE_SUCCESS) {
4,114!
992
    if (pFileState->curRowCount >= recoverNum) {
4,114!
993
      break;
4,114✔
994
    }
995

996
    void*        pVal = NULL;
4,114✔
997
    int32_t      vlen = 0;
4,114✔
998
    SRowBuffPos* pNewPos = getNewRowPosForWrite(pFileState);
4,114✔
999
    if (!pNewPos || !pNewPos->pRowBuff) {
4,114!
1000
      code = TSDB_CODE_OUT_OF_MEMORY;
×
1001
      QUERY_CHECK_CODE(code, lino, _end);
×
1002
    }
1003

1004
    winCode = streamStateGetKVByCur_rocksdb(getStateFileStore(pFileState), pCur, pNewPos->pKey, (const void**)&pVal, &vlen);
4,114✔
1005
    qDebug("===stream=== get state by cur winres:%d. %s", winCode, __func__);
4,114✔
1006
    if (winCode != TSDB_CODE_SUCCESS || pFileState->getTs(pNewPos->pKey) < pFileState->flushMark) {
4,114!
1007
      destroyRowBuffPos(pNewPos);
4,114✔
1008
      SListNode* pNode = tdListPopTail(pFileState->usedBuffs);
4,114✔
1009
      taosMemoryFreeClear(pNode);
4,114!
1010
      taosMemoryFreeClear(pVal);
4,114!
1011
      break;
4,114✔
1012
    }
1013
    if (vlen != pFileState->rowSize) {
×
1014
      qError("row size mismatch, expect:%d, actual:%d", pFileState->rowSize, vlen);
×
1015
      code = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
1016
      taosMemoryFreeClear(pVal);
×
1017
      QUERY_CHECK_CODE(code, lino, _end);
×
1018
    }
1019
    memcpy(pNewPos->pRowBuff, pVal, vlen);
×
1020
    taosMemoryFreeClear(pVal);
×
1021
    pNewPos->beFlushed = true;
×
1022
    pNewPos->beUsed = false;
×
1023
    qDebug("===stream=== read checkpoint state from disc. %s", __func__);
×
1024
    code = tSimpleHashPut(pFileState->rowStateBuff, pNewPos->pKey, pFileState->keyLen, &pNewPos, POINTER_BYTES);
×
1025
    if (code != TSDB_CODE_SUCCESS) {
×
1026
      destroyRowBuffPos(pNewPos);
×
1027
      break;
×
1028
    }
1029
    streamStateCurPrev_rocksdb(pCur);
×
1030
  }
1031

1032
_end:
×
1033
  if (code != TSDB_CODE_SUCCESS) {
4,114!
1034
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1035
  }
1036
  streamStateFreeCur(pCur);
4,114✔
1037
  return code;
4,114✔
1038
}
1039

1040
int32_t streamFileStateGetSelectRowSize(SStreamFileState* pFileState) { return pFileState->selectivityRowSize; }
248,994✔
1041

1042
void streamFileStateReloadInfo(SStreamFileState* pFileState, TSKEY ts) {
1,503✔
1043
  pFileState->flushMark = TMAX(pFileState->flushMark, ts);
1,503✔
1044
  pFileState->maxTs = TMAX(pFileState->maxTs, ts);
1,503✔
1045
}
1,503✔
1046

1047
void* getRowStateBuff(SStreamFileState* pFileState) { return pFileState->rowStateBuff; }
41,708✔
1048
void* getSearchBuff(SStreamFileState* pFileState) { return pFileState->searchBuff; }
9,728,343✔
1049

1050
void* getStateFileStore(SStreamFileState* pFileState) { return pFileState->pFileStore; }
21,664✔
1051

1052
bool isDeteled(SStreamFileState* pFileState, TSKEY ts) {
9,084,940✔
1053
  return pFileState->deleteMark != INT64_MAX && pFileState->maxTs > 0 &&
14,868,126!
1054
         ts < (pFileState->maxTs - pFileState->deleteMark);
5,783,186✔
1055
}
1056

1057
bool isFlushedState(SStreamFileState* pFileState, TSKEY ts, TSKEY gap) { return ts <= (pFileState->flushMark + gap); }
6,595,067✔
1058

1059
TSKEY getFlushMark(SStreamFileState* pFileState) { return pFileState->flushMark; };
647✔
1060

1061
int32_t getRowStateRowSize(SStreamFileState* pFileState) { return pFileState->rowSize; }
250,034✔
1062

1063
int32_t getFunctionRowBuff(SStreamFileState* pFileState, void* pKey, int32_t keyLen, void** pVal, int32_t* pVLen) {
248,993✔
1064
  int32_t winCode = TSDB_CODE_SUCCESS;
248,993✔
1065
  return pFileState->stateFunctionGetFn(pFileState, pKey, keyLen, pVal, pVLen, &winCode);
248,993✔
1066
}
1067

1068
int32_t recoverFillSnapshot(SStreamFileState* pFileState, int64_t ckId) {
265✔
1069
  int32_t code = TSDB_CODE_SUCCESS;
265✔
1070
  int32_t lino = 0;
265✔
1071
  if (pFileState->maxTs != INT64_MIN) {
265!
1072
    int64_t mark = (INT64_MIN + pFileState->deleteMark >= pFileState->maxTs)
×
1073
                       ? INT64_MIN
1074
                       : pFileState->maxTs - pFileState->deleteMark;
×
1075
    code = deleteExpiredCheckPoint(pFileState, mark);
×
1076
    QUERY_CHECK_CODE(code, lino, _end);
×
1077
  }
1078

1079
  SStreamStateCur* pCur = streamStateFillSeekToLast_rocksdb(pFileState->pFileStore);
265✔
1080
  if (pCur == NULL) {
265✔
1081
    return code;
263✔
1082
  }
1083
  int32_t recoverNum = TMIN(MIN_NUM_OF_RECOVER_ROW_BUFF, pFileState->maxRowCount);
2✔
1084
  int32_t winRes = TSDB_CODE_SUCCESS;
2✔
1085
  while (winRes == TSDB_CODE_SUCCESS) {
4!
1086
    if (pFileState->curRowCount >= recoverNum) {
4!
1087
      break;
2✔
1088
    }
1089

1090
    void*        pVal = NULL;
4✔
1091
    int32_t      vlen = 0;
4✔
1092
    SRowBuffPos* pNewPos = getNewRowPosForWrite(pFileState);
4✔
1093
    winRes = streamStateFillGetKVByCur_rocksdb(pCur, pNewPos->pKey, (const void**)&pVal, &vlen);
4✔
1094
    qDebug("===stream=== get state by cur winres:%d. %s", winRes, __func__);
4!
1095
    if (winRes != TSDB_CODE_SUCCESS || isFlushedState(pFileState, pFileState->getTs(pNewPos->pKey), 0)) {
4!
1096
      destroyRowBuffPos(pNewPos);
2✔
1097
      SListNode* pNode = tdListPopTail(pFileState->usedBuffs);
2✔
1098
      taosMemoryFreeClear(pNode);
2!
1099
      taosMemoryFreeClear(pVal);
2!
1100
      break;
2✔
1101
    }
1102

1103
    if (vlen != pFileState->rowSize) {
2!
1104
      qError("row size mismatch, expect:%d, actual:%d", pFileState->rowSize, vlen);
×
1105
      destroyRowBuffPos(pNewPos);
×
1106
      code = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
1107
      taosMemoryFreeClear(pVal);
×
1108
      QUERY_CHECK_CODE(code, lino, _end);
×
1109
    }
1110

1111
    memcpy(pNewPos->pRowBuff, pVal, vlen);
2✔
1112
    taosMemoryFreeClear(pVal);
2!
1113
    pNewPos->beFlushed = true;
2✔
1114
    pNewPos->beUsed = false;
2✔
1115
    qDebug("===stream=== read checkpoint state from disc. %s", __func__);
2!
1116
    winRes = tSimpleHashPut(pFileState->rowStateBuff, pNewPos->pKey, pFileState->keyLen, &pNewPos, POINTER_BYTES);
2✔
1117
    if (winRes != TSDB_CODE_SUCCESS) {
2!
1118
      destroyRowBuffPos(pNewPos);
×
1119
      break;
×
1120
    }
1121
    streamStateCurPrev_rocksdb(pCur);
2✔
1122
  }
1123
  streamStateFreeCur(pCur);
2✔
1124

1125
_end:
2✔
1126
  if (code != TSDB_CODE_SUCCESS) {
2!
1127
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1128
  }
1129
  return code;
2✔
1130
}
1131

1132
int32_t getRowBuff(SStreamFileState* pFileState, void* pKey, int32_t keyLen, void** pVal, int32_t* pVLen,
1,769✔
1133
                   int32_t* pWinCode) {
1134
  int32_t code = TSDB_CODE_SUCCESS;
1,769✔
1135
  int32_t lino = 0;
1,769✔
1136
  (*pWinCode) = TSDB_CODE_FAILED;
1,769✔
1137
  pFileState->maxTs = TMAX(pFileState->maxTs, pFileState->getTs(pKey));
1,769✔
1138
  SRowBuffPos** ppPos = tSimpleHashGet(pFileState->rowStateBuff, pKey, keyLen);
1,769✔
1139
  if (ppPos) {
1,769✔
1140
    *pVLen = pFileState->rowSize;
839✔
1141
    *pVal = *ppPos;
839✔
1142
    (*ppPos)->beUsed = true;
839✔
1143
    (*ppPos)->beFlushed = false;
839✔
1144
    (*pWinCode) = TSDB_CODE_SUCCESS;
839✔
1145
    if ((*ppPos)->pRowBuff == NULL) {
839!
1146
      code = recoverStateRowBuff(pFileState, *ppPos);
×
1147
      QUERY_CHECK_CODE(code, lino, _end);
×
1148
    }
1149
    goto _end;
839✔
1150
  }
1151
  TSKEY ts = pFileState->getTs(pKey);
930✔
1152
  if (!isDeteled(pFileState, ts) && isFlushedState(pFileState, ts, 0)) {
930!
1153
    int32_t len = 0;
26✔
1154
    void*   p = NULL;
26✔
1155
    (*pWinCode) = pFileState->stateFileGetFn(pFileState, pKey, &p, &len);
26✔
1156
    qDebug("===stream===get %" PRId64 " from disc, res %d", ts, (*pWinCode));
26!
1157
    if ((*pWinCode) == TSDB_CODE_SUCCESS) {
26✔
1158
      SRowBuffPos* pNewPos = getNewRowPosForWrite(pFileState);
21✔
1159
      if (!pNewPos || !pNewPos->pRowBuff) {
21!
1160
        code = TSDB_CODE_OUT_OF_MEMORY;
×
1161
        QUERY_CHECK_CODE(code, lino, _end);
×
1162
      }
1163

1164
      memcpy(pNewPos->pKey, pKey, keyLen);
21✔
1165
      memcpy(pNewPos->pRowBuff, p, len);
21✔
1166
      code = tSimpleHashPut(pFileState->rowStateBuff, pKey, keyLen, &pNewPos, POINTER_BYTES);
21✔
1167
      QUERY_CHECK_CODE(code, lino, _end);
21!
1168

1169
      if (pVal) {
21!
1170
        *pVLen = pFileState->rowSize;
21✔
1171
        *pVal = pNewPos;
21✔
1172
      }
1173
    }
1174
    taosMemoryFree(p);
26!
1175
  }
1176

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

1184
int32_t streamFileStateGroupPut(SStreamFileState* pFileState, int64_t groupId, void* value, int32_t vLen) {
472✔
1185
  int32_t code = TSDB_CODE_SUCCESS;
472✔
1186
  int32_t lino = 0;
472✔
1187
  if (value != NULL) {
472!
1188
    code = TSDB_CODE_INVALID_PARA;
×
1189
    QUERY_CHECK_CODE(code, lino, _end);
×
1190
  }
1191

1192
  if (tSimpleHashGet(pFileState->pGroupIdMap, &groupId, sizeof(int64_t)) == NULL) {
472✔
1193
    if (tSimpleHashGetSize(pFileState->pGroupIdMap) <= MAX_GROUP_ID_NUM) {
169!
1194
      code = tSimpleHashPut(pFileState->pGroupIdMap, &groupId, sizeof(int64_t), NULL, 0);
169✔
1195
      QUERY_CHECK_CODE(code, lino, _end);
169!
1196
    }
1197
    code = streamStatePutParTag_rocksdb(pFileState->pFileStore, groupId, value, vLen);
169✔
1198
    QUERY_CHECK_CODE(code, lino, _end);
169!
1199
  }
1200

1201
_end:
472✔
1202
  if (code != TSDB_CODE_SUCCESS) {
472!
1203
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1204
  }
1205
  return code;
472✔
1206
}
1207

1208
void streamFileStateGroupCurNext(SStreamStateCur* pCur) {
1,321✔
1209
  SStreamFileState* pFileState = (SStreamFileState*)pCur->pStreamFileState;
1,321✔
1210
  if (pCur->hashIter == -1) {
1,321✔
1211
    streamStateCurNext(pFileState->pFileStore, pCur);
68✔
1212
    return;
68✔
1213
  }
1214

1215
  int64_t gpId = *(int64_t*)tSimpleHashGetKey(pCur->pHashData, NULL);
1,253!
1216
  pCur->minGpId = TMAX(pCur->minGpId, gpId);
1,253✔
1217

1218
  SSHashObj* pHash = pFileState->pGroupIdMap;
1,253✔
1219
  pCur->pHashData = tSimpleHashIterate(pHash, pCur->pHashData, &pCur->hashIter);
1,253✔
1220
  if (!pCur->pHashData) {
1,253✔
1221
    pCur->hashIter = -1;
774✔
1222
    streamStateParTagSeekKeyNext_rocksdb(pFileState->pFileStore, pCur->minGpId, pCur);
774✔
1223
    return;
774✔
1224
  }
1225
}
1226

1227
int32_t streamFileStateGroupGetKVByCur(SStreamStateCur* pCur, int64_t* pKey, void** pVal, int32_t* pVLen) {
4,026✔
1228
  int32_t code = TSDB_CODE_SUCCESS;
4,026✔
1229
  if (pCur->pHashData) {
4,026✔
1230
    *pKey = *(int64_t*)tSimpleHashGetKey(pCur->pHashData, NULL);
1,253!
1231
    return code;
1,253✔
1232
  }
1233
  return streamStateParTagGetKVByCur_rocksdb(pCur, pKey, NULL, NULL);
2,773✔
1234
}
1235

1236
SSHashObj* getGroupIdCache(SStreamFileState* pFileState) {
2,705✔
1237
  return pFileState->pGroupIdMap;
2,705✔
1238
}
1239

1240
void clearExpiredState(SStreamFileState* pFileState) {
4,363✔
1241
  int32_t    code = TSDB_CODE_SUCCESS;
4,363✔
1242
  int32_t    lino = 0;
4,363✔
1243
  SSHashObj* pSearchBuff = pFileState->searchBuff;
4,363✔
1244
  void*      pIte = NULL;
4,363✔
1245
  int32_t    iter = 0;
4,363✔
1246
  while ((pIte = tSimpleHashIterate(pSearchBuff, pIte, &iter)) != NULL) {
6,459✔
1247
    SArray* pWinStates = *((void**)pIte);
2,096✔
1248
    int32_t size = taosArrayGetSize(pWinStates);
2,096✔
1249
    for (int32_t i = 0; i < size - 1; i++) {
2,529✔
1250
      SWinKey* pKey = taosArrayGet(pWinStates, i);
433✔
1251
      int32_t  code_buff = pFileState->stateBuffRemoveFn(pFileState->rowStateBuff, pKey, sizeof(SWinKey));
433✔
1252
      qTrace("clear expired buff, ts:%" PRId64 ". %s at line %d res:%d", pKey->ts, __func__, __LINE__, code_buff);
433!
1253

1254
      if (isFlushedState(pFileState, pKey->ts, 0)) {
433✔
1255
        int32_t code_file = pFileState->stateFileRemoveFn(pFileState, pKey);
320✔
1256
        qTrace("clear expired file, ts:%" PRId64 ". %s at line %d res:%d", pKey->ts, __func__, __LINE__, code_file);
320!
1257
      }
1258
    }
1259
    taosArrayRemoveBatch(pWinStates, 0, size - 1, NULL);
2,096✔
1260
  }
1261
  code = clearRowBuff(pFileState);
4,363✔
1262
  QUERY_CHECK_CODE(code, lino, _end);
4,364!
1263

1264
_end:
4,364✔
1265
  if (code != TSDB_CODE_SUCCESS) {
4,364!
1266
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1267
  }
1268
}
4,364✔
1269

1270
#ifdef BUILD_NO_CALL
1271
int32_t getStateSearchRowBuff(SStreamFileState* pFileState, const SWinKey* pKey, void** pVal, int32_t* pVLen,
1272
                           int32_t* pWinCode) {
1273
  int32_t code = TSDB_CODE_SUCCESS;
1274
  int32_t lino = 0;
1275

1276
  code = addRowBuffIfNotExist(pFileState, (void*)pKey, sizeof(SWinKey), pVal, pVLen, pWinCode);
1277
  QUERY_CHECK_CODE(code, lino, _end);
1278

1279
  SArray*    pWinStates = NULL;
1280
  SSHashObj* pSearchBuff = getSearchBuff(pFileState);
1281
  void**     ppBuff = tSimpleHashGet(pSearchBuff, &pKey->groupId, sizeof(uint64_t));
1282
  if (ppBuff) {
1283
    pWinStates = (SArray*)(*ppBuff);
1284
  } else {
1285
    pWinStates = taosArrayInit(16, sizeof(SWinKey));
1286
    QUERY_CHECK_NULL(pWinStates, code, lino, _end, terrno);
1287

1288
    code = tSimpleHashPut(pSearchBuff, &pKey->groupId, sizeof(uint64_t), &pWinStates, POINTER_BYTES);
1289
    QUERY_CHECK_CODE(code, lino, _end);
1290
  }
1291

1292
  // recover
1293
  if (taosArrayGetSize(pWinStates) == 0 && needClearDiskBuff(pFileState)) {
1294
    TSKEY            ts = getFlushMark(pFileState);
1295
    SWinKey          start = {.groupId = pKey->groupId, .ts = INT64_MAX};
1296
    void*            pState = getStateFileStore(pFileState);
1297
    SStreamStateCur* pCur = streamStateSeekKeyPrev_rocksdb(pState, &start);
1298
    for (int32_t i = 0; i < NUM_OF_CACHE_WIN; i++) {
1299
      SWinKey tmpKey = {.groupId = pKey->groupId};
1300
      int32_t tmpRes = streamStateGetGroupKVByCur_rocksdb(pState, pCur, &tmpKey, NULL, 0);
1301
      if (tmpRes != TSDB_CODE_SUCCESS) {
1302
        break;
1303
      }
1304
      void* tmp = taosArrayPush(pWinStates, &tmpKey);
1305
      QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
1306
      streamStateCurPrev_rocksdb(pCur);
1307
    }
1308
    taosArraySort(pWinStates, winKeyCmprImpl);
1309
    streamStateFreeCur(pCur);
1310
  }
1311

1312
  int32_t size = taosArrayGetSize(pWinStates);
1313
  int32_t index = binarySearch(pWinStates, size, pKey, fillStateKeyCompare);
1314
  if (!isFlushedState(pFileState, pKey->ts, 0)|| index >= 0) {
1315
    // find the first position which is smaller than the pKey
1316
    if (index >= 0) {
1317
      SWinKey* pTmpKey = taosArrayGet(pWinStates, index);
1318
      if (winKeyCmprImpl(pTmpKey, pKey) == 0) {
1319
        goto _end;
1320
      }
1321
    }
1322
    index++;
1323
    void* tmp = taosArrayInsert(pWinStates, index, pKey);
1324
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
1325
  }
1326

1327
  if (size >= MAX_NUM_OF_CACHE_WIN) {
1328
    int32_t num = size - NUM_OF_CACHE_WIN;
1329
    taosArrayRemoveBatch(pWinStates, 0, num, NULL);
1330
  }
1331

1332
_end:
1333
  if (code != TSDB_CODE_SUCCESS) {
1334
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
1335
  }
1336
  return code;
1337
}
1338
#endif
1339

1340
int32_t getRowStatePrevRow(SStreamFileState* pFileState, const SWinKey* pKey, SWinKey* pResKey, void** ppVal,
107✔
1341
                           int32_t* pVLen, int32_t* pWinCode) {
1342
  int32_t    code = TSDB_CODE_SUCCESS;
107✔
1343
  int32_t    lino = 0;
107✔
1344
  SArray*    pWinStates = NULL;
107✔
1345
  SSHashObj* pSearchBuff = getSearchBuff(pFileState);
107✔
1346
  void*      pState = getStateFileStore(pFileState);
107✔
1347
  void**     ppBuff = (void**) tSimpleHashGet(pSearchBuff, &pKey->groupId, sizeof(uint64_t));
107✔
1348
  if (ppBuff) {
107!
1349
    pWinStates = (SArray*)(*ppBuff);
107✔
1350
  } else {
1351
    qDebug("===stream=== search buff is empty.group id:%" PRId64, pKey->groupId);
×
1352
    SStreamStateCur* pCur = streamStateSeekKeyPrev_rocksdb(pState, pKey);
×
1353
    void*            tmpVal = NULL;
×
1354
    int32_t          len = 0;
×
1355
    (*pWinCode) = streamStateGetGroupKVByCur_rocksdb(pState, pCur, pResKey, (const void**)&tmpVal, &len);
×
1356
    if ((*pWinCode) == TSDB_CODE_SUCCESS) {
×
1357
      SRowBuffPos* pNewPos = getNewRowPosForWrite(pFileState);
×
1358
      if (!pNewPos || !pNewPos->pRowBuff) {
×
1359
        code = TSDB_CODE_OUT_OF_MEMORY;
×
1360
        QUERY_CHECK_CODE(code, lino, _end);
×
1361
      }
1362
      memcpy(pNewPos->pRowBuff, tmpVal, len);
×
1363
      taosMemoryFreeClear(tmpVal);
×
1364
      *pVLen = getRowStateRowSize(pFileState);
×
1365
      (*ppVal) = pNewPos;
×
1366
    }
1367
    streamStateFreeCur(pCur);
×
1368
    return code;
×
1369
  }
1370
  int32_t size = taosArrayGetSize(pWinStates);
107✔
1371
  int32_t index = binarySearch(pWinStates, size, pKey, fillStateKeyCompare);
107✔
1372
  if (index >= 0) {
107!
1373
    SWinKey* pCurKey = taosArrayGet(pWinStates, index);
107✔
1374
    if (winKeyCmprImpl(pCurKey, pKey) == 0) {
107!
1375
      index--;
107✔
1376
    } else {
1377
      qDebug("%s failed at line %d since do not find cur SWinKey. trigger may be force window close", __func__, __LINE__);
×
1378
    }
1379
  }
1380
  if (index == -1) {
107✔
1381
    SStreamStateCur* pCur = streamStateSeekKeyPrev_rocksdb(pState, pKey);
28✔
1382
    void*            tmpVal = NULL;
28✔
1383
    int32_t          len = 0;
28✔
1384
    (*pWinCode) = streamStateGetGroupKVByCur_rocksdb(pState, pCur, pResKey, (const void**)&tmpVal, &len);
28✔
1385
    if ((*pWinCode) == TSDB_CODE_SUCCESS) {
28!
1386
      SRowBuffPos* pNewPos = getNewRowPosForWrite(pFileState);
×
1387
      if (!pNewPos || !pNewPos->pRowBuff) {
×
1388
        code = TSDB_CODE_OUT_OF_MEMORY;
×
1389
        QUERY_CHECK_CODE(code, lino, _end);
×
1390
      }
1391
      memcpy(pNewPos->pRowBuff, tmpVal, len);
×
1392
      taosMemoryFreeClear(tmpVal);
×
1393
      *pVLen = getRowStateRowSize(pFileState);
×
1394
      (*ppVal) = pNewPos;
×
1395
    }
1396
    streamStateFreeCur(pCur);
28✔
1397
    return code;
28✔
1398
  } else {
1399
    SWinKey* pPrevKey = taosArrayGet(pWinStates, index);
79✔
1400
    *pResKey = *pPrevKey;
79✔
1401
    return addRowBuffIfNotExist(pFileState, (void*)pPrevKey, sizeof(SWinKey), ppVal, pVLen, pWinCode);
79✔
1402
  }
1403
  (*pWinCode) = TSDB_CODE_FAILED;
1404

1405
_end:
×
1406
  if (code != TSDB_CODE_SUCCESS) {
×
1407
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1408
  }
1409
  return code;
×
1410
}
1411

1412
int32_t addSearchItem(SStreamFileState* pFileState, SArray* pWinStates, const SWinKey* pKey) {
2,522✔
1413
  int32_t code = TSDB_CODE_SUCCESS;
2,522✔
1414
  int32_t lino = 0;
2,522✔
1415
  int32_t size = taosArrayGetSize(pWinStates);
2,522✔
1416
  int32_t index = binarySearch(pWinStates, size, pKey, fillStateKeyCompare);
2,522✔
1417
  if (!isFlushedState(pFileState, pKey->ts, 0) || index >= 0 || size == 0) {
2,522!
1418
    if (index >= 0) {
2,522✔
1419
      SWinKey* pTmpKey = taosArrayGet(pWinStates, index);
2,212✔
1420
      if (winKeyCmprImpl(pTmpKey, pKey) == 0) {
2,212✔
1421
        goto _end;
1,581✔
1422
      }
1423
    }
1424
    index++;
941✔
1425
    void* tmp = taosArrayInsert(pWinStates, index, pKey);
941✔
1426
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
941!
1427
  }
1428

1429
  if (size >= MAX_NUM_OF_CACHE_WIN) {
941!
1430
    int32_t num = size - NUM_OF_CACHE_WIN;
×
1431
    taosArrayRemoveBatch(pWinStates, 0, num, NULL);
×
1432
  }
1433
_end:
941✔
1434
  if (code != TSDB_CODE_SUCCESS) {
2,522!
1435
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1436
  }
1437
  return code;
2,522✔
1438
}
1439

1440
int32_t addArrayBuffIfNotExist(SSHashObj* pSearchBuff, uint64_t groupId, SArray** ppResStates) {
3,893✔
1441
  int32_t code = TSDB_CODE_SUCCESS;
3,893✔
1442
  int32_t lino = 0; 
3,893✔
1443
  SArray*    pWinStates = NULL;
3,893✔
1444
  void**     ppBuff = tSimpleHashGet(pSearchBuff, &groupId, sizeof(uint64_t));
3,893✔
1445
  if (ppBuff) {
3,893✔
1446
    pWinStates = (SArray*)(*ppBuff);
3,595✔
1447
  } else {
1448
    pWinStates = taosArrayInit(16, sizeof(SWinKey));
298✔
1449
    QUERY_CHECK_NULL(pWinStates, code, lino, _end, terrno);
298!
1450

1451
    code = tSimpleHashPut(pSearchBuff, &groupId, sizeof(uint64_t), &pWinStates, POINTER_BYTES);
298✔
1452
    QUERY_CHECK_CODE(code, lino, _end);
298!
1453
  }
1454

1455
  (*ppResStates) = pWinStates;
3,893✔
1456

1457
_end:
3,893✔
1458
  if (code != TSDB_CODE_SUCCESS) {
3,893!
1459
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1460
  }
1461
  return code;
3,893✔
1462
}
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