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

taosdata / TDengine / #3847

11 Apr 2025 06:14AM UTC coverage: 62.612% (+0.2%) from 62.398%
#3847

push

travis-ci

web-flow
Merge pull request #30758 from taosdata/merge/mainto3.0

merge: from main to 3.0 branch

154571 of 315259 branches covered (49.03%)

Branch coverage included in aggregate %.

63 of 80 new or added lines in 9 files covered. (78.75%)

946 existing lines in 106 files now uncovered.

240135 of 315138 relevant lines covered (76.2%)

19768383.08 hits per line

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

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

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

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

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

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

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

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

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

90
_OUT:
483✔
91
  return code;
25,270✔
92
}
93

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

98
  int32_t numOfActions = taosArrayGetSize(pTrans->prepareActions);
53,507✔
99
  if (numOfActions == 0) {
53,507✔
100
    code = 0;
37,456✔
101
    goto _OUT;
37,456✔
102
  }
103

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

106
  for (action = 0; action < numOfActions; ++action) {
41,321✔
107
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, action);
25,270✔
108

109
    if (pAction->actionType != TRANS_ACTION_RAW) {
25,270!
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);
25,270✔
115
    if (code != 0) {
25,270!
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;
16,051✔
122
_OUT:
53,507✔
123
  return code;
53,507✔
124
}
125

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

134
    return mndTransValidatePrepareStage(pMnode, pTrans);
53,507✔
135
  }
136
  TAOS_RETURN(code);
62,627✔
137
}
138

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

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

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

157
  code = mndTransValidateImp(pMnode, pTrans);
116,134✔
158

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

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

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

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

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

222
  sdbSetApplyInfo(pMnode->pSdb, pMeta->index, pMeta->term, pMeta->lastConfigIndex);
116,134✔
223
  code = sdbWriteFile(pMnode->pSdb, tsMndSdbWriteDelta);
116,134✔
224

225
_OUT:
116,134✔
226
  if (pTrans) mndReleaseTrans(pMnode, pTrans);
116,134!
227
  TAOS_RETURN(code);
116,134✔
228
}
229

230
static int32_t mndPostMgmtCode(SMnode *pMnode, int32_t code) {
118,126✔
231
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
118,126✔
232
  (void)taosThreadMutexLock(&pMgmt->lock);
118,126✔
233
  if (pMgmt->transId == 0) {
118,126✔
234
    goto _OUT;
20,996✔
235
  }
236

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

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

UNCOV
252
_OUT:
×
253
  (void)taosThreadMutexUnlock(&pMgmt->lock);
118,126✔
254
  return 0;
118,126✔
255
}
256

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

264
  pMsg->info.conn.applyIndex = pMeta->index;
118,126✔
265
  pMsg->info.conn.applyTerm = pMeta->term;
118,126✔
266
  pMeta->code = 0;
118,126✔
267

268
  atomic_store_64(&pMnode->applied, pMsg->info.conn.applyIndex);
118,126✔
269

270
  if (!syncUtilUserCommit(pMsg->msgType)) {
118,126✔
271
    goto _OUT;
1,992✔
272
  }
273

274
  code = mndProcessWriteMsg(pMnode, pMsg, pMeta);
116,134✔
275

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

395
  mndUpdateStreamExecInfoRole(pMnode, NODE_ROLE_FOLLOWER);
324✔
396
}
324✔
397

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

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

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

421
  mndUpdateStreamExecInfoRole(pMnode, NODE_ROLE_LEADER);
1,536✔
422
  mndStreamResetInitTaskListLoadFlag();
1,536✔
423
}
1,536✔
424

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

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

436
static int32_t mndApplyQueueItems(const SSyncFSM *pFsm) {
22,229✔
437
  SMnode *pMnode = pFsm->data;
22,229✔
438

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

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

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

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

498
  snprintf(syncInfo.path, sizeof(syncInfo.path), "%s%ssync", pMnode->path, TD_DIRSEP);
1,767✔
499
  syncInfo.pFsm = mndSyncMakeFsm(pMnode);
1,767✔
500

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

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

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

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

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

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

575
int32_t mndSyncPropose(SMnode *pMnode, SSdbRaw *pRaw, int32_t transId) {
97,142✔
576
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
97,142✔
577

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

581
  req.pCont = rpcMallocCont(req.contLen);
97,142✔
582
  if (req.pCont == NULL) return terrno;
97,142!
583
  memcpy(req.pCont, pRaw, req.contLen);
97,142✔
584

585
  (void)taosThreadMutexLock(&pMgmt->lock);
97,142✔
586
  pMgmt->errCode = 0;
97,142✔
587

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

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

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

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

634
  terrno = pMgmt->errCode;
97,140✔
635
  return terrno;
97,140✔
636
}
637

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

648
void mndSyncStop(SMnode *pMnode) {
1,766✔
649
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
1,766✔
650

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

664
bool mndIsLeader(SMnode *pMnode) {
1,007,747✔
665
  terrno = 0;
1,007,747✔
666
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
1,007,853✔
667

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

673
  if (state.state != TAOS_SYNC_STATE_LEADER) {
1,007,865✔
674
    terrno = TSDB_CODE_SYN_NOT_LEADER;
44,886✔
675
    mDebug("vgId:1, mnode not leader, state:%s", syncStr(state.state));
44,886✔
676
    return false;
44,886✔
677
  }
678

679
  if (!state.restored || !pMnode->restored) {
962,979✔
680
    terrno = TSDB_CODE_SYN_RESTORING;
24,678✔
681
    mDebug("vgId:1, mnode not restored:%d:%d", state.restored, pMnode->restored);
24,667✔
682
    return false;
24,667✔
683
  }
684

685
  return true;
938,301✔
686
}
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