smaUtil.c 9.0 KB
Newer Older
C
Cary Xu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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/>.
 */

#include "sma.h"

C
Cary Xu 已提交
18 19
// smaFileUtil ================

C
Cary Xu 已提交
20 21 22 23 24 25 26 27 28 29 30 31
#define TD_FILE_STATE_OK  0
#define TD_FILE_STATE_BAD 1

#define TD_FILE_INIT_MAGIC 0xFFFFFFFF

static int32_t tdEncodeTFInfo(void **buf, STFInfo *pInfo);
static void   *tdDecodeTFInfo(void *buf, STFInfo *pInfo);

static int32_t tdEncodeTFInfo(void **buf, STFInfo *pInfo) {
  int32_t tlen = 0;

  tlen += taosEncodeFixedU32(buf, pInfo->magic);
C
Cary Xu 已提交
32
  tlen += taosEncodeFixedU32(buf, pInfo->ftype);
C
Cary Xu 已提交
33
  tlen += taosEncodeFixedU32(buf, pInfo->fver);
C
Cary Xu 已提交
34
  tlen += taosEncodeFixedI64(buf, pInfo->fsize);
35 36 37
  if (pInfo->ftype == TD_FTYPE_RSMA_QTASKINFO) {
    tlen += taosEncodeFixedI64(buf, pInfo->qTaskInfo.submitVer);
  }
C
Cary Xu 已提交
38 39 40 41 42 43

  return tlen;
}

static void *tdDecodeTFInfo(void *buf, STFInfo *pInfo) {
  buf = taosDecodeFixedU32(buf, &(pInfo->magic));
C
Cary Xu 已提交
44
  buf = taosDecodeFixedU32(buf, &(pInfo->ftype));
C
Cary Xu 已提交
45
  buf = taosDecodeFixedU32(buf, &(pInfo->fver));
C
Cary Xu 已提交
46
  buf = taosDecodeFixedI64(buf, &(pInfo->fsize));
47 48 49 50 51
  // specific
  if (pInfo->ftype == TD_FTYPE_RSMA_QTASKINFO) {
    buf = taosDecodeFixedI64(buf, &(pInfo->qTaskInfo.submitVer));
  }

C
Cary Xu 已提交
52 53 54 55
  return buf;
}

int64_t tdWriteTFile(STFile *pTFile, void *buf, int64_t nbyte) {
C
Cary Xu 已提交
56
  ASSERT(TD_TFILE_OPENED(pTFile));
C
Cary Xu 已提交
57 58 59 60 61 62 63 64 65 66 67

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

  return nwrite;
}

int64_t tdSeekTFile(STFile *pTFile, int64_t offset, int whence) {
C
Cary Xu 已提交
68
  ASSERT(TD_TFILE_OPENED(pTFile));
C
Cary Xu 已提交
69

C
Cary Xu 已提交
70
  int64_t loffset = taosLSeekFile(TD_TFILE_PFILE(pTFile), offset, whence);
C
Cary Xu 已提交
71 72 73 74 75 76 77 78
  if (loffset < 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }

  return loffset;
}

79
int64_t tdGetTFileSize(STFile *pTFile, int64_t *size) {
C
Cary Xu 已提交
80
  ASSERT(TD_TFILE_OPENED(pTFile));
81 82 83
  return taosFStatFile(pTFile->pFile, size, NULL);
}

C
Cary Xu 已提交
84
int64_t tdReadTFile(STFile *pTFile, void *buf, int64_t nbyte) {
C
Cary Xu 已提交
85
  ASSERT(TD_TFILE_OPENED(pTFile));
C
Cary Xu 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117

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

  return nread;
}

