tsdbFile.c 13.2 KB
Newer Older
H
hzcheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
H
TD-353  
Hongze Cheng 已提交
15

H
Hongze Cheng 已提交
16
#include "tsdb.h"
H
TD-353  
Hongze Cheng 已提交
17

H
refact  
Hongze Cheng 已提交
18
static const char *TSDB_FNAME_SUFFIX[] = {
H
Hongze Cheng 已提交
19 20 21
    "head",  // TSDB_FILE_HEAD
    "data",  // TSDB_FILE_DATA
    "last",  // TSDB_FILE_LAST
22 23
    "smad",  // TSDB_FILE_SMAD
    "smal",  // TSDB_FILE_SMAL
H
Hongze Cheng 已提交
24 25
    "",      // TSDB_FILE_MAX
    "meta",  // TSDB_FILE_META
C
Cary Xu 已提交
26 27
    "tsma",  // TSDB_FILE_TSMA
    "rsma",  // TSDB_FILE_RSMA
H
refact  
Hongze Cheng 已提交
28
};
H
TD-353  
Hongze Cheng 已提交
29

C
Cary Xu 已提交
30
const char *TSDB_LEVEL_DNAME[] = {
C
Cary Xu 已提交
31 32 33 34 35 36
    "tsdb",
    "rsma1",
    "rsma2",
};

static void tsdbGetFilename(int vid, int fid, uint32_t ver, TSDB_FILE_T ftype, const char* dname, char *fname);
H
Hongze Cheng 已提交
37
// static int   tsdbRollBackMFile(SMFile *pMFile);
H
Hongze Cheng 已提交
38 39 40 41
static int   tsdbEncodeDFInfo(void **buf, SDFInfo *pInfo);
static void *tsdbDecodeDFInfo(void *buf, SDFInfo *pInfo);
static int   tsdbRollBackDFile(SDFile *pDFile);

H
Hongze Cheng 已提交
42
// ============== Operations on SDFile
S
Shengliang Guan 已提交
43
void tsdbInitDFile(STsdb *pRepo, SDFile *pDFile, SDiskID did, int fid, uint32_t ver, TSDB_FILE_T ftype) {
H
refact  
Hongze Cheng 已提交
44 45
  char fname[TSDB_FILENAME_LEN];

H
Hongze Cheng 已提交
46 47
  TSDB_FILE_SET_STATE(pDFile, TSDB_FILE_STATE_OK);

H
Hongze Cheng 已提交
48
  TSDB_FILE_SET_CLOSED(pDFile);
H
refact  
Hongze Cheng 已提交
49

H
Hongze Cheng 已提交
50 51
  memset(&(pDFile->info), 0, sizeof(pDFile->info));
  pDFile->info.magic = TSDB_FILE_INIT_MAGIC;
52
  pDFile->info.fver = tsdbGetDFSVersion(ftype);
H
refact  
Hongze Cheng 已提交
53

C
Cary Xu 已提交
54
  tsdbGetFilename(REPO_ID(pRepo), fid, ver, ftype, TSDB_LEVEL_DNAME[pRepo->level], fname);
H
Hongze Cheng 已提交
55
  tfsInitFile(REPO_TFS(pRepo), &(pDFile->f), did, fname);
H
Hongze Cheng 已提交
56 57
}

H
Hongze Cheng 已提交
58 59
void tsdbInitDFileEx(SDFile *pDFile, SDFile *pODFile) {
  *pDFile = *pODFile;
H
Hongze Cheng 已提交
60 61 62
  TSDB_FILE_SET_CLOSED(pDFile);
}

H
refact  
Hongze Cheng 已提交
63
int tsdbEncodeSDFile(void **buf, SDFile *pDFile) {
H
Hongze Cheng 已提交
64
  int tlen = 0;
65

H
Hongze Cheng 已提交
66 67
  tlen += tsdbEncodeDFInfo(buf, &(pDFile->info));
  tlen += tfsEncodeFile(buf, &(pDFile->f));
H
TD-34  
hzcheng 已提交
68

H
Hongze Cheng 已提交
69
  return tlen;
H
TD-34  
hzcheng 已提交
70 71
}

