dnodeMgmt.c 11.8 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
#include "ihash.h"
S
slguan 已提交
19 20
#include "taoserror.h"
#include "taosmsg.h"
S
slguan 已提交
21
#include "tlog.h"
H
hzcheng 已提交
22
#include "trpc.h"
S
slguan 已提交
23
#include "tstatus.h"
S
slguan 已提交
24
#include "tsdb.h"
S
slguan 已提交
25
#include "ttimer.h"
S
slguan 已提交
26
#include "dnodeMgmt.h"
S
slguan 已提交
27 28
#include "dnodeRead.h"
#include "dnodeWrite.h"
S
slguan 已提交
29 30 31

typedef struct {
  int32_t vgId;     // global vnode group ID
S
slguan 已提交
32 33
  int32_t status;   // status: master, slave, notready, deleting
  int32_t refCount; // reference count
S
slguan 已提交
34
  int64_t version;
S
slguan 已提交
35 36 37 38 39 40 41
  void   *wworker;
  void   *rworker;
  void   *wal;
  void   *tsdb;
  void   *replica;
  void   *events;
  void   *cq;      // continuous query
S
slguan 已提交
42 43
} SVnodeObj;

S
slguan 已提交
44 45 46 47
static int32_t  dnodeOpenVnodes();
static void     dnodeCleanupVnodes();
static int32_t  dnodeOpenVnode(int32_t vgId);
static void     dnodeCleanupVnode(SVnodeObj *pVnode);
S
slguan 已提交
48
static int32_t  dnodeCreateVnode(SMDCreateVnodeMsg *cfg);
S
slguan 已提交
49
static void     dnodeDropVnode(SVnodeObj *pVnode);
S
slguan 已提交
50 51
static void     dnodeProcessCreateVnodeMsg(SRpcMsg *pMsg);
static void     dnodeProcessDropVnodeMsg(SRpcMsg *pMsg);
S
slguan 已提交
52
static void     dnodeProcessAlterVnodeMsg(SRpcMsg *pMsg);
S
slguan 已提交
53 54
static void     dnodeProcessAlterStreamMsg(SRpcMsg *pMsg);
static void     dnodeProcessConfigDnodeMsg(SRpcMsg *pMsg);
S
slguan 已提交
55
static void   (*dnodeProcessMgmtMsgFp[TSDB_MSG_TYPE_MAX])(SRpcMsg *pMsg);
S
slguan 已提交
56
static void     dnodeSendStatusMsg(void *handle, void *tmrId);
S
slguan 已提交
57 58

static void * tsDnodeVnodesHash = NULL;
S
slguan 已提交
59 60
static void  *tsDnodeTmr = NULL;
static void  *tsStatusTimer = NULL;
S
slguan 已提交
61 62

int32_t dnodeInitMgmt() {
S
slguan 已提交
63 64
  dnodeProcessMgmtMsgFp[TSDB_MSG_TYPE_MD_CREATE_VNODE] = dnodeProcessCreateVnodeMsg;
  dnodeProcessMgmtMsgFp[TSDB_MSG_TYPE_MD_DROP_VNODE]   = dnodeProcessDropVnodeMsg;
S
slguan 已提交
65
  dnodeProcessMgmtMsgFp[TSDB_MSG_TYPE_MD_ALTER_VNODE]  = dnodeProcessAlterVnodeMsg;
S
slguan 已提交
66 67
  dnodeProcessMgmtMsgFp[TSDB_MSG_TYPE_MD_ALTER_STREAM]  = dnodeProcessAlterStreamMsg;
  dnodeProcessMgmtMsgFp[TSDB_MSG_TYPE_MD_CONFIG_DNODE]  = dnodeProcessConfigDnodeMsg;
S
slguan 已提交
68 69 70

  tsDnodeVnodesHash = taosInitIntHash(TSDB_MAX_VNODES, sizeof(SVnodeObj), taosHashInt);
  if (tsDnodeVnodesHash == NULL) {
S
slguan 已提交
71 72
    dError("failed to init vnode list");
    return -1;
S
slguan 已提交
73
  }
S
slguan 已提交
74

S
slguan 已提交
75 76 77 78 79 80 81
  tsDnodeTmr = taosTmrInit(100, 200, 60000, "DND-DM");
  if (tsDnodeTmr == NULL) {
    dError("failed to init dnode timer");
    return -1;
  }
  taosTmrReset(dnodeSendStatusMsg, 500, NULL, tsDnodeTmr, &tsStatusTimer);

S
slguan 已提交
82
  return dnodeOpenVnodes();
S
#1177  
slguan 已提交
83 84
}

