sergey-chugunov-1985 commented on code in PR #12442:
URL: https://github.com/apache/ignite/pull/12442#discussion_r2460621699


##########
modules/core/src/main/java/org/apache/ignite/topology/MdcTopologyValidator.java:
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.topology;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.Objects;
+import java.util.stream.Stream;
+import org.apache.ignite.cluster.ClusterNode;
+import org.apache.ignite.configuration.TopologyValidator;
+import org.apache.ignite.lang.IgniteExperimental;
+
+/**
+ * Multi-Datacenter topology validator.

Review Comment:
   Documentation is not sufficient.
   
   Please describe in more details use cases - how this TopologyValidator is 
supposed to be used in 2DC and 3DC deployments. It worth mentioning that in (2 
* N) DC, N > 1 deployment (4DC, 6DC etc) majority doesn't work at all and in 
case of mainDc going unreachable all other DCs switch to read-only mode even if 
they clearly have a majority (3 vs 1 for 4DC, 5 vs 1 for 6DC).



##########
modules/core/src/main/java/org/apache/ignite/topology/MdcTopologyValidator.java:
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.topology;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.Objects;
+import java.util.stream.Stream;
+import org.apache.ignite.cluster.ClusterNode;
+import org.apache.ignite.configuration.TopologyValidator;
+import org.apache.ignite.lang.IgniteExperimental;
+
+/**
+ * Multi-Datacenter topology validator.
+ * Performs data protection in case of DC failure.
+ * Covered DCs MUST be specified via {@link 
MdcTopologyValidator#setDatacenters}
+ * and primary DC MUST be specified via {@link 
MdcTopologyValidator#setPrimaryDatacenter} in case of even DC count.
+ * When primary datacenter is specified Topology Validator keeps cluster write 
accessed while primary DC is visible,
+ * otherwise DC majority check is used.
+ * */
+@IgniteExperimental
+public class MdcTopologyValidator implements TopologyValidator {
+    /** */
+    private static final long serialVersionUID = 0L;
+
+    /** */
+    private List<String> dcs;
+
+    /** */
+    private String primDc;
+
+    /** @param datacenters Datacenters.*/
+    public void setDatacenters(List<String> datacenters) {
+        if (primDc != null && datacenters.size() % 2 == 1)
+            throw new IllegalArgumentException("Datacenters count must be even 
when primary datacenter is set.");
+
+        dcs = datacenters;
+    }
+
+    /** @param primaryDatacenter Primary datacenter.*/
+    public void setPrimaryDatacenter(String primaryDatacenter) {
+        if (primaryDatacenter != null && dcs != null && dcs.size() % 2 == 1)
+            throw new IllegalArgumentException("Datacenters count must be even 
when primary datacenter is set.");
+
+        primDc = primaryDatacenter;
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean validate(Collection<ClusterNode> nodes) {
+        Stream<ClusterNode> servers = nodes.stream().filter(node -> 
!node.isClient());
+
+        if (primDc != null) {

Review Comment:
   Curly braces are not neccesary for this block.



##########
modules/core/src/main/java/org/apache/ignite/topology/MdcTopologyValidator.java:
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.topology;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.Objects;
+import java.util.stream.Stream;
+import org.apache.ignite.cluster.ClusterNode;
+import org.apache.ignite.configuration.TopologyValidator;
+import org.apache.ignite.lang.IgniteExperimental;
+
+/**
+ * Multi-Datacenter topology validator.
+ * Performs data protection in case of DC failure.
+ * Covered DCs MUST be specified via {@link 
MdcTopologyValidator#setDatacenters}
+ * and primary DC MUST be specified via {@link 
MdcTopologyValidator#setPrimaryDatacenter} in case of even DC count.
+ * When primary datacenter is specified Topology Validator keeps cluster write 
accessed while primary DC is visible,
+ * otherwise DC majority check is used.
+ * */
+@IgniteExperimental
+public class MdcTopologyValidator implements TopologyValidator {
+    /** */
+    private static final long serialVersionUID = 0L;
+
+    /** */
+    private List<String> dcs;
+
+    /** */
+    private String primDc;
+
+    /** @param datacenters Datacenters.*/
+    public void setDatacenters(List<String> datacenters) {
+        if (primDc != null && datacenters.size() % 2 == 1)
+            throw new IllegalArgumentException("Datacenters count must be even 
when primary datacenter is set.");
+
+        dcs = datacenters;
+    }
+
+    /** @param primaryDatacenter Primary datacenter.*/
+    public void setPrimaryDatacenter(String primaryDatacenter) {
+        if (primaryDatacenter != null && dcs != null && dcs.size() % 2 == 1)
+            throw new IllegalArgumentException("Datacenters count must be even 
when primary datacenter is set.");
+
+        primDc = primaryDatacenter;
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean validate(Collection<ClusterNode> nodes) {
+        Stream<ClusterNode> servers = nodes.stream().filter(node -> 
!node.isClient());
+
+        if (primDc != null) {
+            return servers.anyMatch(n -> n.dataCenterId() != null && 
n.dataCenterId().equals(primDc));
+        }
+
+        long visible = servers.map(ClusterNode::dataCenterId).count();

Review Comment:
   We could have duplicates in dataCenterIds among cluster nodes. I believe we 
should add `distinct` to this chain and write a test for it.



##########
modules/core/src/main/java/org/apache/ignite/topology/MdcTopologyValidator.java:
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.topology;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.Objects;
+import java.util.stream.Stream;
+import org.apache.ignite.cluster.ClusterNode;
+import org.apache.ignite.configuration.TopologyValidator;
+import org.apache.ignite.lang.IgniteExperimental;
+
+/**
+ * Multi-Datacenter topology validator.
+ * Performs data protection in case of DC failure.
+ * Covered DCs MUST be specified via {@link 
MdcTopologyValidator#setDatacenters}
+ * and primary DC MUST be specified via {@link 
MdcTopologyValidator#setPrimaryDatacenter} in case of even DC count.
+ * When primary datacenter is specified Topology Validator keeps cluster write 
accessed while primary DC is visible,
+ * otherwise DC majority check is used.
+ * */
+@IgniteExperimental
+public class MdcTopologyValidator implements TopologyValidator {
+    /** */
+    private static final long serialVersionUID = 0L;
+
+    /** */
+    private List<String> dcs;
+
+    /** */
+    private String primDc;
+
+    /** @param datacenters Datacenters.*/
+    public void setDatacenters(List<String> datacenters) {
+        if (primDc != null && datacenters.size() % 2 == 1)
+            throw new IllegalArgumentException("Datacenters count must be even 
when primary datacenter is set.");
+
+        dcs = datacenters;
+    }
+
+    /** @param primaryDatacenter Primary datacenter.*/
+    public void setPrimaryDatacenter(String primaryDatacenter) {
+        if (primaryDatacenter != null && dcs != null && dcs.size() % 2 == 1)
+            throw new IllegalArgumentException("Datacenters count must be even 
when primary datacenter is set.");
+
+        primDc = primaryDatacenter;
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean validate(Collection<ClusterNode> nodes) {
+        Stream<ClusterNode> servers = nodes.stream().filter(node -> 
!node.isClient());
+
+        if (primDc != null) {
+            return servers.anyMatch(n -> n.dataCenterId() != null && 
n.dataCenterId().equals(primDc));
+        }
+
+        long visible = servers.map(ClusterNode::dataCenterId).count();
+        int half = dcs.size() / 2;
+
+        return visible > half;
+    }
+
+    /** {@inheritDoc} */
+    @Override public boolean equals(Object o) {
+        if (o == null || getClass() != o.getClass())
+            return false;
+
+        MdcTopologyValidator validator = (MdcTopologyValidator)o;
+
+        return Objects.equals(dcs, validator.dcs) && Objects.equals(primDc, 
validator.primDc);

Review Comment:
   As `dcs` field is declared as `List` it is possible that two instances of 
this class differs only by order of dc ids stored inside them.
   But semantically order of dcs doesn't matter, so we could use a `Set` 
interface instead of a `List` for this field.



##########
modules/core/src/main/java/org/apache/ignite/topology/MdcTopologyValidator.java:
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.topology;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.Objects;
+import java.util.stream.Stream;
+import org.apache.ignite.cluster.ClusterNode;
+import org.apache.ignite.configuration.TopologyValidator;
+import org.apache.ignite.lang.IgniteExperimental;
+
+/**
+ * Multi-Datacenter topology validator.
+ * Performs data protection in case of DC failure.
+ * Covered DCs MUST be specified via {@link 
MdcTopologyValidator#setDatacenters}
+ * and primary DC MUST be specified via {@link 
MdcTopologyValidator#setPrimaryDatacenter} in case of even DC count.
+ * When primary datacenter is specified Topology Validator keeps cluster write 
accessed while primary DC is visible,
+ * otherwise DC majority check is used.
+ * */
+@IgniteExperimental
+public class MdcTopologyValidator implements TopologyValidator {

Review Comment:
   I suggest to initialize validator in constructor instead of providing 
setters.
   
   With setters user is able to create an empty validator, start cluster and 
get NPE on `validate` call because all fields are just nulls.
   
   I think we should protect users from such configuration errors.
   
   Does it make sense to you?



##########
modules/core/src/main/java/org/apache/ignite/topology/MdcTopologyValidator.java:
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.topology;
+
+import java.util.Collection;
+import java.util.List;
+import java.util.Objects;
+import java.util.stream.Stream;
+import org.apache.ignite.cluster.ClusterNode;
+import org.apache.ignite.configuration.TopologyValidator;
+import org.apache.ignite.lang.IgniteExperimental;
+
+/**
+ * Multi-Datacenter topology validator.
+ * Performs data protection in case of DC failure.
+ * Covered DCs MUST be specified via {@link 
MdcTopologyValidator#setDatacenters}
+ * and primary DC MUST be specified via {@link 
MdcTopologyValidator#setPrimaryDatacenter} in case of even DC count.
+ * When primary datacenter is specified Topology Validator keeps cluster write 
accessed while primary DC is visible,
+ * otherwise DC majority check is used.
+ * */
+@IgniteExperimental
+public class MdcTopologyValidator implements TopologyValidator {
+    /** */
+    private static final long serialVersionUID = 0L;
+
+    /** */
+    private List<String> dcs;
+
+    /** */
+    private String primDc;
+
+    /** @param datacenters Datacenters.*/
+    public void setDatacenters(List<String> datacenters) {
+        if (primDc != null && datacenters.size() % 2 == 1)
+            throw new IllegalArgumentException("Datacenters count must be even 
when primary datacenter is set.");
+
+        dcs = datacenters;
+    }
+
+    /** @param primaryDatacenter Primary datacenter.*/
+    public void setPrimaryDatacenter(String primaryDatacenter) {

Review Comment:
   Lets rename `primaryDatacenter` to `mainDatacenter` to not overload term 
`primary`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to