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