mgmtVgroup.c 21.4 KB
Newer Older
H
hzcheng 已提交
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
slguan 已提交
16
#define _DEFAULT_SOURCE
17
#include "os.h"
S
slguan 已提交
18 19 20
#include "taoserror.h"
#include "tlog.h"
#include "tstatus.h"
S
slguan 已提交
21
#include "mnode.h"
S
slguan 已提交
22 23
#include "mgmtBalance.h"
#include "mgmtDb.h"
S
slguan 已提交
24
#include "mgmtDClient.h"
S
slguan 已提交
25
#include "mgmtDnode.h"
26
#include "mgmtProfile.h"
S
slguan 已提交
27
#include "mgmtSdb.h"
S
slguan 已提交
28
#include "mgmtShell.h"
S
#1177  
slguan 已提交
29
#include "mgmtTable.h"
S
slguan 已提交
30
#include "mgmtVgroup.h"
H
hzcheng 已提交
31

S
slguan 已提交
32 33 34
static void *tsVgroupSdb = NULL;
static int32_t tsVgUpdateSize = 0;

H
hjxilinx 已提交
35
static int32_t mgmtGetVgroupMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn);
S
slguan 已提交
36
static int32_t mgmtRetrieveVgroups(SShowObj *pShow, char *data, int32_t rows, void *pConn);
37
static void    mgmtProcessCreateVnodeRsp(SRpcMsg *rpcMsg);
38
static void    mgmtProcessDropVnodeRsp(SRpcMsg *rpcMsg);
39

40 41
static void mgmtSendDropVgroupMsg(SVgObj *pVgroup, void *ahandle);
static void mgmtSendCreateVgroupMsg(SVgObj *pVgroup, void *ahandle);
S
slguan 已提交
42

S
slguan 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
static int32_t mgmtVgroupActionDestroy(void *pObj) {
  SVgObj *pVgroup = (SVgObj *) pObj;
  if (pVgroup->idPool) {
    taosIdPoolCleanUp(pVgroup->idPool);
    pVgroup->idPool = NULL;
  }
  if (pVgroup->tableList) tfree(pVgroup->tableList);
  tfree(pObj);
  return TSDB_CODE_SUCCESS;
}

static int32_t mgmtVgroupActionInsert(void *pObj) {
  SVgObj *pVgroup = pObj;
  for (int32_t i = 0; i < pVgroup->numOfVnodes; ++i) {
    pVgroup->vnodeGid[i].vnode = pVgroup->vgId;
  }

  return TSDB_CODE_SUCCESS;
}

static int32_t mgmtVgroupActionDelete(void *pObj) {
  SVgObj *pVgroup = pObj;
  SDbObj *pDb = mgmtGetDb(pVgroup->dbName);

  if (pDb != NULL) {
    mgmtRemoveVgroupFromDb(pDb, pVgroup);
  }

  // mgmtUnSetDnodeVgid(pVgroup->vnodeGid, pVgroup->numOfVnodes);
  tfree(pVgroup->tableList);

  return TSDB_CODE_SUCCESS;
}
S
slguan 已提交
76

S
slguan 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
static int32_t mgmtVgroupActionUpdate(void *pObj) {
  SVgObj  *pVgroup  = (SVgObj *) pObj;
  int32_t oldTables = taosIdPoolMaxSize(pVgroup->idPool);

  SDbObj *pDb = mgmtGetDb(pVgroup->dbName);
  if (pDb != NULL) {
    if (pDb->cfg.maxSessions != oldTables) {
      mPrint("vgroup:%d tables change from %d to %d", pVgroup->vgId, oldTables, pDb->cfg.maxSessions);
      taosUpdateIdPool(pVgroup->idPool, pDb->cfg.maxSessions);
      int32_t size = sizeof(STableInfo *) * pDb->cfg.maxSessions;
      pVgroup->tableList = (STableInfo **)realloc(pVgroup->tableList, size);
    }
  }

  mTrace("vgroup:%d update, numOfVnode:%d", pVgroup->vgId, pVgroup->numOfVnodes);
  return TSDB_CODE_SUCCESS;
H
hzcheng 已提交
93 94
}

S
slguan 已提交
95 96 97 98 99 100 101
static int32_t mgmtVgroupActionEncode(void *pObj, void *pData, int32_t maxRowSize) {
  SVgObj *pVgroup = (SVgObj *) pObj;
  if (maxRowSize < tsVgUpdateSize) {
    return -1;
  } else {
    memcpy(pData, pVgroup, tsVgUpdateSize);
    return tsVgUpdateSize;
H
hzcheng 已提交
102
  }
S
slguan 已提交
103 104 105 106 107 108 109 110 111
}

