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

taosdata / TDengine / #3608

12 Feb 2025 05:57AM UTC coverage: 63.066% (+1.4%) from 61.715%
#3608

push

travis-ci

web-flow
Merge pull request #29746 from taosdata/merge/mainto3.02

merge: from main to 3.0 branch

140199 of 286257 branches covered (48.98%)

Branch coverage included in aggregate %.

89 of 161 new or added lines in 18 files covered. (55.28%)

3211 existing lines in 190 files now uncovered.

218998 of 283298 relevant lines covered (77.3%)

5949310.66 hits per line

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

61.82
/source/libs/sync/src/syncRaftLog.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 "syncRaftLog.h"
18
#include "syncRaftCfg.h"
19
#include "syncRaftStore.h"
20
#include "syncUtil.h"
21

22
// log[m .. n]
23

24
// public function
25
static int32_t   raftLogRestoreFromSnapshot(struct SSyncLogStore* pLogStore, SyncIndex snapshotIndex);
26
static int32_t   raftLogAppendEntry(struct SSyncLogStore* pLogStore, SSyncRaftEntry* pEntry, bool forceSync);
27
static int32_t   raftLogTruncate(struct SSyncLogStore* pLogStore, SyncIndex fromIndex);
28
static bool      raftLogExist(struct SSyncLogStore* pLogStore, SyncIndex index);
29
static int32_t   raftLogUpdateCommitIndex(SSyncLogStore* pLogStore, SyncIndex index);
30
static SyncIndex raftlogCommitIndex(SSyncLogStore* pLogStore);
31
static int32_t   raftLogGetLastEntry(SSyncLogStore* pLogStore, SSyncRaftEntry** ppLastEntry);
32

33
SSyncLogStore* logStoreCreate(SSyncNode* pSyncNode) {
12,555✔
34
  SSyncLogStore* pLogStore = taosMemoryCalloc(1, sizeof(SSyncLogStore));
12,555!
35
  if (pLogStore == NULL) {
12,559!
36
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
37
    return NULL;
×
38
  }
39

40
  // pLogStore->pCache = taosLRUCacheInit(10 * 1024 * 1024, 1, .5);
41
  pLogStore->pCache = taosLRUCacheInit(30 * 1024 * 1024, 1, .5);
12,559✔
42
  if (pLogStore->pCache == NULL) {
12,559!
43
    taosMemoryFree(pLogStore);
×
44
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
45
    return NULL;
×
46
  }
47

48
  pLogStore->cacheHit = 0;
12,559✔
49
  pLogStore->cacheMiss = 0;
12,559✔
50

51
  taosLRUCacheSetStrictCapacity(pLogStore->pCache, false);
12,559✔
52

53
  pLogStore->data = taosMemoryMalloc(sizeof(SSyncLogStoreData));
12,559!
54
  if (!pLogStore->data) {
12,559!
55
    taosMemoryFree(pLogStore);
×
56
    taosLRUCacheCleanup(pLogStore->pCache);
×
57
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
58
    return NULL;
×
59
  }
60

61
  SSyncLogStoreData* pData = pLogStore->data;
12,559✔
62
  pData->pSyncNode = pSyncNode;
12,559✔
63
  pData->pWal = pSyncNode->pWal;
12,559✔
64
  if (pData->pWal == NULL) {
12,559!
65
    terrno = TSDB_CODE_SYN_INTERNAL_ERROR;
×
66
    return NULL;
×
67
  }
68

69
  (void)taosThreadMutexInit(&(pData->mutex), NULL);
12,559✔
70
  pData->pWalHandle = walOpenReader(pData->pWal, NULL, 0);
12,559✔
71
  if (!pData->pWalHandle) {
12,559!
72
    taosMemoryFree(pLogStore);
×
73
    taosLRUCacheCleanup(pLogStore->pCache);
×
74
    (void)taosThreadMutexDestroy(&(pData->mutex));
×
75
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
76
    return NULL;
×
77
  }
78

79
  pLogStore->syncLogUpdateCommitIndex = raftLogUpdateCommitIndex;
12,559✔
80
  pLogStore->syncLogCommitIndex = raftlogCommitIndex;
12,559✔
81
  pLogStore->syncLogRestoreFromSnapshot = raftLogRestoreFromSnapshot;
12,559✔
82
  pLogStore->syncLogBeginIndex = raftLogBeginIndex;
12,559✔
83
  pLogStore->syncLogEndIndex = raftLogEndIndex;
12,559✔
84
  pLogStore->syncLogIsEmpty = raftLogIsEmpty;
12,559✔
85
  pLogStore->syncLogEntryCount = raftLogEntryCount;
12,559✔
86
  pLogStore->syncLogLastIndex = raftLogLastIndex;
12,559✔
87
  pLogStore->syncLogIndexRetention = raftLogIndexRetention;
12,559✔
88
  pLogStore->syncLogLastTerm = raftLogLastTerm;
12,559✔
89
  pLogStore->syncLogAppendEntry = raftLogAppendEntry;
12,559✔
90
  pLogStore->syncLogGetEntry = raftLogGetEntry;
12,559✔
91
  pLogStore->syncLogTruncate = raftLogTruncate;
12,559✔
92
  pLogStore->syncLogWriteIndex = raftLogWriteIndex;
12,559✔
93
  pLogStore->syncLogExist = raftLogExist;
12,559✔
94

95
  return pLogStore;
12,559✔
96
}
97

