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

taosdata / TDengine / #4857

17 Nov 2025 09:53AM UTC coverage: 64.135% (-0.2%) from 64.286%
#4857

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

218 of 311 new or added lines in 32 files covered. (70.1%)

5044 existing lines in 121 files now uncovered.

151302 of 235910 relevant lines covered (64.14%)

116627960.99 hits per line

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

70.5
/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) {
43,898,250✔
47
  if (pMsg == NULL || pMsg->pCont == NULL) {
43,898,250✔
48
    return -1;
×
49
  }
50

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

55
  if (msgcb == NULL || msgcb->putToQueueFp == NULL) {
43,898,250✔
56
    rpcFreeCont(pMsg->pCont);
×
57
    pMsg->pCont = NULL;
×
58
    return -1;
×
59
  }
60

61
  int32_t code = tmsgPutToQueue(msgcb, SYNC_QUEUE, pMsg);
43,898,250✔
62
  if (code != 0) {
43,898,250✔
63
    rpcFreeCont(pMsg->pCont);
7,698✔
64
    pMsg->pCont = NULL;
7,698✔
65
  }
66
  return code;
43,898,250✔
67
}
68

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

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

83
  if (pRaw->status != SDB_STATUS_CREATING) goto _OUT;
12,762,163✔
84

85
  SdbValidateFp validateFp = pSdb->validateFps[pRaw->type];
4,171,082✔
86
  if (validateFp) {
4,171,082✔
87
    code = validateFp(pMnode, pTrans, pRaw);
4,144,454✔
88
  }
89

90
_OUT:
26,628✔
91
  return code;
12,762,163✔
92
}
93

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

98
  int32_t numOfActions = taosArrayGetSize(pTrans->prepareActions);
19,399,970✔
99
  if (numOfActions == 0) {
19,399,970✔
100
    code = 0;
9,579,019✔
101
    goto _OUT;
9,579,019✔
102
  }
103

104
  mInfo("trans:%d, validate %d prepare actions.", pTrans->id, numOfActions);
9,820,951✔
105

106
  for (action = 0; action < numOfActions; ++action) {
22,583,114✔
107
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, action);
12,762,163✔
108

109
    if (pAction->actionType != TRANS_ACTION_RAW) {
12,762,163✔
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);
12,762,163✔
115
    if (code != 0) {
12,762,163✔
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;
9,820,951✔
122
_OUT:
19,399,970✔
123
  return code;
19,399,970✔
124
}
125

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

134
    return mndTransValidatePrepareStage(pMnode, pTrans);
19,399,970✔
135
  }
136
  TAOS_RETURN(code);
22,428,927✔
137
}
138

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

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

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

157
  code = mndTransValidateImp(pMnode, pTrans);
41,828,897✔
158

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

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

173
  if (transId <= 0) {
41,828,897✔
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
41,828,897✔
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);
41,828,897✔
185
  if (code != 0) {
41,828,897✔
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);
41,828,897✔
193
  code = sdbWriteWithoutFree(pMnode->pSdb, pRaw);
41,828,897✔
194
  if (code != 0) {
41,828,897✔
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);
41,828,897✔
202

203
  pTrans = mndAcquireTrans(pMnode, transId);
41,828,897✔
204
  if (pTrans == NULL) {
41,828,897✔
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) {
41,828,897✔
212
    bool continueExec = mndTransPerformPrepareStage(pMnode, pTrans, false);
19,399,970✔
213
    if (!continueExec) {
19,399,970✔
214
      if (terrno != 0) code = terrno;
×
215
      goto _OUT;
×
216
    }
217
  }
218

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

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

226
_OUT:
41,828,897✔
227
  if (pTrans) mndReleaseTrans(pMnode, pTrans);
41,828,897✔
228
  TAOS_RETURN(code);
41,828,897✔
229
}
230

231
static int32_t mndPostMgmtCode(SMnode *pMnode, int32_t code) {
42,391,259✔
232
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
42,391,259✔
233
  (void)taosThreadMutexLock(&pMgmt->lock);
42,391,259✔
234
  if (pMgmt->transId == 0) {
42,391,259✔
235
    goto _OUT;
7,473,917✔
236
  }
237

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

247
  if (pMgmt->errCode != 0) {
34,917,342✔
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);
34,917,342✔
251
  }
252

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

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

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

269
  atomic_store_64(&pMnode->applied, pMsg->info.conn.applyIndex);
42,391,259✔
270

271
  if (!syncUtilUserCommit(pMsg->msgType)) {
42,391,259✔
272
    goto _OUT;
562,362✔
273
  }
274

275
  code = mndProcessWriteMsg(pMnode, pMsg, pMeta);
41,828,897✔
276

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

284
SyncIndex mndSyncAppliedIndex(const SSyncFSM *pFSM) {
38,014,812✔
285
  SMnode *pMnode = pFSM->data;
38,014,812✔
286
  return atomic_load_64(&pMnode->applied);
38,014,812✔
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) {
13,068,676✔
298
  SMnode *pMnode = pFsm->data;
13,068,676✔
299
  sdbGetCommitInfo(pMnode->pSdb, &pSnapshot->lastApplyIndex, &pSnapshot->lastApplyTerm, &pSnapshot->lastConfigIndex);
13,068,676✔
300
  return 0;
13,068,676✔
301
}
302

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

