tsdbFile.c 16.1 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
};

H
refact  
Hongze Cheng 已提交
28
static void  tsdbGetFilename(int vid, int fid, uint32_t ver, TSDB_FILE_T ftype, const char *dname, char *fname);
H
Hongze Cheng 已提交
29 30 31 32
static int   tsdbEncodeDFInfo(void **buf, SDFInfo *pInfo);
static void *tsdbDecodeDFInfo(void *buf, SDFInfo *pInfo);
static int   tsdbRollBackDFile(SDFile *pDFile);

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

H
Hongze Cheng 已提交
37 38
  TSDB_FILE_SET_STATE(pDFile, TSDB_FILE_STATE_OK);

H
Hongze Cheng 已提交
39
  TSDB_FILE_SET_CLOSED(pDFile);
H
refact  
Hongze Cheng 已提交
40

H
Hongze Cheng 已提交
41 42
  memset(&(pDFile->info), 0, sizeof(pDFile->info));
  pDFile->info.magic = TSDB_FILE_INIT_MAGIC;
43
  pDFile->info.fver = tsdbGetDFSVersion(ftype);
H
refact  
Hongze Cheng 已提交
44

45
  tsdbGetFilename(REPO_ID(pRepo), fid, ver, ftype, pRepo->dir, fname);
H
Hongze Cheng 已提交
46
  tfsInitFile(REPO_TFS(pRepo), &(pDFile->f), did, fname);
H
Hongze Cheng 已提交
47 48
}

H
Hongze Cheng 已提交
49 50
void tsdbInitDFileEx(SDFile *pDFile, SDFile *pODFile) {
  *pDFile = *pODFile;
H
Hongze Cheng 已提交
51 52 53
  TSDB_FILE_SET_CLOSED(pDFile);
}

H
refact  
Hongze Cheng 已提交
54
int tsdbEncodeSDFile(void **buf, SDFile *pDFile) {
H
Hongze Cheng 已提交
55
  int tlen = 0;
56

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

H
Hongze Cheng 已提交
60
  return tlen;
H
TD-34  
hzcheng 已提交
61 62
}

S
Shengliang Guan 已提交
63
void *tsdbDecodeSDFile(STsdb *pRepo, void *buf, SDFile *pDFile) {
H
Hongze Cheng 已提交
64
  buf = tsdbDecodeDFInfo(buf, &(pDFile->info));
H
Hongze Cheng 已提交
65
  buf = tfsDecodeFile(REPO_TFS(pRepo), buf, &(pDFile->f));
H
Hongze Cheng 已提交
66
  TSDB_FILE_SET_CLOSED(pDFile);
H
TD-353  
Hongze Cheng 已提交
67

H
Hongze Cheng 已提交
68
  return buf;
H
TD-353  
Hongze Cheng 已提交
69
}
H
TD-353  
Hongze Cheng 已提交
70

H
Hongze Cheng 已提交
71 72 73 74 75 76 77 78 79 80
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) {
81
  char *aname = NULL;
H
Hongze Cheng 已提交
82 83 84 85 86

  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 已提交
87
  taosMemoryFreeClear(aname);
H
Hongze Cheng 已提交
88 89 90 91

  return buf;
}

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

