ankitsultana commented on code in PR #17296:
URL: https://github.com/apache/pinot/pull/17296#discussion_r2629149137


##########
pinot-broker/src/main/java/org/apache/pinot/broker/routing/RemoteClusterBrokerRoutingManager.java:
##########
@@ -0,0 +1,123 @@
+/**
+ * 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.broker.routing;
+
+import com.google.common.annotations.VisibleForTesting;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.stream.Collectors;
+import org.apache.pinot.common.metadata.ZKMetadataProvider;
+import org.apache.pinot.common.metrics.BrokerMetrics;
+import org.apache.pinot.common.metrics.BrokerTimer;
+import 
org.apache.pinot.core.transport.server.routing.stats.ServerRoutingStatsManager;
+import org.apache.pinot.spi.env.PinotConfiguration;
+import org.apache.pinot.spi.utils.builder.TableNameBuilder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * The {@code RemoteClusterBrokerRoutingManager} manages the routing for a 
single remote Pinot cluster available for
+ * federation. It periodically checks for changes in the set of tables 
available in the remote cluster and updates the
+ * routing accordingly.
+ */
+public class RemoteClusterBrokerRoutingManager extends BrokerRoutingManager {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(RemoteClusterBrokerRoutingManager.class);
+  private static final long ROUTING_CHANGE_DETECTION_INTERVAL_MS = 10_000L;
+
+  private final String _remoteClusterName;
+  private final AtomicBoolean _processChangeInRouting = new 
AtomicBoolean(false);
+  private final ScheduledExecutorService _routingChangeExecutor;
+
+  public RemoteClusterBrokerRoutingManager(String remoteClusterName, 
BrokerMetrics brokerMetrics,
+      ServerRoutingStatsManager serverRoutingStatsManager, PinotConfiguration 
pinotConfig) {
+    super(brokerMetrics, serverRoutingStatsManager, pinotConfig);
+    _remoteClusterName = remoteClusterName;
+    _routingChangeExecutor = Executors.newSingleThreadScheduledExecutor();
+    
_routingChangeExecutor.scheduleAtFixedRate(this::determineRoutingChangeForTables,
 0,
+        ROUTING_CHANGE_DETECTION_INTERVAL_MS, TimeUnit.MILLISECONDS);
+  }
+
+  public void shutdown() {
+    _routingChangeExecutor.shutdownNow();
+  }
+
+  public void determineRoutingChangeForTables() {
+    if (!_processChangeInRouting.getAndSet(false)) {
+      return;
+    }
+    LOGGER.info("Processing routing changes for remote cluster: {}", 
_remoteClusterName);
+    long startTimeMs = System.currentTimeMillis();
+
+    String externalViewPath = _externalViewPathPrefix.substring(0, 
_externalViewPathPrefix.length() - 1);
+    Set<String> currentTables = 
_zkDataAccessor.getChildNames(externalViewPath, 0).stream()
+        .filter(t -> TableNameBuilder.getTableTypeFromTableName(t) != null)
+        .collect(Collectors.toSet());
+    Set<String> knownTables = new HashSet<>(_routingEntryMap.keySet());
+
+    Set<String> toAdd = new HashSet<>(currentTables);
+    toAdd.removeAll(knownTables);
+    Set<String> toRemove = new HashSet<>(knownTables);
+    toRemove.removeAll(currentTables);
+
+    toAdd.forEach(this::addRouting);
+    toRemove.forEach(this::dropRouting);
+
+    _brokerMetrics.addTimedValue(_remoteClusterName, 
BrokerTimer.REMOTE_CLUSTER_BROKER_ROUTING_CALCULATION_TIME_MS,
+        System.currentTimeMillis() - startTimeMs, TimeUnit.MILLISECONDS);
+  }
+
+  private void addRouting(String table) {
+    LOGGER.info("Adding routing for table: {} in cluster: {}", table, 
_remoteClusterName);
+    if (ZKMetadataProvider.isLogicalTableExists(_propertyStore, table)) {

Review Comment:
   related but doesn't apply to this line directly: if a logical table ends up 
having the same name as an actual table, what could be the side-effects across 
all of Pinot's components?
   
   Also, what if a user ends up creating a logical table with the same name as 
the actual table that it is based on. Do we have enough validations to prevent 
chaos from that?



##########
pinot-broker/src/main/java/org/apache/pinot/broker/routing/RemoteClusterBrokerRoutingManager.java:
##########
@@ -0,0 +1,123 @@
+/**
+ * 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.broker.routing;
+
+import com.google.common.annotations.VisibleForTesting;
+import java.util.HashSet;
+import java.util.Set;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.stream.Collectors;
+import org.apache.pinot.common.metadata.ZKMetadataProvider;
+import org.apache.pinot.common.metrics.BrokerMetrics;
+import org.apache.pinot.common.metrics.BrokerTimer;
+import 
org.apache.pinot.core.transport.server.routing.stats.ServerRoutingStatsManager;
+import org.apache.pinot.spi.env.PinotConfiguration;
+import org.apache.pinot.spi.utils.builder.TableNameBuilder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+/**
+ * The {@code RemoteClusterBrokerRoutingManager} manages the routing for a 
single remote Pinot cluster available for
+ * federation. It periodically checks for changes in the set of tables 
available in the remote cluster and updates the
+ * routing accordingly.
+ */
+public class RemoteClusterBrokerRoutingManager extends BrokerRoutingManager {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(RemoteClusterBrokerRoutingManager.class);
+  private static final long ROUTING_CHANGE_DETECTION_INTERVAL_MS = 10_000L;
+
+  private final String _remoteClusterName;
+  private final AtomicBoolean _processChangeInRouting = new 
AtomicBoolean(false);
+  private final ScheduledExecutorService _routingChangeExecutor;
+
+  public RemoteClusterBrokerRoutingManager(String remoteClusterName, 
BrokerMetrics brokerMetrics,
+      ServerRoutingStatsManager serverRoutingStatsManager, PinotConfiguration 
pinotConfig) {
+    super(brokerMetrics, serverRoutingStatsManager, pinotConfig);
+    _remoteClusterName = remoteClusterName;
+    _routingChangeExecutor = Executors.newSingleThreadScheduledExecutor();
+    
_routingChangeExecutor.scheduleAtFixedRate(this::determineRoutingChangeForTables,
 0,
+        ROUTING_CHANGE_DETECTION_INTERVAL_MS, TimeUnit.MILLISECONDS);
+  }
+
+  public void shutdown() {
+    _routingChangeExecutor.shutdownNow();
+  }
+
+  public void determineRoutingChangeForTables() {
+    if (!_processChangeInRouting.getAndSet(false)) {
+      return;
+    }
+    LOGGER.info("Processing routing changes for remote cluster: {}", 
_remoteClusterName);
+    long startTimeMs = System.currentTimeMillis();
+
+    String externalViewPath = _externalViewPathPrefix.substring(0, 
_externalViewPathPrefix.length() - 1);

Review Comment:
   nit: recommend adding a precondition check here to ensure that the prefix 
endsWith `/`



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