当前位置: 首页>>代码示例>>Java>>正文


Java PartitionMetadata.leader方法代码示例

本文整理汇总了Java中kafka.javaapi.PartitionMetadata.leader方法的典型用法代码示例。如果您正苦于以下问题:Java PartitionMetadata.leader方法的具体用法?Java PartitionMetadata.leader怎么用?Java PartitionMetadata.leader使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在kafka.javaapi.PartitionMetadata的用法示例。


在下文中一共展示了PartitionMetadata.leader方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getOffset

import kafka.javaapi.PartitionMetadata; //导入方法依赖的package包/类
private static OffsetInfo getOffset(String topic, PartitionMetadata partition) {
  Broker broker = partition.leader();

  SimpleConsumer consumer = new SimpleConsumer(broker.host(), broker.port(), 10000, 1000000,
                                               "com.rekko.newrelic.storm.kafka");
  try {
    TopicAndPartition
        topicAndPartition =
        new TopicAndPartition(topic, partition.partitionId());
    PartitionOffsetRequestInfo rquest = new PartitionOffsetRequestInfo(-1, 1);
    Map<TopicAndPartition, PartitionOffsetRequestInfo>
        map =
        new HashMap<TopicAndPartition, PartitionOffsetRequestInfo>();
    map.put(topicAndPartition, rquest);
    OffsetRequest req = new OffsetRequest(map, (short) 0, "com.rekko.newrelic.storm.kafka");
    OffsetResponse resp = consumer.getOffsetsBefore(req);
    OffsetInfo offset = new OffsetInfo();
    offset.offset = resp.offsets(topic, partition.partitionId())[0];
    return offset;
  } finally {
    consumer.close();
  }
}
 
开发者ID:ghais,项目名称:newrelic_storm_kafka,代码行数:24,代码来源:Kafka.java

示例2: findNewLeader

import kafka.javaapi.PartitionMetadata; //导入方法依赖的package包/类
private String findNewLeader(KafkaBrokerInfo brokerInfo, String topic, int partition) throws Exception {
    for (int i = 0; i < 3; i++) {
        boolean goToSleep = false;
        PartitionMetadata metadata = findLeader(brokerInfo, topic, partition);
        if (metadata == null) {
            goToSleep = true;
        } else if (metadata.leader() == null) {
            goToSleep = true;
        } else {
            return metadata.leader().host();
        }
        if (goToSleep) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ie) {
            }
        }
    }
    throw new Exception("Unable to find new leader after Broker failure. Exiting");
}
 
开发者ID:wngn123,项目名称:wngn-jms-kafka,代码行数:21,代码来源:SimpleConsumerExample.java

示例3: findNewLeader

import kafka.javaapi.PartitionMetadata; //导入方法依赖的package包/类
private String findNewLeader(List<KafkaBrokerInfo> brokerInfoList, String topic, int partition) throws Exception {
    for (int i = 0; i < 3; i++) {
        boolean goToSleep = false;
        PartitionMetadata metadata = findLeader(brokerInfoList, topic, partition);
        if (metadata == null) {
            goToSleep = true;
        } else if (metadata.leader() == null) {
            goToSleep = true;
        } else {
            return metadata.leader().host();
        }
        if (goToSleep) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ie) {
            }
        }
    }
    throw new Exception("Unable to find new leader after Broker failure. Exiting");
}
 
开发者ID:wngn123,项目名称:wngn-jms-kafka,代码行数:21,代码来源:KafkaSimpleConsumer.java

示例4: findNewLeader

import kafka.javaapi.PartitionMetadata; //导入方法依赖的package包/类
private String findNewLeader(String a_oldLeader, String a_topic, int a_partition, int a_port) throws Exception {
    for (int i = 0; i < 3; i++) {
        boolean goToSleep = false;
        PartitionMetadata metadata = findLeader(m_replicaBrokers, a_port, a_topic, a_partition);
        if (metadata == null) {
            goToSleep = true;
        } else if (metadata.leader() == null) {
            goToSleep = true;
        } else if (a_oldLeader.equalsIgnoreCase(metadata.leader().host()) && i == 0) {
            // first time through if the leader hasn't changed give ZooKeeper a second to recover
            // second time, assume the broker did recover before failover, or it was a non-Broker issue
            //
            goToSleep = true;
        } else {
            return metadata.leader().host();
        }
        if (goToSleep) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ie) {
            }
        }
    }
    LOG.info("Unable to find new leader after Broker failure. Exiting");
    throw new Exception("Unable to find new leader after Broker failure. Exiting");
}
 
