mndSync.c 9.8 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

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

25 26 27 28 29 30
  int32_t code = tmsgPutToQueue(msgcb, SYNC_QUEUE, pMsg);
  if (code != 0) {
    rpcFreeCont(pMsg->pCont);
    pMsg->pCont = NULL;
  }
  return code;
M
Minghao Li 已提交
31
}
M
Minghao Li 已提交
32

33 34 35 36 37 38 39 40
static int32_t mndSyncSendMsg(const SEpSet *pEpSet, SRpcMsg *pMsg) {
  int32_t code = tmsgSendReq(pEpSet, pMsg);
  if (code != 0) {
    rpcFreeCont(pMsg->pCont);
    pMsg->pCont = NULL;
  }
  return code;
}
M
Minghao Li 已提交
41

42
void mndSyncCommitMsg(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SFsmCbMeta cbMeta) {
S
Shengliang Guan 已提交
43 44 45 46
  SMnode    *pMnode = pFsm->data;
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
  SSdbRaw   *pRaw = pMsg->pCont;

47 48 49 50
  // delete msg handle
  SRpcMsg rpcMsg = {0};
  syncGetAndDelRespRpc(pMnode->syncMgmt.sync, cbMeta.seqNum, &rpcMsg.info);

S
Shengliang Guan 已提交
51
  int32_t transId = sdbGetIdFromRaw(pMnode->pSdb, pRaw);
S
Shengliang Guan 已提交
52
  pMgmt->errCode = cbMeta.code;
53 54 55 56
  mDebug("trans:%d, is proposed, saved:%d code:0x%x, apply index:%" PRId64 " term:%" PRIu64 " config:%" PRId64
         " role:%s raw:%p",
         transId, pMgmt->transId, cbMeta.code, cbMeta.index, cbMeta.term, cbMeta.lastConfigIndex, syncStr(cbMeta.state),
         pRaw);
S
Shengliang Guan 已提交
57 58 59

  if (pMgmt->errCode == 0) {
    sdbWriteWithoutFree(pMnode->pSdb, pRaw);
60
    sdbSetApplyInfo(pMnode->pSdb, cbMeta.index, cbMeta.term, cbMeta.lastConfigIndex);
S
Shengliang Guan 已提交
61 62
  }

63 64 65
  if (transId <= 0) {
    mError("trans:%d, invalid commit msg", transId);
  } else if (transId == pMgmt->transId) {
S
Shengliang Guan 已提交
66 67 68
    if (pMgmt->errCode != 0) {
      mError("trans:%d, failed to propose since %s", transId, tstrerror(pMgmt->errCode));
    }
S
Shengliang Guan 已提交
69
    pMgmt->transId = 0;
S
Shengliang Guan 已提交
70
    tsem_post(&pMgmt->syncSem);
71
  } else {
72 73
    STrans *pTrans = mndAcquireTrans(pMnode, transId);
    if (pTrans != NULL) {
74
      mDebug("trans:%d, execute in mnode which not leader", transId);
75 76
      mndTransExecute(pMnode, pTrans);
      mndReleaseTrans(pMnode, pTrans);
77 78 79
      // sdbWriteFile(pMnode->pSdb, SDB_WRITE_DELTA);
    } else {
      mError("trans:%d, not found while execute in mnode since %s", transId, terrstr());
80
    }
M
Minghao Li 已提交
81 82 83
  }
}

84
int32_t mndSyncGetSnapshot(struct SSyncFSM *pFsm, SSnapshot *pSnapshot, void *pReaderParam, void **ppReader) {
S
Shengliang Guan 已提交
85
  mDebug("start to read snapshot from sdb in atomic way");
86 87 88
  SMnode *pMnode = pFsm->data;
  return sdbStartRead(pMnode->pSdb, (SSdbIter **)ppReader, &pSnapshot->lastApplyIndex, &pSnapshot->lastApplyTerm,
                      &pSnapshot->lastConfigIndex);
89 90 91 92
  return 0;
}

