1 16 package org.directwebremoting.impl; 17 18 import org.directwebremoting.extend.ServerLoadMonitor; 19 import org.directwebremoting.extend.WaitController; 20 import org.directwebremoting.util.HitMonitor; 21 import org.directwebremoting.util.Logger; 22 23 77 public class DefaultServerLoadMonitor extends AbstractServerLoadMonitor implements ServerLoadMonitor 78 { 79 82 public long getConnectedTime() 83 { 84 return connectedTime; 85 } 86 87 90 public int getDisconnectedTime() 91 { 92 return disconnectedTime; 93 } 94 95 98 public void threadWaitStarting(WaitController controller) 99 { 100 hitMonitor.recordHit(); 101 waitingThreads++; 102 super.threadWaitStarting(controller); 103 104 checkLoading(); 105 } 106 107 110 public void threadWaitEnding(WaitController controller) 111 { 112 waitingThreads--; 113 super.threadWaitEnding(controller); 114 } 115 116 119 private void checkLoading() 120 { 121 float hitsPerSecond = (float) hitMonitor.getHitsInLastPeriod() / SECONDS_MONITORED; 122 123 if (waitingThreads < maxWaitingThreads) 124 { 125 connectedTime = maxConnectedTime; 126 disconnectedTime = 0; 127 128 setMode(USAGE_LOW); 129 return; 130 } 131 132 int roundTripAtThreadOutSeconds = threadOutRoundTripTime / 1000; 133 134 int hitsPerSecondAtThreadOut = maxWaitingThreads / roundTripAtThreadOutSeconds; 135 int hitsPerSecondAtHitOut = maxHitsPerSecond; 136 137 if (hitsPerSecond < hitsPerSecondAtThreadOut) 138 { 139 connectedTime = usageHighInitialConnectedTime; 142 disconnectedTime = usageHighDisconnectedTime; 143 144 setMode(USAGE_HIGH); 145 return; 146 } 147 148 if (mode == USAGE_DIGG) 149 { 150 float load = hitsPerSecond / maxHitsPerSecond; 152 connectedTime = usageDiggConnectedTime; 153 disconnectedTime = (int) (disconnectedTime * load); 154 155 if (disconnectedTime > usageDiggMinDisconnectedTime) 157 { 158 setMode(USAGE_DIGG); 159 return; 160 } 161 162 } 165 166 if (hitsPerSecond < hitsPerSecondAtHitOut) 167 { 168 float factor = (float) waitingThreads / maxWaitingThreads; 171 connectedTime = (int) (connectedTime / factor); 172 173 if (connectedTime > usageHighInitialConnectedTime) 174 { 175 connectedTime = usageHighInitialConnectedTime; 176 } 177 178 if (connectedTime < usageHighFinalConnectedTime) 179 { 180 connectedTime = usageHighFinalConnectedTime; 181 } 182 183 disconnectedTime = usageHighDisconnectedTime; 184 185 setMode(USAGE_HIGH); 186 return; 187 } 188 189 float load = hitsPerSecond / maxHitsPerSecond; 190 connectedTime = usageDiggConnectedTime; 191 disconnectedTime = (int) (disconnectedTime * load); 192 193 if (disconnectedTime < usageDiggMinDisconnectedTime) 194 { 195 disconnectedTime = usageDiggMinDisconnectedTime; 196 } 197 198 setMode(USAGE_DIGG); 199 return; 200 } 201 202 206 protected void setMode(int mode) 207 { 208 if (log.isDebugEnabled() && mode != this.mode) 209 { 210 log.debug("Changing modes, from " + USAGE_NAMES[this.mode] + " to " + USAGE_NAMES[mode]); 211 } 212 213 this.mode = mode; 214 } 215 216 219 public void setMaxWaitingThreads(int maxWaitingThreads) 220 { 221 this.maxWaitingThreads = maxWaitingThreads; 222 } 223 224 227 public void setMaxHitsPerSecond(int maxHitsPerSecond) 228 { 229 this.maxHitsPerSecond = maxHitsPerSecond; 230 } 231 232 238 void setMaxConnectedTime(int maxConnectedTime) 239 { 240 this.maxConnectedTime = maxConnectedTime; 241 } 242 243 246 protected static final int usageHighDisconnectedTime = 1000; 247 protected static final int usageHighInitialConnectedTime = 49000; 248 protected static final int usageHighFinalConnectedTime = 1000; 249 protected static final int usageDiggConnectedTime = 0; 250 protected static final int usageDiggMinDisconnectedTime = usageHighDisconnectedTime + usageHighFinalConnectedTime; 251 protected static final int hitOutRoundTripTime = usageHighDisconnectedTime + usageHighFinalConnectedTime; 252 protected static final int threadOutRoundTripTime = usageHighInitialConnectedTime + usageHighDisconnectedTime; 253 254 258 protected int maxWaitingThreads = 100; 259 260 266 protected int maxHitsPerSecond = 100; 267 268 272 protected int maxConnectedTime = 60000; 273 274 277 protected static final int USAGE_LOW = 0; 278 279 282 protected static final int USAGE_HIGH = 1; 283 284 287 protected static final int USAGE_DIGG = 2; 288 289 292 protected static final String [] USAGE_NAMES = { "Low", "High", "Digg" }; 293 294 297 protected int mode = USAGE_LOW; 298 299 303 protected int connectedTime = 60000; 304 305 308 protected int disconnectedTime = 1000; 309 310 314 protected static final int SECONDS_MONITORED = 10; 315 316 319 protected HitMonitor hitMonitor = new HitMonitor(SECONDS_MONITORED); 320 321 324 protected int waitingThreads = 0; 325 326 329 private static final Logger log = Logger.getLogger(DefaultServerLoadMonitor.class); 330 } 331 | Popular Tags |