-
Notifications
You must be signed in to change notification settings - Fork 178
/
backend.go
297 lines (254 loc) · 8.42 KB
/
backend.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
294
295
296
297
// (c) 2019 Dapper Labs - ALL RIGHTS RESERVED
package stdmap
import (
"math"
"sync"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/module/mempool"
_ "github.com/onflow/flow-go/utils/binstat"
)
// Backdata implements a generic memory pool backed by a Go map.
type Backdata struct {
entities map[flow.Identifier]flow.Entity
}
func NewBackdata() Backdata {
bd := Backdata{
entities: make(map[flow.Identifier]flow.Entity),
}
return bd
}
// Has checks if we already contain the item with the given hash.
func (b *Backdata) Has(entityID flow.Identifier) bool {
_, exists := b.entities[entityID]
return exists
}
// Add adds the given item to the pool.
func (b *Backdata) Add(entityID flow.Identifier, entity flow.Entity) bool {
_, exists := b.entities[entityID]
if exists {
return false
}
b.entities[entityID] = entity
return true
}
// Rem will remove the item with the given hash.
func (b *Backdata) Rem(entityID flow.Identifier) (flow.Entity, bool) {
entity, exists := b.entities[entityID]
if !exists {
return nil, false
}
delete(b.entities, entityID)
return entity, true
}
// Adjust will adjust the value item using the given function if the given key can be found.
// Returns a bool which indicates whether the value was updated as well as the updated value
func (b *Backdata) Adjust(entityID flow.Identifier, f func(flow.Entity) flow.Entity) (flow.Entity, bool) {
entity, ok := b.entities[entityID]
if !ok {
return nil, false
}
newentity := f(entity)
newentityID := newentity.ID()
delete(b.entities, entityID)
b.entities[newentityID] = newentity
return newentity, true
}
// ByID returns the given item from the pool.
func (b *Backdata) ByID(entityID flow.Identifier) (flow.Entity, bool) {
entity, exists := b.entities[entityID]
if !exists {
return nil, false
}
return entity, true
}
// Size will return the size of the backend.
func (b *Backdata) Size() uint {
return uint(len(b.entities))
}
// All returns all entities from the pool.
func (b *Backdata) All() []flow.Entity {
entities := make([]flow.Entity, 0, len(b.entities))
for _, item := range b.entities {
entities = append(entities, item)
}
return entities
}
// Clear removes all entities from the pool.
func (b *Backdata) Clear() {
b.entities = make(map[flow.Identifier]flow.Entity)
}
// Hash will use a merkle root hash to hash all items.
func (b *Backdata) Hash() flow.Identifier {
return flow.MerkleRoot(flow.GetIDs(b.All())...)
}
// Backend provides synchronized access to a backdata
type Backend struct {
sync.RWMutex
Backdata
guaranteedCapacity uint
batchEject BatchEjectFunc
eject EjectFunc
ejectionCallbacks []mempool.OnEjection
}
// NewBackend creates a new memory pool backend.
// This is using EjectTrueRandomFast()
func NewBackend(options ...OptionFunc) *Backend {
b := Backend{
Backdata: NewBackdata(),
guaranteedCapacity: uint(math.MaxUint32),
batchEject: EjectTrueRandomFast,
eject: nil,
ejectionCallbacks: nil,
}
for _, option := range options {
option(&b)
}
return &b
}
// Has checks if we already contain the item with the given hash.
func (b *Backend) Has(entityID flow.Identifier) bool {
//bs1 := binstat.EnterTime(binstat.BinStdmap + ".r_lock.(Backend)Has")
b.RLock()
//binstat.Leave(bs1)
//bs2 := binstat.EnterTime(binstat.BinStdmap + ".inlock.(Backend)Has")
//defer binstat.Leave(bs2)
defer b.RUnlock()
has := b.Backdata.Has(entityID)
return has
}
// Add adds the given item to the pool.
func (b *Backend) Add(entity flow.Entity) bool {
//bs0 := binstat.EnterTime(binstat.BinStdmap + ".<<lock.(Backend)Add")
entityID := entity.ID() // this expensive operation done OUTSIDE of lock :-)
//binstat.Leave(bs0)
//bs1 := binstat.EnterTime(binstat.BinStdmap + ".w_lock.(Backend)Add")
b.Lock()
//binstat.Leave(bs1)
//bs2 := binstat.EnterTime(binstat.BinStdmap + ".inlock.(Backend)Add")
//defer binstat.Leave(bs2)
defer b.Unlock()
added := b.Backdata.Add(entityID, entity)
b.reduce()
return added
}
// Rem will remove the item with the given hash.
func (b *Backend) Rem(entityID flow.Identifier) bool {
//bs1 := binstat.EnterTime(binstat.BinStdmap + ".w_lock.(Backend)Rem")
b.Lock()
//binstat.Leave(bs1)
//bs2 := binstat.EnterTime(binstat.BinStdmap + ".inlock.(Backend)Rem")
//defer binstat.Leave(bs2)
defer b.Unlock()
_, removed := b.Backdata.Rem(entityID)
return removed
}
// Adjust will adjust the value item using the given function if the given key can be found.
// Returns a bool which indicates whether the value was updated.
func (b *Backend) Adjust(entityID flow.Identifier, f func(flow.Entity) flow.Entity) (flow.Entity, bool) {
//bs1 := binstat.EnterTime(binstat.BinStdmap + ".w_lock.(Backend)Adjust")
b.Lock()
//binstat.Leave(bs1)
//bs2 := binstat.EnterTime(binstat.BinStdmap + ".inlock.(Backend)Adjust")
//defer binstat.Leave(bs2)
defer b.Unlock()
entity, wasUpdated := b.Backdata.Adjust(entityID, f)
return entity, wasUpdated
}
// ByID returns the given item from the pool.
func (b *Backend) ByID(entityID flow.Identifier) (flow.Entity, bool) {
//bs1 := binstat.EnterTime(binstat.BinStdmap + ".r_lock.(Backend)ByID")
b.RLock()
//binstat.Leave(bs1)
//bs2 := binstat.EnterTime(binstat.BinStdmap + ".inlock.(Backend)ByID")
//defer binstat.Leave(bs2)
defer b.RUnlock()
entity, exists := b.Backdata.ByID(entityID)
return entity, exists
}
// Run executes a function giving it exclusive access to the backdata
func (b *Backend) Run(f func(backdata map[flow.Identifier]flow.Entity) error) error {
//bs1 := binstat.EnterTime(binstat.BinStdmap + ".w_lock.(Backend)Run")
b.Lock()
//binstat.Leave(bs1)
//bs2 := binstat.EnterTime(binstat.BinStdmap + ".inlock.(Backend)Run")
//defer binstat.Leave(bs2)
defer b.Unlock()
err := f(b.Backdata.entities)
b.reduce()
return err
}
// Size will return the size of the backend.
func (b *Backend) Size() uint {
//bs1 := binstat.EnterTime(binstat.BinStdmap + ".r_lock.(Backend)Size")
b.RLock()
//binstat.Leave(bs1)
//bs2 := binstat.EnterTime(binstat.BinStdmap + ".inlock.(Backend)Size")
//defer binstat.Leave(bs2)
defer b.RUnlock()
size := b.Backdata.Size()
return size
}
// Limit returns the maximum number of items allowed in the backend.
func (b *Backend) Limit() uint {
return b.guaranteedCapacity
}
// All returns all entities from the pool.
func (b *Backend) All() []flow.Entity {
//bs1 := binstat.EnterTime(binstat.BinStdmap + ".r_lock.(Backend)All")
b.RLock()
//binstat.Leave(bs1)
//bs2 := binstat.EnterTime(binstat.BinStdmap + ".inlock.(Backend)All")
//defer binstat.Leave(bs2)
defer b.RUnlock()
return b.Backdata.All()
}
// Clear removes all entities from the pool.
func (b *Backend) Clear() {
//bs1 := binstat.EnterTime(binstat.BinStdmap + ".w_lock.(Backend)Clear")
b.Lock()
//binstat.Leave(bs1)
//bs2 := binstat.EnterTime(binstat.BinStdmap + ".inlock.(Backend)Clear")
//defer binstat.Leave(bs2)
defer b.Unlock()
b.Backdata.Clear()
}
// Hash will use a merkle root hash to hash all items.
func (b *Backend) Hash() flow.Identifier {
//bs1 := binstat.EnterTime(binstat.BinStdmap + ".r_lock.(Backend)Hash")
b.RLock()
//binstat.Leave(bs1)
//bs2 := binstat.EnterTime(binstat.BinStdmap + ".inlock.(Backend)Hash")
//defer binstat.Leave(bs2)
defer b.RUnlock()
identifier := b.Backdata.Hash()
return identifier
}
// RegisterEjectionCallbacks adds the provided OnEjection callbacks
func (b *Backend) RegisterEjectionCallbacks(callbacks ...mempool.OnEjection) {
//bs1 := binstat.EnterTime(binstat.BinStdmap + ".r_lock.(Backend)RegisterEjectionCallbacks")
b.Lock()
//binstat.Leave(bs1)
//bs2 := binstat.EnterTime(binstat.BinStdmap + ".inlock.(Backend)RegisterEjectionCallbacks")
//defer binstat.Leave(bs2)
defer b.Unlock()
b.ejectionCallbacks = append(b.ejectionCallbacks, callbacks...)
}
// reduce will reduce the size of the kept entities until we are within the
// configured memory pool size limit.
func (b *Backend) reduce() {
//bs := binstat.EnterTime(binstat.BinStdmap + ".??lock.(Backend)reduce")
//defer binstat.Leave(bs)
// we keep reducing the cache size until we are at limit again
// this was a loop, but the loop is now in EjectTrueRandomFast()
// the ejections are batched, so this call to eject() may not actually
// do anything until the batch threshold is reached (currently 128)
if len(b.entities) > int(b.guaranteedCapacity) {
// get the key from the eject function
// we don't do anything if there is an error
if b.batchEject != nil {
_ = b.batchEject(b)
} else {
_, _, _ = b.eject(b)
}
}
}