smaUtil.c 11.3 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);
C
Cary Xu 已提交
35 36 37 38 39 40

  return tlen;
}

static void *tdDecodeTFInfo(void *buf, STFInfo *pInfo) {
  buf = taosDecodeFixedU32(buf, &(pInfo->magic));
C
Cary Xu 已提交
41
  buf = taosDecodeFixedU32(buf, &(pInfo->ftype));
C
Cary Xu 已提交
42
  buf = taosDecodeFixedU32(buf, &(pInfo->fver));
C
Cary Xu 已提交
43
  buf = taosDecodeFixedI64(buf, &(pInfo->fsize));
44

C
Cary Xu 已提交
45 46 47 48
  return buf;
}

int64_t tdWriteTFile(STFile *pTFile, void *buf, int64_t nbyte) {
C
Cary Xu 已提交
49
  ASSERT(TD_TFILE_OPENED(pTFile));
C
Cary Xu 已提交
50 51 52 53 54 55 56 57 58 59 60

  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 已提交
61
  ASSERT(TD_TFILE_OPENED(pTFile));
C
Cary Xu 已提交
62

C
Cary Xu 已提交
63
  int64_t loffset = taosLSeekFile(TD_TFILE_PFILE(pTFile), offset, whence);
C
Cary Xu 已提交
64 65 66 67 68 69 70 71
  if (loffset < 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }

  return loffset;
}

72
int64_t tdGetTFileSize(STFile *pTFile, int64_t *size) {
C
Cary Xu 已提交
73
  ASSERT(TD_TFILE_OPENED(pTFile));
74 75 76
  return taosFStatFile(pTFile->pFile, size, NULL);
}

C
Cary Xu 已提交
77
int64_t tdReadTFile(STFile *pTFile, void *buf, int64_t nbyte) {
C
Cary Xu 已提交
78
  ASSERT(TD_TFILE_OPENED(pTFile));
C
Cary Xu 已提交
79 80 81 82 83 84 85 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

  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 已提交
111
  ASSERT(TD_TFILE_OPENED(pTFile));
C
Cary Xu 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135

  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 已提交
136
  ASSERT(TD_TFILE_OPENED(pTFile));
C
Cary Xu 已提交
137 138 139 140 141 142 143

  int64_t toffset;

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

C
Cary Xu 已提交
144
#if 0
145 146
  smaDebug("append to file %s, offset:%" PRIi64 " nbyte:%" PRIi64 " fsize:%" PRIi64, TD_TFILE_FULL_NAME(pTFile),
           toffset, nbyte, toffset + nbyte);
C
Cary Xu 已提交
147 148
#endif

C
Cary Xu 已提交
149
  ASSERT(pTFile->info.fsize == toffset);
C
Cary Xu 已提交
150 151 152 153 154 155 156 157 158

  if (offset) {
    *offset = toffset;
  }

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

C
Cary Xu 已提交
159
  pTFile->info.fsize += nbyte;
C
Cary Xu 已提交
160 161 162 163 164

  return nbyte;
}

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

C
Cary Xu 已提交
167
  pTFile->pFile = taosOpenFile(TD_TFILE_FULL_NAME(pTFile), flags);
C
Cary Xu 已提交
168 169 170 171 172 173 174 175 176
  if (pTFile->pFile == NULL) {
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }

  return 0;
}

void tdCloseTFile(STFile *pTFile) {
C
Cary Xu 已提交
177
  if (TD_TFILE_OPENED(pTFile)) {
C
Cary Xu 已提交
178
    taosCloseFile(&pTFile->pFile);
C
Cary Xu 已提交
179
    TD_TFILE_SET_CLOSED(pTFile);
C
Cary Xu 已提交
180 181 182
  }
}

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

C
Cary Xu 已提交
185 186
void tdGetVndFileName(int32_t vgId, const char *pdname, const char *dname, const char *fname, int64_t version,
                      char *outputName) {
C
Cary Xu 已提交
187
  if (version < 0) {
C
Cary Xu 已提交
188 189 190 191 192 193 194
    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 已提交
195
  } else {
C
Cary Xu 已提交
196 197 198 199 200 201 202
    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 已提交
203
  }
C
Cary Xu 已提交
204 205
}

C
Cary Xu 已提交
206
void tdGetVndDirName(int32_t vgId, const char *pdname, const char *dname, bool endWithSep, char *outputName) {
C
Cary Xu 已提交
207
  if (pdname) {
C
Cary Xu 已提交
208 209 210 211 212 213 214
    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 已提交
215
  } else {
C
Cary Xu 已提交
216 217 218 219 220
    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 已提交
221
  }
C
Cary Xu 已提交
222
}
C
Cary Xu 已提交
223

