tsdbFile.c 18.3 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 "tsdbDef.h"
H
TD-353  
Hongze Cheng 已提交
17

H
refact  
Hongze Cheng 已提交
18
static const char *TSDB_FNAME_SUFFIX[] = {
H
Hongze Cheng 已提交
19 20 21 22 23
    "head",  // TSDB_FILE_HEAD
    "data",  // TSDB_FILE_DATA
    "last",  // TSDB_FILE_LAST
    "",      // TSDB_FILE_MAX
    "meta",  // TSDB_FILE_META
H
refact  
Hongze Cheng 已提交
24
};
H
TD-353  
Hongze Cheng 已提交
25

H
Hongze Cheng 已提交
26 27
static void tsdbGetFilename(int vid, int fid, uint32_t ver, TSDB_FILE_T ftype, char *fname);
// static int   tsdbRollBackMFile(SMFile *pMFile);
H
Hongze Cheng 已提交
28 29 30 31
static int   tsdbEncodeDFInfo(void **buf, SDFInfo *pInfo);
static void *tsdbDecodeDFInfo(void *buf, SDFInfo *pInfo);
static int   tsdbRollBackDFile(SDFile *pDFile);

H
Hongze Cheng 已提交
32
#if 0
H
refact  
Hongze Cheng 已提交
33
// ============== SMFile
34
void tsdbInitMFile(SMFile *pMFile, SDiskID did, int vid, uint32_t ver) {
H
refact  
Hongze Cheng 已提交
35 36
  char fname[TSDB_FILENAME_LEN];

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

H
Hongze Cheng 已提交
39 40
  memset(&(pMFile->info), 0, sizeof(pMFile->info));
  pMFile->info.magic = TSDB_FILE_INIT_MAGIC;
H
Hongze Cheng 已提交
41

42
  tsdbGetFilename(vid, 0, ver, TSDB_FILE_META, fname);
H
Hongze Cheng 已提交
43 44 45
  tfsInitFile(TSDB_FILE_F(pMFile), did.level, did.id, fname);
}

H
Hongze Cheng 已提交
46
void tsdbInitMFileEx(SMFile *pMFile, const SMFile *pOMFile) {
H
Hongze Cheng 已提交
47 48
  *pMFile = *pOMFile;
  TSDB_FILE_SET_CLOSED(pMFile);
H
Hongze Cheng 已提交
49
}
H
Hongze Cheng 已提交
50

H
refact  
Hongze Cheng 已提交
51
int tsdbEncodeSMFile(void **buf, SMFile *pMFile) {
H
Hongze Cheng 已提交
52
  int tlen = 0;
H
hzcheng 已提交
53

H
Hongze Cheng 已提交
54 55
  tlen += tsdbEncodeMFInfo(buf, &(pMFile->info));
  tlen += tfsEncodeFile(buf, &(pMFile->f));
H
hzcheng 已提交
56

H
Hongze Cheng 已提交
57
  return tlen;
H
Hongze Cheng 已提交
58 59
}

H
refact  
Hongze Cheng 已提交
60
void *tsdbDecodeSMFile(void *buf, SMFile *pMFile) {
H
Hongze Cheng 已提交
61 62
  buf = tsdbDecodeMFInfo(buf, &(pMFile->info));
  buf = tfsDecodeFile(buf, &(pMFile->f));
H
Hongze Cheng 已提交
63
  TSDB_FILE_SET_CLOSED(pMFile);
H
Hongze Cheng 已提交
64

H
Hongze Cheng 已提交
65 66
  return buf;
}
H
Hongze Cheng 已提交
67

H
Hongze Cheng 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
int tsdbEncodeSMFileEx(void **buf, SMFile *pMFile) {
  int tlen = 0;

  tlen += tsdbEncodeMFInfo(buf, &(pMFile->info));
  tlen += taosEncodeString(buf, TSDB_FILE_FULL_NAME(pMFile));

  return tlen;
}

void *tsdbDecodeSMFileEx(void *buf, SMFile *pMFile) {
  char *aname;
  buf = tsdbDecodeMFInfo(buf, &(pMFile->info));
  buf = taosDecodeString(buf, &aname);
  strncpy(TSDB_FILE_FULL_NAME(pMFile), aname, TSDB_FILENAME_LEN);
  TSDB_FILE_SET_CLOSED(pMFile);

  tfree(aname);

  return buf;
}

