Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip empty checkpoints #18059

Merged
merged 2 commits into from
Jan 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -585,43 +585,11 @@ public async Task ListCheckpointsUsesOffsetAsTheStartingPositionWhenPresentInLeg
/// Verifies basic functionality of ListCheckpointsAsync and ensures the starting position is set correctly.
/// </summary>
///
[Test]
public async Task ListCheckpointsUsesSequenceNumberAsTheStartingPositionWhenNoOffsetIsPresentInLegacyCheckpoint()
{
var blobList = new List<BlobItem>
{
BlobsModelFactory.BlobItem($"{FullyQualifiedNamespace}/{EventHubName}/{ConsumerGroup}/0",
false,
BlobsModelFactory.BlobItemProperties(true, lastModified: DateTime.UtcNow, eTag: new ETag(MatchingEtag)),
"snapshot")
};

var containerClient = new MockBlobContainerClient() { Blobs = blobList };
containerClient.AddBlobClient($"{FullyQualifiedNamespace}/{EventHubName}/{ConsumerGroup}/0", client =>
{
client.Content = Encoding.UTF8.GetBytes("{" +
"\"PartitionId\":\"0\"," +
"\"Owner\":\"681d365b-de1b-4288-9733-76294e17daf0\"," +
"\"Token\":\"2d0c4276-827d-4ca4-a345-729caeca3b82\"," +
"\"Epoch\":386," +
"\"SequenceNumber\":960180" +
"}");
});

var target = new BlobsCheckpointStore(containerClient, new BasicRetryPolicy(new EventHubsRetryOptions()), initializeWithLegacyCheckpoints: true);
var checkpoints = await target.ListCheckpointsAsync(FullyQualifiedNamespace, EventHubName, ConsumerGroup, new CancellationToken());

Assert.That(checkpoints, Is.Not.Null, "A set of checkpoints should have been returned.");
Assert.That(checkpoints.Single().StartingPosition, Is.EqualTo(EventPosition.FromSequenceNumber(960180, false)));
Assert.That(checkpoints.Single().PartitionId, Is.EqualTo("0"));
}

/// <summary>
/// Verifies basic functionality of ListCheckpointsAsync and ensures the starting position is set correctly.
/// </summary>
///
[Test]
public async Task ListCheckpointsUsesSequenceNumberAsTheStartingPositionWhenOffsetIsNullInLegacyCheckpoint()
[TestCase("null", "0")]
[TestCase("null", "78")]
[TestCase("\"\"", "0")]
[TestCase("\"\"", "78")]
public async Task ListCheckpointSkipsCheckpointsWhenOffsetIsNullOrEmptyInLegacyCheckpoint(string offset, string sequenceNumber)
{
var blobList = new List<BlobItem>
{
Expand All @@ -635,8 +603,8 @@ public async Task ListCheckpointsUsesSequenceNumberAsTheStartingPositionWhenOffs
containerClient.AddBlobClient($"{FullyQualifiedNamespace}/{EventHubName}/{ConsumerGroup}/0", client =>
{
client.Content = Encoding.UTF8.GetBytes("{" +
"\"Offset\":null," +
"\"SequenceNumber\":0," +
"\"Offset\":" + offset + "," +
"\"SequenceNumber\":" + sequenceNumber + "," +
"\"PartitionId\":\"8\"," +
"\"Owner\":\"cc397fe0-6771-4eaa-a8df-1997efeb3c87\"," +
"\"Token\":\"ab00a395-4c39-4939-89d5-10c04b4553af\"," +
Expand All @@ -648,8 +616,7 @@ public async Task ListCheckpointsUsesSequenceNumberAsTheStartingPositionWhenOffs
var checkpoints = await target.ListCheckpointsAsync(FullyQualifiedNamespace, EventHubName, ConsumerGroup, new CancellationToken());

Assert.That(checkpoints, Is.Not.Null, "A set of checkpoints should have been returned.");
Assert.That(checkpoints.Single().StartingPosition, Is.EqualTo(EventPosition.FromSequenceNumber(0, false)));
Assert.That(checkpoints.Single().PartitionId, Is.EqualTo("0"));
Assert.That(checkpoints, Is.Empty, "No valid checkpoints should exist.");
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -370,18 +370,20 @@ async Task<List<EventProcessorCheckpoint>> listLegacyCheckpointsAsync(List<Event
using var memoryStream = new MemoryStream();
await blobClient.DownloadToAsync(memoryStream, listCheckpointsToken).ConfigureAwait(false);

TryReadLegacyCheckpoint(
if (TryReadLegacyCheckpoint(
memoryStream.GetBuffer().AsSpan(0, (int)memoryStream.Length),
out long? offset,
out long? sequenceNumber);

if (offset.HasValue)
{
startingPosition = EventPosition.FromOffset(offset.Value, false);
}
else if (sequenceNumber.HasValue)
out long? sequenceNumber))
{
startingPosition = EventPosition.FromSequenceNumber(sequenceNumber.Value, false);
if (offset.HasValue)
{
startingPosition = EventPosition.FromOffset(offset.Value, false);
}
else
{
// Skip checkpoints without an offset without logging an error
continue;
}
}

if (startingPosition.HasValue)
Expand Down Expand Up @@ -598,16 +600,18 @@ async Task wrapper(CancellationToken token)
/// "SequenceNumber":960180
/// }
/// /// </remarks>
private static void TryReadLegacyCheckpoint(Span<byte> data, out long? offset, out long? sequenceNumber)
private static bool TryReadLegacyCheckpoint(Span<byte> data, out long? offset, out long? sequenceNumber)
{
offset = null;
sequenceNumber = null;

var hadOffset = false;
var jsonReader = new Utf8JsonReader(data);

try
{
if (!jsonReader.Read() || jsonReader.TokenType != JsonTokenType.StartObject) return;
if (!jsonReader.Read() || jsonReader.TokenType != JsonTokenType.StartObject)
return false;

while (jsonReader.Read() && jsonReader.TokenType == JsonTokenType.PropertyName)
{
Expand All @@ -617,19 +621,21 @@ private static void TryReadLegacyCheckpoint(Span<byte> data, out long? offset, o

if (!jsonReader.Read())
{
return;
return false;
}

hadOffset = true;

var offsetString = jsonReader.GetString();
if (offsetString != null)
if (!string.IsNullOrEmpty(offsetString))
{
if (long.TryParse(offsetString, out long offsetValue))
{
offset = offsetValue;
}
else
{
return;
return false;
}
}

Expand All @@ -638,7 +644,7 @@ private static void TryReadLegacyCheckpoint(Span<byte> data, out long? offset, o
if (!jsonReader.Read() ||
!jsonReader.TryGetInt64(out long sequenceNumberValue))
{
return;
return false;
}

sequenceNumber = sequenceNumberValue;
Expand All @@ -652,7 +658,10 @@ private static void TryReadLegacyCheckpoint(Span<byte> data, out long? offset, o
catch (JsonException)
{
// Ignore this because if the data is malformed, it will be treated as if the checkpoint didn't exist.
return false;
}

return hadOffset && sequenceNumber != null;
}

/// <summary>
Expand Down