tfsTier.c 3.4 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

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

S
Shengliang Guan 已提交
22 23
int32_t tfsInitTier(STier *pTier, int32_t level) {
  memset(pTier, 0, sizeof(STier));
H
Hongze Cheng 已提交
24

25 26
  if (pthread_spin_init(&pTier->lock, 0) != 0) {
    terrno = TAOS_SYSTEM_ERROR(errno);
H
Hongze Cheng 已提交
27 28 29 30 31 32
    return -1;
  }

  pTier->level = level;
  return 0;
}
H
Hongze Cheng 已提交
33

H
refact  
Hongze Cheng 已提交
34
void tfsDestroyTier(STier *pTier) {
S
Shengliang Guan 已提交
35
  for (int32_t id = 0; id < TSDB_MAX_DISKS_PER_TIER; id++) {
36
    pTier->disks[id] = tfsFreeDisk(pTier->disks[id]);
H
Hongze Cheng 已提交
37
  }
H
Hongze Cheng 已提交
38

H
refact  
Hongze Cheng 已提交
39
  pTier->ndisk = 0;
H
Hongze Cheng 已提交
40
  pthread_spin_destroy(&(pTier->lock));
H
Hongze Cheng 已提交
41 42
}

H
refact  
Hongze Cheng 已提交
43
SDisk *tfsMountDiskToTier(STier *pTier, SDiskCfg *pCfg) {
44
  if (pTier->ndisk >= TSDB_MAX_DISKS_PER_TIER) {
H
refact  
Hongze Cheng 已提交
45
    terrno = TSDB_CODE_FS_TOO_MANY_MOUNT;
H
Hongze Cheng 已提交
46
    return NULL;
H
Hongze Cheng 已提交
47 48
  }

S
Shengliang Guan 已提交
49
  int32_t id = 0;
H
refact  
Hongze Cheng 已提交
50
  if (pTier->level == 0) {
51
    if (pTier->disks[0] != NULL) {
H
Hongze Cheng 已提交
52
      id = pTier->ndisk;
H
refact  
Hongze Cheng 已提交
53
    } else {
H
fix bug  
Hongze Cheng 已提交
54 55 56 57 58
      if (pCfg->primary) {
        id = 0;
      } else {
        id = pTier->ndisk + 1;
      }
H
Hongze Cheng 已提交
59
    }
H
refact  
Hongze Cheng 已提交
60 61
  } else {
    id = pTier->ndisk;
H
Hongze Cheng 已提交
62 63
  }

64 65 66 67 68
  if (id >= TSDB_MAX_DISKS_PER_TIER) {
    terrno = TSDB_CODE_FS_TOO_MANY_MOUNT;
    return NULL;
  }

S
Shengliang Guan 已提交
69
  SDisk *pDisk = tfsNewDisk(pCfg->level, id, pCfg->dir);
H
Hongze Cheng 已提交
70
  if (pDisk == NULL) return NULL;
S
Shengliang Guan 已提交
71

72
  pTier->disks[id] = pDisk;
H
refact  
Hongze Cheng 已提交
73
  pTier->ndisk++;
H
Hongze Cheng 已提交
74

H
more  
Hongze Cheng 已提交
75
  fInfo("disk %s is mounted to tier level %d id %d", pCfg->dir, pCfg->level, id);
76
  return pTier->disks[id];
H
Hongze Cheng 已提交
77 78
}

79 80 81
void tfsUpdateTierSize(STier *pTier) {
  SDiskSize size = {0};
  int16_t   nAvailDisks = 0;
H
Hongze Cheng 已提交
82 83

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

S
Shengliang Guan 已提交
85
  for (int32_t id = 0; id < pTier->ndisk; id++) {
86 87
    SDisk *pDisk = pTier->disks[id];
    if (pDisk == NULL) continue;
S
Shengliang Guan 已提交
88

89 90 91 92
    size.total += pDisk->size.total;
    size.used += pDisk->size.used;
    size.avail += pDisk->size.avail;
    nAvailDisks++;
H
Hongze Cheng 已提交
93 94
  }

95 96
  pTier->size = size;
  pTier->nAvailDisks = nAvailDisks;
H
Hongze Cheng 已提交
97 98 99 100 101

  tfsUnLockTier(pTier);
}

// Round-Robin to allocate disk on a tier
S
Shengliang Guan 已提交
102
int32_t tfsAllocDiskOnTier(STier *pTier) {
103
  terrno = TSDB_CODE_FS_NO_VALID_DISK;
H
Hongze Cheng 已提交
104 105 106

  tfsLockTier(pTier);

107
  if (pTier->ndisk <= 0 || pTier->nAvailDisks <= 0) {
H
Hongze Cheng 已提交
108
    tfsUnLockTier(pTier);
109
    return -1;
H
Hongze Cheng 已提交
110 111
  }

112 113 114 115
  int32_t retId = -1;
  for (int32_t id = 0; id < TSDB_MAX_DISKS_PER_TIER; ++id) {
    int32_t diskId = (pTier->nextid + id) % pTier->ndisk;
    SDisk  *pDisk = pTier->disks[diskId];
H
Hongze Cheng 已提交
116

117
    if (pDisk == NULL) continue;
H
Hongze Cheng 已提交
118

119
    if (pDisk->size.avail < TFS_MIN_DISK_FREE_SIZE) continue;
H
Hongze Cheng 已提交
120

121 122 123 124 125
    retId = diskId;
    terrno = 0;
    pTier->nextid = (diskId + 1) % pTier->ndisk;
    break;
  }
H
Hongze Cheng 已提交
126 127

  tfsUnLockTier(pTier);
128
  return retId;
H
Hongze Cheng 已提交
129 130 131
}

void tfsPosNextId(STier *pTier) {
S
Shengliang Guan 已提交
132
  int32_t nextid = 0;
H
Hongze Cheng 已提交
133

S
Shengliang Guan 已提交
134
  for (int32_t id = 1; id < pTier->ndisk; id++) {
135 136 137
    SDisk *pLDisk = pTier->disks[nextid];
    SDisk *pDisk = pTier->disks[id];
    if (pDisk->size.avail > TFS_MIN_DISK_FREE_SIZE && pDisk->size.avail > pLDisk->size.avail) {
H
Hongze Cheng 已提交
138 139
      nextid = id;
    }
H
Hongze Cheng 已提交
140
  }
H
Hongze Cheng 已提交
141

H
Hongze Cheng 已提交
142
  pTier->nextid = nextid;
143
}