S
Shengliang Guan 已提交
72
void *tsdbDecodeSDFile(STsdb *pRepo, void *buf, SDFile *pDFile) {
H
Hongze Cheng 已提交
73
  buf = tsdbDecodeDFInfo(buf, &(pDFile->info));
H
Hongze Cheng 已提交
74
  buf = tfsDecodeFile(REPO_TFS(pRepo), buf, &(pDFile->f));
H
Hongze Cheng 已提交
75
  TSDB_FILE_SET_CLOSED(pDFile);
H
TD-353  
Hongze Cheng 已提交
76

H
Hongze Cheng 已提交
77
  return buf;
H
TD-353  
Hongze Cheng 已提交
78
}
H
TD-353  
Hongze Cheng 已提交
79

H
Hongze Cheng 已提交
80 81 82 83 84 85 86 87 88 89
static int tsdbEncodeSDFileEx(void **buf, SDFile *pDFile) {
  int tlen = 0;

  tlen += tsdbEncodeDFInfo(buf, &(pDFile->info));
  tlen += taosEncodeString(buf, TSDB_FILE_FULL_NAME(pDFile));

  return tlen;
}

static void *tsdbDecodeSDFileEx(void *buf, SDFile *pDFile) {
90
  char *aname = NULL;
H
Hongze Cheng 已提交
91 92 93 94 95

  buf = tsdbDecodeDFInfo(buf, &(pDFile->info));
  buf = taosDecodeString(buf, &aname);
  strncpy(TSDB_FILE_FULL_NAME(pDFile), aname, TSDB_FILENAME_LEN);
  TSDB_FILE_SET_CLOSED(pDFile);
wafwerar's avatar
wafwerar 已提交
96
  taosMemoryFreeClear(aname);
H
Hongze Cheng 已提交
97 98 99 100

  return buf;
}

