cryptoe commented on code in PR #17899:
URL: https://github.com/apache/druid/pull/17899#discussion_r2054336133


##########
server/src/main/java/org/apache/druid/client/BrokerViewOfCoordinatorConfig.java:
##########
@@ -0,0 +1,138 @@
+/*
+ * 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.client;
+
+import com.google.common.collect.ImmutableSet;
+import com.google.errorprone.annotations.concurrent.GuardedBy;
+import com.google.inject.Inject;
+import it.unimi.dsi.fastutil.ints.Int2ObjectRBTreeMap;
+import org.apache.druid.client.coordinator.CoordinatorClient;
+import org.apache.druid.client.selector.HistoricalFilter;
+import org.apache.druid.java.util.common.lifecycle.LifecycleStart;
+import org.apache.druid.java.util.common.logger.Logger;
+import org.apache.druid.query.CloneQueryMode;
+import org.apache.druid.server.DruidInternalDynamicConfigResource;
+import org.apache.druid.server.coordinator.CoordinatorDynamicConfig;
+
+import javax.validation.constraints.NotNull;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/**
+ * Broker view of the coordinator dynamic configuration, and its derived 
values such as target and source clone servers.
+ * This class is registered as a managed lifecycle to fetch the coordinator 
dynamic configuration on startup. Further
+ * updates are handled through {@link DruidInternalDynamicConfigResource}.
+ */
+public class BrokerViewOfCoordinatorConfig implements HistoricalFilter
+{
+  private static final Logger log = new 
Logger(BrokerViewOfCoordinatorConfig.class);
+  private final CoordinatorClient coordinatorClient;
+
+  @GuardedBy("this")
+  private CoordinatorDynamicConfig config;
+  @GuardedBy("this")
+  private Set<String> cloneServers;
+  @GuardedBy("this")
+  private Set<String> serversBeingCloned;
+
+  @Inject
+  public BrokerViewOfCoordinatorConfig(CoordinatorClient coordinatorClient)
+  {
+    this.coordinatorClient = coordinatorClient;
+  }
+
+  public synchronized CoordinatorDynamicConfig getDynamicConfiguration()
+  {
+    return config;
+  }
+
+  /**
+   * Update the config view with a new coordinator dynamic config snapshot. 
Also updates the source and target clone
+   * servers based on the new dynamic configuration.
+   */
+  public synchronized void setDynamicConfig(@NotNull CoordinatorDynamicConfig 
updatedConfig)
+  {
+    config = updatedConfig;
+    final Map<String, String> cloneServers = config.getCloneServers();
+    this.cloneServers = ImmutableSet.copyOf(cloneServers.keySet());
+    this.serversBeingCloned = ImmutableSet.copyOf(cloneServers.values());
+  }
+
+  @LifecycleStart
+  public void start()
+  {
+    try {
+      log.info("Fetching coordinator dynamic configuration.");
+
+      CoordinatorDynamicConfig coordinatorDynamicConfig = 
coordinatorClient.getCoordinatorDynamicConfig().get();
+      setDynamicConfig(coordinatorDynamicConfig);
+
+      log.info("Successfully initialized dynamic config: [%s]", 
coordinatorDynamicConfig);
+    }
+    catch (Exception e) {
+      // If the fetch fails, the broker should not serve queries. Throw the 
exception and try again on restart.
+      log.error(e, "Failed to initialize coordinator dynamic config");
+      throw new RuntimeException(e);
+    }
+  }
+
+  @Override
+  public Int2ObjectRBTreeMap<Set<QueryableDruidServer>> getQueryableServers(
+      Int2ObjectRBTreeMap<Set<QueryableDruidServer>> historicalServers,
+      CloneQueryMode mode
+  )
+  {
+    final Set<String> serversToIgnore = getCurrentServersToIgnore(mode);
+
+    if (serversToIgnore.isEmpty()) {
+      return historicalServers;
+    }
+
+    final Int2ObjectRBTreeMap<Set<QueryableDruidServer>> filteredHistoricals = 
new Int2ObjectRBTreeMap<>();
+    for (int priority : historicalServers.keySet()) {
+      Set<QueryableDruidServer> servers = historicalServers.get(priority);
+      filteredHistoricals.put(priority,
+                              servers.stream()
+                                     .filter(server -> 
!serversToIgnore.contains(server.getServer().getHost()))
+                                     .collect(Collectors.toSet())
+      );
+    }
+
+    return filteredHistoricals;
+  }
+
+  private synchronized Set<String> getCurrentServersToIgnore(CloneQueryMode 
cloneQueryMode)
+  {
+    switch (cloneQueryMode) {
+      case CLONES_PREFERRED:
+        // Remove servers being cloned targets, so that clones are queried.
+        return serversBeingCloned;

Review Comment:
   Since we are iterating an immutable map, I guess it should be fine. Bdw, I 
think these changes are much more performant. 
   Thanks for this. 



##########
server/src/main/java/org/apache/druid/server/http/BrokerSyncStatus.java:
##########
@@ -0,0 +1,99 @@
+/*
+ * 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.server.http;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import org.apache.druid.rpc.ServiceLocation;
+
+import java.util.Objects;
+
+public class BrokerSyncStatus
+{
+  private final String host;
+  private final int port;
+  private final long syncTime;
+
+  @JsonCreator
+  public BrokerSyncStatus(
+      @JsonProperty("host") String host,
+      @JsonProperty("port") int port,
+      @JsonProperty("syncTime") long syncTime

Review Comment:
   ```suggestion
         @JsonProperty("syncTimeInMs") long syncTimeInMs
   ```



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