anton-vinogradov commented on a change in pull request #8881:
URL: https://github.com/apache/ignite/pull/8881#discussion_r704318995



##########
File path: 
modules/core/src/main/java/org/apache/ignite/spi/discovery/tcp/ServerImpl.java
##########
@@ -6541,6 +6518,52 @@ private void checkConnection() {
         }
     }
 
+    /**
+     * Segment local node if ring connection failed while incoming traffic is 
present.
+     */
+    private void checkOutgoingConnection() {

Review comment:
       `checkOutgoingConnectionAlive`?

##########
File path: 
modules/core/src/test/java/org/apache/ignite/util/NetFailureTcpDiscoverySpi.java
##########
@@ -0,0 +1,393 @@
+/*
+ * 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.util;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.InetAddress;
+import java.net.Socket;
+import java.net.SocketAddress;
+import java.net.SocketException;
+import java.net.SocketTimeoutException;
+import java.nio.channels.SocketChannel;
+import org.apache.ignite.internal.util.lang.IgnitePair;
+import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Tcp Discovery able to simulate network failure.
+ */
+public class NetFailureTcpDiscoverySpi extends TcpDiscoverySpi {
+    /**
+     * If not {@code null}, enables network timeout simulation. First value 
switches traffic droppage: negative for all
+     * incoming, positive for all outgoing, 0 for both.
+     */
+    protected volatile IgnitePair<Integer> simulatedTimeout;
+
+    /** {@inheritDoc} */
+    @Override protected Socket createSocket0() throws IOException {
+        return new SocketWrap(super.createSocket0());
+    }
+
+    /**
+     * Enables simulation of network timeout.
+     *
+     * @param direction If negative, enables timeout simulation for incomming 
traffic. If positive, enables timeout
+     *                  simulation for outgoing traffic. Set 0 to simlate 
failure for both traffics.
+     * @param delay     Milliseconds of awaiting before raising {@code 
SocketTimeoutException}.
+     * @see SocketWrap#simulateTimeout(Socket)
+     */
+    public void setNetworkTimeout(int direction, int delay) {
+        simulatedTimeout = new IgnitePair<>(direction, delay);
+    }
+
+    /**
+     * Simulates network timeout if enabled, raises {@code 
SocketTimeoutException}.
+     *
+     * @param sock The socket to simulate failure at.
+     * @see #setNetworkTimeout(int, int)
+     */
+    private void simulateTimeout(Socket sock) throws SocketTimeoutException {
+        IgnitePair<Integer> simulatedTimeout = this.simulatedTimeout;
+
+        if (simulatedTimeout == null)
+            return;
+
+        boolean isClientSock = sock.getLocalPort() < locPort || 
sock.getLocalPort() > locPort + locPortRange;
+
+        if (isClientSock && simulatedTimeout.get1() < 0 || !isClientSock && 
simulatedTimeout.get1() > 0)
+            return;
+
+        try {
+            Thread.sleep(simulatedTimeout.get2());
+        }
+        catch (InterruptedException ignored) {
+            // No-op.
+        }
+
+        throw new SocketTimeoutException("Simulated failure after delay: " + 
simulatedTimeout.get2() + "ms.");
+    }
+
+    /**
+     * Network failure simulator.
+     */
+    private class SocketWrap extends Socket {
+        /** The real socket to simulate failure of. */
+        private final Socket delegate;
+
+        /**
+         * Constructor.
+         *
+         * @param sock The real socket to simulate failure of.
+         */
+        private SocketWrap(Socket sock) {
+            delegate = sock;
+        }
+
+        /** {@inheritDoc} */
+        @Override public OutputStream getOutputStream() throws IOException {
+            OutputStream src = delegate.getOutputStream();
+
+            return new OutputStream() {
+                /** {@inheritDoc} */
+                @Override public void write(@NotNull byte[] b) throws 
IOException {

Review comment:
       1) `@NotNull` is useless (IMHO)
   2) Primitives cannot be annotated.

##########
File path: 
modules/core/src/test/java/org/apache/ignite/util/FailureSimulatingTcpDiscoverySpi.java
##########
@@ -0,0 +1,404 @@
+/*
+ * 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.util;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.InetAddress;
+import java.net.Socket;
+import java.net.SocketAddress;
+import java.net.SocketException;
+import java.net.SocketTimeoutException;
+import java.nio.channels.SocketChannel;
+import org.apache.ignite.internal.util.lang.IgnitePair;
+import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * The TcpDiscovery able to simulate network failure.
+ */
+public class FailureSimulatingTcpDiscoverySpi extends TcpDiscoverySpi {
+    /**
+     * If not {@code null}, enables network timeout simulation. First value 
switches traffic droppage: negative for all
+     * incoming, positive for all outgoing, 0 for both.
+     */
+    protected volatile IgnitePair<Integer> simulatedTimeout;
+
+    /** {@inheritDoc} */
+    @Override protected Socket createSocket0() throws IOException {
+        return new SocketWrap(super.createSocket0());
+    }
+
+    /**
+     * Enables simulation of network timeout.
+     *
+     * @param direction If negative, enables timeout simulation for incomming 
traffic. If positive, enables timeout
+     *                  simulation for outgoing traffic. Set 0 to simlate 
failure for both traffics.
+     * @param delay     Milliseconds of awaiting before raising {@code 
SocketTimeoutException}.
+     * @see SocketWrap#simulateTimeout(Socket, int)
+     */
+    public void enableNetworkTimeoutSimulation(int direction, int delay) {
+        simulatedTimeout = new IgnitePair<>(direction, delay);
+    }
+
+    /**
+     * Simulates network timeout if enabled, raises {@code 
SocketTimeoutException}.
+     *
+     * @param sock         The socket to simulate failure at.
+     * @param forceTimeout If positive of 0, overrides the delay preset in 
{@link #enableNetworkTimeoutSimulation(int,
+     *                     int)}.
+     * @see #enableNetworkTimeoutSimulation(int, int)
+     */
+    private void simulateTimeout(Socket sock, int forceTimeout) throws 
SocketTimeoutException {
+        IgnitePair<Integer> simulatedTimeout = this.simulatedTimeout;
+
+        if (simulatedTimeout == null)
+            return;
+
+        boolean isClientSock = sock.getLocalPort() < locPort || 
sock.getLocalPort() > locPort + locPortRange;
+
+        if (isClientSock && simulatedTimeout.get1() < 0 || !isClientSock && 
simulatedTimeout.get1() > 0)
+            return;
+
+        int timeout = forceTimeout >= 0 ? forceTimeout : 
simulatedTimeout.get2();
+
+        try {
+            Thread.sleep(timeout);
+        }
+        catch (InterruptedException ignored) {
+            // No-op.
+        }
+
+        throw new SocketTimeoutException("Simulated failure after delay: " + 
timeout + "ms.");
+    }
+
+    /**
+     * @see #simulateTimeout(Socket, int)
+     */
+    private void simulateTimeout(Socket sock) throws SocketTimeoutException {
+        simulateTimeout(sock, -1);
+    }
+
+    /**
+     * Network failure simulator.
+     */
+    private class SocketWrap extends Socket {
+        /** The real socket to simulate failure of. */
+        private final Socket delegate;
+
+        /**
+         * Constructor.
+         *
+         * @param sock The real socket to simulate failure of.
+         */
+        private SocketWrap(Socket sock) {
+            delegate = sock;
+        }
+
+        /** {@inheritDoc} */
+        @Override public OutputStream getOutputStream() throws IOException {
+            OutputStream src = delegate.getOutputStream();
+
+            return new OutputStream() {
+                /** {@inheritDoc} */
+                @Override public void write(@NotNull byte[] b) throws 
IOException {
+                    simulateTimeout(delegate);
+
+                    src.write(b);

Review comment:
       Still disagree with the approach.
   In case we emulate filtering, we should just do this.
   
   Just set up forward filtering. Will writing to the socket cause an exception 
in this case?

##########
File path: 
modules/core/src/main/java/org/apache/ignite/spi/discovery/tcp/ServerImpl.java
##########
@@ -6541,6 +6518,52 @@ private void checkConnection() {
         }
     }
 
+    /**
+     * Segment local node if ring connection failed while incoming traffic is 
present.
+     */
+    private void checkOutgoingConnection() {
+        if (firstRingMsgSendFailedTime > 0 && lastRingMsgReceivedTime > 
firstRingMsgSendFailedTime +
+            U.millisToNanos(connCheckInterval)) {

Review comment:
       ```suggestion
           if (firstRingMsgSendFailedTime > 0 &&
               lastRingMsgReceivedTime > firstRingMsgSendFailedTime + 
U.millisToNanos(connCheckInterval)) {
   ```
   seems to be more readable

##########
File path: 
modules/core/src/test/java/org/apache/ignite/util/NetFailureTcpDiscoverySpi.java
##########
@@ -0,0 +1,393 @@
+/*
+ * 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.util;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.InetAddress;
+import java.net.Socket;
+import java.net.SocketAddress;
+import java.net.SocketException;
+import java.net.SocketTimeoutException;
+import java.nio.channels.SocketChannel;
+import org.apache.ignite.internal.util.lang.IgnitePair;
+import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Tcp Discovery able to simulate network failure.
+ */
+public class NetFailureTcpDiscoverySpi extends TcpDiscoverySpi {

Review comment:
       Should have `test` word at its name

##########
File path: 
modules/core/src/test/java/org/apache/ignite/util/NetFailureTcpDiscoverySpi.java
##########
@@ -0,0 +1,393 @@
+/*
+ * 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.util;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.InetAddress;
+import java.net.Socket;
+import java.net.SocketAddress;
+import java.net.SocketException;
+import java.net.SocketTimeoutException;
+import java.nio.channels.SocketChannel;
+import org.apache.ignite.internal.util.lang.IgnitePair;
+import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Tcp Discovery able to simulate network failure.
+ */
+public class NetFailureTcpDiscoverySpi extends TcpDiscoverySpi {
+    /**
+     * If not {@code null}, enables network timeout simulation. First value 
switches traffic droppage: negative for all
+     * incoming, positive for all outgoing, 0 for both.
+     */
+    protected volatile IgnitePair<Integer> simulatedTimeout;
+
+    /** {@inheritDoc} */
+    @Override protected Socket createSocket0() throws IOException {
+        return new SocketWrap(super.createSocket0());
+    }
+
+    /**
+     * Enables simulation of network timeout.
+     *
+     * @param direction If negative, enables timeout simulation for incomming 
traffic. If positive, enables timeout
+     *                  simulation for outgoing traffic. Set 0 to simlate 
failure for both traffics.
+     * @param delay     Milliseconds of awaiting before raising {@code 
SocketTimeoutException}.
+     * @see SocketWrap#simulateTimeout(Socket)
+     */
+    public void setNetworkTimeout(int direction, int delay) {
+        simulatedTimeout = new IgnitePair<>(direction, delay);
+    }
+
+    /**
+     * Simulates network timeout if enabled, raises {@code 
SocketTimeoutException}.
+     *
+     * @param sock The socket to simulate failure at.
+     * @see #setNetworkTimeout(int, int)
+     */
+    private void simulateTimeout(Socket sock) throws SocketTimeoutException {
+        IgnitePair<Integer> simulatedTimeout = this.simulatedTimeout;
+
+        if (simulatedTimeout == null)
+            return;
+
+        boolean isClientSock = sock.getLocalPort() < locPort || 
sock.getLocalPort() > locPort + locPortRange;
+
+        if (isClientSock && simulatedTimeout.get1() < 0 || !isClientSock && 
simulatedTimeout.get1() > 0)
+            return;

Review comment:
       this newer happens at the tests.
   how can we be sure this code works?

##########
File path: 
modules/core/src/main/java/org/apache/ignite/spi/discovery/tcp/ServerImpl.java
##########
@@ -3949,6 +3957,8 @@ else if (!failedNextNode && sndState != null && 
sndState.isBackward()) {
                 if (!sent) {
                     assert next == null : next;
 
+                    checkOutgoingConnection();

Review comment:
       Any reason to check here? 
   Difference between `lastRingMsgReceivedTime` and 
`firstRingMsgSendFailedTime` will not grow here.
   The only place to check this is 
`ServerImpl.SocketReader#ringMessageReceived`.

##########
File path: 
modules/core/src/test/java/org/apache/ignite/util/NetFailureTcpDiscoverySpi.java
##########
@@ -0,0 +1,393 @@
+/*
+ * 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.util;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.InetAddress;
+import java.net.Socket;
+import java.net.SocketAddress;
+import java.net.SocketException;
+import java.net.SocketTimeoutException;
+import java.nio.channels.SocketChannel;
+import org.apache.ignite.internal.util.lang.IgnitePair;
+import org.apache.ignite.spi.discovery.tcp.TcpDiscoverySpi;
+import org.jetbrains.annotations.NotNull;
+
+/**
+ * Tcp Discovery able to simulate network failure.
+ */
+public class NetFailureTcpDiscoverySpi extends TcpDiscoverySpi {
+    /**
+     * If not {@code null}, enables network timeout simulation. First value 
switches traffic droppage: negative for all
+     * incoming, positive for all outgoing, 0 for both.
+     */
+    protected volatile IgnitePair<Integer> simulatedTimeout;

Review comment:
       Why not just `direction` and `delay`? :)




-- 
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