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

taosdata / TDengine / #4935

22 Jan 2026 06:38AM UTC coverage: 66.708% (+0.02%) from 66.691%
#4935

push

travis-ci

web-flow
merge: from main to 3.0 #34371

121 of 271 new or added lines in 17 files covered. (44.65%)

9066 existing lines in 149 files now uncovered.

203884 of 305637 relevant lines covered (66.71%)

125811266.68 hits per line

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

48.88
/source/dnode/mnode/sdb/src/sdbFile.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 "crypt.h"
18
#include "sdb.h"
19
#include "sync.h"
20
#include "tchecksum.h"
21
#include "tencrypt.h"
22
#include "tglobal.h"
23
#include "tjson.h"
24
#include "wal.h"
25

26
#define SDB_TABLE_SIZE   24
27
#define SDB_RESERVE_SIZE 512
28
#define SDB_FILE_VER     1
29

30
#define SDB_TABLE_SIZE_EXTRA   SDB_MAX
31
#define SDB_RESERVE_SIZE_EXTRA (512 - (SDB_TABLE_SIZE_EXTRA - SDB_TABLE_SIZE) * 2 * sizeof(int64_t))
32

33
// Forward declaration
34
static int32_t sdbWriteFileImp(SSdb *pSdb, int32_t skip_type);
35

36
static int32_t sdbDeployData(SSdb *pSdb) {
280,905✔
37
  int32_t code = 0;
280,905✔
38
  mInfo("start to deploy sdb");
280,905✔
39

40
  for (int32_t i = SDB_MAX - 1; i >= 0; --i) {
13,483,440✔
41
    SdbDeployFp fp = pSdb->deployFps[i];
13,202,535✔
42
    if (fp == NULL) continue;
13,202,535✔
43

44
    mInfo("start to deploy sdb:%s", sdbTableName(i));
1,966,335✔
45
    code = (*fp)(pSdb->pMnode);
1,966,335✔
46
    if (code != 0) {
1,966,335✔
47
      mError("failed to deploy sdb:%s since %s", sdbTableName(i), tstrerror(code));
×
48
      return -1;
×
49
    }
50
  }
51

52
  mInfo("sdb deploy success");
280,905✔
53
  return 0;
280,905✔
54
}
55

56
static int32_t sdbUpgradeData(SSdb *pSdb, int32_t version) {
354,284✔
57
  int32_t code = 0;
354,284✔
58
  mInfo("start to upgrade sdb");
354,284✔
59

60
  for (int32_t i = SDB_MAX - 1; i >= 0; --i) {
17,005,632✔
61
    SdbUpgradeFp fp = pSdb->upgradeFps[i];
16,651,348✔
62
    if (fp == NULL) continue;
16,651,348✔
63

64
    mInfo("start to upgrade sdb:%s", sdbTableName(i));
354,284✔
65
    code = (*fp)(pSdb->pMnode, version);
354,284✔
66
    if (code != 0) {
354,284✔
67
      mError("failed to upgrade sdb:%s since %s", sdbTableName(i), tstrerror(code));
×
68
      return -1;
×
69
    }
70
  }
71

72
  mInfo("sdb upgrade success");
354,284✔
73
  return 0;
354,284✔
74
}
75

76
static int32_t sdbAfterRestoredData(SSdb *pSdb) {
187,341✔
77
  int32_t code = 0;
187,341✔
78
  mInfo("start to prepare sdb");
187,341✔
79

80
  for (int32_t i = SDB_MAX - 1; i >= 0; --i) {
8,992,368✔
81
    SdbAfterRestoredFp fp = pSdb->afterRestoredFps[i];
8,805,027✔
82
    if (fp == NULL) continue;
8,805,027✔
83

84
    mInfo("start to prepare sdb:%s", sdbTableName(i));
187,341✔
85
    code = (*fp)(pSdb->pMnode);
187,341✔
86
    if (code != 0) {
187,341✔
87
      mError("failed to prepare sdb:%s since %s", sdbTableName(i), tstrerror(code));
×
88
      return -1;
×
89
    }
90
  }
91

92
  mInfo("sdb prepare success");
187,341✔
93
  return 0;
187,341✔
94
}
95

96
static void sdbResetData(SSdb *pSdb) {
112,704✔
97
  mInfo("start to reset sdb");
112,704✔
98

99
  for (ESdbType i = 0; i < SDB_MAX; ++i) {
5,409,792✔
100
    SHashObj *hash = pSdb->hashObjs[i];
5,297,088✔
101
    if (hash == NULL) continue;
5,297,088✔
102

103
    sdbWriteLock(pSdb, i);
4,846,272✔
104

105
    SSdbRow **ppRow = taosHashIterate(hash, NULL);
4,846,272✔
106
    while (ppRow != NULL) {
4,846,272✔
107
      SSdbRow *pRow = *ppRow;
×
108
      if (pRow == NULL) continue;
×
109

110
      sdbFreeRow(pSdb, pRow, true);
×
111
      ppRow = taosHashIterate(hash, ppRow);
×
112
    }
113

114
    taosHashClear(pSdb->hashObjs[i]);
4,846,272✔
115
    pSdb->tableVer[i] = 0;
4,846,272✔
116
    pSdb->maxId[i] = 0;
4,846,272✔
117

118
    sdbUnLock(pSdb, i);
4,846,272✔
119

120
    mInfo("sdb:%s is reset", sdbTableName(i));
4,846,272✔
121
  }
122

123
  pSdb->applyIndex = -1;
112,704✔
124
  pSdb->applyTerm = -1;
112,704✔
125
  pSdb->applyConfig = -1;
112,704✔
126
  pSdb->commitIndex = -1;
112,704✔
127
  pSdb->commitTerm = -1;
112,704✔
128
  pSdb->commitConfig = -1;
112,704✔
129
  mInfo("sdb reset success");
112,704✔
130
}
112,704✔
131

