anshumg commented on a change in pull request #2010:
URL: https://github.com/apache/lucene-solr/pull/2010#discussion_r520351889



##########
File path: solr/solrj/src/java/org/apache/solr/common/cloud/UrlScheme.java
##########
@@ -0,0 +1,260 @@
+/*
+ * 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.solr.common.cloud;
+
+import java.io.IOException;
+import java.lang.invoke.MethodHandles;
+import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
+import java.util.Map;
+import java.util.Objects;
+import java.util.SortedSet;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.solr.common.util.Utils;
+import org.apache.zookeeper.KeeperException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static org.apache.solr.common.cloud.ZkStateReader.URL_SCHEME;
+
+/**
+ * Singleton access to global urlScheme, which although is stored in ZK as a 
cluster property
+ * really should be treated like a static global that is set at initialization 
and not altered after.
+ *
+ * Client applications should not use this class directly; it is only included 
in SolrJ because Replica
+ * and ZkNodeProps depend on it.
+ */
+public enum UrlScheme implements LiveNodesListener, ClusterPropertiesListener {
+  INSTANCE;
+
+  private static final Logger log = 
LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+
+  public static final String HTTP = "http";
+  public static final String HTTPS = "https";
+  public static final String HTTPS_PORT_PROP = "solr.jetty.https.port";
+  public static final String USE_LIVENODES_URL_SCHEME = 
"ext.useLiveNodesUrlScheme";
+
+  private volatile String urlScheme = HTTP;
+  private volatile boolean useLiveNodesUrlScheme = false;
+  private volatile SortedSet<String> liveNodes = null;
+  private volatile SolrZkClient zkClient = null;
+
+  private final ConcurrentMap<String,String> nodeSchemeCache = new 
ConcurrentHashMap<>();
+
+  /**
+   * Called during ZkController initialization to set the urlScheme based on 
cluster properties.
+   * @param client The SolrZkClient needed to read cluster properties from ZK.
+   * @throws IOException If a connection or other I/O related error occurs 
while reading from ZK.
+   */
+  public void initFromClusterProps(final SolrZkClient client) throws 
IOException {
+    this.zkClient = client;
+
+    // Have to go directly to the cluster props b/c this needs to happen 
before ZkStateReader does its thing
+    ClusterProperties clusterProps = new ClusterProperties(client);
+    this.useLiveNodesUrlScheme =
+      
"true".equals(clusterProps.getClusterProperty(UrlScheme.USE_LIVENODES_URL_SCHEME,
 "false"));
+    setUrlSchemeFromClusterProps(clusterProps.getClusterProperties());
+  }
+
+  private void setUrlSchemeFromClusterProps(Map<String,Object> props) {
+    // Set the global urlScheme from cluster prop or if that is not set, look 
at the urlScheme sys prop
+    final String scheme = (String)props.get(ZkStateReader.URL_SCHEME);
+    if (StringUtils.isNotEmpty(scheme)) {
+      // track the urlScheme in a global so we can use it during ZK read / 
write operations for cluster state objects
+      this.urlScheme = HTTPS.equals(scheme) ? HTTPS : HTTP;
+    } else {
+      String urlSchemeFromSysProp = System.getProperty(URL_SCHEME, HTTP);
+      if (HTTPS.equals(urlSchemeFromSysProp)) {
+        log.warn("Cluster property 'urlScheme' not set but system property is 
set to 'https'. " +
+            "You should set the cluster property and restart all nodes for 
consistency.");

Review comment:
       Should we instead mention that Solr doesn't support partial TLS enabled 
clusters?

##########
File path: solr/solrj/src/java/org/apache/solr/common/cloud/UrlScheme.java
##########
@@ -0,0 +1,260 @@
+/*
+ * 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.solr.common.cloud;
+
+import java.io.IOException;
+import java.lang.invoke.MethodHandles;
+import java.net.URLEncoder;
+import java.nio.charset.StandardCharsets;
+import java.util.Map;
+import java.util.Objects;
+import java.util.SortedSet;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+
+import org.apache.commons.lang.StringUtils;
+import org.apache.solr.common.util.Utils;
+import org.apache.zookeeper.KeeperException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static org.apache.solr.common.cloud.ZkStateReader.URL_SCHEME;
+
+/**
+ * Singleton access to global urlScheme, which although is stored in ZK as a 
cluster property
+ * really should be treated like a static global that is set at initialization 
and not altered after.
+ *
+ * Client applications should not use this class directly; it is only included 
in SolrJ because Replica
+ * and ZkNodeProps depend on it.
+ */
+public enum UrlScheme implements LiveNodesListener, ClusterPropertiesListener {
+  INSTANCE;
+
+  private static final Logger log = 
LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+
+  public static final String HTTP = "http";
+  public static final String HTTPS = "https";
+  public static final String HTTPS_PORT_PROP = "solr.jetty.https.port";
+  public static final String USE_LIVENODES_URL_SCHEME = 
"ext.useLiveNodesUrlScheme";
+
+  private volatile String urlScheme = HTTP;
+  private volatile boolean useLiveNodesUrlScheme = false;
+  private volatile SortedSet<String> liveNodes = null;
+  private volatile SolrZkClient zkClient = null;
+
+  private final ConcurrentMap<String,String> nodeSchemeCache = new 
ConcurrentHashMap<>();
+
+  /**
+   * Called during ZkController initialization to set the urlScheme based on 
cluster properties.
+   * @param client The SolrZkClient needed to read cluster properties from ZK.
+   * @throws IOException If a connection or other I/O related error occurs 
while reading from ZK.
+   */
+  public void initFromClusterProps(final SolrZkClient client) throws 
IOException {
+    this.zkClient = client;
+
+    // Have to go directly to the cluster props b/c this needs to happen 
before ZkStateReader does its thing
+    ClusterProperties clusterProps = new ClusterProperties(client);
+    this.useLiveNodesUrlScheme =
+      
"true".equals(clusterProps.getClusterProperty(UrlScheme.USE_LIVENODES_URL_SCHEME,
 "false"));
+    setUrlSchemeFromClusterProps(clusterProps.getClusterProperties());
+  }
+
+  private void setUrlSchemeFromClusterProps(Map<String,Object> props) {
+    // Set the global urlScheme from cluster prop or if that is not set, look 
at the urlScheme sys prop
+    final String scheme = (String)props.get(ZkStateReader.URL_SCHEME);
+    if (StringUtils.isNotEmpty(scheme)) {
+      // track the urlScheme in a global so we can use it during ZK read / 
write operations for cluster state objects
+      this.urlScheme = HTTPS.equals(scheme) ? HTTPS : HTTP;
+    } else {
+      String urlSchemeFromSysProp = System.getProperty(URL_SCHEME, HTTP);
+      if (HTTPS.equals(urlSchemeFromSysProp)) {
+        log.warn("Cluster property 'urlScheme' not set but system property is 
set to 'https'. " +
+            "You should set the cluster property and restart all nodes for 
consistency.");
+      }
+      // TODO: We may not want this? See: 
https://issues.apache.org/jira/browse/SOLR-10202
+      this.urlScheme = HTTPS.equals(urlSchemeFromSysProp) ? HTTPS : HTTP;
+    }
+  }
+
+  public boolean isOnServer() {
+    return zkClient != null;
+  }
+
+  /**
+   * Set the global urlScheme variable; ideally this should be immutable once 
set, but some tests rely on changing

Review comment:
       👍🏽




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

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org
For additional commands, e-mail: issues-h...@lucene.apache.org

Reply via email to