Search in sources :

Example 6 with SuperRoot

use of org.apache.ignite.internal.configuration.SuperRoot in project ignite-3 by apache.

the class ConfigurationUtilTest method testSuperRootWithInternalConfig.

@Test
void testSuperRootWithInternalConfig() {
    ConfigurationAsmGenerator generator = new ConfigurationAsmGenerator();
    Class<?> schemaClass = InternalWithoutSuperclassConfigurationSchema.class;
    RootKey<?, ?> schemaKey = InternalWithoutSuperclassConfiguration.KEY;
    generator.compileRootSchema(schemaClass, Map.of(), Map.of());
    SuperRoot superRoot = new SuperRoot(s -> new RootInnerNode(schemaKey, generator.instantiateNode(schemaClass)));
    assertThrows(NoSuchElementException.class, () -> superRoot.construct(schemaKey.key(), null, false));
    superRoot.construct(schemaKey.key(), null, true);
    superRoot.addRoot(schemaKey, generator.instantiateNode(schemaClass));
    assertThrows(KeyNotFoundException.class, () -> find(List.of(schemaKey.key()), superRoot, false));
    assertNotNull(find(List.of(schemaKey.key()), superRoot, true));
    Map<String, Object> config = (Map<String, Object>) superRoot.accept(schemaKey.key(), new ConverterToMapVisitor(false));
    assertTrue(config.isEmpty());
    config = (Map<String, Object>) superRoot.accept(schemaKey.key(), new ConverterToMapVisitor(true));
    assertEquals(1, config.size());
    assertNotNull(config.get(schemaKey.key()));
}
Also used : RootInnerNode(org.apache.ignite.internal.configuration.RootInnerNode) ConfigurationAsmGenerator(org.apache.ignite.internal.configuration.asm.ConfigurationAsmGenerator) ConverterToMapVisitor(org.apache.ignite.internal.configuration.tree.ConverterToMapVisitor) SuperRoot(org.apache.ignite.internal.configuration.SuperRoot) Matchers.hasToString(org.hamcrest.Matchers.hasToString) Map(java.util.Map) ConfigurationFlattener.createFlattenedUpdatesMap(org.apache.ignite.internal.configuration.util.ConfigurationFlattener.createFlattenedUpdatesMap) HashMap(java.util.HashMap) Collections.singletonMap(java.util.Collections.singletonMap) Matchers.anEmptyMap(org.hamcrest.Matchers.anEmptyMap) Test(org.junit.jupiter.api.Test)

Example 7 with SuperRoot

use of org.apache.ignite.internal.configuration.SuperRoot in project ignite-3 by apache.

the class ValidationUtilTest method validateNamedListNode.

@Test
public void validateNamedListNode() throws Exception {
    var rootsNode = new SuperRoot(key -> null, Map.of(ValidatedRootConfiguration.KEY, root));
    Validator<NamedListValidation, NamedListView<?>> validator = new Validator<>() {

        @Override
        public void validate(NamedListValidation annotation, ValidationContext<NamedListView<?>> ctx) {
            assertEquals("root.elements", ctx.currentKey());
            assertEquals(List.of(), ctx.getOldValue().namedListKeys());
            assertEquals(List.of(), ctx.getNewValue().namedListKeys());
            ctx.addIssue(new ValidationIssue("bar"));
        }
    };
    Map<Class<? extends Annotation>, Set<Validator<?, ?>>> validators = Map.of(NamedListValidation.class, Set.of(validator));
    List<ValidationIssue> issues = ValidationUtil.validate(rootsNode, rootsNode, null, new HashMap<>(), validators);
    assertEquals(1, issues.size());
    assertEquals("bar", issues.get(0).message());
}
Also used : Set(java.util.Set) SuperRoot(org.apache.ignite.internal.configuration.SuperRoot) ValidationIssue(org.apache.ignite.configuration.validation.ValidationIssue) Annotation(java.lang.annotation.Annotation) ValidationContext(org.apache.ignite.configuration.validation.ValidationContext) NamedListView(org.apache.ignite.configuration.NamedListView) Validator(org.apache.ignite.configuration.validation.Validator) Test(org.junit.jupiter.api.Test)

Example 8 with SuperRoot

use of org.apache.ignite.internal.configuration.SuperRoot in project ignite-3 by apache.

the class ConfigurationExtension method cfgValue.

/**
 * Instantiates a configuration instance for injection.
 *
 * @param type Type of the field or parameter. Class name must end with {@code Configuration}.
 * @param annotation Annotation present on the field or parameter.
 * @param cgen Runtime code generator associated with the extension instance.
 * @param pool Single-threaded executor service to perform configuration changes.
 * @param revisionListenerHolder Configuration storage revision change listener holder.
 * @return Mock configuration instance.
 * @throws ClassNotFoundException If corresponding configuration schema class is not found.
 */
