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

taosdata / TDengine / #3798

31 Mar 2025 10:39AM UTC coverage: 9.424% (-20.9%) from 30.372%
#3798

push

travis-ci

happyguoxy
test:add test cases

21549 of 307601 branches covered (7.01%)

Branch coverage included in aggregate %.

36084 of 303967 relevant lines covered (11.87%)

58620.7 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 "tcompare.h"
23
#include "thash.h"
24
#include "tsimplehash.h"
25

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

39
#define DEFAULT_STATE_MAP_CAPACITY 10240
40
#define MAX_STATE_MAP_SIZE         10240000
41

42
#define SET_TSDATA_FLAG(ptr, len)   ((*(char*)POINTER_SHIFT(ptr, (len - 1))) |= 1)
43
#define UNSET_TSDATA_FLAG(ptr, len) ((*(char*)POINTER_SHIFT(ptr, (len - 1))) &= 0)
44
#define HAS_TSDATA_FLAG(ptr, len)   ((*(char*)POINTER_SHIFT(ptr, (len - 1))) & 1)
45

46
#define TASK_KEY               "streamFileState"
47
#define STREAM_STATE_INFO_NAME "StreamStateCheckPoint"
48

49
struct SStreamFileState {
50
  SList*     usedBuffs;
51
  SList*     freeBuffs;
52
  void*      rowStateBuff;
53
  void*      pFileStore;
54
  int32_t    rowSize;
55
  int32_t    selectivityRowSize;
56
  int32_t    keyLen;
57
  uint64_t   preCheckPointVersion;
58
  uint64_t   checkPointVersion;
59
  TSKEY      maxTs;
60
  TSKEY      deleteMark;
61
  TSKEY      flushMark;
62
  uint64_t   maxRowCount;
63
  uint64_t   curRowCount;
64
  GetTsFun   getTs;
65
  char*      id;
66
  char*      cfName;
67
  void*      searchBuff;
68
  SSHashObj* pGroupIdMap;
69
  bool       hasFillCatch;
70
  SSHashObj* pRecFlagMap;
71

72
  _state_buff_cleanup_fn         stateBuffCleanupFn;
73
  _state_buff_remove_fn          stateBuffRemoveFn;
74
  _state_buff_remove_by_pos_fn   stateBuffRemoveByPosFn;
75
  _state_buff_create_statekey_fn stateBuffCreateStateKeyFn;
76

77
  _state_file_remove_fn stateFileRemoveFn;
78
  _state_file_get_fn    stateFileGetFn;
79

80
  _state_fun_get_fn stateFunctionGetFn;
81
};
82

83
typedef SRowBuffPos SRowBuffInfo;
84

85
int fillStateKeyCompare(const void* pWin1, const void* pDatas, int pos) {
×
86
  SWinKey* pWin2 = taosArrayGet(pDatas, pos);
×
87
  return winKeyCmprImpl((SWinKey*)pWin1, pWin2);
×
88
}
89

90
int fillTSKeyCompare(const void* pKey1, const void* pDatas, int pos) {
×
91
  SWinKey* pWin1 = (SWinKey*)pKey1;
×
92
  SWinKey* pWin2 = taosArrayGet(pDatas, pos);
×
93
  if (pWin1->ts > pWin2->ts) {
×
94
    return 1;
×
95
  } else if (pWin1->ts < pWin2->ts) {
×
96
    return -1;
×
97
  }
98

99
  return 0;
×
100
}
101

102
int32_t stateHashBuffRemoveFn(void* pBuff, const void* pKey, size_t keyLen) {
×
103
  SRowBuffPos** pos = tSimpleHashGet(pBuff, pKey, keyLen);
×
104
  if (pos) {
×
105
    (*pos)->beFlushed = true;
×
106
    (*pos)->invalid = true;
×
107
  }
108
  return tSimpleHashRemove(pBuff, pKey, keyLen);
×
109
}
110

111
void stateHashBuffRemoveByPosFn(SStreamFileState* pFileState, SRowBuffPos* pPos) {
×
112
  size_t        keyLen = pFileState->keyLen;
×
113
  SRowBuffPos** ppPos = tSimpleHashGet(pFileState->rowStateBuff, pPos->pKey, keyLen);
×
114
  if (ppPos) {
×
115
    if ((*ppPos) == pPos) {
×
116
      int32_t tmpRes = tSimpleHashRemove(pFileState->rowStateBuff, pPos->pKey, keyLen);
×
117
      qTrace("%s at line %d res:%d", __func__, __LINE__, tmpRes);
×
118
    }
119
  }
120
}
×
121

122
void stateHashBuffClearFn(void* pBuff) { tSimpleHashClear(pBuff); }
×
123

124
void stateHashBuffCleanupFn(void* pBuff) { tSimpleHashCleanup(pBuff); }
×
125

126
int32_t intervalFileRemoveFn(SStreamFileState* pFileState, const void* pKey) {
×
127
  return streamStateDel_rocksdb(pFileState->pFileStore, pKey);
×
128
}
129

130
int32_t intervalFileGetFn(SStreamFileState* pFileState, void* pKey, void** data, int32_t* pDataLen) {
×
131
  return streamStateGet_rocksdb(pFileState->pFileStore, pKey, data, pDataLen);
×
132
}
133

134
void* intervalCreateStateKey(SRowBuffPos* pPos, int64_t num) {
×
135
  SStateKey* pStateKey = taosMemoryCalloc(1, sizeof(SStateKey));
×
136
  if (pStateKey == NULL) {
×
137
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
138
    return NULL;
×
139
  }
140
  SWinKey* pWinKey = pPos->pKey;
×
141
  pStateKey->key = *pWinKey;
×
142
  pStateKey->opNum = num;
×
143
  return pStateKey;
×
144
}
145

146
void* defaultCreateStateKey(SRowBuffPos* pPos, int64_t num) {
×
147
  SWinKey* pStateKey = taosMemoryCalloc(1, sizeof(SWinKey));
×
148
  if (pStateKey == NULL) {
×
149
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
150
    return NULL;
×
151
  }
152
  SWinKey* pWinKey = pPos->pKey;
×
153
  *pStateKey = *pWinKey;
×
154
  return pStateKey;
×
155
}
156

157
int32_t sessionFileRemoveFn(SStreamFileState* pFileState, const void* pKey) {
×
158
  return streamStateSessionDel_rocksdb(pFileState->pFileStore, pKey);
×
159
}
160

161
int32_t sessionFileGetFn(SStreamFileState* pFileState, void* pKey, void** data, int32_t* pDataLen) {
×
162
  return streamStateSessionGet_rocksdb(pFileState->pFileStore, pKey, data, pDataLen);
×
163
}
164

165
void* sessionCreateStateKey(SRowBuffPos* pPos, int64_t num) {
×
166
  SStateSessionKey* pStateKey = taosMemoryCalloc(1, sizeof(SStateSessionKey));
×
167
  if (pStateKey == NULL) {
×
168
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
169
    return NULL;
×
170
  }
171
  SSessionKey* pWinKey = pPos->pKey;
×
172
  pStateKey->key = *pWinKey;
×
173
  pStateKey->opNum = num;
×
174
  return pStateKey;
×
175
}
176

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

179
static int32_t streamFileStateEncode(TSKEY* pKey, void** pVal, int32_t* pLen) {
×
180
  *pLen = sizeof(TSKEY);
×
181
  (*pVal) = taosMemoryCalloc(1, *pLen);
×
182
  if ((*pVal) == NULL) {
×
183
    qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno));
×
184
    return terrno;
×
185
  }
186
  void*   buff = *pVal;
×
187
  int32_t tmp = taosEncodeFixedI64(&buff, *pKey);
×
188
  return TSDB_CODE_SUCCESS;
×
189
}
190

191
int32_t streamFileStateInit(int64_t memSize, uint32_t keySize, uint32_t rowSize, uint32_t selectRowSize, GetTsFun fp,
×
192
                            void* pFile, TSKEY delMark, const char* taskId, int64_t checkpointId, int8_t type,
193
                            SStreamFileState** ppFileState) {
194
  int32_t code = TSDB_CODE_SUCCESS;
×
195
  int32_t lino = 0;
×
196
  if (memSize <= 0) {
×
197
    memSize = DEFAULT_MAX_STREAM_BUFFER_SIZE;
×
198
  }
199
  if (rowSize == 0) {
×
200
    code = TSDB_CODE_INVALID_PARA;
×
201
    QUERY_CHECK_CODE(code, lino, _end);
×
202
  }
203

204
  SStreamFileState* pFileState = taosMemoryCalloc(1, sizeof(SStreamFileState));
×
205
  QUERY_CHECK_NULL(pFileState, code, lino, _end, terrno);
×
206

207
  rowSize += selectRowSize;
×
208
  pFileState->maxRowCount = TMAX((uint64_t)memSize / rowSize, FLUSH_NUM * 2);
×
209
  pFileState->usedBuffs = tdListNew(POINTER_BYTES);
×
210
  QUERY_CHECK_NULL(pFileState->usedBuffs, code, lino, _end, terrno);
×
211

212
  pFileState->freeBuffs = tdListNew(POINTER_BYTES);
×
213
  QUERY_CHECK_NULL(pFileState->freeBuffs, code, lino, _end, terrno);
×
214

215
  _hash_fn_t hashFn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY);
×
216
  int32_t    cap = TMIN(MIN_NUM_OF_ROW_BUFF, pFileState->maxRowCount);
×
217
  if (type == STREAM_STATE_BUFF_HASH || type == STREAM_STATE_BUFF_HASH_SEARCH) {
×
218
    pFileState->rowStateBuff = tSimpleHashInit(cap, hashFn);
×
219
    pFileState->stateBuffCleanupFn = stateHashBuffCleanupFn;
×
220
    pFileState->stateBuffRemoveFn = stateHashBuffRemoveFn;
×
221
    pFileState->stateBuffRemoveByPosFn = stateHashBuffRemoveByPosFn;
×
222
    pFileState->stateBuffCreateStateKeyFn = intervalCreateStateKey;
×
223

224
    pFileState->stateFileRemoveFn = intervalFileRemoveFn;
×
225
    pFileState->stateFileGetFn = intervalFileGetFn;
×
226
    pFileState->cfName = taosStrdup("state");
×
227
    pFileState->stateFunctionGetFn = addRowBuffIfNotExist;
×
228
  } else if (type == STREAM_STATE_BUFF_SORT) {
×
229
    pFileState->rowStateBuff = tSimpleHashInit(cap, hashFn);
×
230
    pFileState->stateBuffCleanupFn = sessionWinStateCleanup;
×
231
    pFileState->stateBuffRemoveFn = deleteSessionWinStateBuffFn;
×
232
    pFileState->stateBuffRemoveByPosFn = deleteSessionWinStateBuffByPosFn;
×
233
    pFileState->stateBuffCreateStateKeyFn = sessionCreateStateKey;
×
234

235
    pFileState->stateFileRemoveFn = sessionFileRemoveFn;
×
236
    pFileState->stateFileGetFn = sessionFileGetFn;
×
237
    pFileState->cfName = taosStrdup("sess");
×
238
    pFileState->stateFunctionGetFn = getSessionRowBuff;
×
239
  } else if (type == STREAM_STATE_BUFF_HASH_SORT) {
×
240
    pFileState->rowStateBuff = tSimpleHashInit(cap, hashFn);
×
241
    pFileState->searchBuff = tSimpleHashInit(MIN_NUM_SEARCH_BUCKET, hashFn);
×
242
    QUERY_CHECK_NULL(pFileState->searchBuff, code, lino, _end, terrno);
×
243
    pFileState->stateBuffCleanupFn = stateHashBuffCleanupFn;
×
244
    pFileState->stateBuffRemoveFn = stateHashBuffRemoveFn;
×
245
    pFileState->stateBuffRemoveByPosFn = stateHashBuffRemoveByPosFn;
×
246
    pFileState->stateBuffCreateStateKeyFn = defaultCreateStateKey;
×
247

248
    pFileState->stateFileRemoveFn = hashSortFileRemoveFn;
×
249
    pFileState->stateFileGetFn = hashSortFileGetFn;
×
250
    pFileState->cfName = taosStrdup("fill");
×
251
    pFileState->stateFunctionGetFn = NULL;
×
252
  }
253

254
  QUERY_CHECK_NULL(pFileState->usedBuffs, code, lino, _end, terrno);
×
255
  QUERY_CHECK_NULL(pFileState->freeBuffs, code, lino, _end, terrno);
×
256
  QUERY_CHECK_NULL(pFileState->rowStateBuff, code, lino, _end, terrno);
×
257
  QUERY_CHECK_NULL(pFileState->cfName, code, lino, _end, terrno);
×
258

259
  if (type == STREAM_STATE_BUFF_HASH_SEARCH) {
×
260
    pFileState->searchBuff = tSimpleHashInit(MIN_NUM_SEARCH_BUCKET, hashFn);
×
261
    QUERY_CHECK_NULL(pFileState->searchBuff, code, lino, _end, terrno);
×
262
  }
263

264
  pFileState->keyLen = keySize;
×
265
  pFileState->rowSize = rowSize;
×
266
  pFileState->selectivityRowSize = selectRowSize;
×
267
  pFileState->preCheckPointVersion = 0;
×
268
  pFileState->checkPointVersion = 1;
×
269
  pFileState->pFileStore = pFile;
×
270
  pFileState->getTs = fp;
×
271
  pFileState->curRowCount = 0;
×
272
  pFileState->deleteMark = delMark;
×
273
  pFileState->flushMark = INT64_MIN;
×
274
  pFileState->maxTs = INT64_MIN;
×
275
  pFileState->id = taosStrdup(taskId);
×
276
  QUERY_CHECK_NULL(pFileState->id, code, lino, _end, terrno);
×
277

278
  pFileState->pGroupIdMap = tSimpleHashInit(1024, hashFn);
×
279
  QUERY_CHECK_NULL(pFileState->pGroupIdMap, code, lino, _end, terrno);