S
slguan 已提交
85
void dnodeCleanupMgmt() {
S
slguan 已提交
86 87 88 89 90
  if (tsStatusTimer != NULL) {
    taosTmrStopA(&tsStatusTimer);
    tsStatusTimer = NULL;
  }

S
slguan 已提交
91 92
  dnodeCleanupVnodes();
  taosCleanUpIntHash(tsDnodeVnodesHash);
S
#1177  
slguan 已提交
93 94
}

S
slguan 已提交
95
void dnodeMgmt(SRpcMsg *pMsg) {
S
slguan 已提交
96 97 98 99
  terrno = 0;

  if (dnodeProcessMgmtMsgFp[pMsg->msgType]) {
    (*dnodeProcessMgmtMsgFp[pMsg->msgType])(pMsg);
S
slguan 已提交
100
  } else {
101 102 103 104 105
    SRpcMsg rsp;
    rsp.handle = pMsg->handle;
    rsp.code   = TSDB_CODE_MSG_NOT_PROCESSED;
    rsp.pCont  = NULL;
    rpcSendResponse(&rsp);
S
slguan 已提交
106
  }
S
slguan 已提交
107 108

  rpcFreeCont(pMsg->pCont);  // free the received message
S
#1177  
slguan 已提交
109
}
S
slguan 已提交
110

S
slguan 已提交
111
void *dnodeGetVnode(int32_t vgId) {
S
slguan 已提交
112
  SVnodeObj *pVnode = (SVnodeObj *) taosGetIntHashData(tsDnodeVnodesHash, vgId);
S
slguan 已提交
113 114 115 116
  if (pVnode == NULL) {
    terrno = TSDB_CODE_INVALID_VGROUP_ID;
    return NULL;
  }
S
slguan 已提交
117

S
slguan 已提交
118 119 120 121
  if (pVnode->status != TSDB_VN_STATUS_MASTER && pVnode->status == TSDB_VN_STATUS_SLAVE) {
    terrno = TSDB_CODE_INVALID_VNODE_STATUS;
    return NULL;
  }
S
slguan 已提交
122

S
slguan 已提交
123 124
  atomic_add_fetch_32(&pVnode->refCount, 1);
  return pVnode;
S
slguan 已提交
125 126
}

S
slguan 已提交
127
int32_t dnodeGetVnodeStatus(void *pVnode) {
S
slguan 已提交
128 129
  return ((SVnodeObj *)pVnode)->status;
}
S
slguan 已提交
130

S
slguan 已提交
131 132 133 134 135 136 137 138 139 140
void *dnodeGetVnodeWworker(void *pVnode) {
  return ((SVnodeObj *)pVnode)->wworker;
}
 
void *dnodeGetVnodeRworker(void *pVnode) {
  return ((SVnodeObj *)pVnode)->rworker;
}
 
void *dnodeGetVnodeWal(void *pVnode) {
  return ((SVnodeObj *)pVnode)->wal;
S
slguan 已提交
141 142
}

S
slguan 已提交
143 144
void *dnodeGetVnodeTsdb(void *pVnode) {
  return ((SVnodeObj *)pVnode)->tsdb;
S
slguan 已提交
145
}
S
#1177  
slguan 已提交
146

S
slguan 已提交
147 148
void dnodeReleaseVnode(void *pVnode) {
  atomic_sub_fetch_32(&((SVnodeObj *) pVnode)->refCount, 1);
S
slguan 已提交
149
}
H
hzcheng 已提交
150

S
slguan 已提交
151 152 153
static int32_t dnodeOpenVnodes() {
  dPrint("open all vnodes");
  return TSDB_CODE_SUCCESS;
H
hzcheng 已提交
154 155
}

S
slguan 已提交
156
static void dnodeCleanupVnodes() {
S
slguan 已提交
157
  dPrint("clean all vnodes");
S
slguan 已提交
158 159
}