101
int tsdbCreateDFile(STsdb *pRepo, SDFile *pDFile, bool updateHeader, TSDB_FILE_T fType) {
H
Hongze Cheng 已提交
102 103
  ASSERT(pDFile->info.size == 0 && pDFile->info.magic == TSDB_FILE_INIT_MAGIC);

104
  pDFile->pFile = taosOpenFile(TSDB_FILE_FULL_NAME(pDFile), TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
105
  if (pDFile->pFile == NULL) {
H
Hongze Cheng 已提交
106 107
    if (errno == ENOENT) {
      // Try to create directory recursively
S
Shengliang Guan 已提交
108
      char *s = strdup(TSDB_FILE_REL_NAME(pDFile));
H
Hongze Cheng 已提交
109
      if (tfsMkdirRecurAt(REPO_TFS(pRepo), taosDirName(s), TSDB_FILE_DID(pDFile)) < 0) {
wafwerar's avatar
wafwerar 已提交
110
        taosMemoryFreeClear(s);
H
Hongze Cheng 已提交
111 112
        return -1;
      }
wafwerar's avatar
wafwerar 已提交
113
      taosMemoryFreeClear(s);
H
Hongze Cheng 已提交
114

115
      pDFile->pFile = taosOpenFile(TSDB_FILE_FULL_NAME(pDFile), TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
116
      if (pDFile->pFile == NULL) {
H
Hongze Cheng 已提交
117 118 119 120 121 122 123
        terrno = TAOS_SYSTEM_ERROR(errno);
        return -1;
      }
    } else {
      terrno = TAOS_SYSTEM_ERROR(errno);
      return -1;
    }
H
Hongze Cheng 已提交
124 125
  }

H
Hongze Cheng 已提交
126 127 128 129
  if (!updateHeader) {
    return 0;
  }

H
Hongze Cheng 已提交
130
  pDFile->info.size += TSDB_FILE_HEAD_SIZE;
131
  pDFile->info.fver = tsdbGetDFSVersion(fType);
H
Hongze Cheng 已提交
132

H
Hongze Cheng 已提交
133
  if (tsdbUpdateDFileHeader(pDFile) < 0) {
H
Hongze Cheng 已提交
134 135 136 137 138
    tsdbCloseDFile(pDFile);
    tsdbRemoveDFile(pDFile);
    return -1;
  }

H
Hongze Cheng 已提交
139 140 141
  return 0;
}

H
Hongze Cheng 已提交
142 143 144 145 146 147
int tsdbUpdateDFileHeader(SDFile *pDFile) {
  char buf[TSDB_FILE_HEAD_SIZE] = "\0";

  if (tsdbSeekDFile(pDFile, 0, SEEK_SET) < 0) {
    return -1;
  }
H
Hongze Cheng 已提交
148

H
Hongze Cheng 已提交
149
  void *ptr = buf;
C
Cary Xu 已提交
150
  // taosEncodeFixedU32(&ptr, 0); // fver moved to SDFInfo and saved to current
H
Hongze Cheng 已提交
151
  tsdbEncodeDFInfo(&ptr, &(pDFile->info));
H
Hongze Cheng 已提交
152

H
refact  
Hongze Cheng 已提交
153
  taosCalcChecksumAppend(0, (uint8_t *)buf, TSDB_FILE_HEAD_SIZE);
H
Hongze Cheng 已提交
154
  if (tsdbWriteDFile(pDFile, buf, TSDB_FILE_HEAD_SIZE) < 0) {
H
Hongze Cheng 已提交
155 156
    return -1;
  }
H
Hongze Cheng 已提交
157 158

  return 0;
H
Hongze Cheng 已提交
159 160
}

H
Hongze Cheng 已提交
161
int tsdbLoadDFileHeader(SDFile *pDFile, SDFInfo *pInfo) {
H
Hongze Cheng 已提交
162
  char     buf[TSDB_FILE_HEAD_SIZE] = "\0";
163
  uint32_t _version;
H
Hongze Cheng 已提交
164 165 166 167 168 169 170 171 172 173 174

  ASSERT(TSDB_FILE_OPENED(pDFile));

  if (tsdbSeekDFile(pDFile, 0, SEEK_SET) < 0) {
    return -1;
  }

  if (tsdbReadDFile(pDFile, buf, TSDB_FILE_HEAD_SIZE) < 0) {
    return -1;
  }

H
refact  
Hongze Cheng 已提交
175 176 177 178 179
  if (!taosCheckChecksumWhole((uint8_t *)buf, TSDB_FILE_HEAD_SIZE)) {
    terrno = TSDB_CODE_TDB_FILE_CORRUPTED;
    return -1;
  }

H
Hongze Cheng 已提交
180
  void *pBuf = buf;
C
Cary Xu 已提交
181
  // pBuf = taosDecodeFixedU32(pBuf, &_version);
H
Hongze Cheng 已提交
182
  pBuf = tsdbDecodeDFInfo(pBuf, pInfo);
H
Hongze Cheng 已提交
183 184 185
  return 0;
}

H
Hongze Cheng 已提交
186
static int tsdbScanAndTryFixDFile(STsdb *pRepo, SDFile *pDFile) {
H
Hongze Cheng 已提交
187
  SDFile df;
H
refact  
Hongze Cheng 已提交
188 189

  tsdbInitDFileEx(&df, pDFile);
H
Hongze Cheng 已提交
190

191
  if (!taosCheckExistFile(TSDB_FILE_FULL_NAME(pDFile))) {
H
Hongze Cheng 已提交
192 193
    tsdbError("vgId:%d data file %s not exit, report to upper layer to fix it", REPO_ID(pRepo),
              TSDB_FILE_FULL_NAME(pDFile));
H
Hongze Cheng 已提交
194
    // pRepo->state |= TSDB_STATE_BAD_DATA;
H
Hongze Cheng 已提交
195
    TSDB_FILE_SET_STATE(pDFile, TSDB_FILE_STATE_BAD);
H
Hongze Cheng 已提交
196 197
    return 0;
  }
198 199
  int64_t file_size = 0;
  if (taosStatFile(TSDB_FILE_FULL_NAME(&df), &file_size, NULL) < 0) {
H
Hongze Cheng 已提交
200 201 202 203
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }

204
  if (pDFile->info.size < file_size) {
205 206
    // if (tsdbOpenDFile(&df, O_WRONLY) < 0) {
    if (tsdbOpenDFile(&df, TD_FILE_WRITE) < 0) {
H
Hongze Cheng 已提交
207 208 209
      return -1;
    }

210
    if (taosFtruncateFile(df.pFile, df.info.size) < 0) {
H
Hongze Cheng 已提交
211 212 213 214 215 216 217 218 219 220 221
      terrno = TAOS_SYSTEM_ERROR(errno);
      tsdbCloseDFile(&df);
      return -1;
    }

    if (tsdbUpdateDFileHeader(&df) < 0) {
      tsdbCloseDFile(&df);
      return -1;
    }

    tsdbCloseDFile(&df);
H
Hongze Cheng 已提交
222
    tsdbInfo("vgId:%d file %s is truncated from %" PRId64 " to %" PRId64, REPO_ID(pRepo), TSDB_FILE_FULL_NAME(pDFile),
223 224
             file_size, pDFile->info.size);
  } else if (pDFile->info.size > file_size) {
H
Hongze Cheng 已提交
225
    tsdbError("vgId:%d data file %s has wrong size %" PRId64 " expected %" PRId64 ", report to upper layer to fix it",
226
              REPO_ID(pRepo), TSDB_FILE_FULL_NAME(pDFile), file_size, pDFile->info.size);
H
Hongze Cheng 已提交
227
    // pRepo->state |= TSDB_STATE_BAD_DATA;
H
Hongze Cheng 已提交
228
    TSDB_FILE_SET_STATE(pDFile, TSDB_FILE_STATE_BAD);
H
Hongze Cheng 已提交
229
    terrno = TSDB_CODE_TDB_FILE_CORRUPTED;
H
Hongze Cheng 已提交
230
    return 0;
H
refact  
Hongze Cheng 已提交
231 232
  } else {
    tsdbDebug("vgId:%d file %s passes the scan", REPO_ID(pRepo), TSDB_FILE_FULL_NAME(pDFile));
H
Hongze Cheng 已提交
233 234 235 236 237
  }

  return 0;
}

H
Hongze Cheng 已提交
238
static int tsdbEncodeDFInfo(void **buf, SDFInfo *pInfo) {
H
TD-353  
Hongze Cheng 已提交
239
  int tlen = 0;
H
Hongze Cheng 已提交
240

H
Hongze Cheng 已提交
241
  tlen += taosEncodeFixedU32(buf, pInfo->magic);
242
  tlen += taosEncodeFixedU32(buf, pInfo->fver);
H
TD-353  
Hongze Cheng 已提交
243 244 245
  tlen += taosEncodeFixedU32(buf, pInfo->len);
  tlen += taosEncodeFixedU32(buf, pInfo->totalBlocks);
  tlen += taosEncodeFixedU32(buf, pInfo->totalSubBlocks);
H
Hongze Cheng 已提交
246 247 248
  tlen += taosEncodeFixedU32(buf, pInfo->offset);
  tlen += taosEncodeFixedU64(buf, pInfo->size);
  tlen += taosEncodeFixedU64(buf, pInfo->tombSize);
H
TD-353  
Hongze Cheng 已提交
249

H
TD-353  
Hongze Cheng 已提交
250
  return tlen;
H
TD-353  
Hongze Cheng 已提交
251 252
}

H
Hongze Cheng 已提交
253
static void *tsdbDecodeDFInfo(void *buf, SDFInfo *pInfo) {
H
Hongze Cheng 已提交
254
  buf = taosDecodeFixedU32(buf, &(pInfo->magic));
255
  buf = taosDecodeFixedU32(buf, &(pInfo->fver));
H
TD-353  
Hongze Cheng 已提交
256 257 258
  buf = taosDecodeFixedU32(buf, &(pInfo->len));
  buf = taosDecodeFixedU32(buf, &(pInfo->totalBlocks));
  buf = taosDecodeFixedU32(buf, &(pInfo->totalSubBlocks));
H
Hongze Cheng 已提交
259 260 261
  buf = taosDecodeFixedU32(buf, &(pInfo->offset));
  buf = taosDecodeFixedU64(buf, &(pInfo->size));
  buf = taosDecodeFixedU64(buf, &(pInfo->tombSize));
H
TD-353  
Hongze Cheng 已提交
262 263 264 265

  return buf;
}

H
Hongze Cheng 已提交
266 267 268 269 270 271 272 273 274
static int tsdbApplyDFileChange(SDFile *from, SDFile *to) {
  ASSERT(from != NULL || to != NULL);

  if (from != NULL) {
    if (to == NULL) {
      tsdbRemoveDFile(from);
    } else {
      if (tfsIsSameFile(TSDB_FILE_F(from), TSDB_FILE_F(to))) {
        if (from->info.size > to->info.size) {
H
Hongze Cheng 已提交
275
          tsdbRollBackDFile(to);
H
Hongze Cheng 已提交
276 277
        }
      } else {
278
        (void)tsdbRemoveDFile(from);
H
Hongze Cheng 已提交
279 280 281 282 283 284 285
      }
    }
  }

  return 0;
}

H
Hongze Cheng 已提交
286
static int tsdbRollBackDFile(SDFile *pDFile) {
H
Hongze Cheng 已提交
287 288
  SDFile df = *pDFile;

289 290
  // if (tsdbOpenDFile(&df, O_WRONLY) < 0) {
  if (tsdbOpenDFile(&df, TD_FILE_WRITE) < 0) {
H
Hongze Cheng 已提交
291 292 293
    return -1;
  }

294
  if (taosFtruncateFile(TSDB_FILE_PFILE(&df), pDFile->info.size) < 0) {
H
Hongze Cheng 已提交
295 296 297 298 299 300 301 302 303 304
    terrno = TAOS_SYSTEM_ERROR(errno);
    tsdbCloseDFile(&df);
    return -1;
  }

  if (tsdbUpdateDFileHeader(&df) < 0) {
    tsdbCloseDFile(&df);
    return -1;
  }

H
Hongze Cheng 已提交
305 306
  TSDB_FILE_FSYNC(&df);

H
Hongze Cheng 已提交
307 308 309 310
  tsdbCloseDFile(&df);
  return 0;
}

H
Hongze Cheng 已提交
311
// ============== Operations on SDFileSet
S
Shengliang Guan 已提交
312
void tsdbInitDFileSet(STsdb *pRepo, SDFileSet *pSet, SDiskID did, int fid, uint32_t ver) {
313 314 315 316
  TSDB_FSET_FID(pSet) = fid;
  TSDB_FSET_VER(pSet) = TSDB_LATEST_FSET_VER;
  TSDB_FSET_STATE(pSet) = 0;
  pSet->reserve = 0;
H
Hongze Cheng 已提交
317

H
Hongze Cheng 已提交
318 319
  for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
    SDFile *pDFile = TSDB_DFILE_IN_SET(pSet, ftype);
S
Shengliang Guan 已提交
320
    tsdbInitDFile(pRepo, pDFile, did, fid, ver, ftype);
H
Hongze Cheng 已提交
321 322 323
  }
}

H
Hongze Cheng 已提交
324
void tsdbInitDFileSetEx(SDFileSet *pSet, SDFileSet *pOSet) {
325
  TSDB_FSET_FID(pSet) = TSDB_FSET_FID(pOSet);
H
Hongze Cheng 已提交
326
  for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
H
Hongze Cheng 已提交
327
    tsdbInitDFileEx(TSDB_DFILE_IN_SET(pSet, ftype), TSDB_DFILE_IN_SET(pOSet, ftype));
H
Hongze Cheng 已提交
328
  }
H
Hongze Cheng 已提交
329 330
}

