mndCluster.c 11.7 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
23 24
char    tsVersionName[16] = "community";
int64_t tsExpireTime = 0;
S
Shengliang Guan 已提交
25

S
Shengliang Guan 已提交
26 27 28 29
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 已提交
30
static int32_t  mndClusterActionUpdate(SSdb *pSdb, SClusterObj *pOldCluster, SClusterObj *pNewCluster);
S
Shengliang Guan 已提交
31
static int32_t  mndCreateDefaultCluster(SMnode *pMnode);
S
Shengliang Guan 已提交
32
static int32_t  mndRetrieveClusters(SRpcMsg *pMsg, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
S
Shengliang Guan 已提交
33
static void     mndCancelGetNextCluster(SMnode *pMnode, void *pIter);
34
static int32_t  mndProcessUptimeTimer(SRpcMsg *pReq);
S
Shengliang Guan 已提交
35 36

int32_t mndInitCluster(SMnode *pMnode) {
37 38 39 40 41 42 43 44 45 46
  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 已提交
47

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

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

void mndCleanupCluster(SMnode *pMnode) {}

S
Shengliang Guan 已提交
57 58 59 60
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 已提交
61
  if (pCluster == NULL) {
S
Shengliang Guan 已提交
62 63 64 65 66 67 68 69
    return -1;
  }

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

“happyguoxy” 已提交
70
static SClusterObj *mndAcquireCluster(SMnode *pMnode, void **ppIter) {
71 72
  SSdb *pSdb = pMnode->pSdb;
  void *pIter = NULL;
S
shm  
Shengliang Guan 已提交
73 74 75 76 77 78

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

“happyguoxy” 已提交
79 80
    *ppIter = pIter;

81 82 83 84 85 86
    return pCluster;
  }

  return NULL;
}

“happyguoxy” 已提交
87
static void mndReleaseCluster(SMnode *pMnode, SClusterObj *pCluster, void *pIter) {
88
  SSdb *pSdb = pMnode->pSdb;
“happyguoxy” 已提交
89
  sdbCancelFetch(pSdb, pIter);
90 91 92 93 94
  sdbRelease(pSdb, pCluster);
}

int64_t mndGetClusterId(SMnode *pMnode) {
  int64_t      clusterId = 0;
“happyguoxy” 已提交
95 96
  void        *pIter = NULL;
  SClusterObj *pCluster = mndAcquireCluster(pMnode, &pIter);
97
  if (pCluster != NULL) {
S
shm  
Shengliang Guan 已提交
98
    clusterId = pCluster->id;
“happyguoxy” 已提交
99
    mndReleaseCluster(pMnode, pCluster, pIter);
S
shm  
Shengliang Guan 已提交
100 101 102 103 104
  }

  return clusterId;
}

C
Cary Xu 已提交
105
int64_t mndGetClusterCreateTime(SMnode *pMnode) {
106
  int64_t      createTime = 0;
“happyguoxy” 已提交
107 108
  void        *pIter = NULL;
  SClusterObj *pCluster = mndAcquireCluster(pMnode, &pIter);
109
  if (pCluster != NULL) {
C
Cary Xu 已提交
110
    createTime = pCluster->createdTime;
“happyguoxy” 已提交
111
    mndReleaseCluster(pMnode, pCluster, pIter);
C
Cary Xu 已提交
112 113 114 115 116
  }

  return createTime;
}

117 118 119 120 121 122 123 124 125 126 127 128
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;
“happyguoxy” 已提交
129 130
  void        *pIter = NULL;
  SClusterObj *pCluster = mndAcquireCluster(pMnode, &pIter);
131 132
  if (pCluster != NULL) {
    upTime = mndGetClusterUpTimeImp(pCluster);
“happyguoxy” 已提交
133
    mndReleaseCluster(pMnode, pCluster, pIter);
134 135 136 137 138
  }

  return upTime / 86400.0f;
}

S
Shengliang Guan 已提交
139
static SSdbRaw *mndClusterActionEncode(SClusterObj *pCluster) {
140 141
  terrno = TSDB_CODE_OUT_OF_MEMORY;

142 143
  SSdbRaw *pRaw = sdbAllocRaw(SDB_CLUSTER, CLUSTER_VER_NUMBE, sizeof(SClusterObj) + CLUSTER_RESERVE_SIZE);
  if (pRaw == NULL) goto _OVER;
S
Shengliang Guan 已提交
144 145

  int32_t dataPos = 0;
146 147 148 149
  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)
