github.com/sykesm/fabric@v1.1.0-preview.0.20200129034918-2aa12b1a0181/orderer/consensus/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-lib-go/healthz" 12 cb "github.com/hyperledger/fabric-protos-go/common" 13 "github.com/hyperledger/fabric/common/flogging" 14 "github.com/hyperledger/fabric/common/metrics" 15 "github.com/hyperledger/fabric/orderer/common/localconfig" 16 "github.com/hyperledger/fabric/orderer/consensus" 17 ) 18 19 //go:generate counterfeiter -o mock/health_checker.go -fake-name HealthChecker . healthChecker 20 21 // healthChecker defines the contract for health checker 22 type healthChecker interface { 23 RegisterChecker(component string, checker healthz.HealthChecker) error 24 } 25 26 // New creates a Kafka-based consenter. Called by orderer's main.go. 27 func New(config localconfig.Kafka, metricsProvider metrics.Provider, healthChecker healthChecker) (consensus.Consenter, *Metrics) { 28 if config.Verbose { 29 flogging.ActivateSpec(flogging.Global.Spec() + ":orderer.consensus.kafka.sarama=debug") 30 } 31 32 brokerConfig := newBrokerConfig( 33 config.TLS, 34 config.SASLPlain, 35 config.Retry, 36 config.Version, 37 defaultPartition) 38 39 metrics := NewMetrics(metricsProvider, brokerConfig.MetricRegistry) 40 41 return &consenterImpl{ 42 brokerConfigVal: brokerConfig, 43 tlsConfigVal: config.TLS, 44 retryOptionsVal: config.Retry, 45 kafkaVersionVal: config.Version, 46 topicDetailVal: &sarama.TopicDetail{ 47 NumPartitions: 1, 48 ReplicationFactor: config.Topic.ReplicationFactor, 49 }, 50 healthChecker: healthChecker, 51 metrics: metrics, 52 }, metrics 53 } 54 55 // consenterImpl holds the implementation of type that satisfies the 56 // consensus.Consenter interface --as the HandleChain contract requires-- and 57 // the commonConsenter one. 58 type consenterImpl struct { 59 brokerConfigVal *sarama.Config 60 tlsConfigVal localconfig.TLS 61 retryOptionsVal localconfig.Retry 62 kafkaVersionVal sarama.KafkaVersion 63 topicDetailVal *sarama.TopicDetail 64 healthChecker healthChecker 65 metrics *Metrics 66 } 67 68 // HandleChain creates/returns a reference to a consensus.Chain object for the 69 // given set of support resources. Implements the consensus.Consenter 70 // interface. Called by consensus.newChainSupport(), which is itself called by 71 // multichannel.NewManagerImpl() when ranging over the ledgerFactory's 72 // existingChains. 73 func (consenter *consenterImpl) HandleChain(support consensus.ConsenterSupport, metadata *cb.Metadata) (consensus.Chain, error) { 74 lastOffsetPersisted, lastOriginalOffsetProcessed, lastResubmittedConfigOffset := getOffsets(metadata.Value, support.ChannelID()) 75 ch, err := newChain(consenter, support, lastOffsetPersisted, lastOriginalOffsetProcessed, lastResubmittedConfigOffset) 76 if err != nil { 77 return nil, err 78 } 79 consenter.healthChecker.RegisterChecker(ch.channel.String(), ch) 80 return ch, nil 81 } 82 83 // commonConsenter allows us to retrieve the configuration options set on the 84 // consenter object. These will be common across all chain objects derived by 85 // this consenter. They are set using local configuration settings. This 86 // interface is satisfied by consenterImpl. 87 type commonConsenter interface { 88 brokerConfig() *sarama.Config 89 retryOptions() localconfig.Retry 90 topicDetail() *sarama.TopicDetail 91 Metrics() *Metrics 92 } 93 94 func (consenter *consenterImpl) Metrics() *Metrics { 95 return consenter.metrics 96 } 97 98 func (consenter *consenterImpl) brokerConfig() *sarama.Config { 99 return consenter.brokerConfigVal 100 } 101 102 func (consenter *consenterImpl) retryOptions() localconfig.Retry { 103 return consenter.retryOptionsVal 104 } 105 106 func (consenter *consenterImpl) topicDetail() *sarama.TopicDetail { 107 return consenter.topicDetailVal 108 }