• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #4661

08 Aug 2025 08:36AM UTC coverage: 59.883% (-0.2%) from 60.053%
#4661

push

travis-ci

web-flow
test: update cases desc (#32498)

137331 of 291923 branches covered (47.04%)

Branch coverage included in aggregate %.

207730 of 284307 relevant lines covered (73.07%)

4552406.61 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

59.15
/source/dnode/mnode/impl/src/mndSync.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#define _DEFAULT_SOURCE
17
#include "mndCluster.h"
18
#include "mndStream.h"
19
#include "mndSync.h"
20
#include "mndTrans.h"
21
#include "mndUser.h"
22

23
static int32_t mndSyncEqCtrlMsg(const SMsgCb *msgcb, SRpcMsg *pMsg) {
×
24
  if (pMsg == NULL || pMsg->pCont == NULL) {
×
25
    return -1;
×
26
  }
27

28
  SMsgHead *pHead = pMsg->pCont;
×
29
  pHead->contLen = htonl(pHead->contLen);
×
30
  pHead->vgId = htonl(pHead->vgId);
×
31

32
  if (msgcb == NULL || msgcb->putToQueueFp == NULL) {
×
33
    rpcFreeCont(pMsg->pCont);
×
34
    pMsg->pCont = NULL;
×
35
    return -1;
×
36
  }
37

38
  int32_t code = tmsgPutToQueue(msgcb, SYNC_RD_QUEUE, pMsg);
×
39
  if (code != 0) {
×
40
    rpcFreeCont(pMsg->pCont);
×
41
    pMsg->pCont = NULL;
×
42
  }
43
  return code;
×
44
}
45

46
static int32_t mndSyncEqMsg(const SMsgCb *msgcb, SRpcMsg *pMsg) {
132,386✔
47
  if (pMsg == NULL || pMsg->pCont == NULL) {
132,386!
48
    return -1;
×
49
  }
50

51
  SMsgHead *pHead = pMsg->pCont;
132,386✔
52
  pHead->contLen = htonl(pHead->contLen);
132,386✔
53
  pHead->vgId = htonl(pHead->vgId);
132,386✔
54

55
  if (msgcb == NULL || msgcb->putToQueueFp == NULL) {
132,386!
56
    rpcFreeCont(pMsg->pCont);
×
57
    pMsg->pCont = NULL;
×
58
    return -1;
×
59
  }
60

61
  int32_t code = tmsgPutToQueue(msgcb, SYNC_QUEUE, pMsg);
132,386✔
62
  if (code != 0) {
132,386✔
63
    rpcFreeCont(pMsg->pCont);
19✔
64
    pMsg->pCont = NULL;
19✔
65
  }
66
  return code;
132,386✔
67
}
68

69
static int32_t mndSyncSendMsg(const SEpSet *pEpSet, SRpcMsg *pMsg) {
58,571✔
70
  int32_t code = tmsgSendSyncReq(pEpSet, pMsg);
58,571✔
71
  if (code != 0) {
58,571!
72
    rpcFreeCont(pMsg->pCont);
×
73
    pMsg->pCont = NULL;
×
74
  }
75
  return code;
58,571✔
76
}
77

78
static int32_t mndTransValidatePrepareAction(SMnode *pMnode, STrans *pTrans, STransAction *pAction) {
30,566✔
79
  SSdbRaw *pRaw = pAction->pRaw;
30,566✔
80
  SSdb    *pSdb = pMnode->pSdb;
30,566✔
81
  int      code = 0;
30,566✔
82

83
  if (pRaw->status != SDB_STATUS_CREATING) goto _OUT;
30,566✔
84

85
  SdbValidateFp validateFp = pSdb->validateFps[pRaw->type];
14,446✔
86
  if (validateFp) {
14,446✔
87
    code = validateFp(pMnode, pTrans, pRaw);
14,432✔
88
  }
89

90
_OUT:
14✔
91
  return code;
30,566✔
92
}
93

94
static int32_t mndTransValidatePrepareStage(SMnode *pMnode, STrans *pTrans) {
61,791✔
95
  int32_t code = -1;
61,791✔
96
  int32_t action = 0;
61,791✔
97

98
  int32_t numOfActions = taosArrayGetSize(pTrans->prepareActions);
61,791✔
99
  if (numOfActions == 0) {
61,791✔
100
    code = 0;
41,182✔
101
    goto _OUT;
41,182✔
102
  }
103

104
  mInfo("trans:%d, validate %d prepare actions.", pTrans->id, numOfActions);
20,609!
105

106
  for (action = 0; action < numOfActions; ++action) {
51,175✔
107
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, action);
30,566✔
108

109
    if (pAction->actionType != TRANS_ACTION_RAW) {
30,566!
110
      mError("trans:%d, prepare action:%d of unexpected type:%d", pTrans->id, action, pAction->actionType);
×
111
      goto _OUT;
×
112
    }
113

114
    code = mndTransValidatePrepareAction(pMnode, pTrans, pAction);
30,566✔
115
    if (code != 0) {
30,566!
116
      mError("trans:%d, failed to validate prepare action: %d, numOfActions:%d", pTrans->id, action, numOfActions);
×
117
      goto _OUT;
×
118
    }
119
  }
120

121
  code = 0;
20,609✔
122
_OUT:
61,791✔
123
  return code;
61,791✔
124
}
125

