Skip to content

Commit

Permalink
Ensured flush token is released if offset commit fails. (#2860)
Browse files Browse the repository at this point in the history
  • Loading branch information
Luke S. Phillips authored Oct 8, 2023
1 parent d04c3f6 commit 9cc5cbc
Showing 1 changed file with 27 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -411,27 +411,33 @@ public int StoredOffsets()
/// </summary>
private void CommitOffsets()
{

var listOffsets = new List<TopicPartitionOffset>();
for (int i = 0; i < _maxBatchSize; 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("Commiting offsets: {0} {Offset}", Environment.NewLine, offsetAsString);
}

_consumer.Commit(listOffsets);
_flushToken.Release(1);
try
{
var listOffsets = new List<TopicPartitionOffset>();
for (int i = 0; i < _maxBatchSize; 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("Commiting offsets: {0} {Offset}", Environment.NewLine, offsetAsString);
}

_consumer.Commit(listOffsets);
}
finally
{
_flushToken.Release(1);
}
}

private void CommitAllOffsets(DateTime flushTime)
Expand Down

0 comments on commit 9cc5cbc

Please sign in to comment.