132
static int32_t sdbReadFileHead(SSdb *pSdb, TdFilePtr pFile) {
98,666✔
133
  int32_t code = 0;
98,666✔
134
  int64_t sver = 0;
98,666✔
135
  int32_t ret = taosReadFile(pFile, &sver, sizeof(int64_t));
98,666✔
136
  if (ret < 0) {
98,666✔
137
    return terrno;
×
138
  }
139
  if (ret != sizeof(int64_t)) {
98,666✔
140
    code = TSDB_CODE_FILE_CORRUPTED;
×
141
    TAOS_RETURN(code);
×
142
  }
143
  if (sver != SDB_FILE_VER) {
98,666✔
144
    code = TSDB_CODE_FILE_CORRUPTED;
×
145
    TAOS_RETURN(code);
×
146
  }
147

148
  ret = taosReadFile(pFile, &pSdb->applyIndex, sizeof(int64_t));
98,666✔
149
  if (ret < 0) {
98,666✔
150
    return terrno;
×
151
  }
152
  if (ret != sizeof(int64_t)) {
98,666✔
153
    code = TSDB_CODE_FILE_CORRUPTED;
×
154
    TAOS_RETURN(code);
×
155
  }
156

157
  ret = taosReadFile(pFile, &pSdb->applyTerm, sizeof(int64_t));
98,666✔
158
  if (ret < 0) {
98,666✔
159
    return terrno;
×
160
  }
161
  if (ret != sizeof(int64_t)) {
98,666✔
162
    code = TSDB_CODE_FILE_CORRUPTED;
×
163
    TAOS_RETURN(code);
×
164
  }
165

166
  ret = taosReadFile(pFile, &pSdb->applyConfig, sizeof(int64_t));
98,666✔
167
  if (ret < 0) {
98,666✔
168
    return terrno;
×
169
  }
170
  if (ret != sizeof(int64_t)) {
98,666✔
171
    code = TSDB_CODE_FILE_CORRUPTED;
×
172
    TAOS_RETURN(code);
×
173
  }
174

175
  for (int32_t i = 0; i < SDB_TABLE_SIZE; ++i) {
2,466,650✔
176
    int64_t maxId = 0;
2,367,984✔
177
    ret = taosReadFile(pFile, &maxId, sizeof(int64_t));
2,367,984✔
178
    if (ret < 0) {
2,367,984✔
179
      return terrno;
×
180
    }
181
    if (ret != sizeof(int64_t)) {
2,367,984✔
182
      code = TSDB_CODE_FILE_CORRUPTED;
×
183
      TAOS_RETURN(code);
×
184
    }
185
    if (i < SDB_MAX) {
2,367,984✔
186
      pSdb->maxId[i] = maxId;
2,367,984✔
187
    }
188
  }
189

190
  for (int32_t i = 0; i < SDB_TABLE_SIZE; ++i) {
2,466,650✔
191
    int64_t ver = 0;
2,367,984✔
192
    ret = taosReadFile(pFile, &ver, sizeof(int64_t));
2,367,984✔
193
    if (ret < 0) {
2,367,984✔
194
      return terrno;
×
195
    }
196
    if (ret != sizeof(int64_t)) {
2,367,984✔
197
      code = TSDB_CODE_FILE_CORRUPTED;
×
198
      TAOS_RETURN(code);
×
199
    }
200
    if (i < SDB_MAX) {
2,367,984✔
201
      pSdb->tableVer[i] = ver;
2,367,984✔
202
    }
203
  }
204

205
  // for sdb compatibility
206
  for (int32_t i = SDB_TABLE_SIZE; i < SDB_TABLE_SIZE_EXTRA; ++i) {
2,367,984✔
207
    int64_t maxId = 0;
2,269,318✔
208
    ret = taosReadFile(pFile, &maxId, sizeof(int64_t));
2,269,318✔
209
    if (ret < 0) {
2,269,318✔
210
      code = TAOS_SYSTEM_ERROR(ERRNO);
×
211
      TAOS_RETURN(code);
×
212
    }
213
    if (ret != sizeof(int64_t)) {
2,269,318✔
214
      code = TSDB_CODE_FILE_CORRUPTED;
×
215
      TAOS_RETURN(code);
×
216
    }
217
    if (i < SDB_MAX) {
2,269,318✔
218
      pSdb->maxId[i] = maxId;
2,269,318✔
219
    }
220

221
    int64_t ver = 0;
2,269,318✔
222
    ret = taosReadFile(pFile, &ver, sizeof(int64_t));
2,269,318✔
223
    if (ret < 0) {
2,269,318✔
224
      code = TAOS_SYSTEM_ERROR(ERRNO);
×
225
      TAOS_RETURN(code);
×
226
    }
227
    if (ret != sizeof(int64_t)) {
2,269,318✔
228
      code = TSDB_CODE_FILE_CORRUPTED;
×
229
      TAOS_RETURN(code);
×
230
    }
231
    if (i < SDB_MAX) {
2,269,318✔
232
      pSdb->tableVer[i] = ver;
2,269,318✔
233
    }
234
  }
235

236
  char reserve[SDB_RESERVE_SIZE_EXTRA] = {0};
98,666✔
237
  ret = taosReadFile(pFile, reserve, sizeof(reserve));
98,666✔
238
  if (ret < 0) {
98,666✔
239
    return terrno;
×
240
  }
241
  if (ret != sizeof(reserve)) {
98,666✔
242
    code = TSDB_CODE_FILE_CORRUPTED;
×
243
    TAOS_RETURN(code);
×
244
  }
245

246
  return 0;
98,666✔
247
}
248