126
static int32_t mndTransValidateImp(SMnode *pMnode, STrans *pTrans) {
131,228✔
127
  int32_t code = 0;
131,228✔
128
  if (pTrans->stage == TRN_STAGE_PREPARE) {
131,228✔
129
    if ((code = mndTransCheckConflict(pMnode, pTrans)) < 0) {
61,791!
130
      mError("trans:%d, failed to validate trans conflicts.", pTrans->id);
×
131
      TAOS_RETURN(code);
×
132
    }
133

134
    return mndTransValidatePrepareStage(pMnode, pTrans);
61,791✔
135
  }
136
  TAOS_RETURN(code);
69,437✔
137
}
138

139
static int32_t mndTransValidate(SMnode *pMnode, SSdbRaw *pRaw) {
131,228✔
140
  STrans *pTrans = NULL;
131,228✔
141
  int32_t code = -1;
131,228✔
142

143
  SSdbRow *pRow = mndTransDecode(pRaw);
131,228✔
144
  if (pRow == NULL) {
131,228!
145
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
146
    if (terrno != 0) code = terrno;
×
147
    goto _OUT;
×
148
  }
149

150
  pTrans = sdbGetRowObj(pRow);
131,228✔
151
  if (pTrans == NULL) {
131,228!
152
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
153
    if (terrno != 0) code = terrno;
×
154
    goto _OUT;
×
155
  }
156

157
  code = mndTransValidateImp(pMnode, pTrans);
131,228✔
158

159
_OUT:
131,228✔
160
  if (pTrans) mndTransDropData(pTrans);
131,228!
161
  if (pRow) taosMemoryFreeClear(pRow);
131,228!
162
  if (code) terrno = (terrno ? terrno : TSDB_CODE_MND_TRANS_CONFLICT);
131,228!
163
  TAOS_RETURN(code);
131,228✔
164
}
165

166
int32_t mndProcessWriteMsg(SMnode *pMnode, SRpcMsg *pMsg, SFsmCbMeta *pMeta) {
131,228✔
167
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
131,228✔
168
  SSdbRaw   *pRaw = pMsg->pCont;
131,228✔
169
  STrans    *pTrans = NULL;
131,228✔
170
  int32_t    code = -1;
131,228✔
171
  int32_t    transId = sdbGetIdFromRaw(pMnode->pSdb, pRaw);
131,228✔
172

173
  if (transId <= 0) {
131,228!
174
    mError("trans:%d, invalid commit msg, cache transId:%d seq:%" PRId64, transId, pMgmt->transId, pMgmt->transSeq);
×
175
    code = TSDB_CODE_INVALID_MSG;
×
176
    goto _OUT;
×
177
  }
178

179
  mInfo("trans:%d, process sync proposal, saved:%d code:0x%x, apply index:%" PRId64 " term:%" PRIu64 " config:%" PRId64
131,228!
180
        " role:%s raw:%p sec:%d seq:%" PRId64,
181
        transId, pMgmt->transId, pMeta->code, pMeta->index, pMeta->term, pMeta->lastConfigIndex, syncStr(pMeta->state),
182
        pRaw, pMgmt->transSec, pMgmt->transSeq);
183

184
  code = mndTransValidate(pMnode, pRaw);
131,228✔
185
  if (code != 0) {
131,228!
186
    mError("trans:%d, failed to validate requested trans since %s", transId, terrstr());
×
187
    // code = 0;
188
    pMeta->code = code;
×
189
    goto _OUT;
×
190
  }
191

192
  (void)taosThreadMutexLock(&pMnode->pSdb->filelock);
131,228✔
193
  code = sdbWriteWithoutFree(pMnode->pSdb, pRaw);
131,228✔
194
  if (code != 0) {
131,228!
195
    mError("trans:%d, failed to write to sdb since %s", transId, terrstr());
×
196
    // code = 0;
197
    (void)taosThreadMutexUnlock(&pMnode->pSdb->filelock);
×
198
    pMeta->code = code;
×
199
    goto _OUT;
×
200
  }
201
  (void)taosThreadMutexUnlock(&pMnode->pSdb->filelock);
131,228✔
202

203
  pTrans = mndAcquireTrans(pMnode, transId);
131,228✔
204
  if (pTrans == NULL) {
131,228!
205
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
206
    if (terrno != 0) code = terrno;
×
207
    mError("trans:%d, not found while execute in mnode since %s", transId, tstrerror(code));
×
208
    goto _OUT;
×
209
  }
210

211
  if (pTrans->stage == TRN_STAGE_PREPARE) {
131,228✔
212
    bool continueExec = mndTransPerformPrepareStage(pMnode, pTrans, false);
61,791✔
213
    if (!continueExec) {
61,791!
214
      if (terrno != 0) code = terrno;
×
215
      goto _OUT;
×
216
    }
217
  }
218

219
  mInfo("trans:%d, refresh transaction in process write msg", transId);
131,228!
220
  mndTransRefresh(pMnode, pTrans);
131,228✔
221
  mInfo("trans:%d, refresh transaction in process write msg finished", transId);
131,228!
222

223
  sdbSetApplyInfo(pMnode->pSdb, pMeta->index, pMeta->term, pMeta->lastConfigIndex);
131,228✔
224
  code = sdbWriteFile(pMnode->pSdb, tsMndSdbWriteDelta);
131,228✔
225

226
_OUT:
131,228✔
227
  if (pTrans) mndReleaseTrans(pMnode, pTrans);
131,228!
228
  TAOS_RETURN(code);
131,228✔
229
}
230