95
  pDFile->pFile = taosOpenFile(TSDB_FILE_FULL_NAME(pDFile), TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
96
  if (pDFile->pFile == NULL) {
H
Hongze Cheng 已提交
97 98
    if (errno == ENOENT) {
      // Try to create directory recursively
S
Shengliang Guan 已提交
99
      char *s = strdup(TSDB_FILE_REL_NAME(pDFile));
H
Hongze Cheng 已提交
100
      if (tfsMkdirRecurAt(REPO_TFS(pRepo), taosDirName(s), TSDB_FILE_DID(pDFile)) < 0) {
wafwerar's avatar
wafwerar 已提交
101
        taosMemoryFreeClear(s);
H
Hongze Cheng 已提交
102 103
        return -1;
      }
wafwerar's avatar
wafwerar 已提交
104
      taosMemoryFreeClear(s);
H
Hongze Cheng 已提交
105

106
      pDFile->pFile = taosOpenFile(TSDB_FILE_FULL_NAME(pDFile), TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
107
      if (pDFile->pFile == NULL) {
H
Hongze Cheng 已提交
108 109 110 111 112 113 114
        terrno = TAOS_SYSTEM_ERROR(errno);
        return -1;
      }
    } else {
      terrno = TAOS_SYSTEM_ERROR(errno);
      return -1;
    }
H
Hongze Cheng 已提交
115 116
  }

H
Hongze Cheng 已提交
117 118 119 120
  if (!updateHeader) {
    return 0;
  }

H
Hongze Cheng 已提交
121
  pDFile->info.size += TSDB_FILE_HEAD_SIZE;
122
  pDFile->info.fver = tsdbGetDFSVersion(fType);
H
Hongze Cheng 已提交
123

H
Hongze Cheng 已提交
124
  if (tsdbUpdateDFileHeader(pDFile) < 0) {
H
Hongze Cheng 已提交
125 126 127 128 129
    tsdbCloseDFile(pDFile);
    tsdbRemoveDFile(pDFile);
    return -1;
  }

H
Hongze Cheng 已提交
130 131 132
  return 0;
}

H
Hongze Cheng 已提交
133 134 135 136 137 138
int tsdbUpdateDFileHeader(SDFile *pDFile) {
  char buf[TSDB_FILE_HEAD_SIZE] = "\0";

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

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

H
refact  
Hongze Cheng 已提交
144
  taosCalcChecksumAppend(0, (uint8_t *)buf, TSDB_FILE_HEAD_SIZE);
H
Hongze Cheng 已提交
145
  if (tsdbWriteDFile(pDFile, buf, TSDB_FILE_HEAD_SIZE) < 0) {
H
Hongze Cheng 已提交
146 147
    return -1;
  }
H
Hongze Cheng 已提交
148 149

  return 0;
H
Hongze Cheng 已提交
150 151
}

H
Hongze Cheng 已提交
152
int tsdbLoadDFileHeader(SDFile *pDFile, SDFInfo *pInfo) {
H
Hongze Cheng 已提交
153
  char     buf[TSDB_FILE_HEAD_SIZE] = "\0";
154
  uint32_t _version;
H
Hongze Cheng 已提交
155 156 157 158 159 160 161 162 163 164 165

  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 已提交
166 167 168 169 170
  if (!taosCheckChecksumWhole((uint8_t *)buf, TSDB_FILE_HEAD_SIZE)) {
    terrno = TSDB_CODE_TDB_FILE_CORRUPTED;
    return -1;
  }

H
Hongze Cheng 已提交
171
  void *pBuf = buf;
C
Cary Xu 已提交
172
  // pBuf = taosDecodeFixedU32(pBuf, &_version);
H
Hongze Cheng 已提交
173
  pBuf = tsdbDecodeDFInfo(pBuf, pInfo);
H
Hongze Cheng 已提交
174 175 176
  return 0;
}

H
Hongze Cheng 已提交
177
static int tsdbScanAndTryFixDFile(STsdb *pRepo, SDFile *pDFile) {
H
Hongze Cheng 已提交
178
  SDFile df;
H
refact  
Hongze Cheng 已提交
179 180

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

182
  if (!taosCheckExistFile(TSDB_FILE_FULL_NAME(pDFile))) {
S
Shengliang Guan 已提交
183
    tsdbError("vgId:%d, data file %s not exit, report to upper layer to fix it", REPO_ID(pRepo),
H
Hongze Cheng 已提交
184
              TSDB_FILE_FULL_NAME(pDFile));
H
Hongze Cheng 已提交
185
    // pRepo->state |= TSDB_STATE_BAD_DATA;
H
Hongze Cheng 已提交
186
    TSDB_FILE_SET_STATE(pDFile, TSDB_FILE_STATE_BAD);
H
Hongze Cheng 已提交
187 188
    return 0;
  }
189 190
  int64_t file_size = 0;
  if (taosStatFile(TSDB_FILE_FULL_NAME(&df), &file_size, NULL) < 0) {
H
Hongze Cheng 已提交
191 192 193 194
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }

195
  if (pDFile->info.size < file_size) {
196 197
    // if (tsdbOpenDFile(&df, O_WRONLY) < 0) {
    if (tsdbOpenDFile(&df, TD_FILE_WRITE) < 0) {
H
Hongze Cheng 已提交
198 199 200
      return -1;
    }

201
    if (taosFtruncateFile(df.pFile, df.info.size) < 0) {
H
Hongze Cheng 已提交
202 203 204 205 206 207 208 209 210 211 212
      terrno = TAOS_SYSTEM_ERROR(errno);
      tsdbCloseDFile(&df);
      return -1;
    }

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

    tsdbCloseDFile(&df);
S
Shengliang Guan 已提交
213
    tsdbInfo("vgId:%d, file %s is truncated from %" PRId64 " to %" PRId64, REPO_ID(pRepo), TSDB_FILE_FULL_NAME(pDFile),
214 215
             file_size, pDFile->info.size);
  } else if (pDFile->info.size > file_size) {
S
Shengliang Guan 已提交
216
    tsdbError("vgId:%d, data file %s has wrong size %" PRId64 " expected %" PRId64 ", report to upper layer to fix it",
217
              REPO_ID(pRepo), TSDB_FILE_FULL_NAME(pDFile), file_size, pDFile->info.size);
H
Hongze Cheng 已提交
218
    // pRepo->state |= TSDB_STATE_BAD_DATA;
H
Hongze Cheng 已提交
219
    TSDB_FILE_SET_STATE(pDFile, TSDB_FILE_STATE_BAD);
H
Hongze Cheng 已提交
220
    terrno = TSDB_CODE_TDB_FILE_CORRUPTED;
H
Hongze Cheng 已提交
221
    return 0;
H
refact  
Hongze Cheng 已提交
222
  } else {
S
Shengliang Guan 已提交
223
    tsdbDebug("vgId:%d, file %s passes the scan", REPO_ID(pRepo), TSDB_FILE_FULL_NAME(pDFile));
H
Hongze Cheng 已提交
224 225 226 227 228
  }

  return 0;
}

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

H
Hongze Cheng 已提交
232
  tlen += taosEncodeFixedU32(buf, pInfo->magic);
233
  tlen += taosEncodeFixedU32(buf, pInfo->fver);
H
TD-353  
Hongze Cheng 已提交
234 235 236
  tlen += taosEncodeFixedU32(buf, pInfo->len);
  tlen += taosEncodeFixedU32(buf, pInfo->totalBlocks);
  tlen += taosEncodeFixedU32(buf, pInfo->totalSubBlocks);
H
Hongze Cheng 已提交
237 238 239
  tlen += taosEncodeFixedU32(buf, pInfo->offset);
  tlen += taosEncodeFixedU64(buf, pInfo->size);
  tlen += taosEncodeFixedU64(buf, pInfo->tombSize);
H
TD-353  
Hongze Cheng 已提交
240

H
TD-353  
Hongze Cheng 已提交
241
  return tlen;
H
TD-353  
Hongze Cheng 已提交
242 243
}