static void *mgmtVgroupActionDecode(void *pObj) {
  SVgObj *pVgroup = (SVgObj *) malloc(sizeof(SVgObj));
  if (pVgroup == NULL) return NULL;
  memset(pVgroup, 0, sizeof(SVgObj));
  memcpy(pVgroup, pObj, tsVgUpdateSize);

  return pVgroup;
H
hzcheng 已提交
112 113
}

S
slguan 已提交
114
int32_t mgmtInitVgroups() {
S
slguan 已提交
115
  void *pNode = NULL;
H
hzcheng 已提交
116 117
  SVgObj *pVgroup = NULL;

S
slguan 已提交
118 119
  SVgObj tObj;
  tsVgUpdateSize = tObj.updateEnd - (int8_t *)&tObj;
H
hzcheng 已提交
120

S
slguan 已提交
121 122 123 124 125 126 127 128 129 130 131 132 133 134
  SSdbTableDesc tableDesc = {
    .tableName    = "vgroups",
    .hashSessions = TSDB_MAX_VGROUPS,
    .maxRowSize   = tsVgUpdateSize,
    .keyType      = SDB_KEYTYPE_AUTO,
    .insertFp     = mgmtVgroupActionInsert,
    .deleteFp     = mgmtVgroupActionDelete,
    .updateFp     = mgmtVgroupActionUpdate,
    .encodeFp     = mgmtVgroupActionEncode,
    .decodeFp     = mgmtVgroupActionDecode,
    .destroyFp    = mgmtVgroupActionDestroy,
  };

  tsVgroupSdb = sdbOpenTable(&tableDesc);
S
slguan 已提交
135
  if (tsVgroupSdb == NULL) {
S
slguan 已提交
136
    mError("failed to init vgroups data");
H
hzcheng 已提交
137 138 139 140
    return -1;
  }

  while (1) {
S
slguan 已提交
141
    pNode = sdbFetchRow(tsVgroupSdb, pNode, (void **)&pVgroup);
H
hzcheng 已提交
142 143 144 145 146 147 148
    if (pVgroup == NULL) break;

    SDbObj *pDb = mgmtGetDb(pVgroup->dbName);
    if (pDb == NULL) continue;

    pVgroup->prev = NULL;
    pVgroup->next = NULL;
S
slguan 已提交
149

S
slguan 已提交
150 151 152 153
    int32_t size = sizeof(STableInfo *) * pDb->cfg.maxSessions;
    pVgroup->tableList = (STableInfo **)malloc(size);
    if (pVgroup->tableList == NULL) {
      mError("failed to malloc(size:%d) for the tableList of vgroups", size);
S
slguan 已提交
154 155 156
      return -1;
    }
    
S
slguan 已提交
157
    memset(pVgroup->tableList, 0, size);
H
hzcheng 已提交
158 159

    pVgroup->idPool = taosInitIdPool(pDb->cfg.maxSessions);
S
slguan 已提交
160 161
    if (pVgroup->idPool == NULL) {
      mError("failed to taosInitIdPool for vgroups");
S
slguan 已提交
162
      free(pVgroup->tableList);
S
slguan 已提交
163 164 165
      return -1;
    }
    
H
hzcheng 已提交
166
    taosIdPoolReinit(pVgroup->idPool);
S
slguan 已提交
167

S
slguan 已提交
168
    if (tsIsCluster && pVgroup->vnodeGid[0].publicIp == 0) {
S
slguan 已提交
169
      pVgroup->vnodeGid[0].publicIp = inet_addr(tsPublicIp);
170
      pVgroup->vnodeGid[0].privateIp = inet_addr(tsPrivateIp);
S
slguan 已提交
171
      sdbUpdateRow(tsVgroupSdb, pVgroup, tsVgUpdateSize, SDB_OPER_GLOBAL);
S
slguan 已提交
172
    }
H
hzcheng 已提交
173

174
    // mgmtSetDnodeVgid(pVgroup->vnodeGid, pVgroup->numOfVnodes, pVgroup->vgId);
H
hzcheng 已提交
175 176
  }

S
slguan 已提交
177 178
  mgmtAddShellShowMetaHandle(TSDB_MGMT_TABLE_VGROUP, mgmtGetVgroupMeta);
  mgmtAddShellShowRetrieveHandle(TSDB_MGMT_TABLE_VGROUP, mgmtRetrieveVgroups);
179
  mgmtAddDClientRspHandle(TSDB_MSG_TYPE_MD_CREATE_VNODE_RSP, mgmtProcessCreateVnodeRsp);
180
  mgmtAddDClientRspHandle(TSDB_MSG_TYPE_MD_DROP_VNODE_RSP, mgmtProcessDropVnodeRsp);
S
slguan 已提交
181

H
hzcheng 已提交
182 183 184 185
  mTrace("vgroup is initialized");
  return 0;
}