231
static int32_t mndPostMgmtCode(SMnode *pMnode, int32_t code) {
133,902✔
232
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
133,902✔
233
  (void)taosThreadMutexLock(&pMgmt->lock);
133,902✔
234
  if (pMgmt->transId == 0) {
133,902✔
235
    goto _OUT;
22,330✔
236
  }
237

238
  int32_t transId = pMgmt->transId;
111,572✔
239
  pMgmt->transId = 0;
111,572✔
240
  pMgmt->transSec = 0;
111,572✔
241
  pMgmt->transSeq = 0;
111,572✔
242
  pMgmt->errCode = code;
111,572✔
243
  if (tsem_post(&pMgmt->syncSem) < 0) {
111,572!
244
    mError("trans:%d, failed to post sem", transId);
×
245
  }
246

247
  if (pMgmt->errCode != 0) {
111,572!
248
    mError("trans:%d, failed to propose since %s, post sem", transId, tstrerror(pMgmt->errCode));
×
249
  } else {
250
    mInfo("trans:%d, is proposed and post sem, seq:%" PRId64, transId, pMgmt->transSeq);
111,572!
251
  }
252

253
_OUT:
×
254
  (void)taosThreadMutexUnlock(&pMgmt->lock);
133,902✔
255
  return 0;
133,902✔
256
}
257

258
int32_t mndSyncCommitMsg(const SSyncFSM *pFsm, SRpcMsg *pMsg, SFsmCbMeta *pMeta) {
133,902✔
259
  SMnode *pMnode = pFsm->data;
133,902✔
260
  int32_t code = pMsg->code;
133,902✔
261
  if (code != 0) {
133,902!
262
    goto _OUT;
×
263
  }
264

265
  pMsg->info.conn.applyIndex = pMeta->index;
133,902✔
266
  pMsg->info.conn.applyTerm = pMeta->term;
133,902✔
267
  pMeta->code = 0;
133,902✔
268

269
  atomic_store_64(&pMnode->applied, pMsg->info.conn.applyIndex);
133,902✔
270

271
  if (!syncUtilUserCommit(pMsg->msgType)) {
133,902✔
272
    goto _OUT;
2,674✔
273
  }
274

275
  code = mndProcessWriteMsg(pMnode, pMsg, pMeta);
131,228✔
276

277
_OUT:
133,902✔
278
  mndPostMgmtCode(pMnode, code ? code : pMeta->code);
133,902!
279
  rpcFreeCont(pMsg->pCont);
133,902✔
280
  pMsg->pCont = NULL;
133,902✔
281
  TAOS_RETURN(code);
133,902✔
282
}
283

284
SyncIndex mndSyncAppliedIndex(const SSyncFSM *pFSM) {
125,400✔
285
  SMnode *pMnode = pFSM->data;
125,400✔
286
  return atomic_load_64(&pMnode->applied);
125,400✔
287
}
288