H
Hongze Cheng 已提交
331 332
int tsdbEncodeDFileSet(void **buf, SDFileSet *pSet) {
  int tlen = 0;
H
Hongze Cheng 已提交
333

334 335 336 337
  tlen += taosEncodeFixedI32(buf, TSDB_FSET_FID(pSet));
  // state not included
  tlen += taosEncodeFixedU8(buf, TSDB_FSET_VER(pSet));
  tlen += taosEncodeFixedU16(buf, pSet->reserve);
H
Hongze Cheng 已提交
338 339
  for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
    tlen += tsdbEncodeSDFile(buf, TSDB_DFILE_IN_SET(pSet, ftype));
H
Hongze Cheng 已提交
340
  }
H
Hongze Cheng 已提交
341

H
Hongze Cheng 已提交
342
  return tlen;
H
Hongze Cheng 已提交
343 344
}

S
Shengliang Guan 已提交
345
void *tsdbDecodeDFileSet(STsdb *pRepo, void *buf, SDFileSet *pSet) {
346 347 348 349
  buf = taosDecodeFixedI32(buf, &(TSDB_FSET_FID(pSet)));
  TSDB_FSET_STATE(pSet) = 0;
  buf = taosDecodeFixedU8(buf, &(TSDB_FSET_VER(pSet)));
  buf = taosDecodeFixedU16(buf, &(pSet->reserve));
H
Hongze Cheng 已提交
350

H
Hongze Cheng 已提交
351
  for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
S
Shengliang Guan 已提交
352
    buf = tsdbDecodeSDFile(pRepo, buf, TSDB_DFILE_IN_SET(pSet, ftype));
H
Hongze Cheng 已提交
353
  }