int32_t tdUpdateTFileHeader(STFile *pTFile) {
  char buf[TD_FILE_HEAD_SIZE] = "\0";

  if (tdSeekTFile(pTFile, 0, SEEK_SET) < 0) {
    return -1;
  }

  void *ptr = buf;
  tdEncodeTFInfo(&ptr, &(pTFile->info));

  taosCalcChecksumAppend(0, (uint8_t *)buf, TD_FILE_HEAD_SIZE);
  if (tdWriteTFile(pTFile, buf, TD_FILE_HEAD_SIZE) < 0) {
    return -1;
  }

  return 0;
}

int32_t tdLoadTFileHeader(STFile *pTFile, STFInfo *pInfo) {
  char     buf[TD_FILE_HEAD_SIZE] = "\0";
  uint32_t _version;

C
Cary Xu 已提交
118
  ASSERT(TD_TFILE_OPENED(pTFile));
C
Cary Xu 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142

  if (tdSeekTFile(pTFile, 0, SEEK_SET) < 0) {
    return -1;
  }

  if (tdReadTFile(pTFile, buf, TD_FILE_HEAD_SIZE) < 0) {
    return -1;
  }

  if (!taosCheckChecksumWhole((uint8_t *)buf, TD_FILE_HEAD_SIZE)) {
    terrno = TSDB_CODE_FILE_CORRUPTED;
    return -1;
  }

  void *pBuf = buf;
  pBuf = tdDecodeTFInfo(pBuf, pInfo);
  return 0;
}

void tdUpdateTFileMagic(STFile *pTFile, void *pCksm) {
  pTFile->info.magic = taosCalcChecksum(pTFile->info.magic, (uint8_t *)(pCksm), sizeof(TSCKSUM));
}

int64_t tdAppendTFile(STFile *pTFile, void *buf, int64_t nbyte, int64_t *offset) {
C
Cary Xu 已提交
143
  ASSERT(TD_TFILE_OPENED(pTFile));
C
Cary Xu 已提交
144 145 146 147 148 149 150

  int64_t toffset;

  if ((toffset = tdSeekTFile(pTFile, 0, SEEK_END)) < 0) {
    return -1;
  }

C
Cary Xu 已提交
151
#if 0
152 153
  smaDebug("append to file %s, offset:%" PRIi64 " nbyte:%" PRIi64 " fsize:%" PRIi64, TD_TFILE_FULL_NAME(pTFile),
           toffset, nbyte, toffset + nbyte);
C
Cary Xu 已提交
154 155
#endif

C
Cary Xu 已提交
156
  ASSERT(pTFile->info.fsize == toffset);
C
Cary Xu 已提交
157 158 159 160 161 162 163 164 165

  if (offset) {
    *offset = toffset;
  }

  if (tdWriteTFile(pTFile, buf, nbyte) < 0) {
    return -1;
  }

C
Cary Xu 已提交
166
  pTFile->info.fsize += nbyte;
C
Cary Xu 已提交
167 168 169 170 171

  return nbyte;
}

int32_t tdOpenTFile(STFile *pTFile, int flags) {
C
Cary Xu 已提交
172
  ASSERT(!TD_TFILE_OPENED(pTFile));
C
Cary Xu 已提交
173

C
Cary Xu 已提交
174
  pTFile->pFile = taosOpenFile(TD_TFILE_FULL_NAME(pTFile), flags);
C
Cary Xu 已提交
175 176 177 178 179 180 181 182 183
  if (pTFile->pFile == NULL) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }

  return 0;
}

void tdCloseTFile(STFile *pTFile) {
C
Cary Xu 已提交
184
  if (TD_TFILE_OPENED(pTFile)) {
C
Cary Xu 已提交
185
    taosCloseFile(&pTFile->pFile);
C
Cary Xu 已提交
186
    TD_TFILE_SET_CLOSED(pTFile);
C
Cary Xu 已提交
187 188 189
  }
}

C
Cary Xu 已提交
190 191
void tdDestroyTFile(STFile *pTFile) { taosMemoryFreeClear(TD_TFILE_FULL_NAME(pTFile)); }

