Search in sources :

Example 1 with ConfigurationSource

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);
    }
}
Also used : ConfigurationValidationException(org.apache.ignite.configuration.validation.ConfigurationValidationException) ConfigurationSource(org.apache.ignite.internal.configuration.tree.ConfigurationSource) ConfigurationChangeException(org.apache.ignite.configuration.ConfigurationChangeException) ConstructableTreeNode(org.apache.ignite.internal.configuration.tree.ConstructableTreeNode) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException)

Example 2 with ConfigurationSource

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);
}
Also used : ConfigurationSource(org.apache.ignite.internal.configuration.tree.ConfigurationSource) RandomAccess(java.util.RandomAccess) ConstructableTreeNode(org.apache.ignite.internal.configuration.tree.ConstructableTreeNode) InnerNode(org.apache.ignite.internal.configuration.tree.InnerNode)

Example 3 with ConfigurationSource

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"));
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.fail(org.junit.jupiter.api.Assertions.fail) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Arrays(java.util.Arrays) Data(org.apache.ignite.internal.configuration.storage.Data) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) TimeoutException(java.util.concurrent.TimeoutException) ConstructableTreeNode(org.apache.ignite.internal.configuration.tree.ConstructableTreeNode) CompletableFuture(java.util.concurrent.CompletableFuture) ConfigurationStorage(org.apache.ignite.internal.configuration.storage.ConfigurationStorage) Validator(org.apache.ignite.configuration.validation.Validator) Retention(java.lang.annotation.Retention) NamedConfigValue(org.apache.ignite.configuration.annotation.NamedConfigValue) Immutable(org.apache.ignite.configuration.validation.Immutable) AfterAll(org.junit.jupiter.api.AfterAll) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) KeyPathNode(org.apache.ignite.internal.configuration.direct.KeyPathNode) Map(java.util.Map) KEY(org.apache.ignite.internal.configuration.FirstConfiguration.KEY) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) NoSuchElementException(java.util.NoSuchElementException) FIELD(java.lang.annotation.ElementType.FIELD) ConfigurationRoot(org.apache.ignite.configuration.annotation.ConfigurationRoot) ValidationContext(org.apache.ignite.configuration.validation.ValidationContext) NamedListView(org.apache.ignite.configuration.NamedListView) Set(java.util.Set) Target(java.lang.annotation.Target) ConfigurationChangeException(org.apache.ignite.configuration.ConfigurationChangeException) RootKey(org.apache.ignite.configuration.RootKey) RUNTIME(java.lang.annotation.RetentionPolicy.RUNTIME) Serializable(java.io.Serializable) Assertions.assertArrayEquals(org.junit.jupiter.api.Assertions.assertArrayEquals) ExecutionException(java.util.concurrent.ExecutionException) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) ConfigValue(org.apache.ignite.configuration.annotation.ConfigValue) List(java.util.List) ValidationIssue(org.apache.ignite.configuration.validation.ValidationIssue) LOCAL(org.apache.ignite.configuration.annotation.ConfigurationType.LOCAL) Config(org.apache.ignite.configuration.annotation.Config) TestConfigurationStorage(org.apache.ignite.internal.configuration.storage.TestConfigurationStorage) ConfigurationSource(org.apache.ignite.internal.configuration.tree.ConfigurationSource) Matchers.containsString(org.hamcrest.Matchers.containsString) SECONDS(java.util.concurrent.TimeUnit.SECONDS) Value(org.apache.ignite.configuration.annotation.Value) ConfigurationAsmGenerator(org.apache.ignite.internal.configuration.asm.ConfigurationAsmGenerator) ConfigurationSource(org.apache.ignite.internal.configuration.tree.ConfigurationSource) NoSuchElementException(java.util.NoSuchElementException) Test(org.junit.jupiter.api.Test)

Example 4 with ConfigurationSource

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);
    }
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.fail(org.junit.jupiter.api.Assertions.fail) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Arrays(java.util.Arrays) Data(org.apache.ignite.internal.configuration.storage.Data) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) TimeoutException(java.util.concurrent.TimeoutException) ConstructableTreeNode(org.apache.ignite.internal.configuration.tree.ConstructableTreeNode) CompletableFuture(java.util.concurrent.CompletableFuture) ConfigurationStorage(org.apache.ignite.internal.configuration.storage.ConfigurationStorage) Validator(org.apache.ignite.configuration.validation.Validator) Retention(java.lang.annotation.Retention) NamedConfigValue(org.apache.ignite.configuration.annotation.NamedConfigValue) Immutable(org.apache.ignite.configuration.validation.Immutable) AfterAll(org.junit.jupiter.api.AfterAll) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) KeyPathNode(org.apache.ignite.internal.configuration.direct.KeyPathNode) Map(java.util.Map) KEY(org.apache.ignite.internal.configuration.FirstConfiguration.KEY) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) NoSuchElementException(java.util.NoSuchElementException) FIELD(java.lang.annotation.ElementType.FIELD) ConfigurationRoot(org.apache.ignite.configuration.annotation.ConfigurationRoot) ValidationContext(org.apache.ignite.configuration.validation.ValidationContext) NamedListView(org.apache.ignite.configuration.NamedListView) Set(java.util.Set) Target(java.lang.annotation.Target) ConfigurationChangeException(org.apache.ignite.configuration.ConfigurationChangeException) RootKey(org.apache.ignite.configuration.RootKey) RUNTIME(java.lang.annotation.RetentionPolicy.RUNTIME) Serializable(java.io.Serializable) Assertions.assertArrayEquals(org.junit.jupiter.api.Assertions.assertArrayEquals) ExecutionException(java.util.concurrent.ExecutionException) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) ConfigValue(org.apache.ignite.configuration.annotation.ConfigValue) List(java.util.List) ValidationIssue(org.apache.ignite.configuration.validation.ValidationIssue) LOCAL(org.apache.ignite.configuration.annotation.ConfigurationType.LOCAL) Config(org.apache.ignite.configuration.annotation.Config) TestConfigurationStorage(org.apache.ignite.internal.configuration.storage.TestConfigurationStorage) ConfigurationSource(org.apache.ignite.internal.configuration.tree.ConfigurationSource) Matchers.containsString(org.hamcrest.Matchers.containsString) SECONDS(java.util.concurrent.TimeUnit.SECONDS) Value(org.apache.ignite.configuration.annotation.Value) ConfigurationAsmGenerator(org.apache.ignite.internal.configuration.asm.ConfigurationAsmGenerator) ConfigurationSource(org.apache.ignite.internal.configuration.tree.ConfigurationSource) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.jupiter.api.Test)