H
Hongze Cheng 已提交
354
  return buf;
H
Hongze Cheng 已提交
355 356
}

H
Hongze Cheng 已提交
357 358 359
int tsdbEncodeDFileSetEx(void **buf, SDFileSet *pSet) {
  int tlen = 0;

360 361 362
  tlen += taosEncodeFixedI32(buf, TSDB_FSET_FID(pSet));
  tlen += taosEncodeFixedU8(buf, TSDB_FSET_VER(pSet));
  tlen += taosEncodeFixedU16(buf, pSet->reserve);
H
Hongze Cheng 已提交
363 364 365 366 367 368 369 370
  for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
    tlen += tsdbEncodeSDFileEx(buf, TSDB_DFILE_IN_SET(pSet, ftype));
  }

  return tlen;
}

void *tsdbDecodeDFileSetEx(void *buf, SDFileSet *pSet) {
371 372 373
  buf = taosDecodeFixedI32(buf, &(TSDB_FSET_FID(pSet)));
  buf = taosDecodeFixedU8(buf, &(TSDB_FSET_VER(pSet)));
  buf = taosDecodeFixedU16(buf, &(pSet->reserve));
H
Hongze Cheng 已提交
374 375 376 377 378 379 380

  for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
    buf = tsdbDecodeSDFileEx(buf, TSDB_DFILE_IN_SET(pSet, ftype));
  }
  return buf;
}

