forked from apache/pulsar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[fix][client] Make the whole grabCnx() progress atomic
### Motivation In `ConnectionHandler`, there is a `Connection` interface whose methods will be called after the connection in `grabCnx` is established, the implementation of `Connection` might send some requests after the connection is established. For example, the consumer will send the `CommandSubscribe` request in `connectionOpened`. However, the whole process is not atomic, which leads to the message lost reported in apache#20591. ### Modifications Modify the `Connection` interface to have a single method: ```java CompletableFuture<Void> handleNewConnection(ClientCnx cnx, PulsarClientException e); ``` The returned future should be completed once the implementation has done everything, e.g. for the consumer, the future should only be completed after receiving the response for `CommandSubscribe`. In `grabCnx`, the `ConnectionHandler` could only connect to the broker once the whole process is completed. Add `ConnectionHandlerTest` to verify the behavior.
- Loading branch information
1 parent
a85e9df
commit b9d9637
Showing
7 changed files
with
224 additions
and
53 deletions.
There are no files selected for viewing
105 changes: 105 additions & 0 deletions
105
pulsar-broker/src/test/java/org/apache/pulsar/client/impl/ConnectionHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.pulsar.client.impl; | ||
|
||
import java.time.Duration; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.pulsar.client.api.ProducerConsumerBase; | ||
import org.awaitility.Awaitility; | ||
import org.awaitility.core.ConditionTimeoutException; | ||
import org.testng.Assert; | ||
import org.testng.annotations.AfterClass; | ||
import org.testng.annotations.BeforeClass; | ||
import org.testng.annotations.Test; | ||
|
||
@Slf4j | ||
@Test(groups = "broker-impl") | ||
public class ConnectionHandlerTest extends ProducerConsumerBase { | ||
|
||
private static final Backoff BACKOFF = new BackoffBuilder().setInitialTime(1, TimeUnit.MILLISECONDS) | ||
.setMandatoryStop(1, TimeUnit.SECONDS) | ||
.setMax(3, TimeUnit.SECONDS).create(); | ||
private final ExecutorService executor = Executors.newFixedThreadPool(4); | ||
|
||
@BeforeClass(alwaysRun = true) | ||
@Override | ||
protected void setup() throws Exception { | ||
super.internalSetup(); | ||
super.producerBaseSetup(); | ||
} | ||
|
||
@AfterClass | ||
@Override | ||
protected void cleanup() throws Exception { | ||
super.internalCleanup(); | ||
executor.shutdown(); | ||
} | ||
|
||
@Test(timeOut = 30000) | ||
public void testSynchronousGrabCnx() { | ||
for (int i = 0; i < 10; i++) { | ||
final CompletableFuture<Integer> future = new CompletableFuture<>(); | ||
final int index = i; | ||
final ConnectionHandler handler = new ConnectionHandler( | ||
new MockedHandlerState((PulsarClientImpl) pulsarClient, "my-topic"), BACKOFF, | ||
(cnx, e) -> { | ||
future.complete(index); | ||
return CompletableFuture.completedFuture(null); | ||
}); | ||
handler.grabCnx(); | ||
Assert.assertEquals(future.join(), i); | ||
} | ||
} | ||
|
||
@Test | ||
public void testConcurrentGrabCnx() { | ||
final AtomicInteger cnt = new AtomicInteger(0); | ||
final ConnectionHandler handler = new ConnectionHandler( | ||
new MockedHandlerState((PulsarClientImpl) pulsarClient, "my-topic"), BACKOFF, | ||
(cnx, e) -> { | ||
cnt.incrementAndGet(); | ||
return CompletableFuture.completedFuture(null); | ||
}); | ||
final int numGrab = 10; | ||
for (int i = 0; i < numGrab; i++) { | ||
handler.grabCnx(); | ||
} | ||
Awaitility.await().atMost(Duration.ofSeconds(3)).until(() -> cnt.get() > 0); | ||
Assert.assertThrows(ConditionTimeoutException.class, | ||
() -> Awaitility.await().atMost(Duration.ofMillis(500)).until(() -> cnt.get() == numGrab)); | ||
Assert.assertEquals(cnt.get(), 1); | ||
} | ||
|
||
private static class MockedHandlerState extends HandlerState { | ||
|
||
public MockedHandlerState(PulsarClientImpl client, String topic) { | ||
super(client, topic); | ||
} | ||
|
||
@Override | ||
String getHandlerName() { | ||
return "mocked"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.