|
| 1 | +using System.Collections.Immutable; |
| 2 | +using System.Diagnostics.CodeAnalysis; |
| 3 | +using Google.Protobuf; |
| 4 | +using Google.Protobuf.Collections; |
| 5 | +using Google.Protobuf.WellKnownTypes; |
| 6 | +using Ydb.Topic; |
| 7 | + |
| 8 | +namespace Ydb.Sdk.Services.Topic.Reader; |
| 9 | + |
| 10 | +internal class InternalBatchMessages |
| 11 | +{ |
| 12 | + public InternalBatchMessages( |
| 13 | + ByteString data, |
| 14 | + string topic, |
| 15 | + long partitionId, |
| 16 | + string producerId, |
| 17 | + OffsetsRange offsetsRange, |
| 18 | + Timestamp createdAt, |
| 19 | + RepeatedField<MetadataItem> metadataItems, |
| 20 | + long approximatelyBytesSize) |
| 21 | + { |
| 22 | + Data = data; |
| 23 | + Topic = topic; |
| 24 | + PartitionId = partitionId; |
| 25 | + ProducerId = producerId; |
| 26 | + OffsetsRange = offsetsRange; |
| 27 | + CreatedAt = createdAt; |
| 28 | + MetadataItems = metadataItems; |
| 29 | + ApproximatelyBytesSize = approximatelyBytesSize; |
| 30 | + } |
| 31 | + |
| 32 | + private ByteString Data { get; } |
| 33 | + private string Topic { get; } |
| 34 | + private long PartitionId { get; } |
| 35 | + private string ProducerId { get; } |
| 36 | + private OffsetsRange OffsetsRange { get; } |
| 37 | + private Timestamp CreatedAt { get; } |
| 38 | + private RepeatedField<MetadataItem> MetadataItems { get; } |
| 39 | + private long ApproximatelyBytesSize { get; } |
| 40 | + |
| 41 | + internal Message<TValue> ToPublicMessage<TValue>(IDeserializer<TValue> deserializer, |
| 42 | + ReaderSession<TValue> readerSession) |
| 43 | + { |
| 44 | + readerSession.TryReadRequestBytes(ApproximatelyBytesSize); |
| 45 | + |
| 46 | + return new Message<TValue>( |
| 47 | + data: deserializer.Deserialize(Data.ToByteArray()), |
| 48 | + topic: Topic, |
| 49 | + partitionId: PartitionId, |
| 50 | + producerId: ProducerId, |
| 51 | + createdAt: CreatedAt.ToDateTime(), |
| 52 | + metadata: MetadataItems.Select(item => new Metadata(item.Key, item.Value.ToByteArray())).ToImmutableArray(), |
| 53 | + offsetsRange: OffsetsRange, |
| 54 | + readerSession: readerSession, |
| 55 | + approximatelyBytesSize: ApproximatelyBytesSize |
| 56 | + ); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +internal class InternalBatchMessages<TValue> |
| 61 | +{ |
| 62 | + private readonly StreamReadMessage.Types.ReadResponse.Types.Batch _batch; |
| 63 | + private readonly PartitionSession _partitionSession; |
| 64 | + private readonly IDeserializer<TValue> _deserializer; |
| 65 | + private readonly ReaderSession<TValue> _readerSession; |
| 66 | + private readonly long _approximatelyBatchSizeOriginal; |
| 67 | + |
| 68 | + private int _startMessageDataIndex = 0; |
| 69 | + private long _approximatelyBatchSize; |
| 70 | + |
| 71 | + private int OriginalMessageCount => _batch.MessageData.Count; |
| 72 | + |
| 73 | + internal bool IsActive => _startMessageDataIndex < OriginalMessageCount && _readerSession.IsActive; |
| 74 | + |
| 75 | + public InternalBatchMessages( |
| 76 | + StreamReadMessage.Types.ReadResponse.Types.Batch batch, |
| 77 | + PartitionSession partitionsSession, |
| 78 | + ReaderSession<TValue> readerSession, |
| 79 | + long approximatelyBatchSize, |
| 80 | + IDeserializer<TValue> deserializer) |
| 81 | + { |
| 82 | + _batch = batch; |
| 83 | + _partitionSession = partitionsSession; |
| 84 | + _readerSession = readerSession; |
| 85 | + _deserializer = deserializer; |
| 86 | + _approximatelyBatchSizeOriginal = approximatelyBatchSize; |
| 87 | + _approximatelyBatchSize = approximatelyBatchSize; |
| 88 | + } |
| 89 | + |
| 90 | + internal bool TryDequeueMessage([MaybeNullWhen(false)] out Message<TValue> message) |
| 91 | + { |
| 92 | + if (!IsActive) |
| 93 | + { |
| 94 | + message = default; |
| 95 | + return false; |
| 96 | + } |
| 97 | + |
| 98 | + var index = _startMessageDataIndex++; |
| 99 | + var approximatelyMessageBytesSize = Utils |
| 100 | + .CalculateApproximatelyBytesSize(_approximatelyBatchSizeOriginal, OriginalMessageCount, index); |
| 101 | + var messageData = _batch.MessageData[index]; |
| 102 | + |
| 103 | + TValue value; |
| 104 | + try |
| 105 | + { |
| 106 | + value = _deserializer.Deserialize(messageData.Data.ToByteArray()); |
| 107 | + } |
| 108 | + catch (Exception e) |
| 109 | + { |
| 110 | + throw new ReaderException("Error when deserializing message data", e); |
| 111 | + } |
| 112 | + |
| 113 | + _approximatelyBatchSize -= approximatelyMessageBytesSize; |
| 114 | + |
| 115 | + message = new Message<TValue>( |
| 116 | + value, |
| 117 | + _partitionSession.TopicPath, |
| 118 | + _partitionSession.PartitionId, |
| 119 | + _batch.ProducerId, |
| 120 | + messageData.CreatedAt.ToDateTime(), |
| 121 | + messageData.MetadataItems.Select(item => new Metadata(item.Key, item.Value.ToByteArray())) |
| 122 | + .ToImmutableArray(), |
| 123 | + new OffsetsRange { Start = _partitionSession.PrevEndOffsetMessage, End = messageData.Offset }, |
| 124 | + _readerSession, |
| 125 | + approximatelyMessageBytesSize |
| 126 | + ); |
| 127 | + _partitionSession.PrevEndOffsetMessage = messageData.Offset + 1; |
| 128 | + |
| 129 | + return true; |
| 130 | + } |
| 131 | + |
| 132 | + internal bool TryPublicBatch([MaybeNullWhen(false)] out BatchMessages<TValue> batchMessages) |
| 133 | + { |
| 134 | + if (!IsActive) |
| 135 | + { |
| 136 | + batchMessages = default; |
| 137 | + return false; |
| 138 | + } |
| 139 | + |
| 140 | + var offsetsRangeBatch = new OffsetsRange |
| 141 | + { Start = _partitionSession.PrevEndOffsetMessage, End = _batch.MessageData.Last().Offset }; |
| 142 | + var approximatelyBatchSize = _approximatelyBatchSize; |
| 143 | + |
| 144 | + var messages = new List<Message<TValue>>(); |
| 145 | + while (TryDequeueMessage(out var message)) |
| 146 | + { |
| 147 | + messages.Add(message); |
| 148 | + } |
| 149 | + |
| 150 | + batchMessages = new BatchMessages<TValue>(messages, _readerSession, approximatelyBatchSize, offsetsRangeBatch); |
| 151 | + |
| 152 | + return true; |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +internal record CommitSending( |
| 157 | + OffsetsRange OffsetsRange, |
| 158 | + long PartitionSessionId, |
| 159 | + TaskCompletionSource TcsCommit, |
| 160 | + long ApproximatelyBytesSize |
| 161 | +); |
0 commit comments