mndCluster.c 8.1 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;
}

S
Shengliang Guan 已提交
82
static SSdbRaw *mndClusterActionEncode(SClusterObj *pCluster) {
83 84
  terrno = TSDB_CODE_OUT_OF_MEMORY;

85 86
  SSdbRaw *pRaw = sdbAllocRaw(SDB_CLUSTER, CLUSTER_VER_NUMBE, sizeof(SClusterObj) + CLUSTER_RESERVE_SIZE);
  if (pRaw == NULL) goto _OVER;
S
Shengliang Guan 已提交
87 88

  int32_t dataPos = 0;
89 90 91 92 93
  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)
94 95 96

  terrno = 0;

97
_OVER:
98 99 100 101 102
  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 已提交
103

104
  mTrace("cluster:%" PRId64 ", encode to raw:%p, row:%p", pCluster->id, pRaw, pCluster);
S
Shengliang Guan 已提交
105 106 107 108
  return pRaw;
}

static SSdbRow *mndClusterActionDecode(SSdbRaw *pRaw) {
109 110
  terrno = TSDB_CODE_OUT_OF_MEMORY;

S
Shengliang Guan 已提交
111
  int8_t sver = 0;
112
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto _OVER;
S
Shengliang Guan 已提交
113

114
  if (sver != CLUSTER_VER_NUMBE) {
S
Shengliang Guan 已提交
115
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
116
    goto _OVER;
S
Shengliang Guan 已提交
117 118
  }

119
  SSdbRow *pRow = sdbAllocRow(sizeof(SClusterObj));
120
  if (pRow == NULL) goto _OVER;
121

S
Shengliang Guan 已提交
122
  SClusterObj *pCluster = sdbGetRowObj(pRow);
123
  if (pCluster == NULL) goto _OVER;
S
Shengliang Guan 已提交
124 125

  int32_t dataPos = 0;
126 127 128 129 130
  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)
131 132 133

  terrno = 0;

134
_OVER:
135 136
  if (terrno != 0) {
    mError("cluster:%" PRId64 ", failed to decode from raw:%p since %s", pCluster->id, pRaw, terrstr());
wafwerar's avatar
wafwerar 已提交
137
    taosMemoryFreeClear(pRow);
138 139
    return NULL;
  }
S
Shengliang Guan 已提交
140

141
  mTrace("cluster:%" PRId64 ", decode from raw:%p, row:%p", pCluster->id, pRaw, pCluster);
S
Shengliang Guan 已提交
142 143 144 145
  return pRow;
}

static int32_t mndClusterActionInsert(SSdb *pSdb, SClusterObj *pCluster) {
146
  mTrace("cluster:%" PRId64 ", perform insert action, row:%p", pCluster->id, pCluster);
S
Shengliang Guan 已提交
147
  pSdb->pMnode->clusterId = pCluster->id;
S
Shengliang Guan 已提交
148 149 150 151
  return 0;
}

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

156
static int32_t mndClusterActionUpdate(SSdb *pSdb, SClusterObj *pOld, SClusterObj *pNew) {
S
Shengliang Guan 已提交
157
  mTrace("cluster:%" PRId64 ", perform update action, old row:%p new row:%p", pOld->id, pOld, pNew);
S
Shengliang Guan 已提交
158 159 160 161 162 163 164 165
  return 0;
}

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

H
Haojun Liao 已提交
166
  int32_t code = taosGetSystemUUID(clusterObj.name, TSDB_CLUSTER_ID_LEN);
S
Shengliang Guan 已提交
167
  if (code != 0) {
168
    strcpy(clusterObj.name, "tdengine3.0");
S
Shengliang Guan 已提交
169
    mError("failed to get name from system, set to default val %s", clusterObj.name);
S
Shengliang Guan 已提交
170
  }
171 172

  clusterObj.id = mndGenerateUid(clusterObj.name, TSDB_CLUSTER_ID_LEN);
173
  clusterObj.id = (clusterObj.id >= 0 ? clusterObj.id : -clusterObj.id);
S
Shengliang Guan 已提交
174
  pMnode->clusterId = clusterObj.id;
175
  mDebug("cluster:%" PRId64 ", name is %s", clusterObj.id, clusterObj.name);
S
Shengliang Guan 已提交
176 177 178 179 180

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

181
  mDebug("cluster:%" PRId64 ", will be created while deploy sdb, raw:%p", clusterObj.id, pRaw);
182
#if 0
S
Shengliang Guan 已提交
183
  return sdbWrite(pMnode->pSdb, pRaw);
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
#else
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_TYPE_CREATE_CLUSTER, NULL);
  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;
#endif
S
Shengliang Guan 已提交
208 209
}

S
Shengliang Guan 已提交
210 211
static int32_t mndRetrieveClusters(SRpcMsg *pMsg, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
  SMnode      *pMnode = pMsg->info.node;
S
Shengliang Guan 已提交
212 213 214 215
  SSdb        *pSdb = pMnode->pSdb;
  int32_t      numOfRows = 0;
  int32_t      cols = 0;
  SClusterObj *pCluster = NULL;
S
Shengliang Guan 已提交
216

S
Shengliang Guan 已提交
217 218 219
  while (numOfRows < rows) {
    pShow->pIter = sdbFetch(pSdb, SDB_CLUSTER, pShow->pIter, (void **)&pCluster);
    if (pShow->pIter == NULL) break;
S
Shengliang Guan 已提交
220

S
Shengliang Guan 已提交
221
    cols = 0;
222 223
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, (const char *)&pCluster->id, false);
S
Shengliang Guan 已提交
224

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

228 229
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
    colDataAppend(pColInfo, numOfRows, buf, false);
S
Shengliang Guan 已提交
230

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

S
Shengliang Guan 已提交
234 235 236
    sdbRelease(pSdb, pCluster);
    numOfRows++;
  }
S
Shengliang Guan 已提交
237

238
  pShow->numOfRows += numOfRows;
S
Shengliang Guan 已提交
239
  return numOfRows;
S
Shengliang Guan 已提交
240 241
}

S
Shengliang Guan 已提交
242 243 244
static void mndCancelGetNextCluster(SMnode *pMnode, void *pIter) {
  SSdb *pSdb = pMnode->pSdb;
  sdbCancelFetch(pSdb, pIter);
S
Shengliang Guan 已提交
245
}