tstreamFileState.c 8.6 KB
Newer Older
5
54liuyao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "tstreamFileState.h"

#include "taos.h"
#include "thash.h"
#include "tsimplehash.h"
#include "streamBackendRocksdb.h"


#define FLUSH_RATIO 0.2
#define DEFAULT_MAX_STREAM_BUFFER_SIZE (128 * 1024 * 1024);

struct SStreamFileState {
  SList*     usedBuffs;
  SList*     freeBuffs;
  SSHashObj* rowBuffMap;
  void*      pFileStore;
  int32_t    rowSize;
  int32_t    keyLen;
  uint64_t   preCheckPointVersion;
  uint64_t   checkPointVersion;
  TSKEY      maxTs;
  TSKEY      deleteMark;
5
54liuyao 已提交
38
  TSKEY      flushMark;
5
54liuyao 已提交
39 40
  uint64_t   maxRowCount;
  uint64_t   curRowCount;
5
54liuyao 已提交
41
  GetTsFun   getTs;
5
54liuyao 已提交
42 43 44 45
};

typedef SRowBuffPos SRowBuffInfo;

5
54liuyao 已提交
46
SStreamFileState* streamFileStateInit(int64_t memSize, uint32_t rowSize, GetTsFun fp, void* pFile, TSKEY delMark) {
5
54liuyao 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
  if (memSize <= 0) {
    memSize = DEFAULT_MAX_STREAM_BUFFER_SIZE;
  }
  if (rowSize == 0) {
    goto _error;
  }

  SStreamFileState* pFileState = taosMemoryCalloc(1, sizeof(SStreamFileState));
  if (!pFileState) {
    goto _error;
  }
  pFileState->usedBuffs = tdListNew(POINTER_BYTES);
  pFileState->freeBuffs = tdListNew(POINTER_BYTES);
  _hash_fn_t hashFn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY);
  pFileState->rowBuffMap = tSimpleHashInit(1024, hashFn);
  if (!pFileState->usedBuffs || !pFileState->freeBuffs || !pFileState->rowBuffMap) {
    goto _error;
  }
  pFileState->rowSize = rowSize;
  pFileState->preCheckPointVersion = 0;
  pFileState->checkPointVersion = 1;
  pFileState->pFileStore = pFile;
5
54liuyao 已提交
69
  pFileState->getTs = fp;
5
54liuyao 已提交
70 71
  pFileState->maxRowCount = memSize / rowSize;
  pFileState->curRowCount = 0;
5
54liuyao 已提交
72 73
  pFileState->deleteMark = delMark;
  pFileState->flushMark = -1;
5
54liuyao 已提交
74 75 76
  return pFileState;

_error:
5
54liuyao 已提交
77
  streamFileStateDestroy(pFileState);
5
54liuyao 已提交
78 79 80 81
  return NULL;
}

void destroyRowBuffPos(SRowBuffPos* pPos) {
5
54liuyao 已提交
82
  taosMemoryFreeClear(pPos->pKey);
5
54liuyao 已提交
83 84 85 86 87 88 89 90
  taosMemoryFreeClear(pPos->pRowBuff);
  taosMemoryFree(pPos);
}

void destroyRowBuffPosPtr(void* ptr) {
  if (!ptr) {
    return;
  }
5
54liuyao 已提交
91
  SRowBuffPos* pPos = *(SRowBuffPos**)ptr;
5
54liuyao 已提交
92 93 94
  destroyRowBuffPos(pPos);
}

5
54liuyao 已提交
95 96 97 98 99 100 101 102 103 104 105
void destroyRowBuff(void* ptr) {
  if (!ptr) {
    return;
  }
  taosMemoryFree(*(void**)ptr);
}

void streamFileStateDestroy(SStreamFileState* pFileState) {
  if (!pFileState) {
    return;
  }
5
54liuyao 已提交
106
  tdListFreeP(pFileState->usedBuffs, destroyRowBuffPosPtr);
5
54liuyao 已提交
107
  tdListFreeP(pFileState->freeBuffs, destroyRowBuff);
5
54liuyao 已提交
108
  tSimpleHashCleanup(pFileState->rowBuffMap);
5
54liuyao 已提交
109
  taosMemoryFree(pFileState);
5
54liuyao 已提交
110 111
}