289
int32_t mndSyncGetSnapshot(const SSyncFSM *pFsm, SSnapshot *pSnapshot, void *pReaderParam, void **ppReader) {
×
290
  mInfo("start to read snapshot from sdb in atomic way");
×
291
  SMnode *pMnode = pFsm->data;
×
292
  return sdbStartRead(pMnode->pSdb, (SSdbIter **)ppReader, &pSnapshot->lastApplyIndex, &pSnapshot->lastApplyTerm,
×
293
                      &pSnapshot->lastConfigIndex);
×
294
  return 0;
295
}
296

297
static int32_t mndSyncGetSnapshotInfo(const SSyncFSM *pFsm, SSnapshot *pSnapshot) {
72,733✔
298
  SMnode *pMnode = pFsm->data;
72,733✔
299
  sdbGetCommitInfo(pMnode->pSdb, &pSnapshot->lastApplyIndex, &pSnapshot->lastApplyTerm, &pSnapshot->lastConfigIndex);
72,733✔
300
  return 0;
72,733✔
301
}
302

303
void mndRestoreFinish(const SSyncFSM *pFsm, const SyncIndex commitIdx) {
2,898✔
304
  SMnode *pMnode = pFsm->data;
2,898✔
305

306
  if (!pMnode->deploy) {
2,898✔
307
    if (!pMnode->restored) {
1,028✔
308
      mInfo("vgId:1, sync restore finished, and will handle outstanding transactions");
642!
309
      mndTransPullup(pMnode);
642✔
310
      mndSetRestored(pMnode, true);
642✔
311
    } else {
312
      mInfo("vgId:1, sync restore finished, repeat call");
386!
313
    }
314
  } else {
315
    mInfo("vgId:1, sync restore finished");
1,870!
316
  }
317
  int32_t code = mndRefreshUserIpWhiteList(pMnode);
2,898✔
318
  if (code != 0) {
2,898!
319
    mError("vgId:1, failed to refresh user ip white list since %s", tstrerror(code));
×
320
    mndSetRestored(pMnode, false);
×
321
  }
322

323
  SyncIndex fsmIndex = mndSyncAppliedIndex(pFsm);
2,898✔
324
  if (commitIdx != fsmIndex) {
2,898!
325
    mError("vgId:1, failed to sync restore, commitIdx:%" PRId64 " is not equal to appliedIdx:%" PRId64, commitIdx,
×
326
           fsmIndex);
327
    mndSetRestored(pMnode, false);
×
328
  }
329
}
2,898✔
330

331
void mndAfterRestored(const SSyncFSM *pFsm, const SyncIndex commitIdx) {
2,898✔
332
  SMnode *pMnode = pFsm->data;
2,898✔
333

334
  if (!pMnode->deploy) {
2,898✔
335
    if (sdbAfterRestored(pMnode->pSdb) != 0) {
1,028!
336
      mError("failed to prepare sdb while start mnode");
×
337
    }
338
    mInfo("vgId:1, sync restore finished and restore sdb success");
1,028!
339
  }
340
}
2,898✔
341

342
int32_t mndSnapshotStartRead(const SSyncFSM *pFsm, void *pParam, void **ppReader) {
×
343
  mInfo("start to read snapshot from sdb");
×
344
  SMnode *pMnode = pFsm->data;
×
345
  return sdbStartRead(pMnode->pSdb, (SSdbIter **)ppReader, NULL, NULL, NULL);
×
346
}
347

348
static void mndSnapshotStopRead(const SSyncFSM *pFsm, void *pReader) {
×
349
  mInfo("stop to read snapshot from sdb");
×
350
  SMnode *pMnode = pFsm->data;
×
351
  sdbStopRead(pMnode->pSdb, pReader);
×
352
}
×
353

354
int32_t mndSnapshotDoRead(const SSyncFSM *pFsm, void *pReader, void **ppBuf, int32_t *len) {
×
355
  SMnode *pMnode = pFsm->data;
×
356
  return sdbDoRead(pMnode->pSdb, pReader, ppBuf, len);
×
357
}
358

359
int32_t mndSnapshotStartWrite(const SSyncFSM *pFsm, void *pParam, void **ppWriter) {
×
360
  mInfo("start to apply snapshot to sdb");
×
361
  SMnode *pMnode = pFsm->data;
×
362
  return sdbStartWrite(pMnode->pSdb, (SSdbIter **)ppWriter);
×
363
}
364

365
int32_t mndSnapshotStopWrite(const SSyncFSM *pFsm, void *pWriter, bool isApply, SSnapshot *pSnapshot) {
×
366
  mInfo("stop to apply snapshot to sdb, apply:%d, index:%" PRId64 " term:%" PRIu64 " config:%" PRId64, isApply,
×
367
        pSnapshot->lastApplyIndex, pSnapshot->lastApplyTerm, pSnapshot->lastConfigIndex);
368
  SMnode *pMnode = pFsm->data;
×
369
  return sdbStopWrite(pMnode->pSdb, pWriter, isApply, pSnapshot->lastApplyIndex, pSnapshot->lastApplyTerm,
×
370
                      pSnapshot->lastConfigIndex);
371
}
372