98
void logStoreDestory(SSyncLogStore* pLogStore) {
12,556✔
99
  if (pLogStore != NULL) {
12,556!
100
    SSyncLogStoreData* pData = pLogStore->data;
12,557✔
101

102
    (void)taosThreadMutexLock(&(pData->mutex));
12,557✔
103
    if (pData->pWalHandle != NULL) {
12,558!
104
      walCloseReader(pData->pWalHandle);
12,558✔
105
      pData->pWalHandle = NULL;
12,558✔
106
    }
107
    (void)taosThreadMutexUnlock(&(pData->mutex));
12,558✔
108
    (void)taosThreadMutexDestroy(&(pData->mutex));
12,558✔
109

110
    taosMemoryFree(pLogStore->data);
12,558!
111

112
    taosLRUCacheEraseUnrefEntries(pLogStore->pCache);
12,558✔
113
    taosLRUCacheCleanup(pLogStore->pCache);
12,558✔
114

115
    taosMemoryFree(pLogStore);
12,556!
116
  }
117
}
12,556✔
118

119
// log[m .. n]
120
static int32_t raftLogRestoreFromSnapshot(struct SSyncLogStore* pLogStore, SyncIndex snapshotIndex) {
55✔
121
  if (!(snapshotIndex >= 0)) return TSDB_CODE_SYN_INTERNAL_ERROR;
55!
122

123
  SSyncLogStoreData* pData = pLogStore->data;
55✔
124
  SWal*              pWal = pData->pWal;
55✔
125
  int32_t            code = walRestoreFromSnapshot(pWal, snapshotIndex);
55✔
126
  if (code != 0) {
55!
127
    int32_t     err = code;
×
128
    const char* errStr = tstrerror(err);
×
129
    int32_t     sysErr = errno;
×
130
    const char* sysErrStr = strerror(errno);
×
131

132
    sNError(pData->pSyncNode,
×
133
            "wal restore from snapshot error, index:%" PRId64 ", err:0x%x, msg:%s, syserr:%d, sysmsg:%s", snapshotIndex,
134
            err, errStr, sysErr, sysErrStr);
135
    TAOS_RETURN(err);
×
136
  }
137

138
  TAOS_RETURN(TSDB_CODE_SUCCESS);
55✔
139
}
140

141
SyncIndex raftLogBeginIndex(struct SSyncLogStore* pLogStore) {
265,612✔
142
  SSyncLogStoreData* pData = pLogStore->data;
265,612✔
143
  SWal*              pWal = pData->pWal;
265,612✔
144
  SyncIndex          firstVer = walGetFirstVer(pWal);
265,612✔
145
  return firstVer;
265,612✔
146
}
147