int32_t mndSyncGetSnapshotInfo(struct SSyncFSM *pFsm, SSnapshot *pSnapshot) {
93
  SMnode *pMnode = pFsm->data;
94
  sdbGetCommitInfo(pMnode->pSdb, &pSnapshot->lastApplyIndex, &pSnapshot->lastApplyTerm, &pSnapshot->lastConfigIndex);
M
Minghao Li 已提交
95 96 97
  return 0;
}

98
void mndRestoreFinish(struct SSyncFSM *pFsm) {
99
  SMnode *pMnode = pFsm->data;
S
Shengliang Guan 已提交
100

S
Shengliang Guan 已提交
101
  if (!pMnode->deploy) {
102
    mInfo("mnode sync restore finished, and will handle outstanding transactions");
S
Shengliang Guan 已提交
103
    mndTransPullup(pMnode);
S
Shengliang Guan 已提交
104
    mndSetRestore(pMnode, true);
S
Shengliang Guan 已提交
105
  } else {
S
Shengliang Guan 已提交
106
    mInfo("mnode sync restore finished");
S
Shengliang Guan 已提交
107
  }
108 109
}

110
void mndReConfig(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SReConfigCbMeta cbMeta) {
S
Shengliang Guan 已提交
111 112 113 114
  SMnode    *pMnode = pFsm->data;
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;

  pMgmt->errCode = cbMeta.code;
115 116
  mInfo("trans:-1, sync reconfig is proposed, saved:%d code:0x%x, index:%" PRId64 " term:%" PRId64, pMgmt->transId,
        cbMeta.code, cbMeta.index, cbMeta.term);
S
Shengliang Guan 已提交
117 118 119 120 121

  if (pMgmt->transId == -1) {
    if (pMgmt->errCode != 0) {
      mError("trans:-1, failed to propose sync reconfig since %s", tstrerror(pMgmt->errCode));
    }
S
Shengliang Guan 已提交
122
    pMgmt->transId = 0;
S
Shengliang Guan 已提交
123
    tsem_post(&pMgmt->syncSem);
S
Shengliang Guan 已提交
124
  }
125 126
}

127
int32_t mndSnapshotStartRead(struct SSyncFSM *pFsm, void *pParam, void **ppReader) {
S
Shengliang Guan 已提交
128
  mDebug("start to read snapshot from sdb");
S
Shengliang Guan 已提交
129
  SMnode *pMnode = pFsm->data;
130
  return sdbStartRead(pMnode->pSdb, (SSdbIter **)ppReader, NULL, NULL, NULL);
S
Shengliang Guan 已提交
131 132 133
}

int32_t mndSnapshotStopRead(struct SSyncFSM *pFsm, void *pReader) {
S
Shengliang Guan 已提交
134
  mDebug("stop to read snapshot from sdb");
S
Shengliang Guan 已提交
135 136 137 138 139 140 141 142 143
  SMnode *pMnode = pFsm->data;
  return sdbStopRead(pMnode->pSdb, pReader);
}

int32_t mndSnapshotDoRead(struct SSyncFSM *pFsm, void *pReader, void **ppBuf, int32_t *len) {
  SMnode *pMnode = pFsm->data;
  return sdbDoRead(pMnode->pSdb, pReader, ppBuf, len);
}

144
int32_t mndSnapshotStartWrite(struct SSyncFSM *pFsm, void *pParam, void **ppWriter) {
S
Shengliang Guan 已提交
145 146 147 148 149
  mInfo("start to apply snapshot to sdb");
  SMnode *pMnode = pFsm->data;
  return sdbStartWrite(pMnode->pSdb, (SSdbIter **)ppWriter);
}

150
int32_t mndSnapshotStopWrite(struct SSyncFSM *pFsm, void *pWriter, bool isApply, SSnapshot *pSnapshot) {
151 152
  mInfo("stop to apply snapshot to sdb, apply:%d, index:%" PRId64 " term:%" PRIu64 " config:%" PRId64, isApply,
        pSnapshot->lastApplyIndex, pSnapshot->lastApplyTerm, pSnapshot->lastApplyIndex);
S
Shengliang Guan 已提交
153
  SMnode *pMnode = pFsm->data;
154 155
  return sdbStopWrite(pMnode->pSdb, pWriter, isApply, pSnapshot->lastApplyIndex, pSnapshot->lastApplyTerm,
                      pSnapshot->lastConfigIndex);
S
Shengliang Guan 已提交
156 157 158 159 160 161 162
}

