GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [fix][client] Release seek ByteBuf on early-return error paths (#27592) · apache/pulsar@a93c308

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • pulsar-client/src
    • main/java/org/apache/pulsar/client/impl
      • ConsumerImpl.java
    • test/java/org/apache/pulsar/client/impl
      • ConsumerImplTest.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎pulsar-client/src/main/java/org/apache/pulsar/client/impl/ConsumerImpl.java‎

Copy file name to clipboardExpand all lines: pulsar-client/src/main/java/org/apache/pulsar/client/impl/ConsumerImpl.java
+16-6Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import java.util.concurrent.CompletableFuture;
5959
import java.util.concurrent.ConcurrentHashMap;
6060
import java.util.concurrent.ExecutionException;
61+
import java.util.concurrent.RejectedExecutionException;
6162
import java.util.concurrent.ScheduledExecutorService;
6263
import java.util.concurrent.TimeUnit;
6364
import java.util.concurrent.atomic.AtomicBoolean;
@@ -2909,6 +2910,7 @@ private CompletableFuture<Void> seekAsyncInternal(long requestId, ByteBuf seek,
29092910
.build();
29102911

29112912
if (!seekStatus.compareAndSet(SeekStatus.NOT_STARTED, SeekStatus.IN_PROGRESS)) {
2913+
seek.release();
29122914
final String message = String.format(
29132915
"[%s][%s] attempting to seek operation that is already in progress (seek by %s)",
29142916
topic, subscription, seekBy);
@@ -2966,19 +2968,27 @@ private void seekAsyncInternal(long requestId, ByteBuf seek, MessageId seekId, L
29662968
} else {
29672969
long nextDelay = Math.min(backoff.next().toMillis(), remainingTime.get());
29682970
if (nextDelay <= 0) {
2971+
seek.release();
29692972
failSeek(
29702973
new PulsarClientException.TimeoutException(
29712974
String.format("The subscription %s of the topic %s could not seek "
29722975
+ "withing configured timeout", subscription, topicName.toString())));
29732976
return;
29742977
}
29752978

2976-
((ScheduledExecutorService) client.getScheduledExecutorProvider().getExecutor()).schedule(() -> {
2977-
log.warn("[{}] [{}] Could not get connection while seek -- Will try again in {} ms",
2978-
topic, getHandlerName(), nextDelay);
2979-
remainingTime.addAndGet(-nextDelay);
2980-
seekAsyncInternal(requestId, seek, seekId, seekTimestamp, seekBy, backoff, remainingTime);
2981-
}, nextDelay, TimeUnit.MILLISECONDS);
2979+
try {
2980+
((ScheduledExecutorService) client.getScheduledExecutorProvider().getExecutor()).schedule(() -> {
2981+
log.warn("[{}] [{}] Could not get connection while seek -- Will try again in {} ms",
2982+
topic, getHandlerName(), nextDelay);
2983+
remainingTime.addAndGet(-nextDelay);
2984+
seekAsyncInternal(requestId, seek, seekId, seekTimestamp, seekBy, backoff, remainingTime);
2985+
}, nextDelay, TimeUnit.MILLISECONDS);
2986+
} catch (RejectedExecutionException e) {
2987+
seek.release();
2988+
failSeek(new PulsarClientException(
2989+
String.format("Seek retry for subscription %s of topic %s was rejected",
2990+
subscription, topicName.toString()), e));
2991+
}
29822992
}
29832993
}
29842994

Collapse file

‎pulsar-client/src/test/java/org/apache/pulsar/client/impl/ConsumerImplTest.java‎

Copy file name to clipboardExpand all lines: pulsar-client/src/test/java/org/apache/pulsar/client/impl/ConsumerImplTest.java
+84Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import static org.mockito.Mockito.doNothing;
2929
import static org.mockito.Mockito.doReturn;
3030
import static org.mockito.Mockito.mock;
31+
import static org.mockito.Mockito.mockStatic;
3132
import static org.mockito.Mockito.never;
3233
import static org.mockito.Mockito.spy;
3334
import static org.mockito.Mockito.times;
@@ -93,6 +94,7 @@
9394
import org.apache.pulsar.common.protocol.Commands;
9495
import org.apache.pulsar.common.util.Backoff;
9596
import org.awaitility.Awaitility;
97+
import org.mockito.MockedStatic;
9698
import org.testng.Assert;
9799
import org.testng.annotations.AfterMethod;
98100
import org.testng.annotations.BeforeMethod;
@@ -1050,6 +1052,88 @@ public void testSeekAsyncInternal() {
10501052
verify(cnx, times(1)).sendRequestWithId(any(ByteBuf.class), anyLong());
10511053
}
10521054

1055+
@Test
1056+
public void testSeekAsyncReleasesBufferOnInProgressConflict() {
1057+
ClientCnx cnx = mock(ClientCnx.class);
1058+
CompletableFuture<ProducerResponse> clientReq = new CompletableFuture<>();
1059+
when(cnx.sendRequestWithId(any(ByteBuf.class), anyLong())).thenAnswer(inv -> {
1060+
inv.<ByteBuf>getArgument(0).release();
1061+
return clientReq;
1062+
});
1063+
consumer.setClientCnx(cnx);
1064+
consumer.setState(HandlerState.State.Ready);
1065+
1066+
List<ByteBuf> capturedBuffers = new ArrayList<>();
1067+
try (MockedStatic<Commands> commands = mockStatic(Commands.class, inv -> {
1068+
Object result = inv.callRealMethod();
1069+
if ("newSeek".equals(inv.getMethod().getName())) {
1070+
capturedBuffers.add((ByteBuf) result);
1071+
}
1072+
return result;
1073+
})) {
1074+
CompletableFuture<Void> firstResult = consumer.seekAsync(1L);
1075+
CompletableFuture<Void> secondResult = consumer.seekAsync(1L);
1076+
1077+
assertThat(capturedBuffers).hasSize(2);
1078+
assertThat(capturedBuffers.get(1).refCnt()).isZero();
1079+
assertTrue(secondResult.isCompletedExceptionally());
1080+
}
1081+
}
1082+
1083+
@Test
1084+
public void testSeekAsyncReleasesBufferOnTimeout() {
1085+
ScheduledExecutorProvider provider = mock(ScheduledExecutorProvider.class);
1086+
ScheduledExecutorService scheduledExecutorService = mock(ScheduledExecutorService.class);
1087+
when(provider.getExecutor()).thenReturn(scheduledExecutorService);
1088+
when(consumer.getClient().getScheduledExecutorProvider()).thenReturn(provider);
1089+
1090+
doAnswer(invocation -> {
1091+
((Runnable) invocation.getArgument(0)).run();
1092+
return null;
1093+
}).when(scheduledExecutorService).schedule(any(Runnable.class), anyLong(), any(TimeUnit.class));
1094+
1095+
List<ByteBuf> capturedBuffers = new ArrayList<>();
1096+
try (MockedStatic<Commands> commands = mockStatic(Commands.class, inv -> {
1097+
Object result = inv.callRealMethod();
1098+
if ("newSeek".equals(inv.getMethod().getName())) {
1099+
capturedBuffers.add((ByteBuf) result);
1100+
}
1101+
return result;
1102+
})) {
1103+
CompletableFuture<Void> result = consumer.seekAsync(1L);
1104+
1105+
assertThat(capturedBuffers).hasSize(1);
1106+
assertThat(capturedBuffers.get(0).refCnt()).isZero();
1107+
assertTrue(result.isCompletedExceptionally());
1108+
}
1109+
}
1110+
1111+
@Test
1112+
public void testSeekAsyncReleasesBufferOnRejectedRetry() {
1113+
ScheduledExecutorProvider provider = mock(ScheduledExecutorProvider.class);
1114+
ScheduledExecutorService scheduledExecutorService = mock(ScheduledExecutorService.class);
1115+
when(provider.getExecutor()).thenReturn(scheduledExecutorService);
1116+
when(consumer.getClient().getScheduledExecutorProvider()).thenReturn(provider);
1117+
1118+
when(scheduledExecutorService.schedule(any(Runnable.class), anyLong(), any(TimeUnit.class)))
1119+
.thenThrow(new java.util.concurrent.RejectedExecutionException("shutdown"));
1120+
1121+
List<ByteBuf> capturedBuffers = new ArrayList<>();
1122+
try (MockedStatic<Commands> commands = mockStatic(Commands.class, inv -> {
1123+
Object result = inv.callRealMethod();
1124+
if ("newSeek".equals(inv.getMethod().getName())) {
1125+
capturedBuffers.add((ByteBuf) result);
1126+
}
1127+
return result;
1128+
})) {
1129+
CompletableFuture<Void> result = consumer.seekAsync(1L);
1130+
1131+
assertThat(capturedBuffers).hasSize(1);
1132+
assertThat(capturedBuffers.get(0).refCnt()).isZero();
1133+
assertTrue(result.isCompletedExceptionally());
1134+
}
1135+
}
1136+
10531137
@Test(invocationTimeOut = 1000)
10541138
public void testAutoGenerateConsumerName() {
10551139
Pattern consumerNamePattern = Pattern.compile("[a-zA-Z0-9]{5}");

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.