Author: bstansberry(a)jboss.com
Date: 2008-11-03 12:42:21 -0500 (Mon, 03 Nov 2008)
New Revision: 2048
Modified:
trunk/mod_cluster/src/demo/java/org/jboss/modcluster/demo/client/RequestDriver.java
Log:
Rework session count tracking
Modified:
trunk/mod_cluster/src/demo/java/org/jboss/modcluster/demo/client/RequestDriver.java
===================================================================
---
trunk/mod_cluster/src/demo/java/org/jboss/modcluster/demo/client/RequestDriver.java 2008-11-01
18:02:11 UTC (rev 2047)
+++
trunk/mod_cluster/src/demo/java/org/jboss/modcluster/demo/client/RequestDriver.java 2008-11-03
17:42:21 UTC (rev 2048)
@@ -168,7 +168,7 @@
AtomicBoolean stopped)
{
this.request_url = request_url;
- this.destroy_url = destroy_url;
+ this.destroy_url = (request_url.equals(destroy_url)) ? null : destroy_url;
this.sessionLife = sessionLife * 1000;
this.sleepTime = sleepTime;
this.requests = requests;
@@ -195,14 +195,22 @@
stop = System.currentTimeMillis();
try
{
- if (cookie != null)
+ // Make an attempt to terminate any ongoing session
+ // Only bother if our destroy
+ if (cookie != null && destroy_url != null)
{
- executeRequest(destroy_url, false);
+ executeRequest(destroy_url);
}
}
catch (IOException e)
{
}
+ finally
+ {
+ // If we haven't already cleaned up this thread's
+ // session info, do so now
+ handleSessionTermination();
+ }
}
}
@@ -264,7 +272,7 @@
break;
}
- rc = executeRequest(request_url, false);
+ rc = executeRequest(request_url);
if (rc == 200)
{
successful_reads++;
@@ -278,14 +286,17 @@
}
}
- if (!failed) // if we failed, executeRequest destroyed session for us
+ if (!failed && destroy_url != null)
{
- executeRequest(destroy_url, true);
+ // Send invalidation request
+ executeRequest(destroy_url);
}
+
+ handleSessionTermination();
}
}
- private int executeRequest(URL url, boolean destroySession) throws IOException
+ private int executeRequest(URL url) throws IOException
{
InputStream input = null;
HttpURLConnection conn = null;
@@ -303,44 +314,33 @@
input.close(); // discard data
String handlerNode = conn.getHeaderField(Constants.NODE_HEADER);
- String tmp_cookie = conn.getHeaderField("set-cookie");
- if (tmp_cookie != null && cookie == null)
- {
- // New session
- cookie = tmp_cookie;
- modifyCount(handlerNode, sessions, true);
- }
modifyCount(handlerNode, requests, true);
- // If we failed over, decrement the previous handler's count
- if (!handlerNode.equals(lastHandler))
- {
- modifyCount(lastHandler, sessions, false);
- }
+ String tmp_cookie = conn.getHeaderField("set-cookie");
- int rc = conn.getResponseCode();
- if ((rc != 200 || destroySession) && cookie != null)
+ if (tmp_cookie != null && cookie == null)
{
- cookie = null;
- lastHandler = null;
- modifyCount(handlerNode, sessions, false);
- }
- else if (cookie != null)
- {
+ // New session -- track it and its handler
+ cookie = tmp_cookie;
+ modifyCount(handlerNode, sessions, true);
// Track this handler so we can decrement the session
// count in case of failover or error
lastHandler = handlerNode;
}
+ else if (lastHandler != null && !lastHandler.equals(handlerNode))
+ {
+ // Ongoing session has failed over in an unplanned way,
+ // so decrement the previous handler's count.
+ modifyCount(lastHandler, sessions, false);
+ lastHandler = null;
+ }
- return rc;
+ return conn.getResponseCode();
}
catch (Exception e)
{
- // Decrement the session count for whoever last handled a request
- cookie = null;
- modifyCount(lastHandler, sessions, false);
- lastHandler = null;
+ handleSessionTermination();
if (e instanceof IOException)
throw (IOException) e;
@@ -353,6 +353,25 @@
conn.disconnect();
}
}
+
+ private void invalidateSession()
+ {
+ if (!request_url.equals(destroy_url))
+ {
+ }
+ }
+
+ private void handleSessionTermination()
+ {
+ cookie = null;
+ String last = lastHandler;
+ lastHandler = null;
+ if (last != null)
+ {
+ // Decrement the session count for whoever last handled a request
+ modifyCount(last, sessions, false);
+ }
+ }
private static void modifyCount(String handlerNode, ConcurrentMap<String,
AtomicInteger> map, boolean increment)
{
Show replies by date