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

taosdata / TDengine / #3558

17 Dec 2024 06:05AM UTC coverage: 59.778% (+1.6%) from 58.204%
#3558

push

travis-ci

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

merge: form main to 3.0 branch

132787 of 287595 branches covered (46.17%)

Branch coverage included in aggregate %.

104 of 191 new or added lines in 5 files covered. (54.45%)

6085 existing lines in 168 files now uncovered.

209348 of 284746 relevant lines covered (73.52%)

8164844.48 hits per line

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

61.54
/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) {
11,653✔
34
  SSyncLogStore* pLogStore = taosMemoryCalloc(1, sizeof(SSyncLogStore));
11,653!
35
  if (pLogStore == NULL) {
11,662!
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);
11,662✔
42
  if (pLogStore->pCache == NULL) {
11,662!
43
    taosMemoryFree(pLogStore);
×
44
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
45
    return NULL;
×
46
  }
47

48
  pLogStore->cacheHit = 0;
11,662✔
49
  pLogStore->cacheMiss = 0;
11,662✔
50

51
  taosLRUCacheSetStrictCapacity(pLogStore->pCache, false);
11,662✔
52

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

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

69
  (void)taosThreadMutexInit(&(pData->mutex), NULL);
11,662✔
70
  pData->pWalHandle = walOpenReader(pData->pWal, NULL, 0);
11,662✔
71
  if (!pData->pWalHandle) {
11,659!
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;
11,659✔
80
  pLogStore->syncLogCommitIndex = raftlogCommitIndex;
11,659✔
81
  pLogStore->syncLogRestoreFromSnapshot = raftLogRestoreFromSnapshot;
11,659✔
82
  pLogStore->syncLogBeginIndex = raftLogBeginIndex;
11,659✔
83
  pLogStore->syncLogEndIndex = raftLogEndIndex;
11,659✔
84
  pLogStore->syncLogIsEmpty = raftLogIsEmpty;
11,659✔
85
  pLogStore->syncLogEntryCount = raftLogEntryCount;
11,659✔
86
  pLogStore->syncLogLastIndex = raftLogLastIndex;
11,659✔
87
  pLogStore->syncLogIndexRetention = raftLogIndexRetention;
11,659✔
88
  pLogStore->syncLogLastTerm = raftLogLastTerm;
11,659✔
89
  pLogStore->syncLogAppendEntry = raftLogAppendEntry;
11,659✔
90
  pLogStore->syncLogGetEntry = raftLogGetEntry;
11,659✔
91
  pLogStore->syncLogTruncate = raftLogTruncate;
11,659✔
92
  pLogStore->syncLogWriteIndex = raftLogWriteIndex;
11,659✔
93
  pLogStore->syncLogExist = raftLogExist;
11,659✔
94

95
  return pLogStore;
11,659✔
96
}
97

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

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

110
    taosMemoryFree(pLogStore->data);
11,660!
111

112
    taosLRUCacheEraseUnrefEntries(pLogStore->pCache);
11,661✔
113
    taosLRUCacheCleanup(pLogStore->pCache);
11,661✔
114

115
    taosMemoryFree(pLogStore);
11,661!
116
  }
117
}
11,661✔
118

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

123
  SSyncLogStoreData* pData = pLogStore->data;
68✔
124
  SWal*              pWal = pData->pWal;
68✔
125
  int32_t            code = walRestoreFromSnapshot(pWal, snapshotIndex);
68✔
126
  if (code != 0) {
68!
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);
68✔
139
}
140

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

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

150
bool raftLogIsEmpty(struct SSyncLogStore* pLogStore) {
14,522✔
151
  SSyncLogStoreData* pData = pLogStore->data;
14,522✔
152
  SWal*              pWal = pData->pWal;
14,522✔
153
  return walIsEmpty(pWal);
14,522✔
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) {
50,367,503✔
164
  SyncIndex          lastIndex;
165
  SSyncLogStoreData* pData = pLogStore->data;
50,367,503✔
166
  SWal*              pWal = pData->pWal;
50,367,503✔
167
  SyncIndex          lastVer = walGetLastVer(pWal);
50,367,503✔
168

169
  return lastVer;
50,367,844✔
170
}
171

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

178
  return lastVer;