H
Hongze Cheng 已提交
89
int tsdbApplyMFileChange(SMFile *from, SMFile *to) {
H
Hongze Cheng 已提交
90
  if (from == NULL && to == NULL) return 0;
H
Hongze Cheng 已提交
91 92 93

  if (from != NULL) {
    if (to == NULL) {
H
Hongze Cheng 已提交
94
      return tsdbRemoveMFile(from);
H
Hongze Cheng 已提交
95 96 97
    } else {
      if (tfsIsSameFile(TSDB_FILE_F(from), TSDB_FILE_F(to))) {
        if (from->info.size > to->info.size) {
H
Hongze Cheng 已提交
98
          tsdbRollBackMFile(to);
H
Hongze Cheng 已提交
99 100
        }
      } else {
H
Hongze Cheng 已提交
101
        return tsdbRemoveMFile(from);
H
Hongze Cheng 已提交
102 103 104 105 106 107 108
      }
    }
  }

  return 0;
}

H
Hongze Cheng 已提交
109
int tsdbCreateMFile(SMFile *pMFile, bool updateHeader) {
H
Hongze Cheng 已提交
110 111
  ASSERT(pMFile->info.size == 0 && pMFile->info.magic == TSDB_FILE_INIT_MAGIC);

S
Shengliang Guan 已提交
112
  pMFile->fd = open(TSDB_FILE_FULL_NAME(pMFile), O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0755);
H
Hongze Cheng 已提交
113
  if (pMFile->fd < 0) {
H
Hongze Cheng 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
    if (errno == ENOENT) {
      // Try to create directory recursively
      char *s = strdup(TFILE_REL_NAME(&(pMFile->f)));
      if (tfsMkdirRecurAt(dirname(s), TSDB_FILE_LEVEL(pMFile), TSDB_FILE_ID(pMFile)) < 0) {
        tfree(s);
        return -1;
      }
      tfree(s);

      pMFile->fd = open(TSDB_FILE_FULL_NAME(pMFile), O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0755);
      if (pMFile->fd < 0) {
        terrno = TAOS_SYSTEM_ERROR(errno);
        return -1;
      }
    } else {
      terrno = TAOS_SYSTEM_ERROR(errno);
      return -1;
    }
H
Hongze Cheng 已提交
132 133
  }

H
Hongze Cheng 已提交
134 135 136 137
  if (!updateHeader) {
    return 0;
  }

H
Hongze Cheng 已提交
138 139
  pMFile->info.size += TSDB_FILE_HEAD_SIZE;

H
Hongze Cheng 已提交
140
  if (tsdbUpdateMFileHeader(pMFile) < 0) {
H
Hongze Cheng 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
    tsdbCloseMFile(pMFile);
    tsdbRemoveMFile(pMFile);
    return -1;
  }

  return 0;
}

int tsdbUpdateMFileHeader(SMFile *pMFile) {
  char buf[TSDB_FILE_HEAD_SIZE] = "\0";

  if (tsdbSeekMFile(pMFile, 0, SEEK_SET) < 0) {
    return -1;
  }

  void *ptr = buf;
H
Hongze Cheng 已提交
157
  tsdbEncodeMFInfo(&ptr, TSDB_FILE_INFO(pMFile));
H
Hongze Cheng 已提交
158

H
refact  
Hongze Cheng 已提交
159
  taosCalcChecksumAppend(0, (uint8_t *)buf, TSDB_FILE_HEAD_SIZE);
H
Hongze Cheng 已提交
160 161 162 163 164 165 166
  if (tsdbWriteMFile(pMFile, buf, TSDB_FILE_HEAD_SIZE) < 0) {
    return -1;
  }

  return 0;
}

H
Hongze Cheng 已提交
167 168 169 170 171 172 173 174 175 176 177 178 179
int tsdbLoadMFileHeader(SMFile *pMFile, SMFInfo *pInfo) {
  char buf[TSDB_FILE_HEAD_SIZE] = "\0";

  ASSERT(TSDB_FILE_OPENED(pMFile));

  if (tsdbSeekMFile(pMFile, 0, SEEK_SET) < 0) {
    return -1;
  }

  if (tsdbReadMFile(pMFile, buf, TSDB_FILE_HEAD_SIZE) < 0) {
    return -1;
  }

H
refact  
Hongze Cheng 已提交
180 181 182 183 184
  if (!taosCheckChecksumWhole((uint8_t *)buf, TSDB_FILE_HEAD_SIZE)) {
    terrno = TSDB_CODE_TDB_FILE_CORRUPTED;
    return -1;
  }

H
Hongze Cheng 已提交
185 186 187 188
  tsdbDecodeMFInfo(buf, pInfo);
  return 0;
}

