mndSync.c 11.5 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
  mInfo("trans:%d, is proposed, saved:%d code:0x%x, apply index:%" PRId64 " term:%" PRIu64 " config:%" PRId64
H
Hongze Cheng 已提交
54 55 56
        " 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
  taosWLockLatch(&pMgmt->lock);
64
  if (transId <= 0) {
65
    taosWUnLockLatch(&pMgmt->lock);
66 67
    mError("trans:%d, invalid commit msg", transId);
  } else if (transId == pMgmt->transId) {
S
Shengliang Guan 已提交
68
    if (pMgmt->errCode != 0) {
69 70
      mError("trans:%d, failed to propose since %s, post sem", transId, tstrerror(pMgmt->errCode));
    } else {
S
Shengliang Guan 已提交
71
      mInfo("trans:%d, is proposed and post sem", transId);
S
Shengliang Guan 已提交
72
    }
S
Shengliang Guan 已提交
73
    pMgmt->transId = 0;
S
Shengliang Guan 已提交
74
    tsem_post(&pMgmt->syncSem);
75
    taosWUnLockLatch(&pMgmt->lock);
76
  } else {
77
    taosWUnLockLatch(&pMgmt->lock);
78 79
    STrans *pTrans = mndAcquireTrans(pMnode, transId);
    if (pTrans != NULL) {
80
      mInfo("trans:%d, execute in mnode which not leader", transId);
81 82
      mndTransExecute(pMnode, pTrans);
      mndReleaseTrans(pMnode, pTrans);
83 84 85
      // sdbWriteFile(pMnode->pSdb, SDB_WRITE_DELTA);
    } else {
      mError("trans:%d, not found while execute in mnode since %s", transId, terrstr());
86
    }
M
Minghao Li 已提交
87 88 89
  }
}

90
int32_t mndSyncGetSnapshot(struct SSyncFSM *pFsm, SSnapshot *pSnapshot, void *pReaderParam, void **ppReader) {
91
  mInfo("start to read snapshot from sdb in atomic way");
92 93 94
  SMnode *pMnode = pFsm->data;
  return sdbStartRead(pMnode->pSdb, (SSdbIter **)ppReader, &pSnapshot->lastApplyIndex, &pSnapshot->lastApplyTerm,
                      &pSnapshot->lastConfigIndex);
95 96 97 98
  return 0;
}

int32_t mndSyncGetSnapshotInfo(struct SSyncFSM *pFsm, SSnapshot *pSnapshot) {
99
  SMnode *pMnode = pFsm->data;
100
  sdbGetCommitInfo(pMnode->pSdb, &pSnapshot->lastApplyIndex, &pSnapshot->lastApplyTerm, &pSnapshot->lastConfigIndex);
M
Minghao Li 已提交
101 102 103
  return 0;
}

104
void mndRestoreFinish(struct SSyncFSM *pFsm) {
105
  SMnode *pMnode = pFsm->data;
S
Shengliang Guan 已提交
106

S
Shengliang Guan 已提交
107
  if (!pMnode->deploy) {
108
    mInfo("vgId:1, sync restore finished, and will handle outstanding transactions");
S
Shengliang Guan 已提交
109
    mndTransPullup(pMnode);
110
    mndSetRestored(pMnode, true);
S
Shengliang Guan 已提交
111
  } else {
112
    mInfo("vgId:1, sync restore finished");
S
Shengliang Guan 已提交
113
  }
114 115
}

116
void mndReConfig(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SReConfigCbMeta *cbMeta) {}
117

118
int32_t mndSnapshotStartRead(struct SSyncFSM *pFsm, void *pParam, void **ppReader) {
119
  mInfo("start to read snapshot from sdb");
S
Shengliang Guan 已提交
120
  SMnode *pMnode = pFsm->data;
121
  return sdbStartRead(pMnode->pSdb, (SSdbIter **)ppReader, NULL, NULL, NULL);
S
Shengliang Guan 已提交
122 123 124
}

int32_t mndSnapshotStopRead(struct SSyncFSM *pFsm, void *pReader) {
125
  mInfo("stop to read snapshot from sdb");
S
Shengliang Guan 已提交
126 127 128 129 130 131 132 133 134
  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);
}

