magibney 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_r310278050
########## 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: That would certainly be more succinct. I think the intention here was to be relaxed about parsing (i.e., if you get a garbage dividend param, fall back to hashing the hashParam -- the assumption being that we're doing our best to honor the user's request for _stability_, one way or another). Given that approach, recovering from SolrParams.getInt(String) would have required catching a SolrException, which felt weird to me for some reason. Assuming a change to SolrParams.getInt(String), do you think it makes sense to just be strict, or to be forgiving and catch/log the SolrException that could result from a non-integer dividendParam? (I'm generally in favor of strictness, but in this case it feels a little arbitrary, since param in question doesn't affect the ability to return accurate results ...) ---------------------------------------------------------------- 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 With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@lucene.apache.org For additional commands, e-mail: dev-h...@lucene.apache.org