heesung-sn commented on code in PR #21789: URL: https://github.com/apache/pulsar/pull/21789#discussion_r1435236442
########## 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: Let's say a user is upgrading the cluster to migrate from pulsar-proxy to something else(istio). In this case, the connected clients should get connection errors from the proxy (as the proxy is shut down). Then, the clients should try to reconnect and soon get the broker addresses (assuming proxy hosts are removed under the service url and the brokers are added under it). To simulate this case, I guess we need the following mocks. 0. mock resolveHost() to return the proxy address when called the first time and return the broker address when called the second time. 1. let the client connect to the proxy (assert useProxy is true) 3. shutdown proxy 4. assert that the client connects to the broker (assert useProxy is false) -- 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]