373
int32_t mndSnapshotDoWrite(const SSyncFSM *pFsm, void *pWriter, void *pBuf, int32_t len) {
×
374
  SMnode *pMnode = pFsm->data;
×
375
  return sdbDoWrite(pMnode->pSdb, pWriter, pBuf, len);
×
376
}
377

378
static void mndBecomeFollower(const SSyncFSM *pFsm) {
347✔
379
  SMnode    *pMnode = pFsm->data;
347✔
380
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
347✔
381
  mInfo("vgId:1, becomefollower callback");
347!
382

383
  (void)taosThreadMutexLock(&pMgmt->lock);
347✔
384
  if (pMgmt->transId != 0) {
347!
385
    mInfo("vgId:1, become follower and post sem, trans:%d, failed to propose since not leader", pMgmt->transId);
×
386
    pMgmt->transId = 0;
×
387
    pMgmt->transSec = 0;
×
388
    pMgmt->transSeq = 0;
×
389
    pMgmt->errCode = TSDB_CODE_SYN_NOT_LEADER;
×
390
    if (tsem_post(&pMgmt->syncSem) < 0) {
×
391
      mError("failed to post sem");
×
392
    }
393
  }
394
  (void)taosThreadMutexUnlock(&pMgmt->lock);
347✔
395

396
  msmHandleBecomeNotLeader(pMnode);  
347✔
397
}
347✔
398

399
static void mndBecomeLearner(const SSyncFSM *pFsm) {
107✔
400
  SMnode    *pMnode = pFsm->data;
107✔
401
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
107✔
402
  mInfo("vgId:1, become learner");
107!
403

404
  (void)taosThreadMutexLock(&pMgmt->lock);
107✔
405
  if (pMgmt->transId != 0) {
107!
406
    mInfo("vgId:1, become learner and post sem, trans:%d, failed to propose since not leader", pMgmt->transId);
×
407
    pMgmt->transId = 0;
×
408
    pMgmt->transSec = 0;
×
409
    pMgmt->transSeq = 0;
×
410
    pMgmt->errCode = TSDB_CODE_SYN_NOT_LEADER;
×
411
    if (tsem_post(&pMgmt->syncSem) < 0) {
×
412
      mError("failed to post sem");
×
413
    }
414
  }
415
  (void)taosThreadMutexUnlock(&pMgmt->lock);
107✔
416

417
  msmHandleBecomeNotLeader(pMnode);  
107✔
418
}
107✔
419

420
static void mndBecomeLeader(const SSyncFSM *pFsm) {
2,259✔
421
  mInfo("vgId:1, becomeleader callback");
2,259!
422
  SMnode *pMnode = pFsm->data;
2,259✔
423

424
  msmHandleBecomeLeader(pMnode);
2,259✔
425
}
2,259✔
426

427
static bool mndApplyQueueEmpty(const SSyncFSM *pFsm) {
×
428
  SMnode *pMnode = pFsm->data;
×
429

430
  if (pMnode != NULL && pMnode->msgCb.qsizeFp != NULL) {
×
431
    int32_t itemSize = tmsgGetQueueSize(&pMnode->msgCb, 1, APPLY_QUEUE);
×
432
    return (itemSize == 0);
×
433
  } else {
434
    return true;
×
435
  }
436
}
437

438
static int32_t mndApplyQueueItems(const SSyncFSM *pFsm) {
8,551✔
439
  SMnode *pMnode = pFsm->data;
8,551✔
440

441
  if (pMnode != NULL && pMnode->msgCb.qsizeFp != NULL) {
8,551!
442
    int32_t itemSize = tmsgGetQueueSize(&pMnode->msgCb, 1, APPLY_QUEUE);
×
443
    return itemSize;
×
444
  } else {
445
    return -1;
8,551✔
446
  }
447
}
448

