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

taosdata / TDengine / #3633

11 Mar 2025 12:59PM UTC coverage: 0.0% (-60.7%) from 60.719%
#3633

push

travis-ci

web-flow
Merge pull request #30118 from taosdata/wl30

udpate ci workflow

0 of 280412 branches covered (0.0%)

Branch coverage included in aggregate %.

0 of 275582 relevant lines covered (0.0%)

0.0 hits per line

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

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

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

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

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

100
void stateHashBuffCleanupFn(void* pBuff) { tSimpleHashCleanup(pBuff); }
×
101

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

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

110
void* intervalCreateStateKey(SRowBuffPos* pPos, int64_t num) {
×
111
  SStateKey* pStateKey = taosMemoryCalloc(1, sizeof(SStateKey));
×
112
  if (pStateKey == NULL) {
×
113
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
114
    return NULL;
×
115
  }
116
  SWinKey* pWinKey = pPos->pKey;
×
117
  pStateKey->key = *pWinKey;
×
118
  pStateKey->opNum = num;
×
119
  return pStateKey;
×
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) {
×
134
  return streamStateSessionDel_rocksdb(pFileState->pFileStore, pKey);
×
135
}
136

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

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

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

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

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

180
  SStreamFileState* pFileState = taosMemoryCalloc(1, sizeof(SStreamFileState));
×
181
  QUERY_CHECK_NULL(pFileState, code, lino, _end, terrno);
×
182

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

188
  pFileState->freeBuffs = tdListNew(POINTER_BYTES);
×
189
  QUERY_CHECK_NULL(pFileState->freeBuffs, code, lino, _end, terrno);
×
190

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

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

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

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

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

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

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

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

257
  pFileState->hasFillCatch = true;
×
258

259
  if (type == STREAM_STATE_BUFF_HASH || type == STREAM_STATE_BUFF_HASH_SEARCH) {
×
260
    code = recoverSnapshot(pFileState, checkpointId);
×
261
  } else if (type == STREAM_STATE_BUFF_SORT) {
×
262
    code = recoverSession(pFileState, checkpointId);
×
263
  } else if (type == STREAM_STATE_BUFF_HASH_SORT) {
×
264
    code = recoverFillSnapshot(pFileState, checkpointId);
×
265
  }
266
  QUERY_CHECK_CODE(code, lino, _end);
×
267

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

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

287
void destroyRowBuffPos(SRowBuffPos* pPos) {
×
288
  taosMemoryFreeClear(pPos->pKey);
×
289
  taosMemoryFreeClear(pPos->pRowBuff);
×
290
  taosMemoryFree(pPos);
×
291
}
×
292

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

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

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

318
void streamFileStateDestroy(SStreamFileState* pFileState) {
×
319
  if (!pFileState) {
×
320
    return;
×
321
  }
322

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

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

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

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

355
  SListNode* pNode = NULL;
×
356
  while ((pNode = tdListNext(&iter)) != NULL) {
×
357
    SRowBuffPos* pPos = *(SRowBuffPos**)(pNode->data);
×
358
    if (all || (pFileState->getTs(pPos->pKey) < ts && !pPos->beUsed)) {
×
359
      code = putFreeBuff(pFileState, pPos);
×
360
      QUERY_CHECK_CODE(code, lino, _end);
×
361

362
      if (!all) {
×
363
        pFileState->stateBuffRemoveByPosFn(pFileState, pPos);
×
364
      }
365
      destroyRowBuffPos(pPos);
×
366
      SListNode* tmp = tdListPopNode(pFileState->usedBuffs, pNode);
×
367
      taosMemoryFreeClear(tmp);
×
368
    }
369
  }
370

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

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

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

395
        pFileState->flushMark = TMAX(pFileState->flushMark, pFileState->getTs(pPos->pKey));
×
396
        pFileState->stateBuffRemoveByPosFn(pFileState, pPos);
×
397
        if (pPos->beUsed == false) {
×
398
          SListNode* tmp = tdListPopNode(pFileState->usedBuffs, pNode);
×
399
          taosMemoryFreeClear(tmp);
×
400
        }
401
        if (pPos->pRowBuff) {
×
402
          i++;
×
403
        }
404
      }
405
    }
406
  }
407
  qDebug("clear flushed row buff. %d rows to disk. is all:%d", listNEles(pFlushList), all);
×
408

409
_end:
×
410
  if (code != TSDB_CODE_SUCCESS) {
×
411
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
412
  }
413
  return code;
×
414
}
415

416
void streamFileStateClear(SStreamFileState* pFileState) {
×
417
  pFileState->flushMark = INT64_MIN;
×
418
  pFileState->maxTs = INT64_MIN;
×
419
  tSimpleHashClear(pFileState->rowStateBuff);
×
420
  clearExpiredRowBuff(pFileState, 0, true);
×
421
}
×
422

423
bool needClearDiskBuff(SStreamFileState* pFileState) { return pFileState->flushMark > 0; }
×
424

425
void streamFileStateReleaseBuff(SStreamFileState* pFileState, SRowBuffPos* pPos, bool used) { pPos->beUsed = used; }
×
426

427
int32_t popUsedBuffs(SStreamFileState* pFileState, SStreamSnapshot* pFlushList, uint64_t max, bool used) {
×
428
  int32_t   code = TSDB_CODE_SUCCESS;
×
429
  int32_t   lino = 0;
×
430
  uint64_t  i = 0;
×
431
  SListIter iter = {0};
×
432
  tdListInitIter(pFileState->usedBuffs, &iter, TD_LIST_FORWARD);
×
433

434
  SListNode* pNode = NULL;
×
435
  while ((pNode = tdListNext(&iter)) != NULL && i < max) {
×
436
    SRowBuffPos* pPos = *(SRowBuffPos**)pNode->data;
×
437
    if (pPos->beUsed == used) {
×
438
      if (used && !pPos->pRowBuff) {
×
439
        continue;
×
440
      }
441
      code = tdListAppend(pFlushList, &pPos);
×
442
      QUERY_CHECK_CODE(code, lino, _end);
×
443

444
      pFileState->flushMark = TMAX(pFileState->flushMark, pFileState->getTs(pPos->pKey));
×
445
      pFileState->stateBuffRemoveByPosFn(pFileState, pPos);
×
446
      if (pPos->beUsed == false) {
×
447
        SListNode* tmp = tdListPopNode(pFileState->usedBuffs, pNode);
×
448
        taosMemoryFreeClear(tmp);
×
449
      }
450
      if (pPos->pRowBuff) {
×
451
        i++;
×
452
      }
453
    }
454
  }
455

456
  qInfo("stream state flush %d rows to disk. is used:%d", listNEles(pFlushList), used);
×
457

458
_end:
×
459
  if (code != TSDB_CODE_SUCCESS) {
×
460
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
461
  }
462
  return code;
×
463
}
464

