Skip to content
This repository was archived by the owner on Jul 15, 2023. It is now read-only.

Commit 186b9cd

Browse files
author
Som Sahu
committed
1) Fix log loss scenario during rebalance, 2) use api version 0 instead of
1, 3) Some minor performance improvement and bug fixes
1 parent 0398697 commit 186b9cd

5 files changed

Lines changed: 14 additions & 55 deletions

File tree

src/KafkaNET.Library/Consumers/ConsumerIterator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ private TData MakeNext()
247247
currentTopicInfo = currentDataChunk.TopicInfo;
248248
Logger.DebugFormat("CurrentTopicInfo: ConsumedOffset({0}), FetchOffset({1})",
249249
currentTopicInfo.ConsumeOffset, currentTopicInfo.FetchOffset);
250-
if (currentTopicInfo.ConsumeOffset != currentDataChunk.FetchOffset)
250+
if (currentTopicInfo.FetchOffset < currentDataChunk.FetchOffset)
251251
{
252252
Logger.ErrorFormat("consumed offset: {0} doesn't match fetch offset: {1} for {2}; consumer may lose data",
253253
currentTopicInfo.ConsumeOffset,

src/KafkaNET.Library/Consumers/PartitionTopicInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,8 +232,8 @@ public int Add(BufferedMessageSet messages)
232232

233233
Logger.InfoFormat("{2} : Updating fetch offset = {0} with value = {1}", this.fetchedOffset, offset, this.PartitionId);
234234
this.chunkQueue.Add(new FetchedDataChunk(messages, this, this.fetchedOffset));
235-
long newOffset = Interlocked.Exchange(ref this.fetchedOffset, offset);
236-
Logger.Debug("Updated fetch offset of " + this + " to " + newOffset);
235+
Interlocked.Exchange(ref this.fetchedOffset, offset);
236+
Logger.Debug("Updated fetch offset of " + this + " to " + offset);
237237
}
238238

239239
return size;

src/KafkaNET.Library/Messages/BufferedMessageSet.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ private MessageAndOffset MakeNextOuter()
316316
return AllDone();
317317
}
318318

319-
Message newMessage = this.Messages.ToList()[topIterPosition];
319+
Message newMessage = this.Messages.ElementAt(topIterPosition);
320320
lastMessageSize = newMessage.Size;
321321
topIterPosition++;
322322
switch (newMessage.CompressionCodec)

src/KafkaNET.Library/Requests/FetchRequest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public class FetchRequest : AbstractRequest, IWritable
5656
public const byte DefaultMinBytesSize = 4;
5757
public const byte DefaultOffsetInfoSizeSize = 4;
5858

59-
public const short CurrentVersion = 1;
59+
public const short CurrentVersion = 0;
6060

6161
public FetchRequest(int correlationId, string clientId, int maxWait, int minBytes, Dictionary<string, List<PartitionFetchInfo>> fetchInfos)
6262
{

src/KafkaNET.Library/ZooKeeperIntegration/Listeners/ZKRebalancerListener.cs

Lines changed: 9 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -496,34 +496,19 @@ private void AddPartitionTopicInfo(ZKGroupTopicDirs topicDirs, string partition,
496496
}
497497
var leader = leaderOpt.Value;
498498
var znode = topicDirs.ConsumerOffsetDir + "/" + partition;
499-
var offsetString = this.zkClient.ReadData<string>(znode, true);
499+
500+
var offsetCommitedString = this.zkClient.ReadData<string>(znode, true);
500501

501502
//if first time starting a consumer, set the initial offset based on the config
502-
long offset = 0;
503-
long offsetCommited = 0;
504-
if (offsetString == null)
505-
{
506-
switch (config.AutoOffsetReset)
507-
{
508-
case OffsetRequest.SmallestTime:
509-
offset = this.EarliestOrLatestOffset(topic, leader, partitionId, OffsetRequest.EarliestTime);
510-
break;
511-
case OffsetRequest.LargestTime:
512-
offset = this.EarliestOrLatestOffset(topic, leader, partitionId, OffsetRequest.LatestTime);
513-
break;
514-
default:
515-
throw new ConfigurationErrorsException("Wrong value in autoOffsetReset in ConsumerConfig");
516-
}
517-
offsetCommited = Math.Max(offset - 1, 0);
518-
}
519-
else
503+
long offset = -1;
504+
long offsetCommited = -1;
505+
if (offsetCommitedString != null)
520506
{
521-
offsetCommited = long.Parse(offsetString);
522-
long latestOffset = this.EarliestOrLatestOffset(topic, leader, partitionId, OffsetRequest.LatestTime);
523-
offset = Math.Min(offsetCommited + 1, latestOffset);
524-
Logger.InfoFormat("Final offset {0} for topic {1} partition {2} OffsetCommited {3} latestOffset {4}"
525-
, offset, topic, partition, offsetCommited, latestOffset);
507+
offsetCommited = long.Parse(offsetCommitedString);
508+
offset = offsetCommited + 1;
526509
}
510+
Logger.InfoFormat("Final offset {0} for topic {1} partition {2} OffsetCommited {3}"
511+
, offset, topic, partition, offsetCommited);
527512

528513
var queue = this.queues[new Tuple<string, string>(topic, consumerThreadId)];
529514
var partTopicInfo = new PartitionTopicInfo(
@@ -540,32 +525,6 @@ private void AddPartitionTopicInfo(ZKGroupTopicDirs topicDirs, string partition,
540525
Logger.InfoFormat("{0} selected new offset {1}", partTopicInfo, offset);
541526
}
542527

543-
private long EarliestOrLatestOffset(string topic, int brokerId, int partitionId, long earliestIoLatest)
544-
{
545-
Consumer consumer = null;
546-
long producedOffset = -1;
547-
try
548-
{
549-
var cluster = new Cluster(this.zkClient);
550-
var broker = cluster.GetBroker(brokerId);
551-
if (broker == null)
552-
{
553-
throw new IllegalStateException(string.Format("Broker {0} is unavailable. Cannot issue GetOffsetsBefore request", brokerId));
554-
}
555-
consumer = new Consumer(this.config, broker.Host, broker.Port);
556-
var requestInfos = new Dictionary<string, List<PartitionOffsetRequestInfo>>();
557-
requestInfos[topic] = new List<PartitionOffsetRequestInfo>() { new PartitionOffsetRequestInfo(partitionId, earliestIoLatest, 1) };
558-
var offsets = consumer.GetOffsetsBefore(new OffsetRequest(requestInfos));
559-
560-
producedOffset = offsets.ResponseMap[topic].First().Offsets[0];
561-
}
562-
catch (Exception ex)
563-
{
564-
Logger.ErrorFormat("error in EarliestOrLatestOffset() : {0}", ex.FormatException());
565-
}
566-
return producedOffset;
567-
}
568-
569528
private void ReleasePartitionOwnership(IDictionary<string, IList<string>> topicThreadIdsMap)
570529
{
571530

0 commit comments

Comments
 (0)