mndSync.c 14.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 "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) {
S
Shengliang Guan 已提交
88
    sdbWriteWithoutFree(pMnode->pSdb, pRaw);
S
Shengliang Guan 已提交
89
    sdbSetApplyInfo(pMnode->pSdb, pMeta->index, pMeta->term, pMeta->lastConfigIndex);
S
Shengliang Guan 已提交
90 91
  }

92
  taosThreadMutexLock(&pMgmt->lock);
93 94
  pMgmt->errCode = pMeta->code;

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

  return 0;
M
Minghao Li 已提交
123 124
}

125
int32_t mndSyncCommitMsg(const SSyncFSM *pFsm, SRpcMsg *pMsg, const SFsmCbMeta *pMeta) {
126 127 128 129 130 131 132
  int32_t code = 0;
  if (!syncUtilUserCommit(pMsg->msgType)) {
    goto _out;
  }
  code = mndProcessWriteMsg(pFsm, pMsg, pMeta);

_out:
133 134
  rpcFreeCont(pMsg->pCont);
  pMsg->pCont = NULL;
135
  return code;
136 137
}

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

146
static void mndSyncGetSnapshotInfo(const SSyncFSM *pFsm, SSnapshot *pSnapshot) {
147
  SMnode *pMnode = pFsm->data;
148
  sdbGetCommitInfo(pMnode->pSdb, &pSnapshot->lastApplyIndex, &pSnapshot->lastApplyTerm, &pSnapshot->lastConfigIndex);
M
Minghao Li 已提交
149 150
}

S
Shengliang Guan 已提交
151
void mndRestoreFinish(const SSyncFSM *pFsm) {
152
  SMnode *pMnode = pFsm->data;
S
Shengliang Guan 已提交
153

S
Shengliang Guan 已提交
154
  if (!pMnode->deploy) {
S
Shengliang Guan 已提交
155
    if (!pMnode->restored) {
156 157 158 159 160 161
      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 已提交
162
  } else {
163
    mInfo("vgId:1, sync restore finished");
S
Shengliang Guan 已提交
164
  }
165 166
}

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

173
static void mndSnapshotStopRead(const SSyncFSM *pFsm, void *pReader) {
174
  mInfo("stop to read snapshot from sdb");
S
Shengliang Guan 已提交
175
  SMnode *pMnode = pFsm->data;
176
  sdbStopRead(pMnode->pSdb, pReader);
S
Shengliang Guan 已提交
177 178
}

S
Shengliang Guan 已提交
179
int32_t mndSnapshotDoRead(const SSyncFSM *pFsm, void *pReader, void **ppBuf, int32_t *len) {
S
Shengliang Guan 已提交
180 181 182 183
  SMnode *pMnode = pFsm->data;
  return sdbDoRead(pMnode->pSdb, pReader, ppBuf, len);
}

S
Shengliang Guan 已提交
184
int32_t mndSnapshotStartWrite(const SSyncFSM *pFsm, void *pParam, void **ppWriter) {
S
Shengliang Guan 已提交
185 186 187 188 189
  mInfo("start to apply snapshot to sdb");
  SMnode *pMnode = pFsm->data;
  return sdbStartWrite(pMnode->pSdb, (SSdbIter **)ppWriter);
}

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

S
Shengliang Guan 已提交
198
int32_t mndSnapshotDoWrite(const SSyncFSM *pFsm, void *pWriter, void *pBuf, int32_t len) {
S
Shengliang Guan 已提交
199 200 201 202
  SMnode *pMnode = pFsm->data;
  return sdbDoWrite(pMnode->pSdb, pWriter, pBuf, len);
}

S
Shengliang Guan 已提交
203
static void mndBecomeFollower(const SSyncFSM *pFsm) {
204 205
  SMnode    *pMnode = pFsm->data;
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
206
  mInfo("vgId:1, become follower");
207

208 209 210 211
  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;
212
    pMgmt->transSec = 0;
S
Shengliang Guan 已提交
213
    pMgmt->transSeq = 0;
214 215
    pMgmt->errCode = TSDB_CODE_SYN_NOT_LEADER;
    tsem_post(&pMgmt->syncSem);
216
  }
217
  taosThreadMutexUnlock(&pMgmt->lock);
218 219
}

S
Shengliang Guan 已提交
220
static void mndBecomeLeader(const SSyncFSM *pFsm) {
221
  mInfo("vgId:1, become leader");
222
  SMnode *pMnode = pFsm->data;
223 224
}

225 226 227
static bool mndApplyQueueEmpty(const SSyncFSM *pFsm) {
  SMnode *pMnode = pFsm->data;

228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
  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;
  }
245 246
}

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