S
slguan 已提交
186 187 188
SVgObj *mgmtGetVgroup(int32_t vgId) {
  return (SVgObj *)sdbGetRow(tsVgroupSdb, &vgId);
}
H
hzcheng 已提交
189

S
slguan 已提交
190 191 192
SVgObj *mgmtGetAvailableVgroup(SDbObj *pDb) {
  return pDb->pHead;
}
S
slguan 已提交
193

194 195 196
void mgmtCreateVgroup(SQueuedMsg *pMsg) {
  SDbObj *pDb = pMsg->pDb;
  if (pDb == NULL) {
S
slguan 已提交
197
    mError("failed to create vgroup, db not found");
198 199 200 201
    mgmtSendSimpleResp(pMsg->thandle, TSDB_CODE_INVALID_DB);
    return;
  }

S
slguan 已提交
202
  SVgObj *pVgroup = (SVgObj *)calloc(sizeof(SVgObj), 1);
H
hzcheng 已提交
203
  strcpy(pVgroup->dbName, pDb->name);
S
slguan 已提交
204 205
  pVgroup->numOfVnodes = pDb->cfg.replications;
  if (mgmtAllocVnodes(pVgroup) != 0) {
S
slguan 已提交
206
    mError("db:%s, no enough dnode to alloc %d vnodes to vgroup", pDb->name, pVgroup->numOfVnodes);
H
hzcheng 已提交
207
    free(pVgroup);
208 209
    mgmtSendSimpleResp(pMsg->thandle, TSDB_CODE_NO_ENOUGH_DNODES);
    return;
H
hzcheng 已提交
210 211
  }

212
  pVgroup->createdTime = taosGetTimestampMs();
S
slguan 已提交
213 214 215 216 217
  pVgroup->tableList   = (STableInfo **) calloc(sizeof(STableInfo *), pDb->cfg.maxSessions);
  pVgroup->numOfTables = 0;
  pVgroup->idPool      = taosInitIdPool(pDb->cfg.maxSessions);

  mgmtAddVgroupIntoDb(pDb, pVgroup);
218
  // mgmtSetDnodeVgid(pVgroup->vnodeGid, pVgroup->numOfVnodes, pVgroup->vgId);
S
slguan 已提交
219

S
slguan 已提交
220
  sdbInsertRow(tsVgroupSdb, pVgroup, SDB_OPER_GLOBAL);
H
hzcheng 已提交
221

S
slguan 已提交
222
  mPrint("vgroup:%d, is created in mnode, db:%s replica:%d", pVgroup->vgId, pDb->name, pVgroup->numOfVnodes);
223
  for (int32_t i = 0; i < pVgroup->numOfVnodes; ++i) {
S
slguan 已提交
224
    mPrint("vgroup:%d, dnode:%d vnode:%d", pVgroup->vgId, pVgroup->vnodeGid[i].dnodeId, pVgroup->vnodeGid[i].vnode);
225
  }
H
hzcheng 已提交
226

S
slguan 已提交
227 228
  pMsg->ahandle = pVgroup;
  pMsg->expected = pVgroup->numOfVnodes;
229
  mgmtSendCreateVgroupMsg(pVgroup, pMsg);
H
hzcheng 已提交
230 231
}

S
slguan 已提交
232 233 234 235 236 237
void mgmtDropVgroup(SVgObj *pVgroup, void *ahandle) {
  if (ahandle != NULL) {
    mgmtSendDropVgroupMsg(pVgroup, ahandle);
  } else {
    mTrace("vgroup:%d, replica:%d is deleting from sdb", pVgroup->vgId, pVgroup->numOfVnodes);
    mgmtSendDropVgroupMsg(pVgroup, NULL);
S
slguan 已提交
238
    sdbDeleteRow(tsVgroupSdb, pVgroup, SDB_OPER_GLOBAL);
H
hzcheng 已提交
239 240 241 242 243 244 245 246 247
  }
}