H
Hongze Cheng 已提交
189
int tsdbScanAndTryFixMFile(STsdb *pRepo) {
H
Hongze Cheng 已提交
190
  SMFile *    pMFile = pRepo->fs->cstatus->pmf;
H
Hongze Cheng 已提交
191
  struct stat mfstat;
H
Hongze Cheng 已提交
192 193 194
  SMFile      mf;

  if (pMFile == NULL) {
H
refact  
Hongze Cheng 已提交
195
    // No meta file, no need to scan
H
Hongze Cheng 已提交
196 197 198
    return 0;
  }

H
refact  
Hongze Cheng 已提交
199
  tsdbInitMFileEx(&mf, pMFile);
H
Hongze Cheng 已提交
200 201 202 203 204

  if (access(TSDB_FILE_FULL_NAME(pMFile), F_OK) != 0) {
    tsdbError("vgId:%d meta file %s not exit, report to upper layer to fix it", REPO_ID(pRepo),
              TSDB_FILE_FULL_NAME(pMFile));
    pRepo->state |= TSDB_STATE_BAD_META;
H
Hongze Cheng 已提交
205
    TSDB_FILE_SET_STATE(pMFile, TSDB_FILE_STATE_BAD);
H
Hongze Cheng 已提交
206 207
    return 0;
  }
H
Hongze Cheng 已提交
208 209 210 211 212 213

  if (stat(TSDB_FILE_FULL_NAME(&mf), &mfstat) < 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }

H
Hongze Cheng 已提交
214
  if (pMFile->info.size < mfstat.st_size) {
H
Hongze Cheng 已提交
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
    if (tsdbOpenMFile(&mf, O_WRONLY) < 0) {
      return -1;
    }

    if (taosFtruncate(mf.fd, mf.info.size) < 0) {
      terrno = TAOS_SYSTEM_ERROR(errno);
      tsdbCloseMFile(&mf);
      return -1;
    }

    if (tsdbUpdateMFileHeader(&mf) < 0) {
      tsdbCloseMFile(&mf);
      return -1;
    }

    tsdbCloseMFile(&mf);
H
Hongze Cheng 已提交
231 232
    tsdbInfo("vgId:%d file %s is truncated from %" PRId64 " to %" PRId64, REPO_ID(pRepo), TSDB_FILE_FULL_NAME(pMFile),
             mfstat.st_size, pMFile->info.size);
H
Hongze Cheng 已提交
233
  } else if (pMFile->info.size > mfstat.st_size) {
H
Hongze Cheng 已提交
234 235 236
    tsdbError("vgId:%d meta file %s has wrong size %" PRId64 " expected %" PRId64 ", report to upper layer to fix it",
              REPO_ID(pRepo), TSDB_FILE_FULL_NAME(pMFile), mfstat.st_size, pMFile->info.size);
    pRepo->state |= TSDB_STATE_BAD_META;
H
Hongze Cheng 已提交
237
    TSDB_FILE_SET_STATE(pMFile, TSDB_FILE_STATE_BAD);
H
Hongze Cheng 已提交
238
    terrno = TSDB_CODE_TDB_FILE_CORRUPTED;
H
Hongze Cheng 已提交
239
    return 0;
H
refact  
Hongze Cheng 已提交
240 241
  } else {
    tsdbDebug("vgId:%d meta file %s passes the scan", REPO_ID(pRepo), TSDB_FILE_FULL_NAME(pMFile));
H
Hongze Cheng 已提交
242 243 244 245 246
  }

  return 0;
}

H
Hongze Cheng 已提交
247
int tsdbEncodeMFInfo(void **buf, SMFInfo *pInfo) {
H
Hongze Cheng 已提交
248
  int tlen = 0;
H
Hongze Cheng 已提交
249

H
Hongze Cheng 已提交
250 251 252 253 254
  tlen += taosEncodeVariantI64(buf, pInfo->size);
  tlen += taosEncodeVariantI64(buf, pInfo->tombSize);
  tlen += taosEncodeVariantI64(buf, pInfo->nRecords);
  tlen += taosEncodeVariantI64(buf, pInfo->nDels);
  tlen += taosEncodeFixedU32(buf, pInfo->magic);
H
Hongze Cheng 已提交
255

H
Hongze Cheng 已提交
256
  return tlen;
H
Hongze Cheng 已提交
257 258
}

