tsdbFile.c 6.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"
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 tPutLastFile(uint8_t *p, SLastFile *pLastFile) {
H
Hongze Cheng 已提交
57 58 59 60
  int32_t n = 0;

  n += tPutI64v(p ? p + n : p, pLastFile->commitID);
  n += tPutI64v(p ? p + n : p, pLastFile->size);
H
Hongze Cheng 已提交
61
  n += tPutI64v(p ? p + n : p, pLastFile->offset);
H
Hongze Cheng 已提交
62 63 64 65 66 67 68 69 70

  return n;
}

static int32_t tGetLastFile(uint8_t *p, SLastFile *pLastFile) {
  int32_t n = 0;

  n += tGetI64v(p + n, &pLastFile->commitID);
  n += tGetI64v(p + n, &pLastFile->size);
H
Hongze Cheng 已提交
71
  n += tGetI64v(p + n, &pLastFile->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
Hongze Cheng 已提交
95 96 97 98
void tsdbHeadFileName(STsdb *pTsdb, SDiskID did, int32_t fid, SHeadFile *pHeadF, char fname[]) {
  snprintf(fname, TSDB_FILENAME_LEN - 1, "%s%s%s%sv%df%dver%" PRId64 "%s", tfsGetDiskPath(pTsdb->pVnode->pTfs, did),
           TD_DIRSEP, pTsdb->path, TD_DIRSEP, TD_VID(pTsdb->pVnode), fid, pHeadF->commitID, ".head");
}
H
Hongze Cheng 已提交
99

H
Hongze Cheng 已提交
100 101 102
void tsdbDataFileName(STsdb *pTsdb, SDiskID did, int32_t fid, SDataFile *pDataF, char fname[]) {
  snprintf(fname, TSDB_FILENAME_LEN - 1, "%s%s%s%sv%df%dver%" PRId64 "%s", tfsGetDiskPath(pTsdb->pVnode->pTfs, did),
           TD_DIRSEP, pTsdb->path, TD_DIRSEP, TD_VID(pTsdb->pVnode), fid, pDataF->commitID, ".data");
H
Hongze Cheng 已提交
103
}
H
Hongze Cheng 已提交
104

H
Hongze Cheng 已提交
105 106 107 108
void tsdbLastFileName(STsdb *pTsdb, SDiskID did, int32_t fid, SLastFile *pLastF, char fname[]) {
  snprintf(fname, TSDB_FILENAME_LEN - 1, "%s%s%s%sv%df%dver%" PRId64 "%s", tfsGetDiskPath(pTsdb->pVnode->pTfs, did),
           TD_DIRSEP, pTsdb->path, TD_DIRSEP, TD_VID(pTsdb->pVnode), fid, pLastF->commitID, ".last");
}
H
Hongze Cheng 已提交
109

H
Hongze Cheng 已提交
110 111 112
void tsdbSmaFileName(STsdb *pTsdb, SDiskID did, int32_t fid, SSmaFile *pSmaF, char fname[]) {
  snprintf(fname, TSDB_FILENAME_LEN - 1, "%s%s%s%sv%df%dver%" PRId64 "%s", tfsGetDiskPath(pTsdb->pVnode->pTfs, did),
           TD_DIRSEP, pTsdb->path, TD_DIRSEP, TD_VID(pTsdb->pVnode), fid, pSmaF->commitID, ".sma");
H
Hongze Cheng 已提交
113 114
}

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

H
Hongze Cheng 已提交
117 118 119
int32_t tsdbDFileRollback(STsdb *pTsdb, SDFileSet *pSet, EDataFileT ftype) {
  int32_t   code = 0;
  int64_t   size;
H
Hongze Cheng 已提交
120
  int64_t   n;
H
Hongze Cheng 已提交
121 122
  TdFilePtr pFD;
  char      fname[TSDB_FILENAME_LEN];
H
Hongze Cheng 已提交
123
  char      hdr[TSDB_FHDR_SIZE] = {0};
H
Hongze Cheng 已提交
124 125 126 127

  // truncate
  switch (ftype) {
    case TSDB_DATA_FILE:
H
Hongze Cheng 已提交
128 129 130
      size = pSet->pDataF->size;
      tsdbDataFileName(pTsdb, pSet->diskId, pSet->fid, pSet->pDataF, fname);
      tPutDataFile(hdr, pSet->pDataF);
H
Hongze Cheng 已提交
131 132
      break;
    case TSDB_SMA_FILE:
H
Hongze Cheng 已提交
133 134 135
      size = pSet->pSmaF->size;
      tsdbSmaFileName(pTsdb, pSet->diskId, pSet->fid, pSet->pSmaF, fname);
      tPutSmaFile(hdr, pSet->pSmaF);
H
Hongze Cheng 已提交
136 137 138 139
      break;
    default:
      ASSERT(0);
  }
H
Hongze Cheng 已提交
140 141 142 143 144 145 146 147 148 149 150

  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 已提交
151 152 153 154 155 156
  if (taosFtruncateFile(pFD, size) < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

  // update header
H
Hongze Cheng 已提交
157 158 159 160 161 162 163 164 165 166 167
  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 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180

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

  // close
  taosCloseFile(&pFD);

  return code;

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

H
Hongze Cheng 已提交
185 186 187 188 189 190
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 已提交
191 192

  // data
H
Hongze Cheng 已提交
193 194 195
  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 已提交
196

H
Hongze Cheng 已提交
197 198 199 200
  // last
  n += tPutU8(p ? p + n : p, 1);  // for future compatibility
  n += tPutLastFile(p ? p + n : p, pSet->pLastF);

H
Hongze Cheng 已提交
201 202
  return n;
}
H
Hongze Cheng 已提交
203

H
Hongze Cheng 已提交
204 205
int32_t tGetDFileSet(uint8_t *p, SDFileSet *pSet) {
  int32_t n = 0;
H
Hongze Cheng 已提交
206

H
Hongze Cheng 已提交
207 208 209
  n += tGetI32v(p + n, &pSet->diskId.level);
  n += tGetI32v(p + n, &pSet->diskId.id);
  n += tGetI32v(p + n, &pSet->fid);
H
Hongze Cheng 已提交
210 211

  // data
H
Hongze Cheng 已提交
212 213 214
  n += tGetHeadFile(p + n, pSet->pHeadF);
  n += tGetDataFile(p + n, pSet->pDataF);
  n += tGetSmaFile(p + n, pSet->pSmaF);
H
Hongze Cheng 已提交
215

H
Hongze Cheng 已提交
216 217 218 219 220
  // last
  uint8_t nLast;
  n += tGetU8(p + n, &nLast);
  n += tGetLastFile(p + n, pSet->pLastF);

H
Hongze Cheng 已提交
221 222
  return n;
}
H
Hongze Cheng 已提交
223 224 225

// SDelFile ===============================================
void tsdbDelFileName(STsdb *pTsdb, SDelFile *pFile, char fname[]) {
H
Hongze Cheng 已提交
226 227
  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 已提交
228 229 230 231 232
}

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

H
Hongze Cheng 已提交
233
  n += tPutI64v(p ? p + n : p, pDelFile->commitID);
H
Hongze Cheng 已提交
234 235 236 237 238 239 240 241 242
  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 已提交
243
  n += tGetI64v(p + n, &pDelFile->commitID);
H
Hongze Cheng 已提交
244 245 246 247 248
  n += tGetI64v(p + n, &pDelFile->size);
  n += tGetI64v(p + n, &pDelFile->offset);

  return n;
}