tsdbOpen.c 3.2 KB
Newer Older
H
Hongze Cheng 已提交
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 "tsdb.h"

18
static int tsdbSetKeepCfg(STsdbKeepCfg *pKeepCfg, STsdbCfg *pCfg);
C
Cary Xu 已提交
19

C
Cary Xu 已提交
20

21
// implementation
C
Cary Xu 已提交
22

23
static int tsdbSetKeepCfg(STsdbKeepCfg *pKeepCfg, STsdbCfg *pCfg) {
C
Cary Xu 已提交
24
  pKeepCfg->precision = pCfg->precision;
25 26 27 28
  pKeepCfg->days = pCfg->days;
  pKeepCfg->keep0 = pCfg->keep0;
  pKeepCfg->keep1 = pCfg->keep1;
  pKeepCfg->keep2 = pCfg->keep2;
C
Cary Xu 已提交
29 30 31
  return 0;
}

C
Cary Xu 已提交
32
/**
C
Cary Xu 已提交
33 34 35 36 37 38
 * @brief
 *
 * @param pVnode
 * @param ppTsdb
 * @param dir
 * @return int
C
Cary Xu 已提交
39
 */
40
int tsdbOpen(SVnode *pVnode, STsdb **ppTsdb, const char *dir, STsdbKeepCfg *pKeepCfg) {
H
Hongze Cheng 已提交
41 42 43 44
  STsdb *pTsdb = NULL;
  int    slen = 0;

  *ppTsdb = NULL;
C
Cary Xu 已提交
45
  slen = strlen(tfsGetPrimaryPath(pVnode->pTfs)) + strlen(pVnode->path) + strlen(dir) + 3;
H
Hongze Cheng 已提交
46 47 48 49 50 51 52 53

  // create handle
  pTsdb = (STsdb *)taosMemoryCalloc(1, sizeof(*pTsdb) + slen);
  if (pTsdb == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

54 55
  ASSERT(strlen(dir) < TSDB_DATA_DIR_LEN);
  memcpy(pTsdb->dir, dir, strlen(dir));
H
Hongze Cheng 已提交
56
  pTsdb->path = (char *)&pTsdb[1];
C
Cary Xu 已提交
57
  sprintf(pTsdb->path, "%s%s%s%s%s", tfsGetPrimaryPath(pVnode->pTfs), TD_DIRSEP, pVnode->path, TD_DIRSEP, dir);
wafwerar's avatar
wafwerar 已提交
58
  taosRealPath(pTsdb->path, NULL, slen);
H
Hongze Cheng 已提交
59 60
  pTsdb->pVnode = pVnode;
  pTsdb->repoLocked = false;
H
Hongze Cheng 已提交
61
  taosThreadMutexInit(&pTsdb->mutex, NULL);
62 63 64 65 66
  if (!pKeepCfg) {
    tsdbSetKeepCfg(&pTsdb->keepCfg, &pVnode->config.tsdbCfg);
  } else {
    memcpy(&pTsdb->keepCfg, pKeepCfg, sizeof(STsdbKeepCfg));
  }
C
Cary Xu 已提交
67
  pTsdb->fs = tsdbNewFS(REPO_KEEP_CFG(pTsdb));
H
Hongze Cheng 已提交
68 69 70 71 72 73 74 75 76

  // create dir (TODO: use tfsMkdir)
  taosMkDir(pTsdb->path);

  // open tsdb
  if (tsdbOpenFS(pTsdb) < 0) {
    goto _err;
  }

S
Shengliang Guan 已提交
77
  tsdbDebug("vgId:%d, tsdb is opened for %s, days:%d, keep:%d,%d,%d", TD_VID(pVnode), pTsdb->path, pTsdb->keepCfg.days,
C
Cary Xu 已提交
78
            pTsdb->keepCfg.keep0, pTsdb->keepCfg.keep1, pTsdb->keepCfg.keep2);
H
Hongze Cheng 已提交
79 80 81 82 83 84 85 86 87

  *ppTsdb = pTsdb;
  return 0;

_err:
  taosMemoryFree(pTsdb);
  return -1;
}

88 89
int tsdbClose(STsdb **pTsdb) {
  if (*pTsdb) {
C
Cary Xu 已提交
90
    // TODO: destroy mem/imem
91 92 93 94
    taosThreadMutexDestroy(&(*pTsdb)->mutex);
    tsdbCloseFS(*pTsdb);
    tsdbFreeFS((*pTsdb)->fs);
    taosMemoryFreeClear(*pTsdb);
H
Hongze Cheng 已提交
95 96 97 98 99 100 101
  }
  return 0;
}

int tsdbLockRepo(STsdb *pTsdb) {
  int code = taosThreadMutexLock(&pTsdb->mutex);
  if (code != 0) {
S
Shengliang Guan 已提交
102
    tsdbError("vgId:%d, failed to lock tsdb since %s", REPO_ID(pTsdb), strerror(errno));
H
Hongze Cheng 已提交
103 104 105 106 107 108 109 110 111 112 113 114
    terrno = TAOS_SYSTEM_ERROR(code);
    return -1;
  }
  pTsdb->repoLocked = true;
  return 0;
}

int tsdbUnlockRepo(STsdb *pTsdb) {
  ASSERT(IS_REPO_LOCKED(pTsdb));
  pTsdb->repoLocked = false;
  int code = taosThreadMutexUnlock(&pTsdb->mutex);
  if (code != 0) {
S
Shengliang Guan 已提交
115
    tsdbError("vgId:%d, failed to unlock tsdb since %s", REPO_ID(pTsdb), strerror(errno));
H
Hongze Cheng 已提交
116 117 118 119 120
    terrno = TAOS_SYSTEM_ERROR(code);
    return -1;
  }
  return 0;
}