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

taosdata / TDengine / #3531

19 Nov 2024 10:42AM UTC coverage: 60.213% (-0.006%) from 60.219%
#3531

push

travis-ci

web-flow
Merge pull request #28777 from taosdata/fix/3.0/TD-32366

fix:TD-32366/stmt add geometry datatype check

118529 of 252344 branches covered (46.97%)

Branch coverage included in aggregate %.

7 of 48 new or added lines in 3 files covered. (14.58%)

2282 existing lines in 115 files now uncovered.

199096 of 275161 relevant lines covered (72.36%)

6067577.83 hits per line

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

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

48
  pLogStore->cacheHit = 0;
14,227✔
49
  pLogStore->cacheMiss = 0;
14,227✔
50

51
  taosLRUCacheSetStrictCapacity(pLogStore->pCache, false);
14,227✔
52

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

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

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

95
  return pLogStore;
14,226✔
96
}
97

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

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

110
    taosMemoryFree(pLogStore->data);
14,226✔
111

112
    taosLRUCacheEraseUnrefEntries(pLogStore->pCache);
14,226✔
113
    taosLRUCacheCleanup(pLogStore->pCache);
14,226✔
114

115
    taosMemoryFree(pLogStore);
14,226✔
116
  }
117
}
14,225✔
118

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

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

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

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

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

169
  return lastVer;
9,658,182✔
170
}
171

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

178
  return lastVer;
1,689✔
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) {
17,260✔
199
  SSyncLogStoreData* pData = pLogStore->data;
17,260✔
200
  SWal*              pWal = pData->pWal;
17,260✔
201
  if (walIsEmpty(pWal)) {
17,260✔
202
    return 0;
12,838✔
203
  } else {
204
    SSyncRaftEntry* pLastEntry;
205
    int32_t         code = raftLogGetLastEntry(pLogStore, &pLastEntry);
4,422✔
206
    if (code == 0 && pLastEntry != NULL) {
4,422!
207
      SyncTerm lastTerm = pLastEntry->term;
4,422✔
208
      taosMemoryFree(pLastEntry);
4,422✔
209
      return lastTerm;
4,422✔
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,316,640✔
220
  SSyncLogStoreData* pData = pLogStore->data;
2,316,640✔
221
  SWal*              pWal = pData->pWal;
2,316,640✔
222

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

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

233
  if (TSDB_CODE_SUCCESS != code) {
2,316,857!
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,316,857✔
246
  if (TSDB_CODE_SUCCESS != code) {
2,316,758!
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,316,758!
252
          TMSG_INFO(pEntry->msgType), TMSG_INFO(pEntry->originalRpcType), tsElapsed);
253
  TAOS_RETURN(TSDB_CODE_SUCCESS);
2,316,764✔
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) {
340,975✔
260
  SSyncLogStoreData* pData = pLogStore->data;
340,975✔
261
  SWal*              pWal = pData->pWal;
340,975✔
262
  int32_t            code = 0;
340,975✔
263

264
  *ppEntry = NULL;
340,975✔
265

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

269
  SWalReader* pWalHandle = pData->pWalHandle;
340,977✔
270
  if (pWalHandle == NULL) {
340,977!
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();
340,971✔
278
  code = walReadVer(pWalHandle, index);
340,971✔
279
  walReadReset(pWalHandle);
340,792✔
280
  int64_t ts3 = taosGetTimestampNs();
341,006✔
281

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

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

305
    TAOS_RETURN(code);
122✔
306
  }
307

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

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

325
  (void)taosThreadMutexUnlock(&(pData->mutex));
340,843✔
326
  int64_t ts4 = taosGetTimestampNs();
340,886✔
327

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

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

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

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

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

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

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

381
  TAOS_RETURN(TSDB_CODE_FAILED);
382
}
383

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

388
  // need not update
389
  SyncIndex snapshotVer = walGetSnapshotVer(pWal);
2,340,414✔
390
  SyncIndex walCommitVer = walGetCommittedVer(pWal);
2,340,414✔
391
  SyncIndex wallastVer = walGetLastVer(pWal);
2,340,504✔
392

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

398
  int32_t code = walCommit(pWal, index);
2,340,554✔
399
  if (code != 0) {
2,340,609!
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,340,609✔
411
}
412

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