|
| 1 | +/* |
| 2 | +Copyright IBM Corp. 2016 All Rights Reserved. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package state |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "github.com/hyperledger/fabric/gossip/proto" |
| 22 | + "strconv" |
| 23 | + "sync" |
| 24 | + "sync/atomic" |
| 25 | +) |
| 26 | + |
| 27 | +// PayloadsBuffer is used to store payloads into which used to |
| 28 | +// support payloads with blocks reordering according to the |
| 29 | +// sequence numbers. It also will provide the capability |
| 30 | +// to signal whenever expected block has arrived. |
| 31 | +type PayloadsBuffer interface { |
| 32 | + // Adds new block into the buffer |
| 33 | + Push(payload *proto.Payload) error |
| 34 | + |
| 35 | + // Returns next expected sequence number |
| 36 | + Next() uint64 |
| 37 | + |
| 38 | + // Remove and return payload with given sequence number |
| 39 | + Pop() *proto.Payload |
| 40 | + |
| 41 | + // Get current buffer size |
| 42 | + Size() int |
| 43 | + |
| 44 | + // Minimum available seq number |
| 45 | + MinAvail() (uint64, error) |
| 46 | + |
| 47 | + // Channel to indicate event when new payload pushed with sequence |
| 48 | + // number equal to the next expected value. |
| 49 | + Ready() chan struct{} |
| 50 | +} |
| 51 | + |
| 52 | +// PayloadsBufferImpl structure to implement PayloadsBuffer interface |
| 53 | +// store inner state of available payloads and sequence numbers |
| 54 | +type PayloadsBufferImpl struct { |
| 55 | + buf map[uint64]*proto.Payload |
| 56 | + |
| 57 | + minQueue []uint64 |
| 58 | + |
| 59 | + next uint64 |
| 60 | + |
| 61 | + readyChan chan struct{} |
| 62 | + |
| 63 | + mutex sync.RWMutex |
| 64 | +} |
| 65 | + |
| 66 | +// NewPayloadsBuffer is factory function to create new payloads buffer |
| 67 | +func NewPayloadsBuffer(next uint64) PayloadsBuffer { |
| 68 | + return &PayloadsBufferImpl{ |
| 69 | + buf: make(map[uint64]*proto.Payload), |
| 70 | + minQueue: make([]uint64, 0), |
| 71 | + readyChan: make(chan struct{}), |
| 72 | + next: next, |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +// Ready function returns the channel which indicates whenever expected |
| 77 | +// next block has arrived and one could safely pop out |
| 78 | +// next sequence of blocks |
| 79 | +func (b *PayloadsBufferImpl) Ready() chan struct{} { |
| 80 | + return b.readyChan |
| 81 | +} |
| 82 | + |
| 83 | +// Push new payload into the buffer structure in case new arrived payload |
| 84 | +// sequence number is below the expected next block number payload will be |
| 85 | +// thrown away and error will be returned. |
| 86 | +func (b *PayloadsBufferImpl) Push(payload *proto.Payload) error { |
| 87 | + b.mutex.Lock() |
| 88 | + defer b.mutex.Unlock() |
| 89 | + |
| 90 | + seqNum := payload.SeqNum |
| 91 | + |
| 92 | + if seqNum < b.next || b.buf[seqNum] != nil { |
| 93 | + return fmt.Errorf("Payload with sequence number = %s has been already processed", |
| 94 | + strconv.FormatUint(payload.SeqNum, 10)) |
| 95 | + } |
| 96 | + |
| 97 | + b.buf[seqNum] = payload |
| 98 | + |
| 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 | + |
| 111 | + // Send notification that next sequence has arrived |
| 112 | + if seqNum == b.next { |
| 113 | + // Do not block execution of current routine |
| 114 | + go func() { |
| 115 | + b.readyChan <- struct{}{} |
| 116 | + }() |
| 117 | + } |
| 118 | + return nil |
| 119 | +} |
| 120 | + |
| 121 | +// Next function provides the number of the next expected block |
| 122 | +func (b *PayloadsBufferImpl) Next() uint64 { |
| 123 | + // Atomically read the value of the top sequence number |
| 124 | + return atomic.LoadUint64(&b.next) |
| 125 | +} |
| 126 | + |
| 127 | +// Pop function extracts the payload according to the next expected block |
| 128 | +// number, if no next block arrived yet, function returns nil. |
| 129 | +func (b *PayloadsBufferImpl) Pop() *proto.Payload { |
| 130 | + b.mutex.Lock() |
| 131 | + defer b.mutex.Unlock() |
| 132 | + |
| 133 | + result := b.buf[b.Next()] |
| 134 | + |
| 135 | + if result != nil { |
| 136 | + // If there is such sequence in the buffer need to delete it |
| 137 | + delete(b.buf, b.Next()) |
| 138 | + b.minQueue = b.minQueue[:len(b.minQueue) - 1] |
| 139 | + // Increment next expect block index |
| 140 | + atomic.AddUint64(&b.next, 1) |
| 141 | + } |
| 142 | + return result |
| 143 | +} |
| 144 | + |
| 145 | +// Size returns current number of payloads stored within buffer |
| 146 | +func (b *PayloadsBufferImpl) Size() int { |
| 147 | + b.mutex.Lock() |
| 148 | + defer b.mutex.Unlock() |
| 149 | + return len(b.buf) |
| 150 | +} |
| 151 | + |
| 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 |
| 163 | +} |
0 commit comments