Skip to content

Handle closed connections in stream reader (backport #3397) #3412

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

Closed
wants to merge 3 commits into from
Closed
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
166 changes: 121 additions & 45 deletions deps/rabbitmq_stream/src/rabbit_stream_reader.erl
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,7 @@ open(cast,
} = StatemData)
when Offset > -1 ->
{Connection1, State1} =
<<<<<<< HEAD
case maps:get(StreamName, StreamSubscriptions, undefined) of
undefined ->
rabbit_log:debug("Stream protocol connection: osiris offset event for ~p, but no subscription (leftover messages after unsubscribe?)",
Expand Down Expand Up @@ -969,6 +970,54 @@ open(cast,
=
Credit1}
end
=======
case maps:get(StreamName, StreamSubscriptions, undefined) of
undefined ->
rabbit_log:debug("Stream protocol connection: osiris offset event for ~p, but no subscription (leftover messages after unsubscribe?)",
[StreamName]),
{Connection, State};
[] ->
rabbit_log:debug("Stream protocol connection: osiris offset event for ~p, but no registered consumers!",
[StreamName]),
{Connection#stream_connection{stream_subscriptions =
maps:remove(StreamName,
StreamSubscriptions)},
State};
CorrelationIds when is_list(CorrelationIds) ->
Consumers1 =
lists:foldl(fun(CorrelationId, ConsumersAcc) ->
#{CorrelationId := Consumer} = ConsumersAcc,
#consumer{credit = Credit} = Consumer,
Consumer1 =
case Credit of
0 ->
Consumer;
_ ->
case send_chunks(Transport,
Consumer,
SendFileOct)
of
{error, closed} ->
rabbit_log_connection:info("Stream protocol connection has been closed by peer",
[]),
throw({stop, normal});
{error, Reason} ->
rabbit_log_connection:info("Error while sending chunks: ~p",
[Reason]),
%% likely a connection problem
Consumer;
{{segment, Segment1},
{credit, Credit1}} ->
Consumer#consumer{segment
=
Segment1,
credit
=
Credit1}
end
end,
ConsumersAcc#{CorrelationId => Consumer1}
>>>>>>> d6301a3e11 (Handle closed connections in stream reader)
end,
ConsumersAcc#{CorrelationId =>
Consumer1}
Expand Down Expand Up @@ -1741,52 +1790,64 @@ handle_frame_post_auth(Transport,

rabbit_log:debug("Distributing existing messages to subscription ~p",
[SubscriptionId]),
{{segment, Segment1}, {credit, Credit1}} =
send_chunks(Transport, ConsumerState,
SendFileOct),
ConsumerState1 =
ConsumerState#consumer{segment = Segment1,
credit = Credit1},
Consumers1 =
Consumers#{SubscriptionId => ConsumerState1},

StreamSubscriptions1 =
case StreamSubscriptions of
#{Stream := SubscriptionIds} ->
StreamSubscriptions#{Stream =>
[SubscriptionId]
++ SubscriptionIds};
_ ->
StreamSubscriptions#{Stream =>
[SubscriptionId]}
end,

#consumer{counters = ConsumerCounters1} =
ConsumerState1,

ConsumerOffset = osiris_log:next_offset(Segment1),
ConsumerOffsetLag =
consumer_i(offset_lag, ConsumerState1),

rabbit_log:debug("Subscription ~p is now at offset ~p with ~p message(s) "
"distributed after subscription",
[SubscriptionId, ConsumerOffset,
messages_consumed(ConsumerCounters1)]),

rabbit_stream_metrics:consumer_created(self(),
stream_r(Stream,
Connection1),
SubscriptionId,
Credit1,
messages_consumed(ConsumerCounters1),
ConsumerOffset,
ConsumerOffsetLag,
Properties),
{Connection1#stream_connection{stream_subscriptions
=
StreamSubscriptions1},
State#stream_connection_state{consumers =
Consumers1}}
case send_chunks(Transport, ConsumerState,
SendFileOct)
of
{error, closed} ->
rabbit_log_connection:info("Stream protocol connection has been closed by peer",
[]),
throw({stop, normal});
{{segment, Segment1}, {credit, Credit1}} ->
ConsumerState1 =
ConsumerState#consumer{segment =
Segment1,
credit =
Credit1},
Consumers1 =
Consumers#{SubscriptionId =>
ConsumerState1},

StreamSubscriptions1 =
case StreamSubscriptions of
#{Stream := SubscriptionIds} ->
StreamSubscriptions#{Stream =>
[SubscriptionId]
++ SubscriptionIds};
_ ->
StreamSubscriptions#{Stream =>
[SubscriptionId]}
end,

#consumer{counters = ConsumerCounters1} =
ConsumerState1,

ConsumerOffset =
osiris_log:next_offset(Segment1),
ConsumerOffsetLag =
consumer_i(offset_lag, ConsumerState1),

rabbit_log:debug("Subscription ~p is now at offset ~p with ~p message(s) "
"distributed after subscription",
[SubscriptionId,
ConsumerOffset,
messages_consumed(ConsumerCounters1)]),

rabbit_stream_metrics:consumer_created(self(),
stream_r(Stream,
Connection1),
SubscriptionId,
Credit1,
messages_consumed(ConsumerCounters1),
ConsumerOffset,
ConsumerOffsetLag,
Properties),
{Connection1#stream_connection{stream_subscriptions
=
StreamSubscriptions1},
State#stream_connection_state{consumers =
Consumers1}}
end
end
end;
error ->
Expand All @@ -1813,6 +1874,9 @@ handle_frame_post_auth(Transport,
SendFileOct)
of
{error, closed} ->
<<<<<<< HEAD
<<<<<<< HEAD
<<<<<<< HEAD
rabbit_log:warning("Stream protocol connection for subscription ~p has been closed, removing "
"subscription",
[SubscriptionId]),
Expand All @@ -1827,6 +1891,18 @@ handle_frame_post_auth(Transport,
send(Transport, S, Frame),
rabbit_global_counters:increase_protocol_counter(stream, ?SUBSCRIPTION_ID_DOES_NOT_EXIST, 1),
{Connection1, State1};
=======
rabbit_log:warning("Stream protocol connection has been closed by peer", []),
throw({stop, connection_closed});
>>>>>>> d6301a3e11 (Handle closed connections in stream reader)
=======
rabbit_log_connection:info("Stream protocol connection has been closed by peer", []),
=======
rabbit_log_connection:info("Stream protocol connection has been closed by peer",
[]),
>>>>>>> 3b1714cbe3 (formatting)
throw({stop, normal});
>>>>>>> f10db03b4d (Gracefully terminate stream reaader)
{{segment, Segment1}, {credit, Credit1}} ->
Consumer1 =
Consumer#consumer{segment = Segment1, credit = Credit1},
Expand Down