github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/orderer/kafka/broker.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  
    22  	"github.com/Shopify/sarama"
    23  )
    24  
    25  // Broker allows the caller to get info on the cluster's partitions
    26  type Broker interface {
    27  	GetOffset(cp ChainPartition, req *sarama.OffsetRequest) (int64, error)
    28  	Closeable
    29  }
    30  
    31  type brokerImpl struct {
    32  	broker *sarama.Broker
    33  }
    34  
    35  // Connects to the broker that handles all produce and consume
    36  // requests for the given chain (Partition Leader Replica)
    37  func newBroker(brokers []string, cp ChainPartition) (Broker, error) {
    38  	var candidateBroker, connectedBroker, leaderBroker *sarama.Broker
    39  
    40  	// Connect to one of the given brokers
    41  	for _, hostPort := range brokers {
    42  		candidateBroker = sarama.NewBroker(hostPort)
    43  		if err := candidateBroker.Open(nil); err != nil {
    44  			logger.Warningf("Failed to connect to broker %s: %s", hostPort, err)
    45  			continue
    46  		}
    47  		if connected, err := candidateBroker.Connected(); !connected {
    48  			logger.Warningf("Failed to connect to broker %s: %s", hostPort, err)
    49  			continue
    50  		}
    51  		connectedBroker = candidateBroker
    52  		break
    53  	}
    54  
    55  	if connectedBroker == nil {
    56  		return nil, fmt.Errorf("Failed to connect to any of the given brokers (%v) for metadata request", brokers)
    57  	}
    58  	logger.Debugf("Connected to broker %s", connectedBroker.Addr())
    59  
    60  	// Get metadata for the topic that corresponds to this chain
    61  	metadata, err := connectedBroker.GetMetadata(&sarama.MetadataRequest{Topics: []string{cp.Topic()}})
    62  	if err != nil {
    63  		return nil, fmt.Errorf("Failed to get metadata for topic %s: %s", cp, err)
    64  	}
    65  
    66  	// Get the leader broker for this chain partition
    67  	if (cp.Partition() >= 0) && (cp.Partition() < int32(len(metadata.Topics[0].Partitions))) {
    68  		leaderBrokerID := metadata.Topics[0].Partitions[cp.Partition()].Leader
    69  		logger.Debugf("Leading broker for chain %s is broker ID %d", cp, leaderBrokerID)
    70  		for _, availableBroker := range metadata.Brokers {
    71  			if availableBroker.ID() == leaderBrokerID {
    72  				leaderBroker = availableBroker
    73  				break
    74  			}
    75  		}
    76  	}
    77  
    78  	if leaderBroker == nil {
    79  		return nil, fmt.Errorf("Can't find leader for chain %s", cp)
    80  	}
    81  
    82  	// Connect to broker
    83  	if err := leaderBroker.Open(nil); err != nil {
    84  		return nil, fmt.Errorf("Failed to connect ho Kafka broker: %s", err)
    85  	}
    86  	if connected, err := leaderBroker.Connected(); !connected {
    87  		return nil, fmt.Errorf("Failed to connect to Kafka broker: %s", err)
    88  	}
    89  
    90  	return &brokerImpl{broker: leaderBroker}, nil
    91  }
    92  
    93  // GetOffset retrieves the offset number that corresponds
    94  // to the requested position in the log.
    95  func (b *brokerImpl) GetOffset(cp ChainPartition, req *sarama.OffsetRequest) (int64, error) {
    96  	resp, err := b.broker.GetAvailableOffsets(req)
    97  	if err != nil {
    98  		return int64(-1), err
    99  	}
   100  	return resp.GetBlock(cp.Topic(), cp.Partition()).Offsets[0], nil
   101  }
   102  
   103  // Close terminates the broker.
   104  // This is invoked by the session deliverer's getOffset method.
   105  func (b *brokerImpl) Close() error {
   106  	return b.broker.Close()
   107  }