Skip to content

Commit 5981d37

Browse files
committed
Cleanups and refactoring of payloads buffer
Removed redundant methonds to keep API cleaner. Change-Id: I9610062f82bb5a2a95ac9e2656fe6b2284017307 Signed-off-by: Artem Barger <[email protected]>
1 parent 2bed988 commit 5981d37

File tree

2 files changed

+27
-132
lines changed

2 files changed

+27
-132
lines changed

gossip/state/payloads_buffer.go

+16-35
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ package state
1818

1919
import (
2020
"fmt"
21-
"github.com/hyperledger/fabric/gossip/proto"
2221
"strconv"
2322
"sync"
2423
"sync/atomic"
24+
25+
"github.com/hyperledger/fabric/gossip/proto"
26+
"github.com/op/go-logging"
2527
)
2628

2729
// PayloadsBuffer is used to store payloads into which used to
@@ -41,35 +43,35 @@ type PayloadsBuffer interface {
4143
// Get current buffer size
4244
Size() int
4345

44-
// Minimum available seq number
45-
MinAvail() (uint64, error)
46-
4746
// Channel to indicate event when new payload pushed with sequence
4847
// number equal to the next expected value.
4948
Ready() chan struct{}
49+
50+
Close()
5051
}
5152

5253
// PayloadsBufferImpl structure to implement PayloadsBuffer interface
5354
// store inner state of available payloads and sequence numbers
5455
type PayloadsBufferImpl struct {
55-
buf map[uint64]*proto.Payload
56-
57-
minQueue []uint64
56+
buf map[uint64]*proto.Payload
5857

59-
next uint64
58+
next uint64
6059

6160
readyChan chan struct{}
6261

63-
mutex sync.RWMutex
62+
mutex sync.RWMutex
63+
64+
logger *logging.Logger
6465
}
6566

6667
// NewPayloadsBuffer is factory function to create new payloads buffer
6768
func NewPayloadsBuffer(next uint64) PayloadsBuffer {
69+
logger, _ := logging.GetLogger("GossipStateProvider")
6870
return &PayloadsBufferImpl{
6971
buf: make(map[uint64]*proto.Payload),
70-
minQueue: make([]uint64, 0),
71-
readyChan: make(chan struct{}),
72+
readyChan: make(chan struct{}, 0),
7273
next: next,
74+
logger: logger,
7375
}
7476
}
7577

