dmInt.c 4.6 KB
Newer Older
S
shm  
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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/>.
 */

#define _DEFAULT_SOURCE
#include "dmInt.h"
S
shm  
Shengliang Guan 已提交
18

S
shm  
Shengliang Guan 已提交
19
void dmGetMnodeEpSet(SDnodeMgmt *pMgmt, SEpSet *pEpSet) {
S
shm  
Shengliang Guan 已提交
20 21 22 23 24
  taosRLockLatch(&pMgmt->latch);
  *pEpSet = pMgmt->mnodeEpSet;
  taosRUnLockLatch(&pMgmt->latch);
}

S
shm  
Shengliang Guan 已提交
25
void dmUpdateMnodeEpSet(SDnodeMgmt *pMgmt, SEpSet *pEpSet) {
S
shm  
Shengliang Guan 已提交
26 27 28 29 30 31 32 33 34 35 36
  dInfo("mnode is changed, num:%d use:%d", pEpSet->numOfEps, pEpSet->inUse);

  taosWLockLatch(&pMgmt->latch);
  pMgmt->mnodeEpSet = *pEpSet;
  for (int32_t i = 0; i < pEpSet->numOfEps; ++i) {
    dInfo("mnode index:%d %s:%u", i, pEpSet->eps[i].fqdn, pEpSet->eps[i].port);
  }

  taosWUnLockLatch(&pMgmt->latch);
}

S
shm  
Shengliang Guan 已提交
37 38
void dmGetDnodeEp(SMgmtWrapper *pWrapper, int32_t dnodeId, char *pEp, char *pFqdn, uint16_t *pPort) {
  SDnodeMgmt *pMgmt = pWrapper->pMgmt;
S
shm  
Shengliang Guan 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
  taosRLockLatch(&pMgmt->latch);

  SDnodeEp *pDnodeEp = taosHashGet(pMgmt->dnodeHash, &dnodeId, sizeof(int32_t));
  if (pDnodeEp != NULL) {
    if (pPort != NULL) {
      *pPort = pDnodeEp->ep.port;
    }
    if (pFqdn != NULL) {
      tstrncpy(pFqdn, pDnodeEp->ep.fqdn, TSDB_FQDN_LEN);
    }
    if (pEp != NULL) {
      snprintf(pEp, TSDB_EP_LEN, "%s:%u", pDnodeEp->ep.fqdn, pDnodeEp->ep.port);
    }
  }

  taosRUnLockLatch(&pMgmt->latch);
}

S
shm  
Shengliang Guan 已提交
57
void dmSendRedirectRsp(SDnodeMgmt *pMgmt, const SRpcMsg *pReq) {
S
shm  
Shengliang Guan 已提交
58
  SDnode *pDnode = pMgmt->pDnode;
S
shm  
Shengliang Guan 已提交
59 60

  SEpSet epSet = {0};
S
shm  
Shengliang Guan 已提交
61
  dmGetMnodeEpSet(pMgmt, &epSet);
S
shm  
Shengliang Guan 已提交
62

S
shm  
Shengliang Guan 已提交
63
  dDebug("RPC %p, req is redirected, num:%d use:%d", pReq->handle, epSet.numOfEps, epSet.inUse);
S
shm  
Shengliang Guan 已提交
64 65
  for (int32_t i = 0; i < epSet.numOfEps; ++i) {
    dDebug("mnode index:%d %s:%u", i, epSet.eps[i].fqdn, epSet.eps[i].port);
S
shm  
Shengliang Guan 已提交
66
    if (strcmp(epSet.eps[i].fqdn, pDnode->localFqdn) == 0 && epSet.eps[i].port == pDnode->serverPort) {
S
shm  
Shengliang Guan 已提交
67 68 69 70 71 72 73 74 75
      epSet.inUse = (i + 1) % epSet.numOfEps;
    }

    epSet.eps[i].port = htons(epSet.eps[i].port);
  }

  rpcSendRedirectRsp(pReq->handle, &epSet);
}

S
shm  
Shengliang Guan 已提交
76
static int32_t dmStart(SMgmtWrapper *pWrapper) {
S
Shengliang Guan 已提交
77
  dDebug("dnode-mgmt start to run");
S
shm  
Shengliang Guan 已提交
78 79
  return dmStartThread(pWrapper->pMgmt);
}
S
shm  
Shengliang Guan 已提交
80