H
Hongze Cheng 已提交
381
int tsdbApplyDFileSetChange(SDFileSet *from, SDFileSet *to) {
H
Hongze Cheng 已提交
382
  for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
H
Hongze Cheng 已提交
383 384 385
    SDFile *pDFileFrom = (from) ? TSDB_DFILE_IN_SET(from, ftype) : NULL;
    SDFile *pDFileTo = (to) ? TSDB_DFILE_IN_SET(to, ftype) : NULL;
    if (tsdbApplyDFileChange(pDFileFrom, pDFileTo) < 0) {
H
Hongze Cheng 已提交
386 387 388 389 390 391 392
      return -1;
    }
  }

  return 0;
}

S
Shengliang Guan 已提交
393
int tsdbCreateDFileSet(STsdb *pRepo, SDFileSet *pSet, bool updateHeader) {
H
Hongze Cheng 已提交
394
  for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
395
    if (tsdbCreateDFile(pRepo, TSDB_DFILE_IN_SET(pSet, ftype), updateHeader, ftype) < 0) {
H
Hongze Cheng 已提交
396 397 398 399 400 401
      tsdbCloseDFileSet(pSet);
      tsdbRemoveDFileSet(pSet);
      return -1;
    }
  }

H
Hongze Cheng 已提交
402 403
  return 0;
}
H
Hongze Cheng 已提交
404

