You need to sign in or sign up before continuing.
mndSync.c 6.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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
S
Shengliang Guan 已提交
17
#include "mndSync.h"
18
#include "mndTrans.h"
19

M
Minghao Li 已提交
20 21 22 23 24 25 26
int32_t mndSyncEqMsg(const SMsgCb *msgcb, SRpcMsg *pMsg) { 
  SMsgHead *pHead = pMsg->pCont;
  pHead->contLen = htonl(pHead->contLen);
  pHead->vgId = htonl(pHead->vgId);

  return tmsgPutToQueue(msgcb, SYNC_QUEUE, pMsg); 
}
M
Minghao Li 已提交
27

28
int32_t mndSyncSendMsg(const SEpSet *pEpSet, SRpcMsg *pMsg) { return tmsgSendReq(pEpSet, pMsg); }
M
Minghao Li 已提交
29

30
void mndSyncCommitMsg(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SFsmCbMeta cbMeta) {
S
Shengliang Guan 已提交
31 32
  SMnode  *pMnode = pFsm->data;
  SSdbRaw *pRaw = pMsg->pCont;
M
Minghao Li 已提交
33

S
Shengliang Guan 已提交
34
  mTrace("raw:%p, apply to sdb, ver:%" PRId64 " role:%s", pRaw, cbMeta.index, syncStr(cbMeta.state));
S
Shengliang Guan 已提交
35 36 37
  sdbWriteWithoutFree(pMnode->pSdb, pRaw);
  sdbSetApplyIndex(pMnode->pSdb, cbMeta.index);
  sdbSetApplyTerm(pMnode->pSdb, cbMeta.term);
38
  if (cbMeta.state == TAOS_SYNC_STATE_LEADER) {
S
Shengliang Guan 已提交
39
    tsem_post(&pMnode->syncMgmt.syncSem);
M
Minghao Li 已提交
40 41 42
  }
}

43
int32_t mndSyncGetSnapshot(struct SSyncFSM *pFsm, SSnapshot *pSnapshot) {
44 45
  SMnode *pMnode = pFsm->data;
  pSnapshot->lastApplyIndex = sdbGetApplyIndex(pMnode->pSdb);
46
  pSnapshot->lastApplyTerm = sdbGetApplyTerm(pMnode->pSdb);
M
Minghao Li 已提交
47 48 49
  return 0;
}

50
void mndRestoreFinish(struct SSyncFSM *pFsm) {
51
  SMnode *pMnode = pFsm->data;
S
Shengliang Guan 已提交
52 53 54 55
  if (!pMnode->deploy) {
    mndTransPullup(pMnode);
    pMnode->syncMgmt.restored = true;
  }
56 57
}

58
int32_t mndSnapshotRead(struct SSyncFSM* pFsm, const SSnapshot* pSnapshot, void** ppIter, char** ppBuf, int32_t* len) {
59 60 61
  /*
  SMnode *pMnode = pFsm->data;
  SSdbIter *pIter;
62
  if (iter == NULL) { 
63 64 65 66 67 68
    pIter = sdbIterInit(pMnode->sdb)
  } else {
    pIter = iter;
  }
  */

69
  return 0;
70 71
}

72
int32_t mndSnapshotApply(struct SSyncFSM* pFsm, const SSnapshot* pSnapshot, char* pBuf, int32_t len) {
73 74 75 76
  SMnode *pMnode = pFsm->data;
  sdbWrite(pMnode->pSdb, (SSdbRaw*)pBuf);
  return 0;
}
S
Shengliang Guan 已提交
77 78 79 80 81 82 83

void mndReConfig(struct SSyncFSM *pFsm, SSyncCfg newCfg, SReConfigCbMeta cbMeta) {
  mInfo("mndReConfig cbMeta.code:%d, cbMeta.currentTerm:%" PRId64 ", cbMeta.term:%" PRId64 ", cbMeta.index:%" PRId64,
        cbMeta.code, cbMeta.currentTerm, cbMeta.term, cbMeta.index);
  SMnode *pMnode = pFsm->data;
  pMnode->syncMgmt.errCode = cbMeta.code;
  tsem_post(&pMnode->syncMgmt.syncSem);
84 85
}

86 87
SSyncFSM *mndSyncMakeFsm(SMnode *pMnode) {
  SSyncFSM *pFsm = taosMemoryCalloc(1, sizeof(SSyncFSM));
M
Minghao Li 已提交
88
  pFsm->data = pMnode;
89

90
  pFsm->FpCommitCb = mndSyncCommitMsg;
91 92
  pFsm->FpPreCommitCb = NULL;
  pFsm->FpRollBackCb = NULL;
93

94
  pFsm->FpGetSnapshot = mndSyncGetSnapshot;
95 96 97 98 99
  pFsm->FpRestoreFinishCb = mndRestoreFinish;
  pFsm->FpSnapshotRead = mndSnapshotRead;
  pFsm->FpSnapshotApply = mndSnapshotApply;
  pFsm->FpReConfigCb = mndReConfig;
  
M
Minghao Li 已提交
100
  return pFsm;
101 102 103 104 105
}

