From 3004ae191abe370e0cac3d7a6e158a18fa15e450 Mon Sep 17 00:00:00 2001 From: David McKnight Date: Tue, 23 Oct 2007 16:18:44 +0000 Subject: [PATCH] merge from IBM rse to allow for user-configured idle time shutdown --- .../eclipse/dstore/core/model/DataStore.java | 24 +++++++ .../core/server/ServerCommandHandler.java | 69 ++++++++++++++++++- 2 files changed, 92 insertions(+), 1 deletion(-) diff --git a/rse/plugins/org.eclipse.dstore.core/src/org/eclipse/dstore/core/model/DataStore.java b/rse/plugins/org.eclipse.dstore.core/src/org/eclipse/dstore/core/model/DataStore.java index 8444bae3f4f..4452a9803ab 100644 --- a/rse/plugins/org.eclipse.dstore.core/src/org/eclipse/dstore/core/model/DataStore.java +++ b/rse/plugins/org.eclipse.dstore.core/src/org/eclipse/dstore/core/model/DataStore.java @@ -111,6 +111,8 @@ public final class DataStore private boolean _logTimes; private int _timeout; + private int _serverIdleShutdownTimeout = 0; + private HashMap _hashMap; private HashMap _objDescriptorMap; private HashMap _cmdDescriptorMap; @@ -466,6 +468,14 @@ public final class DataStore return _timeout; } + /** + * Gets the time the server may remain idle before shutting down + * @return the idle time before shutdown + */ + public int getServerIdleShutdownTimeout() + { + return _serverIdleShutdownTimeout; + } /** * Sets an attribute of the DataStore @@ -3475,6 +3485,20 @@ public final class DataStore _dataStoreSchema = new DataStoreSchema(this); + if (!isVirtual()) + { + // get the time the server can remain idle before automatically shutting down + // if the idle is 0 or not set then it is considered indefinite. + // The server is considered idle for the period of which no commands are + // received in server command handler + String serverIdleShutdownTimeout = System.getProperty("DSTORE_IDLE_SHUTDOWN_TIMEOUT"); + if (serverIdleShutdownTimeout != null) + { + _serverIdleShutdownTimeout = Integer.parseInt(serverIdleShutdownTimeout); + } + } + + String tracingProperty = System.getProperty("DSTORE_TRACING_ON"); //$NON-NLS-1$ if (tracingProperty != null && tracingProperty.equals("true")) //$NON-NLS-1$ { diff --git a/rse/plugins/org.eclipse.dstore.core/src/org/eclipse/dstore/internal/core/server/ServerCommandHandler.java b/rse/plugins/org.eclipse.dstore.core/src/org/eclipse/dstore/internal/core/server/ServerCommandHandler.java index 08f46f24d09..4edb834e376 100644 --- a/rse/plugins/org.eclipse.dstore.core/src/org/eclipse/dstore/internal/core/server/ServerCommandHandler.java +++ b/rse/plugins/org.eclipse.dstore.core/src/org/eclipse/dstore/internal/core/server/ServerCommandHandler.java @@ -39,11 +39,56 @@ import org.eclipse.dstore.core.model.IDataStoreConstants; */ public class ServerCommandHandler extends CommandHandler { + public class ServerIdleThread extends Thread + { + private long _timeout; + private boolean _serverTimedOut = false; + + public ServerIdleThread(long timeout) + { + _timeout = timeout; + } + + public void run() + { + while (!_serverTimedOut) + { + waitForTimeout(); + } + if (_serverTimedOut) + { + + _dataStore.getCommandHandler().finish(); + _dataStore.getUpdateHandler().finish(); + _dataStore.finish(); + System.out.println(ServerReturnCodes.RC_FINISHED); + System.exit(0); + } + } + + protected synchronized void waitForTimeout() + { + try + { + wait(_timeout); + } + catch (InterruptedException e) + { + // whenver a new command comes through we interrupt this + // if we do timeout then it's time to shutdown the server + return; + } + System.out.println("server timed out!"); + _serverTimedOut = true; + } + + } private ArrayList _loaders; private MinerLoader _minerLoader; - + private ServerIdleThread _serverIdleThread; + /** * Constructor * @@ -494,5 +539,27 @@ public class ServerCommandHandler extends CommandHandler } + /** + * Overridden so that ServerIdleThread knows when new commands are received + */ + public void addCommand(DataElement command, boolean immediate) + { + super.addCommand(command, immediate); + + int serverIdleShutdownTimeout = _dataStore.getServerIdleShutdownTimeout(); + if (serverIdleShutdownTimeout > 0) + { + if (_serverIdleThread != null) + { + // new command so restart timeout + _serverIdleThread.interrupt(); + } + else + { + _serverIdleThread = new ServerIdleThread(serverIdleShutdownTimeout); + _serverIdleThread.start(); + } + } + } }