KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > web > ara > rules > ThreadRatioRule


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

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 JavaDoc;
34 import java.util.Collection JavaDoc;
35 import java.util.HashMap JavaDoc;
36 import java.util.StringTokenizer JavaDoc;
37 import java.util.logging.Level JavaDoc;
38
39 /**
40  * Based on the application context-root, configure the <code>ReadTask</code>
41  * <code>Pipeline</code>. Based on the thread-ratio defined in domain.xml, an
42  * application can have privileged <code>Pipeline</code>, configured to
43  * use specific percentage of the maximum number of threads. This
44  * <code>Rule</code> instanciate two types of <code>Pipeline</code>
45  *
46  * <code>privilegedPipeline</code> is will be used to execute privileged
47  * applications.
48  *
49  * <code>victimsPipeline</code> is will be used to execute others
50  * application that aren't included within the privileged tokens.
51  *
52  * An application is marked privileged if the set of <code>Rule</code> applied
53  * to the application requests is matched.
54  *
55  * @author Jeanfrancois Arcand
56  */

57 public class ThreadRatioRule implements Rule<ReadTask> {
58     
59     protected final static String JavaDoc RESERVE = "reserve";
60     protected final static String JavaDoc CEILING = "ceiling";
61     
62     protected final static String JavaDoc ALLOCATION_MODE =
63       "com.sun.enterprise.web.ara.policyMethod";
64     
65     protected final static String JavaDoc RULE_TOKENS =
66       "com.sun.enterprise.web.ara.policyMetric";
67     
68     protected final static String JavaDoc QUERY_STRING="?";
69     protected final static String JavaDoc PATH_STRING="/";
70     
71     /**
72      * The <code>ReadTask</code> attached to this <code>Rule</code>
73      */

74     protected ReadTask readTask;
75     
76     
77     /**
78      * The <code>Pipeline</code> configured based on the
79      * <code>threadRatio</code>. This <code>Pipeline</code> is only used
80      * by privileged application.
81      */

82     protected static HashMap JavaDoc<String JavaDoc,Pipeline> pipelines
83             = new HashMap JavaDoc<String JavaDoc,Pipeline>();
84     
85     
86     /**
87      * The list of privileged token used to decide if a request can be
88      * serviced by the privileged <code>Pipeline</code>.
89      */

90     protected static HashMap JavaDoc<String JavaDoc,Double JavaDoc>
91             privilegedTokens = new HashMap JavaDoc<String JavaDoc,Double JavaDoc>();
92     
93     
94     /**
95      * The thread ratio used when an application isn't listed as a privileged
96      * application.
97      */

98     protected static double leftRatio = 1;
99       
100     
101     /**
102      * The allocation mode used: celling or Reserve. With Ceiling policy,
103      * the strategy is to wait until all apps queus are showing some slack.
104      * With Reserve policiy, if 100% reservation is made by other apps,
105      * cancel the request processing.
106      */

107     protected static String JavaDoc allocationPolicy = RESERVE;
108         
109    
110     static {
111         try{
112             if ( System.getProperty(RULE_TOKENS) != null){
113                 StringTokenizer JavaDoc privList =
114                         new StringTokenizer JavaDoc(System.getProperty(RULE_TOKENS),",");
115                 StringTokenizer JavaDoc privElement;
116                 String JavaDoc tokens;
117                 double countRatio = 0;
118                 double tokenValue;
119                 while (privList.hasMoreElements()){
120                     privElement = new StringTokenizer JavaDoc(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 JavaDoc 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     /**
157      * Creates a new ThreadRationRule.
158      */

159     public ThreadRatioRule() {
160     }
161
162     
163     /**
164      * Attach a <code>ReadTask</code> to this rule.
165      */

166     public void attach(ReadTask o) {
167         this.readTask = o;
168     }
169
170
171     /**
172      * Return the current attachement.
173      */

174     public ReadTask attachement(){
175         return readTask;
176     }
177
178     
179     /**
180      * Invoke the rule. Based on the result of the
181      * <code>ContextRootAlgorithm</code>, configure the <code>ReadTask</code>
182      * <code>Pipeline</code>.
183      */

184     public Integer JavaDoc call() throws Exception JavaDoc{
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 true, then we need to wait for free space. If false, then
192
// we can go ahead and let the task execute with its default
193
// pipeline
194
if ( isPipelineInUse() )
195                     return IsolationRulesExecutor.RULE_DELAY;
196                 else
197                     noCache = true;
198             }
199         }
200              
201         String JavaDoc token = getContextRoot();
202         
203         // Lazy instanciation
204
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     // ------------------------------------------------ Pipeline ------------//
219

220     
221     /***
222      * Get the context-root from the <code>ByteBuffer</code>
223      */

224     protected String JavaDoc getContextRoot(){
225         // (1) Get the token the Algorithm has processed for us.
226
ByteBuffer JavaDoc byteBuffer = readTask.getByteBuffer();
227         byte[] chars = new byte[byteBuffer.limit() - byteBuffer.position()];
228                
229         byteBuffer.get(chars);
230         
231         String JavaDoc token = new String JavaDoc(chars);
232   
233         int index = token.indexOf(PATH_STRING);
234         if ( index != -1){
235             token = token.substring(0,index);
236         }
237                
238         // Remove query string.
239
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     /**
253      * Apply the thread ratio.
254      */

255     protected Pipeline applyRule(String JavaDoc token){
256         Pipeline p = readTask.getPipeline();
257         int maxThreads = p.getMaxThreads();
258         
259         Double JavaDoc 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     /**
272      * Creates a new <code>Pipeline</code>
273      */

274     protected Pipeline newPipeline(int threadCount,Pipeline p){
275         // Run the Task on the SelectorThread
276
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     /**
292      * Check to see if the privileged pipeline are in-use right now.
293      */

294     protected boolean isPipelineInUse(){
295         Collection JavaDoc<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     // ---------------------------------------------------------------------//
305

306     
307     /**
308      * Cancel execution of this rule.
309      */

310     public void cancel() {
311         readTask = null;
312     }
313
314     
315     /**
316      * Return the time in second before this rule will be executed.
317      */

318     public int getExecutionTime() {
319         return -1; // now
320
}
321     
322     
323     /**
324      * Set the interval in seconds to wait before executing this rule.
325      */

326     public void setExecutionTime(int time) {
327         ;
328     }
329     
330     
331     /**
332      * Set the <code>Future</code> associated with this execution of this rule.
333      */

334     public void setFuture(java.util.concurrent.Future JavaDoc future) {
335         ;
336     }
337 }
338
Popular Tags