private static Object cfgValue(Class<?> type, InjectConfiguration annotation, ConfigurationAsmGenerator cgen, ExecutorService pool, StorageRevisionListenerHolderImpl revisionListenerHolder) throws ClassNotFoundException {
    // Trying to find a schema class using configuration naming convention. This code won't work for inner Java
    // classes, extension is designed to mock actual configurations from public API to configure Ignite components.
    Class<?> schemaClass = Class.forName(type.getCanonicalName() + "Schema");
    cgen.compileRootSchema(schemaClass, internalSchemaExtensions(List.of(annotation.internalExtensions())), polymorphicSchemaExtensions(List.of(annotation.polymorphicExtensions())));
    // RootKey must be mocked, there's no way to instantiate it using a public constructor.
    RootKey rootKey = mock(RootKey.class);
    when(rootKey.key()).thenReturn("mock");
    when(rootKey.type()).thenReturn(LOCAL);
    when(rootKey.schemaClass()).thenReturn(schemaClass);
    when(rootKey.internal()).thenReturn(false);
    SuperRoot superRoot = new SuperRoot(s -> new RootInnerNode(rootKey, cgen.instantiateNode(schemaClass)));
    ConfigObject hoconCfg = ConfigFactory.parseString(annotation.value()).root();
    HoconConverter.hoconSource(hoconCfg).descend(superRoot);
    ConfigurationUtil.addDefaults(superRoot);
    // Reference to the super root is required to make DynamicConfigurationChanger#change method atomic.
    var superRootRef = new AtomicReference<>(superRoot);
    // Reference that's required for notificator.
    var cfgRef = new AtomicReference<DynamicConfiguration<?, ?>>();
    cfgRef.set(cgen.instantiateCfg(rootKey, new DynamicConfigurationChanger() {

        /**
         * {@inheritDoc}
         */
        @Override
        public CompletableFuture<Void> change(ConfigurationSource change) {
            return CompletableFuture.supplyAsync(() -> {
                SuperRoot sr = superRootRef.get();
                SuperRoot copy = sr.copy();
                change.descend(copy);
                ConfigurationUtil.dropNulls(copy);
                if (superRootRef.compareAndSet(sr, copy)) {
                    long storageRevision = revisionListenerHolder.storageRev.incrementAndGet();
                    long notificationNumber = revisionListenerHolder.notificationListenerCnt.incrementAndGet();
                    List<CompletableFuture<?>> futures = new ArrayList<>();
                    futures.addAll(notifyListeners(sr.getRoot(rootKey), copy.getRoot(rootKey), (DynamicConfiguration<InnerNode, ?>) cfgRef.get(), storageRevision, notificationNumber));
                    futures.addAll(revisionListenerHolder.notifyStorageRevisionListeners(storageRevision, notificationNumber));
                    return CompletableFuture.allOf(futures.toArray(CompletableFuture[]::new));
                }
                return change(change);
            }, pool).thenCompose(Function.identity());
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public InnerNode getRootNode(RootKey<?, ?> rk) {
            return superRootRef.get().getRoot(rk);
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public <T> T getLatest(List<KeyPathNode> path) {
            return findEx(path, superRootRef.get());
        }

        /**
         * {@inheritDoc}
         */
        @Override
        public long notificationCount() {
            return revisionListenerHolder.notificationListenerCnt.get();
        }
    }));
    touch(cfgRef.get());
    return cfgRef.get();
}
Also used : ConfigurationSource(org.apache.ignite.internal.configuration.tree.ConfigurationSource) RootKey(org.apache.ignite.configuration.RootKey) SuperRoot(org.apache.ignite.internal.configuration.SuperRoot) AtomicReference(java.util.concurrent.atomic.AtomicReference) DynamicConfigurationChanger(org.apache.ignite.internal.configuration.DynamicConfigurationChanger) RootInnerNode(org.apache.ignite.internal.configuration.RootInnerNode) InnerNode(org.apache.ignite.internal.configuration.tree.InnerNode) RootInnerNode(org.apache.ignite.internal.configuration.RootInnerNode) ArrayList(java.util.ArrayList) List(java.util.List) ConfigObject(com.typesafe.config.ConfigObject)

Aggregations

SuperRoot (org.apache.ignite.internal.configuration.SuperRoot)8 Test (org.junit.jupiter.api.Test)6 Set (java.util.Set)5 RootInnerNode (org.apache.ignite.internal.configuration.RootInnerNode)4 Annotation (java.lang.annotation.Annotation)3 Collections.singletonMap (java.util.Collections.singletonMap)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Map (java.util.Map)3 RootKey (org.apache.ignite.configuration.RootKey)3 ValidationContext (org.apache.ignite.configuration.validation.ValidationContext)3 ValidationIssue (org.apache.ignite.configuration.validation.ValidationIssue)3 Validator (org.apache.ignite.configuration.validation.Validator)3 ConfigurationAsmGenerator (org.apache.ignite.internal.configuration.asm.ConfigurationAsmGenerator)3 ConverterToMapVisitor (org.apache.ignite.internal.configuration.tree.ConverterToMapVisitor)3 InnerNode (org.apache.ignite.internal.configuration.tree.InnerNode)3 NotNull (org.jetbrains.annotations.NotNull)3 Assertions.assertNotNull (org.junit.jupiter.api.Assertions.assertNotNull)3 Serializable (java.io.Serializable)2 Arrays (java.util.Arrays)2