mndCluster.c 8.4 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 22
#define CLUSTER_VER_NUMBE    1
#define CLUSTER_RESERVE_SIZE 64
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 32 33
static void     mndCancelGetNextCluster(SMnode *pMnode, void *pIter);

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

  mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_CLUSTER, mndRetrieveClusters);
  mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_CLUSTER, mndCancelGetNextCluster);
  return sdbSetTable(pMnode->pSdb, table);
}

void mndCleanupCluster(SMnode *pMnode) {}

S
Shengliang Guan 已提交
52 53 54 55
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 已提交
56
  if (pCluster == NULL) {
S
Shengliang Guan 已提交
57 58 59 60 61 62 63 64
    return -1;
  }

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

S
shm  
Shengliang Guan 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
int64_t mndGetClusterId(SMnode *pMnode) {
  SSdb   *pSdb = pMnode->pSdb;
  void   *pIter = NULL;
  int64_t clusterId = -1;

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

    clusterId = pCluster->id;
    sdbRelease(pSdb, pCluster);
  }

  return clusterId;
}

C
Cary Xu 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
int64_t mndGetClusterCreateTime(SMnode *pMnode) {
  SSdb   *pSdb = pMnode->pSdb;
  void   *pIter = NULL;
  int64_t createTime = INT64_MAX;

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

    createTime = pCluster->createdTime;
    sdbRelease(pSdb, pCluster);
  }

  return createTime;
}

S
Shengliang Guan 已提交
99
static SSdbRaw *mndClusterActionEncode(SClusterObj *pCluster) {
100 101
  terrno = TSDB_CODE_OUT_OF_MEMORY;

102 103
  SSdbRaw *pRaw = sdbAllocRaw(SDB_CLUSTER, CLUSTER_VER_NUMBE, sizeof(SClusterObj) + CLUSTER_RESERVE_SIZE);
  if (pRaw == NULL) goto _OVER;
S
Shengliang Guan 已提交
104 105

  int32_t dataPos = 0;
106 107 108 109 110
  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)
  SDB_SET_RESERVE(pRaw, dataPos, CLUSTER_RESERVE_SIZE, _OVER)
111 112 113

  terrno = 0;

114
_OVER:
115 116 117 118 119
  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 已提交
120

121
  mTrace("cluster:%" PRId64 ", encode to raw:%p, row:%p", pCluster->id, pRaw, pCluster);
S
Shengliang Guan 已提交
122 123 124 125
  return pRaw;
}

static SSdbRow *mndClusterActionDecode(SSdbRaw *pRaw) {
126 127
  terrno = TSDB_CODE_OUT_OF_MEMORY;

S
Shengliang Guan 已提交
128
  int8_t sver = 0;
129
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto _OVER;
S
Shengliang Guan 已提交
130

131
  if (sver != CLUSTER_VER_NUMBE) {
S
Shengliang Guan 已提交
132
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
133
    goto _OVER;
S
Shengliang Guan 已提交
134 135
  }

136
  SSdbRow *pRow = sdbAllocRow(sizeof(SClusterObj));
137
  if (pRow == NULL) goto _OVER;
138

S
Shengliang Guan 已提交
139
  SClusterObj *pCluster = sdbGetRowObj(pRow);
140
  if (pCluster == NULL) goto _OVER;
S
Shengliang Guan 已提交
141 142

  int32_t dataPos = 0;
143 144 145 146 147
  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)
  SDB_GET_RESERVE(pRaw, dataPos, CLUSTER_RESERVE_SIZE, _OVER)
148 149 150

  terrno = 0;

151
_OVER:
152 153
  if (terrno != 0) {
    mError("cluster:%" PRId64 ", failed to decode from raw:%p since %s", pCluster->id, pRaw, terrstr());
wafwerar's avatar
wafwerar 已提交
154
    taosMemoryFreeClear(pRow);
155 156
    return NULL;
  }
S
Shengliang Guan 已提交
157

158
  mTrace("cluster:%" PRId64 ", decode from raw:%p, row:%p", pCluster->id, pRaw, pCluster);
S
Shengliang Guan 已提交
159 160 161 162
  return pRow;
}

