saurabhd336 commented on code in PR #8467: URL: https://github.com/apache/pinot/pull/8467#discussion_r854134359
########## 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: Ack -- 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: commits-unsubscr...@pinot.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org For additional commands, e-mail: commits-h...@pinot.apache.org