mndSync.c 15.1 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 "mndCluster.h"
19
#include "mndTrans.h"
20

21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
static int32_t mndSyncEqCtrlMsg(const SMsgCb *msgcb, SRpcMsg *pMsg) {
  if (pMsg == NULL || pMsg->pCont == NULL) {
    return -1;
  }

  SMsgHead *pHead = pMsg->pCont;
  pHead->contLen = htonl(pHead->contLen);
  pHead->vgId = htonl(pHead->vgId);

  if (msgcb == NULL || msgcb->putToQueueFp == NULL) {
    rpcFreeCont(pMsg->pCont);
    pMsg->pCont = NULL;
    return -1;
  }

  int32_t code = tmsgPutToQueue(msgcb, SYNC_CTRL_QUEUE, pMsg);
  if (code != 0) {
    rpcFreeCont(pMsg->pCont);
    pMsg->pCont = NULL;
  }
  return code;
}

44
static int32_t mndSyncEqMsg(const SMsgCb *msgcb, SRpcMsg *pMsg) {
45 46 47 48
  if (pMsg == NULL || pMsg->pCont == NULL) {
    return -1;
  }

M
Minghao Li 已提交
49 50 51 52
  SMsgHead *pHead = pMsg->pCont;
  pHead->contLen = htonl(pHead->contLen);
  pHead->vgId = htonl(pHead->vgId);

53 54 55 56 57 58
  if (msgcb == NULL || msgcb->putToQueueFp == NULL) {
    rpcFreeCont(pMsg->pCont);
    pMsg->pCont = NULL;
    return -1;
  }

59 60 61 62 63 64
  int32_t code = tmsgPutToQueue(msgcb, SYNC_QUEUE, pMsg);
  if (code != 0) {
    rpcFreeCont(pMsg->pCont);
    pMsg->pCont = NULL;
  }
  return code;
M
Minghao Li 已提交
65
}
M
Minghao Li 已提交
66

67 68 69 70 71 72 73 74
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 已提交
75

76
int32_t mndProcessWriteMsg(const SSyncFSM *pFsm, SRpcMsg *pMsg, const SFsmCbMeta *pMeta) {
S
Shengliang Guan 已提交
77 78 79 80
  SMnode    *pMnode = pFsm->data;
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
  SSdbRaw   *pRaw = pMsg->pCont;

S
Shengliang Guan 已提交
81
  int32_t transId = sdbGetIdFromRaw(pMnode->pSdb, pRaw);
82
  mInfo("trans:%d, is proposed, saved:%d code:0x%x, apply index:%" PRId64 " term:%" PRIu64 " config:%" PRId64
83
        " role:%s raw:%p sec:%d seq:%" PRId64,
S
Shengliang Guan 已提交
84
        transId, pMgmt->transId, pMeta->code, pMeta->index, pMeta->term, pMeta->lastConfigIndex, syncStr(pMeta->state),
85
        pRaw, pMgmt->transSec, pMgmt->transSeq);
S
Shengliang Guan 已提交
86

87
  if (pMeta->code == 0) {
88 89 90 91 92
    int32_t code = sdbWriteWithoutFree(pMnode->pSdb, pRaw);
    if (code != 0) {
      mError("trans:%d, failed to write to sdb since %s", transId, terrstr());
      return 0;
    }
S
Shengliang Guan 已提交
93
    sdbSetApplyInfo(pMnode->pSdb, pMeta->index, pMeta->term, pMeta->lastConfigIndex);
S
Shengliang Guan 已提交
94 95
  }

96
  taosThreadMutexLock(&pMgmt->lock);
97 98
  pMgmt->errCode = pMeta->code;

99
  if (transId <= 0) {
100
    taosThreadMutexUnlock(&pMgmt->lock);
101
    mError("trans:%d, invalid commit msg, cache transId:%d seq:%" PRId64, transId, pMgmt->transId, pMgmt->transSeq);
102
  } else if (transId == pMgmt->transId) {
S
Shengliang Guan 已提交
103
    if (pMgmt->errCode != 0) {
104 105
      mError("trans:%d, failed to propose since %s, post sem", transId, tstrerror(pMgmt->errCode));
    } else {
106
      mInfo("trans:%d, is proposed and post sem, seq:%" PRId64, transId, pMgmt->transSeq);
S
Shengliang Guan 已提交
107
    }
S
Shengliang Guan 已提交
108
    pMgmt->transId = 0;
109
    pMgmt->transSec = 0;
S
Shengliang Guan 已提交
110
    pMgmt->transSeq = 0;
S
Shengliang Guan 已提交
111
    tsem_post(&pMgmt->syncSem);
112
    taosThreadMutexUnlock(&pMgmt->lock);
113
  } else {
114
    taosThreadMutexUnlock(&pMgmt->lock);
115 116
    STrans *pTrans = mndAcquireTrans(pMnode, transId);
    if (pTrans != NULL) {
117 118
      mInfo("trans:%d, execute in mnode which not leader or sync timeout, createTime:%" PRId64 " saved trans:%d",
            transId, pTrans->createdTime, pMgmt->transId);
119
      mndTransExecute(pMnode, pTrans, false);
120
      mndReleaseTrans(pMnode, pTrans);
121 122
    } else {
      mError("trans:%d, not found while execute in mnode since %s", transId, terrstr());
123
    }
M
Minghao Li 已提交
124
  }
125

126
  sdbWriteFile(pMnode->pSdb, tsMndSdbWriteDelta);
127
  return 0;
M
Minghao Li 已提交
128 129
}