135
int32_t mndSnapshotStartWrite(struct SSyncFSM *pFsm, void *pParam, void **ppWriter) {
S
Shengliang Guan 已提交
136 137 138 139 140
  mInfo("start to apply snapshot to sdb");
  SMnode *pMnode = pFsm->data;
  return sdbStartWrite(pMnode->pSdb, (SSdbIter **)ppWriter);
}

141
int32_t mndSnapshotStopWrite(struct SSyncFSM *pFsm, void *pWriter, bool isApply, SSnapshot *pSnapshot) {
142
  mInfo("stop to apply snapshot to sdb, apply:%d, index:%" PRId64 " term:%" PRIu64 " config:%" PRId64, isApply,
S
Shengliang Guan 已提交
143
        pSnapshot->lastApplyIndex, pSnapshot->lastApplyTerm, pSnapshot->lastConfigIndex);
S
Shengliang Guan 已提交
144
  SMnode *pMnode = pFsm->data;
145 146
  return sdbStopWrite(pMnode->pSdb, pWriter, isApply, pSnapshot->lastApplyIndex, pSnapshot->lastApplyTerm,
                      pSnapshot->lastConfigIndex);
S
Shengliang Guan 已提交
147 148 149 150 151 152 153
}

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

154 155 156
void mndLeaderTransfer(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SFsmCbMeta cbMeta) {
  SMnode *pMnode = pFsm->data;
  atomic_store_8(&(pMnode->syncMgmt.leaderTransferFinish), 1);
157
  mInfo("vgId:1, mnode leader transfer finish");
158 159
}

160 161
static void mndBecomeFollower(struct SSyncFSM *pFsm) {
  SMnode *pMnode = pFsm->data;
162
  mInfo("vgId:1, become follower");
163

164
  taosWLockLatch(&pMnode->syncMgmt.lock);
165
  if (pMnode->syncMgmt.transId != 0) {
166 167
    mInfo("vgId:1, become follower and post sem, trans:%d, failed to propose since not leader",
          pMnode->syncMgmt.transId);
168
    pMnode->syncMgmt.transId = 0;
169
    pMnode->syncMgmt.errCode = TSDB_CODE_SYN_NOT_LEADER;
170 171
    tsem_post(&pMnode->syncMgmt.syncSem);
  }
172
  taosWUnLockLatch(&pMnode->syncMgmt.lock);
173 174 175
}

static void mndBecomeLeader(struct SSyncFSM *pFsm) {
176
  mInfo("vgId:1, become leader");
177
  SMnode *pMnode = pFsm->data;
178 179
}

180 181
SSyncFSM *mndSyncMakeFsm(SMnode *pMnode) {
  SSyncFSM *pFsm = taosMemoryCalloc(1, sizeof(SSyncFSM));
M
Minghao Li 已提交
182
  pFsm->data = pMnode;
183
  pFsm->FpCommitCb = mndSyncCommitMsg;
184 185
  pFsm->FpPreCommitCb = NULL;
  pFsm->FpRollBackCb = NULL;
186
  pFsm->FpRestoreFinishCb = mndRestoreFinish;
187
  pFsm->FpLeaderTransferCb = mndLeaderTransfer;
188
  pFsm->FpReConfigCb = mndReConfig;
189 190
  pFsm->FpBecomeLeaderCb = mndBecomeLeader;
  pFsm->FpBecomeFollowerCb = mndBecomeFollower;
S
Shengliang Guan 已提交
191
  pFsm->FpGetSnapshot = mndSyncGetSnapshot;
192
  pFsm->FpGetSnapshotInfo = mndSyncGetSnapshotInfo;
S
Shengliang Guan 已提交
193 194 195 196 197 198
  pFsm->FpSnapshotStartRead = mndSnapshotStartRead;
  pFsm->FpSnapshotStopRead = mndSnapshotStopRead;
  pFsm->FpSnapshotDoRead = mndSnapshotDoRead;
  pFsm->FpSnapshotStartWrite = mndSnapshotStartWrite;
  pFsm->FpSnapshotStopWrite = mndSnapshotStopWrite;
  pFsm->FpSnapshotDoWrite = mndSnapshotDoWrite;
M
Minghao Li 已提交
199
  return pFsm;
200 201 202 203
}