449
SSyncFSM *mndSyncMakeFsm(SMnode *pMnode) {
2,512✔
450
  SSyncFSM *pFsm = taosMemoryCalloc(1, sizeof(SSyncFSM));
2,512!
451
  pFsm->data = pMnode;
2,512✔
452
  pFsm->FpCommitCb = mndSyncCommitMsg;
2,512✔
453
  pFsm->FpAppliedIndexCb = mndSyncAppliedIndex;
2,512✔
454
  pFsm->FpPreCommitCb = NULL;
2,512✔
455
  pFsm->FpRollBackCb = NULL;
2,512✔
456
  pFsm->FpRestoreFinishCb = mndRestoreFinish;
2,512✔
457
  pFsm->FpAfterRestoredCb = mndAfterRestored;
2,512✔
458
  pFsm->FpLeaderTransferCb = NULL;
2,512✔
459
  pFsm->FpApplyQueueEmptyCb = mndApplyQueueEmpty;
2,512✔
460
  pFsm->FpApplyQueueItems = mndApplyQueueItems;
2,512✔
461
  pFsm->FpReConfigCb = NULL;
2,512✔
462
  pFsm->FpBecomeLeaderCb = mndBecomeLeader;
2,512✔
463
  pFsm->FpBecomeAssignedLeaderCb = NULL;
2,512✔
464
  pFsm->FpBecomeFollowerCb = mndBecomeFollower;
2,512✔
465
  pFsm->FpBecomeLearnerCb = mndBecomeLearner;
2,512✔
466
  pFsm->FpGetSnapshot = mndSyncGetSnapshot;
2,512✔
467
  pFsm->FpGetSnapshotInfo = mndSyncGetSnapshotInfo;
2,512✔
468
  pFsm->FpSnapshotStartRead = mndSnapshotStartRead;
2,512✔
469
  pFsm->FpSnapshotStopRead = mndSnapshotStopRead;
2,512✔
470
  pFsm->FpSnapshotDoRead = mndSnapshotDoRead;
2,512✔
471
  pFsm->FpSnapshotStartWrite = mndSnapshotStartWrite;
2,512✔
472
  pFsm->FpSnapshotStopWrite = mndSnapshotStopWrite;
2,512✔
473
  pFsm->FpSnapshotDoWrite = mndSnapshotDoWrite;
2,512✔
474
  return pFsm;
2,512✔
475
}
476

477
int32_t mndInitSync(SMnode *pMnode) {
2,512✔
478
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
2,512✔
479
  (void)taosThreadMutexInit(&pMgmt->lock, NULL);
2,512✔
480
  (void)taosThreadMutexLock(&pMgmt->lock);
2,512✔
481
  pMgmt->transId = 0;
2,512✔
482
  pMgmt->transSec = 0;
2,512✔
483
  pMgmt->transSeq = 0;
2,512✔
484
  (void)taosThreadMutexUnlock(&pMgmt->lock);
2,512✔
485

486
  SSyncInfo syncInfo = {
2,512✔
487
      .snapshotStrategy = SYNC_STRATEGY_STANDARD_SNAPSHOT,
488
      .batchSize = 1,
489
      .vgId = 1,
490
      .pWal = pMnode->pWal,
2,512✔
491
      .msgcb = &pMnode->msgCb,
2,512✔
492
      .syncSendMSg = mndSyncSendMsg,
493
      .syncEqMsg = mndSyncEqMsg,
494
      .syncEqCtrlMsg = mndSyncEqCtrlMsg,
495
      .pingMs = 5000,
496
      .electMs = 3000,
497
      .heartbeatMs = 500,
498
  };
499

500
  snprintf(syncInfo.path, sizeof(syncInfo.path), "%s%ssync", pMnode->path, TD_DIRSEP);
2,512✔
501
  syncInfo.pFsm = mndSyncMakeFsm(pMnode);
2,512✔
502

503
  mInfo("vgId:1, start to open mnode sync, replica:%d selfIndex:%d", pMgmt->numOfReplicas, pMgmt->selfIndex);
2,512!
504
  SSyncCfg *pCfg = &syncInfo.syncCfg;
2,512✔
505
  pCfg->totalReplicaNum = pMgmt->numOfTotalReplicas;
2,512✔
506
  pCfg->replicaNum = pMgmt->numOfReplicas;
2,512✔
507
  pCfg->myIndex = pMgmt->selfIndex;
2,512✔
508
  pCfg->lastIndex = pMgmt->lastIndex;
2,512✔
509
  for (int32_t i = 0; i < pMgmt->numOfTotalReplicas; ++i) {
4,912✔
510
    SNodeInfo *pNode = &pCfg->nodeInfo[i];
2,400✔
511
    pNode->nodeId = pMgmt->replicas[i].id;
2,400✔
512
    pNode->nodePort = pMgmt->replicas[i].port;
2,400✔
513
    tstrncpy(pNode->nodeFqdn, pMgmt->replicas[i].fqdn, sizeof(pNode->nodeFqdn));
2,400✔
514
    pNode->nodeRole = pMgmt->nodeRoles[i];
2,400✔
515
    bool update = tmsgUpdateDnodeInfo(&pNode->nodeId, &pNode->clusterId, pNode->nodeFqdn, &pNode->nodePort);
2,400✔
516
    mInfo("vgId:1, index:%d ep:%s:%u dnode:%d cluster:%" PRId64 ", update:%d", i, pNode->nodeFqdn, pNode->nodePort,
2,400!
517
          pNode->nodeId, pNode->clusterId, update);
518
  }
519

520
  int32_t code = 0;
2,512✔
521
  if ((code = tsem_init(&pMgmt->syncSem, 0, 0)) < 0) {
2,512!
522
    mError("failed to open sync, tsem_init, since %s", tstrerror(code));
×
523
    TAOS_RETURN(code);
×
524
  }
525
  pMgmt->sync = syncOpen(&syncInfo, 1);  // always check
2,512✔
526
  if (pMgmt->sync <= 0) {
2,512!
527
    if (terrno != 0) code = terrno;
×
528
    mError("failed to open sync since %s", tstrerror(code));
×
529
    TAOS_RETURN(code);
×
530
  }
531
  pMnode->pSdb->sync = pMgmt->sync;
2,512✔
532

533
  mInfo("vgId:1, mnode sync is opened, id:%" PRId64, pMgmt->sync);
2,512!
534
  TAOS_RETURN(code);
2,512✔
535
}
536

