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

taosdata / TDengine / #4142

20 May 2025 07:22AM UTC coverage: 62.238% (-0.9%) from 63.096%
#4142

push

travis-ci

web-flow
docs(datain): add topic meta options docs in tmq (#31147)

155113 of 318088 branches covered (48.76%)

Branch coverage included in aggregate %.

240242 of 317147 relevant lines covered (75.75%)

13602566.83 hits per line

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

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

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

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

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

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

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

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

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

90
_OUT:
464✔
91
  return code;
27,359✔
92
}
93

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

98
  int32_t numOfActions = taosArrayGetSize(pTrans->prepareActions);
56,758✔
99
  if (numOfActions == 0) {
56,758✔
100
    code = 0;
39,972✔
101
    goto _OUT;
39,972✔
102
  }
103

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

106
  for (action = 0; action < numOfActions; ++action) {
44,145✔
107
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, action);
27,359✔
108

109
    if (pAction->actionType != TRANS_ACTION_RAW) {
27,359!
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);
27,359✔
115
    if (code != 0) {
27,359!
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,786✔
122
_OUT:
56,758✔
123
  return code;
56,758✔
124
}
125

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

134
    return mndTransValidatePrepareStage(pMnode, pTrans);
56,758✔
135
  }
136
  TAOS_RETURN(code);
65,286✔
137
}
138

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

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

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

157
  code = mndTransValidateImp(pMnode, pTrans);
122,044✔
158

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

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

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

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

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

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

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

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

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

246
  if (pMgmt->errCode != 0) {
102,486!
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);
102,486!
250
  }
251

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

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

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

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

270
  if (!syncUtilUserCommit(pMsg->msgType)) {
124,395✔
271
    goto _OUT;
2,351✔
272
  }
273

274
  code = mndProcessWriteMsg(pMnode, pMsg, pMeta);
122,044✔
275

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

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

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) {
78,156✔
297
  SMnode *pMnode = pFsm->data;
78,156✔
298
  sdbGetCommitInfo(pMnode->pSdb, &pSnapshot->lastApplyIndex, &pSnapshot->lastApplyTerm, &pSnapshot->lastConfigIndex);
78,156✔
299
  return 0;
78,156✔
300
}
301

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

305
  if (!pMnode->deploy) {
2,410✔
306
    if (!pMnode->restored) {
834✔
307
      mInfo("vgId:1, sync restore finished, and will handle outstanding transactions");
552!
308
      mndTransPullup(pMnode);
552✔
309
      mndSetRestored(pMnode, true);
552✔
310
    } else {
311
      mInfo("vgId:1, sync restore finished, repeat call");
282!
312
    }
313
  } else {
314
    mInfo("vgId:1, sync restore finished");
1,576!
315
  }
316
  int32_t code = mndRefreshUserIpWhiteList(pMnode);
2,410✔
317
  if (code != 0) {
2,410!
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,410✔
323
  if (commitIdx != fsmIndex) {
2,410!
324
    mError("vgId:1, failed to sync restore, commitIdx:%" PRId64 " is not equal to appliedIdx:%" PRId64, commitIdx,
×
325
           fsmIndex);
326
    mndSetRestored(pMnode, false);
×
327
  }
328
}
2,410✔
329

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

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

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

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

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

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

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);
367
  SMnode *pMnode = pFsm->data;
×
368
  return sdbStopWrite(pMnode->pSdb, pWriter, isApply, pSnapshot->lastApplyIndex, pSnapshot->lastApplyTerm,
×
369
                      pSnapshot->lastConfigIndex);
370
}
371

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) {
352✔
378
  SMnode    *pMnode = pFsm->data;
352✔
379
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
352✔
380
  mInfo("vgId:1, become follower");
352!
381

382
  (void)taosThreadMutexLock(&pMgmt->lock);
352✔
383
  if (pMgmt->transId != 0) {
352!
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);
352✔
394

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

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

403
  (void)taosThreadMutexLock(&pMgmt->lock);
107✔
404
  if (pMgmt->transId != 0) {
107!
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);
107✔
415
}
107✔
416

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

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

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

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

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

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

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

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

484
  SSyncInfo syncInfo = {
2,130✔
485
      .snapshotStrategy = SYNC_STRATEGY_STANDARD_SNAPSHOT,
486
      .batchSize = 1,
487
      .vgId = 1,
488
      .pWal = pMnode->pWal,
2,130✔
489
      .msgcb = &pMnode->msgCb,
2,130✔
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);
2,130✔
499
  syncInfo.pFsm = mndSyncMakeFsm(pMnode);
2,130✔
500

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

518
  int32_t code = 0;
2,130✔
519
  if ((code = tsem_init(&pMgmt->syncSem, 0, 0)) < 0) {
2,130!
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
2,130✔
524
  if (pMgmt->sync <= 0) {
2,130!
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;
2,130✔
530

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

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

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

547
void mndSyncCheckTimeout(SMnode *pMnode) {
1,578✔
548
  mTrace("check sync timeout");
1,578✔
549
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
1,578✔
550
  (void)taosThreadMutexLock(&pMgmt->lock);
1,578✔
551
  if (pMgmt->transId != 0) {
1,578✔
552
    int32_t curSec = taosGetTimestampSec();
13✔
553
    int32_t delta = curSec - pMgmt->transSec;
13✔
554
    if (delta > MNODE_TIMEOUT_SEC) {
13!
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,
13✔
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);
1,578✔
573
}
1,578✔
574

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

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

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

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

588
  if (pMgmt->transId != 0) {
102,495!
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);
102,495!
596
  pMgmt->transId = transId;
102,495✔
597
  pMgmt->transSec = taosGetTimestampSec();
102,495✔
598

599
  int64_t seq = 0;
102,495✔
600
  int32_t code = syncPropose(pMgmt->sync, &req, false, &seq);
102,495✔
601
  if (code == 0) {
102,495✔
602
    mInfo("trans:%d, is proposing and wait sem, seq:%" PRId64, transId, seq);
102,494!
603
    pMgmt->transSeq = seq;
102,494✔
604
    (void)taosThreadMutexUnlock(&pMgmt->lock);
102,494✔
605
    code = tsem_wait(&pMgmt->syncSem);
102,494✔
606
  } else if (code > 0) {
1!
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());
1!
618
    pMgmt->transId = 0;
1✔
619
    pMgmt->transSec = 0;
1✔
620
    pMgmt->transSeq = 0;
1✔
621
    (void)taosThreadMutexUnlock(&pMgmt->lock);
1✔
622
    if (terrno == 0) {
1!
623
      terrno = TSDB_CODE_APP_ERROR;
×
624
    }
625
  }
626

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

634
  terrno = pMgmt->errCode;
102,494✔
635
  return terrno;
102,494✔
636
}
637

638
void mndSyncStart(SMnode *pMnode) {
2,129✔
639
  mInfo("vgId:1, start to start mnode sync");
2,129!
640
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
2,129✔
641
  if (syncStart(pMgmt->sync) < 0) {
2,129!
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);
2,129!
646
}
647

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

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

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

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

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

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

685
  return true;
386,087✔
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