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

taosdata / TDengine / #5039

29 Apr 2026 11:44AM UTC coverage: 73.153% (+0.002%) from 73.151%
#5039

push

travis-ci

web-flow
feat(statewindow): support multi columns (#35136)

1566 of 1828 new or added lines in 18 files covered. (85.67%)

7270 existing lines in 141 files now uncovered.

277497 of 379338 relevant lines covered (73.15%)

133293066.15 hits per line

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

70.33
/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
#include "mndXnode.h"
23
#include "mndToken.h"
24

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

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

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

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

48
static int32_t mndSyncEqMsg(const SMsgCb *msgcb, SRpcMsg *pMsg) {
52,901,368✔
49
  if (pMsg == NULL || pMsg->pCont == NULL) {
52,901,368✔
50
    return -1;
×
51
  }
52

53
  SMsgHead *pHead = pMsg->pCont;
52,901,368✔
54
  pHead->contLen = htonl(pHead->contLen);
52,901,368✔
55
  pHead->vgId = htonl(pHead->vgId);
52,901,368✔
56

57
  if (msgcb == NULL || msgcb->putToQueueFp == NULL) {
52,901,368✔
58
    rpcFreeCont(pMsg->pCont);
×
59
    pMsg->pCont = NULL;
×
60
    return -1;
×
61
  }
62

63
  int32_t code = tmsgPutToQueue(msgcb, SYNC_QUEUE, pMsg);
52,901,368✔
64
  if (code != 0) {
52,901,368✔
65
    rpcFreeCont(pMsg->pCont);
1,272✔
66
    pMsg->pCont = NULL;
1,272✔
67
  }
68
  return code;
52,901,368✔
69
}
70

71
static int32_t mndSyncSendMsg(const SEpSet *pEpSet, SRpcMsg *pMsg) {
13,261,338✔
72
  int32_t code = tmsgSendSyncReq(pEpSet, pMsg);
13,261,338✔
73
  // if (code != 0) {
74
  //   rpcFreeCont(pMsg->pCont);
75
  //   pMsg->pCont = NULL;
76
  // }
77
  return code;
13,261,338✔
78
}
79

80
static int32_t mndTransValidatePrepareAction(SMnode *pMnode, STrans *pTrans, STransAction *pAction) {
11,822,322✔
81
  SSdbRaw *pRaw = pAction->pRaw;
11,822,322✔
82
  SSdb    *pSdb = pMnode->pSdb;
11,822,322✔
83
  int      code = 0;
11,822,322✔
84

85
  if (pRaw->status != SDB_STATUS_CREATING) goto _OUT;
11,822,322✔
86

87
  SdbValidateFp validateFp = pSdb->validateFps[pRaw->type];
4,611,634✔
88
  if (validateFp) {
4,611,634✔
89
    code = validateFp(pMnode, pTrans, pRaw);
4,580,424✔
90
  }
91

92
_OUT:
31,210✔
93
  return code;
11,822,322✔
94
}
95

96
static int32_t mndTransValidatePrepareStage(SMnode *pMnode, STrans *pTrans) {
21,883,530✔
97
  int32_t code = -1;
21,883,530✔
98
  int32_t action = 0;
21,883,530✔
99

100
  int32_t numOfActions = taosArrayGetSize(pTrans->prepareActions);
21,883,530✔
101
  if (numOfActions == 0) {
21,883,530✔
102
    code = 0;
13,224,027✔
103
    goto _OUT;
13,224,027✔
104
  }
105

106
  mInfo("trans:%d, validate %d prepare actions.", pTrans->id, numOfActions);
8,659,503✔
107

108
  for (action = 0; action < numOfActions; ++action) {
20,481,825✔
109
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, action);
11,822,322✔
110

111
    if (pAction->actionType != TRANS_ACTION_RAW) {
11,822,322✔
112
      mError("trans:%d, prepare action:%d of unexpected type:%d", pTrans->id, action, pAction->actionType);
×
113
      goto _OUT;
×
114
    }
115

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

123
  code = 0;
8,659,503✔
124
_OUT:
21,883,530✔
125
  return code;
21,883,530✔
126
}
127

128
static int32_t mndTransValidateImp(SMnode *pMnode, STrans *pTrans) {
46,665,360✔
129
  int32_t code = 0;
46,665,360✔
130
  if (pTrans->stage == TRN_STAGE_PREPARE) {
46,665,360✔
131
    if ((code = mndTransCheckConflict(pMnode, pTrans)) < 0) {
21,883,530✔
UNCOV
132
      mError("trans:%d, failed to validate trans conflicts.", pTrans->id);
×
UNCOV
133
      TAOS_RETURN(code);
×
134
    }
135

136
    return mndTransValidatePrepareStage(pMnode, pTrans);
21,883,530✔
137
  }
138
  TAOS_RETURN(code);
24,781,830✔
139
}
140

141
static int32_t mndTransValidate(SMnode *pMnode, SSdbRaw *pRaw) {
46,665,360✔
142
  STrans *pTrans = NULL;
46,665,360✔
143
  int32_t code = -1;
46,665,360✔
144

145
  SSdbRow *pRow = mndTransDecode(pRaw);
46,665,360✔
146
  if (pRow == NULL) {
46,665,360✔
147
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
148
    if (terrno != 0) code = terrno;
×
149
    goto _OUT;
×
150
  }
151

152
  pTrans = sdbGetRowObj(pRow);
46,665,360✔
153
  if (pTrans == NULL) {
46,665,360✔
154
    code = TSDB_CODE_MND_RETURN_VALUE_NULL;
×
155
    if (terrno != 0) code = terrno;
×
156
    goto _OUT;
×
157
  }
158

159
  code = mndTransValidateImp(pMnode, pTrans);
46,665,360✔
160

161
_OUT:
46,665,360✔
162
  if (pTrans) mndTransDropData(pTrans);
46,665,360✔
163
  if (pRow) taosMemoryFreeClear(pRow);
46,665,360✔
164
  if (code) terrno = (terrno ? terrno : TSDB_CODE_MND_TRANS_CONFLICT);
46,665,360✔
165
  TAOS_RETURN(code);
46,665,360✔
166
}
167

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

175
  if (transId <= 0) {
46,665,360✔
176
    mError("trans:%d, invalid commit msg, cache transId:%d seq:%" PRId64, transId, pMgmt->transId, pMgmt->transSeq);
×
177
    code = TSDB_CODE_INVALID_MSG;
×
178
    goto _OUT;
×
179
  }
180

181
  mInfo("trans:%d, process sync proposal, saved:%d code:0x%x, apply index:%" PRId64 " term:%" PRIu64 " config:%" PRId64
46,665,360✔
182
        " role:%s raw:%p sec:%d seq:%" PRId64,
183
        transId, pMgmt->transId, pMeta->code, pMeta->index, pMeta->term, pMeta->lastConfigIndex, syncStr(pMeta->state),
184
        pRaw, pMgmt->transSec, pMgmt->transSeq);
185

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

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

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

213
  if (pTrans->stage == TRN_STAGE_PREPARE) {
46,665,360✔
214
    bool continueExec = mndTransPerformPrepareStage(pMnode, pTrans, false);
21,883,530✔
215
    if (!continueExec) {
21,883,530✔
216
      if (terrno != 0) code = terrno;
×
217
      goto _OUT;
×
218
    }
219
  }
220

221
  mInfo("trans:%d, refresh transaction in process write msg", transId);
46,665,360✔
222
  mndTransRefresh(pMnode, pTrans);
46,665,360✔
223
  mInfo("trans:%d, refresh transaction in process write msg finished", transId);
46,665,360✔
224

225
  sdbSetApplyInfo(pMnode->pSdb, pMeta->index, pMeta->term, pMeta->lastConfigIndex);
46,665,360✔
226
  code = sdbWriteFile(pMnode->pSdb, tsMndSdbWriteDelta);
46,665,360✔
227

228
_OUT:
46,665,360✔
229
  if (pTrans) mndReleaseTrans(pMnode, pTrans);
46,665,360✔
230
  TAOS_RETURN(code);
46,665,360✔
231
}
232

