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

taosdata / TDengine / #3561

19 Dec 2024 03:15AM UTC coverage: 58.812% (-1.3%) from 60.124%
#3561

push

travis-ci

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

merge: from main to 3.0 branch

130770 of 287658 branches covered (45.46%)

Branch coverage included in aggregate %.

32 of 78 new or added lines in 4 files covered. (41.03%)

7347 existing lines in 166 files now uncovered.

205356 of 283866 relevant lines covered (72.34%)

7187865.64 hits per line

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

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

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

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

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

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

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

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

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

90
_OUT:
11✔
91
  return code;
20,534✔
92
}
93

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

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

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

106
  for (action = 0; action < numOfActions; ++action) {
33,771✔
107
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, action);
20,534✔
108

109
    if (pAction->actionType != TRANS_ACTION_RAW) {
20,534!
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);
20,534✔
115
    if (code != 0) {
20,534!
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;
13,237✔
122
_OUT:
37,247✔
123
  return code;
37,247✔
124
}
125

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

134
    return mndTransValidatePrepareStage(pMnode, pTrans);
37,247✔
135
  }
136
  TAOS_RETURN(code);
42,734✔
137
}
138

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

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

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

157
  code = mndTransValidateImp(pMnode, pTrans);
79,981✔
158

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

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

173
  if (transId <= 0) {
79,981!
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
79,981!
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);
79,981✔
185
  if (code != 0) {
79,981!
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);
79,981✔
193
  code = sdbWriteWithoutFree(pMnode->pSdb, pRaw);
79,981✔
194
  if (code != 0) {
79,981!
UNCOV
195
    mError("trans:%d, failed to write to sdb since %s", transId, terrstr());
×
196
    // code = 0;
UNCOV
197
    (void)taosThreadMutexUnlock(&pMnode->pSdb->filelock);
×
UNCOV
198
    pMeta->code = code;
×
UNCOV
199
    goto _OUT;
×
200
  }
201
  (void)taosThreadMutexUnlock(&pMnode->pSdb->filelock);
79,981✔
202

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

219
  mndTransRefresh(pMnode, pTrans);
79,981✔
220

221
  sdbSetApplyInfo(pMnode->pSdb, pMeta->index, pMeta->term, pMeta->lastConfigIndex);
79,981✔
222
  code = sdbWriteFile(pMnode->pSdb, tsMndSdbWriteDelta);
79,981✔
223

224
_OUT:
79,981✔
225
  if (pTrans) mndReleaseTrans(pMnode, pTrans);
79,981!
226
  TAOS_RETURN(code);
79,981✔
227
}
228

229
static int32_t mndPostMgmtCode(SMnode *pMnode, int32_t code) {
81,451✔
230
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
81,451✔
231
  (void)taosThreadMutexLock(&pMgmt->lock);
81,451✔
232
  if (pMgmt->transId == 0) {
81,451✔
233
    goto _OUT;
11,953✔
234
  }
235

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

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

251
_OUT:
×
252
  (void)taosThreadMutexUnlock(&pMgmt->lock);
81,451✔
253
  return 0;
81,451✔
254
}
255

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

263
  pMsg->info.conn.applyIndex = pMeta->index;
81,451✔
264
  pMsg->info.conn.applyTerm = pMeta->term;
81,451✔
265
  pMeta->code = 0;
81,451✔
266

267
  atomic_store_64(&pMnode->applied, pMsg->info.conn.applyIndex);
81,451✔
268

269
  if (!syncUtilUserCommit(pMsg->msgType)) {
81,451✔
270
    goto _OUT;
1,470✔
271
  }
272

273
  code = mndProcessWriteMsg(pMnode, pMsg, pMeta);
79,981✔
274

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

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

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

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

