mndCluster.c 11.0 KB
Newer Older
H
refact  
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

S
Shengliang Guan 已提交
16
#define _DEFAULT_SOURCE
S
Shengliang Guan 已提交
17
#include "mndCluster.h"
S
Shengliang Guan 已提交
18
#include "mndShow.h"
19
#include "mndTrans.h"
S
Shengliang Guan 已提交
20

21
#define CLUSTER_VER_NUMBE    1
22
#define CLUSTER_RESERVE_SIZE 60
S
Shengliang Guan 已提交
23

S
Shengliang Guan 已提交
24 25 26 27
static SSdbRaw *mndClusterActionEncode(SClusterObj *pCluster);
static SSdbRow *mndClusterActionDecode(SSdbRaw *pRaw);
static int32_t  mndClusterActionInsert(SSdb *pSdb, SClusterObj *pCluster);
static int32_t  mndClusterActionDelete(SSdb *pSdb, SClusterObj *pCluster);
S
Shengliang Guan 已提交
28
static int32_t  mndClusterActionUpdate(SSdb *pSdb, SClusterObj *pOldCluster, SClusterObj *pNewCluster);
S
Shengliang Guan 已提交
29
static int32_t  mndCreateDefaultCluster(SMnode *pMnode);
S
Shengliang Guan 已提交
30
static int32_t  mndRetrieveClusters(SRpcMsg *pMsg, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
S
Shengliang Guan 已提交
31
static void     mndCancelGetNextCluster(SMnode *pMnode, void *pIter);
32
static int32_t  mndProcessUptimeTimer(SRpcMsg *pReq);
S
Shengliang Guan 已提交
33 34

int32_t mndInitCluster(SMnode *pMnode) {
35 36 37 38 39 40 41 42 43 44
  SSdbTable table = {
      .sdbType = SDB_CLUSTER,
      .keyType = SDB_KEY_INT64,
      .deployFp = (SdbDeployFp)mndCreateDefaultCluster,
      .encodeFp = (SdbEncodeFp)mndClusterActionEncode,
      .decodeFp = (SdbDecodeFp)mndClusterActionDecode,
      .insertFp = (SdbInsertFp)mndClusterActionInsert,
      .updateFp = (SdbUpdateFp)mndClusterActionUpdate,
      .deleteFp = (SdbDeleteFp)mndClusterActionDelete,
  };
S
Shengliang Guan 已提交
45

46
  mndSetMsgHandle(pMnode, TDMT_MND_UPTIME_TIMER, mndProcessUptimeTimer);
S
Shengliang Guan 已提交
47 48
  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_CLUSTER, mndRetrieveClusters);
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_CLUSTER, mndCancelGetNextCluster);
49

S
Shengliang Guan 已提交
50 51 52 53 54
  return sdbSetTable(pMnode->pSdb, table);
}

void mndCleanupCluster(SMnode *pMnode) {}

S
Shengliang Guan 已提交
55 56 57 58
int32_t mndGetClusterName(SMnode *pMnode, char *clusterName, int32_t len) {
  SSdb *pSdb = pMnode->pSdb;

  SClusterObj *pCluster = sdbAcquire(pSdb, SDB_CLUSTER, &pMnode->clusterId);
S
Shengliang Guan 已提交
59
  if (pCluster == NULL) {
S
Shengliang Guan 已提交
60 61 62 63 64 65 66 67
    return -1;
  }

  tstrncpy(clusterName, pCluster->name, len);
  sdbRelease(pSdb, pCluster);
  return 0;
}

68 69 70
static SClusterObj *mndAcquireCluster(SMnode *pMnode) {
  SSdb *pSdb = pMnode->pSdb;
  void *pIter = NULL;
S
shm  
Shengliang Guan 已提交
71 72 73 74 75 76

  while (1) {
    SClusterObj *pCluster = NULL;
    pIter = sdbFetch(pSdb, SDB_CLUSTER, pIter, (void **)&pCluster);
    if (pIter == NULL) break;

77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
    return pCluster;
  }

  return NULL;
}

static void mndReleaseCluster(SMnode *pMnode, SClusterObj *pCluster) {
  SSdb *pSdb = pMnode->pSdb;
  sdbRelease(pSdb, pCluster);
}

