-
Notifications
You must be signed in to change notification settings - Fork 405
/
evictor.go
215 lines (169 loc) · 4.32 KB
/
evictor.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
package agent
import (
"sync"
"sync/atomic"
"github.com/fnproject/fn/api/id"
"github.com/sirupsen/logrus"
)
// Evictor For Agent
// Agent hot containers register themselves to the evictor system.
// A starved request can call PerformEviction() to scan the evictable
// hot containers and if a number of these can be evicted to satisfy
// memory+cpu needs of the starved request, then those hot-containers
// are evicted.
type tokenKey struct {
id string
slotId string
memory uint64
cpu uint64
}
type EvictToken struct {
key tokenKey
evictable uint32
C chan struct{}
DoneChan chan struct{}
}
type Evictor interface {
// CreateEvictToken creates an eviction token to be used in evictor tracking. Returns
// an eviction token.
CreateEvictToken(slotId string, mem, cpu uint64) *EvictToken
// DeleteEvictToken deletes an eviction token from evictor system
DeleteEvictToken(token *EvictToken)
// PerformEviction performs evictions to satisfy cpu & mem arguments
// and returns a slice of channels for evictions performed. The callers
// can wait on these channel to ensure evictions are completed.
PerformEviction(slotId string, mem, cpu uint64) []chan struct{}
}
type evictor struct {
lock sync.Mutex
id uint64
tokens map[string]*EvictToken
slots []tokenKey
}
func NewEvictor() Evictor {
return &evictor{
tokens: make(map[string]*EvictToken),
slots: make([]tokenKey, 0),
}
}
func (tok *EvictToken) isEvicted() bool {
select {
case <-tok.C:
return true
default:
}
return false
}
func (token *EvictToken) SetEvictable(isEvictable bool) {
val := uint32(0)
if isEvictable {
val = 1
}
atomic.StoreUint32(&token.evictable, val)
}
func (tok *EvictToken) isEligible() bool {
// if no resource limits are in place, then this
// function is not eligible.
if tok.key.memory == 0 && tok.key.cpu == 0 {
return false
}
return true
}
func (e *evictor) CreateEvictToken(slotId string, mem, cpu uint64) *EvictToken {
key := tokenKey{
id: id.New().String(),
slotId: slotId,
memory: mem,
cpu: cpu,
}
token := &EvictToken{
key: key,
C: make(chan struct{}),
DoneChan: make(chan struct{}),
}
if !token.isEligible() {
return token
}
e.lock.Lock()
_, ok := e.tokens[token.key.id]
if ok {
logrus.Fatalf("id collusion key=%+v", key)
}
e.tokens[token.key.id] = token
e.slots = append(e.slots, token.key)
e.lock.Unlock()
return token
}
func (e *evictor) DeleteEvictToken(token *EvictToken) {
if !token.isEligible() {
return
}
e.lock.Lock()
for idx, val := range e.slots {
if val.id == token.key.id {
e.slots = append(e.slots[:idx], e.slots[idx+1:]...)
break
}
}
delete(e.tokens, token.key.id)
e.lock.Unlock()
close(token.DoneChan)
}
func (e *evictor) PerformEviction(slotId string, mem, cpu uint64) []chan struct{} {
var notifyChans []chan struct{}
// if no resources are defined for this function, then
// we don't know what to do here. We cannot evict anyone
// in this case.
if mem == 0 && cpu == 0 {
return notifyChans
}
// Our eviction sum so far
totalMemory := uint64(0)
totalCpu := uint64(0)
isSatisfied := false
var keys []string
var completionChans []chan struct{}
e.lock.Lock()
for _, val := range e.slots {
// lets not evict from our own slot queue
if slotId == val.slotId {
continue
}
// descend into map to verify evictable state
if atomic.LoadUint32(&e.tokens[val.id].evictable) == 0 {
continue
}
totalMemory += val.memory
totalCpu += val.cpu
keys = append(keys, val.id)
// did we satisfy the need?
if totalMemory >= mem && totalCpu >= cpu {
isSatisfied = true
break
}
}
// If we can satisfy the need, then let's commit/perform eviction
if isSatisfied {
notifyChans = make([]chan struct{}, 0, len(keys))
completionChans = make([]chan struct{}, 0, len(keys))
idx := 0
for _, id := range keys {
// do not initialize idx, we continue where we left off
// since keys are in order from above.
for ; idx < len(e.slots); idx++ {
if id == e.slots[idx].id {
e.slots = append(e.slots[:idx], e.slots[idx+1:]...)
break
}
}
notifyChans = append(notifyChans, e.tokens[id].C)
completionChans = append(completionChans, e.tokens[id].DoneChan)
delete(e.tokens, id)
}
}
e.lock.Unlock()
for _, ch := range notifyChans {
close(ch)
}
return completionChans
}