streamMeta.c 8.3 KB
Newer Older
L
Liu Jicong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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 "executor.h"
17
#include "streamInc.h"
L
Liu Jicong 已提交
18
#include "ttimer.h"
L
Liu Jicong 已提交
19

20
SStreamMeta* streamMetaOpen(const char* path, void* ahandle, FTaskExpand expandFunc, int32_t vgId) {
L
Liu Jicong 已提交
21 22 23 24 25
  SStreamMeta* pMeta = taosMemoryCalloc(1, sizeof(SStreamMeta));
  if (pMeta == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }
L
Liu Jicong 已提交
26 27
  int32_t len = strlen(path) + 20;
  char*   streamPath = taosMemoryCalloc(1, len);
28
  sprintf(streamPath, "%s/%s", path, "stream");
29
  pMeta->path = taosStrdup(streamPath);
30
  if (tdbOpen(pMeta->path, 16 * 1024, 1, &pMeta->db, 0) < 0) {
L
Liu Jicong 已提交
31
    taosMemoryFree(streamPath);
L
Liu Jicong 已提交
32 33 34
    goto _err;
  }

L
Liu Jicong 已提交
35
  sprintf(streamPath, "%s/%s", pMeta->path, "checkpoints");
wafwerar's avatar
wafwerar 已提交
36
  taosMulModeMkDir(streamPath, 0755);
L
Liu Jicong 已提交
37
  taosMemoryFree(streamPath);
38

39
  if (tdbTbOpen("task.db", sizeof(int32_t), -1, NULL, pMeta->db, &pMeta->pTaskDb, 0) < 0) {
L
Liu Jicong 已提交
40 41 42
    goto _err;
  }

43
  if (tdbTbOpen("checkpoint.db", sizeof(int32_t), -1, NULL, pMeta->db, &pMeta->pCheckpointDb, 0) < 0) {
L
Liu Jicong 已提交
44 45 46
    goto _err;
  }

L
Liu Jicong 已提交
47
  pMeta->pTasks = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK);
L
Liu Jicong 已提交
48 49 50 51 52 53 54 55
  if (pMeta->pTasks == NULL) {
    goto _err;
  }

  if (streamMetaBegin(pMeta) < 0) {
    goto _err;
  }

L
Liu Jicong 已提交
56 57
  pMeta->ahandle = ahandle;
  pMeta->expandFunc = expandFunc;
L
Liu Jicong 已提交
58

59
  return pMeta;
L
Liu Jicong 已提交
60

L
Liu Jicong 已提交
61
_err:
L
Liu Jicong 已提交
62
  taosMemoryFree(pMeta->path);
L
Liu Jicong 已提交
63 64
  if (pMeta->pTasks) taosHashCleanup(pMeta->pTasks);
  if (pMeta->pTaskDb) tdbTbClose(pMeta->pTaskDb);
65
  if (pMeta->pCheckpointDb) tdbTbClose(pMeta->pCheckpointDb);
L
Liu Jicong 已提交
66 67
  if (pMeta->db) tdbClose(pMeta->db);
  taosMemoryFree(pMeta);
L
Liu Jicong 已提交
68 69 70 71
  return NULL;
}

void streamMetaClose(SStreamMeta* pMeta) {
72
  tdbAbort(pMeta->db, pMeta->txn);
73
  tdbTbClose(pMeta->pTaskDb);
74
  tdbTbClose(pMeta->pCheckpointDb);
75
  tdbClose(pMeta->db);
L
Liu Jicong 已提交
76 77 78 79 80 81

  void* pIter = NULL;
  while (1) {
    pIter = taosHashIterate(pMeta->pTasks, pIter);
    if (pIter == NULL) break;
    SStreamTask* pTask = *(SStreamTask**)pIter;
L
Liu Jicong 已提交
82 83 84 85
    if (pTask->timer) {
      taosTmrStop(pTask->timer);
      pTask->timer = NULL;
    }
L
Liu Jicong 已提交
86
    tFreeSStreamTask(pTask);
L
Liu Jicong 已提交
87
    /*streamMetaReleaseTask(pMeta, pTask);*/
L
Liu Jicong 已提交
88 89
  }
  taosHashCleanup(pMeta->pTasks);
90
  taosHashCleanup(pMeta->pRecoverStatus);
L
Liu Jicong 已提交
91 92 93 94
  taosMemoryFree(pMeta->path);
  taosMemoryFree(pMeta);
}