×
280

281
  pFileState->pRecFlagMap = tSimpleHashInit(1024, hashFn);
×
282
  QUERY_CHECK_NULL(pFileState->pRecFlagMap, code, lino, _end, terrno);
×
283

284

285
  pFileState->hasFillCatch = true;
×
286

287
  if (type == STREAM_STATE_BUFF_HASH || type == STREAM_STATE_BUFF_HASH_SEARCH) {
×
288
    code = recoverSnapshot(pFileState, checkpointId);
×
289
  } else if (type == STREAM_STATE_BUFF_SORT) {
×
290
    code = recoverSession(pFileState, checkpointId);
×
291
  } else if (type == STREAM_STATE_BUFF_HASH_SORT) {
×
292
    code = recoverFillSnapshot(pFileState, checkpointId);
×
293
  }
294
  QUERY_CHECK_CODE(code, lino, _end);
×
295

296
  void*   valBuf = NULL;
×
297
  int32_t len = 0;
×
298
  int32_t tmpRes = streamDefaultGet_rocksdb(pFileState->pFileStore, STREAM_STATE_INFO_NAME, &valBuf, &len);
×
299
  if (tmpRes == TSDB_CODE_SUCCESS) {
×
300
    QUERY_CHECK_CONDITION((len == sizeof(TSKEY)), code, lino, _end, TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
×
301
    streamFileStateDecode(&pFileState->flushMark, valBuf, len);
×
302
    qDebug("===stream===flushMark  read:%" PRId64, pFileState->flushMark);
×
303
  }
304
  taosMemoryFreeClear(valBuf);
×
305
  (*ppFileState) = pFileState;
×
306

307
_end:
×
308
  if (code != TSDB_CODE_SUCCESS) {
×
309
    streamFileStateDestroy(pFileState);
×
310
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
311
  }
312
  return code;
×
313
}
314

315
void destroyRowBuffPos(SRowBuffPos* pPos) {
×
316
  taosMemoryFreeClear(pPos->pKey);
×
317
  taosMemoryFreeClear(pPos->pRowBuff);
×
318
  taosMemoryFree(pPos);
×
319
}
×
320

321
void destroyRowBuffPosPtr(void* ptr) {
×
322
  if (!ptr) {
×
323
    return;
×
324
  }
325
  SRowBuffPos* pPos = *(SRowBuffPos**)ptr;
×
326
  if (!pPos->beUsed) {
×
327
    destroyRowBuffPos(pPos);
×
328
  }
329
}
330

331
void destroyRowBuffAllPosPtr(void* ptr) {
×
332
  if (!ptr) {
×
333
    return;
×
334
  }
335
  SRowBuffPos* pPos = *(SRowBuffPos**)ptr;
×
336
  destroyRowBuffPos(pPos);
×
337
}
338

339
void destroyRowBuff(void* ptr) {
×
340
  if (!ptr) {
×
341
    return;
×
342
  }
343
  taosMemoryFree(*(void**)ptr);
×
344
}
345

346
void streamFileStateDestroy(SStreamFileState* pFileState) {
×
347
  if (!pFileState) {
×
348
    return;
×
349
  }
350

351
  taosMemoryFree(pFileState->id);
×
352
  taosMemoryFree(pFileState->cfName);
×
353
  tdListFreeP(pFileState->usedBuffs, destroyRowBuffAllPosPtr);
×
354
  tdListFreeP(pFileState->freeBuffs, destroyRowBuff);
×
355
  pFileState->stateBuffCleanupFn(pFileState->rowStateBuff);
×
356
  sessionWinStateCleanup(pFileState->searchBuff);
×
357
  tSimpleHashCleanup(pFileState->pGroupIdMap);
×
358
  tSimpleHashCleanup(pFileState->pRecFlagMap);
×
359
  taosMemoryFree(pFileState);
×
360
}
361

