1 5 package com.tctest.management; 6 7 import com.tc.logging.TCLogger; 8 import com.tc.logging.TCLogging; 9 import com.tc.management.TerracottaManagement; 10 import com.tc.management.beans.sessions.SessionMonitorMBean; 11 import com.tc.test.server.appserver.load.Node; 12 import com.tc.test.server.appserver.unit.AbstractAppServerTestCase; 13 14 import java.io.IOException ; 15 import java.io.PrintWriter ; 16 import java.net.URL ; 17 import java.util.ArrayList ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 import java.util.Properties ; 21 import java.util.Set ; 22 23 import javax.management.MBeanServerConnection ; 24 import javax.management.ObjectName ; 25 import javax.management.remote.JMXConnector ; 26 import javax.management.remote.JMXConnectorFactory ; 27 import javax.management.remote.JMXServiceURL ; 28 import javax.servlet.http.HttpServlet ; 29 import javax.servlet.http.HttpServletRequest ; 30 import javax.servlet.http.HttpServletResponse ; 31 import javax.servlet.http.HttpSession ; 32 33 public final class RequestCountTest extends AbstractAppServerTestCase { 34 35 private static final TCLogger logger = TCLogging.getTestingLogger(RequestCountTest.class); 36 37 private static final int SESSIONS_PER_NODE = 10; 38 private static final long TEST_DURATION = 30 * 1000; 39 private static final String CLIENT_NAME_PREFIX = "client-"; 40 41 public void testRequestCount() throws Throwable { 42 collectVmStats(); 43 assertTimeDirection(); 44 List jvmArgs = new ArrayList (1); 45 jvmArgs.add("-Dcom.sun.management.jmxremote"); 46 addDsoServerJvmArgs(jvmArgs); 47 startDsoServer(); 48 runNodes(2); 49 } 50 51 private void runNodes(int nodeCount) throws Throwable { 52 Thread [] nodeRunners = new Thread [nodeCount]; 53 NodeWithJMX[] nodes = new NodeWithJMX[nodeCount]; 54 int[] ports = new int[nodeCount]; 55 56 for (int i = 0; i < nodeCount; i++) { 57 ports[i] = startAppServer(true, new Properties (), new String [] { "-Dtc.node-name=" + CLIENT_NAME_PREFIX + i }) 58 .serverPort(); 59 } 60 61 final JMXConnector jmxConnector = getJMXConnector(); 62 final MBeanServerConnection mbs = jmxConnector.getMBeanServerConnection(); 63 final long beanCutoffTime = System.currentTimeMillis() + (60 * 1000); 64 Set beanSet = null; 65 for (beanSet = TerracottaManagement.getAllSessionMonitorMBeans(mbs); beanSet.size() != nodeCount; beanSet = TerracottaManagement 66 .getAllSessionMonitorMBeans(mbs)) { 67 if (!beanSet.isEmpty()) { 68 logger.info("Found some session beans but not all:"); 69 for (final Iterator pos = beanSet.iterator(); pos.hasNext();) { 70 logger.info("Session bean[" + ((ObjectName ) pos.next()).getCanonicalName() + "]"); 71 } 72 } else { 73 logger.info("No session beans found, expecting " + nodeCount + " of them"); 74 } 75 if (System.currentTimeMillis() > beanCutoffTime) { 76 final String errMsg = "Unable to find DSO client MBeans within 60 seconds of starting the node"; 77 logger.error(errMsg); 78 fail(errMsg); 79 } else { 80 logger.info("Unable to find DSO client MBeans after " + (beanCutoffTime - System.currentTimeMillis()) 81 + "ms, sleeping for 3 seconds"); 82 try { 83 Thread.sleep(3000); 84 } catch (InterruptedException ie) { 85 } 87 } 88 } 89 logger.info("We have all of our DSO client Mbeans after " + (beanCutoffTime - System.currentTimeMillis()) + ":"); 90 for (final Iterator pos = beanSet.iterator(); pos.hasNext();) { 91 logger.info("Session bean[" + ((ObjectName ) pos.next()).getCanonicalName() + "]"); 92 } 93 final SessionMonitorMBean[] mbeans = new SessionMonitorMBean[nodeCount]; 94 for (int i = 0; i < nodeCount; i++) { 95 SessionMonitorMBean clientMBean = TerracottaManagement.getClientSessionMonitorMBean(mbs, beanSet, 96 CLIENT_NAME_PREFIX + i); 97 if (clientMBean == null) { 98 final String errMsg = "We thought we had all of our DSO client MBeans, turns out we don't have one for client[" 99 + CLIENT_NAME_PREFIX + i + "]"; 100 logger.error(errMsg); 101 fail(errMsg); 102 } 103 mbeans[i] = clientMBean; 104 } 105 106 for (int i = 0; i < nodeCount; i++) { 107 URL mutateUrl = createUrl(ports[i], RequestCountingServlet.class); 108 URL validateUrl = createUrl(ports[(i + 1) % nodeCount], RequestCountingServlet.class, "read=true"); 109 nodes[i] = new NodeWithJMX(mutateUrl, validateUrl, SESSIONS_PER_NODE, TEST_DURATION); 110 nodeRunners[i] = new Thread (nodes[i], "Runner for server at port " + ports[i]); 111 } 112 113 for (int i = 0; i < nodeCount; i++) { 114 nodeRunners[i].start(); 115 } 116 117 for (int i = 0; i < nodeCount; i++) { 118 nodeRunners[i].join(); 119 } 120 121 for (int i = 0; i < nodeCount; i++) { 122 nodes[i].checkError(); 123 } 124 125 for (int i = 0; i < nodeCount; i++) { 126 nodes[i].validateMBean(mbeans[i]); 127 } 128 129 jmxConnector.close(); 130 } 131 132 private JMXConnector getJMXConnector() throws IOException { 133 JMXServiceURL jmxServerUrl = new JMXServiceURL ("service:jmx:rmi:///jndi/rmi://localhost:" + getJMXPort() + "/jmxrmi"); 134 JMXConnector jmxConnector = JMXConnectorFactory.newJMXConnector(jmxServerUrl, null); 135 jmxConnector.connect(); 136 return jmxConnector; 137 } 138 139 private static class NodeWithJMX extends Node { 140 141 public NodeWithJMX(URL mutateUrl, URL validateUrl, int numSessions, long duration) { 142 super(mutateUrl, validateUrl, numSessions, duration); 143 } 144 145 public void validateMBean(SessionMonitorMBean sessionMonitorMBean) { 146 int totalRequests = 0; 147 for (int i = 0; i < numRequests.length; i++) { 148 totalRequests += numRequests[i]; 149 } 150 151 assertEquals(totalRequests, sessionMonitorMBean.getRequestCount()); 152 153 assertEquals(1, validateUrls.length); 154 final URL validateUrl = validateUrls[0]; 155 156 System.err.println("validated value of " + totalRequests + " for client " + validateUrl); 157 } 158 } 159 160 public static final class RequestCountingServlet extends HttpServlet { 161 162 public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { 163 HttpSession session = request.getSession(true); 164 response.setContentType("text/html"); 165 PrintWriter out = response.getWriter(); 166 167 Integer count = (Integer ) session.getAttribute("count"); 168 if (count == null) { 169 count = new Integer (0); 170 } 171 172 if (request.getParameter("read") != null) { 173 if (session.isNew()) { 174 out.println("session is new"); } else { 176 out.println(count.intValue()); 177 } 178 } else { 179 int newValue = count.intValue() + 1; 180 session.setAttribute("count", new Integer (newValue)); 181 out.println(newValue); 182 } 183 } 184 } 185 } 186
| Popular Tags
|