Skip to content
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

[improve] [client] PIP-344 support feature flag supportsGetPartitionedMetadataWithoutAutoCreation #22773

Merged
merged 4 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -20,17 +20,23 @@

import com.google.common.collect.Sets;
import io.netty.channel.ChannelHandlerContext;
import java.lang.reflect.Field;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.apache.pulsar.PulsarVersion;
import org.apache.pulsar.broker.BrokerTestUtil;
import org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest;
import org.apache.pulsar.client.api.Consumer;
import org.apache.pulsar.client.api.Producer;
import org.apache.pulsar.client.api.PulsarClient;
import org.apache.pulsar.client.api.Schema;
import org.apache.pulsar.common.policies.data.ClusterData;
import org.apache.pulsar.common.policies.data.TenantInfoImpl;
import org.awaitility.Awaitility;
import org.mockito.Mockito;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
Expand Down Expand Up @@ -124,4 +130,44 @@ public void testClientVersion() throws Exception {
producer.close();
consumer.close();
}

@Test
public void testSupportsGetPartitionedMetadataWithoutAutoCreation() throws Exception {
final String topic = BrokerTestUtil.newUniqueName( "persistent://" + NAMESPACE + "/tp");
admin.topics().createNonPartitionedTopic(topic);
PulsarClientImpl clientWitBinaryLookup = (PulsarClientImpl) PulsarClient.builder()
.maxNumberOfRejectedRequestPerConnection(1)
.connectionMaxIdleSeconds(Integer.MAX_VALUE)
.serviceUrl(pulsar.getBrokerServiceUrl())
.build();
ProducerImpl producer = (ProducerImpl) clientWitBinaryLookup.newProducer().topic(topic).create();

// Verify: the variable "isSupportsGetPartitionedMetadataWithoutAutoCreation" responded from the broker is true.
Awaitility.await().untilAsserted(() -> {
ClientCnx clientCnx = producer.getClientCnx();
Assert.assertNotNull(clientCnx);
Assert.assertTrue(clientCnx.isSupportsGetPartitionedMetadataWithoutAutoCreation());
});
Assert.assertEquals(
clientWitBinaryLookup.getPartitionsForTopic(topic, true).get().size(), 1);

// Inject a "false" value for the variable "isSupportsGetPartitionedMetadataWithoutAutoCreation".
// Verify: client will get a not support error.
Field field = ClientCnx.class.getDeclaredField("supportsGetPartitionedMetadataWithoutAutoCreation");
field.setAccessible(true);
for (CompletableFuture<ClientCnx> clientCnxFuture : clientWitBinaryLookup.getCnxPool().getConnections()) {
field.set(clientCnxFuture.get(), false);
}
try {
clientWitBinaryLookup.getPartitionsForTopic(topic, false).join();
Assert.fail("Expected an error that the broker version is too old.");
} catch (Exception ex) {
Assert.assertTrue(ex.getMessage().contains("version is too old to support getting partitions"));
}

// cleanup.
producer.close();
clientWitBinaryLookup.close();
admin.topics().delete(topic, false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,12 @@ private CompletableFuture<PartitionedTopicMetadata> getPartitionedTopicMetadata(
CompletableFuture<PartitionedTopicMetadata> partitionFuture = new CompletableFuture<>();

client.getCnxPool().getConnection(socketAddress).thenAccept(clientCnx -> {
if (!metadataAutoCreationEnabled && !clientCnx.isSupportsGetPartitionedMetadataWithoutAutoCreation()) {
partitionFuture.completeExceptionally(new PulsarClientException.NotSupportedException("The feature of"
+ " getting partitions without auto-creation is not supported from the broker,"
+ " please upgrade the broker to the latest version."));
return;
}
long requestId = client.newRequestId();
ByteBuf request = Commands.newPartitionMetadataRequest(topicName.toString(), requestId,
metadataAutoCreationEnabled);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ public class ClientCnx extends PulsarHandler {
protected AuthenticationDataProvider authenticationDataProvider;
private TransactionBufferHandler transactionBufferHandler;
private boolean supportsTopicWatchers;
@Getter
private boolean supportsGetPartitionedMetadataWithoutAutoCreation;

/** Idle stat. **/
@Getter
Expand Down Expand Up @@ -400,6 +402,9 @@ protected void handleConnected(CommandConnected connected) {

supportsTopicWatchers =
connected.hasFeatureFlags() && connected.getFeatureFlags().isSupportsTopicWatchers();
supportsGetPartitionedMetadataWithoutAutoCreation =
connected.hasFeatureFlags()
&& connected.getFeatureFlags().isSupportsGetPartitionedMetadataWithoutAutoCreation();

// set remote protocol version to the correct version before we complete the connection future
setRemoteEndpointProtocolVersion(connected.getProtocolVersion());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ public static BaseCommand newConnectedCommand(int clientProtocolVersion, int max
connected.setProtocolVersion(versionToAdvertise);

connected.setFeatureFlags().setSupportsTopicWatchers(supportsTopicWatchers);
connected.setFeatureFlags().setSupportsGetPartitionedMetadataWithoutAutoCreation(true);
return cmd;
}

Expand Down
Loading