Skip to content

Commit 458edff

Browse files
dev: handle rpc exceptions in reader
1 parent 20d3c38 commit 458edff

File tree

3 files changed

+53
-44
lines changed

3 files changed

+53
-44
lines changed

src/Ydb.Sdk/src/Services/Topic/Reader/Message.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,7 @@ public class BatchMessage<TValue>
5959

6060
public ImmutableArray<Message<TValue>> Batch { get; }
6161

62-
internal BatchMessage(
63-
ImmutableArray<Message<TValue>> batch,
64-
ReaderSession readerSession)
62+
internal BatchMessage(ImmutableArray<Message<TValue>> batch, ReaderSession readerSession)
6563
{
6664
Batch = batch;
6765
_readerSession = readerSession;

src/Ydb.Sdk/src/Services/Topic/Reader/Reader.cs

Lines changed: 51 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
using System.Collections.Concurrent;
22
using System.Collections.Immutable;
3-
using System.Net.Sockets;
43
using System.Threading.Channels;
5-
using Google.Protobuf;
6-
using Google.Protobuf.Collections;
74
using Google.Protobuf.WellKnownTypes;
85
using Microsoft.Extensions.Logging;
96
using Ydb.Topic;
@@ -310,46 +307,62 @@ await Stream.Write(new MessageFromClient
310307
}
311308
catch (Driver.TransportException e)
312309
{
310+
Logger.LogError(e, "ReaderSession[{SessionId}] have transport error on Commit", SessionId);
311+
313312
ReconnectSession();
314313
}
315314
});
316315

317-
while (await Stream.MoveNextAsync())
316+
try
318317
{
319-
switch (Stream.Current.ServerMessageCase)
318+
while (await Stream.MoveNextAsync())
320319
{
321-
case ServerMessageOneofCase.ReadResponse:
322-
await HandleReadResponse();
323-
break;
324-
case ServerMessageOneofCase.StartPartitionSessionRequest:
325-
var startPartitionSessionRequest = Stream.Current.StartPartitionSessionRequest;
326-
var partitionSession = startPartitionSessionRequest.PartitionSession;
327-
328-
_partitionSessions[partitionSession.PartitionSessionId] = new PartitionSession(
329-
partitionSession.PartitionSessionId,
330-
partitionSession.Path,
331-
partitionSession.PartitionId,
332-
startPartitionSessionRequest.CommittedOffset
333-
);
334-
break;
335-
case ServerMessageOneofCase.CommitOffsetResponse:
336-
// foreach (var offset in Stream.Current.CommitOffsetResponse.PartitionsCommittedOffsets)
337-
// {
338-
// offset.CommittedOffset;
339-
// offset.PartitionSessionId;
340-
// }
341-
342-
break;
343-
case ServerMessageOneofCase.PartitionSessionStatusResponse:
344-
case ServerMessageOneofCase.UpdateTokenResponse:
345-
case ServerMessageOneofCase.StopPartitionSessionRequest:
346-
case ServerMessageOneofCase.InitResponse:
347-
case ServerMessageOneofCase.None:
348-
break;
349-
default:
350-
throw new ArgumentOutOfRangeException();
320+
switch (Stream.Current.ServerMessageCase)
321+
{
322+
case ServerMessageOneofCase.ReadResponse:
323+
await HandleReadResponse();
324+
break;
325+
case ServerMessageOneofCase.StartPartitionSessionRequest:
326+
var startPartitionSessionRequest = Stream.Current.StartPartitionSessionRequest;
327+
var partitionSession = startPartitionSessionRequest.PartitionSession;
328+
329+
_partitionSessions[partitionSession.PartitionSessionId] = new PartitionSession(
330+
partitionSession.PartitionSessionId,
331+
partitionSession.Path,
332+
partitionSession.PartitionId,
333+
startPartitionSessionRequest.CommittedOffset
334+
);
335+
336+
Logger.LogInformation("ReaderSession[{SessionId}] started PartitionSession[]", SessionId);
337+
break;
338+
case ServerMessageOneofCase.CommitOffsetResponse:
339+
// foreach (var offset in Stream.Current.CommitOffsetResponse.PartitionsCommittedOffsets)
340+
// {
341+
// offset.CommittedOffset;
342+
// offset.PartitionSessionId;
343+
// }
344+
345+
break;
346+
case ServerMessageOneofCase.PartitionSessionStatusResponse:
347+
case ServerMessageOneofCase.UpdateTokenResponse:
348+
case ServerMessageOneofCase.StopPartitionSessionRequest:
349+
case ServerMessageOneofCase.InitResponse:
350+
case ServerMessageOneofCase.None:
351+
break;
352+
default:
353+
throw new ArgumentOutOfRangeException();
354+
}
351355
}
352356
}
357+
catch (Driver.TransportException e)
358+
{
359+
Logger.LogError(e, "ReaderSession[{SessionId}] have transport error on processing server messages",
360+
SessionId);
361+
}
362+
finally
363+
{
364+
ReconnectSession();
365+
}
353366
}
354367

355368
public async Task<TopicPartitionOffset> CommitOffsetRange(OffsetsRange offsetsRange, long partitionId)
@@ -367,7 +380,7 @@ private async Task HandleReadResponse()
367380

368381
Interlocked.Add(ref _memoryUsageMaxBytes, -readResponse.BytesSize);
369382
var readResponsesInBatch = 0;
370-
383+
371384
foreach (var partition in readResponse.PartitionData)
372385
{
373386
var partitionSessionId = partition.PartitionSessionId;
@@ -376,7 +389,7 @@ private async Task HandleReadResponse()
376389
{
377390
var startOffsetBatch = partitionSession.CommitedOffset;
378391
var endOffsetBatch = partitionSession.CommitedOffset;
379-
392+
380393
var batch = partition.Batches;
381394
for (var i = 0; i < partition.Batches.Count; i++)
382395
{
@@ -386,7 +399,7 @@ private async Task HandleReadResponse()
386399
foreach (var messageData in batch[i].MessageData)
387400
{
388401
actuallySummaryBatchPayload += messageData.Data.Length;
389-
402+
390403
internalBatchMessages.Enqueue(new InternalMessage(
391404
data: messageData.Data,
392405
topic: partitionSession.TopicPath,

src/Ydb.Sdk/src/Services/Topic/TopicSession.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ internal abstract class TopicSession<TFromClient, TFromServer> : IDisposable
1212

1313
private int _isActive = 1;
1414

15-
public bool IsActive => Volatile.Read(ref _isActive) == 1;
16-
1715
protected TopicSession(
1816
IBidirectionalStream<TFromClient, TFromServer> stream,
1917
ILogger logger,
@@ -26,7 +24,7 @@ protected TopicSession(
2624
_initialize = initialize;
2725
}
2826

29-
internal bool IsActive => Volatile.Read(ref _isActive) == 1;
27+
public bool IsActive => Volatile.Read(ref _isActive) == 1;
3028

3129
protected async void ReconnectSession()
3230
{

0 commit comments

Comments
 (0)