148
SyncIndex raftLogEndIndex(struct SSyncLogStore* pLogStore) { return raftLogLastIndex(pLogStore); }
25,792✔
149

150
bool raftLogIsEmpty(struct SSyncLogStore* pLogStore) {
25,792✔
151
  SSyncLogStoreData* pData = pLogStore->data;
25,792✔
152
  SWal*              pWal = pData->pWal;
25,792✔
153
  return walIsEmpty(pWal);
25,792✔
154
}
155

156
int32_t raftLogEntryCount(struct SSyncLogStore* pLogStore) {
×
157
  SyncIndex beginIndex = raftLogBeginIndex(pLogStore);
×
158
  SyncIndex endIndex = raftLogEndIndex(pLogStore);
×
159
  int32_t   count = endIndex - beginIndex + 1;
×
160
  return count > 0 ? count : 0;
×
161
}
162

163
SyncIndex raftLogLastIndex(struct SSyncLogStore* pLogStore) {
9,346,656✔
164
  SyncIndex          lastIndex;
165
  SSyncLogStoreData* pData = pLogStore->data;
9,346,656✔
166
  SWal*              pWal = pData->pWal;
9,346,656✔
167
  SyncIndex          lastVer = walGetLastVer(pWal);
9,346,656✔
168

169
  return lastVer;
9,346,971✔
170
}
171

172
SyncIndex raftLogIndexRetention(struct SSyncLogStore* pLogStore, int64_t bytes) {
775✔
173
  SyncIndex          lastIndex;
174
  SSyncLogStoreData* pData = pLogStore->data;
775✔
175
  SWal*              pWal = pData->pWal;
775✔
176
  SyncIndex          lastVer = walGetVerRetention(pWal, bytes);
775✔
177

178
  return lastVer;
775✔
179
}
180

181
SyncIndex raftLogWriteIndex(struct SSyncLogStore* pLogStore) {
×
182
  SSyncLogStoreData* pData = pLogStore->data;
×
183
  SWal*              pWal = pData->pWal;
×
184
  SyncIndex          lastVer = walGetLastVer(pWal);
×
185
  return lastVer + 1;
×
186
}
187

188
static bool raftLogExist(struct SSyncLogStore* pLogStore, SyncIndex index) {
×
189
  SSyncLogStoreData* pData = pLogStore->data;
×
190
  SWal*              pWal = pData->pWal;
×
191
  bool               b = walLogExist(pWal, index);
×
192
  return b;
×
193
}
194

195
// if success, return last term
196
// if not log, return 0
197
// if error, return SYNC_TERM_INVALID
198
SyncTerm raftLogLastTerm(struct SSyncLogStore* pLogStore) {
15,851✔
199
  SSyncLogStoreData* pData = pLogStore->data;
15,851✔
200
  SWal*              pWal = pData->pWal;
15,851✔
201
  if (walIsEmpty(pWal)) {
15,851✔
202
    return 0;
11,393✔
203
  } else {
204
    SSyncRaftEntry* pLastEntry;
205
    int32_t         code = raftLogGetLastEntry(pLogStore, &pLastEntry);
4,458✔
206
    if (code == 0 && pLastEntry != NULL) {
4,457!
207
      SyncTerm lastTerm = pLastEntry->term;
4,458✔
208
      taosMemoryFree(pLastEntry);
4,458!
209
      return lastTerm;
4,458✔
210
    } else {
211
      return SYNC_TERM_INVALID;
×
212
    }
213
  }
214

215
  // can not be here!
216
  return SYNC_TERM_INVALID;
217
}
218