C
Cary Xu 已提交
224
int32_t tdInitTFile(STFile *pTFile, const char *dname, const char *fname) {
C
Cary Xu 已提交
225 226
  TD_TFILE_SET_STATE(pTFile, TD_FILE_STATE_OK);
  TD_TFILE_SET_CLOSED(pTFile);
C
Cary Xu 已提交
227 228 229 230

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

C
Cary Xu 已提交
231 232 233 234 235 236
  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 已提交
237 238
    return -1;
  }
C
Cary Xu 已提交
239
  tstrncpy(pTFile->fname, tmpName, tmpNameLen);
C
Cary Xu 已提交
240 241 242 243

  return 0;
}

C
Cary Xu 已提交
244
int32_t tdCreateTFile(STFile *pTFile, bool updateHeader, int8_t fType) {
C
Cary Xu 已提交
245
  ASSERT(pTFile->info.fsize == 0 && pTFile->info.magic == TD_FILE_INIT_MAGIC);
C
Cary Xu 已提交
246
  pTFile->pFile = taosOpenFile(TD_TFILE_FULL_NAME(pTFile), TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
C
Cary Xu 已提交
247 248 249
  if (pTFile->pFile == NULL) {
    if (errno == ENOENT) {
      // Try to create directory recursively
C
Cary Xu 已提交
250 251 252 253 254 255 256 257 258
      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 已提交
259 260 261 262
        terrno = TAOS_SYSTEM_ERROR(errno);
        return -1;
      }
    }
C
Cary Xu 已提交
263
  }
C
Cary Xu 已提交
264

C
Cary Xu 已提交
265 266 267
  if (!updateHeader) {
    return 0;
  }
C
Cary Xu 已提交
268

C
Cary Xu 已提交
269 270
  pTFile->info.fsize += TD_FILE_HEAD_SIZE;
  pTFile->info.fver = 0;
C
Cary Xu 已提交
271

C
Cary Xu 已提交
272 273 274 275
  if (tdUpdateTFileHeader(pTFile) < 0) {
    tdCloseTFile(pTFile);
    tdRemoveTFile(pTFile);
    return -1;
C
Cary Xu 已提交
276 277 278 279 280
  }

  return 0;
}

C
Cary Xu 已提交
281 282 283 284 285 286 287
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 已提交
288 289

// smaXXXUtil ================
C
Cary Xu 已提交
290
void *tdAcquireSmaRef(int32_t rsetId, int64_t refId) {
C
Cary Xu 已提交
291 292
  void *pResult = taosAcquireRef(rsetId, refId);
  if (!pResult) {
C
Cary Xu 已提交
293
    smaWarn("rsma acquire ref for rsetId:%" PRIi64 " refId:%d failed since %s", rsetId, refId, terrstr());
C
Cary Xu 已提交
294
  } else {
C
Cary Xu 已提交
295
    smaDebug("rsma acquire ref for rsetId:%" PRIi64 " refId:%d success", rsetId, refId);
C
Cary Xu 已提交
296 297 298 299
  }
  return pResult;
}

C
Cary Xu 已提交
300
int32_t tdReleaseSmaRef(int32_t rsetId, int64_t refId) {
C
Cary Xu 已提交
301
  if (taosReleaseRef(rsetId, refId) < 0) {
C
Cary Xu 已提交
302
    smaWarn("rsma release ref for rsetId:%" PRIi64 " refId:%d failed since %s", rsetId, refId, terrstr());
C
Cary Xu 已提交
303 304
    return TSDB_CODE_FAILED;
  }
C
Cary Xu 已提交
305
  smaDebug("rsma release ref for rsetId:%" PRIi64 " refId:%d success", rsetId, refId);
C
Cary Xu 已提交
306 307 308

  return TSDB_CODE_SUCCESS;
}
C
Cary Xu 已提交
309 310 311 312 313 314 315