537
void mndCleanupSync(SMnode *pMnode) {
2,512✔
538
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
2,512✔
539
  syncStop(pMgmt->sync);
2,512✔
540
  mInfo("mnode-sync is stopped, id:%" PRId64, pMgmt->sync);
2,512!
541

542
  if (tsem_destroy(&pMgmt->syncSem) < 0) {
2,512!
543
    mError("failed to destroy sem");
×
544
  }
545
  (void)taosThreadMutexDestroy(&pMgmt->lock);
2,512✔
546
  memset(pMgmt, 0, sizeof(SSyncMgmt));
2,512✔
547
}
2,512✔
548

549
void mndSyncCheckTimeout(SMnode *pMnode) {
1,292✔
550
  mTrace("check sync timeout");
1,292✔
551
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
1,292✔
552
  (void)taosThreadMutexLock(&pMgmt->lock);
1,292✔
553
  if (pMgmt->transId != 0) {
1,292✔
554
    int32_t curSec = taosGetTimestampSec();
6✔
555
    int32_t delta = curSec - pMgmt->transSec;
6✔
556
    if (delta > MNODE_TIMEOUT_SEC) {
6!
557
      mError("trans:%d, failed to propose since timeout, start:%d cur:%d delta:%d seq:%" PRId64, pMgmt->transId,
×
558
             pMgmt->transSec, curSec, delta, pMgmt->transSeq);
559
      // pMgmt->transId = 0;
560
      // pMgmt->transSec = 0;
561
      // pMgmt->transSeq = 0;
562
      // terrno = TSDB_CODE_SYN_TIMEOUT;
563
      // pMgmt->errCode = TSDB_CODE_SYN_TIMEOUT;
564
      // if (tsem_post(&pMgmt->syncSem) < 0) {
565
      //  mError("failed to post sem");
566
      //}
567
    } else {
568
      mDebug("trans:%d, waiting for sync confirm, start:%d cur:%d delta:%d seq:%" PRId64, pMgmt->transId,
6!
569
             pMgmt->transSec, curSec, curSec - pMgmt->transSec, pMgmt->transSeq);
570
    }
571
  } else {
572
    // mTrace("check sync timeout msg, no trans waiting for confirm");
573
  }
574
  (void)taosThreadMutexUnlock(&pMgmt->lock);
1,292✔
575
}
1,292✔
576