249
static int32_t sdbWriteFileHead(SSdb *pSdb, TdFilePtr pFile) {
754,477✔
250
  int64_t sver = SDB_FILE_VER;
754,477✔
251
  if (taosWriteFile(pFile, &sver, sizeof(int64_t)) != sizeof(int64_t)) {
754,477✔
252
    return terrno;
×
253
  }
254

255
  mInfo("vgId:1, write sdb file with sdb applyIndex:%" PRId64 " term:%" PRId64 " config:%" PRId64, pSdb->applyIndex,
754,477✔
256
        pSdb->applyTerm, pSdb->applyConfig);
257
  if (taosWriteFile(pFile, &pSdb->applyIndex, sizeof(int64_t)) != sizeof(int64_t)) {
754,477✔
258
    return terrno;
×
259
  }
260

261
  if (taosWriteFile(pFile, &pSdb->applyTerm, sizeof(int64_t)) != sizeof(int64_t)) {
754,477✔
262
    return terrno;
×
263
  }
264

265
  if (taosWriteFile(pFile, &pSdb->applyConfig, sizeof(int64_t)) != sizeof(int64_t)) {
754,477✔
266
    return terrno;
×
267
  }
268

269
  for (int32_t i = 0; i < SDB_TABLE_SIZE; ++i) {
18,861,925✔
270
    int64_t maxId = 0;
18,107,448✔
271
    if (i < SDB_MAX) {
18,107,448✔
272
      maxId = pSdb->maxId[i];
18,107,448✔
273
    }
274
    if (taosWriteFile(pFile, &maxId, sizeof(int64_t)) != sizeof(int64_t)) {
18,107,448✔
275
      return terrno;
×
276
    }
277
  }
278

279
  for (int32_t i = 0; i < SDB_TABLE_SIZE; ++i) {
18,861,925✔
280
    int64_t ver = 0;
18,107,448✔
281
    if (i < SDB_MAX) {
18,107,448✔
282
      ver = pSdb->tableVer[i];
18,107,448✔
283
    }
284
    if (taosWriteFile(pFile, &ver, sizeof(int64_t)) != sizeof(int64_t)) {
18,107,448✔
285
      return terrno;
×
286
    }
287
  }
288

289
  // for sdb compatibility
290
  for (int32_t i = SDB_TABLE_SIZE; i < SDB_TABLE_SIZE_EXTRA; ++i) {
18,107,448✔
291
    int64_t maxId = 0;
17,352,971✔
292
    if (i < SDB_MAX) {
17,352,971✔
293
      maxId = pSdb->maxId[i];
17,352,971✔
294
    }
295
    if (taosWriteFile(pFile, &maxId, sizeof(int64_t)) != sizeof(int64_t)) {
17,352,971✔
296
      return terrno;
×
297
    }
298

299
    int64_t ver = 0;
17,352,971✔
300
    if (i < SDB_MAX) {
17,352,971✔
301
      ver = pSdb->tableVer[i];
17,352,971✔
302
    }
303
    if (taosWriteFile(pFile, &ver, sizeof(int64_t)) != sizeof(int64_t)) {
17,352,971✔
304
      return terrno;
×
305
    }
306
  }
307

308
  char reserve[SDB_RESERVE_SIZE_EXTRA] = {0};
754,477✔
309
  if (taosWriteFile(pFile, reserve, sizeof(reserve)) != sizeof(reserve)) {
754,477✔
310
    return terrno;
×
311
  }
312

313
  return 0;
754,477✔
314
}
315

316
// Read encrypted flag from mnode.json for defensive check
317
// This is more reliable than heuristics when receiving snapshots
NEW
318
static bool sdbReadEncryptedFlagFromMnodeJson(SSdb *pSdb) {
×
NEW
319
  if (pSdb->mnodePath[0] == '\0') {
×
320
    // No mnode path configured, use heuristic
NEW
321
    return (tsMetaKey[0] != '\0');
×
322
  }
323

324
  // Read the encrypted flag from mnode.json
NEW
325
  char file[PATH_MAX] = {0};
×
NEW
326
  snprintf(file, sizeof(file), "%s%smnode.json", pSdb->mnodePath, TD_DIRSEP);
×
327

328
  // Try to read mnode.json
NEW
329
  char   *pData = NULL;
×
NEW
330
  int32_t dataLen = 0;
×
NEW
331
  if (taosReadCfgFile(file, &pData, &dataLen) != 0) {
×
332
    // Cannot read, use heuristic
NEW
333
    return (tsMetaKey[0] != '\0');
×
334
  }
335

NEW
336
  SJson *pJson = tjsonParse(pData);
×
NEW
337
  taosMemoryFree(pData);
×
338

NEW
339
  if (pJson == NULL) {
×
NEW
340
    return (tsMetaKey[0] != '\0');
×
341
  }
342

343
  // Read encrypted field (default to false if not found)
NEW
344
  int32_t encrypted = 0;
×
NEW
345
  int32_t code = 0;
×
NEW
346
  tjsonGetInt32ValueFromDouble(pJson, "encrypted", encrypted, code);
×
NEW
347
  cJSON_Delete(pJson);
×
348

NEW
349
  return (encrypted != 0);
×
350
}
351