130
int32_t mndSyncCommitMsg(const SSyncFSM *pFsm, SRpcMsg *pMsg, const SFsmCbMeta *pMeta) {
131 132 133 134 135 136 137
  int32_t code = 0;
  if (!syncUtilUserCommit(pMsg->msgType)) {
    goto _out;
  }
  code = mndProcessWriteMsg(pFsm, pMsg, pMeta);

_out:
138 139
  rpcFreeCont(pMsg->pCont);
  pMsg->pCont = NULL;
140
  return code;
141 142
}

S
Shengliang Guan 已提交
143
int32_t mndSyncGetSnapshot(const SSyncFSM *pFsm, SSnapshot *pSnapshot, void *pReaderParam, void **ppReader) {
144
  mInfo("start to read snapshot from sdb in atomic way");
145 146 147
  SMnode *pMnode = pFsm->data;
  return sdbStartRead(pMnode->pSdb, (SSdbIter **)ppReader, &pSnapshot->lastApplyIndex, &pSnapshot->lastApplyTerm,
                      &pSnapshot->lastConfigIndex);
148 149 150
  return 0;
}

151
static void mndSyncGetSnapshotInfo(const SSyncFSM *pFsm, SSnapshot *pSnapshot) {
152
  SMnode *pMnode = pFsm->data;
153
  sdbGetCommitInfo(pMnode->pSdb, &pSnapshot->lastApplyIndex, &pSnapshot->lastApplyTerm, &pSnapshot->lastConfigIndex);
M
Minghao Li 已提交
154 155
}

S
Shengliang Guan 已提交
156
void mndRestoreFinish(const SSyncFSM *pFsm) {
157
  SMnode *pMnode = pFsm->data;
S
Shengliang Guan 已提交
158

S
Shengliang Guan 已提交
159
  if (!pMnode->deploy) {
S
Shengliang Guan 已提交
160
    if (!pMnode->restored) {
161 162 163 164 165 166
      mInfo("vgId:1, sync restore finished, and will handle outstanding transactions");
      mndTransPullup(pMnode);
      mndSetRestored(pMnode, true);
    } else {
      mInfo("vgId:1, sync restore finished, repeat call");
    }
S
Shengliang Guan 已提交
167
  } else {
168
    mInfo("vgId:1, sync restore finished");
S
Shengliang Guan 已提交
169
  }
170 171
}

S
Shengliang Guan 已提交
172
int32_t mndSnapshotStartRead(const SSyncFSM *pFsm, void *pParam, void **ppReader) {
173
  mInfo("start to read snapshot from sdb");
S
Shengliang Guan 已提交
174
  SMnode *pMnode = pFsm->data;
175
  return sdbStartRead(pMnode->pSdb, (SSdbIter **)ppReader, NULL, NULL, NULL);
S
Shengliang Guan 已提交
176 177
}