219
static int32_t raftLogAppendEntry(struct SSyncLogStore* pLogStore, SSyncRaftEntry* pEntry, bool forceSync) {
2,251,514✔
220
  SSyncLogStoreData* pData = pLogStore->data;
2,251,514✔
221
  SWal*              pWal = pData->pWal;
2,251,514✔
222

223
  SWalSyncInfo syncMeta = {0};
2,251,514✔
224
  syncMeta.isWeek = pEntry->isWeak;
2,251,514✔
225
  syncMeta.seqNum = pEntry->seqNum;
2,251,514✔
226
  syncMeta.term = pEntry->term;
2,251,514✔
227

228
  int64_t tsWriteBegin = taosGetTimestampNs();
2,251,636✔
229
  int32_t code = walAppendLog(pWal, pEntry->index, pEntry->originalRpcType, syncMeta, pEntry->data, pEntry->dataLen);
2,251,636✔
230
  int64_t tsWriteEnd = taosGetTimestampNs();
2,251,681✔
231
  int64_t tsElapsed = tsWriteEnd - tsWriteBegin;
2,251,681✔
232

233
  if (TSDB_CODE_SUCCESS != code) {
2,251,681!
234
    int32_t     err = terrno;
×
235
    const char* errStr = tstrerror(err);
×
236
    int32_t     sysErr = errno;
×
237
    const char* sysErrStr = strerror(errno);
×
238

239
    sNError(pData->pSyncNode, "wal write error, index:%" PRId64 ", err:0x%x, msg:%s, syserr:%d, sysmsg:%s",
×
240
            pEntry->index, err, errStr, sysErr, sysErrStr);
241

242
    TAOS_RETURN(err);
×
243
  }
244

245
  code = walFsync(pWal, forceSync);
2,251,681✔
246
  if (TSDB_CODE_SUCCESS != code) {
2,251,562!
247
    sNError(pData->pSyncNode, "wal fsync failed since %s", tstrerror(code));
×
248
    TAOS_RETURN(code);
×
249
  }
250

251
  sNTrace(pData->pSyncNode, "write index:%" PRId64 ", type:%s, origin type:%s, elapsed:%" PRId64, pEntry->index,
2,251,562!
252
          TMSG_INFO(pEntry->msgType), TMSG_INFO(pEntry->originalRpcType), tsElapsed);
253
  TAOS_RETURN(TSDB_CODE_SUCCESS);
2,251,573✔
254
}
255