S
slguan 已提交
160
static int32_t dnodeOpenVnode(int32_t vgId) {
S
slguan 已提交
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
  char rootDir[TSDB_FILENAME_LEN] = {0};
  sprintf(rootDir, "%s/vnode%d", tsDirectory, vgId);

  void *pTsdb = tsdbOpenRepo(rootDir);
  if (pTsdb != NULL) {
    return terrno;
  }

  SVnodeObj vnodeObj;
  vnodeObj.vgId     = vgId;
  vnodeObj.status   = TSDB_VN_STATUS_NOT_READY;
  vnodeObj.refCount = 1;
  vnodeObj.version  = 0;
  vnodeObj.wworker  = dnodeAllocateWriteWorker();
  vnodeObj.rworker  = dnodeAllocateReadWorker();
  vnodeObj.wal      = NULL;
  vnodeObj.tsdb     = pTsdb;
  vnodeObj.replica  = NULL;
  vnodeObj.events   = NULL;
  vnodeObj.cq       = NULL;

  taosAddIntHash(tsDnodeVnodesHash, vnodeObj.vgId, (char *) (&vnodeObj));
S
slguan 已提交
183 184

  return TSDB_CODE_SUCCESS;
H
hzcheng 已提交
185 186
}

S
slguan 已提交
187
static void dnodeCleanupVnode(SVnodeObj *pVnode) {
S
slguan 已提交
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
  pVnode->status = TSDB_VN_STATUS_NOT_READY;
  int32_t count = atomic_sub_fetch_32(&pVnode->refCount, 1);
  if (count > 0) {
    // wait refcount
  }

  // remove replica

  // remove read queue
  dnodeFreeReadWorker(pVnode->rworker);
  pVnode->rworker = NULL;

  // remove write queue
  dnodeFreeWriteWorker(pVnode->wworker);
  pVnode->wworker = NULL;

  // remove wal

  // remove tsdb
  if (pVnode->tsdb) {
    tsdbCloseRepo(pVnode->tsdb);
    pVnode->tsdb = NULL;
  }

  taosDeleteIntHash(tsDnodeVnodesHash, pVnode->vgId);
S
slguan 已提交
213
}
H
hzcheng 已提交
214

S
slguan 已提交
215
static int32_t dnodeCreateVnode(SMDCreateVnodeMsg *pVnodeCfg) {
S
slguan 已提交
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
  STsdbCfg tsdbCfg;
  tsdbCfg.precision           = pVnodeCfg->cfg.precision;
  tsdbCfg.tsdbId              = pVnodeCfg->vnode;
  tsdbCfg.maxTables           = pVnodeCfg->cfg.maxSessions;
  tsdbCfg.daysPerFile         = pVnodeCfg->cfg.daysPerFile;
  tsdbCfg.minRowsPerFileBlock = -1;
  tsdbCfg.maxRowsPerFileBlock = -1;
  tsdbCfg.keep                = -1;
  tsdbCfg.maxCacheSize        = -1;

  char rootDir[TSDB_FILENAME_LEN] = {0};
  sprintf(rootDir, "%s/vnode%d", tsDirectory, pVnodeCfg->cfg.vgId);

  void *pTsdb = tsdbCreateRepo(rootDir, &tsdbCfg, NULL);
  if (pTsdb != NULL) {
    return terrno;
  }

  SVnodeObj vnodeObj;
  vnodeObj.vgId     = pVnodeCfg->cfg.vgId;
  vnodeObj.status   = TSDB_VN_STATUS_NOT_READY;
  vnodeObj.refCount = 1;
  vnodeObj.version  = 0;
  vnodeObj.wworker  = dnodeAllocateWriteWorker();
  vnodeObj.rworker  = dnodeAllocateReadWorker();
  vnodeObj.wal      = NULL;
  vnodeObj.tsdb     = pTsdb;
  vnodeObj.replica  = NULL;
  vnodeObj.events   = NULL;
  vnodeObj.cq       = NULL;

  taosAddIntHash(tsDnodeVnodesHash, vnodeObj.vgId, (char *) (&vnodeObj));
S
slguan 已提交
248 249 250 251 252

  return TSDB_CODE_SUCCESS;
}

