generated from kubernetes/kubernetes-template-project
/
cache.go
237 lines (200 loc) · 6.71 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
Copyright 2023 The Fluid Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package alluxio
import (
"fmt"
"time"
"github.com/fluid-cloudnative/fluid/api/v1alpha1"
"github.com/fluid-cloudnative/fluid/pkg/ddc/alluxio/operations"
"github.com/fluid-cloudnative/fluid/pkg/utils"
"github.com/fluid-cloudnative/fluid/pkg/utils/kubeclient"
"github.com/pkg/errors"
)
var (
cachedPercentageFormat = "%.1f%%"
)
// queryCacheStatus checks the cache status
func (e *AlluxioEngine) queryCacheStatus() (states cacheStates, err error) {
// get alluxio fsadmin report summary
summary, err := e.GetReportSummary()
if err != nil {
e.Log.Error(err, "Failed to get Alluxio summary when query cache status")
return states, err
}
if len(summary) == 0 {
return states, errors.New("Alluxio summary is empty")
}
// parse alluxio fsadmin report summary
states = e.ParseReportSummary(summary)
dataset, err := utils.GetDataset(e.Client, e.name, e.namespace)
if err != nil {
if utils.IgnoreNotFound(err) != nil {
e.Log.Error(err, "Failed to get dataset when query cache status")
}
return states, err
}
e.patchDatasetStatus(dataset, &states)
states.cacheHitStates = e.GetCacheHitStates()
return states, nil
}
func (e AlluxioEngine) patchDatasetStatus(dataset *v1alpha1.Dataset, states *cacheStates) {
// skip when `dataset.Status.UfsTotal` is empty
if dataset.Status.UfsTotal == "" {
return
}
// skip when `dataset.Status.UfsTotal` is "[Calculating]"
if dataset.Status.UfsTotal == metadataSyncNotDoneMsg {
return
}
usedInBytes, _ := utils.FromHumanSize(states.cached)
ufsTotalInBytes, _ := utils.FromHumanSize(dataset.Status.UfsTotal)
states.cachedPercentage = fmt.Sprintf(cachedPercentageFormat, float64(usedInBytes)/float64(ufsTotalInBytes)*100.0)
}
// getCacheHitStates gets cache hit related info by parsing Alluxio metrics
func (e *AlluxioEngine) GetCacheHitStates() (cacheHitStates cacheHitStates) {
// get cache hit states every 1 minute(cacheHitQueryIntervalMin * 20s)
cacheHitStates.timestamp = time.Now()
if e.lastCacheHitStates != nil && cacheHitStates.timestamp.Sub(e.lastCacheHitStates.timestamp).Minutes() < cacheHitQueryIntervalMin {
return *e.lastCacheHitStates
}
metrics, err := e.GetReportMetrics()
if err != nil {
e.Log.Error(err, "Failed to get Alluxio metrics when get cache hit states")
if e.lastCacheHitStates != nil {
return *e.lastCacheHitStates
}
return
}
e.ParseReportMetric(metrics, &cacheHitStates, e.lastCacheHitStates)
// refresh last cache hit states
e.lastCacheHitStates = &cacheHitStates
return
}
// getCachedCapacityOfNode cacluates the node
//func (e *AlluxioEngine) getCurrentCachedCapacity() (totalCapacity int64, err error) {
// workerName := e.getWorkerDaemonsetName()
// pods, err := e.getRunningPodsOfDaemonset(workerName, e.namespace)
// if err != nil {
// return totalCapacity, err
// }
//
// for _, pod := range pods {
// nodeName := pod.Spec.NodeName
// if nodeName == "" {
// e.Log.Info("The node is skipped due to its node name is null", "node", pod.Spec.NodeName,
// "pod", pod.Name, "namespace", e.namespace)
// continue
// }
//
// capacity, err := e.getCurrentCacheCapacityOfNode(nodeName)
// if err != nil {
// return totalCapacity, err
// }
// totalCapacity += capacity
// }
//
// return
//
//}
// getCurrentCacheCapacityOfNode cacluates the node
//func (e *AlluxioEngine) getCurrentCacheCapacityOfNode(nodeName string) (capacity int64, err error) {
// labelName := e.getStoragetLabelname(humanReadType, totalStorageType)
//
// node, err := kubeclient.GetNode(e.Client, nodeName)
// if err != nil {
// return capacity, err
// }
//
// if !kubeclient.IsReady(*node) {
// e.Log.Info("Skip the not ready node", "node", node.Name)
// return 0, nil
// }
//
// for k, v := range node.Labels {
// if k == labelName {
// value := "0"
// if v != "" {
// value = v
// }
// // capacity = units.BytesSize(float64(value))
// capacity, err = units.RAMInBytes(value)
// if err != nil {
// return capacity, err
// }
// e.Log.V(1).Info("getCurrentCacheCapacityOfNode", k, value)
// e.Log.V(1).Info("getCurrentCacheCapacityOfNode byteSize", k, capacity)
// }
// }
//
// return
//
//}
// get the value of cached
// func (e *AlluxioEngine) cachedState() (int64, error) {
// podName, containerName := e.getMasterPodInfo()
// fileUitls := operations.NewAlluxioFileUtils(podName, containerName, e.namespace, e.Log)
// cached, err := fileUitls.CachedState()
// return int64(cached), err
// }
// clean cache
func (e *AlluxioEngine) invokeCleanCache(path string) (err error) {
// 1. Check if master is ready, if not, just return
masterName := e.getMasterName()
master, err := kubeclient.GetStatefulSet(e.Client, masterName, e.namespace)
if err != nil {
if utils.IgnoreNotFound(err) == nil {
e.Log.Info("Failed to get master", "err", err.Error())
return nil
}
// other error
return err
}
if master.Status.ReadyReplicas == 0 {
e.Log.Info("The master is not ready, just skip clean cache.", "master", masterName)
return nil
} else {
e.Log.Info("The master is ready, so start cleaning cache", "master", masterName)
}
// 2. run clean action
podName, containerName := e.getMasterPodInfo()
fileUitls := operations.NewAlluxioFileUtils(podName, containerName, e.namespace, e.Log)
cleanCacheGracePeriodSeconds, err := e.getCleanCacheGracePeriodSeconds()
if err != nil {
return err
}
return fileUitls.CleanCache(path, cleanCacheGracePeriodSeconds)
}
func (e *AlluxioEngine) getGracefulShutdownLimits() (gracefulShutdownLimits int32, err error) {
runtime, err := e.getRuntime()
if err != nil {
return
}
if runtime.Spec.RuntimeManagement.CleanCachePolicy.MaxRetryAttempts != nil {
gracefulShutdownLimits = *runtime.Spec.RuntimeManagement.CleanCachePolicy.MaxRetryAttempts
} else {
gracefulShutdownLimits = defaultGracefulShutdownLimits
}
return
}
func (e *AlluxioEngine) getCleanCacheGracePeriodSeconds() (cleanCacheGracePeriodSeconds int32, err error) {
runtime, err := e.getRuntime()
if err != nil {
return
}
if runtime.Spec.RuntimeManagement.CleanCachePolicy.GracePeriodSeconds != nil {
cleanCacheGracePeriodSeconds = *runtime.Spec.RuntimeManagement.CleanCachePolicy.GracePeriodSeconds
} else {
cleanCacheGracePeriodSeconds = defaultCleanCacheGracePeriodSeconds
}
return
}