H
Hongze Cheng 已提交
244
static void *tsdbDecodeDFInfo(void *buf, SDFInfo *pInfo) {
H
Hongze Cheng 已提交
245
  buf = taosDecodeFixedU32(buf, &(pInfo->magic));
246
  buf = taosDecodeFixedU32(buf, &(pInfo->fver));
H
TD-353  
Hongze Cheng 已提交
247 248 249
  buf = taosDecodeFixedU32(buf, &(pInfo->len));
  buf = taosDecodeFixedU32(buf, &(pInfo->totalBlocks));
  buf = taosDecodeFixedU32(buf, &(pInfo->totalSubBlocks));
H
Hongze Cheng 已提交
250 251 252
  buf = taosDecodeFixedU32(buf, &(pInfo->offset));
  buf = taosDecodeFixedU64(buf, &(pInfo->size));
  buf = taosDecodeFixedU64(buf, &(pInfo->tombSize));
H
TD-353  
Hongze Cheng 已提交
253 254 255 256

  return buf;
}

H
Hongze Cheng 已提交
257 258 259 260 261 262 263 264 265
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 已提交
266
          tsdbRollBackDFile(to);
H
Hongze Cheng 已提交
267 268
        }
      } else {
269
        (void)tsdbRemoveDFile(from);
H
Hongze Cheng 已提交
270 271 272 273 274 275 276
      }
    }
  }

  return 0;
}

