本文整理汇总了Java中kafka.common.ErrorMapping类的典型用法代码示例。如果您正苦于以下问题:Java ErrorMapping类的具体用法?Java ErrorMapping怎么用?Java ErrorMapping使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ErrorMapping类属于kafka.common包,在下文中一共展示了ErrorMapping类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getOffset
import kafka.common.ErrorMapping; //导入依赖的package包/类
private static Long getOffset(OffsetResponse response, TopicAndPartition topicPartition) {
String topic = topicPartition.topic();
int partition = topicPartition.partition();
long[] offsets = response.offsets(topic, partition);
if (offsets.length > 0) {
return offsets[0];
}
short errorCode = response.errorCode(topic, partition);
if (errorCode == ErrorMapping.UnknownTopicOrPartitionCode()) {
log.info("Unknown topic or partition {} {}", topic, partition);
return null;
}
throw new IllegalStateException(
"Error reading offset for " + topic + " / " + partition + ": " +
ErrorMapping.exceptionNameFor(errorCode));
}
示例2: getTopicMetadata
import kafka.common.ErrorMapping; //导入依赖的package包/类
private Map<String, TopicVO> getTopicMetadata(BlockingChannel channel, String... topics)
{
final TopicMetadataRequest request =
new TopicMetadataRequest((short) 0, 0, clientId(), Arrays.asList(topics));
LOG.debug("Sending topic metadata request: {}", request);
channel.send(request);
final kafka.api.TopicMetadataResponse underlyingResponse =
kafka.api.TopicMetadataResponse.readFrom(channel.receive().buffer());
LOG.debug("Received topic metadata response: {}", underlyingResponse);
TopicMetadataResponse response = new TopicMetadataResponse(underlyingResponse);
return response.topicsMetadata().stream()
.filter(tmd -> tmd.errorCode() == ErrorMapping.NoError())
.map(this::processTopicMetadata)
.collect(Collectors.toMap(TopicVO::getName, t -> t));
}
示例3: getMessageSetSince
import kafka.common.ErrorMapping; //导入依赖的package包/类
private ByteBufferMessageSet getMessageSetSince(long offset, int timeoutInMs) {
if (timeoutInMs < 0) {
throw new IllegalArgumentException(String.format("Timeout must not lower than 0, timeout is: %d", timeoutInMs));
}
FetchRequest request = new FetchRequestBuilder()
.clientId(generateClientId())
.addFetch(assignedTopicPartition.topic(), assignedTopicPartition.partition(), offset, consumerConfig.bufferSize())
.maxWait(timeoutInMs)
.minBytes(consumerConfig.bufferSize())
.build();
FetchResponse response = partitionConsumer.fetch(request);
if (response.hasError()) {
short errorCode = response.errorCode(assignedTopicPartition.topic(), assignedTopicPartition.partition());
// @todo retry during broker failover
throw new PartitionConsumerException(ErrorMapping.exceptionFor(errorCode));
}
return response.messageSet(assignedTopicPartition.topic(), assignedTopicPartition.partition());
}
示例4: continueItr
import kafka.common.ErrorMapping; //导入依赖的package包/类
/**
* THIS METHOD HAS SIDE EFFECTS - it will update {@code currentMessageItr} (if necessary) and then return true iff
* the iterator still has elements to be read. If you call {@link scala.collection.Iterator#next()} when this method
* returns false, you risk a {@link NullPointerException} OR a no-more-elements exception.
*
* @return true if you can call {@link scala.collection.Iterator#next()} on {@code currentMessageItr}.
*/
@VisibleForTesting
boolean continueItr() {
final long remaining = end - currentOffset;
if (!canCallNext() && remaining > 0) {
final int theFetchSize = (fetchSize > remaining) ? (int) remaining : fetchSize;
LOG.debug(String.format("%s fetching %d bytes starting at offset %d", split.toString(), theFetchSize,
currentOffset));
final FetchRequest request = new FetchRequest(split.getPartition().getTopic(), split.getPartition()
.getPartId(), currentOffset, theFetchSize);
final ByteBufferMessageSet msg = consumer.fetch(request);
final int errorCode = msg.getErrorCode();
if (errorCode == ErrorMapping.OffsetOutOfRangeCode()) {
return false;
}
if (errorCode != ErrorMapping.NoError()) {
ErrorMapping.maybeThrowException(errorCode);
} // --> else we try to grab the next iterator
currentMessageItr = msg.iterator();
currentOffset += msg.validBytes();
}
return canCallNext();
}
示例5: handleError
import kafka.common.ErrorMapping; //导入依赖的package包/类
@Override
public void handleError(final Throwable e, RequestChannel requestChannel, Request request) {
if (((ProducerRequest) request.requestObj).requiredAcks == 0) {
requestChannel.closeConnection(request.processor, request);
} else {
Map<TopicAndPartition, ProducerResponseStatus> producerResponseStatus = Utils.map(data, new Function2<TopicAndPartition, ByteBufferMessageSet, Tuple2<TopicAndPartition, ProducerResponseStatus>>() {
@Override
public Tuple2<TopicAndPartition, ProducerResponseStatus> apply(TopicAndPartition arg1, ByteBufferMessageSet arg2) {
return Tuple2.make(arg1, new ProducerResponseStatus(ErrorMapping.codeFor(e.getClass()), -1l));
}
});
ProducerResponse errorResponse = new ProducerResponse(correlationId, producerResponseStatus);
requestChannel.sendResponse(new Response(request, new BoundedByteBufferSend(errorResponse)));
}
}
示例6: hasError
import kafka.common.ErrorMapping; //导入依赖的package包/类
/**
* Called by the default implementation of {@link #map} to check error code
* to determine whether to continue.
*/
protected boolean hasError(ByteBufferMessageSet messages)
throws IOException {
int errorCode = messages.getErrorCode();
if (errorCode == ErrorMapping.OffsetOutOfRangeCode()) {
/* offset cannot cross the maximum offset (guaranteed by Kafka protocol).
Kafka server may delete old files from time to time */
System.err.println("WARNING: current offset=" + _offset + ". It is out of range.");
if (_retry >= MAX_RETRY_TIME) return true;
_retry++;
// get the current offset range
_offsetRange = getOffsetRange();
_offset = _offsetRange[0];
return false;
} else if (errorCode == ErrorMapping.InvalidMessageCode()) {
throw new IOException(_input + " current offset=" + _offset
+ " : invalid offset.");
} else if (errorCode == ErrorMapping.WrongPartitionCode()) {
throw new IOException(_input + " : wrong partition");
} else if (errorCode != ErrorMapping.NoError()) {
throw new IOException(_input + " current offset=" + _offset
+ " error:" + errorCode);
} else
return false;
}
示例7: getOffsetOfTopicAndPartition
import kafka.common.ErrorMapping; //导入依赖的package包/类
/**
* 从保存consumer消费者offset偏移量的位置获取当前consumer对应的偏移量
*
* @param consumer 消费者
* @param groupId Group Id
* @param clientName client名称
* @param topic topic名称
* @param partitionID 分区id
*
* @return
*/
public long getOffsetOfTopicAndPartition(SimpleConsumer consumer, String groupId, String clientName, String
topic, int partitionID) {
TopicAndPartition topicAndPartition = new TopicAndPartition(topic, partitionID);
List<TopicAndPartition> requestInfo = new ArrayList<TopicAndPartition>();
requestInfo.add(topicAndPartition);
OffsetFetchRequest request = new OffsetFetchRequest(groupId, requestInfo, 0, clientName);
OffsetFetchResponse response = consumer.fetchOffsets(request);
// 获取返回值
Map<TopicAndPartition, OffsetMetadataAndError> returnOffsetMetadata = response.offsets();
// 处理返回值
if (returnOffsetMetadata != null && !returnOffsetMetadata.isEmpty()) {
// 获取当前分区对应的偏移量信息
OffsetMetadataAndError offset = returnOffsetMetadata.get(topicAndPartition);
if (offset.error().code() == ErrorMapping.NoError()) {
// 没有异常,表示是正常的,获取偏移量
return offset.offset();
} else {
// 当Consumer第一次连接的时候(zk中不在当前topic对应数据的时候),会产生UnknownTopicOrPartitionCode异常
System.out.println("Error fetching data Offset Data the Topic and Partition. Reason: " + offset.error
());
}
}
// 所有异常情况直接返回0
return 0;
}
示例8: getLastOffset
import kafka.common.ErrorMapping; //导入依赖的package包/类
/**
* Retrieves the last offset before the given timestamp for a given topic partition.
*
* @return The last offset before the given timestamp or {@code 0} if failed to do so.
*/
private long getLastOffset(TopicPartition topicPart, long timestamp) {
BrokerInfo brokerInfo = brokerService.getLeader(topicPart.getTopic(), topicPart.getPartition());
SimpleConsumer consumer = brokerInfo == null ? null : consumers.getUnchecked(brokerInfo);
// If no broker, treat it as failure attempt.
if (consumer == null) {
LOG.warn("Failed to talk to any broker. Default offset to 0 for {}", topicPart);
return 0L;
}
// Fire offset request
OffsetRequest request = new OffsetRequest(ImmutableMap.of(
new TopicAndPartition(topicPart.getTopic(), topicPart.getPartition()),
new PartitionOffsetRequestInfo(timestamp, 1)
), kafka.api.OffsetRequest.CurrentVersion(), consumer.clientId());
OffsetResponse response = consumer.getOffsetsBefore(request);
// Retrieve offsets from response
long[] offsets = response.hasError() ? null : response.offsets(topicPart.getTopic(), topicPart.getPartition());
if (offsets == null || offsets.length <= 0) {
short errorCode = response.errorCode(topicPart.getTopic(), topicPart.getPartition());
// If the topic partition doesn't exists, use offset 0 without logging error.
if (errorCode != ErrorMapping.UnknownTopicOrPartitionCode()) {
consumers.refresh(brokerInfo);
LOG.warn("Failed to fetch offset for {} with timestamp {}. Error: {}. Default offset to 0.",
topicPart, timestamp, errorCode);
}
return 0L;
}
LOG.debug("Offset {} fetched for {} with timestamp {}.", offsets[0], topicPart, timestamp);
return offsets[0];
}
示例9: checkNeedNewLeader
import kafka.common.ErrorMapping; //导入依赖的package包/类
@SuppressWarnings("squid:MethodCyclomaticComplexity")
private void checkNeedNewLeader(final short errorCode) {
LOG.warn("Error fetching data from the Broker: [{}:{}] Topic: {}-[{}:{}]@{} Error: {}", consumer.host(), consumer.port(), consumerGroup, topic, partitionId, currentOffset, errorCode);
boolean needNewLeader = false;
if (errorCode == ErrorMapping.OffsetOutOfRangeCode()) {
PartitionMetadata partitionMetadata = KafkaMetaData.getPartitionMetadata(consumer, Collections.singletonList(topic), partitionId);
if (partitionMetadata == null || !LeaderBrokerChecker.isSameBroker(
KafkaMetaData.findNewLeader(brokersList, partitionMetadata.leader(), topic, partitionId).leader(),
partitionMetadata.leader())) {
needNewLeader = true;
} else {
long earliestOffset = getEarliestOffset();
long latestOffset = getLatestOffset();
if (latestOffset < 0 || earliestOffset < 0)
needNewLeader = true;
else if (currentOffset > latestOffset)
throw new KafkaException("Offset Out of Higher Bound for [" + topic + ":" + partitionId + "@" + currentOffset + "] latest:" + latestOffset);
else if (currentOffset < earliestOffset)
throw new KafkaException("Offset Out of Lower Bound for [" + topic + ":" + partitionId + "@" + currentOffset + "] earliest:" + earliestOffset);
}
} else {
needNewLeader = true;
}
if (needNewLeader)
connect();
}
示例10: getConsumerOffsets
import kafka.common.ErrorMapping; //导入依赖的package包/类
/**
* Returns the map of partitionId to consumer offset for the given group and
* topic. Uses the given blocking channel to execute the offset fetch request.
*
* @param channel The channel to send requests on
* @param groupId Consumer group to use
* @param topic Topic to query
* @param zookeeperOffsets If true, use a version of the API that retrieves
* offsets from Zookeeper. Otherwise use a version
* that pulls the offsets from Kafka itself.
* @return Map where the key is partitionId and the value is the consumer
* offset for that partition.
*/
private Map<Integer, Long> getConsumerOffsets(BlockingChannel channel,
String groupId,
TopicVO topic,
boolean zookeeperOffsets)
{
final OffsetFetchRequest request = new OffsetFetchRequest(
groupId,
topic.getPartitions().stream()
.map(p -> new TopicAndPartition(topic.getName(), p.getId()))
.collect(Collectors.toList()),
(short) (zookeeperOffsets ? 0 : 1), 0, // version 0 = zookeeper offsets, 1 = kafka offsets
clientId());
LOG.debug("Sending consumer offset request: {}", request);
channel.send(request.underlying());
final kafka.api.OffsetFetchResponse underlyingResponse =
kafka.api.OffsetFetchResponse.readFrom(channel.receive().buffer());
LOG.debug("Received consumer offset response: {}", underlyingResponse);
OffsetFetchResponse response = new OffsetFetchResponse(underlyingResponse);
return response.offsets().entrySet().stream()
.filter(entry -> entry.getValue().error() == ErrorMapping.NoError())
.collect(Collectors.toMap(entry -> entry.getKey().partition(), entry -> entry.getValue().offset()));
}
示例11: offsetManagerBroker
import kafka.common.ErrorMapping; //导入依赖的package包/类
private Integer offsetManagerBroker(BlockingChannel channel, String groupId)
{
final ConsumerMetadataRequest request =
new ConsumerMetadataRequest(groupId, (short) 0, 0, clientId());
LOG.debug("Sending consumer metadata request: {}", request);
channel.send(request);
ConsumerMetadataResponse response =
ConsumerMetadataResponse.readFrom(channel.receive().buffer());
LOG.debug("Received consumer metadata response: {}", response);
return (response.errorCode() == ErrorMapping.NoError()) ? response.coordinator().id() : null;
}
示例12: getOffsetForPartition
import kafka.common.ErrorMapping; //导入依赖的package包/类
private long getOffsetForPartition(long whichTime) throws OffsetFetchException {
Map<TopicAndPartition, PartitionOffsetRequestInfo> requestInfo = new HashMap<>();
requestInfo.put(new TopicAndPartition(assignedTopicPartition.topic(), assignedTopicPartition.partition()), new PartitionOffsetRequestInfo(whichTime, 1));
kafka.javaapi.OffsetRequest request = new kafka.javaapi.OffsetRequest(
requestInfo, kafka.api.OffsetRequest.CurrentVersion(), partitionConsumer.clientId());
OffsetResponse response = partitionConsumer.getOffsetsBefore(request);
if (response.hasError()) {
short errorCode = response.errorCode(assignedTopicPartition.topic(), assignedTopicPartition.partition());
throw new OffsetFetchException("Error fetching data Offset Data the Broker. Error code: " + errorCode, ErrorMapping.exceptionFor(errorCode));
}
long[] offsets = response.offsets(assignedTopicPartition.topic(), assignedTopicPartition.partition());
return offsets[0];
}
示例13: checkLeader
import kafka.common.ErrorMapping; //导入依赖的package包/类
/**
* Check the leader.
*
* @param a_topic topic name
* @param a_partition partition number
* @param a_beginOffset begin offset
* @return boolean
*/
private boolean checkLeader(String a_topic, int a_partition,
long a_beginOffset) {
if (checkConsumer(a_topic, a_partition)) {
FetchRequest req = new FetchRequestBuilder()
.clientId(pool.getClientId())
.addFetch(a_topic, a_partition, a_beginOffset,
KafkaConstants.FETCH_SIZE).build();
fetchResponse = consumer.get().fetch(req);
String leadHost = metadata.leader().host();
if (fetchResponse.hasError()) {
// Something went wrong!
short code = fetchResponse.errorCode(a_topic, a_partition);
logger.error("Error fetching data from the Broker:" + leadHost
+ " Reason: " + code);
if (code == ErrorMapping.OffsetOutOfRangeCode()) {
// We asked for an invalid offset. For simple case ask for
// the last element to reset
a_beginOffset = getLatestOffset(a_topic, a_partition);
}
consumer.get().close();
consumer.set(null);
try {
metadata = findNewLeader(leadHost, a_topic, a_partition);
} catch (MQException e) {
logger.error("Find new leader failed.", e);
}
return false;
}
return true;
}
return false;
}
示例14: handlePartitionReaderException
import kafka.common.ErrorMapping; //导入依赖的package包/类
public void handlePartitionReaderException(PartitionReader reader,KafkaPartitionReaderException e){
if(e instanceof KafkaPartitionReaderException){
LOG.log("ErrorExcepiton:"+e.getCode());
LOG.log(ErrorMapping.exceptionFor(e.getCode()).getClass().getName());
try{
if (e.getCode() == ErrorMapping.OffsetOutOfRangeCode()) {
reader.resetOffset();
}else if(e.getCode()==ErrorMapping.NotLeaderForPartitionCode()){
reader.reinit();
}else if(e.getCode()==ErrorMapping.LeaderNotAvailableCode()){
try {
Thread.sleep(config.refreshLeaderBackoffMs());
} catch (InterruptedException e1) {
}
reader.reinit();
}else {
LOG.log("Un-predicated KafkaException:"+e.getCode());
reader.reinit();
}
}catch(Exception t){
LOG.log("Handle Kafka PartitionReader Exception.",e);
}
}else{
}
}
示例15: init
import kafka.common.ErrorMapping; //导入依赖的package包/类
private void init() {
//LOG.log("Start to Init PartitionReader, Topic["+topic+"] partition["+partition+"]");
if(consumer!=null){
//LOG.log("Start to Close PartitionReader, Topic["+topic+"] partition["+partition+"]");
consumer.close();
//LOG.log("Closed for PartitionReader, Topic["+topic+"] partition["+partition+"]");
}
int retryTimes=config.retryTimes();
do{//Fixed when topic partition only has on replicas, the broker down, we couldn't get the leader for this partitions.
this.leader =KafkaZKData.getBrokerInfo(zkConnector,
KafkaZKData.leaderForPartition(zkConnector, topic, partition));
if(this.leader==null) {
try{
Thread.sleep(config.sleepMsBetweenRetries());
}catch(Exception ignore){
}
}
retryTimes--;
}while(!Thread.currentThread().isInterrupted() && (retryTimes>0) && this.leader==null);
if(this.leader==null) throw new KafkaPartitionReaderException(ErrorMapping.LeaderNotAvailableCode(),"Leader not available for partition["+partition+"] topic["+topic+"]");
this.consumer = new SimpleConsumer(leader.host(), leader.port(),
config.socketTimeoutMs(),
config.socketReceiveBufferBytes(), clientId);
this.nextBatchSizeBytes = config.fetchMinBytes();//config.getBatchSizeBytes();
this.closed.set(false);
taken.set(true);
}