tsdbFile.c 7.7 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
Hongze Cheng 已提交
18
int32_t tPutHeadFile(uint8_t *p, SHeadFile *pHeadFile) {
H
Hongze Cheng 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
  int32_t n = 0;

  n += tPutI64v(p ? p + n : p, pHeadFile->commitID);
  n += tPutI64v(p ? p + n : p, pHeadFile->size);
  n += tPutI64v(p ? p + n : p, pHeadFile->offset);

  return n;
}

static int32_t tGetHeadFile(uint8_t *p, SHeadFile *pHeadFile) {
  int32_t n = 0;

  n += tGetI64v(p + n, &pHeadFile->commitID);
  n += tGetI64v(p + n, &pHeadFile->size);
  n += tGetI64v(p + n, &pHeadFile->offset);

  return n;
}

H
Hongze Cheng 已提交
38
int32_t tPutDataFile(uint8_t *p, SDataFile *pDataFile) {
H
Hongze Cheng 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
  int32_t n = 0;

  n += tPutI64v(p ? p + n : p, pDataFile->commitID);
  n += tPutI64v(p ? p + n : p, pDataFile->size);

  return n;
}

static int32_t tGetDataFile(uint8_t *p, SDataFile *pDataFile) {
  int32_t n = 0;

  n += tGetI64v(p + n, &pDataFile->commitID);
  n += tGetI64v(p + n, &pDataFile->size);

  return n;
}

H
Hongze Cheng 已提交
56
int32_t tPutSttFile(uint8_t *p, SSttFile *pSttFile) {
H
Hongze Cheng 已提交
57 58
  int32_t n = 0;

H
Hongze Cheng 已提交
59 60 61
  n += tPutI64v(p ? p + n : p, pSttFile->commitID);
  n += tPutI64v(p ? p + n : p, pSttFile->size);
  n += tPutI64v(p ? p + n : p, pSttFile->offset);
H
Hongze Cheng 已提交
62 63 64 65

  return n;
}

H
Hongze Cheng 已提交
66
static int32_t tGetSttFile(uint8_t *p, SSttFile *pSttFile) {
H
Hongze Cheng 已提交
67 68
  int32_t n = 0;

H
Hongze Cheng 已提交
69 70 71
  n += tGetI64v(p + n, &pSttFile->commitID);
  n += tGetI64v(p + n, &pSttFile->size);
  n += tGetI64v(p + n, &pSttFile->offset);
H
Hongze Cheng 已提交
72 73 74 75

  return n;
}

H
Hongze Cheng 已提交
76
int32_t tPutSmaFile(uint8_t *p, SSmaFile *pSmaFile) {
H
Hongze Cheng 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
  int32_t n = 0;

  n += tPutI64v(p ? p + n : p, pSmaFile->commitID);
  n += tPutI64v(p ? p + n : p, pSmaFile->size);

  return n;
}

static int32_t tGetSmaFile(uint8_t *p, SSmaFile *pSmaFile) {
  int32_t n = 0;

  n += tGetI64v(p + n, &pSmaFile->commitID);
  n += tGetI64v(p + n, &pSmaFile->size);

  return n;
}

// EXPOSED APIS ==================================================
H
Haojun Liao 已提交
95
static char* getFileNamePrefix(STsdb *pTsdb, SDiskID did, int32_t fid, uint64_t commitId, char fname[]) {
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
  const char* p1 = tfsGetDiskPath(pTsdb->pVnode->pTfs, did);
  int32_t len = strlen(p1);

  char* p = memcpy(fname, p1, len);
  p += len;

  *(p++) = TD_DIRSEP[0];
  len = strlen(pTsdb->path);

  memcpy(p, pTsdb->path, len);
  p += len;

  *(p++) = TD_DIRSEP[0];
  *(p++) = 'v';

111
  p += titoa(TD_VID(pTsdb->pVnode), 10, p);
112 113
  *(p++) = 'f';

114
  p += titoa(fid, 10, p);
115 116 117 118

  memcpy(p, "ver", 3);
  p += 3;

H
Haojun Liao 已提交
119 120 121 122 123 124
  p += titoa(commitId, 10, p);
  return p;
}

void tsdbHeadFileName(STsdb *pTsdb, SDiskID did, int32_t fid, SHeadFile *pHeadF, char fname[]) {
  char* p = getFileNamePrefix(pTsdb, did, fid, pHeadF->commitID, fname);
125 126
  memcpy(p, ".head", 5);
  p[5] = 0;
H
Hongze Cheng 已提交
127
}
H
Hongze Cheng 已提交
128

H
Hongze Cheng 已提交
129
void tsdbDataFileName(STsdb *pTsdb, SDiskID did, int32_t fid, SDataFile *pDataF, char fname[]) {
H
Haojun Liao 已提交
130 131 132
  char* p = getFileNamePrefix(pTsdb, did, fid, pDataF->commitID, fname);
  memcpy(p, ".data", 5);
  p[5] = 0;
H
Hongze Cheng 已提交
133
}
H
Hongze Cheng 已提交
134

