tfsTier.c 4.0 KB
Newer Older
H
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * 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/>.
 */
H
refact  
Hongze Cheng 已提交
15
#include "os.h"
H
Hongze Cheng 已提交
16

H
Hongze Cheng 已提交
17
#include "taosdef.h"
H
refact  
Hongze Cheng 已提交
18
#include "taoserror.h"
H
Hongze Cheng 已提交
19
#include "tfsint.h"
H
Hongze Cheng 已提交
20

H
Hongze Cheng 已提交
21 22 23
#define tfsLockTier(pTier) pthread_spin_lock(&((pTier)->lock))
#define tfsUnLockTier(pTier) pthread_spin_unlock(&((pTier)->lock))

H
refact  
Hongze Cheng 已提交
24
// PROTECTED ==========================================
H
Hongze Cheng 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37
int tfsInitTier(STier *pTier, int level) {
  memset((void *)pTier, 0, sizeof(*pTier));

  int code = pthread_spin_init(&(pTier->lock), 0);
  if (code) {
    terrno = TAOS_SYSTEM_ERROR(code);
    return -1;
  }

  pTier->level = level;

  return 0;
}
H
Hongze Cheng 已提交
38

H
refact  
Hongze Cheng 已提交
39
void tfsDestroyTier(STier *pTier) {
H
Hongze Cheng 已提交
40
  for (int id = 0; id < TSDB_MAX_DISKS_PER_TIER; id++) {
H
Hongze Cheng 已提交
41
    DISK_AT_TIER(pTier, id) = tfsFreeDisk(DISK_AT_TIER(pTier, id));
H
Hongze Cheng 已提交
42
  }
H
Hongze Cheng 已提交
43

H
refact  
Hongze Cheng 已提交
44
  pTier->ndisk = 0;
H
Hongze Cheng 已提交
45 46

  pthread_spin_destroy(&(pTier->lock));
H
Hongze Cheng 已提交
47 48
}

H
refact  
Hongze Cheng 已提交
49
SDisk *tfsMountDiskToTier(STier *pTier, SDiskCfg *pCfg) {
H
Hongze Cheng 已提交
50
  ASSERT(pTier->level == pCfg->level);
H
Hongze Cheng 已提交
51 52 53

  int    id = 0;
  SDisk *pDisk;
H
Hongze Cheng 已提交
54

H
Hongze Cheng 已提交
55
  if (TIER_NDISKS(pTier) >= TSDB_MAX_DISKS_PER_TIER) {
H
refact  
Hongze Cheng 已提交
56
    terrno = TSDB_CODE_FS_TOO_MANY_MOUNT;
H
Hongze Cheng 已提交
57
    return NULL;
H
Hongze Cheng 已提交
58 59
  }

H
refact  
Hongze Cheng 已提交
60 61
  if (pTier->level == 0) {
    if (DISK_AT_TIER(pTier, 0) != NULL) {
H
Hongze Cheng 已提交
62
      id = pTier->ndisk;
H
refact  
Hongze Cheng 已提交
63
    } else {
H
fix bug  
Hongze Cheng 已提交
64 65 66 67 68
      if (pCfg->primary) {
        id = 0;
      } else {
        id = pTier->ndisk + 1;
      }
H
Hongze Cheng 已提交
69
      if (id >= TSDB_MAX_DISKS_PER_TIER) {
H
refact  
Hongze Cheng 已提交
70
        terrno = TSDB_CODE_FS_TOO_MANY_MOUNT;
H
Hongze Cheng 已提交
71
        return NULL;
H
refact  
Hongze Cheng 已提交
72
      }
H
Hongze Cheng 已提交
73
    }
H
refact  
Hongze Cheng 已提交
74 75
  } else {
    id = pTier->ndisk;
H
Hongze Cheng 已提交
76 77
  }

H
Hongze Cheng 已提交
78 79 80
  pDisk = tfsNewDisk(pCfg->level, id, pCfg->dir);
  if (pDisk == NULL) return NULL;
  DISK_AT_TIER(pTier, id) = pDisk;
H
refact  
Hongze Cheng 已提交
81
  pTier->ndisk++;
H
Hongze Cheng 已提交
82

H
more  
Hongze Cheng 已提交
83
  fInfo("disk %s is mounted to tier level %d id %d", pCfg->dir, pCfg->level, id);
H
Hongze Cheng 已提交
84

H
Hongze Cheng 已提交
85
  return DISK_AT_TIER(pTier, id);
H
Hongze Cheng 已提交
86 87
}

