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

taosdata / TDengine / #3523

06 Nov 2024 02:29AM UTC coverage: 55.861% (-2.4%) from 58.216%
#3523

push

travis-ci

web-flow
Merge pull request #28551 from taosdata/feat/TS-5215-2

test(blob): testing & fixes for blob

106075 of 245834 branches covered (43.15%)

Branch coverage included in aggregate %.

0 of 15 new or added lines in 2 files covered. (0.0%)

17003 existing lines in 254 files now uncovered.

181910 of 269703 relevant lines covered (67.45%)

1527639.59 hits per line

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

57.82
/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 "mndSync.h"
18
#include "mndCluster.h"
19
#include "mndTrans.h"
20
#include "mndUser.h"
21
#include "mndStream.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) {
40,825✔
47
  if (pMsg == NULL || pMsg->pCont == NULL) {
40,825!
48
    return -1;
×
49
  }
50

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

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

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

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

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

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

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

90
_OUT:
290✔
91
  return code;
8,738✔
92
}
93

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

98
  int32_t numOfActions = taosArrayGetSize(pTrans->prepareActions);
15,298✔
99
  if (numOfActions == 0) {
15,298✔
100
    code = 0;
11,320✔
101
    goto _OUT;
11,320✔
102
  }
103

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

106
  for (action = 0; action < numOfActions; ++action) {
12,716✔
107
    STransAction *pAction = taosArrayGet(pTrans->prepareActions, action);
8,738✔
108

109
    if (pAction->actionType != TRANS_ACTION_RAW) {
8,738!
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);
8,738✔
115
    if (code != 0) {
8,738!
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;
3,978✔
122
_OUT:
15,298✔
123
  return code;
15,298✔
124
}
125

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

134
    return mndTransValidatePrepareStage(pMnode, pTrans);
15,298✔
135
  }
136
  TAOS_RETURN(code);
18,803✔
137
}
138

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

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

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

157
  code = mndTransValidateImp(pMnode, pTrans);
34,101✔
158

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

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

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

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

219
  mndTransRefresh(pMnode, pTrans);
34,101✔
220

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

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

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

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

245
  if (pMgmt->errCode != 0) {
30,765!
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);
30,765!
249
  }
250

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

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

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

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

269
  if (!syncUtilUserCommit(pMsg->msgType)) {
34,895✔
270
    goto _OUT;
794✔
271
  }
272

273
  code = mndProcessWriteMsg(pMnode, pMsg, pMeta);
34,101✔
274

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

282
SyncIndex mndSyncAppliedIndex(const SSyncFSM *pFSM) {
34,913✔
283
  SMnode *pMnode = pFSM->data;
34,913✔
284
  return atomic_load_64(&pMnode->applied);
34,913✔
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) {
19,709✔
296
  SMnode *pMnode = pFsm->data;
19,709✔
297
  sdbGetCommitInfo(pMnode->pSdb, &pSnapshot->lastApplyIndex, &pSnapshot->lastApplyTerm, &pSnapshot->lastConfigIndex);
19,709✔
298
  return 0;
19,709✔
299
}
300

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

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

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

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

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

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

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

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

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

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

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

383
  mndUpdateStreamExecInfoRole(pMnode, NODE_ROLE_FOLLOWER);
104✔
384
}
104✔
385

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

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

405
static void mndBecomeLeader(const SSyncFSM *pFsm) {
620✔
406
  mInfo("vgId:1, become leader");
620!
407
  SMnode *pMnode = pFsm->data;
620✔
408

409
  mndUpdateStreamExecInfoRole(pMnode, NODE_ROLE_LEADER);
620✔
410
  mndStreamResetInitTaskListLoadFlag();
620✔
411
}
620✔
412

