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

taosdata / TDengine / #3534

21 Nov 2024 07:36AM UTC coverage: 60.825% (+2.0%) from 58.848%
#3534

push

travis-ci

web-flow
Merge pull request #28810 from taosdata/ehn/add-sync-heartbeat-sent-time-to-log

ehn:add-sync-heartbeat-sent-time-to-log

120023 of 252376 branches covered (47.56%)

Branch coverage included in aggregate %.

43 of 47 new or added lines in 3 files covered. (91.49%)

2254 existing lines in 162 files now uncovered.

200876 of 275203 relevant lines covered (72.99%)

16110754.39 hits per line

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

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

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

87
void stateHashBuffRemoveByPosFn(SStreamFileState* pFileState, SRowBuffPos* pPos) {
5,786,159✔
88
  size_t        keyLen = pFileState->keyLen;
5,786,159✔
89
  SRowBuffPos** ppPos = tSimpleHashGet(pFileState->rowStateBuff, pPos->pKey, keyLen);
5,786,159✔
90
  if (ppPos) {
5,786,537✔
91
    if ((*ppPos) == pPos) {
5,786,481!
92
      int32_t tmpRes = tSimpleHashRemove(pFileState->rowStateBuff, pPos->pKey, keyLen);
5,786,483✔
93
      qTrace("%s at line %d res:%d", __func__, __LINE__, tmpRes);
5,786,512✔
94
    }
95
  }
96
}
5,786,715✔
97

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

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

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

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

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

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

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

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

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

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

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

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

180
  SStreamFileState* pFileState = taosMemoryCalloc(1, sizeof(SStreamFileState));
5,810✔
181
  QUERY_CHECK_NULL(pFileState, code, lino, _end, terrno);
5,811!
182

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

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

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

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

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

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

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

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

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

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

257
  pFileState->hasFillCatch = true;
5,810✔
258

259
  if (type == STREAM_STATE_BUFF_HASH || type == STREAM_STATE_BUFF_HASH_SEARCH) {
5,810✔
260
    code = recoverSnapshot(pFileState, checkpointId);
4,104✔
261
  } else if (type == STREAM_STATE_BUFF_SORT) {
1,706✔
262
    code = recoverSesssion(pFileState, checkpointId);
1,695✔
263
  } else if (type == STREAM_STATE_BUFF_HASH_SORT) {
11!
264
    code = recoverFillSnapshot(pFileState, checkpointId);
11✔
265
  }
266
  QUERY_CHECK_CODE(code, lino, _end);
5,811!
267

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

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

287
void destroyRowBuffPos(SRowBuffPos* pPos) {
15,469,715✔
288
  taosMemoryFreeClear(pPos->pKey);
15,469,715!
289
  taosMemoryFreeClear(pPos->pRowBuff);
15,490,618✔
290
  taosMemoryFree(pPos);
15,487,546✔
291
}
15,487,530✔
292

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

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

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

