Skip to content

Apache Lucene open-source search software

SOLR-12662: Eliminate possible race conditions by moving Type-by-name map construction to Variable.Type, accessible via Variable.Type.get(name)

Steve Rowe098f475a671c
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/Variable.java b/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/Variable.java
index d817c2f94c0..1ffb0a5372d 100644
--- a/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/Variable.java
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/Variable.java
@@ -22,7 +22,9 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Arrays;
+import java.util.HashMap;
import java.util.HashSet;
+import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
@@ -30,6 +32,7 @@ import org.apache.solr.common.cloud.rule.ImplicitSnitch;
import static java.util.Collections.emptySet;
import static java.util.Collections.unmodifiableSet;
+import static java.util.Collections.unmodifiableMap;
/**
* A Variable Type used in Autoscaling policy rules. Each variable type may have unique implementation
@@ -328,6 +331,18 @@ public interface Variable {
public boolean match(Object inputVal, Operand op, Object val, String name, Row row) {
return impl.match(inputVal, op, val, name, row);
}
+
+ private static final Map<String, Type> typeByNameMap;
+ static {
+ HashMap<String, Type> m = new HashMap<>();
+ for (Type t : Type.values()) {
+ m.put(t.tagName, t);
+ }
+ typeByNameMap = unmodifiableMap(m);
+ }
+ static Type get(String name) {
+ return typeByNameMap.get(name);
+ }
}
@Target(ElementType.FIELD)
diff --git a/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/VariableBase.java b/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/VariableBase.java
index 8b0c1cf52ea..82a5ce6165c 100644
--- a/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/VariableBase.java
+++ b/solr/solrj/src/java/org/apache/solr/client/solrj/cloud/autoscaling/VariableBase.java
@@ -17,8 +17,6 @@
package org.apache.solr.client.solrj.cloud.autoscaling;
-import java.util.HashMap;
-import java.util.Map;
import org.apache.solr.common.cloud.rule.ImplicitSnitch;
import org.apache.solr.common.util.StrUtils;
@@ -77,7 +75,7 @@ public class VariableBase implements Variable {
}
public static Type getTagType(String name) {
- Type info = getValidatetypes().get(name);
+ Type info = Type.get(name);
if (info == null && name.startsWith(ImplicitSnitch.SYSPROP)) info = Type.STRING;
if (info == null && name.startsWith(Clause.METRICS_PREFIX)) info = Type.LAZY;
return info;
@@ -192,19 +190,5 @@ public class VariableBase implements Variable {
public void getSuggestions(Suggestion.Ctx ctx) {
perNodeSuggestions(ctx);
}
-
-
- }
-
- private static Map<String, Type> validatetypes = null;
-
- /** SOLR-12662: Lazily init validatetypes to avoid Type.values() NPE due to static initializer ordering */
- private static Map<String, Type> getValidatetypes() {
- if (validatetypes == null) {
- validatetypes = new HashMap<>();
- for (Type t : Type.values())
- validatetypes.put(t.tagName, t);
- }
- return validatetypes;
}
}