forked from hyperledger/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
payloads_buffer.go
144 lines (114 loc) · 3.84 KB
/
payloads_buffer.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
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package state
import (
"fmt"
"strconv"
"sync"
"sync/atomic"
"github.com/hyperledger/fabric/gossip/util"
proto "github.com/hyperledger/fabric/protos/gossip"
"github.com/op/go-logging"
)
// PayloadsBuffer is used to store payloads into which used to
// support payloads with blocks reordering according to the
// sequence numbers. It also will provide the capability
// to signal whenever expected block has arrived.
type PayloadsBuffer interface {
// Adds new block into the buffer
Push(payload *proto.Payload) error
// Returns next expected sequence number
Next() uint64
// Remove and return payload with given sequence number
Pop() *proto.Payload
// Get current buffer size
Size() int
// Channel to indicate event when new payload pushed with sequence
// number equal to the next expected value.
Ready() chan struct{}
Close()
}
// PayloadsBufferImpl structure to implement PayloadsBuffer interface
// store inner state of available payloads and sequence numbers
type PayloadsBufferImpl struct {
next uint64
buf map[uint64]*proto.Payload
readyChan chan struct{}
mutex sync.RWMutex
logger *logging.Logger
}
// NewPayloadsBuffer is factory function to create new payloads buffer
func NewPayloadsBuffer(next uint64) PayloadsBuffer {
return &PayloadsBufferImpl{
buf: make(map[uint64]*proto.Payload),
readyChan: make(chan struct{}, 0),
next: next,
logger: util.GetLogger(util.LoggingStateModule, ""),
}
}
// Ready function returns the channel which indicates whenever expected
// next block has arrived and one could safely pop out
// next sequence of blocks
func (b *PayloadsBufferImpl) Ready() chan struct{} {
return b.readyChan
}
// Push new payload into the buffer structure in case new arrived payload
// sequence number is below the expected next block number payload will be
// thrown away and error will be returned.
func (b *PayloadsBufferImpl) Push(payload *proto.Payload) error {
b.mutex.Lock()
defer b.mutex.Unlock()
seqNum := payload.SeqNum
if seqNum < b.next || b.buf[seqNum] != nil {
return fmt.Errorf("Payload with sequence number = %s has been already processed",
strconv.FormatUint(payload.SeqNum, 10))
}
b.buf[seqNum] = payload
// Send notification that next sequence has arrived
if seqNum == b.next {
// Do not block execution of current routine
go func() {
b.readyChan <- struct{}{}
}()
}
return nil
}
// Next function provides the number of the next expected block
func (b *PayloadsBufferImpl) Next() uint64 {
// Atomically read the value of the top sequence number
return atomic.LoadUint64(&b.next)
}
// Pop function extracts the payload according to the next expected block
// number, if no next block arrived yet, function returns nil.
func (b *PayloadsBufferImpl) Pop() *proto.Payload {
b.mutex.Lock()
defer b.mutex.Unlock()
result := b.buf[b.Next()]
if result != nil {
// If there is such sequence in the buffer need to delete it
delete(b.buf, b.Next())
// Increment next expect block index
atomic.AddUint64(&b.next, 1)
}
return result
}
// Size returns current number of payloads stored within buffer
func (b *PayloadsBufferImpl) Size() int {
b.mutex.RLock()
defer b.mutex.RUnlock()
return len(b.buf)
}
// Close cleanups resources and channels in maintained
func (b *PayloadsBufferImpl) Close() {
close(b.readyChan)
}