H
Hongze Cheng 已提交
259
void *tsdbDecodeMFInfo(void *buf, SMFInfo *pInfo) {
H
Hongze Cheng 已提交
260 261 262 263 264 265 266
  buf = taosDecodeVariantI64(buf, &(pInfo->size));
  buf = taosDecodeVariantI64(buf, &(pInfo->tombSize));
  buf = taosDecodeVariantI64(buf, &(pInfo->nRecords));
  buf = taosDecodeVariantI64(buf, &(pInfo->nDels));
  buf = taosDecodeFixedU32(buf, &(pInfo->magic));

  return buf;
H
Hongze Cheng 已提交
267 268
}

H
Hongze Cheng 已提交
269
static int tsdbRollBackMFile(SMFile *pMFile) {
H
refact  
Hongze Cheng 已提交
270 271 272
  SMFile mf;

  tsdbInitMFileEx(&mf, pMFile);
H
Hongze Cheng 已提交
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294

  if (tsdbOpenMFile(&mf, O_WRONLY) < 0) {
    return -1;
  }

  if (taosFtruncate(TSDB_FILE_FD(&mf), pMFile->info.size) < 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    tsdbCloseMFile(&mf);
    return -1;
  }

  if (tsdbUpdateMFileHeader(&mf) < 0) {
    tsdbCloseMFile(&mf);
    return -1;
  }

  TSDB_FILE_FSYNC(&mf);

  tsdbCloseMFile(&mf);
  return 0;
}

H
Hongze Cheng 已提交
295 296
#endif

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

H
Hongze Cheng 已提交
301 302
  TSDB_FILE_SET_STATE(pDFile, TSDB_FILE_STATE_OK);

H
Hongze Cheng 已提交
303
  TSDB_FILE_SET_CLOSED(pDFile);
H
refact  
Hongze Cheng 已提交
304

H
Hongze Cheng 已提交
305 306
  memset(&(pDFile->info), 0, sizeof(pDFile->info));
  pDFile->info.magic = TSDB_FILE_INIT_MAGIC;
H
refact  
Hongze Cheng 已提交
307

S
Shengliang Guan 已提交
308 309
  tsdbGetFilename(pRepo->vgId, fid, ver, ftype, fname);
  tfsInitFile(pRepo->pTfs, &(pDFile->f), did, fname);
H
Hongze Cheng 已提交
310 311
}

H
Hongze Cheng 已提交
312 313
void tsdbInitDFileEx(SDFile *pDFile, SDFile *pODFile) {
  *pDFile = *pODFile;
H
Hongze Cheng 已提交
314 315 316
  TSDB_FILE_SET_CLOSED(pDFile);
}

H
refact  
Hongze Cheng 已提交
317
int tsdbEncodeSDFile(void **buf, SDFile *pDFile) {
H
Hongze Cheng 已提交
318
  int tlen = 0;
319

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

H
Hongze Cheng 已提交
323
  return tlen;
H
TD-34  
hzcheng 已提交
324 325
}

S
Shengliang Guan 已提交
326
void *tsdbDecodeSDFile(STsdb *pRepo, void *buf, SDFile *pDFile) {
H
Hongze Cheng 已提交
327
  buf = tsdbDecodeDFInfo(buf, &(pDFile->info));
S
Shengliang Guan 已提交
328
  buf = tfsDecodeFile(pRepo->pTfs, buf, &(pDFile->f));
H
Hongze Cheng 已提交
329
  TSDB_FILE_SET_CLOSED(pDFile);
H
TD-353  
Hongze Cheng 已提交
330

H
Hongze Cheng 已提交
331
  return buf;
H
TD-353  
Hongze Cheng 已提交
332
}
H
TD-353  
Hongze Cheng 已提交
333

H
Hongze Cheng 已提交
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
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) {
  char *aname;

  buf = tsdbDecodeDFInfo(buf, &(pDFile->info));
  buf = taosDecodeString(buf, &aname);
  strncpy(TSDB_FILE_FULL_NAME(pDFile), aname, TSDB_FILENAME_LEN);
  TSDB_FILE_SET_CLOSED(pDFile);
  tfree(aname);

  return buf;
}

