use of org.apache.ignite.internal.configuration.tree.ConfigurationSource in project ignite-3 by apache.
the class ConfigurationChanger method initializeDefaults.
/**
* Initializes the configuration storage - reads data and sets default values for missing configuration properties.
*
* @throws ConfigurationValidationException If configuration validation failed.
* @throws ConfigurationChangeException If configuration framework failed to add default values and save them to storage.
*/
public void initializeDefaults() throws ConfigurationValidationException, ConfigurationChangeException {
try {
ConfigurationSource defaultsCfgSource = new ConfigurationSource() {
/**
* {@inheritDoc}
*/
@Override
public void descend(ConstructableTreeNode node) {
addDefaults((InnerNode) node);
}
};
changeInternally(defaultsCfgSource).get(5, TimeUnit.SECONDS);
} catch (ExecutionException e) {
Throwable cause = e.getCause();
if (cause instanceof ConfigurationValidationException) {
throw (ConfigurationValidationException) cause;
}
if (cause instanceof ConfigurationChangeException) {
throw (ConfigurationChangeException) cause;
}
throw new ConfigurationChangeException("Failed to write default configuration values into the storage " + storage.getClass(), e);
} catch (InterruptedException | TimeoutException e) {
throw new ConfigurationChangeException("Failed to initialize configuration storage " + storage.getClass(), e);
}
}
use of org.apache.ignite.internal.configuration.tree.ConfigurationSource in project ignite-3 by apache.
the class DynamicConfiguration method change.
/**
* {@inheritDoc}
*/
@Override
public final CompletableFuture<Void> change(Consumer<CHANGET> change) {
Objects.requireNonNull(change, "Configuration consumer cannot be null.");
if (listenOnly) {
throw listenOnlyException();
}
assert keys instanceof RandomAccess;
ConfigurationSource src = new ConfigurationSource() {
/**
* Current index in the {@code keys}.
*/
private int level = 0;
/**
* {@inheritDoc}
*/
@Override
public void descend(ConstructableTreeNode node) {
if (level == keys.size()) {
if (node instanceof InnerNode) {
// To support polymorphic configuration.
change.accept(((InnerNode) node).specificNode());
} else {
// To support namedList configuration.
change.accept((CHANGET) node);
}
} else {
node.construct(keys.get(level++), this, true);
}
}
/**
* {@inheritDoc}
*/
@Override
public void reset() {
level = 0;
}
};
// Use resulting tree as update request for the storage.
return changer.change(src);
}
use of org.apache.ignite.internal.configuration.tree.ConfigurationSource in project ignite-3 by apache.
the class ConfigurationChangerTest method testGetLatestMissingKey.
/**
* Tests the {@link DynamicConfigurationChanger#getLatest} method by trying to find non-existend values on different configuration
* levels.
*/
@Test
public void testGetLatestMissingKey() throws Exception {
ConfigurationChanger changer = createChanger(DefaultsConfiguration.KEY);
changer.start();
changer.initializeDefaults();
ConfigurationSource source = source(DefaultsConfiguration.KEY, (DefaultsChange change) -> change.changeChildrenList(children -> children.create("name", child -> {
})));
changer.change(source).get(1, SECONDS);
NoSuchElementException e = assertThrows(NoSuchElementException.class, () -> changer.getLatest(List.of(node("foo"))));
assertThat(e.getMessage(), containsString("foo"));
e = assertThrows(NoSuchElementException.class, () -> changer.getLatest(List.of(node("def"), node("foo"))));
assertThat(e.getMessage(), containsString("foo"));
e = assertThrows(NoSuchElementException.class, () -> changer.getLatest(List.of(node("def"), node("defStr"), node("foo"))));
assertThat(e.getMessage(), containsString("def.defStr.foo"));
e = assertThrows(NoSuchElementException.class, () -> changer.getLatest(List.of(node("def"), node("child"), node("foo"))));
assertThat(e.getMessage(), containsString("foo"));
e = assertThrows(NoSuchElementException.class, () -> changer.getLatest(List.of(node("def"), node("child"), node("defStr"), node("foo"))));
assertThat(e.getMessage(), containsString("def.child.defStr.foo"));
e = assertThrows(NoSuchElementException.class, () -> changer.getLatest(List.of(node("def"), node("childrenList"), listNode("foo"))));
assertThat(e.getMessage(), containsString("def.childrenList.foo"));
e = assertThrows(NoSuchElementException.class, () -> changer.getLatest(List.of(node("def"), node("childrenList"), listNode("name"), node("foo"))));
assertThat(e.getMessage(), containsString("def.childrenList.name.foo"));
e = assertThrows(NoSuchElementException.class, () -> changer.getLatest(List.of(node("def"), node("childrenList"), listNode("name"), node("defStr"), node("foo"))));
assertThat(e.getMessage(), containsString("def.childrenList.name.defStr"));
}
use of org.apache.ignite.internal.configuration.tree.ConfigurationSource in project ignite-3 by apache.
the class ConfigurationChangerTest method testGetLatestNamedList.
/**
* Tests the {@link DynamicConfigurationChanger#getLatest} method by retrieving different Named List configuration values.
*/
@Test
public void testGetLatestNamedList() throws Exception {
ConfigurationChanger changer = createChanger(DefaultsConfiguration.KEY);
changer.start();
changer.initializeDefaults();
ConfigurationSource source = source(DefaultsConfiguration.KEY, (DefaultsChange change) -> change.changeChildrenList(children -> {
children.create("name1", child -> {
});
children.create("name2", child -> {
});
}));
changer.change(source).get(1, SECONDS);
for (String name : List.of("name1", "name2")) {
NamedListView<DefaultsChildView> childrenListView = changer.getLatest(List.of(node("def"), node("childrenList")));
DefaultsChildView childrenListElementView = childrenListView.get(name);
assertEquals("bar", childrenListElementView.defStr());
assertArrayEquals(new String[] { "xyz" }, childrenListElementView.arr());
childrenListElementView = changer.getLatest(List.of(node("def"), node("childrenList"), listNode(name)));
assertEquals("bar", childrenListElementView.defStr());
assertArrayEquals(new String[] { "xyz" }, childrenListElementView.arr());
String childrenListStrValueView = changer.getLatest(List.of(node("def"), node("childrenList"), listNode(name), node("defStr")));
assertEquals("bar", childrenListStrValueView);
String[] childrenListArrView = changer.getLatest(List.of(node("def"), node("childrenList"), listNode(name), node("arr")));
assertArrayEquals(new String[] { "xyz" }, childrenListArrView);
}
}
use of org.apache.ignite.internal.configuration.tree.ConfigurationSource in project ignite-3 by apache.
the class ConfigurationChangerTest method testGetLatest.
/**
* Tests the {@link DynamicConfigurationChanger#getLatest} method by retrieving different configuration values.
*/
@Test
public void testGetLatest() throws Exception {
ConfigurationChanger changer = createChanger(DefaultsConfiguration.KEY);
changer.start();
changer.initializeDefaults();
ConfigurationSource source = source(DefaultsConfiguration.KEY, (DefaultsChange change) -> change.changeChildrenList(children -> children.create("name", child -> {
})));
changer.change(source).get(1, SECONDS);
DefaultsView configurationView = changer.getLatest(List.of(node("def")));
assertEquals("foo", configurationView.defStr());
assertEquals("bar", configurationView.child().defStr());
assertArrayEquals(new String[] { "xyz" }, configurationView.child().arr());
assertEquals("bar", configurationView.childrenList().get("name").defStr());
}
Aggregations