github.com/myafeier/fabric@v1.0.1-0.20170722181825-3a4b1f2bce86/orderer/common/blockcutter/blockcutter.go (about) 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 blockcutter 18 19 import ( 20 "github.com/hyperledger/fabric/common/config" 21 "github.com/hyperledger/fabric/orderer/common/filter" 22 cb "github.com/hyperledger/fabric/protos/common" 23 24 "github.com/op/go-logging" 25 ) 26 27 var logger = logging.MustGetLogger("orderer/common/blockcutter") 28 29 // Receiver defines a sink for the ordered broadcast messages 30 type Receiver interface { 31 // Ordered should be invoked sequentially as messages are ordered 32 // If the current message valid, and no batches need to be cut: 33 // - Ordered will return (nil, true) (indicating ok). 34 // If the current message is valid, and batches need to be cut: 35 // - Ordered will return 1 or 2 batches of messages, and true (indicating ok). 36 // If the current message is invalid: 37 // - Ordered will return (nil, false) (to indicate not ok). 38 // 39 // Given a valid message, if the current message needs to be isolated because it exceeds the preferred batch size 40 // - Ordered will return: 41 // * The pending batch (if not empty), and a second batch containing only the isolated message. 42 // * true (indicating ok). 43 // Otherwise, given a valid message, the pending batch, if not empty, will be cut and returned if: 44 // - The current message will cause the pending batch size in bytes to exceed BatchSize.PreferredMaxBytes. 45 // - After adding the current message to the pending batch, the message count has reached BatchSize.MaxMessageCount. 46 Ordered(msg *cb.Envelope) ([][]*cb.Envelope, bool) 47 48 // Cut returns the current batch and starts a new one 49 Cut() []*cb.Envelope 50 } 51 52 type receiver struct { 53 sharedConfigManager config.Orderer 54 filters *filter.RuleSet 55 pendingBatch []*cb.Envelope 56 pendingBatchSizeBytes uint32 57 pendingCommitters []filter.Committer 58 } 59 60 // NewReceiverImpl creates a Receiver implementation based on the given configtxorderer manager and filters 61 func NewReceiverImpl(sharedConfigManager config.Orderer, filters *filter.RuleSet) Receiver { 62 return &receiver{ 63 sharedConfigManager: sharedConfigManager, 64 filters: filters, 65 } 66 } 67 68 // Ordered should be invoked sequentially as messages are ordered 69 // If the current message valid, and no batches need to be cut: 70 // - Ordered will return nil, nil, and true (indicating ok). 71 // If the current message valid, and batches need to be cut: 72 // - Ordered will return 1 or 2 batches of messages, 1 or 2 batches of committers, and true (indicating ok). 73 // If the current message is invalid: 74 // - Ordered will return nil, nil, and false (to indicate not ok). 75 // 76 // Given a valid message, if the current message needs to be isolated (as determined during filtering). 77 // - Ordered will return: 78 // * The pending batch of (if not empty), and a second batch containing only the isolated message. 79 // * The corresponding batches of committers. 80 // * true (indicating ok). 81 // Otherwise, given a valid message, the pending batch, if not empty, will be cut and returned if: 82 // - The current message needs to be isolated (as determined during filtering). 83 // - The current message will cause the pending batch size in bytes to exceed BatchSize.PreferredMaxBytes. 84 // - After adding the current message to the pending batch, the message count has reached BatchSize.MaxMessageCount. 85 func (r *receiver) Ordered(msg *cb.Envelope) ([][]*cb.Envelope, bool) { 86 // The messages must be filtered a second time in case configuration has changed since the message was received 87 committer, err := r.filters.Apply(msg) 88 if err != nil { 89 logger.Debugf("Rejecting message: %s", err) 90 return nil, false 91 } 92 93 if committer.Isolated() { 94 logger.Panicf("The use of isolated committers has been deprecated and should no longer appear in this path") 95 } 96 97 messageSizeBytes := messageSizeBytes(msg) 98 99 if messageSizeBytes > r.sharedConfigManager.BatchSize().PreferredMaxBytes { 100 logger.Debugf("The current message, with %v bytes, is larger than the preferred batch size of %v bytes and will be isolated.", messageSizeBytes, r.sharedConfigManager.BatchSize().PreferredMaxBytes) 101 102 var messageBatches [][]*cb.Envelope 103 104 // cut pending batch, if it has any messages 105 if len(r.pendingBatch) > 0 { 106 messageBatch := r.Cut() 107 messageBatches = append(messageBatches, messageBatch) 108 } 109 110 // create new batch with single message 111 messageBatches = append(messageBatches, []*cb.Envelope{msg}) 112 113 return messageBatches, true 114 } 115 116 var messageBatches [][]*cb.Envelope 117 118 messageWillOverflowBatchSizeBytes := r.pendingBatchSizeBytes+messageSizeBytes > r.sharedConfigManager.BatchSize().PreferredMaxBytes 119 120 if messageWillOverflowBatchSizeBytes { 121 logger.Debugf("The current message, with %v bytes, will overflow the pending batch of %v bytes.", messageSizeBytes, r.pendingBatchSizeBytes) 122 logger.Debugf("Pending batch would overflow if current message is added, cutting batch now.") 123 messageBatch := r.Cut() 124 messageBatches = append(messageBatches, messageBatch) 125 } 126 127 logger.Debugf("Enqueuing message into batch") 128 r.pendingBatch = append(r.pendingBatch, msg) 129 r.pendingBatchSizeBytes += messageSizeBytes 130 131 if uint32(len(r.pendingBatch)) >= r.sharedConfigManager.BatchSize().MaxMessageCount { 132 logger.Debugf("Batch size met, cutting batch") 133 messageBatch := r.Cut() 134 messageBatches = append(messageBatches, messageBatch) 135 } 136 137 return messageBatches, true 138 } 139 140 // Cut returns the current batch and starts a new one 141 func (r *receiver) Cut() []*cb.Envelope { 142 batch := r.pendingBatch 143 r.pendingBatch = nil 144 r.pendingBatchSizeBytes = 0 145 return batch 146 } 147 148 func messageSizeBytes(message *cb.Envelope) uint32 { 149 return uint32(len(message.Payload) + len(message.Signature)) 150 }