tsdbFile.c 7.9 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"
17
#include "vnd.h"
H
TD-353  
Hongze Cheng 已提交
18

H
Hongze Cheng 已提交
19
int32_t tPutHeadFile(uint8_t *p, SHeadFile *pHeadFile) {
H
Hongze Cheng 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
  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 已提交
39
int32_t tPutDataFile(uint8_t *p, SDataFile *pDataFile) {
H
Hongze Cheng 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
  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 已提交
57
int32_t tPutSttFile(uint8_t *p, SSttFile *pSttFile) {
H
Hongze Cheng 已提交
58 59
  int32_t n = 0;

H
Hongze Cheng 已提交
60 61 62
  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 已提交
63 64 65 66

  return n;
}

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

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

  return n;
}

H
Hongze Cheng 已提交
77
int32_t tPutSmaFile(uint8_t *p, SSmaFile *pSmaFile) {
H
Hongze Cheng 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
  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 ==================================================
96 97 98
static char* getFileNamePrefix(STsdb *pTsdb, SDiskID did, int32_t fid, uint64_t commitId, char fname[]) {
  const char* p1 = tfsGetDiskPath(pTsdb->pVnode->pTfs, did);
  int32_t len = strlen(p1);
99

100
  char* p = memcpy(fname, p1, len);
101 102 103 104 105 106 107 108 109 110 111
  p += len;

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

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

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

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

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

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

H
Haojun Liao 已提交
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[]) {
125
  char* p = getFileNamePrefix(pTsdb, did, fid, pHeadF->commitID, fname);
126 127
  memcpy(p, ".head", 5);
  p[5] = 0;
H
Hongze Cheng 已提交
128
}
H
Hongze Cheng 已提交
129

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

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

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

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

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

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

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

  // update header
H
Hongze Cheng 已提交
190 191 192 193 194 195 196 197 198 199 200
  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 已提交
201 202 203 204 205 206 207 208 209 210 211 212 213

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

  // close
  taosCloseFile(&pFD);

  return code;

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

H
Hongze Cheng 已提交
218 219 220 221 222 223
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 已提交
224 225

  // data
H
Hongze Cheng 已提交
226 227 228
  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 已提交
229

H
Hongze Cheng 已提交
230 231 232 233
  // 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 已提交
234
  }
H
Hongze Cheng 已提交
235

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

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

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

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

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

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

H
Hongze Cheng 已提交
270 271 272 273 274
  // 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 已提交
275 276
      return -1;
    }
H
Hongze Cheng 已提交
277 278
    pSet->aSttF[iStt]->nRef = 1;
    n += tGetSttFile(p + n, pSet->aSttF[iStt]);
H
Hongze Cheng 已提交
279
  }
H
Hongze Cheng 已提交
280

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

// SDelFile ===============================================
void tsdbDelFileName(STsdb *pTsdb, SDelFile *pFile, char fname[]) {
286 287
  int32_t offset = 0;

288
  vnodeGetPrimaryDir(pTsdb->path, pTsdb->pVnode->pTfs, fname, TSDB_FILENAME_LEN);
289 290 291
  offset = strlen(fname);
  snprintf((char *)fname + offset, TSDB_FILENAME_LEN - offset - 1, "%sv%dver%" PRId64 ".del", TD_DIRSEP,
           TD_VID(pTsdb->pVnode), pFile->commitID);
H
Hongze Cheng 已提交
292 293 294 295 296
}

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

H
Hongze Cheng 已提交
297
  n += tPutI64v(p ? p + n : p, pDelFile->commitID);
H
Hongze Cheng 已提交
298 299 300 301 302 303 304 305 306
  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 已提交
307
  n += tGetI64v(p + n, &pDelFile->commitID);
H
Hongze Cheng 已提交
308 309 310 311 312
  n += tGetI64v(p + n, &pDelFile->size);
  n += tGetI64v(p + n, &pDelFile->offset);

  return n;
}