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

19
#define TD_QTASKINFO_FNAME_PREFIX "main.tdb"
C
Cary Xu 已提交
20

21 22
void tdRSmaQTaskInfoGetFileName(SVnode *pVnode, int64_t suid, int8_t level, int64_t version, char *outputName) {
  tdRSmaGetFileName(pVnode, NULL, TD_QTASKINFO_FNAME_PREFIX, suid, level, version, outputName);
C
Cary Xu 已提交
23 24
}

25
void tdRSmaQTaskInfoGetFullName(SVnode *pVnode, int64_t suid, int8_t level, int64_t version, STfs *pTfs,
K
kailixu 已提交
26
                                char *outputName) {
27
  tdRSmaGetFileName(pVnode, pTfs, TD_QTASKINFO_FNAME_PREFIX, suid, level, version, outputName);
C
Cary Xu 已提交
28 29
}

30 31
void tdRSmaQTaskInfoGetFullPath(SVnode *pVnode, int8_t level, STfs *pTfs, char *outputName) {
  tdRSmaGetDirName(pVnode, pTfs, true, outputName);
K
kailixu 已提交
32 33
  int32_t rsmaLen = strlen(outputName);
  snprintf(outputName + rsmaLen, TSDB_FILENAME_LEN - rsmaLen, "%" PRIi8, level);
C
Cary Xu 已提交
34 35
}

36 37
void tdRSmaQTaskInfoGetFullPathEx(SVnode *pVnode, tb_uid_t suid, int8_t level, STfs *pTfs, char *outputName) {
  tdRSmaGetDirName(pVnode, pTfs, true, outputName);
K
kailixu 已提交
38
  int32_t rsmaLen = strlen(outputName);
K
kailixu 已提交
39
  snprintf(outputName + rsmaLen, TSDB_FILENAME_LEN - rsmaLen, "%" PRIi8 "%s%" PRIi64, level, TD_DIRSEP, suid);
40 41
}

42 43 44 45 46 47 48 49 50 51 52 53 54
void tdRSmaGetFileName(SVnode *pVnode, STfs *pTfs, const char *fname, int64_t suid, int8_t level, int64_t version,
                       char *outputName) {
  int32_t offset = 0;

  // vnode
  vnodeGetAbsDir(pVnode->path, pTfs, outputName, TSDB_FILENAME_LEN);
  offset = strlen(outputName);

  // rsma
  snprintf(outputName + offset, TSDB_FILENAME_LEN - offset - 1, "%s%s", TD_DIRSEP, VNODE_RSMA_DIR);
  offset = strlen(outputName);

  // level & suid || vgid
K
kailixu 已提交
55
  if (level >= 0 && suid > 0) {
56 57
    snprintf(outputName + offset, TSDB_FILENAME_LEN - offset - 1, "%s%" PRIi8 "%s%" PRIi64 "%s", TD_DIRSEP, level,
             TD_DIRSEP, suid, TD_DIRSEP);
C
Cary Xu 已提交
58
  } else {
59
    snprintf(outputName + offset, TSDB_FILENAME_LEN - offset - 1, "%sv%d", TD_DIRSEP, TD_VID(pVnode));
C
Cary Xu 已提交
60
  }
61
  offset = strlen(outputName);
C
Cary Xu 已提交
62

63 64 65 66 67 68 69
  // fname
  snprintf(outputName + offset, TSDB_FILENAME_LEN - offset - 1, "%s", fname);
  offset = strlen(outputName);

  // version
  if (version >= 0) {
    snprintf(outputName + offset, TSDB_FILENAME_LEN - offset - 1, ".%" PRIi64, version);
C
Cary Xu 已提交
70
  }
C
Cary Xu 已提交
71
}
C
Cary Xu 已提交
72

73 74 75 76 77 78 79 80 81 82 83 84
void tdRSmaGetDirName(SVnode *pVnode, STfs *pTfs, bool endWithSep, char *outputName) {
  int32_t offset = 0;

  // vnode
  vnodeGetAbsDir(pVnode->path, pTfs, outputName, TSDB_FILENAME_LEN);
  offset = strlen(outputName);

  // rsma
  snprintf(outputName + offset, TSDB_FILENAME_LEN - offset - 1, "%s%s%s", TD_DIRSEP, VNODE_RSMA_DIR,
           (endWithSep ? TD_DIRSEP : ""));
}

C
Cary Xu 已提交
85
// smaXXXUtil ================
C
Cary Xu 已提交
86
void *tdAcquireSmaRef(int32_t rsetId, int64_t refId) {
C
Cary Xu 已提交
87 88
  void *pResult = taosAcquireRef(rsetId, refId);
  if (!pResult) {
C
Cary Xu 已提交
89
    smaWarn("rsma acquire ref for rsetId:%d refId:%" PRIi64 " failed since %s", rsetId, refId, terrstr());
C
Cary Xu 已提交
90
  } else {
K
kailixu 已提交
91
    smaTrace("rsma acquire ref for rsetId:%d refId:%" PRIi64 " success", rsetId, refId);
C
Cary Xu 已提交
92 93 94 95
  }
  return pResult;
}

C
Cary Xu 已提交
96
int32_t tdReleaseSmaRef(int32_t rsetId, int64_t refId) {
C
Cary Xu 已提交
97
  if (taosReleaseRef(rsetId, refId) < 0) {
C
Cary Xu 已提交
98
    smaWarn("rsma release ref for rsetId:%d refId:%" PRIi64 " failed since %s", rsetId, refId, terrstr());
C
Cary Xu 已提交
99 100
    return TSDB_CODE_FAILED;
  }
K
kailixu 已提交
101
  smaTrace("rsma release ref for rsetId:%d refId:%" PRIi64 " success", rsetId, refId);
C
Cary Xu 已提交
102 103

  return TSDB_CODE_SUCCESS;
104
}