H
Hongze Cheng 已提交
277
static int tsdbRollBackDFile(SDFile *pDFile) {
H
Hongze Cheng 已提交
278 279
  SDFile df = *pDFile;

280 281
  // if (tsdbOpenDFile(&df, O_WRONLY) < 0) {
  if (tsdbOpenDFile(&df, TD_FILE_WRITE) < 0) {
H
Hongze Cheng 已提交
282 283 284
    return -1;
  }

285
  if (taosFtruncateFile(TSDB_FILE_PFILE(&df), pDFile->info.size) < 0) {
H
Hongze Cheng 已提交
286 287 288 289 290 291 292 293 294 295
    terrno = TAOS_SYSTEM_ERROR(errno);
    tsdbCloseDFile(&df);
    return -1;
  }

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

H
Hongze Cheng 已提交
296 297
  TSDB_FILE_FSYNC(&df);

H
Hongze Cheng 已提交
298 299 300 301
  tsdbCloseDFile(&df);
  return 0;
}

H
Hongze Cheng 已提交
302
// ============== Operations on SDFileSet
S
Shengliang Guan 已提交
303
void tsdbInitDFileSet(STsdb *pRepo, SDFileSet *pSet, SDiskID did, int fid, uint32_t ver) {
304 305 306 307
  TSDB_FSET_FID(pSet) = fid;
  TSDB_FSET_VER(pSet) = TSDB_LATEST_FSET_VER;
  TSDB_FSET_STATE(pSet) = 0;
  pSet->reserve = 0;
H
Hongze Cheng 已提交
308

H
Hongze Cheng 已提交
309 310
  for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
    SDFile *pDFile = TSDB_DFILE_IN_SET(pSet, ftype);
S
Shengliang Guan 已提交
311
    tsdbInitDFile(pRepo, pDFile, did, fid, ver, ftype);
H
Hongze Cheng 已提交
312 313 314
  }
}

H
Hongze Cheng 已提交
315
void tsdbInitDFileSetEx(SDFileSet *pSet, SDFileSet *pOSet) {
316
  TSDB_FSET_FID(pSet) = TSDB_FSET_FID(pOSet);
H
Hongze Cheng 已提交
317
  for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
H
Hongze Cheng 已提交
318
    tsdbInitDFileEx(TSDB_DFILE_IN_SET(pSet, ftype), TSDB_DFILE_IN_SET(pOSet, ftype));
H
Hongze Cheng 已提交
319
  }
H
Hongze Cheng 已提交
320 321
}

H
Hongze Cheng 已提交
322 323
int tsdbEncodeDFileSet(void **buf, SDFileSet *pSet) {
  int tlen = 0;
H
Hongze Cheng 已提交
324

325 326 327 328
  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 已提交
329 330
  for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
    tlen += tsdbEncodeSDFile(buf, TSDB_DFILE_IN_SET(pSet, ftype));
H
Hongze Cheng 已提交
331
  }
H
Hongze Cheng 已提交
332

H
Hongze Cheng 已提交
333
  return tlen;
H
Hongze Cheng 已提交
334 335
}

S
Shengliang Guan 已提交
336
void *tsdbDecodeDFileSet(STsdb *pRepo, void *buf, SDFileSet *pSet) {
337 338 339 340
  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 已提交
341

H
Hongze Cheng 已提交
342
  for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
S
Shengliang Guan 已提交
343
    buf = tsdbDecodeSDFile(pRepo, buf, TSDB_DFILE_IN_SET(pSet, ftype));
H
Hongze Cheng 已提交
344
  }
H
Hongze Cheng 已提交
345
  return buf;
H
Hongze Cheng 已提交
346 347
}

