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

taosdata / TDengine / #3543

29 Nov 2024 02:58AM UTC coverage: 60.842% (+0.02%) from 60.819%
#3543

push

travis-ci

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

merge: from main to 3.0

120460 of 253224 branches covered (47.57%)

Branch coverage included in aggregate %.

706 of 908 new or added lines in 18 files covered. (77.75%)

2401 existing lines in 137 files now uncovered.

201633 of 276172 relevant lines covered (73.01%)

19045673.23 hits per line

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

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

48
  pLogStore->cacheHit = 0;
15,908✔
49
  pLogStore->cacheMiss = 0;
15,908✔
50

51
  taosLRUCacheSetStrictCapacity(pLogStore->pCache, false);
15,908✔
52

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

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

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

95
  return pLogStore;
15,908✔
96
}
97

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

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

110
    taosMemoryFree(pLogStore->data);
15,907✔
111

112
    taosLRUCacheEraseUnrefEntries(pLogStore->pCache);
15,907✔
113
    taosLRUCacheCleanup(pLogStore->pCache);
15,906✔
114

115
    taosMemoryFree(pLogStore);
15,906✔
116
  }
117
}
15,906✔
118

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

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

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

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

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

169
  return lastVer;
53,300,155✔
170
}
171

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

178
  return lastVer;
1,762✔
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) {
19,148✔
199
  SSyncLogStoreData* pData = pLogStore->data;
19,148✔
200
  SWal*              pWal = pData->pWal;
19,148✔
201
  if (walIsEmpty(pWal)) {
19,148✔
202
    return 0;
14,248✔
203
  } else {
204
    SSyncRaftEntry* pLastEntry;
205
    int32_t         code = raftLogGetLastEntry(pLogStore, &pLastEntry);
4,900✔
206
    if (code == 0 && pLastEntry != NULL) {
4,900!
207
      SyncTerm lastTerm = pLastEntry->term;
4,900✔
208
      taosMemoryFree(pLastEntry);
4,900✔
209
      return lastTerm;
4,900✔
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) {
13,260,322✔
220
  SSyncLogStoreData* pData = pLogStore->data;
13,260,322✔
221
  SWal*              pWal = pData->pWal;
13,260,322✔
222

223
  SWalSyncInfo syncMeta = {0};
13,260,322✔
224
  syncMeta.isWeek = pEntry->isWeak;
13,260,322✔
225
  syncMeta.seqNum = pEntry->seqNum;
13,260,322✔
226
  syncMeta.term = pEntry->term;
13,260,322✔
227

228
  int64_t tsWriteBegin = taosGetTimestampNs();
13,260,579✔
229
  int32_t code = walAppendLog(pWal, pEntry->index, pEntry->originalRpcType, syncMeta, pEntry->data, pEntry->dataLen);
13,260,579✔
230
  int64_t tsWriteEnd = taosGetTimestampNs();
13,260,735✔
231
  int64_t tsElapsed = tsWriteEnd - tsWriteBegin;
13,260,735✔
232

233
  if (TSDB_CODE_SUCCESS != code) {
13,260,735!
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);
13,260,735✔
246
  if (TSDB_CODE_SUCCESS != code) {
13,260,504!
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,
13,260,504!
252
          TMSG_INFO(pEntry->msgType), TMSG_INFO(pEntry->originalRpcType), tsElapsed);
253
  TAOS_RETURN(TSDB_CODE_SUCCESS);
13,260,506✔
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) {
2,742,803✔
260
  SSyncLogStoreData* pData = pLogStore->data;
2,742,803✔
261
  SWal*              pWal = pData->pWal;
2,742,803✔
262
  int32_t            code = 0;
2,742,803✔
263

264
  *ppEntry = NULL;
2,742,803✔
265

266
  int64_t ts1 = taosGetTimestampNs();
2,742,806✔
267
  (void)taosThreadMutexLock(&(pData->mutex));
2,742,806✔
268

269
  SWalReader* pWalHandle = pData->pWalHandle;
2,742,808✔
270
  if (pWalHandle == NULL) {
2,742,808!
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();
2,742,802✔
278
  code = walReadVer(pWalHandle, index);
2,742,802✔
279
  walReadReset(pWalHandle);
2,742,784✔
280
  int64_t ts3 = taosGetTimestampNs();
2,742,806✔
281

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

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

305
    TAOS_RETURN(code);
145✔
306
  }
307

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

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

325
  (void)taosThreadMutexUnlock(&(pData->mutex));
2,742,613✔
326
  int64_t ts4 = taosGetTimestampNs();
2,742,657✔
327

328
  int64_t tsElapsed = ts4 - ts1;
2,742,657✔
329
  int64_t tsElapsedLock = ts2 - ts1;
2,742,657✔
330
  int64_t tsElapsedRead = ts3 - ts2;
2,742,657✔
331
  int64_t tsElapsedBuild = ts4 - ts3;
2,742,657✔
332

333
  sNTrace(pData->pSyncNode,
2,742,657✔
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);
2,742,657✔
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,900✔
366
  SSyncLogStoreData* pData = pLogStore->data;
4,900✔
367
  SWal*              pWal = pData->pWal;
4,900✔
368
  if (ppLastEntry == NULL) return TSDB_CODE_SYN_INTERNAL_ERROR;
4,900!
369

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

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

381
  TAOS_RETURN(TSDB_CODE_FAILED);
382
}
383

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

388
  // need not update
389
  SyncIndex snapshotVer = walGetSnapshotVer(pWal);
13,118,614✔
390
  SyncIndex walCommitVer = walGetCommittedVer(pWal);
13,118,693✔
391
  SyncIndex wallastVer = walGetLastVer(pWal);
13,118,950✔
392

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

398
  int32_t code = walCommit(pWal, index);
13,119,026✔
399
  if (code != 0) {
13,118,859!
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);
13,118,859✔
411
}
412

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