95 96
#if 0
int32_t streamMetaAddSerializedTask(SStreamMeta* pMeta, int64_t ver, char* msg, int32_t msgLen) {
L
Liu Jicong 已提交
97 98 99 100 101 102 103
  SStreamTask* pTask = taosMemoryCalloc(1, sizeof(SStreamTask));
  if (pTask == NULL) {
    return -1;
  }
  SDecoder decoder;
  tDecoderInit(&decoder, (uint8_t*)msg, msgLen);
  if (tDecodeSStreamTask(&decoder, pTask) < 0) {
L
Liu Jicong 已提交
104
    tDecoderClear(&decoder);
L
Liu Jicong 已提交
105 106 107 108
    goto FAIL;
  }
  tDecoderClear(&decoder);

109
  if (pMeta->expandFunc(pMeta->ahandle, pTask, ver) < 0) {
L
Liu Jicong 已提交
110 111 112 113
    ASSERT(0);
    goto FAIL;
  }

L
Liu Jicong 已提交
114 115 116
  if (taosHashPut(pMeta->pTasks, &pTask->taskId, sizeof(int32_t), &pTask, sizeof(void*)) < 0) {
    goto FAIL;
  }
L
Liu Jicong 已提交
117

118
  if (tdbTbUpsert(pMeta->pTaskDb, &pTask->taskId, sizeof(int32_t), msg, msgLen, pMeta->txn) < 0) {
L
Liu Jicong 已提交
119
    taosHashRemove(pMeta->pTasks, &pTask->taskId, sizeof(int32_t));
L
Liu Jicong 已提交
120
    ASSERT(0);
L
Liu Jicong 已提交
121
    goto FAIL;
L
Liu Jicong 已提交
122
  }
L
Liu Jicong 已提交
123

L
Liu Jicong 已提交
124 125 126
  return 0;

FAIL:
L
Liu Jicong 已提交
127
  if (pTask) tFreeSStreamTask(pTask);
L
Liu Jicong 已提交
128
  return -1;
L
Liu Jicong 已提交
129
}
130
#endif
L
Liu Jicong 已提交
131

L
Liu Jicong 已提交
132 133
int32_t streamMetaSaveTask(SStreamMeta* pMeta, SStreamTask* pTask) {
  void*   buf = NULL;
L
Liu Jicong 已提交
134 135 136 137 138 139
  int32_t len;
  int32_t code;
  tEncodeSize(tEncodeSStreamTask, pTask, len, code);
  if (code < 0) {
    return -1;
  }
L
Liu Jicong 已提交
140
  buf = taosMemoryCalloc(1, len);
L
Liu Jicong 已提交
141 142 143 144
  if (buf == NULL) {
    return -1;
  }

145
  SEncoder encoder = {0};
L
Liu Jicong 已提交
146 147
  tEncoderInit(&encoder, buf, len);
  tEncodeSStreamTask(&encoder, pTask);
148
  tEncoderClear(&encoder);
L
Liu Jicong 已提交
149

150
  if (tdbTbUpsert(pMeta->pTaskDb, &pTask->taskId, sizeof(int32_t), buf, len, pMeta->txn) < 0) {
L
Liu Jicong 已提交
151 152 153
    return -1;
  }

L
Liu Jicong 已提交
154
  taosMemoryFree(buf);
L
Liu Jicong 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167
  return 0;
}