178
static void mndSnapshotStopRead(const SSyncFSM *pFsm, void *pReader) {
179
  mInfo("stop to read snapshot from sdb");
S
Shengliang Guan 已提交
180
  SMnode *pMnode = pFsm->data;
181
  sdbStopRead(pMnode->pSdb, pReader);
S
Shengliang Guan 已提交
182 183
}

S
Shengliang Guan 已提交
184
int32_t mndSnapshotDoRead(const SSyncFSM *pFsm, void *pReader, void **ppBuf, int32_t *len) {
S
Shengliang Guan 已提交
185 186 187 188
  SMnode *pMnode = pFsm->data;
  return sdbDoRead(pMnode->pSdb, pReader, ppBuf, len);
}

S
Shengliang Guan 已提交
189
int32_t mndSnapshotStartWrite(const SSyncFSM *pFsm, void *pParam, void **ppWriter) {
S
Shengliang Guan 已提交
190 191 192 193 194
  mInfo("start to apply snapshot to sdb");
  SMnode *pMnode = pFsm->data;
  return sdbStartWrite(pMnode->pSdb, (SSdbIter **)ppWriter);
}

S
Shengliang Guan 已提交
195
int32_t mndSnapshotStopWrite(const SSyncFSM *pFsm, void *pWriter, bool isApply, SSnapshot *pSnapshot) {
196
  mInfo("stop to apply snapshot to sdb, apply:%d, index:%" PRId64 " term:%" PRIu64 " config:%" PRId64, isApply,
S
Shengliang Guan 已提交
197
        pSnapshot->lastApplyIndex, pSnapshot->lastApplyTerm, pSnapshot->lastConfigIndex);
S
Shengliang Guan 已提交
198
  SMnode *pMnode = pFsm->data;
199 200
  return sdbStopWrite(pMnode->pSdb, pWriter, isApply, pSnapshot->lastApplyIndex, pSnapshot->lastApplyTerm,
                      pSnapshot->lastConfigIndex);
S
Shengliang Guan 已提交
201 202
}

S
Shengliang Guan 已提交
203
int32_t mndSnapshotDoWrite(const SSyncFSM *pFsm, void *pWriter, void *pBuf, int32_t len) {
S
Shengliang Guan 已提交
204 205 206 207
  SMnode *pMnode = pFsm->data;
  return sdbDoWrite(pMnode->pSdb, pWriter, pBuf, len);
}

S
Shengliang Guan 已提交
208
static void mndBecomeFollower(const SSyncFSM *pFsm) {
209 210
  SMnode    *pMnode = pFsm->data;
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
211
  mInfo("vgId:1, become follower");
212

213 214 215 216
  taosThreadMutexLock(&pMgmt->lock);
  if (pMgmt->transId != 0) {
    mInfo("vgId:1, become follower and post sem, trans:%d, failed to propose since not leader", pMgmt->transId);
    pMgmt->transId = 0;
217
    pMgmt->transSec = 0;
S
Shengliang Guan 已提交
218
    pMgmt->transSeq = 0;
219 220
    pMgmt->errCode = TSDB_CODE_SYN_NOT_LEADER;
    tsem_post(&pMgmt->syncSem);
221
  }
222
  taosThreadMutexUnlock(&pMgmt->lock);
223 224
}

S
Shengliang Guan 已提交
225
static void mndBecomeLeader(const SSyncFSM *pFsm) {
226
  mInfo("vgId:1, become leader");
227
  SMnode *pMnode = pFsm->data;
228 229
}

230 231 232
static bool mndApplyQueueEmpty(const SSyncFSM *pFsm) {
  SMnode *pMnode = pFsm->data;

233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
  if (pMnode != NULL && pMnode->msgCb.qsizeFp != NULL) {
    int32_t itemSize = tmsgGetQueueSize(&pMnode->msgCb, 1, APPLY_QUEUE);
    return (itemSize == 0);
  } else {
    return true;
  }
}

static int32_t mndApplyQueueItems(const SSyncFSM *pFsm) {
  SMnode *pMnode = pFsm->data;

  if (pMnode != NULL && pMnode->msgCb.qsizeFp != NULL) {
    int32_t itemSize = tmsgGetQueueSize(&pMnode->msgCb, 1, APPLY_QUEUE);
    return itemSize;
  } else {
    return -1;
  }
250 251
}