413
static bool mndApplyQueueEmpty(const SSyncFSM *pFsm) {
×
414
  SMnode *pMnode = pFsm->data;
×
415

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

424
static int32_t mndApplyQueueItems(const SSyncFSM *pFsm) {
2,729✔
425
  SMnode *pMnode = pFsm->data;
2,729✔
426

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

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

462
int32_t mndInitSync(SMnode *pMnode) {
716✔
463
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
716✔
464
  (void)taosThreadMutexInit(&pMgmt->lock, NULL);
716✔
465
  (void)taosThreadMutexLock(&pMgmt->lock);
716✔
466
  pMgmt->transId = 0;
716✔
467
  pMgmt->transSec = 0;
716✔
468
  pMgmt->transSeq = 0;
716✔
469
  (void)taosThreadMutexUnlock(&pMgmt->lock);
716✔
470

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

485
  snprintf(syncInfo.path, sizeof(syncInfo.path), "%s%ssync", pMnode->path, TD_DIRSEP);
716✔
486
  syncInfo.pFsm = mndSyncMakeFsm(pMnode);
716✔
487

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

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

518
  mInfo("vgId:1, mnode sync is opened, id:%" PRId64, pMgmt->sync);
716!
519
  TAOS_RETURN(code);
716✔
520
}
521

522
void mndCleanupSync(SMnode *pMnode) {
715✔
523
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
715✔
524
  syncStop(pMgmt->sync);
715✔
525
  mInfo("mnode-sync is stopped, id:%" PRId64, pMgmt->sync);
715!
526

527
  if (tsem_destroy(&pMgmt->syncSem) < 0) {
715!
528
    mError("failed to destroy sem");
×
529
  }
530
  (void)taosThreadMutexDestroy(&pMgmt->lock);
715✔
531
  memset(pMgmt, 0, sizeof(SSyncMgmt));
715✔
532
}
715✔
533

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

562
int32_t mndSyncPropose(SMnode *pMnode, SSdbRaw *pRaw, int32_t transId) {
30,786✔
563
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
30,786✔
564

565
  SRpcMsg req = {.msgType = TDMT_MND_APPLY_MSG, .contLen = sdbGetRawTotalSize(pRaw)};
30,786✔
566
  if (req.contLen <= 0) return terrno;
30,786!
567

568
  req.pCont = rpcMallocCont(req.contLen);
30,786✔
569
  if (req.pCont == NULL) return terrno;
30,786!
570
  memcpy(req.pCont, pRaw, req.contLen);
30,786✔
571

572
  (void)taosThreadMutexLock(&pMgmt->lock);
30,786✔
573
  pMgmt->errCode = 0;
30,786✔
574

575
  if (pMgmt->transId != 0) {
30,786!
576
    mError("trans:%d, can't be proposed since trans:%d already waiting for confirm", transId, pMgmt->transId);
×
577
    (void)taosThreadMutexUnlock(&pMgmt->lock);
×
578
    rpcFreeCont(req.pCont);
×
579
    TAOS_RETURN(TSDB_CODE_MND_LAST_TRANS_NOT_FINISHED);
×
580
  }
581

582
  mInfo("trans:%d, will be proposed", transId);
30,786!
583
  pMgmt->transId = transId;
30,786✔
584
  pMgmt->transSec = taosGetTimestampSec();
30,786✔
585

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

614
  rpcFreeCont(req.pCont);
30,786✔
615
  req.pCont = NULL;
30,786✔
616
  if (code != 0) {
30,786✔
617
    mError("trans:%d, failed to propose, code:0x%x", pMgmt->transId, code);
5!
618
    return code;
5✔
619
  }
620

621
  terrno = pMgmt->errCode;
30,781✔
622
  return terrno;
30,781✔
623
}
624

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

635
void mndSyncStop(SMnode *pMnode) {
715✔
636
  SSyncMgmt *pMgmt = &pMnode->syncMgmt;
715✔
637

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

651
bool mndIsLeader(SMnode *pMnode) {
180,911✔
652
  terrno = 0;
180,911✔
653
  SSyncState state = syncGetState(pMnode->syncMgmt.sync);
180,920✔
654

655
  if (terrno != 0) {
180,921!
656
    mDebug("vgId:1, mnode is stopping");
×
657
    return false;
×
658
  }
659

660
  if (state.state != TAOS_SYNC_STATE_LEADER) {
180,921✔
661
    terrno = TSDB_CODE_SYN_NOT_LEADER;
11,074✔
662
    mDebug("vgId:1, mnode not leader, state:%s", syncStr(state.state));
11,073!
663
    return false;
11,074✔
664
  }
665

666
  if (!state.restored || !pMnode->restored) {
169,847✔
667
    terrno = TSDB_CODE_SYN_RESTORING;
236✔
668
    mDebug("vgId:1, mnode not restored:%d:%d", state.restored, pMnode->restored);
233✔
669
    return false;
233✔
670
  }
671

672
  return true;
169,611✔
673
}
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