-
Notifications
You must be signed in to change notification settings - Fork 115
/
nodequeue.go
183 lines (149 loc) · 4.76 KB
/
nodequeue.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
package schedulerutils
import (
"container/heap"
"fmt"
"time"
"github.com/iotaledger/hive.go/crypto/ed25519"
"github.com/iotaledger/hive.go/identity"
"go.uber.org/atomic"
)
// ElementIDLength defines the length of an ElementID.
const ElementIDLength = 32
// ElementID defines the ID of element.
type ElementID [ElementIDLength]byte
// ElementIDFromBytes converts byte array to an ElementID.
func ElementIDFromBytes(bytes []byte) (result ElementID) {
// check arguments
if len(bytes) < ElementIDLength {
panic("bytes not long enough to encode a valid message id")
}
copy(result[:], bytes)
return
}
// Element represents the generic interface for an message in NodeQueue.
type Element interface {
// IDBytes returns the ID of an Element as a byte slice.
IDBytes() []byte
// Size returns the size of the element.
Size() int
// IssuerPublicKey returns the issuer public key of the element.
IssuerPublicKey() ed25519.PublicKey
// IssuingTime returns the issuing time of the message.
IssuingTime() time.Time
}
// region NodeQueue /////////////////////////////////////////////////////////////////////////////////////////////
// NodeQueue keeps the submitted messages of a node
type NodeQueue struct {
nodeID identity.ID
submitted map[ElementID]*Element
inbox *ElementHeap
size atomic.Int64
}
// NewNodeQueue returns a new NodeQueue
func NewNodeQueue(nodeID identity.ID) *NodeQueue {
return &NodeQueue{
nodeID: nodeID,
submitted: make(map[ElementID]*Element),
inbox: new(ElementHeap),
}
}
// Size returns the total size of the messages in the queue.
// This function is thread-safe.
func (q *NodeQueue) Size() int {
if q == nil {
return 0
}
return int(q.size.Load())
}
// NodeID returns the ID of the node belonging to the queue.
func (q *NodeQueue) NodeID() identity.ID {
return q.nodeID
}
// Submit submits a message for the queue.
func (q *NodeQueue) Submit(element Element) bool {
// this is just a debugging check, it will never happen in practice
if msgNodeID := identity.NewID(element.IssuerPublicKey()); q.nodeID != msgNodeID {
panic(fmt.Sprintf("nodequeue: queue node ID(%x) and issuer ID(%x) does not match.", q.nodeID, msgNodeID))
}
id := ElementIDFromBytes(element.IDBytes())
if _, submitted := q.submitted[id]; submitted {
return false
}
q.submitted[id] = &element
q.size.Add(int64(element.Size()))
return true
}
// Unsubmit removes a previously submitted message from the queue.
func (q *NodeQueue) Unsubmit(element Element) bool {
id := ElementIDFromBytes(element.IDBytes())
if _, submitted := q.submitted[id]; !submitted {
return false
}
delete(q.submitted, id)
q.size.Sub(int64(element.Size()))
return true
}
// Ready marks a previously submitted message as ready to be scheduled.
func (q *NodeQueue) Ready(element Element) bool {
id := ElementIDFromBytes(element.IDBytes())
if _, submitted := q.submitted[id]; !submitted {
return false
}
delete(q.submitted, id)
heap.Push(q.inbox, element)
return true
}
// IDs returns the IDs of all submitted messages (ready or not).
func (q *NodeQueue) IDs() (ids []ElementID) {
for id := range q.submitted {
ids = append(ids, id)
}
for _, element := range *q.inbox {
ids = append(ids, ElementIDFromBytes(element.IDBytes()))
}
return ids
}
// Front returns the first ready message in the queue.
func (q *NodeQueue) Front() Element {
if q == nil || q.inbox.Len() == 0 {
return nil
}
return (*q.inbox)[0]
}
// PopFront removes the first ready message from the queue.
func (q *NodeQueue) PopFront() Element {
msg := heap.Pop(q.inbox).(Element)
q.size.Sub(int64(msg.Size()))
return msg
}
// endregion ///////////////////////////////////////////////////////////////////////////////////////////////////////////
// region ElementHeap /////////////////////////////////////////////////////////////////////////////////////////////
// ElementHeap holds a heap of messages with respect to their IssuingTime.
type ElementHeap []Element
// Len is the number of elements in the collection.
func (h ElementHeap) Len() int {
return len(h)
}
// Less reports whether the element with index i must sort before the element with index j.
func (h ElementHeap) Less(i, j int) bool {
return h[i].IssuingTime().Before(h[j].IssuingTime())
}
// Swap swaps the elements with indexes i and j.
func (h ElementHeap) Swap(i, j int) {
h[i], h[j] = h[j], h[i]
}
// Push adds x as element with index Len().
// It panics if x is not Element.
func (h *ElementHeap) Push(x interface{}) {
*h = append(*h, x.(Element))
}
// Pop removes and returns element with index Len() - 1.
func (h *ElementHeap) Pop() interface{} {
tmp := *h
n := len(tmp)
x := tmp[n-1]
tmp[n-1] = nil
*h = tmp[:n-1]
return x
}
// endregion ///////////////////////////////////////////////////////////////////////////////////////////////////////////