tdisk.h 2.7 KB
Newer Older
H
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
/*
 * 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/>.
 */

#ifndef TD_TDISK_H
#define TD_TDISK_H

#include "taosdef.h"
#include "hash.h"
#include "hash.h"
#include "taoserror.h"
#include "tglobal.h"

#ifdef __cplusplus
extern "C" {
#endif

typedef struct {
  int level;
  int did;
} SDiskID;

typedef struct {
  uint64_t size;
  uint64_t free;
  uint64_t nfiles;
} SDiskMeta;

typedef struct {
H
Hongze Cheng 已提交
41 42 43 44 45 46 47
  uint64_t tsize;
  uint64_t avail; // bytes
} STiersMeta;

typedef struct {
  int       level;
  int       did;
H
Hongze Cheng 已提交
48 49 50 51 52 53 54 55 56 57 58 59
  char      dir[TSDB_FILENAME_LEN];
  SDiskMeta dmeta;
} SDisk;

typedef struct {
  int    level;
  int    nDisks;
  SDisk *disks[TSDB_MAX_DISKS_PER_TIER];
} STier;

typedef struct SDnodeTier {
  pthread_mutex_t lock;
H
Hongze Cheng 已提交
60
  STiersMeta      meta;
H
Hongze Cheng 已提交
61 62 63 64 65 66 67 68
  int             nTiers;
  STier           tiers[TSDB_MAX_TIERS];
  SHashObj *      map;
} SDnodeTier;

extern struct SDnodeTier *tsDnodeTier;
#define DNODE_PRIMARY_DISK(pDnodeTier) (pDnodeTier)->tiers[0].disks[0]

H
Hongze Cheng 已提交
69
static FORCE_INLINE int tdLockTiers(SDnodeTier *pDnodeTier) {
H
Hongze Cheng 已提交
70 71 72 73 74 75 76 77
  int code = pthread_mutex_lock(&(pDnodeTier->lock));
  if (code != 0) {
    terrno = TAOS_SYSTEM_ERROR(code);
    return -1;
  }
  return 0;
}

H
Hongze Cheng 已提交
78
static FORCE_INLINE int tdUnLockTiers(SDnodeTier *pDnodeTier) {
H
Hongze Cheng 已提交
79 80 81 82 83 84 85 86
  int code = pthread_mutex_unlock(&(pDnodeTier->lock));
  if (code != 0) {
    terrno = TAOS_SYSTEM_ERROR(code);
    return -1;
  }
  return 0;
}

H
Hongze Cheng 已提交
87
static FORCE_INLINE SDisk *tdGetDisk(SDnodeTier *pDnodeTier, int level, int did) {
H
Hongze Cheng 已提交
88 89 90 91 92 93 94
  if (level < 0 || level >= pDnodeTier->nTiers) return NULL;

  if (did < 0 || did >= pDnodeTier->tiers[level].nDisks) return NULL;

  return pDnodeTier->tiers[level].disks[did];
}

H
Hongze Cheng 已提交
95 96 97 98 99 100 101 102 103
SDnodeTier *tdNewTier();
void *      tdCloseTier(SDnodeTier *pDnodeTier);
int         tdAddDisks(SDnodeTier *pDnodeTier, SDiskCfg *pDiskCfgs, int ndisks);
int         tdUpdateTiersInfo(SDnodeTier *pDnodeTier);
int         tdCheckTiers(SDnodeTier *pDnodeTier);
SDisk *     tdAssignDisk(SDnodeTier *pDnodeTier, int level);
SDisk *     tdGetDiskByName(SDnodeTier *pDnodeTier, char *dirName);
void        tdIncDiskFiles(SDnodeTier *pDnodeTier, SDisk *pDisk, bool lock);
void        tdDecDiskFiles(SDnodeTier *pDnodeTier, SDisk *pDisk, bool lock);
H
Hongze Cheng 已提交
104 105 106 107 108 109

#ifdef __cplusplus
}
#endif

#endif