Skip to content

Commit

Permalink
[Bug] Add finally block to Kafka Offset Commit (#2662)
Browse files Browse the repository at this point in the history
* Allow a consumer to set the rebalancing strategy

* Add a try--finally to semaphore release

---------

Co-authored-by: Paul Reardon <Paul@ReardonTech.UK>
  • Loading branch information
iancooper and preardon authored May 11, 2023
1 parent 7ea5be7 commit 254c426
Showing 1 changed file with 26 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -436,28 +436,37 @@ private void CommitOffsets()

private void CommitAllOffsets(DateTime flushTime)
{
var listOffsets = new List<TopicPartitionOffset>();
var currentOffsetsInBag = _offsetStorage.Count;
for (int i = 0; i < currentOffsetsInBag; i++)
try
{
bool hasOffsets = _offsetStorage.TryTake(out var offset);
if (hasOffsets)
listOffsets.Add(offset);
else
break;


var listOffsets = new List<TopicPartitionOffset>();
var currentOffsetsInBag = _offsetStorage.Count;
for (int i = 0; i < currentOffsetsInBag; i++)
{
bool hasOffsets = _offsetStorage.TryTake(out var offset);
if (hasOffsets)
listOffsets.Add(offset);
else
break;

}

if (s_logger.IsEnabled(LogLevel.Information))
{
var offsets = listOffsets.Select(tpo =>
$"Topic: {tpo.Topic} Partition: {tpo.Partition.Value} Offset: {tpo.Offset.Value}");
var offsetAsString = string.Join(Environment.NewLine, offsets);
s_logger.LogInformation("Sweeping offsets: {0} {Offset}", Environment.NewLine, offsetAsString);
}

_consumer.Commit(listOffsets);
_lastFlushAt = flushTime;
}

if (s_logger.IsEnabled(LogLevel.Information))
finally
{
var offsets = listOffsets.Select(tpo => $"Topic: {tpo.Topic} Partition: {tpo.Partition.Value} Offset: {tpo.Offset.Value}");
var offsetAsString = string.Join(Environment.NewLine, offsets);
s_logger.LogInformation("Sweeping offsets: {0} {Offset}", Environment.NewLine, offsetAsString);
_flushToken.Release(1);
}

_consumer.Commit(listOffsets);
_lastFlushAt = flushTime;
_flushToken.Release(1);
}

// The batch size has been exceeded, so flush our offsets
Expand Down

0 comments on commit 254c426

Please sign in to comment.