233
static int32_t mndPostMgmtCode(SMnode *pMnode, int32_t code) {
47,185,127✔
234
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
47,185,127✔
235
  (void)taosThreadMutexLock(&pMgmt->lock);
47,185,127✔
236
  if (pMgmt->transId == 0) {
47,185,127✔
237
    goto _OUT;
3,818,250✔
238
  }
239

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

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

255
_OUT:
×
256
  (void)taosThreadMutexUnlock(&pMgmt->lock);
47,185,127✔
257
  return 0;
47,185,127✔
258
}
259

260
int32_t mndSyncCommitMsg(const SSyncFSM *pFsm, SRpcMsg *pMsg, SFsmCbMeta *pMeta) {
47,185,127✔
261
  SMnode *pMnode = pFsm->data;
47,185,127✔
262
  int32_t code = pMsg->code;
47,185,127✔
263
  if (code != 0) {
47,185,127✔
264
    goto _OUT;
×
265
  }
266

267
  pMsg->info.conn.applyIndex = pMeta->index;
47,185,127✔
268
  pMsg->info.conn.applyTerm = pMeta->term;
47,185,127✔
269
  pMeta->code = 0;
47,185,127✔
270

271
  atomic_store_64(&pMnode->applied, pMsg->info.conn.applyIndex);
47,185,127✔
272

273
  if (!syncUtilUserCommit(pMsg->msgType)) {
47,185,127✔
274
    goto _OUT;
519,767✔
275
  }
276

277
  code = mndProcessWriteMsg(pMnode, pMsg, pMeta);
46,665,360✔
278

279
_OUT:
47,185,127✔
280
  mndPostMgmtCode(pMnode, code ? code : pMeta->code);
47,185,127✔
281
  rpcFreeCont(pMsg->pCont);
47,185,127✔
282
  pMsg->pCont = NULL;
47,185,127✔
283
  TAOS_RETURN(code);
47,185,127✔
284
}
285