H
Hongze Cheng 已提交
88 89 90 91 92 93 94 95 96
void tfsUpdateTierInfo(STier *pTier, STierMeta *pTierMeta) {
  STierMeta tmeta;

  if (pTierMeta == NULL) {
    pTierMeta = &tmeta;
  }
  memset(pTierMeta, 0, sizeof(*pTierMeta));

  tfsLockTier(pTier);
H
Hongze Cheng 已提交
97

H
Hongze Cheng 已提交
98
  for (int id = 0; id < pTier->ndisk; id++) {
H
Hongze Cheng 已提交
99 100 101 102
    if (tfsUpdateDiskInfo(DISK_AT_TIER(pTier, id)) < 0) {
      continue;
    }
    pTierMeta->size += DISK_SIZE(DISK_AT_TIER(pTier, id));
103
    pTierMeta->used += DISK_USED_SIZE(DISK_AT_TIER(pTier, id));
H
Hongze Cheng 已提交
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
    pTierMeta->free += DISK_FREE_SIZE(DISK_AT_TIER(pTier, id));
    pTierMeta->nAvailDisks++;
  }

  pTier->tmeta = *pTierMeta;

  tfsUnLockTier(pTier);
}

// Round-Robin to allocate disk on a tier
int tfsAllocDiskOnTier(STier *pTier) {
  ASSERT(pTier->ndisk > 0);
  int    id = TFS_UNDECIDED_ID;
  SDisk *pDisk;

  tfsLockTier(pTier);

  if (TIER_AVAIL_DISKS(pTier) <= 0) {
    tfsUnLockTier(pTier);
    return id;
  }

  id = pTier->nextid;
  while (true) {
    pDisk = DISK_AT_TIER(pTier, id);
    ASSERT(pDisk != NULL);

    if (DISK_FREE_SIZE(pDisk) < TFS_MIN_DISK_FREE_SIZE) {
      id = (id + 1) % pTier->ndisk;
      if (id == pTier->nextid) {
        tfsUnLockTier(pTier);
        return TFS_UNDECIDED_ID;
      } else {
        continue;
      }
    } else {
      pTier->nextid = (id + 1) % pTier->ndisk;
      break;
    }
  }

  tfsUnLockTier(pTier);
  return id;
}

void tfsGetTierMeta(STier *pTier, STierMeta *pTierMeta) {
  ASSERT(pTierMeta != NULL);

  tfsLockTier(pTier);
  *pTierMeta = pTier->tmeta;
  tfsUnLockTier(pTier);
}

void tfsPosNextId(STier *pTier) {
  ASSERT(pTier->ndisk > 0);
  int nextid = 0;

  for (int id = 1; id < pTier->ndisk; id++) {
    SDisk *pLDisk = DISK_AT_TIER(pTier, nextid);
    SDisk *pDisk = DISK_AT_TIER(pTier, id);
    if (DISK_FREE_SIZE(pDisk) > TFS_MIN_DISK_FREE_SIZE && DISK_FREE_SIZE(pDisk) > DISK_FREE_SIZE(pLDisk)) {
      nextid = id;
    }
H
Hongze Cheng 已提交
167
  }
H
Hongze Cheng 已提交
168

H
Hongze Cheng 已提交
169
  pTier->nextid = nextid;
170
}