150
  SDB_SET_INT32(pRaw, dataPos, pCluster->upTime, _OVER)
151
  SDB_SET_RESERVE(pRaw, dataPos, CLUSTER_RESERVE_SIZE, _OVER)
152 153 154

  terrno = 0;

155
_OVER:
156 157 158 159 160
  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 已提交
161

162
  mTrace("cluster:%" PRId64 ", encode to raw:%p, row:%p", pCluster->id, pRaw, pCluster);
S
Shengliang Guan 已提交
163 164 165 166
  return pRaw;
}

static SSdbRow *mndClusterActionDecode(SSdbRaw *pRaw) {
167
  terrno = TSDB_CODE_OUT_OF_MEMORY;
168 169
  SClusterObj *pCluster = NULL;
  SSdbRow *pRow = NULL;
170

S
Shengliang Guan 已提交
171
  int8_t sver = 0;
172
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto _OVER;
S
Shengliang Guan 已提交
173

174
  if (sver != CLUSTER_VER_NUMBE) {
S
Shengliang Guan 已提交
175
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
176
    goto _OVER;
S
Shengliang Guan 已提交
177 178
  }

179
  pRow = sdbAllocRow(sizeof(SClusterObj));
180
  if (pRow == NULL) goto _OVER;
181

182
  pCluster = sdbGetRowObj(pRow);
183
  if (pCluster == NULL) goto _OVER;
S
Shengliang Guan 已提交
184 185

  int32_t dataPos = 0;
186 187 188 189
  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)
190
  SDB_GET_INT32(pRaw, dataPos, &pCluster->upTime, _OVER)
191
  SDB_GET_RESERVE(pRaw, dataPos, CLUSTER_RESERVE_SIZE, _OVER)
192 193 194

  terrno = 0;

195
_OVER:
196
  if (terrno != 0) {
197 198
    mError("cluster:%" PRId64 ", failed to decode from raw:%p since %s", pCluster == NULL ? 0 : pCluster->id, pRaw,
           terrstr());
wafwerar's avatar
wafwerar 已提交
199
    taosMemoryFreeClear(pRow);
200 201
    return NULL;
  }
S
Shengliang Guan 已提交
202

203
  mTrace("cluster:%" PRId64 ", decode from raw:%p, row:%p", pCluster->id, pRaw, pCluster);
S
Shengliang Guan 已提交
204 205 206 207
  return pRow;
}

static int32_t mndClusterActionInsert(SSdb *pSdb, SClusterObj *pCluster) {
208
  mTrace("cluster:%" PRId64 ", perform insert action, row:%p", pCluster->id, pCluster);
S
Shengliang Guan 已提交
209
  pSdb->pMnode->clusterId = pCluster->id;
210
  pCluster->updateTime = taosGetTimestampMs();
S
Shengliang Guan 已提交
211 212 213 214
  return 0;
}

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

219
static int32_t mndClusterActionUpdate(SSdb *pSdb, SClusterObj *pOld, SClusterObj *pNew) {
220 221 222 223
  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 已提交
224 225 226 227 228 229 230 231
  return 0;
}

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

H
Haojun Liao 已提交
232
  int32_t code = taosGetSystemUUID(clusterObj.name, TSDB_CLUSTER_ID_LEN);
S
Shengliang Guan 已提交
233
  if (code != 0) {
234
    strcpy(clusterObj.name, "tdengine3.0");
S
Shengliang Guan 已提交
235
    mError("failed to get name from system, set to default val %s", clusterObj.name);
S
Shengliang Guan 已提交
236
  }
237 238

  clusterObj.id = mndGenerateUid(clusterObj.name, TSDB_CLUSTER_ID_LEN);
239
  clusterObj.id = (clusterObj.id >= 0 ? clusterObj.id : -clusterObj.id);
S
Shengliang Guan 已提交
240
  pMnode->clusterId = clusterObj.id;
241
  mInfo("cluster:%" PRId64 ", name is %s", clusterObj.id, clusterObj.name);
S
Shengliang Guan 已提交
242 243 244

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

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