256
// entry found, return 0
257
// entry not found, return -1, terrno = TSDB_CODE_WAL_LOG_NOT_EXIST
258
// other error, return -1
259
int32_t raftLogGetEntry(struct SSyncLogStore* pLogStore, SyncIndex index, SSyncRaftEntry** ppEntry) {
442,968✔
260
  SSyncLogStoreData* pData = pLogStore->data;
442,968✔
261
  SWal*              pWal = pData->pWal;
442,968✔
262
  int32_t            code = 0;
442,968✔
263

264
  *ppEntry = NULL;
442,968✔
265

266
  int64_t ts1 = taosGetTimestampNs();
442,966✔
267
  (void)taosThreadMutexLock(&(pData->mutex));
442,966✔
268

269
  SWalReader* pWalHandle = pData->pWalHandle;
442,987✔
270
  if (pWalHandle == NULL) {
442,987!
271
    sError("vgId:%d, wal handle is NULL", pData->pSyncNode->vgId);
×
272
    (void)taosThreadMutexUnlock(&(pData->mutex));
×
273

274
    TAOS_RETURN(TSDB_CODE_SYN_INTERNAL_ERROR);
×
275
  }
276

277
  int64_t ts2 = taosGetTimestampNs();
442,978✔
278
  code = walReadVer(pWalHandle, index);
442,978✔
279
  walReadReset(pWalHandle);
442,938✔
280
  int64_t ts3 = taosGetTimestampNs();
442,992✔
281

282
  // code = walReadVerCached(pWalHandle, index);
283
  if (code != 0) {
442,992✔
284
    int32_t     err = code;
84✔
285
    const char* errStr = tstrerror(err);
84✔
286
    int32_t     sysErr = errno;
84✔
287
    const char* sysErrStr = strerror(errno);
84✔
288

289
    if (terrno == TSDB_CODE_WAL_LOG_NOT_EXIST) {
84!
290
      sNTrace(pData->pSyncNode, "wal read not exist, index:%" PRId64 ", err:0x%x, msg:%s, syserr:%d, sysmsg:%s", index,
84!
291
              err, errStr, sysErr, sysErrStr);
292
    } else {
293
      sNTrace(pData->pSyncNode, "wal read error, index:%" PRId64 ", err:0x%x, msg:%s, syserr:%d, sysmsg:%s", index, err,
×
294
              errStr, sysErr, sysErrStr);
295
    }
296

297
    /*
298
        int32_t saveErr = terrno;
299
        walCloseReadHandle(pWalHandle);
300
        terrno = saveErr;
301
    */
302

303
    (void)taosThreadMutexUnlock(&(pData->mutex));
84✔
304

305
    TAOS_RETURN(code);
84✔
306
  }
307

308
  *ppEntry = syncEntryBuild(pWalHandle->pHead->head.bodyLen);
442,908✔
309
  if (*ppEntry == NULL) return TSDB_CODE_SYN_INTERNAL_ERROR;
442,872!
310
  (*ppEntry)->msgType = TDMT_SYNC_CLIENT_REQUEST;
442,872✔
311
  (*ppEntry)->originalRpcType = pWalHandle->pHead->head.msgType;
442,872✔
312
  (*ppEntry)->seqNum = pWalHandle->pHead->head.syncMeta.seqNum;
442,872✔
313
  (*ppEntry)->isWeak = pWalHandle->pHead->head.syncMeta.isWeek;
442,872✔
314
  (*ppEntry)->term = pWalHandle->pHead->head.syncMeta.term;
442,872✔
315
  (*ppEntry)->index = index;
442,872✔
316
  if ((*ppEntry)->dataLen != pWalHandle->pHead->head.bodyLen) return TSDB_CODE_SYN_INTERNAL_ERROR;
442,872!
317
  (void)memcpy((*ppEntry)->data, pWalHandle->pHead->head.body, pWalHandle->pHead->head.bodyLen);
442,872✔
318

319
  /*
320
    int32_t saveErr = terrno;
321
    walCloseReadHandle(pWalHandle);
322
    terrno = saveErr;
323
  */
324

325
  (void)taosThreadMutexUnlock(&(pData->mutex));
442,872✔
326
  int64_t ts4 = taosGetTimestampNs();
442,910✔
327

328
  int64_t tsElapsed = ts4 - ts1;
442,910✔
329
  int64_t tsElapsedLock = ts2 - ts1;
442,910✔
330
  int64_t tsElapsedRead = ts3 - ts2;
442,910✔
331
  int64_t tsElapsedBuild = ts4 - ts3;
442,910✔
332

333
  sNTrace(pData->pSyncNode,
442,910!
334
          "read index:%" PRId64 ", elapsed:%" PRId64 ", elapsed-lock:%" PRId64 ", elapsed-read:%" PRId64
335
          ", elapsed-build:%" PRId64,
336
          index, tsElapsed, tsElapsedLock, tsElapsedRead, tsElapsedBuild);
337

338
  TAOS_RETURN(code);
442,910✔
339
}
340

341
// truncate semantic
342
static int32_t raftLogTruncate(struct SSyncLogStore* pLogStore, SyncIndex fromIndex) {
3✔
343
  SSyncLogStoreData* pData = pLogStore->data;
3✔
344
  SWal*              pWal = pData->pWal;
3✔
345

346
  int32_t code = walRollback(pWal, fromIndex);
3✔
347
  if (code != 0) {
3!
348
    int32_t     err = code;
×
349
    const char* errStr = tstrerror(err);
×
350
    int32_t     sysErr = errno;
×
351
    const char* sysErrStr = strerror(errno);
×
352
    sError("vgId:%d, wal truncate error, from-index:%" PRId64 ", err:0x%x, msg:%s, syserr:%d, sysmsg:%s",
×
353
           pData->pSyncNode->vgId, fromIndex, err, errStr, sysErr, sysErrStr);
354
  }
355

356
  // event log
357
  sNTrace(pData->pSyncNode, "log truncate, from-index:%" PRId64, fromIndex);
3!
358

359
  TAOS_RETURN(code);
3✔
360
}
361