352
static int32_t sdbReadFileImp(SSdb *pSdb) {
112,704✔
353
  int64_t offset = 0;
112,704✔
354
  int32_t code = 0;
112,704✔
355
  int32_t readLen = 0;
112,704✔
356
  int64_t ret = 0;
112,704✔
357
  char    file[PATH_MAX] = {0};
112,704✔
358
  int32_t bufLen = TSDB_MAX_MSG_SIZE;
112,704✔
359
  bool    needMigration = false;
112,704✔
360

361
  snprintf(file, sizeof(file), "%s%ssdb.data", pSdb->currDir, TD_DIRSEP);
112,704✔
362

363
  // Wait for encryption keys to be loaded
364
  code = taosWaitCfgKeyLoaded();
112,704✔
365
  if (code != 0) {
112,704✔
NEW
366
    mError("failed to wait for encryption keys since %s", tstrerror(code));
×
NEW
367
    return code;
×
368
  }
369

370
  // Check if sdb.data needs encryption migration
371
  // This info is stored in mnode.json encrypted flag
372
  bool isEncrypted = pSdb->encrypted;
112,704✔
373
  if (!isEncrypted && tsMetaKey[0] != '\0') {
112,704✔
NEW
374
    mInfo("sdb.data not encrypted but tsMetaKey available, checking encrypted flag from mnode.json");
×
NEW
375
    isEncrypted = sdbReadEncryptedFlagFromMnodeJson(pSdb);  // Fix: don't redeclare, just assign
×
NEW
376
    if (isEncrypted) {
×
NEW
377
      mInfo("reload encrypted flag from mnode.json is true, will not migrate after reading");
×
NEW
378
      needMigration = false;
×
379
    } else {
NEW
380
      mInfo("reload encrypted flag from mnode.json is false, will migrate after reading");
×
NEW
381
      needMigration = true;
×
382
    }
383
  }
384

385
  pSdb->encrypted = isEncrypted;
112,704✔
386

387
  mInfo("start to read sdb file:%s, encrypted:%d, needMigration:%d", file, isEncrypted, needMigration);
112,704✔
388

389
  SSdbRaw *pRaw = taosMemoryMalloc(bufLen + 100);
112,704✔
390
  if (pRaw == NULL) {
112,704✔
391
    code = terrno;
×
392
    mError("failed read sdb file since %s", tstrerror(code));
×
393
    TAOS_RETURN(code);
×
394
  }
395

396
  TdFilePtr pFile = taosOpenFile(file, TD_FILE_READ);
112,704✔
397
  if (pFile == NULL) {
112,704✔
398
    taosMemoryFree(pRaw);
14,038✔
399
    code = terrno;
14,038✔
400
    mInfo("read sdb file:%s finished since %s", file, tstrerror(code));
14,038✔
401
    return 0;
14,038✔
402
  }
403

404
  code = sdbReadFileHead(pSdb, pFile);
98,666✔
405
  if (code != 0) {
98,666✔
406
    mError("failed to read sdb file:%s head since %s", file, tstrerror(code));
×
407
    taosMemoryFree(pRaw);
×
408
    int32_t ret = 0;
×
409
    if ((ret = taosCloseFile(&pFile)) != 0) {
×
410
      mError("failed to close sdb file:%s since %s", file, tstrerror(ret));
×
411
    }
412
    return code;
×
413
  }
414

415
  int64_t tableVer[SDB_MAX] = {0};
98,666✔
416
  memcpy(tableVer, pSdb->tableVer, sizeof(tableVer));
98,666✔
417

418
  while (1) {
14,285,801✔
419
    readLen = sizeof(SSdbRaw);
14,384,467✔
420
    ret = taosReadFile(pFile, pRaw, readLen);
14,384,467✔
421
    if (ret == 0) break;
14,384,467✔
422

423
    if (ret < 0) {
14,285,801✔
424
      code = terrno;
×
425
      mError("failed to read sdb file:%s since %s", file, tstrerror(code));
×
426
      goto _OVER;
×
427
    }
428

429
    if (ret != readLen) {
14,285,801✔
430
      code = TSDB_CODE_FILE_CORRUPTED;
×
431
      mError("failed to read sdb file:%s since %s, ret:%" PRId64 " != readLen:%d", file, tstrerror(code), ret, readLen);
×
432
      goto _OVER;
×
433
    }
434

435
    readLen = pRaw->dataLen + sizeof(int32_t);
14,285,801✔
436
    if (tsMetaKey[0] != '\0' && !needMigration) {
14,285,801✔
437
      readLen = ENCRYPTED_LEN(pRaw->dataLen) + sizeof(int32_t);
197,910✔
438
    }
439
    if (readLen >= bufLen) {
14,285,801✔
440
      bufLen = pRaw->dataLen * 2;
×
441
      SSdbRaw *pNewRaw = taosMemoryMalloc(bufLen + 100);
×
442
      if (pNewRaw == NULL) {
×
443
        code = terrno;
×
444
        mError("failed read sdb file since malloc new sdbRaw size:%d failed", bufLen);
×
445
        goto _OVER;
×
446
      }
447
      mInfo("malloc new sdb raw size:%d, type:%d", bufLen, pRaw->type);
×
448
      memcpy(pNewRaw, pRaw, sizeof(SSdbRaw));
×
449
      sdbFreeRaw(pRaw);
×
450
      pRaw = pNewRaw;
×
451
    }
452

453
    ret = taosReadFile(pFile, pRaw->pData, readLen);
14,285,801✔
454
    if (ret < 0) {
14,285,801✔
455
      code = terrno;
×
456
      mError("failed to read sdb file:%s since %s, ret:%" PRId64 " readLen:%d", file, tstrerror(code), ret, readLen);
×
457
      goto _OVER;
×
458
    }
459

460
    if (ret != readLen) {
14,285,801✔
461
      code = TSDB_CODE_FILE_CORRUPTED;
×
462
      mError("failed to read sdb file:%s since %s, ret:%" PRId64 " != readLen:%d", file, tstrerror(code), ret, readLen);
×
463
      goto _OVER;
×
464
    }
465

466
    if (taosWaitCfgKeyLoaded() != 0) {
14,285,801✔
467
      code = terrno;
×
468
      goto _OVER;
×
469
    }
470

471
    if (tsMetaKey[0] != '\0' && !needMigration) {
14,285,801✔
472
      int32_t count = 0;
197,910✔
473

474
      char *plantContent = taosMemoryMalloc(ENCRYPTED_LEN(pRaw->dataLen));
197,910✔
475
      if (plantContent == NULL) {
197,910✔
476
        code = terrno;
×
477
        goto _OVER;
×
478
      }
479

480
      SCryptOpts opts = {0};
197,910✔
481
      opts.len = ENCRYPTED_LEN(pRaw->dataLen);
197,910✔
482
      opts.source = pRaw->pData;
197,910✔
483
      opts.result = plantContent;
197,910✔
484
      opts.unitLen = 16;
197,910✔
485
      opts.pOsslAlgrName = taosGetEncryptAlgoName(tsEncryptAlgorithmType);
197,910✔
486
      tstrncpy(opts.key, tsMetaKey, ENCRYPT_KEY_LEN + 1);
197,910✔
487

488
      count = CBC_Decrypt(&opts);
197,910✔
489
      if (count <= 0) {
197,910✔
490
        code = terrno;
×
491
        goto _OVER;
×
492
      }
493

494
      // mDebug("read sdb, CBC Decrypt dataLen:%d, descrypted len:%d, %s", pRaw->dataLen, count, __FUNCTION__);
495

496
      memcpy(pRaw->pData, plantContent, pRaw->dataLen);
197,910✔
497
      taosMemoryFree(plantContent);
197,910✔
498
      memcpy(pRaw->pData + pRaw->dataLen, &pRaw->pData[ENCRYPTED_LEN(pRaw->dataLen)], sizeof(int32_t));
197,910✔
499
    }
500

501
    int32_t totalLen = sizeof(SSdbRaw) + pRaw->dataLen + sizeof(int32_t);
14,285,801✔
502
    if ((!taosCheckChecksumWhole((const uint8_t *)pRaw, totalLen)) != 0) {
28,571,602✔
503
      code = TSDB_CODE_CHECKSUM_ERROR;
×
504
      mError("failed to read sdb file:%s since %s, readLen:%d", file, tstrerror(code), readLen);
×
505
      goto _OVER;
×
506
    }
507

508
    if (pRaw->type >= SDB_MAX) {
14,285,801✔
509
      mInfo("skip sdb raw type:%d since it is not supported", pRaw->type);
×
510
      continue;
×
511
    }
512

513
    code = sdbWriteWithoutFree(pSdb, pRaw);
14,285,801✔
514
    if (code != 0) {
14,285,801✔
515
      mError("failed to exec sdbWrite while read sdb file:%s since %s", file, terrstr());
×
516
      goto _OVER;
×
517
    }
518
  }
519

520
  code = 0;
98,666✔
521
  pSdb->commitIndex = pSdb->applyIndex;
98,666✔
522
  pSdb->commitTerm = pSdb->applyTerm;
98,666✔
523
  pSdb->commitConfig = pSdb->applyConfig;
98,666✔
524
  memcpy(pSdb->tableVer, tableVer, sizeof(tableVer));
98,666✔
525
  mInfo("vgId:1, trans:0, read sdb file:%s success, commit index:%" PRId64 " term:%" PRId64 " config:%" PRId64, file,
98,666✔
526
        pSdb->commitIndex, pSdb->commitTerm, pSdb->commitConfig);
527

528
_OVER:
98,600✔
529
  if ((ret = taosCloseFile(&pFile)) != 0) {
98,666✔
530
    mError("failed to close sdb file:%s since %s", file, tstrerror(ret));
×
531
  }
532
  sdbFreeRaw(pRaw);
98,666✔
533

534
  TAOS_RETURN(code);
98,666✔
535
}
536