S
Shengliang Guan 已提交
355
int tsdbCreateDFile(STsdb *pRepo, SDFile *pDFile, bool updateHeader) {
H
Hongze Cheng 已提交
356 357
  ASSERT(pDFile->info.size == 0 && pDFile->info.magic == TSDB_FILE_INIT_MAGIC);

S
Shengliang Guan 已提交
358
  pDFile->fd = open(TSDB_FILE_FULL_NAME(pDFile), O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0755);
H
Hongze Cheng 已提交
359
  if (pDFile->fd < 0) {
H
Hongze Cheng 已提交
360 361
    if (errno == ENOENT) {
      // Try to create directory recursively
S
Shengliang Guan 已提交
362 363
      char *s = strdup(TSDB_FILE_REL_NAME(pDFile));
      if (tfsMkdirRecurAt(pRepo->pTfs, dirname(s), TSDB_FILE_DID(pDFile)) < 0) {
H
Hongze Cheng 已提交
364 365 366 367 368 369 370 371 372 373 374 375 376 377
        tfree(s);
        return -1;
      }
      tfree(s);

      pDFile->fd = open(TSDB_FILE_FULL_NAME(pDFile), O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0755);
      if (pDFile->fd < 0) {
        terrno = TAOS_SYSTEM_ERROR(errno);
        return -1;
      }
    } else {
      terrno = TAOS_SYSTEM_ERROR(errno);
      return -1;
    }
H
Hongze Cheng 已提交
378 379
  }

H
Hongze Cheng 已提交
380 381 382 383
  if (!updateHeader) {
    return 0;
  }

H
Hongze Cheng 已提交
384 385
  pDFile->info.size += TSDB_FILE_HEAD_SIZE;

H
Hongze Cheng 已提交
386
  if (tsdbUpdateDFileHeader(pDFile) < 0) {
H
Hongze Cheng 已提交
387 388 389 390 391
    tsdbCloseDFile(pDFile);
    tsdbRemoveDFile(pDFile);
    return -1;
  }

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

H
Hongze Cheng 已提交
395 396 397 398 399 400
int tsdbUpdateDFileHeader(SDFile *pDFile) {
  char buf[TSDB_FILE_HEAD_SIZE] = "\0";

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

H
Hongze Cheng 已提交
402
  void *ptr = buf;
H
Hongze Cheng 已提交
403
  taosEncodeFixedU32(&ptr, 0);
H
Hongze Cheng 已提交
404
  tsdbEncodeDFInfo(&ptr, &(pDFile->info));
H
Hongze Cheng 已提交
405

H
refact  
Hongze Cheng 已提交
406
  taosCalcChecksumAppend(0, (uint8_t *)buf, TSDB_FILE_HEAD_SIZE);
H
Hongze Cheng 已提交
407
  if (tsdbWriteDFile(pDFile, buf, TSDB_FILE_HEAD_SIZE) < 0) {
H
Hongze Cheng 已提交
408 409
    return -1;
  }
H
Hongze Cheng 已提交
410 411

  return 0;
H
Hongze Cheng 已提交
412 413
}

H
Hongze Cheng 已提交
414
int tsdbLoadDFileHeader(SDFile *pDFile, SDFInfo *pInfo) {
H
Hongze Cheng 已提交
415
  char     buf[TSDB_FILE_HEAD_SIZE] = "\0";
416
  uint32_t _version;
H
Hongze Cheng 已提交
417 418 419 420 421 422 423 424 425 426 427

  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 已提交
428 429 430 431 432
  if (!taosCheckChecksumWhole((uint8_t *)buf, TSDB_FILE_HEAD_SIZE)) {
    terrno = TSDB_CODE_TDB_FILE_CORRUPTED;
    return -1;
  }

H
Hongze Cheng 已提交
433
  void *pBuf = buf;
434
  pBuf = taosDecodeFixedU32(pBuf, &_version);
H
Hongze Cheng 已提交
435
  pBuf = tsdbDecodeDFInfo(pBuf, pInfo);
H
Hongze Cheng 已提交
436 437 438
  return 0;
}

H
Hongze Cheng 已提交
439
static int tsdbScanAndTryFixDFile(STsdb *pRepo, SDFile *pDFile) {
H
Hongze Cheng 已提交
440
  struct stat dfstat;
H
refact  
Hongze Cheng 已提交
441 442 443
  SDFile      df;

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

H
Hongze Cheng 已提交
445 446 447
  if (access(TSDB_FILE_FULL_NAME(pDFile), F_OK) != 0) {
    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 已提交
448
    // pRepo->state |= TSDB_STATE_BAD_DATA;
H
Hongze Cheng 已提交
449
    TSDB_FILE_SET_STATE(pDFile, TSDB_FILE_STATE_BAD);
H
Hongze Cheng 已提交
450 451 452
    return 0;
  }

H
Hongze Cheng 已提交
453 454 455 456 457
  if (stat(TSDB_FILE_FULL_NAME(&df), &dfstat) < 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }

H
Hongze Cheng 已提交
458
  if (pDFile->info.size < dfstat.st_size) {
H
Hongze Cheng 已提交
459 460 461 462
    if (tsdbOpenDFile(&df, O_WRONLY) < 0) {
      return -1;
    }

H
Hongze Cheng 已提交
463
    if (taosFtruncateFile(df.fd, df.info.size) < 0) {
H
Hongze Cheng 已提交
464 465 466 467 468 469 470 471 472 473 474
      terrno = TAOS_SYSTEM_ERROR(errno);
      tsdbCloseDFile(&df);
      return -1;
    }

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

    tsdbCloseDFile(&df);
H
Hongze Cheng 已提交
475 476
    tsdbInfo("vgId:%d file %s is truncated from %" PRId64 " to %" PRId64, REPO_ID(pRepo), TSDB_FILE_FULL_NAME(pDFile),
             dfstat.st_size, pDFile->info.size);
H
Hongze Cheng 已提交
477
  } else if (pDFile->info.size > dfstat.st_size) {
H
Hongze Cheng 已提交
478 479
    tsdbError("vgId:%d data file %s has wrong size %" PRId64 " expected %" PRId64 ", report to upper layer to fix it",
              REPO_ID(pRepo), TSDB_FILE_FULL_NAME(pDFile), dfstat.st_size, pDFile->info.size);
H
Hongze Cheng 已提交
480
    // pRepo->state |= TSDB_STATE_BAD_DATA;
H
Hongze Cheng 已提交
481
    TSDB_FILE_SET_STATE(pDFile, TSDB_FILE_STATE_BAD);
H
Hongze Cheng 已提交
482
    terrno = TSDB_CODE_TDB_FILE_CORRUPTED;
H
Hongze Cheng 已提交
483
    return 0;
H
refact  
Hongze Cheng 已提交
484 485
  } else {
    tsdbDebug("vgId:%d file %s passes the scan", REPO_ID(pRepo), TSDB_FILE_FULL_NAME(pDFile));
H
Hongze Cheng 已提交
486 487 488 489 490
  }

  return 0;
}

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

H
Hongze Cheng 已提交
494
  tlen += taosEncodeFixedU32(buf, pInfo->magic);
H
TD-353  
Hongze Cheng 已提交
495 496 497
  tlen += taosEncodeFixedU32(buf, pInfo->len);
  tlen += taosEncodeFixedU32(buf, pInfo->totalBlocks);
  tlen += taosEncodeFixedU32(buf, pInfo->totalSubBlocks);
H
Hongze Cheng 已提交
498 499 500
  tlen += taosEncodeFixedU32(buf, pInfo->offset);
  tlen += taosEncodeFixedU64(buf, pInfo->size);
  tlen += taosEncodeFixedU64(buf, pInfo->tombSize);
H
TD-353  
Hongze Cheng 已提交
501

H
TD-353  
Hongze Cheng 已提交
502
  return tlen;
H
TD-353  
Hongze Cheng 已提交
503 504
}