int32_t mndSnapshotDoWrite(struct SSyncFSM *pFsm, void *pWriter, void *pBuf, int32_t len) {
  SMnode *pMnode = pFsm->data;
  return sdbDoWrite(pMnode->pSdb, pWriter, pBuf, len);
}

163 164 165
void mndLeaderTransfer(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SFsmCbMeta cbMeta) {
  SMnode *pMnode = pFsm->data;
  atomic_store_8(&(pMnode->syncMgmt.leaderTransferFinish), 1);
166
  mDebug("vgId:1, mnode leader transfer finish");
167 168
}

169 170
SSyncFSM *mndSyncMakeFsm(SMnode *pMnode) {
  SSyncFSM *pFsm = taosMemoryCalloc(1, sizeof(SSyncFSM));
M
Minghao Li 已提交
171
  pFsm->data = pMnode;
172
  pFsm->FpCommitCb = mndSyncCommitMsg;
173 174
  pFsm->FpPreCommitCb = NULL;
  pFsm->FpRollBackCb = NULL;
175
  pFsm->FpRestoreFinishCb = mndRestoreFinish;
176
  pFsm->FpLeaderTransferCb = mndLeaderTransfer;
177
  pFsm->FpReConfigCb = mndReConfig;
S
Shengliang Guan 已提交
178
  pFsm->FpGetSnapshot = mndSyncGetSnapshot;
179
  pFsm->FpGetSnapshotInfo = mndSyncGetSnapshotInfo;
S
Shengliang Guan 已提交
180 181 182 183 184 185
  pFsm->FpSnapshotStartRead = mndSnapshotStartRead;
  pFsm->FpSnapshotStopRead = mndSnapshotStopRead;
  pFsm->FpSnapshotDoRead = mndSnapshotDoRead;
  pFsm->FpSnapshotStartWrite = mndSnapshotStartWrite;
  pFsm->FpSnapshotStopWrite = mndSnapshotStopWrite;
  pFsm->FpSnapshotDoWrite = mndSnapshotDoWrite;
M
Minghao Li 已提交
186
  return pFsm;
187 188 189 190 191
}

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

192 193
  SSyncInfo syncInfo = {.vgId = 1, .FpSendMsg = mndSyncSendMsg, .FpEqMsg = mndSyncEqMsg};
  snprintf(syncInfo.path, sizeof(syncInfo.path), "%s%ssync", pMnode->path, TD_DIRSEP);
194
  syncInfo.pWal = pMnode->pWal;
195
  syncInfo.pFsm = mndSyncMakeFsm(pMnode);
M
Minghao Li 已提交
196
  syncInfo.isStandBy = pMgmt->standby;
M
Minghao Li 已提交
197
  syncInfo.snapshotStrategy = SYNC_STRATEGY_STANDARD_SNAPSHOT;
198

S
Shengliang Guan 已提交
199 200 201 202 203 204 205 206
  mInfo("start to open mnode sync, standby:%d", pMgmt->standby);
  if (pMgmt->standby || pMgmt->replica.id > 0) {
    SSyncCfg *pCfg = &syncInfo.syncCfg;
    pCfg->replicaNum = 1;
    pCfg->myIndex = 0;
    SNodeInfo *pNode = &pCfg->nodeInfo[0];
    tstrncpy(pNode->nodeFqdn, pMgmt->replica.fqdn, sizeof(pNode->nodeFqdn));
    pNode->nodePort = pMgmt->replica.port;
S
Shengliang Guan 已提交
207
    mInfo("mnode ep:%s:%u", pNode->nodeFqdn, pNode->nodePort);
M
Minghao Li 已提交
208 209
  }

210
  tsem_init(&pMgmt->syncSem, 0, 0);
211 212 213 214 215
  pMgmt->sync = syncOpen(&syncInfo);
  if (pMgmt->sync <= 0) {
    mError("failed to open sync since %s", terrstr());
    return -1;
  }
M
Minghao Li 已提交
216

217
  // decrease election timer
