summaryrefslogtreecommitdiffstats
path: root/config-model
diff options
context:
space:
mode:
authorHarald Musum <musum@yahooinc.com>2022-10-21 11:09:17 +0200
committerHarald Musum <musum@yahooinc.com>2022-10-21 11:09:17 +0200
commit9df3c42278fc2b58f4be3185099a56d2faaa9005 (patch)
tree37bd5789120971148b7b9fea11a558b881ec111e /config-model
parent2bd351f58044c4a0418116d5c3418ef1c3bd25d0 (diff)
Remove unused class
Diffstat (limited to 'config-model')
-rw-r--r--config-model/src/main/java/com/yahoo/vespa/model/InstanceResolver.java123
-rw-r--r--config-model/src/main/java/com/yahoo/vespa/model/VespaModel.java9
-rw-r--r--config-model/src/test/java/com/yahoo/vespa/model/InstanceResolverTest.java236
3 files changed, 3 insertions, 365 deletions
diff --git a/config-model/src/main/java/com/yahoo/vespa/model/InstanceResolver.java b/config-model/src/main/java/com/yahoo/vespa/model/InstanceResolver.java
deleted file mode 100644
index 4c24678d4ac..00000000000
--- a/config-model/src/main/java/com/yahoo/vespa/model/InstanceResolver.java
+++ /dev/null
@@ -1,123 +0,0 @@
-// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
-package com.yahoo.vespa.model;
-
-import com.yahoo.config.ConfigBuilder;
-import com.yahoo.config.codegen.CNode;
-import com.yahoo.config.codegen.InnerCNode;
-import com.yahoo.config.codegen.LeafCNode;
-import com.yahoo.vespa.config.ConfigDefinitionKey;
-import com.yahoo.yolean.Exceptions;
-
-import java.lang.reflect.Field;
-import java.lang.reflect.Method;
-import java.util.List;
-import java.util.Map;
-
-
-/**
- * <p>
- * This class is capable of resolving config from a config model for a given request. It will handle
- * incompatibilities of the def version in the request and the version of the config classes the model
- * is using.
- * </p>
- * <p>
- * This class is agnostic of transport protocol and server implementation.
- * </p>
- * <p>
- * Thread safe.
- * </p>
- *
- * @author Vegard Havdal
- */
-// TODO: Most of this has been copied to ConfigInstance.Builder.buildInstance() and can be removed from here
-// when Model.getConfig is removed
-class InstanceResolver {
-
- private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(InstanceResolver.class.getName());
-
- /**
- * If some fields on the builder are null now, set them from the def. Do recursively.
- * <p>
- * If the targetDef has some schema incompatibilities, they are not handled here
- * (except logging in some cases), but in ConfigInstance.serialize().
- *
- * @param builder a {@link com.yahoo.config.ConfigBuilder}
- * @param targetDef a config definition
- * @throws Exception if applying values form config definitions fails
- */
- static void applyDef(ConfigBuilder builder, InnerCNode targetDef) throws Exception {
- for (Map.Entry<String, CNode> e: targetDef.children().entrySet()) {
- CNode node = e.getValue();
- if (node instanceof LeafCNode) {
- setLeafValueIfUnset(targetDef, builder, (LeafCNode)node);
- } else if (node instanceof InnerCNode) {
- // Is there a private field on the builder that matches this inner node in the def?
- if (hasField(builder.getClass(), node.getName())) {
- Field innerField = builder.getClass().getDeclaredField(node.getName());
- innerField.setAccessible(true);
- Object innerFieldVal = innerField.get(builder);
- if (innerFieldVal instanceof List) {
- // inner array? Check that list elems are ConfigBuilder
- List<?> innerList = (List<?>) innerFieldVal;
- for (Object b : innerList) {
- if (b instanceof ConfigBuilder) {
- applyDef((ConfigBuilder) b, (InnerCNode) node);
- }
- }
- } else if (innerFieldVal instanceof ConfigBuilder) {
- // Struct perhaps
- applyDef((ConfigBuilder) innerFieldVal, (InnerCNode) node);
- } else {
- // Likely a config value mismatch. That is handled in ConfigInstance.serialize() (error message, omit from response.)
- }
- }
- }
- }
- }
-
- private static boolean hasField(Class<?> aClass, String name) {
- for (Field field : aClass.getDeclaredFields()) {
- if (name.equals(field.getName())) {
- return true;
- }
- }
- return false;
- }
-
- private static void setLeafValueIfUnset(InnerCNode targetDef, Object builder, LeafCNode node) throws Exception {
- if (hasField(builder.getClass(), node.getName())) {
- Field field = builder.getClass().getDeclaredField(node.getName());
- field.setAccessible(true);
- Object val = field.get(builder);
- if (val==null) {
- // Not set on builder, if the leaf node has a default value, try the private setter that takes String
- try {
- if (node.getDefaultValue()!=null) {
- Method setter = builder.getClass().getDeclaredMethod(node.getName(), String.class);
- setter.setAccessible(true);
- setter.invoke(builder, node.getDefaultValue().getValue());
- }
- } catch (Exception e) {
- log.severe("For config '"+targetDef.getFullName()+"': Unable to apply the default value for field '"+node.getName()+
- "' to config Builder (where it wasn't set): "+
- Exceptions.toMessageString(e));
- }
- }
- }
- }
-
- static String packageName(ConfigDefinitionKey cKey, PackagePrefix packagePrefix) {
- return packagePrefix.value + cKey.getNamespace();
- }
-
- enum PackagePrefix {
- COM_YAHOO("com.yahoo."),
- NONE("");
-
- final String value;
- PackagePrefix (String value) {
- this.value = value;
- }
- }
-
-}
diff --git a/config-model/src/main/java/com/yahoo/vespa/model/VespaModel.java b/config-model/src/main/java/com/yahoo/vespa/model/VespaModel.java
index 86402981898..f00227c2ee8 100644
--- a/config-model/src/main/java/com/yahoo/vespa/model/VespaModel.java
+++ b/config-model/src/main/java/com/yahoo/vespa/model/VespaModel.java
@@ -38,7 +38,6 @@ import com.yahoo.vespa.config.ConfigDefinitionKey;
import com.yahoo.vespa.config.ConfigKey;
import com.yahoo.vespa.config.ConfigPayloadBuilder;
import com.yahoo.vespa.config.GenericConfig.GenericConfigBuilder;
-import com.yahoo.vespa.model.InstanceResolver.PackagePrefix;
import com.yahoo.vespa.model.admin.Admin;
import com.yahoo.vespa.model.builder.VespaModelBuilder;
import com.yahoo.vespa.model.builder.xml.dom.VespaDomBuilder;
@@ -512,9 +511,6 @@ public final class VespaModel extends AbstractConfigProducerRoot implements Mode
clazz = classLoader.loadClass(builderName);
} catch (ClassNotFoundException e) {
log.log(Level.FINE, () -> "Tried to load " + builderName + ", not found, trying with generic builder");
- // TODO: Enable config compiler when configserver is using new API.
- // ConfigCompiler compiler = new LazyConfigCompiler(Files.createTempDir());
- // return compiler.compile(targetDef.generateClass()).newInstance();
return new GenericConfigBuilder(key, new ConfigPayloadBuilder());
}
Object i;
@@ -536,8 +532,9 @@ public final class VespaModel extends AbstractConfigProducerRoot implements Mode
* returns the full class name with prefix, and null for the class loader.
*/
private Pair<String, ClassLoader> getClassLoaderForProducer(ConfigDefinitionKey key, String shortClassName) {
- String fullClassNameWithComYahoo = InstanceResolver.packageName(key, PackagePrefix.COM_YAHOO) + "." + shortClassName;
- String fullClassNameWithoutPrefix = InstanceResolver.packageName(key, PackagePrefix.NONE) + "." + shortClassName;
+ // TODO: Stop supporting fullClassNameWithComYahoo below, should not be used
+ String fullClassNameWithComYahoo = "com.yahoo" + key.getNamespace() + "." + shortClassName;
+ String fullClassNameWithoutPrefix = key.getNamespace() + "." + shortClassName;
String producerSuffix = "$Producer";
ClassLoader loader = getConfigClassLoader(fullClassNameWithoutPrefix + producerSuffix);
diff --git a/config-model/src/test/java/com/yahoo/vespa/model/InstanceResolverTest.java b/config-model/src/test/java/com/yahoo/vespa/model/InstanceResolverTest.java
deleted file mode 100644
index 8440951a06f..00000000000
--- a/config-model/src/test/java/com/yahoo/vespa/model/InstanceResolverTest.java
+++ /dev/null
@@ -1,236 +0,0 @@
-// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
-package com.yahoo.vespa.model;
-
-import com.yahoo.config.UrlReference;
-import com.yahoo.test.FunctionTestConfig;
-import com.yahoo.test.FunctionTestConfig.*;
-import com.yahoo.test.SimpletypesConfig;
-import com.yahoo.config.codegen.*;
-import com.yahoo.text.StringUtilities;
-import org.junit.jupiter.api.Test;
-
-import java.io.StringReader;
-import java.util.ArrayList;
-import java.util.Arrays;
-
-import static org.junit.jupiter.api.Assertions.*;
-
-public class InstanceResolverTest {
-
- @Test
- void testApplyDefToBuilder() throws Exception {
- FunctionTestConfig.Builder builder = createVariableAccessBuilder();
- InnerCNode targetDef = getDef(FunctionTestConfig.CONFIG_DEF_SCHEMA);
-
- // Mutate the def, user has set different schema ...
- ((LeafCNode) targetDef.getChild("stringwithdef")).setDefaultValue(new DefaultValue("newDef", new DefLine.Type("string")));
- ((LeafCNode) targetDef.getChild("string_val")).setDefaultValue(new DefaultValue("newDefVal", new DefLine.Type("string")));
- ((LeafCNode) targetDef.getChild("enumwithdef")).setDefaultValue(new DefaultValue(Enumwithdef.FOOBAR2.toString(), new DefLine.Type("enum")));
- ((LeafCNode) targetDef.getChild("enum_val")).setDefaultValue(new DefaultValue(Enum_val.FOO.toString(), new DefLine.Type("enum")));
- ((LeafCNode) targetDef.getChild("basicStruct").getChild("foo")).setDefaultValue(new DefaultValue("basicSchmasic", new DefLine.Type("string")));
- ((LeafCNode) targetDef.getChild("basicStruct").getChild("bar")).setDefaultValue(new DefaultValue("89", new DefLine.Type("int")));
- InnerCNode rootStruct = ((InnerCNode) targetDef.getChild("rootStruct"));
- InnerCNode innerArr = (InnerCNode) rootStruct.getChild("innerArr");
- ((LeafCNode) innerArr.getChild("boolVal")).setDefaultValue(new DefaultValue("true", new DefLine.Type("bool")));
- ((LeafCNode) innerArr.getChild("stringVal")).setDefaultValue(new DefaultValue("derp", new DefLine.Type("string")));
- InnerCNode myArray = ((InnerCNode) targetDef.getChild("myarray"));
- myArray.children().put("intval", LeafCNode.newInstance(new DefLine.Type("int"), myArray, "intval", "-123424"));
- targetDef.children().put("myarray", myArray);
- InstanceResolver.applyDef(builder, targetDef);
- FunctionTestConfig c = new FunctionTestConfig(builder);
- assertEquals(c.string_val(), "foo");
- assertEquals(c.stringwithdef(), "newDef");
- assertEquals(c.enumwithdef(), Enumwithdef.FOOBAR2);
- assertEquals(c.enum_val(), Enum_val.FOOBAR);
- assertEquals(c.double_with_def(), -12, 0.0001);
- assertEquals(c.basicStruct().foo(), "basicSchmasic");
- assertEquals(c.basicStruct().bar(), 3);
- assertTrue(c.rootStruct().innerArr(0).boolVal());
- assertEquals(c.rootStruct().innerArr(0).stringVal(), "deep");
- assertEquals(c.myarray(0).intval(), -123424);
- assertEquals(c.urlVal().toString(), "url");
- assertEquals(c.urlArr(0).toString(), "url");
- assertEquals(c.myarray(0).urlVal().toString(), "url1");
- assertEquals(c.myarray(1).urlVal().toString(), "url2");
- }
-
- /**
- * Values unset on builder, trying to set them from def file, but type mismatches there
- */
- @Test
- void testApplyDefToBuilderMismatches() throws Exception {
- FunctionTestConfig.Builder builder = createVariableAccessBuilderManyUnset();
- InnerCNode targetDef = getDef(FunctionTestConfig.CONFIG_DEF_SCHEMA);
-
- // Break the defs for these, they are unset on builder:
- targetDef.children().put("stringwithdef", LeafCNode.newInstance(new DefLine.Type("int"), targetDef, "stringwithdef", "1"));
- targetDef.children().put("int_val", LeafCNode.newInstance(new DefLine.Type("string"), targetDef, "int_val", "fooOO"));
-
- InstanceResolver.applyDef(builder, targetDef);
- try {
- FunctionTestConfig c = new FunctionTestConfig(builder);
- fail("No exception on incomplete builder");
- } catch (Exception e) {
- }
- }
-
- // copied from FunctionTest
- private FunctionTestConfig.Builder createVariableAccessBuilder() {
- return new FunctionTestConfig.Builder().
- bool_val(false).
- bool_with_def(true).
- int_val(5).
- int_with_def(-14).
- long_val(12345678901L).
- long_with_def(-9876543210L).
- double_val(41.23).
- double_with_def(-12).
- string_val("foo").
- //stringwithdef("bar").
- enum_val(Enum_val.FOOBAR).
- //enumwithdef(Enumwithdef.BAR2).
- refval(":parent:").
- refwithdef(":parent:").
- fileVal("etc").
- urlVal(new UrlReference("url")).
- boolarr(false).
- longarr(9223372036854775807L).
- longarr(-9223372036854775808L).
- doublearr(2344.0).
- doublearr(123.0).
- stringarr("bar").
- enumarr(Enumarr.VALUES).
- refarr(Arrays.asList(":parent:", ":parent", "parent:")). // test collection based setter
- fileArr("bin").
- urlArr(new UrlReference("url")).
-
- basicStruct(new BasicStruct.Builder().
- //foo("basicFoo").
- bar(3).
- intArr(310)).
-
- rootStruct(new RootStruct.Builder().
- inner0(new RootStruct.Inner0.Builder().
- index(11)).
- inner1(new RootStruct.Inner1.Builder().
- index(12)).
- innerArr(new RootStruct.InnerArr.Builder().
- //boolVal(true).
- stringVal("deep"))).
-
- myarray(new Myarray.Builder().
- //intval(-5).
- stringval("baah").
- stringval("yikes").
- enumval(Myarray.Enumval.INNER).
- refval(":parent:").
- fileVal("file0").
- urlVal(new UrlReference("url1")).
- anotherarray(new Myarray.Anotherarray.Builder().
- foo(7)).
- myStruct(new Myarray.MyStruct.Builder().
- a(1).
- b(2))).
-
- myarray(new Myarray.Builder().
- intval(5).
- enumval(Myarray.Enumval.INNER).
- refval(":parent:").
- fileVal("file1").
- urlVal(new UrlReference("url2")).
- anotherarray(new Myarray.Anotherarray.Builder().
- foo(1).
- foo(2)).
- myStruct(new Myarray.MyStruct.Builder().
- a(-1).
- b(-2)));
-
- }
-
- private FunctionTestConfig.Builder createVariableAccessBuilderManyUnset() {
- return new FunctionTestConfig.Builder().
- bool_val(false).
- bool_with_def(true).
- //int_val(5).
- int_with_def(-14).
- long_val(12345678901L).
- long_with_def(-9876543210L).
- double_val(41.23).
- double_with_def(-12).
- string_val("foo").
- //stringwithdef("bar").
- enum_val(Enum_val.FOOBAR).
- //enumwithdef(Enumwithdef.BAR2).
- refval(":parent:").
- refwithdef(":parent:").
- fileVal("etc").
- boolarr(false).
- longarr(9223372036854775807L).
- longarr(-9223372036854775808L).
- doublearr(2344.0).
- doublearr(123.0).
- stringarr("bar").
- enumarr(Enumarr.VALUES).
- refarr(Arrays.asList(":parent:", ":parent", "parent:")). // test collection based setter
- fileArr("bin").
-
- basicStruct(new BasicStruct.Builder().
- //foo("basicFoo").
- bar(3).
- intArr(310)).
-
- rootStruct(new RootStruct.Builder().
- inner0(new RootStruct.Inner0.Builder().
- index(11)).
- inner1(new RootStruct.Inner1.Builder().
- index(12)).
- innerArr(new RootStruct.InnerArr.Builder().
- //boolVal(true).
- stringVal("deep"))).
-
- myarray(new Myarray.Builder().
- intval(-5).
- stringval("baah").
- stringval("yikes").
- enumval(Myarray.Enumval.INNER).
- refval(":parent:").
- fileVal("file0").
- anotherarray(new Myarray.Anotherarray.Builder().
- foo(7)).
- myStruct(new Myarray.MyStruct.Builder().
- a(1).
- b(2))).
-
- myarray(new Myarray.Builder().
- intval(5).
- enumval(Myarray.Enumval.INNER).
- refval(":parent:").
- fileVal("file1").
- anotherarray(new Myarray.Anotherarray.Builder().
- foo(1).
- foo(2)).
- myStruct(new Myarray.MyStruct.Builder().
- a(-1).
- b(-2)));
-
- }
-
- private InnerCNode getDef(String[] schema) {
- ArrayList<String> def = new ArrayList<>();
- def.addAll(Arrays.asList(schema));
- return new DefParser("documentmanager",
- new StringReader(StringUtilities.implode(def.toArray(new String[def.size()]), "\n"))).getTree();
- }
-
- @Test
- void testExtraFieldsAreIgnored() throws Exception {
- try {
- SimpletypesConfig.Builder builder = new SimpletypesConfig.Builder();
- InnerCNode defWithExtra = new DefParser(SimpletypesConfig.CONFIG_DEF_NAME, new StringReader(StringUtilities.implode(SimpletypesConfig.CONFIG_DEF_SCHEMA, "\n") + "\nnewfield string default=\"foo\"\n")).getTree();
- InstanceResolver.applyDef(builder, defWithExtra);
- } catch (NoSuchFieldException e) {
- fail("Should not fail on extra field");
- }
- }
-
-}