667✔
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) {
14,099✔
199
  SSyncLogStoreData* pData = pLogStore->data;
14,099✔
200
  SWal*              pWal = pData->pWal;
14,099✔
201
  if (walIsEmpty(pWal)) {
14,099✔
202
    return 0;
10,182✔
203
  } else {
204
    SSyncRaftEntry* pLastEntry;
205
    int32_t         code = raftLogGetLastEntry(pLogStore, &pLastEntry);
3,917✔
206
    if (code == 0 && pLastEntry != NULL) {
3,917!
207
      SyncTerm lastTerm = pLastEntry->term;
3,917✔
208
      taosMemoryFree(pLastEntry);
3,917!
209
      return lastTerm;
3,917✔
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) {
12,557,730✔
220
  SSyncLogStoreData* pData = pLogStore->data;
12,557,730✔
221
  SWal*              pWal = pData->pWal;
12,557,730✔
222

223
  SWalSyncInfo syncMeta = {0};
12,557,730✔
224
  syncMeta.isWeek = pEntry->isWeak;
12,557,730✔
225
  syncMeta.seqNum = pEntry->seqNum;
12,557,730✔
226
  syncMeta.term = pEntry->term;
12,557,730✔
227

228
  int64_t tsWriteBegin = taosGetTimestampNs();
12,557,881✔
229
  int32_t code = walAppendLog(pWal, pEntry->index, pEntry->originalRpcType, syncMeta, pEntry->data, pEntry->dataLen);
12,557,881✔
230
  int64_t tsWriteEnd = taosGetTimestampNs();
12,557,935✔
231
  int64_t tsElapsed = tsWriteEnd - tsWriteBegin;
12,557,935✔
232

233
  if (TSDB_CODE_SUCCESS != code) {
12,557,935!
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);
12,557,935✔
246
  if (TSDB_CODE_SUCCESS != code) {
12,557,789!
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,
12,557,789!
252
          TMSG_INFO(pEntry->msgType), TMSG_INFO(pEntry->originalRpcType), tsElapsed);
253
  TAOS_RETURN(TSDB_CODE_SUCCESS);
12,557,790✔
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) {
3,000,192✔
260
  SSyncLogStoreData* pData = pLogStore->data;
3,000,192✔
261
  SWal*              pWal = pData->pWal;
3,000,192✔
262
  int32_t            code = 0;
3,000,192✔
263

264
  *ppEntry = NULL;
3,000,192✔
265

266
  int64_t ts1 = taosGetTimestampNs();
3,000,235✔
267
  (void)taosThreadMutexLock(&(pData->mutex));
3,000,235✔
268

269
  SWalReader* pWalHandle = pData->pWalHandle;
3,000,310✔
270
  if (pWalHandle == NULL) {
3,000,310!
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();
3,000,251✔
278
  code = walReadVer(pWalHandle, index);
3,000,251✔
279
  walReadReset(pWalHandle);
2,999,791✔
280
  int64_t ts3 = taosGetTimestampNs();
3,000,333✔
281

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

289
    if (terrno == TSDB_CODE_WAL_LOG_NOT_EXIST) {
92!
290
      sNTrace(pData->pSyncNode, "wal read not exist, index:%" PRId64 ", err:0x%x, msg:%s, syserr:%d, sysmsg:%s", index,
92!
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));
92✔
304

305
    TAOS_RETURN(code);
92✔
306
  }
307

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

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

325
  (void)taosThreadMutexUnlock(&(pData->mutex));
3,000,188✔
326
  int64_t ts4 = taosGetTimestampNs();
3,000,230✔
327

328
  int64_t tsElapsed = ts4 - ts1;
3,000,230✔
329
  int64_t tsElapsedLock = ts2 - ts1;
3,000,230✔
330
  int64_t tsElapsedRead = ts3 - ts2;
3,000,230✔
331
  int64_t tsElapsedBuild = ts4 - ts3;
3,000,230✔
332

333
  sNTrace(pData->pSyncNode,
3,000,230!
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);
3,000,230✔
339
}
340

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

346
  int32_t code = walRollback(pWal, fromIndex);
1✔
347
  if (code != 0) {
1!
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);
1!
358

359
  TAOS_RETURN(code);
1✔
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) {
3,917✔
366
  SSyncLogStoreData* pData = pLogStore->data;
3,917✔
367
  SWal*              pWal = pData->pWal;
3,917✔
368
  if (ppLastEntry == NULL) return TSDB_CODE_SYN_INTERNAL_ERROR;
3,917!
369

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

378
    TAOS_RETURN(code);
3,917✔
379
  }
380

381
  TAOS_RETURN(TSDB_CODE_FAILED);
382
}
383

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

388
  // need not update
389
  SyncIndex snapshotVer = walGetSnapshotVer(pWal);
12,417,873✔
390
  SyncIndex walCommitVer = walGetCommittedVer(pWal);
12,417,954✔
391
  SyncIndex wallastVer = walGetLastVer(pWal);
12,417,977✔
392

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

398
  int32_t code = walCommit(pWal, index);
12,418,097✔
399
  if (code != 0) {
12,418,035!
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);
12,418,035✔
411
}
412

413
SyncIndex raftlogCommitIndex(SSyncLogStore* pLogStore) {
11,661✔
414
  SSyncLogStoreData* pData = pLogStore->data;
11,661✔
415
  return pData->pSyncNode->commitIndex;
11,661✔
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