adarshsanjeev commented on code in PR #14322:
URL: https://github.com/apache/druid/pull/14322#discussion_r1315343045


##########
server/src/main/java/org/apache/druid/discovery/BrokerClient.java:
##########
@@ -0,0 +1,121 @@
+/*
+ * 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.druid.discovery;
+
+import com.google.inject.Inject;
+import org.apache.druid.error.DruidException;
+import org.apache.druid.guice.annotations.EscalatedGlobal;
+import org.apache.druid.java.util.common.IOE;
+import org.apache.druid.java.util.common.RetryUtils;
+import org.apache.druid.java.util.common.StringUtils;
+import org.apache.druid.java.util.http.client.HttpClient;
+import org.apache.druid.java.util.http.client.Request;
+import 
org.apache.druid.java.util.http.client.response.StringFullResponseHandler;
+import 
org.apache.druid.java.util.http.client.response.StringFullResponseHolder;
+import org.jboss.netty.channel.ChannelException;
+import org.jboss.netty.handler.codec.http.HttpMethod;
+import org.jboss.netty.handler.codec.http.HttpResponseStatus;
+
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.nio.charset.StandardCharsets;
+import java.util.concurrent.ExecutionException;
+
+/**
+ * This class facilitates interaction with Broker.
+ */
+public class BrokerClient
+{
+  private static final int MAX_RETRIES = 5;
+
+  private final HttpClient brokerHttpClient;
+  private final DruidNodeDiscovery druidNodeDiscovery;
+
+  @Inject
+  public BrokerClient(
+      @EscalatedGlobal HttpClient brokerHttpClient,
+      DruidNodeDiscoveryProvider druidNodeDiscoveryProvider
+  )
+  {
+    this.brokerHttpClient = brokerHttpClient;
+    this.druidNodeDiscovery = 
druidNodeDiscoveryProvider.getForNodeRole(NodeRole.BROKER);
+  }
+
+  /**
+   * Creates and returns a {@link Request} after choosing a broker.
+   */
+  public Request makeRequest(HttpMethod httpMethod, String urlPath) throws 
IOException
+  {
+    String host = ClientUtils.pickOneHost(druidNodeDiscovery);
+
+    if (host == null) {
+      throw DruidException.forPersona(DruidException.Persona.ADMIN)
+                          .ofCategory(DruidException.Category.NOT_FOUND)
+                          .build("A leader node could not be found for [%s] 
service. Check the logs to validate that service is healthy.", NodeRole.BROKER);
+    }
+    return new Request(httpMethod, new URL(StringUtils.format("%s%s", host, 
urlPath)));
+  }
+
+  public String sendQuery(final Request request) throws Exception
+  {
+    return RetryUtils.retry(
+        () -> {
+          Request newRequestUrl = getNewRequestUrl(request);
+          final StringFullResponseHolder fullResponseHolder = 
brokerHttpClient.go(newRequestUrl, new 
StringFullResponseHandler(StandardCharsets.UTF_8)).get();
+
+          HttpResponseStatus responseStatus = 
fullResponseHolder.getResponse().getStatus();
+          if (HttpResponseStatus.SERVICE_UNAVAILABLE.equals(responseStatus)
+              || HttpResponseStatus.GATEWAY_TIMEOUT.equals(responseStatus)) {
+            throw new IOE(StringUtils.format("Request to broker failed due to 
failed response status: [%s]", responseStatus));

Review Comment:
   Should these be defensive? These would be thrown if the broker is not 
reachable which is a condition that can happen without any bugs in the code, 
right?



-- 
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]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to