int64_t mndGetClusterId(SMnode *pMnode) {
  int64_t      clusterId = 0;
  SClusterObj *pCluster = mndAcquireCluster(pMnode);
  if (pCluster != NULL) {
S
shm  
Shengliang Guan 已提交
92
    clusterId = pCluster->id;
93
    mndReleaseCluster(pMnode, pCluster);
S
shm  
Shengliang Guan 已提交
94 95 96 97 98
  }

  return clusterId;
}

C
Cary Xu 已提交
99
int64_t mndGetClusterCreateTime(SMnode *pMnode) {
100 101 102
  int64_t      createTime = 0;
  SClusterObj *pCluster = mndAcquireCluster(pMnode);
  if (pCluster != NULL) {
C
Cary Xu 已提交
103
    createTime = pCluster->createdTime;
104
    mndReleaseCluster(pMnode, pCluster);
C
Cary Xu 已提交
105 106 107 108 109
  }

  return createTime;
}

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
static int32_t mndGetClusterUpTimeImp(SClusterObj *pCluster) {
#if 0
  int32_t upTime = taosGetTimestampSec() - pCluster->updateTime / 1000;
  upTime = upTime + pCluster->upTime;
  return upTime;
#else
  return pCluster->upTime;
#endif
}

float mndGetClusterUpTime(SMnode *pMnode) {
  int64_t      upTime = 0;
  SClusterObj *pCluster = mndAcquireCluster(pMnode);
  if (pCluster != NULL) {
    upTime = mndGetClusterUpTimeImp(pCluster);
    mndReleaseCluster(pMnode, pCluster);
  }

  return upTime / 86400.0f;
}

S
Shengliang Guan 已提交
131
static SSdbRaw *mndClusterActionEncode(SClusterObj *pCluster) {
132 133
  terrno = TSDB_CODE_OUT_OF_MEMORY;

134 135
  SSdbRaw *pRaw = sdbAllocRaw(SDB_CLUSTER, CLUSTER_VER_NUMBE, sizeof(SClusterObj) + CLUSTER_RESERVE_SIZE);
  if (pRaw == NULL) goto _OVER;
S
Shengliang Guan 已提交
136 137

  int32_t dataPos = 0;
138 139 140 141
  SDB_SET_INT64(pRaw, dataPos, pCluster->id, _OVER)
  SDB_SET_INT64(pRaw, dataPos, pCluster->createdTime, _OVER)
  SDB_SET_INT64(pRaw, dataPos, pCluster->updateTime, _OVER)
  SDB_SET_BINARY(pRaw, dataPos, pCluster->name, TSDB_CLUSTER_ID_LEN, _OVER)
142
  SDB_SET_INT32(pRaw, dataPos, pCluster->upTime, _OVER)
143
  SDB_SET_RESERVE(pRaw, dataPos, CLUSTER_RESERVE_SIZE, _OVER)
144 145 146

  terrno = 0;

147
_OVER:
148 149 150 151 152
  if (terrno != 0) {
    mError("cluster:%" PRId64 ", failed to encode to raw:%p since %s", pCluster->id, pRaw, terrstr());
    sdbFreeRaw(pRaw);
    return NULL;
  }
S
Shengliang Guan 已提交
153

154
  mTrace("cluster:%" PRId64 ", encode to raw:%p, row:%p", pCluster->id, pRaw, pCluster);
S
Shengliang Guan 已提交
155 156 157 158
  return pRaw;
}