465
int32_t flushRowBuff(SStreamFileState* pFileState) {
×
466
  int32_t          code = TSDB_CODE_SUCCESS;
×
467
  int32_t          lino = 0;
×
468
  SStreamSnapshot* pFlushList = tdListNew(POINTER_BYTES);
×
469
  if (!pFlushList) {
×
470
    code = TSDB_CODE_OUT_OF_MEMORY;
×
471
    QUERY_CHECK_CODE(code, lino, _end);
×
472
  }
473

474
  uint64_t num = (uint64_t)(pFileState->curRowCount * FLUSH_RATIO);
×
475
  num = TMAX(num, FLUSH_NUM);
×
476
  code = clearFlushedRowBuff(pFileState, pFlushList, num, false);
×
477
  QUERY_CHECK_CODE(code, lino, _end);
×
478

479
  if (isListEmpty(pFlushList)) {
×
480
    code = popUsedBuffs(pFileState, pFlushList, num, false);
×
481
    QUERY_CHECK_CODE(code, lino, _end);
×
482

483
    if (isListEmpty(pFlushList)) {
×
484
      code = popUsedBuffs(pFileState, pFlushList, num, true);
×
485
      QUERY_CHECK_CODE(code, lino, _end);
×
486
    }
487
  }
488

489
  if (pFileState->searchBuff) {
×
490
    code = clearFlushedRowBuff(pFileState, pFlushList, pFileState->curRowCount, true);
×
491
    QUERY_CHECK_CODE(code, lino, _end);
×
492
  }
493

494
  flushSnapshot(pFileState, pFlushList, false);
×
495

496
  SListIter fIter = {0};
×
497
  tdListInitIter(pFlushList, &fIter, TD_LIST_FORWARD);
×
498
  SListNode* pNode = NULL;
×
499
  while ((pNode = tdListNext(&fIter)) != NULL) {
×
500
    SRowBuffPos* pPos = *(SRowBuffPos**)pNode->data;
×
501
    code = putFreeBuff(pFileState, pPos);
×
502
    QUERY_CHECK_CODE(code, lino, _end);
×
503
  }
504

505
  tdListFreeP(pFlushList, destroyRowBuffPosPtr);
×
506

507
_end:
×
508
  if (code != TSDB_CODE_SUCCESS) {
×
509
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
510
  }
511
  return code;
×
512
}
513

514
int32_t clearRowBuff(SStreamFileState* pFileState) {
×
515
  if (pFileState->deleteMark != INT64_MAX) {
×
516
    clearExpiredRowBuff(pFileState, pFileState->maxTs - pFileState->deleteMark, false);
×
517
  }
518
  do {
519
    int32_t code = flushRowBuff(pFileState);
×
520
    if (code != TSDB_CODE_SUCCESS) {
×
521
      return code;
×
522
    }
523
  } while (isListEmpty(pFileState->freeBuffs) && pFileState->curRowCount == pFileState->maxRowCount);
×
524
  return TSDB_CODE_SUCCESS;
×
525
}
526

527
void* getFreeBuff(SStreamFileState* pFileState) {
×
528
  SList*     lists = pFileState->freeBuffs;
×
529
  int32_t    buffSize = pFileState->rowSize;
×
530
  SListNode* pNode = tdListPopHead(lists);
×
531
  if (!pNode) {
×
532
    return NULL;
×
533
  }
534
  void* ptr = *(void**)pNode->data;
×
535
  memset(ptr, 0, buffSize);
×
536
  taosMemoryFree(pNode);
×
537
  return ptr;
×
538
}
539

540
void streamFileStateClearBuff(SStreamFileState* pFileState, SRowBuffPos* pPos) {
×
541
  if (pPos->pRowBuff) {
×
542
    memset(pPos->pRowBuff, 0, pFileState->rowSize);
×
543
  }
544
}
×
545

