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.Rule; 30 import com.sun.enterprise.web.connector.grizzly.ReadTask; 31 import com.sun.enterprise.web.connector.grizzly.SelectorThread; 32 33 import java.nio.ByteBuffer ; 34 import java.util.Collection ; 35 import java.util.HashMap ; 36 import java.util.StringTokenizer ; 37 import java.util.logging.Level ; 38 39 57 public class ThreadRatioRule implements Rule<ReadTask> { 58 59 protected final static String RESERVE = "reserve"; 60 protected final static String CEILING = "ceiling"; 61 62 protected final static String ALLOCATION_MODE = 63 "com.sun.enterprise.web.ara.policyMethod"; 64 65 protected final static String RULE_TOKENS = 66 "com.sun.enterprise.web.ara.policyMetric"; 67 68 protected final static String QUERY_STRING="?"; 69 protected final static String PATH_STRING="/"; 70 71 74 protected ReadTask readTask; 75 76 77 82 protected static HashMap <String ,Pipeline> pipelines 83 = new HashMap <String ,Pipeline>(); 84 85 86 90 protected static HashMap <String ,Double > 91 privilegedTokens = new HashMap <String ,Double >(); 92 93 94 98 protected static double leftRatio = 1; 99 100 101 107 protected static String allocationPolicy = RESERVE; 108 109 110 static { 111 try{ 112 if ( System.getProperty(RULE_TOKENS) != null){ 113 StringTokenizer privList = 114 new StringTokenizer (System.getProperty(RULE_TOKENS),","); 115 StringTokenizer privElement; 116 String tokens; 117 double countRatio = 0; 118 double tokenValue; 119 while (privList.hasMoreElements()){ 120 privElement = new StringTokenizer (privList.nextToken()); 121 122 while (privElement.hasMoreElements()){ 123 tokens = privElement.nextToken(); 124 int index = tokens.indexOf("|"); 125 tokenValue = Double.valueOf(tokens.substring(index+1)); 126 privilegedTokens.put 127 (tokens.substring(0, index),tokenValue); 128 countRatio += tokenValue; 129 } 130 } 131 if ( countRatio > 1 ) { 132 SelectorThread.logger().log(Level.WARNING, 133 "Thread ratio too high. The total must be lower or equal to 1"); 134 } else { 135 leftRatio = 1 - countRatio; 136 } 137 } 138 } catch (Exception ex){ 139 SelectorThread.logger() 140 .log(Level.WARNING,"Unable to parse thread ratio", ex); 141 } 142 143 if ( System.getProperty(ALLOCATION_MODE) != null){ 144 allocationPolicy = System.getProperty(ALLOCATION_MODE); 145 if ( !allocationPolicy.equals(RESERVE) && 146 !allocationPolicy.equals(CEILING) ){ 147 SelectorThread.logger() 148 .log(Level.WARNING,"Invalid allocation policy"); 149 allocationPolicy = RESERVE; 150 } 151 } 152 } 153 154 155 156 159 public ThreadRatioRule() { 160 } 161 162 163 166 public void attach(ReadTask o) { 167 this.readTask = o; 168 } 169 170 171 174 public ReadTask attachement(){ 175 return readTask; 176 } 177 178 179 184 public Integer call() throws Exception { 185 boolean noCache = false; 186 if ( leftRatio == 0 ) { 187 if ( allocationPolicy.equals(RESERVE) ) 188 return IsolationRulesExecutor.RULE_BLOCKED; 189 else if ( allocationPolicy.equals(CEILING) ) { 190 191 if ( isPipelineInUse() ) 195 return IsolationRulesExecutor.RULE_DELAY; 196 else 197 noCache = true; 198 } 199 } 200 201 String token = getContextRoot(); 202 203 Pipeline pipeline = pipelines.get(token); 205 if ( pipeline == null ){ 206 pipeline = applyRule(token); 207 pipelines.put(token,pipeline); 208 } 209 210 readTask.setPipeline(pipeline); 211 if (!noCache) 212 return IsolationRulesExecutor.RULE_OK; 213 else 214 return IsolationRulesExecutor.RULE_OK_NOCACHE; 215 } 216 217 218 220 221 224 protected String getContextRoot(){ 225 ByteBuffer byteBuffer = readTask.getByteBuffer(); 227 byte[] chars = new byte[byteBuffer.limit() - byteBuffer.position()]; 228 229 byteBuffer.get(chars); 230 231 String token = new String (chars); 232 233 int index = token.indexOf(PATH_STRING); 234 if ( index != -1){ 235 token = token.substring(0,index); 236 } 237 238 index = token.indexOf(QUERY_STRING); 240 if ( index != -1){ 241 token = token.substring(0,index); 242 } 243 244 boolean slash = token.endsWith(PATH_STRING); 245 if ( slash ){ 246 token = token.substring(0,token.length() -1); 247 } 248 return token; 249 } 250 251 252 255 protected Pipeline applyRule(String token){ 256 Pipeline p = readTask.getPipeline(); 257 int maxThreads = p.getMaxThreads(); 258 259 Double threadRatio = privilegedTokens.get(token); 260 if (threadRatio == null) { 261 threadRatio = (leftRatio == 0? 0.5:leftRatio); 262 } 263 264 int privilegedCount = (threadRatio==1 ? maxThreads : 265 (int) (maxThreads * threadRatio) + 1); 266 267 return newPipeline(privilegedCount,p); 268 } 269 270 271 274 protected Pipeline newPipeline(int threadCount,Pipeline p){ 275 if ( threadCount == 0){ 277 return null; 278 } 279 Pipeline pipeline = new LinkedListPipeline(); 280 pipeline.setMinThreads(1); 281 pipeline.setMaxThreads(threadCount); 282 pipeline.setName(p.getName()); 283 pipeline.setQueueSizeInBytes( 284 readTask.getSelectorThread().getQueueSizeInBytes()); 285 pipeline.initPipeline(); 286 pipeline.startPipeline(); 287 return pipeline; 288 } 289 290 291 294 protected boolean isPipelineInUse(){ 295 Collection <Pipeline> collection = pipelines.values(); 296 for (Pipeline pipeline: collection){ 297 if (pipeline.size() > 0) { 298 return true; 299 } 300 } 301 return false; 302 } 303 304 306 307 310 public void cancel() { 311 readTask = null; 312 } 313 314 315 318 public int getExecutionTime() { 319 return -1; } 321 322 323 326 public void setExecutionTime(int time) { 327 ; 328 } 329 330 331 334 public void setFuture(java.util.concurrent.Future future) { 335 ; 336 } 337 } 338 | Popular Tags |