mndSync.c 7.2 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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
  SMnode    *pMnode = pFsm->data;
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
  SSdbRaw   *pRaw = pMsg->pCont;

  int32_t transId = sdbGetIdFromRaw(pRaw);
  pMgmt->errCode = cbMeta.code;
  mTrace("trans:%d, is proposed, savedTransId:%d code:0x%x, ver:%" PRId64 " term:%" PRId64 " role:%s raw:%p", transId,
         pMgmt->transId, cbMeta.code, cbMeta.index, cbMeta.term, syncStr(cbMeta.state), pRaw);

  if (pMgmt->errCode == 0) {
    sdbWriteWithoutFree(pMnode->pSdb, pRaw);
    sdbSetApplyIndex(pMnode->pSdb, cbMeta.index);
    sdbSetApplyTerm(pMnode->pSdb, cbMeta.term);
  }

  if (pMgmt->transId == transId) {
    if (pMgmt->errCode != 0) {
      mError("trans:%d, failed to propose since %s", transId, tstrerror(pMgmt->errCode));
    }
    tsem_post(&pMgmt->syncSem);
M
Minghao Li 已提交
51 52 53
  }
}

54
int32_t mndSyncGetSnapshot(struct SSyncFSM *pFsm, SSnapshot *pSnapshot) {
55 56
  SMnode *pMnode = pFsm->data;
  pSnapshot->lastApplyIndex = sdbGetApplyIndex(pMnode->pSdb);
57
  pSnapshot->lastApplyTerm = sdbGetApplyTerm(pMnode->pSdb);
M
Minghao Li 已提交
58 59 60
  return 0;
}

61
void mndRestoreFinish(struct SSyncFSM *pFsm) {
62
  SMnode *pMnode = pFsm->data;
S
Shengliang Guan 已提交
63
  if (!pMnode->deploy) {
S
Shengliang Guan 已提交
64
    mInfo("mnode sync restore finished");
S
Shengliang Guan 已提交
65 66 67
    mndTransPullup(pMnode);
    pMnode->syncMgmt.restored = true;
  }
68 69
}

70
int32_t mndSnapshotRead(struct SSyncFSM* pFsm, const SSnapshot* pSnapshot, void** ppIter, char** ppBuf, int32_t* len) {
71 72 73
  /*
  SMnode *pMnode = pFsm->data;
  SSdbIter *pIter;
74
  if (iter == NULL) { 
75 76 77 78 79 80
    pIter = sdbIterInit(pMnode->sdb)
  } else {
    pIter = iter;
  }
  */

81
  return 0;
82 83
}

84
int32_t mndSnapshotApply(struct SSyncFSM* pFsm, const SSnapshot* pSnapshot, char* pBuf, int32_t len) {
85 86 87 88
  SMnode *pMnode = pFsm->data;
  sdbWrite(pMnode->pSdb, (SSdbRaw*)pBuf);
  return 0;
}
S
Shengliang Guan 已提交
89 90

void mndReConfig(struct SSyncFSM *pFsm, SSyncCfg newCfg, SReConfigCbMeta cbMeta) {
S
Shengliang Guan 已提交
91 92 93 94 95 96 97 98 99 100 101 102 103
  SMnode    *pMnode = pFsm->data;
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;

  pMgmt->errCode = cbMeta.code;
  mInfo("trans:-1, sync reconfig is proposed, savedTransId:%d code:0x%x, curTerm:%" PRId64 " term:%" PRId64,
        pMgmt->transId, cbMeta.code, cbMeta.index, cbMeta.term);

  if (pMgmt->transId == -1) {
    if (pMgmt->errCode != 0) {
      mError("trans:-1, failed to propose sync reconfig since %s", tstrerror(pMgmt->errCode));
    }
    tsem_post(&pMgmt->syncSem);
  }
104 105
}

106 107
SSyncFSM *mndSyncMakeFsm(SMnode *pMnode) {
  SSyncFSM *pFsm = taosMemoryCalloc(1, sizeof(SSyncFSM));
M
Minghao Li 已提交
108
  pFsm->data = pMnode;
109

110
  pFsm->FpCommitCb = mndSyncCommitMsg;
111 112
  pFsm->FpPreCommitCb = NULL;
  pFsm->FpRollBackCb = NULL;
113

114
  pFsm->FpGetSnapshot = mndSyncGetSnapshot;
115 116 117 118 119
  pFsm->FpRestoreFinishCb = mndRestoreFinish;
  pFsm->FpSnapshotRead = mndSnapshotRead;
  pFsm->FpSnapshotApply = mndSnapshotApply;
  pFsm->FpReConfigCb = mndReConfig;
  
M
Minghao Li 已提交
120
  return pFsm;
121 122 123 124 125
}

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

