1 23 24 package com.sun.enterprise.web.ara.rules; 25 26 import com.sun.enterprise.web.ara.IsolationRulesExecutor; 27 import com.sun.enterprise.web.connector.grizzly.Pipeline; 28 import com.sun.enterprise.web.connector.grizzly.LinkedListPipeline; 29 import com.sun.enterprise.web.connector.grizzly.ReadTask; 30 import com.sun.enterprise.web.connector.grizzly.SelectorThread; 31 import com.sun.enterprise.web.connector.grizzly.Task; 32 import com.sun.enterprise.web.connector.grizzly.TaskEvent; 33 import com.sun.enterprise.web.connector.grizzly.TaskListener; 34 import com.sun.enterprise.web.connector.grizzly.WorkerThread; 35 import java.util.Iterator ; 36 import java.util.concurrent.ConcurrentHashMap ; 37 import java.util.logging.Level ; 38 39 45 public class HeapMemoryRule extends ThreadRatioRule implements TaskListener{ 46 47 50 protected static ConcurrentHashMap <String ,Long > 51 memoryAllowed = new ConcurrentHashMap <String ,Long >(); 52 53 54 57 private static long availableMemory = -1L; 58 59 60 63 protected static ConcurrentHashMap <String ,Long > 64 appMemoryUsage = new ConcurrentHashMap <String ,Long >(); 65 66 67 70 protected static ConcurrentHashMap <ReadTask,String > 71 contextRootyCache = new ConcurrentHashMap <ReadTask,String >(); 72 73 74 78 private static HeapMemoryRulePipeline hmrPipeline; 79 80 81 85 private static Pipeline defaultPipeline; 86 87 88 91 private static ConcurrentHashMap <String ,Long > 92 consolidatedMemoryUsed = new ConcurrentHashMap <String ,Long >(); 93 95 public Integer call() throws Exception { 96 if (availableMemory == -1L){ 97 availableMemory = usedMemory(); 98 } 99 100 if ( hmrPipeline == null ) { 101 hmrPipeline = new HeapMemoryRulePipeline(); 102 hmrPipeline.initPipeline(); 103 hmrPipeline.startPipeline(); 104 } 105 106 String token = getContextRoot(); 107 Long memoryAllowedSize = memoryAllowed.get(token); 108 Double allowedRatio = privilegedTokens.get(token); 109 110 Pipeline pipeline = pipelines.get(token); 111 if ( pipeline == null ){ 112 if ( defaultPipeline == null) { 113 defaultPipeline = newPipeline(readTask.getPipeline() 114 .getMaxThreads(),readTask.getPipeline()); 115 } 116 pipelines.put(token,defaultPipeline); 117 readTask.setPipeline(defaultPipeline); 118 } else { 119 readTask.setPipeline(pipeline); 120 } 121 122 if ( memoryAllowedSize == null && allowedRatio == null){ 125 if ( countReservedMemory() <= availableMemory) { 126 return IsolationRulesExecutor.RULE_OK_NOCACHE; 127 } 128 129 if ( allocationPolicy.equals(RESERVE) ) 130 return IsolationRulesExecutor.RULE_BLOCKED; 131 else if ( allocationPolicy.equals(CEILING) ) 132 return IsolationRulesExecutor.RULE_DELAY; 133 } 134 135 boolean isAllowed = 136 isAllowedToExecute(token,memoryAllowedSize,allowedRatio); 137 138 pipeline = pipelines.get(token); 139 if ( pipeline != null) 140 readTask.setPipeline(pipeline); 141 142 if ( !isAllowed ) { 143 if ( allocationPolicy.equals(RESERVE) ) 144 return IsolationRulesExecutor.RULE_BLOCKED; 145 else if ( allocationPolicy.equals(CEILING) ) 146 return IsolationRulesExecutor.RULE_DELAY; 147 } 148 return IsolationRulesExecutor.RULE_OK_NOCACHE; 149 } 150 151 152 156 protected boolean isAllowedToExecute(String token, Long memoryAllowedSize, 157 Double allowedRatio) throws Exception { 158 159 long currentMemory = usedMemory(); 160 161 if ( memoryAllowedSize == null ){ 163 memoryAllowedSize = 164 (availableMemory * allowedRatio.longValue())/100; 165 memoryAllowed.put(token,memoryAllowedSize); 167 } 168 169 if ( memoryAllowedSize > currentMemory) { 170 return false; 171 } 172 173 Long usage = appMemoryUsage.get(token); 175 contextRootyCache.put(readTask,token); 176 177 if ( usage == null) { 180 pipelines.put(token,hmrPipeline); 181 return true; 182 } 183 184 Long currentAppUsage = consolidatedMemoryUsed.get(token); 186 if ( currentAppUsage == null ) { 187 currentAppUsage = 0L; 188 } 189 190 usage = currentAppUsage + usage; 191 192 if ( usage > currentMemory ) return false; 193 194 if ( usage > memoryAllowedSize ) return false; 195 196 consolidatedMemoryUsed.put(token,usage); 197 readTask.addTaskListener(this); 198 return true; 199 } 200 201 202 206 private long countReservedMemory(){ 207 Iterator <Long > iterator = memoryAllowed.values().iterator(); 208 long count = 0L; 209 while (iterator.hasNext()){ 210 count += iterator.next(); 211 } 212 return count; 213 } 214 215 216 219 private static long usedMemory(){ 220 Runtime.getRuntime().gc(); 221 return Runtime.getRuntime().totalMemory () 222 - Runtime.getRuntime().freeMemory (); 223 } 224 225 226 229 public void taskEvent(TaskEvent event) { 230 if ( event.getStatus() == TaskEvent.COMPLETED){ 231 232 String token = contextRootyCache.remove(event.attachement()); 233 if ( token != null ) { 234 Long count = consolidatedMemoryUsed.get(token); 235 count -= appMemoryUsage.get(token); 236 consolidatedMemoryUsed.put(token,count); 237 } 238 } 239 } 240 241 242 246 private static class HeapMemoryRulePipeline extends LinkedListPipeline{ 247 248 public void initPipeline(){ 249 workerThreads = new WorkerThread[1]; 250 WorkerThread workerThread = new WorkerThread(this, 251 "HeapMemoryRuleThread"){ 252 253 public void run(){ 254 while (true) { 255 try{ 256 ReadTask t = 257 (ReadTask)HeapMemoryRulePipeline.this.getTask(); 258 if ( t != null){ 259 long current = usedMemory(); 260 t.run(); 261 long usage = usedMemory() - current; 262 if ( usage > 0){ 263 String token = 264 contextRootyCache.get(t); 265 appMemoryUsage.put(token,usage); 266 pipelines.remove(token); 267 } 268 } 269 } catch (Throwable t) { 270 SelectorThread.logger().log(Level.SEVERE, 271 "workerThread.httpException",t); 272 } 273 } 274 } 275 }; 276 workerThread.setPriority(priority); 277 workerThreads[0] = workerThread; 278 threadCount++; 279 } 280 281 282 286 public void startPipeline(){ 287 if (!isStarted) { 288 workerThreads[0].start(); 289 isStarted = true; 290 } 291 } 292 293 294 298 public void stopPipeline(){ 299 if (isStarted) { 300 workerThreads[0].terminate(); 301 isStarted = false; 302 } 303 } 304 305 306 309 public synchronized void addTask(Task task) { 310 addLast(task); 311 notify(); 312 } 313 } 314 } | Popular Tags |