M
Minghao Li 已提交
218
  setPingTimerMS(pMgmt->sync, 5000);
219 220 221
  setElectTimerMS(pMgmt->sync, 600);
  setHeartbeatTimerMS(pMgmt->sync, 300);

S
Shengliang Guan 已提交
222
  mDebug("mnode-sync is opened, id:%" PRId64, pMgmt->sync);
S
Shengliang Guan 已提交
223 224 225 226 227
  return 0;
}

void mndCleanupSync(SMnode *pMnode) {
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
228
  syncStop(pMgmt->sync);
S
Shengliang Guan 已提交
229
  mDebug("mnode-sync is stopped, id:%" PRId64, pMgmt->sync);
230

231
  tsem_destroy(&pMgmt->syncSem);
232 233
  memset(pMgmt, 0, sizeof(SSyncMgmt));
}
M
Minghao Li 已提交
234

S
Shengliang Guan 已提交
235
int32_t mndSyncPropose(SMnode *pMnode, SSdbRaw *pRaw, int32_t transId) {
S
Shengliang Guan 已提交
236
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
237 238 239 240
  SRpcMsg    req = {.msgType = TDMT_MND_APPLY_MSG, .contLen = sdbGetRawTotalSize(pRaw)};
  req.pCont = rpcMallocCont(req.contLen);
  if (req.pCont == NULL) return -1;
  memcpy(req.pCont, pRaw, req.contLen);
S
Shengliang Guan 已提交
241

S
Shengliang Guan 已提交
242 243 244 245
  pMgmt->errCode = 0;
  pMgmt->transId = transId;
  mTrace("trans:%d, will be proposed", pMgmt->transId);

246
  const bool isWeak = false;
247
  int32_t    code = syncPropose(pMgmt->sync, &req, isWeak);
248 249
  if (code == 0) {
    tsem_wait(&pMgmt->syncSem);
M
Minghao Li 已提交
250
  } else if (code == -1 && terrno == TSDB_CODE_SYN_NOT_LEADER) {
251
    terrno = TSDB_CODE_APP_NOT_READY;
M
Minghao Li 已提交
252
  } else if (code == -1 && terrno == TSDB_CODE_SYN_INTERNAL_ERROR) {
253 254
    terrno = TSDB_CODE_SYN_INTERNAL_ERROR;
  } else {
255
    terrno = TSDB_CODE_APP_ERROR;
256
  }
257

258
  rpcFreeCont(req.pCont);
S
Shengliang Guan 已提交
259 260 261 262 263
  if (code != 0) {
    mError("trans:%d, failed to propose, code:0x%x", pMgmt->transId, code);
    return code;
  }

S
Shengliang Guan 已提交
264
  return pMgmt->errCode;
265 266
}

267
void mndSyncStart(SMnode *pMnode) {
268 269
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
  syncSetMsgCb(pMgmt->sync, &pMnode->msgCb);
M
Minghao Li 已提交
270 271
  syncStart(pMgmt->sync);
  mDebug("mnode sync started, id:%" PRId64 " standby:%d", pMgmt->sync, pMgmt->standby);
272 273
}

S
Shengliang Guan 已提交
274 275
void mndSyncStop(SMnode *pMnode) {
  if (pMnode->syncMgmt.transId != 0) {
S
Shengliang Guan 已提交
276
    pMnode->syncMgmt.transId = 0;
S
Shengliang Guan 已提交
277 278 279
    tsem_post(&pMnode->syncMgmt.syncSem);
  }
}
280

S
Shengliang Guan 已提交
281
bool mndIsMaster(SMnode *pMnode) {
S
Shengliang Guan 已提交
282
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
283

S
Shengliang Guan 已提交
284
  if (!syncIsReady(pMgmt->sync)) {
285 286
    // get terrno from syncIsReady
    // terrno = TSDB_CODE_SYN_NOT_LEADER;
287 288 289
    return false;
  }

S
Shengliang Guan 已提交
290
  if (!pMnode->restored) {
291 292 293 294 295
    terrno = TSDB_CODE_APP_NOT_READY;
    return false;
  }

  return true;
L
Liu Jicong 已提交
296
}