-
Notifications
You must be signed in to change notification settings - Fork 12
/
basicbuffer.go
389 lines (320 loc) · 9.88 KB
/
basicbuffer.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package drr
import (
"container/ring"
"fmt"
"math"
"time"
"go.uber.org/atomic"
"github.com/iotaledger/hive.go/ds/shrinkingmap"
"github.com/iotaledger/hive.go/lo"
"github.com/iotaledger/iota-core/pkg/protocol/engine/blocks"
iotago "github.com/iotaledger/iota.go/v4"
)
// region BasicBuffer /////////////////////////////////////////////////////////////////////////////////////////////
// BasicBuffer represents a buffer of IssuerQueue.
type BasicBuffer struct {
activeIssuers *shrinkingmap.ShrinkingMap[iotago.AccountID, *ring.Ring]
ring *ring.Ring
readyBlocksCount atomic.Int64
totalBlocksCount atomic.Int64
tokenBucket float64
lastScheduleTime time.Time
blockChan chan *blocks.Block
}
// NewBasicBuffer returns a new BasicBuffer.
func NewBasicBuffer() *BasicBuffer {
return &BasicBuffer{
activeIssuers: shrinkingmap.New[iotago.AccountID, *ring.Ring](),
ring: nil,
lastScheduleTime: time.Now(),
blockChan: make(chan *blocks.Block, 1),
}
}
// NumActiveIssuers returns the number of active issuers in b.
func (b *BasicBuffer) NumActiveIssuers() int {
return b.activeIssuers.Size()
}
func (b *BasicBuffer) Clear() {
select {
case <-b.blockChan:
default:
}
b.activeIssuers.ForEachKey(func(issuedID iotago.AccountID) bool {
b.RemoveIssuerQueue(issuedID)
return true
})
}
// IssuerQueue returns the queue for the corresponding issuer.
func (b *BasicBuffer) IssuerQueue(issuerID iotago.AccountID) *IssuerQueue {
element, exists := b.activeIssuers.Get(issuerID)
if !exists {
return nil
}
issuerQueue, isIQ := element.Value.(*IssuerQueue)
if !isIQ {
panic("buffer contains elements that are not issuer queues")
}
return issuerQueue
}
// IssuerQueueWork returns the total WorkScore of block in the queue for the corresponding issuer.
func (b *BasicBuffer) IssuerQueueWork(issuerID iotago.AccountID) iotago.WorkScore {
issuerQueue := b.IssuerQueue(issuerID)
if issuerQueue == nil {
return 0
}
return issuerQueue.Work()
}
// IssuerQueueSize returns the number of blocks in the queue for the corresponding issuer.
func (b *BasicBuffer) IssuerQueueBlockCount(issuerID iotago.AccountID) int {
issuerQueue := b.IssuerQueue(issuerID)
if issuerQueue == nil {
return 0
}
return issuerQueue.Size()
}
func (b *BasicBuffer) CreateIssuerQueue(issuerID iotago.AccountID) *IssuerQueue {
element := b.activeIssuers.Compute(issuerID, func(_ *ring.Ring, exists bool) *ring.Ring {
if exists {
panic(fmt.Sprintf("issuer queue already exists: %s", issuerID.String()))
}
return b.ringInsert(NewIssuerQueue(issuerID, func(totalSizeDelta int64, readySizeDelta int64) {
if totalSizeDelta != 0 {
b.totalBlocksCount.Add(totalSizeDelta)
}
if readySizeDelta != 0 {
b.readyBlocksCount.Add(readySizeDelta)
}
}))
})
issuerQueue, isIQ := element.Value.(*IssuerQueue)
if !isIQ {
panic("buffer contains elements that are not issuer queues")
}
return issuerQueue
}
func (b *BasicBuffer) GetOrCreateIssuerQueue(issuerID iotago.AccountID) *IssuerQueue {
element, issuerActive := b.activeIssuers.Get(issuerID)
if !issuerActive {
// create new issuer queue
return b.CreateIssuerQueue(issuerID)
}
issuerQueue, isIQ := element.Value.(*IssuerQueue)
if !isIQ {
panic("buffer contains elements that are not issuer queues")
}
return issuerQueue
}
// RemoveIssuerQueue removes all blocks (submitted and ready) for the given issuer and deletes the issuer queue.
func (b *BasicBuffer) RemoveIssuerQueue(issuerID iotago.AccountID) {
element, exists := b.activeIssuers.Get(issuerID)
if !exists {
return
}
issuerQueue, isIQ := element.Value.(*IssuerQueue)
if !isIQ {
panic("buffer contains elements that are not issuer queues")
}
issuerQueue.Clear()
b.ringRemove(element)
b.activeIssuers.Delete(issuerID)
}
// RemoveIssuerQueueIfEmpty removes all blocks (submitted and ready) for the given issuer and deletes the issuer queue if it is empty.
func (b *BasicBuffer) RemoveIssuerQueueIfEmpty(issuerID iotago.AccountID) {
element, exists := b.activeIssuers.Get(issuerID)
if !exists {
return
}
issuerQueue, isIQ := element.Value.(*IssuerQueue)
if !isIQ {
panic("buffer contains elements that are not issuer queues")
}
if issuerQueue.Size() == 0 {
b.ringRemove(element)
b.activeIssuers.Delete(issuerID)
}
}
// Submit submits a block. Return blocks dropped from the scheduler to make room for the submitted block.
// The submitted block can also be returned as dropped if the issuer does not have enough mana.
func (b *BasicBuffer) Submit(blk *blocks.Block, issuerQueue *IssuerQueue, quantumFunc func(iotago.AccountID) Deficit, maxBuffer int) ([]*blocks.Block, bool) {
// first we submit the block, and if it turns out that the issuer doesn't have enough bandwidth to submit, it will be removed by dropTail
if !issuerQueue.Submit(blk) {
return nil, false
}
// if max buffer size exceeded, drop from tail of the longest mana-scaled queue
if b.TotalBlocksCount() > maxBuffer {
return b.dropTail(quantumFunc, maxBuffer), true
}
return nil, true
}
// Ready marks a previously submitted block as ready to be scheduled.
func (b *BasicBuffer) Ready(block *blocks.Block) bool {
issuerQueue := b.IssuerQueue(block.IssuerID())
if issuerQueue == nil {
return false
}
return issuerQueue.Ready(block)
}
// TotalBlocksCount returns the number of blocks in the buffer.
func (b *BasicBuffer) TotalBlocksCount() (blocksCount int) {
return int(b.totalBlocksCount.Load())
}
// ReadyBlocksCount returns the number of ready blocks in the buffer.
func (b *BasicBuffer) ReadyBlocksCount() (readyBlocksCount int) {
return int(b.readyBlocksCount.Load())
}
// Next returns the next IssuerQueue in round-robin order.
func (b *BasicBuffer) Next() *IssuerQueue {
if b.ring != nil {
b.ring = b.ring.Next()
if issuerQueue, isIQ := b.ring.Value.(*IssuerQueue); isIQ {
return issuerQueue
}
}
return nil
}
// ForEach applies a consumer function to each IssuerQueue in the BasicBuffer.
func (b *BasicBuffer) ForEach(consumer func(*IssuerQueue) bool) {
if b.ring == nil {
return
}
// Create a temporary slice to hold the IssuerQueues
var queues []*IssuerQueue
// Start at the current ring position
start := b.ring
for {
if issuerQueue, isIQ := b.ring.Value.(*IssuerQueue); isIQ {
queues = append(queues, issuerQueue)
}
// Move to the next position in the ring
b.ring = b.ring.Next()
// If we've looped back to the start, break out of the loop
if b.ring == start {
break
}
}
// Apply the consumer function to each IssuerQueue
for _, queue := range queues {
if !consumer(queue) {
return
}
}
}
// Current returns the current IssuerQueue in round-robin order.
func (b *BasicBuffer) Current() *IssuerQueue {
if b.ring == nil {
return nil
}
if issuerQueue, isIQ := b.ring.Value.(*IssuerQueue); isIQ {
return issuerQueue
}
return nil
}
// PopFront removes the first ready block from the queue of the current issuer.
func (b *BasicBuffer) PopFront() *blocks.Block {
q := b.Current()
if q == nil {
return nil
}
block := q.PopFront()
if block == nil {
return nil
}
return block
}
// IssuerIDs returns the issuerIDs of all issuers.
func (b *BasicBuffer) IssuerIDs() []iotago.AccountID {
var issuerIDs []iotago.AccountID
start := b.Current()
if start == nil {
return nil
}
for q := start; ; {
issuerIDs = append(issuerIDs, q.IssuerID())
q = b.Next()
if q == start {
break
}
}
return issuerIDs
}
func (b *BasicBuffer) dropTail(quantumFunc func(iotago.AccountID) Deficit, maxBuffer int) (droppedBlocks []*blocks.Block) {
// remove as many blocks as necessary to stay within max buffer size
for b.TotalBlocksCount() > maxBuffer {
// find the longest mana-scaled queue
maxIssuerID := b.mustLongestQueueIssuerID(quantumFunc)
longestQueue := b.IssuerQueue(maxIssuerID)
if longestQueue == nil {
panic("buffer is full, but longest queue does not exist")
}
tail := longestQueue.RemoveTail()
if tail == nil {
panic("buffer is full, but tail of longest queue does not exist")
}
droppedBlocks = append(droppedBlocks, tail)
}
return droppedBlocks
}
// mustLongestQueueIssuerID returns the issuerID of the longest queue in the buffer.
// This function panics if no longest queue is found.
func (b *BasicBuffer) mustLongestQueueIssuerID(quantumFunc func(iotago.AccountID) Deficit) iotago.AccountID {
start := b.Current()
ringStart := b.ring
maxScale := math.Inf(-1)
maxIssuerID := iotago.EmptyAccountID
for q := start; ; {
if issuerQuantum := quantumFunc(q.IssuerID()); issuerQuantum > 0 {
if scale := float64(q.Work()) / float64(issuerQuantum); scale > maxScale {
maxScale = scale
maxIssuerID = q.IssuerID()
}
} else if q.Size() > 0 {
// if the issuer has no quantum, then this is the max queue size
maxIssuerID = q.IssuerID()
b.ring = ringStart
break
}
q = b.Next()
if q == start {
break
}
}
if maxIssuerID == iotago.EmptyAccountID {
panic("no longest queue determined")
}
return maxIssuerID
}
func (b *BasicBuffer) ringRemove(r *ring.Ring) {
n := b.ring.Next()
if r == b.ring {
if n == b.ring {
b.ring = nil
return
}
b.ring = n
}
r.Prev().Link(n)
}
func (b *BasicBuffer) ringInsert(v interface{}) *ring.Ring {
p := ring.New(1)
p.Value = v
if b.ring == nil {
b.ring = p
return p
}
return p.Link(b.ring)
}
func (b *BasicBuffer) waitTime(rate float64, block *blocks.Block) time.Duration {
tokensRequired := float64(block.WorkScore()) - (b.tokenBucket + rate*time.Since(b.lastScheduleTime).Seconds())
return lo.Max(0, time.Duration(tokensRequired/rate))
}
func (b *BasicBuffer) updateTokenBucket(rate float64, tokenBucketSize float64) {
b.tokenBucket = lo.Min(
tokenBucketSize,
b.tokenBucket+rate*time.Since(b.lastScheduleTime).Seconds(),
)
b.lastScheduleTime = time.Now()
}
func (b *BasicBuffer) deductTokens(tokens float64) {
b.tokenBucket -= tokens
}
// endregion ///////////////////////////////////////////////////////////////////////////////////////////////////////////