537
int32_t sdbReadFile(SSdb *pSdb) {
112,704✔
538
  (void)taosThreadMutexLock(&pSdb->filelock);
112,704✔
539

540
  sdbResetData(pSdb);
112,704✔
541
  int32_t code = sdbReadFileImp(pSdb);
112,704✔
542
  if (code != 0) {
112,704✔
543
    mError("failed to read sdb file since %s", tstrerror(code));
×
544
    sdbResetData(pSdb);
×
NEW
545
    return code;
×
546
  }
547

548
  // Check if sdb.data needs encryption migration
549
  bool isEncrypted = pSdb->encrypted;
112,704✔
550
  if (!isEncrypted && tsMetaKey[0] != '\0') {
112,704✔
NEW
551
    mInfo("sdb.data read successfully, now migrating to encrypted format");
×
552

553
    // Rewrite sdb.data with encryption (atomic operation)
NEW
554
    code = sdbWriteFileImp(pSdb, -1);
×
NEW
555
    if (code != 0) {
×
NEW
556
      mError("failed to migrate sdb.data to encrypted format since %s", tstrerror(code));
×
557
      // Don't fail the read, continue
NEW
558
      (void)taosThreadMutexUnlock(&pSdb->filelock);
×
NEW
559
      return code;
×
560
    }
561

562
    // Update encrypted flag and persist to mnode.json immediately (atomic operation)
NEW
563
    pSdb->encrypted = true;
×
564

NEW
565
    if (pSdb->persistEncryptedFlagFp != NULL && pSdb->pMnodeForCallback != NULL) {
×
NEW
566
      code = pSdb->persistEncryptedFlagFp(pSdb->pMnodeForCallback);
×
NEW
567
      if (code == 0) {
×
NEW
568
        mInfo("sdb.data migrated to encrypted format and persisted encrypted flag to mnode.json successfully");
×
569
      } else {
NEW
570
        mError("failed to persist encrypted flag to mnode.json since %s", tstrerror(code));
×
571
        // Rollback encrypted flag
NEW
572
        pSdb->encrypted = false;
×
573
      }
574
    } else {
NEW
575
      mWarn("sdb.data migrated to encrypted format but no callback to persist flag");
×
576
    }
577
  }
578

579
  (void)taosThreadMutexUnlock(&pSdb->filelock);
112,704✔
580
  return code;
112,704✔
581
}
582