286
SyncIndex mndSyncAppliedIndex(const SSyncFSM *pFSM) {
50,496,928✔
287
  SMnode *pMnode = pFSM->data;
50,496,928✔
288
  return atomic_load_64(&pMnode->applied);
50,496,928✔
289
}
290

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

299
static int32_t mndSyncGetSnapshotInfo(const SSyncFSM *pFsm, SSnapshot *pSnapshot) {
12,556,462✔
300
  SMnode *pMnode = pFsm->data;
12,556,462✔
301
  sdbGetCommitInfo(pMnode->pSdb, &pSnapshot->lastApplyIndex, &pSnapshot->lastApplyTerm, &pSnapshot->lastConfigIndex);
12,556,462✔
302
  return 0;
12,556,462✔
303
}
304

305
void mndRestoreFinish(const SSyncFSM *pFsm, const SyncIndex commitIdx) {
581,927✔
306
  SMnode *pMnode = pFsm->data;
581,927✔
307

308
  if (!pMnode->deploy) {
581,927✔
309
    if (!pMnode->restored) {
226,585✔
310
      mInfo("vgId:1, sync restore finished, and will handle outstanding transactions");
134,798✔
311
      mndTransPullup(pMnode);
134,798✔
312
      mndSetRestored(pMnode, true);
134,798✔
313
    } else {
314
      mInfo("vgId:1, sync restore finished, repeat call");
91,787✔
315
    }
316
  } else {
317
    mInfo("vgId:1, sync restore finished");
355,342✔
318
  }
319
  int32_t code = mndRefreshUserIpWhiteList(pMnode);
581,927✔
320
  if (code != 0) {
581,927✔
321
    mError("vgId:1, failed to refresh user ip white list since %s", tstrerror(code));
×
322
    mndSetRestored(pMnode, false);
×
323
  }
324

325
  code = mndRefreshUserDateTimeWhiteList(pMnode);
581,927✔
326
  if (code != 0) {
581,927✔
327
    mError("vgId:1, failed to refresh user date time white list since %s", tstrerror(code));
×
328
    mndSetRestored(pMnode, false);
×
329
  }
330

331
  code = mndTokenCacheRebuild(pMnode);
581,927✔
332
  if (code != 0) {
581,927✔
333
    mError("vgId:1, failed to rebuild token cache since %s", tstrerror(code));
×
334
    mndSetRestored(pMnode, false);
×
335
  }
336

337
  SyncIndex fsmIndex = mndSyncAppliedIndex(pFsm);
581,927✔
338
  if (commitIdx != fsmIndex) {
581,927✔
339
    mError("vgId:1, failed to sync restore, commitIdx:%" PRId64 " is not equal to appliedIdx:%" PRId64, commitIdx,
×
340
           fsmIndex);
341
    mndSetRestored(pMnode, false);
×
342
  }
343
}
581,927✔
344