306
  if (!pMnode->deploy) {
906,957✔
307
    if (!pMnode->restored) {
268,745✔
308
      mInfo("vgId:1, sync restore finished, and will handle outstanding transactions");
173,740✔
309
      mndTransPullup(pMnode);
173,740✔
310
      mndSetRestored(pMnode, true);
173,740✔
311
    } else {
312
      mInfo("vgId:1, sync restore finished, repeat call");
95,005✔
313
    }
314
  } else {
315
    mInfo("vgId:1, sync restore finished");
638,212✔
316
  }
317
  int32_t code = mndRefreshUserIpWhiteList(pMnode);
906,957✔
318
  if (code != 0) {
906,957✔
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);
906,957✔
324
  if (commitIdx != fsmIndex) {
906,957✔
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
}
906,957✔
330

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

334
  if (!pMnode->deploy) {
906,957✔
335
    if (sdbAfterRestored(pMnode->pSdb) != 0) {
268,745✔
336
      mError("failed to prepare sdb while start mnode");
×
337
    }
338
    mInfo("vgId:1, sync restore finished and restore sdb success");
268,745✔
339
  }
340
}
906,957✔
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) {
98,471✔
379
  SMnode    *pMnode = pFsm->data;
98,471✔
380
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
98,471✔
381
  mInfo("vgId:1, becomefollower callback");
98,471✔
382

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

396
  msmHandleBecomeNotLeader(pMnode);  
98,471✔
397
}
98,471✔
398

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

404
  (void)taosThreadMutexLock(&pMgmt->lock);
30,206✔
405
  if (pMgmt->transId != 0) {
30,206✔
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);
30,206✔
416

417
  msmHandleBecomeNotLeader(pMnode);  
30,206✔
418
}
30,206✔
419

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

424
  msmHandleBecomeLeader(pMnode);
421,364✔
425
}
421,364✔
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) {
1,736,622✔
439
  SMnode *pMnode = pFsm->data;
1,736,622✔
440

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

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

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

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

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

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

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

534
  mInfo("vgId:1, mnode sync is opened, id:%" PRId64, pMgmt->sync);
493,662✔
535
  TAOS_RETURN(code);
493,662✔
536
}
537

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

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

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

578
int32_t mndSyncPropose(SMnode *pMnode, SSdbRaw *pRaw, int32_t transId) {
34,925,661✔
579
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
34,925,661✔
580

581
  SRpcMsg req = {.msgType = TDMT_MND_APPLY_MSG, .contLen = sdbGetRawTotalSize(pRaw)};
34,925,661✔
582
  if (req.contLen <= 0) return terrno;
34,925,661✔
583

584
  req.pCont = rpcMallocCont(req.contLen);
34,925,661✔
585
  if (req.pCont == NULL) return terrno;
34,925,661✔
586
  memcpy(req.pCont, pRaw, req.contLen);
34,925,661✔
587

588
  (void)taosThreadMutexLock(&pMgmt->lock);
34,925,661✔
589
  pMgmt->errCode = 0;
34,925,661✔
590

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

598
  mInfo("trans:%d, will be proposed", transId);
34,925,661✔
599
  pMgmt->transId = transId;
34,925,661✔
600
  pMgmt->transSec = taosGetTimestampSec();
34,925,661✔
601

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

630
  rpcFreeCont(req.pCont);
34,925,661✔
631
  req.pCont = NULL;
34,925,661✔
632
  if (code != 0) {
34,925,661✔
633
    mError("trans:%d, failed to propose, code:0x%x", pMgmt->transId, code);
7,698✔
634
    return code;
7,698✔
635
  }
636

637
  terrno = pMgmt->errCode;
34,917,963✔
638
  return terrno;
34,917,963✔
639
}
640

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

651
void mndSyncStop(SMnode *pMnode) {
492,846✔
652
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
492,846✔
653

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

667
bool mndIsLeader(SMnode *pMnode) {
138,757,932✔
668
  terrno = 0;
138,757,932✔
669
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
138,757,932✔
670

671
  if (terrno != 0) {
138,757,932✔
672
    mDebug("vgId:1, mnode is stopping");
×
673
    return false;
×
674
  }
675

676
  if (state.state != TAOS_SYNC_STATE_LEADER) {
138,757,932✔
677
    terrno = TSDB_CODE_SYN_NOT_LEADER;
6,929,692✔
678
    mDebug("vgId:1, mnode not leader, state:%s", syncStr(state.state));
6,929,692✔
679
    return false;
6,929,692✔
680
  }
681

682
  if (!state.restored || !pMnode->restored) {
131,828,240✔
683
    terrno = TSDB_CODE_SYN_RESTORING;
5,924,156✔
684
    mDebug("vgId:1, mnode not restored:%d:%d", state.restored, pMnode->restored);
5,924,156✔
685
    return false;
5,924,156✔
686
  }
687

688
  return true;
125,904,084✔
689
}
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