Jackie-Jiang commented on code in PR #8467:
URL: https://github.com/apache/pinot/pull/8467#discussion_r853569680


##########
pinot-clients/pinot-java-client/src/main/java/org/apache/pinot/client/BrokerCache.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.pinot.client;
+
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Random;
+import java.util.Set;
+import java.util.concurrent.Future;
+import org.apache.pinot.spi.utils.builder.ControllerRequestURLBuilder;
+import org.asynchttpclient.AsyncHttpClient;
+import org.asynchttpclient.BoundRequestBuilder;
+import org.asynchttpclient.Dsl;
+import org.asynchttpclient.Response;
+
+
+/**
+ * Maintains table -> list of brokers, supports update
+ * TODO can we introduce a simple websocket based controller endpoint to make 
the update reactive in the client?
+ */
+public class BrokerCache {
+
+  private static final TypeReference<Map<String, List<String>>> 
RESPONSE_TYPE_REF =
+      new TypeReference<Map<String, List<String>>>() {
+      };
+  private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
+  private final Random _random = new Random();
+  private final AsyncHttpClient _client;
+  private final String _address;
+  private volatile BrokerData _brokerData;
+
+  public BrokerCache(String scheme, String controllerHost, int controllerPort) 
{
+    _client = Dsl.asyncHttpClient();
+    ControllerRequestURLBuilder controllerRequestURLBuilder =
+        ControllerRequestURLBuilder.baseUrl(scheme + "://" + controllerHost + 
":" + controllerPort);
+    _address = controllerRequestURLBuilder.forLiveBrokerTablesGet();
+  }
+
+  protected void updateBrokerData()
+      throws Exception {
+    BoundRequestBuilder getRequest = _client.prepareGet(_address);
+    Future<Response> responseFuture = getRequest.addHeader("accept", 
"application/json").execute();
+    Response response = responseFuture.get();
+    String responseBody = response.getResponseBody(StandardCharsets.UTF_8);
+    Map<String, List<String>> responses = 
OBJECT_MAPPER.readValue(responseBody, RESPONSE_TYPE_REF);
+    List<String> brokers = new ArrayList<>();
+    Map<String, List<String>> tableToBrokersMap = new HashMap<>();
+    Set<String> uniqueTableNames = new HashSet<>();
+    for (Map.Entry<String, List<String>> tableToBrokers : 
responses.entrySet()) {
+      List<String> brokersForTable = new ArrayList<>();
+      tableToBrokers.getValue().forEach(br -> {
+        String[] split = br.split("_");
+        String brokerHostPort = split[1] + ":" + split[2];

Review Comment:
   This is not robust. We might have brokers with custom instance id that does 
not follow this naming convention. We should read all instance configs, and use 
the host/port info from the instance config



##########
pinot-clients/pinot-java-client/src/main/java/org/apache/pinot/client/BrokerCache.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.pinot.client;
+
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Random;
+import java.util.Set;
+import java.util.concurrent.Future;
+import org.apache.pinot.spi.utils.builder.ControllerRequestURLBuilder;
+import org.asynchttpclient.AsyncHttpClient;
+import org.asynchttpclient.BoundRequestBuilder;
+import org.asynchttpclient.Dsl;
+import org.asynchttpclient.Response;
+
+
+/**
+ * Maintains table -> list of brokers, supports update
+ * TODO can we introduce a simple websocket based controller endpoint to make 
the update reactive in the client?
+ */
+public class BrokerCache {
+
+  private static final TypeReference<Map<String, List<String>>> 
RESPONSE_TYPE_REF =
+      new TypeReference<Map<String, List<String>>>() {
+      };
+  private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
+  private final Random _random = new Random();
+  private final AsyncHttpClient _client;
+  private final String _address;
+  private volatile BrokerData _brokerData;
+
+  public BrokerCache(String scheme, String controllerHost, int controllerPort) 
{
+    _client = Dsl.asyncHttpClient();
+    ControllerRequestURLBuilder controllerRequestURLBuilder =
+        ControllerRequestURLBuilder.baseUrl(scheme + "://" + controllerHost + 
":" + controllerPort);
+    _address = controllerRequestURLBuilder.forLiveBrokerTablesGet();
+  }
+
+  protected void updateBrokerData()
+      throws Exception {
+    BoundRequestBuilder getRequest = _client.prepareGet(_address);
+    Future<Response> responseFuture = getRequest.addHeader("accept", 
"application/json").execute();
+    Response response = responseFuture.get();
+    String responseBody = response.getResponseBody(StandardCharsets.UTF_8);
+    Map<String, List<String>> responses = 
OBJECT_MAPPER.readValue(responseBody, RESPONSE_TYPE_REF);
+    List<String> brokers = new ArrayList<>();
+    Map<String, List<String>> tableToBrokersMap = new HashMap<>();
+    Set<String> uniqueTableNames = new HashSet<>();
+    for (Map.Entry<String, List<String>> tableToBrokers : 
responses.entrySet()) {
+      List<String> brokersForTable = new ArrayList<>();
+      tableToBrokers.getValue().forEach(br -> {
+        String[] split = br.split("_");
+        String brokerHostPort = split[1] + ":" + split[2];
+        brokersForTable.add(brokerHostPort);
+        if (!brokers.contains(brokerHostPort)) {
+          brokers.add(brokerHostPort);
+        }
+      });
+      String tableName = tableToBrokers.getKey();
+      tableToBrokersMap.put(tableName, brokersForTable);
+
+      String tableNameWithoutSuffix =
+          tableName.replace(ExternalViewReader.OFFLINE_SUFFIX, 
"").replace(ExternalViewReader.REALTIME_SUFFIX, "");

Review Comment:
   ```suggestion
             TableNameBuilder.extractRawTableName(tableNameWithType);
   ```



##########
pinot-clients/pinot-java-client/src/main/java/org/apache/pinot/client/BrokerCacheUpdaterManual.java:
##########
@@ -0,0 +1,55 @@
+/**
+ * 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.pinot.client;
+
+import java.util.List;
+
+
+/**
+ * Controller based broker cache that only supports manual refresh
+ */
+public class BrokerCacheUpdaterManual implements UpdatableBrokerCache {
+  private final BrokerCache _brokerCache;
+
+  public BrokerCacheUpdaterManual(String scheme, String controllerHost, int 
controllerPort) {
+    _brokerCache = new BrokerCache(scheme, controllerHost, controllerPort);
+  }
+
+  public void init() throws Exception { }
+
+  @Override
+  public String getBroker(String tableName) {
+    return _brokerCache.getBroker(tableName);
+  }
+
+  @Override
+  public List<String> getBrokers() {
+    return _brokerCache.getBrokers();
+  }
+
+  @Override
+  public void triggerBrokerCacheUpdate() throws Exception {

Review Comment:
   (code style) This does not follow `Pinot Style`



##########
pinot-clients/pinot-java-client/src/main/java/org/apache/pinot/client/BrokerCache.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.pinot.client;
+
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Random;
+import java.util.Set;
+import java.util.concurrent.Future;
+import org.apache.pinot.spi.utils.builder.ControllerRequestURLBuilder;
+import org.asynchttpclient.AsyncHttpClient;
+import org.asynchttpclient.BoundRequestBuilder;
+import org.asynchttpclient.Dsl;
+import org.asynchttpclient.Response;
+
+
+/**
+ * Maintains table -> list of brokers, supports update
+ * TODO can we introduce a simple websocket based controller endpoint to make 
the update reactive in the client?
+ */
+public class BrokerCache {
+
+  private static final TypeReference<Map<String, List<String>>> 
RESPONSE_TYPE_REF =
+      new TypeReference<Map<String, List<String>>>() {
+      };
+  private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
+  private final Random _random = new Random();
+  private final AsyncHttpClient _client;
+  private final String _address;
+  private volatile BrokerData _brokerData;
+
+  public BrokerCache(String scheme, String controllerHost, int controllerPort) 
{
+    _client = Dsl.asyncHttpClient();
+    ControllerRequestURLBuilder controllerRequestURLBuilder =
+        ControllerRequestURLBuilder.baseUrl(scheme + "://" + controllerHost + 
":" + controllerPort);
+    _address = controllerRequestURLBuilder.forLiveBrokerTablesGet();
+  }
+
+  protected void updateBrokerData()
+      throws Exception {
+    BoundRequestBuilder getRequest = _client.prepareGet(_address);
+    Future<Response> responseFuture = getRequest.addHeader("accept", 
"application/json").execute();
+    Response response = responseFuture.get();
+    String responseBody = response.getResponseBody(StandardCharsets.UTF_8);
+    Map<String, List<String>> responses = 
OBJECT_MAPPER.readValue(responseBody, RESPONSE_TYPE_REF);
+    List<String> brokers = new ArrayList<>();

Review Comment:
   Use a `Set` to avoid `contains` check which can be slow for lots of brokers



##########
pinot-clients/pinot-java-client/src/main/java/org/apache/pinot/client/BrokerCache.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.pinot.client;
+
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Random;
+import java.util.Set;
+import java.util.concurrent.Future;
+import org.apache.pinot.spi.utils.builder.ControllerRequestURLBuilder;
+import org.asynchttpclient.AsyncHttpClient;
+import org.asynchttpclient.BoundRequestBuilder;
+import org.asynchttpclient.Dsl;
+import org.asynchttpclient.Response;
+
+
+/**
+ * Maintains table -> list of brokers, supports update
+ * TODO can we introduce a simple websocket based controller endpoint to make 
the update reactive in the client?
+ */
+public class BrokerCache {
+
+  private static final TypeReference<Map<String, List<String>>> 
RESPONSE_TYPE_REF =
+      new TypeReference<Map<String, List<String>>>() {
+      };
+  private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
+  private final Random _random = new Random();
+  private final AsyncHttpClient _client;
+  private final String _address;
+  private volatile BrokerData _brokerData;
+
+  public BrokerCache(String scheme, String controllerHost, int controllerPort) 
{
+    _client = Dsl.asyncHttpClient();
+    ControllerRequestURLBuilder controllerRequestURLBuilder =
+        ControllerRequestURLBuilder.baseUrl(scheme + "://" + controllerHost + 
":" + controllerPort);
+    _address = controllerRequestURLBuilder.forLiveBrokerTablesGet();
+  }
+
+  protected void updateBrokerData()
+      throws Exception {
+    BoundRequestBuilder getRequest = _client.prepareGet(_address);
+    Future<Response> responseFuture = getRequest.addHeader("accept", 
"application/json").execute();
+    Response response = responseFuture.get();
+    String responseBody = response.getResponseBody(StandardCharsets.UTF_8);
+    Map<String, List<String>> responses = 
OBJECT_MAPPER.readValue(responseBody, RESPONSE_TYPE_REF);
+    List<String> brokers = new ArrayList<>();
+    Map<String, List<String>> tableToBrokersMap = new HashMap<>();
+    Set<String> uniqueTableNames = new HashSet<>();
+    for (Map.Entry<String, List<String>> tableToBrokers : 
responses.entrySet()) {
+      List<String> brokersForTable = new ArrayList<>();
+      tableToBrokers.getValue().forEach(br -> {
+        String[] split = br.split("_");
+        String brokerHostPort = split[1] + ":" + split[2];
+        brokersForTable.add(brokerHostPort);
+        if (!brokers.contains(brokerHostPort)) {
+          brokers.add(brokerHostPort);
+        }
+      });
+      String tableName = tableToBrokers.getKey();
+      tableToBrokersMap.put(tableName, brokersForTable);
+
+      String tableNameWithoutSuffix =

Review Comment:
   (minor) We usually call it `rawTableName`



##########
pinot-clients/pinot-java-client/src/main/java/org/apache/pinot/client/UpdatableBrokerCache.java:
##########
@@ -0,0 +1,40 @@
+/**
+ * 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.pinot.client;
+
+import java.util.List;
+
+
+/**
+ * A updatable cache of table name to list of eligible brokers.
+ * Implementations should implement manual refreshing mechanism
+ */
+public interface UpdatableBrokerCache {
+  void init()

Review Comment:
   Add some javadoc for all the APIs



##########
pinot-spi/pom.xml:
##########
@@ -149,6 +149,20 @@
       <groupId>org.reflections</groupId>
       <artifactId>reflections</artifactId>
     </dependency>
+    <dependency>
+      <groupId>org.apache.httpcomponents</groupId>

Review Comment:
   Suggest leaving the `URIUtils.buildURI()` in the original class so that we 
don't need to add the dependency here



##########
pinot-clients/pinot-java-client/src/main/java/org/apache/pinot/client/BrokerCache.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.pinot.client;
+
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Random;
+import java.util.Set;
+import java.util.concurrent.Future;
+import org.apache.pinot.spi.utils.builder.ControllerRequestURLBuilder;
+import org.asynchttpclient.AsyncHttpClient;
+import org.asynchttpclient.BoundRequestBuilder;
+import org.asynchttpclient.Dsl;
+import org.asynchttpclient.Response;
+
+
+/**
+ * Maintains table -> list of brokers, supports update
+ * TODO can we introduce a simple websocket based controller endpoint to make 
the update reactive in the client?
+ */
+public class BrokerCache {
+
+  private static final TypeReference<Map<String, List<String>>> 
RESPONSE_TYPE_REF =
+      new TypeReference<Map<String, List<String>>>() {
+      };
+  private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
+  private final Random _random = new Random();
+  private final AsyncHttpClient _client;
+  private final String _address;
+  private volatile BrokerData _brokerData;
+
+  public BrokerCache(String scheme, String controllerHost, int controllerPort) 
{
+    _client = Dsl.asyncHttpClient();
+    ControllerRequestURLBuilder controllerRequestURLBuilder =
+        ControllerRequestURLBuilder.baseUrl(scheme + "://" + controllerHost + 
":" + controllerPort);
+    _address = controllerRequestURLBuilder.forLiveBrokerTablesGet();
+  }
+
+  protected void updateBrokerData()
+      throws Exception {
+    BoundRequestBuilder getRequest = _client.prepareGet(_address);
+    Future<Response> responseFuture = getRequest.addHeader("accept", 
"application/json").execute();
+    Response response = responseFuture.get();
+    String responseBody = response.getResponseBody(StandardCharsets.UTF_8);
+    Map<String, List<String>> responses = 
OBJECT_MAPPER.readValue(responseBody, RESPONSE_TYPE_REF);
+    List<String> brokers = new ArrayList<>();
+    Map<String, List<String>> tableToBrokersMap = new HashMap<>();
+    Set<String> uniqueTableNames = new HashSet<>();
+    for (Map.Entry<String, List<String>> tableToBrokers : 
responses.entrySet()) {
+      List<String> brokersForTable = new ArrayList<>();
+      tableToBrokers.getValue().forEach(br -> {
+        String[] split = br.split("_");
+        String brokerHostPort = split[1] + ":" + split[2];
+        brokersForTable.add(brokerHostPort);
+        if (!brokers.contains(brokerHostPort)) {
+          brokers.add(brokerHostPort);
+        }
+      });
+      String tableName = tableToBrokers.getKey();
+      tableToBrokersMap.put(tableName, brokersForTable);
+
+      String tableNameWithoutSuffix =
+          tableName.replace(ExternalViewReader.OFFLINE_SUFFIX, 
"").replace(ExternalViewReader.REALTIME_SUFFIX, "");
+      uniqueTableNames.add(tableNameWithoutSuffix);
+    }
+
+    // Add table names without suffixes
+    uniqueTableNames.forEach(tableName -> {

Review Comment:
   Consider putting both raw table name and table name with type so that the 
java client supports query only offline/realtime table



##########
pinot-clients/pinot-java-client/src/main/java/org/apache/pinot/client/BrokerCache.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.pinot.client;
+
+import com.fasterxml.jackson.core.type.TypeReference;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import java.nio.charset.StandardCharsets;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Random;
+import java.util.Set;
+import java.util.concurrent.Future;
+import org.apache.pinot.spi.utils.builder.ControllerRequestURLBuilder;
+import org.asynchttpclient.AsyncHttpClient;
+import org.asynchttpclient.BoundRequestBuilder;
+import org.asynchttpclient.Dsl;
+import org.asynchttpclient.Response;
+
+
+/**
+ * Maintains table -> list of brokers, supports update
+ * TODO can we introduce a simple websocket based controller endpoint to make 
the update reactive in the client?
+ */
+public class BrokerCache {
+
+  private static final TypeReference<Map<String, List<String>>> 
RESPONSE_TYPE_REF =
+      new TypeReference<Map<String, List<String>>>() {
+      };
+  private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
+  private final Random _random = new Random();
+  private final AsyncHttpClient _client;
+  private final String _address;
+  private volatile BrokerData _brokerData;
+
+  public BrokerCache(String scheme, String controllerHost, int controllerPort) 
{
+    _client = Dsl.asyncHttpClient();
+    ControllerRequestURLBuilder controllerRequestURLBuilder =
+        ControllerRequestURLBuilder.baseUrl(scheme + "://" + controllerHost + 
":" + controllerPort);
+    _address = controllerRequestURLBuilder.forLiveBrokerTablesGet();
+  }
+
+  protected void updateBrokerData()
+      throws Exception {
+    BoundRequestBuilder getRequest = _client.prepareGet(_address);
+    Future<Response> responseFuture = getRequest.addHeader("accept", 
"application/json").execute();
+    Response response = responseFuture.get();
+    String responseBody = response.getResponseBody(StandardCharsets.UTF_8);
+    Map<String, List<String>> responses = 
OBJECT_MAPPER.readValue(responseBody, RESPONSE_TYPE_REF);
+    List<String> brokers = new ArrayList<>();
+    Map<String, List<String>> tableToBrokersMap = new HashMap<>();
+    Set<String> uniqueTableNames = new HashSet<>();
+    for (Map.Entry<String, List<String>> tableToBrokers : 
responses.entrySet()) {
+      List<String> brokersForTable = new ArrayList<>();
+      tableToBrokers.getValue().forEach(br -> {
+        String[] split = br.split("_");
+        String brokerHostPort = split[1] + ":" + split[2];
+        brokersForTable.add(brokerHostPort);
+        if (!brokers.contains(brokerHostPort)) {
+          brokers.add(brokerHostPort);
+        }
+      });
+      String tableName = tableToBrokers.getKey();
+      tableToBrokersMap.put(tableName, brokersForTable);
+
+      String tableNameWithoutSuffix =
+          tableName.replace(ExternalViewReader.OFFLINE_SUFFIX, 
"").replace(ExternalViewReader.REALTIME_SUFFIX, "");
+      uniqueTableNames.add(tableNameWithoutSuffix);
+    }
+
+    // Add table names without suffixes
+    uniqueTableNames.forEach(tableName -> {
+      if (!tableToBrokersMap.containsKey(tableName)) {
+        List<String> unifiedList =

Review Comment:
   We want to do a intersection instead of a union (this is not even union, but 
combining 2 lists)



##########
pinot-clients/pinot-java-client/src/main/java/org/apache/pinot/client/BrokerCacheUpdaterManual.java:
##########
@@ -0,0 +1,55 @@
+/**
+ * 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.pinot.client;
+
+import java.util.List;
+
+
+/**
+ * Controller based broker cache that only supports manual refresh
+ */
+public class BrokerCacheUpdaterManual implements UpdatableBrokerCache {

Review Comment:
   Consider merging this with the periodic one. If the frequency is 
non-positive, then it is manual mode. Periodic one should also support manual 
update



##########
pinot-clients/pinot-java-client/src/main/java/org/apache/pinot/client/BrokerCacheUpdaterManual.java:
##########
@@ -0,0 +1,55 @@
+/**
+ * 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.pinot.client;
+
+import java.util.List;
+
+
+/**
+ * Controller based broker cache that only supports manual refresh
+ */
+public class BrokerCacheUpdaterManual implements UpdatableBrokerCache {
+  private final BrokerCache _brokerCache;
+
+  public BrokerCacheUpdaterManual(String scheme, String controllerHost, int 
controllerPort) {
+    _brokerCache = new BrokerCache(scheme, controllerHost, controllerPort);
+  }
+
+  public void init() throws Exception { }

Review Comment:
   (code style) This does not follow `Pinot Style`
   ```suggestion
     public void init()
         throws Exception {
     }
   ```



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