From 0c1ad8ac88747abd0fb4ab9999715e635c3fd9f4 Mon Sep 17 00:00:00 2001 From: Sheng Yang Date: Wed, 11 May 2011 15:30:26 -0700 Subject: [PATCH] Fix another "Too many open file open" Resources need to be released before creating new NioClient instance during the reconnecting. --- agent/src/com/cloud/agent/Agent.java | 11 ++++++++++- utils/src/com/cloud/utils/nio/NioConnection.java | 10 ++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/agent/src/com/cloud/agent/Agent.java b/agent/src/com/cloud/agent/Agent.java index 43e0ad02f54..26267c5e1f7 100755 --- a/agent/src/com/cloud/agent/Agent.java +++ b/agent/src/com/cloud/agent/Agent.java @@ -370,7 +370,7 @@ public class Agent implements HandlerFactory, IAgentControl { do { _shell.getBackoffAlgorithm().waitBeforeRetry(); - s_logger.info("Lost connection to the server. Reconnecting...."); + s_logger.info("Lost connection to the server. Dealing with the remaining commands..."); inProgress = _inProgress.get(); if (inProgress > 0) { @@ -379,6 +379,15 @@ public class Agent implements HandlerFactory, IAgentControl { } while (inProgress > 0); _connection.stop(); + while (_connection.isStartup()){ + _shell.getBackoffAlgorithm().waitBeforeRetry(); + } + + try { + _connection.cleanUp(); + } catch (IOException e) { + s_logger.warn("Fail to clean up old connection. " + e); + } _connection = new NioClient( "Agent", _shell.getHost(), diff --git a/utils/src/com/cloud/utils/nio/NioConnection.java b/utils/src/com/cloud/utils/nio/NioConnection.java index 264fce8d4f0..2843136eede 100755 --- a/utils/src/com/cloud/utils/nio/NioConnection.java +++ b/utils/src/com/cloud/utils/nio/NioConnection.java @@ -175,6 +175,9 @@ public abstract class NioConnection implements Runnable { s_logger.warn("Caught an exception but continuing on.", e); } } + synchronized(_thread) { + _isStartup = false; + } } abstract void init() throws IOException; @@ -550,6 +553,13 @@ public abstract class NioConnection implements Runnable { _selector.wakeup(); } + /* Release the resource used by the instance */ + public void cleanUp() throws IOException { + if (_selector != null && _selector.isOpen()) { + _selector.close(); + } + } + public class ChangeRequest { public static final int REGISTER = 1; public static final int CHANGEOPS = 2;