546
SRowBuffPos* getNewRowPos(SStreamFileState* pFileState) {
×
547
  int32_t      code = TSDB_CODE_SUCCESS;
×
548
  int32_t      lino = 0;
×
549
  SRowBuffPos* pPos = taosMemoryCalloc(1, sizeof(SRowBuffPos));
×
550
  if (!pPos) {
×
551
    code = terrno;
×
552
    QUERY_CHECK_CODE(code, lino, _error);
×
553
  }
554

555
  pPos->pKey = taosMemoryCalloc(1, pFileState->keyLen);
×
556
  if (!pPos->pKey) {
×
557
    code = terrno;
×
558
    QUERY_CHECK_CODE(code, lino, _error);
×
559
  }
560

561
  void* pBuff = getFreeBuff(pFileState);
×
562
  if (pBuff) {
×
563
    pPos->pRowBuff = pBuff;
×
564
    goto _end;
×
565
  }
566

567
  if (pFileState->curRowCount < pFileState->maxRowCount) {
×
568
    pBuff = taosMemoryCalloc(1, pFileState->rowSize);
×
569
    QUERY_CHECK_NULL(pBuff, code, lino, _error, terrno);
×
570
    pPos->pRowBuff = pBuff;
×
571
    pFileState->curRowCount++;
×
572
    goto _end;
×
573
  }
574

575
  code = clearRowBuff(pFileState);
×
576
  QUERY_CHECK_CODE(code, lino, _error);
×
577

578
  pPos->pRowBuff = getFreeBuff(pFileState);
×
579

580
_end:
×
581
  code = tdListAppend(pFileState->usedBuffs, &pPos);
×
582
  QUERY_CHECK_CODE(code, lino, _error);
×
583

584
  QUERY_CHECK_CONDITION((pPos->pRowBuff != NULL), code, lino, _error, TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
×
585
_error:
×
586
  if (code != TSDB_CODE_SUCCESS) {
×
587
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
588
    return NULL;
×
589
  }
590

591
  return pPos;
×
592
}
593

594
SRowBuffPos* getNewRowPosForWrite(SStreamFileState* pFileState) {
×
595
  int32_t      code = TSDB_CODE_SUCCESS;
×
596
  int32_t      lino = 0;
×
597
  SRowBuffPos* newPos = getNewRowPos(pFileState);
×
598
  if (!newPos) {
×
599
    code = TSDB_CODE_OUT_OF_MEMORY;
×
600
    QUERY_CHECK_CODE(code, lino, _error);
×
601
  }
602
  newPos->beUsed = true;
×
603
  newPos->beFlushed = false;
×
604
  newPos->needFree = false;
×
605
  newPos->beUpdated = true;
×
606
  return newPos;
×
607

608
_error:
×
609
  if (code != TSDB_CODE_SUCCESS) {
×
610
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
611
  }
612
  return NULL;
×
613
}
614

615
int32_t addRowBuffIfNotExist(SStreamFileState* pFileState, void* pKey, int32_t keyLen, void** pVal, int32_t* pVLen,
×
616
                             int32_t* pWinCode) {
617
  int32_t code = TSDB_CODE_SUCCESS;
×
618
  int32_t lino = 0;
×
619
  (*pWinCode) = TSDB_CODE_SUCCESS;
×
620
  pFileState->maxTs = TMAX(pFileState->maxTs, pFileState->getTs(pKey));
×
621
  SRowBuffPos** pos = tSimpleHashGet(pFileState->rowStateBuff, pKey, keyLen);
×
622
  if (pos) {
×
623
    if (pVal != NULL) {
×
624
      *pVLen = pFileState->rowSize;
×
625
      *pVal = *pos;
×
626
      (*pos)->beUsed = true;
×
627
      (*pos)->beFlushed = false;
×
628
    }
629
    goto _end;
×
630
  }
631
  SRowBuffPos* pNewPos = getNewRowPosForWrite(pFileState);
×
632
  if (!pNewPos || !pNewPos->pRowBuff) {
×
633
    code = TSDB_CODE_OUT_OF_MEMORY;
×
634
    QUERY_CHECK_CODE(code, lino, _end);
×
635
  }
636

637
  memcpy(pNewPos->pKey, pKey, keyLen);
×
638
  (*pWinCode) = TSDB_CODE_FAILED;
×
639

640
  TSKEY ts = pFileState->getTs(pKey);
×
641
  if (!isDeteled(pFileState, ts) && isFlushedState(pFileState, ts, 0)) {
×
642
    int32_t len = 0;
×
643
    void*   p = NULL;
×
644
    (*pWinCode) = pFileState->stateFileGetFn(pFileState, pKey, &p, &len);
×
645
    qDebug("===stream===get %" PRId64 " from disc, res %d", ts, (*pWinCode));
×
646
    if ((*pWinCode) == TSDB_CODE_SUCCESS) {
×
647
      memcpy(pNewPos->pRowBuff, p, len);
×
648
    }
649
    taosMemoryFree(p);
×
650
  }
651

652
  code = tSimpleHashPut(pFileState->rowStateBuff, pKey, keyLen, &pNewPos, POINTER_BYTES);
×
653
  QUERY_CHECK_CODE(code, lino, _end);
×
654

655
  if (pVal) {
×
656
    *pVLen = pFileState->rowSize;
×
657
    *pVal = pNewPos;
×
658
  }
659

660
_end:
×
661
  if (code != TSDB_CODE_SUCCESS) {
×
662
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
663
  }
664
  return code;
×
665
}
666

667
void deleteRowBuff(SStreamFileState* pFileState, const void* pKey, int32_t keyLen) {
×
668
  int32_t code_buff = pFileState->stateBuffRemoveFn(pFileState->rowStateBuff, pKey, keyLen);
×
669
  qTrace("%s at line %d res:%d", __func__, __LINE__, code_buff);
×
670
  int32_t code_file = pFileState->stateFileRemoveFn(pFileState, pKey);
×
671
  qTrace("%s at line %d res:%d", __func__, __LINE__, code_file);
×
672
  if (pFileState->searchBuff != NULL) {
×
673
    deleteHashSortRowBuff(pFileState, pKey);
×
674
  }
675
}
×
676

677
void deleteRowBuffByGroupId(SStreamFileState* pFileState, uint64_t groupId) {
×
678
  SSHashObj* pRowMap = pFileState->rowStateBuff;
×
679
  void*   pIte = NULL;
×
680
  int32_t iter = 0;
×
681
  while ((pIte = tSimpleHashIterate(pRowMap, pIte, &iter)) != NULL) {
×
682
    size_t keyLen = 0;
×
683
    SWinKey* pKey = tSimpleHashGetKey(pIte, &keyLen);
×
684
    if (pKey->groupId == groupId) {
×
685
      int32_t tmpRes = tSimpleHashIterateRemove(pRowMap, pKey, keyLen, &pIte, &iter);
×
686
      qTrace("%s at line %d res:%d", __func__, __LINE__, tmpRes);
×
687
    }
688
  }
689

690
  while (1) {
×
691
    SWinKey tmp = {.ts = INT64_MIN, .groupId = groupId};
×
692
    SStreamStateCur* pCur = streamStateSeekKeyNext_rocksdb(pFileState->pFileStore, &tmp);
×
693
    SWinKey delKey = {.groupId = groupId};
×
694
    int32_t code = streamStateGetGroupKVByCur_rocksdb(pFileState->pFileStore, pCur, &delKey, NULL, 0);
×
695
    if (code != TSDB_CODE_SUCCESS) {
×
696
      break;
×
697
    }
698
    code = streamStateDel_rocksdb(pFileState->pFileStore, &delKey);
×
699
    qTrace("%s at line %d res:%d", __func__, __LINE__, code);
×
700
  }
701
}
×
702

703
static int32_t recoverSessionRowBuff(SStreamFileState* pFileState, SRowBuffPos* pPos) {
×
704
  int32_t code = TSDB_CODE_SUCCESS;
×
705
  int32_t lino = 0;
×
706
  int32_t len = 0;
×
707
  void*   pBuff = NULL;
×
708
  code = pFileState->stateFileGetFn(pFileState, pPos->pKey, &pBuff, &len);
×
709
  QUERY_CHECK_CODE(code, lino, _end);
×
710
  memcpy(pPos->pRowBuff, pBuff, len);
×
711
  taosMemoryFree(pBuff);
×
712

713
_end:
×
714
  if (code != TSDB_CODE_SUCCESS) {
×
715
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
716
  }
717
  return code;
×
718
}
719

720
static int32_t recoverStateRowBuff(SStreamFileState* pFileState, SRowBuffPos* pPos) {
×
721
  int32_t code = TSDB_CODE_SUCCESS;
×
722
  int32_t lino = 0;
×
723
  pPos->pRowBuff = getFreeBuff(pFileState);
×
724
  if (!pPos->pRowBuff) {
×
725
    if (pFileState->curRowCount < pFileState->maxRowCount) {
×
726
      pPos->pRowBuff = taosMemoryCalloc(1, pFileState->rowSize);
×
727
      if (!pPos->pRowBuff) {
×
728
        code = terrno;
×
729
        QUERY_CHECK_CODE(code, lino, _end);
×
730
      }
731
      pFileState->curRowCount++;
×
732
    } else {
733
      code = clearRowBuff(pFileState);
×
734
      QUERY_CHECK_CODE(code, lino, _end);
×
735
      pPos->pRowBuff = getFreeBuff(pFileState);
×
736
    }
737
    QUERY_CHECK_CONDITION((pPos->pRowBuff != NULL), code, lino, _end, TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
×
738
  }
739

740
  code = recoverSessionRowBuff(pFileState, pPos);
×
741
  QUERY_CHECK_CODE(code, lino, _end);
×
742

743
_end:
×
744
  if (code != TSDB_CODE_SUCCESS) {
×
745
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
746
  }
747
  return code;
×
748
}
749

750
int32_t getRowBuffByPos(SStreamFileState* pFileState, SRowBuffPos* pPos, void** pVal) {
×
751
  int32_t code = TSDB_CODE_SUCCESS;
×
752
  int32_t lino = 0;
×
753
  if (pPos->pRowBuff) {
×
754
    if (pPos->needFree) {
×
755
      code = recoverSessionRowBuff(pFileState, pPos);
×
756
      QUERY_CHECK_CODE(code, lino, _end);
×
757
    }
758
    (*pVal) = pPos->pRowBuff;
×
759
    goto _end;
×
760
  }
761

762
  code = recoverStateRowBuff(pFileState, pPos);
×
763
  QUERY_CHECK_CODE(code, lino, _end);
×
764

765
  (*pVal) = pPos->pRowBuff;
×
766
  // if (!pPos->needFree) {
767
  //   code = tdListPrepend(pFileState->usedBuffs, &pPos);
768
  //   QUERY_CHECK_CODE(code, lino, _end);
769
  // }
770

771
_end:
×
772
  if (code != TSDB_CODE_SUCCESS) {
×
773
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
774
  }
775
  return code;
×
776
}
777

778
bool hasRowBuff(SStreamFileState* pFileState, void* pKey, int32_t keyLen) {
×
779
  SRowBuffPos** pos = tSimpleHashGet(pFileState->rowStateBuff, pKey, keyLen);
×
780
  if (pos) {
×
781
    return true;
×
782
  }
783
  return false;
×
784
}
785

786
SStreamSnapshot* getSnapshot(SStreamFileState* pFileState) {
×
787
  int64_t mark = (pFileState->deleteMark == INT64_MAX || pFileState->maxTs == INT64_MIN)
×
788
                     ? INT64_MIN
789
                     : pFileState->maxTs - pFileState->deleteMark;
×
790
  clearExpiredRowBuff(pFileState, mark, false);
×
791
  return pFileState->usedBuffs;
×
792
}
793

794
void flushSnapshot(SStreamFileState* pFileState, SStreamSnapshot* pSnapshot, bool flushState) {
×
795
  int32_t   code = TSDB_CODE_SUCCESS;
×
796
  int32_t   lino = 0;
×
797
  SListIter iter = {0};
×
798
  tdListInitIter(pSnapshot, &iter, TD_LIST_FORWARD);
×
799

800
  const int32_t BATCH_LIMIT = 256;
×
801

802
  int64_t    st = taosGetTimestampMs();
×
803
  SListNode* pNode = NULL;
×
804

805
  int idx = streamStateGetCfIdx(pFileState->pFileStore, pFileState->cfName);
×
806

807
  int32_t len = (pFileState->rowSize + sizeof(uint64_t) + sizeof(int32_t) + 64) * 2;
×
808
  char*   buf = taosMemoryCalloc(1, len);
×
809
  if (!buf) {
×
810
    code = terrno;
×
811
    QUERY_CHECK_CODE(code, lino, _end);
×
812
  }
813

814
  void* batch = streamStateCreateBatch();
×
815
  if (!batch) {
×
816
    code = TSDB_CODE_OUT_OF_MEMORY;
×
817
    QUERY_CHECK_CODE(code, lino, _end);
×
818
  }
819

820
  while ((pNode = tdListNext(&iter)) != NULL && code == TSDB_CODE_SUCCESS) {
×
821
    SRowBuffPos* pPos = *(SRowBuffPos**)pNode->data;
×
822
    if (pPos->beFlushed || !pPos->pRowBuff) {
×
823
      continue;
×
824
    }
825
    pPos->beFlushed = true;
×
826
    pFileState->flushMark = TMAX(pFileState->flushMark, pFileState->getTs(pPos->pKey));
×
827

828
    qDebug("===stream===flushed start:%" PRId64, pFileState->getTs(pPos->pKey));
×
829
    if (streamStateGetBatchSize(batch) >= BATCH_LIMIT) {
×
830
      code = streamStatePutBatch_rocksdb(pFileState->pFileStore, batch);
×
831
      streamStateClearBatch(batch);
×
832
      QUERY_CHECK_CODE(code, lino, _end);
×
833
    }
834

835
    void* pSKey = pFileState->stateBuffCreateStateKeyFn(pPos, ((SStreamState*)pFileState->pFileStore)->number);
×
836
    QUERY_CHECK_NULL(pSKey, code, lino, _end, terrno);
×
837

838
    code = streamStatePutBatchOptimize(pFileState->pFileStore, idx, batch, pSKey, pPos->pRowBuff, pFileState->rowSize,
×
839
                                       0, buf);
840
    taosMemoryFreeClear(pSKey);
×
841
    QUERY_CHECK_CODE(code, lino, _end);
×
842
    // todo handle failure
843
    memset(buf, 0, len);
×
844
  }
845
  taosMemoryFreeClear(buf);
×
846

847
  int32_t numOfElems = streamStateGetBatchSize(batch);
×
848
  if (numOfElems > 0) {
×
849
    code = streamStatePutBatch_rocksdb(pFileState->pFileStore, batch);
×
850
    QUERY_CHECK_CODE(code, lino, _end);
×
851
  } else {
852
    goto _end;
×
853
  }
854

855
  streamStateClearBatch(batch);
×
856

857
  clearSearchBuff(pFileState);
×
858

859
  int64_t elapsed = taosGetTimestampMs() - st;
×
860
  qDebug("%s flush to disk in batch model completed, rows:%d, batch size:%d, elapsed time:%" PRId64 "ms",
×
861
         pFileState->id, numOfElems, BATCH_LIMIT, elapsed);
862

863
  if (flushState) {
×
864
    void*   valBuf = NULL;
×
865
    int32_t len = 0;
×
866
    code = streamFileStateEncode(&pFileState->flushMark, &valBuf, &len);
×
867
    QUERY_CHECK_CODE(code, lino, _end);
×
868

869
    qDebug("===stream===flushMark write:%" PRId64, pFileState->flushMark);
×
870
    code = streamStatePutBatch(pFileState->pFileStore, "default", batch, STREAM_STATE_INFO_NAME, valBuf, len, 0);
×
871
    taosMemoryFree(valBuf);
×
872
    QUERY_CHECK_CODE(code, lino, _end);
×
873

874
    code = streamStatePutBatch_rocksdb(pFileState->pFileStore, batch);
×
875
    QUERY_CHECK_CODE(code, lino, _end);
×
876
  }
877

878
_end:
×
879
  if (code != TSDB_CODE_SUCCESS) {
×
880
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
881
  }
882
  taosMemoryFree(buf);
×
883
  streamStateDestroyBatch(batch);
×
884
}
×
885

886
int32_t forceRemoveCheckpoint(SStreamFileState* pFileState, int64_t checkpointId) {
×
887
  char keyBuf[128] = {0};
×
888
  TAOS_UNUSED(tsnprintf(keyBuf, sizeof(keyBuf), "%s:%" PRId64 "", TASK_KEY, checkpointId));
×
889
  return streamDefaultDel_rocksdb(pFileState->pFileStore, keyBuf);
×
890
}
891

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

922
    TSKEY ts;
923
    ts = taosStr2Int64((char*)buf, NULL, 10);
×
924
    if (ts < mark) {
×
925
      // statekey winkey.ts < mark
926
      int32_t tmpRes = forceRemoveCheckpoint(pFileState, i);
×
927
      qTrace("%s at line %d res:%d", __func__, __LINE__, tmpRes);
×
928
      break;
×
929
    }
930
  }
931
  return code;
×
932
}
933

