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
int32_t tfsInitTier(STfsTier *pTier, int32_t level) {
  memset(pTier, 0, sizeof(STfsTier));
H
Hongze Cheng 已提交
21

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

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

S
Shengliang Guan 已提交
31 32
void tfsDestroyTier(STfsTier *pTier) {
  for (int32_t id = 0; id < TFS_MAX_DISKS_PER_TIER; id++) {
33
    pTier->disks[id] = tfsFreeDisk(pTier->disks[id]);
H
Hongze Cheng 已提交
34
  }
H
Hongze Cheng 已提交
35

H
refact  
Hongze Cheng 已提交
36
  pTier->ndisk = 0;
S
Shengliang Guan 已提交
37
  pthread_spin_destroy(&pTier->lock);
H
Hongze Cheng 已提交
38 39
}

S
Shengliang Guan 已提交
40 41
STfsDisk *tfsMountDiskToTier(STfsTier *pTier, SDiskCfg *pCfg) {
  if (pTier->ndisk >= TFS_MAX_DISKS_PER_TIER) {
H
refact  
Hongze Cheng 已提交
42
    terrno = TSDB_CODE_FS_TOO_MANY_MOUNT;
H
Hongze Cheng 已提交
43
    return NULL;
H
Hongze Cheng 已提交
44 45
  }

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

S
Shengliang Guan 已提交
61
  if (id >= TFS_MAX_DISKS_PER_TIER) {
62 63 64 65
    terrno = TSDB_CODE_FS_TOO_MANY_MOUNT;
    return NULL;
  }

S
Shengliang Guan 已提交
66
  STfsDisk *pDisk = tfsNewDisk(pCfg->level, id, pCfg->dir);
H
Hongze Cheng 已提交
67
  if (pDisk == NULL) return NULL;
S
Shengliang Guan 已提交
68

69
  pTier->disks[id] = pDisk;
H
refact  
Hongze Cheng 已提交
70
  pTier->ndisk++;
H
Hongze Cheng 已提交
71

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

S
Shengliang Guan 已提交
76
void tfsUpdateTierSize(STfsTier *pTier) {
77
  SDiskSize size = {0};
S
Shengliang Guan 已提交
78
  int32_t   nAvailDisks = 0;
H
Hongze Cheng 已提交
79 80

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

S
Shengliang Guan 已提交
82
  for (int32_t id = 0; id < pTier->ndisk; id++) {
S
Shengliang Guan 已提交
83
    STfsDisk *pDisk = pTier->disks[id];
84
    if (pDisk == NULL) continue;
S
Shengliang Guan 已提交
85
    if (tfsUpdateDiskSize(pDisk) < 0) continue;
S
Shengliang Guan 已提交
86

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

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

  tfsUnLockTier(pTier);
}

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

  tfsLockTier(pTier);

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

110
  int32_t retId = -1;
S
Shengliang Guan 已提交
111 112 113
  for (int32_t id = 0; id < TFS_MAX_DISKS_PER_TIER; ++id) {
    int32_t   diskId = (pTier->nextid + id) % pTier->ndisk;
    STfsDisk *pDisk = pTier->disks[diskId];
H
Hongze Cheng 已提交
114

115
    if (pDisk == NULL) continue;
H
Hongze Cheng 已提交
116

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

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

  tfsUnLockTier(pTier);
126
  return retId;
H
Hongze Cheng 已提交
127 128
}

S
Shengliang Guan 已提交
129
void tfsPosNextId(STfsTier *pTier) {
S
Shengliang Guan 已提交
130
  int32_t nextid = 0;
H
Hongze Cheng 已提交
131

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

H
Hongze Cheng 已提交
140
  pTier->nextid = nextid;
141
}