5
54liuyao 已提交
112
void clearExpiredRowBuff(SStreamFileState* pFileState, TSKEY ts, bool all) {
5
54liuyao 已提交
113 114 115 116 117
  SListIter iter = {0};
  tdListInitIter(pFileState->usedBuffs, &iter, TD_LIST_FORWARD);

  SListNode* pNode = NULL;
  while ((pNode = tdListNext(&iter)) != NULL) {
5
54liuyao 已提交
118 119 120 121 122
    SRowBuffPos* pPos = *(SRowBuffPos**)(pNode->data);
    if (all || (pFileState->getTs(pPos->pKey) <ts) ) {
      tdListPopNode(pFileState->usedBuffs, pNode);
      taosMemoryFreeClear(pNode);
      tdListAppend(pFileState->freeBuffs, &(pPos->pRowBuff));
5
54liuyao 已提交
123
      pPos->pRowBuff = NULL;
5
54liuyao 已提交
124
      tSimpleHashRemove(pFileState->rowBuffMap, pPos->pKey, pFileState->keyLen);
5
54liuyao 已提交
125 126 127 128 129
      destroyRowBuffPos(pPos);
    }
  }
}

5
54liuyao 已提交
130 131 132 133 134
void streamFileStateClear(SStreamFileState* pFileState) {
  tSimpleHashClear(pFileState->rowBuffMap);
  clearExpiredRowBuff(pFileState, 0, true);
}

5
54liuyao 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
int32_t flushRowBuff(SStreamFileState* pFileState) {
  SStreamSnapshot* pFlushList = tdListNew(POINTER_BYTES);
  if (!pFlushList) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }
  uint64_t num = (uint64_t)(pFileState->curRowCount * FLUSH_RATIO);
  uint64_t i = 0;
  SListIter iter = {0};
  tdListInitIter(pFileState->usedBuffs, &iter, TD_LIST_FORWARD);

  SListNode* pNode = NULL;
  while ((pNode = tdListNext(&iter)) != NULL && i < num) {
    SRowBuffPos* pPos = *(SRowBuffPos**)pNode->data;
    if (!pPos->beUsed) {
      tdListAppend(pFlushList, &pPos);
5
54liuyao 已提交
150 151
      pFileState->flushMark = TMAX(pFileState->flushMark, pFileState->getTs(pPos->pKey));
      tSimpleHashRemove(pFileState->rowBuffMap, pPos->pKey, pFileState->keyLen);
5
54liuyao 已提交
152 153 154 155 156 157 158 159
      i++;
    }
  }
  flushSnapshot(pFileState->pFileStore, pFlushList, pFileState->rowSize);
  return TSDB_CODE_SUCCESS;
}

int32_t clearRowBuff(SStreamFileState* pFileState) {
5
54liuyao 已提交
160
  clearExpiredRowBuff(pFileState, pFileState->maxTs - pFileState->deleteMark, false);
5
54liuyao 已提交
161 162 163 164 165 166
  if (isListEmpty(pFileState->freeBuffs)) {
    return flushRowBuff(pFileState);
  }
  return TSDB_CODE_SUCCESS;
}

5
54liuyao 已提交
167
void* getFreeBuff(SList* lists, int32_t buffSize) {
5
54liuyao 已提交
168 169 170 171 172
  SListNode* pNode = tdListPopHead(lists);
  if (!pNode) {
    return NULL;
  }
  void* ptr = *(void**)pNode->data;
5
54liuyao 已提交
173
  memset(ptr, 0, buffSize);
5
54liuyao 已提交
174 175 176 177 178 179 180
  taosMemoryFree(pNode);
  return ptr;
}

SRowBuffPos* getNewRowPos(SStreamFileState* pFileState) {
  SRowBuffPos* pPos = taosMemoryCalloc(1, sizeof(SRowBuffPos));
  tdListAppend(pFileState->usedBuffs, &pPos);
5
54liuyao 已提交
181
  void* pBuff = getFreeBuff(pFileState->freeBuffs, pFileState->rowSize);
5
54liuyao 已提交
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
  if (pBuff) {
    pPos->pRowBuff = pBuff;
    return pPos;
  }

  if (pFileState->curRowCount < pFileState->maxRowCount) {
    pBuff = taosMemoryCalloc(1, pFileState->rowSize);
    if (pBuff) {
      pPos->pRowBuff = pBuff;
      pFileState->curRowCount++;
      return pPos;
    }
  }

  int32_t code = clearRowBuff(pFileState);
  ASSERT(code == 0);
5
54liuyao 已提交
198
  pPos->pRowBuff = getFreeBuff(pFileState->freeBuffs, pFileState->rowSize);
5
54liuyao 已提交
199 200 201 202
  return pPos;
}