934
int32_t recoverSession(SStreamFileState* pFileState, int64_t ckId) {
×
935
  int32_t code = TSDB_CODE_SUCCESS;
×
936
  int32_t lino = 0;
×
937
  int32_t winRes = TSDB_CODE_SUCCESS;
×
938
  if (pFileState->maxTs != INT64_MIN) {
×
939
    int64_t mark = (INT64_MIN + pFileState->deleteMark >= pFileState->maxTs)
×
940
                       ? INT64_MIN
941
                       : pFileState->maxTs - pFileState->deleteMark;
×
942
    int32_t tmpRes = deleteExpiredCheckPoint(pFileState, mark);
×
943
    qTrace("%s at line %d res:%d", __func__, __LINE__, tmpRes);
×
944
  }
945

946
  SStreamStateCur* pCur = streamStateSessionSeekToLast_rocksdb(pFileState->pFileStore, INT64_MAX);
×
947
  int32_t          recoverNum = TMIN(MIN_NUM_OF_RECOVER_ROW_BUFF, pFileState->maxRowCount);
×
948
  while (winRes == TSDB_CODE_SUCCESS) {
×
949
    if (pFileState->curRowCount >= recoverNum) {
×
950
      break;
×
951
    }
952

953
    void*       pVal = NULL;
×
954
    int32_t     vlen = 0;
×
955
    SSessionKey key = {0};
×
956
    winRes = streamStateSessionGetKVByCur_rocksdb(getStateFileStore(pFileState), pCur, &key, &pVal, &vlen);
×
957
    if (winRes != TSDB_CODE_SUCCESS) {
×
958
      break;
×
959
    }
960

961
    if (vlen != pFileState->rowSize) {
×
962
      code = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
963
      QUERY_CHECK_CODE(code, lino, _end);
×
964
    }
965

966
    SRowBuffPos* pPos = createSessionWinBuff(pFileState, &key, pVal, &vlen);
×
967
    pPos->beUsed = false;
×
968
    winRes = putSessionWinResultBuff(pFileState, pPos);
×
969
    if (winRes != TSDB_CODE_SUCCESS) {
×
970
      break;
×
971
    }
972

973
    winRes = streamStateSessionCurPrev_rocksdb(pCur);
×
974
  }
975

976
_end:
×
977
  if (code != TSDB_CODE_SUCCESS) {
×
978
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
979
  }
980
  streamStateFreeCur(pCur);
×
981
  return code;
×
982
}
983

