github.com/tenywen/fabric@v1.0.0-beta.0.20170620030522-a5b1ed380643/orderer/kafka/consenter.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package kafka
     8  
     9  import (
    10  	"github.com/Shopify/sarama"
    11  	"github.com/hyperledger/fabric/common/flogging"
    12  	localconfig "github.com/hyperledger/fabric/orderer/localconfig"
    13  	"github.com/hyperledger/fabric/orderer/multichain"
    14  	cb "github.com/hyperledger/fabric/protos/common"
    15  	logging "github.com/op/go-logging"
    16  )
    17  
    18  const pkgLogID = "orderer/kafka"
    19  
    20  var logger *logging.Logger
    21  
    22  func init() {
    23  	logger = flogging.MustGetLogger(pkgLogID)
    24  }
    25  
    26  // New creates a Kafka-based consenter. Called by orderer's main.go.
    27  func New(tlsConfig localconfig.TLS, retryOptions localconfig.Retry, kafkaVersion sarama.KafkaVersion) multichain.Consenter {
    28  	brokerConfig := newBrokerConfig(tlsConfig, retryOptions, kafkaVersion, defaultPartition)
    29  	return &consenterImpl{
    30  		brokerConfigVal: brokerConfig,
    31  		tlsConfigVal:    tlsConfig,
    32  		retryOptionsVal: retryOptions,
    33  		kafkaVersionVal: kafkaVersion}
    34  }
    35  
    36  // consenterImpl holds the implementation of type that satisfies the
    37  // multichain.Consenter interface --as the HandleChain contract requires-- and
    38  // the commonConsenter one.
    39  type consenterImpl struct {
    40  	brokerConfigVal *sarama.Config
    41  	tlsConfigVal    localconfig.TLS
    42  	retryOptionsVal localconfig.Retry
    43  	kafkaVersionVal sarama.KafkaVersion
    44  }
    45  
    46  // HandleChain creates/returns a reference to a multichain.Chain object for the
    47  // given set of support resources. Implements the multichain.Consenter
    48  // interface. Called by multichain.newChainSupport(), which is itself called by
    49  // multichain.NewManagerImpl() when ranging over the ledgerFactory's
    50  // existingChains.
    51  func (consenter *consenterImpl) HandleChain(support multichain.ConsenterSupport, metadata *cb.Metadata) (multichain.Chain, error) {
    52  	lastOffsetPersisted := getLastOffsetPersisted(metadata.Value, support.ChainID())
    53  	return newChain(consenter, support, lastOffsetPersisted)
    54  }
    55  
    56  // commonConsenter allows us to retrieve the configuration options set on the
    57  // consenter object. These will be common across all chain objects derived by
    58  // this consenter. They are set using using local configuration settings. This
    59  // interface is satisfied by consenterImpl.
    60  type commonConsenter interface {
    61  	brokerConfig() *sarama.Config
    62  	retryOptions() localconfig.Retry
    63  }
    64  
    65  func (consenter *consenterImpl) brokerConfig() *sarama.Config {
    66  	return consenter.brokerConfigVal
    67  }
    68  
    69  func (consenter *consenterImpl) retryOptions() localconfig.Retry {
    70  	return consenter.retryOptionsVal
    71  }
    72  
    73  // closeable allows the shut down of the calling resource.
    74  type closeable interface {
    75  	close() error
    76  }