H
Hongze Cheng 已提交
505
static void *tsdbDecodeDFInfo(void *buf, SDFInfo *pInfo) {
H
Hongze Cheng 已提交
506
  buf = taosDecodeFixedU32(buf, &(pInfo->magic));
H
TD-353  
Hongze Cheng 已提交
507 508 509
  buf = taosDecodeFixedU32(buf, &(pInfo->len));
  buf = taosDecodeFixedU32(buf, &(pInfo->totalBlocks));
  buf = taosDecodeFixedU32(buf, &(pInfo->totalSubBlocks));
H
Hongze Cheng 已提交
510 511 512
  buf = taosDecodeFixedU32(buf, &(pInfo->offset));
  buf = taosDecodeFixedU64(buf, &(pInfo->size));
  buf = taosDecodeFixedU64(buf, &(pInfo->tombSize));
H
TD-353  
Hongze Cheng 已提交
513 514 515 516

  return buf;
}

H
Hongze Cheng 已提交
517 518 519 520 521 522 523 524 525
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 已提交
526
          tsdbRollBackDFile(to);
H
Hongze Cheng 已提交
527 528
        }
      } else {
529
        (void)tsdbRemoveDFile(from);
H
Hongze Cheng 已提交
530 531 532 533 534 535 536
      }
    }
  }

  return 0;
}

