Skip to content

Commit ff2729d

Browse files
committed
CLOUDSTACK-9348: Optimize NioTest and NioConnection main loop
- Reduces SSL handshake timeout to 15s, previously this was only 10s in commit debfcde - Adds an aggresive explicit wakeup to save the Nio main IO loop/handler from getting blocked - Fix NioTest to fail/succeed in about 60s, previously this was 300s - Due to aggresive wakeup usage, NioTest should complete in less than 5s on most systems. On virtualized environment this may slightly increase due to thread, CPU burst/scheduling delays. Signed-off-by: Rohit Yadav <[email protected]>
1 parent e51f524 commit ff2729d

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

utils/src/com/cloud/utils/nio/Link.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -598,8 +598,8 @@ public static boolean doHandshake(final SocketChannel socketChannel, final SSLEn
598598
while (handshakeStatus != SSLEngineResult.HandshakeStatus.FINISHED
599599
&& handshakeStatus != SSLEngineResult.HandshakeStatus.NOT_HANDSHAKING) {
600600
final long timeTaken = System.currentTimeMillis() - startTimeMills;
601-
if (timeTaken > 60000L) {
602-
s_logger.warn("SSL Handshake has taken more than 60s to connect to: " + socketChannel.getRemoteAddress() +
601+
if (timeTaken > 15000L) {
602+
s_logger.warn("SSL Handshake has taken more than 15s to connect to: " + socketChannel.getRemoteAddress() +
603603
". Please investigate this connection.");
604604
return false;
605605
}

utils/src/com/cloud/utils/nio/NioConnection.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ public void run() {
170170
processTodos();
171171
} catch (Throwable e) {
172172
s_logger.warn("Caught an exception but continuing on.", e);
173+
} finally {
174+
_selector.wakeup();
173175
}
174176
}
175177
synchronized (_thread) {

utils/test/com/cloud/utils/testcase/NioTest.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,17 @@ public class NioTest {
6060
private static final Logger LOGGER = Logger.getLogger(NioTest.class);
6161

6262
// Test should fail in due time instead of looping forever
63-
private static final int TESTTIMEOUT = 120000;
63+
private static final int TESTTIMEOUT = 60000;
6464

65-
final private int totalTestCount = 5;
65+
final private int totalTestCount = 4;
6666
private int completedTestCount = 0;
6767

6868
private NioServer server;
6969
private List<NioClient> clients = new ArrayList<>();
7070
private List<NioClient> maliciousClients = new ArrayList<>();
7171

7272
private ExecutorService clientExecutor = Executors.newFixedThreadPool(totalTestCount, new NamedThreadFactory("NioClientHandler"));;
73-
private ExecutorService maliciousExecutor = Executors.newFixedThreadPool(5*totalTestCount, new NamedThreadFactory("MaliciousNioClientHandler"));;
73+
private ExecutorService maliciousExecutor = Executors.newFixedThreadPool(totalTestCount, new NamedThreadFactory("MaliciousNioClientHandler"));;
7474

7575
private Random randomGenerator = new Random();
7676
private byte[] testBytes;
@@ -97,21 +97,25 @@ public void setUp() {
9797
testBytes = new byte[1000000];
9898
randomGenerator.nextBytes(testBytes);
9999

100-
// Server configured with one worker
101100
server = new NioServer("NioTestServer", 0, 1, new NioTestServer());
102101
try {
103102
server.start();
104103
} catch (Exception e) {
105104
Assert.fail(e.getMessage());
106105
}
107106

108-
// 5 malicious clients per valid client
107+
/**
108+
* The malicious client(s) tries to block NioServer's main IO loop
109+
* thread until SSL handshake timeout value (from Link class, 15s) after
110+
* which the valid NioClient(s) get the opportunity to make connection(s)
111+
*/
112+
for (int i = 0; i < totalTestCount; i++) {
113+
final NioClient maliciousClient = new NioMaliciousClient("NioMaliciousTestClient-" + i, "127.0.0.1", server.getPort(), 1, new NioMaliciousTestClient());
114+
maliciousClients.add(maliciousClient);
115+
maliciousExecutor.submit(new ThreadedNioClient(maliciousClient));
116+
}
117+
109118
for (int i = 0; i < totalTestCount; i++) {
110-
for (int j = 0; j < 5; j++) {
111-
final NioClient maliciousClient = new NioMaliciousClient("NioMaliciousTestClient-" + i, "127.0.0.1", server.getPort(), 1, new NioMaliciousTestClient());
112-
maliciousClients.add(maliciousClient);
113-
maliciousExecutor.submit(new ThreadedNioClient(maliciousClient));
114-
}
115119
final NioClient client = new NioClient("NioTestClient-" + i, "127.0.0.1", server.getPort(), 1, new NioTestClient());
116120
clients.add(client);
117121
clientExecutor.submit(new ThreadedNioClient(client));
@@ -199,7 +203,7 @@ protected void init() throws IOException {
199203
_selector.close();
200204
throw e;
201205
} catch (InterruptedException e) {
202-
LOGGER.trace(e.getMessage());
206+
LOGGER.debug(e.getMessage());
203207
}
204208
}
205209
}
@@ -287,7 +291,6 @@ public void doTask(final Task task) {
287291
LOGGER.info("Server: Received OTHER task");
288292
}
289293
}
290-
291294
}
292295
}
293296
}

0 commit comments

Comments
 (0)