252 253
SSyncFSM *mndSyncMakeFsm(SMnode *pMnode) {
  SSyncFSM *pFsm = taosMemoryCalloc(1, sizeof(SSyncFSM));
M
Minghao Li 已提交
254
  pFsm->data = pMnode;
255
  pFsm->FpCommitCb = mndSyncCommitMsg;
256 257
  pFsm->FpPreCommitCb = NULL;
  pFsm->FpRollBackCb = NULL;
258
  pFsm->FpRestoreFinishCb = mndRestoreFinish;
S
Shengliang Guan 已提交
259
  pFsm->FpLeaderTransferCb = NULL;
260
  pFsm->FpApplyQueueEmptyCb = mndApplyQueueEmpty;
261
  pFsm->FpApplyQueueItems = mndApplyQueueItems;
S
Shengliang Guan 已提交
262
  pFsm->FpReConfigCb = NULL;
263 264
  pFsm->FpBecomeLeaderCb = mndBecomeLeader;
  pFsm->FpBecomeFollowerCb = mndBecomeFollower;
S
Shengliang Guan 已提交
265
  pFsm->FpGetSnapshot = mndSyncGetSnapshot;
266
  pFsm->FpGetSnapshotInfo = mndSyncGetSnapshotInfo;
S
Shengliang Guan 已提交
267 268 269 270 271 272
  pFsm->FpSnapshotStartRead = mndSnapshotStartRead;
  pFsm->FpSnapshotStopRead = mndSnapshotStopRead;
  pFsm->FpSnapshotDoRead = mndSnapshotDoRead;
  pFsm->FpSnapshotStartWrite = mndSnapshotStartWrite;
  pFsm->FpSnapshotStopWrite = mndSnapshotStopWrite;
  pFsm->FpSnapshotDoWrite = mndSnapshotDoWrite;
M
Minghao Li 已提交
273
  return pFsm;
274 275 276 277
}

int32_t mndInitSync(SMnode *pMnode) {
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
278
  taosThreadMutexInit(&pMgmt->lock, NULL);
S
Shengliang Guan 已提交
279 280 281 282 283
  taosThreadMutexLock(&pMgmt->lock);
  pMgmt->transId = 0;
  pMgmt->transSec = 0;
  pMgmt->transSeq = 0;
  taosThreadMutexUnlock(&pMgmt->lock);
284

S
Shengliang Guan 已提交
285 286 287 288 289
  SSyncInfo syncInfo = {
      .snapshotStrategy = SYNC_STRATEGY_STANDARD_SNAPSHOT,
      .batchSize = 1,
      .vgId = 1,
      .pWal = pMnode->pWal,
S
Shengliang Guan 已提交
290
      .msgcb = &pMnode->msgCb,
S
Shengliang Guan 已提交
291 292
      .syncSendMSg = mndSyncSendMsg,
      .syncEqMsg = mndSyncEqMsg,
293
      .syncEqCtrlMsg = mndSyncEqCtrlMsg,
S
Shengliang Guan 已提交
294 295 296
      .pingMs = 5000,
      .electMs = 3000,
      .heartbeatMs = 500,
S
Shengliang Guan 已提交
297 298
  };

299 300 301
  snprintf(syncInfo.path, sizeof(syncInfo.path), "%s%ssync", pMnode->path, TD_DIRSEP);
  syncInfo.pFsm = mndSyncMakeFsm(pMnode);

S
Shengliang Guan 已提交
302
  mInfo("vgId:1, start to open sync, replica:%d selfIndex:%d", pMgmt->numOfReplicas, pMgmt->selfIndex);
303 304 305 306 307
  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];
308
    pNode->nodeId = pMgmt->replicas[i].id;
309
    pNode->nodePort = pMgmt->replicas[i].port;
310
    tstrncpy(pNode->nodeFqdn, pMgmt->replicas[i].fqdn, sizeof(pNode->nodeFqdn));
311
    (void)tmsgUpdateDnodeInfo(&pNode->nodeId, &pNode->clusterId, pNode->nodeFqdn, &pNode->nodePort);