249
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_NOTHING, NULL, "create-cluster");
250
  if (pTrans == NULL) {
S
Shengliang Guan 已提交
251
    sdbFreeRaw(pRaw);
252 253 254
    mError("cluster:%" PRId64 ", failed to create since %s", clusterObj.id, terrstr());
    return -1;
  }
255
  mInfo("trans:%d, used to create cluster:%" PRId64, pTrans->id, clusterObj.id);
256 257 258 259 260 261

  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 已提交
262
  (void)sdbSetRawStatus(pRaw, SDB_STATUS_READY);
263 264 265 266 267 268 269 270 271

  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 已提交
272 273
}

S
Shengliang Guan 已提交
274 275
static int32_t mndRetrieveClusters(SRpcMsg *pMsg, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
  SMnode      *pMnode = pMsg->info.node;
S
Shengliang Guan 已提交
276 277 278 279
  SSdb        *pSdb = pMnode->pSdb;
  int32_t      numOfRows = 0;
  int32_t      cols = 0;
  SClusterObj *pCluster = NULL;
S
Shengliang Guan 已提交
280

S
Shengliang Guan 已提交
281 282 283
  while (numOfRows < rows) {
    pShow->pIter = sdbFetch(pSdb, SDB_CLUSTER, pShow->pIter, (void **)&pCluster);
    if (pShow->pIter == NULL) break;
S
Shengliang Guan 已提交
284

S
Shengliang Guan 已提交
285
    cols = 0;
286
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
287
    colDataSetVal(pColInfo, numOfRows, (const char *)&pCluster->id, false);
S
Shengliang Guan 已提交
288

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

292
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
293
    colDataSetVal(pColInfo, numOfRows, buf, false);
S
Shengliang Guan 已提交
294

295 296
    int32_t upTime = mndGetClusterUpTimeImp(pCluster);
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
297
    colDataSetVal(pColInfo, numOfRows, (const char *)&upTime, false);
298

299
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
300
    colDataSetVal(pColInfo, numOfRows, (const char *)&pCluster->createdTime, false);
S
Shengliang Guan 已提交
301

302 303 304
    char ver[12] = {0};
    STR_WITH_MAXSIZE_TO_VARSTR(ver, tsVersionName, pShow->pMeta->pSchemas[cols].bytes);
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
305
    colDataSetVal(pColInfo, numOfRows, (const char *)ver, false);
306 307 308

    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    if (tsExpireTime <= 0) {
309
      colDataSetNULL(pColInfo, numOfRows);
310
    } else {
311
      colDataSetVal(pColInfo, numOfRows, (const char *)&tsExpireTime, false);
312 313
    }

S
Shengliang Guan 已提交
314 315 316
    sdbRelease(pSdb, pCluster);
    numOfRows++;
  }
S
Shengliang Guan 已提交
317

318
  pShow->numOfRows += numOfRows;
S
Shengliang Guan 已提交
319
  return numOfRows;
S
Shengliang Guan 已提交
320 321
}

S
Shengliang Guan 已提交
322 323 324
static void mndCancelGetNextCluster(SMnode *pMnode, void *pIter) {
  SSdb *pSdb = pMnode->pSdb;
  sdbCancelFetch(pSdb, pIter);
S
Shengliang Guan 已提交
325
}
326 327 328 329

static int32_t mndProcessUptimeTimer(SRpcMsg *pReq) {
  SMnode      *pMnode = pReq->info.node;
  SClusterObj  clusterObj = {0};
“happyguoxy” 已提交
330 331
  void        *pIter = NULL;
  SClusterObj *pCluster = mndAcquireCluster(pMnode, &pIter);
332 333 334
  if (pCluster != NULL) {
    memcpy(&clusterObj, pCluster, sizeof(SClusterObj));
    clusterObj.upTime += tsUptimeInterval;
“happyguoxy” 已提交
335
    mndReleaseCluster(pMnode, pCluster, pIter);
336 337 338 339 340 341 342
  }

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

S
Shengliang Guan 已提交
343
  mInfo("update cluster uptime to %d", clusterObj.upTime);
344
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, pReq, "update-uptime");
345 346 347 348 349 350 351 352
  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 已提交
353
  (void)sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY);
354 355 356 357 358 359 360 361 362 363

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

  mndTransDrop(pTrans);
  return 0;
}