You need to sign in or sign up before continuing.
streamMeta.c 7.1 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 21 22 23 24 25

SStreamMeta* streamMetaOpen(const char* path, void* ahandle, FTaskExpand expandFunc) {
  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 29
  sprintf(streamPath, "%s/%s", path, "stream");
  pMeta->path = strdup(streamPath);
L
Liu Jicong 已提交
30 31 32 33
  if (tdbOpen(pMeta->path, 16 * 1024, 1, &pMeta->db) < 0) {
    goto _err;
  }

L
Liu Jicong 已提交
34 35 36
  sprintf(streamPath, "%s/%s", pMeta->path, "checkpoints");
  mkdir(streamPath, 0755);
  taosMemoryFree(streamPath);
37

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

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

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

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

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

58
  return pMeta;
L
Liu Jicong 已提交
59

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

void streamMetaClose(SStreamMeta* pMeta) {
71 72
  tdbCommit(pMeta->db, &pMeta->txn);
  tdbTbClose(pMeta->pTaskDb);
73
  tdbTbClose(pMeta->pCheckpointDb);
74
  tdbClose(pMeta->db);
L
Liu Jicong 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87

  void* pIter = NULL;
  while (1) {
    pIter = taosHashIterate(pMeta->pTasks, pIter);
    if (pIter == NULL) break;
    SStreamTask* pTask = *(SStreamTask**)pIter;
    tFreeSStreamTask(pTask);
  }
  taosHashCleanup(pMeta->pTasks);
  taosMemoryFree(pMeta->path);
  taosMemoryFree(pMeta);
}

88
int32_t streamMetaAddSerializedTask(SStreamMeta* pMeta, int64_t startVer, char* msg, int32_t msgLen) {
L
Liu Jicong 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
  SStreamTask* pTask = taosMemoryCalloc(1, sizeof(SStreamTask));
  if (pTask == NULL) {
    return -1;
  }
  SDecoder decoder;
  tDecoderInit(&decoder, (uint8_t*)msg, msgLen);
  if (tDecodeSStreamTask(&decoder, pTask) < 0) {
    ASSERT(0);
    goto FAIL;
  }
  tDecoderClear(&decoder);

  if (pMeta->expandFunc(pMeta->ahandle, pTask) < 0) {
    ASSERT(0);
    goto FAIL;
  }

L
Liu Jicong 已提交
106 107 108
  if (taosHashPut(pMeta->pTasks, &pTask->taskId, sizeof(int32_t), &pTask, sizeof(void*)) < 0) {
    goto FAIL;
  }
L
Liu Jicong 已提交
109 110

  if (tdbTbUpsert(pMeta->pTaskDb, &pTask->taskId, sizeof(int32_t), msg, msgLen, &pMeta->txn) < 0) {
L
Liu Jicong 已提交
111
    taosHashRemove(pMeta->pTasks, &pTask->taskId, sizeof(int32_t));
L
Liu Jicong 已提交
112
    ASSERT(0);
L
Liu Jicong 已提交
113
    goto FAIL;
L
Liu Jicong 已提交
114 115 116 117
  }
  return 0;

FAIL:
L
Liu Jicong 已提交
118
  if (pTask) tFreeSStreamTask(pTask);
L
Liu Jicong 已提交
119
  return -1;
L
Liu Jicong 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
}

int32_t streamMetaAddTask(SStreamMeta* pMeta, SStreamTask* pTask) {
  void* buf = NULL;
  if (pMeta->expandFunc(pMeta->ahandle, pTask) < 0) {
    return -1;
  }
  taosHashPut(pMeta->pTasks, &pTask->taskId, sizeof(int32_t), &pTask, sizeof(void*));

  int32_t len;
  int32_t code;
  tEncodeSize(tEncodeSStreamTask, pTask, len, code);
  if (code < 0) {
    return -1;
  }
  buf = taosMemoryCalloc(1, sizeof(len));
  if (buf == NULL) {
    return -1;
  }

  SEncoder encoder;
  tEncoderInit(&encoder, buf, len);
  tEncodeSStreamTask(&encoder, pTask);

  if (tdbTbUpsert(pMeta->pTaskDb, &pTask->taskId, sizeof(int32_t), buf, len, &pMeta->txn) < 0) {
    ASSERT(0);
    return -1;
  }

  return 0;
}

L
Liu Jicong 已提交
152 153 154 155 156 157 158 159 160 161
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;
  }
}

