anton-vinogradov commented on a change in pull request #8881: URL: https://github.com/apache/ignite/pull/8881#discussion_r704505152
########## 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? -- 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]
