-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: supported Commit() & ReadAsync() in TopicReader (#265)
- Loading branch information
1 parent
943f8a0
commit ab8fb03
Showing
10 changed files
with
525 additions
and
373 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
src/Ydb.Sdk/src/Services/Topic/Reader/InternalBatchMessages.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
using System.Collections.Immutable; | ||
using System.Diagnostics.CodeAnalysis; | ||
using Ydb.Topic; | ||
|
||
namespace Ydb.Sdk.Services.Topic.Reader; | ||
|
||
internal class InternalBatchMessages<TValue> | ||
{ | ||
private readonly StreamReadMessage.Types.ReadResponse.Types.Batch _batch; | ||
private readonly PartitionSession _partitionSession; | ||
private readonly IDeserializer<TValue> _deserializer; | ||
private readonly ReaderSession<TValue> _readerSession; | ||
private readonly long _approximatelyBatchSize; | ||
|
||
private int _startMessageDataIndex; | ||
|
||
private int OriginalMessageCount => _batch.MessageData.Count; | ||
private bool IsActive => _startMessageDataIndex < OriginalMessageCount && _readerSession.IsActive; | ||
|
||
public InternalBatchMessages( | ||
StreamReadMessage.Types.ReadResponse.Types.Batch batch, | ||
PartitionSession partitionsSession, | ||
ReaderSession<TValue> readerSession, | ||
long approximatelyBatchSize, | ||
IDeserializer<TValue> deserializer) | ||
{ | ||
_batch = batch; | ||
_partitionSession = partitionsSession; | ||
_readerSession = readerSession; | ||
_deserializer = deserializer; | ||
_approximatelyBatchSize = approximatelyBatchSize; | ||
} | ||
|
||
internal bool TryDequeueMessage([MaybeNullWhen(false)] out Message<TValue> message) | ||
{ | ||
if (!IsActive) | ||
{ | ||
message = default; | ||
return false; | ||
} | ||
|
||
var index = _startMessageDataIndex++; | ||
var approximatelyMessageBytesSize = Utils | ||
.CalculateApproximatelyBytesSize(_approximatelyBatchSize, OriginalMessageCount, index); | ||
var messageData = _batch.MessageData[index]; | ||
|
||
TValue value; | ||
try | ||
{ | ||
value = _deserializer.Deserialize(messageData.Data.ToByteArray()); | ||
} | ||
catch (Exception e) | ||
{ | ||
throw new ReaderException("Error when deserializing message data", e); | ||
} | ||
|
||
_readerSession.TryReadRequestBytes(approximatelyMessageBytesSize); | ||
var nextCommitedOffset = messageData.Offset + 1; | ||
|
||
message = new Message<TValue>( | ||
data: value, | ||
topic: _partitionSession.TopicPath, | ||
partitionId: _partitionSession.PartitionId, | ||
partitionSessionId: _partitionSession.PartitionSessionId, | ||
producerId: _batch.ProducerId, | ||
createdAt: messageData.CreatedAt.ToDateTime(), | ||
metadata: messageData.MetadataItems.Select(item => new Metadata(item.Key, item.Value.ToByteArray())) | ||
.ToImmutableArray(), | ||
offsetsRange: new OffsetsRange | ||
{ Start = _partitionSession.PrevEndOffsetMessage, End = nextCommitedOffset }, | ||
readerSession: _readerSession | ||
); | ||
_partitionSession.PrevEndOffsetMessage = nextCommitedOffset; | ||
|
||
return true; | ||
} | ||
|
||
internal bool TryPublicBatch([MaybeNullWhen(false)] out BatchMessages<TValue> batchMessages) | ||
{ | ||
if (!IsActive) | ||
{ | ||
batchMessages = default; | ||
return false; | ||
} | ||
|
||
var nextCommitedOffset = _batch.MessageData.Last().Offset + 1; | ||
var offsetsRangeBatch = new OffsetsRange | ||
{ Start = _partitionSession.PrevEndOffsetMessage, End = nextCommitedOffset }; | ||
_partitionSession.PrevEndOffsetMessage = nextCommitedOffset; | ||
|
||
var messages = new List<Message<TValue>>(); | ||
while (TryDequeueMessage(out var message)) | ||
{ | ||
messages.Add(message); | ||
} | ||
|
||
batchMessages = new BatchMessages<TValue>( | ||
batch: messages, | ||
readerSession: _readerSession, | ||
offsetsRange: offsetsRangeBatch, | ||
partitionSessionId: _partitionSession.PartitionSessionId | ||
); | ||
|
||
return true; | ||
} | ||
} | ||
|
||
internal record CommitSending( | ||
OffsetsRange OffsetsRange, | ||
TaskCompletionSource TcsCommit | ||
); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.