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

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

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

  tfsUnLockTier(pTier);
}

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

  tfsLockTier(pTier);

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

109
  int32_t retId = -1;
S
Shengliang Guan 已提交
110 111 112
  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 已提交
113

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

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

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

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

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

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

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