int32_t mndInitSync(SMnode *pMnode) {
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;

106 107 108 109 110 111 112 113 114 115 116 117 118 119
  char path[PATH_MAX + 20] = {0};
  snprintf(path, sizeof(path), "%s%swal", pMnode->path, TD_DIRSEP);
  SWalCfg cfg = {
      .vgId = 1,
      .fsyncPeriod = 0,
      .rollPeriod = -1,
      .segSize = -1,
      .retentionPeriod = -1,
      .retentionSize = -1,
      .level = TAOS_WAL_FSYNC,
  };

  pMgmt->pWal = walOpen(path, &cfg);
  if (pMgmt->pWal == NULL) {
120 121 122 123
    mError("failed to open wal since %s", terrstr());
    return -1;
  }

124 125 126 127 128 129
  SSyncInfo syncInfo = {.vgId = 1, .FpSendMsg = mndSyncSendMsg, .FpEqMsg = mndSyncEqMsg};
  snprintf(syncInfo.path, sizeof(syncInfo.path), "%s%ssync", pMnode->path, TD_DIRSEP);
  syncInfo.pWal = pMgmt->pWal;
  syncInfo.pFsm = mndSyncMakeFsm(pMnode);

  SSyncCfg *pCfg = &syncInfo.syncCfg;
M
Minghao Li 已提交
130 131
  pCfg->replicaNum = pMnode->replica;
  pCfg->myIndex = pMnode->selfIndex;
S
Shengliang Guan 已提交
132 133
  mInfo("start to open mnode sync, replica:%d myindex:%d standby:%d", pCfg->replicaNum, pCfg->myIndex,
        pMgmt->standby);
134
  for (int32_t i = 0; i < pMnode->replica; ++i) {
135 136 137
    SNodeInfo *pNode = &pCfg->nodeInfo[i];
    tstrncpy(pNode->nodeFqdn, pMnode->replicas[i].fqdn, sizeof(pNode->nodeFqdn));
    pNode->nodePort = pMnode->replicas[i].port;
S
Shengliang Guan 已提交
138
    mInfo("index:%d, fqdn:%s port:%d", i, pNode->nodeFqdn, pNode->nodePort);
M
Minghao Li 已提交
139 140
  }

141
  tsem_init(&pMgmt->syncSem, 0, 0);
142 143 144 145 146
  pMgmt->sync = syncOpen(&syncInfo);
  if (pMgmt->sync <= 0) {
    mError("failed to open sync since %s", terrstr());
    return -1;
  }
M
Minghao Li 已提交
147

148
  mDebug("mnode sync is opened, id:%" PRId64, pMgmt->sync);
S
Shengliang Guan 已提交
149 150 151 152 153
  return 0;
}

void mndCleanupSync(SMnode *pMnode) {
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
154 155 156
  syncStop(pMgmt->sync);
  mDebug("sync:%" PRId64 " is stopped", pMgmt->sync);

157
  tsem_destroy(&pMgmt->syncSem);
158 159
  if (pMgmt->pWal != NULL) {
    walClose(pMgmt->pWal);
S
Shengliang Guan 已提交
160 161
  }

162 163
  memset(pMgmt, 0, sizeof(SSyncMgmt));
}
M
Minghao Li 已提交
164

165
int32_t mndSyncPropose(SMnode *pMnode, SSdbRaw *pRaw) {
S
Shengliang Guan 已提交
166 167 168
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
  pMgmt->errCode = 0;

169
  SRpcMsg rsp = {.code = TDMT_MND_APPLY_MSG, .contLen = sdbGetRawTotalSize(pRaw)};
170
  rsp.pCont = rpcMallocCont(rsp.contLen);
171
  if (rsp.pCont == NULL) return -1;
172
  memcpy(rsp.pCont, pRaw, rsp.contLen);
S
Shengliang Guan 已提交
173

174 175
  const bool isWeak = false;
  int32_t    code = syncPropose(pMgmt->sync, &rsp, isWeak);
176 177 178 179 180 181 182
  if (code == 0) {
    tsem_wait(&pMgmt->syncSem);
  } else if (code == TAOS_SYNC_PROPOSE_NOT_LEADER) {
    terrno = TSDB_CODE_APP_NOT_READY;
  } else if (code == TAOS_SYNC_PROPOSE_OTHER_ERROR) {
    terrno = TSDB_CODE_SYN_INTERNAL_ERROR;
  } else {
183
    terrno = TSDB_CODE_APP_ERROR;
184
  }
185

186
  rpcFreeCont(rsp.pCont);
S
Shengliang Guan 已提交
187 188
  if (code != 0) return code;
  return pMgmt->errCode;
189 190
}

191
void mndSyncStart(SMnode *pMnode) {
192 193
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
  syncSetMsgCb(pMgmt->sync, &pMnode->msgCb);
S
Shengliang Guan 已提交
194
  if (pMgmt->standby) {
M
Minghao Li 已提交
195 196 197 198
    syncStartStandBy(pMgmt->sync);
  } else {
    syncStart(pMgmt->sync);
  }
199
  mDebug("sync:%" PRId64 " is started", pMgmt->sync);
200 201
}

202
void mndSyncStop(SMnode *pMnode) {}
203

S
Shengliang Guan 已提交
204
bool mndIsMaster(SMnode *pMnode) {
S
Shengliang Guan 已提交
205
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
206 207
  ESyncState state = syncGetMyRole(pMgmt->sync);
  return (state == TAOS_SYNC_STATE_LEADER) && (pMnode->syncMgmt.restored);
L
Liu Jicong 已提交
208
}