S
Shengliang Guan 已提交
312
    mInfo("vgId:1, index:%d ep:%s:%u dnode:%d cluster:%" PRId64, i, pNode->nodeFqdn, pNode->nodePort, pNode->nodeId,
313
          pNode->clusterId);
M
Minghao Li 已提交
314 315
  }

316
  tsem_init(&pMgmt->syncSem, 0, 0);
317 318 319 320 321
  pMgmt->sync = syncOpen(&syncInfo);
  if (pMgmt->sync <= 0) {
    mError("failed to open sync since %s", terrstr());
    return -1;
  }
322
  pMnode->pSdb->sync = pMgmt->sync;
M
Minghao Li 已提交
323

324
  mInfo("mnode-sync is opened, id:%" PRId64, pMgmt->sync);
S
Shengliang Guan 已提交
325 326 327 328 329
  return 0;
}

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

333
  tsem_destroy(&pMgmt->syncSem);
334
  taosThreadMutexDestroy(&pMgmt->lock);
335 336
  memset(pMgmt, 0, sizeof(SSyncMgmt));
}
M
Minghao Li 已提交
337

338
void mndSyncCheckTimeout(SMnode *pMnode) {
339
  mTrace("check sync timeout");
340 341 342 343 344
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
  taosThreadMutexLock(&pMgmt->lock);
  if (pMgmt->transId != 0) {
    int32_t curSec = taosGetTimestampSec();
    int32_t delta = curSec - pMgmt->transSec;
S
Shengliang Guan 已提交
345 346 347
    if (delta > MNODE_TIMEOUT_SEC) {
      mError("trans:%d, failed to propose since timeout, start:%d cur:%d delta:%d seq:%" PRId64, pMgmt->transId,
             pMgmt->transSec, curSec, delta, pMgmt->transSeq);
348 349
      pMgmt->transId = 0;
      pMgmt->transSec = 0;
S
Shengliang Guan 已提交
350
      pMgmt->transSeq = 0;
351 352 353 354
      terrno = TSDB_CODE_SYN_TIMEOUT;
      pMgmt->errCode = TSDB_CODE_SYN_TIMEOUT;
      tsem_post(&pMgmt->syncSem);
    } else {
S
Shengliang Guan 已提交
355 356
      mDebug("trans:%d, waiting for sync confirm, start:%d cur:%d delta:%d seq:%" PRId64, pMgmt->transId,
             pMgmt->transSec, curSec, curSec - pMgmt->transSec, pMgmt->transSeq);
357 358
    }
  } else {
359
    // mTrace("check sync timeout msg, no trans waiting for confirm");
360 361 362 363
  }
  taosThreadMutexUnlock(&pMgmt->lock);
}

