-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache_policy.go
124 lines (95 loc) · 3.92 KB
/
cache_policy.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
/*
Licensed under the MIT License <http://opensource.org/licenses/MIT>.
Copyright © 2023-2024 Seagate Technology LLC and/or its Affiliates
Copyright © 2020-2024 Microsoft Corporation. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE
*/
package file_cache
import (
"fmt"
"os"
"github.com/Seagate/cloudfuse/common"
"github.com/Seagate/cloudfuse/common/log"
"github.com/Seagate/cloudfuse/internal/stats_manager"
)
const DefaultEvictTime = 10
type cachePolicyConfig struct {
tmpPath string
cacheTimeout uint32
maxEviction uint32
maxSizeMB float64
highThreshold float64
lowThreshold float64
fileLocks *common.LockMap
policyTrace bool
}
type cachePolicy interface {
StartPolicy() error
ShutdownPolicy() error
UpdateConfig(cachePolicyConfig) error
CacheValid(name string) // Mark the file as hit
CacheInvalidate(name string) // Invalidate the file
CachePurge(name string) // Schedule the file for deletion
IsCached(name string) bool // Whether or not the cache policy considers this file cached
Name() string // The name of the policy
}
// getUsagePercentage: The current cache usage as a percentage of the maxSize
func getUsagePercentage(path string, maxSize float64) float64 {
var currSize float64
var usagePercent float64
var err error
if maxSize == 0 {
currSize, usagePercent, err = common.GetDiskUsageFromStatfs(path)
if err != nil {
log.Err("cachePolicy::getUsagePercentage : failed to get disk usage for %s [%v]", path, err.Error)
}
} else {
// We need to compute % usage of temp directory against configured limit
currSize, err = common.GetUsage(path)
if err != nil {
log.Err("cachePolicy::getUsagePercentage : failed to get directory usage for %s [%v]", path, err.Error)
}
usagePercent = (currSize / float64(maxSize)) * 100
}
log.Debug("cachePolicy::getUsagePercentage : current cache usage : %f%%", usagePercent)
fileCacheStatsCollector.UpdateStats(stats_manager.Replace, cacheUsage, fmt.Sprintf("%f MB", currSize))
fileCacheStatsCollector.UpdateStats(stats_manager.Replace, usgPer, fmt.Sprintf("%f%%", usagePercent))
return usagePercent
}
// Delete a given file
func deleteFile(name string) error {
log.Debug("cachePolicy::deleteFile : attempting to delete %s", name)
err := os.Remove(name)
if err != nil && os.IsPermission(err) {
// File is not having delete permissions so change the mode and retry deletion
log.Warn("cachePolicy::deleteFile : failed to delete %s due to permission", name)
err = os.Chmod(name, os.FileMode(0666))
if err != nil {
log.Err("cachePolicy::deleteFile : %s failed to reset permissions", name)
return err
}
err = os.Remove(name)
} else if err != nil && os.IsNotExist(err) {
log.Debug("cachePolicy::deleteFile : %s does not exist in local cache", name)
return nil
}
if err != nil {
log.Err("lruPolicy::DeleteItem : Failed to delete local file %s, [%s]", name, err.Error())
return err
}
return nil
}