merlimat commented on a change in pull request #548: PIP-1 - Introduce Pulsar proxy component URL: https://github.com/apache/incubator-pulsar/pull/548#discussion_r126543539
########## File path: pulsar-proxy/src/main/java/org/apache/pulsar/proxy/server/LookupProxyHandler.java ########## @@ -0,0 +1,160 @@ +/** + * 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 java.net.InetSocketAddress; +import java.net.SocketAddress; +import java.net.URI; +import java.net.URISyntaxException; + +import org.apache.pulsar.common.api.Commands; +import org.apache.pulsar.common.api.proto.PulsarApi.CommandLookupTopic; +import org.apache.pulsar.common.api.proto.PulsarApi.CommandLookupTopicResponse.LookupType; +import org.apache.pulsar.common.api.proto.PulsarApi.CommandPartitionedTopicMetadata; +import org.apache.pulsar.common.api.proto.PulsarApi.ServerError; +import org.apache.pulsar.common.naming.DestinationName; +import org.apache.pulsar.policies.data.loadbalancer.LoadReport; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import io.prometheus.client.Counter; + +public class LookupProxyHandler { + private final ProxyService service; + private final ProxyConnection proxyConnection; + private final boolean connectWithTLS; + + private SocketAddress clientAddress; + + private static final Counter lookupRequests = Counter + .build("pulsar_proxy_lookup_requests", "Counter of topic lookup requests").create().register(); + + private static final Counter partitionsMetadataRequests = Counter + .build("pulsar_proxy_partitions_metadata_requests", "Counter of partitions metadata requests").create() + .register(); + + public LookupProxyHandler(ProxyService proxy, ProxyConnection proxyConnection) { + this.service = proxy; + this.proxyConnection = proxyConnection; + this.clientAddress = proxyConnection.clientAddress(); + this.connectWithTLS = proxy.getConfiguration().isTlsEnabledWithBroker(); + } + + public void handleLookup(CommandLookupTopic lookup) { + if (log.isDebugEnabled()) { + log.debug("Received Lookup from {}", clientAddress); + } + + lookupRequests.inc(); + long clientRequestId = lookup.getRequestId(); + String topic = lookup.getTopic(); + + LoadReport availableBroker = null; + try { + availableBroker = service.getDiscoveryProvider().nextBroker(); + } catch (Exception e) { + log.warn("[{}] Failed to get next active broker {}", clientAddress, e.getMessage(), e); + proxyConnection.ctx().writeAndFlush( + Commands.newLookupErrorResponse(ServerError.ServiceNotReady, e.getMessage(), clientRequestId)); + return; + } + + performLookup(clientRequestId, topic, availableBroker.getPulsarServiceUrl(), false, 10); + } + + private void performLookup(long clientRequestId, String topic, String brokerServiceUrl, boolean authoritative, + int numberOfRetries) { + if (numberOfRetries == 0) { + proxyConnection.ctx().writeAndFlush(Commands.newLookupErrorResponse(ServerError.ServiceNotReady, + "Reached max number of redirections", clientRequestId)); + return; + } + + URI brokerURI; + try { + brokerURI = new URI(brokerServiceUrl); + } catch (URISyntaxException e) { + proxyConnection.ctx().writeAndFlush( + Commands.newLookupErrorResponse(ServerError.ServiceNotReady, e.getMessage(), clientRequestId)); Review comment: Makes more sense. Fixed ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
