-
Notifications
You must be signed in to change notification settings - Fork 0
/
bfs.go
135 lines (125 loc) · 3.19 KB
/
bfs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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
package service
import (
"context"
"go-common/app/admin/main/bfs/model"
"go-common/library/ecode"
)
// Total .
func (s *Service) Total(c context.Context, arg *model.ArgCluster) (resp *model.RespTotal, err error) {
volumes, err := s.Volumes(c, arg)
if err != nil {
return
}
num := int64(len(volumes.Volumes))
var fs int64
for _, volume := range volumes.Volumes {
fs += int64(volume.FreeSpace)
}
fs = (fs * 8) / 1024 / 1024 / 1024 // GB
// groups
groups, err := s.d.Groups(arg.Cluster)
if err != nil {
return
}
// stores
racks, err := s.d.Racks(arg.Cluster)
if err != nil {
return
}
var stores int64
for _, rack := range racks {
stores += int64(len(rack.Stores))
}
resp = &model.RespTotal{
Space: 32 * num,
FreeSpace: fs,
Groups: int64(len(groups)),
Stores: stores,
Volumes: num,
}
return
}
// Racks .
func (s *Service) Racks(c context.Context, arg *model.ArgCluster) (resp *model.RespRack, err error) {
racks, err := s.d.Racks(arg.Cluster)
if err != nil {
return
}
for _, rack := range racks {
for _, store := range rack.Stores {
store.ParseStates()
}
}
resp = &model.RespRack{Racks: racks}
return
}
// Groups .
func (s *Service) Groups(c context.Context, arg *model.ArgCluster) (resp *model.RespGroup, err error) {
groups, err := s.d.Groups(arg.Cluster)
if err != nil {
return
}
racks, err := s.Racks(c, arg)
if err != nil {
return
}
volumes, err := s.Volumes(c, arg)
if err != nil {
return
}
for _, group := range groups {
group.StoreDatas = make(map[string]*model.Store)
for _, rack := range racks.Racks {
for sname, store := range rack.Stores {
for _, name := range group.Stores {
if sname == name {
group.StoreDatas[name] = store
}
}
}
}
for _, store := range group.StoreDatas {
num := int64(len(store.Volumes))
var fs int64
for _, volume := range store.Volumes {
fs += int64(volumes.Volumes[volume].FreeSpace)
}
fs = (fs * 8) / 1024 / 1024 / 1024 // GB
group.Total.Space = 32 * num
group.Total.FreeSpace = fs
group.Total.Volumes = num
break
}
}
resp = &model.RespGroup{Groups: groups}
return
}
// Volumes .
func (s *Service) Volumes(c context.Context, arg *model.ArgCluster) (resp *model.RespVolume, err error) {
volumes, err := s.d.Volumes(arg.Cluster)
if err != nil {
return
}
resp = &model.RespVolume{Volumes: volumes}
return
}
// AddVolume add volume.
func (s *Service) AddVolume(c context.Context, arg *model.ArgAddVolume) (err error) {
return s.d.AddVolume(c, arg.Group, arg.Num)
}
// AddFreeVolume add free volume.
func (s *Service) AddFreeVolume(c context.Context, arg *model.ArgAddFreeVolume) (err error) {
return s.d.AddFreeVolume(c, arg.Group, arg.Dir, arg.Num)
}
// Compact compact store.
func (s *Service) Compact(c context.Context, arg *model.ArgCompact) (err error) {
return s.d.Compact(c, arg.Group, arg.Vid)
}
// SetGroupStatus set group status(read,write,sync,health).
func (s *Service) SetGroupStatus(c context.Context, arg *model.ArgGroupStatus) (err error) {
if arg.Status != "read" && arg.Status != "write" && arg.Status != "sync" && arg.Status != "health" {
err = ecode.RequestErr
return
}
return s.d.SetGroupStatus(c, arg.Group, arg.Status)
}