6185d424eb9ccfc0ca3037148cb7bdaa6bccff49
[jabaws.git] / engine / compbio / engine / LoadBalancer.java
1 /* Copyright (c) 2009 Peter Troshin\r
2  *  \r
3  *  JAva Bioinformatics Analysis Web Services (JABAWS) @version: 1.0     \r
4  * \r
5  *  This library is free software; you can redistribute it and/or modify it under the terms of the\r
6  *  Apache License version 2 as published by the Apache Software Foundation\r
7  * \r
8  *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without\r
9  *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the Apache \r
10  *  License for more details.\r
11  * \r
12  *  A copy of the license is in apache_license.txt. It is also available here:\r
13  * @see: http://www.apache.org/licenses/LICENSE-2.0.txt\r
14  * \r
15  * Any republication or derived work distributed in source code form\r
16  * must include this copyright and license notice.\r
17  */\r
18 \r
19 package compbio.engine;\r
20 \r
21 import java.util.List;\r
22 \r
23 import org.apache.log4j.Logger;\r
24 \r
25 import compbio.data.sequence.FastaSequence;\r
26 import compbio.engine.client.Executable;\r
27 import compbio.engine.local.LocalExecutorService;\r
28 import compbio.metadata.Limit;\r
29 import compbio.metadata.PresetManager;\r
30 \r
31 /**\r
32  * This class decides where to execute the job. If the local engine is enabled\r
33  * in the configuration file and it has free threads and the size of the tasks\r
34  * permits the local execution, then the local execution will be favoured.\r
35  * \r
36  * @author pvtroshin\r
37  * @version 1.0 March 2009\r
38  */\r
39 public class LoadBalancer {\r
40 \r
41         private static Logger log = Logger.getLogger(LoadBalancer.class);\r
42 \r
43         private LoadBalancer() {\r
44         }\r
45 \r
46         public static Executable.ExecProvider getEngine(Executable<?> executable) {\r
47                 if (LocalExecutorService.getExecutor().canAcceptMoreWork()) {\r
48                         log.debug("LOCAL engine HAS FREE threads will execute ... ");\r
49                         return Executable.ExecProvider.Local;\r
50                 }\r
51                 log.debug("NO free threads on the LOCAL engine! Targeting for CLUSTER execution... ");\r
52                 return Executable.ExecProvider.Cluster;\r
53         }\r
54 \r
55         public static <T, V> Executable.ExecProvider getEngine(\r
56                         Executable<V> executable, List<FastaSequence> dataSet) {\r
57 \r
58                 // If data set is deemed too big for local execution, than give a\r
59                 // cluster engine\r
60                 // If limit is not defined then defaults to executing on the cluster\r
61                 Limit<V> limit = executable\r
62                                 .getLimit(PresetManager.LOCAL_ENGINE_LIMIT_PRESET);\r
63                 log.trace("Inspecting whether the job can be executed locally using limit: "\r
64                                 + limit);\r
65                 if (limit == null || limit.isExceeded(dataSet)) {\r
66                         log.debug("Job EXCEEDS LOCAL execution LIMIT targeting for cluster execution! ");\r
67                         return Executable.ExecProvider.Cluster;\r
68                 }\r
69                 log.debug("Job FITS into the LOCAL execution limit consulting load balancer... ");\r
70                 // Even if the data satisfies criteria for local execution it may still\r
71                 // be executed on the cluster as the maximum capacity for local engine\r
72                 // may be reached.\r
73                 return getEngine(executable);\r
74         }\r
75 \r
76 }\r