345
void mndAfterRestored(const SSyncFSM *pFsm, const SyncIndex commitIdx) {
581,927✔
346
  SMnode *pMnode = pFsm->data;
581,927✔
347

348
  if (!pMnode->deploy) {
581,927✔
349
    if (sdbAfterRestored(pMnode->pSdb) != 0) {
226,585✔
350
      mError("failed to prepare sdb while start mnode");
×
351
    }
352
    mInfo("vgId:1, sync restore finished and restore sdb success");
226,585✔
353
  }
354
}
581,927✔
355

356
int32_t mndSnapshotStartRead(const SSyncFSM *pFsm, void *pParam, void **ppReader) {
×
357
  mInfo("start to read snapshot from sdb");
×
358
  SMnode *pMnode = pFsm->data;
×
359
  return sdbStartRead(pMnode->pSdb, (SSdbIter **)ppReader, NULL, NULL, NULL);
×
360
}
361

362
static void mndSnapshotStopRead(const SSyncFSM *pFsm, void *pReader) {
×
363
  mInfo("stop to read snapshot from sdb");
×
364
  SMnode *pMnode = pFsm->data;
×
365
  sdbStopRead(pMnode->pSdb, pReader);
×
366
}
×
367

368
int32_t mndSnapshotDoRead(const SSyncFSM *pFsm, void *pReader, void **ppBuf, int32_t *len) {
×
369
  SMnode *pMnode = pFsm->data;
×
370
  return sdbDoRead(pMnode->pSdb, pReader, ppBuf, len);
×
371
}
372

373
int32_t mndSnapshotStartWrite(const SSyncFSM *pFsm, void *pParam, void **ppWriter) {
×
374
  mInfo("start to apply snapshot to sdb");
×
375
  SMnode *pMnode = pFsm->data;
×
376
  return sdbStartWrite(pMnode->pSdb, (SSdbIter **)ppWriter);
×
377
}
378

379
int32_t mndSnapshotStopWrite(const SSyncFSM *pFsm, void *pWriter, bool isApply, SSnapshot *pSnapshot) {
×
380
  mInfo("stop to apply snapshot to sdb, apply:%d, index:%" PRId64 " term:%" PRIu64 " config:%" PRId64, isApply,
×
381
        pSnapshot->lastApplyIndex, pSnapshot->lastApplyTerm, pSnapshot->lastConfigIndex);
382
  SMnode *pMnode = pFsm->data;
×
383
  return sdbStopWrite(pMnode->pSdb, pWriter, isApply, pSnapshot->lastApplyIndex, pSnapshot->lastApplyTerm,
×
384
                      pSnapshot->lastConfigIndex);
385
}
386

387
int32_t mndSnapshotDoWrite(const SSyncFSM *pFsm, void *pWriter, void *pBuf, int32_t len) {
×
388
  SMnode *pMnode = pFsm->data;
×
389
  return sdbDoWrite(pMnode->pSdb, pWriter, pBuf, len);
×
390
}
391

392
static void mndBecomeFollower(const SSyncFSM *pFsm) {
47,274✔
393
  SMnode    *pMnode = pFsm->data;
47,274✔
394
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
47,274✔
395
  mInfo("vgId:1, becomefollower callback");
47,274✔
396

397
  (void)taosThreadMutexLock(&pMgmt->lock);
47,274✔
398
  if (pMgmt->transId != 0) {
47,274✔
399
    mInfo("vgId:1, become follower and post sem, trans:%d, failed to propose since not leader", pMgmt->transId);
×
400
    pMgmt->transId = 0;
×
401
    pMgmt->transSec = 0;
×
402
    pMgmt->transSeq = 0;
×
403
    pMgmt->errCode = TSDB_CODE_SYN_NOT_LEADER;
×
404
    if (tsem_post(&pMgmt->syncSem) < 0) {
×
405
      mError("failed to post sem");
×
406
    }
407
  }
408
  (void)taosThreadMutexUnlock(&pMgmt->lock);
47,274✔
409

410
  msmHandleBecomeNotLeader(pMnode);
47,274✔
411
  mndXnodeHandleBecomeNotLeader();
47,274✔
412
}
47,274✔
413

