-
Notifications
You must be signed in to change notification settings - Fork 178
/
backend.go
218 lines (185 loc) · 6.46 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
// (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/module/mempool/stdmap/backdata"
_ "github.com/onflow/flow-go/utils/binstat"
)
// Backend provides synchronized access to a backdata
type Backend struct {
sync.RWMutex
backData mempool.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 {
backData := backdata.NewMapBackData()
return NewBackendWithBackData(backData, options...)
}
func NewBackendWithBackData(backData mempool.BackData, options ...OptionFunc) *Backend {
b := Backend{
backData: backData,
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 mempool.BackData) 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)
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.Entities()
}
// 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 b.backData.Size() > 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)
}
}
}