H
Hongze Cheng 已提交
348 349 350
int tsdbEncodeDFileSetEx(void **buf, SDFileSet *pSet) {
  int tlen = 0;

351 352 353
  tlen += taosEncodeFixedI32(buf, TSDB_FSET_FID(pSet));
  tlen += taosEncodeFixedU8(buf, TSDB_FSET_VER(pSet));
  tlen += taosEncodeFixedU16(buf, pSet->reserve);
H
Hongze Cheng 已提交
354 355 356 357 358 359 360 361
  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) {
362 363 364
  buf = taosDecodeFixedI32(buf, &(TSDB_FSET_FID(pSet)));
  buf = taosDecodeFixedU8(buf, &(TSDB_FSET_VER(pSet)));
  buf = taosDecodeFixedU16(buf, &(pSet->reserve));
H
Hongze Cheng 已提交
365 366 367 368 369 370 371

  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 已提交
372
int tsdbApplyDFileSetChange(SDFileSet *from, SDFileSet *to) {
H
Hongze Cheng 已提交
373
  for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
H
Hongze Cheng 已提交
374 375 376
    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 已提交
377 378 379 380 381 382 383
      return -1;
    }
  }

  return 0;
}

S
Shengliang Guan 已提交
384
int tsdbCreateDFileSet(STsdb *pRepo, SDFileSet *pSet, bool updateHeader) {
H
Hongze Cheng 已提交
385
  for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
386
    if (tsdbCreateDFile(pRepo, TSDB_DFILE_IN_SET(pSet, ftype), updateHeader, ftype) < 0) {
H
Hongze Cheng 已提交
387 388 389 390 391 392
      tsdbCloseDFileSet(pSet);
      tsdbRemoveDFileSet(pSet);
      return -1;
    }
  }

H
Hongze Cheng 已提交
393 394
  return 0;
}
H
Hongze Cheng 已提交
395

H
Hongze Cheng 已提交
396
int tsdbUpdateDFileSetHeader(SDFileSet *pSet) {
H
Hongze Cheng 已提交
397
  for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
H
Hongze Cheng 已提交
398
    if (tsdbUpdateDFileHeader(TSDB_DFILE_IN_SET(pSet, ftype)) < 0) {
H
Hongze Cheng 已提交
399 400 401
      return -1;
    }
  }
H
Hongze Cheng 已提交
402
  return 0;
H
refact  
Hongze Cheng 已提交
403 404
}

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

414
int tsdbParseDFilename(const char *fname, int *vid, int *fid, TSDB_FILE_T *ftype, uint32_t *_version) {
H
Hongze Cheng 已提交
415
  char *p = NULL;
416
  *_version = 0;
H
Hongze Cheng 已提交
417 418
  *ftype = TSDB_FILE_MAX;

419
  sscanf(fname, "v%df%d.%m[a-z]-ver%" PRIu32, vid, fid, &p, _version);
H
Hongze Cheng 已提交
420 421 422 423 424 425 426
  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 已提交
427
  taosMemoryFreeClear(p);
H
Hongze Cheng 已提交
428 429 430
  return 0;
}

C
Cary Xu 已提交
431
static void tsdbGetFilename(int vid, int fid, uint32_t ver, TSDB_FILE_T ftype, const char *dname, char *fname) {
H
refact  
Hongze Cheng 已提交
432 433 434 435
  ASSERT(ftype != TSDB_FILE_MAX);

  if (ftype < TSDB_FILE_MAX) {
    if (ver == 0) {
C
Cary Xu 已提交
436 437
      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 已提交
438
    } else {
C
Cary Xu 已提交
439
      snprintf(fname, TSDB_FILENAME_LEN, "vnode/vnode%d/%s/data/v%df%d.%s-ver%" PRIu32, vid, dname, vid, fid,
H
Hongze Cheng 已提交
440
               TSDB_FNAME_SUFFIX[ftype], ver);
H
refact  
Hongze Cheng 已提交
441 442 443
    }
  } else {
    if (ver == 0) {
H
Hongze Cheng 已提交
444
      snprintf(fname, TSDB_FILENAME_LEN, "vnode/vnode%d/tsdb/%s", vid, TSDB_FNAME_SUFFIX[ftype]);
H
refact  
Hongze Cheng 已提交
445
    } else {
H
Hongze Cheng 已提交
446
      snprintf(fname, TSDB_FILENAME_LEN, "vnode/vnode%d/tsdb/%s-ver%" PRIu32, vid, TSDB_FNAME_SUFFIX[ftype], ver);
H
refact  
Hongze Cheng 已提交
447 448
    }
  }
H
refact  
Hongze Cheng 已提交
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543
}