984
int32_t recoverSnapshot(SStreamFileState* pFileState, int64_t ckId) {
×
985
  int32_t code = TSDB_CODE_SUCCESS;
×
986
  int32_t lino = 0;
×
987
  int32_t winCode = TSDB_CODE_SUCCESS;
×
988
  if (pFileState->maxTs != INT64_MIN) {
×
989
    int64_t mark = (INT64_MIN + pFileState->deleteMark >= pFileState->maxTs)
×
990
                       ? INT64_MIN
991
                       : pFileState->maxTs - pFileState->deleteMark;
×
992
    int32_t tmpRes = deleteExpiredCheckPoint(pFileState, mark);
×
993
    qTrace("%s at line %d res:%d", __func__, __LINE__, tmpRes);
×
994
  }
995

996
  SStreamStateCur* pCur = streamStateSeekToLast_rocksdb(pFileState->pFileStore);
×
997
  int32_t          recoverNum = TMIN(MIN_NUM_OF_RECOVER_ROW_BUFF, pFileState->maxRowCount);
×
998
  while (winCode == TSDB_CODE_SUCCESS) {
×
999
    if (pFileState->curRowCount >= recoverNum) {
×
1000
      break;
×
1001
    }
1002

1003
    void*        pVal = NULL;
×
1004
    int32_t      vlen = 0;
×
1005
    SRowBuffPos* pNewPos = getNewRowPosForWrite(pFileState);
×
1006
    if (!pNewPos || !pNewPos->pRowBuff) {
×
1007
      code = TSDB_CODE_OUT_OF_MEMORY;
×
1008
      QUERY_CHECK_CODE(code, lino, _end);
×
1009
    }
1010

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

1039
_end:
×
1040
  if (code != TSDB_CODE_SUCCESS) {
×
1041
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1042
  }
1043
  streamStateFreeCur(pCur);
×
1044
  return code;
×
1045
}
1046

1047
int32_t streamFileStateGetSelectRowSize(SStreamFileState* pFileState) { return pFileState->selectivityRowSize; }
×
1048

1049
void streamFileStateReloadInfo(SStreamFileState* pFileState, TSKEY ts) {
×
1050
  pFileState->flushMark = TMAX(pFileState->flushMark, ts);
×
1051
  pFileState->maxTs = TMAX(pFileState->maxTs, ts);
×
1052
}
×
1053

1054
void* getRowStateBuff(SStreamFileState* pFileState) { return pFileState->rowStateBuff; }
×
1055
void* getSearchBuff(SStreamFileState* pFileState) { return pFileState->searchBuff; }
×
1056

1057
void* getStateFileStore(SStreamFileState* pFileState) { return pFileState->pFileStore; }
×
1058

1059
bool isDeteled(SStreamFileState* pFileState, TSKEY ts) {
×
1060
  return pFileState->deleteMark != INT64_MAX && pFileState->maxTs > 0 &&
×
1061
         ts < (pFileState->maxTs - pFileState->deleteMark);
×
1062
}
1063

1064
bool isFlushedState(SStreamFileState* pFileState, TSKEY ts, TSKEY gap) { return ts <= (pFileState->flushMark + gap); }
×
1065

1066
TSKEY getFlushMark(SStreamFileState* pFileState) { return pFileState->flushMark; };
×
1067

1068
int32_t getRowStateRowSize(SStreamFileState* pFileState) { return pFileState->rowSize; }
×
1069

1070
int32_t getFunctionRowBuff(SStreamFileState* pFileState, void* pKey, int32_t keyLen, void** pVal, int32_t* pVLen) {
×
1071
  int32_t winCode = TSDB_CODE_SUCCESS;
×
1072
  return pFileState->stateFunctionGetFn(pFileState, pKey, keyLen, pVal, pVLen, &winCode);
×
1073
}
1074

1075
int32_t recoverFillSnapshot(SStreamFileState* pFileState, int64_t ckId) {
×
1076
  int32_t code = TSDB_CODE_SUCCESS;
×
1077
  int32_t lino = 0;
×
1078
  if (pFileState->maxTs != INT64_MIN) {
×
1079
    int64_t mark = (INT64_MIN + pFileState->deleteMark >= pFileState->maxTs)
×
1080
                       ? INT64_MIN
1081
                       : pFileState->maxTs - pFileState->deleteMark;
×
1082
    code = deleteExpiredCheckPoint(pFileState, mark);
×
1083
    QUERY_CHECK_CODE(code, lino, _end);
×
1084
  }
1085

1086
  SStreamStateCur* pCur = streamStateFillSeekToLast_rocksdb(pFileState->pFileStore);
×
1087
  if (pCur == NULL) {
×
1088
    return code;
×
1089
  }
1090
  int32_t recoverNum = TMIN(MIN_NUM_OF_RECOVER_ROW_BUFF, pFileState->maxRowCount);
×
1091
  int32_t winRes = TSDB_CODE_SUCCESS;
×
1092
  while (winRes == TSDB_CODE_SUCCESS) {
×
1093
    if (pFileState->curRowCount >= recoverNum) {
×
1094
      break;
×
1095
    }
1096

1097
    void*        pVal = NULL;
×
1098
    int32_t      vlen = 0;
×
1099
    SRowBuffPos* pNewPos = getNewRowPosForWrite(pFileState);
×
1100
    winRes = streamStateFillGetKVByCur_rocksdb(pCur, pNewPos->pKey, (const void**)&pVal, &vlen);
×
1101
    qDebug("===stream=== get state by cur winres:%d. %s", winRes, __func__);
×
1102
    if (winRes != TSDB_CODE_SUCCESS || isFlushedState(pFileState, pFileState->getTs(pNewPos->pKey), 0)) {
×
1103
      destroyRowBuffPos(pNewPos);
×
1104
      SListNode* pNode = tdListPopTail(pFileState->usedBuffs);
×
1105
      taosMemoryFreeClear(pNode);
×
1106
      taosMemoryFreeClear(pVal);
×
1107
      break;
×
1108
    }
1109

1110
    if (vlen != pFileState->rowSize) {
×
1111
      qError("row size mismatch, expect:%d, actual:%d", pFileState->rowSize, vlen);
×
1112
      destroyRowBuffPos(pNewPos);
×
1113
      code = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
1114
      taosMemoryFreeClear(pVal);
×
1115
      QUERY_CHECK_CODE(code, lino, _end);
×
1116
    }
1117

1118
    memcpy(pNewPos->pRowBuff, pVal, vlen);
×
1119
    taosMemoryFreeClear(pVal);
×
1120
    pNewPos->beFlushed = true;
×
1121
    pNewPos->beUsed = false;
×
1122
    qDebug("===stream=== read checkpoint state from disc. %s", __func__);
×
1123
    winRes = tSimpleHashPut(pFileState->rowStateBuff, pNewPos->pKey, pFileState->keyLen, &pNewPos, POINTER_BYTES);
×
1124
    if (winRes != TSDB_CODE_SUCCESS) {
×
1125
      destroyRowBuffPos(pNewPos);
×
1126
      break;
×
1127
    }
1128
    streamStateCurPrev_rocksdb(pCur);
×
1129
  }
1130
  streamStateFreeCur(pCur);
×
1131

1132
_end:
×
1133
  if (code != TSDB_CODE_SUCCESS) {
×
1134
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1135
  }
1136
  return code;
×
1137
}
1138

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

1171
      memcpy(pNewPos->pKey, pKey, keyLen);
×
1172
      memcpy(pNewPos->pRowBuff, p, len);
×
1173
      code = tSimpleHashPut(pFileState->rowStateBuff, pKey, keyLen, &pNewPos, POINTER_BYTES);
×
1174
      QUERY_CHECK_CODE(code, lino, _end);
×
1175

1176
      if (pVal) {
×
1177
        *pVLen = pFileState->rowSize;
×
1178
        *pVal = pNewPos;
×
1179
      }
1180
    }