414
static void mndBecomeLearner(const SSyncFSM *pFsm) {
16,236✔
415
  SMnode    *pMnode = pFsm->data;
16,236✔
416
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
16,236✔
417
  mInfo("vgId:1, become learner");
16,236✔
418

419
  (void)taosThreadMutexLock(&pMgmt->lock);
16,236✔
420
  if (pMgmt->transId != 0) {
16,236✔
421
    mInfo("vgId:1, become learner and post sem, trans:%d, failed to propose since not leader", pMgmt->transId);
×
422
    pMgmt->transId = 0;
×
423
    pMgmt->transSec = 0;
×
424
    pMgmt->transSeq = 0;
×
425
    pMgmt->errCode = TSDB_CODE_SYN_NOT_LEADER;
×
426
    if (tsem_post(&pMgmt->syncSem) < 0) {
×
427
      mError("failed to post sem");
×
428
    }
429
  }
430
  (void)taosThreadMutexUnlock(&pMgmt->lock);
16,236✔
431

432
  msmHandleBecomeNotLeader(pMnode);
16,236✔
433
  mndXnodeHandleBecomeNotLeader();
16,236✔
434
}
16,236✔
435

436
static void mndBecomeLeader(const SSyncFSM *pFsm) {
454,017✔
437
  mInfo("vgId:1, becomeleader callback");
454,017✔
438
  SMnode *pMnode = pFsm->data;
454,017✔
439

440
  msmHandleBecomeLeader(pMnode);
454,017✔
441
  mndXnodeHandleBecomeLeader(pMnode);
454,017✔
442
}
454,017✔
443

444
static bool mndApplyQueueEmpty(const SSyncFSM *pFsm) {
×
445
  SMnode *pMnode = pFsm->data;
×
446

447
  if (pMnode != NULL && pMnode->msgCb.qsizeFp != NULL) {
×
448
    int32_t itemSize = tmsgGetQueueSize(&pMnode->msgCb, 1, APPLY_QUEUE);
×
449
    return (itemSize == 0);
×
450
  } else {
451
    return true;
×
452
  }
453
}
454

455
static int32_t mndApplyQueueItems(const SSyncFSM *pFsm) {
1,852,320✔
456
  SMnode *pMnode = pFsm->data;
1,852,320✔
457

458
  if (pMnode != NULL && pMnode->msgCb.qsizeFp != NULL) {
1,852,320✔
459
    int32_t itemSize = tmsgGetQueueSize(&pMnode->msgCb, 1, APPLY_QUEUE);
×
460
    return itemSize;
×
461
  } else {
462
    return -1;
1,852,320✔
463
  }
464
}
465

466
SSyncFSM *mndSyncMakeFsm(SMnode *pMnode) {
490,275✔
467
  SSyncFSM *pFsm = taosMemoryCalloc(1, sizeof(SSyncFSM));
490,275✔
468
  pFsm->data = pMnode;
490,275✔
469
  pFsm->FpCommitCb = mndSyncCommitMsg;
490,275✔
470
  pFsm->FpAppliedIndexCb = mndSyncAppliedIndex;
490,275✔
471
  pFsm->FpPreCommitCb = NULL;
490,275✔
472
  pFsm->FpRollBackCb = NULL;
490,275✔
473
  pFsm->FpRestoreFinishCb = mndRestoreFinish;
490,275✔
474
  pFsm->FpAfterRestoredCb = mndAfterRestored;
490,275✔
475
  pFsm->FpLeaderTransferCb = NULL;
490,275✔
476
  pFsm->FpApplyQueueEmptyCb = mndApplyQueueEmpty;
490,275✔
477
  pFsm->FpApplyQueueItems = mndApplyQueueItems;
490,275✔
478
  pFsm->FpReConfigCb = NULL;
490,275✔
479
  pFsm->FpBecomeLeaderCb = mndBecomeLeader;
490,275✔
480
  pFsm->FpBecomeAssignedLeaderCb = NULL;
490,275✔
481
  pFsm->FpBecomeFollowerCb = mndBecomeFollower;
490,275✔
482
  pFsm->FpBecomeLearnerCb = mndBecomeLearner;
490,275✔
483
  pFsm->FpGetSnapshot = mndSyncGetSnapshot;
490,275✔
484
  pFsm->FpGetSnapshotInfo = mndSyncGetSnapshotInfo;
490,275✔
485
  pFsm->FpSnapshotStartRead = mndSnapshotStartRead;
490,275✔
486
  pFsm->FpSnapshotStopRead = mndSnapshotStopRead;
490,275✔
487
  pFsm->FpSnapshotDoRead = mndSnapshotDoRead;
490,275✔
488
  pFsm->FpSnapshotStartWrite = mndSnapshotStartWrite;
490,275✔
489
  pFsm->FpSnapshotStopWrite = mndSnapshotStopWrite;
490,275✔
490
  pFsm->FpSnapshotDoWrite = mndSnapshotDoWrite;
490,275✔
491
  return pFsm;
490,275✔
492
}
493

