github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/orderer/kafka/producer.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 kafka
    18  
    19  import (
    20  	"fmt"
    21  	"time"
    22  
    23  	"github.com/Shopify/sarama"
    24  	"github.com/hyperledger/fabric/orderer/localconfig"
    25  )
    26  
    27  // Producer allows the caller to post blobs to a chain partition on the Kafka cluster.
    28  type Producer interface {
    29  	Send(cp ChainPartition, payload []byte) error
    30  	Closeable
    31  }
    32  
    33  type producerImpl struct {
    34  	producer sarama.SyncProducer
    35  }
    36  
    37  func newProducer(brokers []string, kafkaVersion sarama.KafkaVersion, retryOptions config.Retry, tls config.TLS) Producer {
    38  	var p sarama.SyncProducer
    39  	var err error
    40  	brokerConfig := newBrokerConfig(kafkaVersion, rawPartition, tls)
    41  
    42  	repeatTick := time.NewTicker(retryOptions.Period)
    43  	panicTick := time.NewTicker(retryOptions.Stop)
    44  	defer repeatTick.Stop()
    45  	defer panicTick.Stop()
    46  
    47  loop:
    48  	for {
    49  		select {
    50  		case <-panicTick.C:
    51  			panic(fmt.Errorf("Failed to create Kafka producer: %v", err))
    52  		case <-repeatTick.C:
    53  			logger.Debug("Connecting to Kafka cluster:", brokers)
    54  			p, err = sarama.NewSyncProducer(brokers, brokerConfig)
    55  			if err == nil {
    56  				break loop
    57  			}
    58  		}
    59  	}
    60  
    61  	logger.Debug("Connected to the Kafka cluster")
    62  	return &producerImpl{producer: p}
    63  }
    64  
    65  // Close shuts down the Producer component of the orderer.
    66  func (p *producerImpl) Close() error {
    67  	return p.producer.Close()
    68  }
    69  
    70  // Send posts a blob to a chain partition on the Kafka cluster.
    71  func (p *producerImpl) Send(cp ChainPartition, payload []byte) error {
    72  	prt, ofs, err := p.producer.SendMessage(newProducerMessage(cp, payload))
    73  	if prt != cp.Partition() {
    74  		// If this happens, something's up with the partitioner
    75  		logger.Warningf("Blob destined for partition %d, but posted to %d instead", cp.Partition(), prt)
    76  	}
    77  	if err == nil {
    78  		logger.Debugf("Forwarded blob with offset number %d to chain partition %s on the Kafka cluster", ofs, cp)
    79  	} else {
    80  		logger.Infof("Failed to send message to chain partition %s on the Kafka cluster: %s", cp, err)
    81  	}
    82  	return err
    83  }