1181
    taosMemoryFree(p);
×
1182
  }
1183

1184
_end:
×
1185
  if (code != TSDB_CODE_SUCCESS) {
×
1186
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1187
  }
1188
  return code;
×
1189
}
1190

1191
int32_t streamFileStateGroupPut(SStreamFileState* pFileState, int64_t groupId, void* value, int32_t vLen) {
×
1192
  int32_t code = TSDB_CODE_SUCCESS;
×
1193
  int32_t lino = 0;
×
1194
  if (value != NULL) {
×
1195
    code = TSDB_CODE_INVALID_PARA;
×
1196
    QUERY_CHECK_CODE(code, lino, _end);
×
1197
  }
1198

1199
  if (tSimpleHashGet(pFileState->pGroupIdMap, &groupId, sizeof(int64_t)) == NULL) {
×
1200
    if (tSimpleHashGetSize(pFileState->pGroupIdMap) <= MAX_GROUP_ID_NUM) {
×
1201
      code = tSimpleHashPut(pFileState->pGroupIdMap, &groupId, sizeof(int64_t), NULL, 0);
×
1202
      QUERY_CHECK_CODE(code, lino, _end);
×
1203
    }
1204
    code = streamStatePutParTag_rocksdb(pFileState->pFileStore, groupId, value, vLen);
×
1205
    QUERY_CHECK_CODE(code, lino, _end);
×
1206
  }
1207

1208
_end:
×
1209
  if (code != TSDB_CODE_SUCCESS) {
×
1210
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1211
  }
1212
  return code;
×
1213
}
1214