Example 5 with ConfigurationSource

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());
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) Assertions.fail(org.junit.jupiter.api.Assertions.fail) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Arrays(java.util.Arrays) Data(org.apache.ignite.internal.configuration.storage.Data) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) TimeoutException(java.util.concurrent.TimeoutException) ConstructableTreeNode(org.apache.ignite.internal.configuration.tree.ConstructableTreeNode) CompletableFuture(java.util.concurrent.CompletableFuture) ConfigurationStorage(org.apache.ignite.internal.configuration.storage.ConfigurationStorage) Validator(org.apache.ignite.configuration.validation.Validator) Retention(java.lang.annotation.Retention) NamedConfigValue(org.apache.ignite.configuration.annotation.NamedConfigValue) Immutable(org.apache.ignite.configuration.validation.Immutable) AfterAll(org.junit.jupiter.api.AfterAll) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) KeyPathNode(org.apache.ignite.internal.configuration.direct.KeyPathNode) Map(java.util.Map) KEY(org.apache.ignite.internal.configuration.FirstConfiguration.KEY) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) NoSuchElementException(java.util.NoSuchElementException) FIELD(java.lang.annotation.ElementType.FIELD) ConfigurationRoot(org.apache.ignite.configuration.annotation.ConfigurationRoot) ValidationContext(org.apache.ignite.configuration.validation.ValidationContext) NamedListView(org.apache.ignite.configuration.NamedListView) Set(java.util.Set) Target(java.lang.annotation.Target) ConfigurationChangeException(org.apache.ignite.configuration.ConfigurationChangeException) RootKey(org.apache.ignite.configuration.RootKey) RUNTIME(java.lang.annotation.RetentionPolicy.RUNTIME) Serializable(java.io.Serializable) Assertions.assertArrayEquals(org.junit.jupiter.api.Assertions.assertArrayEquals) ExecutionException(java.util.concurrent.ExecutionException) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) ConfigValue(org.apache.ignite.configuration.annotation.ConfigValue) List(java.util.List) ValidationIssue(org.apache.ignite.configuration.validation.ValidationIssue) LOCAL(org.apache.ignite.configuration.annotation.ConfigurationType.LOCAL) Config(org.apache.ignite.configuration.annotation.Config) TestConfigurationStorage(org.apache.ignite.internal.configuration.storage.TestConfigurationStorage) ConfigurationSource(org.apache.ignite.internal.configuration.tree.ConfigurationSource) Matchers.containsString(org.hamcrest.Matchers.containsString) SECONDS(java.util.concurrent.TimeUnit.SECONDS) Value(org.apache.ignite.configuration.annotation.Value) ConfigurationAsmGenerator(org.apache.ignite.internal.configuration.asm.ConfigurationAsmGenerator) ConfigurationSource(org.apache.ignite.internal.configuration.tree.ConfigurationSource) Test(org.junit.jupiter.api.Test)

Aggregations

ConfigurationSource (org.apache.ignite.internal.configuration.tree.ConfigurationSource)9 ConstructableTreeNode (org.apache.ignite.internal.configuration.tree.ConstructableTreeNode)7 List (java.util.List)4 NoSuchElementException (java.util.NoSuchElementException)4 ExecutionException (java.util.concurrent.ExecutionException)4 TimeoutException (java.util.concurrent.TimeoutException)4 ConfigurationChangeException (org.apache.ignite.configuration.ConfigurationChangeException)4 RootKey (org.apache.ignite.configuration.RootKey)4 Serializable (java.io.Serializable)3 FIELD (java.lang.annotation.ElementType.FIELD)3 Retention (java.lang.annotation.Retention)3 RUNTIME (java.lang.annotation.RetentionPolicy.RUNTIME)3 Target (java.lang.annotation.Target)3 Arrays (java.util.Arrays)3 Map (java.util.Map)3 Set (java.util.Set)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 SECONDS (java.util.concurrent.TimeUnit.SECONDS)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Consumer (java.util.function.Consumer)3