static void dnodeDropVnode(SVnodeObj *pVnode) {
S
slguan 已提交
253 254 255 256 257 258 259 260 261 262 263 264 265
  pVnode->status = TSDB_VN_STATUS_NOT_READY;

  int32_t count = atomic_sub_fetch_32(&pVnode->refCount, 1);
  if (count > 0) {
    // wait refcount
  }

  if (pVnode->tsdb) {
    tsdbDropRepo(pVnode->tsdb);
    pVnode->tsdb = NULL;
  }

  dnodeCleanupVnode(pVnode);
H
hzcheng 已提交
266 267
}

S
slguan 已提交
268
static void dnodeProcessCreateVnodeMsg(SRpcMsg *rpcMsg) {
S
slguan 已提交
269
  SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0};
S
slguan 已提交
270

S
slguan 已提交
271
  SMDCreateVnodeMsg *pCreate = rpcMsg->pCont;
S
slguan 已提交
272 273 274 275
  pCreate->vnode           = htonl(pCreate->vnode);
  pCreate->cfg.vgId        = htonl(pCreate->cfg.vgId);
  pCreate->cfg.maxSessions = htonl(pCreate->cfg.maxSessions);
  pCreate->cfg.daysPerFile = htonl(pCreate->cfg.daysPerFile);
S
slguan 已提交
276

277 278 279 280 281 282 283
//  SVnodeObj *pVnodeObj = (SVnodeObj *) taosGetIntHashData(tsDnodeVnodesHash, pCreate->cfg.vgId);
//  if (pVnodeObj != NULL) {
//    rpcRsp.code = TSDB_CODE_SUCCESS;
//  } else {
//    rpcRsp.code = dnodeCreateVnode(pCreate);
//  }
  rpcRsp.code = TSDB_CODE_SUCCESS;
S
slguan 已提交
284

S
slguan 已提交
285
  rpcSendResponse(&rpcRsp);
S
slguan 已提交
286 287
}

S
slguan 已提交
288
static void dnodeProcessDropVnodeMsg(SRpcMsg *rpcMsg) {
S
slguan 已提交
289
  SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0};
S
slguan 已提交
290

S
slguan 已提交
291
  SMDDropVnodeMsg *pDrop = rpcMsg->pCont;
S
slguan 已提交
292
  pDrop->vgId = htonl(pDrop->vgId);
S
slguan 已提交
293

S
slguan 已提交
294
  SVnodeObj *pVnodeObj = (SVnodeObj *) taosGetIntHashData(tsDnodeVnodesHash, pDrop->vgId);
S
slguan 已提交
295 296 297 298 299 300
  if (pVnodeObj != NULL) {
    dnodeDropVnode(pVnodeObj);
    rpcRsp.code = TSDB_CODE_SUCCESS;
  } else {
    rpcRsp.code = TSDB_CODE_INVALID_VGROUP_ID;
  }
S
slguan 已提交
301

S
slguan 已提交
302
  rpcSendResponse(&rpcRsp);
H
hzcheng 已提交
303 304
}

S
slguan 已提交
305 306
static void dnodeProcessAlterVnodeMsg(SRpcMsg *rpcMsg) {
  SRpcMsg rpcRsp = {.handle = rpcMsg->handle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0};
S
slguan 已提交
307

S
slguan 已提交
308
  SMDCreateVnodeMsg *pCreate = rpcMsg->pCont;
S
slguan 已提交
309 310 311 312
  pCreate->vnode           = htonl(pCreate->vnode);
  pCreate->cfg.vgId        = htonl(pCreate->cfg.vgId);
  pCreate->cfg.maxSessions = htonl(pCreate->cfg.maxSessions);
  pCreate->cfg.daysPerFile = htonl(pCreate->cfg.daysPerFile);
S
slguan 已提交
313

S
slguan 已提交
314
  SVnodeObj *pVnodeObj = (SVnodeObj *) taosGetIntHashData(tsDnodeVnodesHash, pCreate->cfg.vgId);
S
slguan 已提交
315 316 317 318 319
  if (pVnodeObj != NULL) {
    rpcRsp.code = TSDB_CODE_SUCCESS;
  } else {
    rpcRsp.code = dnodeCreateVnode(pCreate);;
  }
S
#1177  
slguan 已提交
320

S
slguan 已提交
321
  rpcSendResponse(&rpcRsp);
S
slguan 已提交
322
}
S
slguan 已提交
323 324