void mgmtSetVgroupIdPool() {
  void *  pNode = NULL;
  SVgObj *pVgroup = NULL;
  SDbObj *pDb;

  while (1) {
S
slguan 已提交
248
    pNode = sdbFetchRow(tsVgroupSdb, pNode, (void **)&pVgroup);
H
hzcheng 已提交
249 250 251
    if (pVgroup == NULL || pVgroup->idPool == 0) break;

    taosIdPoolSetFreeList(pVgroup->idPool);
S
slguan 已提交
252
    pVgroup->numOfTables = taosIdPoolNumOfUsed(pVgroup->idPool);
H
hzcheng 已提交
253 254

    pDb = mgmtGetDb(pVgroup->dbName);
S
slguan 已提交
255 256
    pDb->numOfTables += pVgroup->numOfTables;
    if (pVgroup->numOfTables >= pDb->cfg.maxSessions - 1)
H
hzcheng 已提交
257 258 259 260 261 262
      mgmtAddVgroupIntoDbTail(pDb, pVgroup);
    else
      mgmtAddVgroupIntoDb(pDb, pVgroup);
  }
}

S
slguan 已提交
263 264 265
void mgmtCleanUpVgroups() {
  sdbCloseTable(tsVgroupSdb);
}
H
hzcheng 已提交
266

H
hjxilinx 已提交
267
int32_t mgmtGetVgroupMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) {
S
slguan 已提交
268 269 270 271 272 273
  SDbObj *pDb = mgmtGetDb(pShow->db);
  if (pDb == NULL) {
    return TSDB_CODE_DB_NOT_SELECTED;
  }

  int32_t cols = 0;
H
hjxilinx 已提交
274
  SSchema *pSchema = pMeta->schema;
S
slguan 已提交
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289

  pShow->bytes[cols] = 4;
  pSchema[cols].type = TSDB_DATA_TYPE_INT;
  strcpy(pSchema[cols].name, "vgId");
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

  pShow->bytes[cols] = 4;
  pSchema[cols].type = TSDB_DATA_TYPE_INT;
  strcpy(pSchema[cols].name, "tables");
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

  pShow->bytes[cols] = 9;
  pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
H
hjxilinx 已提交
290
  strcpy(pSchema[cols].name, "vgroup_status");
S
slguan 已提交
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
  pSchema[cols].bytes = htons(pShow->bytes[cols]);
  cols++;

  int32_t maxReplica = 0;
  SVgObj  *pVgroup   = NULL;
  STableInfo *pTable = NULL;
  if (pShow->payloadLen > 0 ) {
    pTable = mgmtGetTable(pShow->payload);
    if (NULL == pTable) {
      return TSDB_CODE_INVALID_TABLE_ID;
    }

    pVgroup = mgmtGetVgroup(pTable->vgId);
    if (NULL == pVgroup) return TSDB_CODE_INVALID_TABLE_ID;

    maxReplica = pVgroup->numOfVnodes > maxReplica ? pVgroup->numOfVnodes : maxReplica;
  } else {
    SVgObj *pVgroup = pDb->pHead;
    while (pVgroup != NULL) {
      maxReplica = pVgroup->numOfVnodes > maxReplica ? pVgroup->numOfVnodes : maxReplica;
      pVgroup = pVgroup->next;
    }
  }

  for (int32_t i = 0; i < maxReplica; ++i) {
    pShow->bytes[cols] = 16;
    pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
    strcpy(pSchema[cols].name, "ip");
    pSchema[cols].bytes = htons(pShow->bytes[cols]);
    cols++;

    pShow->bytes[cols] = 2;
    pSchema[cols].type = TSDB_DATA_TYPE_SMALLINT;
    strcpy(pSchema[cols].name, "vnode");
    pSchema[cols].bytes = htons(pShow->bytes[cols]);
    cols++;

    pShow->bytes[cols] = 9;
    pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
H
hjxilinx 已提交
330
    strcpy(pSchema[cols].name, "vnode_status");
S
slguan 已提交
331 332 333 334 335
    pSchema[cols].bytes = htons(pShow->bytes[cols]);
    cols++;

    pShow->bytes[cols] = 16;
    pSchema[cols].type = TSDB_DATA_TYPE_BINARY;
H
hjxilinx 已提交
336
    strcpy(pSchema[cols].name, "public_ip");
S
slguan 已提交
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
    pSchema[cols].bytes = htons(pShow->bytes[cols]);
    cols++;
  }

  pMeta->numOfColumns = htons(cols);
  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];

  pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1];

  if (NULL == pTable) {
    pShow->numOfRows = pDb->numOfVgroups;
    pShow->pNode = pDb->pHead;
  } else {
    pShow->numOfRows = 1;
    pShow->pNode = pVgroup;
  }
L
[1361]  
lihui 已提交
356

H
hzcheng 已提交
357 358 359
  return 0;
}