583
int32_t sdbWriteFileImp(SSdb *pSdb, int32_t skip_type) {
754,477✔
584
  int32_t code = 0;
754,477✔
585

586
  char tmpfile[PATH_MAX] = {0};
754,477✔
587
  snprintf(tmpfile, sizeof(tmpfile), "%s%ssdb.data", pSdb->tmpDir, TD_DIRSEP);
754,477✔
588
  char curfile[PATH_MAX] = {0};
754,477✔
589
  snprintf(curfile, sizeof(curfile), "%s%ssdb.data", pSdb->currDir, TD_DIRSEP);
754,477✔
590

591
  mInfo("start to write sdb file, apply index:%" PRId64 " term:%" PRId64 " config:%" PRId64 ", commit index:%" PRId64
754,477✔
592
        " term:%" PRId64 " config:%" PRId64 ", file:%s",
593
        pSdb->applyIndex, pSdb->applyTerm, pSdb->applyConfig, pSdb->commitIndex, pSdb->commitTerm, pSdb->commitConfig,
594
        curfile);
595

596
  TdFilePtr pFile = taosOpenFile(tmpfile, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
754,477✔
597
  if (pFile == NULL) {
754,477✔
598
    code = terrno;
×
599
    mError("failed to open sdb file:%s for write since %s", tmpfile, tstrerror(code));
×
600
    TAOS_RETURN(code);
×
601
  }
602

603
  code = sdbWriteFileHead(pSdb, pFile);
754,477✔
604
  if (code != 0) {
754,477✔
605
    mError("failed to write sdb file:%s head since %s", tmpfile, tstrerror(code));
×
606
    int32_t ret = 0;
×
607
    if ((ret = taosCloseFile(&pFile)) != 0) {
×
608
      mError("failed to close sdb file:%s since %s", tmpfile, tstrerror(ret));
×
609
    }
610
    return code;
×
611
  }
612

613
  for (int32_t i = SDB_MAX - 1; i >= 0; --i) {
36,214,896✔
614
    if (i == skip_type) continue;
35,460,419✔
615
    SdbEncodeFp encodeFp = pSdb->encodeFps[i];
35,460,419✔
616
    if (encodeFp == NULL) continue;
35,460,419✔
617

618
    mInfo("write %s to sdb file, total %d rows", sdbTableName(i), sdbGetSize(pSdb, i));
31,688,034✔
619

620
    SHashObj *hash = pSdb->hashObjs[i];
31,688,034✔
621
    sdbWriteLock(pSdb, i);
31,688,034✔
622

623
    SSdbRow **ppRow = taosHashIterate(hash, NULL);
31,688,034✔
624
    while (ppRow != NULL) {
136,106,460✔
625
      SSdbRow *pRow = *ppRow;
104,418,426✔
626
      if (pRow == NULL) {
104,418,426✔
627
        ppRow = taosHashIterate(hash, ppRow);
×
628
        continue;
×
629
      }
630

631
      if (pRow->status != SDB_STATUS_READY && pRow->status != SDB_STATUS_DROPPING) {
104,418,426✔
632
        sdbPrintOper(pSdb, pRow, "not-write");
27,645✔
633
        ppRow = taosHashIterate(hash, ppRow);
27,645✔
634
        continue;
27,645✔
635
      }
636

637
      sdbPrintOper(pSdb, pRow, "write");
104,390,781✔
638

639
      SSdbRaw *pRaw = (*encodeFp)(pRow->pObj);
104,390,781✔
640
      if (pRaw != NULL) {
104,390,781✔
641
        pRaw->status = pRow->status;
104,390,781✔
642

643
        if (taosWriteFile(pFile, pRaw, sizeof(SSdbRaw)) != sizeof(SSdbRaw)) {
104,390,781✔
644
          code = terrno;
×
645
          taosHashCancelIterate(hash, ppRow);
×
646
          sdbFreeRaw(pRaw);
×
647
          break;
×
648
        }
649

650
        int32_t newDataLen = pRaw->dataLen;
104,390,781✔
651
        char   *newData = pRaw->pData;
104,390,781✔
652

653
        if (taosWaitCfgKeyLoaded() != 0) {
104,390,781✔
654
          code = terrno;
×
655
          taosHashCancelIterate(hash, ppRow);
×
656
          sdbFreeRaw(pRaw);
×
657
          break;
×
658
        }
659

660
        if (tsMetaKey[0] != '\0') {
104,390,781✔
661
          newDataLen = ENCRYPTED_LEN(pRaw->dataLen);
616,458✔
662
          newData = taosMemoryMalloc(newDataLen);
616,458✔
663
          if (newData == NULL) {
616,458✔
664
            code = terrno;
×
665
            taosHashCancelIterate(hash, ppRow);
×
666
            sdbFreeRaw(pRaw);
×
667
            break;
×
668
          }
669

670
          SCryptOpts opts = {0};
616,458✔
671
          opts.len = newDataLen;
616,458✔
672
          opts.source = pRaw->pData;
616,458✔
673
          opts.result = newData;
616,458✔
674
          opts.unitLen = 16;
616,458✔
675
          opts.pOsslAlgrName = taosGetEncryptAlgoName(tsEncryptAlgorithmType);
616,458✔
676
          tstrncpy(opts.key, tsMetaKey, ENCRYPT_KEY_LEN + 1);
616,458✔
677

678
          int32_t count = CBC_Encrypt(&opts);
616,458✔
679
          if (count <= 0) {
616,458✔
680
            code = terrno;
×
681
            taosHashCancelIterate(hash, ppRow);
×
682
            sdbFreeRaw(pRaw);
×
683
            break;
×
684
          }
685

686
          // mDebug("write sdb, CBC Encrypt encryptedDataLen:%d, dataLen:%d, %s",
687
          //       newDataLen, pRaw->dataLen, __FUNCTION__);
688
        }
689

690
        if (taosWriteFile(pFile, newData, newDataLen) != newDataLen) {
104,390,781✔
691
          code = terrno;
×
692
          taosHashCancelIterate(hash, ppRow);
×
693
          sdbFreeRaw(pRaw);
×
694
          break;
×
695
        }
696

697
        if (tsMetaKey[0] != '\0') {
104,390,781✔
698
          taosMemoryFree(newData);
616,458✔
699
        }
700

701
        int32_t cksum = taosCalcChecksum(0, (const uint8_t *)pRaw, sizeof(SSdbRaw) + pRaw->dataLen);
104,390,781✔
702
        if (taosWriteFile(pFile, &cksum, sizeof(int32_t)) != sizeof(int32_t)) {
104,390,781✔
703
          code = terrno;
×
704
          taosHashCancelIterate(hash, ppRow);
×
705
          sdbFreeRaw(pRaw);
×
706
          break;
×
707
        }
708
      } else {
709
        code = TSDB_CODE_APP_ERROR;
×
710
        taosHashCancelIterate(hash, ppRow);
×
711
        break;
×
712
      }
713

714
      sdbFreeRaw(pRaw);
104,390,781✔
715
      ppRow = taosHashIterate(hash, ppRow);
104,390,781✔
716
    }
717
    sdbUnLock(pSdb, i);
31,688,034✔
718
  }
719

720
  if (code == 0) {
754,477✔
721
    code = taosFsyncFile(pFile);
754,477✔
722
    if (code != 0) {
754,477✔
723
      code = TAOS_SYSTEM_ERROR(ERRNO);
×
724
      mError("failed to sync sdb file:%s since %s", tmpfile, tstrerror(code));
×
725
    }
726
  }
727

728
  if (taosCloseFile(&pFile) != 0) {
754,477✔
729
    code = taosRenameFile(tmpfile, curfile);
×
730
  }
731

732
  if (code == 0) {
754,477✔
733
    code = taosRenameFile(tmpfile, curfile);
754,477✔
734
    if (code != 0) {
754,477✔
735
      mError("failed to write sdb file:%s since %s", curfile, tstrerror(code));
×
736
    }
737
  }
738

739
  if (code != 0) {
754,477✔
740
    mError("failed to write sdb file:%s since %s", curfile, tstrerror(code));
×
741
  } else {
742
    pSdb->commitIndex = pSdb->applyIndex;
754,477✔
743
    pSdb->commitTerm = pSdb->applyTerm;
754,477✔
744
    pSdb->commitConfig = pSdb->applyConfig;
754,477✔
745
    mInfo("vgId:1, trans:0, write sdb file success, commit index:%" PRId64 " term:%" PRId64 " config:%" PRId64
754,477✔
746
          " file:%s",
747
          pSdb->commitIndex, pSdb->commitTerm, pSdb->commitConfig, curfile);
748

749
    if (tsMetaKey[0] != '\0') {
754,477✔
750
      if (pSdb->persistEncryptedFlagFp != NULL && pSdb->pMnodeForCallback != NULL) {
4,522✔
751
        code = pSdb->persistEncryptedFlagFp(pSdb->pMnodeForCallback);
4,522✔
752
        if (code == 0) {
4,522✔
753
          mInfo("sdb.data now is encrypted format and persisted encrypted flag to mnode.json successfully");
4,522✔
754
          pSdb->encrypted = true;
4,522✔
755
        } else {
NEW
756
          mError("failed to persist encrypted flag to mnode.json since %s", tstrerror(code));
×
NEW
757
          return code;
×
758
        }
759
      } else {
NEW
760
        mWarn("sdb.data written in encrypted format but no callback to persist flag");
×
NEW
761
        pSdb->encrypted = true;
×
762
      }
763
    }
764
  }
765

766
  terrno = code;
754,477✔
767
  return code;
754,477✔
768
}
769

770
int32_t sdbWriteFile(SSdb *pSdb, int32_t delta) {
38,031,916✔
771
  int32_t code = 0;
38,031,916✔
772
  if (pSdb->applyIndex == pSdb->commitIndex) {
38,031,916✔
773
    return 0;
741,149✔
774
  }
775

776
  if (pSdb->applyIndex - pSdb->commitIndex < delta) {
37,290,767✔
777
    return 0;
36,532,475✔
778
  }
779

780
  (void)taosThreadMutexLock(&pSdb->filelock);
758,292✔
781
  if (pSdb->pWal != NULL) {
758,292✔
782
    if (pSdb->sync > 0) {
758,292✔
783
      code = syncBeginSnapshot(pSdb->sync, pSdb->applyIndex);
758,292✔
784
    }
785
  }
786
  if (code == 0) {
758,292✔
787
    code = sdbWriteFileImp(pSdb, -1);
754,477✔
788
  }
789
  if (code == 0) {
758,292✔
790
    if (pSdb->pWal != NULL) {
754,477✔
791
      if (pSdb->sync > 0) {
754,477✔
792
        code = syncEndSnapshot(pSdb->sync, false);
754,477✔
793
      }
794
    }
795
  }
796
  if (code != 0) {
758,292✔
797
    mError("failed to write sdb file since %s", tstrerror(code));
3,815✔
798
  } else {
799
    mInfo("vgId:1, trans:0, write sdb file success, apply index:%" PRId64 ", term:%" PRId64 ", config:%" PRId64,
754,477✔
800
          pSdb->applyIndex, pSdb->applyTerm, pSdb->applyConfig);
801
  }
802
  (void)taosThreadMutexUnlock(&pSdb->filelock);
758,292✔
803

804
  return code;
758,292✔
805
}
806

807
int32_t sdbWriteFileForDump(SSdb *pSdb, int32_t skip_type) {
×
808
  int32_t code = 0;
×
809

810
  code = sdbWriteFileImp(pSdb, skip_type);
×
811

812
  return code;
×
813
}
814

815
int32_t sdbDeploy(SSdb *pSdb) {
280,905✔
816
  int32_t code = 0;
280,905✔
817
  code = sdbDeployData(pSdb);
280,905✔
818
  if (code != 0) {
280,905✔
819
    TAOS_RETURN(code);
×
820
  }
821

822
  code = sdbWriteFile(pSdb, 0);
280,905✔
823
  if (code != 0) {
280,905✔
824
    TAOS_RETURN(code);
×
825
  }
826

827
  return 0;
280,905✔
828
}
829

830
int32_t sdbUpgrade(SSdb *pSdb, int32_t version) {
354,284✔
831
  int32_t code = 0;
354,284✔
832
  code = sdbUpgradeData(pSdb, version);
354,284✔
833
  if (code != 0) {
354,284✔
834
    TAOS_RETURN(code);
×
835
  }
836

837
  code = sdbWriteFile(pSdb, 0);
354,284✔
838
  if (code != 0) {
354,284✔
839
    TAOS_RETURN(code);
×
840
  }
841

842
  return 0;
354,284✔
843
}
844

845
int32_t sdbAfterRestored(SSdb *pSdb) {
187,341✔
846
  int32_t code = 0;
187,341✔
847
  code = sdbAfterRestoredData(pSdb);
187,341✔
848
  if (code != 0) {
187,341✔
849
    TAOS_RETURN(code);
×
850
  }
851
  return 0;
187,341✔
852
}
853

854
static SSdbIter *sdbCreateIter(SSdb *pSdb) {
×
855
  SSdbIter *pIter = taosMemoryCalloc(1, sizeof(SSdbIter));
×
856
  if (pIter == NULL) {
×
857
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
858
    return NULL;
×
859
  }
860

861
  char name[PATH_MAX + 100] = {0};
×
862
  snprintf(name, sizeof(name), "%s%ssdb.data.%" PRIu64, pSdb->tmpDir, TD_DIRSEP, (uint64_t)pIter);
×
863
  pIter->name = taosStrdup(name);
×
864
  if (pIter->name == NULL) {
×
865
    taosMemoryFree(pIter);
×
866
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
867
    return NULL;
×
868
  }
869

870
  return pIter;
×
871
}
872

873
static void sdbCloseIter(SSdbIter *pIter) {
×
874
  if (pIter == NULL) return;
×
875

876
  if (pIter->file != NULL) {
×
877
    int32_t ret = 0;
×
878
    if ((ret = taosCloseFile(&pIter->file)) != 0) {
×
879
      mError("failed to close sdb file since %s", tstrerror(ret));
×
880
    }
881
    pIter->file = NULL;
×
882
  }
883

884
  if (pIter->name != NULL) {
×
885
    int32_t ret = 0;
×
886
    if ((ret = taosRemoveFile(pIter->name)) != 0) {
×
887
      mError("failed to remove sdb file:%s since %s", pIter->name, tstrerror(ret));
×
888
    }
889
    taosMemoryFree(pIter->name);
×
890
    pIter->name = NULL;
×
891
  }
892

893
  mInfo("sdbiter:%p, is closed, total:%" PRId64, pIter, pIter->total);
×
894
  taosMemoryFree(pIter);
×
895
}
896

897
int32_t sdbStartRead(SSdb *pSdb, SSdbIter **ppIter, int64_t *index, int64_t *term, int64_t *config) {
×
898
  int32_t   code = 0;
×
899
  SSdbIter *pIter = sdbCreateIter(pSdb);
×
900
  if (pIter == NULL) return -1;
×
901

902
  char datafile[PATH_MAX] = {0};
×
903
  snprintf(datafile, sizeof(datafile), "%s%ssdb.data", pSdb->currDir, TD_DIRSEP);
×
904

905
  (void)taosThreadMutexLock(&pSdb->filelock);
×
906
  int64_t commitIndex = pSdb->commitIndex;
×
907
  int64_t commitTerm = pSdb->commitTerm;
×
908
  int64_t commitConfig = pSdb->commitConfig;
×
909
  if (taosCopyFile(datafile, pIter->name) < 0) {
×
910
    code = terrno;
×
911
    (void)taosThreadMutexUnlock(&pSdb->filelock);
×
912
    mError("failed to copy sdb file %s to %s since %s", datafile, pIter->name, tstrerror(code));
×
913
    sdbCloseIter(pIter);
×
914
    TAOS_RETURN(code);
×
915
  }
916
  (void)taosThreadMutexUnlock(&pSdb->filelock);
×
917

918
  pIter->file = taosOpenFile(pIter->name, TD_FILE_READ);
×
919
  if (pIter->file == NULL) {
×
920
    code = terrno;
×
921
    mError("failed to open sdb file:%s since %s", pIter->name, tstrerror(code));
×
922
    sdbCloseIter(pIter);
×
923
    TAOS_RETURN(code);
×
924
  }
925

926
  *ppIter = pIter;
×
927
  if (index != NULL) *index = commitIndex;
×
928
  if (term != NULL) *term = commitTerm;
×
929
  if (config != NULL) *config = commitConfig;
×
930

931
  mInfo("sdbiter:%p, is created to read snapshot, commit index:%" PRId64 " term:%" PRId64 " config:%" PRId64 " file:%s",
×
932
        pIter, commitIndex, commitTerm, commitConfig, pIter->name);
933
  return 0;
×
934
}
935

936
void sdbStopRead(SSdb *pSdb, SSdbIter *pIter) { sdbCloseIter(pIter); }
×
937

938
int32_t sdbDoRead(SSdb *pSdb, SSdbIter *pIter, void **ppBuf, int32_t *len) {
×
939
  int32_t code = 0;
×
940
  int32_t maxlen = 4096;
×
941
  void   *pBuf = taosMemoryCalloc(1, maxlen);
×
942
  if (pBuf == NULL) {
×
943
    code = terrno;
×
944
    TAOS_RETURN(code);
×
945
  }
946

947
  int32_t readlen = taosReadFile(pIter->file, pBuf, maxlen);
×
948
  if (readlen < 0 || readlen > maxlen) {
×
949
    code = terrno;
×
950
    mError("sdbiter:%p, failed to read snapshot since %s, total:%" PRId64, pIter, tstrerror(code), pIter->total);
×
951
    *ppBuf = NULL;
×
952
    *len = 0;
×
953
    taosMemoryFree(pBuf);
×
954
    TAOS_RETURN(code);
×
955
  } else if (readlen == 0) {
×
956
    mInfo("sdbiter:%p, read snapshot to the end, total:%" PRId64, pIter, pIter->total);
×
957
    *ppBuf = NULL;
×
958
    *len = 0;
×
959
    taosMemoryFree(pBuf);
×
960
    return 0;
×
961
  } else {  // (readlen <= maxlen)
962
    pIter->total += readlen;
×
963
    mInfo("sdbiter:%p, read:%d bytes from snapshot, total:%" PRId64, pIter, readlen, pIter->total);
×
964
    *ppBuf = pBuf;
×
965
    *len = readlen;
×
966
    return 0;
×
967
  }
968
}
969

970
int32_t sdbStartWrite(SSdb *pSdb, SSdbIter **ppIter) {
×
971
  int32_t   code = 0;
×
972
  SSdbIter *pIter = sdbCreateIter(pSdb);
×
973
  if (pIter == NULL) {
×
974
    code = terrno;
×
975
    TAOS_RETURN(code);
×
976
  }
977

978
  pIter->file = taosOpenFile(pIter->name, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
×
979
  if (pIter->file == NULL) {
×
980
    code = terrno;
×
981
    mError("failed to open %s since %s", pIter->name, tstrerror(code));
×
982
    sdbCloseIter(pIter);
×
983
    TAOS_RETURN(code);
×
984
  }
985

986
  *ppIter = pIter;
×
987
  mInfo("sdbiter:%p, is created to write snapshot, file:%s", pIter, pIter->name);
×
988
  return 0;
×
989
}
990

991
int32_t sdbStopWrite(SSdb *pSdb, SSdbIter *pIter, bool isApply, int64_t index, int64_t term, int64_t config) {
×
992
  int32_t code = -1;
×
993

994
  if (!isApply) {
×
995
    mInfo("sdbiter:%p, not apply to sdb", pIter);
×
996
    code = 0;
×
997
    goto _OVER;
×
998
  }
999

1000
  if (taosFsyncFile(pIter->file) != 0) {
×
1001
    code = TAOS_SYSTEM_ERROR(ERRNO);
×
1002
    mError("sdbiter:%p, failed to fasync file %s since %s", pIter, pIter->name, tstrerror(code));
×
1003
    goto _OVER;
×
1004
  }
1005

1006
  if (taosCloseFile(&pIter->file) != 0) {
×
1007
    code = TAOS_SYSTEM_ERROR(ERRNO);
×
1008
    goto _OVER;
×
1009
  }
1010
  pIter->file = NULL;
×
1011

1012
  char datafile[PATH_MAX] = {0};
×
1013
  snprintf(datafile, sizeof(datafile), "%s%ssdb.data", pSdb->currDir, TD_DIRSEP);
×
1014
  code = taosRenameFile(pIter->name, datafile);
×
1015
  if (code != 0) {
×
1016
    mError("sdbiter:%p, failed to rename file %s to %s since %s", pIter, pIter->name, datafile, tstrerror(code));
×
1017
    goto _OVER;
×
1018
  }
1019

1020
  // Defensive check: re-read encrypted flag from mnode.json to ensure consistency
1021
  // This handles edge cases like CREATE MNODE where flag might not have been passed correctly
NEW
1022
  bool mnodeJsonEncrypted = sdbReadEncryptedFlagFromMnodeJson(pSdb);
×
NEW
1023
  if (mnodeJsonEncrypted != pSdb->encrypted) {
×
NEW
1024
    mInfo("sdbiter:%p, sdb encrypted flag mismatch: memory=%d, mnode.json=%d, correcting to %d", pIter, pSdb->encrypted,
×
1025
          mnodeJsonEncrypted, mnodeJsonEncrypted);
NEW
1026
    pSdb->encrypted = mnodeJsonEncrypted;
×
1027
  }
1028

1029
  code = sdbReadFile(pSdb);
×
1030
  if (code != 0) {
×
1031
    mError("sdbiter:%p, failed to read from %s since %s", pIter, datafile, tstrerror(code));
×
1032
    goto _OVER;
×
1033
  }
1034

1035
  if (config > 0) {
×
1036
    pSdb->commitConfig = config;
×
1037
  }
1038
  if (term > 0) {
×
1039
    pSdb->commitTerm = term;
×
1040
  }
1041
  if (index > 0) {
×
1042
    pSdb->commitIndex = index;
×
1043
  }
1044

1045
  mInfo("sdbiter:%p, success applyed to sdb", pIter);
×
1046
  code = 0;
×
1047

1048
_OVER:
×
1049
  sdbCloseIter(pIter);
×
1050
  return code;
×
1051
}
1052

1053
int32_t sdbDoWrite(SSdb *pSdb, SSdbIter *pIter, void *pBuf, int32_t len) {
×
1054
  int32_t code = 0;
×
1055
  int32_t writelen = taosWriteFile(pIter->file, pBuf, len);
×
1056
  if (writelen != len) {
×
1057
    code = terrno;
×
1058
    mError("failed to write len:%d since %s, total:%" PRId64, len, tstrerror(code), pIter->total);
×
1059
    TAOS_RETURN(code);
×
1060
  }
1061

1062
  pIter->total += writelen;
×
1063
  mInfo("sdbiter:%p, write:%d bytes to snapshot, total:%" PRId64, pIter, writelen, pIter->total);
×
1064
  return 0;
×
1065
}
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