362
int32_t putFreeBuff(SStreamFileState* pFileState, SRowBuffPos* pPos) {
×
363
  int32_t code = TSDB_CODE_SUCCESS;
×
364
  int32_t lino = 0;
×
365
  if (pPos->pRowBuff) {
×
366
    code = tdListAppend(pFileState->freeBuffs, &(pPos->pRowBuff));
×
367
    QUERY_CHECK_CODE(code, lino, _end);
×
368
    pPos->pRowBuff = NULL;
×
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
  return code;
×
376
}
377

378
void clearExpiredRowBuff(SStreamFileState* pFileState, TSKEY ts, bool all) {
×
379
  int32_t   code = TSDB_CODE_SUCCESS;
×
380
  int32_t   lino = 0;
×
381
  SListIter iter = {0};
×
382
  tdListInitIter(pFileState->usedBuffs, &iter, TD_LIST_FORWARD);
×
383

384
  SListNode* pNode = NULL;
×
385
  while ((pNode = tdListNext(&iter)) != NULL) {
×
386
    SRowBuffPos* pPos = *(SRowBuffPos**)(pNode->data);
×
387
    if (all || (pFileState->getTs(pPos->pKey) < ts && !pPos->beUsed)) {
×
388
      code = putFreeBuff(pFileState, pPos);
×
389
      QUERY_CHECK_CODE(code, lino, _end);
×
390

391
      if (!all) {
×
392
        pFileState->stateBuffRemoveByPosFn(pFileState, pPos);
×
393
      }
394
      destroyRowBuffPos(pPos);
×
395
      SListNode* tmp = tdListPopNode(pFileState->usedBuffs, pNode);
×
396
      taosMemoryFreeClear(tmp);
×
397
    }
398
  }
399

400
_end:
×
401
  if (code != TSDB_CODE_SUCCESS) {
×
402
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
403
  }
404
}
×
405

406
int32_t clearFlushedRowBuff(SStreamFileState* pFileState, SStreamSnapshot* pFlushList, uint64_t max, bool all) {
×
407
  int32_t   code = TSDB_CODE_SUCCESS;
×
408
  int32_t   lino = 0;
×
409
  uint64_t  i = 0;
×
410
  SListIter iter = {0};
×
411
  tdListInitIter(pFileState->usedBuffs, &iter, TD_LIST_FORWARD);
×
412

413
  SListNode* pNode = NULL;
×
414
  while ((pNode = tdListNext(&iter)) != NULL && i < max) {
×
415
    SRowBuffPos* pPos = *(SRowBuffPos**)pNode->data;
×
416
    if (isFlushedState(pFileState, pFileState->getTs(pPos->pKey), 0)) {
×
417
      if (all || !pPos->beUsed) {
×
418
        if (all && !pPos->pRowBuff) {
×
419
          continue;
×
420
        }
421
        code = tdListAppend(pFlushList, &pPos);
×
422
        QUERY_CHECK_CODE(code, lino, _end);
×
423

424
        pFileState->flushMark = TMAX(pFileState->flushMark, pFileState->getTs(pPos->pKey));
×
425
        pFileState->stateBuffRemoveByPosFn(pFileState, pPos);
×
426
        if (pPos->beUsed == false) {
×
427
          SListNode* tmp = tdListPopNode(pFileState->usedBuffs, pNode);
×
428
          taosMemoryFreeClear(tmp);
×
429
        }
430
        if (pPos->pRowBuff) {
×
431
          i++;
×
432
        }
433
      }
434
    }
435
  }
436
  qDebug("clear flushed row buff. %d rows to disk. is all:%d", listNEles(pFlushList), all);
×
437

438
_end:
×
439
  if (code != TSDB_CODE_SUCCESS) {
×
440
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
441
  }
442
  return code;
×
443
}
444

445
void streamFileStateClear(SStreamFileState* pFileState) {
×
446
  pFileState->flushMark = INT64_MIN;
×
447
  pFileState->maxTs = INT64_MIN;
×
448
  tSimpleHashClear(pFileState->rowStateBuff);
×
449
  clearExpiredRowBuff(pFileState, 0, true);
×
450
}
×
451

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

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

456
int32_t popUsedBuffs(SStreamFileState* pFileState, SStreamSnapshot* pFlushList, uint64_t max, bool used) {
×
457
  int32_t   code = TSDB_CODE_SUCCESS;
×
458
  int32_t   lino = 0;
×
459
  uint64_t  i = 0;
×
460
  SListIter iter = {0};
×
461
  tdListInitIter(pFileState->usedBuffs, &iter, TD_LIST_FORWARD);
×
462

463
  SListNode* pNode = NULL;
×
464
  while ((pNode = tdListNext(&iter)) != NULL && i < max) {
×
465
    SRowBuffPos* pPos = *(SRowBuffPos**)pNode->data;
×
466
    if (pPos->beUsed == used) {
×
467
      if (used && !pPos->pRowBuff) {
×
468
        continue;
×
469
      }
470
      code = tdListAppend(pFlushList, &pPos);
×
471
      QUERY_CHECK_CODE(code, lino, _end);
×
472

473
      pFileState->flushMark = TMAX(pFileState->flushMark, pFileState->getTs(pPos->pKey));
×
474
      pFileState->stateBuffRemoveByPosFn(pFileState, pPos);
×
475
      if (pPos->beUsed == false) {
×
476
        SListNode* tmp = tdListPopNode(pFileState->usedBuffs, pNode);
×
477
        taosMemoryFreeClear(tmp);
×
478
      }
479
      if (pPos->pRowBuff) {
×
480
        i++;
×
481
      }
482
    }
483
  }
484

485
  qInfo("%s stream state flush %d rows to disk. is used:%d", pFileState->id, listNEles(pFlushList), used);
×
486

487
_end:
×
488
  if (code != TSDB_CODE_SUCCESS) {
×
489
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
490
  }
491
  return code;
×
492
}
493

494
int32_t flushRowBuff(SStreamFileState* pFileState) {
×
495
  int32_t          code = TSDB_CODE_SUCCESS;
×
496
  int32_t          lino = 0;
×
497
  SStreamSnapshot* pFlushList = tdListNew(POINTER_BYTES);
×
498
  if (!pFlushList) {
×
499
    code = TSDB_CODE_OUT_OF_MEMORY;
×
500
    QUERY_CHECK_CODE(code, lino, _end);
×
501
  }
502

503
  uint64_t num = (uint64_t)(pFileState->curRowCount * FLUSH_RATIO);
×
504
  num = TMAX(num, FLUSH_NUM);
×
505
  code = clearFlushedRowBuff(pFileState, pFlushList, num, false);
×
506
  QUERY_CHECK_CODE(code, lino, _end);
×
507

508
  if (isListEmpty(pFlushList)) {
×
509
    code = popUsedBuffs(pFileState, pFlushList, num, false);
×
510
    QUERY_CHECK_CODE(code, lino, _end);
×
511

512
    if (isListEmpty(pFlushList)) {
×
513
      code = popUsedBuffs(pFileState, pFlushList, num, true);
×
514
      QUERY_CHECK_CODE(code, lino, _end);
×
515
    }
516
  }
517

518
  if (pFileState->searchBuff) {
×
519
    code = clearFlushedRowBuff(pFileState, pFlushList, pFileState->curRowCount, true);
×
520
    QUERY_CHECK_CODE(code, lino, _end);
×
521
  }
522

523
  flushSnapshot(pFileState, pFlushList, false);
×
524

525
  SListIter fIter = {0};
×
526
  tdListInitIter(pFlushList, &fIter, TD_LIST_FORWARD);
×
527
  SListNode* pNode = NULL;
×
528
  while ((pNode = tdListNext(&fIter)) != NULL) {
×
529
    SRowBuffPos* pPos = *(SRowBuffPos**)pNode->data;
×
530
    code = putFreeBuff(pFileState, pPos);
×
531
    QUERY_CHECK_CODE(code, lino, _end);
×
532
  }
533

534
  tdListFreeP(pFlushList, destroyRowBuffPosPtr);
×
535

536
_end:
×
537
  if (code != TSDB_CODE_SUCCESS) {
×
538
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
539
  }
540
  return code;
×
541
}
542

543
int32_t clearRowBuff(SStreamFileState* pFileState) {
×
544
  if (pFileState->deleteMark != INT64_MAX) {
×
545
    clearExpiredRowBuff(pFileState, pFileState->maxTs - pFileState->deleteMark, false);
×
546
  }
547
  do {
548
    int32_t code = flushRowBuff(pFileState);
×
549
    if (code != TSDB_CODE_SUCCESS) {
×
550
      return code;
×
551
    }
552
  } while (isListEmpty(pFileState->freeBuffs) && pFileState->curRowCount == pFileState->maxRowCount);
×
553
  return TSDB_CODE_SUCCESS;
×
554
}
555

556
int32_t clearFlushedRowBuffByFlag(SStreamFileState* pFileState, uint64_t max) {
×
557
  int32_t   code = TSDB_CODE_SUCCESS;
×
558
  int32_t   lino = 0;
×
559
  uint64_t  i = 0;
×
560
  SListIter iter = {0};
×
561
  tdListInitIter(pFileState->usedBuffs, &iter, TD_LIST_FORWARD);
×
562

563
  SListNode* pNode = NULL;
×
564
  while ((pNode = tdListNext(&iter)) != NULL && i < max) {
×
565
    SRowBuffPos* pPos = *(SRowBuffPos**)pNode->data;
×
566
    if (pPos->invalid) {
×
567
      if (!pPos->beUsed) {
×
568
        SListNode* tmp = tdListPopNode(pFileState->usedBuffs, pNode);
×
569
        taosMemoryFreeClear(tmp);
×
570
        if (pPos->pRowBuff) {
×
571
          i++;
×
572
        }
573
        code = putFreeBuff(pFileState, pPos);
×
574
        QUERY_CHECK_CODE(code, lino, _end);
×
575
        destroyRowBuffPos(pPos);
×
576
      }
577
    }
578
  }
579

580
_end:
×
581
  if (code != TSDB_CODE_SUCCESS) {
×
582
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
583
  }
584
  return code;
×
585
}
586

587
int32_t clearRowBuffNonFlush(SStreamFileState* pFileState) {
×
588
  int32_t code = TSDB_CODE_SUCCESS;
×
589
  int32_t lino = 0;
×
590

591
  if (pFileState->deleteMark != INT64_MAX) {
×
592
    clearExpiredRowBuff(pFileState, pFileState->maxTs - pFileState->deleteMark, false);
×
593
  }
594

595
  uint64_t num = (uint64_t)(pFileState->curRowCount * FLUSH_RATIO);
×
596
  num = TMAX(num, FLUSH_NUM);
×
597
  code = clearFlushedRowBuffByFlag(pFileState, num);
×
598
  QUERY_CHECK_CODE(code, lino, _end);
×
599

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

607
void* getFreeBuff(SStreamFileState* pFileState) {
×
608
  SList*     lists = pFileState->freeBuffs;
×
609
  int32_t    buffSize = pFileState->rowSize;
×
610
  SListNode* pNode = tdListPopHead(lists);
×
611
  if (!pNode) {
×
612
    return NULL;
×
613
  }
614
  void* ptr = *(void**)pNode->data;
×
615
  memset(ptr, 0, buffSize);
×
616
  taosMemoryFree(pNode);
×
617
  return ptr;
×
618
}
619

620
void streamFileStateClearBuff(SStreamFileState* pFileState, SRowBuffPos* pPos) {
×
621
  if (pPos->pRowBuff) {
×
622
    memset(pPos->pRowBuff, 0, pFileState->rowSize);
×
623
  }
624
}
×
625

626
SRowBuffPos* getNewRowPos(SStreamFileState* pFileState) {
×
627
  int32_t      code = TSDB_CODE_SUCCESS;
×
628
  int32_t      lino = 0;
×
629
  SRowBuffPos* pPos = taosMemoryCalloc(1, sizeof(SRowBuffPos));
×
630
  if (!pPos) {
×
631
    code = terrno;
×
632
    QUERY_CHECK_CODE(code, lino, _error);
×
633
  }
634

635
  pPos->pKey = taosMemoryCalloc(1, pFileState->keyLen);
×
636
  if (!pPos->pKey) {
×
637
    code = terrno;
×
638
    QUERY_CHECK_CODE(code, lino, _error);
×
639
  }
640

641
  void* pBuff = getFreeBuff(pFileState);
×
642
  if (pBuff) {
×
643
    pPos->pRowBuff = pBuff;
×
644
    goto _end;
×
645
  }
646

647
  if (pFileState->curRowCount < pFileState->maxRowCount) {
×
648
    pBuff = taosMemoryCalloc(1, pFileState->rowSize);
×
649
    QUERY_CHECK_NULL(pBuff, code, lino, _error, terrno);
×
650
    pPos->pRowBuff = pBuff;
×
651
    pFileState->curRowCount++;
×
652
    goto _end;
×
653
  }
654

655
  code = clearRowBuff(pFileState);
×
656
  QUERY_CHECK_CODE(code, lino, _error);
×
657

658
  pPos->pRowBuff = getFreeBuff(pFileState);
×
659

660
_end:
×
661
  code = tdListAppend(pFileState->usedBuffs, &pPos);
×
662
  QUERY_CHECK_CODE(code, lino, _error);
×
663

664
  QUERY_CHECK_CONDITION((pPos->pRowBuff != NULL), code, lino, _error, TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
×
665
_error:
×
666
  if (code != TSDB_CODE_SUCCESS) {
×
667
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
668
    return NULL;
×
669
  }
670

671
  return pPos;
×
672
}
673

674
SRowBuffPos* getNewRowPosForWrite(SStreamFileState* pFileState) {
×
675
  int32_t      code = TSDB_CODE_SUCCESS;
×
676
  int32_t      lino = 0;
×
677
  SRowBuffPos* newPos = getNewRowPos(pFileState);
×
678
  if (!newPos) {
×
679
    code = TSDB_CODE_OUT_OF_MEMORY;
×
680
    QUERY_CHECK_CODE(code, lino, _error);
×
681
  }
682
  newPos->beUsed = true;
×
683
  newPos->beFlushed = false;
×
684
  newPos->needFree = false;
×
685
  newPos->beUpdated = true;
×
686
  newPos->invalid = false;
×
687
  return newPos;
×
688

689
_error:
×
690
  if (code != TSDB_CODE_SUCCESS) {
×
691
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
692
  }
693
  return NULL;
×
694
}
695

696
int32_t addRowBuffIfNotExist(SStreamFileState* pFileState, void* pKey, int32_t keyLen, void** pVal, int32_t* pVLen,
×
697
                             int32_t* pWinCode) {
698
  int32_t code = TSDB_CODE_SUCCESS;
×
699
  int32_t lino = 0;
×
700
  (*pWinCode) = TSDB_CODE_SUCCESS;
×
701
  pFileState->maxTs = TMAX(pFileState->maxTs, pFileState->getTs(pKey));
×
702
  SRowBuffPos** pos = tSimpleHashGet(pFileState->rowStateBuff, pKey, keyLen);
×
703
  if (pos) {
×
704
    if (pVal != NULL) {
×
705
      *pVLen = pFileState->rowSize;
×
706
      *pVal = *pos;
×
707
      (*pos)->beUsed = true;
×
708
      (*pos)->beFlushed = false;
×
709
    }
710
    goto _end;
×
711
  }
712
  SRowBuffPos* pNewPos = getNewRowPosForWrite(pFileState);
×
713
  if (!pNewPos || !pNewPos->pRowBuff) {
×
714
    code = TSDB_CODE_OUT_OF_MEMORY;
×
715
    QUERY_CHECK_CODE(code, lino, _end);
×
716
  }
717

718
  memcpy(pNewPos->pKey, pKey, keyLen);
×
719
  (*pWinCode) = TSDB_CODE_FAILED;
×
720

721
  TSKEY ts = pFileState->getTs(pKey);
×
722
  if (!isDeteled(pFileState, ts) && isFlushedState(pFileState, ts, 0)) {
×
723
    int32_t len = 0;
×
724
    void*   p = NULL;
×
725
    (*pWinCode) = pFileState->stateFileGetFn(pFileState, pKey, &p, &len);
×
726
    qDebug("===stream===get %" PRId64 " from disc, res %d", ts, (*pWinCode));
×
727
    if ((*pWinCode) == TSDB_CODE_SUCCESS) {
×
728
      memcpy(pNewPos->pRowBuff, p, len);
×
729
    }
730
    taosMemoryFree(p);
×
731
  }
732

733
  code = tSimpleHashPut(pFileState->rowStateBuff, pKey, keyLen, &pNewPos, POINTER_BYTES);
×
734
  QUERY_CHECK_CODE(code, lino, _end);
×
735

736
  if (pVal) {
×
737
    *pVLen = pFileState->rowSize;
×
738
    *pVal = pNewPos;
×
739
  }
740

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

748
int32_t createRowBuff(SStreamFileState* pFileState, void* pKey, int32_t keyLen, void** pVal, int32_t* pVLen) {
×
749
  int32_t code = TSDB_CODE_SUCCESS;
×
750
  int32_t lino = 0;
×
751

752
  pFileState->maxTs = TMAX(pFileState->maxTs, pFileState->getTs(pKey));
×
753

754
  SRowBuffPos* pNewPos = getNewRowPosForWrite(pFileState);
×
755
  if (!pNewPos || !pNewPos->pRowBuff) {
×
756
    code = TSDB_CODE_OUT_OF_MEMORY;
×
757
    QUERY_CHECK_CODE(code, lino, _end);
×
758
  }
759

760
  memcpy(pNewPos->pKey, pKey, keyLen);
×
761

762
  code = tSimpleHashPut(pFileState->rowStateBuff, pKey, keyLen, &pNewPos, POINTER_BYTES);
×
763
  QUERY_CHECK_CODE(code, lino, _end);
×
764

765
  if (pVal) {
×
766
    *pVLen = pFileState->rowSize;
×
767
    *pVal = pNewPos;
×
768
  }
769

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

777
void deleteRowBuff(SStreamFileState* pFileState, const void* pKey, int32_t keyLen) {
×
778
  int32_t code_buff = pFileState->stateBuffRemoveFn(pFileState->rowStateBuff, pKey, keyLen);
×
779
  qTrace("%s at line %d res:%d", __func__, __LINE__, code_buff);
×
780
  int32_t code_file = pFileState->stateFileRemoveFn(pFileState, pKey);
×
781
  qTrace("%s at line %d res:%d", __func__, __LINE__, code_file);
×
782
  if (pFileState->searchBuff != NULL) {
×
783
    deleteHashSortRowBuff(pFileState, pKey);
×
784
  }
785
}
×
786

787
void deleteRowBuffByGroupId(SStreamFileState* pFileState, uint64_t groupId) {
×
788
  SSHashObj* pRowMap = pFileState->rowStateBuff;
×
789
  void*   pIte = NULL;
×
790
  int32_t iter = 0;
×
791
  while ((pIte = tSimpleHashIterate(pRowMap, pIte, &iter)) != NULL) {
×
792
    size_t keyLen = 0;
×
793
    SWinKey* pKey = tSimpleHashGetKey(pIte, &keyLen);
×
794
    if (pKey->groupId == groupId) {
×
795
      int32_t tmpRes = tSimpleHashIterateRemove(pRowMap, pKey, keyLen, &pIte, &iter);
×
796
      qTrace("%s at line %d res:%d", __func__, __LINE__, tmpRes);
×
797
    }
798
  }
799

800
  while (1) {
×
801
    SWinKey tmp = {.ts = INT64_MIN, .groupId = groupId};
×
802
    SStreamStateCur* pCur = streamStateSeekKeyNext_rocksdb(pFileState->pFileStore, &tmp);
×
803
    SWinKey delKey = {.groupId = groupId};
×
804
    int32_t code = streamStateGetGroupKVByCur_rocksdb(pFileState->pFileStore, pCur, &delKey, NULL, 0);
×
805
    if (code != TSDB_CODE_SUCCESS) {
×
806
      break;
×
807
    }
808
    code = streamStateDel_rocksdb(pFileState->pFileStore, &delKey);
×
809
    qTrace("%s at line %d res:%d", __func__, __LINE__, code);
×
810
  }
811
}
×
812

813
static int32_t recoverSessionRowBuff(SStreamFileState* pFileState, SRowBuffPos* pPos) {
×
814
  int32_t code = TSDB_CODE_SUCCESS;
×
815
  int32_t lino = 0;
×
816
  int32_t len = 0;
×
817
  void*   pBuff = NULL;
×
818
  code = pFileState->stateFileGetFn(pFileState, pPos->pKey, &pBuff, &len);
×
819
  QUERY_CHECK_CODE(code, lino, _end);
×
820
  memcpy(pPos->pRowBuff, pBuff, len);
×
821
  taosMemoryFree(pBuff);
×
822

823
_end:
×
824
  if (code != TSDB_CODE_SUCCESS) {
×
825
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
826
  }
827
  return code;
×
828
}
829

830
static int32_t recoverStateRowBuff(SStreamFileState* pFileState, SRowBuffPos* pPos) {
×
831
  int32_t code = TSDB_CODE_SUCCESS;
×
832
  int32_t lino = 0;
×
833
  pPos->pRowBuff = getFreeBuff(pFileState);
×
834
  if (!pPos->pRowBuff) {
×
835
    if (pFileState->curRowCount < pFileState->maxRowCount) {
×
836
      pPos->pRowBuff = taosMemoryCalloc(1, pFileState->rowSize);
×
837
      if (!pPos->pRowBuff) {
×
838
        code = terrno;
×
839
        QUERY_CHECK_CODE(code, lino, _end);
×
840
      }
841
      pFileState->curRowCount++;
×
842
    } else {
843
      code = clearRowBuff(pFileState);
×
844
      QUERY_CHECK_CODE(code, lino, _end);
×
845
      pPos->pRowBuff = getFreeBuff(pFileState);
×
846
    }
847
    QUERY_CHECK_CONDITION((pPos->pRowBuff != NULL), code, lino, _end, TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR);
×
848
  }
849

850
  code = recoverSessionRowBuff(pFileState, pPos);
×
851
  QUERY_CHECK_CODE(code, lino, _end);
×
852

853
_end:
×
854
  if (code != TSDB_CODE_SUCCESS) {
×
855
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
856
  }
857
  return code;
×
858
}
859

860
int32_t getRowBuffByPos(SStreamFileState* pFileState, SRowBuffPos* pPos, void** pVal) {
×
861
  int32_t code = TSDB_CODE_SUCCESS;
×
862
  int32_t lino = 0;
×
863
  if (pPos->pRowBuff) {
×
864
    if (pPos->needFree) {
×
865
      code = recoverSessionRowBuff(pFileState, pPos);
×
866
      QUERY_CHECK_CODE(code, lino, _end);
×
867
    }
868
    (*pVal) = pPos->pRowBuff;
×
869
    goto _end;
×
870
  }
871

872
  code = recoverStateRowBuff(pFileState, pPos);
×
873
  QUERY_CHECK_CODE(code, lino, _end);
×
874

875
  (*pVal) = pPos->pRowBuff;
×
876
  // if (!pPos->needFree) {
877
  //   code = tdListPrepend(pFileState->usedBuffs, &pPos);
878
  //   QUERY_CHECK_CODE(code, lino, _end);
879
  // }
880

881
_end:
×
882
  if (code != TSDB_CODE_SUCCESS) {
×
883
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
884
  }
885
  return code;
×
886
}
887

888
bool hasRowBuff(SStreamFileState* pFileState, const SWinKey* pKey, bool hasLimit, bool* pIsLast) {
×
889
  bool res = false;
×
890
  if (pIsLast != NULL) {
×
891
    (*pIsLast) = false;
×
892
  }
893

894
  SRowBuffPos** pos = tSimpleHashGet(pFileState->rowStateBuff, pKey, sizeof(SWinKey));
×
895
  if (pos) {
×
896
    res = true;
×
897
  }
898
  void* pSearchBuff = getSearchBuff(pFileState);
×
899
  if (pSearchBuff != NULL) {
×
900
    void** ppBuff = (void**)tSimpleHashGet(pSearchBuff, &pKey->groupId, sizeof(uint64_t));
×
901
    if (ppBuff != NULL) {
×
902
      SArray* pWinStates = (SArray*)(*ppBuff);
×
903
      if (pIsLast != NULL) {
×
904
        SWinKey* pLastKey = (SWinKey*)taosArrayGetLast(pWinStates);
×
905
        *pIsLast = (winKeyCmprImpl(pKey, pLastKey) == 0);
×
906
      }
907
      if (hasLimit && taosArrayGetSize(pWinStates) <= MIN_NUM_OF_SORT_CACHE_WIN) {
×
908
        res = true;
×
909
      }
910
      if (qDebugFlag & DEBUG_DEBUG) {
×
911
        if (taosArrayGetSize(pWinStates) > 0) {
×
912
          SWinKey* fistKey = (SWinKey*)taosArrayGet(pWinStates, 0);
×
913
          qDebug("===stream===check window state. buff min ts:%" PRId64 ",groupId:%" PRIu64 ".key ts:%" PRId64
×
914
                 ",groupId:%" PRIu64,
915
                 fistKey->ts, fistKey->groupId, pKey->ts, pKey->groupId);
916
        }
917
      }
918
    } else {
919
      res = true;
×
920
    }
921
  }
922
  return res;
×
923
}
924

925
SStreamSnapshot* getSnapshot(SStreamFileState* pFileState) {
×
926
  int64_t mark = (pFileState->deleteMark == INT64_MAX || pFileState->maxTs == INT64_MIN)
×
927
                     ? INT64_MIN
928
                     : pFileState->maxTs - pFileState->deleteMark;
×
929
  clearExpiredRowBuff(pFileState, mark, false);
×
930
  return pFileState->usedBuffs;
×
931
}
932

933
void flushSnapshot(SStreamFileState* pFileState, SStreamSnapshot* pSnapshot, bool flushState) {
×
934
  int32_t   code = TSDB_CODE_SUCCESS;
×
935
  int32_t   lino = 0;
×
936
  SListIter iter = {0};
×
937
  tdListInitIter(pSnapshot, &iter, TD_LIST_FORWARD);
×
938

939
  int64_t    st = taosGetTimestampMs();
×
940
  SListNode* pNode = NULL;
×
941

942
  int idx = streamStateGetCfIdx(pFileState->pFileStore, pFileState->cfName);
×
943

944
  int32_t len = (pFileState->rowSize + sizeof(uint64_t) + sizeof(int32_t) + 64) * 2;
×
945
  char*   buf = taosMemoryCalloc(1, len);
×
946
  if (!buf) {
×
947
    code = terrno;
×
948
    QUERY_CHECK_CODE(code, lino, _end);
×
949
  }
950

951
  void* batch = streamStateCreateBatch();
×
952
  if (!batch) {
×
953
    code = TSDB_CODE_OUT_OF_MEMORY;
×
954
    QUERY_CHECK_CODE(code, lino, _end);
×
955
  }
956

957
  while ((pNode = tdListNext(&iter)) != NULL && code == TSDB_CODE_SUCCESS) {
×
958
    SRowBuffPos* pPos = *(SRowBuffPos**)pNode->data;
×
959
    if (pPos->beFlushed || !pPos->pRowBuff) {
×
960
      continue;
×
961
    }
962
    pPos->beFlushed = true;
×
963
    pFileState->flushMark = TMAX(pFileState->flushMark, pFileState->getTs(pPos->pKey));
×
964

965
    qDebug("===stream===flushed start:%" PRId64, pFileState->getTs(pPos->pKey));
×
966
    if (streamStateGetBatchSize(batch) >= BATCH_LIMIT) {
×
967
      code = streamStatePutBatch_rocksdb(pFileState->pFileStore, batch);
×
968
      streamStateClearBatch(batch);
×
969
      QUERY_CHECK_CODE(code, lino, _end);
×
970
    }
971

972
    void* pSKey = pFileState->stateBuffCreateStateKeyFn(pPos, ((SStreamState*)pFileState->pFileStore)->number);
×
973
    QUERY_CHECK_NULL(pSKey, code, lino, _end, terrno);
×
974

975
    code = streamStatePutBatchOptimize(pFileState->pFileStore, idx, batch, pSKey, pPos->pRowBuff, pFileState->rowSize,
×
976
                                       0, buf);
977
    taosMemoryFreeClear(pSKey);
×
978
    QUERY_CHECK_CODE(code, lino, _end);
×
979
    // todo handle failure
980
    memset(buf, 0, len);
×
981
  }
982
  taosMemoryFreeClear(buf);
×
983

984
  int32_t numOfElems = streamStateGetBatchSize(batch);
×
985
  if (numOfElems > 0) {
×
986
    code = streamStatePutBatch_rocksdb(pFileState->pFileStore, batch);
×
987
    QUERY_CHECK_CODE(code, lino, _end);
×
988
  } else {
989
    goto _end;
×
990
  }
991

992
  streamStateClearBatch(batch);
×
993

994
  clearSearchBuff(pFileState);
×
995

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

1000
  if (flushState) {
×
1001
    void*   valBuf = NULL;
×
1002
    int32_t len = 0;
×
1003
    code = streamFileStateEncode(&pFileState->flushMark, &valBuf, &len);
×
1004
    QUERY_CHECK_CODE(code, lino, _end);
×
1005

1006
    qDebug("===stream===flushMark write:%" PRId64, pFileState->flushMark);
×
1007
    code = streamStatePutBatch(pFileState->pFileStore, "default", batch, STREAM_STATE_INFO_NAME, valBuf, len, 0);
×
1008
    taosMemoryFree(valBuf);
×
1009
    QUERY_CHECK_CODE(code, lino, _end);
×
1010

1011
    code = streamStatePutBatch_rocksdb(pFileState->pFileStore, batch);
×
1012
    QUERY_CHECK_CODE(code, lino, _end);
×
1013
  }
1014

1015
_end:
×
1016
  if (code != TSDB_CODE_SUCCESS) {
×
1017
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1018
  }
1019
  taosMemoryFree(buf);
×
1020
  streamStateDestroyBatch(batch);
×
1021
}
×
1022

1023
int32_t forceRemoveCheckpoint(SStreamFileState* pFileState, int64_t checkpointId) {
×
1024
  char keyBuf[128] = {0};
×
1025
  TAOS_UNUSED(tsnprintf(keyBuf, sizeof(keyBuf), "%s:%" PRId64, TASK_KEY, checkpointId));
×
1026
  return streamDefaultDel_rocksdb(pFileState->pFileStore, keyBuf);
×
1027
}
1028

1029
int32_t deleteExpiredCheckPoint(SStreamFileState* pFileState, TSKEY mark) {
×
1030
  int32_t code = TSDB_CODE_SUCCESS;
×
1031
  int64_t maxCheckPointId = 0;
×
1032
  {
1033
    char    buf[128] = {0};
×
1034
    void*   val = NULL;
×
1035
    int32_t len = 0;
×
1036
    memcpy(buf, TASK_KEY, strlen(TASK_KEY));
×
1037
    code = streamDefaultGet_rocksdb(pFileState->pFileStore, buf, &val, &len);
×
1038
    if (code != 0 || len == 0 || val == NULL) {
×
1039
      return TSDB_CODE_FAILED;
×
1040
    }
1041
    memcpy(buf, val, len);
×
1042
    buf[len] = 0;
×
1043
    maxCheckPointId = taosStr2Int64((char*)buf, NULL, 10);
×
1044
    taosMemoryFree(val);
×
1045
  }
1046
  for (int64_t i = maxCheckPointId; i > 0; i--) {
×
1047
    char    buf[128] = {0};
×
1048
    void*   val = 0;
×
1049
    int32_t len = 0;
×
1050
    TAOS_UNUSED(tsnprintf(buf, sizeof(buf), "%s:%" PRId64, TASK_KEY, i));
×
1051
    code = streamDefaultGet_rocksdb(pFileState->pFileStore, buf, &val, &len);
×
1052
    if (code != 0) {
×
1053
      return TSDB_CODE_FAILED;
×
1054
    }
1055
    memcpy(buf, val, len);
×
1056
    buf[len] = 0;
×
1057
    taosMemoryFree(val);
×
1058

1059
    TSKEY ts;
1060
    ts = taosStr2Int64((char*)buf, NULL, 10);
×
1061
    if (ts < mark) {
×
1062
      // statekey winkey.ts < mark
1063
      int32_t tmpRes = forceRemoveCheckpoint(pFileState, i);
×
1064
      qTrace("%s at line %d res:%d", __func__, __LINE__, tmpRes);
×
1065
      break;
×
1066
    }
1067
  }
1068
  return code;
×
1069
}
1070

1071
int32_t recoverSession(SStreamFileState* pFileState, int64_t ckId) {
×
1072
  int32_t code = TSDB_CODE_SUCCESS;
×
1073
  int32_t lino = 0;
×
1074
  int32_t winRes = TSDB_CODE_SUCCESS;
×
1075
  if (pFileState->maxTs != INT64_MIN) {
×
1076
    int64_t mark = (INT64_MIN + pFileState->deleteMark >= pFileState->maxTs)
×
1077
                       ? INT64_MIN
1078
                       : pFileState->maxTs - pFileState->deleteMark;
×
1079
    int32_t tmpRes = deleteExpiredCheckPoint(pFileState, mark);
×
1080
    qTrace("%s at line %d res:%d", __func__, __LINE__, tmpRes);
×
1081
  }
1082

1083
  SStreamStateCur* pCur = streamStateSessionSeekToLast_rocksdb(pFileState->pFileStore, INT64_MAX);
×
1084
  int32_t          recoverNum = TMIN(MIN_NUM_OF_RECOVER_ROW_BUFF, pFileState->maxRowCount);
×
1085
  while (winRes == TSDB_CODE_SUCCESS) {
×
1086
    if (pFileState->curRowCount >= recoverNum) {
×
1087
      break;
×
1088
    }
1089

1090
    void*       pVal = NULL;
×
1091
    int32_t     vlen = 0;
×
1092
    SSessionKey key = {0};
×
1093
    winRes = streamStateSessionGetKVByCur_rocksdb(getStateFileStore(pFileState), pCur, &key, &pVal, &vlen);
×
1094
    if (winRes != TSDB_CODE_SUCCESS) {
×
1095
      break;
×
1096
    }
1097

1098
    if (vlen != pFileState->rowSize) {
×
1099
      code = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
1100
      qError("[InternalERR] read key:[skey:%"PRId64 ",ekey:%"PRId64 ",groupId:%"PRIu64 "],vlen:%d, rowSize:%d", key.win.skey, key.win.ekey, key.groupId, vlen, pFileState->rowSize);
×
1101
      QUERY_CHECK_CODE(code, lino, _end);
×
1102
    }
1103

1104
    SRowBuffPos* pPos = createSessionWinBuff(pFileState, &key, pVal, &vlen);
×
1105
    pPos->beUsed = false;
×
1106
    winRes = putSessionWinResultBuff(pFileState, pPos);
×
1107
    if (winRes != TSDB_CODE_SUCCESS) {
×
1108
      break;
×
1109
    }
1110

1111
    winRes = streamStateSessionCurPrev_rocksdb(pCur);
×
1112
  }
1113

1114
_end:
×
1115
  if (code != TSDB_CODE_SUCCESS) {
×
1116
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1117
  }
1118
  streamStateFreeCur(pCur);
×
1119
  return code;
×
1120
}
1121

1122
int32_t recoverSnapshot(SStreamFileState* pFileState, int64_t ckId) {
×
1123
  int32_t code = TSDB_CODE_SUCCESS;
×
1124
  int32_t lino = 0;
×
1125
  int32_t winCode = TSDB_CODE_SUCCESS;
×
1126
  if (pFileState->maxTs != INT64_MIN) {
×
1127
    int64_t mark = (INT64_MIN + pFileState->deleteMark >= pFileState->maxTs)
×
1128
                       ? INT64_MIN
1129
                       : pFileState->maxTs - pFileState->deleteMark;
×
1130
    int32_t tmpRes = deleteExpiredCheckPoint(pFileState, mark);
×
1131
    qTrace("%s at line %d res:%d", __func__, __LINE__, tmpRes);
×
1132
  }
1133

1134
  SStreamStateCur* pCur = streamStateSeekToLast_rocksdb(pFileState->pFileStore);
×
1135
  int32_t          recoverNum = TMIN(MIN_NUM_OF_RECOVER_ROW_BUFF, pFileState->maxRowCount);
×
1136
  while (winCode == TSDB_CODE_SUCCESS) {
×
1137
    if (pFileState->curRowCount >= recoverNum) {
×
1138
      break;
×
1139
    }
1140

1141
    void*        pVal = NULL;
×
1142
    int32_t      vlen = 0;
×
1143
    SRowBuffPos* pNewPos = getNewRowPosForWrite(pFileState);
×
1144
    if (!pNewPos || !pNewPos->pRowBuff) {
×
1145
      code = TSDB_CODE_OUT_OF_MEMORY;
×
1146
      QUERY_CHECK_CODE(code, lino, _end);
×
1147
    }
1148

1149
    winCode =
1150
        streamStateGetKVByCur_rocksdb(getStateFileStore(pFileState), pCur, pNewPos->pKey, (const void**)&pVal, &vlen);
×
1151
    qDebug("===stream=== get state by cur winres:%d. %s", winCode, __func__);
×
1152
    if (winCode != TSDB_CODE_SUCCESS || pFileState->getTs(pNewPos->pKey) < pFileState->flushMark) {
×
1153
      destroyRowBuffPos(pNewPos);
×
1154
      SListNode* pNode = tdListPopTail(pFileState->usedBuffs);
×
1155
      taosMemoryFreeClear(pNode);
×
1156
      taosMemoryFreeClear(pVal);
×
1157
      break;
×
1158
    }
1159
    if (vlen != pFileState->rowSize) {
×
1160
      qError("row size mismatch, expect:%d, actual:%d", pFileState->rowSize, vlen);
×
1161
      code = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
1162
      taosMemoryFreeClear(pVal);
×
1163
      QUERY_CHECK_CODE(code, lino, _end);
×
1164
    }
1165
    memcpy(pNewPos->pRowBuff, pVal, vlen);
×
1166
    taosMemoryFreeClear(pVal);
×
1167
    pNewPos->beFlushed = true;
×
1168
    pNewPos->beUsed = false;
×
1169
    qDebug("===stream=== read checkpoint state from disc. %s", __func__);
×
1170
    code = tSimpleHashPut(pFileState->rowStateBuff, pNewPos->pKey, pFileState->keyLen, &pNewPos, POINTER_BYTES);
×
1171
    if (code != TSDB_CODE_SUCCESS) {
×
1172
      destroyRowBuffPos(pNewPos);
×
1173
      break;
×
1174
    }
1175
    streamStateCurPrev_rocksdb(pCur);
×
1176
  }
1177

1178
_end:
×
1179
  if (code != TSDB_CODE_SUCCESS) {
×
1180
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1181
  }
1182
  streamStateFreeCur(pCur);
×
1183
  return code;
×
1184
}
1185

1186
int32_t streamFileStateGetSelectRowSize(SStreamFileState* pFileState) { return pFileState->selectivityRowSize; }
×
1187

1188
void streamFileStateReloadInfo(SStreamFileState* pFileState, TSKEY ts) {
×
1189
  pFileState->flushMark = TMAX(pFileState->flushMark, ts);
×
1190
  pFileState->maxTs = TMAX(pFileState->maxTs, ts);
×
1191
}
×
1192

1193
void* getRowStateBuff(SStreamFileState* pFileState) { return pFileState->rowStateBuff; }
×
1194
void* getSearchBuff(SStreamFileState* pFileState) { return pFileState->searchBuff; }
×
1195

1196
void* getStateFileStore(SStreamFileState* pFileState) { return pFileState->pFileStore; }
×
1197

1198
bool isDeteled(SStreamFileState* pFileState, TSKEY ts) {
×
1199
  return pFileState->deleteMark != INT64_MAX && pFileState->maxTs > 0 &&
×
1200
         ts < (pFileState->maxTs - pFileState->deleteMark);
×
1201
}
1202

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

1205
TSKEY getFlushMark(SStreamFileState* pFileState) { return pFileState->flushMark; };
×
1206

1207
int32_t getRowStateRowSize(SStreamFileState* pFileState) { return pFileState->rowSize; }
×
1208

1209
int32_t getFunctionRowBuff(SStreamFileState* pFileState, void* pKey, int32_t keyLen, void** pVal, int32_t* pVLen) {
×
1210
  int32_t winCode = TSDB_CODE_SUCCESS;
×
1211
  return pFileState->stateFunctionGetFn(pFileState, pKey, keyLen, pVal, pVLen, &winCode);
×
1212
}
1213

1214
int32_t recoverFillSnapshot(SStreamFileState* pFileState, int64_t ckId) {
×
1215
  int32_t code = TSDB_CODE_SUCCESS;
×
1216
  int32_t lino = 0;
×
1217
  if (pFileState->maxTs != INT64_MIN) {
×
1218
    int64_t mark = (INT64_MIN + pFileState->deleteMark >= pFileState->maxTs)
×
1219
                       ? INT64_MIN
1220
                       : pFileState->maxTs - pFileState->deleteMark;
×
1221
    code = deleteExpiredCheckPoint(pFileState, mark);
×
1222
    QUERY_CHECK_CODE(code, lino, _end);
×
1223
  }
1224

1225
  SStreamStateCur* pCur = streamStateFillSeekToLast_rocksdb(pFileState->pFileStore);
×
1226
  if (pCur == NULL) {
×
1227
    return code;
×
1228
  }
1229
  int32_t recoverNum = TMIN(MIN_NUM_OF_RECOVER_ROW_BUFF, pFileState->maxRowCount);
×
1230
  int32_t winRes = TSDB_CODE_SUCCESS;
×
1231
  while (winRes == TSDB_CODE_SUCCESS) {
×
1232
    if (pFileState->curRowCount >= recoverNum) {
×
1233
      break;
×
1234
    }
1235

1236
    void*        pVal = NULL;
×
1237
    int32_t      vlen = 0;
×
1238
    SRowBuffPos* pNewPos = getNewRowPosForWrite(pFileState);
×
1239
    winRes = streamStateFillGetKVByCur_rocksdb(pCur, pNewPos->pKey, (const void**)&pVal, &vlen);
×
1240
    qDebug("===stream=== get state by cur winres:%d. %s", winRes, __func__);
×
1241
    if (winRes != TSDB_CODE_SUCCESS || isFlushedState(pFileState, pFileState->getTs(pNewPos->pKey), 0)) {
×
1242
      destroyRowBuffPos(pNewPos);
×
1243
      SListNode* pNode = tdListPopTail(pFileState->usedBuffs);
×
1244
      taosMemoryFreeClear(pNode);
×
1245
      taosMemoryFreeClear(pVal);
×
1246
      break;
×
1247
    }
1248

1249
    if (vlen != pFileState->rowSize) {
×
1250
      qError("row size mismatch, expect:%d, actual:%d", pFileState->rowSize, vlen);
×
1251
      destroyRowBuffPos(pNewPos);
×
1252
      code = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR;
×
1253
      taosMemoryFreeClear(pVal);
×
1254
      QUERY_CHECK_CODE(code, lino, _end);
×
1255
    }
1256

1257
    memcpy(pNewPos->pRowBuff, pVal, vlen);
×
1258
    taosMemoryFreeClear(pVal);
×
1259
    pNewPos->beFlushed = true;
×
1260
    pNewPos->beUsed = false;
×
1261
    qDebug("===stream=== read checkpoint state from disc. %s", __func__);
×
1262
    winRes = tSimpleHashPut(pFileState->rowStateBuff, pNewPos->pKey, pFileState->keyLen, &pNewPos, POINTER_BYTES);
×
1263
    if (winRes != TSDB_CODE_SUCCESS) {
×
1264
      destroyRowBuffPos(pNewPos);
×
1265
      break;
×
1266
    }
1267
    streamStateCurPrev_rocksdb(pCur);
×
1268
  }
1269
  streamStateFreeCur(pCur);
×
1270

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

1278
int32_t getRowBuff(SStreamFileState* pFileState, void* pKey, int32_t keyLen, void** pVal, int32_t* pVLen,
×
1279
                   int32_t* pWinCode) {
1280
  int32_t code = TSDB_CODE_SUCCESS;
×
1281
  int32_t lino = 0;
×
1282
  (*pWinCode) = TSDB_CODE_FAILED;
×
1283
  pFileState->maxTs = TMAX(pFileState->maxTs, pFileState->getTs(pKey));
×
1284
  SRowBuffPos** ppPos = tSimpleHashGet(pFileState->rowStateBuff, pKey, keyLen);
×
1285
  if (ppPos) {
×
1286
    *pVLen = pFileState->rowSize;
×
1287
    *pVal = *ppPos;
×
1288
    (*ppPos)->beUsed = true;
×
1289
    (*ppPos)->beFlushed = false;
×
1290
    (*pWinCode) = TSDB_CODE_SUCCESS;
×
1291
    if ((*ppPos)->pRowBuff == NULL) {
×
1292
      code = recoverStateRowBuff(pFileState, *ppPos);
×
1293
      QUERY_CHECK_CODE(code, lino, _end);
×
1294
    }
1295
    goto _end;
×
1296
  }
1297
  TSKEY ts = pFileState->getTs(pKey);
×
1298
  if (!isDeteled(pFileState, ts) && isFlushedState(pFileState, ts, 0)) {
×
1299
    int32_t len = 0;
×
1300
    void*   p = NULL;
×
1301
    (*pWinCode) = pFileState->stateFileGetFn(pFileState, pKey, &p, &len);
×
1302
    qDebug("===stream===get %" PRId64 " from disc, res %d", ts, (*pWinCode));
×
1303
    if ((*pWinCode) == TSDB_CODE_SUCCESS) {
×
1304
      SRowBuffPos* pNewPos = getNewRowPosForWrite(pFileState);
×
1305
      if (!pNewPos || !pNewPos->pRowBuff) {
×
1306
        code = TSDB_CODE_OUT_OF_MEMORY;
×
1307
        QUERY_CHECK_CODE(code, lino, _end);
×
1308
      }
1309

1310
      memcpy(pNewPos->pKey, pKey, keyLen);
×
1311
      memcpy(pNewPos->pRowBuff, p, len);
×
1312
      code = tSimpleHashPut(pFileState->rowStateBuff, pKey, keyLen, &pNewPos, POINTER_BYTES);
×
1313
      QUERY_CHECK_CODE(code, lino, _end);
×
1314

1315
      if (pVal) {
×
1316
        *pVLen = pFileState->rowSize;
×
1317
        *pVal = pNewPos;
×
1318
      }
1319
    }
1320
    taosMemoryFree(p);
×
1321
  }
1322

1323
_end:
×
1324
  if (code != TSDB_CODE_SUCCESS) {
×
1325
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1326
  }
1327
  return code;
×
1328
}
1329

1330
int32_t streamFileStateGroupPut(SStreamFileState* pFileState, int64_t groupId, void* value, int32_t vLen) {
×
1331
  int32_t code = TSDB_CODE_SUCCESS;
×
1332
  int32_t lino = 0;
×
1333
  if (value != NULL) {
×
1334
    code = TSDB_CODE_INVALID_PARA;
×
1335
    QUERY_CHECK_CODE(code, lino, _end);
×
1336
  }
1337

1338
  if (tSimpleHashGet(pFileState->pGroupIdMap, &groupId, sizeof(int64_t)) == NULL) {
×
1339
    if (tSimpleHashGetSize(pFileState->pGroupIdMap) <= MAX_GROUP_ID_NUM) {
×
1340
      code = tSimpleHashPut(pFileState->pGroupIdMap, &groupId, sizeof(int64_t), NULL, 0);
×
1341
      QUERY_CHECK_CODE(code, lino, _end);
×
1342
    }
1343
    code = streamStatePutParTag_rocksdb(pFileState->pFileStore, groupId, value, vLen);
×
1344
    QUERY_CHECK_CODE(code, lino, _end);
×
1345
  }
1346

1347
_end:
×
1348
  if (code != TSDB_CODE_SUCCESS) {
×
1349
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1350
  }
1351
  return code;
×
1352
}
1353

1354
void streamFileStateGroupCurNext(SStreamStateCur* pCur) {
×
1355
  SStreamFileState* pFileState = (SStreamFileState*)pCur->pStreamFileState;
×
1356
  if (pCur->hashIter == -1) {
×
1357
    streamStateCurNext(pFileState->pFileStore, pCur);
×
1358
    return;
×
1359
  }
1360

1361
  int64_t gpId = *(int64_t*)tSimpleHashGetKey(pCur->pHashData, NULL);
×
1362
  pCur->minGpId = TMAX(pCur->minGpId, gpId);
×
1363

1364
  SSHashObj* pHash = pFileState->pGroupIdMap;
×
1365
  pCur->pHashData = tSimpleHashIterate(pHash, pCur->pHashData, &pCur->hashIter);
×
1366
  if (!pCur->pHashData) {
×
1367
    pCur->hashIter = -1;
×
1368
    streamStateParTagSeekKeyNext_rocksdb(pFileState->pFileStore, pCur->minGpId, pCur);
×
1369
    return;
×
1370
  }
1371
}
1372

1373
int32_t streamFileStateGroupGetKVByCur(SStreamStateCur* pCur, int64_t* pKey, void** pVal, int32_t* pVLen) {
×
1374
  int32_t code = TSDB_CODE_SUCCESS;
×
1375
  if (pCur->pHashData) {
×
1376
    *pKey = *(int64_t*)tSimpleHashGetKey(pCur->pHashData, NULL);
×
1377
    return code;
×
1378
  }
1379
  return streamStateParTagGetKVByCur_rocksdb(pCur, pKey, NULL, NULL);
×
1380
}
1381

1382
SSHashObj* getGroupIdCache(SStreamFileState* pFileState) {
×
1383
  return pFileState->pGroupIdMap;
×
1384
}
1385

1386
void clearExpiredState(SStreamFileState* pFileState, int32_t numOfKeep, TSKEY minTs) {
×
1387
  int32_t    code = TSDB_CODE_SUCCESS;
×
1388
  int32_t    lino = 0;
×
1389
  SSHashObj* pSearchBuff = pFileState->searchBuff;
×
1390
  void*      pIte = NULL;
×
1391
  int32_t    iter = 0;
×
1392
  while ((pIte = tSimpleHashIterate(pSearchBuff, pIte, &iter)) != NULL) {
×
1393
    SArray* pWinStates = *((void**)pIte);
×
1394
    int32_t arraySize = TARRAY_SIZE(pWinStates);
×
1395
    if (minTs != INT64_MAX && arraySize > numOfKeep) {
×
1396
      SWinKey key = {.ts = minTs};
×
1397
      key.groupId = *(uint64_t*)tSimpleHashGetKey(pIte, NULL);
×
1398
      int32_t index = binarySearch(pWinStates, arraySize, &key, fillStateKeyCompare);
×
1399
      numOfKeep = TMAX(arraySize - index, MIN_NUM_OF_SORT_CACHE_WIN);
×
1400
      qDebug("modify numOfKeep, numOfKeep:%d. %s at line %d", numOfKeep, __func__, __LINE__);
×
1401
    }
1402

1403
    int32_t size = arraySize - numOfKeep;
×
1404
    for (int32_t i = 0; i < size; i++) {
×
1405
      SWinKey* pKey = taosArrayGet(pWinStates, i);
×
1406
      int32_t  code_buff = pFileState->stateBuffRemoveFn(pFileState->rowStateBuff, pKey, sizeof(SWinKey));
×
1407
      qTrace("clear expired buff, ts:%" PRId64 ",groupid:%" PRIu64 ". %s at line %d res:%d", pKey->ts, pKey->groupId, __func__, __LINE__, code_buff);
×
1408

1409
      if (isFlushedState(pFileState, pKey->ts, 0)) {
×
1410
        int32_t code_file = pFileState->stateFileRemoveFn(pFileState, pKey);
×
1411
        qTrace("clear expired file, ts:%" PRId64 ". %s at line %d res:%d", pKey->ts, __func__, __LINE__, code_file);
×
1412
      }
1413

1414
      if (tSimpleHashGetSize(pFileState->pRecFlagMap) > 0) {
×
1415
        tSimpleHashRemove(pFileState->pRecFlagMap, pKey, sizeof(SWinKey));
×
1416
      }
1417
    }
1418
    taosArrayRemoveBatch(pWinStates, 0, size, NULL);
×
1419
  }
1420
  code = clearRowBuffNonFlush(pFileState);
×
1421
  QUERY_CHECK_CODE(code, lino, _end);
×
1422

1423
_end:
×
1424
  if (code != TSDB_CODE_SUCCESS) {
×
1425
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1426
  }
1427
}
×
1428

1429
#ifdef BUILD_NO_CALL
1430
int32_t getStateSearchRowBuff(SStreamFileState* pFileState, const SWinKey* pKey, void** pVal, int32_t* pVLen,
1431
                              int32_t* pWinCode) {
1432
  int32_t code = TSDB_CODE_SUCCESS;
1433
  int32_t lino = 0;
1434

1435
  code = addRowBuffIfNotExist(pFileState, (void*)pKey, sizeof(SWinKey), pVal, pVLen, pWinCode);
1436
  QUERY_CHECK_CODE(code, lino, _end);
1437

1438
  SArray*    pWinStates = NULL;
1439
  SSHashObj* pSearchBuff = getSearchBuff(pFileState);
1440
  void**     ppBuff = tSimpleHashGet(pSearchBuff, &pKey->groupId, sizeof(uint64_t));
1441
  if (ppBuff) {
1442
    pWinStates = (SArray*)(*ppBuff);
1443
  } else {
1444
    pWinStates = taosArrayInit(16, sizeof(SWinKey));
1445
    QUERY_CHECK_NULL(pWinStates, code, lino, _end, terrno);
1446

1447
    code = tSimpleHashPut(pSearchBuff, &pKey->groupId, sizeof(uint64_t), &pWinStates, POINTER_BYTES);
1448
    QUERY_CHECK_CODE(code, lino, _end);
1449
  }
1450

1451
  // recover
1452
  if (taosArrayGetSize(pWinStates) == 0 && needClearDiskBuff(pFileState)) {
1453
    recoverHashSortBuff(pFileState, pWinStates, pKey->groupId);
1454
  }
1455

1456
  int32_t size = taosArrayGetSize(pWinStates);
1457
  int32_t index = binarySearch(pWinStates, size, pKey, fillStateKeyCompare);
1458
  if (!isFlushedState(pFileState, pKey->ts, 0) || index >= 0) {
1459
    // find the first position which is smaller than the pKey
1460
    if (index >= 0) {
1461
      SWinKey* pTmpKey = taosArrayGet(pWinStates, index);
1462
      if (winKeyCmprImpl(pTmpKey, pKey) == 0) {
1463
        goto _end;
1464
      }
1465
    }
1466
    index++;
1467
    void* tmp = taosArrayInsert(pWinStates, index, pKey);
1468
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
1469
  }
1470

1471
  if (size >= MAX_NUM_OF_CACHE_WIN) {
1472
    int32_t num = size - NUM_OF_CACHE_WIN;
1473
    taosArrayRemoveBatch(pWinStates, 0, num, NULL);
1474
  }
1475

1476
_end:
1477
  if (code != TSDB_CODE_SUCCESS) {
1478
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
1479
  }
1480
  return code;
1481
}
1482
#endif
1483

1484
int32_t getRowStatePrevRow(SStreamFileState* pFileState, const SWinKey* pKey, SWinKey* pResKey, void** ppVal,
×
1485
                           int32_t* pVLen, int32_t* pWinCode) {
1486
  int32_t    code = TSDB_CODE_SUCCESS;
×
1487
  int32_t    lino = 0;
×
1488
  SArray*    pWinStates = NULL;
×
1489
  SSHashObj* pSearchBuff = getSearchBuff(pFileState);
×
1490
  void*      pState = getStateFileStore(pFileState);
×
1491
  void**     ppBuff = (void**)tSimpleHashGet(pSearchBuff, &pKey->groupId, sizeof(uint64_t));
×
1492
  if (ppBuff) {
×
1493
    pWinStates = (SArray*)(*ppBuff);
×
1494
  } else if (needClearDiskBuff(pFileState)) {
×
1495
    qDebug("===stream=== search buff is empty.group id:%" PRId64, pKey->groupId);
×
1496
    SStreamStateCur* pCur = streamStateSeekKeyPrev_rocksdb(pState, pKey);
×
1497
    void*            tmpVal = NULL;
×
1498
    int32_t          len = 0;
×
1499
    (*pWinCode) = streamStateGetGroupKVByCur_rocksdb(pState, pCur, pResKey, (const void**)&tmpVal, &len);
×
1500
    if ((*pWinCode) == TSDB_CODE_SUCCESS) {
×
1501
      SRowBuffPos* pNewPos = getNewRowPosForWrite(pFileState);
×
1502
      if (!pNewPos || !pNewPos->pRowBuff) {
×
1503
        code = TSDB_CODE_OUT_OF_MEMORY;
×
1504
        QUERY_CHECK_CODE(code, lino, _end);
×
1505
      }
1506
      memcpy(pNewPos->pRowBuff, tmpVal, len);
×
1507
      taosMemoryFreeClear(tmpVal);
×
1508
      *pVLen = getRowStateRowSize(pFileState);
×
1509
      (*ppVal) = pNewPos;
×
1510
    }
1511
    streamStateFreeCur(pCur);
×
1512
    return code;
×
1513
  } else {
1514
    (*pWinCode) = TSDB_CODE_FAILED;
×
1515
    return code;
×
1516
  }
1517
  int32_t size = taosArrayGetSize(pWinStates);
×
1518
  int32_t index = binarySearch(pWinStates, size, pKey, fillStateKeyCompare);
×
1519
  if (index >= 0) {
×
1520
    SWinKey* pCurKey = taosArrayGet(pWinStates, index);
×
1521
    if (winKeyCmprImpl(pCurKey, pKey) == 0) {
×
1522
      index--;
×
1523
    } else {
1524
      qDebug("%s failed at line %d since do not find cur SWinKey. trigger may be force window close", __func__,
×
1525
             __LINE__);
1526
    }
1527
  }
1528
  if (index == -1) {
×
1529
    SStreamStateCur* pCur = streamStateSeekKeyPrev_rocksdb(pState, pKey);
×
1530
    void*            tmpVal = NULL;
×
1531
    int32_t          len = 0;
×
1532
    (*pWinCode) = streamStateGetGroupKVByCur_rocksdb(pState, pCur, pResKey, (const void**)&tmpVal, &len);
×
1533
    if ((*pWinCode) == TSDB_CODE_SUCCESS) {
×
1534
      SRowBuffPos* pNewPos = getNewRowPosForWrite(pFileState);
×
1535
      if (!pNewPos || !pNewPos->pRowBuff) {
×
1536
        code = TSDB_CODE_OUT_OF_MEMORY;
×
1537
        QUERY_CHECK_CODE(code, lino, _end);
×
1538
      }
1539
      memcpy(pNewPos->pRowBuff, tmpVal, len);
×
1540
      taosMemoryFreeClear(tmpVal);
×
1541
      *pVLen = getRowStateRowSize(pFileState);
×
1542
      (*ppVal) = pNewPos;
×
1543
    }
1544
    streamStateFreeCur(pCur);
×
1545
    return code;
×
1546
  } else {
1547
    SWinKey* pPrevKey = taosArrayGet(pWinStates, index);
×
1548
    *pResKey = *pPrevKey;
×
1549
    return addRowBuffIfNotExist(pFileState, (void*)pPrevKey, sizeof(SWinKey), ppVal, pVLen, pWinCode);
×
1550
  }
1551
  (*pWinCode) = TSDB_CODE_FAILED;
1552

1553
_end:
×
1554
  if (code != TSDB_CODE_SUCCESS) {
×
1555
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1556
  }
1557
  return code;
×
1558
}
1559

1560
int32_t addSearchItem(SStreamFileState* pFileState, SArray* pWinStates, const SWinKey* pKey, bool* pIsEnd) {
×
1561
  int32_t code = TSDB_CODE_SUCCESS;
×
1562
  int32_t lino = 0;
×
1563
  int32_t size = taosArrayGetSize(pWinStates);
×
1564
  int32_t index = binarySearch(pWinStates, size, pKey, fillTSKeyCompare);
×
1565
  if (!isFlushedState(pFileState, pKey->ts, 0) || index >= 0 || size == 0) {
×
1566
    if (index >= 0) {
×
1567
      SWinKey* pTmpKey = taosArrayGet(pWinStates, index);
×
1568
      if (winKeyCmprImpl(pTmpKey, pKey) == 0) {
×
1569
        goto _end;
×
1570
      }
1571
    }
1572
    index++;
×
1573
    (*pIsEnd) = (index >= size);
×
1574
    void* tmp = taosArrayInsert(pWinStates, index, pKey);
×
1575
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
1576
  }
1577

1578
_end:
×
1579
  if (code != TSDB_CODE_SUCCESS) {
×
1580
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1581
  }
1582
  return code;
×
1583
}
1584

1585
int32_t addArrayBuffIfNotExist(SSHashObj* pSearchBuff, uint64_t groupId, SArray** ppResStates) {
×
1586
  int32_t code = TSDB_CODE_SUCCESS;
×
1587
  int32_t lino = 0;
×
1588
  SArray* pWinStates = NULL;
×
1589
  void**  ppBuff = tSimpleHashGet(pSearchBuff, &groupId, sizeof(uint64_t));
×
1590
  if (ppBuff) {
×
1591
    pWinStates = (SArray*)(*ppBuff);
×
1592
  } else {
1593
    pWinStates = taosArrayInit(16, sizeof(SWinKey));
×
1594
    QUERY_CHECK_NULL(pWinStates, code, lino, _end, terrno);
×
1595

1596
    code = tSimpleHashPut(pSearchBuff, &groupId, sizeof(uint64_t), &pWinStates, POINTER_BYTES);
×
1597
    QUERY_CHECK_CODE(code, lino, _end);
×
1598
  }
1599

1600
  (*ppResStates) = pWinStates;
×
1601

1602
_end:
×
1603
  if (code != TSDB_CODE_SUCCESS) {
×
1604
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1605
  }
1606
  return code;
×
1607
}
1608

1609
static void setValueBuff(TSKEY ts, char* pVal, int32_t len, char* pBuff, int32_t buffLen) {
×
1610
  SET_TSDATA_FLAG(pBuff, buffLen);
×
1611
  if (len == 0) {
×
1612
    *(TSKEY*)pBuff = ts;
×
1613
    return;
×
1614
  }
1615
  memset(pBuff, 0, buffLen - 1);
×
1616
  *(TSKEY*)pBuff = ts;
×
1617
  memcpy(pBuff + sizeof(TSKEY), pVal, len);
×
1618
}
1619

1620
int32_t getAndSetTsData(STableTsDataState* pTsDataState, uint64_t tableUid, TSKEY* pCurTs, void** ppCurPkVal,
×
1621
                        TSKEY lastTs, void* pLastPkVal, int32_t lastPkLen, int32_t* pWinCode) {
1622
  int32_t code = TSDB_CODE_SUCCESS;
×
1623
  int32_t lino = 0;
×
1624
  bool    hasPk = (lastPkLen != 0);
×
1625

1626
  TSKEY* pDataVal = tSimpleHashGet(pTsDataState->pTableTsDataMap, &tableUid, sizeof(uint64_t));
×
1627
  if (pDataVal != NULL) {
×
1628
    (*pWinCode) = TSDB_CODE_SUCCESS;
×
1629
    *pCurTs = *pDataVal;
×
1630
    if ((*pCurTs) < lastTs) {
×
1631
      setValueBuff(lastTs, pLastPkVal, lastPkLen, (char*)pDataVal, pTsDataState->pkValLen);
×
1632
    } else {
1633
      if (hasPk) {
×
1634
        (*ppCurPkVal) = POINTER_SHIFT(pDataVal, sizeof(TSKEY));
×
1635
        if ((*pCurTs) == lastTs && pTsDataState->comparePkColFn((*ppCurPkVal), pLastPkVal) < 0) {
×
1636
          setValueBuff(lastTs, pLastPkVal, lastPkLen, (char*)pDataVal, pTsDataState->pkValLen);
×
1637
        }
1638
      }
1639
    }
1640
  } else {
1641
    setValueBuff(lastTs, pLastPkVal, lastPkLen, pTsDataState->pPkValBuff, pTsDataState->pkValLen);
×
1642
    int32_t size = tSimpleHashGetSize(pTsDataState->pTableTsDataMap);
×
1643
    if (size < MAX_STATE_MAP_SIZE) {
×
1644
      (*pWinCode) = TSDB_CODE_FAILED;
×
1645
      code = tSimpleHashPut(pTsDataState->pTableTsDataMap, &tableUid, sizeof(uint64_t), pTsDataState->pPkValBuff,
×
1646
                            pTsDataState->pkValLen);
×
1647
      QUERY_CHECK_CODE(code, lino, _end);
×
1648
    } else {
1649
      (*pWinCode) = streamStateGetParTag_rocksdb(pTsDataState->pState, tableUid, &pTsDataState->pPkValBuff,
×
1650
                                                 &pTsDataState->pkValLen);
1651
      if ((*pWinCode) == TSDB_CODE_SUCCESS) {
×
1652
        *pCurTs = *(TSKEY*)pTsDataState->pPkValBuff;
×
1653
        if (hasPk) {
×
1654
          (*ppCurPkVal) = POINTER_SHIFT(pTsDataState->pPkValBuff, sizeof(TSKEY));
×
1655
        }
1656
      }
1657

1658
      int32_t tmpCode = streamStatePutParTag_rocksdb(pTsDataState->pState, tableUid, pTsDataState->pPkValBuff,
×
1659
                                                     pTsDataState->pkValLen);
1660
    }
1661
  }
1662

1663
_end:
×
1664
  if (code != TSDB_CODE_SUCCESS) {
×
1665
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1666
  }
1667
  return code;
×
1668
}
1669

1670
int32_t doTsDataCommit(STableTsDataState* pTsDataState) {
×
1671
  int32_t code = TSDB_CODE_SUCCESS;
×
1672
  int32_t lino = 0;
×
1673
  void*   batch = NULL;
×
1674
  char*   pTempBuf = NULL;
×
1675

1676
  batch = streamStateCreateBatch();
×
1677
  QUERY_CHECK_NULL(batch, code, lino, _end, terrno);
×
1678
  int           idx = streamStateGetCfIdx(pTsDataState->pState, "partag");
×
1679
  int32_t       len = (pTsDataState->pkValLen + sizeof(uint64_t) + sizeof(int32_t) + 64) * 2;
×
1680
  pTempBuf = taosMemoryCalloc(1, len);
×
1681
  QUERY_CHECK_NULL(pTempBuf, code, lino, _end, terrno);
×
1682

1683
  void*   pIte = NULL;
×
1684
  int32_t iter = 0;
×
1685
  while ((pIte = tSimpleHashIterate(pTsDataState->pTableTsDataMap, pIte, &iter)) != NULL) {
×
1686
    if (streamStateGetBatchSize(batch) >= BATCH_LIMIT) {
×
1687
      code = streamStatePutBatch_rocksdb(pTsDataState->pState, batch);
×
1688
      streamStateClearBatch(batch);
×
1689
      QUERY_CHECK_CODE(code, lino, _end);
×
1690
    }
1691

1692
    if (HAS_TSDATA_FLAG(pIte, pTsDataState->pkValLen)) {
×
1693
      void* pKey = tSimpleHashGetKey(pIte, NULL);
×
1694
      UNSET_TSDATA_FLAG(pIte, pTsDataState->pkValLen);
×
1695
      code = streamStatePutBatchOptimize(pTsDataState->pState, idx, batch, pKey, pIte, pTsDataState->pkValLen, 0,
×
1696
                                         pTempBuf);
1697
      QUERY_CHECK_CODE(code, lino, _end);
×
1698
      memset(pTempBuf, 0, len);
×
1699
      qDebug("flush ts data,table id:%" PRIu64 , *(uint64_t*)pKey);
×
1700
    }
1701
  }
1702

1703
  int32_t numOfElems = streamStateGetBatchSize(batch);
×
1704
  if (numOfElems > 0) {
×
1705
    code = streamStatePutBatch_rocksdb(pTsDataState->pState, batch);
×
1706
    QUERY_CHECK_CODE(code, lino, _end);
×
1707
  }
1708

1709
_end:
×
1710
  if (code != TSDB_CODE_SUCCESS) {
×
1711
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1712
  }
1713
  taosMemoryFree(pTempBuf);
×
1714
  streamStateDestroyBatch(batch);
×
1715
  return code;
×
1716
}
1717

1718
int32_t doRangeDataCommit(STableTsDataState* pTsDataState) {
×
1719
  int32_t code = TSDB_CODE_SUCCESS;
×
1720
  int32_t lino = 0;
×
1721
  void*   batch = NULL;
×
1722

1723
  batch = streamStateCreateBatch();
×
1724
  QUERY_CHECK_NULL(batch, code, lino, _end, terrno);
×
1725
  int           idx = streamStateGetCfIdx(pTsDataState->pState, "sess");
×
1726
  int32_t       len = (pTsDataState->pkValLen + sizeof(uint64_t) + sizeof(int32_t) + 64) * 2;
×
1727

1728
  int32_t size = taosArrayGetSize(pTsDataState->pScanRanges);
×
1729
  for (int32_t i = 0; i < size; i++) {
×
1730
    SScanRange* pRange = taosArrayGet(pTsDataState->pScanRanges, i);
×
1731
    if (streamStateGetBatchSize(batch) >= BATCH_LIMIT) {
×
1732
      code = streamStatePutBatch_rocksdb(pTsDataState->pState, batch);
×
1733
      streamStateClearBatch(batch);
×
1734
      QUERY_CHECK_CODE(code, lino, _end);
×
1735
    }
1736
    SSessionKey key = {.win = pRange->win, .groupId = 0};
×
1737
    int32_t     uidSize = tSimpleHashGetSize(pRange->pUIds);
×
1738
    int32_t     gpIdSize = tSimpleHashGetSize(pRange->pGroupIds);
×
1739
    int32_t     size = uidSize + gpIdSize;
×
1740
    uint64_t*   pIdBuf = (uint64_t*)taosMemoryCalloc(1, size);
×
1741
    void*       pIte = NULL;
×
1742
    int32_t     iter = 0;
×
1743
    int32_t     i = 0;
×
1744
    while ((pIte = tSimpleHashIterate(pTsDataState->pTableTsDataMap, pIte, &iter)) != NULL) {
×
1745
      void* pTempKey = tSimpleHashGetKey(pIte, NULL);
×
1746
      pIdBuf[i] = *(uint64_t*)pTempKey;
×
1747
      i++;
×
1748
    }
1749

1750
    code = streamStatePutBatchOptimize(pTsDataState->pState, idx, batch, &key, (void*)pIdBuf, size, 0,
×
1751
                                       NULL);
1752
    QUERY_CHECK_CODE(code, lino, _end);
×
1753
  }
1754

1755
  int32_t numOfElems = streamStateGetBatchSize(batch);
×
1756
  if (numOfElems > 0) {
×
1757
    code = streamStatePutBatch_rocksdb(pTsDataState->pState, batch);
×
1758
    QUERY_CHECK_CODE(code, lino, _end);
×
1759
  }
1760

1761
_end:
×
1762
  if (code != TSDB_CODE_SUCCESS) {
×
1763
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1764
  }
1765
  streamStateDestroyBatch(batch);
×
1766
  return code;
×
1767
}
1768

1769
int32_t initTsDataState(STableTsDataState** ppTsDataState, int8_t pkType, int32_t pkLen, void* pState, void* pOtherState) {
×
1770
  int32_t    code = TSDB_CODE_SUCCESS;
×
1771
  int32_t    lino = 0;
×
1772

1773
  STableTsDataState* pTsDataState = taosMemoryCalloc(1, sizeof(STableTsDataState));
×
1774
  QUERY_CHECK_NULL(pTsDataState, code, lino, _end, terrno);
×
1775

1776
  _hash_fn_t hashFn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT);
×
1777
  pTsDataState->pTableTsDataMap = tSimpleHashInit(DEFAULT_STATE_MAP_CAPACITY, hashFn);
×
1778
  QUERY_CHECK_NULL(pTsDataState->pTableTsDataMap, code, lino, _end, terrno);
×
1779

1780
  pTsDataState->pkValLen = sizeof(TSKEY) + pkLen + sizeof(char);
×
1781
  pTsDataState->pPkValBuff = taosMemoryCalloc(1, pTsDataState->pkValLen);
×
1782
  QUERY_CHECK_NULL(pTsDataState->pPkValBuff, code, lino, _end, terrno);
×
1783

1784
  if (pkLen != 0) {
×
1785
    pTsDataState->comparePkColFn = getKeyComparFunc(pkType, TSDB_ORDER_ASC);
×
1786
  } else {
1787
    pTsDataState->comparePkColFn = NULL;
×
1788
  }
1789

1790
  pTsDataState->pScanRanges = taosArrayInit(64, sizeof(SScanRange));
×
1791
  QUERY_CHECK_NULL(pTsDataState->pScanRanges, code, lino, _end, terrno);
×
1792

1793
  pTsDataState->pState = pState;
×
1794
  pTsDataState->recValueLen = sizeof(SRecDataInfo) + pkLen;
×
1795
  pTsDataState->pRecValueBuff = taosMemoryCalloc(1, pTsDataState->recValueLen);
×
1796
  QUERY_CHECK_NULL(pTsDataState->pRecValueBuff, code, lino, _end, terrno);
×
1797

1798
  pTsDataState->curRecId = -1;
×
1799

1800
  pTsDataState->pStreamTaskState = pOtherState;
×
1801

1802
  pTsDataState->cfgIndex = streamStateGetCfIdx(pTsDataState->pState, "sess");
×
1803
  pTsDataState->pBatch = streamStateCreateBatch();
×
1804
  QUERY_CHECK_NULL(pTsDataState->pBatch, code, lino, _end, TSDB_CODE_FAILED);
×
1805
  
1806
  pTsDataState->batchBufflen = (pTsDataState->recValueLen + sizeof(uint64_t) + sizeof(int32_t) + 64) * 2;
×
1807
  pTsDataState->pBatchBuff = taosMemoryCalloc(1, pTsDataState->batchBufflen);
×
1808

1809
  (*ppTsDataState) = pTsDataState;
×
1810

1811
_end:
×
1812
  if (code != TSDB_CODE_SUCCESS) {
×
1813
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1814
  }
1815
  return code;
×
1816
}
1817

1818
static void destroyScanRange(SScanRange* pRange) {
×
1819
  pRange->win.skey = INT64_MIN;
×
1820
  pRange->win.ekey = INT64_MIN;
×
1821
  tSimpleHashCleanup(pRange->pUIds);
×
1822
  pRange->pUIds = NULL;
×
1823
  tSimpleHashCleanup(pRange->pGroupIds);
×
1824
  pRange->pGroupIds = NULL;
×
1825
}
×
1826

1827
void destroyTsDataState(STableTsDataState* pTsDataState) {
×
1828
  SArray* pScanRanges = pTsDataState->pScanRanges;
×
1829
  int32_t size = taosArrayGetSize(pScanRanges);
×
1830
  for (int32_t i = 0; i < size; i++) {
×
1831
    SScanRange* pRange = taosArrayGet(pScanRanges, i);
×
1832
    destroyScanRange(pRange);
×
1833
  }
1834
  taosArrayDestroy(pTsDataState->pScanRanges);
×
1835
  tSimpleHashCleanup(pTsDataState->pTableTsDataMap);
×
1836
  taosMemoryFreeClear(pTsDataState->pPkValBuff);
×
1837
  taosMemoryFreeClear(pTsDataState->pState);
×
1838
  taosMemoryFreeClear(pTsDataState->pRecValueBuff);
×
1839
  pTsDataState->pStreamTaskState = NULL;
×
1840

1841
  streamStateClearBatch(pTsDataState->pBatch);
×
1842
  streamStateDestroyBatch(pTsDataState->pBatch);
×
1843
  pTsDataState->pBatch = NULL;
×
1844
  taosMemoryFreeClear(pTsDataState->pBatchBuff);
×
1845

1846
  taosMemoryFreeClear(pTsDataState);
×
1847
}
×
1848

1849
int32_t recoverTsData(STableTsDataState* pTsDataState) {
×
1850
  int32_t          code = TSDB_CODE_SUCCESS;
×
1851
  int32_t          lino = 0;
×
1852
  SStreamStateCur* pCur = createStateCursor(NULL);
×
1853
  streamStateParTagSeekKeyNext_rocksdb(pTsDataState->pState, INT64_MIN, pCur);
×
1854
  while (1) {
×
1855
    uint64_t tableUid = 0;
×
1856
    void*    pVal = NULL;
×
1857
    int32_t  len = 0;
×
1858
    int32_t  winCode = streamStateParTagGetKVByCur_rocksdb(pCur, &tableUid, &pVal, &len);
×
1859
    if (winCode != TSDB_CODE_SUCCESS) {
×
1860
      break;
×
1861
    }
1862
    if (pTsDataState->pkValLen != len) {
×
1863
      taosMemoryFree(pVal);
×
1864
      streamStateCurNext_rocksdb(pCur);
×
1865
      continue;
×
1866
    }
1867
    UNSET_TSDATA_FLAG(pVal, len);
×
1868
    code = tSimpleHashPut(pTsDataState->pTableTsDataMap, &tableUid, sizeof(uint64_t), pVal, len);
×
1869
    taosMemoryFree(pVal);
×
1870
    QUERY_CHECK_CODE(code, lino, _end);
×
1871
    streamStateCurNext_rocksdb(pCur);
×
1872
  }
1873

1874
_end:
×
1875
  streamStateFreeCur(pCur);
×
1876
  if (code != TSDB_CODE_SUCCESS) {
×
1877
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1878
  }
1879
  return code;
×
1880
}
1881

1882
SStreamStateCur* getLastStateCur(SStreamFileState* pFileState, getStateBuffFn fn) {
×
1883
  SStreamStateCur* pCur = createStateCursor(pFileState);
×
1884
  if (pCur == NULL) {
×
1885
    return NULL;
×
1886
  }
1887
  SSHashObj* pSearchBuff = fn(pFileState);
×
1888
  pCur->buffIndex = 0;
×
1889
  pCur->hashIter = 0;
×
1890
  pCur->pHashData = NULL;
×
1891
  pCur->pHashData = tSimpleHashIterate(pSearchBuff, pCur->pHashData, &pCur->hashIter);
×
1892
  return pCur;
×
1893
}
1894

1895
void moveLastStateCurNext(SStreamStateCur* pCur, getStateBuffFn fn) {
×
1896
  SSHashObj* pSearchBuff = fn(pCur->pStreamFileState);
×
1897
  pCur->pHashData = tSimpleHashIterate(pSearchBuff, pCur->pHashData, &pCur->hashIter);
×
1898
}
×
1899

1900
int32_t getNLastStateKVByCur(SStreamStateCur* pCur, int32_t num, SArray* pRes) {
×
1901
  int32_t code = TSDB_CODE_SUCCESS;
×
1902
  int32_t lino = 0;
×
1903
  SArray*  pWinStates = NULL;
×
1904
  int32_t size = 0;
×
1905

1906
  while(1) {
1907
    if (pCur->pHashData == NULL) {
×
1908
      return TSDB_CODE_FAILED;
×
1909
    }
1910
    pWinStates = *((void**)pCur->pHashData);
×
1911
    size = taosArrayGetSize(pWinStates);
×
1912
    if (size > 0) {
×
1913
      break;
×
1914
    }
1915
    moveLastStateCurNext(pCur, getSearchBuff);
×
1916
  }
1917

1918
  int32_t i = TMAX(size - num, 0);
×
1919

1920
  for ( ; i < size; i++) {
×
1921
    SWinKey* pKey = taosArrayGet(pWinStates, i);
×
1922
    int32_t  len = 0;
×
1923
    void*    pVal = NULL;
×
1924
    int32_t  winCode = TSDB_CODE_SUCCESS;
×
1925
    code = addRowBuffIfNotExist(pCur->pStreamFileState, (void*)pKey, sizeof(SWinKey), &pVal, &len, &winCode);
×
1926
    QUERY_CHECK_CODE(code, lino, _end);
×
1927

1928
    if (winCode != TSDB_CODE_SUCCESS) {
×
1929
      qError("%s failed at line %d since window not exist. ts:%" PRId64 ",groupId:%" PRIu64, __func__, __LINE__,
×
1930
             pKey->ts, pKey->groupId);
1931
    }
1932

1933
    void* pTempRes = taosArrayPush(pRes, &pVal);
×
1934
    QUERY_CHECK_NULL(pTempRes, code, lino, _end, terrno);
×
1935
  }
1936

1937
_end:
×
1938
  if (code != TSDB_CODE_SUCCESS) {
×
1939
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1940
  }
1941
  return code;
×
1942
}
1943

1944
int32_t reloadTsDataState(STableTsDataState* pTsDataState) {
×
1945
  int32_t code = TSDB_CODE_SUCCESS;
×
1946
  int32_t lino = 0;
×
1947

1948
  STableTsDataState tmpState = *pTsDataState;
×
1949
  _hash_fn_t hashFn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT);
×
1950
  tmpState.pTableTsDataMap = tSimpleHashInit(DEFAULT_STATE_MAP_CAPACITY, hashFn);
×
1951
  QUERY_CHECK_NULL(tmpState.pTableTsDataMap, code, lino, _end, terrno);
×
1952

1953
  code = recoverTsData(&tmpState);
×
1954
  QUERY_CHECK_CODE(code, lino, _end);
×
1955

1956
  void*   pIte = NULL;
×
1957
  int32_t iter = 0;
×
1958
  while ((pIte = tSimpleHashIterate(pTsDataState->pTableTsDataMap, pIte, &iter)) != NULL) {
×
1959
    size_t keyLen = 0;
×
1960
    void* pKey = tSimpleHashGetKey(pIte, &keyLen);
×
1961
    code = tSimpleHashPut(tmpState.pTableTsDataMap, pKey, keyLen, pIte, pTsDataState->pkValLen);
×
1962
    QUERY_CHECK_CODE(code, lino, _end);
×
1963
  }
1964
  tSimpleHashCleanup(pTsDataState->pTableTsDataMap);
×
1965
  pTsDataState->pTableTsDataMap = tmpState.pTableTsDataMap;
×
1966

1967
_end:
×
1968
  if (code != TSDB_CODE_SUCCESS) {
×
1969
    tSimpleHashCleanup(tmpState.pTableTsDataMap);
×
1970
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1971
  }
1972
  return code;
×
1973
}
1974

1975
int32_t saveRecInfoToDisk(STableTsDataState* pTsDataState, SSessionKey* pKey, SRecDataInfo* pVal, int32_t vLen) {
×
1976
  int32_t code = TSDB_CODE_SUCCESS;
×
1977
  int32_t lino = 0;
×
1978

1979
  SStateSessionKey stateKey = {.key = *pKey, .opNum = ((SStreamState*)pTsDataState->pState)->number};
×
1980
  code = streamStatePutBatchOptimize(pTsDataState->pState, pTsDataState->cfgIndex, pTsDataState->pBatch, &stateKey, pVal, vLen, 0,
×
1981
                                     pTsDataState->pBatchBuff);
1982
  QUERY_CHECK_CODE(code, lino, _end);
×
1983

1984
  memset(pTsDataState->pBatchBuff, 0, pTsDataState->batchBufflen);
×
1985

1986
  if (streamStateGetBatchSize(pTsDataState->pBatch) >= BATCH_LIMIT) {
×
1987
    code = streamStatePutBatch_rocksdb(pTsDataState->pState, pTsDataState->pBatch);
×
1988
    streamStateClearBatch(pTsDataState->pBatch);
×
1989
    QUERY_CHECK_CODE(code, lino, _end);
×
1990
  }
1991

1992
_end:
×
1993
  if (code != TSDB_CODE_SUCCESS) {
×
1994
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1995
  }
1996
  return code;
×
1997
}
1998

1999
int32_t flushRemainRecInfoToDisk(STableTsDataState* pTsDataState) {
×
2000
  int32_t code = streamStatePutBatch_rocksdb(pTsDataState->pState, pTsDataState->pBatch);
×
2001
  streamStateClearBatch(pTsDataState->pBatch);
×
2002
  return code;
×
2003
}
2004

2005
int32_t recoverHashSortBuff(SStreamFileState* pFileState, SArray* pWinStates, uint64_t groupId) {
×
2006
  int32_t code = TSDB_CODE_SUCCESS;
×
2007
  int32_t lino = 0;
×
2008

2009
  SWinKey          start = {.groupId = groupId, .ts = INT64_MAX};
×
2010
  void*            pState = getStateFileStore(pFileState);
×
2011
  SStreamStateCur* pCur = streamStateSeekKeyPrev_rocksdb(pState, &start);
×
2012
  for (int32_t i = 0; i < NUM_OF_CACHE_WIN; i++) {
×
2013
    SWinKey tmpKey = {.groupId = groupId};
×
2014
    int32_t tmpRes = streamStateGetGroupKVByCur_rocksdb(pState, pCur, &tmpKey, NULL, 0);
×
2015
    if (tmpRes != TSDB_CODE_SUCCESS) {
×
2016
      break;
×
2017
    }
2018
    void* tmp = taosArrayPush(pWinStates, &tmpKey);
×
2019
    QUERY_CHECK_NULL(tmp, code, lino, _end, terrno);
×
2020
    streamStateCurPrev_rocksdb(pCur);
×
2021
  }
2022
  taosArraySort(pWinStates, winKeyCmprImpl);
×
2023
  streamStateFreeCur(pCur);
×
2024

2025
_end:
×
2026
  if (code != TSDB_CODE_SUCCESS) {
×
2027
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
2028
  }
2029
  return code;
×
2030
}
2031

2032
int32_t getRowStateAllPrevRow(SStreamFileState* pFileState, const SWinKey* pKey, SArray* pResArray, int32_t maxNum) {
×
2033
  int32_t    code = TSDB_CODE_SUCCESS;
×
2034
  int32_t    lino = 0;
×
2035
  SWinKey*   pPrevKey = NULL;
×
2036
  SSHashObj* pSearchBuff = getSearchBuff(pFileState);
×
2037
  void*      pState = getStateFileStore(pFileState);
×
2038
  void**     ppBuff = (void**)tSimpleHashGet(pSearchBuff, &pKey->groupId, sizeof(uint64_t));
×
2039
  int32_t    num = 0;
×
2040
  if (ppBuff) {
×
2041
    SArray* pWinStates = (SArray*)(*ppBuff);
×
2042
    int32_t size = taosArrayGetSize(pWinStates);
×
2043
    int32_t index = binarySearch(pWinStates, size, pKey, fillStateKeyCompare);
×
2044
    for (; index >= 0 && num <= maxNum; index--) {
×
2045
      pPrevKey = taosArrayGet(pWinStates, index);
×
2046
      if (winKeyCmprImpl(pPrevKey, pKey) == 0) {
×
2047
        continue;
×
2048
      }
2049
      void*   pVal = NULL;
×
2050
      int32_t len = 0;
×
2051
      int32_t winCode = TSDB_CODE_SUCCESS;
×
2052
      code = addRowBuffIfNotExist(pFileState, (void*)pPrevKey, sizeof(SWinKey), &pVal, &len, &winCode);
×
2053
      QUERY_CHECK_CODE(code, lino, _end);
×
2054
      void* tempRes = taosArrayPush(pResArray, &pVal);
×
2055
      QUERY_CHECK_NULL(tempRes, code, lino, _end, terrno);
×
2056
      num++;
×
2057
    }
2058
  }
2059

2060
_end:
×
2061
  if (code != TSDB_CODE_SUCCESS) {
×
2062
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
2063
  }
2064
  return code;
×
2065
}
2066

2067
int32_t setStateRecFlag(SStreamFileState* pFileState, const void* pKey, int32_t keyLen, int32_t mode) {
×
2068
  return tSimpleHashPut(pFileState->pRecFlagMap, pKey, keyLen, &mode, sizeof(int32_t));
×
2069
}
2070

2071
int32_t getStateRecFlag(SStreamFileState* pFileState, const void* pKey, int32_t keyLen, int32_t* pMode) {
×
2072
  void* pVal = tSimpleHashGet(pFileState->pRecFlagMap, pKey, keyLen);
×
2073
  if (pVal == NULL) {
×
2074
    return TSDB_CODE_FAILED;
×
2075
  }
2076
  *pMode = *(int32_t*) pVal;
×
2077
  return TSDB_CODE_SUCCESS;
×
2078
}
2079

2080
void clearExpiredSessionState(SStreamFileState* pFileState, int32_t numOfKeep, TSKEY minTs, SSHashObj* pFlushGroup) {
×
2081
  int32_t    code = TSDB_CODE_SUCCESS;
×
2082
  int32_t    lino = 0;
×
2083
  SSHashObj* pSessionBuff = pFileState->rowStateBuff;
×
2084
  SStreamSnapshot* pFlushList = NULL;
×
2085
  if (pFlushGroup != NULL) {
×
2086
    pFlushList = tdListNew(POINTER_BYTES);
×
2087
  }
2088
  void*      pIte = NULL;
×
2089
  int32_t    iter = 0;
×
2090
  while ((pIte = tSimpleHashIterate(pSessionBuff, pIte, &iter)) != NULL) {
×
2091
    SArray* pWinStates = *((void**)pIte);
×
2092
    int32_t arraySize = TARRAY_SIZE(pWinStates);
×
2093
    if (minTs != INT64_MAX && arraySize > numOfKeep) {
×
2094
      SSessionKey key = {.win.skey = minTs, .win.ekey = minTs};
×
2095
      key.groupId = *(uint64_t*)tSimpleHashGetKey(pIte, NULL);
×
2096
      int32_t index = binarySearch(pWinStates, arraySize, &key, fillStateKeyCompare);
×
2097
      numOfKeep = TMAX(arraySize - index, MIN_NUM_OF_SORT_CACHE_WIN);
×
2098
      qDebug("modify numOfKeep, numOfKeep:%d. %s at line %d", numOfKeep, __func__, __LINE__);
×
2099
    }
2100

2101
    int32_t size = arraySize - numOfKeep;
×
2102
    for (int32_t i = 0; i < size; i++) {
×
2103
      SRowBuffPos* pPos = taosArrayGetP(pWinStates, i);
×
2104
      SSessionKey* pKey = pPos->pKey;
×
2105
      if (tSimpleHashGetSize(pFileState->pRecFlagMap) > 0) {
×
2106
        tSimpleHashRemove(pFileState->pRecFlagMap, pKey, sizeof(SSessionKey));
×
2107
      }
2108
      pPos->invalid = true;
×
2109

2110
      if (i == 0 && pFlushGroup != NULL) {
×
2111
        void* pGpVal = tSimpleHashGet(pFlushGroup, &pKey->groupId, sizeof(uint64_t));
×
2112
        if (pGpVal == NULL) {
×
2113
          code = tdListAppend(pFlushList, &pPos);
×
2114
          QUERY_CHECK_CODE(code, lino, _end);
×
2115
          code = tSimpleHashPut(pFlushGroup, &pKey->groupId, sizeof(uint64_t), NULL, 0);
×
2116
          QUERY_CHECK_CODE(code, lino, _end);
×
2117
          continue;
×
2118
        }
2119
      }
2120
      pPos->beFlushed = true;
×
2121
      qTrace("clear expired session buff, ts:%" PRId64 ",groupid:%" PRIu64 ". %s at line %d", pKey->win.skey, pKey->groupId, __func__, __LINE__);
×
2122

2123
      if (isFlushedState(pFileState, pKey->win.skey, 0)) {
×
2124
        int32_t code_file = pFileState->stateFileRemoveFn(pFileState, pKey);
×
2125
        qTrace("clear expired file, ts:%" PRId64 ". %s at line %d res:%d", pKey->win.skey, __func__, __LINE__, code_file);
×
2126
      }
2127
    }
2128
    taosArrayRemoveBatch(pWinStates, 0, size, NULL);
×
2129
  }
2130

2131
  if (pFlushList != NULL) {
×
2132
    flushSnapshot(pFileState, pFlushList, false);
×
2133
    code = clearRowBuffNonFlush(pFileState);
×
2134
    QUERY_CHECK_CODE(code, lino, _end);
×
2135
    tdListFreeP(pFlushList, destroyRowBuffPosPtr);
×
2136
  }
2137

2138
_end:
×
2139
  if (code != TSDB_CODE_SUCCESS) {
×
2140
    qError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
2141
  }
2142
}
×
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