S
slguan 已提交
360
char *mgmtGetVnodeStatus(SVgObj *pVgroup, SVnodeGid *pVnode) {
361
  SDnodeObj *pDnode = mgmtGetDnode(pVnode->dnodeId);
S
slguan 已提交
362
  if (pDnode == NULL) {
363
    mError("vgroup:%d, not exist in dnode:%d", pVgroup->vgId, pDnode->dnodeId);
S
slguan 已提交
364 365 366 367 368 369 370
    return "null";
  }

  if (pDnode->status == TSDB_DN_STATUS_OFFLINE) {
    return "offline";
  }

371 372
  for (int i = 0; i < pDnode->openVnodes; ++i) {
    if (pDnode->vload[i].vgId == pVgroup->vgId) {
H
hjxilinx 已提交
373
       return (char*)taosGetVnodeStatusStr(pDnode->vload[i].status);
374
    }
S
slguan 已提交
375
  }
376 377
  
  return "null";
S
slguan 已提交
378 379
}

S
slguan 已提交
380
int32_t mgmtRetrieveVgroups(SShowObj *pShow, char *data, int32_t rows, void *pConn) {
S
slguan 已提交
381
  int32_t numOfRows = 0;
S
slguan 已提交
382 383 384 385 386 387 388 389 390 391 392 393 394 395
  SVgObj *pVgroup = NULL;
  int32_t maxReplica = 0;
  int32_t cols = 0;
  char    ipstr[20];
  char *  pWrite;

  SDbObj *pDb = mgmtGetDb(pShow->db);
  if (pDb == NULL) return 0;

  pVgroup = pDb->pHead;
  while (pVgroup != NULL) {
    maxReplica = pVgroup->numOfVnodes > maxReplica ? pVgroup->numOfVnodes : maxReplica;
    pVgroup    = pVgroup->next;
  }
H
hzcheng 已提交
396

S
slguan 已提交
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
  while (numOfRows < rows) {
    pVgroup = (SVgObj *) pShow->pNode;
    if (pVgroup == NULL) break;
    pShow->pNode = (void *) pVgroup->next;

    cols = 0;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    *(int32_t *) pWrite = pVgroup->vgId;
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    *(int32_t *) pWrite = pVgroup->numOfTables;
    cols++;

    pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
    strcpy(pWrite, taosGetVgroupLbStatusStr(pVgroup->lbStatus));
    cols++;

    for (int32_t i = 0; i < maxReplica; ++i) {
417
      tinet_ntoa(ipstr, pVgroup->vnodeGid[i].privateIp);
S
slguan 已提交
418 419 420 421 422 423 424 425 426
      pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
      strcpy(pWrite, ipstr);
      cols++;

      pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
      *(int16_t *) pWrite = pVgroup->vnodeGid[i].vnode;
      cols++;

      pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
427
      if (pVgroup->vnodeGid[i].dnodeId != 0) {
S
slguan 已提交
428 429 430 431 432 433
        char *vnodeStatus = mgmtGetVnodeStatus(pVgroup, pVgroup->vnodeGid + i);
        strcpy(pWrite, vnodeStatus);
      } else {
        strcpy(pWrite, "null");
      }
      cols++;
H
hzcheng 已提交
434

S
slguan 已提交
435 436 437 438 439
      tinet_ntoa(ipstr, pVgroup->vnodeGid[i].publicIp);
      pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows;
      strcpy(pWrite, ipstr);
      cols++;
    }
H
hzcheng 已提交
440

S
slguan 已提交
441 442
    numOfRows++;
  }
H
hzcheng 已提交
443

S
slguan 已提交
444 445 446 447 448
  pShow->numOfReads += numOfRows;
  return numOfRows;
}

void mgmtUpdateVgroup(SVgObj *pVgroup) {
S
slguan 已提交
449
  sdbUpdateRow(tsVgroupSdb, pVgroup, tsVgUpdateSize, SDB_OPER_LOCAL);
S
slguan 已提交
450 451 452 453 454 455 456 457 458 459 460 461 462 463
}

void mgmtAddTableIntoVgroup(SVgObj *pVgroup, STableInfo *pTable) {
  pVgroup->numOfTables++;
  if (pTable->sid >= 0)
    pVgroup->tableList[pTable->sid] = pTable;
}

void mgmtRemoveTableFromVgroup(SVgObj *pVgroup, STableInfo *pTable) {
  pVgroup->numOfTables--;
  if (pTable->sid >= 0)
    pVgroup->tableList[pTable->sid] = NULL;
  taosFreeId(pVgroup->idPool, pTable->sid);
}
S
slguan 已提交
464