S
Shengliang Guan 已提交
364
int32_t mndSyncPropose(SMnode *pMnode, SSdbRaw *pRaw, int32_t transId) {
S
Shengliang Guan 已提交
365
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
S
Shengliang Guan 已提交
366

S
Shengliang Guan 已提交
367
  SRpcMsg req = {.msgType = TDMT_MND_APPLY_MSG, .contLen = sdbGetRawTotalSize(pRaw)};
S
Shengliang Guan 已提交
368
  if (req.contLen <= 0) return -1;
S
Shengliang Guan 已提交
369

370 371 372
  req.pCont = rpcMallocCont(req.contLen);
  if (req.pCont == NULL) return -1;
  memcpy(req.pCont, pRaw, req.contLen);
S
Shengliang Guan 已提交
373

374
  taosThreadMutexLock(&pMgmt->lock);
375 376
  pMgmt->errCode = 0;

S
Shengliang Guan 已提交
377
  if (pMgmt->transId != 0 /* && pMgmt->transId != transId*/) {
S
Shengliang Guan 已提交
378
    mError("trans:%d, can't be proposed since trans:%d already waiting for confirm", transId, pMgmt->transId);
379
    taosThreadMutexUnlock(&pMgmt->lock);
S
Shengliang Guan 已提交
380
    rpcFreeCont(req.pCont);
381
    terrno = TSDB_CODE_MND_LAST_TRANS_NOT_FINISHED;
382
    return terrno;
383
  }
S
Shengliang Guan 已提交
384

S
Shengliang Guan 已提交
385 386
  mInfo("trans:%d, will be proposed", transId);
  pMgmt->transId = transId;
387
  pMgmt->transSec = taosGetTimestampSec();
388

S
Shengliang Guan 已提交
389 390
  int64_t seq = 0;
  int32_t code = syncPropose(pMgmt->sync, &req, false, &seq);
391
  if (code == 0) {
S
Shengliang Guan 已提交
392 393 394
    mInfo("trans:%d, is proposing and wait sem, seq:%" PRId64, transId, seq);
    pMgmt->transSeq = seq;
    taosThreadMutexUnlock(&pMgmt->lock);
395
    tsem_wait(&pMgmt->syncSem);
396 397 398
  } else if (code > 0) {
    mInfo("trans:%d, confirm at once since replica is 1, continue execute", transId);
    pMgmt->transId = 0;
399
    pMgmt->transSec = 0;
S
Shengliang Guan 已提交
400
    pMgmt->transSeq = 0;
401
    taosThreadMutexUnlock(&pMgmt->lock);
402 403 404
    sdbWriteWithoutFree(pMnode->pSdb, pRaw);
    sdbSetApplyInfo(pMnode->pSdb, req.info.conn.applyIndex, req.info.conn.applyTerm, SYNC_INDEX_INVALID);
    code = 0;
405
  } else {
406
    mError("trans:%d, failed to proposed since %s", transId, terrstr());
407
    pMgmt->transId = 0;
408
    pMgmt->transSec = 0;
S
Shengliang Guan 已提交
409
    pMgmt->transSeq = 0;
410
    taosThreadMutexUnlock(&pMgmt->lock);
411
    if (terrno == 0) {
412 413
      terrno = TSDB_CODE_APP_ERROR;
    }
414
  }
415

416
  rpcFreeCont(req.pCont);
S
Shengliang Guan 已提交
417
  req.pCont = NULL;
S
Shengliang Guan 已提交
418 419 420 421 422
  if (code != 0) {
    mError("trans:%d, failed to propose, code:0x%x", pMgmt->transId, code);
    return code;
  }

S
Shengliang Guan 已提交
423
  terrno = pMgmt->errCode;
424
  return terrno;
425 426
}

427
void mndSyncStart(SMnode *pMnode) {
428
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
B
Benguang Zhao 已提交
429
  if (syncStart(pMgmt->sync) < 0) {
430
    mError("vgId:1, failed to start sync, id:%" PRId64, pMgmt->sync);
B
Benguang Zhao 已提交
431 432
    return;
  }
433
  mInfo("vgId:1, sync started, id:%" PRId64, pMgmt->sync);
434 435
}

S
Shengliang Guan 已提交
436
void mndSyncStop(SMnode *pMnode) {
437 438 439 440 441 442
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;

  taosThreadMutexLock(&pMgmt->lock);
  if (pMgmt->transId != 0) {
    mInfo("vgId:1, is stopped and post sem, trans:%d", pMgmt->transId);
    pMgmt->transId = 0;
443
    pMgmt->transSec = 0;
444
    pMgmt->errCode = TSDB_CODE_APP_IS_STOPPING;
445
    tsem_post(&pMgmt->syncSem);
S
Shengliang Guan 已提交
446
  }
447
  taosThreadMutexUnlock(&pMgmt->lock);
S
Shengliang Guan 已提交
448
}
449

450
bool mndIsLeader(SMnode *pMnode) {
451
  terrno = 0;
452
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
453

454 455 456 457 458 459 460 461
  if (terrno != 0) {
    mDebug("vgId:1, mnode is stopping");
    return false;
  }

  if (state.state != TAOS_SYNC_STATE_LEADER) {
    terrno = TSDB_CODE_SYN_NOT_LEADER;
    mDebug("vgId:1, mnode not leader, state:%s", syncStr(state.state));
462 463 464
    return false;
  }

465 466 467
  if (!state.restored || !pMnode->restored) {
    terrno = TSDB_CODE_SYN_RESTORING;
    mDebug("vgId:1, mnode not restored:%d:%d", state.restored, pMnode->restored);
468 469 470 471
    return false;
  }

  return true;
L
Liu Jicong 已提交
472
}