S
Shengliang Guan 已提交
81
static int32_t dmInit(SMgmtWrapper *pWrapper) {
S
shm  
Shengliang Guan 已提交
82
  SDnode     *pDnode = pWrapper->pDnode;
wafwerar's avatar
wafwerar 已提交
83
  SDnodeMgmt *pMgmt = taosMemoryCalloc(1, sizeof(SDnodeMgmt));
S
Shengliang Guan 已提交
84
  dInfo("dnode-mgmt start to init");
S
shm  
Shengliang Guan 已提交
85

S
shm  
Shengliang Guan 已提交
86 87 88
  pDnode->dnodeId = 0;
  pDnode->dropped = 0;
  pDnode->clusterId = 0;
S
shm  
Shengliang Guan 已提交
89 90
  pMgmt->path = pWrapper->path;
  pMgmt->pDnode = pDnode;
S
shm  
Shengliang Guan 已提交
91
  pMgmt->pWrapper = pWrapper;
S
shm  
Shengliang Guan 已提交
92 93 94 95
  taosInitRWLatch(&pMgmt->latch);

  pMgmt->dnodeHash = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK);
  if (pMgmt->dnodeHash == NULL) {
S
shm  
Shengliang Guan 已提交
96
    dError("failed to init dnode hash");
S
shm  
Shengliang Guan 已提交
97 98 99 100 101
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

  if (dmReadFile(pMgmt) != 0) {
S
shm  
Shengliang Guan 已提交
102
    dError("failed to read file since %s", terrstr());
S
shm  
Shengliang Guan 已提交
103 104 105
    return -1;
  }

S
shm  
Shengliang Guan 已提交
106
  if (pDnode->dropped) {
S
shm  
Shengliang Guan 已提交
107
    dError("dnode will not start since its already dropped");
S
shm  
Shengliang Guan 已提交
108 109 110
    return -1;
  }

S
shm  
Shengliang Guan 已提交
111 112 113 114
  if (dmStartWorker(pMgmt) != 0) {
    return -1;
  }

S
Shengliang Guan 已提交
115 116
  if (dndInitTrans(pDnode) != 0) {
    dError("failed to init transport since %s", terrstr());
S
shm  
Shengliang Guan 已提交
117 118 119
    return -1;
  }

S
shm  
Shengliang Guan 已提交
120
  pWrapper->pMgmt = pMgmt;
S
Shengliang Guan 已提交
121 122
  pMgmt->msgCb = dndCreateMsgcb(pWrapper);

S
shm  
Shengliang Guan 已提交
123
  dInfo("dnode-mgmt is initialized");
S
shm  
Shengliang Guan 已提交
124 125 126
  return 0;
}

S
Shengliang Guan 已提交
127
static void dmCleanup(SMgmtWrapper *pWrapper) {
S
shm  
Shengliang Guan 已提交
128 129 130
  SDnodeMgmt *pMgmt = pWrapper->pMgmt;
  if (pMgmt == NULL) return;

S
shm  
Shengliang Guan 已提交
131
  dInfo("dnode-mgmt start to clean up");
S
shm  
Shengliang Guan 已提交
132
  SDnode *pDnode = pMgmt->pDnode;
S
shm  
Shengliang Guan 已提交
133 134 135 136
  dmStopWorker(pMgmt);

  taosWLockLatch(&pMgmt->latch);

S
shm  
Shengliang Guan 已提交
137 138 139
  if (pMgmt->dnodeEps != NULL) {
    taosArrayDestroy(pMgmt->dnodeEps);
    pMgmt->dnodeEps = NULL;
S
shm  
Shengliang Guan 已提交
140 141 142 143 144 145 146 147
  }

  if (pMgmt->dnodeHash != NULL) {
    taosHashCleanup(pMgmt->dnodeHash);
    pMgmt->dnodeHash = NULL;
  }

  taosWUnLockLatch(&pMgmt->latch);
S
shm  
Shengliang Guan 已提交
148

wafwerar's avatar
wafwerar 已提交
149
  taosMemoryFree(pMgmt);
S
shm  
Shengliang Guan 已提交
150
  pWrapper->pMgmt = NULL;
S
Shengliang Guan 已提交
151
  dndCleanupTrans(pDnode);
S
shm  
Shengliang Guan 已提交
152

S
shm  
Shengliang Guan 已提交
153 154 155
  dInfo("dnode-mgmt is cleaned up");
}

S
Shengliang Guan 已提交
156
static int32_t dmRequire(SMgmtWrapper *pWrapper, bool *required) {
S
shm  
Shengliang Guan 已提交
157 158 159
  *required = true;
  return 0;
}
S
shm  
Shengliang Guan 已提交
160

S
Shengliang Guan 已提交
161
void dmSetMgmtFp(SMgmtWrapper *pWrapper) {
S
shm  
Shengliang Guan 已提交
162 163 164
  SMgmtFp mgmtFp = {0};
  mgmtFp.openFp = dmInit;
  mgmtFp.closeFp = dmCleanup;
S
shm  
Shengliang Guan 已提交
165
  mgmtFp.startFp = dmStart;
S
shm  
Shengliang Guan 已提交
166
  mgmtFp.requiredFp = dmRequire;
S
shm  
Shengliang Guan 已提交
167

S
Shengliang Guan 已提交
168
  dmInitMsgHandle(pWrapper);
S
shm  
Shengliang Guan 已提交
169 170
  pWrapper->name = "dnode";
  pWrapper->fp = mgmtFp;
S
shm  
Shengliang Guan 已提交
171
}