S
slguan 已提交
465
SMDCreateVnodeMsg *mgmtBuildCreateVnodeMsg(SVgObj *pVgroup) {
S
slguan 已提交
466 467 468
  SDbObj *pDb = mgmtGetDb(pVgroup->dbName);
  if (pDb == NULL) return NULL;

469 470
  SMDCreateVnodeMsg *pVnode = rpcMallocCont(sizeof(SMDCreateVnodeMsg));
  if (pVnode == NULL) return NULL;
S
slguan 已提交
471

S
slguan 已提交
472
  pVnode->cfg = pDb->cfg;
S
slguan 已提交
473

474
  SVnodeCfg *pCfg = &pVnode->cfg;
S
slguan 已提交
475 476 477 478 479 480 481 482 483 484 485 486 487
  pCfg->vgId                         = htonl(pVgroup->vgId);
  pCfg->maxSessions                  = htonl(pCfg->maxSessions);
  pCfg->cacheBlockSize               = htonl(pCfg->cacheBlockSize);
  pCfg->cacheNumOfBlocks.totalBlocks = htonl(pCfg->cacheNumOfBlocks.totalBlocks);
  pCfg->daysPerFile                  = htonl(pCfg->daysPerFile);
  pCfg->daysToKeep1                  = htonl(pCfg->daysToKeep1);
  pCfg->daysToKeep2                  = htonl(pCfg->daysToKeep2);
  pCfg->daysToKeep                   = htonl(pCfg->daysToKeep);
  pCfg->commitTime                   = htonl(pCfg->commitTime);
  pCfg->blocksPerTable               = htons(pCfg->blocksPerTable);
  pCfg->replications                 = (char) pVgroup->numOfVnodes;
  pCfg->rowsInFileBlock              = htonl(pCfg->rowsInFileBlock);

488
  SVnodeDesc *vpeerDesc = pVnode->vpeerDesc;
S
slguan 已提交
489
  for (int32_t j = 0; j < pVgroup->numOfVnodes; ++j) {
490
    vpeerDesc[j].ip    = htonl(pVgroup->vnodeGid[j].privateIp);
S
slguan 已提交
491 492
  }

493
  return pVnode;
S
slguan 已提交
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
}

SVgObj *mgmtGetVgroupByVnode(uint32_t dnode, int32_t vnode) {
  if (vnode < 0 || vnode >= TSDB_MAX_VNODES) {
    return NULL;
  }

  SDnodeObj *pDnode = mgmtGetDnode(dnode);
  if (pDnode == NULL) {
    return NULL;
  }

  int32_t vgId = pDnode->vload[vnode].vgId;
  return mgmtGetVgroup(vgId);
}

SRpcIpSet mgmtGetIpSetFromVgroup(SVgObj *pVgroup) {
511 512 513
  SRpcIpSet ipSet = {
    .numOfIps = pVgroup->numOfVnodes,
    .inUse = 0,
S
slguan 已提交
514
    .port = tsDnodeMnodePort
515
  };
S
slguan 已提交
516
  for (int i = 0; i < pVgroup->numOfVnodes; ++i) {
517
    ipSet.ip[i] = pVgroup->vnodeGid[i].privateIp;
S
slguan 已提交
518 519 520 521 522
  }
  return ipSet;
}

SRpcIpSet mgmtGetIpSetFromIp(uint32_t ip) {
523 524 525 526
  SRpcIpSet ipSet = {
    .ip[0]    = ip,
    .numOfIps = 1,
    .inUse    = 0,
S
slguan 已提交
527
    .port     = tsDnodeMnodePort
528
  };
S
slguan 已提交
529
  return ipSet;
S
slguan 已提交
530 531
}

S
slguan 已提交
532
void mgmtSendCreateVnodeMsg(SVgObj *pVgroup, SRpcIpSet *ipSet, void *ahandle) {
S
slguan 已提交
533
  mTrace("vgroup:%d, send create vnode:%d msg, ahandle:%p", pVgroup->vgId, pVgroup->vgId, ahandle);
S
slguan 已提交
534
  SMDCreateVnodeMsg *pCreate = mgmtBuildCreateVnodeMsg(pVgroup);
S
slguan 已提交
535
  SRpcMsg rpcMsg = {
536 537 538 539 540
    .handle  = ahandle,
    .pCont   = pCreate,
    .contLen = pCreate ? sizeof(SMDCreateVnodeMsg) : 0,
    .code    = 0,
    .msgType = TSDB_MSG_TYPE_MD_CREATE_VNODE
S
slguan 已提交
541 542 543 544 545
  };
  mgmtSendMsgToDnode(ipSet, &rpcMsg);
}

