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_r310721190
########## 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 makes sense; I agree that strict is the way to go here, for the reasons you state. I'll also address the priority of dividend vs. hash param in the forthcoming revised documentation. ---------------------------------------------------------------- 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