mndCluster.c 8.5 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"
S
Shengliang Guan 已提交
19

S
Shengliang Guan 已提交
20 21
#define TSDB_CLUSTER_VER_NUMBE 1
#define TSDB_CLUSTER_RESERVE_SIZE 64
S
Shengliang Guan 已提交
22

S
Shengliang Guan 已提交
23 24 25 26
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 已提交
27
static int32_t  mndClusterActionUpdate(SSdb *pSdb, SClusterObj *pOldCluster, SClusterObj *pNewCluster);
S
Shengliang Guan 已提交
28
static int32_t  mndCreateDefaultCluster(SMnode *pMnode);
S
Shengliang Guan 已提交
29 30
static int32_t  mndGetClusterMeta(SNodeMsg *pMsg, SShowObj *pShow, STableMetaRsp *pMeta);
static int32_t  mndRetrieveClusters(SNodeMsg *pMsg, SShowObj *pShow, char *data, int32_t rows);
S
Shengliang Guan 已提交
31 32 33 34
static void     mndCancelGetNextCluster(SMnode *pMnode, void *pIter);

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

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

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

S
Shengliang Guan 已提交
64
static SSdbRaw *mndClusterActionEncode(SClusterObj *pCluster) {
65 66
  terrno = TSDB_CODE_OUT_OF_MEMORY;

S
Shengliang Guan 已提交
67
  SSdbRaw *pRaw = sdbAllocRaw(SDB_CLUSTER, TSDB_CLUSTER_VER_NUMBE, sizeof(SClusterObj) + TSDB_CLUSTER_RESERVE_SIZE);
68
  if (pRaw == NULL) goto CLUSTER_ENCODE_OVER;
S
Shengliang Guan 已提交
69 70

  int32_t dataPos = 0;
71 72 73 74 75 76 77 78 79 80 81 82 83 84
  SDB_SET_INT64(pRaw, dataPos, pCluster->id, CLUSTER_ENCODE_OVER)
  SDB_SET_INT64(pRaw, dataPos, pCluster->createdTime, CLUSTER_ENCODE_OVER)
  SDB_SET_INT64(pRaw, dataPos, pCluster->updateTime, CLUSTER_ENCODE_OVER)
  SDB_SET_BINARY(pRaw, dataPos, pCluster->name, TSDB_CLUSTER_ID_LEN, CLUSTER_ENCODE_OVER)
  SDB_SET_RESERVE(pRaw, dataPos, TSDB_CLUSTER_RESERVE_SIZE, CLUSTER_ENCODE_OVER)

  terrno = 0;

CLUSTER_ENCODE_OVER:
  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 已提交
85

86
  mTrace("cluster:%" PRId64 ", encode to raw:%p, row:%p", pCluster->id, pRaw, pCluster);
S
Shengliang Guan 已提交
87 88 89 90
  return pRaw;
}