static SSdbRow *mndClusterActionDecode(SSdbRaw *pRaw) {
159
  terrno = TSDB_CODE_OUT_OF_MEMORY;
160 161
  SClusterObj *pCluster = NULL;
  SSdbRow *pRow = NULL;
162

S
Shengliang Guan 已提交
163
  int8_t sver = 0;
164
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto _OVER;
S
Shengliang Guan 已提交
165

166
  if (sver != CLUSTER_VER_NUMBE) {
S
Shengliang Guan 已提交
167
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
168
    goto _OVER;
S
Shengliang Guan 已提交
169 170
  }

171
  pRow = sdbAllocRow(sizeof(SClusterObj));
172
  if (pRow == NULL) goto _OVER;
173

174
  pCluster = sdbGetRowObj(pRow);
175
  if (pCluster == NULL) goto _OVER;
S
Shengliang Guan 已提交
176 177

  int32_t dataPos = 0;
178 179 180 181
  SDB_GET_INT64(pRaw, dataPos, &pCluster->id, _OVER)
  SDB_GET_INT64(pRaw, dataPos, &pCluster->createdTime, _OVER)
  SDB_GET_INT64(pRaw, dataPos, &pCluster->updateTime, _OVER)
  SDB_GET_BINARY(pRaw, dataPos, pCluster->name, TSDB_CLUSTER_ID_LEN, _OVER)
182
  SDB_GET_INT32(pRaw, dataPos, &pCluster->upTime, _OVER)
183
  SDB_GET_RESERVE(pRaw, dataPos, CLUSTER_RESERVE_SIZE, _OVER)
184 185 186

  terrno = 0;

187
_OVER:
188
  if (terrno != 0) {
189 190
    mError("cluster:%" PRId64 ", failed to decode from raw:%p since %s", pCluster == NULL ? 0 : pCluster->id, pRaw,
           terrstr());
wafwerar's avatar
wafwerar 已提交
191
    taosMemoryFreeClear(pRow);
192 193
    return NULL;
  }
S
Shengliang Guan 已提交
194

195
  mTrace("cluster:%" PRId64 ", decode from raw:%p, row:%p", pCluster->id, pRaw, pCluster);
S
Shengliang Guan 已提交
196 197 198 199
  return pRow;
}

static int32_t mndClusterActionInsert(SSdb *pSdb, SClusterObj *pCluster) {
200
  mTrace("cluster:%" PRId64 ", perform insert action, row:%p", pCluster->id, pCluster);
S
Shengliang Guan 已提交
201
  pSdb->pMnode->clusterId = pCluster->id;
202
  pCluster->updateTime = taosGetTimestampMs();
S
Shengliang Guan 已提交
203 204 205 206
  return 0;
}

static int32_t mndClusterActionDelete(SSdb *pSdb, SClusterObj *pCluster) {
207
  mTrace("cluster:%" PRId64 ", perform delete action, row:%p", pCluster->id, pCluster);
S
Shengliang Guan 已提交
208 209 210
  return 0;
}

211
static int32_t mndClusterActionUpdate(SSdb *pSdb, SClusterObj *pOld, SClusterObj *pNew) {
212 213 214 215
  mTrace("cluster:%" PRId64 ", perform update action, old row:%p new row:%p, uptime from %d to %d", pOld->id, pOld,
         pNew, pOld->upTime, pNew->upTime);
  pOld->upTime = pNew->upTime;
  pOld->updateTime = taosGetTimestampMs();
S
Shengliang Guan 已提交
216 217 218 219 220 221 222 223
  return 0;
}

static int32_t mndCreateDefaultCluster(SMnode *pMnode) {
  SClusterObj clusterObj = {0};
  clusterObj.createdTime = taosGetTimestampMs();
  clusterObj.updateTime = clusterObj.createdTime;

H
Haojun Liao 已提交
224
  int32_t code = taosGetSystemUUID(clusterObj.name, TSDB_CLUSTER_ID_LEN);
S
Shengliang Guan 已提交
225
  if (code != 0) {
226
    strcpy(clusterObj.name, "tdengine3.0");
S
Shengliang Guan 已提交
227
    mError("failed to get name from system, set to default val %s", clusterObj.name);
S
Shengliang Guan 已提交
228
  }
229 230

  clusterObj.id = mndGenerateUid(clusterObj.name, TSDB_CLUSTER_ID_LEN);
231
  clusterObj.id = (clusterObj.id >= 0 ? clusterObj.id : -clusterObj.id);
S
Shengliang Guan 已提交
232
  pMnode->clusterId = clusterObj.id;
233
  mInfo("cluster:%" PRId64 ", name is %s", clusterObj.id, clusterObj.name);
S
Shengliang Guan 已提交
234 235 236

  SSdbRaw *pRaw = mndClusterActionEncode(&clusterObj);
  if (pRaw == NULL) return -1;
S
Shengliang Guan 已提交
237
  (void)sdbSetRawStatus(pRaw, SDB_STATUS_READY);
S
Shengliang Guan 已提交
238

239
  mInfo("cluster:%" PRId64 ", will be created when deploying, raw:%p", clusterObj.id, pRaw);
240

241
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_NOTHING, NULL, "create-cluster");
242
  if (pTrans == NULL) {
S
Shengliang Guan 已提交
243
    sdbFreeRaw(pRaw);
244 245 246
    mError("cluster:%" PRId64 ", failed to create since %s", clusterObj.id, terrstr());
    return -1;
  }