L
Liu Jicong 已提交
162 163 164 165 166 167 168
int32_t streamMetaRemoveTask(SStreamMeta* pMeta, int32_t taskId) {
  SStreamTask** ppTask = (SStreamTask**)taosHashGet(pMeta->pTasks, &taskId, sizeof(int32_t));
  if (ppTask) {
    SStreamTask* pTask = *ppTask;
    taosHashRemove(pMeta->pTasks, &taskId, sizeof(int32_t));
    atomic_store_8(&pTask->taskStatus, TASK_STATUS__DROPPING);

L
Liu Jicong 已提交
169 170 171 172
    if (tdbTbDelete(pMeta->pTaskDb, &taskId, sizeof(int32_t), &pMeta->txn) < 0) {
      /*return -1;*/
    }

L
Liu Jicong 已提交
173 174 175 176
    if (pTask->triggerParam != 0) {
      taosTmrStop(pTask->timer);
    }

L
Liu Jicong 已提交
177 178 179 180 181 182
    while (1) {
      int8_t schedStatus =
          atomic_val_compare_exchange_8(&pTask->schedStatus, TASK_SCHED_STATUS__INACTIVE, TASK_SCHED_STATUS__DROPPING);
      if (schedStatus == TASK_SCHED_STATUS__INACTIVE) {
        tFreeSStreamTask(pTask);
        break;
L
Liu Jicong 已提交
183 184
      } else if (schedStatus == TASK_SCHED_STATUS__DROPPING) {
        break;
L
Liu Jicong 已提交
185
      }
L
Liu Jicong 已提交
186
      taosMsleep(10);
L
Liu Jicong 已提交
187
    }
L
Liu Jicong 已提交
188
  }
L
Liu Jicong 已提交
189

L
Liu Jicong 已提交
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
  return 0;
}

int32_t streamMetaBegin(SStreamMeta* pMeta) {
  if (tdbTxnOpen(&pMeta->txn, 0, tdbDefaultMalloc, tdbDefaultFree, NULL, TDB_TXN_WRITE | TDB_TXN_READ_UNCOMMITTED) <
      0) {
    return -1;
  }

  if (tdbBegin(pMeta->db, &pMeta->txn) < 0) {
    return -1;
  }
  return 0;
}

int32_t streamMetaCommit(SStreamMeta* pMeta) {
  if (tdbCommit(pMeta->db, &pMeta->txn) < 0) {
    return -1;
  }
209 210 211 212 213 214 215 216
  memset(&pMeta->txn, 0, sizeof(TXN));
  if (tdbTxnOpen(&pMeta->txn, 0, tdbDefaultMalloc, tdbDefaultFree, NULL, TDB_TXN_WRITE | TDB_TXN_READ_UNCOMMITTED) <
      0) {
    return -1;
  }
  if (tdbBegin(pMeta->db, &pMeta->txn) < 0) {
    return -1;
  }
L
Liu Jicong 已提交
217 218 219
  return 0;
}

220 221 222 223 224 225 226 227 228 229 230 231
int32_t streamMetaAbort(SStreamMeta* pMeta) {
  if (tdbAbort(pMeta->db, &pMeta->txn) < 0) {
    return -1;
  }
  memset(&pMeta->txn, 0, sizeof(TXN));
  if (tdbTxnOpen(&pMeta->txn, 0, tdbDefaultMalloc, tdbDefaultFree, NULL, TDB_TXN_WRITE | TDB_TXN_READ_UNCOMMITTED) <
      0) {
    return -1;
  }
  if (tdbBegin(pMeta->db, &pMeta->txn) < 0) {
    return -1;
  }
L
Liu Jicong 已提交
232 233
  return 0;
}
234

L
Liu Jicong 已提交
235
int32_t streamLoadTasks(SStreamMeta* pMeta) {
L
Liu Jicong 已提交
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
  TBC* pCur = NULL;
  if (tdbTbcOpen(pMeta->pTaskDb, &pCur, NULL) < 0) {
    ASSERT(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) {
      return -1;
    }
    tDecoderInit(&decoder, (uint8_t*)pVal, vLen);
    tDecodeSStreamTask(&decoder, pTask);
    tDecoderClear(&decoder);
258 259 260 261 262 263 264 265 266 267 268 269

    if (pMeta->expandFunc(pMeta->ahandle, pTask) < 0) {
      return -1;
    }

    if (taosHashPut(pMeta->pTasks, &pTask->taskId, sizeof(int32_t), &pTask, sizeof(void*)) < 0) {
      return -1;
    }
  }

  if (tdbTbcClose(pCur) < 0) {
    return -1;
L
Liu Jicong 已提交
270 271 272 273
  }

  return 0;
}