static void dnodeProcessAlterStreamMsg(SRpcMsg *pMsg) {
325
//  SMDAlterStreamMsg *pStream = pCont;
S
slguan 已提交
326 327 328 329 330 331 332
//  pStream->uid    = htobe64(pStream->uid);
//  pStream->stime  = htobe64(pStream->stime);
//  pStream->vnode  = htonl(pStream->vnode);
//  pStream->sid    = htonl(pStream->sid);
//  pStream->status = htonl(pStream->status);
//
//  int32_t code = dnodeCreateStream(pStream);
S
slguan 已提交
333 334 335
}

static void dnodeProcessConfigDnodeMsg(SRpcMsg *pMsg) {
S
slguan 已提交
336 337 338 339 340 341 342 343 344 345 346 347 348 349
//  SCfgDnodeMsg *pCfg = (SCfgDnodeMsg *)pCont;
//
//  int32_t code = tsCfgDynamicOptions(pCfg->config);
//  dnodeSendRspToMnode(pConn, msgType + 1, code, NULL, 0);
}


static void dnodeSendStatusMsg(void *handle, void *tmrId) {
  taosTmrReset(dnodeSendStatusMsg, tsStatusInterval * 1000, NULL, tsDnodeTmr, &tsStatusTimer);
  if (tsStatusTimer == NULL) {
    dError("failed to start status timer");
    return;
  }

350 351
//  int32_t contLen = sizeof(SDMStatusMsg) + dnodeGetVnodesNum() * sizeof(SVnodeLoad);
//  SDMStatusMsg *pStatus = rpcMallocCont(contLen);
S
slguan 已提交
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
//  if (pStatus == NULL) {
//    dError("Failed to malloc status message");
//    return;
//  }
//
//  int32_t totalVnodes = dnodeGetVnodesNum();
//
//  pStatus->version          = htonl(tsVersion);
//  pStatus->privateIp        = htonl(inet_addr(tsPrivateIp));
//  pStatus->publicIp         = htonl(inet_addr(tsPublicIp));
//  pStatus->lastReboot       = htonl(tsRebootTime);
//  pStatus->numOfTotalVnodes = htons((uint16_t) tsNumOfTotalVnodes);
//  pStatus->openVnodes       = htons((uint16_t) totalVnodes);
//  pStatus->numOfCores       = htons((uint16_t) tsNumOfCores);
//  pStatus->diskAvailable    = tsAvailDataDirGB;
//  pStatus->alternativeRole  = (uint8_t) tsAlternativeRole;
//
//  SVnodeLoad *pLoad = (SVnodeLoad *)pStatus->load;

  //TODO loop all vnodes
  //  for (int32_t vnode = 0, count = 0; vnode <= totalVnodes; ++vnode) {
  //    if (vnodeList[vnode].cfg.maxSessions <= 0) continue;
  //
  //    SVnodeObj *pVnode = vnodeList + vnode;
  //    pLoad->vnode = htonl(vnode);
  //    pLoad->vgId = htonl(pVnode->cfg.vgId);
  //    pLoad->status = (uint8_t)vnodeList[vnode].vnodeStatus;
  //    pLoad->syncStatus =(uint8_t)vnodeList[vnode].syncStatus;
  //    pLoad->accessState = (uint8_t)(pVnode->accessState);
  //    pLoad->totalStorage = htobe64(pVnode->vnodeStatistic.totalStorage);
  //    pLoad->compStorage = htobe64(pVnode->vnodeStatistic.compStorage);
  //    if (pVnode->vnodeStatus == TSDB_VN_STATUS_MASTER) {
  //      pLoad->pointsWritten = htobe64(pVnode->vnodeStatistic.pointsWritten);
  //    } else {
  //      pLoad->pointsWritten = htobe64(0);
  //    }
  //    pLoad++;
  //
  //    if (++count >= tsOpenVnodes) {
  //      break;
  //    }
  //  }

//  dnodeSendMsgToMnode(TSDB_MSG_TYPE_STATUS, pStatus, contLen);
}


S
slguan 已提交
399