247
  mInfo("trans:%d, used to create cluster:%" PRId64, pTrans->id, clusterObj.id);
248 249 250 251 252 253

  if (mndTransAppendCommitlog(pTrans, pRaw) != 0) {
    mError("trans:%d, failed to commit redo log since %s", pTrans->id, terrstr());
    mndTransDrop(pTrans);
    return -1;
  }
S
Shengliang Guan 已提交
254
  (void)sdbSetRawStatus(pRaw, SDB_STATUS_READY);
255 256 257 258 259 260 261 262 263

  if (mndTransPrepare(pMnode, pTrans) != 0) {
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
    mndTransDrop(pTrans);
    return -1;
  }

  mndTransDrop(pTrans);
  return 0;
S
Shengliang Guan 已提交
264 265
}

S
Shengliang Guan 已提交
266 267
static int32_t mndRetrieveClusters(SRpcMsg *pMsg, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
  SMnode      *pMnode = pMsg->info.node;
S
Shengliang Guan 已提交
268 269 270 271
  SSdb        *pSdb = pMnode->pSdb;
  int32_t      numOfRows = 0;
  int32_t      cols = 0;
  SClusterObj *pCluster = NULL;
S
Shengliang Guan 已提交
272

S
Shengliang Guan 已提交
273 274 275
  while (numOfRows < rows) {
    pShow->pIter = sdbFetch(pSdb, SDB_CLUSTER, pShow->pIter, (void **)&pCluster);
    if (pShow->pIter == NULL) break;
S
Shengliang Guan 已提交
276

S
Shengliang Guan 已提交
277
    cols = 0;
278 279
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char *)&pCluster->id, false);
S
Shengliang Guan 已提交
280

281
    char buf[tListLen(pCluster->name) + VARSTR_HEADER_SIZE] = {0};
282
    STR_WITH_MAXSIZE_TO_VARSTR(buf, pCluster->name, pShow->pMeta->pSchemas[cols].bytes);
S
Shengliang Guan 已提交
283

284 285
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, buf, false);
S
Shengliang Guan 已提交
286

287 288 289 290
    int32_t upTime = mndGetClusterUpTimeImp(pCluster);
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char *)&upTime, false);

291
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
292
    colDataAppend(pColInfo, numOfRows, (const char *)&pCluster->createdTime, false);
S
Shengliang Guan 已提交
293

S
Shengliang Guan 已提交
294 295 296
    sdbRelease(pSdb, pCluster);
    numOfRows++;
  }
S
Shengliang Guan 已提交
297

298
  pShow->numOfRows += numOfRows;
S
Shengliang Guan 已提交
299
  return numOfRows;
S
Shengliang Guan 已提交
300 301
}

S
Shengliang Guan 已提交
302 303 304
static void mndCancelGetNextCluster(SMnode *pMnode, void *pIter) {
  SSdb *pSdb = pMnode->pSdb;
  sdbCancelFetch(pSdb, pIter);
S
Shengliang Guan 已提交
305
}
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321

static int32_t mndProcessUptimeTimer(SRpcMsg *pReq) {
  SMnode      *pMnode = pReq->info.node;
  SClusterObj  clusterObj = {0};
  SClusterObj *pCluster = mndAcquireCluster(pMnode);
  if (pCluster != NULL) {
    memcpy(&clusterObj, pCluster, sizeof(SClusterObj));
    clusterObj.upTime += tsUptimeInterval;
    mndReleaseCluster(pMnode, pCluster);
  }

  if (clusterObj.id <= 0) {
    mError("can't get cluster info while update uptime");
    return 0;
  }

S
Shengliang Guan 已提交
322
  mInfo("update cluster uptime to %d", clusterObj.upTime);
323
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, pReq, "update-uptime");
324 325 326 327 328 329 330 331
  if (pTrans == NULL) return -1;

  SSdbRaw *pCommitRaw = mndClusterActionEncode(&clusterObj);
  if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) {
    mError("trans:%d, failed to append commit log since %s", pTrans->id, terrstr());
    mndTransDrop(pTrans);
    return -1;
  }
S
Shengliang Guan 已提交
332
  (void)sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
333 334 335 336 337 338 339 340 341 342

  if (mndTransPrepare(pMnode, pTrans) != 0) {
    mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr());
    mndTransDrop(pTrans);
    return -1;
  }

  mndTransDrop(pTrans);
  return 0;
}