static SSdbRow *mndClusterActionDecode(SSdbRaw *pRaw) {
91 92
  terrno = TSDB_CODE_OUT_OF_MEMORY;

S
Shengliang Guan 已提交
93
  int8_t sver = 0;
94
  if (sdbGetRawSoftVer(pRaw, &sver) != 0) goto CLUSTER_DECODE_OVER;
S
Shengliang Guan 已提交
95

S
Shengliang Guan 已提交
96
  if (sver != TSDB_CLUSTER_VER_NUMBE) {
S
Shengliang Guan 已提交
97
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
98
    goto CLUSTER_DECODE_OVER;
S
Shengliang Guan 已提交
99 100
  }

101 102 103
  SSdbRow *pRow = sdbAllocRow(sizeof(SClusterObj));
  if (pRow == NULL) goto CLUSTER_DECODE_OVER;

S
Shengliang Guan 已提交
104
  SClusterObj *pCluster = sdbGetRowObj(pRow);
105
  if (pCluster == NULL) goto CLUSTER_DECODE_OVER;
S
Shengliang Guan 已提交
106 107

  int32_t dataPos = 0;
108 109 110 111 112 113 114 115 116 117 118
  SDB_GET_INT64(pRaw, dataPos, &pCluster->id, CLUSTER_DECODE_OVER)
  SDB_GET_INT64(pRaw, dataPos, &pCluster->createdTime, CLUSTER_DECODE_OVER)
  SDB_GET_INT64(pRaw, dataPos, &pCluster->updateTime, CLUSTER_DECODE_OVER)
  SDB_GET_BINARY(pRaw, dataPos, pCluster->name, TSDB_CLUSTER_ID_LEN, CLUSTER_DECODE_OVER)
  SDB_GET_RESERVE(pRaw, dataPos, TSDB_CLUSTER_RESERVE_SIZE, CLUSTER_DECODE_OVER)

  terrno = 0;

CLUSTER_DECODE_OVER:
  if (terrno != 0) {
    mError("cluster:%" PRId64 ", failed to decode from raw:%p since %s", pCluster->id, pRaw, terrstr());
wafwerar's avatar
wafwerar 已提交
119
    taosMemoryFreeClear(pRow);
120 121
    return NULL;
  }
S
Shengliang Guan 已提交
122

123
  mTrace("cluster:%" PRId64 ", decode from raw:%p, row:%p", pCluster->id, pRaw, pCluster);
S
Shengliang Guan 已提交
124 125 126 127
  return pRow;
}

static int32_t mndClusterActionInsert(SSdb *pSdb, SClusterObj *pCluster) {
128
  mTrace("cluster:%" PRId64 ", perform insert action, row:%p", pCluster->id, pCluster);
S
Shengliang Guan 已提交
129 130 131 132
  return 0;
}

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

137
static int32_t mndClusterActionUpdate(SSdb *pSdb, SClusterObj *pOld, SClusterObj *pNew) {
S
Shengliang Guan 已提交
138
  mTrace("cluster:%" PRId64 ", perform update action, old row:%p new row:%p", pOld->id, pOld, pNew);
S
Shengliang Guan 已提交
139 140 141 142 143 144 145 146
  return 0;
}

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

H
Haojun Liao 已提交
147
  int32_t code = taosGetSystemUUID(clusterObj.name, TSDB_CLUSTER_ID_LEN);
S
Shengliang Guan 已提交
148
  if (code != 0) {
S
Shengliang Guan 已提交
149 150
    strcpy(clusterObj.name, "tdengine2.0");
    mError("failed to get name from system, set to default val %s", clusterObj.name);
S
Shengliang Guan 已提交
151
  }
152 153

  clusterObj.id = mndGenerateUid(clusterObj.name, TSDB_CLUSTER_ID_LEN);
154
  clusterObj.id = (clusterObj.id >= 0 ? clusterObj.id : -clusterObj.id);
S
Shengliang Guan 已提交
155
  pMnode->clusterId = clusterObj.id;
156
  mDebug("cluster:%" PRId64 ", name is %s", clusterObj.id, clusterObj.name);
S
Shengliang Guan 已提交
157 158 159 160 161

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

162
  mDebug("cluster:%" PRId64 ", will be created while deploy sdb, raw:%p", clusterObj.id, pRaw);
S
Shengliang Guan 已提交
163 164 165
  return sdbWrite(pMnode->pSdb, pRaw);
}