126 127 128 129 130 131 132 133 134 135 136 137 138 139
  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) {
140 141 142 143
    mError("failed to open wal since %s", terrstr());
    return -1;
  }

144 145 146 147
  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);
M
Minghao Li 已提交
148
  syncInfo.isStandBy = pMgmt->standby;
149 150

  SSyncCfg *pCfg = &syncInfo.syncCfg;
M
Minghao Li 已提交
151 152
  pCfg->replicaNum = pMnode->replica;
  pCfg->myIndex = pMnode->selfIndex;
S
Shengliang Guan 已提交
153 154
  mInfo("start to open mnode sync, replica:%d myindex:%d standby:%d", pCfg->replicaNum, pCfg->myIndex,
        pMgmt->standby);
155
  for (int32_t i = 0; i < pMnode->replica; ++i) {
156 157 158
    SNodeInfo *pNode = &pCfg->nodeInfo[i];
    tstrncpy(pNode->nodeFqdn, pMnode->replicas[i].fqdn, sizeof(pNode->nodeFqdn));
    pNode->nodePort = pMnode->replicas[i].port;
S
Shengliang Guan 已提交
159
    mInfo("index:%d, fqdn:%s port:%d", i, pNode->nodeFqdn, pNode->nodePort);
M
Minghao Li 已提交
160 161
  }

162
  tsem_init(&pMgmt->syncSem, 0, 0);
163 164 165 166 167
  pMgmt->sync = syncOpen(&syncInfo);
  if (pMgmt->sync <= 0) {
    mError("failed to open sync since %s", terrstr());
    return -1;
  }
M
Minghao Li 已提交
168

169
  mDebug("mnode sync is opened, id:%" PRId64, pMgmt->sync);
S
Shengliang Guan 已提交
170 171 172 173 174
  return 0;
}

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

178
  tsem_destroy(&pMgmt->syncSem);
179 180
  if (pMgmt->pWal != NULL) {
    walClose(pMgmt->pWal);
S
Shengliang Guan 已提交
181 182
  }

183 184
  memset(pMgmt, 0, sizeof(SSyncMgmt));
}
M
Minghao Li 已提交
185

S
Shengliang Guan 已提交
186
int32_t mndSyncPropose(SMnode *pMnode, SSdbRaw *pRaw, int32_t transId) {
S
Shengliang Guan 已提交
187
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
S
Shengliang Guan 已提交
188
  SRpcMsg    rsp = {.code = TDMT_MND_APPLY_MSG, .contLen = sdbGetRawTotalSize(pRaw)};
189
  rsp.pCont = rpcMallocCont(rsp.contLen);
190
  if (rsp.pCont == NULL) return -1;
191
  memcpy(rsp.pCont, pRaw, rsp.contLen);
S
Shengliang Guan 已提交
192

S
Shengliang Guan 已提交
193 194 195 196
  pMgmt->errCode = 0;
  pMgmt->transId = transId;
  mTrace("trans:%d, will be proposed", pMgmt->transId);

197 198
  const bool isWeak = false;
  int32_t    code = syncPropose(pMgmt->sync, &rsp, isWeak);
199 200 201 202 203 204 205
  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 {
206
    terrno = TSDB_CODE_APP_ERROR;
207
  }
208

209
  rpcFreeCont(rsp.pCont);
S
Shengliang Guan 已提交
210 211 212 213 214
  if (code != 0) {
    mError("trans:%d, failed to propose, code:0x%x", pMgmt->transId, code);
    return code;
  }

S
Shengliang Guan 已提交
215
  return pMgmt->errCode;
216 217
}

218
void mndSyncStart(SMnode *pMnode) {
219 220
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
  syncSetMsgCb(pMgmt->sync, &pMnode->msgCb);
M
Minghao Li 已提交
221

S
Shengliang Guan 已提交
222
  if (pMgmt->standby) {
M
Minghao Li 已提交
223 224 225 226
    syncStartStandBy(pMgmt->sync);
  } else {
    syncStart(pMgmt->sync);
  }
227
  mDebug("sync:%" PRId64 " is started, standby:%d", pMgmt->sync, pMgmt->standby);
228 229
}

230
void mndSyncStop(SMnode *pMnode) {}
231

S
Shengliang Guan 已提交
232
bool mndIsMaster(SMnode *pMnode) {
S
Shengliang Guan 已提交
233
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
234

235
  ESyncState state = syncGetMyRole(pMgmt->sync);
236 237 238 239 240 241 242 243 244 245 246
  if (state != TAOS_SYNC_STATE_LEADER) {
    terrno = TSDB_CODE_SYN_NOT_LEADER;
    return false;
  }

  if (!pMgmt->restored) {
    terrno = TSDB_CODE_APP_NOT_READY;
    return false;
  }

  return true;
L
Liu Jicong 已提交
247
}