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

taosdata / TDengine / #4192

30 May 2025 03:55AM UTC coverage: 63.023% (-0.2%) from 63.267%
#4192

push

travis-ci

web-flow
fix:defined col bind in interlace mode (#31246)

157832 of 318864 branches covered (49.5%)

Branch coverage included in aggregate %.

1 of 3 new or added lines in 1 file covered. (33.33%)

2934 existing lines in 172 files now uncovered.

243367 of 317732 relevant lines covered (76.6%)

17346426.62 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) {
133,735✔
47
  if (pMsg == NULL || pMsg->pCont == NULL) {
133,735!
48
    return -1;
×
49
  }
50

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

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

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

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

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

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

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

90
_OUT:
456✔
91
  return code;
30,975✔
92
}
93

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

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

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

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

109
    if (pAction->actionType != TRANS_ACTION_RAW) {
30,975!
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,975✔
115
    if (code != 0) {
30,975!
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;
19,734✔
122
_OUT:
61,887✔
123
  return code;
61,887✔
124
}
125

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

134
    return mndTransValidatePrepareStage(pMnode, pTrans);
61,887✔
135
  }
136
  TAOS_RETURN(code);
71,303✔
137
}
138

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

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

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

157
  code = mndTransValidateImp(pMnode, pTrans);
133,190✔
158

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

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

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

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

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

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

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

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

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

247
  if (pMgmt->errCode != 0) {
113,154!
UNCOV
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);
113,154!
251
  }
252

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

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

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

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

271
  if (!syncUtilUserCommit(pMsg->msgType)) {
135,593✔
272
    goto _OUT;
2,403✔
273
  }
274

275
  code = mndProcessWriteMsg(pMnode, pMsg, pMeta);
133,190✔
276

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

284
SyncIndex mndSyncAppliedIndex(const SSyncFSM *pFSM) {
136,986✔
285
  SMnode *pMnode = pFSM->data;
136,986✔
286
  return atomic_load_64(&pMnode->applied);
136,986✔
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,
×
UNCOV
293
                      &pSnapshot->lastConfigIndex);
×
294
  return 0;
295
}
296

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

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

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

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

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

334
  if (!pMnode->deploy) {
2,476✔
335
    if (sdbAfterRestored(pMnode->pSdb) != 0) {
842!
UNCOV
336
      mError("failed to prepare sdb while start mnode");
×
337
    }
338
    mInfo("vgId:1, sync restore finished and restore sdb success");
842!
339
  }
340
}
2,476✔
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;
×
UNCOV
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);
×
UNCOV
352
}
×
353

354
int32_t mndSnapshotDoRead(const SSyncFSM *pFsm, void *pReader, void **ppBuf, int32_t *len) {
×
355
  SMnode *pMnode = pFsm->data;
×
UNCOV
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;
×
UNCOV
362
  return sdbStartWrite(pMnode->pSdb, (SSdbIter **)ppWriter);
×
363
}
364

365
int32_t mndSnapshotStopWrite(const SSyncFSM *pFsm, void *pWriter, bool isApply, SSnapshot *pSnapshot) {
×
UNCOV
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;
×
UNCOV
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;
×
UNCOV
375
  return sdbDoWrite(pMnode->pSdb, pWriter, pBuf, len);
×
376
}
377

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

383
  (void)taosThreadMutexLock(&pMgmt->lock);
338✔
384
  if (pMgmt->transId != 0) {
338!
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) {
×
UNCOV
391
      mError("failed to post sem");
×
392
    }
393
  }
394
  (void)taosThreadMutexUnlock(&pMgmt->lock);
338✔
395

396
  mndUpdateStreamExecInfoRole(pMnode, NODE_ROLE_FOLLOWER);
338✔
397
}
338✔
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) {
×
UNCOV
412
      mError("failed to post sem");
×
413
    }
414
  }
415
  (void)taosThreadMutexUnlock(&pMgmt->lock);
107✔
416
}
107✔
417

418
static void mndBecomeLeader(const SSyncFSM *pFsm) {
1,942✔
419
  mInfo("vgId:1, become leader");
1,942!
420
  SMnode *pMnode = pFsm->data;
1,942✔
421

422
  mndUpdateStreamExecInfoRole(pMnode, NODE_ROLE_LEADER);
1,942✔
423
  mndStreamResetInitTaskListLoadFlag();
1,942✔
424
}
1,942✔
425

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

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

437
static int32_t mndApplyQueueItems(const SSyncFSM *pFsm) {
19,296✔
438
  SMnode *pMnode = pFsm->data;
19,296✔
439

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

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

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

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

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

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

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

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

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

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

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

576
int32_t mndSyncPropose(SMnode *pMnode, SSdbRaw *pRaw, int32_t transId) {
113,171✔
577
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
113,171✔
578

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

582
  req.pCont = rpcMallocCont(req.contLen);
113,171✔
583
  if (req.pCont == NULL) return terrno;
113,171!
584
  memcpy(req.pCont, pRaw, req.contLen);
113,171✔
585

586
  (void)taosThreadMutexLock(&pMgmt->lock);
113,171✔
587
  pMgmt->errCode = 0;
113,171✔
588

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

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

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

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

635
  terrno = pMgmt->errCode;
113,166✔
636
  return terrno;
113,166✔
637
}
638

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

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

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

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

669
  if (terrno != 0) {
480,594!
670
    mDebug("vgId:1, mnode is stopping");
×
UNCOV
671
    return false;
×
672
  }
673

674
  if (state.state != TAOS_SYNC_STATE_LEADER) {
480,594✔
675
    terrno = TSDB_CODE_SYN_NOT_LEADER;
19,930✔
676
    mDebug("vgId:1, mnode not leader, state:%s", syncStr(state.state));
19,930!
677
    return false;
19,930✔
678
  }
679

680
  if (!state.restored || !pMnode->restored) {
460,664✔
681
    terrno = TSDB_CODE_SYN_RESTORING;
29,640✔
682
    mDebug("vgId:1, mnode not restored:%d:%d", state.restored, pMnode->restored);
29,640✔
683
    return false;
29,640✔
684
  }
685

686
  return true;
431,024✔
687
}
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