S
Shengliang Guan 已提交
166
static int32_t mndGetClusterMeta(SNodeMsg *pMsg, SShowObj *pShow, STableMetaRsp *pMeta) {
S
Shengliang Guan 已提交
167
  int32_t  cols = 0;
S
Shengliang Guan 已提交
168
  SSchema *pSchema = pMeta->pSchemas;
S
Shengliang Guan 已提交
169

170 171
  pShow->bytes[cols] = 8;
  pSchema[cols].type = TSDB_DATA_TYPE_BIGINT;
S
Shengliang Guan 已提交
172
  strcpy(pSchema[cols].name, "id");
S
Shengliang Guan 已提交
173
  pSchema[cols].bytes = pShow->bytes[cols];
S
Shengliang Guan 已提交
174 175 176 177 178
  cols++;

  pShow->bytes[cols] = TSDB_CLUSTER_ID_LEN + VARSTR_HEADER_SIZE;
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
  strcpy(pSchema[cols].name, "name");
S
Shengliang Guan 已提交
179
  pSchema[cols].bytes = pShow->bytes[cols];
S
Shengliang Guan 已提交
180 181 182 183 184
  cols++;

  pShow->bytes[cols] = 8;
  pSchema[cols].type = TSDB_DATA_TYPE_TIMESTAMP;
  strcpy(pSchema[cols].name, "create_time");
S
Shengliang Guan 已提交
185
  pSchema[cols].bytes = pShow->bytes[cols];
S
Shengliang Guan 已提交
186 187
  cols++;

S
Shengliang Guan 已提交
188
  pMeta->numOfColumns = cols;
D
dapan1121 已提交
189
  strcpy(pMeta->tbName, mndShowStr(pShow->type));
S
Shengliang Guan 已提交
190 191 192 193 194 195
  pShow->numOfColumns = cols;

  pShow->offset[0] = 0;
  for (int32_t i = 1; i < cols; ++i) {
    pShow->offset[i] = pShow->offset[i - 1] + pShow->bytes[i - 1];
  }
S
Shengliang Guan 已提交
196

S
Shengliang Guan 已提交
197 198
  pShow->numOfRows = 1;
  pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1];
D
dapan1121 已提交
199
  strcpy(pMeta->tbName, mndShowStr(pShow->type));
S
Shengliang Guan 已提交
200

S
Shengliang Guan 已提交
201 202
  return 0;
}
S
Shengliang Guan 已提交
203

S
Shengliang Guan 已提交
204 205
static int32_t mndRetrieveClusters(SNodeMsg *pMsg, SShowObj *pShow, char *data, int32_t rows) {
  SMnode      *pMnode = pMsg->pNode;
S
Shengliang Guan 已提交
206 207 208 209 210
  SSdb        *pSdb = pMnode->pSdb;
  int32_t      numOfRows = 0;
  int32_t      cols = 0;
  char        *pWrite;
  SClusterObj *pCluster = NULL;
S
Shengliang Guan 已提交
211

S
Shengliang Guan 已提交
212 213 214
  while (numOfRows < rows) {
    pShow->pIter = sdbFetch(pSdb, SDB_CLUSTER, pShow->pIter, (void **)&pCluster);
    if (pShow->pIter == NULL) break;
S
Shengliang Guan 已提交
215

S
Shengliang Guan 已提交
216
    cols = 0;
S
Shengliang Guan 已提交
217

S
Shengliang Guan 已提交
218
    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
219
    *(int64_t *)pWrite = pCluster->id;
S
Shengliang Guan 已提交
220
    cols++;
S
Shengliang Guan 已提交
221

S
Shengliang Guan 已提交
222 223 224
    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    STR_WITH_MAXSIZE_TO_VARSTR(pWrite, pCluster->name, TSDB_CLUSTER_ID_LEN);
    cols++;
S
Shengliang Guan 已提交
225

S
Shengliang Guan 已提交
226 227 228
    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    *(int64_t *)pWrite = pCluster->createdTime;
    cols++;
S
Shengliang Guan 已提交
229

S
Shengliang Guan 已提交
230 231 232
    sdbRelease(pSdb, pCluster);
    numOfRows++;
  }
S
Shengliang Guan 已提交
233

S
Shengliang Guan 已提交
234
  mndVacuumResult(data, pShow->numOfColumns, numOfRows, rows, pShow);
S
Shengliang Guan 已提交
235 236
  pShow->numOfReads += numOfRows;
  return numOfRows;
S
Shengliang Guan 已提交
237 238
}

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