H
Hongze Cheng 已提交
405
int tsdbUpdateDFileSetHeader(SDFileSet *pSet) {
H
Hongze Cheng 已提交
406
  for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
H
Hongze Cheng 已提交
407
    if (tsdbUpdateDFileHeader(TSDB_DFILE_IN_SET(pSet, ftype)) < 0) {
H
Hongze Cheng 已提交
408 409 410
      return -1;
    }
  }
H
Hongze Cheng 已提交
411
  return 0;
H
refact  
Hongze Cheng 已提交
412 413
}

H
Hongze Cheng 已提交
414
int tsdbScanAndTryFixDFileSet(STsdb *pRepo, SDFileSet *pSet) {
H
Hongze Cheng 已提交
415
  for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
H
Hongze Cheng 已提交
416
    if (tsdbScanAndTryFixDFile(pRepo, TSDB_DFILE_IN_SET(pSet, ftype)) < 0) {
H
Hongze Cheng 已提交
417 418 419 420 421 422
      return -1;
    }
  }
  return 0;
}

423
int tsdbParseDFilename(const char *fname, int *vid, int *fid, TSDB_FILE_T *ftype, uint32_t *_version) {
H
Hongze Cheng 已提交
424
  char *p = NULL;
425
  *_version = 0;
H
Hongze Cheng 已提交
426 427
  *ftype = TSDB_FILE_MAX;

428
  sscanf(fname, "v%df%d.%m[a-z]-ver%" PRIu32, vid, fid, &p, _version);
H
Hongze Cheng 已提交
429 430 431 432 433 434 435
  for (TSDB_FILE_T i = 0; i < TSDB_FILE_MAX; i++) {
    if (strcmp(p, TSDB_FNAME_SUFFIX[i]) == 0) {
      *ftype = i;
      break;
    }
  }

wafwerar's avatar
wafwerar 已提交
436
  taosMemoryFreeClear(p);
H
Hongze Cheng 已提交
437 438 439
  return 0;
}

C
Cary Xu 已提交
440
static void tsdbGetFilename(int vid, int fid, uint32_t ver, TSDB_FILE_T ftype, const char *dname, char *fname) {
H
refact  
Hongze Cheng 已提交
441 442 443 444
  ASSERT(ftype != TSDB_FILE_MAX);

  if (ftype < TSDB_FILE_MAX) {
    if (ver == 0) {
C
Cary Xu 已提交
445 446
      snprintf(fname, TSDB_FILENAME_LEN, "vnode/vnode%d/%s/data/v%df%d.%s", vid, dname, vid, fid,
               TSDB_FNAME_SUFFIX[ftype]);
H
refact  
Hongze Cheng 已提交
447
    } else {
C
Cary Xu 已提交
448
      snprintf(fname, TSDB_FILENAME_LEN, "vnode/vnode%d/%s/data/v%df%d.%s-ver%" PRIu32, vid, dname, vid, fid,
H
Hongze Cheng 已提交
449
               TSDB_FNAME_SUFFIX[ftype], ver);
H
refact  
Hongze Cheng 已提交
450 451 452
    }
  } else {
    if (ver == 0) {
H
Hongze Cheng 已提交
453
      snprintf(fname, TSDB_FILENAME_LEN, "vnode/vnode%d/tsdb/%s", vid, TSDB_FNAME_SUFFIX[ftype]);
H
refact  
Hongze Cheng 已提交
454
    } else {
H
Hongze Cheng 已提交
455
      snprintf(fname, TSDB_FILENAME_LEN, "vnode/vnode%d/tsdb/%s-ver%" PRIu32, vid, TSDB_FNAME_SUFFIX[ftype], ver);
H
refact  
Hongze Cheng 已提交
456 457
    }
  }
H
Hongze Cheng 已提交
458
}