void mgmtSendCreateVgroupMsg(SVgObj *pVgroup, void *ahandle) {
S
slguan 已提交
546
  mTrace("vgroup:%d, send create all vnodes msg, ahandle:%p", pVgroup->vgId, ahandle);
S
slguan 已提交
547
  for (int32_t i = 0; i < pVgroup->numOfVnodes; ++i) {
548
    SRpcIpSet ipSet = mgmtGetIpSetFromIp(pVgroup->vnodeGid[i].privateIp);
S
slguan 已提交
549
    mgmtSendCreateVnodeMsg(pVgroup, &ipSet, ahandle);
S
slguan 已提交
550
  }
551 552 553 554 555 556 557 558 559 560 561 562 563
}

static void mgmtProcessCreateVnodeRsp(SRpcMsg *rpcMsg) {
  if (rpcMsg->handle == NULL) return;

  SQueuedMsg *queueMsg = rpcMsg->handle;
  queueMsg->received++;
  if (rpcMsg->code == TSDB_CODE_SUCCESS) {
    queueMsg->code = rpcMsg->code;
    queueMsg->successed++;
  }

  SVgObj *pVgroup = queueMsg->ahandle;
S
slguan 已提交
564 565 566
  mTrace("vgroup:%d, create vnode rsp received, result:%s received:%d successed:%d expected:%d, thandle:%p ahandle:%p",
         pVgroup->vgId, tstrerror(rpcMsg->code), queueMsg->received, queueMsg->successed, queueMsg->expected,
         queueMsg->thandle, rpcMsg->handle);
567 568 569 570 571 572 573 574 575 576 577 578 579 580

  if (queueMsg->received != queueMsg->expected) return;

  if (queueMsg->received == queueMsg->successed) {
    SQueuedMsg *newMsg = calloc(1, sizeof(SQueuedMsg));
    newMsg->msgType = queueMsg->msgType;
    newMsg->thandle = queueMsg->thandle;
    newMsg->pDb     = queueMsg->pDb;
    newMsg->pUser   = queueMsg->pUser;
    newMsg->contLen = queueMsg->contLen;
    newMsg->pCont   = rpcMallocCont(newMsg->contLen);
    memcpy(newMsg->pCont, queueMsg->pCont, newMsg->contLen);
    mgmtAddToShellQueue(newMsg);
  } else {
S
slguan 已提交
581
    sdbDeleteRow(tsVgroupSdb, pVgroup, SDB_OPER_GLOBAL);
582 583 584 585
    mgmtSendSimpleResp(queueMsg->thandle, rpcMsg->code);
  }

  free(queueMsg);
586 587
}

S
slguan 已提交
588
static SMDDropVnodeMsg *mgmtBuildDropVnodeMsg(int32_t vgId) {
589 590 591
  SMDDropVnodeMsg *pDrop = rpcMallocCont(sizeof(SMDDropVnodeMsg));
  if (pDrop == NULL) return NULL;

S
slguan 已提交
592
  pDrop->vgId = htonl(vgId);
593 594 595
  return pDrop;
}

S
slguan 已提交
596 597 598
void mgmtSendDropVnodeMsg(int32_t vgId, SRpcIpSet *ipSet, void *ahandle) {
  mTrace("vgroup:%d, send drop vnode msg, ahandle:%p", vgId, ahandle);
  SMDDropVnodeMsg *pDrop = mgmtBuildDropVnodeMsg(vgId);
599 600 601 602 603 604 605 606 607 608 609 610 611
  SRpcMsg rpcMsg = {
      .handle  = ahandle,
      .pCont   = pDrop,
      .contLen = pDrop ? sizeof(SMDDropVnodeMsg) : 0,
      .code    = 0,
      .msgType = TSDB_MSG_TYPE_MD_DROP_VNODE
  };
  mgmtSendMsgToDnode(ipSet, &rpcMsg);
}

static void mgmtSendDropVgroupMsg(SVgObj *pVgroup, void *ahandle) {
  mTrace("vgroup:%d, send drop all vnodes msg, ahandle:%p", pVgroup->vgId, ahandle);
  for (int32_t i = 0; i < pVgroup->numOfVnodes; ++i) {
612
    SRpcIpSet ipSet = mgmtGetIpSetFromIp(pVgroup->vnodeGid[i].privateIp);
S
slguan 已提交
613
    mgmtSendDropVnodeMsg(pVgroup->vgId, &ipSet, ahandle);
614 615 616 617 618
  }
}