H
Hongze Cheng 已提交
537
static int tsdbRollBackDFile(SDFile *pDFile) {
H
Hongze Cheng 已提交
538 539 540 541 542 543
  SDFile df = *pDFile;

  if (tsdbOpenDFile(&df, O_WRONLY) < 0) {
    return -1;
  }

H
Hongze Cheng 已提交
544
  if (taosFtruncateFile(TSDB_FILE_FD(&df), pDFile->info.size) < 0) {
H
Hongze Cheng 已提交
545 546 547 548 549 550 551 552 553 554
    terrno = TAOS_SYSTEM_ERROR(errno);
    tsdbCloseDFile(&df);
    return -1;
  }

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

H
Hongze Cheng 已提交
555 556
  TSDB_FILE_FSYNC(&df);

H
Hongze Cheng 已提交
557 558 559 560
  tsdbCloseDFile(&df);
  return 0;
}

H
Hongze Cheng 已提交
561
// ============== Operations on SDFileSet
S
Shengliang Guan 已提交
562
void tsdbInitDFileSet(STsdb *pRepo, SDFileSet *pSet, SDiskID did, int fid, uint32_t ver) {
H
Hongze Cheng 已提交
563 564
  pSet->fid = fid;
  pSet->state = 0;
H
Hongze Cheng 已提交
565

H
Hongze Cheng 已提交
566 567
  for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
    SDFile *pDFile = TSDB_DFILE_IN_SET(pSet, ftype);
S
Shengliang Guan 已提交
568
    tsdbInitDFile(pRepo, pDFile, did, fid, ver, ftype);
H
Hongze Cheng 已提交
569 570 571
  }
}

H
Hongze Cheng 已提交
572
void tsdbInitDFileSetEx(SDFileSet *pSet, SDFileSet *pOSet) {
H
Hongze Cheng 已提交
573
  pSet->fid = pOSet->fid;
H
Hongze Cheng 已提交
574
  for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
H
Hongze Cheng 已提交
575
    tsdbInitDFileEx(TSDB_DFILE_IN_SET(pSet, ftype), TSDB_DFILE_IN_SET(pOSet, ftype));
H
Hongze Cheng 已提交
576
  }
H
Hongze Cheng 已提交
577 578
}

H
Hongze Cheng 已提交
579 580
int tsdbEncodeDFileSet(void **buf, SDFileSet *pSet) {
  int tlen = 0;
H
Hongze Cheng 已提交
581

H
Hongze Cheng 已提交
582
  tlen += taosEncodeFixedI32(buf, pSet->fid);
H
Hongze Cheng 已提交
583 584
  for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
    tlen += tsdbEncodeSDFile(buf, TSDB_DFILE_IN_SET(pSet, ftype));
H
Hongze Cheng 已提交
585
  }
H
Hongze Cheng 已提交
586

H
Hongze Cheng 已提交
587
  return tlen;
H
Hongze Cheng 已提交
588 589
}

S
Shengliang Guan 已提交
590
void *tsdbDecodeDFileSet(STsdb *pRepo, void *buf, SDFileSet *pSet) {
H
Hongze Cheng 已提交
591 592 593
  int32_t fid;

  buf = taosDecodeFixedI32(buf, &(fid));
H
refact  
Hongze Cheng 已提交
594
  pSet->state = 0;
H
Hongze Cheng 已提交
595
  pSet->fid = fid;
H
Hongze Cheng 已提交
596
  for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
S
Shengliang Guan 已提交
597
    buf = tsdbDecodeSDFile(pRepo, buf, TSDB_DFILE_IN_SET(pSet, ftype));
H
Hongze Cheng 已提交
598
  }
H
Hongze Cheng 已提交
599
  return buf;
H
Hongze Cheng 已提交
600 601
}

H
Hongze Cheng 已提交
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623
int tsdbEncodeDFileSetEx(void **buf, SDFileSet *pSet) {
  int tlen = 0;

  tlen += taosEncodeFixedI32(buf, pSet->fid);
  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) {
  int32_t fid;

  buf = taosDecodeFixedI32(buf, &(fid));
  pSet->fid = fid;
  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 已提交
624
int tsdbApplyDFileSetChange(SDFileSet *from, SDFileSet *to) {
H
Hongze Cheng 已提交
625
  for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
H
Hongze Cheng 已提交
626 627 628
    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 已提交
629 630 631 632 633 634 635
      return -1;
    }
  }

  return 0;
}