#if 1
int32_t streamMetaAddTask(SStreamMeta* pMeta, int64_t ver, SStreamTask* pTask) {
  if (pMeta->expandFunc(pMeta->ahandle, pTask, ver) < 0) {
    return -1;
  }

  if (streamMetaSaveTask(pMeta, pTask) < 0) {
    return -1;
  }

168 169
  taosHashPut(pMeta->pTasks, &pTask->taskId, sizeof(int32_t), &pTask, sizeof(void*));

L
Liu Jicong 已提交
170 171
  return 0;
}
L
Liu Jicong 已提交
172
#endif
L
Liu Jicong 已提交
173

5
54liuyao 已提交
174
#if 0
L
Liu Jicong 已提交
175 176 177 178 179 180 181 182 183
SStreamTask* streamMetaGetTask(SStreamMeta* pMeta, int32_t taskId) {
  SStreamTask** ppTask = (SStreamTask**)taosHashGet(pMeta->pTasks, &taskId, sizeof(int32_t));
  if (ppTask) {
    ASSERT((*ppTask)->taskId == taskId);
    return *ppTask;
  } else {
    return NULL;
  }
}
5
54liuyao 已提交
184
#endif
L
Liu Jicong 已提交
185

L
Liu Jicong 已提交
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
SStreamTask* streamMetaAcquireTask(SStreamMeta* pMeta, int32_t taskId) {
  taosRLockLatch(&pMeta->lock);

  SStreamTask** ppTask = (SStreamTask**)taosHashGet(pMeta->pTasks, &taskId, sizeof(int32_t));
  if (ppTask) {
    SStreamTask* pTask = *ppTask;
    if (atomic_load_8(&pTask->taskStatus) != TASK_STATUS__DROPPING) {
      atomic_add_fetch_32(&pTask->refCnt, 1);
      taosRUnLockLatch(&pMeta->lock);
      return pTask;
    } else {
      taosRUnLockLatch(&pMeta->lock);
      return NULL;
    }
  }
  taosRUnLockLatch(&pMeta->lock);
  return NULL;
}

void streamMetaReleaseTask(SStreamMeta* pMeta, SStreamTask* pTask) {
  int32_t left = atomic_sub_fetch_32(&pTask->refCnt, 1);
  ASSERT(left >= 0);
  if (left == 0) {
    ASSERT(atomic_load_8(&pTask->taskStatus) == TASK_STATUS__DROPPING);
    tFreeSStreamTask(pTask);
  }
}

214
void streamMetaRemoveTask(SStreamMeta* pMeta, int32_t taskId) {
L
Liu Jicong 已提交
215 216 217 218
  SStreamTask** ppTask = (SStreamTask**)taosHashGet(pMeta->pTasks, &taskId, sizeof(int32_t));
  if (ppTask) {
    SStreamTask* pTask = *ppTask;
    taosHashRemove(pMeta->pTasks, &taskId, sizeof(int32_t));
L
Liu Jicong 已提交
219
    tdbTbDelete(pMeta->pTaskDb, &taskId, sizeof(int32_t), pMeta->txn);
L
Liu Jicong 已提交
220 221 222 223
    /*if (pTask->timer) {
     * taosTmrStop(pTask->timer);*/
    /*pTask->timer = NULL;*/
    /*}*/
L
Liu Jicong 已提交
224 225 226 227 228 229 230 231
    atomic_store_8(&pTask->taskStatus, TASK_STATUS__DROPPING);

    taosWLockLatch(&pMeta->lock);
    streamMetaReleaseTask(pMeta, pTask);
    taosWUnLockLatch(&pMeta->lock);
  }
}

L
Liu Jicong 已提交
232
int32_t streamMetaBegin(SStreamMeta* pMeta) {
233 234
  if (tdbBegin(pMeta->db, &pMeta->txn, tdbDefaultMalloc, tdbDefaultFree, NULL,
               TDB_TXN_WRITE | TDB_TXN_READ_UNCOMMITTED) < 0) {
L
Liu Jicong 已提交
235 236 237 238 239 240
    return -1;
  }
  return 0;
}