开发者ID:sakserv,项目名称:hadoop-mini-clusters,代码行数:27,代码来源:KafkaTestConsumer.java

示例5: findNewLeader

import kafka.javaapi.PartitionMetadata; //导入方法依赖的package包/类
private HostAndPort findNewLeader(HostAndPort oldLeader, String topic, int partition) throws StageException {
  //try 3 times to find a new leader
  for (int i = 0; i < 3; i++) {
    boolean sleep;
    PartitionMetadata metadata = getPartitionMetadata(replicaBrokers, topic, partition);
    if (metadata == null || metadata.leader() == null) {
      sleep = true;
    } else if (oldLeader.getHostText().equalsIgnoreCase(metadata.leader().host()) && i == 0) {
      //leader has not yet changed, give zookeeper sometime
      sleep = true;
    } else {
      return HostAndPort.fromParts(metadata.leader().host(), metadata.leader().port());
    }
    if (sleep) {
      ThreadUtil.sleep(ONE_SECOND);
    }
  }
  LOG.error(KafkaErrors.KAFKA_21.getMessage());
  throw new StageException(KafkaErrors.KAFKA_21);
}
 
开发者ID:streamsets,项目名称:datacollector,代码行数:21,代码来源:KafkaLowLevelConsumer08.java

示例6: findNewLeader

import kafka.javaapi.PartitionMetadata; //导入方法依赖的package包/类
private String findNewLeader(String a_oldLeader, String a_topic, int a_partition, int a_port) throws Exception {
    for (int i = 0; i < 3; i++) {
        boolean goToSleep = false;
        PartitionMetadata metadata = findLeader(m_replicaBrokers, a_port, a_topic, a_partition);
        if (metadata == null) {
            goToSleep = true;
        } else if (metadata.leader() == null) {
            goToSleep = true;
        } else if (a_oldLeader.equalsIgnoreCase(metadata.leader().host()) && i == 0) {
            // first time through if the leader hasn't changed give ZooKeeper a second to recover
            // second time, assume the broker did recover before failover, or it was a non-Broker issue
            //
            goToSleep = true;
        } else {
            return metadata.leader().host();
        }
        if (goToSleep) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ie) {
            }
        }
    }
    System.out.println("Unable to find new leader after Broker failure. Exiting");
    throw new Exception("Unable to find new leader after Broker failure. Exiting");
}
 
开发者ID:ogidogi,项目名称:laughing-octo-sansa,代码行数:27,代码来源:SimpleExample.java

示例7: findNewLeader

import kafka.javaapi.PartitionMetadata; //导入方法依赖的package包/类
private Broker findNewLeader(Broker oldLeader) throws InterruptedException {
    long retryCnt = 0;
    while (true) {
        PartitionMetadata metadata = findLeader();
        logger.debug("findNewLeader - meta leader {}, previous leader {}", metadata, oldLeader);
        if (metadata != null && metadata.leader() != null && (oldLeader == null ||
                (!(oldLeader.host().equalsIgnoreCase(metadata.leader().host()) &&
                  (oldLeader.port() == metadata.leader().port())) || retryCnt != 0))) {
            // first time through if the leader hasn't changed give ZooKeeper a second to recover
            // second time, assume the broker did recover before failover, or it was a non-Broker issue
            logger.info("findNewLeader - using new leader {} from meta data, previous leader {}", metadata.leader(), oldLeader);
            return metadata.leader();
        }
        //TODO: backoff retry
        Thread.sleep(1000L);
        retryCnt ++;
        // if could not find the leader for current replicaBrokers, let's try to find one via allBrokers
        if (retryCnt >= 3 && (retryCnt - 3) % 5 == 0) {
            logger.warn("can nof find leader for {} - {} after {} retries", topic, partitionId, retryCnt);
            replicaBrokers.clear();
            replicaBrokers.addAll(allBrokers);
        }
    }
}
 
开发者ID:lyogavin,项目名称:Pistachio,代码行数:25,代码来源:KafkaSimpleConsumer.java

示例8: getPartitionsForTopic