1215
void streamFileStateGroupCurNext(SStreamStateCur* pCur) {
×
1216
  SStreamFileState* pFileState = (SStreamFileState*)pCur->pStreamFileState;
×
1217
  if (pCur->hashIter == -1) {
×
1218
    streamStateCurNext(pFileState->pFileStore, pCur);
×
1219
    return;
×
1220
  }
1221

1222
  int64_t gpId = *(int64_t*)tSimpleHashGetKey(pCur->pHashData, NULL);
×
1223
  pCur->minGpId = TMAX(pCur->minGpId, gpId);
×
1224

1225
  SSHashObj* pHash = pFileState->pGroupIdMap;
×
1226
  pCur->pHashData = tSimpleHashIterate(pHash, pCur->pHashData, &pCur->hashIter);
×
1227
  if (!pCur->pHashData) {
×
1228
    pCur->hashIter = -1;
×
1229
    streamStateParTagSeekKeyNext_rocksdb(pFileState->pFileStore, pCur->minGpId, pCur);
×
1230
    return;
×
1231
  }
1232
}
1233

1234
int32_t streamFileStateGroupGetKVByCur(SStreamStateCur* pCur, int64_t* pKey, void** pVal, int32_t* pVLen) {
×
1235
  int32_t code = TSDB_CODE_SUCCESS;
×
1236
  if (pCur->pHashData) {
×
1237
    *pKey = *(int64_t*)tSimpleHashGetKey(pCur->pHashData, NULL);
×
1238
    return code;
×
1239
  }
1240
  return streamStateParTagGetKVByCur_rocksdb(pCur, pKey, NULL, NULL);
×
1241
}
1242

1243
SSHashObj* getGroupIdCache(SStreamFileState* pFileState) {
×
1244
  return pFileState->pGroupIdMap;
×
1245
}
1246

