Search in sources :

Example 1 with DynamicConfigurationChanger

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

the class DirectProxyAsmGenerator method addConstructor.

/**
 * Generates constructor.
 */
private void addConstructor() {
    // public FooDirectProxy(List<KeyPathNode> keys, DynamicConfigurationChanger changer) {
    MethodDefinition ctor = classDef.declareConstructor(of(PUBLIC), arg("keys", List.class), arg("changer", DynamicConfigurationChanger.class));
    // super(keys, changer);
    // }
    ctor.getBody().append(ctor.getThis()).append(ctor.getScope().getVariable("keys")).append(ctor.getScope().getVariable("changer")).invokeConstructor(DIRECT_CFG_CTOR).ret();
}
Also used : MethodDefinition(com.facebook.presto.bytecode.MethodDefinition) Arrays.asList(java.util.Arrays.asList) List(java.util.List) DynamicConfigurationChanger(org.apache.ignite.internal.configuration.DynamicConfigurationChanger)

Example 2 with DynamicConfigurationChanger

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

List (java.util.List)2 DynamicConfigurationChanger (org.apache.ignite.internal.configuration.DynamicConfigurationChanger)2 MethodDefinition (com.facebook.presto.bytecode.MethodDefinition)1 ConfigObject (com.typesafe.config.ConfigObject)1 ArrayList (java.util.ArrayList)1 Arrays.asList (java.util.Arrays.asList)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 RootKey (org.apache.ignite.configuration.RootKey)1 RootInnerNode (org.apache.ignite.internal.configuration.RootInnerNode)1 SuperRoot (org.apache.ignite.internal.configuration.SuperRoot)1 ConfigurationSource (org.apache.ignite.internal.configuration.tree.ConfigurationSource)1 InnerNode (org.apache.ignite.internal.configuration.tree.InnerNode)1