import kafka.javaapi.PartitionMetadata; //导入方法依赖的package包/类
private List<KafkaPartition> getPartitionsForTopic(TopicMetadata topicMetadata) {
  List<KafkaPartition> partitions = Lists.newArrayList();

  for (PartitionMetadata partitionMetadata : topicMetadata.partitionsMetadata()) {
    if (null == partitionMetadata) {
      LOG.error("Ignoring topic with null partition metadata " + topicMetadata.topic());
      return Collections.emptyList();
    }
    if (null == partitionMetadata.leader()) {
      LOG.error(
          "Ignoring topic with null partition leader " + topicMetadata.topic() + " metatada=" + partitionMetadata);
      return Collections.emptyList();
    }
    partitions.add(new KafkaPartition.Builder().withId(partitionMetadata.partitionId())
        .withTopicName(topicMetadata.topic()).withLeaderId(partitionMetadata.leader().id())
        .withLeaderHostAndPort(partitionMetadata.leader().host(), partitionMetadata.leader().port()).build());
  }
  return partitions;
}
 
开发者ID:apache,项目名称:incubator-gobblin,代码行数:20,代码来源:KafkaWrapper.java

示例9: getPartitionsForTopic

import kafka.javaapi.PartitionMetadata; //导入方法依赖的package包/类
private List<KafkaPartition> getPartitionsForTopic(TopicMetadata topicMetadata) {
  List<KafkaPartition> partitions = Lists.newArrayList();

  for (PartitionMetadata partitionMetadata : topicMetadata.partitionsMetadata()) {
    if (null == partitionMetadata) {
      log.error("Ignoring topic with null partition metadata " + topicMetadata.topic());
      return Collections.emptyList();
    }
    if (null == partitionMetadata.leader()) {
      log.error("Ignoring topic with null partition leader " + topicMetadata.topic() + " metatada="
          + partitionMetadata);
      return Collections.emptyList();
    }
    partitions.add(new KafkaPartition.Builder().withId(partitionMetadata.partitionId())
        .withTopicName(topicMetadata.topic()).withLeaderId(partitionMetadata.leader().id())
        .withLeaderHostAndPort(partitionMetadata.leader().host(), partitionMetadata.leader().port()).build());
  }
  return partitions;
}
 
开发者ID:apache,项目名称:incubator-gobblin,代码行数:20,代码来源:Kafka08ConsumerClient.java

示例10: findNewLeader

import kafka.javaapi.PartitionMetadata; //导入方法依赖的package包/类
private HostPort findNewLeader(String oldHost, int oldPort) throws LostLeadershipException {
  for (int i = 0; i < 3; i++) {
    boolean goToSleep = false;
    PartitionMetadata metadata = findLeader(replicaBrokers, topic, partition);
    if (metadata == null) {
      goToSleep = true;
    } else if (metadata.leader() == null) {
      goToSleep = true;
    } else if (oldHost.equalsIgnoreCase(metadata.leader().host()) &&
        oldPort == metadata.leader().port()) {
      // first time through if the leader hasn't changed give ZooKeeper a second to recover
      // second time, assume the broker did recover before failover, or it was a non-Broker issue
      goToSleep = true;
    } else {
      return new HostPort(metadata.leader().host(), metadata.leader().port());
    }
    if (goToSleep) {
      try {
        Thread.sleep(1000);
      } catch (InterruptedException ie) {
      }
    }
  }
  // Can't recover from a leadership disappearance.
  throw new LostLeadershipException();
}
 
开发者ID:DemandCube,项目名称:Scribengin,代码行数:27,代码来源:ScribeConsumer.java

示例11: run

import kafka.javaapi.PartitionMetadata; //导入方法依赖的package包/类
public void run() {
  try {
    while (!exit) {
      KafkaTool kafkaTool = new KafkaTool(topic, cluster.getZKConnect());
      kafkaTool.connect();
      TopicMetadata topicMeta = kafkaTool.findTopicMetadata(topic);
      PartitionMetadata partitionMeta = findPartition(topicMeta, partition);
      Broker partitionLeader = partitionMeta.leader();
      Server kafkaServer = cluster.findKafkaServerByPort(partitionLeader.port());
      System.out.println("Shutdown kafka server " + kafkaServer.getPort());
      kafkaServer.shutdown();
      failureCount++;
      Thread.sleep(sleepBeforeRestart);
      kafkaServer.start();
      kafkaTool.close();
      Thread.sleep(10000); //wait to make sure that the kafka server start
    }
  } catch (Exception e) {
    e.printStackTrace();
  }
  synchronized (this) {
    notify();
  }
}
 