494
int32_t mndInitSync(SMnode *pMnode) {
490,275✔
495
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
490,275✔
496
  (void)taosThreadMutexInit(&pMgmt->lock, NULL);
490,275✔
497
  (void)taosThreadMutexLock(&pMgmt->lock);
490,275✔
498
  pMgmt->transId = 0;
490,275✔
499
  pMgmt->transSec = 0;
490,275✔
500
  pMgmt->transSeq = 0;
490,275✔
501
  (void)taosThreadMutexUnlock(&pMgmt->lock);
490,275✔
502

503
  SSyncInfo syncInfo = {
490,275✔
504
      .snapshotStrategy = SYNC_STRATEGY_STANDARD_SNAPSHOT,
505
      .batchSize = 1,
506
      .vgId = 1,
507
      .pWal = pMnode->pWal,
490,275✔
508
      .msgcb = &pMnode->msgCb,
490,275✔
509
      .syncSendMSg = mndSyncSendMsg,
510
      .syncEqMsg = mndSyncEqMsg,
511
      .syncEqCtrlMsg = mndSyncEqCtrlMsg,
512
      .pingMs = 5000,
513
      .electMs = tsMnodeElectIntervalMs,
514
      .heartbeatMs = tsMnodeHeartbeatIntervalMs,
515
  };
516

517
  snprintf(syncInfo.path, sizeof(syncInfo.path), "%s%ssync", pMnode->path, TD_DIRSEP);
490,275✔
518
  syncInfo.pFsm = mndSyncMakeFsm(pMnode);
490,275✔
519

520
  SSyncCfg *pCfg = &syncInfo.syncCfg;
490,275✔
521
  mInfo("vgId:1, start to open mnode sync, in syncMgmt, numOfTotalReplicas:%d selfIndex:%d, electMs:%d, heartbeatMs:%d",
490,275✔
522
        pMgmt->numOfTotalReplicas, pMgmt->selfIndex, syncInfo.electMs, syncInfo.heartbeatMs);
523
  pCfg->totalReplicaNum = pMgmt->numOfTotalReplicas;
490,275✔
524
  pCfg->replicaNum = pMgmt->numOfReplicas;
490,275✔
525
  pCfg->myIndex = pMgmt->selfIndex;
490,275✔
526
  pCfg->lastIndex = pMgmt->lastIndex;
490,275✔
527
  for (int32_t i = 0; i < pMgmt->numOfTotalReplicas; ++i) {
926,285✔
528
    SNodeInfo *pNode = &pCfg->nodeInfo[i];
436,010✔
529
    pNode->nodeId = pMgmt->replicas[i].id;
436,010✔
530
    pNode->nodePort = pMgmt->replicas[i].port;
436,010✔
531
    tstrncpy(pNode->nodeFqdn, pMgmt->replicas[i].fqdn, sizeof(pNode->nodeFqdn));
436,010✔
532
    pNode->nodeRole = pMgmt->nodeRoles[i];
436,010✔
533
    bool update = tmsgUpdateDnodeInfo(&pNode->nodeId, &pNode->clusterId, pNode->nodeFqdn, &pNode->nodePort);
436,010✔
534
    mInfo("vgId:1, index:%d ep:%s:%u dnode:%d cluster:%" PRId64 ", update:%d", i, pNode->nodeFqdn, pNode->nodePort,
436,010✔
535
          pNode->nodeId, pNode->clusterId, update);
536
  }
537

538
  int32_t code = 0;
490,275✔
539
  if ((code = tsem_init(&pMgmt->syncSem, 0, 0)) < 0) {
490,275✔
540
    mError("failed to open sync, tsem_init, since %s", tstrerror(code));
×
541
    TAOS_RETURN(code);
×
542
  }
543
  pMgmt->sync = syncOpen(&syncInfo, 1);  // always check
490,275✔
544
  if (pMgmt->sync <= 0) {
490,275✔
545
    if (terrno != 0) code = terrno;
×
546
    mError("failed to open sync since %s", tstrerror(code));
×
547
    TAOS_RETURN(code);
×
548
  }
549
  pMnode->pSdb->sync = pMgmt->sync;
490,275✔
550

551
  mInfo("vgId:1, mnode sync is opened, id:%" PRId64, pMgmt->sync);
490,275✔
552
  TAOS_RETURN(code);
490,275✔
553
}
554