static int32_t tdCloneQTaskInfo(SSma *pSma, qTaskInfo_t dstTaskInfo, qTaskInfo_t srcTaskInfo, SRSmaParam *param,
                                tb_uid_t suid, int8_t idx) {
  SVnode *pVnode = pSma->pVnode;
  char   *pOutput = NULL;
  int32_t len = 0;

C
Cary Xu 已提交
316
  if ((terrno = qSerializeTaskStatus(srcTaskInfo, &pOutput, &len)) < 0) {
C
Cary Xu 已提交
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
    smaError("vgId:%d, rsma clone, table %" PRIi64 " serialize qTaskInfo failed since %s", TD_VID(pVnode), suid,
             terrstr());
    goto _err;
  }

  SReadHandle handle = {
      .meta = pVnode->pMeta,
      .vnode = pVnode,
      .initTqReader = 1,
  };
  ASSERT(!dstTaskInfo);
  dstTaskInfo = qCreateStreamExecTaskInfo(param->qmsg[idx], &handle);
  if (!dstTaskInfo) {
    terrno = TSDB_CODE_RSMA_QTASKINFO_CREATE;
    goto _err;
  }

  if (qDeserializeTaskStatus(dstTaskInfo, pOutput, len) < 0) {
    smaError("vgId:%d, rsma clone, restore rsma task for table:%" PRIi64 " failed since %s", TD_VID(pVnode), suid,
             terrstr());
    goto _err;
  }

C
Cary Xu 已提交
340
  smaDebug("vgId:%d, rsma clone, restore rsma task for table:%" PRIi64 " succeed", TD_VID(pVnode), suid);
C
Cary Xu 已提交
341 342 343 344 345 346

  taosMemoryFreeClear(pOutput);
  return TSDB_CODE_SUCCESS;
_err:
  taosMemoryFreeClear(pOutput);
  tdFreeQTaskInfo(dstTaskInfo, TD_VID(pVnode), idx + 1);
C
Cary Xu 已提交
347 348
  smaError("vgId:%d, rsma clone, restore rsma task for table:%" PRIi64 " failed since %s", TD_VID(pVnode), suid,
           terrstr());
C
Cary Xu 已提交
349 350 351 352
  return TSDB_CODE_FAILED;
}

/**
C
Cary Xu 已提交
353
 * @brief Clone qTaskInfo of SRSmaInfo
C
Cary Xu 已提交
354 355
 *
 * @param pSma
C
Cary Xu 已提交
356
 * @param pInfo
C
Cary Xu 已提交
357
 * @return int32_t
C
Cary Xu 已提交
358
 */
C
Cary Xu 已提交
359
int32_t tdCloneRSmaInfo(SSma *pSma, SRSmaInfo *pInfo) {
C
Cary Xu 已提交
360
  SRSmaParam *param = NULL;
C
Cary Xu 已提交
361
  if (!pInfo) {
C
Cary Xu 已提交
362 363 364 365 366
    return TSDB_CODE_SUCCESS;
  }

  SMetaReader mr = {0};
  metaReaderInit(&mr, SMA_META(pSma), 0);
C
Cary Xu 已提交
367 368 369
  smaDebug("vgId:%d, rsma clone qTaskInfo for suid:%" PRIi64, SMA_VID(pSma), pInfo->suid);
  if (metaGetTableEntryByUid(&mr, pInfo->suid) < 0) {
    smaError("vgId:%d, rsma clone, failed to get table meta for %" PRIi64 " since %s", SMA_VID(pSma), pInfo->suid,
C
Cary Xu 已提交
370 371 372 373
             terrstr());
    goto _err;
  }
  ASSERT(mr.me.type == TSDB_SUPER_TABLE);
C
Cary Xu 已提交
374
  ASSERT(mr.me.uid == pInfo->suid);
C
Cary Xu 已提交
375 376
  if (TABLE_IS_ROLLUP(mr.me.flags)) {
    param = &mr.me.stbEntry.rsmaParam;
C
Cary Xu 已提交
377
    for (int32_t i = 0; i < TSDB_RETENTION_L2; ++i) {
C
Cary Xu 已提交
378 379 380
      if (!pInfo->iTaskInfo[i]) {
        continue;
      }
C
Cary Xu 已提交
381
      if (tdCloneQTaskInfo(pSma, pInfo->taskInfo[i], pInfo->iTaskInfo[i], param, pInfo->suid, i) < 0) {
C
Cary Xu 已提交
382 383
        goto _err;
      }
C
Cary Xu 已提交
384
    }
C
Cary Xu 已提交
385 386 387 388
    smaDebug("vgId:%d, rsma clone env success for %" PRIi64, SMA_VID(pSma), pInfo->suid);
  } else {
    terrno = TSDB_CODE_RSMA_INVALID_SCHEMA;
    goto _err;
C
Cary Xu 已提交
389 390 391 392 393 394
  }

  metaReaderClear(&mr);
  return TSDB_CODE_SUCCESS;
_err:
  metaReaderClear(&mr);
C
Cary Xu 已提交
395
  smaError("vgId:%d, rsma clone env failed for %" PRIi64 " since %s", SMA_VID(pSma), pInfo->suid, terrstr());
C
Cary Xu 已提交
396
  return TSDB_CODE_FAILED;
C
Cary Xu 已提交
397
}