int32_t mndInitSync(SMnode *pMnode) {
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
273
  taosThreadMutexInit(&pMgmt->lock, NULL);
S
Shengliang Guan 已提交
274 275 276 277 278
  taosThreadMutexLock(&pMgmt->lock);
  pMgmt->transId = 0;
  pMgmt->transSec = 0;
  pMgmt->transSeq = 0;
  taosThreadMutexUnlock(&pMgmt->lock);
279

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

294 295 296
  snprintf(syncInfo.path, sizeof(syncInfo.path), "%s%ssync", pMnode->path, TD_DIRSEP);
  syncInfo.pFsm = mndSyncMakeFsm(pMnode);

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

311
  tsem_init(&pMgmt->syncSem, 0, 0);
312 313 314 315 316
  pMgmt->sync = syncOpen(&syncInfo);
  if (pMgmt->sync <= 0) {
    mError("failed to open sync since %s", terrstr());
    return -1;
  }
M
Minghao Li 已提交
317

318
  mInfo("mnode-sync is opened, id:%" PRId64, pMgmt->sync);
S
Shengliang Guan 已提交
319 320 321 322 323
  return 0;
}

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

327
  tsem_destroy(&pMgmt->syncSem);
328
  taosThreadMutexDestroy(&pMgmt->lock);
329 330
  memset(pMgmt, 0, sizeof(SSyncMgmt));
}
M
Minghao Li 已提交
331

332
void mndSyncCheckTimeout(SMnode *pMnode) {
333
  mTrace("check sync timeout");
334 335 336 337 338
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
  taosThreadMutexLock(&pMgmt->lock);
  if (pMgmt->transId != 0) {
    int32_t curSec = taosGetTimestampSec();
    int32_t delta = curSec - pMgmt->transSec;
S
Shengliang Guan 已提交
339 340 341
    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);
342 343
      pMgmt->transId = 0;
      pMgmt->transSec = 0;
S
Shengliang Guan 已提交
344
      pMgmt->transSeq = 0;
345 346 347 348
      terrno = TSDB_CODE_SYN_TIMEOUT;
      pMgmt->errCode = TSDB_CODE_SYN_TIMEOUT;
      tsem_post(&pMgmt->syncSem);
    } else {
S
Shengliang Guan 已提交
349 350
      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);
351 352
    }
  } else {
353
    // mTrace("check sync timeout msg, no trans waiting for confirm");
354 355 356 357
  }
  taosThreadMutexUnlock(&pMgmt->lock);
}

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

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

364 365 366
  req.pCont = rpcMallocCont(req.contLen);
  if (req.pCont == NULL) return -1;
  memcpy(req.pCont, pRaw, req.contLen);
S
Shengliang Guan 已提交
367

368
  taosThreadMutexLock(&pMgmt->lock);
369 370
  pMgmt->errCode = 0;

371
  if (pMgmt->transId != 0) {
S
Shengliang Guan 已提交
372
    mError("trans:%d, can't be proposed since trans:%d already waiting for confirm", transId, pMgmt->transId);
373
    taosThreadMutexUnlock(&pMgmt->lock);
S
Shengliang Guan 已提交
374
    rpcFreeCont(req.pCont);
375
    terrno = TSDB_CODE_MND_LAST_TRANS_NOT_FINISHED;
376
    return terrno;
377
  }
S
Shengliang Guan 已提交
378

S
Shengliang Guan 已提交
379 380
  mInfo("trans:%d, will be proposed", transId);
  pMgmt->transId = transId;
381
  pMgmt->transSec = taosGetTimestampSec();
382

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

410
  rpcFreeCont(req.pCont);
S
Shengliang Guan 已提交
411
  req.pCont = NULL;
S
Shengliang Guan 已提交
412 413 414 415 416
  if (code != 0) {
    mError("trans:%d, failed to propose, code:0x%x", pMgmt->transId, code);
    return code;
  }

S
Shengliang Guan 已提交
417
  terrno = pMgmt->errCode;
418
  return terrno;
419 420
}

421
void mndSyncStart(SMnode *pMnode) {
422
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
B
Benguang Zhao 已提交
423
  if (syncStart(pMgmt->sync) < 0) {
424
    mError("vgId:1, failed to start sync, id:%" PRId64, pMgmt->sync);
B
Benguang Zhao 已提交
425 426
    return;
  }
427
  mInfo("vgId:1, sync started, id:%" PRId64, pMgmt->sync);
428 429
}

S
Shengliang Guan 已提交
430
void mndSyncStop(SMnode *pMnode) {
431 432 433 434 435 436
  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;
437
    pMgmt->transSec = 0;
438
    pMgmt->errCode = TSDB_CODE_APP_IS_STOPPING;
439
    tsem_post(&pMgmt->syncSem);
S
Shengliang Guan 已提交
440
  }
441
  taosThreadMutexUnlock(&pMgmt->lock);
S
Shengliang Guan 已提交
442
}
443

444
bool mndIsLeader(SMnode *pMnode) {
445
  terrno = 0;
446
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
447

448 449 450 451 452 453 454 455
  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));
456 457 458
    return false;
  }

459 460 461
  if (!state.restored || !pMnode->restored) {
    terrno = TSDB_CODE_SYN_RESTORING;
    mDebug("vgId:1, mnode not restored:%d:%d", state.restored, pMnode->restored);
462 463 464 465
    return false;
  }

  return true;
L
Liu Jicong 已提交
466
}