static int32_t mndClusterActionInsert(SSdb *pSdb, SClusterObj *pCluster) {
163
  mTrace("cluster:%" PRId64 ", perform insert action, row:%p", pCluster->id, pCluster);
S
Shengliang Guan 已提交
164
  pSdb->pMnode->clusterId = pCluster->id;
S
Shengliang Guan 已提交
165 166 167 168
  return 0;
}

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

173
static int32_t mndClusterActionUpdate(SSdb *pSdb, SClusterObj *pOld, SClusterObj *pNew) {
S
Shengliang Guan 已提交
174
  mTrace("cluster:%" PRId64 ", perform update action, old row:%p new row:%p", pOld->id, pOld, pNew);
S
Shengliang Guan 已提交
175 176 177 178 179 180 181 182
  return 0;
}

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

H
Haojun Liao 已提交
183
  int32_t code = taosGetSystemUUID(clusterObj.name, TSDB_CLUSTER_ID_LEN);
S
Shengliang Guan 已提交
184
  if (code != 0) {
185
    strcpy(clusterObj.name, "tdengine3.0");
S
Shengliang Guan 已提交
186
    mError("failed to get name from system, set to default val %s", clusterObj.name);
S
Shengliang Guan 已提交
187
  }
188 189

  clusterObj.id = mndGenerateUid(clusterObj.name, TSDB_CLUSTER_ID_LEN);
190
  clusterObj.id = (clusterObj.id >= 0 ? clusterObj.id : -clusterObj.id);
S
Shengliang Guan 已提交
191
  pMnode->clusterId = clusterObj.id;
192
  mInfo("cluster:%" PRId64 ", name is %s", clusterObj.id, clusterObj.name);
S
Shengliang Guan 已提交
193 194 195 196 197

  SSdbRaw *pRaw = mndClusterActionEncode(&clusterObj);
  if (pRaw == NULL) return -1;
  sdbSetRawStatus(pRaw, SDB_STATUS_READY);

198
  mDebug("cluster:%" PRId64 ", will be created when deploying, raw:%p", clusterObj.id, pRaw);
199 200

  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_NOTHING, NULL);
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
  if (pTrans == NULL) {
    mError("cluster:%" PRId64 ", failed to create since %s", clusterObj.id, terrstr());
    return -1;
  }
  mDebug("trans:%d, used to create cluster:%" PRId64, pTrans->id, clusterObj.id);

  if (mndTransAppendCommitlog(pTrans, pRaw) != 0) {
    mError("trans:%d, failed to commit redo log since %s", pTrans->id, terrstr());
    mndTransDrop(pTrans);
    return -1;
  }
  sdbSetRawStatus(pRaw, SDB_STATUS_READY);

  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 已提交
222 223
}

S
Shengliang Guan 已提交
224 225
static int32_t mndRetrieveClusters(SRpcMsg *pMsg, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
  SMnode      *pMnode = pMsg->info.node;
S
Shengliang Guan 已提交
226 227 228 229
  SSdb        *pSdb = pMnode->pSdb;
  int32_t      numOfRows = 0;
  int32_t      cols = 0;
  SClusterObj *pCluster = NULL;
S
Shengliang Guan 已提交
230

S
Shengliang Guan 已提交
231 232 233
  while (numOfRows < rows) {
    pShow->pIter = sdbFetch(pSdb, SDB_CLUSTER, pShow->pIter, (void **)&pCluster);
    if (pShow->pIter == NULL) break;
S
Shengliang Guan 已提交
234

S
Shengliang Guan 已提交
235
    cols = 0;
236 237
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char *)&pCluster->id, false);
S
Shengliang Guan 已提交
238

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

242 243
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, buf, false);
S
Shengliang Guan 已提交
244

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

S
Shengliang Guan 已提交
248 249 250
    sdbRelease(pSdb, pCluster);
    numOfRows++;
  }
S
Shengliang Guan 已提交
251

252
  pShow->numOfRows += numOfRows;
S
Shengliang Guan 已提交
253
  return numOfRows;
S
Shengliang Guan 已提交
254 255
}

S
Shengliang Guan 已提交
256 257 258
static void mndCancelGetNextCluster(SMnode *pMnode, void *pIter) {
  SSdb *pSdb = pMnode->pSdb;
  sdbCancelFetch(pSdb, pIter);
S
Shengliang Guan 已提交
259
}