-
Notifications
You must be signed in to change notification settings - Fork 1
/
cache.go
293 lines (255 loc) · 5.98 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package main
import (
"sync"
"time"
"github.com/golang/glog"
)
const (
// Count the number of requests in this window
// Use the amount to determine how much to prefetch
predictWindowInSeconds = 1.0
// How long to wait for next prefetch if last prefetch fails.
closeWaitWindowInSeconds = 0.5
// Minimum prefetch amount
minPrefetchAmount = 10
)
var (
// Converts to time.Duration type for easy use.
predictWindow = time.Duration(predictWindowInSeconds * float64(time.Second))
closeWaitWindow = time.Duration(closeWaitWindowInSeconds * float64(time.Second))
// cache_id for logging in multiple cache case
cache_id int
)
type BucketMode int
const (
OPEN BucketMode = iota
CLOSE
)
// Use this always incremented ID to detect if a node has been
// re-cycled in circular list.
type NodeId uint64
type Node struct {
available int
id NodeId
}
type CacheStat struct {
prefetch_num uint64
prefetch uint64
over_used uint64
expired uint64
}
type Cache struct {
queue *Queue
mode BucketMode
last_prefetch_time time.Time
requests *RollingWindow
server *Server
s CacheStat
mutex sync.Mutex
next_node_id NodeId
id int
}
func max(x, y int) int {
if x > y {
return x
}
return y
}
func min(x, y int) int {
if x < y {
return x
}
return y
}
func NewCache(s *Server) *Cache {
cache_id++
return &Cache{
queue: NewQueue(10),
requests: NewRollingWindow(predictWindow),
server: s,
id: cache_id,
}
}
func (c *Cache) nextNodeId() NodeId {
c.next_node_id++
return c.next_node_id
}
// Decrease the amount from the available queue,
// return the amount that could not been decreased.
func (c *Cache) dec(delta int) int {
n := c.queue.Head()
for n != nil && delta > 0 {
if n.available > 0 {
d := min(n.available, delta)
n.available -= d
delta -= d
}
if n.available > 0 {
return 0
}
c.queue.Pop()
n = c.queue.Head()
}
return delta
}
func (c *Cache) countAvailable() int {
var count int
c.queue.Iterate(func(n *Node) bool {
count += n.available
return true
})
return count
}
func (c *Cache) tryPrefetch() {
// If it is in Close mode, and too close to last request time,
// Not issue a new prefetch, most likely will not get any.
if c.mode == CLOSE &&
time.Since(c.last_prefetch_time) < closeWaitWindow {
return
}
avail := c.countAvailable()
// Count the requests from last second window.
pass_count := c.requests.Count()
// Desired amount.
amount := max(pass_count, minPrefetchAmount)
if glog.V(4) {
glog.Infof("[%d]tryPrefetch, avail: %d, pass: %d, req: %d",
c.id, avail, pass_count, amount)
}
// If available is less than half of desired amount, prefetch them.
if avail < amount/2 {
// Some rare conditions to use tokens before it is granted.
// Specifically designed not to reject the first request after
// long period of no traffic while the prefetch is still on the way.
use_not_granted := avail == 0 && c.mode == OPEN
c.prefetch(amount, use_not_granted)
}
}
// The main entry call to check the rate limit.
// It is a sync call and will return true/false right away.
func (c *Cache) Check() bool {
c.mutex.Lock()
defer c.mutex.Unlock()
if glog.V(4) {
glog.Infof("[%d]Check called", c.id)
}
c.tryPrefetch()
c.requests.Inc()
ret := c.dec(1) == 0
if !ret {
if glog.V(4) {
glog.Infof("[%d]***rejected", c.id)
}
}
return ret
}
func (c *Cache) findNodeById(id NodeId) *Node {
var found *Node
c.queue.Iterate(func(n *Node) bool {
if n.id == id {
found = n
return false
}
return true
})
return found
}
func (c *Cache) prefetch(req_amount int, use_not_granted bool) {
var node_id NodeId
// add the prefetch amount to available queue before it is granted.
if use_not_granted {
node_id = c.nextNodeId()
n := &Node{
available: req_amount,
id: node_id,
}
c.queue.Push(n)
}
if glog.V(2) {
glog.Infof("[%d]Prefetch(%d) request amount: %v", c.id, node_id, req_amount)
}
c.s.prefetch_num++
c.s.prefetch += uint64(req_amount)
c.last_prefetch_time = time.Now()
c.server.Alloc(req_amount, func(resp Response) {
c.onAllocResponse(node_id, req_amount, resp)
})
}
func (c *Cache) onAllocResponse(node_id NodeId, req_amount int, resp Response) {
c.mutex.Lock()
defer c.mutex.Unlock()
var n *Node
// The prefetched amount was added to the available queue
if node_id != 0 {
n = c.findNodeById(node_id)
if resp.amount < req_amount {
delta := req_amount - resp.amount
// Substract it from its own request node.
if n != nil {
d := min(n.available, delta)
n.available -= d
delta -= d
}
if delta > 0 {
// Substract it from other prefetched amounts
d := c.dec(delta)
if d > 0 {
// These are over-used amount
c.s.over_used += uint64(d)
glog.Infof("[%d]===Over-used %v", c.id, d)
}
}
}
} else {
if resp.amount > 0 {
node_id = c.nextNodeId()
n = &Node{
available: resp.amount,
id: node_id,
}
c.queue.Push(n)
}
}
if glog.V(2) {
glog.Infof("[%d]Prefetch(%d) granted: %v", c.id, node_id, resp.amount)
}
if resp.amount == req_amount {
if c.mode != OPEN {
c.mode = OPEN
if glog.V(2) {
glog.Infof("[%d]Prefetch(%d) change mode to OPEN", c.id, node_id)
}
}
} else {
if c.mode != CLOSE {
c.mode = CLOSE
if glog.V(2) {
glog.Infof("[%d]Prefetch(%d) change mode to CLOSE", c.id, node_id)
}
}
}
if n != nil && n.available > 0 {
if glog.V(4) {
glog.Infof("[%d]add timer to expire id=%d, in: %v second", c.id, node_id, resp.expire)
}
time.AfterFunc(resp.expire*time.Second, func() {
c.onExpire(node_id)
})
}
}
func (c *Cache) onExpire(node_id NodeId) {
c.mutex.Lock()
defer c.mutex.Unlock()
// Called when prefetched tokens are expired.
n := c.findNodeById(node_id)
if n == nil {
return
}
if n.available > 0 {
c.s.expired += uint64(n.available)
if glog.V(2) {
glog.Infof("[%d]===Expired %v from id: %d", c.id, n.available, node_id)
}
n.available = 0
}
}