int32_t streamMetaCommit(SStreamMeta* pMeta) {
241
  if (tdbCommit(pMeta->db, pMeta->txn) < 0) {
L
Liu Jicong 已提交
242 243
    return -1;
  }
244
  if (tdbPostCommit(pMeta->db, pMeta->txn) < 0) {
245 246
    return -1;
  }
247 248 249

  if (tdbBegin(pMeta->db, &pMeta->txn, tdbDefaultMalloc, tdbDefaultFree, NULL,
               TDB_TXN_WRITE | TDB_TXN_READ_UNCOMMITTED) < 0) {
250 251
    return -1;
  }
L
Liu Jicong 已提交
252 253 254
  return 0;
}

255
int32_t streamMetaAbort(SStreamMeta* pMeta) {
256
  if (tdbAbort(pMeta->db, pMeta->txn) < 0) {
257 258
    return -1;
  }
259 260 261

  if (tdbBegin(pMeta->db, &pMeta->txn, tdbDefaultMalloc, tdbDefaultFree, NULL,
               TDB_TXN_WRITE | TDB_TXN_READ_UNCOMMITTED) < 0) {
262 263
    return -1;
  }
L
Liu Jicong 已提交
264 265
  return 0;
}
266

L
Liu Jicong 已提交
267
int32_t streamLoadTasks(SStreamMeta* pMeta, int64_t ver) {
L
Liu Jicong 已提交
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
  TBC* pCur = NULL;
  if (tdbTbcOpen(pMeta->pTaskDb, &pCur, NULL) < 0) {
    return -1;
  }

  void*    pKey = NULL;
  int32_t  kLen = 0;
  void*    pVal = NULL;
  int32_t  vLen = 0;
  SDecoder decoder;

  tdbTbcMoveToFirst(pCur);

  while (tdbTbcNext(pCur, &pKey, &kLen, &pVal, &vLen) == 0) {
    SStreamTask* pTask = taosMemoryCalloc(1, sizeof(SStreamTask));
    if (pTask == NULL) {
L
Liu Jicong 已提交
284 285
      tdbFree(pKey);
      tdbFree(pVal);
5
54liuyao 已提交
286
      tdbTbcClose(pCur);
L
Liu Jicong 已提交
287 288 289 290 291
      return -1;
    }
    tDecoderInit(&decoder, (uint8_t*)pVal, vLen);
    tDecodeSStreamTask(&decoder, pTask);
    tDecoderClear(&decoder);
292

293
    if (pMeta->expandFunc(pMeta->ahandle, pTask, -1) < 0) {
L
Liu Jicong 已提交
294 295
      tdbFree(pKey);
      tdbFree(pVal);
5
54liuyao 已提交
296
      tdbTbcClose(pCur);
297 298 299 300
      return -1;
    }

    if (taosHashPut(pMeta->pTasks, &pTask->taskId, sizeof(int32_t), &pTask, sizeof(void*)) < 0) {
L
Liu Jicong 已提交
301 302
      tdbFree(pKey);
      tdbFree(pVal);
5
54liuyao 已提交
303
      tdbTbcClose(pCur);
304 305
      return -1;
    }
L
Liu Jicong 已提交
306 307 308 309 310
    /*pTask->taskStatus = TASK_STATUS__NORMAL;*/
    if (pTask->fillHistory) {
      pTask->taskStatus = TASK_STATUS__WAIT_DOWNSTREAM;
      streamTaskCheckDownstream(pTask, ver);
    }
311 312
  }

313 314
  tdbFree(pKey);
  tdbFree(pVal);
315 316
  if (tdbTbcClose(pCur) < 0) {
    return -1;
L
Liu Jicong 已提交
317 318 319 320
  }

  return 0;
}