C
Cary Xu 已提交
192 193
void tdGetVndFileName(int32_t vgId, const char *pdname, const char *dname, const char *fname, int64_t version,
                      char *outputName) {
C
Cary Xu 已提交
194
  if (version < 0) {
C
Cary Xu 已提交
195 196 197 198 199 200 201
    if (pdname) {
      snprintf(outputName, TSDB_FILENAME_LEN, "%s%svnode%svnode%d%s%s%sv%d%s", pdname, TD_DIRSEP, TD_DIRSEP, vgId,
               TD_DIRSEP, dname, TD_DIRSEP, vgId, fname);
    } else {
      snprintf(outputName, TSDB_FILENAME_LEN, "vnode%svnode%d%s%s%sv%d%s", TD_DIRSEP, vgId, TD_DIRSEP, dname, TD_DIRSEP,
               vgId, fname);
    }
C
Cary Xu 已提交
202
  } else {
C
Cary Xu 已提交
203 204 205 206 207 208 209
    if (pdname) {
      snprintf(outputName, TSDB_FILENAME_LEN, "%s%svnode%svnode%d%s%s%sv%d%s%" PRIi64, pdname, TD_DIRSEP, TD_DIRSEP,
               vgId, TD_DIRSEP, dname, TD_DIRSEP, vgId, fname, version);
    } else {
      snprintf(outputName, TSDB_FILENAME_LEN, "vnode%svnode%d%s%s%sv%d%s%" PRIi64, TD_DIRSEP, vgId, TD_DIRSEP, dname,
               TD_DIRSEP, vgId, fname, version);
    }
C
Cary Xu 已提交
210
  }
C
Cary Xu 已提交
211 212
}

C
Cary Xu 已提交
213
void tdGetVndDirName(int32_t vgId, const char *pdname, const char *dname, bool endWithSep, char *outputName) {
C
Cary Xu 已提交
214
  if (pdname) {
C
Cary Xu 已提交
215 216 217 218 219 220 221
    if (endWithSep) {
      snprintf(outputName, TSDB_FILENAME_LEN, "%s%svnode%svnode%d%s%s%s", pdname, TD_DIRSEP, TD_DIRSEP, vgId, TD_DIRSEP,
               dname, TD_DIRSEP);
    } else {
      snprintf(outputName, TSDB_FILENAME_LEN, "%s%svnode%svnode%d%s%s", pdname, TD_DIRSEP, TD_DIRSEP, vgId, TD_DIRSEP,
               dname);
    }
C
Cary Xu 已提交
222
  } else {
C
Cary Xu 已提交
223 224 225 226 227
    if (endWithSep) {
      snprintf(outputName, TSDB_FILENAME_LEN, "vnode%svnode%d%s%s%s", TD_DIRSEP, vgId, TD_DIRSEP, dname, TD_DIRSEP);
    } else {
      snprintf(outputName, TSDB_FILENAME_LEN, "vnode%svnode%d%s%s", TD_DIRSEP, vgId, TD_DIRSEP, dname);
    }
C
Cary Xu 已提交
228
  }
C
Cary Xu 已提交
229
}
C
Cary Xu 已提交
230

C
Cary Xu 已提交
231
int32_t tdInitTFile(STFile *pTFile, const char *dname, const char *fname) {
C
Cary Xu 已提交
232 233
  TD_TFILE_SET_STATE(pTFile, TD_FILE_STATE_OK);
  TD_TFILE_SET_CLOSED(pTFile);
C
Cary Xu 已提交
234 235 236 237

  memset(&(pTFile->info), 0, sizeof(pTFile->info));
  pTFile->info.magic = TD_FILE_INIT_MAGIC;

C
Cary Xu 已提交
238 239 240 241 242 243
  char tmpName[TSDB_FILENAME_LEN * 2 + 32] = {0};
  snprintf(tmpName, TSDB_FILENAME_LEN * 2 + 32, "%s%s%s", dname, TD_DIRSEP, fname);
  int32_t tmpNameLen = strlen(tmpName) + 1;
  pTFile->fname = taosMemoryMalloc(tmpNameLen);
  if (!pTFile->fname) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
C
Cary Xu 已提交
244 245
    return -1;
  }
