github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/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  	configvaluesapi "github.com/hyperledger/fabric/common/configvalues"
    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, nil, and true (indicating ok).
    34  	// If the current message valid, and batches need to be cut:
    35  	//   - Ordered will return 1 or 2 batches of messages, 1 or 2 batches of committers, and true (indicating ok).
    36  	// If the current message is invalid:
    37  	//   - Ordered will return nil, nil, and false (to indicate not ok).
    38  	//
    39  	// Given a valid message, if the current message needs to be isolated (as determined during filtering).
    40  	//   - Ordered will return:
    41  	//     * The pending batch of (if not empty), and a second batch containing only the isolated message.
    42  	//     * The corresponding batches of committers.
    43  	//     * true (indicating ok).
    44  	// Otherwise, given a valid message, the pending batch, if not empty, will be cut and returned if:
    45  	//   - The current message needs to be isolated (as determined during filtering).
    46  	//   - The current message will cause the pending batch size in bytes to exceed BatchSize.PreferredMaxBytes.
    47  	//   - After adding the current message to the pending batch, the message count has reached BatchSize.MaxMessageCount.
    48  	Ordered(msg *cb.Envelope) ([][]*cb.Envelope, [][]filter.Committer, bool)
    49  
    50  	// Cut returns the current batch and starts a new one
    51  	Cut() ([]*cb.Envelope, []filter.Committer)
    52  }
    53  
    54  type receiver struct {
    55  	sharedConfigManager   configvaluesapi.Orderer
    56  	filters               *filter.RuleSet
    57  	pendingBatch          []*cb.Envelope
    58  	pendingBatchSizeBytes uint32
    59  	pendingCommitters     []filter.Committer
    60  }
    61  
    62  // NewReceiverImpl creates a Receiver implementation based on the given configtxorderer manager and filters
    63  func NewReceiverImpl(sharedConfigManager configvaluesapi.Orderer, filters *filter.RuleSet) Receiver {
    64  	return &receiver{
    65  		sharedConfigManager: sharedConfigManager,
    66  		filters:             filters,
    67  	}
    68  }
    69  
    70  // Ordered should be invoked sequentially as messages are ordered
    71  // If the current message valid, and no batches need to be cut:
    72  //   - Ordered will return nil, nil, and true (indicating ok).
    73  // If the current message valid, and batches need to be cut:
    74  //   - Ordered will return 1 or 2 batches of messages, 1 or 2 batches of committers, and true (indicating ok).
    75  // If the current message is invalid:
    76  //   - Ordered will return nil, nil, and false (to indicate not ok).
    77  //
    78  // Given a valid message, if the current message needs to be isolated (as determined during filtering).
    79  //   - Ordered will return:
    80  //     * The pending batch of (if not empty), and a second batch containing only the isolated message.
    81  //     * The corresponding batches of committers.
    82  //     * true (indicating ok).
    83  // Otherwise, given a valid message, the pending batch, if not empty, will be cut and returned if:
    84  //   - The current message needs to be isolated (as determined during filtering).
    85  //   - The current message will cause the pending batch size in bytes to exceed BatchSize.PreferredMaxBytes.
    86  //   - After adding the current message to the pending batch, the message count has reached BatchSize.MaxMessageCount.
    87  func (r *receiver) Ordered(msg *cb.Envelope) ([][]*cb.Envelope, [][]filter.Committer, bool) {
    88  	// The messages must be filtered a second time in case configuration has changed since the message was received
    89  	committer, err := r.filters.Apply(msg)
    90  	if err != nil {
    91  		logger.Debugf("Rejecting message: %s", err)
    92  		return nil, nil, false
    93  	}
    94  
    95  	messageSizeBytes := messageSizeBytes(msg)
    96  
    97  	if committer.Isolated() || messageSizeBytes > r.sharedConfigManager.BatchSize().PreferredMaxBytes {
    98  
    99  		if committer.Isolated() {
   100  			logger.Debugf("Found message which requested to be isolated, cutting into its own batch")
   101  		} else {
   102  			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)
   103  		}
   104  
   105  		messageBatches := [][]*cb.Envelope{}
   106  		committerBatches := [][]filter.Committer{}
   107  
   108  		// cut pending batch, if it has any messages
   109  		if len(r.pendingBatch) > 0 {
   110  			messageBatch, committerBatch := r.Cut()
   111  			messageBatches = append(messageBatches, messageBatch)
   112  			committerBatches = append(committerBatches, committerBatch)
   113  		}
   114  
   115  		// create new batch with single message
   116  		messageBatches = append(messageBatches, []*cb.Envelope{msg})
   117  		committerBatches = append(committerBatches, []filter.Committer{committer})
   118  
   119  		return messageBatches, committerBatches, true
   120  	}
   121  
   122  	messageBatches := [][]*cb.Envelope{}
   123  	committerBatches := [][]filter.Committer{}
   124  
   125  	messageWillOverflowBatchSizeBytes := r.pendingBatchSizeBytes+messageSizeBytes > r.sharedConfigManager.BatchSize().PreferredMaxBytes
   126  
   127  	if messageWillOverflowBatchSizeBytes {
   128  		logger.Debugf("The current message, with %v bytes, will overflow the pending batch of %v bytes.", messageSizeBytes, r.pendingBatchSizeBytes)
   129  		logger.Debugf("Pending batch would overflow if current message is added, cutting batch now.")
   130  		messageBatch, committerBatch := r.Cut()
   131  		messageBatches = append(messageBatches, messageBatch)
   132  		committerBatches = append(committerBatches, committerBatch)
   133  	}
   134  
   135  	logger.Debugf("Enqueuing message into batch")
   136  	r.pendingBatch = append(r.pendingBatch, msg)
   137  	r.pendingBatchSizeBytes += messageSizeBytes
   138  	r.pendingCommitters = append(r.pendingCommitters, committer)
   139  
   140  	if uint32(len(r.pendingBatch)) >= r.sharedConfigManager.BatchSize().MaxMessageCount {
   141  		logger.Debugf("Batch size met, cutting batch")
   142  		messageBatch, committerBatch := r.Cut()
   143  		messageBatches = append(messageBatches, messageBatch)
   144  		committerBatches = append(committerBatches, committerBatch)
   145  	}
   146  
   147  	// return nils instead of empty slices
   148  	if len(messageBatches) == 0 {
   149  		return nil, nil, true
   150  	}
   151  
   152  	return messageBatches, committerBatches, true
   153  
   154  }
   155  
   156  // Cut returns the current batch and starts a new one
   157  func (r *receiver) Cut() ([]*cb.Envelope, []filter.Committer) {
   158  	batch := r.pendingBatch
   159  	r.pendingBatch = nil
   160  	committers := r.pendingCommitters
   161  	r.pendingCommitters = nil
   162  	r.pendingBatchSizeBytes = 0
   163  	return batch, committers
   164  }
   165  
   166  func messageSizeBytes(message *cb.Envelope) uint32 {
   167  	return uint32(len(message.Payload) + len(message.Signature))
   168  }