555
void mndCleanupSync(SMnode *pMnode) {
490,213✔
556
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
490,213✔
557
  syncStop(pMgmt->sync);
490,213✔
558
  mInfo("mnode-sync is stopped, id:%" PRId64, pMgmt->sync);
490,213✔
559

560
  if (tsem_destroy(&pMgmt->syncSem) < 0) {
490,213✔
561
    mError("failed to destroy sem");
×
562
  }
563
  (void)taosThreadMutexDestroy(&pMgmt->lock);
490,213✔
564
  memset(pMgmt, 0, sizeof(SSyncMgmt));
490,213✔
565
}
490,213✔
566

567
void mndSyncCheckTimeout(SMnode *pMnode) {
1,195,659✔
568
  mTrace("check sync timeout");
1,195,659✔
569
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
1,195,659✔
570
  (void)taosThreadMutexLock(&pMgmt->lock);
1,195,659✔
571
  if (pMgmt->transId != 0) {
1,195,659✔
572
    int32_t curSec = taosGetTimestampSec();
35,577✔
573
    int32_t delta = curSec - pMgmt->transSec;
35,577✔
574
    if (delta > MNODE_TIMEOUT_SEC) {
35,577✔
575
      mError("trans:%d, failed to propose since timeout, start:%d cur:%d delta:%d seq:%" PRId64, pMgmt->transId,
×
576
             pMgmt->transSec, curSec, delta, pMgmt->transSeq);
577
      // pMgmt->transId = 0;
578
      // pMgmt->transSec = 0;
579
      // pMgmt->transSeq = 0;
580
      // terrno = TSDB_CODE_SYN_TIMEOUT;
581
      // pMgmt->errCode = TSDB_CODE_SYN_TIMEOUT;
582
      // if (tsem_post(&pMgmt->syncSem) < 0) {
583
      //  mError("failed to post sem");
584
      //}
585
    } else {
586
      mDebug("trans:%d, waiting for sync confirm, start:%d cur:%d delta:%d seq:%" PRId64, pMgmt->transId,
35,577✔
587
             pMgmt->transSec, curSec, curSec - pMgmt->transSec, pMgmt->transSeq);
588
    }
589
  } else {
590
    // mTrace("check sync timeout msg, no trans waiting for confirm");
591
  }
592
  (void)taosThreadMutexUnlock(&pMgmt->lock);
1,195,659✔
593
}
1,195,659✔
594