1247
void clearExpiredState(SStreamFileState* pFileState) {
×
1248
  int32_t    code = TSDB_CODE_SUCCESS;
×
1249
  int32_t    lino = 0;
×
1250
  SSHashObj* pSearchBuff = pFileState->searchBuff;
×
1251
  void*      pIte = NULL;
×
1252
  int32_t    iter = 0;
×
1253
  while ((pIte = tSimpleHashIterate(pSearchBuff, pIte, &iter)) != NULL) {
×
1254
    SArray* pWinStates = *((void**)pIte);
×
1255
    int32_t size = taosArrayGetSize(pWinStates);
×
1256
    for (int32_t i = 0; i < size - 1; i++) {
×
1257
      SWinKey* pKey = taosArrayGet(pWinStates, i);
×
1258
      int32_t  code_buff = pFileState->stateBuffRemoveFn(pFileState->rowStateBuff, pKey, sizeof(SWinKey));
×
1259
      qTrace("clear expired buff, ts:%" PRId64 ". %s at line %d res:%d", pKey->ts, __func__, __LINE__, code_buff);
×
1260

1261
      if (isFlushedState(pFileState, pKey->ts, 0)) {
×
1262
        int32_t code_file = pFileState->stateFileRemoveFn(pFileState, pKey);
×
1263
        qTrace("clear expired file, ts:%" PRId64 ". %s at line %d res:%d", pKey->ts, __func__, __LINE__, code_file);
×
1264
      }
1265
    }
1266
    taosArrayRemoveBatch(pWinStates, 0, size - 1, NULL);
×
1267
  }
1268
  code = clearRowBuff(pFileState);
×
1269
  QUERY_CHECK_CODE(code, lino, _end);
×
1270

1271
_end:
×
1272
  if (code != TSDB_CODE_SUCCESS) {
×
1273
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1274
  }
1275
}
×
1276

1277
#ifdef BUILD_NO_CALL
1278
int32_t getStateSearchRowBuff(SStreamFileState* pFileState, const SWinKey* pKey, void** pVal, int32_t* pVLen,
1279
                           int32_t* pWinCode) {
1280
  int32_t code = TSDB_CODE_SUCCESS;
1281
  int32_t lino = 0;
1282

1283
  code = addRowBuffIfNotExist(pFileState, (void*)pKey, sizeof(SWinKey), pVal, pVLen, pWinCode);
1284
  QUERY_CHECK_CODE(code, lino, _end);
1285

1286
  SArray*    pWinStates = NULL;
1287
  SSHashObj* pSearchBuff = getSearchBuff(pFileState);
1288
  void**     ppBuff = tSimpleHashGet(pSearchBuff, &pKey->groupId, sizeof(uint64_t));
1289
  if (ppBuff) {
1290
    pWinStates = (SArray*)(*ppBuff);
1291
  } else {
1292
    pWinStates = taosArrayInit(16, sizeof(SWinKey));
1293
    QUERY_CHECK_NULL(pWinStates, code, lino, _end, terrno);
1294

1295
    code = tSimpleHashPut(pSearchBuff, &pKey->groupId, sizeof(uint64_t), &pWinStates, POINTER_BYTES);
1296
    QUERY_CHECK_CODE(code, lino, _end);
1297
  }
1298

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

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

1334
  if (size >= MAX_NUM_OF_CACHE_WIN) {
1335
    int32_t num = size - NUM_OF_CACHE_WIN;
1336
    taosArrayRemoveBatch(pWinStates, 0, num, NULL);
1337
  }
1338

1339
_end:
1340
  if (code != TSDB_CODE_SUCCESS) {
1341
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
1342
  }
1343
  return code;
1344
}
1345
#endif
1346

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

1412
_end:
×
1413
  if (code != TSDB_CODE_SUCCESS) {
×
1414
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1415
  }
1416
  return code;
×
1417
}
1418

1419
int32_t addSearchItem(SStreamFileState* pFileState, SArray* pWinStates, const SWinKey* pKey) {
×
1420
  int32_t code = TSDB_CODE_SUCCESS;
×
1421
  int32_t lino = 0;
×
1422
  int32_t size = taosArrayGetSize(pWinStates);
×
1423
  int32_t index = binarySearch(pWinStates, size, pKey, fillStateKeyCompare);
×
1424
  if (!isFlushedState(pFileState, pKey->ts, 0) || index >= 0 || size == 0) {
×
1425
    if (index >= 0) {
×
1426
      SWinKey* pTmpKey = taosArrayGet(pWinStates, index);
×
1427
      if (winKeyCmprImpl(pTmpKey, pKey) == 0) {
×
1428
        goto _end;
×
1429
      }
1430
    }
1431
    index++;
×
1432
    void* tmp = taosArrayInsert(pWinStates, index, pKey);
×
1433
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1434
  }
1435

1436
  if (size >= MAX_NUM_OF_CACHE_WIN) {
×
1437
    int32_t num = size - NUM_OF_CACHE_WIN;
×
1438
    taosArrayRemoveBatch(pWinStates, 0, num, NULL);
×
1439
  }
1440
_end:
×
1441
  if (code != TSDB_CODE_SUCCESS) {
×
1442
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1443
  }
1444
  return code;
×
1445
}
1446

1447
int32_t addArrayBuffIfNotExist(SSHashObj* pSearchBuff, uint64_t groupId, SArray** ppResStates) {
×
1448
  int32_t code = TSDB_CODE_SUCCESS;
×
1449
  int32_t lino = 0; 
×
1450
  SArray*    pWinStates = NULL;
×
1451
  void**     ppBuff = tSimpleHashGet(pSearchBuff, &groupId, sizeof(uint64_t));
×
1452
  if (ppBuff) {
×
1453
    pWinStates = (SArray*)(*ppBuff);
×
1454
  } else {
1455
    pWinStates = taosArrayInit(16, sizeof(SWinKey));
×
1456
    QUERY_CHECK_NULL(pWinStates, code, lino, _end, terrno);
×
1457

1458
    code = tSimpleHashPut(pSearchBuff, &groupId, sizeof(uint64_t), &pWinStates, POINTER_BYTES);
×
1459
    QUERY_CHECK_CODE(code, lino, _end);
×
1460
  }
1461

1462
  (*ppResStates) = pWinStates;
×
1463

1464
_end:
×
1465
  if (code != TSDB_CODE_SUCCESS) {
×
1466
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1467
  }
1468
  return code;
×
1469
}
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