/
cache.go
152 lines (138 loc) · 3.68 KB
/
cache.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright (C) 2014-2018 Goodrain Co., Ltd.
// RAINBOND, Application Management Platform
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version. For any non-GPL usage of Rainbond,
// one or multiple Commercial Licenses authorized by Goodrain Co., Ltd.
// must be obtained first.
// 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. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package statistical
import (
"fmt"
"os"
"strings"
"sync"
"time"
"github.com/Sirupsen/logrus"
"github.com/goodrain/rainbond/db"
"github.com/goodrain/rainbond/db/model"
"github.com/goodrain/rainbond/util"
"golang.org/x/net/context"
)
//DiskCache 磁盘异步统计
type DiskCache struct {
cache []struct {
Key string
Value float64
}
dbmanager db.Manager
ctx context.Context
lock sync.Mutex
cancel context.CancelFunc
}
//CreatDiskCache 创建
func CreatDiskCache(ctx context.Context) *DiskCache {
cctx, cancel := context.WithCancel(ctx)
return &DiskCache{
dbmanager: db.GetManager(),
ctx: cctx,
cancel: cancel,
}
}
//Start 开始启动统计
func (d *DiskCache) Start() {
d.setcache()
timer := time.NewTimer(time.Minute * 5)
defer timer.Stop()
for {
select {
case <-d.ctx.Done():
return
case <-timer.C:
d.setcache()
timer.Reset(time.Minute * 5)
}
}
}
//Stop stop
func (d *DiskCache) Stop() {
d.cancel()
}
func (d *DiskCache) setcache() {
logrus.Info("start get all service disk size")
start := time.Now()
var diskcache []struct {
Key string
Value float64
}
services, err := d.dbmanager.TenantServiceDao().GetAllServicesID()
if err != nil {
logrus.Errorln("Error get tenant service when select db :", err)
return
}
volumes, err := d.dbmanager.TenantServiceVolumeDao().GetAllVolumes()
if err != nil {
logrus.Errorln("Error get tenant service volume when select db :", err)
return
}
sharePath := os.Getenv("SHARE_DATA_PATH")
if sharePath == "" {
sharePath = "/grdata"
}
var cache = make(map[string]*model.TenantServices)
for _, service := range services {
//service nfs volume
size := util.GetDirSize(fmt.Sprintf("%s/tenant/%s/service/%s", sharePath, service.TenantID, service.ServiceID))
if size != 0 {
diskcache = append(diskcache, struct {
Key string
Value float64
}{
Key: service.ServiceID + "_" + service.TenantID,
Value: size,
})
}
cache[service.ServiceID] = service
}
for _, v := range volumes {
if v.VolumeType == string(model.LocalVolumeType) {
//TODO
}
}
d.cache = diskcache
logrus.Infof("end get all service disk size,time consum %2.f s", time.Now().Sub(start).Seconds())
}
//Get 获取磁盘统计结果
func (d *DiskCache) Get() map[string]float64 {
newcache := make(map[string]float64)
for _, v := range d.cache {
newcache[v.Key] += v.Value
}
return newcache
}
//GetTenantDisk GetTenantDisk
func (d *DiskCache) GetTenantDisk(tenantID string) float64 {
var value float64
for _, v := range d.cache {
if strings.HasSuffix(v.Key, "_"+tenantID) {
value += v.Value
}
}
return value
}
//GetServiceDisk GetServiceDisk
func (d *DiskCache) GetServiceDisk(serviceID string) float64 {
var value float64
for _, v := range d.cache {
if strings.HasPrefix(v.Key, serviceID+"_") {
value += v.Value
}
}
return value
}