Skip to content
Open
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 @@ -77,6 +77,7 @@
import io.netty.handler.codec.mqtt.MqttTopicSubscription;
import io.netty.handler.codec.mqtt.MqttUnsubscribeMessage;
import java.time.Duration;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
Expand Down Expand Up @@ -1286,6 +1287,7 @@ private void scheduleResend() {
private void resend() {
long now = sessionCtx.nanoTime();
boolean flush = false;
List<ConfirmingMessage> toDrop = new ArrayList<>();
for (ConfirmingMessage confirmingMsg : unconfirmedPacketIds.values()) {
if (confirmingMsg.sentCount <= settings.maxResendTimes) {
if (ctx.channel().isWritable()) {
Expand All @@ -1306,14 +1308,18 @@ private void resend() {
break;
}
} else {
reportDropConfirmableMsgEvent(confirmingMsg.message, DropReason.MaxRetried);
confirm(confirmingMsg, false);
receiveQuota.onErrorSignal(now);
toDrop.add(confirmingMsg);
}
}
if (flush) {
flush(true);
}
// drop after the iteration completes: confirm() structurally modifies unconfirmedPacketIds
for (ConfirmingMessage dropped : toDrop) {
reportDropConfirmableMsgEvent(dropped.message, DropReason.MaxRetried);
confirm(dropped, false);
receiveQuota.onErrorSignal(now);
}
if (!unconfirmedPacketIds.isEmpty()) {
scheduleResend();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1730,4 +1730,52 @@ public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
verify(eventCollector, atLeast(1)).report(argThat(e ->
e instanceof QoS1Confirmed c && !c.delivered()));
}

@Test
public void resendDroppingHeadAbortsDropOfRemaining() {
when(settingProvider.provide(eq(ResendTimeoutSeconds), anyString())).thenReturn(1);
when(settingProvider.provide(eq(MaxResendTimes), anyString())).thenReturn(1);
when(settingProvider.provide(eq(ReceivingMaximum), anyString())).thenReturn(2);

channel.pipeline().removeLast();
channel.pipeline().addLast(new ChannelDuplexHandler() {
@Override
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
super.handlerAdded(ctx);
ctx.pipeline().addLast(
MQTT3TransientSessionHandler.builder().settings(new TenantSettings(tenantId, settingProvider))
.tenantMeter(tenantMeter).oomCondition(oomCondition).userSessionId(userSessionId(clientInfo))
.keepAliveTimeSeconds(120).clientInfo(clientInfo).willMessage(null).ctx(ctx).build());
ctx.pipeline().remove(this);
}
});
transientSessionHandler = (MQTT3TransientSessionHandler) channel.pipeline().last();

mockCheckPermission(true);
mockDistMatch(true);
transientSessionHandler.subscribe(System.nanoTime(), topicFilter, QoS.AT_LEAST_ONCE);
channel.runPendingTasks();
ArgumentCaptor<Long> longCaptor = ArgumentCaptor.forClass(Long.class);
verify(localDistService).match(anyLong(), eq(topicFilter), longCaptor.capture(), any());

transientSessionHandler.publish(s2cMessageList(topic, 2, QoS.AT_LEAST_ONCE),
Collections.singleton(new IMQTTTransientSession.MatchedTopicFilter(topicFilter, longCaptor.getValue())));
channel.runPendingTasks();

assertNotNull(channel.readOutbound());
assertNotNull(channel.readOutbound());
// deliberately never ack: both stay in flight, so the head is dropped while another entry remains

for (int i = 0; i < 6; i++) {
testTicker.advanceTimeBy(2, TimeUnit.SECONDS);
channel.advanceTimeBy(2, TimeUnit.SECONDS);
channel.runScheduledPendingTasks();
channel.runPendingTasks();
channel.flushOutbound();
}

// both in-flight messages have exceeded maxResendTimes and must be dropped
verify(eventCollector, times(2)).report(argThat(e ->
e instanceof QoS1Dropped d && d.reason() == DropReason.MaxRetried));
}
}