cpoerschke commented on a change in pull request #677: SOLR-13257: support for stable replica routing preferences URL: https://github.com/apache/lucene-solr/pull/677#discussion_r310710025
########## File path: solr/core/src/java/org/apache/solr/handler/component/AffinityReplicaListTransformer.java ########## @@ -0,0 +1,122 @@ +/* + * 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.handler.component; + +import java.lang.invoke.MethodHandles; +import java.util.Arrays; +import java.util.Comparator; +import java.util.List; +import java.util.ListIterator; +import org.apache.solr.common.cloud.Replica; +import org.apache.solr.common.params.SolrParams; +import org.apache.solr.common.util.Hash; +import org.apache.solr.request.SolrQueryRequest; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Allows better caching by establishing deterministic evenly-distributed replica routing preferences according to + * either explicitly configured hash routing parameter, or the hash of a query parameter (configurable, usually related + * to the main query). + */ +public class AffinityReplicaListTransformer implements ReplicaListTransformer { + + private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); + + private final int routingDividend; + + public AffinityReplicaListTransformer(String hashVal) { + this.routingDividend = Math.abs(Hash.lookup3ycs(hashVal, 0, hashVal.length(), 0)); + } + + public AffinityReplicaListTransformer(int routingDividend) { + this.routingDividend = routingDividend; + } + + /** + * + * @param dividendParam int param to be used directly for mod-based routing + * @param hashParam String param to be hashed into an int for mod-based routing + * @param req the request from which param values will be drawn + * @return null if specified routing vals are not able to be parsed properly + */ + public static ReplicaListTransformer getInstance(String dividendParam, String hashParam, SolrQueryRequest req) { + SolrParams params = req.getOriginalParams(); + String dividendVal; + if (dividendParam != null && (dividendVal = params.get(dividendParam)) != null && !dividendVal.isEmpty()) { + try { + return new AffinityReplicaListTransformer(Integer.parseInt(dividendVal)); Review comment: Yes, that's an interesting question, how to handle problematic input, in a forgiving or in a strict way. I'd tend to favour strictness in this case probably since the parameters would be within the Solr client's control (i.e. they are unlikely to be direct user input) or they would be part of the configuration via default or invariant elements in the config. So the queries would then relatively obviously fail and the client code can be fixed. If the queries succeed (with warnings logged) then it might be less apparent to Solr's client that something is amiss i.e. that routing and performance and cache hits etc. did not happen as intended, despite it returning, as you say, accurate results. A semi-related edge case might also be affinity ambiguity i.e. both a dividend param and a hash param being supplied, though that ambiguity could be eliminated via documenation too. ---------------------------------------------------------------- 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: [email protected] With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