H
Hongze Cheng 已提交
135
void tsdbSttFileName(STsdb *pTsdb, SDiskID did, int32_t fid, SSttFile *pSttF, char fname[]) {
H
Haojun Liao 已提交
136 137 138
  char* p = getFileNamePrefix(pTsdb, did, fid, pSttF->commitID, fname);
  memcpy(p, ".stt", 4);
  p[4] = 0;
H
Hongze Cheng 已提交
139
}
H
Hongze Cheng 已提交
140

H
Hongze Cheng 已提交
141
void tsdbSmaFileName(STsdb *pTsdb, SDiskID did, int32_t fid, SSmaFile *pSmaF, char fname[]) {
H
Haojun Liao 已提交
142 143 144
  char* p = getFileNamePrefix(pTsdb, did, fid, pSmaF->commitID, fname);
  memcpy(p, ".sma", 4);
  p[4] = 0;
H
Hongze Cheng 已提交
145 146
}

H
Hongze Cheng 已提交
147 148
bool tsdbDelFileIsSame(SDelFile *pDelFile1, SDelFile *pDelFile2) { return pDelFile1->commitID == pDelFile2->commitID; }

H
Hongze Cheng 已提交
149 150
int32_t tsdbDFileRollback(STsdb *pTsdb, SDFileSet *pSet, EDataFileT ftype) {
  int32_t   code = 0;
H
Haojun Liao 已提交
151
  int64_t   size = 0;
H
Hongze Cheng 已提交
152
  int64_t   n;
H
Hongze Cheng 已提交
153
  TdFilePtr pFD;
H
Haojun Liao 已提交
154
  char      fname[TSDB_FILENAME_LEN] = {0};
H
Hongze Cheng 已提交
155
  char      hdr[TSDB_FHDR_SIZE] = {0};
H
Hongze Cheng 已提交
156 157 158 159

  // truncate
  switch (ftype) {
    case TSDB_DATA_FILE:
H
Hongze Cheng 已提交
160 161 162
      size = pSet->pDataF->size;
      tsdbDataFileName(pTsdb, pSet->diskId, pSet->fid, pSet->pDataF, fname);
      tPutDataFile(hdr, pSet->pDataF);
H
Hongze Cheng 已提交
163 164
      break;
    case TSDB_SMA_FILE:
H
Hongze Cheng 已提交
165 166 167
      size = pSet->pSmaF->size;
      tsdbSmaFileName(pTsdb, pSet->diskId, pSet->fid, pSet->pSmaF, fname);
      tPutSmaFile(hdr, pSet->pSmaF);
H
Hongze Cheng 已提交
168 169 170 171
      break;
    default:
      ASSERT(0);
  }
H
Hongze Cheng 已提交
172 173 174 175 176 177 178 179 180 181 182

  taosCalcChecksumAppend(0, hdr, TSDB_FHDR_SIZE);

  // open
  pFD = taosOpenFile(fname, TD_FILE_WRITE);
  if (pFD == NULL) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

  // ftruncate
H
Hongze Cheng 已提交
183
  if (taosFtruncateFile(pFD, tsdbLogicToFileSize(size, pTsdb->pVnode->config.tsdbPageSize)) < 0) {
H
Hongze Cheng 已提交
184 185 186 187 188
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

  // update header
H
Hongze Cheng 已提交
189 190 191 192 193 194 195 196 197 198 199
  n = taosLSeekFile(pFD, 0, SEEK_SET);
  if (n < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

  n = taosWriteFile(pFD, hdr, TSDB_FHDR_SIZE);
  if (n < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }
H
Hongze Cheng 已提交
200 201 202 203 204 205 206 207 208 209 210 211 212

  // sync
  if (taosFsyncFile(pFD) < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

  // close
  taosCloseFile(&pFD);

  return code;

_err:
S
Shengliang Guan 已提交
213
  tsdbError("vgId:%d, tsdb rollback file failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
214 215 216
  return code;
}

H
Hongze Cheng 已提交
217 218 219 220 221 222
int32_t tPutDFileSet(uint8_t *p, SDFileSet *pSet) {
  int32_t n = 0;

  n += tPutI32v(p ? p + n : p, pSet->diskId.level);
  n += tPutI32v(p ? p + n : p, pSet->diskId.id);
  n += tPutI32v(p ? p + n : p, pSet->fid);
H
Hongze Cheng 已提交
223 224

  // data
H
Hongze Cheng 已提交
225 226 227
  n += tPutHeadFile(p ? p + n : p, pSet->pHeadF);
  n += tPutDataFile(p ? p + n : p, pSet->pDataF);
  n += tPutSmaFile(p ? p + n : p, pSet->pSmaF);
H
Hongze Cheng 已提交
228

H
Hongze Cheng 已提交
229 230 231 232
  // stt
  n += tPutU8(p ? p + n : p, pSet->nSttF);
  for (int32_t iStt = 0; iStt < pSet->nSttF; iStt++) {
    n += tPutSttFile(p ? p + n : p, pSet->aSttF[iStt]);
H
Hongze Cheng 已提交
233
  }
H
Hongze Cheng 已提交
234

H
Hongze Cheng 已提交
235 236
  return n;
}
H
Hongze Cheng 已提交
237

H
Hongze Cheng 已提交
238 239
int32_t tGetDFileSet(uint8_t *p, SDFileSet *pSet) {
  int32_t n = 0;
H
Hongze Cheng 已提交
240

H
Hongze Cheng 已提交
241 242 243
  n += tGetI32v(p + n, &pSet->diskId.level);
  n += tGetI32v(p + n, &pSet->diskId.id);
  n += tGetI32v(p + n, &pSet->fid);
H
Hongze Cheng 已提交
244

H
Hongze Cheng 已提交
245 246 247 248 249 250
  // head
  pSet->pHeadF = (SHeadFile *)taosMemoryCalloc(1, sizeof(SHeadFile));
  if (pSet->pHeadF == NULL) {
    return -1;
  }
  pSet->pHeadF->nRef = 1;
H
Hongze Cheng 已提交
251
  n += tGetHeadFile(p + n, pSet->pHeadF);
H
Hongze Cheng 已提交
252 253 254 255 256 257 258

  // data
  pSet->pDataF = (SDataFile *)taosMemoryCalloc(1, sizeof(SDataFile));
  if (pSet->pDataF == NULL) {
    return -1;
  }
  pSet->pDataF->nRef = 1;
H
Hongze Cheng 已提交
259
  n += tGetDataFile(p + n, pSet->pDataF);
H
Hongze Cheng 已提交
260 261 262 263 264 265 266

  // sma
  pSet->pSmaF = (SSmaFile *)taosMemoryCalloc(1, sizeof(SSmaFile));
  if (pSet->pSmaF == NULL) {
    return -1;
  }
  pSet->pSmaF->nRef = 1;
H
Hongze Cheng 已提交
267
  n += tGetSmaFile(p + n, pSet->pSmaF);
H
Hongze Cheng 已提交
268

H
Hongze Cheng 已提交
269 270 271 272 273
  // stt
  n += tGetU8(p + n, &pSet->nSttF);
  for (int32_t iStt = 0; iStt < pSet->nSttF; iStt++) {
    pSet->aSttF[iStt] = (SSttFile *)taosMemoryCalloc(1, sizeof(SSttFile));
    if (pSet->aSttF[iStt] == NULL) {
H
Hongze Cheng 已提交
274 275
      return -1;
    }
H
Hongze Cheng 已提交
276 277
    pSet->aSttF[iStt]->nRef = 1;
    n += tGetSttFile(p + n, pSet->aSttF[iStt]);
H
Hongze Cheng 已提交
278
  }
H
Hongze Cheng 已提交
279

H
Hongze Cheng 已提交
280 281
  return n;
}
H
Hongze Cheng 已提交
282 283 284

// SDelFile ===============================================
void tsdbDelFileName(STsdb *pTsdb, SDelFile *pFile, char fname[]) {
H
Hongze Cheng 已提交
285 286
  snprintf(fname, TSDB_FILENAME_LEN - 1, "%s%s%s%sv%dver%" PRId64 "%s", tfsGetPrimaryPath(pTsdb->pVnode->pTfs),
           TD_DIRSEP, pTsdb->path, TD_DIRSEP, TD_VID(pTsdb->pVnode), pFile->commitID, ".del");
H
Hongze Cheng 已提交
287 288 289 290 291
}

int32_t tPutDelFile(uint8_t *p, SDelFile *pDelFile) {
  int32_t n = 0;

H
Hongze Cheng 已提交
292
  n += tPutI64v(p ? p + n : p, pDelFile->commitID);
H
Hongze Cheng 已提交
293 294 295 296 297 298 299 300 301
  n += tPutI64v(p ? p + n : p, pDelFile->size);
  n += tPutI64v(p ? p + n : p, pDelFile->offset);

  return n;
}

int32_t tGetDelFile(uint8_t *p, SDelFile *pDelFile) {
  int32_t n = 0;

H
Hongze Cheng 已提交
302
  n += tGetI64v(p + n, &pDelFile->commitID);
H
Hongze Cheng 已提交
303 304 305 306 307
  n += tGetI64v(p + n, &pDelFile->size);
  n += tGetI64v(p + n, &pDelFile->offset);

  return n;
}