362
// entry found, return 0
363
// entry not found, return -1, terrno = TSDB_CODE_WAL_LOG_NOT_EXIST
364
// other error, return -1
365
static int32_t raftLogGetLastEntry(SSyncLogStore* pLogStore, SSyncRaftEntry** ppLastEntry) {
4,458✔
366
  SSyncLogStoreData* pData = pLogStore->data;
4,458✔
367
  SWal*              pWal = pData->pWal;
4,458✔
368
  if (ppLastEntry == NULL) return TSDB_CODE_SYN_INTERNAL_ERROR;
4,458!
369

370
  *ppLastEntry = NULL;
4,458✔
371
  if (walIsEmpty(pWal)) {
4,458!
372
    TAOS_RETURN(TSDB_CODE_WAL_LOG_NOT_EXIST);
×
373
  } else {
374
    SyncIndex lastIndex = raftLogLastIndex(pLogStore);
4,458✔
375
    if (!(lastIndex >= SYNC_INDEX_BEGIN)) return TSDB_CODE_SYN_INTERNAL_ERROR;
4,458!
376
    int32_t code = raftLogGetEntry(pLogStore, lastIndex, ppLastEntry);
4,458✔
377

378
    TAOS_RETURN(code);
4,458✔
379
  }
380

381
  TAOS_RETURN(TSDB_CODE_FAILED);
382
}
383

384
int32_t raftLogUpdateCommitIndex(SSyncLogStore* pLogStore, SyncIndex index) {
2,265,175✔
385
  SSyncLogStoreData* pData = pLogStore->data;
2,265,175✔
386
  SWal*              pWal = pData->pWal;
2,265,175✔
387

388
  // need not update
389
  SyncIndex snapshotVer = walGetSnapshotVer(pWal);
2,265,175✔
390
  SyncIndex walCommitVer = walGetCommittedVer(pWal);
2,265,283✔
391
  SyncIndex wallastVer = walGetLastVer(pWal);
2,265,334✔
392

393
  if (index < snapshotVer || index > wallastVer) {
2,265,349!
394
    // ignore
UNCOV
395
    TAOS_RETURN(TSDB_CODE_SUCCESS);
×
396
  }
397

398
  int32_t code = walCommit(pWal, index);
2,265,349✔
399
  if (code != 0) {
2,265,347!
400
    int32_t     err = code;
×
401
    const char* errStr = tstrerror(err);
×
402
    int32_t     sysErr = errno;
×
403
    const char* sysErrStr = strerror(errno);
×
404
    sError("vgId:%d, wal update commit index error, index:%" PRId64 ", err:0x%x, msg:%s, syserr:%d, sysmsg:%s",
×
405
           pData->pSyncNode->vgId, index, err, errStr, sysErr, sysErrStr);
406

407
    TAOS_RETURN(code);
×
408
  }
409

410
  TAOS_RETURN(TSDB_CODE_SUCCESS);
2,265,347✔
411
}
412

413
SyncIndex raftlogCommitIndex(SSyncLogStore* pLogStore) {
12,558✔
414
  SSyncLogStoreData* pData = pLogStore->data;
12,558✔
415
  return pData->pSyncNode->commitIndex;
12,558✔
416
}
417

418
SyncIndex logStoreFirstIndex(SSyncLogStore* pLogStore) {
×
419
  SSyncLogStoreData* pData = pLogStore->data;
×
420
  SWal*              pWal = pData->pWal;
×
421
  return walGetFirstVer(pWal);
×
422
}
423

424
SyncIndex logStoreWalCommitVer(SSyncLogStore* pLogStore) {
×
425
  SSyncLogStoreData* pData = pLogStore->data;
×
426
  SWal*              pWal = pData->pWal;
×
427

428
  return walGetCommittedVer(pWal);
×
429
}
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