[104] | 1 | package test.rewolucja.resources.logical; |
---|
| 2 | |
---|
| 3 | import eduni.simjava.Sim_event; |
---|
| 4 | import eduni.simjava.Sim_port; |
---|
| 5 | import eduni.simjava.Sim_system; |
---|
| 6 | import gridsim.GridSim; |
---|
| 7 | import gridsim.GridSimTags; |
---|
| 8 | import gridsim.IO_data; |
---|
| 9 | import gridsim.gssim.GssimTags; |
---|
| 10 | import gridsim.gssim.network.ExtendedGridSimCore; |
---|
| 11 | |
---|
| 12 | import java.util.ArrayList; |
---|
| 13 | import java.util.HashMap; |
---|
| 14 | import java.util.HashSet; |
---|
| 15 | import java.util.Iterator; |
---|
| 16 | import java.util.List; |
---|
| 17 | import java.util.Map; |
---|
| 18 | import java.util.Properties; |
---|
| 19 | import java.util.Set; |
---|
| 20 | |
---|
| 21 | import org.apache.commons.logging.Log; |
---|
| 22 | import org.apache.commons.logging.LogFactory; |
---|
| 23 | |
---|
| 24 | import schedframe.resources.ResourceStateDescription; |
---|
| 25 | import schedframe.resources.providers.LocalSystem; |
---|
| 26 | import schedframe.resources.units.ResourceUnit; |
---|
| 27 | import schedframe.scheduling.events.SchedulingEventType; |
---|
| 28 | import schedframe.scheduling.plugin.SchedulingPluginConfiguration; |
---|
| 29 | import schedframe.scheduling.utils.ResourceParameterName; |
---|
| 30 | import test.rewolucja.GSSIMJobInterface; |
---|
| 31 | import test.rewolucja.resources.Resource; |
---|
| 32 | import test.rewolucja.resources.ResourceStatus; |
---|
| 33 | import test.rewolucja.resources.ResourceType; |
---|
| 34 | import test.rewolucja.resources.StructureEntityInterface; |
---|
| 35 | import test.rewolucja.resources.exception.ResourceException; |
---|
| 36 | import test.rewolucja.resources.physical.base.ComputingResource; |
---|
| 37 | import test.rewolucja.scheduling.implementation.ManagementSystem; |
---|
| 38 | |
---|
| 39 | public class LogicalResource extends ExtendedGridSimCore implements Resource, StructureEntityInterface<LogicalResource>{ |
---|
| 40 | |
---|
| 41 | private Log log = LogFactory.getLog(LogicalResource.class); |
---|
| 42 | |
---|
| 43 | protected ManagementSystem managementSystem; |
---|
| 44 | |
---|
| 45 | protected List<ComputingResource> resources; |
---|
| 46 | |
---|
| 47 | protected LogicalResource parent; |
---|
| 48 | protected List<LogicalResource> children; |
---|
| 49 | |
---|
| 50 | protected ResourceStatus status; |
---|
| 51 | |
---|
| 52 | public LogicalResource(ManagementSystem manSys) throws Exception { |
---|
| 53 | super(manSys.getName(), 1); |
---|
| 54 | this.managementSystem = manSys; |
---|
| 55 | this.resources = new ArrayList<ComputingResource>(); |
---|
| 56 | this.status = ResourceStatus.AVAILABLE; |
---|
| 57 | init(); |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | public LogicalResource(ManagementSystem manSys, List<ComputingResource> res) throws Exception { |
---|
| 61 | super(manSys.getName(), 1); |
---|
| 62 | this.managementSystem = manSys; |
---|
| 63 | this.resources = new ArrayList<ComputingResource>(); |
---|
| 64 | this.status = ResourceStatus.AVAILABLE; |
---|
| 65 | addResources(res); |
---|
| 66 | init(); |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | public void init(){ |
---|
| 70 | managementSystem.init(this); |
---|
| 71 | Integer lResIdObj = new Integer(get_id()); |
---|
| 72 | |
---|
| 73 | List<Integer> res = GridSim.getGridResourceList(); |
---|
| 74 | res.add(lResIdObj); |
---|
| 75 | |
---|
| 76 | /*if (supportsAR) { |
---|
| 77 | res = GridSim.getAdvancedReservationList(); |
---|
| 78 | res.add(resIdObj); |
---|
| 79 | } */ |
---|
| 80 | |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | public void addChild(LogicalResource child) { |
---|
| 84 | child.setParent(this); |
---|
| 85 | if (children == null) { |
---|
| 86 | children = new ArrayList<LogicalResource>(); |
---|
| 87 | } |
---|
| 88 | children.add(child); |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | public List<LogicalResource> getChildren() { |
---|
| 92 | return children; |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | public void setParent(LogicalResource newParent) { |
---|
| 96 | parent = newParent; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | public LogicalResource getParent() { |
---|
| 100 | return parent; |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | public void addResource(ComputingResource resource) { |
---|
| 104 | resources.add(resource); |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | public void addResources(List<ComputingResource> res) { |
---|
| 108 | for(ComputingResource resource:res){ |
---|
| 109 | resources.add(resource); |
---|
| 110 | resource.setLogicalResource(this); |
---|
| 111 | for(ComputingResource child: resource.filterDescendants(new Properties())){ |
---|
| 112 | child.setLogicalResource(this); |
---|
| 113 | } |
---|
| 114 | } |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | public List<ComputingResource> getResources() { |
---|
| 118 | return resources; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | public void body() { |
---|
| 122 | |
---|
| 123 | SchedulingPluginConfiguration pluginConfig = managementSystem.getPluginConfiguration(); |
---|
| 124 | if (pluginConfig != null) { |
---|
| 125 | Map<SchedulingEventType, Object> events = pluginConfig.getServedEvents(); |
---|
| 126 | if (events != null) { |
---|
| 127 | Object obj = events.get(SchedulingEventType.TIMER); |
---|
| 128 | if (obj != null) { |
---|
| 129 | int delay = (Integer) obj; |
---|
| 130 | send(this.get_id(), delay, GssimTags.TIMER); |
---|
| 131 | |
---|
| 132 | } |
---|
| 133 | } |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | // Process events until END_OF_SIMULATION is received from the |
---|
| 137 | // GridSimShutdown Entity |
---|
| 138 | Sim_event ev = new Sim_event(); |
---|
| 139 | sim_get_next(ev); |
---|
| 140 | // boolean run = true; |
---|
| 141 | boolean run = true; |
---|
| 142 | while (Sim_system.running() && run) { |
---|
| 143 | // sim_get_next(ev); |
---|
| 144 | // if the simulation finishes then exit the loop |
---|
| 145 | if (ev.get_tag() == GridSimTags.END_OF_SIMULATION) { |
---|
| 146 | // managemetnSystem_.setEndSimulation(); |
---|
| 147 | run = false; |
---|
| 148 | break; |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | // process the received event |
---|
| 152 | processRequest(ev); |
---|
| 153 | sim_get_next(ev); |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | protected void processRequest(Sim_event ev) { |
---|
| 159 | switch (ev.get_tag()) { |
---|
| 160 | |
---|
| 161 | case GridSimTags.GRIDLET_SUBMIT: |
---|
| 162 | processGSSIMJobSubmit(ev, false); |
---|
| 163 | break; |
---|
| 164 | |
---|
| 165 | case GridSimTags.GRIDLET_SUBMIT_ACK: |
---|
| 166 | processGSSIMJobSubmit(ev, true); |
---|
| 167 | break; |
---|
| 168 | |
---|
| 169 | case GridSimTags.GRIDLET_RETURN: |
---|
| 170 | processGSSIMJobReturn(ev); |
---|
| 171 | break; |
---|
| 172 | |
---|
| 173 | default: |
---|
| 174 | processOtherRequest(ev); |
---|
| 175 | break; |
---|
| 176 | } |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | protected void processOtherRequest(Sim_event ev) { |
---|
| 180 | switch (ev.get_tag()) { |
---|
| 181 | case GssimTags.QUERY_RESOURCE_DESC: |
---|
| 182 | ResourceStateDescription desc = new ResourceStateDescription(new LocalSystem(get_name(), null, null)); |
---|
| 183 | Map<ResourceParameterName, ResourceUnit> units = new HashMap<ResourceParameterName, ResourceUnit>(); |
---|
| 184 | //ResourceComponentsNew resourceComponents = new ResourceComponentsNew(get_name()); |
---|
| 185 | //resourceComponents.addAll(resources); |
---|
| 186 | //units.put(ResourceParameterName.RESOURCECOMPONENTS, resourceComponents); |
---|
| 187 | |
---|
| 188 | desc.addResourceUnit(units); |
---|
| 189 | desc.setQueuesSize(managementSystem.getQueuesSize()); |
---|
| 190 | IO_data data = new IO_data(desc, 0, ev.get_src()); |
---|
| 191 | send(ev.get_src(), GridSimTags.SCHEDULE_NOW, GssimTags.QUERY_RESOURCE_DESC_RESULT, data); |
---|
| 192 | break; |
---|
| 193 | |
---|
| 194 | default: |
---|
| 195 | managementSystem.processEvent(ev); |
---|
| 196 | break; |
---|
| 197 | } |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | /*public boolean processOtherEvent(Sim_event ev) { |
---|
| 201 | return false; |
---|
| 202 | }*/ |
---|
| 203 | |
---|
| 204 | protected void processGSSIMJobReturn(Sim_event ev) { |
---|
| 205 | GSSIMJobInterface<?> job = (GSSIMJobInterface<?>) ev.get_data(); |
---|
| 206 | managementSystem.notifyReturnedJob(job); |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | protected void processGSSIMJobSubmit(Sim_event ev, boolean ack) { |
---|
| 210 | GSSIMJobInterface<?> job = (GSSIMJobInterface<?>) ev.get_data(); |
---|
| 211 | managementSystem.notifySubmittedJob(job, ack); |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | public void send(String entityName, double delay, int tag, Object data){ |
---|
| 215 | super.send(entityName, delay, tag, data); |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | public void send(int destID, double delay, int tag){ |
---|
| 219 | super.send(destID, delay, tag); |
---|
| 220 | } |
---|
| 221 | |
---|
| 222 | public void send(int destID, double delay, int tag, Object data){ |
---|
| 223 | super.send(destID, delay, tag, data); |
---|
| 224 | } |
---|
| 225 | |
---|
| 226 | public void send(Sim_port destPort, double delay, int tag, Object data) { |
---|
| 227 | super.send(destPort, delay, tag, data); |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | public void sendInternal(double delay, int tag, Object data) { |
---|
| 231 | this.send(this.get_id(), delay, tag, data); |
---|
| 232 | } |
---|
| 233 | |
---|
| 234 | /*protected void send(int dest, int tag, int transaction, Object obj){ |
---|
| 235 | IO_data data = new Transaction_IO_data(transaction, obj, 8, dest); |
---|
| 236 | super.send(dest, GridSimTags.SCHEDULE_NOW, tag, data); |
---|
| 237 | }*/ |
---|
| 238 | |
---|
| 239 | public Sim_port getOutputPort() { |
---|
| 240 | return output; |
---|
| 241 | } |
---|
| 242 | |
---|
| 243 | public LogicalResource getLogicalResource(String resourceName){ |
---|
| 244 | ComputingResource resourceWithName = null; |
---|
| 245 | for(int i = 0 ; i < resources.size() && resourceWithName == null; i++){ |
---|
| 246 | ComputingResource resource = resources.get(i); |
---|
| 247 | if(resource.getName().equals(resourceName)) |
---|
| 248 | resourceWithName = resource; |
---|
| 249 | else |
---|
| 250 | try { |
---|
| 251 | resourceWithName = resource.getDescendantsByName(resourceName); |
---|
| 252 | } catch (ResourceException e) { |
---|
| 253 | return null; |
---|
| 254 | } |
---|
| 255 | } |
---|
| 256 | if(resourceWithName == null) |
---|
| 257 | return null; |
---|
| 258 | List<ComputingResource> children = resourceWithName.getChildren(); |
---|
| 259 | Set<LogicalResource> childrenControllers = new HashSet<LogicalResource>(); |
---|
| 260 | for(ComputingResource child:children) { |
---|
| 261 | childrenControllers.add(child.getLogicalResource()); |
---|
| 262 | } |
---|
| 263 | Set<LogicalResource> tempChildrenControllers = new HashSet<LogicalResource>(childrenControllers); |
---|
| 264 | while(childrenControllers.size() != 1){ |
---|
| 265 | childrenControllers = new HashSet<LogicalResource>(); |
---|
| 266 | for(LogicalResource lr:tempChildrenControllers){ |
---|
| 267 | childrenControllers.add(lr.getParent()); |
---|
| 268 | } |
---|
| 269 | tempChildrenControllers = new HashSet<LogicalResource>(childrenControllers); |
---|
| 270 | } |
---|
| 271 | Iterator<LogicalResource> it = childrenControllers.iterator(); |
---|
| 272 | LogicalResource potentialLogicalResource = it.next(); |
---|
| 273 | if(potentialLogicalResource.getResources().containsAll(children)) |
---|
| 274 | return potentialLogicalResource; |
---|
| 275 | return null; |
---|
| 276 | |
---|
| 277 | } |
---|
| 278 | |
---|
| 279 | public long getQueueLoad(String queueName) throws NoSuchFieldException{ |
---|
| 280 | Map<String, Integer> queue_size = managementSystem.getQueuesSize(); |
---|
| 281 | long load = 0; |
---|
| 282 | if(queueName == null){ |
---|
| 283 | for(String queue: queue_size.keySet()){ |
---|
| 284 | load += queue_size.get(queue); |
---|
| 285 | } |
---|
| 286 | return load; |
---|
| 287 | } |
---|
| 288 | else if(queue_size.containsKey(queueName)) |
---|
| 289 | return queue_size.get(queueName); |
---|
| 290 | else |
---|
| 291 | throw new NoSuchFieldException("Queue " + queueName + |
---|
| 292 | " is not available in resource " + get_name()); |
---|
| 293 | } |
---|
| 294 | |
---|
| 295 | public ResourceType getType() { |
---|
| 296 | return ResourceType.RESOURCE_PROVIDER; |
---|
| 297 | } |
---|
| 298 | |
---|
| 299 | public ResourceStatus getStatus() { |
---|
| 300 | return status; |
---|
| 301 | } |
---|
| 302 | |
---|
| 303 | public void setStatus(ResourceStatus newStatus) { |
---|
| 304 | status = newStatus; |
---|
| 305 | } |
---|
| 306 | } |
---|