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

taosdata / TDengine / #3544

30 Nov 2024 03:06AM UTC coverage: 60.88% (+0.04%) from 60.842%
#3544

push

travis-ci

web-flow
Merge pull request #28988 from taosdata/main

merge: from main to 3.0 branch

120724 of 253479 branches covered (47.63%)

Branch coverage included in aggregate %.

407 of 489 new or added lines in 21 files covered. (83.23%)

1148 existing lines in 113 files now uncovered.

201919 of 276488 relevant lines covered (73.03%)

18898587.44 hits per line

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

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

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

87
void stateHashBuffRemoveByPosFn(SStreamFileState* pFileState, SRowBuffPos* pPos) {
6,259,567✔
88
  size_t        keyLen = pFileState->keyLen;
6,259,567✔
89
  SRowBuffPos** ppPos = tSimpleHashGet(pFileState->rowStateBuff, pPos->pKey, keyLen);
6,259,567✔
90
  if (ppPos) {
6,255,078!
91
    if ((*ppPos) == pPos) {
6,255,099!
92
      int32_t tmpRes = tSimpleHashRemove(pFileState->rowStateBuff, pPos->pKey, keyLen);
6,255,325✔
93
      qTrace("%s at line %d res:%d", __func__, __LINE__, tmpRes);
6,264,373✔
94
    }
95
  }
96
}
6,264,218✔
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) {
2,998✔
103
  return streamStateDel_rocksdb(pFileState->pFileStore, pKey);
2,998✔
104
}
105

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

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

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

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

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

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

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

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

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

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

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

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

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

200
    pFileState->stateFileRemoveFn = intervalFileRemoveFn;
4,066✔
201
    pFileState->stateFileGetFn = intervalFileGetFn;
4,066✔
202
    pFileState->cfName = taosStrdup("state");
4,066✔
203
    pFileState->stateFunctionGetFn = addRowBuffIfNotExist;
