You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ignite.apache.org by GitBox <gi...@apache.org> on 2021/10/27 18:40:03 UTC

[GitHub] [ignite] ololo3000 opened a new pull request #9534: wip

ololo3000 opened a new pull request #9534:
URL: https://github.com/apache/ignite/pull/9534


   Thank you for submitting the pull request to the Apache Ignite.
   
   In order to streamline the review of the contribution 
   we ask you to ensure the following steps have been taken:
   
   ### The Contribution Checklist
   - [ ] There is a single JIRA ticket related to the pull request. 
   - [ ] The web-link to the pull request is attached to the JIRA ticket.
   - [ ] The JIRA ticket has the _Patch Available_ state.
   - [ ] The pull request body describes changes that have been made. 
   The description explains _WHAT_ and _WHY_ was made instead of _HOW_.
   - [ ] The pull request title is treated as the final commit message. 
   The following pattern must be used: `IGNITE-XXXX Change summary` where `XXXX` - number of JIRA issue.
   - [ ] A reviewer has been mentioned through the JIRA comments 
   (see [the Maintainers list](https://cwiki.apache.org/confluence/display/IGNITE/How+to+Contribute#HowtoContribute-ReviewProcessandMaintainers)) 
   - [ ] The pull request has been checked by the Teamcity Bot and 
   the `green visa` attached to the JIRA ticket (see [TC.Bot: Check PR](https://mtcga.gridgain.com/prs.html))
   
   ### Notes
   - [How to Contribute](https://cwiki.apache.org/confluence/display/IGNITE/How+to+Contribute)
   - [Coding abbreviation rules](https://cwiki.apache.org/confluence/display/IGNITE/Abbreviation+Rules)
   - [Coding Guidelines](https://cwiki.apache.org/confluence/display/IGNITE/Coding+Guidelines)
   - [Apache Ignite Teamcity Bot](https://cwiki.apache.org/confluence/display/IGNITE/Apache+Ignite+Teamcity+Bot)
   
   If you need any help, please email dev@ignite.apache.org or ask anу advice on http://asf.slack.com _#ignite_ channel.
   


-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [ignite] ololo3000 commented on a change in pull request #9534: wip

Posted by GitBox <gi...@apache.org>.
ololo3000 commented on a change in pull request #9534:
URL: https://github.com/apache/ignite/pull/9534#discussion_r741002238



##########
File path: modules/core/src/main/java/org/apache/ignite/cache/validation/IgnitePluggableSegmentationResolver.java
##########
@@ -0,0 +1,275 @@
+/*
+ * 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 org.apache.ignite.cache.validation;
+
+import java.io.Serializable;
+import java.util.Collection;
+import java.util.List;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.stream.Collectors;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.cluster.BaselineNode;
+import org.apache.ignite.cluster.ClusterNode;
+import org.apache.ignite.events.DiscoveryEvent;
+import org.apache.ignite.internal.GridKernalContext;
+import org.apache.ignite.internal.cluster.DetachedClusterNode;
+import org.apache.ignite.internal.managers.discovery.CustomEventListener;
+import org.apache.ignite.internal.managers.discovery.DiscoCache;
+import org.apache.ignite.internal.managers.eventstorage.DiscoveryEventListener;
+import org.apache.ignite.internal.managers.eventstorage.HighPriorityListener;
+import org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion;
+import org.apache.ignite.internal.processors.cluster.ChangeGlobalStateFinishMessage;
+import org.apache.ignite.internal.processors.configuration.distributed.DistributedConfigurationLifecycleListener;
+import org.apache.ignite.internal.processors.configuration.distributed.DistributedPropertyDispatcher;
+import org.apache.ignite.internal.processors.configuration.distributed.SimpleDistributedProperty;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.thread.IgniteThreadPoolExecutor;
+import org.apache.ignite.thread.OomExceptionHandler;
+
+import static java.lang.Boolean.TRUE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE_READ_ONLY;
+import static org.apache.ignite.configuration.IgniteConfiguration.DFLT_THREAD_KEEP_ALIVE_TIME;
+import static org.apache.ignite.events.EventType.EVT_NODE_FAILED;
+import static org.apache.ignite.events.EventType.EVT_NODE_JOINED;
+import static org.apache.ignite.events.EventType.EVT_NODE_LEFT;
+import static org.apache.ignite.internal.cluster.DistributedConfigurationUtils.setDefaultValue;
+import static org.apache.ignite.internal.managers.communication.GridIoPolicy.UNDEFINED;
+
+/** */
+public class IgnitePluggableSegmentationResolver implements PluggableSegmentationResolver {
+   /** */
+    public static final String ATTR_SEG_RESOLVER_CONFIGURED = "org.apache.ignite.segmentation.resolver.configured";
+
+    /** */
+    public static final String SEG_RESOLVER_ENABLED_PROP_NAME = "org.apache.ignite.segmentation.resolver.enabled";
+
+    /** */
+    private static final String SEG_RESOLVER_THREAD_PREFIX = "segmentation-resolver";
+
+    /** */
+    private static final int[] TOP_CHANGED_EVTS = new int[] {
+        EVT_NODE_LEFT,
+        EVT_NODE_JOINED,
+        EVT_NODE_FAILED
+    };
+
+    /** */
+    private final SimpleDistributedProperty<Boolean> segResolverEnabledProp = new SimpleDistributedProperty<>(
+        SEG_RESOLVER_ENABLED_PROP_NAME,
+        Boolean::parseBoolean
+    );
+
+    /** Ignite kernel context. */
+    private final GridKernalContext ctx;
+
+    /** Ignite logger. */
+    private final IgniteLogger log;
+
+    /** */
+    private final IgniteThreadPoolExecutor stateChangeExec;
+
+    /** */
+    private long lastCheckedTopVer;
+
+    /**  */
+    private volatile State state = State.VALID;
+
+    /** @param ctx Ignite kernel context. */
+    public IgnitePluggableSegmentationResolver(GridKernalContext ctx) {
+        this.ctx = ctx;
+
+        log = ctx.log(getClass());
+
+        stateChangeExec = new IgniteThreadPoolExecutor(
+            SEG_RESOLVER_THREAD_PREFIX,
+            ctx.igniteInstanceName(),
+            1,
+            1,
+            DFLT_THREAD_KEEP_ALIVE_TIME,
+            new LinkedBlockingQueue<>(),
+            UNDEFINED,
+            new OomExceptionHandler(ctx));
+
+        stateChangeExec.allowCoreThreadTimeOut(true);
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean isValidSegment() {
+        return isDisabled() || state != State.INVALID;
+    }
+
+    /** */
+    public void start() {
+        if (ctx.clientNode())
+            return;
+
+        ctx.addNodeAttribute(ATTR_SEG_RESOLVER_CONFIGURED, true);
+
+        ctx.event().addDiscoveryEventListener(new TopologyChangedEventListener(), TOP_CHANGED_EVTS);
+
+        ctx.discovery().setCustomEventListener(
+            ChangeGlobalStateFinishMessage.class,
+            new ClusterStateChangedEventListener()
+        );
+
+        ctx.internalSubscriptionProcessor().registerDistributedConfigurationListener(
+            new DistributedConfigurationLifecycleListener() {
+                /** {@inheritDoc} */
+                @Override public void onReadyToRegister(DistributedPropertyDispatcher dispatcher) {
+                    dispatcher.registerProperty(segResolverEnabledProp);
+                }
+
+                /** {@inheritDoc} */
+                @Override public void onReadyToWrite() {
+                    setDefaultValue(segResolverEnabledProp, U.isLocalNodeCoordinator(ctx.discovery()), log);
+                }
+            });
+    }
+
+    /** @return Discovery data. */
+    public Serializable provideDiscoveryData() {
+        return state;
+    }
+
+    /** @param data Discovery data. */
+    public void onDiscoveryDataReceived(Serializable data) {
+        state = (State)data;
+    }
+
+    /** @param node Node. */
+    public void validateNewNode(ClusterNode node) {
+        if (node.isClient())
+            return;
+
+        if (!TRUE.equals(node.attribute(ATTR_SEG_RESOLVER_CONFIGURED))) {
+            throw new IgniteException( "The Segmentation Resolver plugin is not configured for the server node that is" +
+                " trying to join the cluster. Since the Segmentation Resolver is only applicable if all server nodes" +
+                " in the cluster have one, node join request will be rejected [rejectedNodeId=" + node.id() + ']');
+        }
+
+        if (state == State.VALID) {
+            DiscoCache discoCache = ctx.discovery().discoCache(new AffinityTopologyVersion(lastCheckedTopVer, 0));
+
+            if (discoCache != null) {
+                for (ClusterNode srv : discoCache.serverNodes()) {
+                    if (!ctx.discovery().alive(srv))
+                        throw new IgniteException("Node join request will be rejected due to concurrent node left" +
+                            " process handling [rejectedNodeId=" + node.id() + ']');
+                }
+            }
+        }
+    }
+
+    /** */
+    private boolean isDisabled() {
+        Boolean res = segResolverEnabledProp.get();
+
+        return res == null || !res;
+    }
+
+    /** @return return. */
+    private String formatTopologyNodes(Collection<ClusterNode> nodes) {
+        return nodes.stream().map(n -> n.id().toString()).collect(Collectors.joining(", "));
+    }
+
+    /** */
+    private class TopologyChangedEventListener implements DiscoveryEventListener, HighPriorityListener {
+        /** {@inheritDoc} */
+        @Override public void onEvent(DiscoveryEvent evt, DiscoCache discoCache) {
+            lastCheckedTopVer = evt.topologyVersion();
+
+            if (isDisabled())
+                return;
+
+            if (state == State.VALID && evt.type() == EVT_NODE_FAILED) {
+                List<? extends BaselineNode> baselineNodes = discoCache.baselineNodes();
+
+                if (baselineNodes != null && aliveBaselineNodes(baselineNodes) < baselineNodes.size() / 2 + 1) {
+                    state = State.INVALID;
+
+                    stateChangeExec.execute(() -> {
+                        try {
+                            ctx.cluster().get().state(ACTIVE_READ_ONLY);
+                        }
+                        catch (Throwable e) {
+                            U.error(
+                                log,
+                                "Failed to automatically switch state of the segmented cluster to the READ-ONLY mode" +
+                                    " [segmentedNodes=" + formatTopologyNodes(discoCache.allNodes()) + "]. Cache writes" +
+                                    " are already restricted for all configured caches, but this step is still required" +
+                                    " in order to be able to unlock cache writes in the future. Retry this operation" +
+                                    " manually, if possible.",
+                                e
+                            );
+                        }
+                    });
+
+                    U.warn(log, "Cluster segmentation was detected [segmentedNodes=" +
+                        formatTopologyNodes(discoCache.allNodes()) + ']');
+                }
+            }
+
+            if (ctx.state().isBaselineAutoAdjustEnabled())

Review comment:
       .Done.

##########
File path: modules/core/src/main/java/org/apache/ignite/cache/validation/IgnitePluggableSegmentationResolver.java
##########
@@ -0,0 +1,275 @@
+/*
+ * 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 org.apache.ignite.cache.validation;
+
+import java.io.Serializable;
+import java.util.Collection;
+import java.util.List;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.stream.Collectors;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.cluster.BaselineNode;
+import org.apache.ignite.cluster.ClusterNode;
+import org.apache.ignite.events.DiscoveryEvent;
+import org.apache.ignite.internal.GridKernalContext;
+import org.apache.ignite.internal.cluster.DetachedClusterNode;
+import org.apache.ignite.internal.managers.discovery.CustomEventListener;
+import org.apache.ignite.internal.managers.discovery.DiscoCache;
+import org.apache.ignite.internal.managers.eventstorage.DiscoveryEventListener;
+import org.apache.ignite.internal.managers.eventstorage.HighPriorityListener;
+import org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion;
+import org.apache.ignite.internal.processors.cluster.ChangeGlobalStateFinishMessage;
+import org.apache.ignite.internal.processors.configuration.distributed.DistributedConfigurationLifecycleListener;
+import org.apache.ignite.internal.processors.configuration.distributed.DistributedPropertyDispatcher;
+import org.apache.ignite.internal.processors.configuration.distributed.SimpleDistributedProperty;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.thread.IgniteThreadPoolExecutor;
+import org.apache.ignite.thread.OomExceptionHandler;
+
+import static java.lang.Boolean.TRUE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE_READ_ONLY;
+import static org.apache.ignite.configuration.IgniteConfiguration.DFLT_THREAD_KEEP_ALIVE_TIME;
+import static org.apache.ignite.events.EventType.EVT_NODE_FAILED;
+import static org.apache.ignite.events.EventType.EVT_NODE_JOINED;
+import static org.apache.ignite.events.EventType.EVT_NODE_LEFT;
+import static org.apache.ignite.internal.cluster.DistributedConfigurationUtils.setDefaultValue;
+import static org.apache.ignite.internal.managers.communication.GridIoPolicy.UNDEFINED;
+
+/** */
+public class IgnitePluggableSegmentationResolver implements PluggableSegmentationResolver {
+   /** */
+    public static final String ATTR_SEG_RESOLVER_CONFIGURED = "org.apache.ignite.segmentation.resolver.configured";
+
+    /** */
+    public static final String SEG_RESOLVER_ENABLED_PROP_NAME = "org.apache.ignite.segmentation.resolver.enabled";
+
+    /** */
+    private static final String SEG_RESOLVER_THREAD_PREFIX = "segmentation-resolver";
+
+    /** */
+    private static final int[] TOP_CHANGED_EVTS = new int[] {
+        EVT_NODE_LEFT,
+        EVT_NODE_JOINED,
+        EVT_NODE_FAILED
+    };
+
+    /** */
+    private final SimpleDistributedProperty<Boolean> segResolverEnabledProp = new SimpleDistributedProperty<>(
+        SEG_RESOLVER_ENABLED_PROP_NAME,
+        Boolean::parseBoolean
+    );
+
+    /** Ignite kernel context. */
+    private final GridKernalContext ctx;
+
+    /** Ignite logger. */
+    private final IgniteLogger log;
+
+    /** */
+    private final IgniteThreadPoolExecutor stateChangeExec;
+
+    /** */
+    private long lastCheckedTopVer;
+
+    /**  */
+    private volatile State state = State.VALID;
+
+    /** @param ctx Ignite kernel context. */
+    public IgnitePluggableSegmentationResolver(GridKernalContext ctx) {
+        this.ctx = ctx;
+
+        log = ctx.log(getClass());
+
+        stateChangeExec = new IgniteThreadPoolExecutor(
+            SEG_RESOLVER_THREAD_PREFIX,
+            ctx.igniteInstanceName(),
+            1,
+            1,
+            DFLT_THREAD_KEEP_ALIVE_TIME,
+            new LinkedBlockingQueue<>(),
+            UNDEFINED,
+            new OomExceptionHandler(ctx));
+
+        stateChangeExec.allowCoreThreadTimeOut(true);
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean isValidSegment() {
+        return isDisabled() || state != State.INVALID;
+    }
+
+    /** */
+    public void start() {
+        if (ctx.clientNode())
+            return;
+
+        ctx.addNodeAttribute(ATTR_SEG_RESOLVER_CONFIGURED, true);
+
+        ctx.event().addDiscoveryEventListener(new TopologyChangedEventListener(), TOP_CHANGED_EVTS);
+
+        ctx.discovery().setCustomEventListener(
+            ChangeGlobalStateFinishMessage.class,
+            new ClusterStateChangedEventListener()
+        );
+
+        ctx.internalSubscriptionProcessor().registerDistributedConfigurationListener(
+            new DistributedConfigurationLifecycleListener() {
+                /** {@inheritDoc} */
+                @Override public void onReadyToRegister(DistributedPropertyDispatcher dispatcher) {
+                    dispatcher.registerProperty(segResolverEnabledProp);
+                }
+
+                /** {@inheritDoc} */
+                @Override public void onReadyToWrite() {
+                    setDefaultValue(segResolverEnabledProp, U.isLocalNodeCoordinator(ctx.discovery()), log);
+                }
+            });
+    }
+
+    /** @return Discovery data. */
+    public Serializable provideDiscoveryData() {
+        return state;
+    }
+
+    /** @param data Discovery data. */
+    public void onDiscoveryDataReceived(Serializable data) {
+        state = (State)data;
+    }
+
+    /** @param node Node. */
+    public void validateNewNode(ClusterNode node) {
+        if (node.isClient())
+            return;
+
+        if (!TRUE.equals(node.attribute(ATTR_SEG_RESOLVER_CONFIGURED))) {
+            throw new IgniteException( "The Segmentation Resolver plugin is not configured for the server node that is" +
+                " trying to join the cluster. Since the Segmentation Resolver is only applicable if all server nodes" +
+                " in the cluster have one, node join request will be rejected [rejectedNodeId=" + node.id() + ']');
+        }
+
+        if (state == State.VALID) {
+            DiscoCache discoCache = ctx.discovery().discoCache(new AffinityTopologyVersion(lastCheckedTopVer, 0));
+
+            if (discoCache != null) {
+                for (ClusterNode srv : discoCache.serverNodes()) {
+                    if (!ctx.discovery().alive(srv))
+                        throw new IgniteException("Node join request will be rejected due to concurrent node left" +
+                            " process handling [rejectedNodeId=" + node.id() + ']');
+                }
+            }
+        }
+    }
+
+    /** */
+    private boolean isDisabled() {
+        Boolean res = segResolverEnabledProp.get();
+
+        return res == null || !res;
+    }
+
+    /** @return return. */
+    private String formatTopologyNodes(Collection<ClusterNode> nodes) {

Review comment:
       Done.

##########
File path: modules/core/src/main/java/org/apache/ignite/cache/validation/IgnitePluggableSegmentationResolver.java
##########
@@ -0,0 +1,275 @@
+/*
+ * 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 org.apache.ignite.cache.validation;
+
+import java.io.Serializable;
+import java.util.Collection;
+import java.util.List;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.stream.Collectors;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.cluster.BaselineNode;
+import org.apache.ignite.cluster.ClusterNode;
+import org.apache.ignite.events.DiscoveryEvent;
+import org.apache.ignite.internal.GridKernalContext;
+import org.apache.ignite.internal.cluster.DetachedClusterNode;
+import org.apache.ignite.internal.managers.discovery.CustomEventListener;
+import org.apache.ignite.internal.managers.discovery.DiscoCache;
+import org.apache.ignite.internal.managers.eventstorage.DiscoveryEventListener;
+import org.apache.ignite.internal.managers.eventstorage.HighPriorityListener;
+import org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion;
+import org.apache.ignite.internal.processors.cluster.ChangeGlobalStateFinishMessage;
+import org.apache.ignite.internal.processors.configuration.distributed.DistributedConfigurationLifecycleListener;
+import org.apache.ignite.internal.processors.configuration.distributed.DistributedPropertyDispatcher;
+import org.apache.ignite.internal.processors.configuration.distributed.SimpleDistributedProperty;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.thread.IgniteThreadPoolExecutor;
+import org.apache.ignite.thread.OomExceptionHandler;
+
+import static java.lang.Boolean.TRUE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE_READ_ONLY;
+import static org.apache.ignite.configuration.IgniteConfiguration.DFLT_THREAD_KEEP_ALIVE_TIME;
+import static org.apache.ignite.events.EventType.EVT_NODE_FAILED;
+import static org.apache.ignite.events.EventType.EVT_NODE_JOINED;
+import static org.apache.ignite.events.EventType.EVT_NODE_LEFT;
+import static org.apache.ignite.internal.cluster.DistributedConfigurationUtils.setDefaultValue;
+import static org.apache.ignite.internal.managers.communication.GridIoPolicy.UNDEFINED;
+
+/** */
+public class IgnitePluggableSegmentationResolver implements PluggableSegmentationResolver {
+   /** */
+    public static final String ATTR_SEG_RESOLVER_CONFIGURED = "org.apache.ignite.segmentation.resolver.configured";
+
+    /** */
+    public static final String SEG_RESOLVER_ENABLED_PROP_NAME = "org.apache.ignite.segmentation.resolver.enabled";
+
+    /** */
+    private static final String SEG_RESOLVER_THREAD_PREFIX = "segmentation-resolver";
+
+    /** */
+    private static final int[] TOP_CHANGED_EVTS = new int[] {
+        EVT_NODE_LEFT,
+        EVT_NODE_JOINED,
+        EVT_NODE_FAILED
+    };
+
+    /** */
+    private final SimpleDistributedProperty<Boolean> segResolverEnabledProp = new SimpleDistributedProperty<>(
+        SEG_RESOLVER_ENABLED_PROP_NAME,
+        Boolean::parseBoolean
+    );
+
+    /** Ignite kernel context. */
+    private final GridKernalContext ctx;
+
+    /** Ignite logger. */
+    private final IgniteLogger log;
+
+    /** */
+    private final IgniteThreadPoolExecutor stateChangeExec;
+
+    /** */
+    private long lastCheckedTopVer;
+
+    /**  */
+    private volatile State state = State.VALID;
+
+    /** @param ctx Ignite kernel context. */
+    public IgnitePluggableSegmentationResolver(GridKernalContext ctx) {
+        this.ctx = ctx;
+
+        log = ctx.log(getClass());
+
+        stateChangeExec = new IgniteThreadPoolExecutor(
+            SEG_RESOLVER_THREAD_PREFIX,
+            ctx.igniteInstanceName(),
+            1,
+            1,
+            DFLT_THREAD_KEEP_ALIVE_TIME,
+            new LinkedBlockingQueue<>(),
+            UNDEFINED,
+            new OomExceptionHandler(ctx));
+
+        stateChangeExec.allowCoreThreadTimeOut(true);
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean isValidSegment() {
+        return isDisabled() || state != State.INVALID;
+    }
+
+    /** */
+    public void start() {
+        if (ctx.clientNode())
+            return;
+
+        ctx.addNodeAttribute(ATTR_SEG_RESOLVER_CONFIGURED, true);
+
+        ctx.event().addDiscoveryEventListener(new TopologyChangedEventListener(), TOP_CHANGED_EVTS);
+
+        ctx.discovery().setCustomEventListener(
+            ChangeGlobalStateFinishMessage.class,
+            new ClusterStateChangedEventListener()
+        );
+
+        ctx.internalSubscriptionProcessor().registerDistributedConfigurationListener(
+            new DistributedConfigurationLifecycleListener() {
+                /** {@inheritDoc} */
+                @Override public void onReadyToRegister(DistributedPropertyDispatcher dispatcher) {
+                    dispatcher.registerProperty(segResolverEnabledProp);
+                }
+
+                /** {@inheritDoc} */
+                @Override public void onReadyToWrite() {
+                    setDefaultValue(segResolverEnabledProp, U.isLocalNodeCoordinator(ctx.discovery()), log);
+                }
+            });
+    }
+
+    /** @return Discovery data. */
+    public Serializable provideDiscoveryData() {
+        return state;
+    }
+
+    /** @param data Discovery data. */
+    public void onDiscoveryDataReceived(Serializable data) {
+        state = (State)data;
+    }
+
+    /** @param node Node. */
+    public void validateNewNode(ClusterNode node) {
+        if (node.isClient())
+            return;
+
+        if (!TRUE.equals(node.attribute(ATTR_SEG_RESOLVER_CONFIGURED))) {
+            throw new IgniteException( "The Segmentation Resolver plugin is not configured for the server node that is" +
+                " trying to join the cluster. Since the Segmentation Resolver is only applicable if all server nodes" +
+                " in the cluster have one, node join request will be rejected [rejectedNodeId=" + node.id() + ']');
+        }
+
+        if (state == State.VALID) {
+            DiscoCache discoCache = ctx.discovery().discoCache(new AffinityTopologyVersion(lastCheckedTopVer, 0));
+
+            if (discoCache != null) {
+                for (ClusterNode srv : discoCache.serverNodes()) {
+                    if (!ctx.discovery().alive(srv))
+                        throw new IgniteException("Node join request will be rejected due to concurrent node left" +
+                            " process handling [rejectedNodeId=" + node.id() + ']');
+                }
+            }
+        }
+    }
+
+    /** */
+    private boolean isDisabled() {
+        Boolean res = segResolverEnabledProp.get();
+
+        return res == null || !res;
+    }
+
+    /** @return return. */
+    private String formatTopologyNodes(Collection<ClusterNode> nodes) {
+        return nodes.stream().map(n -> n.id().toString()).collect(Collectors.joining(", "));
+    }
+
+    /** */
+    private class TopologyChangedEventListener implements DiscoveryEventListener, HighPriorityListener {
+        /** {@inheritDoc} */
+        @Override public void onEvent(DiscoveryEvent evt, DiscoCache discoCache) {
+            lastCheckedTopVer = evt.topologyVersion();
+
+            if (isDisabled())
+                return;
+
+            if (state == State.VALID && evt.type() == EVT_NODE_FAILED) {

Review comment:
       Done.

##########
File path: modules/core/src/main/java/org/apache/ignite/cache/validation/IgnitePluggableSegmentationResolver.java
##########
@@ -0,0 +1,275 @@
+/*
+ * 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 org.apache.ignite.cache.validation;
+
+import java.io.Serializable;
+import java.util.Collection;
+import java.util.List;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.stream.Collectors;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.cluster.BaselineNode;
+import org.apache.ignite.cluster.ClusterNode;
+import org.apache.ignite.events.DiscoveryEvent;
+import org.apache.ignite.internal.GridKernalContext;
+import org.apache.ignite.internal.cluster.DetachedClusterNode;
+import org.apache.ignite.internal.managers.discovery.CustomEventListener;
+import org.apache.ignite.internal.managers.discovery.DiscoCache;
+import org.apache.ignite.internal.managers.eventstorage.DiscoveryEventListener;
+import org.apache.ignite.internal.managers.eventstorage.HighPriorityListener;
+import org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion;
+import org.apache.ignite.internal.processors.cluster.ChangeGlobalStateFinishMessage;
+import org.apache.ignite.internal.processors.configuration.distributed.DistributedConfigurationLifecycleListener;
+import org.apache.ignite.internal.processors.configuration.distributed.DistributedPropertyDispatcher;
+import org.apache.ignite.internal.processors.configuration.distributed.SimpleDistributedProperty;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.thread.IgniteThreadPoolExecutor;
+import org.apache.ignite.thread.OomExceptionHandler;
+
+import static java.lang.Boolean.TRUE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE_READ_ONLY;
+import static org.apache.ignite.configuration.IgniteConfiguration.DFLT_THREAD_KEEP_ALIVE_TIME;
+import static org.apache.ignite.events.EventType.EVT_NODE_FAILED;
+import static org.apache.ignite.events.EventType.EVT_NODE_JOINED;
+import static org.apache.ignite.events.EventType.EVT_NODE_LEFT;
+import static org.apache.ignite.internal.cluster.DistributedConfigurationUtils.setDefaultValue;
+import static org.apache.ignite.internal.managers.communication.GridIoPolicy.UNDEFINED;
+
+/** */
+public class IgnitePluggableSegmentationResolver implements PluggableSegmentationResolver {
+   /** */
+    public static final String ATTR_SEG_RESOLVER_CONFIGURED = "org.apache.ignite.segmentation.resolver.configured";
+
+    /** */
+    public static final String SEG_RESOLVER_ENABLED_PROP_NAME = "org.apache.ignite.segmentation.resolver.enabled";
+
+    /** */
+    private static final String SEG_RESOLVER_THREAD_PREFIX = "segmentation-resolver";
+
+    /** */
+    private static final int[] TOP_CHANGED_EVTS = new int[] {
+        EVT_NODE_LEFT,
+        EVT_NODE_JOINED,
+        EVT_NODE_FAILED
+    };
+
+    /** */
+    private final SimpleDistributedProperty<Boolean> segResolverEnabledProp = new SimpleDistributedProperty<>(
+        SEG_RESOLVER_ENABLED_PROP_NAME,
+        Boolean::parseBoolean
+    );
+
+    /** Ignite kernel context. */
+    private final GridKernalContext ctx;
+
+    /** Ignite logger. */
+    private final IgniteLogger log;
+
+    /** */
+    private final IgniteThreadPoolExecutor stateChangeExec;
+
+    /** */
+    private long lastCheckedTopVer;
+
+    /**  */
+    private volatile State state = State.VALID;
+
+    /** @param ctx Ignite kernel context. */
+    public IgnitePluggableSegmentationResolver(GridKernalContext ctx) {
+        this.ctx = ctx;
+
+        log = ctx.log(getClass());
+
+        stateChangeExec = new IgniteThreadPoolExecutor(
+            SEG_RESOLVER_THREAD_PREFIX,
+            ctx.igniteInstanceName(),
+            1,
+            1,
+            DFLT_THREAD_KEEP_ALIVE_TIME,
+            new LinkedBlockingQueue<>(),
+            UNDEFINED,
+            new OomExceptionHandler(ctx));
+
+        stateChangeExec.allowCoreThreadTimeOut(true);
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean isValidSegment() {
+        return isDisabled() || state != State.INVALID;
+    }
+
+    /** */
+    public void start() {
+        if (ctx.clientNode())
+            return;
+
+        ctx.addNodeAttribute(ATTR_SEG_RESOLVER_CONFIGURED, true);
+
+        ctx.event().addDiscoveryEventListener(new TopologyChangedEventListener(), TOP_CHANGED_EVTS);
+
+        ctx.discovery().setCustomEventListener(
+            ChangeGlobalStateFinishMessage.class,
+            new ClusterStateChangedEventListener()
+        );
+
+        ctx.internalSubscriptionProcessor().registerDistributedConfigurationListener(
+            new DistributedConfigurationLifecycleListener() {
+                /** {@inheritDoc} */
+                @Override public void onReadyToRegister(DistributedPropertyDispatcher dispatcher) {
+                    dispatcher.registerProperty(segResolverEnabledProp);
+                }
+
+                /** {@inheritDoc} */
+                @Override public void onReadyToWrite() {
+                    setDefaultValue(segResolverEnabledProp, U.isLocalNodeCoordinator(ctx.discovery()), log);
+                }
+            });
+    }
+
+    /** @return Discovery data. */
+    public Serializable provideDiscoveryData() {
+        return state;
+    }
+
+    /** @param data Discovery data. */
+    public void onDiscoveryDataReceived(Serializable data) {
+        state = (State)data;
+    }
+
+    /** @param node Node. */
+    public void validateNewNode(ClusterNode node) {
+        if (node.isClient())
+            return;
+
+        if (!TRUE.equals(node.attribute(ATTR_SEG_RESOLVER_CONFIGURED))) {
+            throw new IgniteException( "The Segmentation Resolver plugin is not configured for the server node that is" +
+                " trying to join the cluster. Since the Segmentation Resolver is only applicable if all server nodes" +
+                " in the cluster have one, node join request will be rejected [rejectedNodeId=" + node.id() + ']');
+        }
+
+        if (state == State.VALID) {
+            DiscoCache discoCache = ctx.discovery().discoCache(new AffinityTopologyVersion(lastCheckedTopVer, 0));
+
+            if (discoCache != null) {
+                for (ClusterNode srv : discoCache.serverNodes()) {
+                    if (!ctx.discovery().alive(srv))
+                        throw new IgniteException("Node join request will be rejected due to concurrent node left" +
+                            " process handling [rejectedNodeId=" + node.id() + ']');
+                }
+            }
+        }
+    }
+
+    /** */
+    private boolean isDisabled() {
+        Boolean res = segResolverEnabledProp.get();
+
+        return res == null || !res;
+    }
+
+    /** @return return. */
+    private String formatTopologyNodes(Collection<ClusterNode> nodes) {
+        return nodes.stream().map(n -> n.id().toString()).collect(Collectors.joining(", "));
+    }
+
+    /** */
+    private class TopologyChangedEventListener implements DiscoveryEventListener, HighPriorityListener {
+        /** {@inheritDoc} */
+        @Override public void onEvent(DiscoveryEvent evt, DiscoCache discoCache) {
+            lastCheckedTopVer = evt.topologyVersion();
+
+            if (isDisabled())
+                return;
+
+            if (state == State.VALID && evt.type() == EVT_NODE_FAILED) {
+                List<? extends BaselineNode> baselineNodes = discoCache.baselineNodes();
+
+                if (baselineNodes != null && aliveBaselineNodes(baselineNodes) < baselineNodes.size() / 2 + 1) {
+                    state = State.INVALID;
+
+                    stateChangeExec.execute(() -> {
+                        try {
+                            ctx.cluster().get().state(ACTIVE_READ_ONLY);
+                        }
+                        catch (Throwable e) {
+                            U.error(
+                                log,
+                                "Failed to automatically switch state of the segmented cluster to the READ-ONLY mode" +
+                                    " [segmentedNodes=" + formatTopologyNodes(discoCache.allNodes()) + "]. Cache writes" +
+                                    " are already restricted for all configured caches, but this step is still required" +
+                                    " in order to be able to unlock cache writes in the future. Retry this operation" +
+                                    " manually, if possible.",
+                                e
+                            );
+                        }
+                    });
+
+                    U.warn(log, "Cluster segmentation was detected [segmentedNodes=" +
+                        formatTopologyNodes(discoCache.allNodes()) + ']');
+                }
+            }
+
+            if (ctx.state().isBaselineAutoAdjustEnabled())
+                U.warn(log, "Segmentation Resolver requires baseline to be configured. If no baseline is" +
+                    " set, any topology change is considered valid.");
+        }
+
+        /** {@inheritDoc} */
+        @Override public int order() {
+            return 0;
+        }
+
+        /** */
+        private int aliveBaselineNodes(Collection<? extends BaselineNode> baselineNodes) {
+            int res = 0;
+
+            for (BaselineNode node : baselineNodes) {
+                if (!(node instanceof DetachedClusterNode))
+                    ++res;
+            }
+
+            return res;
+        }
+    }
+
+    /** */
+    private class ClusterStateChangedEventListener implements CustomEventListener<ChangeGlobalStateFinishMessage> {

Review comment:
       But in this case we will crumble up the `start` method and won't  gain any real profit.

##########
File path: modules/core/src/main/java/org/apache/ignite/cache/validation/IgnitePluggableSegmentationResolver.java
##########
@@ -0,0 +1,275 @@
+/*
+ * 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 org.apache.ignite.cache.validation;
+
+import java.io.Serializable;
+import java.util.Collection;
+import java.util.List;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.stream.Collectors;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.cluster.BaselineNode;
+import org.apache.ignite.cluster.ClusterNode;
+import org.apache.ignite.events.DiscoveryEvent;
+import org.apache.ignite.internal.GridKernalContext;
+import org.apache.ignite.internal.cluster.DetachedClusterNode;
+import org.apache.ignite.internal.managers.discovery.CustomEventListener;
+import org.apache.ignite.internal.managers.discovery.DiscoCache;
+import org.apache.ignite.internal.managers.eventstorage.DiscoveryEventListener;
+import org.apache.ignite.internal.managers.eventstorage.HighPriorityListener;
+import org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion;
+import org.apache.ignite.internal.processors.cluster.ChangeGlobalStateFinishMessage;
+import org.apache.ignite.internal.processors.configuration.distributed.DistributedConfigurationLifecycleListener;
+import org.apache.ignite.internal.processors.configuration.distributed.DistributedPropertyDispatcher;
+import org.apache.ignite.internal.processors.configuration.distributed.SimpleDistributedProperty;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.thread.IgniteThreadPoolExecutor;
+import org.apache.ignite.thread.OomExceptionHandler;
+
+import static java.lang.Boolean.TRUE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE_READ_ONLY;
+import static org.apache.ignite.configuration.IgniteConfiguration.DFLT_THREAD_KEEP_ALIVE_TIME;
+import static org.apache.ignite.events.EventType.EVT_NODE_FAILED;
+import static org.apache.ignite.events.EventType.EVT_NODE_JOINED;
+import static org.apache.ignite.events.EventType.EVT_NODE_LEFT;
+import static org.apache.ignite.internal.cluster.DistributedConfigurationUtils.setDefaultValue;
+import static org.apache.ignite.internal.managers.communication.GridIoPolicy.UNDEFINED;
+
+/** */
+public class IgnitePluggableSegmentationResolver implements PluggableSegmentationResolver {
+   /** */
+    public static final String ATTR_SEG_RESOLVER_CONFIGURED = "org.apache.ignite.segmentation.resolver.configured";
+
+    /** */
+    public static final String SEG_RESOLVER_ENABLED_PROP_NAME = "org.apache.ignite.segmentation.resolver.enabled";
+
+    /** */
+    private static final String SEG_RESOLVER_THREAD_PREFIX = "segmentation-resolver";
+
+    /** */
+    private static final int[] TOP_CHANGED_EVTS = new int[] {
+        EVT_NODE_LEFT,
+        EVT_NODE_JOINED,
+        EVT_NODE_FAILED
+    };
+
+    /** */
+    private final SimpleDistributedProperty<Boolean> segResolverEnabledProp = new SimpleDistributedProperty<>(
+        SEG_RESOLVER_ENABLED_PROP_NAME,
+        Boolean::parseBoolean
+    );
+
+    /** Ignite kernel context. */
+    private final GridKernalContext ctx;
+
+    /** Ignite logger. */
+    private final IgniteLogger log;
+
+    /** */
+    private final IgniteThreadPoolExecutor stateChangeExec;
+
+    /** */
+    private long lastCheckedTopVer;
+
+    /**  */
+    private volatile State state = State.VALID;
+
+    /** @param ctx Ignite kernel context. */
+    public IgnitePluggableSegmentationResolver(GridKernalContext ctx) {
+        this.ctx = ctx;
+
+        log = ctx.log(getClass());
+
+        stateChangeExec = new IgniteThreadPoolExecutor(
+            SEG_RESOLVER_THREAD_PREFIX,
+            ctx.igniteInstanceName(),
+            1,
+            1,
+            DFLT_THREAD_KEEP_ALIVE_TIME,
+            new LinkedBlockingQueue<>(),
+            UNDEFINED,
+            new OomExceptionHandler(ctx));
+
+        stateChangeExec.allowCoreThreadTimeOut(true);
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean isValidSegment() {
+        return isDisabled() || state != State.INVALID;
+    }
+
+    /** */
+    public void start() {
+        if (ctx.clientNode())
+            return;
+
+        ctx.addNodeAttribute(ATTR_SEG_RESOLVER_CONFIGURED, true);
+
+        ctx.event().addDiscoveryEventListener(new TopologyChangedEventListener(), TOP_CHANGED_EVTS);
+
+        ctx.discovery().setCustomEventListener(
+            ChangeGlobalStateFinishMessage.class,
+            new ClusterStateChangedEventListener()
+        );
+
+        ctx.internalSubscriptionProcessor().registerDistributedConfigurationListener(
+            new DistributedConfigurationLifecycleListener() {
+                /** {@inheritDoc} */
+                @Override public void onReadyToRegister(DistributedPropertyDispatcher dispatcher) {
+                    dispatcher.registerProperty(segResolverEnabledProp);
+                }
+
+                /** {@inheritDoc} */
+                @Override public void onReadyToWrite() {
+                    setDefaultValue(segResolverEnabledProp, U.isLocalNodeCoordinator(ctx.discovery()), log);
+                }
+            });
+    }
+
+    /** @return Discovery data. */
+    public Serializable provideDiscoveryData() {
+        return state;
+    }
+
+    /** @param data Discovery data. */
+    public void onDiscoveryDataReceived(Serializable data) {
+        state = (State)data;
+    }
+
+    /** @param node Node. */
+    public void validateNewNode(ClusterNode node) {
+        if (node.isClient())
+            return;
+
+        if (!TRUE.equals(node.attribute(ATTR_SEG_RESOLVER_CONFIGURED))) {
+            throw new IgniteException( "The Segmentation Resolver plugin is not configured for the server node that is" +
+                " trying to join the cluster. Since the Segmentation Resolver is only applicable if all server nodes" +
+                " in the cluster have one, node join request will be rejected [rejectedNodeId=" + node.id() + ']');
+        }
+
+        if (state == State.VALID) {
+            DiscoCache discoCache = ctx.discovery().discoCache(new AffinityTopologyVersion(lastCheckedTopVer, 0));
+
+            if (discoCache != null) {
+                for (ClusterNode srv : discoCache.serverNodes()) {
+                    if (!ctx.discovery().alive(srv))
+                        throw new IgniteException("Node join request will be rejected due to concurrent node left" +
+                            " process handling [rejectedNodeId=" + node.id() + ']');
+                }
+            }
+        }
+    }
+
+    /** */
+    private boolean isDisabled() {
+        Boolean res = segResolverEnabledProp.get();
+
+        return res == null || !res;
+    }
+
+    /** @return return. */
+    private String formatTopologyNodes(Collection<ClusterNode> nodes) {
+        return nodes.stream().map(n -> n.id().toString()).collect(Collectors.joining(", "));
+    }
+
+    /** */
+    private class TopologyChangedEventListener implements DiscoveryEventListener, HighPriorityListener {
+        /** {@inheritDoc} */
+        @Override public void onEvent(DiscoveryEvent evt, DiscoCache discoCache) {
+            lastCheckedTopVer = evt.topologyVersion();
+
+            if (isDisabled())
+                return;
+
+            if (state == State.VALID && evt.type() == EVT_NODE_FAILED) {
+                List<? extends BaselineNode> baselineNodes = discoCache.baselineNodes();
+
+                if (baselineNodes != null && aliveBaselineNodes(baselineNodes) < baselineNodes.size() / 2 + 1) {
+                    state = State.INVALID;
+
+                    stateChangeExec.execute(() -> {
+                        try {
+                            ctx.cluster().get().state(ACTIVE_READ_ONLY);
+                        }
+                        catch (Throwable e) {
+                            U.error(
+                                log,
+                                "Failed to automatically switch state of the segmented cluster to the READ-ONLY mode" +
+                                    " [segmentedNodes=" + formatTopologyNodes(discoCache.allNodes()) + "]. Cache writes" +
+                                    " are already restricted for all configured caches, but this step is still required" +
+                                    " in order to be able to unlock cache writes in the future. Retry this operation" +
+                                    " manually, if possible.",
+                                e
+                            );
+                        }
+                    });
+
+                    U.warn(log, "Cluster segmentation was detected [segmentedNodes=" +
+                        formatTopologyNodes(discoCache.allNodes()) + ']');
+                }
+            }
+
+            if (ctx.state().isBaselineAutoAdjustEnabled())
+                U.warn(log, "Segmentation Resolver requires baseline to be configured. If no baseline is" +
+                    " set, any topology change is considered valid.");
+        }
+
+        /** {@inheritDoc} */
+        @Override public int order() {
+            return 0;
+        }
+
+        /** */
+        private int aliveBaselineNodes(Collection<? extends BaselineNode> baselineNodes) {
+            int res = 0;
+
+            for (BaselineNode node : baselineNodes) {
+                if (!(node instanceof DetachedClusterNode))
+                    ++res;
+            }
+
+            return res;
+        }
+    }
+
+    /** */
+    private class ClusterStateChangedEventListener implements CustomEventListener<ChangeGlobalStateFinishMessage> {

Review comment:
       But in this case we will clutter up the `start` method and won't  gain any real profit.




-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [ignite] ololo3000 commented on a change in pull request #9534: wip

Posted by GitBox <gi...@apache.org>.
ololo3000 commented on a change in pull request #9534:
URL: https://github.com/apache/ignite/pull/9534#discussion_r741002238



##########
File path: modules/core/src/main/java/org/apache/ignite/cache/validation/IgnitePluggableSegmentationResolver.java
##########
@@ -0,0 +1,275 @@
+/*
+ * 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 org.apache.ignite.cache.validation;
+
+import java.io.Serializable;
+import java.util.Collection;
+import java.util.List;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.stream.Collectors;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.cluster.BaselineNode;
+import org.apache.ignite.cluster.ClusterNode;
+import org.apache.ignite.events.DiscoveryEvent;
+import org.apache.ignite.internal.GridKernalContext;
+import org.apache.ignite.internal.cluster.DetachedClusterNode;
+import org.apache.ignite.internal.managers.discovery.CustomEventListener;
+import org.apache.ignite.internal.managers.discovery.DiscoCache;
+import org.apache.ignite.internal.managers.eventstorage.DiscoveryEventListener;
+import org.apache.ignite.internal.managers.eventstorage.HighPriorityListener;
+import org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion;
+import org.apache.ignite.internal.processors.cluster.ChangeGlobalStateFinishMessage;
+import org.apache.ignite.internal.processors.configuration.distributed.DistributedConfigurationLifecycleListener;
+import org.apache.ignite.internal.processors.configuration.distributed.DistributedPropertyDispatcher;
+import org.apache.ignite.internal.processors.configuration.distributed.SimpleDistributedProperty;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.thread.IgniteThreadPoolExecutor;
+import org.apache.ignite.thread.OomExceptionHandler;
+
+import static java.lang.Boolean.TRUE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE_READ_ONLY;
+import static org.apache.ignite.configuration.IgniteConfiguration.DFLT_THREAD_KEEP_ALIVE_TIME;
+import static org.apache.ignite.events.EventType.EVT_NODE_FAILED;
+import static org.apache.ignite.events.EventType.EVT_NODE_JOINED;
+import static org.apache.ignite.events.EventType.EVT_NODE_LEFT;
+import static org.apache.ignite.internal.cluster.DistributedConfigurationUtils.setDefaultValue;
+import static org.apache.ignite.internal.managers.communication.GridIoPolicy.UNDEFINED;
+
+/** */
+public class IgnitePluggableSegmentationResolver implements PluggableSegmentationResolver {
+   /** */
+    public static final String ATTR_SEG_RESOLVER_CONFIGURED = "org.apache.ignite.segmentation.resolver.configured";
+
+    /** */
+    public static final String SEG_RESOLVER_ENABLED_PROP_NAME = "org.apache.ignite.segmentation.resolver.enabled";
+
+    /** */
+    private static final String SEG_RESOLVER_THREAD_PREFIX = "segmentation-resolver";
+
+    /** */
+    private static final int[] TOP_CHANGED_EVTS = new int[] {
+        EVT_NODE_LEFT,
+        EVT_NODE_JOINED,
+        EVT_NODE_FAILED
+    };
+
+    /** */
+    private final SimpleDistributedProperty<Boolean> segResolverEnabledProp = new SimpleDistributedProperty<>(
+        SEG_RESOLVER_ENABLED_PROP_NAME,
+        Boolean::parseBoolean
+    );
+
+    /** Ignite kernel context. */
+    private final GridKernalContext ctx;
+
+    /** Ignite logger. */
+    private final IgniteLogger log;
+
+    /** */
+    private final IgniteThreadPoolExecutor stateChangeExec;
+
+    /** */
+    private long lastCheckedTopVer;
+
+    /**  */
+    private volatile State state = State.VALID;
+
+    /** @param ctx Ignite kernel context. */
+    public IgnitePluggableSegmentationResolver(GridKernalContext ctx) {
+        this.ctx = ctx;
+
+        log = ctx.log(getClass());
+
+        stateChangeExec = new IgniteThreadPoolExecutor(
+            SEG_RESOLVER_THREAD_PREFIX,
+            ctx.igniteInstanceName(),
+            1,
+            1,
+            DFLT_THREAD_KEEP_ALIVE_TIME,
+            new LinkedBlockingQueue<>(),
+            UNDEFINED,
+            new OomExceptionHandler(ctx));
+
+        stateChangeExec.allowCoreThreadTimeOut(true);
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean isValidSegment() {
+        return isDisabled() || state != State.INVALID;
+    }
+
+    /** */
+    public void start() {
+        if (ctx.clientNode())
+            return;
+
+        ctx.addNodeAttribute(ATTR_SEG_RESOLVER_CONFIGURED, true);
+
+        ctx.event().addDiscoveryEventListener(new TopologyChangedEventListener(), TOP_CHANGED_EVTS);
+
+        ctx.discovery().setCustomEventListener(
+            ChangeGlobalStateFinishMessage.class,
+            new ClusterStateChangedEventListener()
+        );
+
+        ctx.internalSubscriptionProcessor().registerDistributedConfigurationListener(
+            new DistributedConfigurationLifecycleListener() {
+                /** {@inheritDoc} */
+                @Override public void onReadyToRegister(DistributedPropertyDispatcher dispatcher) {
+                    dispatcher.registerProperty(segResolverEnabledProp);
+                }
+
+                /** {@inheritDoc} */
+                @Override public void onReadyToWrite() {
+                    setDefaultValue(segResolverEnabledProp, U.isLocalNodeCoordinator(ctx.discovery()), log);
+                }
+            });
+    }
+
+    /** @return Discovery data. */
+    public Serializable provideDiscoveryData() {
+        return state;
+    }
+
+    /** @param data Discovery data. */
+    public void onDiscoveryDataReceived(Serializable data) {
+        state = (State)data;
+    }
+
+    /** @param node Node. */
+    public void validateNewNode(ClusterNode node) {
+        if (node.isClient())
+            return;
+
+        if (!TRUE.equals(node.attribute(ATTR_SEG_RESOLVER_CONFIGURED))) {
+            throw new IgniteException( "The Segmentation Resolver plugin is not configured for the server node that is" +
+                " trying to join the cluster. Since the Segmentation Resolver is only applicable if all server nodes" +
+                " in the cluster have one, node join request will be rejected [rejectedNodeId=" + node.id() + ']');
+        }
+
+        if (state == State.VALID) {
+            DiscoCache discoCache = ctx.discovery().discoCache(new AffinityTopologyVersion(lastCheckedTopVer, 0));
+
+            if (discoCache != null) {
+                for (ClusterNode srv : discoCache.serverNodes()) {
+                    if (!ctx.discovery().alive(srv))
+                        throw new IgniteException("Node join request will be rejected due to concurrent node left" +
+                            " process handling [rejectedNodeId=" + node.id() + ']');
+                }
+            }
+        }
+    }
+
+    /** */
+    private boolean isDisabled() {
+        Boolean res = segResolverEnabledProp.get();
+
+        return res == null || !res;
+    }
+
+    /** @return return. */
+    private String formatTopologyNodes(Collection<ClusterNode> nodes) {
+        return nodes.stream().map(n -> n.id().toString()).collect(Collectors.joining(", "));
+    }
+
+    /** */
+    private class TopologyChangedEventListener implements DiscoveryEventListener, HighPriorityListener {
+        /** {@inheritDoc} */
+        @Override public void onEvent(DiscoveryEvent evt, DiscoCache discoCache) {
+            lastCheckedTopVer = evt.topologyVersion();
+
+            if (isDisabled())
+                return;
+
+            if (state == State.VALID && evt.type() == EVT_NODE_FAILED) {
+                List<? extends BaselineNode> baselineNodes = discoCache.baselineNodes();
+
+                if (baselineNodes != null && aliveBaselineNodes(baselineNodes) < baselineNodes.size() / 2 + 1) {
+                    state = State.INVALID;
+
+                    stateChangeExec.execute(() -> {
+                        try {
+                            ctx.cluster().get().state(ACTIVE_READ_ONLY);
+                        }
+                        catch (Throwable e) {
+                            U.error(
+                                log,
+                                "Failed to automatically switch state of the segmented cluster to the READ-ONLY mode" +
+                                    " [segmentedNodes=" + formatTopologyNodes(discoCache.allNodes()) + "]. Cache writes" +
+                                    " are already restricted for all configured caches, but this step is still required" +
+                                    " in order to be able to unlock cache writes in the future. Retry this operation" +
+                                    " manually, if possible.",
+                                e
+                            );
+                        }
+                    });
+
+                    U.warn(log, "Cluster segmentation was detected [segmentedNodes=" +
+                        formatTopologyNodes(discoCache.allNodes()) + ']');
+                }
+            }
+
+            if (ctx.state().isBaselineAutoAdjustEnabled())

Review comment:
       .Done.

##########
File path: modules/core/src/main/java/org/apache/ignite/cache/validation/IgnitePluggableSegmentationResolver.java
##########
@@ -0,0 +1,275 @@
+/*
+ * 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 org.apache.ignite.cache.validation;
+
+import java.io.Serializable;
+import java.util.Collection;
+import java.util.List;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.stream.Collectors;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.cluster.BaselineNode;
+import org.apache.ignite.cluster.ClusterNode;
+import org.apache.ignite.events.DiscoveryEvent;
+import org.apache.ignite.internal.GridKernalContext;
+import org.apache.ignite.internal.cluster.DetachedClusterNode;
+import org.apache.ignite.internal.managers.discovery.CustomEventListener;
+import org.apache.ignite.internal.managers.discovery.DiscoCache;
+import org.apache.ignite.internal.managers.eventstorage.DiscoveryEventListener;
+import org.apache.ignite.internal.managers.eventstorage.HighPriorityListener;
+import org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion;
+import org.apache.ignite.internal.processors.cluster.ChangeGlobalStateFinishMessage;
+import org.apache.ignite.internal.processors.configuration.distributed.DistributedConfigurationLifecycleListener;
+import org.apache.ignite.internal.processors.configuration.distributed.DistributedPropertyDispatcher;
+import org.apache.ignite.internal.processors.configuration.distributed.SimpleDistributedProperty;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.thread.IgniteThreadPoolExecutor;
+import org.apache.ignite.thread.OomExceptionHandler;
+
+import static java.lang.Boolean.TRUE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE_READ_ONLY;
+import static org.apache.ignite.configuration.IgniteConfiguration.DFLT_THREAD_KEEP_ALIVE_TIME;
+import static org.apache.ignite.events.EventType.EVT_NODE_FAILED;
+import static org.apache.ignite.events.EventType.EVT_NODE_JOINED;
+import static org.apache.ignite.events.EventType.EVT_NODE_LEFT;
+import static org.apache.ignite.internal.cluster.DistributedConfigurationUtils.setDefaultValue;
+import static org.apache.ignite.internal.managers.communication.GridIoPolicy.UNDEFINED;
+
+/** */
+public class IgnitePluggableSegmentationResolver implements PluggableSegmentationResolver {
+   /** */
+    public static final String ATTR_SEG_RESOLVER_CONFIGURED = "org.apache.ignite.segmentation.resolver.configured";
+
+    /** */
+    public static final String SEG_RESOLVER_ENABLED_PROP_NAME = "org.apache.ignite.segmentation.resolver.enabled";
+
+    /** */
+    private static final String SEG_RESOLVER_THREAD_PREFIX = "segmentation-resolver";
+
+    /** */
+    private static final int[] TOP_CHANGED_EVTS = new int[] {
+        EVT_NODE_LEFT,
+        EVT_NODE_JOINED,
+        EVT_NODE_FAILED
+    };
+
+    /** */
+    private final SimpleDistributedProperty<Boolean> segResolverEnabledProp = new SimpleDistributedProperty<>(
+        SEG_RESOLVER_ENABLED_PROP_NAME,
+        Boolean::parseBoolean
+    );
+
+    /** Ignite kernel context. */
+    private final GridKernalContext ctx;
+
+    /** Ignite logger. */
+    private final IgniteLogger log;
+
+    /** */
+    private final IgniteThreadPoolExecutor stateChangeExec;
+
+    /** */
+    private long lastCheckedTopVer;
+
+    /**  */
+    private volatile State state = State.VALID;
+
+    /** @param ctx Ignite kernel context. */
+    public IgnitePluggableSegmentationResolver(GridKernalContext ctx) {
+        this.ctx = ctx;
+
+        log = ctx.log(getClass());
+
+        stateChangeExec = new IgniteThreadPoolExecutor(
+            SEG_RESOLVER_THREAD_PREFIX,
+            ctx.igniteInstanceName(),
+            1,
+            1,
+            DFLT_THREAD_KEEP_ALIVE_TIME,
+            new LinkedBlockingQueue<>(),
+            UNDEFINED,
+            new OomExceptionHandler(ctx));
+
+        stateChangeExec.allowCoreThreadTimeOut(true);
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean isValidSegment() {
+        return isDisabled() || state != State.INVALID;
+    }
+
+    /** */
+    public void start() {
+        if (ctx.clientNode())
+            return;
+
+        ctx.addNodeAttribute(ATTR_SEG_RESOLVER_CONFIGURED, true);
+
+        ctx.event().addDiscoveryEventListener(new TopologyChangedEventListener(), TOP_CHANGED_EVTS);
+
+        ctx.discovery().setCustomEventListener(
+            ChangeGlobalStateFinishMessage.class,
+            new ClusterStateChangedEventListener()
+        );
+
+        ctx.internalSubscriptionProcessor().registerDistributedConfigurationListener(
+            new DistributedConfigurationLifecycleListener() {
+                /** {@inheritDoc} */
+                @Override public void onReadyToRegister(DistributedPropertyDispatcher dispatcher) {
+                    dispatcher.registerProperty(segResolverEnabledProp);
+                }
+
+                /** {@inheritDoc} */
+                @Override public void onReadyToWrite() {
+                    setDefaultValue(segResolverEnabledProp, U.isLocalNodeCoordinator(ctx.discovery()), log);
+                }
+            });
+    }
+
+    /** @return Discovery data. */
+    public Serializable provideDiscoveryData() {
+        return state;
+    }
+
+    /** @param data Discovery data. */
+    public void onDiscoveryDataReceived(Serializable data) {
+        state = (State)data;
+    }
+
+    /** @param node Node. */
+    public void validateNewNode(ClusterNode node) {
+        if (node.isClient())
+            return;
+
+        if (!TRUE.equals(node.attribute(ATTR_SEG_RESOLVER_CONFIGURED))) {
+            throw new IgniteException( "The Segmentation Resolver plugin is not configured for the server node that is" +
+                " trying to join the cluster. Since the Segmentation Resolver is only applicable if all server nodes" +
+                " in the cluster have one, node join request will be rejected [rejectedNodeId=" + node.id() + ']');
+        }
+
+        if (state == State.VALID) {
+            DiscoCache discoCache = ctx.discovery().discoCache(new AffinityTopologyVersion(lastCheckedTopVer, 0));
+
+            if (discoCache != null) {
+                for (ClusterNode srv : discoCache.serverNodes()) {
+                    if (!ctx.discovery().alive(srv))
+                        throw new IgniteException("Node join request will be rejected due to concurrent node left" +
+                            " process handling [rejectedNodeId=" + node.id() + ']');
+                }
+            }
+        }
+    }
+
+    /** */
+    private boolean isDisabled() {
+        Boolean res = segResolverEnabledProp.get();
+
+        return res == null || !res;
+    }
+
+    /** @return return. */
+    private String formatTopologyNodes(Collection<ClusterNode> nodes) {

Review comment:
       Done.

##########
File path: modules/core/src/main/java/org/apache/ignite/cache/validation/IgnitePluggableSegmentationResolver.java
##########
@@ -0,0 +1,275 @@
+/*
+ * 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 org.apache.ignite.cache.validation;
+
+import java.io.Serializable;
+import java.util.Collection;
+import java.util.List;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.stream.Collectors;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.cluster.BaselineNode;
+import org.apache.ignite.cluster.ClusterNode;
+import org.apache.ignite.events.DiscoveryEvent;
+import org.apache.ignite.internal.GridKernalContext;
+import org.apache.ignite.internal.cluster.DetachedClusterNode;
+import org.apache.ignite.internal.managers.discovery.CustomEventListener;
+import org.apache.ignite.internal.managers.discovery.DiscoCache;
+import org.apache.ignite.internal.managers.eventstorage.DiscoveryEventListener;
+import org.apache.ignite.internal.managers.eventstorage.HighPriorityListener;
+import org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion;
+import org.apache.ignite.internal.processors.cluster.ChangeGlobalStateFinishMessage;
+import org.apache.ignite.internal.processors.configuration.distributed.DistributedConfigurationLifecycleListener;
+import org.apache.ignite.internal.processors.configuration.distributed.DistributedPropertyDispatcher;
+import org.apache.ignite.internal.processors.configuration.distributed.SimpleDistributedProperty;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.thread.IgniteThreadPoolExecutor;
+import org.apache.ignite.thread.OomExceptionHandler;
+
+import static java.lang.Boolean.TRUE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE_READ_ONLY;
+import static org.apache.ignite.configuration.IgniteConfiguration.DFLT_THREAD_KEEP_ALIVE_TIME;
+import static org.apache.ignite.events.EventType.EVT_NODE_FAILED;
+import static org.apache.ignite.events.EventType.EVT_NODE_JOINED;
+import static org.apache.ignite.events.EventType.EVT_NODE_LEFT;
+import static org.apache.ignite.internal.cluster.DistributedConfigurationUtils.setDefaultValue;
+import static org.apache.ignite.internal.managers.communication.GridIoPolicy.UNDEFINED;
+
+/** */
+public class IgnitePluggableSegmentationResolver implements PluggableSegmentationResolver {
+   /** */
+    public static final String ATTR_SEG_RESOLVER_CONFIGURED = "org.apache.ignite.segmentation.resolver.configured";
+
+    /** */
+    public static final String SEG_RESOLVER_ENABLED_PROP_NAME = "org.apache.ignite.segmentation.resolver.enabled";
+
+    /** */
+    private static final String SEG_RESOLVER_THREAD_PREFIX = "segmentation-resolver";
+
+    /** */
+    private static final int[] TOP_CHANGED_EVTS = new int[] {
+        EVT_NODE_LEFT,
+        EVT_NODE_JOINED,
+        EVT_NODE_FAILED
+    };
+
+    /** */
+    private final SimpleDistributedProperty<Boolean> segResolverEnabledProp = new SimpleDistributedProperty<>(
+        SEG_RESOLVER_ENABLED_PROP_NAME,
+        Boolean::parseBoolean
+    );
+
+    /** Ignite kernel context. */
+    private final GridKernalContext ctx;
+
+    /** Ignite logger. */
+    private final IgniteLogger log;
+
+    /** */
+    private final IgniteThreadPoolExecutor stateChangeExec;
+
+    /** */
+    private long lastCheckedTopVer;
+
+    /**  */
+    private volatile State state = State.VALID;
+
+    /** @param ctx Ignite kernel context. */
+    public IgnitePluggableSegmentationResolver(GridKernalContext ctx) {
+        this.ctx = ctx;
+
+        log = ctx.log(getClass());
+
+        stateChangeExec = new IgniteThreadPoolExecutor(
+            SEG_RESOLVER_THREAD_PREFIX,
+            ctx.igniteInstanceName(),
+            1,
+            1,
+            DFLT_THREAD_KEEP_ALIVE_TIME,
+            new LinkedBlockingQueue<>(),
+            UNDEFINED,
+            new OomExceptionHandler(ctx));
+
+        stateChangeExec.allowCoreThreadTimeOut(true);
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean isValidSegment() {
+        return isDisabled() || state != State.INVALID;
+    }
+
+    /** */
+    public void start() {
+        if (ctx.clientNode())
+            return;
+
+        ctx.addNodeAttribute(ATTR_SEG_RESOLVER_CONFIGURED, true);
+
+        ctx.event().addDiscoveryEventListener(new TopologyChangedEventListener(), TOP_CHANGED_EVTS);
+
+        ctx.discovery().setCustomEventListener(
+            ChangeGlobalStateFinishMessage.class,
+            new ClusterStateChangedEventListener()
+        );
+
+        ctx.internalSubscriptionProcessor().registerDistributedConfigurationListener(
+            new DistributedConfigurationLifecycleListener() {
+                /** {@inheritDoc} */
+                @Override public void onReadyToRegister(DistributedPropertyDispatcher dispatcher) {
+                    dispatcher.registerProperty(segResolverEnabledProp);
+                }
+
+                /** {@inheritDoc} */
+                @Override public void onReadyToWrite() {
+                    setDefaultValue(segResolverEnabledProp, U.isLocalNodeCoordinator(ctx.discovery()), log);
+                }
+            });
+    }
+
+    /** @return Discovery data. */
+    public Serializable provideDiscoveryData() {
+        return state;
+    }
+
+    /** @param data Discovery data. */
+    public void onDiscoveryDataReceived(Serializable data) {
+        state = (State)data;
+    }
+
+    /** @param node Node. */
+    public void validateNewNode(ClusterNode node) {
+        if (node.isClient())
+            return;
+
+        if (!TRUE.equals(node.attribute(ATTR_SEG_RESOLVER_CONFIGURED))) {
+            throw new IgniteException( "The Segmentation Resolver plugin is not configured for the server node that is" +
+                " trying to join the cluster. Since the Segmentation Resolver is only applicable if all server nodes" +
+                " in the cluster have one, node join request will be rejected [rejectedNodeId=" + node.id() + ']');
+        }
+
+        if (state == State.VALID) {
+            DiscoCache discoCache = ctx.discovery().discoCache(new AffinityTopologyVersion(lastCheckedTopVer, 0));
+
+            if (discoCache != null) {
+                for (ClusterNode srv : discoCache.serverNodes()) {
+                    if (!ctx.discovery().alive(srv))
+                        throw new IgniteException("Node join request will be rejected due to concurrent node left" +
+                            " process handling [rejectedNodeId=" + node.id() + ']');
+                }
+            }
+        }
+    }
+
+    /** */
+    private boolean isDisabled() {
+        Boolean res = segResolverEnabledProp.get();
+
+        return res == null || !res;
+    }
+
+    /** @return return. */
+    private String formatTopologyNodes(Collection<ClusterNode> nodes) {
+        return nodes.stream().map(n -> n.id().toString()).collect(Collectors.joining(", "));
+    }
+
+    /** */
+    private class TopologyChangedEventListener implements DiscoveryEventListener, HighPriorityListener {
+        /** {@inheritDoc} */
+        @Override public void onEvent(DiscoveryEvent evt, DiscoCache discoCache) {
+            lastCheckedTopVer = evt.topologyVersion();
+
+            if (isDisabled())
+                return;
+
+            if (state == State.VALID && evt.type() == EVT_NODE_FAILED) {

Review comment:
       Done.

##########
File path: modules/core/src/main/java/org/apache/ignite/cache/validation/IgnitePluggableSegmentationResolver.java
##########
@@ -0,0 +1,275 @@
+/*
+ * 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 org.apache.ignite.cache.validation;
+
+import java.io.Serializable;
+import java.util.Collection;
+import java.util.List;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.stream.Collectors;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.cluster.BaselineNode;
+import org.apache.ignite.cluster.ClusterNode;
+import org.apache.ignite.events.DiscoveryEvent;
+import org.apache.ignite.internal.GridKernalContext;
+import org.apache.ignite.internal.cluster.DetachedClusterNode;
+import org.apache.ignite.internal.managers.discovery.CustomEventListener;
+import org.apache.ignite.internal.managers.discovery.DiscoCache;
+import org.apache.ignite.internal.managers.eventstorage.DiscoveryEventListener;
+import org.apache.ignite.internal.managers.eventstorage.HighPriorityListener;
+import org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion;
+import org.apache.ignite.internal.processors.cluster.ChangeGlobalStateFinishMessage;
+import org.apache.ignite.internal.processors.configuration.distributed.DistributedConfigurationLifecycleListener;
+import org.apache.ignite.internal.processors.configuration.distributed.DistributedPropertyDispatcher;
+import org.apache.ignite.internal.processors.configuration.distributed.SimpleDistributedProperty;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.thread.IgniteThreadPoolExecutor;
+import org.apache.ignite.thread.OomExceptionHandler;
+
+import static java.lang.Boolean.TRUE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE_READ_ONLY;
+import static org.apache.ignite.configuration.IgniteConfiguration.DFLT_THREAD_KEEP_ALIVE_TIME;
+import static org.apache.ignite.events.EventType.EVT_NODE_FAILED;
+import static org.apache.ignite.events.EventType.EVT_NODE_JOINED;
+import static org.apache.ignite.events.EventType.EVT_NODE_LEFT;
+import static org.apache.ignite.internal.cluster.DistributedConfigurationUtils.setDefaultValue;
+import static org.apache.ignite.internal.managers.communication.GridIoPolicy.UNDEFINED;
+
+/** */
+public class IgnitePluggableSegmentationResolver implements PluggableSegmentationResolver {
+   /** */
+    public static final String ATTR_SEG_RESOLVER_CONFIGURED = "org.apache.ignite.segmentation.resolver.configured";
+
+    /** */
+    public static final String SEG_RESOLVER_ENABLED_PROP_NAME = "org.apache.ignite.segmentation.resolver.enabled";
+
+    /** */
+    private static final String SEG_RESOLVER_THREAD_PREFIX = "segmentation-resolver";
+
+    /** */
+    private static final int[] TOP_CHANGED_EVTS = new int[] {
+        EVT_NODE_LEFT,
+        EVT_NODE_JOINED,
+        EVT_NODE_FAILED
+    };
+
+    /** */
+    private final SimpleDistributedProperty<Boolean> segResolverEnabledProp = new SimpleDistributedProperty<>(
+        SEG_RESOLVER_ENABLED_PROP_NAME,
+        Boolean::parseBoolean
+    );
+
+    /** Ignite kernel context. */
+    private final GridKernalContext ctx;
+
+    /** Ignite logger. */
+    private final IgniteLogger log;
+
+    /** */
+    private final IgniteThreadPoolExecutor stateChangeExec;
+
+    /** */
+    private long lastCheckedTopVer;
+
+    /**  */
+    private volatile State state = State.VALID;
+
+    /** @param ctx Ignite kernel context. */
+    public IgnitePluggableSegmentationResolver(GridKernalContext ctx) {
+        this.ctx = ctx;
+
+        log = ctx.log(getClass());
+
+        stateChangeExec = new IgniteThreadPoolExecutor(
+            SEG_RESOLVER_THREAD_PREFIX,
+            ctx.igniteInstanceName(),
+            1,
+            1,
+            DFLT_THREAD_KEEP_ALIVE_TIME,
+            new LinkedBlockingQueue<>(),
+            UNDEFINED,
+            new OomExceptionHandler(ctx));
+
+        stateChangeExec.allowCoreThreadTimeOut(true);
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean isValidSegment() {
+        return isDisabled() || state != State.INVALID;
+    }
+
+    /** */
+    public void start() {
+        if (ctx.clientNode())
+            return;
+
+        ctx.addNodeAttribute(ATTR_SEG_RESOLVER_CONFIGURED, true);
+
+        ctx.event().addDiscoveryEventListener(new TopologyChangedEventListener(), TOP_CHANGED_EVTS);
+
+        ctx.discovery().setCustomEventListener(
+            ChangeGlobalStateFinishMessage.class,
+            new ClusterStateChangedEventListener()
+        );
+
+        ctx.internalSubscriptionProcessor().registerDistributedConfigurationListener(
+            new DistributedConfigurationLifecycleListener() {
+                /** {@inheritDoc} */
+                @Override public void onReadyToRegister(DistributedPropertyDispatcher dispatcher) {
+                    dispatcher.registerProperty(segResolverEnabledProp);
+                }
+
+                /** {@inheritDoc} */
+                @Override public void onReadyToWrite() {
+                    setDefaultValue(segResolverEnabledProp, U.isLocalNodeCoordinator(ctx.discovery()), log);
+                }
+            });
+    }
+
+    /** @return Discovery data. */
+    public Serializable provideDiscoveryData() {
+        return state;
+    }
+
+    /** @param data Discovery data. */
+    public void onDiscoveryDataReceived(Serializable data) {
+        state = (State)data;
+    }
+
+    /** @param node Node. */
+    public void validateNewNode(ClusterNode node) {
+        if (node.isClient())
+            return;
+
+        if (!TRUE.equals(node.attribute(ATTR_SEG_RESOLVER_CONFIGURED))) {
+            throw new IgniteException( "The Segmentation Resolver plugin is not configured for the server node that is" +
+                " trying to join the cluster. Since the Segmentation Resolver is only applicable if all server nodes" +
+                " in the cluster have one, node join request will be rejected [rejectedNodeId=" + node.id() + ']');
+        }
+
+        if (state == State.VALID) {
+            DiscoCache discoCache = ctx.discovery().discoCache(new AffinityTopologyVersion(lastCheckedTopVer, 0));
+
+            if (discoCache != null) {
+                for (ClusterNode srv : discoCache.serverNodes()) {
+                    if (!ctx.discovery().alive(srv))
+                        throw new IgniteException("Node join request will be rejected due to concurrent node left" +
+                            " process handling [rejectedNodeId=" + node.id() + ']');
+                }
+            }
+        }
+    }
+
+    /** */
+    private boolean isDisabled() {
+        Boolean res = segResolverEnabledProp.get();
+
+        return res == null || !res;
+    }
+
+    /** @return return. */
+    private String formatTopologyNodes(Collection<ClusterNode> nodes) {
+        return nodes.stream().map(n -> n.id().toString()).collect(Collectors.joining(", "));
+    }
+
+    /** */
+    private class TopologyChangedEventListener implements DiscoveryEventListener, HighPriorityListener {
+        /** {@inheritDoc} */
+        @Override public void onEvent(DiscoveryEvent evt, DiscoCache discoCache) {
+            lastCheckedTopVer = evt.topologyVersion();
+
+            if (isDisabled())
+                return;
+
+            if (state == State.VALID && evt.type() == EVT_NODE_FAILED) {
+                List<? extends BaselineNode> baselineNodes = discoCache.baselineNodes();
+
+                if (baselineNodes != null && aliveBaselineNodes(baselineNodes) < baselineNodes.size() / 2 + 1) {
+                    state = State.INVALID;
+
+                    stateChangeExec.execute(() -> {
+                        try {
+                            ctx.cluster().get().state(ACTIVE_READ_ONLY);
+                        }
+                        catch (Throwable e) {
+                            U.error(
+                                log,
+                                "Failed to automatically switch state of the segmented cluster to the READ-ONLY mode" +
+                                    " [segmentedNodes=" + formatTopologyNodes(discoCache.allNodes()) + "]. Cache writes" +
+                                    " are already restricted for all configured caches, but this step is still required" +
+                                    " in order to be able to unlock cache writes in the future. Retry this operation" +
+                                    " manually, if possible.",
+                                e
+                            );
+                        }
+                    });
+
+                    U.warn(log, "Cluster segmentation was detected [segmentedNodes=" +
+                        formatTopologyNodes(discoCache.allNodes()) + ']');
+                }
+            }
+
+            if (ctx.state().isBaselineAutoAdjustEnabled())
+                U.warn(log, "Segmentation Resolver requires baseline to be configured. If no baseline is" +
+                    " set, any topology change is considered valid.");
+        }
+
+        /** {@inheritDoc} */
+        @Override public int order() {
+            return 0;
+        }
+
+        /** */
+        private int aliveBaselineNodes(Collection<? extends BaselineNode> baselineNodes) {
+            int res = 0;
+
+            for (BaselineNode node : baselineNodes) {
+                if (!(node instanceof DetachedClusterNode))
+                    ++res;
+            }
+
+            return res;
+        }
+    }
+
+    /** */
+    private class ClusterStateChangedEventListener implements CustomEventListener<ChangeGlobalStateFinishMessage> {

Review comment:
       But in this case we will crumble up the `start` method and won't  gain any real profit.

##########
File path: modules/core/src/main/java/org/apache/ignite/cache/validation/IgnitePluggableSegmentationResolver.java
##########
@@ -0,0 +1,275 @@
+/*
+ * 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 org.apache.ignite.cache.validation;
+
+import java.io.Serializable;
+import java.util.Collection;
+import java.util.List;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.stream.Collectors;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.cluster.BaselineNode;
+import org.apache.ignite.cluster.ClusterNode;
+import org.apache.ignite.events.DiscoveryEvent;
+import org.apache.ignite.internal.GridKernalContext;
+import org.apache.ignite.internal.cluster.DetachedClusterNode;
+import org.apache.ignite.internal.managers.discovery.CustomEventListener;
+import org.apache.ignite.internal.managers.discovery.DiscoCache;
+import org.apache.ignite.internal.managers.eventstorage.DiscoveryEventListener;
+import org.apache.ignite.internal.managers.eventstorage.HighPriorityListener;
+import org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion;
+import org.apache.ignite.internal.processors.cluster.ChangeGlobalStateFinishMessage;
+import org.apache.ignite.internal.processors.configuration.distributed.DistributedConfigurationLifecycleListener;
+import org.apache.ignite.internal.processors.configuration.distributed.DistributedPropertyDispatcher;
+import org.apache.ignite.internal.processors.configuration.distributed.SimpleDistributedProperty;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.thread.IgniteThreadPoolExecutor;
+import org.apache.ignite.thread.OomExceptionHandler;
+
+import static java.lang.Boolean.TRUE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE_READ_ONLY;
+import static org.apache.ignite.configuration.IgniteConfiguration.DFLT_THREAD_KEEP_ALIVE_TIME;
+import static org.apache.ignite.events.EventType.EVT_NODE_FAILED;
+import static org.apache.ignite.events.EventType.EVT_NODE_JOINED;
+import static org.apache.ignite.events.EventType.EVT_NODE_LEFT;
+import static org.apache.ignite.internal.cluster.DistributedConfigurationUtils.setDefaultValue;
+import static org.apache.ignite.internal.managers.communication.GridIoPolicy.UNDEFINED;
+
+/** */
+public class IgnitePluggableSegmentationResolver implements PluggableSegmentationResolver {
+   /** */
+    public static final String ATTR_SEG_RESOLVER_CONFIGURED = "org.apache.ignite.segmentation.resolver.configured";
+
+    /** */
+    public static final String SEG_RESOLVER_ENABLED_PROP_NAME = "org.apache.ignite.segmentation.resolver.enabled";
+
+    /** */
+    private static final String SEG_RESOLVER_THREAD_PREFIX = "segmentation-resolver";
+
+    /** */
+    private static final int[] TOP_CHANGED_EVTS = new int[] {
+        EVT_NODE_LEFT,
+        EVT_NODE_JOINED,
+        EVT_NODE_FAILED
+    };
+
+    /** */
+    private final SimpleDistributedProperty<Boolean> segResolverEnabledProp = new SimpleDistributedProperty<>(
+        SEG_RESOLVER_ENABLED_PROP_NAME,
+        Boolean::parseBoolean
+    );
+
+    /** Ignite kernel context. */
+    private final GridKernalContext ctx;
+
+    /** Ignite logger. */
+    private final IgniteLogger log;
+
+    /** */
+    private final IgniteThreadPoolExecutor stateChangeExec;
+
+    /** */
+    private long lastCheckedTopVer;
+
+    /**  */
+    private volatile State state = State.VALID;
+
+    /** @param ctx Ignite kernel context. */
+    public IgnitePluggableSegmentationResolver(GridKernalContext ctx) {
+        this.ctx = ctx;
+
+        log = ctx.log(getClass());
+
+        stateChangeExec = new IgniteThreadPoolExecutor(
+            SEG_RESOLVER_THREAD_PREFIX,
+            ctx.igniteInstanceName(),
+            1,
+            1,
+            DFLT_THREAD_KEEP_ALIVE_TIME,
+            new LinkedBlockingQueue<>(),
+            UNDEFINED,
+            new OomExceptionHandler(ctx));
+
+        stateChangeExec.allowCoreThreadTimeOut(true);
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean isValidSegment() {
+        return isDisabled() || state != State.INVALID;
+    }
+
+    /** */
+    public void start() {
+        if (ctx.clientNode())
+            return;
+
+        ctx.addNodeAttribute(ATTR_SEG_RESOLVER_CONFIGURED, true);
+
+        ctx.event().addDiscoveryEventListener(new TopologyChangedEventListener(), TOP_CHANGED_EVTS);
+
+        ctx.discovery().setCustomEventListener(
+            ChangeGlobalStateFinishMessage.class,
+            new ClusterStateChangedEventListener()
+        );
+
+        ctx.internalSubscriptionProcessor().registerDistributedConfigurationListener(
+            new DistributedConfigurationLifecycleListener() {
+                /** {@inheritDoc} */
+                @Override public void onReadyToRegister(DistributedPropertyDispatcher dispatcher) {
+                    dispatcher.registerProperty(segResolverEnabledProp);
+                }
+
+                /** {@inheritDoc} */
+                @Override public void onReadyToWrite() {
+                    setDefaultValue(segResolverEnabledProp, U.isLocalNodeCoordinator(ctx.discovery()), log);
+                }
+            });
+    }
+
+    /** @return Discovery data. */
+    public Serializable provideDiscoveryData() {
+        return state;
+    }
+
+    /** @param data Discovery data. */
+    public void onDiscoveryDataReceived(Serializable data) {
+        state = (State)data;
+    }
+
+    /** @param node Node. */
+    public void validateNewNode(ClusterNode node) {
+        if (node.isClient())
+            return;
+
+        if (!TRUE.equals(node.attribute(ATTR_SEG_RESOLVER_CONFIGURED))) {
+            throw new IgniteException( "The Segmentation Resolver plugin is not configured for the server node that is" +
+                " trying to join the cluster. Since the Segmentation Resolver is only applicable if all server nodes" +
+                " in the cluster have one, node join request will be rejected [rejectedNodeId=" + node.id() + ']');
+        }
+
+        if (state == State.VALID) {
+            DiscoCache discoCache = ctx.discovery().discoCache(new AffinityTopologyVersion(lastCheckedTopVer, 0));
+
+            if (discoCache != null) {
+                for (ClusterNode srv : discoCache.serverNodes()) {
+                    if (!ctx.discovery().alive(srv))
+                        throw new IgniteException("Node join request will be rejected due to concurrent node left" +
+                            " process handling [rejectedNodeId=" + node.id() + ']');
+                }
+            }
+        }
+    }
+
+    /** */
+    private boolean isDisabled() {
+        Boolean res = segResolverEnabledProp.get();
+
+        return res == null || !res;
+    }
+
+    /** @return return. */
+    private String formatTopologyNodes(Collection<ClusterNode> nodes) {
+        return nodes.stream().map(n -> n.id().toString()).collect(Collectors.joining(", "));
+    }
+
+    /** */
+    private class TopologyChangedEventListener implements DiscoveryEventListener, HighPriorityListener {
+        /** {@inheritDoc} */
+        @Override public void onEvent(DiscoveryEvent evt, DiscoCache discoCache) {
+            lastCheckedTopVer = evt.topologyVersion();
+
+            if (isDisabled())
+                return;
+
+            if (state == State.VALID && evt.type() == EVT_NODE_FAILED) {
+                List<? extends BaselineNode> baselineNodes = discoCache.baselineNodes();
+
+                if (baselineNodes != null && aliveBaselineNodes(baselineNodes) < baselineNodes.size() / 2 + 1) {
+                    state = State.INVALID;
+
+                    stateChangeExec.execute(() -> {
+                        try {
+                            ctx.cluster().get().state(ACTIVE_READ_ONLY);
+                        }
+                        catch (Throwable e) {
+                            U.error(
+                                log,
+                                "Failed to automatically switch state of the segmented cluster to the READ-ONLY mode" +
+                                    " [segmentedNodes=" + formatTopologyNodes(discoCache.allNodes()) + "]. Cache writes" +
+                                    " are already restricted for all configured caches, but this step is still required" +
+                                    " in order to be able to unlock cache writes in the future. Retry this operation" +
+                                    " manually, if possible.",
+                                e
+                            );
+                        }
+                    });
+
+                    U.warn(log, "Cluster segmentation was detected [segmentedNodes=" +
+                        formatTopologyNodes(discoCache.allNodes()) + ']');
+                }
+            }
+
+            if (ctx.state().isBaselineAutoAdjustEnabled())
+                U.warn(log, "Segmentation Resolver requires baseline to be configured. If no baseline is" +
+                    " set, any topology change is considered valid.");
+        }
+
+        /** {@inheritDoc} */
+        @Override public int order() {
+            return 0;
+        }
+
+        /** */
+        private int aliveBaselineNodes(Collection<? extends BaselineNode> baselineNodes) {
+            int res = 0;
+
+            for (BaselineNode node : baselineNodes) {
+                if (!(node instanceof DetachedClusterNode))
+                    ++res;
+            }
+
+            return res;
+        }
+    }
+
+    /** */
+    private class ClusterStateChangedEventListener implements CustomEventListener<ChangeGlobalStateFinishMessage> {

Review comment:
       But in this case we will clutter up the `start` method and won't  gain any real profit.




-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [ignite] xtern commented on a change in pull request #9534: wip

Posted by GitBox <gi...@apache.org>.
xtern commented on a change in pull request #9534:
URL: https://github.com/apache/ignite/pull/9534#discussion_r740904230



##########
File path: modules/core/src/main/java/org/apache/ignite/cache/validation/IgnitePluggableSegmentationResolver.java
##########
@@ -0,0 +1,275 @@
+/*
+ * 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 org.apache.ignite.cache.validation;
+
+import java.io.Serializable;
+import java.util.Collection;
+import java.util.List;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.stream.Collectors;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.cluster.BaselineNode;
+import org.apache.ignite.cluster.ClusterNode;
+import org.apache.ignite.events.DiscoveryEvent;
+import org.apache.ignite.internal.GridKernalContext;
+import org.apache.ignite.internal.cluster.DetachedClusterNode;
+import org.apache.ignite.internal.managers.discovery.CustomEventListener;
+import org.apache.ignite.internal.managers.discovery.DiscoCache;
+import org.apache.ignite.internal.managers.eventstorage.DiscoveryEventListener;
+import org.apache.ignite.internal.managers.eventstorage.HighPriorityListener;
+import org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion;
+import org.apache.ignite.internal.processors.cluster.ChangeGlobalStateFinishMessage;
+import org.apache.ignite.internal.processors.configuration.distributed.DistributedConfigurationLifecycleListener;
+import org.apache.ignite.internal.processors.configuration.distributed.DistributedPropertyDispatcher;
+import org.apache.ignite.internal.processors.configuration.distributed.SimpleDistributedProperty;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.thread.IgniteThreadPoolExecutor;
+import org.apache.ignite.thread.OomExceptionHandler;
+
+import static java.lang.Boolean.TRUE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE_READ_ONLY;
+import static org.apache.ignite.configuration.IgniteConfiguration.DFLT_THREAD_KEEP_ALIVE_TIME;
+import static org.apache.ignite.events.EventType.EVT_NODE_FAILED;
+import static org.apache.ignite.events.EventType.EVT_NODE_JOINED;
+import static org.apache.ignite.events.EventType.EVT_NODE_LEFT;
+import static org.apache.ignite.internal.cluster.DistributedConfigurationUtils.setDefaultValue;
+import static org.apache.ignite.internal.managers.communication.GridIoPolicy.UNDEFINED;
+
+/** */
+public class IgnitePluggableSegmentationResolver implements PluggableSegmentationResolver {
+   /** */
+    public static final String ATTR_SEG_RESOLVER_CONFIGURED = "org.apache.ignite.segmentation.resolver.configured";
+
+    /** */
+    public static final String SEG_RESOLVER_ENABLED_PROP_NAME = "org.apache.ignite.segmentation.resolver.enabled";
+
+    /** */
+    private static final String SEG_RESOLVER_THREAD_PREFIX = "segmentation-resolver";
+
+    /** */
+    private static final int[] TOP_CHANGED_EVTS = new int[] {
+        EVT_NODE_LEFT,
+        EVT_NODE_JOINED,
+        EVT_NODE_FAILED
+    };
+
+    /** */
+    private final SimpleDistributedProperty<Boolean> segResolverEnabledProp = new SimpleDistributedProperty<>(
+        SEG_RESOLVER_ENABLED_PROP_NAME,
+        Boolean::parseBoolean
+    );
+
+    /** Ignite kernel context. */
+    private final GridKernalContext ctx;
+
+    /** Ignite logger. */
+    private final IgniteLogger log;
+
+    /** */
+    private final IgniteThreadPoolExecutor stateChangeExec;
+
+    /** */
+    private long lastCheckedTopVer;
+
+    /**  */
+    private volatile State state = State.VALID;
+
+    /** @param ctx Ignite kernel context. */
+    public IgnitePluggableSegmentationResolver(GridKernalContext ctx) {
+        this.ctx = ctx;
+
+        log = ctx.log(getClass());
+
+        stateChangeExec = new IgniteThreadPoolExecutor(
+            SEG_RESOLVER_THREAD_PREFIX,
+            ctx.igniteInstanceName(),
+            1,
+            1,
+            DFLT_THREAD_KEEP_ALIVE_TIME,
+            new LinkedBlockingQueue<>(),
+            UNDEFINED,
+            new OomExceptionHandler(ctx));
+
+        stateChangeExec.allowCoreThreadTimeOut(true);
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean isValidSegment() {
+        return isDisabled() || state != State.INVALID;
+    }
+
+    /** */
+    public void start() {
+        if (ctx.clientNode())
+            return;
+
+        ctx.addNodeAttribute(ATTR_SEG_RESOLVER_CONFIGURED, true);
+
+        ctx.event().addDiscoveryEventListener(new TopologyChangedEventListener(), TOP_CHANGED_EVTS);
+
+        ctx.discovery().setCustomEventListener(
+            ChangeGlobalStateFinishMessage.class,
+            new ClusterStateChangedEventListener()
+        );
+
+        ctx.internalSubscriptionProcessor().registerDistributedConfigurationListener(
+            new DistributedConfigurationLifecycleListener() {
+                /** {@inheritDoc} */
+                @Override public void onReadyToRegister(DistributedPropertyDispatcher dispatcher) {
+                    dispatcher.registerProperty(segResolverEnabledProp);
+                }
+
+                /** {@inheritDoc} */
+                @Override public void onReadyToWrite() {
+                    setDefaultValue(segResolverEnabledProp, U.isLocalNodeCoordinator(ctx.discovery()), log);
+                }
+            });
+    }
+
+    /** @return Discovery data. */
+    public Serializable provideDiscoveryData() {
+        return state;
+    }
+
+    /** @param data Discovery data. */
+    public void onDiscoveryDataReceived(Serializable data) {
+        state = (State)data;
+    }
+
+    /** @param node Node. */
+    public void validateNewNode(ClusterNode node) {
+        if (node.isClient())
+            return;
+
+        if (!TRUE.equals(node.attribute(ATTR_SEG_RESOLVER_CONFIGURED))) {
+            throw new IgniteException( "The Segmentation Resolver plugin is not configured for the server node that is" +
+                " trying to join the cluster. Since the Segmentation Resolver is only applicable if all server nodes" +
+                " in the cluster have one, node join request will be rejected [rejectedNodeId=" + node.id() + ']');
+        }
+
+        if (state == State.VALID) {
+            DiscoCache discoCache = ctx.discovery().discoCache(new AffinityTopologyVersion(lastCheckedTopVer, 0));
+
+            if (discoCache != null) {
+                for (ClusterNode srv : discoCache.serverNodes()) {
+                    if (!ctx.discovery().alive(srv))
+                        throw new IgniteException("Node join request will be rejected due to concurrent node left" +
+                            " process handling [rejectedNodeId=" + node.id() + ']');
+                }
+            }
+        }
+    }
+
+    /** */
+    private boolean isDisabled() {
+        Boolean res = segResolverEnabledProp.get();
+
+        return res == null || !res;
+    }
+
+    /** @return return. */
+    private String formatTopologyNodes(Collection<ClusterNode> nodes) {

Review comment:
       I suggest moving this method inside the `TopologyChangedEventListener` class.

##########
File path: modules/core/src/main/java/org/apache/ignite/cache/validation/IgnitePluggableSegmentationResolver.java
##########
@@ -0,0 +1,275 @@
+/*
+ * 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 org.apache.ignite.cache.validation;
+
+import java.io.Serializable;
+import java.util.Collection;
+import java.util.List;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.stream.Collectors;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.cluster.BaselineNode;
+import org.apache.ignite.cluster.ClusterNode;
+import org.apache.ignite.events.DiscoveryEvent;
+import org.apache.ignite.internal.GridKernalContext;
+import org.apache.ignite.internal.cluster.DetachedClusterNode;
+import org.apache.ignite.internal.managers.discovery.CustomEventListener;
+import org.apache.ignite.internal.managers.discovery.DiscoCache;
+import org.apache.ignite.internal.managers.eventstorage.DiscoveryEventListener;
+import org.apache.ignite.internal.managers.eventstorage.HighPriorityListener;
+import org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion;
+import org.apache.ignite.internal.processors.cluster.ChangeGlobalStateFinishMessage;
+import org.apache.ignite.internal.processors.configuration.distributed.DistributedConfigurationLifecycleListener;
+import org.apache.ignite.internal.processors.configuration.distributed.DistributedPropertyDispatcher;
+import org.apache.ignite.internal.processors.configuration.distributed.SimpleDistributedProperty;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.thread.IgniteThreadPoolExecutor;
+import org.apache.ignite.thread.OomExceptionHandler;
+
+import static java.lang.Boolean.TRUE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE_READ_ONLY;
+import static org.apache.ignite.configuration.IgniteConfiguration.DFLT_THREAD_KEEP_ALIVE_TIME;
+import static org.apache.ignite.events.EventType.EVT_NODE_FAILED;
+import static org.apache.ignite.events.EventType.EVT_NODE_JOINED;
+import static org.apache.ignite.events.EventType.EVT_NODE_LEFT;
+import static org.apache.ignite.internal.cluster.DistributedConfigurationUtils.setDefaultValue;
+import static org.apache.ignite.internal.managers.communication.GridIoPolicy.UNDEFINED;
+
+/** */
+public class IgnitePluggableSegmentationResolver implements PluggableSegmentationResolver {
+   /** */
+    public static final String ATTR_SEG_RESOLVER_CONFIGURED = "org.apache.ignite.segmentation.resolver.configured";
+
+    /** */
+    public static final String SEG_RESOLVER_ENABLED_PROP_NAME = "org.apache.ignite.segmentation.resolver.enabled";
+
+    /** */
+    private static final String SEG_RESOLVER_THREAD_PREFIX = "segmentation-resolver";
+
+    /** */
+    private static final int[] TOP_CHANGED_EVTS = new int[] {
+        EVT_NODE_LEFT,
+        EVT_NODE_JOINED,
+        EVT_NODE_FAILED
+    };
+
+    /** */
+    private final SimpleDistributedProperty<Boolean> segResolverEnabledProp = new SimpleDistributedProperty<>(
+        SEG_RESOLVER_ENABLED_PROP_NAME,
+        Boolean::parseBoolean
+    );
+
+    /** Ignite kernel context. */
+    private final GridKernalContext ctx;
+
+    /** Ignite logger. */
+    private final IgniteLogger log;
+
+    /** */
+    private final IgniteThreadPoolExecutor stateChangeExec;
+
+    /** */
+    private long lastCheckedTopVer;
+
+    /**  */
+    private volatile State state = State.VALID;
+
+    /** @param ctx Ignite kernel context. */
+    public IgnitePluggableSegmentationResolver(GridKernalContext ctx) {
+        this.ctx = ctx;
+
+        log = ctx.log(getClass());
+
+        stateChangeExec = new IgniteThreadPoolExecutor(
+            SEG_RESOLVER_THREAD_PREFIX,
+            ctx.igniteInstanceName(),
+            1,
+            1,
+            DFLT_THREAD_KEEP_ALIVE_TIME,
+            new LinkedBlockingQueue<>(),
+            UNDEFINED,
+            new OomExceptionHandler(ctx));
+
+        stateChangeExec.allowCoreThreadTimeOut(true);
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean isValidSegment() {
+        return isDisabled() || state != State.INVALID;
+    }
+
+    /** */
+    public void start() {
+        if (ctx.clientNode())
+            return;
+
+        ctx.addNodeAttribute(ATTR_SEG_RESOLVER_CONFIGURED, true);
+
+        ctx.event().addDiscoveryEventListener(new TopologyChangedEventListener(), TOP_CHANGED_EVTS);
+
+        ctx.discovery().setCustomEventListener(
+            ChangeGlobalStateFinishMessage.class,
+            new ClusterStateChangedEventListener()
+        );
+
+        ctx.internalSubscriptionProcessor().registerDistributedConfigurationListener(
+            new DistributedConfigurationLifecycleListener() {
+                /** {@inheritDoc} */
+                @Override public void onReadyToRegister(DistributedPropertyDispatcher dispatcher) {
+                    dispatcher.registerProperty(segResolverEnabledProp);
+                }
+
+                /** {@inheritDoc} */
+                @Override public void onReadyToWrite() {
+                    setDefaultValue(segResolverEnabledProp, U.isLocalNodeCoordinator(ctx.discovery()), log);
+                }
+            });
+    }
+
+    /** @return Discovery data. */
+    public Serializable provideDiscoveryData() {
+        return state;
+    }
+
+    /** @param data Discovery data. */
+    public void onDiscoveryDataReceived(Serializable data) {
+        state = (State)data;
+    }
+
+    /** @param node Node. */
+    public void validateNewNode(ClusterNode node) {
+        if (node.isClient())
+            return;
+
+        if (!TRUE.equals(node.attribute(ATTR_SEG_RESOLVER_CONFIGURED))) {
+            throw new IgniteException( "The Segmentation Resolver plugin is not configured for the server node that is" +
+                " trying to join the cluster. Since the Segmentation Resolver is only applicable if all server nodes" +
+                " in the cluster have one, node join request will be rejected [rejectedNodeId=" + node.id() + ']');
+        }
+
+        if (state == State.VALID) {
+            DiscoCache discoCache = ctx.discovery().discoCache(new AffinityTopologyVersion(lastCheckedTopVer, 0));
+
+            if (discoCache != null) {
+                for (ClusterNode srv : discoCache.serverNodes()) {
+                    if (!ctx.discovery().alive(srv))
+                        throw new IgniteException("Node join request will be rejected due to concurrent node left" +
+                            " process handling [rejectedNodeId=" + node.id() + ']');
+                }
+            }
+        }
+    }
+
+    /** */
+    private boolean isDisabled() {
+        Boolean res = segResolverEnabledProp.get();
+
+        return res == null || !res;
+    }
+
+    /** @return return. */
+    private String formatTopologyNodes(Collection<ClusterNode> nodes) {
+        return nodes.stream().map(n -> n.id().toString()).collect(Collectors.joining(", "));
+    }
+
+    /** */
+    private class TopologyChangedEventListener implements DiscoveryEventListener, HighPriorityListener {
+        /** {@inheritDoc} */
+        @Override public void onEvent(DiscoveryEvent evt, DiscoCache discoCache) {
+            lastCheckedTopVer = evt.topologyVersion();
+
+            if (isDisabled())
+                return;
+
+            if (state == State.VALID && evt.type() == EVT_NODE_FAILED) {
+                List<? extends BaselineNode> baselineNodes = discoCache.baselineNodes();
+
+                if (baselineNodes != null && aliveBaselineNodes(baselineNodes) < baselineNodes.size() / 2 + 1) {
+                    state = State.INVALID;
+
+                    stateChangeExec.execute(() -> {
+                        try {
+                            ctx.cluster().get().state(ACTIVE_READ_ONLY);
+                        }
+                        catch (Throwable e) {
+                            U.error(
+                                log,
+                                "Failed to automatically switch state of the segmented cluster to the READ-ONLY mode" +
+                                    " [segmentedNodes=" + formatTopologyNodes(discoCache.allNodes()) + "]. Cache writes" +
+                                    " are already restricted for all configured caches, but this step is still required" +
+                                    " in order to be able to unlock cache writes in the future. Retry this operation" +
+                                    " manually, if possible.",
+                                e
+                            );
+                        }
+                    });
+
+                    U.warn(log, "Cluster segmentation was detected [segmentedNodes=" +
+                        formatTopologyNodes(discoCache.allNodes()) + ']');
+                }
+            }
+
+            if (ctx.state().isBaselineAutoAdjustEnabled())
+                U.warn(log, "Segmentation Resolver requires baseline to be configured. If no baseline is" +
+                    " set, any topology change is considered valid.");
+        }
+
+        /** {@inheritDoc} */
+        @Override public int order() {
+            return 0;
+        }
+
+        /** */
+        private int aliveBaselineNodes(Collection<? extends BaselineNode> baselineNodes) {
+            int res = 0;
+
+            for (BaselineNode node : baselineNodes) {
+                if (!(node instanceof DetachedClusterNode))
+                    ++res;
+            }
+
+            return res;
+        }
+    }
+
+    /** */
+    private class ClusterStateChangedEventListener implements CustomEventListener<ChangeGlobalStateFinishMessage> {

Review comment:
       From my point of view, it would be better to "inline" this class.

##########
File path: modules/core/src/main/java/org/apache/ignite/cache/validation/IgnitePluggableSegmentationResolver.java
##########
@@ -0,0 +1,275 @@
+/*
+ * 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 org.apache.ignite.cache.validation;
+
+import java.io.Serializable;
+import java.util.Collection;
+import java.util.List;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.stream.Collectors;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.cluster.BaselineNode;
+import org.apache.ignite.cluster.ClusterNode;
+import org.apache.ignite.events.DiscoveryEvent;
+import org.apache.ignite.internal.GridKernalContext;
+import org.apache.ignite.internal.cluster.DetachedClusterNode;
+import org.apache.ignite.internal.managers.discovery.CustomEventListener;
+import org.apache.ignite.internal.managers.discovery.DiscoCache;
+import org.apache.ignite.internal.managers.eventstorage.DiscoveryEventListener;
+import org.apache.ignite.internal.managers.eventstorage.HighPriorityListener;
+import org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion;
+import org.apache.ignite.internal.processors.cluster.ChangeGlobalStateFinishMessage;
+import org.apache.ignite.internal.processors.configuration.distributed.DistributedConfigurationLifecycleListener;
+import org.apache.ignite.internal.processors.configuration.distributed.DistributedPropertyDispatcher;
+import org.apache.ignite.internal.processors.configuration.distributed.SimpleDistributedProperty;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.thread.IgniteThreadPoolExecutor;
+import org.apache.ignite.thread.OomExceptionHandler;
+
+import static java.lang.Boolean.TRUE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE_READ_ONLY;
+import static org.apache.ignite.configuration.IgniteConfiguration.DFLT_THREAD_KEEP_ALIVE_TIME;
+import static org.apache.ignite.events.EventType.EVT_NODE_FAILED;
+import static org.apache.ignite.events.EventType.EVT_NODE_JOINED;
+import static org.apache.ignite.events.EventType.EVT_NODE_LEFT;
+import static org.apache.ignite.internal.cluster.DistributedConfigurationUtils.setDefaultValue;
+import static org.apache.ignite.internal.managers.communication.GridIoPolicy.UNDEFINED;
+
+/** */
+public class IgnitePluggableSegmentationResolver implements PluggableSegmentationResolver {
+   /** */
+    public static final String ATTR_SEG_RESOLVER_CONFIGURED = "org.apache.ignite.segmentation.resolver.configured";
+
+    /** */
+    public static final String SEG_RESOLVER_ENABLED_PROP_NAME = "org.apache.ignite.segmentation.resolver.enabled";
+
+    /** */
+    private static final String SEG_RESOLVER_THREAD_PREFIX = "segmentation-resolver";
+
+    /** */
+    private static final int[] TOP_CHANGED_EVTS = new int[] {
+        EVT_NODE_LEFT,
+        EVT_NODE_JOINED,
+        EVT_NODE_FAILED
+    };
+
+    /** */
+    private final SimpleDistributedProperty<Boolean> segResolverEnabledProp = new SimpleDistributedProperty<>(
+        SEG_RESOLVER_ENABLED_PROP_NAME,
+        Boolean::parseBoolean
+    );
+
+    /** Ignite kernel context. */
+    private final GridKernalContext ctx;
+
+    /** Ignite logger. */
+    private final IgniteLogger log;
+
+    /** */
+    private final IgniteThreadPoolExecutor stateChangeExec;
+
+    /** */
+    private long lastCheckedTopVer;
+
+    /**  */
+    private volatile State state = State.VALID;
+
+    /** @param ctx Ignite kernel context. */
+    public IgnitePluggableSegmentationResolver(GridKernalContext ctx) {
+        this.ctx = ctx;
+
+        log = ctx.log(getClass());
+
+        stateChangeExec = new IgniteThreadPoolExecutor(
+            SEG_RESOLVER_THREAD_PREFIX,
+            ctx.igniteInstanceName(),
+            1,
+            1,
+            DFLT_THREAD_KEEP_ALIVE_TIME,
+            new LinkedBlockingQueue<>(),
+            UNDEFINED,
+            new OomExceptionHandler(ctx));
+
+        stateChangeExec.allowCoreThreadTimeOut(true);
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean isValidSegment() {
+        return isDisabled() || state != State.INVALID;
+    }
+
+    /** */
+    public void start() {
+        if (ctx.clientNode())
+            return;
+
+        ctx.addNodeAttribute(ATTR_SEG_RESOLVER_CONFIGURED, true);
+
+        ctx.event().addDiscoveryEventListener(new TopologyChangedEventListener(), TOP_CHANGED_EVTS);
+
+        ctx.discovery().setCustomEventListener(
+            ChangeGlobalStateFinishMessage.class,
+            new ClusterStateChangedEventListener()
+        );
+
+        ctx.internalSubscriptionProcessor().registerDistributedConfigurationListener(
+            new DistributedConfigurationLifecycleListener() {
+                /** {@inheritDoc} */
+                @Override public void onReadyToRegister(DistributedPropertyDispatcher dispatcher) {
+                    dispatcher.registerProperty(segResolverEnabledProp);
+                }
+
+                /** {@inheritDoc} */
+                @Override public void onReadyToWrite() {
+                    setDefaultValue(segResolverEnabledProp, U.isLocalNodeCoordinator(ctx.discovery()), log);
+                }
+            });
+    }
+
+    /** @return Discovery data. */
+    public Serializable provideDiscoveryData() {
+        return state;
+    }
+
+    /** @param data Discovery data. */
+    public void onDiscoveryDataReceived(Serializable data) {
+        state = (State)data;
+    }
+
+    /** @param node Node. */
+    public void validateNewNode(ClusterNode node) {
+        if (node.isClient())
+            return;
+
+        if (!TRUE.equals(node.attribute(ATTR_SEG_RESOLVER_CONFIGURED))) {
+            throw new IgniteException( "The Segmentation Resolver plugin is not configured for the server node that is" +
+                " trying to join the cluster. Since the Segmentation Resolver is only applicable if all server nodes" +
+                " in the cluster have one, node join request will be rejected [rejectedNodeId=" + node.id() + ']');
+        }
+
+        if (state == State.VALID) {
+            DiscoCache discoCache = ctx.discovery().discoCache(new AffinityTopologyVersion(lastCheckedTopVer, 0));
+
+            if (discoCache != null) {
+                for (ClusterNode srv : discoCache.serverNodes()) {
+                    if (!ctx.discovery().alive(srv))
+                        throw new IgniteException("Node join request will be rejected due to concurrent node left" +
+                            " process handling [rejectedNodeId=" + node.id() + ']');
+                }
+            }
+        }
+    }
+
+    /** */
+    private boolean isDisabled() {
+        Boolean res = segResolverEnabledProp.get();
+
+        return res == null || !res;
+    }
+
+    /** @return return. */
+    private String formatTopologyNodes(Collection<ClusterNode> nodes) {
+        return nodes.stream().map(n -> n.id().toString()).collect(Collectors.joining(", "));
+    }
+
+    /** */
+    private class TopologyChangedEventListener implements DiscoveryEventListener, HighPriorityListener {
+        /** {@inheritDoc} */
+        @Override public void onEvent(DiscoveryEvent evt, DiscoCache discoCache) {
+            lastCheckedTopVer = evt.topologyVersion();
+
+            if (isDisabled())
+                return;
+
+            if (state == State.VALID && evt.type() == EVT_NODE_FAILED) {
+                List<? extends BaselineNode> baselineNodes = discoCache.baselineNodes();
+
+                if (baselineNodes != null && aliveBaselineNodes(baselineNodes) < baselineNodes.size() / 2 + 1) {
+                    state = State.INVALID;
+
+                    stateChangeExec.execute(() -> {
+                        try {
+                            ctx.cluster().get().state(ACTIVE_READ_ONLY);
+                        }
+                        catch (Throwable e) {
+                            U.error(
+                                log,
+                                "Failed to automatically switch state of the segmented cluster to the READ-ONLY mode" +
+                                    " [segmentedNodes=" + formatTopologyNodes(discoCache.allNodes()) + "]. Cache writes" +
+                                    " are already restricted for all configured caches, but this step is still required" +
+                                    " in order to be able to unlock cache writes in the future. Retry this operation" +
+                                    " manually, if possible.",
+                                e
+                            );
+                        }
+                    });
+
+                    U.warn(log, "Cluster segmentation was detected [segmentedNodes=" +
+                        formatTopologyNodes(discoCache.allNodes()) + ']');
+                }
+            }
+
+            if (ctx.state().isBaselineAutoAdjustEnabled())

Review comment:
       As I understand it, the problem with auto-adjustment is not that the baseline has not been set, but that it is being set before the resolver gets current baseline nodes.
   For example, `testConsequentSegmentationResolving` can be passed without disabling auto-adjustement if we set an auto-adjustement timeout `srv.cluster().BaselineAutoAdjustTimeout(1_000)`.
   
   So, I see 2 options here:
   1. get the previous baseline (before the last auto-adjustment that was caused by the top change, if possible) when getting the baseline nodes.
   2. correct the warning message that baseline auto-adjustment is enabled and the segmentation resolver may not work at all. This one seems strange because in in-memory cluster this is the default behavior.

##########
File path: modules/core/src/main/java/org/apache/ignite/cache/validation/IgnitePluggableSegmentationResolver.java
##########
@@ -0,0 +1,275 @@
+/*
+ * 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 org.apache.ignite.cache.validation;
+
+import java.io.Serializable;
+import java.util.Collection;
+import java.util.List;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.stream.Collectors;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.cluster.BaselineNode;
+import org.apache.ignite.cluster.ClusterNode;
+import org.apache.ignite.events.DiscoveryEvent;
+import org.apache.ignite.internal.GridKernalContext;
+import org.apache.ignite.internal.cluster.DetachedClusterNode;
+import org.apache.ignite.internal.managers.discovery.CustomEventListener;
+import org.apache.ignite.internal.managers.discovery.DiscoCache;
+import org.apache.ignite.internal.managers.eventstorage.DiscoveryEventListener;
+import org.apache.ignite.internal.managers.eventstorage.HighPriorityListener;
+import org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion;
+import org.apache.ignite.internal.processors.cluster.ChangeGlobalStateFinishMessage;
+import org.apache.ignite.internal.processors.configuration.distributed.DistributedConfigurationLifecycleListener;
+import org.apache.ignite.internal.processors.configuration.distributed.DistributedPropertyDispatcher;
+import org.apache.ignite.internal.processors.configuration.distributed.SimpleDistributedProperty;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.thread.IgniteThreadPoolExecutor;
+import org.apache.ignite.thread.OomExceptionHandler;
+
+import static java.lang.Boolean.TRUE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE_READ_ONLY;
+import static org.apache.ignite.configuration.IgniteConfiguration.DFLT_THREAD_KEEP_ALIVE_TIME;
+import static org.apache.ignite.events.EventType.EVT_NODE_FAILED;
+import static org.apache.ignite.events.EventType.EVT_NODE_JOINED;
+import static org.apache.ignite.events.EventType.EVT_NODE_LEFT;
+import static org.apache.ignite.internal.cluster.DistributedConfigurationUtils.setDefaultValue;
+import static org.apache.ignite.internal.managers.communication.GridIoPolicy.UNDEFINED;
+
+/** */
+public class IgnitePluggableSegmentationResolver implements PluggableSegmentationResolver {
+   /** */
+    public static final String ATTR_SEG_RESOLVER_CONFIGURED = "org.apache.ignite.segmentation.resolver.configured";
+
+    /** */
+    public static final String SEG_RESOLVER_ENABLED_PROP_NAME = "org.apache.ignite.segmentation.resolver.enabled";
+
+    /** */
+    private static final String SEG_RESOLVER_THREAD_PREFIX = "segmentation-resolver";
+
+    /** */
+    private static final int[] TOP_CHANGED_EVTS = new int[] {
+        EVT_NODE_LEFT,
+        EVT_NODE_JOINED,
+        EVT_NODE_FAILED
+    };
+
+    /** */
+    private final SimpleDistributedProperty<Boolean> segResolverEnabledProp = new SimpleDistributedProperty<>(
+        SEG_RESOLVER_ENABLED_PROP_NAME,
+        Boolean::parseBoolean
+    );
+
+    /** Ignite kernel context. */
+    private final GridKernalContext ctx;
+
+    /** Ignite logger. */
+    private final IgniteLogger log;
+
+    /** */
+    private final IgniteThreadPoolExecutor stateChangeExec;
+
+    /** */
+    private long lastCheckedTopVer;
+
+    /**  */
+    private volatile State state = State.VALID;
+
+    /** @param ctx Ignite kernel context. */
+    public IgnitePluggableSegmentationResolver(GridKernalContext ctx) {
+        this.ctx = ctx;
+
+        log = ctx.log(getClass());
+
+        stateChangeExec = new IgniteThreadPoolExecutor(
+            SEG_RESOLVER_THREAD_PREFIX,
+            ctx.igniteInstanceName(),
+            1,
+            1,
+            DFLT_THREAD_KEEP_ALIVE_TIME,
+            new LinkedBlockingQueue<>(),
+            UNDEFINED,
+            new OomExceptionHandler(ctx));
+
+        stateChangeExec.allowCoreThreadTimeOut(true);
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean isValidSegment() {
+        return isDisabled() || state != State.INVALID;
+    }
+
+    /** */
+    public void start() {
+        if (ctx.clientNode())
+            return;
+
+        ctx.addNodeAttribute(ATTR_SEG_RESOLVER_CONFIGURED, true);
+
+        ctx.event().addDiscoveryEventListener(new TopologyChangedEventListener(), TOP_CHANGED_EVTS);
+
+        ctx.discovery().setCustomEventListener(
+            ChangeGlobalStateFinishMessage.class,
+            new ClusterStateChangedEventListener()
+        );
+
+        ctx.internalSubscriptionProcessor().registerDistributedConfigurationListener(
+            new DistributedConfigurationLifecycleListener() {
+                /** {@inheritDoc} */
+                @Override public void onReadyToRegister(DistributedPropertyDispatcher dispatcher) {
+                    dispatcher.registerProperty(segResolverEnabledProp);
+                }
+
+                /** {@inheritDoc} */
+                @Override public void onReadyToWrite() {
+                    setDefaultValue(segResolverEnabledProp, U.isLocalNodeCoordinator(ctx.discovery()), log);
+                }
+            });
+    }
+
+    /** @return Discovery data. */
+    public Serializable provideDiscoveryData() {
+        return state;
+    }
+
+    /** @param data Discovery data. */
+    public void onDiscoveryDataReceived(Serializable data) {
+        state = (State)data;
+    }
+
+    /** @param node Node. */
+    public void validateNewNode(ClusterNode node) {
+        if (node.isClient())
+            return;
+
+        if (!TRUE.equals(node.attribute(ATTR_SEG_RESOLVER_CONFIGURED))) {
+            throw new IgniteException( "The Segmentation Resolver plugin is not configured for the server node that is" +
+                " trying to join the cluster. Since the Segmentation Resolver is only applicable if all server nodes" +
+                " in the cluster have one, node join request will be rejected [rejectedNodeId=" + node.id() + ']');
+        }
+
+        if (state == State.VALID) {
+            DiscoCache discoCache = ctx.discovery().discoCache(new AffinityTopologyVersion(lastCheckedTopVer, 0));
+
+            if (discoCache != null) {
+                for (ClusterNode srv : discoCache.serverNodes()) {
+                    if (!ctx.discovery().alive(srv))
+                        throw new IgniteException("Node join request will be rejected due to concurrent node left" +
+                            " process handling [rejectedNodeId=" + node.id() + ']');
+                }
+            }
+        }
+    }
+
+    /** */
+    private boolean isDisabled() {
+        Boolean res = segResolverEnabledProp.get();
+
+        return res == null || !res;
+    }
+
+    /** @return return. */
+    private String formatTopologyNodes(Collection<ClusterNode> nodes) {
+        return nodes.stream().map(n -> n.id().toString()).collect(Collectors.joining(", "));
+    }
+
+    /** */
+    private class TopologyChangedEventListener implements DiscoveryEventListener, HighPriorityListener {
+        /** {@inheritDoc} */
+        @Override public void onEvent(DiscoveryEvent evt, DiscoCache discoCache) {
+            lastCheckedTopVer = evt.topologyVersion();
+
+            if (isDisabled())
+                return;
+
+            if (state == State.VALID && evt.type() == EVT_NODE_FAILED) {

Review comment:
       When we change cluster state **to** read-only is treated as CLUSTER_WRITE_BLOCKED, but if we start the cluster in the read-only mode (`clusterStateOnStart` config property) it will be treated as "VALID" and this code block executes on segmentation.
   MB we should check the cluster state also.




-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [ignite] ololo3000 commented on a change in pull request #9534: wip

Posted by GitBox <gi...@apache.org>.
ololo3000 commented on a change in pull request #9534:
URL: https://github.com/apache/ignite/pull/9534#discussion_r741004791



##########
File path: modules/core/src/main/java/org/apache/ignite/cache/validation/IgnitePluggableSegmentationResolver.java
##########
@@ -0,0 +1,275 @@
+/*
+ * 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 org.apache.ignite.cache.validation;
+
+import java.io.Serializable;
+import java.util.Collection;
+import java.util.List;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.stream.Collectors;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.cluster.BaselineNode;
+import org.apache.ignite.cluster.ClusterNode;
+import org.apache.ignite.events.DiscoveryEvent;
+import org.apache.ignite.internal.GridKernalContext;
+import org.apache.ignite.internal.cluster.DetachedClusterNode;
+import org.apache.ignite.internal.managers.discovery.CustomEventListener;
+import org.apache.ignite.internal.managers.discovery.DiscoCache;
+import org.apache.ignite.internal.managers.eventstorage.DiscoveryEventListener;
+import org.apache.ignite.internal.managers.eventstorage.HighPriorityListener;
+import org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion;
+import org.apache.ignite.internal.processors.cluster.ChangeGlobalStateFinishMessage;
+import org.apache.ignite.internal.processors.configuration.distributed.DistributedConfigurationLifecycleListener;
+import org.apache.ignite.internal.processors.configuration.distributed.DistributedPropertyDispatcher;
+import org.apache.ignite.internal.processors.configuration.distributed.SimpleDistributedProperty;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.thread.IgniteThreadPoolExecutor;
+import org.apache.ignite.thread.OomExceptionHandler;
+
+import static java.lang.Boolean.TRUE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE_READ_ONLY;
+import static org.apache.ignite.configuration.IgniteConfiguration.DFLT_THREAD_KEEP_ALIVE_TIME;
+import static org.apache.ignite.events.EventType.EVT_NODE_FAILED;
+import static org.apache.ignite.events.EventType.EVT_NODE_JOINED;
+import static org.apache.ignite.events.EventType.EVT_NODE_LEFT;
+import static org.apache.ignite.internal.cluster.DistributedConfigurationUtils.setDefaultValue;
+import static org.apache.ignite.internal.managers.communication.GridIoPolicy.UNDEFINED;
+
+/** */
+public class IgnitePluggableSegmentationResolver implements PluggableSegmentationResolver {
+   /** */
+    public static final String ATTR_SEG_RESOLVER_CONFIGURED = "org.apache.ignite.segmentation.resolver.configured";
+
+    /** */
+    public static final String SEG_RESOLVER_ENABLED_PROP_NAME = "org.apache.ignite.segmentation.resolver.enabled";
+
+    /** */
+    private static final String SEG_RESOLVER_THREAD_PREFIX = "segmentation-resolver";
+
+    /** */
+    private static final int[] TOP_CHANGED_EVTS = new int[] {
+        EVT_NODE_LEFT,
+        EVT_NODE_JOINED,
+        EVT_NODE_FAILED
+    };
+
+    /** */
+    private final SimpleDistributedProperty<Boolean> segResolverEnabledProp = new SimpleDistributedProperty<>(
+        SEG_RESOLVER_ENABLED_PROP_NAME,
+        Boolean::parseBoolean
+    );
+
+    /** Ignite kernel context. */
+    private final GridKernalContext ctx;
+
+    /** Ignite logger. */
+    private final IgniteLogger log;
+
+    /** */
+    private final IgniteThreadPoolExecutor stateChangeExec;
+
+    /** */
+    private long lastCheckedTopVer;
+
+    /**  */
+    private volatile State state = State.VALID;
+
+    /** @param ctx Ignite kernel context. */
+    public IgnitePluggableSegmentationResolver(GridKernalContext ctx) {
+        this.ctx = ctx;
+
+        log = ctx.log(getClass());
+
+        stateChangeExec = new IgniteThreadPoolExecutor(
+            SEG_RESOLVER_THREAD_PREFIX,
+            ctx.igniteInstanceName(),
+            1,
+            1,
+            DFLT_THREAD_KEEP_ALIVE_TIME,
+            new LinkedBlockingQueue<>(),
+            UNDEFINED,
+            new OomExceptionHandler(ctx));
+
+        stateChangeExec.allowCoreThreadTimeOut(true);
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean isValidSegment() {
+        return isDisabled() || state != State.INVALID;
+    }
+
+    /** */
+    public void start() {
+        if (ctx.clientNode())
+            return;
+
+        ctx.addNodeAttribute(ATTR_SEG_RESOLVER_CONFIGURED, true);
+
+        ctx.event().addDiscoveryEventListener(new TopologyChangedEventListener(), TOP_CHANGED_EVTS);
+
+        ctx.discovery().setCustomEventListener(
+            ChangeGlobalStateFinishMessage.class,
+            new ClusterStateChangedEventListener()
+        );
+
+        ctx.internalSubscriptionProcessor().registerDistributedConfigurationListener(
+            new DistributedConfigurationLifecycleListener() {
+                /** {@inheritDoc} */
+                @Override public void onReadyToRegister(DistributedPropertyDispatcher dispatcher) {
+                    dispatcher.registerProperty(segResolverEnabledProp);
+                }
+
+                /** {@inheritDoc} */
+                @Override public void onReadyToWrite() {
+                    setDefaultValue(segResolverEnabledProp, U.isLocalNodeCoordinator(ctx.discovery()), log);
+                }
+            });
+    }
+
+    /** @return Discovery data. */
+    public Serializable provideDiscoveryData() {
+        return state;
+    }
+
+    /** @param data Discovery data. */
+    public void onDiscoveryDataReceived(Serializable data) {
+        state = (State)data;
+    }
+
+    /** @param node Node. */
+    public void validateNewNode(ClusterNode node) {
+        if (node.isClient())
+            return;
+
+        if (!TRUE.equals(node.attribute(ATTR_SEG_RESOLVER_CONFIGURED))) {
+            throw new IgniteException( "The Segmentation Resolver plugin is not configured for the server node that is" +
+                " trying to join the cluster. Since the Segmentation Resolver is only applicable if all server nodes" +
+                " in the cluster have one, node join request will be rejected [rejectedNodeId=" + node.id() + ']');
+        }
+
+        if (state == State.VALID) {
+            DiscoCache discoCache = ctx.discovery().discoCache(new AffinityTopologyVersion(lastCheckedTopVer, 0));
+
+            if (discoCache != null) {
+                for (ClusterNode srv : discoCache.serverNodes()) {
+                    if (!ctx.discovery().alive(srv))
+                        throw new IgniteException("Node join request will be rejected due to concurrent node left" +
+                            " process handling [rejectedNodeId=" + node.id() + ']');
+                }
+            }
+        }
+    }
+
+    /** */
+    private boolean isDisabled() {
+        Boolean res = segResolverEnabledProp.get();
+
+        return res == null || !res;
+    }
+
+    /** @return return. */
+    private String formatTopologyNodes(Collection<ClusterNode> nodes) {
+        return nodes.stream().map(n -> n.id().toString()).collect(Collectors.joining(", "));
+    }
+
+    /** */
+    private class TopologyChangedEventListener implements DiscoveryEventListener, HighPriorityListener {
+        /** {@inheritDoc} */
+        @Override public void onEvent(DiscoveryEvent evt, DiscoCache discoCache) {
+            lastCheckedTopVer = evt.topologyVersion();
+
+            if (isDisabled())
+                return;
+
+            if (state == State.VALID && evt.type() == EVT_NODE_FAILED) {
+                List<? extends BaselineNode> baselineNodes = discoCache.baselineNodes();
+
+                if (baselineNodes != null && aliveBaselineNodes(baselineNodes) < baselineNodes.size() / 2 + 1) {
+                    state = State.INVALID;
+
+                    stateChangeExec.execute(() -> {
+                        try {
+                            ctx.cluster().get().state(ACTIVE_READ_ONLY);
+                        }
+                        catch (Throwable e) {
+                            U.error(
+                                log,
+                                "Failed to automatically switch state of the segmented cluster to the READ-ONLY mode" +
+                                    " [segmentedNodes=" + formatTopologyNodes(discoCache.allNodes()) + "]. Cache writes" +
+                                    " are already restricted for all configured caches, but this step is still required" +
+                                    " in order to be able to unlock cache writes in the future. Retry this operation" +
+                                    " manually, if possible.",
+                                e
+                            );
+                        }
+                    });
+
+                    U.warn(log, "Cluster segmentation was detected [segmentedNodes=" +
+                        formatTopologyNodes(discoCache.allNodes()) + ']');
+                }
+            }
+
+            if (ctx.state().isBaselineAutoAdjustEnabled())
+                U.warn(log, "Segmentation Resolver requires baseline to be configured. If no baseline is" +
+                    " set, any topology change is considered valid.");
+        }
+
+        /** {@inheritDoc} */
+        @Override public int order() {
+            return 0;
+        }
+
+        /** */
+        private int aliveBaselineNodes(Collection<? extends BaselineNode> baselineNodes) {
+            int res = 0;
+
+            for (BaselineNode node : baselineNodes) {
+                if (!(node instanceof DetachedClusterNode))
+                    ++res;
+            }
+
+            return res;
+        }
+    }
+
+    /** */
+    private class ClusterStateChangedEventListener implements CustomEventListener<ChangeGlobalStateFinishMessage> {

Review comment:
       But in this case we will crumble up the `start` method and won't  gain any real profit.




-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [ignite] xtern commented on a change in pull request #9534: wip

Posted by GitBox <gi...@apache.org>.
xtern commented on a change in pull request #9534:
URL: https://github.com/apache/ignite/pull/9534#discussion_r740904230



##########
File path: modules/core/src/main/java/org/apache/ignite/cache/validation/IgnitePluggableSegmentationResolver.java
##########
@@ -0,0 +1,275 @@
+/*
+ * 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 org.apache.ignite.cache.validation;
+
+import java.io.Serializable;
+import java.util.Collection;
+import java.util.List;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.stream.Collectors;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.cluster.BaselineNode;
+import org.apache.ignite.cluster.ClusterNode;
+import org.apache.ignite.events.DiscoveryEvent;
+import org.apache.ignite.internal.GridKernalContext;
+import org.apache.ignite.internal.cluster.DetachedClusterNode;
+import org.apache.ignite.internal.managers.discovery.CustomEventListener;
+import org.apache.ignite.internal.managers.discovery.DiscoCache;
+import org.apache.ignite.internal.managers.eventstorage.DiscoveryEventListener;
+import org.apache.ignite.internal.managers.eventstorage.HighPriorityListener;
+import org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion;
+import org.apache.ignite.internal.processors.cluster.ChangeGlobalStateFinishMessage;
+import org.apache.ignite.internal.processors.configuration.distributed.DistributedConfigurationLifecycleListener;
+import org.apache.ignite.internal.processors.configuration.distributed.DistributedPropertyDispatcher;
+import org.apache.ignite.internal.processors.configuration.distributed.SimpleDistributedProperty;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.thread.IgniteThreadPoolExecutor;
+import org.apache.ignite.thread.OomExceptionHandler;
+
+import static java.lang.Boolean.TRUE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE_READ_ONLY;
+import static org.apache.ignite.configuration.IgniteConfiguration.DFLT_THREAD_KEEP_ALIVE_TIME;
+import static org.apache.ignite.events.EventType.EVT_NODE_FAILED;
+import static org.apache.ignite.events.EventType.EVT_NODE_JOINED;
+import static org.apache.ignite.events.EventType.EVT_NODE_LEFT;
+import static org.apache.ignite.internal.cluster.DistributedConfigurationUtils.setDefaultValue;
+import static org.apache.ignite.internal.managers.communication.GridIoPolicy.UNDEFINED;
+
+/** */
+public class IgnitePluggableSegmentationResolver implements PluggableSegmentationResolver {
+   /** */
+    public static final String ATTR_SEG_RESOLVER_CONFIGURED = "org.apache.ignite.segmentation.resolver.configured";
+
+    /** */
+    public static final String SEG_RESOLVER_ENABLED_PROP_NAME = "org.apache.ignite.segmentation.resolver.enabled";
+
+    /** */
+    private static final String SEG_RESOLVER_THREAD_PREFIX = "segmentation-resolver";
+
+    /** */
+    private static final int[] TOP_CHANGED_EVTS = new int[] {
+        EVT_NODE_LEFT,
+        EVT_NODE_JOINED,
+        EVT_NODE_FAILED
+    };
+
+    /** */
+    private final SimpleDistributedProperty<Boolean> segResolverEnabledProp = new SimpleDistributedProperty<>(
+        SEG_RESOLVER_ENABLED_PROP_NAME,
+        Boolean::parseBoolean
+    );
+
+    /** Ignite kernel context. */
+    private final GridKernalContext ctx;
+
+    /** Ignite logger. */
+    private final IgniteLogger log;
+
+    /** */
+    private final IgniteThreadPoolExecutor stateChangeExec;
+
+    /** */
+    private long lastCheckedTopVer;
+
+    /**  */
+    private volatile State state = State.VALID;
+
+    /** @param ctx Ignite kernel context. */
+    public IgnitePluggableSegmentationResolver(GridKernalContext ctx) {
+        this.ctx = ctx;
+
+        log = ctx.log(getClass());
+
+        stateChangeExec = new IgniteThreadPoolExecutor(
+            SEG_RESOLVER_THREAD_PREFIX,
+            ctx.igniteInstanceName(),
+            1,
+            1,
+            DFLT_THREAD_KEEP_ALIVE_TIME,
+            new LinkedBlockingQueue<>(),
+            UNDEFINED,
+            new OomExceptionHandler(ctx));
+
+        stateChangeExec.allowCoreThreadTimeOut(true);
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean isValidSegment() {
+        return isDisabled() || state != State.INVALID;
+    }
+
+    /** */
+    public void start() {
+        if (ctx.clientNode())
+            return;
+
+        ctx.addNodeAttribute(ATTR_SEG_RESOLVER_CONFIGURED, true);
+
+        ctx.event().addDiscoveryEventListener(new TopologyChangedEventListener(), TOP_CHANGED_EVTS);
+
+        ctx.discovery().setCustomEventListener(
+            ChangeGlobalStateFinishMessage.class,
+            new ClusterStateChangedEventListener()
+        );
+
+        ctx.internalSubscriptionProcessor().registerDistributedConfigurationListener(
+            new DistributedConfigurationLifecycleListener() {
+                /** {@inheritDoc} */
+                @Override public void onReadyToRegister(DistributedPropertyDispatcher dispatcher) {
+                    dispatcher.registerProperty(segResolverEnabledProp);
+                }
+
+                /** {@inheritDoc} */
+                @Override public void onReadyToWrite() {
+                    setDefaultValue(segResolverEnabledProp, U.isLocalNodeCoordinator(ctx.discovery()), log);
+                }
+            });
+    }
+
+    /** @return Discovery data. */
+    public Serializable provideDiscoveryData() {
+        return state;
+    }
+
+    /** @param data Discovery data. */
+    public void onDiscoveryDataReceived(Serializable data) {
+        state = (State)data;
+    }
+
+    /** @param node Node. */
+    public void validateNewNode(ClusterNode node) {
+        if (node.isClient())
+            return;
+
+        if (!TRUE.equals(node.attribute(ATTR_SEG_RESOLVER_CONFIGURED))) {
+            throw new IgniteException( "The Segmentation Resolver plugin is not configured for the server node that is" +
+                " trying to join the cluster. Since the Segmentation Resolver is only applicable if all server nodes" +
+                " in the cluster have one, node join request will be rejected [rejectedNodeId=" + node.id() + ']');
+        }
+
+        if (state == State.VALID) {
+            DiscoCache discoCache = ctx.discovery().discoCache(new AffinityTopologyVersion(lastCheckedTopVer, 0));
+
+            if (discoCache != null) {
+                for (ClusterNode srv : discoCache.serverNodes()) {
+                    if (!ctx.discovery().alive(srv))
+                        throw new IgniteException("Node join request will be rejected due to concurrent node left" +
+                            " process handling [rejectedNodeId=" + node.id() + ']');
+                }
+            }
+        }
+    }
+
+    /** */
+    private boolean isDisabled() {
+        Boolean res = segResolverEnabledProp.get();
+
+        return res == null || !res;
+    }
+
+    /** @return return. */
+    private String formatTopologyNodes(Collection<ClusterNode> nodes) {

Review comment:
       I suggest moving this method inside the `TopologyChangedEventListener` class.

##########
File path: modules/core/src/main/java/org/apache/ignite/cache/validation/IgnitePluggableSegmentationResolver.java
##########
@@ -0,0 +1,275 @@
+/*
+ * 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 org.apache.ignite.cache.validation;
+
+import java.io.Serializable;
+import java.util.Collection;
+import java.util.List;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.stream.Collectors;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.cluster.BaselineNode;
+import org.apache.ignite.cluster.ClusterNode;
+import org.apache.ignite.events.DiscoveryEvent;
+import org.apache.ignite.internal.GridKernalContext;
+import org.apache.ignite.internal.cluster.DetachedClusterNode;
+import org.apache.ignite.internal.managers.discovery.CustomEventListener;
+import org.apache.ignite.internal.managers.discovery.DiscoCache;
+import org.apache.ignite.internal.managers.eventstorage.DiscoveryEventListener;
+import org.apache.ignite.internal.managers.eventstorage.HighPriorityListener;
+import org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion;
+import org.apache.ignite.internal.processors.cluster.ChangeGlobalStateFinishMessage;
+import org.apache.ignite.internal.processors.configuration.distributed.DistributedConfigurationLifecycleListener;
+import org.apache.ignite.internal.processors.configuration.distributed.DistributedPropertyDispatcher;
+import org.apache.ignite.internal.processors.configuration.distributed.SimpleDistributedProperty;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.thread.IgniteThreadPoolExecutor;
+import org.apache.ignite.thread.OomExceptionHandler;
+
+import static java.lang.Boolean.TRUE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE_READ_ONLY;
+import static org.apache.ignite.configuration.IgniteConfiguration.DFLT_THREAD_KEEP_ALIVE_TIME;
+import static org.apache.ignite.events.EventType.EVT_NODE_FAILED;
+import static org.apache.ignite.events.EventType.EVT_NODE_JOINED;
+import static org.apache.ignite.events.EventType.EVT_NODE_LEFT;
+import static org.apache.ignite.internal.cluster.DistributedConfigurationUtils.setDefaultValue;
+import static org.apache.ignite.internal.managers.communication.GridIoPolicy.UNDEFINED;
+
+/** */
+public class IgnitePluggableSegmentationResolver implements PluggableSegmentationResolver {
+   /** */
+    public static final String ATTR_SEG_RESOLVER_CONFIGURED = "org.apache.ignite.segmentation.resolver.configured";
+
+    /** */
+    public static final String SEG_RESOLVER_ENABLED_PROP_NAME = "org.apache.ignite.segmentation.resolver.enabled";
+
+    /** */
+    private static final String SEG_RESOLVER_THREAD_PREFIX = "segmentation-resolver";
+
+    /** */
+    private static final int[] TOP_CHANGED_EVTS = new int[] {
+        EVT_NODE_LEFT,
+        EVT_NODE_JOINED,
+        EVT_NODE_FAILED
+    };
+
+    /** */
+    private final SimpleDistributedProperty<Boolean> segResolverEnabledProp = new SimpleDistributedProperty<>(
+        SEG_RESOLVER_ENABLED_PROP_NAME,
+        Boolean::parseBoolean
+    );
+
+    /** Ignite kernel context. */
+    private final GridKernalContext ctx;
+
+    /** Ignite logger. */
+    private final IgniteLogger log;
+
+    /** */
+    private final IgniteThreadPoolExecutor stateChangeExec;
+
+    /** */
+    private long lastCheckedTopVer;
+
+    /**  */
+    private volatile State state = State.VALID;
+
+    /** @param ctx Ignite kernel context. */
+    public IgnitePluggableSegmentationResolver(GridKernalContext ctx) {
+        this.ctx = ctx;
+
+        log = ctx.log(getClass());
+
+        stateChangeExec = new IgniteThreadPoolExecutor(
+            SEG_RESOLVER_THREAD_PREFIX,
+            ctx.igniteInstanceName(),
+            1,
+            1,
+            DFLT_THREAD_KEEP_ALIVE_TIME,
+            new LinkedBlockingQueue<>(),
+            UNDEFINED,
+            new OomExceptionHandler(ctx));
+
+        stateChangeExec.allowCoreThreadTimeOut(true);
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean isValidSegment() {
+        return isDisabled() || state != State.INVALID;
+    }
+
+    /** */
+    public void start() {
+        if (ctx.clientNode())
+            return;
+
+        ctx.addNodeAttribute(ATTR_SEG_RESOLVER_CONFIGURED, true);
+
+        ctx.event().addDiscoveryEventListener(new TopologyChangedEventListener(), TOP_CHANGED_EVTS);
+
+        ctx.discovery().setCustomEventListener(
+            ChangeGlobalStateFinishMessage.class,
+            new ClusterStateChangedEventListener()
+        );
+
+        ctx.internalSubscriptionProcessor().registerDistributedConfigurationListener(
+            new DistributedConfigurationLifecycleListener() {
+                /** {@inheritDoc} */
+                @Override public void onReadyToRegister(DistributedPropertyDispatcher dispatcher) {
+                    dispatcher.registerProperty(segResolverEnabledProp);
+                }
+
+                /** {@inheritDoc} */
+                @Override public void onReadyToWrite() {
+                    setDefaultValue(segResolverEnabledProp, U.isLocalNodeCoordinator(ctx.discovery()), log);
+                }
+            });
+    }
+
+    /** @return Discovery data. */
+    public Serializable provideDiscoveryData() {
+        return state;
+    }
+
+    /** @param data Discovery data. */
+    public void onDiscoveryDataReceived(Serializable data) {
+        state = (State)data;
+    }
+
+    /** @param node Node. */
+    public void validateNewNode(ClusterNode node) {
+        if (node.isClient())
+            return;
+
+        if (!TRUE.equals(node.attribute(ATTR_SEG_RESOLVER_CONFIGURED))) {
+            throw new IgniteException( "The Segmentation Resolver plugin is not configured for the server node that is" +
+                " trying to join the cluster. Since the Segmentation Resolver is only applicable if all server nodes" +
+                " in the cluster have one, node join request will be rejected [rejectedNodeId=" + node.id() + ']');
+        }
+
+        if (state == State.VALID) {
+            DiscoCache discoCache = ctx.discovery().discoCache(new AffinityTopologyVersion(lastCheckedTopVer, 0));
+
+            if (discoCache != null) {
+                for (ClusterNode srv : discoCache.serverNodes()) {
+                    if (!ctx.discovery().alive(srv))
+                        throw new IgniteException("Node join request will be rejected due to concurrent node left" +
+                            " process handling [rejectedNodeId=" + node.id() + ']');
+                }
+            }
+        }
+    }
+
+    /** */
+    private boolean isDisabled() {
+        Boolean res = segResolverEnabledProp.get();
+
+        return res == null || !res;
+    }
+
+    /** @return return. */
+    private String formatTopologyNodes(Collection<ClusterNode> nodes) {
+        return nodes.stream().map(n -> n.id().toString()).collect(Collectors.joining(", "));
+    }
+
+    /** */
+    private class TopologyChangedEventListener implements DiscoveryEventListener, HighPriorityListener {
+        /** {@inheritDoc} */
+        @Override public void onEvent(DiscoveryEvent evt, DiscoCache discoCache) {
+            lastCheckedTopVer = evt.topologyVersion();
+
+            if (isDisabled())
+                return;
+
+            if (state == State.VALID && evt.type() == EVT_NODE_FAILED) {
+                List<? extends BaselineNode> baselineNodes = discoCache.baselineNodes();
+
+                if (baselineNodes != null && aliveBaselineNodes(baselineNodes) < baselineNodes.size() / 2 + 1) {
+                    state = State.INVALID;
+
+                    stateChangeExec.execute(() -> {
+                        try {
+                            ctx.cluster().get().state(ACTIVE_READ_ONLY);
+                        }
+                        catch (Throwable e) {
+                            U.error(
+                                log,
+                                "Failed to automatically switch state of the segmented cluster to the READ-ONLY mode" +
+                                    " [segmentedNodes=" + formatTopologyNodes(discoCache.allNodes()) + "]. Cache writes" +
+                                    " are already restricted for all configured caches, but this step is still required" +
+                                    " in order to be able to unlock cache writes in the future. Retry this operation" +
+                                    " manually, if possible.",
+                                e
+                            );
+                        }
+                    });
+
+                    U.warn(log, "Cluster segmentation was detected [segmentedNodes=" +
+                        formatTopologyNodes(discoCache.allNodes()) + ']');
+                }
+            }
+
+            if (ctx.state().isBaselineAutoAdjustEnabled())
+                U.warn(log, "Segmentation Resolver requires baseline to be configured. If no baseline is" +
+                    " set, any topology change is considered valid.");
+        }
+
+        /** {@inheritDoc} */
+        @Override public int order() {
+            return 0;
+        }
+
+        /** */
+        private int aliveBaselineNodes(Collection<? extends BaselineNode> baselineNodes) {
+            int res = 0;
+
+            for (BaselineNode node : baselineNodes) {
+                if (!(node instanceof DetachedClusterNode))
+                    ++res;
+            }
+
+            return res;
+        }
+    }
+
+    /** */
+    private class ClusterStateChangedEventListener implements CustomEventListener<ChangeGlobalStateFinishMessage> {

Review comment:
       From my point of view, it would be better to "inline" this class.

##########
File path: modules/core/src/main/java/org/apache/ignite/cache/validation/IgnitePluggableSegmentationResolver.java
##########
@@ -0,0 +1,275 @@
+/*
+ * 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 org.apache.ignite.cache.validation;
+
+import java.io.Serializable;
+import java.util.Collection;
+import java.util.List;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.stream.Collectors;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.cluster.BaselineNode;
+import org.apache.ignite.cluster.ClusterNode;
+import org.apache.ignite.events.DiscoveryEvent;
+import org.apache.ignite.internal.GridKernalContext;
+import org.apache.ignite.internal.cluster.DetachedClusterNode;
+import org.apache.ignite.internal.managers.discovery.CustomEventListener;
+import org.apache.ignite.internal.managers.discovery.DiscoCache;
+import org.apache.ignite.internal.managers.eventstorage.DiscoveryEventListener;
+import org.apache.ignite.internal.managers.eventstorage.HighPriorityListener;
+import org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion;
+import org.apache.ignite.internal.processors.cluster.ChangeGlobalStateFinishMessage;
+import org.apache.ignite.internal.processors.configuration.distributed.DistributedConfigurationLifecycleListener;
+import org.apache.ignite.internal.processors.configuration.distributed.DistributedPropertyDispatcher;
+import org.apache.ignite.internal.processors.configuration.distributed.SimpleDistributedProperty;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.thread.IgniteThreadPoolExecutor;
+import org.apache.ignite.thread.OomExceptionHandler;
+
+import static java.lang.Boolean.TRUE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE_READ_ONLY;
+import static org.apache.ignite.configuration.IgniteConfiguration.DFLT_THREAD_KEEP_ALIVE_TIME;
+import static org.apache.ignite.events.EventType.EVT_NODE_FAILED;
+import static org.apache.ignite.events.EventType.EVT_NODE_JOINED;
+import static org.apache.ignite.events.EventType.EVT_NODE_LEFT;
+import static org.apache.ignite.internal.cluster.DistributedConfigurationUtils.setDefaultValue;
+import static org.apache.ignite.internal.managers.communication.GridIoPolicy.UNDEFINED;
+
+/** */
+public class IgnitePluggableSegmentationResolver implements PluggableSegmentationResolver {
+   /** */
+    public static final String ATTR_SEG_RESOLVER_CONFIGURED = "org.apache.ignite.segmentation.resolver.configured";
+
+    /** */
+    public static final String SEG_RESOLVER_ENABLED_PROP_NAME = "org.apache.ignite.segmentation.resolver.enabled";
+
+    /** */
+    private static final String SEG_RESOLVER_THREAD_PREFIX = "segmentation-resolver";
+
+    /** */
+    private static final int[] TOP_CHANGED_EVTS = new int[] {
+        EVT_NODE_LEFT,
+        EVT_NODE_JOINED,
+        EVT_NODE_FAILED
+    };
+
+    /** */
+    private final SimpleDistributedProperty<Boolean> segResolverEnabledProp = new SimpleDistributedProperty<>(
+        SEG_RESOLVER_ENABLED_PROP_NAME,
+        Boolean::parseBoolean
+    );
+
+    /** Ignite kernel context. */
+    private final GridKernalContext ctx;
+
+    /** Ignite logger. */
+    private final IgniteLogger log;
+
+    /** */
+    private final IgniteThreadPoolExecutor stateChangeExec;
+
+    /** */
+    private long lastCheckedTopVer;
+
+    /**  */
+    private volatile State state = State.VALID;
+
+    /** @param ctx Ignite kernel context. */
+    public IgnitePluggableSegmentationResolver(GridKernalContext ctx) {
+        this.ctx = ctx;
+
+        log = ctx.log(getClass());
+
+        stateChangeExec = new IgniteThreadPoolExecutor(
+            SEG_RESOLVER_THREAD_PREFIX,
+            ctx.igniteInstanceName(),
+            1,
+            1,
+            DFLT_THREAD_KEEP_ALIVE_TIME,
+            new LinkedBlockingQueue<>(),
+            UNDEFINED,
+            new OomExceptionHandler(ctx));
+
+        stateChangeExec.allowCoreThreadTimeOut(true);
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean isValidSegment() {
+        return isDisabled() || state != State.INVALID;
+    }
+
+    /** */
+    public void start() {
+        if (ctx.clientNode())
+            return;
+
+        ctx.addNodeAttribute(ATTR_SEG_RESOLVER_CONFIGURED, true);
+
+        ctx.event().addDiscoveryEventListener(new TopologyChangedEventListener(), TOP_CHANGED_EVTS);
+
+        ctx.discovery().setCustomEventListener(
+            ChangeGlobalStateFinishMessage.class,
+            new ClusterStateChangedEventListener()
+        );
+
+        ctx.internalSubscriptionProcessor().registerDistributedConfigurationListener(
+            new DistributedConfigurationLifecycleListener() {
+                /** {@inheritDoc} */
+                @Override public void onReadyToRegister(DistributedPropertyDispatcher dispatcher) {
+                    dispatcher.registerProperty(segResolverEnabledProp);
+                }
+
+                /** {@inheritDoc} */
+                @Override public void onReadyToWrite() {
+                    setDefaultValue(segResolverEnabledProp, U.isLocalNodeCoordinator(ctx.discovery()), log);
+                }
+            });
+    }
+
+    /** @return Discovery data. */
+    public Serializable provideDiscoveryData() {
+        return state;
+    }
+
+    /** @param data Discovery data. */
+    public void onDiscoveryDataReceived(Serializable data) {
+        state = (State)data;
+    }
+
+    /** @param node Node. */
+    public void validateNewNode(ClusterNode node) {
+        if (node.isClient())
+            return;
+
+        if (!TRUE.equals(node.attribute(ATTR_SEG_RESOLVER_CONFIGURED))) {
+            throw new IgniteException( "The Segmentation Resolver plugin is not configured for the server node that is" +
+                " trying to join the cluster. Since the Segmentation Resolver is only applicable if all server nodes" +
+                " in the cluster have one, node join request will be rejected [rejectedNodeId=" + node.id() + ']');
+        }
+
+        if (state == State.VALID) {
+            DiscoCache discoCache = ctx.discovery().discoCache(new AffinityTopologyVersion(lastCheckedTopVer, 0));
+
+            if (discoCache != null) {
+                for (ClusterNode srv : discoCache.serverNodes()) {
+                    if (!ctx.discovery().alive(srv))
+                        throw new IgniteException("Node join request will be rejected due to concurrent node left" +
+                            " process handling [rejectedNodeId=" + node.id() + ']');
+                }
+            }
+        }
+    }
+
+    /** */
+    private boolean isDisabled() {
+        Boolean res = segResolverEnabledProp.get();
+
+        return res == null || !res;
+    }
+
+    /** @return return. */
+    private String formatTopologyNodes(Collection<ClusterNode> nodes) {
+        return nodes.stream().map(n -> n.id().toString()).collect(Collectors.joining(", "));
+    }
+
+    /** */
+    private class TopologyChangedEventListener implements DiscoveryEventListener, HighPriorityListener {
+        /** {@inheritDoc} */
+        @Override public void onEvent(DiscoveryEvent evt, DiscoCache discoCache) {
+            lastCheckedTopVer = evt.topologyVersion();
+
+            if (isDisabled())
+                return;
+
+            if (state == State.VALID && evt.type() == EVT_NODE_FAILED) {
+                List<? extends BaselineNode> baselineNodes = discoCache.baselineNodes();
+
+                if (baselineNodes != null && aliveBaselineNodes(baselineNodes) < baselineNodes.size() / 2 + 1) {
+                    state = State.INVALID;
+
+                    stateChangeExec.execute(() -> {
+                        try {
+                            ctx.cluster().get().state(ACTIVE_READ_ONLY);
+                        }
+                        catch (Throwable e) {
+                            U.error(
+                                log,
+                                "Failed to automatically switch state of the segmented cluster to the READ-ONLY mode" +
+                                    " [segmentedNodes=" + formatTopologyNodes(discoCache.allNodes()) + "]. Cache writes" +
+                                    " are already restricted for all configured caches, but this step is still required" +
+                                    " in order to be able to unlock cache writes in the future. Retry this operation" +
+                                    " manually, if possible.",
+                                e
+                            );
+                        }
+                    });
+
+                    U.warn(log, "Cluster segmentation was detected [segmentedNodes=" +
+                        formatTopologyNodes(discoCache.allNodes()) + ']');
+                }
+            }
+
+            if (ctx.state().isBaselineAutoAdjustEnabled())

Review comment:
       As I understand it, the problem with auto-adjustment is not that the baseline has not been set, but that it is being set before the resolver gets current baseline nodes.
   For example, `testConsequentSegmentationResolving` can be passed without disabling auto-adjustement if we set an auto-adjustement timeout `srv.cluster().BaselineAutoAdjustTimeout(1_000)`.
   
   So, I see 2 options here:
   1. get the previous baseline (before the last auto-adjustment that was caused by the top change, if possible) when getting the baseline nodes.
   2. correct the warning message that baseline auto-adjustment is enabled and the segmentation resolver may not work at all. This one seems strange because in in-memory cluster this is the default behavior.

##########
File path: modules/core/src/main/java/org/apache/ignite/cache/validation/IgnitePluggableSegmentationResolver.java
##########
@@ -0,0 +1,275 @@
+/*
+ * 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 org.apache.ignite.cache.validation;
+
+import java.io.Serializable;
+import java.util.Collection;
+import java.util.List;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.stream.Collectors;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.cluster.BaselineNode;
+import org.apache.ignite.cluster.ClusterNode;
+import org.apache.ignite.events.DiscoveryEvent;
+import org.apache.ignite.internal.GridKernalContext;
+import org.apache.ignite.internal.cluster.DetachedClusterNode;
+import org.apache.ignite.internal.managers.discovery.CustomEventListener;
+import org.apache.ignite.internal.managers.discovery.DiscoCache;
+import org.apache.ignite.internal.managers.eventstorage.DiscoveryEventListener;
+import org.apache.ignite.internal.managers.eventstorage.HighPriorityListener;
+import org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion;
+import org.apache.ignite.internal.processors.cluster.ChangeGlobalStateFinishMessage;
+import org.apache.ignite.internal.processors.configuration.distributed.DistributedConfigurationLifecycleListener;
+import org.apache.ignite.internal.processors.configuration.distributed.DistributedPropertyDispatcher;
+import org.apache.ignite.internal.processors.configuration.distributed.SimpleDistributedProperty;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.thread.IgniteThreadPoolExecutor;
+import org.apache.ignite.thread.OomExceptionHandler;
+
+import static java.lang.Boolean.TRUE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE_READ_ONLY;
+import static org.apache.ignite.configuration.IgniteConfiguration.DFLT_THREAD_KEEP_ALIVE_TIME;
+import static org.apache.ignite.events.EventType.EVT_NODE_FAILED;
+import static org.apache.ignite.events.EventType.EVT_NODE_JOINED;
+import static org.apache.ignite.events.EventType.EVT_NODE_LEFT;
+import static org.apache.ignite.internal.cluster.DistributedConfigurationUtils.setDefaultValue;
+import static org.apache.ignite.internal.managers.communication.GridIoPolicy.UNDEFINED;
+
+/** */
+public class IgnitePluggableSegmentationResolver implements PluggableSegmentationResolver {
+   /** */
+    public static final String ATTR_SEG_RESOLVER_CONFIGURED = "org.apache.ignite.segmentation.resolver.configured";
+
+    /** */
+    public static final String SEG_RESOLVER_ENABLED_PROP_NAME = "org.apache.ignite.segmentation.resolver.enabled";
+
+    /** */
+    private static final String SEG_RESOLVER_THREAD_PREFIX = "segmentation-resolver";
+
+    /** */
+    private static final int[] TOP_CHANGED_EVTS = new int[] {
+        EVT_NODE_LEFT,
+        EVT_NODE_JOINED,
+        EVT_NODE_FAILED
+    };
+
+    /** */
+    private final SimpleDistributedProperty<Boolean> segResolverEnabledProp = new SimpleDistributedProperty<>(
+        SEG_RESOLVER_ENABLED_PROP_NAME,
+        Boolean::parseBoolean
+    );
+
+    /** Ignite kernel context. */
+    private final GridKernalContext ctx;
+
+    /** Ignite logger. */
+    private final IgniteLogger log;
+
+    /** */
+    private final IgniteThreadPoolExecutor stateChangeExec;
+
+    /** */
+    private long lastCheckedTopVer;
+
+    /**  */
+    private volatile State state = State.VALID;
+
+    /** @param ctx Ignite kernel context. */
+    public IgnitePluggableSegmentationResolver(GridKernalContext ctx) {
+        this.ctx = ctx;
+
+        log = ctx.log(getClass());
+
+        stateChangeExec = new IgniteThreadPoolExecutor(
+            SEG_RESOLVER_THREAD_PREFIX,
+            ctx.igniteInstanceName(),
+            1,
+            1,
+            DFLT_THREAD_KEEP_ALIVE_TIME,
+            new LinkedBlockingQueue<>(),
+            UNDEFINED,
+            new OomExceptionHandler(ctx));
+
+        stateChangeExec.allowCoreThreadTimeOut(true);
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean isValidSegment() {
+        return isDisabled() || state != State.INVALID;
+    }
+
+    /** */
+    public void start() {
+        if (ctx.clientNode())
+            return;
+
+        ctx.addNodeAttribute(ATTR_SEG_RESOLVER_CONFIGURED, true);
+
+        ctx.event().addDiscoveryEventListener(new TopologyChangedEventListener(), TOP_CHANGED_EVTS);
+
+        ctx.discovery().setCustomEventListener(
+            ChangeGlobalStateFinishMessage.class,
+            new ClusterStateChangedEventListener()
+        );
+
+        ctx.internalSubscriptionProcessor().registerDistributedConfigurationListener(
+            new DistributedConfigurationLifecycleListener() {
+                /** {@inheritDoc} */
+                @Override public void onReadyToRegister(DistributedPropertyDispatcher dispatcher) {
+                    dispatcher.registerProperty(segResolverEnabledProp);
+                }
+
+                /** {@inheritDoc} */
+                @Override public void onReadyToWrite() {
+                    setDefaultValue(segResolverEnabledProp, U.isLocalNodeCoordinator(ctx.discovery()), log);
+                }
+            });
+    }
+
+    /** @return Discovery data. */
+    public Serializable provideDiscoveryData() {
+        return state;
+    }
+
+    /** @param data Discovery data. */
+    public void onDiscoveryDataReceived(Serializable data) {
+        state = (State)data;
+    }
+
+    /** @param node Node. */
+    public void validateNewNode(ClusterNode node) {
+        if (node.isClient())
+            return;
+
+        if (!TRUE.equals(node.attribute(ATTR_SEG_RESOLVER_CONFIGURED))) {
+            throw new IgniteException( "The Segmentation Resolver plugin is not configured for the server node that is" +
+                " trying to join the cluster. Since the Segmentation Resolver is only applicable if all server nodes" +
+                " in the cluster have one, node join request will be rejected [rejectedNodeId=" + node.id() + ']');
+        }
+
+        if (state == State.VALID) {
+            DiscoCache discoCache = ctx.discovery().discoCache(new AffinityTopologyVersion(lastCheckedTopVer, 0));
+
+            if (discoCache != null) {
+                for (ClusterNode srv : discoCache.serverNodes()) {
+                    if (!ctx.discovery().alive(srv))
+                        throw new IgniteException("Node join request will be rejected due to concurrent node left" +
+                            " process handling [rejectedNodeId=" + node.id() + ']');
+                }
+            }
+        }
+    }
+
+    /** */
+    private boolean isDisabled() {
+        Boolean res = segResolverEnabledProp.get();
+
+        return res == null || !res;
+    }
+
+    /** @return return. */
+    private String formatTopologyNodes(Collection<ClusterNode> nodes) {
+        return nodes.stream().map(n -> n.id().toString()).collect(Collectors.joining(", "));
+    }
+
+    /** */
+    private class TopologyChangedEventListener implements DiscoveryEventListener, HighPriorityListener {
+        /** {@inheritDoc} */
+        @Override public void onEvent(DiscoveryEvent evt, DiscoCache discoCache) {
+            lastCheckedTopVer = evt.topologyVersion();
+
+            if (isDisabled())
+                return;
+
+            if (state == State.VALID && evt.type() == EVT_NODE_FAILED) {

Review comment:
       When we change cluster state **to** read-only is treated as CLUSTER_WRITE_BLOCKED, but if we start the cluster in the read-only mode (`clusterStateOnStart` config property) it will be treated as "VALID" and this code block executes on segmentation.
   MB we should check the cluster state also.




-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [ignite] ololo3000 commented on a change in pull request #9534: wip

Posted by GitBox <gi...@apache.org>.
ololo3000 commented on a change in pull request #9534:
URL: https://github.com/apache/ignite/pull/9534#discussion_r741002238



##########
File path: modules/core/src/main/java/org/apache/ignite/cache/validation/IgnitePluggableSegmentationResolver.java
##########
@@ -0,0 +1,275 @@
+/*
+ * 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 org.apache.ignite.cache.validation;
+
+import java.io.Serializable;
+import java.util.Collection;
+import java.util.List;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.stream.Collectors;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.cluster.BaselineNode;
+import org.apache.ignite.cluster.ClusterNode;
+import org.apache.ignite.events.DiscoveryEvent;
+import org.apache.ignite.internal.GridKernalContext;
+import org.apache.ignite.internal.cluster.DetachedClusterNode;
+import org.apache.ignite.internal.managers.discovery.CustomEventListener;
+import org.apache.ignite.internal.managers.discovery.DiscoCache;
+import org.apache.ignite.internal.managers.eventstorage.DiscoveryEventListener;
+import org.apache.ignite.internal.managers.eventstorage.HighPriorityListener;
+import org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion;
+import org.apache.ignite.internal.processors.cluster.ChangeGlobalStateFinishMessage;
+import org.apache.ignite.internal.processors.configuration.distributed.DistributedConfigurationLifecycleListener;
+import org.apache.ignite.internal.processors.configuration.distributed.DistributedPropertyDispatcher;
+import org.apache.ignite.internal.processors.configuration.distributed.SimpleDistributedProperty;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.thread.IgniteThreadPoolExecutor;
+import org.apache.ignite.thread.OomExceptionHandler;
+
+import static java.lang.Boolean.TRUE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE_READ_ONLY;
+import static org.apache.ignite.configuration.IgniteConfiguration.DFLT_THREAD_KEEP_ALIVE_TIME;
+import static org.apache.ignite.events.EventType.EVT_NODE_FAILED;
+import static org.apache.ignite.events.EventType.EVT_NODE_JOINED;
+import static org.apache.ignite.events.EventType.EVT_NODE_LEFT;
+import static org.apache.ignite.internal.cluster.DistributedConfigurationUtils.setDefaultValue;
+import static org.apache.ignite.internal.managers.communication.GridIoPolicy.UNDEFINED;
+
+/** */
+public class IgnitePluggableSegmentationResolver implements PluggableSegmentationResolver {
+   /** */
+    public static final String ATTR_SEG_RESOLVER_CONFIGURED = "org.apache.ignite.segmentation.resolver.configured";
+
+    /** */
+    public static final String SEG_RESOLVER_ENABLED_PROP_NAME = "org.apache.ignite.segmentation.resolver.enabled";
+
+    /** */
+    private static final String SEG_RESOLVER_THREAD_PREFIX = "segmentation-resolver";
+
+    /** */
+    private static final int[] TOP_CHANGED_EVTS = new int[] {
+        EVT_NODE_LEFT,
+        EVT_NODE_JOINED,
+        EVT_NODE_FAILED
+    };
+
+    /** */
+    private final SimpleDistributedProperty<Boolean> segResolverEnabledProp = new SimpleDistributedProperty<>(
+        SEG_RESOLVER_ENABLED_PROP_NAME,
+        Boolean::parseBoolean
+    );
+
+    /** Ignite kernel context. */
+    private final GridKernalContext ctx;
+
+    /** Ignite logger. */
+    private final IgniteLogger log;
+
+    /** */
+    private final IgniteThreadPoolExecutor stateChangeExec;
+
+    /** */
+    private long lastCheckedTopVer;
+
+    /**  */
+    private volatile State state = State.VALID;
+
+    /** @param ctx Ignite kernel context. */
+    public IgnitePluggableSegmentationResolver(GridKernalContext ctx) {
+        this.ctx = ctx;
+
+        log = ctx.log(getClass());
+
+        stateChangeExec = new IgniteThreadPoolExecutor(
+            SEG_RESOLVER_THREAD_PREFIX,
+            ctx.igniteInstanceName(),
+            1,
+            1,
+            DFLT_THREAD_KEEP_ALIVE_TIME,
+            new LinkedBlockingQueue<>(),
+            UNDEFINED,
+            new OomExceptionHandler(ctx));
+
+        stateChangeExec.allowCoreThreadTimeOut(true);
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean isValidSegment() {
+        return isDisabled() || state != State.INVALID;
+    }
+
+    /** */
+    public void start() {
+        if (ctx.clientNode())
+            return;
+
+        ctx.addNodeAttribute(ATTR_SEG_RESOLVER_CONFIGURED, true);
+
+        ctx.event().addDiscoveryEventListener(new TopologyChangedEventListener(), TOP_CHANGED_EVTS);
+
+        ctx.discovery().setCustomEventListener(
+            ChangeGlobalStateFinishMessage.class,
+            new ClusterStateChangedEventListener()
+        );
+
+        ctx.internalSubscriptionProcessor().registerDistributedConfigurationListener(
+            new DistributedConfigurationLifecycleListener() {
+                /** {@inheritDoc} */
+                @Override public void onReadyToRegister(DistributedPropertyDispatcher dispatcher) {
+                    dispatcher.registerProperty(segResolverEnabledProp);
+                }
+
+                /** {@inheritDoc} */
+                @Override public void onReadyToWrite() {
+                    setDefaultValue(segResolverEnabledProp, U.isLocalNodeCoordinator(ctx.discovery()), log);
+                }
+            });
+    }
+
+    /** @return Discovery data. */
+    public Serializable provideDiscoveryData() {
+        return state;
+    }
+
+    /** @param data Discovery data. */
+    public void onDiscoveryDataReceived(Serializable data) {
+        state = (State)data;
+    }
+
+    /** @param node Node. */
+    public void validateNewNode(ClusterNode node) {
+        if (node.isClient())
+            return;
+
+        if (!TRUE.equals(node.attribute(ATTR_SEG_RESOLVER_CONFIGURED))) {
+            throw new IgniteException( "The Segmentation Resolver plugin is not configured for the server node that is" +
+                " trying to join the cluster. Since the Segmentation Resolver is only applicable if all server nodes" +
+                " in the cluster have one, node join request will be rejected [rejectedNodeId=" + node.id() + ']');
+        }
+
+        if (state == State.VALID) {
+            DiscoCache discoCache = ctx.discovery().discoCache(new AffinityTopologyVersion(lastCheckedTopVer, 0));
+
+            if (discoCache != null) {
+                for (ClusterNode srv : discoCache.serverNodes()) {
+                    if (!ctx.discovery().alive(srv))
+                        throw new IgniteException("Node join request will be rejected due to concurrent node left" +
+                            " process handling [rejectedNodeId=" + node.id() + ']');
+                }
+            }
+        }
+    }
+
+    /** */
+    private boolean isDisabled() {
+        Boolean res = segResolverEnabledProp.get();
+
+        return res == null || !res;
+    }
+
+    /** @return return. */
+    private String formatTopologyNodes(Collection<ClusterNode> nodes) {
+        return nodes.stream().map(n -> n.id().toString()).collect(Collectors.joining(", "));
+    }
+
+    /** */
+    private class TopologyChangedEventListener implements DiscoveryEventListener, HighPriorityListener {
+        /** {@inheritDoc} */
+        @Override public void onEvent(DiscoveryEvent evt, DiscoCache discoCache) {
+            lastCheckedTopVer = evt.topologyVersion();
+
+            if (isDisabled())
+                return;
+
+            if (state == State.VALID && evt.type() == EVT_NODE_FAILED) {
+                List<? extends BaselineNode> baselineNodes = discoCache.baselineNodes();
+
+                if (baselineNodes != null && aliveBaselineNodes(baselineNodes) < baselineNodes.size() / 2 + 1) {
+                    state = State.INVALID;
+
+                    stateChangeExec.execute(() -> {
+                        try {
+                            ctx.cluster().get().state(ACTIVE_READ_ONLY);
+                        }
+                        catch (Throwable e) {
+                            U.error(
+                                log,
+                                "Failed to automatically switch state of the segmented cluster to the READ-ONLY mode" +
+                                    " [segmentedNodes=" + formatTopologyNodes(discoCache.allNodes()) + "]. Cache writes" +
+                                    " are already restricted for all configured caches, but this step is still required" +
+                                    " in order to be able to unlock cache writes in the future. Retry this operation" +
+                                    " manually, if possible.",
+                                e
+                            );
+                        }
+                    });
+
+                    U.warn(log, "Cluster segmentation was detected [segmentedNodes=" +
+                        formatTopologyNodes(discoCache.allNodes()) + ']');
+                }
+            }
+
+            if (ctx.state().isBaselineAutoAdjustEnabled())

Review comment:
       .Done.

##########
File path: modules/core/src/main/java/org/apache/ignite/cache/validation/IgnitePluggableSegmentationResolver.java
##########
@@ -0,0 +1,275 @@
+/*
+ * 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 org.apache.ignite.cache.validation;
+
+import java.io.Serializable;
+import java.util.Collection;
+import java.util.List;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.stream.Collectors;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.cluster.BaselineNode;
+import org.apache.ignite.cluster.ClusterNode;
+import org.apache.ignite.events.DiscoveryEvent;
+import org.apache.ignite.internal.GridKernalContext;
+import org.apache.ignite.internal.cluster.DetachedClusterNode;
+import org.apache.ignite.internal.managers.discovery.CustomEventListener;
+import org.apache.ignite.internal.managers.discovery.DiscoCache;
+import org.apache.ignite.internal.managers.eventstorage.DiscoveryEventListener;
+import org.apache.ignite.internal.managers.eventstorage.HighPriorityListener;
+import org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion;
+import org.apache.ignite.internal.processors.cluster.ChangeGlobalStateFinishMessage;
+import org.apache.ignite.internal.processors.configuration.distributed.DistributedConfigurationLifecycleListener;
+import org.apache.ignite.internal.processors.configuration.distributed.DistributedPropertyDispatcher;
+import org.apache.ignite.internal.processors.configuration.distributed.SimpleDistributedProperty;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.thread.IgniteThreadPoolExecutor;
+import org.apache.ignite.thread.OomExceptionHandler;
+
+import static java.lang.Boolean.TRUE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE_READ_ONLY;
+import static org.apache.ignite.configuration.IgniteConfiguration.DFLT_THREAD_KEEP_ALIVE_TIME;
+import static org.apache.ignite.events.EventType.EVT_NODE_FAILED;
+import static org.apache.ignite.events.EventType.EVT_NODE_JOINED;
+import static org.apache.ignite.events.EventType.EVT_NODE_LEFT;
+import static org.apache.ignite.internal.cluster.DistributedConfigurationUtils.setDefaultValue;
+import static org.apache.ignite.internal.managers.communication.GridIoPolicy.UNDEFINED;
+
+/** */
+public class IgnitePluggableSegmentationResolver implements PluggableSegmentationResolver {
+   /** */
+    public static final String ATTR_SEG_RESOLVER_CONFIGURED = "org.apache.ignite.segmentation.resolver.configured";
+
+    /** */
+    public static final String SEG_RESOLVER_ENABLED_PROP_NAME = "org.apache.ignite.segmentation.resolver.enabled";
+
+    /** */
+    private static final String SEG_RESOLVER_THREAD_PREFIX = "segmentation-resolver";
+
+    /** */
+    private static final int[] TOP_CHANGED_EVTS = new int[] {
+        EVT_NODE_LEFT,
+        EVT_NODE_JOINED,
+        EVT_NODE_FAILED
+    };
+
+    /** */
+    private final SimpleDistributedProperty<Boolean> segResolverEnabledProp = new SimpleDistributedProperty<>(
+        SEG_RESOLVER_ENABLED_PROP_NAME,
+        Boolean::parseBoolean
+    );
+
+    /** Ignite kernel context. */
+    private final GridKernalContext ctx;
+
+    /** Ignite logger. */
+    private final IgniteLogger log;
+
+    /** */
+    private final IgniteThreadPoolExecutor stateChangeExec;
+
+    /** */
+    private long lastCheckedTopVer;
+
+    /**  */
+    private volatile State state = State.VALID;
+
+    /** @param ctx Ignite kernel context. */
+    public IgnitePluggableSegmentationResolver(GridKernalContext ctx) {
+        this.ctx = ctx;
+
+        log = ctx.log(getClass());
+
+        stateChangeExec = new IgniteThreadPoolExecutor(
+            SEG_RESOLVER_THREAD_PREFIX,
+            ctx.igniteInstanceName(),
+            1,
+            1,
+            DFLT_THREAD_KEEP_ALIVE_TIME,
+            new LinkedBlockingQueue<>(),
+            UNDEFINED,
+            new OomExceptionHandler(ctx));
+
+        stateChangeExec.allowCoreThreadTimeOut(true);
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean isValidSegment() {
+        return isDisabled() || state != State.INVALID;
+    }
+
+    /** */
+    public void start() {
+        if (ctx.clientNode())
+            return;
+
+        ctx.addNodeAttribute(ATTR_SEG_RESOLVER_CONFIGURED, true);
+
+        ctx.event().addDiscoveryEventListener(new TopologyChangedEventListener(), TOP_CHANGED_EVTS);
+
+        ctx.discovery().setCustomEventListener(
+            ChangeGlobalStateFinishMessage.class,
+            new ClusterStateChangedEventListener()
+        );
+
+        ctx.internalSubscriptionProcessor().registerDistributedConfigurationListener(
+            new DistributedConfigurationLifecycleListener() {
+                /** {@inheritDoc} */
+                @Override public void onReadyToRegister(DistributedPropertyDispatcher dispatcher) {
+                    dispatcher.registerProperty(segResolverEnabledProp);
+                }
+
+                /** {@inheritDoc} */
+                @Override public void onReadyToWrite() {
+                    setDefaultValue(segResolverEnabledProp, U.isLocalNodeCoordinator(ctx.discovery()), log);
+                }
+            });
+    }
+
+    /** @return Discovery data. */
+    public Serializable provideDiscoveryData() {
+        return state;
+    }
+
+    /** @param data Discovery data. */
+    public void onDiscoveryDataReceived(Serializable data) {
+        state = (State)data;
+    }
+
+    /** @param node Node. */
+    public void validateNewNode(ClusterNode node) {
+        if (node.isClient())
+            return;
+
+        if (!TRUE.equals(node.attribute(ATTR_SEG_RESOLVER_CONFIGURED))) {
+            throw new IgniteException( "The Segmentation Resolver plugin is not configured for the server node that is" +
+                " trying to join the cluster. Since the Segmentation Resolver is only applicable if all server nodes" +
+                " in the cluster have one, node join request will be rejected [rejectedNodeId=" + node.id() + ']');
+        }
+
+        if (state == State.VALID) {
+            DiscoCache discoCache = ctx.discovery().discoCache(new AffinityTopologyVersion(lastCheckedTopVer, 0));
+
+            if (discoCache != null) {
+                for (ClusterNode srv : discoCache.serverNodes()) {
+                    if (!ctx.discovery().alive(srv))
+                        throw new IgniteException("Node join request will be rejected due to concurrent node left" +
+                            " process handling [rejectedNodeId=" + node.id() + ']');
+                }
+            }
+        }
+    }
+
+    /** */
+    private boolean isDisabled() {
+        Boolean res = segResolverEnabledProp.get();
+
+        return res == null || !res;
+    }
+
+    /** @return return. */
+    private String formatTopologyNodes(Collection<ClusterNode> nodes) {

Review comment:
       Done.

##########
File path: modules/core/src/main/java/org/apache/ignite/cache/validation/IgnitePluggableSegmentationResolver.java
##########
@@ -0,0 +1,275 @@
+/*
+ * 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 org.apache.ignite.cache.validation;
+
+import java.io.Serializable;
+import java.util.Collection;
+import java.util.List;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.stream.Collectors;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.cluster.BaselineNode;
+import org.apache.ignite.cluster.ClusterNode;
+import org.apache.ignite.events.DiscoveryEvent;
+import org.apache.ignite.internal.GridKernalContext;
+import org.apache.ignite.internal.cluster.DetachedClusterNode;
+import org.apache.ignite.internal.managers.discovery.CustomEventListener;
+import org.apache.ignite.internal.managers.discovery.DiscoCache;
+import org.apache.ignite.internal.managers.eventstorage.DiscoveryEventListener;
+import org.apache.ignite.internal.managers.eventstorage.HighPriorityListener;
+import org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion;
+import org.apache.ignite.internal.processors.cluster.ChangeGlobalStateFinishMessage;
+import org.apache.ignite.internal.processors.configuration.distributed.DistributedConfigurationLifecycleListener;
+import org.apache.ignite.internal.processors.configuration.distributed.DistributedPropertyDispatcher;
+import org.apache.ignite.internal.processors.configuration.distributed.SimpleDistributedProperty;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.thread.IgniteThreadPoolExecutor;
+import org.apache.ignite.thread.OomExceptionHandler;
+
+import static java.lang.Boolean.TRUE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE_READ_ONLY;
+import static org.apache.ignite.configuration.IgniteConfiguration.DFLT_THREAD_KEEP_ALIVE_TIME;
+import static org.apache.ignite.events.EventType.EVT_NODE_FAILED;
+import static org.apache.ignite.events.EventType.EVT_NODE_JOINED;
+import static org.apache.ignite.events.EventType.EVT_NODE_LEFT;
+import static org.apache.ignite.internal.cluster.DistributedConfigurationUtils.setDefaultValue;
+import static org.apache.ignite.internal.managers.communication.GridIoPolicy.UNDEFINED;
+
+/** */
+public class IgnitePluggableSegmentationResolver implements PluggableSegmentationResolver {
+   /** */
+    public static final String ATTR_SEG_RESOLVER_CONFIGURED = "org.apache.ignite.segmentation.resolver.configured";
+
+    /** */
+    public static final String SEG_RESOLVER_ENABLED_PROP_NAME = "org.apache.ignite.segmentation.resolver.enabled";
+
+    /** */
+    private static final String SEG_RESOLVER_THREAD_PREFIX = "segmentation-resolver";
+
+    /** */
+    private static final int[] TOP_CHANGED_EVTS = new int[] {
+        EVT_NODE_LEFT,
+        EVT_NODE_JOINED,
+        EVT_NODE_FAILED
+    };
+
+    /** */
+    private final SimpleDistributedProperty<Boolean> segResolverEnabledProp = new SimpleDistributedProperty<>(
+        SEG_RESOLVER_ENABLED_PROP_NAME,
+        Boolean::parseBoolean
+    );
+
+    /** Ignite kernel context. */
+    private final GridKernalContext ctx;
+
+    /** Ignite logger. */
+    private final IgniteLogger log;
+
+    /** */
+    private final IgniteThreadPoolExecutor stateChangeExec;
+
+    /** */
+    private long lastCheckedTopVer;
+
+    /**  */
+    private volatile State state = State.VALID;
+
+    /** @param ctx Ignite kernel context. */
+    public IgnitePluggableSegmentationResolver(GridKernalContext ctx) {
+        this.ctx = ctx;
+
+        log = ctx.log(getClass());
+
+        stateChangeExec = new IgniteThreadPoolExecutor(
+            SEG_RESOLVER_THREAD_PREFIX,
+            ctx.igniteInstanceName(),
+            1,
+            1,
+            DFLT_THREAD_KEEP_ALIVE_TIME,
+            new LinkedBlockingQueue<>(),
+            UNDEFINED,
+            new OomExceptionHandler(ctx));
+
+        stateChangeExec.allowCoreThreadTimeOut(true);
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean isValidSegment() {
+        return isDisabled() || state != State.INVALID;
+    }
+
+    /** */
+    public void start() {
+        if (ctx.clientNode())
+            return;
+
+        ctx.addNodeAttribute(ATTR_SEG_RESOLVER_CONFIGURED, true);
+
+        ctx.event().addDiscoveryEventListener(new TopologyChangedEventListener(), TOP_CHANGED_EVTS);
+
+        ctx.discovery().setCustomEventListener(
+            ChangeGlobalStateFinishMessage.class,
+            new ClusterStateChangedEventListener()
+        );
+
+        ctx.internalSubscriptionProcessor().registerDistributedConfigurationListener(
+            new DistributedConfigurationLifecycleListener() {
+                /** {@inheritDoc} */
+                @Override public void onReadyToRegister(DistributedPropertyDispatcher dispatcher) {
+                    dispatcher.registerProperty(segResolverEnabledProp);
+                }
+
+                /** {@inheritDoc} */
+                @Override public void onReadyToWrite() {
+                    setDefaultValue(segResolverEnabledProp, U.isLocalNodeCoordinator(ctx.discovery()), log);
+                }
+            });
+    }
+
+    /** @return Discovery data. */
+    public Serializable provideDiscoveryData() {
+        return state;
+    }
+
+    /** @param data Discovery data. */
+    public void onDiscoveryDataReceived(Serializable data) {
+        state = (State)data;
+    }
+
+    /** @param node Node. */
+    public void validateNewNode(ClusterNode node) {
+        if (node.isClient())
+            return;
+
+        if (!TRUE.equals(node.attribute(ATTR_SEG_RESOLVER_CONFIGURED))) {
+            throw new IgniteException( "The Segmentation Resolver plugin is not configured for the server node that is" +
+                " trying to join the cluster. Since the Segmentation Resolver is only applicable if all server nodes" +
+                " in the cluster have one, node join request will be rejected [rejectedNodeId=" + node.id() + ']');
+        }
+
+        if (state == State.VALID) {
+            DiscoCache discoCache = ctx.discovery().discoCache(new AffinityTopologyVersion(lastCheckedTopVer, 0));
+
+            if (discoCache != null) {
+                for (ClusterNode srv : discoCache.serverNodes()) {
+                    if (!ctx.discovery().alive(srv))
+                        throw new IgniteException("Node join request will be rejected due to concurrent node left" +
+                            " process handling [rejectedNodeId=" + node.id() + ']');
+                }
+            }
+        }
+    }
+
+    /** */
+    private boolean isDisabled() {
+        Boolean res = segResolverEnabledProp.get();
+
+        return res == null || !res;
+    }
+
+    /** @return return. */
+    private String formatTopologyNodes(Collection<ClusterNode> nodes) {
+        return nodes.stream().map(n -> n.id().toString()).collect(Collectors.joining(", "));
+    }
+
+    /** */
+    private class TopologyChangedEventListener implements DiscoveryEventListener, HighPriorityListener {
+        /** {@inheritDoc} */
+        @Override public void onEvent(DiscoveryEvent evt, DiscoCache discoCache) {
+            lastCheckedTopVer = evt.topologyVersion();
+
+            if (isDisabled())
+                return;
+
+            if (state == State.VALID && evt.type() == EVT_NODE_FAILED) {

Review comment:
       Done.




-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [ignite] xtern commented on a change in pull request #9534: wip

Posted by GitBox <gi...@apache.org>.
xtern commented on a change in pull request #9534:
URL: https://github.com/apache/ignite/pull/9534#discussion_r752385094



##########
File path: modules/core/src/test/java/org/apache/ignite/internal/processors/cache/validation/IgniteTopologyValidatorTest.java
##########
@@ -0,0 +1,575 @@
+/*
+ * 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 org.apache.ignite.internal.processors.cache.validation;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.stream.Collectors;
+import org.apache.ignite.Ignite;
+import org.apache.ignite.IgniteCache;
+import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.cache.validation.IgniteCacheTopologyValidator;
+import org.apache.ignite.cache.validation.SegmentationResolverPluginProvider;
+import org.apache.ignite.cluster.ClusterNode;
+import org.apache.ignite.configuration.CacheConfiguration;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.internal.IgniteEx;
+import org.apache.ignite.internal.processors.cache.CacheInvalidStateException;
+import org.apache.ignite.internal.processors.cache.distributed.dht.IgniteCacheTopologySplitAbstractTest;
+import org.apache.ignite.internal.util.typedef.G;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.spi.IgniteSpiException;
+import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
+import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder;
+import org.apache.ignite.testframework.GridTestUtils.RunnableX;
+import org.junit.Test;
+
+import static java.util.Collections.singletonMap;
+import static java.util.stream.Collectors.toList;
+import static org.apache.ignite.cache.CacheMode.REPLICATED;
+import static org.apache.ignite.cache.CacheWriteSynchronizationMode.PRIMARY_SYNC;
+import static org.apache.ignite.cache.validation.IgnitePluggableSegmentationResolver.SEG_RESOLVER_ENABLED_PROP_NAME;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE_READ_ONLY;
+import static org.apache.ignite.events.EventType.EVT_NODE_JOINED;
+import static org.apache.ignite.internal.processors.cache.distributed.GridCacheModuloAffinityFunction.IDX_ATTR;
+import static org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi.DFLT_PORT;
+import static org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi.DFLT_PORT_RANGE;
+import static org.apache.ignite.testframework.GridTestUtils.assertThrowsAnyCause;
+import static org.apache.ignite.testframework.GridTestUtils.assertThrowsWithCause;
+import static org.apache.ignite.testframework.GridTestUtils.waitForCondition;
+
+/** */
+@SuppressWarnings("ThrowableNotThrown")
+public class IgniteTopologyValidatorTest extends IgniteCacheTopologySplitAbstractTest {
+    /** */
+    private static final int CACHE_KEY_CNT = 1000;
+
+    /** */
+    public static final int CACHE_CNT = 2;
+
+    /** */
+    @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception {
+        return getConfiguration(igniteInstanceName, true);
+    }
+
+    /** */
+    private IgniteConfiguration getConfiguration(
+        String igniteInstanceName,
+        boolean configureSegmentationResolverPlugin
+    ) throws Exception {
+        int idx = getTestIgniteInstanceIndex(igniteInstanceName);
+
+        IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName)
+            .setUserAttributes(singletonMap(IDX_ATTR, idx));
+
+        if (configureSegmentationResolverPlugin)
+            cfg.setPluginProviders(new SegmentationResolverPluginProvider());
+
+        ((TcpDiscoverySpi)cfg.getDiscoverySpi())
+            .setIpFinder(sharedStaticIpFinder)
+            .setLocalPortRange(1)
+            .setLocalPort(discoPort(idx))
+            .setConnectionRecoveryTimeout(0);
+
+        return cfg;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void afterTest() throws Exception {
+        super.afterTest();
+
+        stopAllGrids();
+    }
+
+    /** {@inheritDoc} */
+    @Override protected boolean isBlocked(int locPort, int rmtPort) {
+        return isDiscoPort(locPort) && isDiscoPort(rmtPort) && segment(locPort) != segment(rmtPort);
+    }
+
+    /**  */
+    private int segment(int discoPort) {
+        return (discoPort - DFLT_PORT) % 2 == 0 ? 0 : 1;
+    }
+
+    /** */
+    @Override public int segment(ClusterNode node) {
+        return node.<Integer>attribute(IDX_ATTR) % 2 == 0 ? 0 : 1;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected IgniteEx startGrid(IgniteConfiguration cfg) throws Exception {
+        return super.startGrid(optimize(cfg));
+    }
+
+    /** {@inheritDoc} */
+    @Override protected IgniteEx startClientGrid(IgniteConfiguration cfg) throws Exception {
+        return super.startClientGrid(optimize(cfg));

Review comment:
       Looks redundant, method "optimize" is invoked by default In startClientGrid.

##########
File path: modules/core/src/test/java/org/apache/ignite/internal/processors/cache/validation/IgniteTopologyValidatorTest.java
##########
@@ -0,0 +1,575 @@
+/*
+ * 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 org.apache.ignite.internal.processors.cache.validation;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.stream.Collectors;
+import org.apache.ignite.Ignite;
+import org.apache.ignite.IgniteCache;
+import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.cache.validation.IgniteCacheTopologyValidator;
+import org.apache.ignite.cache.validation.SegmentationResolverPluginProvider;
+import org.apache.ignite.cluster.ClusterNode;
+import org.apache.ignite.configuration.CacheConfiguration;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.internal.IgniteEx;
+import org.apache.ignite.internal.processors.cache.CacheInvalidStateException;
+import org.apache.ignite.internal.processors.cache.distributed.dht.IgniteCacheTopologySplitAbstractTest;
+import org.apache.ignite.internal.util.typedef.G;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.spi.IgniteSpiException;
+import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
+import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder;
+import org.apache.ignite.testframework.GridTestUtils.RunnableX;
+import org.junit.Test;
+
+import static java.util.Collections.singletonMap;
+import static java.util.stream.Collectors.toList;
+import static org.apache.ignite.cache.CacheMode.REPLICATED;
+import static org.apache.ignite.cache.CacheWriteSynchronizationMode.PRIMARY_SYNC;
+import static org.apache.ignite.cache.validation.IgnitePluggableSegmentationResolver.SEG_RESOLVER_ENABLED_PROP_NAME;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE_READ_ONLY;
+import static org.apache.ignite.events.EventType.EVT_NODE_JOINED;
+import static org.apache.ignite.internal.processors.cache.distributed.GridCacheModuloAffinityFunction.IDX_ATTR;
+import static org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi.DFLT_PORT;
+import static org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi.DFLT_PORT_RANGE;
+import static org.apache.ignite.testframework.GridTestUtils.assertThrowsAnyCause;
+import static org.apache.ignite.testframework.GridTestUtils.assertThrowsWithCause;
+import static org.apache.ignite.testframework.GridTestUtils.waitForCondition;
+
+/** */
+@SuppressWarnings("ThrowableNotThrown")
+public class IgniteTopologyValidatorTest extends IgniteCacheTopologySplitAbstractTest {
+    /** */
+    private static final int CACHE_KEY_CNT = 1000;
+
+    /** */
+    public static final int CACHE_CNT = 2;
+
+    /** */
+    @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception {
+        return getConfiguration(igniteInstanceName, true);
+    }
+
+    /** */
+    private IgniteConfiguration getConfiguration(
+        String igniteInstanceName,
+        boolean configureSegmentationResolverPlugin
+    ) throws Exception {
+        int idx = getTestIgniteInstanceIndex(igniteInstanceName);
+
+        IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName)
+            .setUserAttributes(singletonMap(IDX_ATTR, idx));
+
+        if (configureSegmentationResolverPlugin)
+            cfg.setPluginProviders(new SegmentationResolverPluginProvider());
+
+        ((TcpDiscoverySpi)cfg.getDiscoverySpi())
+            .setIpFinder(sharedStaticIpFinder)
+            .setLocalPortRange(1)
+            .setLocalPort(discoPort(idx))
+            .setConnectionRecoveryTimeout(0);
+
+        return cfg;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void afterTest() throws Exception {
+        super.afterTest();
+
+        stopAllGrids();
+    }
+
+    /** {@inheritDoc} */
+    @Override protected boolean isBlocked(int locPort, int rmtPort) {
+        return isDiscoPort(locPort) && isDiscoPort(rmtPort) && segment(locPort) != segment(rmtPort);
+    }
+
+    /**  */
+    private int segment(int discoPort) {
+        return (discoPort - DFLT_PORT) % 2 == 0 ? 0 : 1;
+    }
+
+    /** */
+    @Override public int segment(ClusterNode node) {
+        return node.<Integer>attribute(IDX_ATTR) % 2 == 0 ? 0 : 1;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected IgniteEx startGrid(IgniteConfiguration cfg) throws Exception {
+        return super.startGrid(optimize(cfg));
+    }
+
+    /** {@inheritDoc} */
+    @Override protected IgniteEx startClientGrid(IgniteConfiguration cfg) throws Exception {
+        return super.startClientGrid(optimize(cfg));
+    }
+
+    /** */
+    @Test
+    public void testConnectionToIncompatibleCluster() throws Exception {
+        startGrid(getConfiguration(getTestIgniteInstanceName(0), false));
+
+        startGrid(1);
+
+        grid(0).cluster().baselineAutoAdjustEnabled(false);
+
+        assertTrue(waitForCondition(
+            () -> !(Boolean)grid(1).context().distributedConfiguration().property(SEG_RESOLVER_ENABLED_PROP_NAME).get(),
+            getTestTimeout()
+        ));
+
+        splitAndWait();
+
+        connectNodeToSegment(3, 1);
+
+        assertTrue(waitForCondition(
+            () -> !(Boolean)grid(1).context().distributedConfiguration().property(SEG_RESOLVER_ENABLED_PROP_NAME).get(),
+            getTestTimeout()
+        ));
+    }
+
+    /** */
+    @Test
+    public void testIncompatibleNodeConnection() throws Exception {
+        IgniteEx srv = startGrid(0);
+
+        assertThrowsAnyCause(
+            log,
+            () -> startGrid(getConfiguration(getTestIgniteInstanceName(1), false)),
+            IgniteSpiException.class,
+            "The Segmentation Resolver plugin is not configured for the server node that is trying to join the cluster."
+        );
+
+        startClientGrid(getConfiguration(getTestIgniteInstanceName(2), false));
+
+        assertEquals(2, srv.cluster().nodes().size());
+    }
+
+    /** */
+    @Test
+    public void testCacheCreationWithSegmentationResolverMissed() throws Exception {
+        IgniteEx srv = startGrid(getConfiguration(getTestIgniteInstanceName(0), false));
+
+        assertThrowsWithCause(
+            () -> srv.createCache(new CacheConfiguration<>(DEFAULT_CACHE_NAME)
+                .setTopologyValidator(new IgniteCacheTopologyValidator())),
+            IgniteCheckedException.class
+        );
+    }
+
+    /** */
+    @Test
+    public void testCacheCreationWithSegmentationResolverMissedOnClient() throws Exception {
+        startGrid(0);
+
+        IgniteEx cli = startClientGrid(getConfiguration(getTestIgniteInstanceName(1), false));
+
+        cli.createCache(new CacheConfiguration<>(DEFAULT_CACHE_NAME)
+            .setTopologyValidator(new IgniteCacheTopologyValidator()));
+    }
+
+    /** */
+    @Test
+    public void testConnectionToSegmentedCluster() throws Exception {
+        startGridsMultiThreaded(6);
+
+        grid(0).cluster().baselineAutoAdjustEnabled(false);
+
+        createCaches();
+
+        stopGrid(4);
+        stopGrid(5);
+
+        splitAndWait();
+
+        checkPutGet(G.allGrids(), false);
+
+        connectNodeToSegment(4, 0);
+        checkPutGet(0, false);
+
+        connectNodeToSegment(5, 1);
+        checkPutGet(1, false);
+
+        stopSegmentNodes(1);
+
+        unsplit();
+
+        startGrid(1);
+
+        checkPutGet(G.allGrids(), false);
+    }
+
+    /** */
+    @Test
+    public void testRegularNodeStartStop() throws Exception {
+        startGrid(0);
+
+        createCaches();
+
+        checkPutGetAfter(() -> startGrid(1));
+        checkPutGetAfter(() -> stopGrid(1));
+
+        checkPutGetAfter(() -> startClientGrid(2));
+        checkPutGetAfter(() -> stopGrid(2));
+
+        checkPutGetAfter(() -> startGrid(1));
+
+        grid(0).cluster().baselineAutoAdjustEnabled(false);
+
+        checkPutGetAfter(() -> startGrid(3));
+        checkPutGetAfter(() -> stopGrid(3));
+
+        checkPutGetAfter(() -> stopGrid(1));
+
+        checkPutGetAfter(() -> startClientGrid(2));
+        checkPutGetAfter(() -> stopGrid(2));
+    }
+
+    /** */
+    @Test
+    public void testClientNodeSegmentationIgnored() throws Exception {
+        IgniteEx srv = startGrid(0);
+
+        startClientGrid(1);
+
+        srv.cluster().baselineAutoAdjustEnabled(false);
+
+        createCaches();
+
+        failNode(1, Collections.singleton(srv));
+
+        checkPutGet(Collections.singleton(srv), true);
+    }
+
+    /** */
+    @Test
+    public void testSplitWithoutBaseline() throws Exception {
+        startGridsMultiThreaded(3);
+
+        createCaches();
+
+        startGrid(3);
+
+        splitAndWait();
+
+        checkPutGet(G.allGrids(), true);
+
+        stopSegmentNodes(1);
+
+        unsplit();
+
+        grid(0).cluster().state(ACTIVE_READ_ONLY);
+
+        grid(0).cluster().state(ACTIVE);
+
+        checkPutGet(G.allGrids(), true);
+    }
+
+    /** */
+    @Test
+    public void testSplitWithBaseline() throws Exception {
+        startGridsMultiThreaded(3);
+
+        grid(0).cluster().baselineAutoAdjustEnabled(false);
+
+        createCaches();
+
+        startGrid(3);
+
+        splitAndWait();
+
+        checkPutGet(0, true);
+        checkPutGet(1, false);
+
+        assertTrue(waitForCondition(() -> ACTIVE_READ_ONLY == grid(1).cluster().state(), getTestTimeout()));
+
+        stopSegmentNodes(1);
+
+        unsplit();
+
+        startGrid(1);
+        startGrid(3);
+
+        grid(0).cluster().setBaselineTopology(grid(0).cluster().topologyVersion());
+
+        splitAndWait();
+
+        checkPutGet(G.allGrids(), false);
+
+        assertTrue(waitForCondition(() -> ACTIVE_READ_ONLY == grid(1).cluster().state(), getTestTimeout()));
+        assertTrue(waitForCondition(() -> ACTIVE_READ_ONLY == grid(0).cluster().state(), getTestTimeout()));
+
+        grid(0).cluster().state(ACTIVE);
+
+        checkPutGet(0, true);
+        checkPutGet(1, false);
+    }
+
+    /** */
+    @Test
+    public void testConsequentSegmentationResolving() throws Exception {
+        startGridsMultiThreaded(4);
+
+        grid(0).cluster().baselineAutoAdjustEnabled(false);
+
+        createCaches();
+
+        splitAndWait();
+
+        checkPutGet(G.allGrids(), false);
+
+        grid(1).cluster().state(ACTIVE);
+
+        checkPutGet(0, false);
+        checkPutGet(1, true);
+
+        stopSegmentNodes(0);
+
+        unsplit();
+
+        failNode(1, Collections.singleton(grid(3)));
+
+        checkPutGet(Collections.singleton(grid(3)), false);
+
+        grid(3).cluster().state(ACTIVE);
+
+        checkPutGet(Collections.singleton(grid(3)), true);
+    }
+
+    /** */
+    @Test
+    public void testEnableProperty() throws Exception {
+        startGridsMultiThreaded(4);
+
+        grid(0).cluster().baselineAutoAdjustEnabled(false);
+
+        createCaches();
+
+        grid(1).context().distributedConfiguration().property(SEG_RESOLVER_ENABLED_PROP_NAME).propagate(false);
+
+        splitAndWait();
+
+        checkPutGet(G.allGrids(), true);
+
+        stopSegmentNodes(0);
+
+        unsplit();
+
+        grid(1).context().distributedConfiguration().property(SEG_RESOLVER_ENABLED_PROP_NAME).propagate(true);
+
+        failNode(1, Collections.singleton(grid(3)));
+
+        checkPutGet(Collections.singleton(grid(3)), false);
+    }
+
+    /** */
+    @Test
+    public void testNodeJoinWithHalfBaselineNodesLeft() throws Exception {
+        startGridsMultiThreaded(4);
+
+        grid(0).cluster().baselineAutoAdjustEnabled(false);
+
+        createCaches();
+
+        stopGrid(0);
+        stopGrid(1);
+        stopGrid(2);
+
+        checkPutGet(G.allGrids(), true);
+
+        startGrid(0);
+
+        checkPutGet(G.allGrids(), true);
+    }
+
+    /** */
+    @Test
+    public void testNodeJoinConcurrentWithLeftRejected() throws Exception {
+        IgniteEx srv = startGrids(2);
+
+        grid(0).cluster().baselineAutoAdjustEnabled(false);
+
+        createCaches();
+
+        CountDownLatch discoveryWorkerBlockedLatch = new CountDownLatch(1);
+
+        try {
+            srv.events().localListen(evt -> {
+                try {
+                    discoveryWorkerBlockedLatch.await();
+                }
+                catch (InterruptedException e) {
+                    U.error(log, e);
+                }
+
+                return true;
+            }, EVT_NODE_JOINED);
+
+            startGrid(2);
+
+            stopGrid(1);
+
+            assertThrowsAnyCause(
+                log,
+                () -> startGrid(3),
+                IgniteSpiException.class,
+                "Node join request will be rejected due to concurrent node left process handling"
+            );
+        }
+        finally {
+            discoveryWorkerBlockedLatch.countDown();
+        }
+    }
+
+    /** */
+    @Test
+    public void testPreconfiguredClusterState() throws Exception {
+        startGrid(0);
+
+        startGrid(getConfiguration(getTestIgniteInstanceName(1)).setClusterStateOnStart(ACTIVE_READ_ONLY));
+
+        grid(0).cluster().baselineAutoAdjustEnabled(false);
+
+        createCaches();
+
+        splitAndWait();
+
+        checkPutGet(G.allGrids(), false);
+    }
+
+    /** */
+    private IgniteEx connectNodeToSegment(int nodeIdx, int segment) throws Exception {
+        IgniteConfiguration cfg = getConfiguration(getTestIgniteInstanceName(nodeIdx));
+
+        List<String> segmentDiscoPorts = segmentNodes(segment, false).stream()
+            .map(node -> "127.0.0.1:" + discoPort(node.localNode().<Integer>attribute(IDX_ATTR)))
+            .collect(toList());
+
+        ((TcpDiscoverySpi)cfg.getDiscoverySpi()).setIpFinder(new TcpDiscoveryVmIpFinder().setAddresses(segmentDiscoPorts));
+
+        return startGrid(optimize(cfg));

Review comment:
       optimize already invoked in startGrid.

##########
File path: modules/core/src/test/java/org/apache/ignite/internal/processors/cache/validation/IgniteTopologyValidatorTest.java
##########
@@ -0,0 +1,575 @@
+/*
+ * 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 org.apache.ignite.internal.processors.cache.validation;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.stream.Collectors;
+import org.apache.ignite.Ignite;
+import org.apache.ignite.IgniteCache;
+import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.cache.validation.IgniteCacheTopologyValidator;
+import org.apache.ignite.cache.validation.SegmentationResolverPluginProvider;
+import org.apache.ignite.cluster.ClusterNode;
+import org.apache.ignite.configuration.CacheConfiguration;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.internal.IgniteEx;
+import org.apache.ignite.internal.processors.cache.CacheInvalidStateException;
+import org.apache.ignite.internal.processors.cache.distributed.dht.IgniteCacheTopologySplitAbstractTest;
+import org.apache.ignite.internal.util.typedef.G;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.spi.IgniteSpiException;
+import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
+import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder;
+import org.apache.ignite.testframework.GridTestUtils.RunnableX;
+import org.junit.Test;
+
+import static java.util.Collections.singletonMap;
+import static java.util.stream.Collectors.toList;
+import static org.apache.ignite.cache.CacheMode.REPLICATED;
+import static org.apache.ignite.cache.CacheWriteSynchronizationMode.PRIMARY_SYNC;
+import static org.apache.ignite.cache.validation.IgnitePluggableSegmentationResolver.SEG_RESOLVER_ENABLED_PROP_NAME;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE_READ_ONLY;
+import static org.apache.ignite.events.EventType.EVT_NODE_JOINED;
+import static org.apache.ignite.internal.processors.cache.distributed.GridCacheModuloAffinityFunction.IDX_ATTR;
+import static org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi.DFLT_PORT;
+import static org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi.DFLT_PORT_RANGE;
+import static org.apache.ignite.testframework.GridTestUtils.assertThrowsAnyCause;
+import static org.apache.ignite.testframework.GridTestUtils.assertThrowsWithCause;
+import static org.apache.ignite.testframework.GridTestUtils.waitForCondition;
+
+/** */
+@SuppressWarnings("ThrowableNotThrown")
+public class IgniteTopologyValidatorTest extends IgniteCacheTopologySplitAbstractTest {
+    /** */
+    private static final int CACHE_KEY_CNT = 1000;
+
+    /** */
+    public static final int CACHE_CNT = 2;
+
+    /** */
+    @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception {
+        return getConfiguration(igniteInstanceName, true);
+    }
+
+    /** */
+    private IgniteConfiguration getConfiguration(
+        String igniteInstanceName,
+        boolean configureSegmentationResolverPlugin
+    ) throws Exception {
+        int idx = getTestIgniteInstanceIndex(igniteInstanceName);
+
+        IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName)
+            .setUserAttributes(singletonMap(IDX_ATTR, idx));
+
+        if (configureSegmentationResolverPlugin)
+            cfg.setPluginProviders(new SegmentationResolverPluginProvider());
+
+        ((TcpDiscoverySpi)cfg.getDiscoverySpi())
+            .setIpFinder(sharedStaticIpFinder)
+            .setLocalPortRange(1)
+            .setLocalPort(discoPort(idx))
+            .setConnectionRecoveryTimeout(0);
+
+        return cfg;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void afterTest() throws Exception {
+        super.afterTest();
+
+        stopAllGrids();
+    }
+
+    /** {@inheritDoc} */
+    @Override protected boolean isBlocked(int locPort, int rmtPort) {
+        return isDiscoPort(locPort) && isDiscoPort(rmtPort) && segment(locPort) != segment(rmtPort);
+    }
+
+    /**  */
+    private int segment(int discoPort) {
+        return (discoPort - DFLT_PORT) % 2 == 0 ? 0 : 1;
+    }
+
+    /** */
+    @Override public int segment(ClusterNode node) {
+        return node.<Integer>attribute(IDX_ATTR) % 2 == 0 ? 0 : 1;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected IgniteEx startGrid(IgniteConfiguration cfg) throws Exception {
+        return super.startGrid(optimize(cfg));

Review comment:
       Looks redundant, method "optimize" is invoked by default In startGrid.

##########
File path: modules/core/src/test/java/org/apache/ignite/internal/processors/cache/validation/IgniteTopologyValidatorTest.java
##########
@@ -0,0 +1,575 @@
+/*
+ * 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 org.apache.ignite.internal.processors.cache.validation;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.stream.Collectors;
+import org.apache.ignite.Ignite;
+import org.apache.ignite.IgniteCache;
+import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.cache.validation.IgniteCacheTopologyValidator;
+import org.apache.ignite.cache.validation.SegmentationResolverPluginProvider;
+import org.apache.ignite.cluster.ClusterNode;
+import org.apache.ignite.configuration.CacheConfiguration;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.internal.IgniteEx;
+import org.apache.ignite.internal.processors.cache.CacheInvalidStateException;
+import org.apache.ignite.internal.processors.cache.distributed.dht.IgniteCacheTopologySplitAbstractTest;
+import org.apache.ignite.internal.util.typedef.G;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.spi.IgniteSpiException;
+import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
+import org.apache.ignite.spi.discovery.tcp.ipfinder.vm.TcpDiscoveryVmIpFinder;
+import org.apache.ignite.testframework.GridTestUtils.RunnableX;
+import org.junit.Test;
+
+import static java.util.Collections.singletonMap;
+import static java.util.stream.Collectors.toList;
+import static org.apache.ignite.cache.CacheMode.REPLICATED;
+import static org.apache.ignite.cache.CacheWriteSynchronizationMode.PRIMARY_SYNC;
+import static org.apache.ignite.cache.validation.IgnitePluggableSegmentationResolver.SEG_RESOLVER_ENABLED_PROP_NAME;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE_READ_ONLY;
+import static org.apache.ignite.events.EventType.EVT_NODE_JOINED;
+import static org.apache.ignite.internal.processors.cache.distributed.GridCacheModuloAffinityFunction.IDX_ATTR;
+import static org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi.DFLT_PORT;
+import static org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi.DFLT_PORT_RANGE;
+import static org.apache.ignite.testframework.GridTestUtils.assertThrowsAnyCause;
+import static org.apache.ignite.testframework.GridTestUtils.assertThrowsWithCause;
+import static org.apache.ignite.testframework.GridTestUtils.waitForCondition;
+
+/** */
+@SuppressWarnings("ThrowableNotThrown")
+public class IgniteTopologyValidatorTest extends IgniteCacheTopologySplitAbstractTest {
+    /** */
+    private static final int CACHE_KEY_CNT = 1000;
+
+    /** */
+    public static final int CACHE_CNT = 2;
+
+    /** */
+    @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception {
+        return getConfiguration(igniteInstanceName, true);
+    }
+
+    /** */
+    private IgniteConfiguration getConfiguration(
+        String igniteInstanceName,
+        boolean configureSegmentationResolverPlugin
+    ) throws Exception {
+        int idx = getTestIgniteInstanceIndex(igniteInstanceName);
+
+        IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName)
+            .setUserAttributes(singletonMap(IDX_ATTR, idx));
+
+        if (configureSegmentationResolverPlugin)
+            cfg.setPluginProviders(new SegmentationResolverPluginProvider());
+
+        ((TcpDiscoverySpi)cfg.getDiscoverySpi())
+            .setIpFinder(sharedStaticIpFinder)
+            .setLocalPortRange(1)
+            .setLocalPort(discoPort(idx))
+            .setConnectionRecoveryTimeout(0);
+
+        return cfg;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void afterTest() throws Exception {
+        super.afterTest();
+
+        stopAllGrids();
+    }
+
+    /** {@inheritDoc} */
+    @Override protected boolean isBlocked(int locPort, int rmtPort) {
+        return isDiscoPort(locPort) && isDiscoPort(rmtPort) && segment(locPort) != segment(rmtPort);
+    }
+
+    /**  */
+    private int segment(int discoPort) {
+        return (discoPort - DFLT_PORT) % 2 == 0 ? 0 : 1;
+    }
+
+    /** */
+    @Override public int segment(ClusterNode node) {
+        return node.<Integer>attribute(IDX_ATTR) % 2 == 0 ? 0 : 1;
+    }
+
+    /** {@inheritDoc} */
+    @Override protected IgniteEx startGrid(IgniteConfiguration cfg) throws Exception {
+        return super.startGrid(optimize(cfg));
+    }
+
+    /** {@inheritDoc} */
+    @Override protected IgniteEx startClientGrid(IgniteConfiguration cfg) throws Exception {
+        return super.startClientGrid(optimize(cfg));
+    }
+
+    /** */
+    @Test
+    public void testConnectionToIncompatibleCluster() throws Exception {
+        startGrid(getConfiguration(getTestIgniteInstanceName(0), false));
+
+        startGrid(1);
+
+        grid(0).cluster().baselineAutoAdjustEnabled(false);
+
+        assertTrue(waitForCondition(
+            () -> !(Boolean)grid(1).context().distributedConfiguration().property(SEG_RESOLVER_ENABLED_PROP_NAME).get(),
+            getTestTimeout()
+        ));
+
+        splitAndWait();
+
+        connectNodeToSegment(3, 1);
+
+        assertTrue(waitForCondition(
+            () -> !(Boolean)grid(1).context().distributedConfiguration().property(SEG_RESOLVER_ENABLED_PROP_NAME).get(),
+            getTestTimeout()
+        ));
+    }
+
+    /** */
+    @Test
+    public void testIncompatibleNodeConnection() throws Exception {
+        IgniteEx srv = startGrid(0);
+
+        assertThrowsAnyCause(
+            log,
+            () -> startGrid(getConfiguration(getTestIgniteInstanceName(1), false)),
+            IgniteSpiException.class,
+            "The Segmentation Resolver plugin is not configured for the server node that is trying to join the cluster."
+        );
+
+        startClientGrid(getConfiguration(getTestIgniteInstanceName(2), false));
+
+        assertEquals(2, srv.cluster().nodes().size());
+    }
+
+    /** */
+    @Test
+    public void testCacheCreationWithSegmentationResolverMissed() throws Exception {
+        IgniteEx srv = startGrid(getConfiguration(getTestIgniteInstanceName(0), false));
+
+        assertThrowsWithCause(
+            () -> srv.createCache(new CacheConfiguration<>(DEFAULT_CACHE_NAME)
+                .setTopologyValidator(new IgniteCacheTopologyValidator())),
+            IgniteCheckedException.class
+        );
+    }
+
+    /** */
+    @Test
+    public void testCacheCreationWithSegmentationResolverMissedOnClient() throws Exception {
+        startGrid(0);
+
+        IgniteEx cli = startClientGrid(getConfiguration(getTestIgniteInstanceName(1), false));
+
+        cli.createCache(new CacheConfiguration<>(DEFAULT_CACHE_NAME)
+            .setTopologyValidator(new IgniteCacheTopologyValidator()));
+    }
+
+    /** */
+    @Test
+    public void testConnectionToSegmentedCluster() throws Exception {
+        startGridsMultiThreaded(6);
+
+        grid(0).cluster().baselineAutoAdjustEnabled(false);
+
+        createCaches();
+
+        stopGrid(4);
+        stopGrid(5);
+
+        splitAndWait();
+
+        checkPutGet(G.allGrids(), false);
+
+        connectNodeToSegment(4, 0);
+        checkPutGet(0, false);
+
+        connectNodeToSegment(5, 1);
+        checkPutGet(1, false);
+
+        stopSegmentNodes(1);
+
+        unsplit();
+
+        startGrid(1);
+
+        checkPutGet(G.allGrids(), false);
+    }
+
+    /** */
+    @Test
+    public void testRegularNodeStartStop() throws Exception {
+        startGrid(0);
+
+        createCaches();
+
+        checkPutGetAfter(() -> startGrid(1));
+        checkPutGetAfter(() -> stopGrid(1));
+
+        checkPutGetAfter(() -> startClientGrid(2));
+        checkPutGetAfter(() -> stopGrid(2));
+
+        checkPutGetAfter(() -> startGrid(1));
+
+        grid(0).cluster().baselineAutoAdjustEnabled(false);
+
+        checkPutGetAfter(() -> startGrid(3));
+        checkPutGetAfter(() -> stopGrid(3));
+
+        checkPutGetAfter(() -> stopGrid(1));
+
+        checkPutGetAfter(() -> startClientGrid(2));
+        checkPutGetAfter(() -> stopGrid(2));
+    }
+
+    /** */
+    @Test
+    public void testClientNodeSegmentationIgnored() throws Exception {
+        IgniteEx srv = startGrid(0);
+
+        startClientGrid(1);
+
+        srv.cluster().baselineAutoAdjustEnabled(false);
+
+        createCaches();
+
+        failNode(1, Collections.singleton(srv));
+
+        checkPutGet(Collections.singleton(srv), true);
+    }
+
+    /** */
+    @Test
+    public void testSplitWithoutBaseline() throws Exception {
+        startGridsMultiThreaded(3);
+
+        createCaches();
+
+        startGrid(3);
+
+        splitAndWait();
+
+        checkPutGet(G.allGrids(), true);
+
+        stopSegmentNodes(1);
+
+        unsplit();
+
+        grid(0).cluster().state(ACTIVE_READ_ONLY);
+
+        grid(0).cluster().state(ACTIVE);
+
+        checkPutGet(G.allGrids(), true);
+    }
+
+    /** */
+    @Test
+    public void testSplitWithBaseline() throws Exception {
+        startGridsMultiThreaded(3);
+
+        grid(0).cluster().baselineAutoAdjustEnabled(false);
+
+        createCaches();
+
+        startGrid(3);
+
+        splitAndWait();
+
+        checkPutGet(0, true);
+        checkPutGet(1, false);
+
+        assertTrue(waitForCondition(() -> ACTIVE_READ_ONLY == grid(1).cluster().state(), getTestTimeout()));
+
+        stopSegmentNodes(1);
+
+        unsplit();
+
+        startGrid(1);
+        startGrid(3);
+
+        grid(0).cluster().setBaselineTopology(grid(0).cluster().topologyVersion());
+
+        splitAndWait();
+
+        checkPutGet(G.allGrids(), false);
+
+        assertTrue(waitForCondition(() -> ACTIVE_READ_ONLY == grid(1).cluster().state(), getTestTimeout()));
+        assertTrue(waitForCondition(() -> ACTIVE_READ_ONLY == grid(0).cluster().state(), getTestTimeout()));
+
+        grid(0).cluster().state(ACTIVE);
+
+        checkPutGet(0, true);
+        checkPutGet(1, false);
+    }
+
+    /** */
+    @Test
+    public void testConsequentSegmentationResolving() throws Exception {
+        startGridsMultiThreaded(4);
+
+        grid(0).cluster().baselineAutoAdjustEnabled(false);
+
+        createCaches();
+
+        splitAndWait();
+
+        checkPutGet(G.allGrids(), false);
+
+        grid(1).cluster().state(ACTIVE);
+
+        checkPutGet(0, false);
+        checkPutGet(1, true);
+
+        stopSegmentNodes(0);
+
+        unsplit();
+
+        failNode(1, Collections.singleton(grid(3)));
+
+        checkPutGet(Collections.singleton(grid(3)), false);
+
+        grid(3).cluster().state(ACTIVE);
+
+        checkPutGet(Collections.singleton(grid(3)), true);
+    }
+
+    /** */
+    @Test
+    public void testEnableProperty() throws Exception {
+        startGridsMultiThreaded(4);
+
+        grid(0).cluster().baselineAutoAdjustEnabled(false);
+
+        createCaches();
+
+        grid(1).context().distributedConfiguration().property(SEG_RESOLVER_ENABLED_PROP_NAME).propagate(false);
+
+        splitAndWait();
+
+        checkPutGet(G.allGrids(), true);
+
+        stopSegmentNodes(0);
+
+        unsplit();
+
+        grid(1).context().distributedConfiguration().property(SEG_RESOLVER_ENABLED_PROP_NAME).propagate(true);
+
+        failNode(1, Collections.singleton(grid(3)));
+
+        checkPutGet(Collections.singleton(grid(3)), false);
+    }
+
+    /** */
+    @Test
+    public void testNodeJoinWithHalfBaselineNodesLeft() throws Exception {
+        startGridsMultiThreaded(4);
+
+        grid(0).cluster().baselineAutoAdjustEnabled(false);
+
+        createCaches();
+
+        stopGrid(0);
+        stopGrid(1);
+        stopGrid(2);
+
+        checkPutGet(G.allGrids(), true);
+
+        startGrid(0);
+
+        checkPutGet(G.allGrids(), true);
+    }
+
+    /** */
+    @Test
+    public void testNodeJoinConcurrentWithLeftRejected() throws Exception {
+        IgniteEx srv = startGrids(2);
+
+        grid(0).cluster().baselineAutoAdjustEnabled(false);
+
+        createCaches();
+
+        CountDownLatch discoveryWorkerBlockedLatch = new CountDownLatch(1);
+
+        try {
+            srv.events().localListen(evt -> {
+                try {
+                    discoveryWorkerBlockedLatch.await();
+                }
+                catch (InterruptedException e) {
+                    U.error(log, e);
+                }
+
+                return true;
+            }, EVT_NODE_JOINED);
+
+            startGrid(2);
+
+            stopGrid(1);
+
+            assertThrowsAnyCause(
+                log,
+                () -> startGrid(3),
+                IgniteSpiException.class,
+                "Node join request will be rejected due to concurrent node left process handling"
+            );
+        }
+        finally {
+            discoveryWorkerBlockedLatch.countDown();
+        }
+    }
+
+    /** */
+    @Test
+    public void testPreconfiguredClusterState() throws Exception {
+        startGrid(0);
+
+        startGrid(getConfiguration(getTestIgniteInstanceName(1)).setClusterStateOnStart(ACTIVE_READ_ONLY));
+
+        grid(0).cluster().baselineAutoAdjustEnabled(false);
+
+        createCaches();
+
+        splitAndWait();
+
+        checkPutGet(G.allGrids(), false);
+    }
+
+    /** */
+    private IgniteEx connectNodeToSegment(int nodeIdx, int segment) throws Exception {
+        IgniteConfiguration cfg = getConfiguration(getTestIgniteInstanceName(nodeIdx));
+
+        List<String> segmentDiscoPorts = segmentNodes(segment, false).stream()
+            .map(node -> "127.0.0.1:" + discoPort(node.localNode().<Integer>attribute(IDX_ATTR)))
+            .collect(toList());
+
+        ((TcpDiscoverySpi)cfg.getDiscoverySpi()).setIpFinder(new TcpDiscoveryVmIpFinder().setAddresses(segmentDiscoPorts));
+
+        return startGrid(optimize(cfg));
+    }
+
+    /**  */
+    private boolean isDiscoPort(int port) {
+        return port >= DFLT_PORT &&
+            port <= (DFLT_PORT + DFLT_PORT_RANGE);
+    }
+
+    /** */
+    public void createCaches() {
+        for (int cacheIdx = 0; cacheIdx < CACHE_CNT; cacheIdx++) {
+            grid(0).createCache(new CacheConfiguration<>()
+                .setName(cacheName(cacheIdx))
+                .setCacheMode(REPLICATED)
+                .setReadFromBackup(false)
+                .setWriteSynchronizationMode(PRIMARY_SYNC)

Review comment:
       This is the default behavior. 




-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [ignite] ololo3000 closed pull request #9534: wip

Posted by GitBox <gi...@apache.org>.
ololo3000 closed pull request #9534:
URL: https://github.com/apache/ignite/pull/9534


   


-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [ignite] xtern commented on a change in pull request #9534: wip

Posted by GitBox <gi...@apache.org>.
xtern commented on a change in pull request #9534:
URL: https://github.com/apache/ignite/pull/9534#discussion_r740904230



##########
File path: modules/core/src/main/java/org/apache/ignite/cache/validation/IgnitePluggableSegmentationResolver.java
##########
@@ -0,0 +1,275 @@
+/*
+ * 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 org.apache.ignite.cache.validation;
+
+import java.io.Serializable;
+import java.util.Collection;
+import java.util.List;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.stream.Collectors;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.cluster.BaselineNode;
+import org.apache.ignite.cluster.ClusterNode;
+import org.apache.ignite.events.DiscoveryEvent;
+import org.apache.ignite.internal.GridKernalContext;
+import org.apache.ignite.internal.cluster.DetachedClusterNode;
+import org.apache.ignite.internal.managers.discovery.CustomEventListener;
+import org.apache.ignite.internal.managers.discovery.DiscoCache;
+import org.apache.ignite.internal.managers.eventstorage.DiscoveryEventListener;
+import org.apache.ignite.internal.managers.eventstorage.HighPriorityListener;
+import org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion;
+import org.apache.ignite.internal.processors.cluster.ChangeGlobalStateFinishMessage;
+import org.apache.ignite.internal.processors.configuration.distributed.DistributedConfigurationLifecycleListener;
+import org.apache.ignite.internal.processors.configuration.distributed.DistributedPropertyDispatcher;
+import org.apache.ignite.internal.processors.configuration.distributed.SimpleDistributedProperty;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.thread.IgniteThreadPoolExecutor;
+import org.apache.ignite.thread.OomExceptionHandler;
+
+import static java.lang.Boolean.TRUE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE_READ_ONLY;
+import static org.apache.ignite.configuration.IgniteConfiguration.DFLT_THREAD_KEEP_ALIVE_TIME;
+import static org.apache.ignite.events.EventType.EVT_NODE_FAILED;
+import static org.apache.ignite.events.EventType.EVT_NODE_JOINED;
+import static org.apache.ignite.events.EventType.EVT_NODE_LEFT;
+import static org.apache.ignite.internal.cluster.DistributedConfigurationUtils.setDefaultValue;
+import static org.apache.ignite.internal.managers.communication.GridIoPolicy.UNDEFINED;
+
+/** */
+public class IgnitePluggableSegmentationResolver implements PluggableSegmentationResolver {
+   /** */
+    public static final String ATTR_SEG_RESOLVER_CONFIGURED = "org.apache.ignite.segmentation.resolver.configured";
+
+    /** */
+    public static final String SEG_RESOLVER_ENABLED_PROP_NAME = "org.apache.ignite.segmentation.resolver.enabled";
+
+    /** */
+    private static final String SEG_RESOLVER_THREAD_PREFIX = "segmentation-resolver";
+
+    /** */
+    private static final int[] TOP_CHANGED_EVTS = new int[] {
+        EVT_NODE_LEFT,
+        EVT_NODE_JOINED,
+        EVT_NODE_FAILED
+    };
+
+    /** */
+    private final SimpleDistributedProperty<Boolean> segResolverEnabledProp = new SimpleDistributedProperty<>(
+        SEG_RESOLVER_ENABLED_PROP_NAME,
+        Boolean::parseBoolean
+    );
+
+    /** Ignite kernel context. */
+    private final GridKernalContext ctx;
+
+    /** Ignite logger. */
+    private final IgniteLogger log;
+
+    /** */
+    private final IgniteThreadPoolExecutor stateChangeExec;
+
+    /** */
+    private long lastCheckedTopVer;
+
+    /**  */
+    private volatile State state = State.VALID;
+
+    /** @param ctx Ignite kernel context. */
+    public IgnitePluggableSegmentationResolver(GridKernalContext ctx) {
+        this.ctx = ctx;
+
+        log = ctx.log(getClass());
+
+        stateChangeExec = new IgniteThreadPoolExecutor(
+            SEG_RESOLVER_THREAD_PREFIX,
+            ctx.igniteInstanceName(),
+            1,
+            1,
+            DFLT_THREAD_KEEP_ALIVE_TIME,
+            new LinkedBlockingQueue<>(),
+            UNDEFINED,
+            new OomExceptionHandler(ctx));
+
+        stateChangeExec.allowCoreThreadTimeOut(true);
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean isValidSegment() {
+        return isDisabled() || state != State.INVALID;
+    }
+
+    /** */
+    public void start() {
+        if (ctx.clientNode())
+            return;
+
+        ctx.addNodeAttribute(ATTR_SEG_RESOLVER_CONFIGURED, true);
+
+        ctx.event().addDiscoveryEventListener(new TopologyChangedEventListener(), TOP_CHANGED_EVTS);
+
+        ctx.discovery().setCustomEventListener(
+            ChangeGlobalStateFinishMessage.class,
+            new ClusterStateChangedEventListener()
+        );
+
+        ctx.internalSubscriptionProcessor().registerDistributedConfigurationListener(
+            new DistributedConfigurationLifecycleListener() {
+                /** {@inheritDoc} */
+                @Override public void onReadyToRegister(DistributedPropertyDispatcher dispatcher) {
+                    dispatcher.registerProperty(segResolverEnabledProp);
+                }
+
+                /** {@inheritDoc} */
+                @Override public void onReadyToWrite() {
+                    setDefaultValue(segResolverEnabledProp, U.isLocalNodeCoordinator(ctx.discovery()), log);
+                }
+            });
+    }
+
+    /** @return Discovery data. */
+    public Serializable provideDiscoveryData() {
+        return state;
+    }
+
+    /** @param data Discovery data. */
+    public void onDiscoveryDataReceived(Serializable data) {
+        state = (State)data;
+    }
+
+    /** @param node Node. */
+    public void validateNewNode(ClusterNode node) {
+        if (node.isClient())
+            return;
+
+        if (!TRUE.equals(node.attribute(ATTR_SEG_RESOLVER_CONFIGURED))) {
+            throw new IgniteException( "The Segmentation Resolver plugin is not configured for the server node that is" +
+                " trying to join the cluster. Since the Segmentation Resolver is only applicable if all server nodes" +
+                " in the cluster have one, node join request will be rejected [rejectedNodeId=" + node.id() + ']');
+        }
+
+        if (state == State.VALID) {
+            DiscoCache discoCache = ctx.discovery().discoCache(new AffinityTopologyVersion(lastCheckedTopVer, 0));
+
+            if (discoCache != null) {
+                for (ClusterNode srv : discoCache.serverNodes()) {
+                    if (!ctx.discovery().alive(srv))
+                        throw new IgniteException("Node join request will be rejected due to concurrent node left" +
+                            " process handling [rejectedNodeId=" + node.id() + ']');
+                }
+            }
+        }
+    }
+
+    /** */
+    private boolean isDisabled() {
+        Boolean res = segResolverEnabledProp.get();
+
+        return res == null || !res;
+    }
+
+    /** @return return. */
+    private String formatTopologyNodes(Collection<ClusterNode> nodes) {

Review comment:
       I suggest moving this method inside the `TopologyChangedEventListener` class.

##########
File path: modules/core/src/main/java/org/apache/ignite/cache/validation/IgnitePluggableSegmentationResolver.java
##########
@@ -0,0 +1,275 @@
+/*
+ * 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 org.apache.ignite.cache.validation;
+
+import java.io.Serializable;
+import java.util.Collection;
+import java.util.List;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.stream.Collectors;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.cluster.BaselineNode;
+import org.apache.ignite.cluster.ClusterNode;
+import org.apache.ignite.events.DiscoveryEvent;
+import org.apache.ignite.internal.GridKernalContext;
+import org.apache.ignite.internal.cluster.DetachedClusterNode;
+import org.apache.ignite.internal.managers.discovery.CustomEventListener;
+import org.apache.ignite.internal.managers.discovery.DiscoCache;
+import org.apache.ignite.internal.managers.eventstorage.DiscoveryEventListener;
+import org.apache.ignite.internal.managers.eventstorage.HighPriorityListener;
+import org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion;
+import org.apache.ignite.internal.processors.cluster.ChangeGlobalStateFinishMessage;
+import org.apache.ignite.internal.processors.configuration.distributed.DistributedConfigurationLifecycleListener;
+import org.apache.ignite.internal.processors.configuration.distributed.DistributedPropertyDispatcher;
+import org.apache.ignite.internal.processors.configuration.distributed.SimpleDistributedProperty;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.thread.IgniteThreadPoolExecutor;
+import org.apache.ignite.thread.OomExceptionHandler;
+
+import static java.lang.Boolean.TRUE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE_READ_ONLY;
+import static org.apache.ignite.configuration.IgniteConfiguration.DFLT_THREAD_KEEP_ALIVE_TIME;
+import static org.apache.ignite.events.EventType.EVT_NODE_FAILED;
+import static org.apache.ignite.events.EventType.EVT_NODE_JOINED;
+import static org.apache.ignite.events.EventType.EVT_NODE_LEFT;
+import static org.apache.ignite.internal.cluster.DistributedConfigurationUtils.setDefaultValue;
+import static org.apache.ignite.internal.managers.communication.GridIoPolicy.UNDEFINED;
+
+/** */
+public class IgnitePluggableSegmentationResolver implements PluggableSegmentationResolver {
+   /** */
+    public static final String ATTR_SEG_RESOLVER_CONFIGURED = "org.apache.ignite.segmentation.resolver.configured";
+
+    /** */
+    public static final String SEG_RESOLVER_ENABLED_PROP_NAME = "org.apache.ignite.segmentation.resolver.enabled";
+
+    /** */
+    private static final String SEG_RESOLVER_THREAD_PREFIX = "segmentation-resolver";
+
+    /** */
+    private static final int[] TOP_CHANGED_EVTS = new int[] {
+        EVT_NODE_LEFT,
+        EVT_NODE_JOINED,
+        EVT_NODE_FAILED
+    };
+
+    /** */
+    private final SimpleDistributedProperty<Boolean> segResolverEnabledProp = new SimpleDistributedProperty<>(
+        SEG_RESOLVER_ENABLED_PROP_NAME,
+        Boolean::parseBoolean
+    );
+
+    /** Ignite kernel context. */
+    private final GridKernalContext ctx;
+
+    /** Ignite logger. */
+    private final IgniteLogger log;
+
+    /** */
+    private final IgniteThreadPoolExecutor stateChangeExec;
+
+    /** */
+    private long lastCheckedTopVer;
+
+    /**  */
+    private volatile State state = State.VALID;
+
+    /** @param ctx Ignite kernel context. */
+    public IgnitePluggableSegmentationResolver(GridKernalContext ctx) {
+        this.ctx = ctx;
+
+        log = ctx.log(getClass());
+
+        stateChangeExec = new IgniteThreadPoolExecutor(
+            SEG_RESOLVER_THREAD_PREFIX,
+            ctx.igniteInstanceName(),
+            1,
+            1,
+            DFLT_THREAD_KEEP_ALIVE_TIME,
+            new LinkedBlockingQueue<>(),
+            UNDEFINED,
+            new OomExceptionHandler(ctx));
+
+        stateChangeExec.allowCoreThreadTimeOut(true);
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean isValidSegment() {
+        return isDisabled() || state != State.INVALID;
+    }
+
+    /** */
+    public void start() {
+        if (ctx.clientNode())
+            return;
+
+        ctx.addNodeAttribute(ATTR_SEG_RESOLVER_CONFIGURED, true);
+
+        ctx.event().addDiscoveryEventListener(new TopologyChangedEventListener(), TOP_CHANGED_EVTS);
+
+        ctx.discovery().setCustomEventListener(
+            ChangeGlobalStateFinishMessage.class,
+            new ClusterStateChangedEventListener()
+        );
+
+        ctx.internalSubscriptionProcessor().registerDistributedConfigurationListener(
+            new DistributedConfigurationLifecycleListener() {
+                /** {@inheritDoc} */
+                @Override public void onReadyToRegister(DistributedPropertyDispatcher dispatcher) {
+                    dispatcher.registerProperty(segResolverEnabledProp);
+                }
+
+                /** {@inheritDoc} */
+                @Override public void onReadyToWrite() {
+                    setDefaultValue(segResolverEnabledProp, U.isLocalNodeCoordinator(ctx.discovery()), log);
+                }
+            });
+    }
+
+    /** @return Discovery data. */
+    public Serializable provideDiscoveryData() {
+        return state;
+    }
+
+    /** @param data Discovery data. */
+    public void onDiscoveryDataReceived(Serializable data) {
+        state = (State)data;
+    }
+
+    /** @param node Node. */
+    public void validateNewNode(ClusterNode node) {
+        if (node.isClient())
+            return;
+
+        if (!TRUE.equals(node.attribute(ATTR_SEG_RESOLVER_CONFIGURED))) {
+            throw new IgniteException( "The Segmentation Resolver plugin is not configured for the server node that is" +
+                " trying to join the cluster. Since the Segmentation Resolver is only applicable if all server nodes" +
+                " in the cluster have one, node join request will be rejected [rejectedNodeId=" + node.id() + ']');
+        }
+
+        if (state == State.VALID) {
+            DiscoCache discoCache = ctx.discovery().discoCache(new AffinityTopologyVersion(lastCheckedTopVer, 0));
+
+            if (discoCache != null) {
+                for (ClusterNode srv : discoCache.serverNodes()) {
+                    if (!ctx.discovery().alive(srv))
+                        throw new IgniteException("Node join request will be rejected due to concurrent node left" +
+                            " process handling [rejectedNodeId=" + node.id() + ']');
+                }
+            }
+        }
+    }
+
+    /** */
+    private boolean isDisabled() {
+        Boolean res = segResolverEnabledProp.get();
+
+        return res == null || !res;
+    }
+
+    /** @return return. */
+    private String formatTopologyNodes(Collection<ClusterNode> nodes) {
+        return nodes.stream().map(n -> n.id().toString()).collect(Collectors.joining(", "));
+    }
+
+    /** */
+    private class TopologyChangedEventListener implements DiscoveryEventListener, HighPriorityListener {
+        /** {@inheritDoc} */
+        @Override public void onEvent(DiscoveryEvent evt, DiscoCache discoCache) {
+            lastCheckedTopVer = evt.topologyVersion();
+
+            if (isDisabled())
+                return;
+
+            if (state == State.VALID && evt.type() == EVT_NODE_FAILED) {
+                List<? extends BaselineNode> baselineNodes = discoCache.baselineNodes();
+
+                if (baselineNodes != null && aliveBaselineNodes(baselineNodes) < baselineNodes.size() / 2 + 1) {
+                    state = State.INVALID;
+
+                    stateChangeExec.execute(() -> {
+                        try {
+                            ctx.cluster().get().state(ACTIVE_READ_ONLY);
+                        }
+                        catch (Throwable e) {
+                            U.error(
+                                log,
+                                "Failed to automatically switch state of the segmented cluster to the READ-ONLY mode" +
+                                    " [segmentedNodes=" + formatTopologyNodes(discoCache.allNodes()) + "]. Cache writes" +
+                                    " are already restricted for all configured caches, but this step is still required" +
+                                    " in order to be able to unlock cache writes in the future. Retry this operation" +
+                                    " manually, if possible.",
+                                e
+                            );
+                        }
+                    });
+
+                    U.warn(log, "Cluster segmentation was detected [segmentedNodes=" +
+                        formatTopologyNodes(discoCache.allNodes()) + ']');
+                }
+            }
+
+            if (ctx.state().isBaselineAutoAdjustEnabled())
+                U.warn(log, "Segmentation Resolver requires baseline to be configured. If no baseline is" +
+                    " set, any topology change is considered valid.");
+        }
+
+        /** {@inheritDoc} */
+        @Override public int order() {
+            return 0;
+        }
+
+        /** */
+        private int aliveBaselineNodes(Collection<? extends BaselineNode> baselineNodes) {
+            int res = 0;
+
+            for (BaselineNode node : baselineNodes) {
+                if (!(node instanceof DetachedClusterNode))
+                    ++res;
+            }
+
+            return res;
+        }
+    }
+
+    /** */
+    private class ClusterStateChangedEventListener implements CustomEventListener<ChangeGlobalStateFinishMessage> {

Review comment:
       From my point of view, it would be better to "inline" this class.

##########
File path: modules/core/src/main/java/org/apache/ignite/cache/validation/IgnitePluggableSegmentationResolver.java
##########
@@ -0,0 +1,275 @@
+/*
+ * 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 org.apache.ignite.cache.validation;
+
+import java.io.Serializable;
+import java.util.Collection;
+import java.util.List;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.stream.Collectors;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.cluster.BaselineNode;
+import org.apache.ignite.cluster.ClusterNode;
+import org.apache.ignite.events.DiscoveryEvent;
+import org.apache.ignite.internal.GridKernalContext;
+import org.apache.ignite.internal.cluster.DetachedClusterNode;
+import org.apache.ignite.internal.managers.discovery.CustomEventListener;
+import org.apache.ignite.internal.managers.discovery.DiscoCache;
+import org.apache.ignite.internal.managers.eventstorage.DiscoveryEventListener;
+import org.apache.ignite.internal.managers.eventstorage.HighPriorityListener;
+import org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion;
+import org.apache.ignite.internal.processors.cluster.ChangeGlobalStateFinishMessage;
+import org.apache.ignite.internal.processors.configuration.distributed.DistributedConfigurationLifecycleListener;
+import org.apache.ignite.internal.processors.configuration.distributed.DistributedPropertyDispatcher;
+import org.apache.ignite.internal.processors.configuration.distributed.SimpleDistributedProperty;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.thread.IgniteThreadPoolExecutor;
+import org.apache.ignite.thread.OomExceptionHandler;
+
+import static java.lang.Boolean.TRUE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE_READ_ONLY;
+import static org.apache.ignite.configuration.IgniteConfiguration.DFLT_THREAD_KEEP_ALIVE_TIME;
+import static org.apache.ignite.events.EventType.EVT_NODE_FAILED;
+import static org.apache.ignite.events.EventType.EVT_NODE_JOINED;
+import static org.apache.ignite.events.EventType.EVT_NODE_LEFT;
+import static org.apache.ignite.internal.cluster.DistributedConfigurationUtils.setDefaultValue;
+import static org.apache.ignite.internal.managers.communication.GridIoPolicy.UNDEFINED;
+
+/** */
+public class IgnitePluggableSegmentationResolver implements PluggableSegmentationResolver {
+   /** */
+    public static final String ATTR_SEG_RESOLVER_CONFIGURED = "org.apache.ignite.segmentation.resolver.configured";
+
+    /** */
+    public static final String SEG_RESOLVER_ENABLED_PROP_NAME = "org.apache.ignite.segmentation.resolver.enabled";
+
+    /** */
+    private static final String SEG_RESOLVER_THREAD_PREFIX = "segmentation-resolver";
+
+    /** */
+    private static final int[] TOP_CHANGED_EVTS = new int[] {
+        EVT_NODE_LEFT,
+        EVT_NODE_JOINED,
+        EVT_NODE_FAILED
+    };
+
+    /** */
+    private final SimpleDistributedProperty<Boolean> segResolverEnabledProp = new SimpleDistributedProperty<>(
+        SEG_RESOLVER_ENABLED_PROP_NAME,
+        Boolean::parseBoolean
+    );
+
+    /** Ignite kernel context. */
+    private final GridKernalContext ctx;
+
+    /** Ignite logger. */
+    private final IgniteLogger log;
+
+    /** */
+    private final IgniteThreadPoolExecutor stateChangeExec;
+
+    /** */
+    private long lastCheckedTopVer;
+
+    /**  */
+    private volatile State state = State.VALID;
+
+    /** @param ctx Ignite kernel context. */
+    public IgnitePluggableSegmentationResolver(GridKernalContext ctx) {
+        this.ctx = ctx;
+
+        log = ctx.log(getClass());
+
+        stateChangeExec = new IgniteThreadPoolExecutor(
+            SEG_RESOLVER_THREAD_PREFIX,
+            ctx.igniteInstanceName(),
+            1,
+            1,
+            DFLT_THREAD_KEEP_ALIVE_TIME,
+            new LinkedBlockingQueue<>(),
+            UNDEFINED,
+            new OomExceptionHandler(ctx));
+
+        stateChangeExec.allowCoreThreadTimeOut(true);
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean isValidSegment() {
+        return isDisabled() || state != State.INVALID;
+    }
+
+    /** */
+    public void start() {
+        if (ctx.clientNode())
+            return;
+
+        ctx.addNodeAttribute(ATTR_SEG_RESOLVER_CONFIGURED, true);
+
+        ctx.event().addDiscoveryEventListener(new TopologyChangedEventListener(), TOP_CHANGED_EVTS);
+
+        ctx.discovery().setCustomEventListener(
+            ChangeGlobalStateFinishMessage.class,
+            new ClusterStateChangedEventListener()
+        );
+
+        ctx.internalSubscriptionProcessor().registerDistributedConfigurationListener(
+            new DistributedConfigurationLifecycleListener() {
+                /** {@inheritDoc} */
+                @Override public void onReadyToRegister(DistributedPropertyDispatcher dispatcher) {
+                    dispatcher.registerProperty(segResolverEnabledProp);
+                }
+
+                /** {@inheritDoc} */
+                @Override public void onReadyToWrite() {
+                    setDefaultValue(segResolverEnabledProp, U.isLocalNodeCoordinator(ctx.discovery()), log);
+                }
+            });
+    }
+
+    /** @return Discovery data. */
+    public Serializable provideDiscoveryData() {
+        return state;
+    }
+
+    /** @param data Discovery data. */
+    public void onDiscoveryDataReceived(Serializable data) {
+        state = (State)data;
+    }
+
+    /** @param node Node. */
+    public void validateNewNode(ClusterNode node) {
+        if (node.isClient())
+            return;
+
+        if (!TRUE.equals(node.attribute(ATTR_SEG_RESOLVER_CONFIGURED))) {
+            throw new IgniteException( "The Segmentation Resolver plugin is not configured for the server node that is" +
+                " trying to join the cluster. Since the Segmentation Resolver is only applicable if all server nodes" +
+                " in the cluster have one, node join request will be rejected [rejectedNodeId=" + node.id() + ']');
+        }
+
+        if (state == State.VALID) {
+            DiscoCache discoCache = ctx.discovery().discoCache(new AffinityTopologyVersion(lastCheckedTopVer, 0));
+
+            if (discoCache != null) {
+                for (ClusterNode srv : discoCache.serverNodes()) {
+                    if (!ctx.discovery().alive(srv))
+                        throw new IgniteException("Node join request will be rejected due to concurrent node left" +
+                            " process handling [rejectedNodeId=" + node.id() + ']');
+                }
+            }
+        }
+    }
+
+    /** */
+    private boolean isDisabled() {
+        Boolean res = segResolverEnabledProp.get();
+
+        return res == null || !res;
+    }
+
+    /** @return return. */
+    private String formatTopologyNodes(Collection<ClusterNode> nodes) {
+        return nodes.stream().map(n -> n.id().toString()).collect(Collectors.joining(", "));
+    }
+
+    /** */
+    private class TopologyChangedEventListener implements DiscoveryEventListener, HighPriorityListener {
+        /** {@inheritDoc} */
+        @Override public void onEvent(DiscoveryEvent evt, DiscoCache discoCache) {
+            lastCheckedTopVer = evt.topologyVersion();
+
+            if (isDisabled())
+                return;
+
+            if (state == State.VALID && evt.type() == EVT_NODE_FAILED) {
+                List<? extends BaselineNode> baselineNodes = discoCache.baselineNodes();
+
+                if (baselineNodes != null && aliveBaselineNodes(baselineNodes) < baselineNodes.size() / 2 + 1) {
+                    state = State.INVALID;
+
+                    stateChangeExec.execute(() -> {
+                        try {
+                            ctx.cluster().get().state(ACTIVE_READ_ONLY);
+                        }
+                        catch (Throwable e) {
+                            U.error(
+                                log,
+                                "Failed to automatically switch state of the segmented cluster to the READ-ONLY mode" +
+                                    " [segmentedNodes=" + formatTopologyNodes(discoCache.allNodes()) + "]. Cache writes" +
+                                    " are already restricted for all configured caches, but this step is still required" +
+                                    " in order to be able to unlock cache writes in the future. Retry this operation" +
+                                    " manually, if possible.",
+                                e
+                            );
+                        }
+                    });
+
+                    U.warn(log, "Cluster segmentation was detected [segmentedNodes=" +
+                        formatTopologyNodes(discoCache.allNodes()) + ']');
+                }
+            }
+
+            if (ctx.state().isBaselineAutoAdjustEnabled())

Review comment:
       As I understand it, the problem with auto-adjustment is not that the baseline has not been set, but that it is being set before the resolver gets current baseline nodes.
   For example, `testConsequentSegmentationResolving` can be passed without disabling auto-adjustement if we set an auto-adjustement timeout `srv.cluster().BaselineAutoAdjustTimeout(1_000)`.
   
   So, I see 2 options here:
   1. get the previous baseline (before the last auto-adjustment that was caused by the top change, if possible) when getting the baseline nodes.
   2. correct the warning message that baseline auto-adjustment is enabled and the segmentation resolver may not work at all. This one seems strange because in in-memory cluster this is the default behavior.

##########
File path: modules/core/src/main/java/org/apache/ignite/cache/validation/IgnitePluggableSegmentationResolver.java
##########
@@ -0,0 +1,275 @@
+/*
+ * 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 org.apache.ignite.cache.validation;
+
+import java.io.Serializable;
+import java.util.Collection;
+import java.util.List;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.stream.Collectors;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.cluster.BaselineNode;
+import org.apache.ignite.cluster.ClusterNode;
+import org.apache.ignite.events.DiscoveryEvent;
+import org.apache.ignite.internal.GridKernalContext;
+import org.apache.ignite.internal.cluster.DetachedClusterNode;
+import org.apache.ignite.internal.managers.discovery.CustomEventListener;
+import org.apache.ignite.internal.managers.discovery.DiscoCache;
+import org.apache.ignite.internal.managers.eventstorage.DiscoveryEventListener;
+import org.apache.ignite.internal.managers.eventstorage.HighPriorityListener;
+import org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion;
+import org.apache.ignite.internal.processors.cluster.ChangeGlobalStateFinishMessage;
+import org.apache.ignite.internal.processors.configuration.distributed.DistributedConfigurationLifecycleListener;
+import org.apache.ignite.internal.processors.configuration.distributed.DistributedPropertyDispatcher;
+import org.apache.ignite.internal.processors.configuration.distributed.SimpleDistributedProperty;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.thread.IgniteThreadPoolExecutor;
+import org.apache.ignite.thread.OomExceptionHandler;
+
+import static java.lang.Boolean.TRUE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE_READ_ONLY;
+import static org.apache.ignite.configuration.IgniteConfiguration.DFLT_THREAD_KEEP_ALIVE_TIME;
+import static org.apache.ignite.events.EventType.EVT_NODE_FAILED;
+import static org.apache.ignite.events.EventType.EVT_NODE_JOINED;
+import static org.apache.ignite.events.EventType.EVT_NODE_LEFT;
+import static org.apache.ignite.internal.cluster.DistributedConfigurationUtils.setDefaultValue;
+import static org.apache.ignite.internal.managers.communication.GridIoPolicy.UNDEFINED;
+
+/** */
+public class IgnitePluggableSegmentationResolver implements PluggableSegmentationResolver {
+   /** */
+    public static final String ATTR_SEG_RESOLVER_CONFIGURED = "org.apache.ignite.segmentation.resolver.configured";
+
+    /** */
+    public static final String SEG_RESOLVER_ENABLED_PROP_NAME = "org.apache.ignite.segmentation.resolver.enabled";
+
+    /** */
+    private static final String SEG_RESOLVER_THREAD_PREFIX = "segmentation-resolver";
+
+    /** */
+    private static final int[] TOP_CHANGED_EVTS = new int[] {
+        EVT_NODE_LEFT,
+        EVT_NODE_JOINED,
+        EVT_NODE_FAILED
+    };
+
+    /** */
+    private final SimpleDistributedProperty<Boolean> segResolverEnabledProp = new SimpleDistributedProperty<>(
+        SEG_RESOLVER_ENABLED_PROP_NAME,
+        Boolean::parseBoolean
+    );
+
+    /** Ignite kernel context. */
+    private final GridKernalContext ctx;
+
+    /** Ignite logger. */
+    private final IgniteLogger log;
+
+    /** */
+    private final IgniteThreadPoolExecutor stateChangeExec;
+
+    /** */
+    private long lastCheckedTopVer;
+
+    /**  */
+    private volatile State state = State.VALID;
+
+    /** @param ctx Ignite kernel context. */
+    public IgnitePluggableSegmentationResolver(GridKernalContext ctx) {
+        this.ctx = ctx;
+
+        log = ctx.log(getClass());
+
+        stateChangeExec = new IgniteThreadPoolExecutor(
+            SEG_RESOLVER_THREAD_PREFIX,
+            ctx.igniteInstanceName(),
+            1,
+            1,
+            DFLT_THREAD_KEEP_ALIVE_TIME,
+            new LinkedBlockingQueue<>(),
+            UNDEFINED,
+            new OomExceptionHandler(ctx));
+
+        stateChangeExec.allowCoreThreadTimeOut(true);
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean isValidSegment() {
+        return isDisabled() || state != State.INVALID;
+    }
+
+    /** */
+    public void start() {
+        if (ctx.clientNode())
+            return;
+
+        ctx.addNodeAttribute(ATTR_SEG_RESOLVER_CONFIGURED, true);
+
+        ctx.event().addDiscoveryEventListener(new TopologyChangedEventListener(), TOP_CHANGED_EVTS);
+
+        ctx.discovery().setCustomEventListener(
+            ChangeGlobalStateFinishMessage.class,
+            new ClusterStateChangedEventListener()
+        );
+
+        ctx.internalSubscriptionProcessor().registerDistributedConfigurationListener(
+            new DistributedConfigurationLifecycleListener() {
+                /** {@inheritDoc} */
+                @Override public void onReadyToRegister(DistributedPropertyDispatcher dispatcher) {
+                    dispatcher.registerProperty(segResolverEnabledProp);
+                }
+
+                /** {@inheritDoc} */
+                @Override public void onReadyToWrite() {
+                    setDefaultValue(segResolverEnabledProp, U.isLocalNodeCoordinator(ctx.discovery()), log);
+                }
+            });
+    }
+
+    /** @return Discovery data. */
+    public Serializable provideDiscoveryData() {
+        return state;
+    }
+
+    /** @param data Discovery data. */
+    public void onDiscoveryDataReceived(Serializable data) {
+        state = (State)data;
+    }
+
+    /** @param node Node. */
+    public void validateNewNode(ClusterNode node) {
+        if (node.isClient())
+            return;
+
+        if (!TRUE.equals(node.attribute(ATTR_SEG_RESOLVER_CONFIGURED))) {
+            throw new IgniteException( "The Segmentation Resolver plugin is not configured for the server node that is" +
+                " trying to join the cluster. Since the Segmentation Resolver is only applicable if all server nodes" +
+                " in the cluster have one, node join request will be rejected [rejectedNodeId=" + node.id() + ']');
+        }
+
+        if (state == State.VALID) {
+            DiscoCache discoCache = ctx.discovery().discoCache(new AffinityTopologyVersion(lastCheckedTopVer, 0));
+
+            if (discoCache != null) {
+                for (ClusterNode srv : discoCache.serverNodes()) {
+                    if (!ctx.discovery().alive(srv))
+                        throw new IgniteException("Node join request will be rejected due to concurrent node left" +
+                            " process handling [rejectedNodeId=" + node.id() + ']');
+                }
+            }
+        }
+    }
+
+    /** */
+    private boolean isDisabled() {
+        Boolean res = segResolverEnabledProp.get();
+
+        return res == null || !res;
+    }
+
+    /** @return return. */
+    private String formatTopologyNodes(Collection<ClusterNode> nodes) {
+        return nodes.stream().map(n -> n.id().toString()).collect(Collectors.joining(", "));
+    }
+
+    /** */
+    private class TopologyChangedEventListener implements DiscoveryEventListener, HighPriorityListener {
+        /** {@inheritDoc} */
+        @Override public void onEvent(DiscoveryEvent evt, DiscoCache discoCache) {
+            lastCheckedTopVer = evt.topologyVersion();
+
+            if (isDisabled())
+                return;
+
+            if (state == State.VALID && evt.type() == EVT_NODE_FAILED) {

Review comment:
       When we change cluster state **to** read-only is treated as CLUSTER_WRITE_BLOCKED, but if we start the cluster in the read-only mode (`clusterStateOnStart` config property) it will be treated as "VALID" and this code block executes on segmentation.
   MB we should check the cluster state also.




-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [ignite] ololo3000 commented on a change in pull request #9534: wip

Posted by GitBox <gi...@apache.org>.
ololo3000 commented on a change in pull request #9534:
URL: https://github.com/apache/ignite/pull/9534#discussion_r741004791



##########
File path: modules/core/src/main/java/org/apache/ignite/cache/validation/IgnitePluggableSegmentationResolver.java
##########
@@ -0,0 +1,275 @@
+/*
+ * 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 org.apache.ignite.cache.validation;
+
+import java.io.Serializable;
+import java.util.Collection;
+import java.util.List;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.stream.Collectors;
+import org.apache.ignite.IgniteException;
+import org.apache.ignite.IgniteLogger;
+import org.apache.ignite.cluster.BaselineNode;
+import org.apache.ignite.cluster.ClusterNode;
+import org.apache.ignite.events.DiscoveryEvent;
+import org.apache.ignite.internal.GridKernalContext;
+import org.apache.ignite.internal.cluster.DetachedClusterNode;
+import org.apache.ignite.internal.managers.discovery.CustomEventListener;
+import org.apache.ignite.internal.managers.discovery.DiscoCache;
+import org.apache.ignite.internal.managers.eventstorage.DiscoveryEventListener;
+import org.apache.ignite.internal.managers.eventstorage.HighPriorityListener;
+import org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion;
+import org.apache.ignite.internal.processors.cluster.ChangeGlobalStateFinishMessage;
+import org.apache.ignite.internal.processors.configuration.distributed.DistributedConfigurationLifecycleListener;
+import org.apache.ignite.internal.processors.configuration.distributed.DistributedPropertyDispatcher;
+import org.apache.ignite.internal.processors.configuration.distributed.SimpleDistributedProperty;
+import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.thread.IgniteThreadPoolExecutor;
+import org.apache.ignite.thread.OomExceptionHandler;
+
+import static java.lang.Boolean.TRUE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE;
+import static org.apache.ignite.cluster.ClusterState.ACTIVE_READ_ONLY;
+import static org.apache.ignite.configuration.IgniteConfiguration.DFLT_THREAD_KEEP_ALIVE_TIME;
+import static org.apache.ignite.events.EventType.EVT_NODE_FAILED;
+import static org.apache.ignite.events.EventType.EVT_NODE_JOINED;
+import static org.apache.ignite.events.EventType.EVT_NODE_LEFT;
+import static org.apache.ignite.internal.cluster.DistributedConfigurationUtils.setDefaultValue;
+import static org.apache.ignite.internal.managers.communication.GridIoPolicy.UNDEFINED;
+
+/** */
+public class IgnitePluggableSegmentationResolver implements PluggableSegmentationResolver {
+   /** */
+    public static final String ATTR_SEG_RESOLVER_CONFIGURED = "org.apache.ignite.segmentation.resolver.configured";
+
+    /** */
+    public static final String SEG_RESOLVER_ENABLED_PROP_NAME = "org.apache.ignite.segmentation.resolver.enabled";
+
+    /** */
+    private static final String SEG_RESOLVER_THREAD_PREFIX = "segmentation-resolver";
+
+    /** */
+    private static final int[] TOP_CHANGED_EVTS = new int[] {
+        EVT_NODE_LEFT,
+        EVT_NODE_JOINED,
+        EVT_NODE_FAILED
+    };
+
+    /** */
+    private final SimpleDistributedProperty<Boolean> segResolverEnabledProp = new SimpleDistributedProperty<>(
+        SEG_RESOLVER_ENABLED_PROP_NAME,
+        Boolean::parseBoolean
+    );
+
+    /** Ignite kernel context. */
+    private final GridKernalContext ctx;
+
+    /** Ignite logger. */
+    private final IgniteLogger log;
+
+    /** */
+    private final IgniteThreadPoolExecutor stateChangeExec;
+
+    /** */
+    private long lastCheckedTopVer;
+
+    /**  */
+    private volatile State state = State.VALID;
+
+    /** @param ctx Ignite kernel context. */
+    public IgnitePluggableSegmentationResolver(GridKernalContext ctx) {
+        this.ctx = ctx;
+
+        log = ctx.log(getClass());
+
+        stateChangeExec = new IgniteThreadPoolExecutor(
+            SEG_RESOLVER_THREAD_PREFIX,
+            ctx.igniteInstanceName(),
+            1,
+            1,
+            DFLT_THREAD_KEEP_ALIVE_TIME,
+            new LinkedBlockingQueue<>(),
+            UNDEFINED,
+            new OomExceptionHandler(ctx));
+
+        stateChangeExec.allowCoreThreadTimeOut(true);
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean isValidSegment() {
+        return isDisabled() || state != State.INVALID;
+    }
+
+    /** */
+    public void start() {
+        if (ctx.clientNode())
+            return;
+
+        ctx.addNodeAttribute(ATTR_SEG_RESOLVER_CONFIGURED, true);
+
+        ctx.event().addDiscoveryEventListener(new TopologyChangedEventListener(), TOP_CHANGED_EVTS);
+
+        ctx.discovery().setCustomEventListener(
+            ChangeGlobalStateFinishMessage.class,
+            new ClusterStateChangedEventListener()
+        );
+
+        ctx.internalSubscriptionProcessor().registerDistributedConfigurationListener(
+            new DistributedConfigurationLifecycleListener() {
+                /** {@inheritDoc} */
+                @Override public void onReadyToRegister(DistributedPropertyDispatcher dispatcher) {
+                    dispatcher.registerProperty(segResolverEnabledProp);
+                }
+
+                /** {@inheritDoc} */
+                @Override public void onReadyToWrite() {
+                    setDefaultValue(segResolverEnabledProp, U.isLocalNodeCoordinator(ctx.discovery()), log);
+                }
+            });
+    }
+
+    /** @return Discovery data. */
+    public Serializable provideDiscoveryData() {
+        return state;
+    }
+
+    /** @param data Discovery data. */
+    public void onDiscoveryDataReceived(Serializable data) {
+        state = (State)data;
+    }
+
+    /** @param node Node. */
+    public void validateNewNode(ClusterNode node) {
+        if (node.isClient())
+            return;
+
+        if (!TRUE.equals(node.attribute(ATTR_SEG_RESOLVER_CONFIGURED))) {
+            throw new IgniteException( "The Segmentation Resolver plugin is not configured for the server node that is" +
+                " trying to join the cluster. Since the Segmentation Resolver is only applicable if all server nodes" +
+                " in the cluster have one, node join request will be rejected [rejectedNodeId=" + node.id() + ']');
+        }
+
+        if (state == State.VALID) {
+            DiscoCache discoCache = ctx.discovery().discoCache(new AffinityTopologyVersion(lastCheckedTopVer, 0));
+
+            if (discoCache != null) {
+                for (ClusterNode srv : discoCache.serverNodes()) {
+                    if (!ctx.discovery().alive(srv))
+                        throw new IgniteException("Node join request will be rejected due to concurrent node left" +
+                            " process handling [rejectedNodeId=" + node.id() + ']');
+                }
+            }
+        }
+    }
+
+    /** */
+    private boolean isDisabled() {
+        Boolean res = segResolverEnabledProp.get();
+
+        return res == null || !res;
+    }
+
+    /** @return return. */
+    private String formatTopologyNodes(Collection<ClusterNode> nodes) {
+        return nodes.stream().map(n -> n.id().toString()).collect(Collectors.joining(", "));
+    }
+
+    /** */
+    private class TopologyChangedEventListener implements DiscoveryEventListener, HighPriorityListener {
+        /** {@inheritDoc} */
+        @Override public void onEvent(DiscoveryEvent evt, DiscoCache discoCache) {
+            lastCheckedTopVer = evt.topologyVersion();
+
+            if (isDisabled())
+                return;
+
+            if (state == State.VALID && evt.type() == EVT_NODE_FAILED) {
+                List<? extends BaselineNode> baselineNodes = discoCache.baselineNodes();
+
+                if (baselineNodes != null && aliveBaselineNodes(baselineNodes) < baselineNodes.size() / 2 + 1) {
+                    state = State.INVALID;
+
+                    stateChangeExec.execute(() -> {
+                        try {
+                            ctx.cluster().get().state(ACTIVE_READ_ONLY);
+                        }
+                        catch (Throwable e) {
+                            U.error(
+                                log,
+                                "Failed to automatically switch state of the segmented cluster to the READ-ONLY mode" +
+                                    " [segmentedNodes=" + formatTopologyNodes(discoCache.allNodes()) + "]. Cache writes" +
+                                    " are already restricted for all configured caches, but this step is still required" +
+                                    " in order to be able to unlock cache writes in the future. Retry this operation" +
+                                    " manually, if possible.",
+                                e
+                            );
+                        }
+                    });
+
+                    U.warn(log, "Cluster segmentation was detected [segmentedNodes=" +
+                        formatTopologyNodes(discoCache.allNodes()) + ']');
+                }
+            }
+
+            if (ctx.state().isBaselineAutoAdjustEnabled())
+                U.warn(log, "Segmentation Resolver requires baseline to be configured. If no baseline is" +
+                    " set, any topology change is considered valid.");
+        }
+
+        /** {@inheritDoc} */
+        @Override public int order() {
+            return 0;
+        }
+
+        /** */
+        private int aliveBaselineNodes(Collection<? extends BaselineNode> baselineNodes) {
+            int res = 0;
+
+            for (BaselineNode node : baselineNodes) {
+                if (!(node instanceof DetachedClusterNode))
+                    ++res;
+            }
+
+            return res;
+        }
+    }
+
+    /** */
+    private class ClusterStateChangedEventListener implements CustomEventListener<ChangeGlobalStateFinishMessage> {

Review comment:
       But in this case we will clutter up the `start` method and won't  gain any real profit.




-- 
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: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org