static void mgmtProcessDropVnodeRsp(SRpcMsg *rpcMsg) {
  mTrace("drop vnode msg is received");
S
slguan 已提交
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
  if (rpcMsg->handle == NULL) return;

  SQueuedMsg *queueMsg = rpcMsg->handle;
  queueMsg->received++;
  if (rpcMsg->code == TSDB_CODE_SUCCESS) {
    queueMsg->code = rpcMsg->code;
    queueMsg->successed++;
  }

  SVgObj *pVgroup = queueMsg->ahandle;
  mTrace("vgroup:%d, drop vnode rsp received, result:%s received:%d successed:%d expected:%d, thandle:%p ahandle:%p",
         pVgroup->vgId, tstrerror(rpcMsg->code), queueMsg->received, queueMsg->successed, queueMsg->expected,
         queueMsg->thandle, rpcMsg->handle);

  if (queueMsg->received != queueMsg->expected) return;

S
slguan 已提交
635
  sdbDeleteRow(tsVgroupSdb, pVgroup, SDB_OPER_GLOBAL);
S
slguan 已提交
636 637 638 639 640 641 642 643 644 645 646 647

  SQueuedMsg *newMsg = calloc(1, sizeof(SQueuedMsg));
  newMsg->msgType = queueMsg->msgType;
  newMsg->thandle = queueMsg->thandle;
  newMsg->pDb     = queueMsg->pDb;
  newMsg->pUser   = queueMsg->pUser;
  newMsg->contLen = queueMsg->contLen;
  newMsg->pCont   = rpcMallocCont(newMsg->contLen);
  memcpy(newMsg->pCont, queueMsg->pCont, newMsg->contLen);
  mgmtAddToShellQueue(newMsg);

  free(queueMsg);
S
slguan 已提交
648 649 650 651 652 653 654 655 656 657 658 659
}

void mgmtUpdateVgroupIp(SDnodeObj *pDnode) {
  void *  pNode = NULL;
  SVgObj *pVgroup = NULL;
  while (1) {
    pNode = sdbFetchRow(tsVgroupSdb, pNode, (void **)&pVgroup);
    if (pVgroup == NULL) break;

    for (int32_t i = 0; i < pVgroup->numOfVnodes; ++i) {
      SVnodeGid *vnodeGid = pVgroup->vnodeGid + i;
      if (vnodeGid->dnodeId == pDnode->dnodeId) {
H
hjxilinx 已提交
660 661
        mPrint("vgroup:%d, dnode:%d, privateIp:%s change to %s, publicIp:%s change to %s",
               pVgroup->vgId, vnodeGid->dnodeId, pDnode->privateIp, taosIpStr(vnodeGid->privateIp),
S
slguan 已提交
662 663
               pDnode->publicIp, taosIpStr(vnodeGid->publicIp));
        vnodeGid->publicIp = pDnode->publicIp;
664
        vnodeGid->privateIp = pDnode->privateIp;
S
slguan 已提交
665
        sdbUpdateRow(tsVgroupSdb, pVgroup, tsVgUpdateSize, SDB_OPER_GLOBAL);
S
slguan 已提交
666 667 668 669
      }
    }
  }
}
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693

//static void mgmtProcessVnodeCfgMsg(int8_t msgType, int8_t *pCont, int32_t contLen, void *pConn) {
//  if (!sdbMaster) {
//    mgmtSendRspToDnode(pConn, msgType + 1, TSDB_CODE_REDIRECT, NULL, 0);
//    return;
//  }
//
//  SDMConfigVnodeMsg *pCfg = (SDMConfigVnodeMsg *) pCont;
//  pCfg->dnode = htonl(pCfg->dnode);
//  pCfg->vnode = htonl(pCfg->vnode);
//
//  SVgObj *pVgroup = mgmtGetVgroupByVnode(pCfg->dnode, pCfg->vnode);
//  if (pVgroup == NULL) {
//    mTrace("dnode:%s, vnode:%d, no vgroup info", taosIpStr(pCfg->dnode), pCfg->vnode);
//    mgmtSendRspToDnode(pConn, msgType + 1, TSDB_CODE_NOT_ACTIVE_VNODE, NULL, 0);
//    return;
//  }
//
//  mgmtSendRspToDnode(pConn, msgType + 1, TSDB_CODE_SUCCESS, NULL, 0);
//
//  SRpcIpSet ipSet = mgmtGetIpSetFromIp(pCfg->dnode);
//  mgmtSendCreateVnodeMsg(pVgroup, pCfg->vnode, &ipSet, NULL);
//}
//