int32_t getRowBuff(SStreamFileState* pFileState, void* pKey, int32_t keyLen, void** pVal, int32_t* pVLen) {
5
54liuyao 已提交
203
  pFileState->maxTs = TMAX(pFileState->maxTs, pFileState->getTs(pKey));
5
54liuyao 已提交
204 205
  SRowBuffPos** pos = tSimpleHashGet(pFileState->rowBuffMap, pKey, keyLen);
  if (pos) {
5
54liuyao 已提交
206 207 208 209
    if (pVal) {
      *pVLen = pFileState->rowSize;
      *pVal = *pos;
    }
5
54liuyao 已提交
210 211 212 213
    return TSDB_CODE_SUCCESS;
  }
  SRowBuffPos* pNewPos = getNewRowPos(pFileState);
  ASSERT(pNewPos);// todo(liuyao) delete
5
54liuyao 已提交
214 215 216 217 218 219 220 221 222 223 224 225
  pNewPos->pKey = taosMemoryCalloc(1, keyLen);
  memcpy(pNewPos->pKey, pKey, keyLen);

  TSKEY ts = pFileState->getTs(pKey);
  if (ts > pFileState->maxTs - pFileState->deleteMark && ts < pFileState->flushMark) {
    int32_t len = 0;
    void *pVal = NULL;
    streamStateGet_rocksdb(pFileState->pFileStore, pKey, pVal, &len);
    memcpy(pNewPos->pRowBuff, pVal, len);
    taosMemoryFree(pVal);
  }

5
54liuyao 已提交
226
  tSimpleHashPut(pFileState->rowBuffMap, pKey, keyLen, &pNewPos, POINTER_BYTES);
5
54liuyao 已提交
227 228 229 230
  if (pVal) {
    *pVLen = pFileState->rowSize;
    *pVal = pNewPos;
  }
5
54liuyao 已提交
231 232 233
  return TSDB_CODE_SUCCESS;
}

5
54liuyao 已提交
234 235 236 237 238 239 240
int32_t deleteRowBuff(SStreamFileState* pFileState, const void* pKey, int32_t keyLen) {
  int32_t code_buff = tSimpleHashRemove(pFileState->rowBuffMap, pKey, keyLen);
  int32_t code_rocks = streamStateDel_rocksdb(pFileState->pFileStore, pKey);
  return code_buff == TSDB_CODE_SUCCESS ? code_buff : code_rocks;
}

int32_t getRowBuffByPos(SStreamFileState* pFileState, SRowBuffPos* pPos, void** pVal) {
5
54liuyao 已提交
241
  if (pPos->pRowBuff) {
5
54liuyao 已提交
242 243
    (*pVal) = pPos->pRowBuff;
    return TSDB_CODE_SUCCESS;
5
54liuyao 已提交
244 245 246 247
  }

  int32_t code = clearRowBuff(pFileState);
  ASSERT(code == 0);
5
54liuyao 已提交
248
  pPos->pRowBuff = getFreeBuff(pFileState->freeBuffs, pFileState->rowSize);
5
54liuyao 已提交
249 250

  int32_t len = 0;
5
54liuyao 已提交
251
  streamStateGet_rocksdb(pFileState->pFileStore, pPos->pKey, pVal, &len);
5
54liuyao 已提交
252 253
  memcpy(pPos->pRowBuff, pVal, len);
  taosMemoryFree(pVal);
5
54liuyao 已提交
254 255 256 257 258 259 260 261 262 263
  (*pVal) = pPos->pRowBuff;
  return TSDB_CODE_SUCCESS;
}

bool hasRowBuff(SStreamFileState* pFileState, void* pKey, int32_t keyLen) {
  SRowBuffPos** pos = tSimpleHashGet(pFileState->rowBuffMap, pKey, keyLen);
  if (pos) {
    return true;
  }
  return false;
5
54liuyao 已提交
264 265 266 267 268 269 270
}

void releaseRowBuffPos(SRowBuffPos* pBuff) {
  pBuff->beUsed = false;
}

SStreamSnapshot* getSnapshot(SStreamFileState* pFileState) {
5
54liuyao 已提交
271
  clearExpiredRowBuff(pFileState, pFileState->maxTs - pFileState->deleteMark, false);
5
54liuyao 已提交
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
  return pFileState->usedBuffs;
}

int32_t flushSnapshot(void* pFile, SStreamSnapshot* pSnapshot, int32_t rowSize) {
  int32_t code = TSDB_CODE_SUCCESS;
  SListIter iter = {0};
  tdListInitIter(pSnapshot, &iter, TD_LIST_FORWARD);

  SListNode* pNode = NULL;
  while ((pNode = tdListNext(&iter)) != NULL && code == TSDB_CODE_SUCCESS) {
    SRowBuffPos* pPos = *(SRowBuffPos**)pNode->data;
    code = streamStatePut_rocksdb(pFile, pPos->pKey, pPos->pRowBuff, rowSize);
  }
  return code;
}

int32_t recoverSnapshot(SStreamFileState* pFileState) {
  return TSDB_CODE_SUCCESS;
}