Search in sources :

Example 1 with RootInnerNode

use of org.apache.ignite.internal.configuration.RootInnerNode 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 2 with RootInnerNode

use of org.apache.ignite.internal.configuration.RootInnerNode 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

RootInnerNode (org.apache.ignite.internal.configuration.RootInnerNode)2 SuperRoot (org.apache.ignite.internal.configuration.SuperRoot)2 ConfigObject (com.typesafe.config.ConfigObject)1 ArrayList (java.util.ArrayList)1 Collections.singletonMap (java.util.Collections.singletonMap)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 RootKey (org.apache.ignite.configuration.RootKey)1 DynamicConfigurationChanger (org.apache.ignite.internal.configuration.DynamicConfigurationChanger)1 ConfigurationAsmGenerator (org.apache.ignite.internal.configuration.asm.ConfigurationAsmGenerator)1 ConfigurationSource (org.apache.ignite.internal.configuration.tree.ConfigurationSource)1 ConverterToMapVisitor (org.apache.ignite.internal.configuration.tree.ConverterToMapVisitor)1 InnerNode (org.apache.ignite.internal.configuration.tree.InnerNode)1 ConfigurationFlattener.createFlattenedUpdatesMap (org.apache.ignite.internal.configuration.util.ConfigurationFlattener.createFlattenedUpdatesMap)1 Matchers.anEmptyMap (org.hamcrest.Matchers.anEmptyMap)1 Matchers.hasToString (org.hamcrest.Matchers.hasToString)1 Test (org.junit.jupiter.api.Test)1