From cfa779661d09b0b3e627974361cfac218fc75c3a Mon Sep 17 00:00:00 2001 From: magenoxx Date: Wed, 22 Jun 2011 20:36:48 +0400 Subject: [PATCH] Fixed thread pool leak and work queue growing. --- .../main/java/mage/server/draft/DraftSession.java | 2 ++ .../main/java/mage/server/game/GameController.java | 3 ++- .../src/main/java/mage/server/game/GameSession.java | 8 ++++++++ .../mage/server/tournament/TournamentSession.java | 2 ++ .../main/java/mage/server/util/ThreadExecutor.java | 13 ++++++++++--- 5 files changed, 24 insertions(+), 4 deletions(-) diff --git a/Mage.Server/src/main/java/mage/server/draft/DraftSession.java b/Mage.Server/src/main/java/mage/server/draft/DraftSession.java index ebbb207833..f30e2a3040 100644 --- a/Mage.Server/src/main/java/mage/server/draft/DraftSession.java +++ b/Mage.Server/src/main/java/mage/server/draft/DraftSession.java @@ -32,6 +32,7 @@ import java.rmi.RemoteException; import java.util.UUID; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import java.util.logging.Level; import mage.game.draft.Draft; @@ -142,6 +143,7 @@ public class DraftSession { private synchronized void cancelTimeout() { if (futureTimeout != null) { futureTimeout.cancel(false); + ((ThreadPoolExecutor)timeoutExecutor).getQueue().remove(futureTimeout); } } diff --git a/Mage.Server/src/main/java/mage/server/game/GameController.java b/Mage.Server/src/main/java/mage/server/game/GameController.java index b83e52fb4b..1d610e11d5 100644 --- a/Mage.Server/src/main/java/mage/server/game/GameController.java +++ b/Mage.Server/src/main/java/mage/server/game/GameController.java @@ -278,7 +278,8 @@ public class GameController implements GameCallback { public void kill(UUID sessionId) { if (sessionPlayerMap.containsKey(sessionId)) { - gameSessions.get(sessionPlayerMap.get(sessionId)).setKilled(); + GameSession session = gameSessions.get(sessionPlayerMap.get(sessionId)); + session.destroy(); gameSessions.remove(sessionPlayerMap.get(sessionId)); leave(sessionId); sessionPlayerMap.remove(sessionId); diff --git a/Mage.Server/src/main/java/mage/server/game/GameSession.java b/Mage.Server/src/main/java/mage/server/game/GameSession.java index 1e1255189f..59e9658d83 100644 --- a/Mage.Server/src/main/java/mage/server/game/GameSession.java +++ b/Mage.Server/src/main/java/mage/server/game/GameSession.java @@ -34,6 +34,7 @@ import java.util.Set; import java.util.UUID; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import mage.game.Game; import mage.MageException; @@ -172,6 +173,8 @@ public class GameSession extends GameWatcher { private synchronized void cancelTimeout() { if (futureTimeout != null) { futureTimeout.cancel(false); + ((ThreadPoolExecutor)timeoutExecutor).getQueue().remove(futureTimeout); + //System.out.println("tasks:"+ ((ThreadPoolExecutor)timeoutExecutor).getTaskCount()); } } @@ -194,4 +197,9 @@ public class GameSession extends GameWatcher { cancelTimeout(); game.getPlayer(playerId).setResponseInteger(data); } + + public void destroy() { + cancelTimeout(); + setKilled(); + } } diff --git a/Mage.Server/src/main/java/mage/server/tournament/TournamentSession.java b/Mage.Server/src/main/java/mage/server/tournament/TournamentSession.java index 20f317407a..e91ea8a63b 100644 --- a/Mage.Server/src/main/java/mage/server/tournament/TournamentSession.java +++ b/Mage.Server/src/main/java/mage/server/tournament/TournamentSession.java @@ -32,6 +32,7 @@ import java.rmi.RemoteException; import java.util.UUID; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import java.util.logging.Level; import mage.cards.decks.Deck; @@ -146,6 +147,7 @@ public class TournamentSession { private synchronized void cancelTimeout() { if (futureTimeout != null) { futureTimeout.cancel(false); + ((ThreadPoolExecutor)timeoutExecutor).getQueue().remove(futureTimeout); } } diff --git a/Mage.Server/src/main/java/mage/server/util/ThreadExecutor.java b/Mage.Server/src/main/java/mage/server/util/ThreadExecutor.java index 4576d13087..318aa379b2 100644 --- a/Mage.Server/src/main/java/mage/server/util/ThreadExecutor.java +++ b/Mage.Server/src/main/java/mage/server/util/ThreadExecutor.java @@ -28,9 +28,7 @@ package mage.server.util; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.*; /** * @@ -42,6 +40,15 @@ public class ThreadExecutor { private static ExecutorService gameExecutor = Executors.newFixedThreadPool(ConfigSettings.getInstance().getMaxGameThreads()); private static ScheduledExecutorService timeoutExecutor = Executors.newScheduledThreadPool(5); + static { + ((ThreadPoolExecutor)rmiExecutor).setKeepAliveTime(60, TimeUnit.SECONDS); + ((ThreadPoolExecutor)rmiExecutor).allowCoreThreadTimeOut(true); + ((ThreadPoolExecutor)gameExecutor).setKeepAliveTime(60, TimeUnit.SECONDS); + ((ThreadPoolExecutor)gameExecutor).allowCoreThreadTimeOut(true); + ((ThreadPoolExecutor)timeoutExecutor).setKeepAliveTime(60, TimeUnit.SECONDS); + ((ThreadPoolExecutor)timeoutExecutor).allowCoreThreadTimeOut(true); + } + private final static ThreadExecutor INSTANCE = new ThreadExecutor(); public static ThreadExecutor getInstance() {