chesnokoff commented on code in PR #355: URL: https://github.com/apache/ignite-extensions/pull/355#discussion_r3758336046
########## modules/auto-activation-ext/src/main/java/opt/apache/ignite/activation/AutoActivationPluginProvider.java: ########## @@ -0,0 +1,159 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package opt.apache.ignite.activation; + +import java.io.Serializable; +import java.util.Collection; +import java.util.UUID; +import org.apache.ignite.Ignite; +import org.apache.ignite.IgniteCluster; +import org.apache.ignite.IgniteLogger; +import org.apache.ignite.cluster.ClusterNode; +import org.apache.ignite.cluster.ClusterState; +import org.apache.ignite.lang.IgnitePredicate; +import org.apache.ignite.plugin.CachePluginContext; +import org.apache.ignite.plugin.CachePluginProvider; +import org.apache.ignite.plugin.ExtensionRegistry; +import org.apache.ignite.plugin.IgnitePlugin; +import org.apache.ignite.plugin.PluginConfiguration; +import org.apache.ignite.plugin.PluginContext; +import org.apache.ignite.plugin.PluginProvider; +import org.apache.ignite.plugin.PluginValidationException; + +/** + * Activate cluster when specified condition meet + */ +public class AutoActivationPluginProvider implements PluginProvider<PluginConfiguration> { + /** */ + private final IgnitePredicate<Collection<ClusterNode>> condition; + + /** */ + private IgniteLogger logger; + + /** */ + private Ignite grid; + + /** + * @param condition Auto activation condition. + */ + public AutoActivationPluginProvider(IgnitePredicate<Collection<ClusterNode>> condition) { + if (condition == null) + throw new IllegalArgumentException("Auto activation condition must be set"); + + this.condition = condition; + } + + /** {@inheritDoc} */ + @Override public String name() { + return "Auto Activation Plugin"; + } + + /** {@inheritDoc} */ + @Override public <T extends IgnitePlugin> T plugin() { + return (T)new IgnitePlugin() { + // No-op. + }; + } + + /** {@inheritDoc} */ + @Override public String version() { + return "1.0.0-SNAPSHOT"; + } + + /** {@inheritDoc} */ + @Override public String copyright() { + return ""; + } + + /** {@inheritDoc} */ + @Override public void initExtensions(PluginContext pc, ExtensionRegistry er) { + logger = pc.log(this.getClass()); + grid = pc.grid(); + } + + /** {@inheritDoc} */ + @Override public <T> T createComponent(PluginContext pc, Class<T> type) { + return null; + } + + /** {@inheritDoc} */ + @Override public CachePluginProvider createCacheProvider(CachePluginContext cpc) { + return null; + } + + /** {@inheritDoc} */ + @Override public void start(PluginContext pc) { + // No-op. + } + + /** {@inheritDoc} */ + @Override public void stop(boolean bln) { + // No-op. + } + + /** {@inheritDoc} */ + @Override public void onIgniteStart() { + + IgniteCluster cluster = grid.cluster(); + + if (cluster.state() == ClusterState.ACTIVE || cluster.state() == ClusterState.ACTIVE_READ_ONLY) { + if (logger.isInfoEnabled()) + logger.info("Auto activation skipped - cluster already activated"); + + return; + } + + if (cluster.currentBaselineTopology() != null) { + if (logger.isInfoEnabled()) + logger.info("Auto activation skipped - baseline is not empty"); + + return; + } + + if (condition.apply(cluster.nodes())) { + if (logger.isInfoEnabled()) + logger.info("Auto activation plugin set cluster state ACTIVE - activation condition meet"); + + cluster.state(ClusterState.ACTIVE); + } + else { + if (logger.isInfoEnabled()) + logger.info("Auto activation skipped - activation condition not meet"); + } + } Review Comment: ```suggestion if (condition.apply(cluster.nodes())) { if (logger.isInfoEnabled()) logger.info("Auto activation plugin set cluster state ACTIVE - activation condition meet"); cluster.state(ClusterState.ACTIVE); return; } if (logger.isInfoEnabled()) logger.info("Auto activation skipped - activation condition not meet"); } ``` ########## modules/auto-activation-ext/src/main/java/opt/apache/ignite/activation/ActivateByConsistentID.java: ########## @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package opt.apache.ignite.activation; + +import java.util.Collection; +import java.util.LinkedHashSet; +import java.util.Set; +import org.apache.ignite.IgniteException; +import org.apache.ignite.cluster.ClusterNode; +import org.apache.ignite.lang.IgnitePredicate; + +/** + * Activate cluster when nodes with specified ConsistentID values join topology. + */ +public class ActivateByConsistentID implements IgnitePredicate<Collection<ClusterNode>> { + /** Collection of required nodes ConsistentIDs. */ + private final Set<String> requiredNodes; + + /** + * @param requiredNodes List of ConsistentIDs. + */ + public ActivateByConsistentID(Set<String> requiredNodes) { + if (requiredNodes == null || requiredNodes.isEmpty()) + throw new IllegalArgumentException("requiredNodes must be set"); + + this.requiredNodes = requiredNodes; + } + + /** {@inheritDoc} */ + @Override public boolean apply(Collection<ClusterNode> nodes) { + Set<String> missingNodes = new LinkedHashSet<>(requiredNodes); Review Comment: ```suggestion Set<String> missingNodes = new HashSet<>(requiredNodes); ``` ########## modules/auto-activation-ext/src/main/java/opt/apache/ignite/activation/AutoActivationPluginProvider.java: ########## @@ -0,0 +1,159 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package opt.apache.ignite.activation; + +import java.io.Serializable; +import java.util.Collection; +import java.util.UUID; +import org.apache.ignite.Ignite; +import org.apache.ignite.IgniteCluster; +import org.apache.ignite.IgniteLogger; +import org.apache.ignite.cluster.ClusterNode; +import org.apache.ignite.cluster.ClusterState; +import org.apache.ignite.lang.IgnitePredicate; +import org.apache.ignite.plugin.CachePluginContext; +import org.apache.ignite.plugin.CachePluginProvider; +import org.apache.ignite.plugin.ExtensionRegistry; +import org.apache.ignite.plugin.IgnitePlugin; +import org.apache.ignite.plugin.PluginConfiguration; +import org.apache.ignite.plugin.PluginContext; +import org.apache.ignite.plugin.PluginProvider; +import org.apache.ignite.plugin.PluginValidationException; + +/** + * Activate cluster when specified condition meet + */ +public class AutoActivationPluginProvider implements PluginProvider<PluginConfiguration> { + /** */ + private final IgnitePredicate<Collection<ClusterNode>> condition; + + /** */ + private IgniteLogger logger; + + /** */ + private Ignite grid; + + /** + * @param condition Auto activation condition. + */ + public AutoActivationPluginProvider(IgnitePredicate<Collection<ClusterNode>> condition) { + if (condition == null) + throw new IllegalArgumentException("Auto activation condition must be set"); + + this.condition = condition; + } + + /** {@inheritDoc} */ + @Override public String name() { + return "Auto Activation Plugin"; + } + + /** {@inheritDoc} */ + @Override public <T extends IgnitePlugin> T plugin() { + return (T)new IgnitePlugin() { + // No-op. + }; + } + + /** {@inheritDoc} */ + @Override public String version() { + return "1.0.0-SNAPSHOT"; + } + + /** {@inheritDoc} */ + @Override public String copyright() { + return ""; Review Comment: ```suggestion return "Apache Software Foundation"; ``` ########## modules/auto-activation-ext/src/main/java/opt/apache/ignite/activation/ActivateByNodeAttribute.java: ########## @@ -0,0 +1,72 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package opt.apache.ignite.activation; + +import java.util.Collection; +import java.util.LinkedHashSet; +import java.util.Set; +import org.apache.ignite.IgniteException; +import org.apache.ignite.cluster.ClusterNode; +import org.apache.ignite.lang.IgnitePredicate; + +/** + * Activate cluster when nodes with all specified attributes values join topology. + */ +public class ActivateByNodeAttribute implements IgnitePredicate<Collection<ClusterNode>> { + /** Node's attribute name. */ + private final String attrName; + + /** Collection of values for node's attribute. */ + private final Set<String> requiredValues; + + /** + * @param attributeName Node's attribute name. + * @param requiredValues List of values for node's attribute. + */ + public ActivateByNodeAttribute(String attributeName, Set<String> requiredValues) { + if (attributeName == null || attributeName.isBlank()) + throw new IllegalArgumentException("attributeName must be set"); + + if (requiredValues == null || requiredValues.isEmpty()) + throw new IllegalArgumentException("requiredValues must be set"); + + this.attrName = attributeName; + this.requiredValues = requiredValues; + } + + /** {@inheritDoc} */ + @Override public boolean apply(Collection<ClusterNode> nodes) { + Set<String> missingNodes = new LinkedHashSet<>(requiredValues); Review Comment: ```suggestion Set<String> missingNodes = new HashSet<>(requiredValues); ``` ########## modules/auto-activation-ext/src/main/java/opt/apache/ignite/activation/AutoActivationPluginProvider.java: ########## @@ -0,0 +1,159 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package opt.apache.ignite.activation; + +import java.io.Serializable; +import java.util.Collection; +import java.util.UUID; +import org.apache.ignite.Ignite; +import org.apache.ignite.IgniteCluster; +import org.apache.ignite.IgniteLogger; +import org.apache.ignite.cluster.ClusterNode; +import org.apache.ignite.cluster.ClusterState; +import org.apache.ignite.lang.IgnitePredicate; +import org.apache.ignite.plugin.CachePluginContext; +import org.apache.ignite.plugin.CachePluginProvider; +import org.apache.ignite.plugin.ExtensionRegistry; +import org.apache.ignite.plugin.IgnitePlugin; +import org.apache.ignite.plugin.PluginConfiguration; +import org.apache.ignite.plugin.PluginContext; +import org.apache.ignite.plugin.PluginProvider; +import org.apache.ignite.plugin.PluginValidationException; + +/** + * Activate cluster when specified condition meet + */ +public class AutoActivationPluginProvider implements PluginProvider<PluginConfiguration> { + /** */ + private final IgnitePredicate<Collection<ClusterNode>> condition; + + /** */ + private IgniteLogger logger; + + /** */ + private Ignite grid; + + /** + * @param condition Auto activation condition. + */ + public AutoActivationPluginProvider(IgnitePredicate<Collection<ClusterNode>> condition) { + if (condition == null) + throw new IllegalArgumentException("Auto activation condition must be set"); + + this.condition = condition; + } + + /** {@inheritDoc} */ + @Override public String name() { + return "Auto Activation Plugin"; + } + + /** {@inheritDoc} */ + @Override public <T extends IgnitePlugin> T plugin() { + return (T)new IgnitePlugin() { + // No-op. + }; + } + + /** {@inheritDoc} */ + @Override public String version() { + return "1.0.0-SNAPSHOT"; + } + + /** {@inheritDoc} */ + @Override public String copyright() { + return ""; + } + + /** {@inheritDoc} */ + @Override public void initExtensions(PluginContext pc, ExtensionRegistry er) { + logger = pc.log(this.getClass()); + grid = pc.grid(); + } + + /** {@inheritDoc} */ + @Override public <T> T createComponent(PluginContext pc, Class<T> type) { + return null; + } + + /** {@inheritDoc} */ + @Override public CachePluginProvider createCacheProvider(CachePluginContext cpc) { + return null; + } + + /** {@inheritDoc} */ + @Override public void start(PluginContext pc) { + // No-op. + } + + /** {@inheritDoc} */ + @Override public void stop(boolean bln) { + // No-op. + } + + /** {@inheritDoc} */ + @Override public void onIgniteStart() { + + IgniteCluster cluster = grid.cluster(); + + if (cluster.state() == ClusterState.ACTIVE || cluster.state() == ClusterState.ACTIVE_READ_ONLY) { + if (logger.isInfoEnabled()) + logger.info("Auto activation skipped - cluster already activated"); + + return; + } + + if (cluster.currentBaselineTopology() != null) { + if (logger.isInfoEnabled()) + logger.info("Auto activation skipped - baseline is not empty"); + + return; + } + + if (condition.apply(cluster.nodes())) { Review Comment: Can we use `cluster.forServers().nodes()` here? Then we do not need client checks in both predicate classes. Or replace `IgniteException("Auto-activation-plugin supports on` with `return false` ########## modules/auto-activation-ext/pom.xml: ########## @@ -0,0 +1,86 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<!-- + ~ Licensed to the Apache Software Foundation (ASF) under one or more + ~ contributor license agreements. See the NOTICE file distributed with + ~ this work for additional information regarding copyright ownership. + ~ The ASF licenses this file to You under the Apache License, Version 2.0 + ~ (the "License"); you may not use this file except in compliance with + ~ the License. You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + --> + +<!-- + POM file. +--> Review Comment: ```suggestion ``` ########## modules/auto-activation-ext/src/test/java/opt/apache/ignite/activation/AutoActivationTest.java: ########## @@ -0,0 +1,735 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package opt.apache.ignite.activation; + +import java.util.Collections; +import java.util.Map; +import java.util.Set; +import org.apache.ignite.IgniteException; +import org.apache.ignite.cache.CacheAtomicityMode; +import org.apache.ignite.cache.CacheMode; +import org.apache.ignite.cluster.ClusterState; +import org.apache.ignite.configuration.CacheConfiguration; +import org.apache.ignite.configuration.DataRegionConfiguration; +import org.apache.ignite.configuration.DataStorageConfiguration; +import org.apache.ignite.configuration.IgniteConfiguration; +import org.apache.ignite.configuration.WALMode; +import org.apache.ignite.internal.IgniteEx; +import org.apache.ignite.plugin.PluginProvider; +import org.apache.ignite.testframework.ListeningTestLogger; +import org.apache.ignite.testframework.LogListener; +import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; +import org.junit.Test; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import static org.apache.ignite.cluster.ClusterState.ACTIVE; +import static org.apache.ignite.cluster.ClusterState.ACTIVE_READ_ONLY; +import static org.apache.ignite.cluster.ClusterState.INACTIVE; +import static org.apache.ignite.testframework.GridTestUtils.assertThrows; + +/** + * {@link AutoActivationPluginProvider} test Review Comment: ```suggestion * Tests {@link AutoActivationPluginProvider}. ``` ########## modules/auto-activation-ext/src/main/java/opt/apache/ignite/activation/AutoActivationPluginProvider.java: ########## @@ -0,0 +1,159 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package opt.apache.ignite.activation; + +import java.io.Serializable; +import java.util.Collection; +import java.util.UUID; +import org.apache.ignite.Ignite; +import org.apache.ignite.IgniteCluster; +import org.apache.ignite.IgniteLogger; +import org.apache.ignite.cluster.ClusterNode; +import org.apache.ignite.cluster.ClusterState; +import org.apache.ignite.lang.IgnitePredicate; +import org.apache.ignite.plugin.CachePluginContext; +import org.apache.ignite.plugin.CachePluginProvider; +import org.apache.ignite.plugin.ExtensionRegistry; +import org.apache.ignite.plugin.IgnitePlugin; +import org.apache.ignite.plugin.PluginConfiguration; +import org.apache.ignite.plugin.PluginContext; +import org.apache.ignite.plugin.PluginProvider; +import org.apache.ignite.plugin.PluginValidationException; + +/** + * Activate cluster when specified condition meet + */ +public class AutoActivationPluginProvider implements PluginProvider<PluginConfiguration> { + /** */ + private final IgnitePredicate<Collection<ClusterNode>> condition; + + /** */ + private IgniteLogger logger; + + /** */ + private Ignite grid; + + /** + * @param condition Auto activation condition. + */ + public AutoActivationPluginProvider(IgnitePredicate<Collection<ClusterNode>> condition) { + if (condition == null) + throw new IllegalArgumentException("Auto activation condition must be set"); + + this.condition = condition; + } + + /** {@inheritDoc} */ + @Override public String name() { + return "Auto Activation Plugin"; + } + + /** {@inheritDoc} */ + @Override public <T extends IgnitePlugin> T plugin() { + return (T)new IgnitePlugin() { + // No-op. + }; + } + + /** {@inheritDoc} */ + @Override public String version() { + return "1.0.0-SNAPSHOT"; + } + + /** {@inheritDoc} */ + @Override public String copyright() { + return ""; Review Comment: Same as in `org.apache.ignite.cdc.conflictresolve.CacheVersionConflictResolverPluginProvider#copyright` ########## modules/auto-activation-ext/src/main/java/opt/apache/ignite/activation/AutoActivationPluginProvider.java: ########## @@ -0,0 +1,159 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package opt.apache.ignite.activation; + +import java.io.Serializable; +import java.util.Collection; +import java.util.UUID; +import org.apache.ignite.Ignite; +import org.apache.ignite.IgniteCluster; +import org.apache.ignite.IgniteLogger; +import org.apache.ignite.cluster.ClusterNode; +import org.apache.ignite.cluster.ClusterState; +import org.apache.ignite.lang.IgnitePredicate; +import org.apache.ignite.plugin.CachePluginContext; +import org.apache.ignite.plugin.CachePluginProvider; +import org.apache.ignite.plugin.ExtensionRegistry; +import org.apache.ignite.plugin.IgnitePlugin; +import org.apache.ignite.plugin.PluginConfiguration; +import org.apache.ignite.plugin.PluginContext; +import org.apache.ignite.plugin.PluginProvider; +import org.apache.ignite.plugin.PluginValidationException; + +/** + * Activate cluster when specified condition meet Review Comment: ```suggestion * Activate cluster when specified condition meet. ``` ########## modules/auto-activation-ext/README.md: ########## @@ -0,0 +1,92 @@ +Apache Ignite Auto Activation Plugin +------------------------------------ +Apache Ignite Auto Activation plugin enables cluster activation at startup, subject to configured conditions. + +The plugin skips cluster activation in any of next cases: + +- Cluster state is ACTIVE or ACTIVE_READ_ONLY +- Cluster baseline is not empty +- `condition` contains any client node + +Depending on how you use Ignite, you can an extension using one of the following methods: Review Comment: > you can an extension using one looks like a verb is missing ########## modules/auto-activation-ext/src/test/java/opt/apache/ignite/activation/AutoActivationTest.java: ########## @@ -0,0 +1,735 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package opt.apache.ignite.activation; + +import java.util.Collections; +import java.util.Map; +import java.util.Set; +import org.apache.ignite.IgniteException; +import org.apache.ignite.cache.CacheAtomicityMode; +import org.apache.ignite.cache.CacheMode; +import org.apache.ignite.cluster.ClusterState; +import org.apache.ignite.configuration.CacheConfiguration; +import org.apache.ignite.configuration.DataRegionConfiguration; +import org.apache.ignite.configuration.DataStorageConfiguration; +import org.apache.ignite.configuration.IgniteConfiguration; +import org.apache.ignite.configuration.WALMode; +import org.apache.ignite.internal.IgniteEx; +import org.apache.ignite.plugin.PluginProvider; +import org.apache.ignite.testframework.ListeningTestLogger; +import org.apache.ignite.testframework.LogListener; +import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; +import org.junit.Test; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; + +import static org.apache.ignite.cluster.ClusterState.ACTIVE; +import static org.apache.ignite.cluster.ClusterState.ACTIVE_READ_ONLY; +import static org.apache.ignite.cluster.ClusterState.INACTIVE; +import static org.apache.ignite.testframework.GridTestUtils.assertThrows; + +/** + * {@link AutoActivationPluginProvider} test + */ +public class AutoActivationTest extends GridCommonAbstractTest { + /** Listening test logger. */ + private final ListeningTestLogger listeningLog = new ListeningTestLogger(log); + + /** */ + private final LogListener lsnrAlreadyAct = LogListener + .matches("Auto activation skipped - cluster already activated").build(); + + /** */ + private final LogListener lsnrBaseline = LogListener + .matches("Auto activation skipped - baseline is not empty").build(); + + /** */ + private final LogListener lsnrActMeet = LogListener + .matches("Auto activation plugin set cluster state ACTIVE - activation condition meet").build(); + + /** */ + private final LogListener lsnrActNotMeet = LogListener + .matches("Auto activation skipped - activation condition not meet").build(); + + /** */ + private final String NODE_0 = "node_0"; + + /** */ + private final String NODE_1 = "node_1"; + + /** */ + private final String NODE_2 = "node_2"; + + /** */ + private final String NODE_3 = "node_3"; + + /** */ + private final String ATTR = "CELL"; + + /** */ + private final String ATTR_VAL1 = "CELL_01"; + + /** */ + private final String ATTR_VAL2 = "CELL_02"; + + /** */ + private final Set<String> nodesConsistentIds = Set.of(NODE_0, NODE_1, NODE_2); + + /** {@inheritDoc} */ + @Override protected void beforeTest() throws Exception { + super.beforeTest(); + + stopAllGrids(); + + cleanPersistenceDir(); + + listeningLog.registerAllListeners(lsnrAlreadyAct, lsnrBaseline, lsnrActMeet, lsnrActNotMeet); + } + + /** {@inheritDoc} */ + @Override protected void afterTest() throws Exception { + stopAllGrids(true); + + cleanPersistenceDir(); + + listeningLog.clearListeners(); + + super.afterTest(); + } + + /** {@inheritDoc} */ + @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception { + final IgniteConfiguration igniteConfiguration = super.getConfiguration(igniteInstanceName); + + switch (igniteInstanceName) { + case NODE_0: + igniteConfiguration.setConsistentId(NODE_0); + break; + + case NODE_1: + igniteConfiguration.setConsistentId(NODE_1); + break; + + case NODE_2: + igniteConfiguration.setConsistentId(NODE_2); + break; + + default: throw new IllegalArgumentException("Unknown node: " + igniteInstanceName); + } Review Comment: Looks like switch is overhead here and we can simplify to smth like: ```java return super.getConfiguration(igniteInstanceName) .setConsistentId(igniteInstanceName) .setClusterStateOnStart(INACTIVE) .setGridLogger(listeningLog); ``` ########## modules/auto-activation-ext/src/main/java/opt/apache/ignite/activation/AutoActivationPluginProvider.java: ########## @@ -0,0 +1,159 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package opt.apache.ignite.activation; + +import java.io.Serializable; +import java.util.Collection; +import java.util.UUID; +import org.apache.ignite.Ignite; +import org.apache.ignite.IgniteCluster; +import org.apache.ignite.IgniteLogger; +import org.apache.ignite.cluster.ClusterNode; +import org.apache.ignite.cluster.ClusterState; +import org.apache.ignite.lang.IgnitePredicate; +import org.apache.ignite.plugin.CachePluginContext; +import org.apache.ignite.plugin.CachePluginProvider; +import org.apache.ignite.plugin.ExtensionRegistry; +import org.apache.ignite.plugin.IgnitePlugin; +import org.apache.ignite.plugin.PluginConfiguration; +import org.apache.ignite.plugin.PluginContext; +import org.apache.ignite.plugin.PluginProvider; +import org.apache.ignite.plugin.PluginValidationException; + +/** + * Activate cluster when specified condition meet + */ +public class AutoActivationPluginProvider implements PluginProvider<PluginConfiguration> { + /** */ + private final IgnitePredicate<Collection<ClusterNode>> condition; + + /** */ + private IgniteLogger logger; + + /** */ + private Ignite grid; + + /** + * @param condition Auto activation condition. + */ + public AutoActivationPluginProvider(IgnitePredicate<Collection<ClusterNode>> condition) { + if (condition == null) + throw new IllegalArgumentException("Auto activation condition must be set"); + + this.condition = condition; + } + + /** {@inheritDoc} */ + @Override public String name() { + return "Auto Activation Plugin"; + } + + /** {@inheritDoc} */ + @Override public <T extends IgnitePlugin> T plugin() { + return (T)new IgnitePlugin() { + // No-op. + }; + } + + /** {@inheritDoc} */ + @Override public String version() { + return "1.0.0-SNAPSHOT"; + } + + /** {@inheritDoc} */ + @Override public String copyright() { + return ""; + } + + /** {@inheritDoc} */ + @Override public void initExtensions(PluginContext pc, ExtensionRegistry er) { + logger = pc.log(this.getClass()); + grid = pc.grid(); + } + + /** {@inheritDoc} */ + @Override public <T> T createComponent(PluginContext pc, Class<T> type) { + return null; + } + + /** {@inheritDoc} */ + @Override public CachePluginProvider createCacheProvider(CachePluginContext cpc) { + return null; + } + + /** {@inheritDoc} */ + @Override public void start(PluginContext pc) { + // No-op. + } + + /** {@inheritDoc} */ + @Override public void stop(boolean bln) { + // No-op. + } + + /** {@inheritDoc} */ + @Override public void onIgniteStart() { + + IgniteCluster cluster = grid.cluster(); + + if (cluster.state() == ClusterState.ACTIVE || cluster.state() == ClusterState.ACTIVE_READ_ONLY) { + if (logger.isInfoEnabled()) + logger.info("Auto activation skipped - cluster already activated"); + + return; + } + + if (cluster.currentBaselineTopology() != null) { + if (logger.isInfoEnabled()) + logger.info("Auto activation skipped - baseline is not empty"); + + return; + } + + if (condition.apply(cluster.nodes())) { + if (logger.isInfoEnabled()) + logger.info("Auto activation plugin set cluster state ACTIVE - activation condition meet"); + + cluster.state(ClusterState.ACTIVE); + } + else { + if (logger.isInfoEnabled()) + logger.info("Auto activation skipped - activation condition not meet"); + } + } Review Comment: Usually it's better to use extra return instead of `else` ########## modules/auto-activation-ext/pom.xml: ########## @@ -0,0 +1,86 @@ +<?xml version="1.0" encoding="UTF-8"?> + +<!-- + ~ Licensed to the Apache Software Foundation (ASF) under one or more + ~ contributor license agreements. See the NOTICE file distributed with + ~ this work for additional information regarding copyright ownership. + ~ The ASF licenses this file to You under the Apache License, Version 2.0 + ~ (the "License"); you may not use this file except in compliance with + ~ the License. You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + --> + +<!-- + POM file. +--> Review Comment: Delete to fix `Ignoring multiple XML header comment!` warning for command `mvn clean install -pl :ignite-auto-activation-ext -am -Pcheckstyle -DskipTests` ########## modules/auto-activation-ext/src/main/java/opt/apache/ignite/activation/ActivateByNodeAttribute.java: ########## @@ -0,0 +1,72 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package opt.apache.ignite.activation; + +import java.util.Collection; +import java.util.LinkedHashSet; +import java.util.Set; +import org.apache.ignite.IgniteException; +import org.apache.ignite.cluster.ClusterNode; +import org.apache.ignite.lang.IgnitePredicate; + +/** + * Activate cluster when nodes with all specified attributes values join topology. + */ +public class ActivateByNodeAttribute implements IgnitePredicate<Collection<ClusterNode>> { + /** Node's attribute name. */ + private final String attrName; + + /** Collection of values for node's attribute. */ + private final Set<String> requiredValues; + + /** + * @param attributeName Node's attribute name. + * @param requiredValues List of values for node's attribute. + */ + public ActivateByNodeAttribute(String attributeName, Set<String> requiredValues) { + if (attributeName == null || attributeName.isBlank()) + throw new IllegalArgumentException("attributeName must be set"); + + if (requiredValues == null || requiredValues.isEmpty()) + throw new IllegalArgumentException("requiredValues must be set"); + + this.attrName = attributeName; + this.requiredValues = requiredValues; + } + + /** {@inheritDoc} */ + @Override public boolean apply(Collection<ClusterNode> nodes) { + Set<String> missingNodes = new LinkedHashSet<>(requiredValues); Review Comment: Better to use HashSet when we do not need insertion order for iteration ########## modules/auto-activation-ext/README.md: ########## @@ -0,0 +1,92 @@ +Apache Ignite Auto Activation Plugin +------------------------------------ +Apache Ignite Auto Activation plugin enables cluster activation at startup, subject to configured conditions. + +The plugin skips cluster activation in any of next cases: + +- Cluster state is ACTIVE or ACTIVE_READ_ONLY +- Cluster baseline is not empty +- `condition` contains any client node + +Depending on how you use Ignite, you can an extension using one of the following methods: Review Comment: > you can an extension using one looks like a verb is missing -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