318
void streamFileStateDestroy(SStreamFileState* pFileState) {
12,788✔
319
  if (!pFileState) {
12,788✔
320
    return;
7,095✔
321
  }
322

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

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

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

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

355
  SListNode* pNode = NULL;
7,808✔
356
  while ((pNode = tdListNext(&iter)) != NULL) {
8,676,357✔
357
    SRowBuffPos* pPos = *(SRowBuffPos**)(pNode->data);
8,668,500✔
358
    if (all || (pFileState->getTs(pPos->pKey) < ts && !pPos->beUsed)) {
8,668,500!
359
      code = putFreeBuff(pFileState, pPos);
6,156,559✔
360
      QUERY_CHECK_CODE(code, lino, _end);
6,156,091!
361

362
      if (!all) {
6,156,091✔
363
        pFileState->stateBuffRemoveByPosFn(pFileState, pPos);
5,786,020✔
364
      }
365
      destroyRowBuffPos(pPos);
6,156,727✔
366
      SListNode* tmp = tdListPopNode(pFileState->usedBuffs, pNode);
6,156,712✔
367
      taosMemoryFreeClear(tmp);
6,156,263!
368
    }
369
  }
370

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

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

384
  SListNode* pNode = NULL;
86✔
385
  while ((pNode = tdListNext(&iter)) != NULL && i < max) {
759✔
386
    SRowBuffPos* pPos = *(SRowBuffPos**)pNode->data;
673✔
387
    if (isFlushedState(pFileState, pFileState->getTs(pPos->pKey), 0)) {
673✔
388
      if (all || !pPos->beUsed) {
270!
389
        if (all && !pPos->pRowBuff) {
159!
390
          continue;
×
391
        }
392
        code = tdListAppend(pFlushList, &pPos);
159✔
393
        QUERY_CHECK_CODE(code, lino, _end);
159!
394

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

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

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

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

422
void streamFileStateReleaseBuff(SStreamFileState* pFileState, SRowBuffPos* pPos, bool used) { pPos->beUsed = used; }
14,958,457✔
423

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

431
  SListNode* pNode = NULL;
49✔
432
  while ((pNode = tdListNext(&iter)) != NULL && i < max) {
316✔
433
    SRowBuffPos* pPos = *(SRowBuffPos**)pNode->data;
267✔
434
    if (pPos->beUsed == used) {
267✔
435
      if (used && !pPos->pRowBuff) {
114!
436
        QUERY_CHECK_CONDITION((pPos->needFree == true), code, lino, _end, TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
×
437
        continue;
×
438
      }
439
      code = tdListAppend(pFlushList, &pPos);
114✔
440
      QUERY_CHECK_CODE(code, lino, _end);
114!
441

442
      pFileState->flushMark = TMAX(pFileState->flushMark, pFileState->getTs(pPos->pKey));
114✔
443
      pFileState->stateBuffRemoveByPosFn(pFileState, pPos);
114✔
444
      SListNode* tmp = tdListPopNode(pFileState->usedBuffs, pNode);
114✔
445
      taosMemoryFreeClear(tmp);
114!
446
      if (pPos->pRowBuff) {
114!
447
        i++;
114✔
448
      }
449
    }
450
  }
451

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

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

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

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

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

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

490
  flushSnapshot(pFileState, pFlushList, false);
86✔
491

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

501
  tdListFreeP(pFlushList, destroyRowBuffPosPtr);
86✔
502

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

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

520
void* getFreeBuff(SStreamFileState* pFileState) {
15,489,913✔
521
  SList*     lists = pFileState->freeBuffs;
15,489,913✔
522
  int32_t    buffSize = pFileState->rowSize;
15,489,913✔
523
  SListNode* pNode = tdListPopHead(lists);
15,489,913✔
524
  if (!pNode) {
15,483,121✔
525
    return NULL;
15,315,172✔
526
  }
527
  void* ptr = *(void**)pNode->data;
167,949✔
528
  memset(ptr, 0, buffSize);
167,949✔
529
  taosMemoryFree(pNode);
167,949✔
530
  return ptr;
167,997✔
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) {
15,465,832✔
540
  int32_t      code = TSDB_CODE_SUCCESS;
15,465,832✔
541
  int32_t      lino = 0;
15,465,832✔
542
  SRowBuffPos* pPos = taosMemoryCalloc(1, sizeof(SRowBuffPos));
15,465,832✔
543
  if (!pPos) {
15,486,122!
544
    code = terrno;
×
545
    QUERY_CHECK_CODE(code, lino, _error);
×
546
  }
547

548
  pPos->pKey = taosMemoryCalloc(1, pFileState->keyLen);
15,486,122✔
549
  if (!pPos->pKey) {
15,491,491!
550
    code = terrno;
×
551
    QUERY_CHECK_CODE(code, lino, _error);
×
552
  }
553

554
  void* pBuff = getFreeBuff(pFileState);
15,491,491✔
555
  if (pBuff) {
15,481,651✔
556
    pPos->pRowBuff = pBuff;
167,663✔
557
    goto _end;
167,663✔
558
  }
559

560
  if (pFileState->curRowCount < pFileState->maxRowCount) {
15,313,988!
561
    pBuff = taosMemoryCalloc(1, pFileState->rowSize);
15,314,021✔
562
    QUERY_CHECK_NULL(pBuff, code, lino, _error, terrno);
15,308,808!
563
    pPos->pRowBuff = pBuff;
15,309,010✔
564
    pFileState->curRowCount++;
15,309,010✔
565
    goto _end;
15,309,010✔
566
  }
567

UNCOV
568
  code = clearRowBuff(pFileState);
×
569
  QUERY_CHECK_CODE(code, lino, _error);
56!
570

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

573
_end:
15,476,729✔
574
  code = tdListAppend(pFileState->usedBuffs, &pPos);
15,476,729✔
575
  QUERY_CHECK_CODE(code, lino, _error);
15,483,911!
576

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

584
  return pPos;
15,483,911✔
585
}
586

587
SRowBuffPos* getNewRowPosForWrite(SStreamFileState* pFileState) {
15,466,780✔
588
  int32_t      code = TSDB_CODE_SUCCESS;
15,466,780✔
589
  int32_t      lino = 0;
15,466,780✔
590
  SRowBuffPos* newPos = getNewRowPos(pFileState);
15,466,780✔
591
  if (!newPos) {
15,475,864!
592
    code = TSDB_CODE_OUT_OF_MEMORY;
×
593
    QUERY_CHECK_CODE(code, lino, _error);
×
594
  }
595
  newPos->beUsed = true;
15,475,864✔
596
  newPos->beFlushed = false;
15,475,864✔
597
  newPos->needFree = false;
15,475,864✔
598
  newPos->beUpdated = true;
15,475,864✔
599
  return newPos;
15,475,864✔
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,
16,897,238✔
609
                             int32_t* pWinCode) {
610
  int32_t code = TSDB_CODE_SUCCESS;
16,897,238✔
611
  int32_t lino = 0;
16,897,238✔
612
  (*pWinCode) = TSDB_CODE_SUCCESS;
16,897,238✔
613
  pFileState->maxTs = TMAX(pFileState->maxTs, pFileState->getTs(pKey));
16,897,238✔
614
  SRowBuffPos** pos = tSimpleHashGet(pFileState->rowStateBuff, pKey, keyLen);
16,895,732✔
615
  if (pos) {
16,926,265✔
616
    if (pVal != NULL) {
1,467,417!
617
      *pVLen = pFileState->rowSize;
1,467,417✔
618
      *pVal = *pos;
1,467,417✔
619
      (*pos)->beUsed = true;
1,467,417✔
620
      (*pos)->beFlushed = false;
1,467,417✔
621
    }
622
    goto _end;
1,467,417✔
623
  }
624
  SRowBuffPos* pNewPos = getNewRowPosForWrite(pFileState);
15,458,848✔
625
  if (!pNewPos || !pNewPos->pRowBuff) {
15,464,561!
626
    code = TSDB_CODE_OUT_OF_MEMORY;
39✔
627
    QUERY_CHECK_CODE(code, lino, _end);
39!
628
  }
629

630
  memcpy(pNewPos->pKey, pKey, keyLen);
15,464,561✔
631
  (*pWinCode) = TSDB_CODE_FAILED;
15,464,561✔
632

633
  TSKEY ts = pFileState->getTs(pKey);
15,464,561✔
634
  if (!isDeteled(pFileState, ts) && isFlushedState(pFileState, ts, 0)) {
15,464,135✔
635
    int32_t len = 0;
119,967✔
636
    void*   p = NULL;
119,967✔
637
    (*pWinCode) = pFileState->stateFileGetFn(pFileState, pKey, &p, &len);
119,967✔
638
    qDebug("===stream===get %" PRId64 " from disc, res %d", ts, (*pWinCode));
119,967✔
639
    if ((*pWinCode) == TSDB_CODE_SUCCESS) {
119,967✔
640
      memcpy(pNewPos->pRowBuff, p, len);
44✔
641
    }
642
    taosMemoryFree(p);
119,967✔
643
  }
644

645
  code = tSimpleHashPut(pFileState->rowStateBuff, pKey, keyLen, &pNewPos, POINTER_BYTES);
15,455,360✔
646
  QUERY_CHECK_CODE(code, lino, _end);
15,451,863!
647

648
  if (pVal) {
15,451,863!
649
    *pVLen = pFileState->rowSize;
15,452,055✔
650
    *pVal = pNewPos;
15,452,055✔
651
  }
652

653
_end:
×
654
  if (code != TSDB_CODE_SUCCESS) {
16,919,280!
655
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
656
  }
657
  return code;
16,914,470✔
658
}
659

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

670
int32_t resetRowBuff(SStreamFileState* pFileState, const void* pKey, int32_t keyLen) {
×
671
  int32_t code_buff = pFileState->stateBuffRemoveFn(pFileState->rowStateBuff, pKey, keyLen);
×
672
  int32_t code_file = pFileState->stateFileRemoveFn(pFileState, pKey);
×
673
  if (pFileState->searchBuff != NULL) {
×
674
    deleteHashSortRowBuff(pFileState, pKey);
×
675
  }
676
  if (code_buff == TSDB_CODE_SUCCESS || code_file == TSDB_CODE_SUCCESS) {
×
677
    return TSDB_CODE_SUCCESS;
×
678
  }
679
  return TSDB_CODE_FAILED;
×
680
}
681

682
static int32_t recoverSessionRowBuff(SStreamFileState* pFileState, SRowBuffPos* pPos) {
279✔
683
  int32_t code = TSDB_CODE_SUCCESS;
279✔
684
  int32_t lino = 0;
279✔
685
  int32_t len = 0;
279✔
686
  void*   pBuff = NULL;
279✔
687
  code = pFileState->stateFileGetFn(pFileState, pPos->pKey, &pBuff, &len);
279✔
688
  QUERY_CHECK_CODE(code, lino, _end);
279!
689
  memcpy(pPos->pRowBuff, pBuff, len);
279✔
690
  taosMemoryFree(pBuff);
279✔
691

692
_end:
279✔
693
  if (code != TSDB_CODE_SUCCESS) {
279!
694
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
695
  }
696
  return code;
279✔
697
}
698

699
static int32_t recoverStateRowBuff(SStreamFileState* pFileState, SRowBuffPos* pPos) {
279✔
700
  int32_t code = TSDB_CODE_SUCCESS;
279✔
701
  int32_t lino = 0;
279✔
702
  pPos->pRowBuff = getFreeBuff(pFileState);
279✔
703
  if (!pPos->pRowBuff) {
279✔
704
    if (pFileState->curRowCount < pFileState->maxRowCount) {
32✔
705
      pPos->pRowBuff = taosMemoryCalloc(1, pFileState->rowSize);
2✔
706
      if (!pPos->pRowBuff) {
2!
707
        code = terrno;
×
708
        QUERY_CHECK_CODE(code, lino, _end);
×
709
      }
710
      pFileState->curRowCount++;
2✔
711
    } else {
712
      code = clearRowBuff(pFileState);
30✔
713
      QUERY_CHECK_CODE(code, lino, _end);
30!
714
      pPos->pRowBuff = getFreeBuff(pFileState);
30✔
715
    }
716
    QUERY_CHECK_CONDITION((pPos->pRowBuff != NULL), code, lino, _end, TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
32!
717
  }
718

719
  code = recoverSessionRowBuff(pFileState, pPos);
279✔
720
  QUERY_CHECK_CODE(code, lino, _end);
279!
721

722
_end:
279✔
723
  if (code != TSDB_CODE_SUCCESS) {
279!
724
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
725
  }
726
  return code;
279✔
727
}
728

729
int32_t getRowBuffByPos(SStreamFileState* pFileState, SRowBuffPos* pPos, void** pVal) {
14,753,472✔
730
  int32_t code = TSDB_CODE_SUCCESS;
14,753,472✔
731
  int32_t lino = 0;
14,753,472✔
732
  if (pPos->pRowBuff) {
14,753,472!
733
    if (pPos->needFree) {
14,755,081!
734
      code = recoverSessionRowBuff(pFileState, pPos);
×
735
      QUERY_CHECK_CODE(code, lino, _end);
×
736
    }
737
    (*pVal) = pPos->pRowBuff;
14,755,081✔
738
    goto _end;
14,755,081✔
739
  }
740

UNCOV
741
  code = recoverStateRowBuff(pFileState, pPos);
×
742
  QUERY_CHECK_CODE(code, lino, _end);
279!
743

744
  (*pVal) = pPos->pRowBuff;
279✔
745
  if (!pPos->needFree) {
279✔
746
    code = tdListPrepend(pFileState->usedBuffs, &pPos);
76✔
747
    QUERY_CHECK_CODE(code, lino, _end);
76!
748
  }
749

750
_end:
279✔
751
  if (code != TSDB_CODE_SUCCESS) {
14,755,360!
752
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
753
  }
754
  return code;
14,754,614✔
755
}
756

757
bool hasRowBuff(SStreamFileState* pFileState, void* pKey, int32_t keyLen) {
25✔
758
  SRowBuffPos** pos = tSimpleHashGet(pFileState->rowStateBuff, pKey, keyLen);
25✔
759
  if (pos) {
25✔
760
    return true;
12✔
761
  }
762
  return false;
13✔
763
}
764

765
SStreamSnapshot* getSnapshot(SStreamFileState* pFileState) {
4,642✔
766
  int64_t mark = (pFileState->deleteMark == INT64_MAX || pFileState->maxTs == INT64_MIN)
1,980✔
767
                     ? INT64_MIN
768
                     : pFileState->maxTs - pFileState->deleteMark;
6,622✔
769
  clearExpiredRowBuff(pFileState, mark, false);
4,642✔
770
  return pFileState->usedBuffs;
4,644✔
771
}
772

773
void flushSnapshot(SStreamFileState* pFileState, SStreamSnapshot* pSnapshot, bool flushState) {
4,730✔
774
  int32_t   code = TSDB_CODE_SUCCESS;
4,730✔
775
  int32_t   lino = 0;
4,730✔
776
  SListIter iter = {0};
4,730✔
777
  tdListInitIter(pSnapshot, &iter, TD_LIST_FORWARD);
4,730✔
778

779
  const int32_t BATCH_LIMIT = 256;
4,730✔
780

781
  int64_t    st = taosGetTimestampMs();
4,730✔
782
  SListNode* pNode = NULL;
4,730✔
783

784
  int idx = streamStateGetCfIdx(pFileState->pFileStore, pFileState->cfName);
4,730✔
785

786
  int32_t len = (pFileState->rowSize + sizeof(uint64_t) + sizeof(int32_t) + 64) * 2;
4,730✔
787
  char*   buf = taosMemoryCalloc(1, len);
4,730✔
788
  if (!buf) {
4,728!
789
    code = terrno;
×
790
    QUERY_CHECK_CODE(code, lino, _end);
×
791
  }
792

793
  void* batch = streamStateCreateBatch();
4,728✔
794
  if (!batch) {
4,729!
795
    code = TSDB_CODE_OUT_OF_MEMORY;
×
796
    QUERY_CHECK_CODE(code, lino, _end);
×
797
  }
798

799
  while ((pNode = tdListNext(&iter)) != NULL && code == TSDB_CODE_SUCCESS) {
2,516,907✔
800
    SRowBuffPos* pPos = *(SRowBuffPos**)pNode->data;
2,512,176✔
801
    if (pPos->beFlushed || !pPos->pRowBuff) {
2,512,176!
802
      continue;
138,944✔
803
    }
804
    pPos->beFlushed = true;
2,373,232✔
805
    pFileState->flushMark = TMAX(pFileState->flushMark, pFileState->getTs(pPos->pKey));
2,373,232✔
806

807
    qDebug("===stream===flushed start:%" PRId64, pFileState->getTs(pPos->pKey));
2,373,232✔
808
    if (streamStateGetBatchSize(batch) >= BATCH_LIMIT) {
2,373,234✔
809
      code = streamStatePutBatch_rocksdb(pFileState->pFileStore, batch);
9,194✔
810
      streamStateClearBatch(batch);
9,194✔
811
      QUERY_CHECK_CODE(code, lino, _end);
9,194!
812
    }
813

814
    void* pSKey = pFileState->stateBuffCreateStateKeyFn(pPos, ((SStreamState*)pFileState->pFileStore)->number);
2,373,233✔
815
    QUERY_CHECK_NULL(pSKey, code, lino, _end, terrno);
2,373,232!
816

817
    code = streamStatePutBatchOptimize(pFileState->pFileStore, idx, batch, pSKey, pPos->pRowBuff, pFileState->rowSize,
2,373,232✔
818
                                       0, buf);
819
    taosMemoryFreeClear(pSKey);
2,373,234!
820
    QUERY_CHECK_CODE(code, lino, _end);
2,373,234!
821
    // todo handle failure
822
    memset(buf, 0, len);
2,373,234✔
823
  }
824
  taosMemoryFreeClear(buf);
4,726!
825

826
  int32_t numOfElems = streamStateGetBatchSize(batch);
4,726✔
827
  if (numOfElems > 0) {
4,730✔
828
    code = streamStatePutBatch_rocksdb(pFileState->pFileStore, batch);
1,201✔
829
    QUERY_CHECK_CODE(code, lino, _end);
1,201!
830
  } else {
831
    goto _end;
3,529✔
832
  }
833

834
  streamStateClearBatch(batch);
1,201✔
835

836
  clearSearchBuff(pFileState);
1,201✔
837

838
  int64_t elapsed = taosGetTimestampMs() - st;
1,201✔
839
  qDebug("%s flush to disk in batch model completed, rows:%d, batch size:%d, elapsed time:%" PRId64 "ms",
1,201✔
840
         pFileState->id, numOfElems, BATCH_LIMIT, elapsed);
841

842
  if (flushState) {
1,201✔
843
    void*   valBuf = NULL;
1,167✔
844
    int32_t len = 0;
1,167✔
845
    code = streamFileStateEncode(&pFileState->flushMark, &valBuf, &len);
1,167✔
846
    QUERY_CHECK_CODE(code, lino, _end);
1,167!
847

848
    qDebug("===stream===flushMark write:%" PRId64, pFileState->flushMark);
1,167✔
849
    code = streamStatePutBatch(pFileState->pFileStore, "default", batch, STREAM_STATE_INFO_NAME, valBuf, len, 0);
1,167✔
850
    taosMemoryFree(valBuf);
1,167✔
851
    QUERY_CHECK_CODE(code, lino, _end);
1,167!
852

853
    code = streamStatePutBatch_rocksdb(pFileState->pFileStore, batch);
1,167✔
854
    QUERY_CHECK_CODE(code, lino, _end);
1,167!
855
  }
856

857
_end:
34✔
858
  if (code != TSDB_CODE_SUCCESS) {
4,730!
859
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
860
  }
861
  taosMemoryFree(buf);
4,730✔
862
  streamStateDestroyBatch(batch);
4,728✔
863
}
4,729✔
864

865
int32_t forceRemoveCheckpoint(SStreamFileState* pFileState, int64_t checkpointId) {
×
866
  char keyBuf[128] = {0};
×
867
  TAOS_UNUSED(tsnprintf(keyBuf, sizeof(keyBuf), "%s:%" PRId64 "", TASK_KEY, checkpointId));
×
868
  return streamDefaultDel_rocksdb(pFileState->pFileStore, keyBuf);
×
869
}
870

871
int32_t getSnapshotIdList(SStreamFileState* pFileState, SArray* list) {
×
872
  return streamDefaultIterGet_rocksdb(pFileState->pFileStore, TASK_KEY, NULL, list);
×
873
}
874

875
int32_t deleteExpiredCheckPoint(SStreamFileState* pFileState, TSKEY mark) {
×
876
  int32_t code = TSDB_CODE_SUCCESS;
×
877
  int64_t maxCheckPointId = 0;
×
878
  {
879
    char    buf[128] = {0};
×
880
    void*   val = NULL;
×
881
    int32_t len = 0;
×
882
    memcpy(buf, TASK_KEY, strlen(TASK_KEY));
×
883
    code = streamDefaultGet_rocksdb(pFileState->pFileStore, buf, &val, &len);
×
884
    if (code != 0 || len == 0 || val == NULL) {
×
885
      return TSDB_CODE_FAILED;
×
886
    }
887
    memcpy(buf, val, len);
×
888
    buf[len] = 0;
×
889
    maxCheckPointId = taosStr2Int64((char*)buf, NULL, 10);
×
890
    taosMemoryFree(val);
×
891
  }
892
  for (int64_t i = maxCheckPointId; i > 0; i--) {
×
893
    char    buf[128] = {0};
×
894
    void*   val = 0;
×
895
    int32_t len = 0;
×
896
    TAOS_UNUSED(tsnprintf(buf, sizeof(buf), "%s:%" PRId64 "", TASK_KEY, i));
×
897
    code = streamDefaultGet_rocksdb(pFileState->pFileStore, buf, &val, &len);
×
898
    if (code != 0) {
×
899
      return TSDB_CODE_FAILED;
×
900
    }
901
    memcpy(buf, val, len);
×
902
    buf[len] = 0;
×
903
    taosMemoryFree(val);
×
904

905
    TSKEY ts;
906
    ts = taosStr2Int64((char*)buf, NULL, 10);
×
907
    if (ts < mark) {
×
908
      // statekey winkey.ts < mark
909
      int32_t tmpRes = forceRemoveCheckpoint(pFileState, i);
×
910
      qTrace("%s at line %d res:%d", __func__, __LINE__, tmpRes);
×
911
      break;
×
912
    }
913
  }
914
  return code;
×
915
}
916

917
int32_t recoverSesssion(SStreamFileState* pFileState, int64_t ckId) {
1,695✔
918
  int32_t code = TSDB_CODE_SUCCESS;
1,695✔
919
  int32_t lino = 0;
1,695✔
920
  int32_t winRes = TSDB_CODE_SUCCESS;
1,695✔
921
  if (pFileState->maxTs != INT64_MIN) {
1,695!
922
    int64_t mark = (INT64_MIN + pFileState->deleteMark >= pFileState->maxTs)
×
923
                       ? INT64_MIN
924
                       : pFileState->maxTs - pFileState->deleteMark;
×
925
    int32_t tmpRes = deleteExpiredCheckPoint(pFileState, mark);
×
926
    qTrace("%s at line %d res:%d", __func__, __LINE__, tmpRes);
×
927
  }
928

929
  SStreamStateCur* pCur = streamStateSessionSeekToLast_rocksdb(pFileState->pFileStore, INT64_MAX);
1,695✔
930
  int32_t          recoverNum = TMIN(MIN_NUM_OF_RECOVER_ROW_BUFF, pFileState->maxRowCount);
1,695✔
931
  while (winRes == TSDB_CODE_SUCCESS) {
1,695!
932
    if (pFileState->curRowCount >= recoverNum) {
1,695!
933
      break;
1,695✔
934
    }
935

936
    void*       pVal = NULL;
1,695✔
937
    int32_t     vlen = 0;
1,695✔
938
    SSessionKey key = {0};
1,695✔
939
    winRes = streamStateSessionGetKVByCur_rocksdb(getStateFileStore(pFileState), pCur, &key, &pVal, &vlen);
1,695✔
940
    if (winRes != TSDB_CODE_SUCCESS) {
1,695!
941
      break;
1,695✔
942
    }
943

944
    if (vlen != pFileState->rowSize) {
×
945
      code = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
946
      QUERY_CHECK_CODE(code, lino, _end);
×
947
    }
948

949
    SRowBuffPos* pPos = createSessionWinBuff(pFileState, &key, pVal, &vlen);
×
950
    winRes = putSessionWinResultBuff(pFileState, pPos);
×
951
    if (winRes != TSDB_CODE_SUCCESS) {
×
952
      break;
×
953
    }
954

955
    winRes = streamStateSessionCurPrev_rocksdb(pCur);
×
956
  }
957

958
_end:
×
959
  if (code != TSDB_CODE_SUCCESS) {
1,695!
960
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
961
  }
962
  streamStateFreeCur(pCur);
1,695✔
963
  return code;
1,695✔
964
}
965

966
int32_t recoverSnapshot(SStreamFileState* pFileState, int64_t ckId) {
4,104✔
967
  int32_t code = TSDB_CODE_SUCCESS;
4,104✔
968
  int32_t lino = 0;
4,104✔
969
  int32_t winCode = TSDB_CODE_SUCCESS;
4,104✔
970
  if (pFileState->maxTs != INT64_MIN) {
4,104!
971
    int64_t mark = (INT64_MIN + pFileState->deleteMark >= pFileState->maxTs)
×
972
                       ? INT64_MIN
973
                       : pFileState->maxTs - pFileState->deleteMark;
×
974
    int32_t tmpRes = deleteExpiredCheckPoint(pFileState, mark);
×
975
    qTrace("%s at line %d res:%d", __func__, __LINE__, tmpRes);
×
976
  }
977

978
  SStreamStateCur* pCur = streamStateSeekToLast_rocksdb(pFileState->pFileStore);
4,104✔
979
  int32_t          recoverNum = TMIN(MIN_NUM_OF_RECOVER_ROW_BUFF, pFileState->maxRowCount);
4,105✔
980
  while (winCode == TSDB_CODE_SUCCESS) {
4,105!
981
    if (pFileState->curRowCount >= recoverNum) {
4,105!
982
      break;
4,105✔
983
    }
984

985
    void*        pVal = NULL;
4,105✔
986
    int32_t      vlen = 0;
4,105✔
987
    SRowBuffPos* pNewPos = getNewRowPosForWrite(pFileState);
4,105✔
988
    if (!pNewPos || !pNewPos->pRowBuff) {
4,105!
989
      code = TSDB_CODE_OUT_OF_MEMORY;
×
990
      QUERY_CHECK_CODE(code, lino, _end);
×
991
    }
992

993
    winCode = streamStateGetKVByCur_rocksdb(getStateFileStore(pFileState), pCur, pNewPos->pKey, (const void**)&pVal, &vlen);
4,105✔
994
    if (winCode != TSDB_CODE_SUCCESS || pFileState->getTs(pNewPos->pKey) < pFileState->flushMark) {
4,105!
995
      destroyRowBuffPos(pNewPos);
4,105✔
996
      SListNode* pNode = tdListPopTail(pFileState->usedBuffs);
4,105✔
997
      taosMemoryFreeClear(pNode);
4,105!
998
      taosMemoryFreeClear(pVal);
4,105!
999
      break;
4,105✔
1000
    }
1001
    if (vlen != pFileState->rowSize) {
×
1002
      qError("row size mismatch, expect:%d, actual:%d", pFileState->rowSize, vlen);
×
1003
      code = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
1004
      taosMemoryFreeClear(pVal);
×
1005
      QUERY_CHECK_CODE(code, lino, _end);
×
1006
    }
1007
    memcpy(pNewPos->pRowBuff, pVal, vlen);
×
1008
    taosMemoryFreeClear(pVal);
×
1009
    pNewPos->beFlushed = true;
×
1010
    code = tSimpleHashPut(pFileState->rowStateBuff, pNewPos->pKey, pFileState->keyLen, &pNewPos, POINTER_BYTES);
×
1011
    if (code != TSDB_CODE_SUCCESS) {
×
1012
      destroyRowBuffPos(pNewPos);
×
1013
      break;
×
1014
    }
1015
    streamStateCurPrev_rocksdb(pCur);
×
1016
  }
1017

1018
_end:
×
1019
  if (code != TSDB_CODE_SUCCESS) {
4,105!
1020
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1021
  }
1022
  streamStateFreeCur(pCur);
4,105✔
1023
  return code;
4,105✔
1024
}
1025

1026
int32_t streamFileStateGetSelectRowSize(SStreamFileState* pFileState) { return pFileState->selectivityRowSize; }
751,274✔
1027

1028
void streamFileStateReloadInfo(SStreamFileState* pFileState, TSKEY ts) {
1,500✔
1029
  pFileState->flushMark = TMAX(pFileState->flushMark, ts);
1,500✔
1030
  pFileState->maxTs = TMAX(pFileState->maxTs, ts);
1,500✔
1031
}
1,500✔
1032

1033
void* getRowStateBuff(SStreamFileState* pFileState) { return pFileState->rowStateBuff; }
40,299✔
1034
void* getSearchBuff(SStreamFileState* pFileState) { return pFileState->searchBuff; }
16,164,113✔
1035

1036
void* getStateFileStore(SStreamFileState* pFileState) { return pFileState->pFileStore; }
17,309✔
1037

1038
bool isDeteled(SStreamFileState* pFileState, TSKEY ts) {
15,460,616✔
1039
  return pFileState->deleteMark != INT64_MAX && pFileState->maxTs > 0 &&
21,237,716✔
1040
         ts < (pFileState->maxTs - pFileState->deleteMark);
5,777,100✔
1041
}
1042

1043
bool isFlushedState(SStreamFileState* pFileState, TSKEY ts, TSKEY gap) { return ts <= (pFileState->flushMark + gap); }
12,002,847✔
1044

1045
TSKEY getFlushMark(SStreamFileState* pFileState) { return pFileState->flushMark; };
8✔
1046

1047
int32_t getRowStateRowSize(SStreamFileState* pFileState) { return pFileState->rowSize; }
752,179✔
1048

1049
int32_t getFunctionRowBuff(SStreamFileState* pFileState, void* pKey, int32_t keyLen, void** pVal, int32_t* pVLen) {
751,279✔
1050
  int32_t winCode = TSDB_CODE_SUCCESS;
751,279✔
1051
  return pFileState->stateFunctionGetFn(pFileState, pKey, keyLen, pVal, pVLen, &winCode);
751,279✔
1052
}
1053

1054
int32_t recoverFillSnapshot(SStreamFileState* pFileState, int64_t ckId) {
11✔
1055
  int32_t code = TSDB_CODE_SUCCESS;
11✔
1056
  int32_t lino = 0;
11✔
1057
  if (pFileState->maxTs != INT64_MIN) {
11!
1058
    int64_t mark = (INT64_MIN + pFileState->deleteMark >= pFileState->maxTs)
×
1059
                       ? INT64_MIN
1060
                       : pFileState->maxTs - pFileState->deleteMark;
×
1061
    code = deleteExpiredCheckPoint(pFileState, mark);
×
1062
    QUERY_CHECK_CODE(code, lino, _end);
×
1063
  }
1064

1065
  SStreamStateCur* pCur = streamStateFillSeekToLast_rocksdb(pFileState->pFileStore);
11✔
1066
  if (pCur == NULL) {
11!
1067
    return code;
11✔
1068
  }
1069
  int32_t recoverNum = TMIN(MIN_NUM_OF_RECOVER_ROW_BUFF, pFileState->maxRowCount);
×
1070
  int32_t winRes = TSDB_CODE_SUCCESS;
×
1071
  while (winRes == TSDB_CODE_SUCCESS) {
×
1072
    if (pFileState->curRowCount >= recoverNum) {
×
1073
      break;
×
1074
    }
1075

1076
    void*        pVal = NULL;
×
1077
    int32_t      vlen = 0;
×
1078
    SRowBuffPos* pNewPos = getNewRowPosForWrite(pFileState);
×
1079
    winRes = streamStateFillGetKVByCur_rocksdb(pCur, pNewPos->pKey, (const void**)&pVal, &vlen);
×
1080
    if (winRes != TSDB_CODE_SUCCESS || isFlushedState(pFileState, pFileState->getTs(pNewPos->pKey), 0)) {
×
1081
      destroyRowBuffPos(pNewPos);
×
1082
      SListNode* pNode = tdListPopTail(pFileState->usedBuffs);
×
1083
      taosMemoryFreeClear(pNode);
×
1084
      taosMemoryFreeClear(pVal);
×
1085
      break;
×
1086
    }
1087

1088
    memcpy(pNewPos->pRowBuff, pVal, vlen);
×
1089
    taosMemoryFreeClear(pVal);
×
1090
    pNewPos->beFlushed = true;
×
1091
    winRes = tSimpleHashPut(pFileState->rowStateBuff, pNewPos->pKey, pFileState->keyLen, &pNewPos, POINTER_BYTES);
×
1092
    if (winRes != TSDB_CODE_SUCCESS) {
×
1093
      destroyRowBuffPos(pNewPos);
×
1094
      break;
×
1095
    }
1096
    streamStateCurPrev_rocksdb(pCur);
×
1097
  }
1098
  streamStateFreeCur(pCur);
×
1099

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

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

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

1144
      if (pVal) {
×
1145
        *pVLen = pFileState->rowSize;
×
1146
        *pVal = pNewPos;
×
1147
      }
1148
    }
1149
    taosMemoryFree(p);
×
1150
  }
1151

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

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

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

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

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

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

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

1202
int32_t streamFileStateGroupGetKVByCur(SStreamStateCur* pCur, int64_t* pKey, void** pVal, int32_t* pVLen) {
937✔
1203
  int32_t code = TSDB_CODE_SUCCESS;
937✔
1204
  if (pCur->pHashData) {
937✔
1205
    *pKey = *(int64_t*)tSimpleHashGetKey(pCur->pHashData, NULL);
373!
1206
    return code;
373✔
1207
  }
1208
  return streamStateParTagGetKVByCur_rocksdb(pCur, pKey, NULL, NULL);
564✔
1209
}
1210

1211
SSHashObj* getGroupIdCache(SStreamFileState* pFileState) {
564✔
1212
  return pFileState->pGroupIdMap;
564✔
1213
}
1214

1215
void setFillInfo(SStreamFileState* pFileState) {
18✔
1216
  pFileState->hasFillCatch = false;
18✔
1217
}
18✔
1218

1219
void clearExpiredState(SStreamFileState* pFileState) {
692✔
1220
  SSHashObj* pSearchBuff = pFileState->searchBuff;
692✔
1221
  void*      pIte = NULL;
692✔
1222
  int32_t    iter = 0;
692✔
1223
  while ((pIte = tSimpleHashIterate(pSearchBuff, pIte, &iter)) != NULL) {
1,113✔
1224
    SArray* pWinStates = *((void**)pIte);
421✔
1225
    int32_t size = taosArrayGetSize(pWinStates);
421✔
1226
    for (int32_t i = 0; i < size - 1; i++) {
489✔
1227
      SWinKey* pKey = taosArrayGet(pWinStates, i);
68✔
1228
      int32_t  code_buff = pFileState->stateBuffRemoveFn(pFileState->rowStateBuff, pKey, sizeof(SWinKey));
68✔
1229
      qTrace("clear expired buff, ts:%" PRId64 ". %s at line %d res:%d", pKey->ts, __func__, __LINE__, code_buff);
68!
1230

1231
      if (isFlushedState(pFileState, pKey->ts, 0)) {
68✔
1232
        int32_t code_file = pFileState->stateFileRemoveFn(pFileState, pKey);
8✔
1233
        qTrace("clear expired file, ts:%" PRId64 ". %s at line %d res:%d", pKey->ts, __func__, __LINE__, code_file);
8!
1234
      }
1235

1236
      if (pFileState->hasFillCatch == false) {
68✔
1237
        int32_t code_file = streamStateFillDel_rocksdb(pFileState->pFileStore, pKey);
42✔
1238
        qTrace("force clear expired file, ts:%" PRId64 ". %s at line %d res %d", pKey->ts, __func__, __LINE__, code_file);
42!
1239
      }
1240
    }
1241
    taosArrayRemoveBatch(pWinStates, 0, size - 1, NULL);
421✔
1242
  }
1243
}
692✔
1244

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

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

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

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

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

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

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

1428
  (*ppResStates) = pWinStates;
238✔
1429

1430
_end:
238✔
1431
  if (code != TSDB_CODE_SUCCESS) {
238!
1432
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1433
  }
1434
  return code;
238✔
1435
}
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