595
int32_t mndSyncPropose(SMnode *pMnode, SSdbRaw *pRaw, int32_t transId) {
43,370,906✔
596
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
43,370,906✔
597

598
  SRpcMsg req = {.msgType = TDMT_MND_APPLY_MSG, .contLen = sdbGetRawTotalSize(pRaw)};
43,370,906✔
599
  if (req.contLen <= 0) return terrno;
43,370,906✔
600

601
  req.pCont = rpcMallocCont(req.contLen);
43,370,906✔
602
  if (req.pCont == NULL) return terrno;
43,370,906✔
603
  memcpy(req.pCont, pRaw, req.contLen);
43,370,906✔
604

605
  (void)taosThreadMutexLock(&pMgmt->lock);
43,370,906✔
606
  pMgmt->errCode = 0;
43,370,906✔
607

608
  if (pMgmt->transId != 0) {
43,370,906✔
609
    mError("trans:%d, can't be proposed since trans:%d already waiting for confirm", transId, pMgmt->transId);
×
610
    (void)taosThreadMutexUnlock(&pMgmt->lock);
×
611
    rpcFreeCont(req.pCont);
×
612
    TAOS_RETURN(TSDB_CODE_MND_LAST_TRANS_NOT_FINISHED);
×
613
  }
614

615
  mInfo("trans:%d, will be proposed", transId);
43,370,906✔
616
  pMgmt->transId = transId;
43,370,906✔
617
  pMgmt->transSec = taosGetTimestampSec();
43,370,906✔
618

619
  int64_t seq = 0;
43,370,906✔
620
  int32_t code = syncPropose(pMgmt->sync, &req, false, &seq);
43,370,906✔
621
  if (code == 0) {
43,370,906✔
622
    mInfo("trans:%d, is proposing and wait sem, seq:%" PRId64, transId, seq);
43,369,634✔
623
    pMgmt->transSeq = seq;
43,369,634✔
624
    (void)taosThreadMutexUnlock(&pMgmt->lock);
43,369,634✔
625
    code = tsem_wait(&pMgmt->syncSem);
43,369,634✔
626
  } else if (code > 0) {
1,272✔
627
    mInfo("trans:%d, confirm at once since replica is 1, continue execute", transId);
×
628
    pMgmt->transId = 0;
×
629
    pMgmt->transSec = 0;
×
630
    pMgmt->transSeq = 0;
×
631
    (void)taosThreadMutexUnlock(&pMgmt->lock);
×
632
    code = sdbWriteWithoutFree(pMnode->pSdb, pRaw);
×
633
    if (code == 0) {
×
634
      sdbSetApplyInfo(pMnode->pSdb, req.info.conn.applyIndex, req.info.conn.applyTerm, SYNC_INDEX_INVALID);
×
635
    }
636
  } else {
637
    mError("trans:%d, failed to proposed since %s", transId, terrstr());
1,272✔
638
    pMgmt->transId = 0;
1,272✔
639
    pMgmt->transSec = 0;
1,272✔
640
    pMgmt->transSeq = 0;
1,272✔
641
    (void)taosThreadMutexUnlock(&pMgmt->lock);
1,272✔
642
    if (terrno == 0) {
1,272✔
643
      terrno = TSDB_CODE_APP_ERROR;
×
644
    }
645
  }
646

647
  rpcFreeCont(req.pCont);
43,370,906✔
648
  req.pCont = NULL;
43,370,906✔
649
  if (code != 0) {
43,370,906✔
650
    mError("trans:%d, failed to propose, code:0x%x", pMgmt->transId, code);
1,272✔
651
    return code;
1,272✔
652
  }
653

654
  terrno = pMgmt->errCode;
43,369,634✔
655
  return terrno;
43,369,634✔
656
}
657

658
void mndSyncStart(SMnode *pMnode) {
490,140✔
659
  mInfo("vgId:1, start to start mnode sync");
490,140✔
660
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
490,140✔
661
  if (syncStart(pMgmt->sync) < 0) {
490,140✔
662
    mError("vgId:1, failed to start sync, id:%" PRId64, pMgmt->sync);
×
663
    return;
×
664
  }
665
  mInfo("vgId:1, mnode sync started, id:%" PRId64, pMgmt->sync);
490,140✔
666
}
667

668
void mndSyncStop(SMnode *pMnode) {
490,140✔
669
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
490,140✔
670

671
  (void)taosThreadMutexLock(&pMgmt->lock);
490,140✔
672
  if (pMgmt->transId != 0) {
490,140✔
673
    mInfo("vgId:1, trans:%d, is stopped and post sem", pMgmt->transId);
2,757✔
674
    pMgmt->transId = 0;
2,757✔
675
    pMgmt->transSec = 0;
2,757✔
676
    pMgmt->errCode = TSDB_CODE_APP_IS_STOPPING;
2,757✔
677
    if (tsem_post(&pMgmt->syncSem) < 0) {
2,757✔
678
      mError("failed to post sem");
×
679
    }
680
  }
681
  (void)taosThreadMutexUnlock(&pMgmt->lock);
490,140✔
682
}
490,140✔
683

684
bool mndIsLeader(SMnode *pMnode) {
152,644,229✔
685
  terrno = 0;
152,644,229✔
686
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
152,644,229✔
687

688
  if (terrno != 0) {
152,644,229✔
689
    mDebug("vgId:1, mnode is stopping");
×
690
    return false;
×
691
  }
692

693
  if (state.state != TAOS_SYNC_STATE_LEADER) {
152,644,229✔
694
    terrno = TSDB_CODE_SYN_NOT_LEADER;
3,164,643✔
695
    mDebug("vgId:1, mnode not leader, state:%s", syncStr(state.state));
3,164,643✔
696
    return false;
3,164,643✔
697
  }
698

699
  if (!state.restored || !pMnode->restored) {
149,479,586✔
700
    terrno = TSDB_CODE_SYN_RESTORING;
13,908,463✔
701
    mDebug("vgId:1, mnode not restored:%d:%d", state.restored, pMnode->restored);
13,908,463✔
702
    return false;
13,908,463✔
703
  }
704

705
  return true;
135,571,123✔
706
}
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