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

S
Shengliang Guan 已提交
16 17
#define _DEFAULT_SOURCE
#include "tfsInt.h"
H
Hongze Cheng 已提交
18

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

H
refact  
Hongze Cheng 已提交
22
// PROTECTED ==========================================
H
Hongze Cheng 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35
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 已提交
36

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

H
refact  
Hongze Cheng 已提交
42
  pTier->ndisk = 0;
H
Hongze Cheng 已提交
43 44

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

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

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

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

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

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

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

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

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

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

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

H
Hongze Cheng 已提交
96
  for (int id = 0; id < pTier->ndisk; id++) {
H
Hongze Cheng 已提交
97 98 99 100
    if (tfsUpdateDiskInfo(DISK_AT_TIER(pTier, id)) < 0) {
      continue;
    }
    pTierMeta->size += DISK_SIZE(DISK_AT_TIER(pTier, id));
101
    pTierMeta->used += DISK_USED_SIZE(DISK_AT_TIER(pTier, id));
H
Hongze Cheng 已提交
102 103 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
    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 已提交
165
  }
H
Hongze Cheng 已提交
166

H
Hongze Cheng 已提交
167
  pTier->nextid = nextid;
168
}