github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/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  		// ATTN: If we ever switch to more than one partition per topic, the message
    70  		// below should be updated to print `cp` (i.e. Topic/Partition) instead of
    71  		// `cp.Topic()`.
    72  		logger.Debugf("[channel: %s] Leading broker: %d", cp.Topic(), leaderBrokerID)
    73  		for _, availableBroker := range metadata.Brokers {
    74  			if availableBroker.ID() == leaderBrokerID {
    75  				leaderBroker = availableBroker
    76  				break
    77  			}
    78  		}
    79  	}
    80  
    81  	if leaderBroker == nil {
    82  		// ATTN: If we ever switch to more than one partition per topic, the message
    83  		// below should be updated to print `cp` (i.e. Topic/Partition) instead of
    84  		// `cp.Topic()`.
    85  		return nil, fmt.Errorf("[channel: %s] cannot find leader", cp.Topic())
    86  	}
    87  
    88  	// Connect to broker
    89  	if err := leaderBroker.Open(nil); err != nil {
    90  		return nil, fmt.Errorf("failed to connect to Kafka broker: %s", err)
    91  	}
    92  	if connected, err := leaderBroker.Connected(); !connected {
    93  		return nil, fmt.Errorf("failed to connect to Kafka broker: %s", err)
    94  	}
    95  
    96  	return &brokerImpl{broker: leaderBroker}, nil
    97  }
    98  
    99  // GetOffset retrieves the offset number that corresponds
   100  // to the requested position in the log.
   101  func (b *brokerImpl) GetOffset(cp ChainPartition, req *sarama.OffsetRequest) (int64, error) {
   102  	resp, err := b.broker.GetAvailableOffsets(req)
   103  	if err != nil {
   104  		return int64(-1), err
   105  	}
   106  	return resp.GetBlock(cp.Topic(), cp.Partition()).Offsets[0], nil
   107  }
   108  
   109  // Close terminates the broker.
   110  // This is invoked by the session deliverer's getOffset method.
   111  func (b *brokerImpl) Close() error {
   112  	return b.broker.Close()
   113  }