@@ -96,18 +98,6 @@ func (b *PayloadsBufferImpl) Push(payload *proto.Payload) error {
9698

9799
b.buf[seqNum] = payload
98100

99-
lenMinQueue := len(b.minQueue)
100-
if lenMinQueue == 0 {
101-
// New element to insert
102-
b.minQueue = append(b.minQueue, seqNum)
103-
} else {
104-
if b.minQueue[lenMinQueue - 1] > seqNum {
105-
// in case new sequence number is lower than
106-
// available one add it to the queue
107-
b.minQueue = append(b.minQueue, seqNum)
108-
}
109-
}
110-
111101
// Send notification that next sequence has arrived
112102
if seqNum == b.next {
113103
// Do not block execution of current routine
@@ -135,7 +125,6 @@ func (b *PayloadsBufferImpl) Pop() *proto.Payload {
135125
if result != nil {
136126
// If there is such sequence in the buffer need to delete it
137127
delete(b.buf, b.Next())
138-
b.minQueue = b.minQueue[:len(b.minQueue) - 1]
139128
// Increment next expect block index
140129
atomic.AddUint64(&b.next, 1)
141130
}
@@ -149,15 +138,7 @@ func (b *PayloadsBufferImpl) Size() int {
149138
return len(b.buf)
150139
}
151140

152-
// MinAvail returns minimum available payload sequence number, if no payloads
153-
// within buffer results with error "Empty buffer".
154-
func (b *PayloadsBufferImpl) MinAvail() (uint64, error) {
155-
b.mutex.Lock()
156-
defer b.mutex.Unlock()
157-
158-
if len(b.buf) == 0 {
159-
return ^uint64(0), fmt.Errorf("Empty buffer")
160-
}
161-
162-
return b.minQueue[len(b.minQueue) - 1], nil
141+
// Close cleanups resources and channels in maintained
142+
func (b *PayloadsBufferImpl) Close() {
143+
close(b.readyChan)
163144
}

gossip/state/payloads_buffer_test.go

+11-97
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@ package state
1919
import (
2020
"crypto/rand"
2121
"fmt"
22-
"github.com/hyperledger/fabric/gossip/proto"
23-
"github.com/stretchr/testify/assert"
24-
"testing"
25-
"time"
2622
"sync"
2723
"sync/atomic"
24+
"testing"
25+
"time"
26+
27+
"github.com/hyperledger/fabric/gossip/proto"
28+
"github.com/stretchr/testify/assert"
2829
)
2930

3031
func uuid() (string, error) {
@@ -33,9 +34,9 @@ func uuid() (string, error) {
3334
if err != nil {
3435
return "", err
3536
}
36-
uuid[8] = uuid[8] &^ 0xc0 | 0x80
37+
uuid[8] = uuid[8]&^0xc0 | 0x80
3738

38-
uuid[6] = uuid[6] &^ 0xf0 | 0x40
39+
uuid[6] = uuid[6]&^0xf0 | 0x40
3940
return fmt.Sprintf("%x-%x-%x-%x-%x", uuid[0:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:]), nil
4041
}
4142

@@ -102,7 +103,7 @@ func TestPayloadsBufferImpl_Ready(t *testing.T) {
102103
fin <- struct{}{}
103104
}()
104105

105-
time.AfterFunc(100 * time.Millisecond, func() {
106+
time.AfterFunc(100*time.Millisecond, func() {
106107
payload, err := randomPayloadWithSeqNum(1)
107108

108109
if err != nil {
@@ -120,85 +121,6 @@ func TestPayloadsBufferImpl_Ready(t *testing.T) {
120121
}
121122
}
122123

123-
func TestPayloadsBufferImpl_MinAvail(t *testing.T) {
124-
buffer := NewPayloadsBuffer(1)
125-
126-
assert.Equal(t, buffer.Next(), uint64(1))
127-
128-
// Buffer is empty no messages expected,
129-
// hence no min shoyld be value available
130-
_, err := buffer.MinAvail()
131-
assert.Error(t, err)
132-
133-
pushNewRandomPayload(t, buffer, 10)
134-
135-
min, err := buffer.MinAvail()
136-
assert.NoError(t, err)
137-
assert.Equal(t, min, uint64(10))
138-
139-
pushNewRandomPayload(t, buffer, 17)
140-
141-
// Presence of payload w/ sequence number 17 should not affect the minimum available block
142-
min, err = buffer.MinAvail()
143-
assert.NoError(t, err)
144-
assert.Equal(t, min, uint64(10))
145-
146-
// Add new block w/ lower sequence number
147-
pushNewRandomPayload(t, buffer, 6)
148-
149-
min, err = buffer.MinAvail()
150-
assert.NoError(t, err)
151-
// New sequence number now should be the minimum
152-
assert.Equal(t, min, uint64(6))
153-
}
154-
155-
func TestPayloadsBufferImpl_MinAvail2(t *testing.T) {
156-
buffer := NewPayloadsBuffer(1)
157-
158-
assert.Equal(t, buffer.Next(), uint64(1))
159-
160-
_, err := buffer.MinAvail()
161-
assert.Error(t, err)
162-
163-
pushNewRandomPayload(t, buffer, 3)
164-
min, err := buffer.MinAvail()
165-
assert.NoError(t, err)
166-
assert.Equal(t, min, uint64(3))
167-
168-
pushNewRandomPayload(t, buffer, 1)
169-
min, err = buffer.MinAvail()
170-
assert.NoError(t, err)
171-
assert.Equal(t, min, uint64(1))
172-
173-
done := sync.WaitGroup{}
174-
done.Add(1)
175-
176-
go func() {
177-
select {
178-
case <-buffer.Ready():
179-
{
180-
// Once payload is ready extract it
181-
assert.Equal(t, buffer.Next(), uint64(1))
182-
payload := buffer.Pop()
183-
assert.Equal(t, payload.SeqNum, uint64(1))
184-
185-
// Next min sequence number has to be 3
186-
min, err = buffer.MinAvail()
187-
assert.NoError(t, err)
188-
assert.Equal(t, min, uint64(3))
189-
}
190-
case <-time.After(500 * time.Millisecond):
191-
{
192-
t.Fatalf("Expected to receive notification with next payload")
193-
}
194-
}
195-
done.Done()
196-
}()
197-
198-
// Wait to make sure that payload was extracted
199-
done.Wait()
200-
}
201-
202124
// Test to push several concurrent blocks into the buffer
203125
// with same sequence number, only one expected to succeed
204126
func TestPayloadsBufferImpl_ConcurrentPush(t *testing.T) {
@@ -220,7 +142,7 @@ func TestPayloadsBufferImpl_ConcurrentPush(t *testing.T) {
220142
payload, err := randomPayloadWithSeqNum(nextSeqNum)
221143
assert.NoError(t, err)
222144

223-
errors := make([]error, 0)
145+
var errors []error
224146

225147
ready := int32(0)
226148
go func() {
@@ -235,7 +157,7 @@ func TestPayloadsBufferImpl_ConcurrentPush(t *testing.T) {
235157
startWG.Wait()
236158
errors = append(errors, buffer.Push(payload))
237159
finishWG.Done()
238-
}();
160+
}()
239161
}
240162
startWG.Done()
241163
finishWG.Wait()
@@ -245,7 +167,7 @@ func TestPayloadsBufferImpl_ConcurrentPush(t *testing.T) {
245167
// Only one push attempt expected to succeed
246168
for _, err := range errors {
247169
if err == nil {
248-
success ++
170+
success++
249171
}
250172
}
251173

@@ -254,11 +176,3 @@ func TestPayloadsBufferImpl_ConcurrentPush(t *testing.T) {
254176
// Buffer size has to be only one
255177
assert.Equal(t, 1, buffer.Size())
256178
}
257-
258-
func pushNewRandomPayload(t *testing.T, b PayloadsBuffer, seqNum uint64) {
259-
// Add new block w/ lower sequence number
260-
payload, err := randomPayloadWithSeqNum(seqNum);
261-
assert.NoError(t, err)
262-
err = b.Push(payload)
263-
assert.NoError(t, err)
264-
}

0 commit comments

Comments
 (0)