S
Shengliang Guan 已提交
636
int tsdbCreateDFileSet(STsdb *pRepo, SDFileSet *pSet, bool updateHeader) {
H
Hongze Cheng 已提交
637
  for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
S
Shengliang Guan 已提交
638
    if (tsdbCreateDFile(pRepo, TSDB_DFILE_IN_SET(pSet, ftype), updateHeader) < 0) {
H
Hongze Cheng 已提交
639 640 641 642 643 644
      tsdbCloseDFileSet(pSet);
      tsdbRemoveDFileSet(pSet);
      return -1;
    }
  }

H
Hongze Cheng 已提交
645 646
  return 0;
}
H
Hongze Cheng 已提交
647

H
Hongze Cheng 已提交
648
int tsdbUpdateDFileSetHeader(SDFileSet *pSet) {
H
Hongze Cheng 已提交
649
  for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
H
Hongze Cheng 已提交
650
    if (tsdbUpdateDFileHeader(TSDB_DFILE_IN_SET(pSet, ftype)) < 0) {
H
Hongze Cheng 已提交
651 652 653
      return -1;
    }
  }
H
Hongze Cheng 已提交
654
  return 0;
H
refact  
Hongze Cheng 已提交
655 656
}

H
Hongze Cheng 已提交
657
int tsdbScanAndTryFixDFileSet(STsdb *pRepo, SDFileSet *pSet) {
H
Hongze Cheng 已提交
658
  for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
H
Hongze Cheng 已提交
659
    if (tsdbScanAndTryFixDFile(pRepo, TSDB_DFILE_IN_SET(pSet, ftype)) < 0) {
H
Hongze Cheng 已提交
660 661 662 663 664 665
      return -1;
    }
  }
  return 0;
}

666
int tsdbParseDFilename(const char *fname, int *vid, int *fid, TSDB_FILE_T *ftype, uint32_t *_version) {
H
Hongze Cheng 已提交
667
  char *p = NULL;
668
  *_version = 0;
H
Hongze Cheng 已提交
669 670
  *ftype = TSDB_FILE_MAX;

671
  sscanf(fname, "v%df%d.%m[a-z]-ver%" PRIu32, vid, fid, &p, _version);
H
Hongze Cheng 已提交
672 673 674 675 676 677 678
  for (TSDB_FILE_T i = 0; i < TSDB_FILE_MAX; i++) {
    if (strcmp(p, TSDB_FNAME_SUFFIX[i]) == 0) {
      *ftype = i;
      break;
    }
  }

H
Hongze Cheng 已提交
679
  tfree(p);
H
Hongze Cheng 已提交
680 681 682
  return 0;
}

H
Hongze Cheng 已提交
683
static void tsdbGetFilename(int vid, int fid, uint32_t ver, TSDB_FILE_T ftype, char *fname) {
H
refact  
Hongze Cheng 已提交
684 685 686 687
  ASSERT(ftype != TSDB_FILE_MAX);

  if (ftype < TSDB_FILE_MAX) {
    if (ver == 0) {
H
Hongze Cheng 已提交
688
      snprintf(fname, TSDB_FILENAME_LEN, "vnode/vnode%d/tsdb/data/v%df%d.%s", vid, vid, fid, TSDB_FNAME_SUFFIX[ftype]);
H
refact  
Hongze Cheng 已提交
689
    } else {
H
Hongze Cheng 已提交
690
      snprintf(fname, TSDB_FILENAME_LEN, "vnode/vnode%d/tsdb/data/v%df%d.%s-ver%" PRIu32, vid, vid, fid,
H
Hongze Cheng 已提交
691
               TSDB_FNAME_SUFFIX[ftype], ver);
H
refact  
Hongze Cheng 已提交
692 693 694
    }
  } else {
    if (ver == 0) {
H
Hongze Cheng 已提交
695
      snprintf(fname, TSDB_FILENAME_LEN, "vnode/vnode%d/tsdb/%s", vid, TSDB_FNAME_SUFFIX[ftype]);
H
refact  
Hongze Cheng 已提交
696
    } else {
H
Hongze Cheng 已提交
697
      snprintf(fname, TSDB_FILENAME_LEN, "vnode/vnode%d/tsdb/%s-ver%" PRIu32, vid, TSDB_FNAME_SUFFIX[ftype], ver);
H
refact  
Hongze Cheng 已提交
698 699
    }
  }
H
Hongze Cheng 已提交
700
}