开发者ID:DemandCube,项目名称:Scribengin,代码行数:25,代码来源:AckKafkaWriterTestRunner.java

示例12: findLeader

import kafka.javaapi.PartitionMetadata; //导入方法依赖的package包/类
private PartitionMetadata findLeader(List<KafkaBrokerInfo> brokerInfoList, String topic, int partition) {
    PartitionMetadata returnMetaData = null;
    for (KafkaBrokerInfo brokerInfo : brokerInfoList) {
        PartitionMetadata part = findLeader(brokerInfo, topic, partition);
        if (part != null && part.leader() != null) {
            return part;
        }
    }
    return null;
}
 
开发者ID:wngn123,项目名称:wngn-jms-kafka,代码行数:11,代码来源:SimpleConsumerExample.java

示例13: validatePartitionMetadata

import kafka.javaapi.PartitionMetadata; //导入方法依赖的package包/类
/**
 * 验证分区元数据,如果验证失败,直接抛出IllegalArgumentException异常
 *
 * @param metadata
 */
private void validatePartitionMetadata(PartitionMetadata metadata) {
    if (metadata == null) {
        System.out.println("Can't find metadata for Topic and Partition. Exiting!!");
        throw new IllegalArgumentException("Can't find metadata for Topic and Partition. Exiting!!");
    }
    if (metadata.leader() == null) {
        System.out.println("Can't find Leader for Topic and Partition. Exiting!!");
        throw new IllegalArgumentException("Can't find Leader for Topic and Partition. Exiting!!");
    }
}
 
开发者ID:wngn123,项目名称:wngn-jms-kafka,代码行数:16,代码来源:JavaKafkaSimpleConsumerAPI.java

示例14: findNewLeaderMetadata

import kafka.javaapi.PartitionMetadata; //导入方法依赖的package包/类
/**
 * 根据给定参数获取一个新leader的分区元数据信息
 *
 * @param oldLeader
 * @param topic
 * @param partitionID
 *
 * @return
 */
private PartitionMetadata findNewLeaderMetadata(String oldLeader, String topic, int partitionID) {
    KafkaTopicPartitionInfo topicPartitionInfo = new KafkaTopicPartitionInfo(topic, partitionID);
    List<KafkaBrokerInfo> brokerInfos = this.replicaBrokers.get(topicPartitionInfo);
    for (int i = 0; i < 3; i++) {
        boolean gotoSleep = false;
        PartitionMetadata metadata = this.findLeader(brokerInfos, topic, partitionID);
        if (metadata == null) {
            gotoSleep = true;
        } else if (metadata.leader() == null) {
            gotoSleep = true;
        } else if (oldLeader.equalsIgnoreCase(metadata.leader().host()) && i == 0) {
            // leader切换过程中
            gotoSleep = true;
        } else {
            return metadata;
        }

        if (gotoSleep) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // nothings
            }
        }
    }

    System.out.println("Unable to find new leader after Broker failure. Exiting!!");
    throw new RuntimeException("Unable to find new leader after Broker failure. Exiting!!");
}
 
开发者ID:wngn123,项目名称:wngn-jms-kafka,代码行数:39,代码来源:JavaKafkaSimpleConsumerAPI.java

示例15: findLeader

import kafka.javaapi.PartitionMetadata; //导入方法依赖的package包/类
private PartitionMetadata findLeader(List<KafkaBrokerInfo> brokerInfoList, String topic, int partition) {
    PartitionMetadata returnMetaData = null;
    for (KafkaBrokerInfo brokerInfo : brokerInfoList) {
        PartitionMetadata part = findLeader(brokerInfo.brokerHost, brokerInfo.brokerPort, topic, partition);
        if (part != null && part.leader() != null) {
            return part;
        }
    }
    return null;
}
 
开发者ID:wngn123,项目名称:wngn-jms-kafka,代码行数:11,代码来源:KafkaSimpleConsumer.java


注:本文中的kafka.javaapi.PartitionMetadata.leader方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。