forked from intelsdi-x/snap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lru.go
124 lines (104 loc) · 3.29 KB
/
lru.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
/*
http://www.apache.org/licenses/LICENSE-2.0.txt
Copyright 2015 Intel Corporation
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 strategy
import (
"time"
log "github.com/Sirupsen/logrus"
"github.com/intelsdi-x/snap/core"
)
// lru provides a stragey that selects the least recently used available plugin.
type lru struct {
*cache
logger *log.Entry
}
func NewLRU(cacheTTL time.Duration) *lru {
return &lru{
NewCache(cacheTTL),
log.WithFields(log.Fields{
"_module": "control-routing",
}),
}
}
// String returns the strategy name.
func (l *lru) String() string {
return "least-recently-used"
}
// CacheTTL returns the TTL for the cache.
func (l *lru) CacheTTL(taskID string) (time.Duration, error) {
return l.ttl, nil
}
// Select selects an available plugin using the least-recently-used strategy.
func (l *lru) Select(spa []SelectablePlugin, _ string) (SelectablePlugin, error) {
t := time.Now()
index := -1
for i, sp := range spa {
// look for the least recently used
if sp.LastHit().Before(t) || index == -1 {
index = i
t = sp.LastHit()
}
}
if index > -1 {
l.logger.WithFields(log.Fields{
"block": "select",
"strategy": l.String(),
"pool size": len(spa),
"index": spa[index].String(),
"hitcount": spa[index].HitCount(),
}).Debug("plugin selected")
return spa[index], nil
}
l.logger.WithFields(log.Fields{
"block": "select",
"strategy": l.String(),
"error": ErrCouldNotSelect,
}).Error("error selecting")
return nil, ErrCouldNotSelect
}
// Remove selects a plugin
// Since there is no state to cleanup we only need to return the selected plugin
func (l *lru) Remove(sp []SelectablePlugin, taskID string) (SelectablePlugin, error) {
p, err := l.Select(sp, taskID)
if err != nil {
return nil, err
}
return p, nil
}
// checkCache checks the cache for metric types.
// returns:
// - array of metrics that need to be collected
// - array of metrics that were returned from the cache
func (l *lru) CheckCache(mts []core.Metric, _ string) ([]core.Metric, []core.Metric) {
return l.checkCache(mts)
}
// updateCache updates the cache with the given array of metrics.
func (l *lru) UpdateCache(mts []core.Metric, _ string) {
l.updateCache(mts)
}
// AllCacheHits returns cache hits across all metrics.
func (l *lru) AllCacheHits() uint64 {
return l.allCacheHits()
}
// AllCacheMisses returns cache misses across all metrics.
func (l *lru) AllCacheMisses() uint64 {
return l.allCacheMisses()
}
// CacheHits returns the cache hits for a given metric namespace and version.
func (l *lru) CacheHits(ns string, version int, _ string) (uint64, error) {
return l.cacheHits(ns, version)
}
// CacheMisses returns the cache misses for a given metric namespace and version.
func (l *lru) CacheMisses(ns string, version int, _ string) (uint64, error) {
return l.cacheMisses(ns, version)
}