int32_t mndInitSync(SMnode *pMnode) {
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
204 205
  taosInitRWLatch(&pMgmt->lock);
  pMgmt->transId = 0;
206

S
Shengliang Guan 已提交
207 208 209 210 211 212 213 214 215 216 217
  SSyncInfo syncInfo = {
      .snapshotStrategy = SYNC_STRATEGY_STANDARD_SNAPSHOT,
      .batchSize = 1,
      .vgId = 1,
      .pWal = pMnode->pWal,
      .msgcb = NULL,
      .FpSendMsg = mndSyncSendMsg,
      .FpEqMsg = mndSyncEqMsg,
      .FpEqCtrlMsg = NULL,
  };

218 219 220
  snprintf(syncInfo.path, sizeof(syncInfo.path), "%s%ssync", pMnode->path, TD_DIRSEP);
  syncInfo.pFsm = mndSyncMakeFsm(pMnode);

S
Shengliang Guan 已提交
221
  mInfo("vgId:1, start to open sync, replica:%d selfIndex:%d", pMgmt->numOfReplicas, pMgmt->selfIndex);
222 223 224 225 226 227 228 229
  SSyncCfg *pCfg = &syncInfo.syncCfg;
  pCfg->replicaNum = pMgmt->numOfReplicas;
  pCfg->myIndex = pMgmt->selfIndex;
  for (int32_t i = 0; i < pMgmt->numOfReplicas; ++i) {
    SNodeInfo *pNode = &pCfg->nodeInfo[i];
    tstrncpy(pNode->nodeFqdn, pMgmt->replicas[i].fqdn, sizeof(pNode->nodeFqdn));
    pNode->nodePort = pMgmt->replicas[i].port;
    mInfo("vgId:1, index:%d ep:%s:%u", i, pNode->nodeFqdn, pNode->nodePort);
M
Minghao Li 已提交
230 231
  }

232
  tsem_init(&pMgmt->syncSem, 0, 0);
233 234 235 236 237
  pMgmt->sync = syncOpen(&syncInfo);
  if (pMgmt->sync <= 0) {
    mError("failed to open sync since %s", terrstr());
    return -1;
  }
M
Minghao Li 已提交
238

239
  // decrease election timer
M
Minghao Li 已提交
240
  setPingTimerMS(pMgmt->sync, 5000);
241
  setElectTimerMS(pMgmt->sync, 3000);
242
  setHeartbeatTimerMS(pMgmt->sync, 500);
243

244
  mInfo("mnode-sync is opened, id:%" PRId64, pMgmt->sync);
S
Shengliang Guan 已提交
245 246 247 248 249
  return 0;
}

void mndCleanupSync(SMnode *pMnode) {
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
250
  syncStop(pMgmt->sync);
251
  mInfo("mnode-sync is stopped, id:%" PRId64, pMgmt->sync);
252

253
  tsem_destroy(&pMgmt->syncSem);
254 255
  memset(pMgmt, 0, sizeof(SSyncMgmt));
}
M
Minghao Li 已提交
256