4,066✔
204
  } else if (type == STREAM_STATE_BUFF_SORT) {
1,756✔
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) {
61!
216
    pFileState->rowStateBuff = tSimpleHashInit(cap, hashFn);
61✔
217
    pFileState->searchBuff = tSimpleHashInit(MIN_NUM_SEARCH_BUCKET, hashFn);
61✔
218
    QUERY_CHECK_NULL(pFileState->searchBuff, code, lino, _end, terrno);
61!
219
    pFileState->stateBuffCleanupFn = stateHashBuffCleanupFn;
61✔
220
    pFileState->stateBuffRemoveFn = stateHashBuffRemoveFn;
61✔
221
    pFileState->stateBuffRemoveByPosFn = stateHashBuffRemoveByPosFn;
61✔
222
    pFileState->stateBuffCreateStateKeyFn = defaultCreateStateKey;
61✔
223

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

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

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

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

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

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

259
  if (type == STREAM_STATE_BUFF_HASH || type == STREAM_STATE_BUFF_HASH_SEARCH) {
5,823✔
260
    code = recoverSnapshot(pFileState, checkpointId);
4,067✔
261
  } else if (type == STREAM_STATE_BUFF_SORT) {
1,756✔
262
    code = recoverSession(pFileState, checkpointId);
1,695✔
263
  } else if (type == STREAM_STATE_BUFF_HASH_SORT) {
61!
264
    code = recoverFillSnapshot(pFileState, checkpointId);
61✔
265
  }
266
  QUERY_CHECK_CODE(code, lino, _end);
5,823!
267

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

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

287
void destroyRowBuffPos(SRowBuffPos* pPos) {
13,309,120✔
288
  taosMemoryFreeClear(pPos->pKey);
13,309,120!
289
  taosMemoryFreeClear(pPos->pRowBuff);
13,314,249✔
290
  taosMemoryFree(pPos);
13,315,003✔
291
}
13,317,291✔
292

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

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

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

318
void streamFileStateDestroy(SStreamFileState* pFileState) {
12,754✔
319
  if (!pFileState) {
12,754✔
320
    return;
7,061✔
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,583,866✔
334
  int32_t code = TSDB_CODE_SUCCESS;
6,583,866✔
335
  int32_t lino = 0;
6,583,866✔
336
  if (pPos->pRowBuff) {
6,583,866✔
337
    code = tdListAppend(pFileState->freeBuffs, &(pPos->pRowBuff));
6,578,765✔
338
    QUERY_CHECK_CODE(code, lino, _end);
6,595,595!
339
    pPos->pRowBuff = NULL;
6,595,595✔
340
  }
341

342
_end:
5,101✔
343
  if (code != TSDB_CODE_SUCCESS) {
6,600,696!
344
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
345
  }
346
  return code;
6,594,026✔
347
}
348

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

355
  SListNode* pNode = NULL;
7,375✔
356
  while ((pNode = tdListNext(&iter)) != NULL) {
7,459,590✔
357
    SRowBuffPos* pPos = *(SRowBuffPos**)(pNode->data);
7,452,134✔
358
    if (all || (pFileState->getTs(pPos->pKey) < ts && !pPos->beUsed)) {
7,452,134!
359
      code = putFreeBuff(pFileState, pPos);
6,129,389✔
360
      QUERY_CHECK_CODE(code, lino, _end);
6,129,370!
361

362
      if (!all) {
6,129,370✔
363
        pFileState->stateBuffRemoveByPosFn(pFileState, pPos);
5,786,539✔
364
      }
365
      destroyRowBuffPos(pPos);
6,129,478✔
366
      SListNode* tmp = tdListPopNode(pFileState->usedBuffs, pNode);
6,129,483✔
367
      taosMemoryFreeClear(tmp);
6,129,367✔
368
    }
369
  }
370

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

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

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

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

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

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

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

422
void streamFileStateReleaseBuff(SStreamFileState* pFileState, SRowBuffPos* pPos, bool used) { pPos->beUsed = used; }
12,314,508✔
423

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

431
  SListNode* pNode = NULL;
58✔
432
  while ((pNode = tdListNext(&iter)) != NULL && i < max) {
1,417,427!
433
    SRowBuffPos* pPos = *(SRowBuffPos**)pNode->data;
1,398,920✔
434
    if (pPos->beUsed == used) {
1,398,920✔
435
      if (used && !pPos->pRowBuff) {
473,134!
436
        QUERY_CHECK_CONDITION((pPos->needFree == true), code, lino, _end, TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
×
437
        continue;
×
438
      }
439
      code = tdListAppend(pFlushList, &pPos);
473,134✔
440
      QUERY_CHECK_CODE(code, lino, _end);
474,561!
441

442
      pFileState->flushMark = TMAX(pFileState->flushMark, pFileState->getTs(pPos->pKey));
474,561✔
443
      pFileState->stateBuffRemoveByPosFn(pFileState, pPos);
473,599✔
444
      SListNode* tmp = tdListPopNode(pFileState->usedBuffs, pNode);
476,990✔
445
      taosMemoryFreeClear(tmp);
472,497✔
446
      if (pPos->pRowBuff) {
491,583✔
447
        i++;
479,801✔
448
      }
449
    }
450
  }
451

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

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

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

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

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

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

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

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

501
  tdListFreeP(pFlushList, destroyRowBuffPosPtr);
449✔
502

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

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

520
void* getFreeBuff(SStreamFileState* pFileState) {
13,779,917✔
521
  SList*     lists = pFileState->freeBuffs;
13,779,917✔
522
  int32_t    buffSize = pFileState->rowSize;
13,779,917✔
523
  SListNode* pNode = tdListPopHead(lists);
13,779,917✔
524
  if (!pNode) {
13,766,282✔
525
    return NULL;
13,390,622✔
526
  }
527
  void* ptr = *(void**)pNode->data;
375,660✔
528
  memset(ptr, 0, buffSize);
375,660✔
529
  taosMemoryFree(pNode);
375,660✔
530
  return ptr;
375,208✔
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) {
13,747,964✔
540
  int32_t      code = TSDB_CODE_SUCCESS;
13,747,964✔
541
  int32_t      lino = 0;
13,747,964✔
542
  SRowBuffPos* pPos = taosMemoryCalloc(1, sizeof(SRowBuffPos));
13,747,964✔
543
  if (!pPos) {
13,794,107!
544
    code = terrno;
×
545
    QUERY_CHECK_CODE(code, lino, _error);
×
546
  }
547

548
  pPos->pKey = taosMemoryCalloc(1, pFileState->keyLen);
13,794,107✔
549
  if (!pPos->pKey) {
13,784,634!
550
    code = terrno;
×
551
    QUERY_CHECK_CODE(code, lino, _error);
×
552
  }
553

554
  void* pBuff = getFreeBuff(pFileState);
13,784,634✔
555
  if (pBuff) {
13,760,693✔
556
    pPos->pRowBuff = pBuff;
374,859✔
557
    goto _end;
374,859✔
558
  }
559

560
  if (pFileState->curRowCount < pFileState->maxRowCount) {
13,385,834!
561
    pBuff = taosMemoryCalloc(1, pFileState->rowSize);
13,385,847✔
562
    QUERY_CHECK_NULL(pBuff, code, lino, _error, terrno);
13,373,084!
563
    pPos->pRowBuff = pBuff;
13,374,434✔
564
    pFileState->curRowCount++;
13,374,434✔
565
    goto _end;
13,374,434✔
566
  }
567

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

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

573
_end:
13,749,355✔
574
  code = tdListAppend(pFileState->usedBuffs, &pPos);
13,749,355✔
575
  QUERY_CHECK_CODE(code, lino, _error);
13,792,887!
576

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

584
  return pPos;
13,792,887✔
585
}
586

587
SRowBuffPos* getNewRowPosForWrite(SStreamFileState* pFileState) {
13,751,131✔
588
  int32_t      code = TSDB_CODE_SUCCESS;
13,751,131✔
589
  int32_t      lino = 0;
13,751,131✔
590
  SRowBuffPos* newPos = getNewRowPos(pFileState);
13,751,131✔
591
  if (!newPos) {
13,791,655!
592
    code = TSDB_CODE_OUT_OF_MEMORY;
×
593
    QUERY_CHECK_CODE(code, lino, _error);
×
594
  }
595
  newPos->beUsed = true;
13,791,655✔
596
  newPos->beFlushed = false;
13,791,655✔
597
  newPos->needFree = false;
13,791,655✔
598
  newPos->beUpdated = true;
13,791,655✔
599
  return newPos;
13,791,655✔
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,
14,977,039✔
609
                             int32_t* pWinCode) {
610
  int32_t code = TSDB_CODE_SUCCESS;
14,977,039✔
611
  int32_t lino = 0;
14,977,039✔
612
  (*pWinCode) = TSDB_CODE_SUCCESS;
14,977,039✔
613
  pFileState->maxTs = TMAX(pFileState->maxTs, pFileState->getTs(pKey));
14,977,039✔
614
  SRowBuffPos** pos = tSimpleHashGet(pFileState->rowStateBuff, pKey, keyLen);
14,974,921✔
615
  if (pos) {
15,016,670✔
616
    if (pVal != NULL) {
1,273,698!
617
      *pVLen = pFileState->rowSize;
1,273,771✔
618
      *pVal = *pos;
1,273,771✔
619
      (*pos)->beUsed = true;
1,273,771✔
620
      (*pos)->beFlushed = false;
1,273,771✔
621
    }
622
    goto _end;
1,273,698✔
623
  }
624
  SRowBuffPos* pNewPos = getNewRowPosForWrite(pFileState);
13,742,972✔
625
  if (!pNewPos || !pNewPos->pRowBuff) {
13,779,144!
626
    code = TSDB_CODE_OUT_OF_MEMORY;
×
627
    QUERY_CHECK_CODE(code, lino, _end);
×
628
  }
629

630
  memcpy(pNewPos->pKey, pKey, keyLen);
13,779,144✔
631
  (*pWinCode) = TSDB_CODE_FAILED;
13,779,144✔
632

633
  TSKEY ts = pFileState->getTs(pKey);
13,779,144✔
634
  if (!isDeteled(pFileState, ts) && isFlushedState(pFileState, ts, 0)) {
13,736,583✔
635
    int32_t len = 0;
69,607✔
636
    void*   p = NULL;
69,607✔
637
    (*pWinCode) = pFileState->stateFileGetFn(pFileState, pKey, &p, &len);
69,607✔
638
    qDebug("===stream===get %" PRId64 " from disc, res %d", ts, (*pWinCode));
69,608✔
639
    if ((*pWinCode) == TSDB_CODE_SUCCESS) {
69,608✔
640
      memcpy(pNewPos->pRowBuff, p, len);
33✔
641
    }
642
    taosMemoryFree(p);
69,608✔
643
  }
644

645
  code = tSimpleHashPut(pFileState->rowStateBuff, pKey, keyLen, &pNewPos, POINTER_BYTES);
13,723,344✔
646
  QUERY_CHECK_CODE(code, lino, _end);
13,754,615!
647

648
  if (pVal) {
13,754,615✔
649
    *pVLen = pFileState->rowSize;
13,753,133✔
650
    *pVal = pNewPos;
13,753,133✔
651
  }
652

653
_end:
1,482✔
654
  if (code != TSDB_CODE_SUCCESS) {
15,028,313!
655
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
656
  }
657
  return code;
15,026,973✔
658
}
659

660
void deleteRowBuff(SStreamFileState* pFileState, const void* pKey, int32_t keyLen) {
4,955✔
661
  int32_t code_buff = pFileState->stateBuffRemoveFn(pFileState->rowStateBuff, pKey, keyLen);
4,955✔
662
  qTrace("%s at line %d res:%d", __func__, __LINE__, code_buff);
4,954✔
663
  int32_t code_file = pFileState->stateFileRemoveFn(pFileState, pKey);
4,954✔
664
  qTrace("%s at line %d res:%d", __func__, __LINE__, code_file);
4,956✔
665
  if (pFileState->searchBuff != NULL) {
4,956!
666
    deleteHashSortRowBuff(pFileState, pKey);
×
667
  }
668
}
4,956✔
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) {
284✔
683
  int32_t code = TSDB_CODE_SUCCESS;
284✔
684
  int32_t lino = 0;
284✔
685
  int32_t len = 0;
284✔
686
  void*   pBuff = NULL;
284✔
687
  code = pFileState->stateFileGetFn(pFileState, pPos->pKey, &pBuff, &len);
284✔
688
  QUERY_CHECK_CODE(code, lino, _end);
284!
689
  memcpy(pPos->pRowBuff, pBuff, len);
284✔
690
  taosMemoryFree(pBuff);
284✔
691

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

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

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

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

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

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

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

750
_end:
284✔
751
  if (code != TSDB_CODE_SUCCESS) {
12,154,279!
752
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
753
  }
754
  return code;
12,153,896✔
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;
11✔
761
  }
762
  return false;
14✔
763
}
764

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

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

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

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

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

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

793
  void* batch = streamStateCreateBatch();
4,736✔
794
  if (!batch) {
4,737!
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) {
1,810,495✔
800
    SRowBuffPos* pPos = *(SRowBuffPos**)pNode->data;
1,805,600✔
801
    if (pPos->beFlushed || !pPos->pRowBuff) {
1,805,600✔
802
      continue;
2,620✔
803
    }
804
    pPos->beFlushed = true;
1,802,980✔
805
    pFileState->flushMark = TMAX(pFileState->flushMark, pFileState->getTs(pPos->pKey));
1,802,980✔
806

807
    qDebug("===stream===flushed start:%" PRId64, pFileState->getTs(pPos->pKey));
1,802,849✔
808
    if (streamStateGetBatchSize(batch) >= BATCH_LIMIT) {
1,802,851✔
809
      code = streamStatePutBatch_rocksdb(pFileState->pFileStore, batch);
6,967✔
810
      streamStateClearBatch(batch);
6,967✔
811
      QUERY_CHECK_CODE(code, lino, _end);
6,967!
812
    }
813

814
    void* pSKey = pFileState->stateBuffCreateStateKeyFn(pPos, ((SStreamState*)pFileState->pFileStore)->number);
1,803,000✔
815
    QUERY_CHECK_NULL(pSKey, code, lino, _end, terrno);
1,803,049!
816

817
    code = streamStatePutBatchOptimize(pFileState->pFileStore, idx, batch, pSKey, pPos->pRowBuff, pFileState->rowSize,
1,803,049✔
818
                                       0, buf);
819
    taosMemoryFreeClear(pSKey);
1,803,123!
820
    QUERY_CHECK_CODE(code, lino, _end);
1,803,138!
821
    // todo handle failure
822
    memset(buf, 0, len);
1,803,138✔
823
  }
824
  taosMemoryFreeClear(buf);
4,842✔
825

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

834
  streamStateClearBatch(batch);
1,207✔
835

836
  clearSearchBuff(pFileState);
1,207✔
837

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

842
  if (flushState) {
1,206✔
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,168!
847

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

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

857
_end:
39✔
858
  if (code != TSDB_CODE_SUCCESS) {
4,736!
859
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
860
  }
861
  taosMemoryFree(buf);
4,736✔
862
  streamStateDestroyBatch(batch);
4,736✔
863
}
4,737✔
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 recoverSession(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,699!
932
    if (pFileState->curRowCount >= recoverNum) {
1,699!
933
      break;
1,695✔
934
    }
935

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

944
    if (vlen != pFileState->rowSize) {
4!
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);
4✔
950
    winRes = putSessionWinResultBuff(pFileState, pPos);
4✔
951
    if (winRes != TSDB_CODE_SUCCESS) {
4!
952
      break;
×
953
    }
954

955
    winRes = streamStateSessionCurPrev_rocksdb(pCur);
4✔
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,067✔
967
  int32_t code = TSDB_CODE_SUCCESS;
4,067✔
968
  int32_t lino = 0;
4,067✔
969
  int32_t winCode = TSDB_CODE_SUCCESS;
4,067✔
970
  if (pFileState->maxTs != INT64_MIN) {
4,067!
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,067✔
979
  int32_t          recoverNum = TMIN(MIN_NUM_OF_RECOVER_ROW_BUFF, pFileState->maxRowCount);
4,067✔
980
  while (winCode == TSDB_CODE_SUCCESS) {
4,067!
981
    if (pFileState->curRowCount >= recoverNum) {
4,067!
982
      break;
4,067✔
983
    }
984

985
    void*        pVal = NULL;
4,067✔
986
    int32_t      vlen = 0;
4,067✔
987
    SRowBuffPos* pNewPos = getNewRowPosForWrite(pFileState);
4,067✔
988
    if (!pNewPos || !pNewPos->pRowBuff) {
4,067!
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,067✔
994
    qDebug("===stream=== get state by cur winres:%d. %s", winCode, __func__);
4,067✔
995
    if (winCode != TSDB_CODE_SUCCESS || pFileState->getTs(pNewPos->pKey) < pFileState->flushMark) {
4,067!
996
      destroyRowBuffPos(pNewPos);
4,067✔
997
      SListNode* pNode = tdListPopTail(pFileState->usedBuffs);
4,067✔
998
      taosMemoryFreeClear(pNode);
4,067!
999
      taosMemoryFreeClear(pVal);
4,067!
1000
      break;
4,067✔
1001
    }
1002
    if (vlen != pFileState->rowSize) {
×
1003
      qError("row size mismatch, expect:%d, actual:%d", pFileState->rowSize, vlen);
×
1004
      code = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
1005
      taosMemoryFreeClear(pVal);
×
1006
      QUERY_CHECK_CODE(code, lino, _end);
×
1007
    }
1008
    memcpy(pNewPos->pRowBuff, pVal, vlen);
×
1009
    taosMemoryFreeClear(pVal);
×
1010
    pNewPos->beFlushed = true;
×
NEW
1011
    qDebug("===stream=== read checkpoint state from disc. %s", __func__);
×
1012
    code = tSimpleHashPut(pFileState->rowStateBuff, pNewPos->pKey, pFileState->keyLen, &pNewPos, POINTER_BYTES);
×
1013
    if (code != TSDB_CODE_SUCCESS) {
×
1014
      destroyRowBuffPos(pNewPos);
×
1015
      break;
×
1016
    }
1017
    streamStateCurPrev_rocksdb(pCur);
×
1018
  }
1019

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

1028
int32_t streamFileStateGetSelectRowSize(SStreamFileState* pFileState) { return pFileState->selectivityRowSize; }
595,538✔
1029

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

1035
void* getRowStateBuff(SStreamFileState* pFileState) { return pFileState->rowStateBuff; }
40,487✔
1036
void* getSearchBuff(SStreamFileState* pFileState) { return pFileState->searchBuff; }
14,423,068✔
1037

1038
void* getStateFileStore(SStreamFileState* pFileState) { return pFileState->pFileStore; }
17,990✔
1039

1040
bool isDeteled(SStreamFileState* pFileState, TSKEY ts) {
13,733,833✔
1041
  return pFileState->deleteMark != INT64_MAX && pFileState->maxTs > 0 &&
19,486,752✔
1042
         ts < (pFileState->maxTs - pFileState->deleteMark);
5,752,919✔
1043
}
1044

1045
bool isFlushedState(SStreamFileState* pFileState, TSKEY ts, TSKEY gap) { return ts <= (pFileState->flushMark + gap); }
11,149,344✔
1046

1047
TSKEY getFlushMark(SStreamFileState* pFileState) { return pFileState->flushMark; };
14✔
1048

1049
int32_t getRowStateRowSize(SStreamFileState* pFileState) { return pFileState->rowSize; }
596,738✔
1050

1051
int32_t getFunctionRowBuff(SStreamFileState* pFileState, void* pKey, int32_t keyLen, void** pVal, int32_t* pVLen) {
595,550✔
1052
  int32_t winCode = TSDB_CODE_SUCCESS;
595,550✔
1053
  return pFileState->stateFunctionGetFn(pFileState, pKey, keyLen, pVal, pVLen, &winCode);
595,550✔
1054
}
1055

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

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

1078
    void*        pVal = NULL;
×
1079
    int32_t      vlen = 0;
×
1080
    SRowBuffPos* pNewPos = getNewRowPosForWrite(pFileState);
×
1081
    winRes = streamStateFillGetKVByCur_rocksdb(pCur, pNewPos->pKey, (const void**)&pVal, &vlen);
×
NEW
1082
    qDebug("===stream=== get state by cur winres:%d. %s", winRes, __func__);
×
1083
    if (winRes != TSDB_CODE_SUCCESS || isFlushedState(pFileState, pFileState->getTs(pNewPos->pKey), 0)) {
×
1084
      destroyRowBuffPos(pNewPos);
×
1085
      SListNode* pNode = tdListPopTail(pFileState->usedBuffs);
×
1086
      taosMemoryFreeClear(pNode);
×
1087
      taosMemoryFreeClear(pVal);
×
1088
      break;
×
1089
    }
1090

NEW
1091
    if (vlen != pFileState->rowSize) {
×
NEW
1092
      qError("row size mismatch, expect:%d, actual:%d", pFileState->rowSize, vlen);
×
NEW
1093
      code = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
NEW
1094
      taosMemoryFreeClear(pVal);
×
NEW
1095
      QUERY_CHECK_CODE(code, lino, _end);
×
1096
    }
1097

1098
    memcpy(pNewPos->pRowBuff, pVal, vlen);
×
1099
    taosMemoryFreeClear(pVal);
×
1100
    pNewPos->beFlushed = true;
×
NEW
1101
    qDebug("===stream=== read checkpoint state from disc. %s", __func__);
×
1102
    winRes = tSimpleHashPut(pFileState->rowStateBuff, pNewPos->pKey, pFileState->keyLen, &pNewPos, POINTER_BYTES);
×
1103
    if (winRes != TSDB_CODE_SUCCESS) {
×
1104
      destroyRowBuffPos(pNewPos);
×
1105
      break;
×
1106
    }
1107
    streamStateCurPrev_rocksdb(pCur);
×
1108
  }
1109
  streamStateFreeCur(pCur);
×
1110

1111
_end:
×
1112
  if (code != TSDB_CODE_SUCCESS) {
×
1113
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1114
  }
1115
  return code;
×
1116
}
1117

1118
int32_t getRowBuff(SStreamFileState* pFileState, void* pKey, int32_t keyLen, void** pVal, int32_t* pVLen,
740✔
1119
                   int32_t* pWinCode) {
1120
  int32_t code = TSDB_CODE_SUCCESS;
740✔
1121
  int32_t lino = 0;
740✔
1122
  (*pWinCode) = TSDB_CODE_FAILED;
740✔
1123
  pFileState->maxTs = TMAX(pFileState->maxTs, pFileState->getTs(pKey));
740✔
1124
  SRowBuffPos** ppPos = tSimpleHashGet(pFileState->rowStateBuff, pKey, keyLen);
740✔
1125
  if (ppPos) {
740✔
1126
    *pVLen = pFileState->rowSize;
250✔
1127
    *pVal = *ppPos;
250✔
1128
    (*ppPos)->beUsed = true;
250✔
1129
    (*ppPos)->beFlushed = false;
250✔
1130
    (*pWinCode) = TSDB_CODE_SUCCESS;
250✔
1131
    if ((*ppPos)->pRowBuff == NULL) {
250!
1132
      code = recoverStateRowBuff(pFileState, *ppPos);
×
1133
      QUERY_CHECK_CODE(code, lino, _end);
×
1134
    }
1135
    goto _end;
250✔
1136
  }
1137
  TSKEY ts = pFileState->getTs(pKey);
490✔
1138
  if (!isDeteled(pFileState, ts) && isFlushedState(pFileState, ts, 0)) {
490!
1139
    int32_t len = 0;
×
1140
    void*   p = NULL;
×
1141
    (*pWinCode) = pFileState->stateFileGetFn(pFileState, pKey, &p, &len);
×
1142
    qDebug("===stream===get %" PRId64 " from disc, res %d", ts, (*pWinCode));
×
1143
    if ((*pWinCode) == TSDB_CODE_SUCCESS) {
×
1144
      SRowBuffPos* pNewPos = getNewRowPosForWrite(pFileState);
×
1145
      if (!pNewPos || !pNewPos->pRowBuff) {
×
1146
        code = TSDB_CODE_OUT_OF_MEMORY;
×
1147
        QUERY_CHECK_CODE(code, lino, _end);
×
1148
      }
1149

1150
      memcpy(pNewPos->pKey, pKey, keyLen);
×
1151
      memcpy(pNewPos->pRowBuff, p, len);
×
1152
      code = tSimpleHashPut(pFileState->rowStateBuff, pKey, keyLen, &pNewPos, POINTER_BYTES);
×
1153
      QUERY_CHECK_CODE(code, lino, _end);
×
1154

1155
      if (pVal) {
×
1156
        *pVLen = pFileState->rowSize;
×
1157
        *pVal = pNewPos;
×
1158
      }
1159
    }
1160
    taosMemoryFree(p);
×
1161
  }
1162

1163
_end:
490✔
1164
  if (code != TSDB_CODE_SUCCESS) {
740!
1165
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1166
  }
1167
  return code;
740✔
1168
}
1169

1170
int32_t streamFileStateGroupPut(SStreamFileState* pFileState, int64_t groupId, void* value, int32_t vLen) {
186✔
1171
  int32_t code = TSDB_CODE_SUCCESS;
186✔
1172
  int32_t lino = 0;
186✔
1173
  if (value != NULL) {
186!
1174
    code = TSDB_CODE_INVALID_PARA;
×
1175
    QUERY_CHECK_CODE(code, lino, _end);
×
1176
  }
1177

1178
  if (tSimpleHashGet(pFileState->pGroupIdMap, &groupId, sizeof(int64_t)) == NULL) {
186✔
1179
    if (tSimpleHashGetSize(pFileState->pGroupIdMap) <= MAX_GROUP_ID_NUM) {
42!
1180
      code = tSimpleHashPut(pFileState->pGroupIdMap, &groupId, sizeof(int64_t), NULL, 0);
42✔
1181
      QUERY_CHECK_CODE(code, lino, _end);
42!
1182
    }
1183
    code = streamStatePutParTag_rocksdb(pFileState->pFileStore, groupId, value, vLen);
42✔
1184
    QUERY_CHECK_CODE(code, lino, _end);
42!
1185
  }
1186

1187
_end:
186✔
1188
  if (code != TSDB_CODE_SUCCESS) {
186!
1189
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1190
  }
1191
  return code;
186✔
1192
}
1193

1194
void streamFileStateGroupCurNext(SStreamStateCur* pCur) {
661✔
1195
  SStreamFileState* pFileState = (SStreamFileState*)pCur->pStreamFileState;
661✔
1196
  if (pCur->hashIter == -1) {
661!
1197
    streamStateCurNext(pFileState->pFileStore, pCur);
×
1198
    return;
×
1199
  }
1200

1201
  int64_t gpId = *(int64_t*)tSimpleHashGetKey(pCur->pHashData, NULL);
661!
1202
  pCur->minGpId = TMAX(pCur->minGpId, gpId);
661✔
1203

1204
  SSHashObj* pHash = pFileState->pGroupIdMap;
661✔
1205
  pCur->pHashData = tSimpleHashIterate(pHash, pCur->pHashData, &pCur->hashIter);
661✔
1206
  if (!pCur->pHashData) {
661✔
1207
    pCur->hashIter = -1;
404✔
1208
    streamStateParTagSeekKeyNext_rocksdb(pFileState->pFileStore, pCur->minGpId, pCur);
404✔
1209
    return;
404✔
1210
  }
1211
}
1212

1213
int32_t streamFileStateGroupGetKVByCur(SStreamStateCur* pCur, int64_t* pKey, void** pVal, int32_t* pVLen) {
1,969✔
1214
  int32_t code = TSDB_CODE_SUCCESS;
1,969✔
1215
  if (pCur->pHashData) {
1,969✔
1216
    *pKey = *(int64_t*)tSimpleHashGetKey(pCur->pHashData, NULL);
661!
1217
    return code;
661✔
1218
  }
1219
  return streamStateParTagGetKVByCur_rocksdb(pCur, pKey, NULL, NULL);
1,308✔
1220
}
1221

1222
SSHashObj* getGroupIdCache(SStreamFileState* pFileState) {
1,308✔
1223
  return pFileState->pGroupIdMap;
1,308✔
1224
}
1225

UNCOV
1226
void setFillInfo(SStreamFileState* pFileState) {
×
UNCOV
1227
  pFileState->hasFillCatch = false;
×
UNCOV
1228
}
×
1229

1230
void clearExpiredState(SStreamFileState* pFileState) {
2,456✔
1231
  SSHashObj* pSearchBuff = pFileState->searchBuff;
2,456✔
1232
  void*      pIte = NULL;
2,456✔
1233
  int32_t    iter = 0;
2,456✔
1234
  while ((pIte = tSimpleHashIterate(pSearchBuff, pIte, &iter)) != NULL) {
3,630✔
1235
    SArray* pWinStates = *((void**)pIte);
1,174✔
1236
    int32_t size = taosArrayGetSize(pWinStates);
1,174✔
1237
    for (int32_t i = 0; i < size - 1; i++) {
1,438✔
1238
      SWinKey* pKey = taosArrayGet(pWinStates, i);
264✔
1239
      int32_t  code_buff = pFileState->stateBuffRemoveFn(pFileState->rowStateBuff, pKey, sizeof(SWinKey));
264✔
1240
      qTrace("clear expired buff, ts:%" PRId64 ". %s at line %d res:%d", pKey->ts, __func__, __LINE__, code_buff);
264!
1241

1242
      if (isFlushedState(pFileState, pKey->ts, 0)) {
264✔
1243
        int32_t code_file = pFileState->stateFileRemoveFn(pFileState, pKey);
14✔
1244
        qTrace("clear expired file, ts:%" PRId64 ". %s at line %d res:%d", pKey->ts, __func__, __LINE__, code_file);
14!
1245
      }
1246
    }
1247
    taosArrayRemoveBatch(pWinStates, 0, size - 1, NULL);
1,174✔
1248
  }
1249
}
2,456✔
1250

1251
int32_t getStateSearchRowBuff(SStreamFileState* pFileState, const SWinKey* pKey, void** pVal, int32_t* pVLen,
×
1252
                           int32_t* pWinCode) {
1253
  int32_t code = TSDB_CODE_SUCCESS;
×
1254
  int32_t lino = 0;
×
1255

1256
  code = addRowBuffIfNotExist(pFileState, (void*)pKey, sizeof(SWinKey), pVal, pVLen, pWinCode);
×
1257
  QUERY_CHECK_CODE(code, lino, _end);
×
1258

1259
  SArray*    pWinStates = NULL;
×
1260
  SSHashObj* pSearchBuff = getSearchBuff(pFileState);
×
1261
  void**     ppBuff = tSimpleHashGet(pSearchBuff, &pKey->groupId, sizeof(uint64_t));
×
1262
  if (ppBuff) {
×
1263
    pWinStates = (SArray*)(*ppBuff);
×
1264
  } else {
1265
    pWinStates = taosArrayInit(16, sizeof(SWinKey));
×
1266
    QUERY_CHECK_NULL(pWinStates, code, lino, _end, terrno);
×
1267

1268
    code = tSimpleHashPut(pSearchBuff, &pKey->groupId, sizeof(uint64_t), &pWinStates, POINTER_BYTES);
×
1269
    QUERY_CHECK_CODE(code, lino, _end);
×
1270
  }
1271

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

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

1307
  if (size >= MAX_NUM_OF_CACHE_WIN) {
×
1308
    int32_t num = size - NUM_OF_CACHE_WIN;
×
1309
    taosArrayRemoveBatch(pWinStates, 0, num, NULL);
×
1310
  }
1311

1312
_end:
×
1313
  if (code != TSDB_CODE_SUCCESS) {
×
1314
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1315
  }
1316
  return code;
×
1317
}
1318

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

1384
_end:
×
1385
  if (code != TSDB_CODE_SUCCESS) {
×
1386
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1387
  }
1388
  return code;
×
1389
}
1390

1391
int32_t addSearchItem(SStreamFileState* pFileState, SArray* pWinStates, const SWinKey* pKey) {
930✔
1392
  int32_t code = TSDB_CODE_SUCCESS;
930✔
1393
  int32_t lino = 0;
930✔
1394
  int32_t size = taosArrayGetSize(pWinStates);
930✔
1395
  int32_t index = binarySearch(pWinStates, size, pKey, fillStateKeyCompare);
930✔
1396
  if (!isFlushedState(pFileState, pKey->ts, 0) || index >= 0 || size == 0) {
930!
1397
    if (index >= 0) {
930✔
1398
      SWinKey* pTmpKey = taosArrayGet(pWinStates, index);
856✔
1399
      if (winKeyCmprImpl(pTmpKey, pKey) == 0) {
856✔
1400
        goto _end;
592✔
1401
      }
1402
    }
1403
    index++;
338✔
1404
    void* tmp = taosArrayInsert(pWinStates, index, pKey);
338✔
1405
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
338!
1406
  }
1407

1408
  if (size >= MAX_NUM_OF_CACHE_WIN) {
338!
1409
    int32_t num = size - NUM_OF_CACHE_WIN;
×
1410
    taosArrayRemoveBatch(pWinStates, 0, num, NULL);
×
1411
  }
1412
_end:
338✔
1413
  if (code != TSDB_CODE_SUCCESS) {
930!
1414
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1415
  }
1416
  return code;
930✔
1417
}
1418

1419
int32_t addArrayBuffIfNotExist(SSHashObj* pSearchBuff, uint64_t groupId, SArray** ppResStates) {
1,458✔
1420
  int32_t code = TSDB_CODE_SUCCESS;
1,458✔
1421
  int32_t lino = 0; 
1,458✔
1422
  SArray*    pWinStates = NULL;
1,458✔
1423
  void**     ppBuff = tSimpleHashGet(pSearchBuff, &groupId, sizeof(uint64_t));
1,458✔
1424
  if (ppBuff) {
1,458✔
1425
    pWinStates = (SArray*)(*ppBuff);
1,384✔
1426
  } else {
1427
    pWinStates = taosArrayInit(16, sizeof(SWinKey));
74✔
1428
    QUERY_CHECK_NULL(pWinStates, code, lino, _end, terrno);
74!
1429

1430
    code = tSimpleHashPut(pSearchBuff, &groupId, sizeof(uint64_t), &pWinStates, POINTER_BYTES);
74✔
1431
    QUERY_CHECK_CODE(code, lino, _end);
74!
1432
  }
1433

1434
  (*ppResStates) = pWinStates;
1,458✔
1435

1436
_end:
1,458✔
1437
  if (code != TSDB_CODE_SUCCESS) {
1,458!
1438
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1439
  }
1440
  return code;
1,458✔
1441
}
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