301
void mndRestoreFinish(const SSyncFSM *pFsm, const SyncIndex commitIdx) {
1,369✔
302
  SMnode *pMnode = pFsm->data;
1,369✔
303

304
  if (!pMnode->deploy) {
1,369✔
305
    if (!pMnode->restored) {
282✔
306
      mInfo("vgId:1, sync restore finished, and will handle outstanding transactions");
276!
307
      mndTransPullup(pMnode);
276✔
308
      mndSetRestored(pMnode, true);
276✔
309
    } else {
310
      mInfo("vgId:1, sync restore finished, repeat call");
6!
311
    }
312
    if (sdbAfterRestored(pMnode->pSdb) != 0) {
282!
313
      mError("failed to prepare sdb while start mnode");
×
314
    }
315
  } else {
316
    mInfo("vgId:1, sync restore finished");
1,087!
317
  }
318
  int32_t code = mndRefreshUserIpWhiteList(pMnode);
1,369✔
319
  if (code != 0) {
1,369!
320
    mError("vgId:1, failed to refresh user ip white list since %s", tstrerror(code));
×
321
    mndSetRestored(pMnode, false);
×
322
  }
323

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

332
int32_t mndSnapshotStartRead(const SSyncFSM *pFsm, void *pParam, void **ppReader) {
×
333
  mInfo("start to read snapshot from sdb");
×
334
  SMnode *pMnode = pFsm->data;
×
335
  return sdbStartRead(pMnode->pSdb, (SSdbIter **)ppReader, NULL, NULL, NULL);
×
336
}
337

338
static void mndSnapshotStopRead(const SSyncFSM *pFsm, void *pReader) {
×
339
  mInfo("stop to read snapshot from sdb");
×
340
  SMnode *pMnode = pFsm->data;
×
341
  sdbStopRead(pMnode->pSdb, pReader);
×
342
}
×
343

344
int32_t mndSnapshotDoRead(const SSyncFSM *pFsm, void *pReader, void **ppBuf, int32_t *len) {
×
345
  SMnode *pMnode = pFsm->data;
×
346
  return sdbDoRead(pMnode->pSdb, pReader, ppBuf, len);
×
347
}
348

349
int32_t mndSnapshotStartWrite(const SSyncFSM *pFsm, void *pParam, void **ppWriter) {
×
350
  mInfo("start to apply snapshot to sdb");
×
351
  SMnode *pMnode = pFsm->data;
×
352
  return sdbStartWrite(pMnode->pSdb, (SSdbIter **)ppWriter);
×
353
}
354

355
int32_t mndSnapshotStopWrite(const SSyncFSM *pFsm, void *pWriter, bool isApply, SSnapshot *pSnapshot) {
×
356
  mInfo("stop to apply snapshot to sdb, apply:%d, index:%" PRId64 " term:%" PRIu64 " config:%" PRId64, isApply,
×
357
        pSnapshot->lastApplyIndex, pSnapshot->lastApplyTerm, pSnapshot->lastConfigIndex);
358
  SMnode *pMnode = pFsm->data;
×
359
  return sdbStopWrite(pMnode->pSdb, pWriter, isApply, pSnapshot->lastApplyIndex, pSnapshot->lastApplyTerm,
×
360
                      pSnapshot->lastConfigIndex);
361
}
362

363
int32_t mndSnapshotDoWrite(const SSyncFSM *pFsm, void *pWriter, void *pBuf, int32_t len) {
×
364
  SMnode *pMnode = pFsm->data;
×
365
  return sdbDoWrite(pMnode->pSdb, pWriter, pBuf, len);
×
366
}
367

368
static void mndBecomeFollower(const SSyncFSM *pFsm) {
68✔
369
  SMnode    *pMnode = pFsm->data;
68✔
370
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
68✔
371
  mInfo("vgId:1, become follower");
68!
372

373
  (void)taosThreadMutexLock(&pMgmt->lock);
68✔
374
  if (pMgmt->transId != 0) {
68!
375
    mInfo("vgId:1, become follower and post sem, trans:%d, failed to propose since not leader", pMgmt->transId);
×
376
    pMgmt->transId = 0;
×
377
    pMgmt->transSec = 0;
×
378
    pMgmt->transSeq = 0;
×
379
    pMgmt->errCode = TSDB_CODE_SYN_NOT_LEADER;
×
380
    if (tsem_post(&pMgmt->syncSem) < 0) {
×
381
      mError("failed to post sem");
×
382
    }
383
  }
384
  (void)taosThreadMutexUnlock(&pMgmt->lock);
68✔
385

386
  mndUpdateStreamExecInfoRole(pMnode, NODE_ROLE_FOLLOWER);
68✔
387
}
68✔
388

389
static void mndBecomeLearner(const SSyncFSM *pFsm) {
36✔
390
  SMnode    *pMnode = pFsm->data;
36✔
391
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
36✔
392
  mInfo("vgId:1, become learner");
36!
393

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

408
static void mndBecomeLeader(const SSyncFSM *pFsm) {
1,288✔
409
  mInfo("vgId:1, become leader");
1,288!
410
  SMnode *pMnode = pFsm->data;
1,288✔
411

412
  mndUpdateStreamExecInfoRole(pMnode, NODE_ROLE_LEADER);
1,288✔
413
  mndStreamResetInitTaskListLoadFlag();
1,288✔
414
}
1,288✔
415

416
static bool mndApplyQueueEmpty(const SSyncFSM *pFsm) {
×
417
  SMnode *pMnode = pFsm->data;
×
418

419
  if (pMnode != NULL && pMnode->msgCb.qsizeFp != NULL) {
×
420
    int32_t itemSize = tmsgGetQueueSize(&pMnode->msgCb, 1, APPLY_QUEUE);
×
421
    return (itemSize == 0);
×
422
  } else {
423
    return true;
×
424
  }
425
}
426

427
static int32_t mndApplyQueueItems(const SSyncFSM *pFsm) {
33,437✔
428
  SMnode *pMnode = pFsm->data;
33,437✔
429

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

438
SSyncFSM *mndSyncMakeFsm(SMnode *pMnode) {
1,366✔
439
  SSyncFSM *pFsm = taosMemoryCalloc(1, sizeof(SSyncFSM));
1,366!
440
  pFsm->data = pMnode;
1,366✔
441
  pFsm->FpCommitCb = mndSyncCommitMsg;
1,366✔
442
  pFsm->FpAppliedIndexCb = mndSyncAppliedIndex;
1,366✔
443
  pFsm->FpPreCommitCb = NULL;
1,366✔
444
  pFsm->FpRollBackCb = NULL;
1,366✔
445
  pFsm->FpRestoreFinishCb = mndRestoreFinish;
1,366✔
446
  pFsm->FpLeaderTransferCb = NULL;
1,366✔
447
  pFsm->FpApplyQueueEmptyCb = mndApplyQueueEmpty;
1,366✔
448
  pFsm->FpApplyQueueItems = mndApplyQueueItems;
1,366✔
449
  pFsm->FpReConfigCb = NULL;
1,366✔
450
  pFsm->FpBecomeLeaderCb = mndBecomeLeader;
1,366✔
451
  pFsm->FpBecomeAssignedLeaderCb = NULL;
1,366✔
452
  pFsm->FpBecomeFollowerCb = mndBecomeFollower;
1,366✔
453
  pFsm->FpBecomeLearnerCb = mndBecomeLearner;
1,366✔
454
  pFsm->FpGetSnapshot = mndSyncGetSnapshot;
1,366✔
455
  pFsm->FpGetSnapshotInfo = mndSyncGetSnapshotInfo;
1,366✔
456
  pFsm->FpSnapshotStartRead = mndSnapshotStartRead;
1,366✔
457
  pFsm->FpSnapshotStopRead = mndSnapshotStopRead;
1,366✔
458
  pFsm->FpSnapshotDoRead = mndSnapshotDoRead;
1,366✔
459
  pFsm->FpSnapshotStartWrite = mndSnapshotStartWrite;
1,366✔
460
  pFsm->FpSnapshotStopWrite = mndSnapshotStopWrite;
1,366✔
461
  pFsm->FpSnapshotDoWrite = mndSnapshotDoWrite;
1,366✔
462
  return pFsm;
1,366✔
463
}
464

465
int32_t mndInitSync(SMnode *pMnode) {
1,366✔
466
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
1,366✔
467
  (void)taosThreadMutexInit(&pMgmt->lock, NULL);
1,366✔
468
  (void)taosThreadMutexLock(&pMgmt->lock);
1,366✔
469
  pMgmt->transId = 0;
1,366✔
470
  pMgmt->transSec = 0;
1,366✔
471
  pMgmt->transSeq = 0;
1,366✔
472
  (void)taosThreadMutexUnlock(&pMgmt->lock);
1,366✔
473

474
  SSyncInfo syncInfo = {
1,366✔
475
      .snapshotStrategy = SYNC_STRATEGY_STANDARD_SNAPSHOT,
476
      .batchSize = 1,
477
      .vgId = 1,
478
      .pWal = pMnode->pWal,
1,366✔
479
      .msgcb = &pMnode->msgCb,
1,366✔
480
      .syncSendMSg = mndSyncSendMsg,
481
      .syncEqMsg = mndSyncEqMsg,
482
      .syncEqCtrlMsg = mndSyncEqCtrlMsg,
483
      .pingMs = 5000,
484
      .electMs = 3000,
485
      .heartbeatMs = 500,
486
  };
487

488
  snprintf(syncInfo.path, sizeof(syncInfo.path), "%s%ssync", pMnode->path, TD_DIRSEP);
1,366✔
489
  syncInfo.pFsm = mndSyncMakeFsm(pMnode);
1,366✔
490

491
  mInfo("vgId:1, start to open mnode sync, replica:%d selfIndex:%d", pMgmt->numOfReplicas, pMgmt->selfIndex);
1,366!
492
  SSyncCfg *pCfg = &syncInfo.syncCfg;
1,366✔
493
  pCfg->totalReplicaNum = pMgmt->numOfTotalReplicas;
1,366✔
494
  pCfg->replicaNum = pMgmt->numOfReplicas;
1,366✔
495
  pCfg->myIndex = pMgmt->selfIndex;
1,366✔
496
  pCfg->lastIndex = pMgmt->lastIndex;
1,366✔
497
  for (int32_t i = 0; i < pMgmt->numOfTotalReplicas; ++i) {
2,631✔
498
    SNodeInfo *pNode = &pCfg->nodeInfo[i];
1,265✔
499
    pNode->nodeId = pMgmt->replicas[i].id;
1,265✔
500
    pNode->nodePort = pMgmt->replicas[i].port;
1,265✔
501
    tstrncpy(pNode->nodeFqdn, pMgmt->replicas[i].fqdn, sizeof(pNode->nodeFqdn));
1,265✔
502
    pNode->nodeRole = pMgmt->nodeRoles[i];
1,265✔
503
    bool update = tmsgUpdateDnodeInfo(&pNode->nodeId, &pNode->clusterId, pNode->nodeFqdn, &pNode->nodePort);
1,265✔
504
    mInfo("vgId:1, index:%d ep:%s:%u dnode:%d cluster:%" PRId64 ", update:%d", i, pNode->nodeFqdn, pNode->nodePort,
1,265!
505
          pNode->nodeId, pNode->clusterId, update);
506
  }
507

508
  int32_t code = 0;
1,366✔
509
  if ((code = tsem_init(&pMgmt->syncSem, 0, 0)) < 0) {
1,366!
510
    mError("failed to open sync, tsem_init, since %s", tstrerror(code));
×
511
    TAOS_RETURN(code);
×
512
  }
513
  pMgmt->sync = syncOpen(&syncInfo, 1);  // always check
1,366✔
514
  if (pMgmt->sync <= 0) {
1,366!
515
    if (terrno != 0) code = terrno;
×
516
    mError("failed to open sync since %s", tstrerror(code));
×
517
    TAOS_RETURN(code);
×
518
  }
519
  pMnode->pSdb->sync = pMgmt->sync;
1,366✔
520

521
  mInfo("vgId:1, mnode sync is opened, id:%" PRId64, pMgmt->sync);
1,366!
522
  TAOS_RETURN(code);
1,366✔
523
}
524

525
void mndCleanupSync(SMnode *pMnode) {
1,365✔
526
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
1,365✔
527
  syncStop(pMgmt->sync);
1,365✔
528
  mInfo("mnode-sync is stopped, id:%" PRId64, pMgmt->sync);
1,365!
529

530
  if (tsem_destroy(&pMgmt->syncSem) < 0) {
1,365!
531
    mError("failed to destroy sem");
×
532
  }
533
  (void)taosThreadMutexDestroy(&pMgmt->lock);
1,365✔
534
  memset(pMgmt, 0, sizeof(SSyncMgmt));
1,365✔
535
}
1,365✔
536

537
void mndSyncCheckTimeout(SMnode *pMnode) {
1,039✔
538
  mTrace("check sync timeout");
1,039✔
539
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
1,039✔
540
  (void)taosThreadMutexLock(&pMgmt->lock);
1,039✔
541
  if (pMgmt->transId != 0) {
1,039✔
542
    int32_t curSec = taosGetTimestampSec();
5✔
543
    int32_t delta = curSec - pMgmt->transSec;
5✔
544
    if (delta > MNODE_TIMEOUT_SEC) {
5!
545
      mError("trans:%d, failed to propose since timeout, start:%d cur:%d delta:%d seq:%" PRId64, pMgmt->transId,
×
546
             pMgmt->transSec, curSec, delta, pMgmt->transSeq);
547
      // pMgmt->transId = 0;
548
      // pMgmt->transSec = 0;
549
      // pMgmt->transSeq = 0;
550
      // terrno = TSDB_CODE_SYN_TIMEOUT;
551
      // pMgmt->errCode = TSDB_CODE_SYN_TIMEOUT;
552
      // if (tsem_post(&pMgmt->syncSem) < 0) {
553
      //  mError("failed to post sem");
554
      //}
555
    } else {
556
      mDebug("trans:%d, waiting for sync confirm, start:%d cur:%d delta:%d seq:%" PRId64, pMgmt->transId,
5!
557
             pMgmt->transSec, curSec, curSec - pMgmt->transSec, pMgmt->transSeq);
558
    }
559
  } else {
560
    // mTrace("check sync timeout msg, no trans waiting for confirm");
561
  }
562
  (void)taosThreadMutexUnlock(&pMgmt->lock);
1,039✔
563
}
1,039✔
564

565
int32_t mndSyncPropose(SMnode *pMnode, SSdbRaw *pRaw, int32_t transId) {
69,512✔
566
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
69,512✔
567

568
  SRpcMsg req = {.msgType = TDMT_MND_APPLY_MSG, .contLen = sdbGetRawTotalSize(pRaw)};
69,512✔
569
  if (req.contLen <= 0) return terrno;
69,512!
570

571
  req.pCont = rpcMallocCont(req.contLen);
69,512✔
572
  if (req.pCont == NULL) return terrno;
69,512!
573
  memcpy(req.pCont, pRaw, req.contLen);
69,512✔
574

575
  (void)taosThreadMutexLock(&pMgmt->lock);
69,512✔
576
  pMgmt->errCode = 0;
69,512✔
577

578
  if (pMgmt->transId != 0) {
69,512✔
579
    mError("trans:%d, can't be proposed since trans:%d already waiting for confirm", transId, pMgmt->transId);
1!
580
    (void)taosThreadMutexUnlock(&pMgmt->lock);
1✔
581
    rpcFreeCont(req.pCont);
1✔
582
    TAOS_RETURN(TSDB_CODE_MND_LAST_TRANS_NOT_FINISHED);
1✔
583
  }
584

585
  mInfo("trans:%d, will be proposed", transId);
69,511!
586
  pMgmt->transId = transId;
69,511✔
587
  pMgmt->transSec = taosGetTimestampSec();
69,511✔
588

589
  int64_t seq = 0;
69,511✔
590
  int32_t code = syncPropose(pMgmt->sync, &req, false, &seq);
69,511✔
591
  if (code == 0) {
69,511✔
592
    mInfo("trans:%d, is proposing and wait sem, seq:%" PRId64, transId, seq);
69,510!
593
    pMgmt->transSeq = seq;
69,510✔
594
    (void)taosThreadMutexUnlock(&pMgmt->lock);
69,510✔
595
    code = tsem_wait(&pMgmt->syncSem);
69,510✔
596
  } else if (code > 0) {
1!
597
    mInfo("trans:%d, confirm at once since replica is 1, continue execute", transId);
×
598
    pMgmt->transId = 0;
×
599
    pMgmt->transSec = 0;
×
600
    pMgmt->transSeq = 0;
×
601
    (void)taosThreadMutexUnlock(&pMgmt->lock);
×
602
    code = sdbWriteWithoutFree(pMnode->pSdb, pRaw);
×
603
    if (code == 0) {
×
604
      sdbSetApplyInfo(pMnode->pSdb, req.info.conn.applyIndex, req.info.conn.applyTerm, SYNC_INDEX_INVALID);
×
605
    }
606
  } else {
607
    mError("trans:%d, failed to proposed since %s", transId, terrstr());
1!
608
    pMgmt->transId = 0;
1✔
609
    pMgmt->transSec = 0;
1✔
610
    pMgmt->transSeq = 0;
1✔
611
    (void)taosThreadMutexUnlock(&pMgmt->lock);
1✔
612
    if (terrno == 0) {
1!
613
      terrno = TSDB_CODE_APP_ERROR;
×
614
    }
615
  }
616

617
  rpcFreeCont(req.pCont);
69,511✔
618
  req.pCont = NULL;
69,511✔
619
  if (code != 0) {
69,511✔
620
    mError("trans:%d, failed to propose, code:0x%x", pMgmt->transId, code);
1!
621
    return code;
1✔
622
  }
623

624
  terrno = pMgmt->errCode;
69,510✔
625
  return terrno;
69,510✔
626
}
627

628
void mndSyncStart(SMnode *pMnode) {
1,365✔
629
  mInfo("vgId:1, start to start mnode sync");
1,365!
630
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
1,365✔
631
  if (syncStart(pMgmt->sync) < 0) {
1,365!
UNCOV
632
    mError("vgId:1, failed to start sync, id:%" PRId64, pMgmt->sync);
×
UNCOV
633
    return;
×
634
  }
635
  mInfo("vgId:1, mnode sync started, id:%" PRId64, pMgmt->sync);
1,365!
636
}
637

638
void mndSyncStop(SMnode *pMnode) {
1,365✔
639
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
1,365✔
640

641
  (void)taosThreadMutexLock(&pMgmt->lock);
1,365✔
642
  if (pMgmt->transId != 0) {
1,365✔
643
    mInfo("vgId:1, trans:%d, is stopped and post sem", pMgmt->transId);
12!
644
    pMgmt->transId = 0;
12✔
645
    pMgmt->transSec = 0;
12✔
646
    pMgmt->errCode = TSDB_CODE_APP_IS_STOPPING;
12✔
647
    if (tsem_post(&pMgmt->syncSem) < 0) {
12!
648
      mError("failed to post sem");
×
649
    }
650
  }
651
  (void)taosThreadMutexUnlock(&pMgmt->lock);
1,365✔
652
}
1,365✔
653

654
bool mndIsLeader(SMnode *pMnode) {
379,822✔
655
  terrno = 0;
379,822✔
656
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
379,827✔
657

658
  if (terrno != 0) {
379,826!
659
    mDebug("vgId:1, mnode is stopping");
×
660
    return false;
×
661
  }
662

663
  if (state.state != TAOS_SYNC_STATE_LEADER) {
379,826✔
664
    terrno = TSDB_CODE_SYN_NOT_LEADER;
13,979✔
665
    mDebug("vgId:1, mnode not leader, state:%s", syncStr(state.state));
13,979!
666
    return false;
13,979✔
667
  }
668

669
  if (!state.restored || !pMnode->restored) {
365,847✔
670
    terrno = TSDB_CODE_SYN_RESTORING;
19,887✔
671
    mDebug("vgId:1, mnode not restored:%d:%d", state.restored, pMnode->restored);
19,886✔
672
    return false;
19,886✔
673
  }
674

675
  return true;
345,960✔
676
}
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