S
Shengliang Guan 已提交
257
int32_t mndSyncPropose(SMnode *pMnode, SSdbRaw *pRaw, int32_t transId) {
S
Shengliang Guan 已提交
258
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
259
  SRpcMsg    req = {.msgType = TDMT_MND_APPLY_MSG, .contLen = sdbGetRawTotalSize(pRaw)};
S
Shengliang Guan 已提交
260 261 262 263 264
  if (req.contLen <= 0) {
    terrno = TSDB_CODE_APP_ERROR;
    return -1;
  }

265 266 267
  req.pCont = rpcMallocCont(req.contLen);
  if (req.pCont == NULL) return -1;
  memcpy(req.pCont, pRaw, req.contLen);
S
Shengliang Guan 已提交
268

S
Shengliang Guan 已提交
269
  pMgmt->errCode = 0;
270
  taosWLockLatch(&pMgmt->lock);
271
  if (pMgmt->transId != 0) {
S
Shengliang Guan 已提交
272
    mError("trans:%d, can't be proposed since trans:%d alrady waiting for confirm", transId, pMgmt->transId);
273 274 275 276 277
    taosWUnLockLatch(&pMgmt->lock);
    terrno = TSDB_CODE_APP_NOT_READY;
    return -1;
  } else {
    pMgmt->transId = transId;
278
    mInfo("trans:%d, will be proposed", pMgmt->transId);
279 280
    taosWUnLockLatch(&pMgmt->lock);
  }
S
Shengliang Guan 已提交
281

282
  const bool isWeak = false;
283
  int32_t    code = syncPropose(pMgmt->sync, &req, isWeak);
284

285
  if (code == 0) {
286
    mInfo("trans:%d, is proposing and wait sem", pMgmt->transId);
287
    tsem_wait(&pMgmt->syncSem);
288 289 290 291 292 293 294 295
  } else if (code > 0) {
    mInfo("trans:%d, confirm at once since replica is 1, continue execute", transId);
    taosWLockLatch(&pMgmt->lock);
    pMgmt->transId = 0;
    taosWUnLockLatch(&pMgmt->lock);
    sdbWriteWithoutFree(pMnode->pSdb, pRaw);
    sdbSetApplyInfo(pMnode->pSdb, req.info.conn.applyIndex, req.info.conn.applyTerm, SYNC_INDEX_INVALID);
    code = 0;
296
  } else {
297 298 299 300 301 302 303 304 305
    taosWLockLatch(&pMgmt->lock);
    mInfo("trans:%d, failed to proposed since %s", transId, terrstr());
    pMgmt->transId = 0;
    taosWUnLockLatch(&pMgmt->lock);
    if (terrno == TSDB_CODE_SYN_NOT_LEADER) {
      terrno = TSDB_CODE_APP_NOT_READY;
    } else {
      terrno = TSDB_CODE_APP_ERROR;
    }
306
  }
307

308
  rpcFreeCont(req.pCont);
S
Shengliang Guan 已提交
309 310 311 312 313
  if (code != 0) {
    mError("trans:%d, failed to propose, code:0x%x", pMgmt->transId, code);
    return code;
  }

314
  if (pMgmt->errCode != 0) terrno = pMgmt->errCode;
S
Shengliang Guan 已提交
315
  return pMgmt->errCode;
316 317
}

318
void mndSyncStart(SMnode *pMnode) {
319 320
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
  syncSetMsgCb(pMgmt->sync, &pMnode->msgCb);
M
Minghao Li 已提交
321
  syncStart(pMgmt->sync);
322
  mInfo("vgId:1, sync started, id:%" PRId64, pMgmt->sync);
323 324
}

S
Shengliang Guan 已提交
325
void mndSyncStop(SMnode *pMnode) {
326
  taosWLockLatch(&pMnode->syncMgmt.lock);
S
Shengliang Guan 已提交
327
  if (pMnode->syncMgmt.transId != 0) {
328
    mInfo("vgId:1, is stopped and post sem, trans:%d", pMnode->syncMgmt.transId);
S
Shengliang Guan 已提交
329
    pMnode->syncMgmt.transId = 0;
S
Shengliang Guan 已提交
330 331
    tsem_post(&pMnode->syncMgmt.syncSem);
  }
332
  taosWUnLockLatch(&pMnode->syncMgmt.lock);
S
Shengliang Guan 已提交
333
}
334

335
bool mndIsLeader(SMnode *pMnode) {
S
Shengliang Guan 已提交
336
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
337

S
Shengliang Guan 已提交
338
  if (!syncIsReady(pMgmt->sync)) {
339 340
    // get terrno from syncIsReady
    // terrno = TSDB_CODE_SYN_NOT_LEADER;
341 342 343
    return false;
  }

344
  if (!mndGetRestored(pMnode)) {
345 346 347 348 349
    terrno = TSDB_CODE_APP_NOT_READY;
    return false;
  }

  return true;
L
Liu Jicong 已提交
350
}