577
int32_t mndSyncPropose(SMnode *pMnode, SSdbRaw *pRaw, int32_t transId) {
111,596✔
578
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
111,596✔
579

580
  SRpcMsg req = {.msgType = TDMT_MND_APPLY_MSG, .contLen = sdbGetRawTotalSize(pRaw)};
111,596✔
581
  if (req.contLen <= 0) return terrno;
111,596!
582

583
  req.pCont = rpcMallocCont(req.contLen);
111,596✔
584
  if (req.pCont == NULL) return terrno;
111,596!
585
  memcpy(req.pCont, pRaw, req.contLen);
111,596✔
586

587
  (void)taosThreadMutexLock(&pMgmt->lock);
111,596✔
588
  pMgmt->errCode = 0;
111,596✔
589

590
  if (pMgmt->transId != 0) {
111,596!
591
    mError("trans:%d, can't be proposed since trans:%d already waiting for confirm", transId, pMgmt->transId);
×
592
    (void)taosThreadMutexUnlock(&pMgmt->lock);
×
593
    rpcFreeCont(req.pCont);
×
594
    TAOS_RETURN(TSDB_CODE_MND_LAST_TRANS_NOT_FINISHED);
×
595
  }
596

597
  mInfo("trans:%d, will be proposed", transId);
111,596!
598
  pMgmt->transId = transId;
111,596✔
599
  pMgmt->transSec = taosGetTimestampSec();
111,596✔
600

601
  int64_t seq = 0;
111,596✔
602
  int32_t code = syncPropose(pMgmt->sync, &req, false, &seq);
111,596✔
603
  if (code == 0) {
111,596✔
604
    mInfo("trans:%d, is proposing and wait sem, seq:%" PRId64, transId, seq);
111,577!
605
    pMgmt->transSeq = seq;
111,577✔
606
    (void)taosThreadMutexUnlock(&pMgmt->lock);
111,577✔
607
    code = tsem_wait(&pMgmt->syncSem);
111,577✔
608
  } else if (code > 0) {
19!
609
    mInfo("trans:%d, confirm at once since replica is 1, continue execute", transId);
×
610
    pMgmt->transId = 0;
×
611
    pMgmt->transSec = 0;
×
612
    pMgmt->transSeq = 0;
×
613
    (void)taosThreadMutexUnlock(&pMgmt->lock);
×
614
    code = sdbWriteWithoutFree(pMnode->pSdb, pRaw);
×
615
    if (code == 0) {
×
616
      sdbSetApplyInfo(pMnode->pSdb, req.info.conn.applyIndex, req.info.conn.applyTerm, SYNC_INDEX_INVALID);
×
617
    }
618
  } else {
619
    mError("trans:%d, failed to proposed since %s", transId, terrstr());
19!
620
    pMgmt->transId = 0;
19✔
621
    pMgmt->transSec = 0;
19✔
622
    pMgmt->transSeq = 0;
19✔
623
    (void)taosThreadMutexUnlock(&pMgmt->lock);
19✔
624
    if (terrno == 0) {
19!
625
      terrno = TSDB_CODE_APP_ERROR;
×
626
    }
627
  }
628

629
  rpcFreeCont(req.pCont);
111,596✔
630
  req.pCont = NULL;
111,596✔
631
  if (code != 0) {
111,596✔
632
    mError("trans:%d, failed to propose, code:0x%x", pMgmt->transId, code);
19!
633
    return code;
19✔
634
  }
635

636
  terrno = pMgmt->errCode;
111,577✔
637
  return terrno;
111,577✔
638
}
639

640
void mndSyncStart(SMnode *pMnode) {
2,512✔
641
  mInfo("vgId:1, start to start mnode sync");
2,512!
642
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
2,512✔
643
  if (syncStart(pMgmt->sync) < 0) {
2,512!
644
    mError("vgId:1, failed to start sync, id:%" PRId64, pMgmt->sync);
×
645
    return;
×
646
  }
647
  mInfo("vgId:1, mnode sync started, id:%" PRId64, pMgmt->sync);
2,512!
648
}
649

650
void mndSyncStop(SMnode *pMnode) {
2,512✔
651
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
2,512✔
652

653
  (void)taosThreadMutexLock(&pMgmt->lock);
2,512✔
654
  if (pMgmt->transId != 0) {
2,512✔
655
    mInfo("vgId:1, trans:%d, is stopped and post sem", pMgmt->transId);
5!
656
    pMgmt->transId = 0;
5✔
657
    pMgmt->transSec = 0;
5✔
658
    pMgmt->errCode = TSDB_CODE_APP_IS_STOPPING;
5✔
659
    if (tsem_post(&pMgmt->syncSem) < 0) {
5!
660
      mError("failed to post sem");
×
661
    }
662
  }
663
  (void)taosThreadMutexUnlock(&pMgmt->lock);
2,512✔
664
}
2,512✔
665

666
bool mndIsLeader(SMnode *pMnode) {
382,678✔
667
  terrno = 0;
382,678✔
668
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
382,678✔
669

670
  if (terrno != 0) {
382,678!
671
    mDebug("vgId:1, mnode is stopping");
×
672
    return false;
×
673
  }
674

675
  if (state.state != TAOS_SYNC_STATE_LEADER) {
382,678✔
676
    terrno = TSDB_CODE_SYN_NOT_LEADER;
18,190✔
677
    mDebug("vgId:1, mnode not leader, state:%s", syncStr(state.state));
18,190!
678
    return false;
18,190✔
679
  }
680

681
  if (!state.restored || !pMnode->restored) {
364,488✔
682
    terrno = TSDB_CODE_SYN_RESTORING;
33,860✔
683
    mDebug("vgId:1, mnode not restored:%d:%d", state.restored, pMnode->restored);
33,860✔
684
    return false;
33,860✔
685
  }
686

687
  return true;
330,628✔
688
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc