heesung-sn commented on code in PR #21789:
URL: https://github.com/apache/pulsar/pull/21789#discussion_r1434611818


##########
pulsar-broker/src/test/java/org/apache/pulsar/client/impl/PulsarTestClient.java:
##########
@@ -125,7 +125,7 @@ public CompletableFuture<ClientCnx> getConnection(String 
topic) {
             result.completeExceptionally(new IOException("New connections are 
rejected."));
             return result;
         } else {
-            return super.getConnection(topic, 
getCnxPool().genRandomKeyToSelectCon());
+            return super.getConnection(topic);

Review Comment:
   can we keep ` super.getConnection(topic, 
getCnxPool().genRandomKeyToSelectCon());`?



##########
pulsar-client/src/main/java/org/apache/pulsar/client/impl/HttpLookupService.java:
##########
@@ -101,7 +100,7 @@ public CompletableFuture<Pair<InetSocketAddress, 
InetSocketAddress>> getBroker(T
                 }
 
                 InetSocketAddress brokerAddress = 
InetSocketAddress.createUnresolved(uri.getHost(), uri.getPort());
-                return 
CompletableFuture.completedFuture(Pair.of(brokerAddress, brokerAddress));
+                return CompletableFuture.completedFuture(new 
LookupTopicResult(brokerAddress, brokerAddress, false));

Review Comment:
   nit: can we comment here that the http lookup is always non-proxy?



##########
pulsar-client/src/main/java/org/apache/pulsar/client/impl/ConnectionHandler.java:
##########
@@ -94,10 +96,13 @@ protected void grabCnx(Optional<URI> hostURI) {
         try {
             CompletableFuture<ClientCnx> cnxFuture;
             if (hostURI.isPresent()) {
-                InetSocketAddress address = InetSocketAddress.createUnresolved(
-                        hostURI.get().getHost(),
-                        hostURI.get().getPort());
-                cnxFuture = state.client.getConnection(address, address, 
randomKeyForSelectConnection);
+                URI uri = hostURI.get();
+                InetSocketAddress address = 
InetSocketAddress.createUnresolved(uri.getHost(), uri.getPort());
+                if (useProxy) {
+                    cnxFuture = state.client.getProxiedConnection(address, 
randomKeyForSelectConnection);
+                } else {
+                    cnxFuture = state.client.getConnection(address, address, 
randomKeyForSelectConnection);

Review Comment:
   By default, useProxy is false. I am worried if we come to this block (before 
useProxy is set) even if the cluster is using the proxy.
   
   Do you think we should use `Optional<Boolean> useProxy`? If 
useProxy.isEmpty(), then we should go to line 120 to set the useProxy first? 
Then, for this `if block`, we need to check 
   ```
   if(hostURI.isPresent() && !useProxy.isEmpty()){
   ...
   }
   ```



##########
pulsar-client/src/main/java/org/apache/pulsar/client/impl/ConnectionHandler.java:
##########
@@ -47,6 +47,8 @@ public class ConnectionHandler {
     private final AtomicBoolean duringConnect = new AtomicBoolean(false);
     protected final int randomKeyForSelectConnection;
 
+    private boolean useProxy = false;

Review Comment:
   Should we make this `private final Optional<Boolean> useProxy = 
Optional.Empty()`?



##########
pulsar-client/src/main/java/org/apache/pulsar/client/impl/PulsarClientImpl.java:
##########
@@ -988,6 +990,15 @@ public CompletableFuture<ClientCnx> 
getConnectionToServiceUrl() {
         return getConnection(address, address, 
cnxPool.genRandomKeyToSelectCon());
     }
 
+    public CompletableFuture<ClientCnx> getProxiedConnection(final 
InetSocketAddress logicalAddress,

Review Comment:
   nit: can we rename this to `getProxyConnection`?



##########
pulsar-proxy/src/test/java/org/apache/pulsar/proxy/server/ProxyWithExtensibleLoadManagerTest.java:
##########
@@ -0,0 +1,229 @@
+/*
+ * 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.proxy.server;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.CompletionException;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.Executors;
+import java.util.concurrent.Semaphore;
+import java.util.concurrent.TimeUnit;
+import lombok.Cleanup;
+import org.apache.commons.lang3.reflect.FieldUtils;
+import org.apache.pulsar.broker.BrokerTestUtil;
+import org.apache.pulsar.broker.MultiBrokerBaseTest;
+import org.apache.pulsar.broker.PulsarService;
+import org.apache.pulsar.broker.ServiceConfiguration;
+import org.apache.pulsar.broker.authentication.AuthenticationService;
+import 
org.apache.pulsar.broker.loadbalance.extensions.ExtensibleLoadManagerImpl;
+import org.apache.pulsar.client.api.PulsarClient;
+import org.apache.pulsar.client.api.PulsarClientException;
+import org.apache.pulsar.client.api.Schema;
+import org.apache.pulsar.client.api.SubscriptionInitialPosition;
+import org.apache.pulsar.client.impl.LookupService;
+import org.apache.pulsar.client.impl.PulsarClientImpl;
+import org.apache.pulsar.common.configuration.PulsarConfigurationLoader;
+import org.apache.pulsar.common.naming.NamespaceName;
+import org.apache.pulsar.common.naming.TopicDomain;
+import org.apache.pulsar.common.naming.TopicName;
+import org.apache.pulsar.metadata.impl.ZKMetadataStore;
+import org.mockito.Mockito;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+public class ProxyWithExtensibleLoadManagerTest extends MultiBrokerBaseTest {
+
+    private static final int TEST_TIMEOUT_MS = 30_000;
+
+    private ProxyService proxyService;
+
+    @Override
+    public int numberOfAdditionalBrokers() {
+        return 1;
+    }
+
+    @Override
+    public void doInitConf() throws Exception {
+        super.doInitConf();
+        configureExtensibleLoadManager(conf);
+    }
+
+    @Override
+    protected ServiceConfiguration createConfForAdditionalBroker(int 
additionalBrokerIndex) {
+        return configureExtensibleLoadManager(getDefaultConf());
+    }
+
+    private ServiceConfiguration 
configureExtensibleLoadManager(ServiceConfiguration config) {
+        config.setNumIOThreads(8);
+        config.setLoadBalancerInFlightServiceUnitStateWaitingTimeInMillis(5 * 
1000);
+        config.setLoadBalancerServiceUnitStateMonitorIntervalInSeconds(1);
+        
config.setLoadManagerClassName(ExtensibleLoadManagerImpl.class.getName());
+        config.setLoadBalancerSheddingEnabled(false);
+        return config;
+    }
+
+    private ProxyConfiguration initializeProxyConfig() {
+        var proxyConfig = new ProxyConfiguration();
+        proxyConfig.setNumIOThreads(8);
+        proxyConfig.setServicePort(Optional.of(0));
+        proxyConfig.setBrokerProxyAllowedTargetPorts("*");
+        proxyConfig.setMetadataStoreUrl(DUMMY_VALUE);
+        proxyConfig.setConfigurationMetadataStoreUrl(GLOBAL_DUMMY_VALUE);
+        return proxyConfig;
+    }
+
+    private LookupService spyLookupService(PulsarClient client) throws 
IllegalAccessException {
+        LookupService svc = (LookupService) 
FieldUtils.readDeclaredField(client, "lookup", true);
+        var lookup = spy(svc);
+        FieldUtils.writeDeclaredField(client, "lookup", lookup, true);
+        return lookup;
+    }
+
+    private PulsarClientImpl createClient(ProxyService proxyService) {
+        try {
+            return Mockito.spy((PulsarClientImpl) PulsarClient.builder().
+                    serviceUrl(proxyService.getServiceUrl()).
+                    build());
+        } catch (PulsarClientException e) {
+            throw new CompletionException(e);
+        }
+    }
+
+    @BeforeMethod(alwaysRun = true)
+    public void proxySetup() throws Exception {
+        var proxyConfig = initializeProxyConfig();
+        proxyService = Mockito.spy(new ProxyService(proxyConfig, new 
AuthenticationService(
+                PulsarConfigurationLoader.convertFrom(proxyConfig))));
+        doReturn(registerCloseable(new 
ZKMetadataStore(mockZooKeeper))).when(proxyService).createLocalMetadataStore();
+        doReturn(registerCloseable(new 
ZKMetadataStore(mockZooKeeperGlobal))).when(proxyService)
+                .createConfigurationMetadataStore();
+        proxyService.start();
+    }
+
+    @AfterMethod(alwaysRun = true)
+    public void proxyCleanup() throws Exception {
+        if (proxyService != null) {
+            proxyService.close();
+        }
+    }
+

Review Comment:
   Could we add a case to assert that the clients can connect to the brokers 
directly when we shutdown the proxy in the middle,?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to