-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat][broker] PIP-368: Support lookup based on the lookup properties (…
…#23223) PIP: #23075 ### Motivation This is the implementation for the PIP: #23075 Currently, the lookup process uses only the topic name as its parameter. However, to enhance this process, it's beneficial for clients to provide additional information. This could be done by introducing the `lookupProperties` field in the client configuration. Clients can then share these properties with the broker during lookup. On the broker side, the broker could also contain some properties that are used for the lookup. We can also support the lookupProperties for the broker. The broker can use these properties to make a better decision on which broker to return. Here is the rack-aware lookup scenario for using the client properties for the lookup: Assuming there are two brokers that broker-0 configures the lookup property "rack" with "A" and broker-1 configures the lookup property "rack" with "B". By using the lookup properties, clients can supply rack information during the lookup, enabling the broker to identify and connect them to the nearest broker within the same rack. If a client that configures the "rack" property with "A" connects to a lookup broker, the customized load manager can determine broker-0 as the owner broker since the broker and the client have the same rack property. ### Modifications - Add new configuration `lookupProperties` to the client. While looking up the broker, the client will send the properties to the broker through `CommandLookupTopic` request. - Add `properties` field to the `CommandLookupTopic`. - Add `lookupProperties` to the `LookupOptions`. The Load Manager implementation can access the `properties` through `LookupOptions` to make a better decision on which broker to return. - Introduce a new broker configuration `lookupPropertyPrefix`. Any broker configuration properties that start with the `lookupPropertyPrefix` will be included into the `BrokerLookupData` and be persisted in the metadata store. The broker can use these properties during the lookup. Co-authored-by: Yunze Xu <[email protected]>
- Loading branch information
1 parent
d9bd6b0
commit 9a97c84
Showing
20 changed files
with
220 additions
and
13 deletions.
There are no files selected for viewing
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
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
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
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
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
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
110 changes: 110 additions & 0 deletions
110
pulsar-broker/src/test/java/org/apache/pulsar/client/api/LookupPropertiesTest.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,110 @@ | ||
/* | ||
* 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.api; | ||
|
||
import java.net.InetSocketAddress; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.Properties; | ||
import java.util.Set; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.stream.Collectors; | ||
import lombok.Cleanup; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.pulsar.broker.MultiBrokerBaseTest; | ||
import org.apache.pulsar.broker.ServiceConfiguration; | ||
import org.apache.pulsar.broker.loadbalance.extensions.ExtensibleLoadManagerImpl; | ||
import org.apache.pulsar.broker.namespace.LookupOptions; | ||
import org.apache.pulsar.client.impl.PartitionedProducerImpl; | ||
import org.apache.pulsar.client.impl.ProducerImpl; | ||
import org.apache.pulsar.client.impl.PulsarClientImpl; | ||
import org.apache.pulsar.common.naming.ServiceUnitId; | ||
import org.testng.Assert; | ||
import org.testng.annotations.Test; | ||
|
||
@Slf4j | ||
@Test(groups = "broker-api") | ||
public class LookupPropertiesTest extends MultiBrokerBaseTest { | ||
|
||
private static final String BROKER_KEY = "lookup.broker.id"; | ||
private static final String CLIENT_KEY = "broker.id"; | ||
|
||
@Override | ||
protected void startBroker() throws Exception { | ||
addCustomConfigs(conf, 0); | ||
super.startBroker(); | ||
} | ||
|
||
@Override | ||
protected ServiceConfiguration createConfForAdditionalBroker(int additionalBrokerIndex) { | ||
return addCustomConfigs(getDefaultConf(), additionalBrokerIndex + 10); | ||
} | ||
|
||
private static ServiceConfiguration addCustomConfigs(ServiceConfiguration config, int index) { | ||
config.setDefaultNumberOfNamespaceBundles(16); | ||
config.setLoadBalancerAutoBundleSplitEnabled(false); | ||
config.setLoadManagerClassName(BrokerIdAwareLoadManager.class.getName()); | ||
config.setLoadBalancerAverageResourceUsageDifferenceThresholdPercentage(100); | ||
config.setLoadBalancerDebugModeEnabled(true); | ||
config.setBrokerShutdownTimeoutMs(1000); | ||
final var properties = new Properties(); | ||
properties.setProperty(BROKER_KEY, "broker-" + index); | ||
config.setProperties(properties); | ||
return config; | ||
} | ||
|
||
@Test | ||
public void testLookupProperty() throws Exception { | ||
final var topic = "test-lookup-property"; | ||
admin.topics().createPartitionedTopic(topic, 16); | ||
@Cleanup final var client = (PulsarClientImpl) PulsarClient.builder() | ||
.serviceUrl(pulsar.getBrokerServiceUrl()) | ||
.lookupProperties( | ||
Collections.singletonMap(CLIENT_KEY, "broker-10")) // broker-10 refers to additionalBrokers[0] | ||
.build(); | ||
@Cleanup final var producer = (PartitionedProducerImpl<byte[]>) client.newProducer().topic(topic).create(); | ||
Assert.assertNotNull(producer); | ||
final var connections = producer.getProducers().stream().map(ProducerImpl::getClientCnx) | ||
.collect(Collectors.toSet()); | ||
Assert.assertEquals(connections.size(), 1); | ||
final var port = ((InetSocketAddress) connections.stream().findAny().orElseThrow().ctx().channel() | ||
.remoteAddress()).getPort(); | ||
Assert.assertEquals(port, additionalBrokers.get(0).getBrokerListenPort().orElseThrow()); | ||
} | ||
|
||
public static class BrokerIdAwareLoadManager extends ExtensibleLoadManagerImpl { | ||
@Override | ||
public CompletableFuture<Optional<String>> selectAsync(ServiceUnitId bundle, Set<String> excludeBrokerSet, | ||
LookupOptions options) { | ||
final var clientId = options.getProperties() == null ? null : options.getProperties().get(CLIENT_KEY); | ||
if (clientId == null) { | ||
return super.selectAsync(bundle, excludeBrokerSet, options); | ||
} | ||
return getBrokerRegistry().getAvailableBrokerLookupDataAsync().thenCompose(brokerLookupDataMap -> { | ||
final var optBroker = brokerLookupDataMap.entrySet().stream().filter(entry -> { | ||
final var brokerId = entry.getValue().properties().get(BROKER_KEY); | ||
return brokerId != null && brokerId.equals(clientId); | ||
}).findAny(); | ||
return optBroker.map(Map.Entry::getKey).map(Optional::of).map(CompletableFuture::completedFuture) | ||
.orElseGet(() -> super.selectAsync(bundle, excludeBrokerSet, options)); | ||
}); | ||
} | ||
} | ||
} |
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
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.