int tsdbOpenDFile(SDFile *pDFile, int flags) {
  ASSERT(!TSDB_FILE_OPENED(pDFile));

  pDFile->pFile = taosOpenFile(TSDB_FILE_FULL_NAME(pDFile), flags);
  if (pDFile->pFile == NULL) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }

  return 0;
}

void tsdbCloseDFile(SDFile *pDFile) {
  if (TSDB_FILE_OPENED(pDFile)) {
    taosCloseFile(&pDFile->pFile);
    TSDB_FILE_SET_CLOSED(pDFile);
  }
}

int64_t tsdbSeekDFile(SDFile *pDFile, int64_t offset, int whence) {
  // ASSERT(TSDB_FILE_OPENED(pDFile));

  int64_t loffset = taosLSeekFile(TSDB_FILE_PFILE(pDFile), offset, whence);
  if (loffset < 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }

  return loffset;
}

int64_t tsdbWriteDFile(SDFile *pDFile, void *buf, int64_t nbyte) {
  ASSERT(TSDB_FILE_OPENED(pDFile));

  int64_t nwrite = taosWriteFile(pDFile->pFile, buf, nbyte);
  if (nwrite < nbyte) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }

  return nwrite;
}

void tsdbUpdateDFileMagic(SDFile *pDFile, void *pCksm) {
  pDFile->info.magic = taosCalcChecksum(pDFile->info.magic, (uint8_t *)(pCksm), sizeof(TSCKSUM));
}

int tsdbAppendDFile(SDFile *pDFile, void *buf, int64_t nbyte, int64_t *offset) {
  ASSERT(TSDB_FILE_OPENED(pDFile));

  int64_t toffset;

  if ((toffset = tsdbSeekDFile(pDFile, 0, SEEK_END)) < 0) {
    return -1;
  }

  ASSERT(pDFile->info.size == toffset);

  if (offset) {
    *offset = toffset;
  }

  if (tsdbWriteDFile(pDFile, buf, nbyte) < 0) {
    return -1;
  }

  pDFile->info.size += nbyte;

  return (int)nbyte;
}

int tsdbRemoveDFile(SDFile *pDFile) { return tfsRemoveFile(TSDB_FILE_F(pDFile)); }

int64_t tsdbReadDFile(SDFile *pDFile, void *buf, int64_t nbyte) {
  ASSERT(TSDB_FILE_OPENED(pDFile));

  int64_t nread = taosReadFile(pDFile->pFile, buf, nbyte);
  if (nread < 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }

  return nread;
}

int tsdbCopyDFile(SDFile *pSrc, SDFile *pDest) {
  if (tfsCopyFile(TSDB_FILE_F(pSrc), TSDB_FILE_F(pDest)) < 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }

  pDest->info = pSrc->info;
  return 0;
H
Hongze Cheng 已提交
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
}

void tsdbCloseDFileSet(SDFileSet *pSet) {
  for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
    tsdbCloseDFile(TSDB_DFILE_IN_SET(pSet, ftype));
  }
}

int tsdbOpenDFileSet(SDFileSet *pSet, int flags) {
  for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
    if (tsdbOpenDFile(TSDB_DFILE_IN_SET(pSet, ftype), flags) < 0) {
      tsdbCloseDFileSet(pSet);
      return -1;
    }
  }
  return 0;
}

void tsdbRemoveDFileSet(SDFileSet *pSet) {
  for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
    (void)tsdbRemoveDFile(TSDB_DFILE_IN_SET(pSet, ftype));
  }
}

int tsdbCopyDFileSet(SDFileSet *pSrc, SDFileSet *pDest) {
  for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
    if (tsdbCopyDFile(TSDB_DFILE_IN_SET(pSrc, ftype), TSDB_DFILE_IN_SET(pDest, ftype)) < 0) {
      tsdbRemoveDFileSet(pDest);
      return -1;
    }
  }

  return 0;
}

void tsdbGetFidKeyRange(int days, int8_t precision, int fid, TSKEY *minKey, TSKEY *maxKey) {
  *minKey = fid * days * tsTickPerMin[precision];
  *maxKey = *minKey + days * tsTickPerMin[precision] - 1;
H
Hongze Cheng 已提交
582
}