C
Cary Xu 已提交
246
  tstrncpy(pTFile->fname, tmpName, tmpNameLen);
C
Cary Xu 已提交
247 248 249 250

  return 0;
}

C
Cary Xu 已提交
251
int32_t tdCreateTFile(STFile *pTFile, bool updateHeader, int8_t fType) {
C
Cary Xu 已提交
252
  ASSERT(pTFile->info.fsize == 0 && pTFile->info.magic == TD_FILE_INIT_MAGIC);
C
Cary Xu 已提交
253
  pTFile->pFile = taosOpenFile(TD_TFILE_FULL_NAME(pTFile), TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
C
Cary Xu 已提交
254 255 256
  if (pTFile->pFile == NULL) {
    if (errno == ENOENT) {
      // Try to create directory recursively
C
Cary Xu 已提交
257 258 259 260 261 262 263 264 265
      char *s = strdup(TD_TFILE_FULL_NAME(pTFile));
      if (taosMulMkDir(taosDirName(s)) != 0) {
        terrno = TAOS_SYSTEM_ERROR(errno);
        taosMemoryFree(s);
        return -1;
      }
      taosMemoryFree(s);
      pTFile->pFile = taosOpenFile(TD_TFILE_FULL_NAME(pTFile), TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
      if (pTFile->pFile == NULL) {
C
Cary Xu 已提交
266 267 268 269
        terrno = TAOS_SYSTEM_ERROR(errno);
        return -1;
      }
    }
C
Cary Xu 已提交
270
  }
C
Cary Xu 已提交
271

C
Cary Xu 已提交
272 273 274
  if (!updateHeader) {
    return 0;
  }
C
Cary Xu 已提交
275

C
Cary Xu 已提交
276 277
  pTFile->info.fsize += TD_FILE_HEAD_SIZE;
  pTFile->info.fver = 0;
C
Cary Xu 已提交
278

C
Cary Xu 已提交
279 280 281 282
  if (tdUpdateTFileHeader(pTFile) < 0) {
    tdCloseTFile(pTFile);
    tdRemoveTFile(pTFile);
    return -1;
C
Cary Xu 已提交
283 284 285 286 287
  }

  return 0;
}

C
Cary Xu 已提交
288 289 290 291 292 293 294
int32_t tdRemoveTFile(STFile *pTFile) {
  if (taosRemoveFile(TD_TFILE_FULL_NAME(pTFile)) != 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  };
  return 0;
}
C
Cary Xu 已提交
295 296

// smaXXXUtil ================
C
Cary Xu 已提交
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
void *tdAcquireSmaRef(int32_t rsetId, int64_t refId, const char *tags, int32_t ln) {
  void *pResult = taosAcquireRef(rsetId, refId);
  if (!pResult) {
    smaWarn("%s:%d taosAcquireRef for rsetId:%" PRIi64 " refId:%d failed since %s", tags, ln, rsetId, refId, terrstr());
  } else {
    smaDebug("%s:%d taosAcquireRef for rsetId:%" PRIi64 " refId:%d success", tags, ln, rsetId, refId);
  }
  return pResult;
}

int32_t tdReleaseSmaRef(int32_t rsetId, int64_t refId, const char *tags, int32_t ln) {
  if (taosReleaseRef(rsetId, refId) < 0) {
    smaWarn("%s:%d taosReleaseRef for rsetId:%" PRIi64 " refId:%d failed since %s", tags, ln, rsetId, refId, terrstr());
    return TSDB_CODE_FAILED;
  }
  smaDebug("%s:%d taosReleaseRef for rsetId:%" PRIi64 " refId:%d success", tags, ln, rsetId, refId);

  return TSDB_CODE_SUCCESS;
}
C
Cary Xu 已提交
316
// ...