📄 gridresourcewithfailure.java
字号:
resource_ = resource; resCalendar_ = calendar; // the order between policy and init() is important policy_ = (AllocPolicy) policy; init(); } ///////////////////////////////////////////////// /** * Sets a regional GridInformationService (GIS) entity for this resource * to communicate with. This resource will then register its ID to the * regional GIS entity, rather than {@link GridInformationService} or * <tt>system GIS</tt>. * @param regionalGIS name of regional GIS entity * @return <tt>true</tt> if it is successful, <tt>false</tt> otherwise * @pre regionalGIS != null * @post $none */ public boolean setRegionalGIS(String regionalGIS) { if (regionalGIS == null || GridSim.getEntityId(regionalGIS) == -1) { return false; } regionalGISName_ = regionalGIS; return true; } /** * Sets a regional GridInformationService (GIS) entity for this resource * to communicate with. This resource will then register its ID to the * regional GIS entity, rather than {@link GridInformationService} or * <tt>system GIS</tt>. * @param gis regional GIS entity object * @return <tt>true</tt> if it is successful, <tt>false</tt> otherwise * @pre gis != null * @post $none */ public boolean setRegionalGIS(AbstractGIS gis) { if (gis == null) { return false; } return setRegionalGIS( gis.get_name() ); } /** * Asks this resource to record its activities. <br> * NOTE: this method should be called <b>BEFORE</b> the simulation starts. * If an existing file exists, the new activities will be appended at the * end. The file name is this entity name. * * @param trace <tt>true</tt> if you want to record this resource's * activities, <tt>false</tt> otherwise */ public void setTrace(boolean trace) { record_ = trace; initializeReportFile(); } /** * Handles external events that are coming to this GridResourceWithFailure * entity. This method also registers the identity of this * GridResourceWithFailure entity to <tt>GridInformationService</tt> class. * <p> * The services or tags available for this resource are: * <ul> * <li> {@link gridsim.GridSimTags#RESOURCE_CHARACTERISTICS} </li> * <li> {@link gridsim.GridSimTags#RESOURCE_DYNAMICS} </li> * <li> {@link gridsim.GridSimTags#GRIDLET_SUBMIT} </li> * <li> {@link gridsim.GridSimTags#GRIDLET_CANCEL} </li> * <li> {@link gridsim.GridSimTags#GRIDLET_PAUSE} </li> * <li> {@link gridsim.GridSimTags#GRIDLET_RESUME} </li> * <li> {@link gridsim.GridSimTags#GRIDLET_MOVE} </li> * <li> {@link gridsim.GridSimTags#GRIDLET_STATUS} </li> * </ul> * <br> * This method also calls these methods in the following order: * <ol> * <li> {@link #registerOtherEntity()} method * <li> {@link #processOtherEvent(Sim_event)} method * </ol> * * @pre $none * @post $none */ public void body() { // send the registration to GIS int register = 0; if (policyType_ == ResourceCharacteristics.ADVANCE_RESERVATION) { register = GridSimTags.REGISTER_RESOURCE_AR; } else { register = GridSimTags.REGISTER_RESOURCE; } // this resource should register to regional GIS. // However, if not specified, then register to system GIS (the // default GridInformationService) entity. int gisID = GridSim.getEntityId(regionalGISName_); if (gisID == -1) { gisID = GridSim.getGridInfoServiceEntityId(); } // need to wait for few seconds before registering to a regional GIS. // This is because to allow all routers to fill in their routing tables else { super.sim_pause(GridSim.PAUSE); System.out.println(super.get_name() + ".body(): wait for " + GridSim.PAUSE + " seconds before registering to " + regionalGISName_); } // send the registration to GIS super.send(super.output, GridSimTags.SCHEDULE_NOW, register, new IO_data(new Integer(super.get_id()), SIZE, gisID) ); // Below method is for a child class to override registerOtherEntity(); // Process events until END_OF_SIMULATION is received from the // GridSimShutdown Entity Sim_event ev = new Sim_event(); while ( Sim_system.running() ) { super.sim_get_next(ev); // if the simulation finishes then exit the loop if (ev.get_tag() == GridSimTags.END_OF_SIMULATION) { System.out.println(get_name()+ ": end of simulation..."); policy_.setEndSimulation(); break; } // process the received event processEvent(ev); } // remove I/O entities created during construction of this entity super.terminateIOEntities(); } //////////////////// PROTECTED METHODS /////////////////////////////////// /** * Overrides this method when making a new and different type of resource. * This method is called by {@link #body()} for incoming unknown tags. * <p> * Another approach is to override the * {@link gridsim.AllocPolicy#processOtherEvent(Sim_event)} method. * This approach is desirable if you do not want to create a new type of * grid resource. * * @param ev a Sim_event object * @pre ev != null * @post $none */ protected void processOtherEvent(Sim_event ev) { if (ev == null) { System.out.println(super.get_name() + ".processOtherEvent(): " + "Error - an event is null."); return; } /**** // NOTE: now a resource passes a new event to the scheduler System.out.println(super.get_name()+".processOtherEvent(): Unable to " + "handle request from GridSimTags with tag number " + ev.get_tag() ); *****/ policy_.processOtherEvent(ev); } /** * Overrides this method when making a new and different type of resource. * This method is called by {@link #body()} to register other type to * {@link gridsim.GridInformationService} entity. In doing so, you * need to create a new child class extending from * {@link gridsim.GridInformationService}. * <br> * <b>NOTE:</b> You do not need to override {@link #body()} method, if * you use this method. * * @pre $none * @post $none * @see gridsim.GridInformationService */ protected void registerOtherEntity() { // empty. This should be override by a child class } //////////////////// PRIVATE METHODS /////////////////////////////////// /** * Initializes the resource allocation policy * @throws Exception If number of PEs is zero * @pre $none * @post $none */ private void init() throws Exception { // If this resource doesn't have any PEs then no useful at all if (resource_.getNumPE() == 0) { throw new Exception(super.get_name() + " : Error - this entity " + "has no PEs. Therefore, can't process any Gridlets."); } // stores id of this class resource_.setResourceID( super.get_id() ); // if internal allocation policy is used policyType_ = resource_.getResourceAllocationPolicy(); if (policy_ == null) { switch (policyType_) { case ResourceCharacteristics.TIME_SHARED: policy_ = new TimeSharedWithFailure(super.get_name(), "TimeShared"); break; case ResourceCharacteristics.SPACE_SHARED: policy_ = new SpaceSharedWithFailure(super.get_name(), "SpaceShared"); break; default: throw new Exception(super.get_name()+" : Error - supports"+ " only TimeShared or SpaceShared policy."); } } policy_.init(resource_, resCalendar_, super.output); record_ = false; } /** * Processes events or services that are available for this * GridResourceWithFailure * @param ev a Sim_event object * @pre ev != null * @post $none */ private void processEvent(Sim_event ev) { int src_id = -1; // GRIDRESOURCE_RECOVERY and GRIDRESOURCE_FAILURE_INFO (polling request) // are ALWAYS processed. if (ev.get_tag() == GridSimTags.GRIDRESOURCE_RECOVERY) { processRecovery(ev); } else if (ev.get_tag() == GridSimTags.GRIDRESOURCE_FAILURE_INFO) { processPolling(ev); } // Only if the resource is not failed, then process other events if (getResourceFailed() == false) { switch (ev.get_tag()) { // Resource characteristics inquiry case GridSimTags.RESOURCE_CHARACTERISTICS: src_id = ((Integer) ev.get_data()).intValue(); super.send(super.output, 0.0, ev.get_tag(), new IO_data(resource_, resource_.getByteSize(), src_id)); break; // Resource dynamic info inquiry case GridSimTags.RESOURCE_DYNAMICS: src_id = ((Integer) ev.get_data()).intValue(); super.send(super.output, 0.0, ev.get_tag(), new IO_data(policy_.getTotalLoad(), Accumulator.getByteSize(), src_id)); break; case GridSimTags.RESOURCE_NUM_PE: src_id = ((Integer) ev.get_data()).intValue(); int numPE = resource_.getNumPE(); super.send(super.output, 0.0, ev.get_tag(), new IO_data(new Integer(numPE), SIZE, src_id)); break; case GridSimTags.RESOURCE_NUM_FREE_PE: src_id = ((Integer) ev.get_data()).intValue(); int numFreePE = resource_.getNumFreePE(); super.send(super.output, 0.0, ev.get_tag(), new IO_data(new Integer(numFreePE), SIZE, src_id)); break; // New Gridlet arrives case GridSimTags.GRIDLET_SUBMIT: processGridletSubmit(ev, false); break; // New Gridlet arrives, but the sender asks for an ack case GridSimTags.GRIDLET_SUBMIT_ACK: processGridletSubmit(ev, true); break; // Cancels a previously submitted Gridlet case GridSimTags.GRIDLET_CANCEL: processGridlet(ev, GridSimTags.GRIDLET_CANCEL); break; // Pauses a previously submitted Gridlet case GridSimTags.GRIDLET_PAUSE: processGridlet(ev, GridSimTags.GRIDLET_PAUSE); break; // Pauses a previously submitted Gridlet, but the sender // asks for an acknowledgement case GridSimTags.GRIDLET_PAUSE_ACK: processGridlet(ev, GridSimTags.GRIDLET_PAUSE_ACK); break; // Resumes a previously submitted Gridlet case GridSimTags.GRIDLET_RESUME: processGridlet(ev, GridSimTags.GRIDLET_RESUME); break; // Resumes a previously submitted Gridlet, but the sender // asks for an acknowledgement case GridSimTags.GRIDLET_RESUME_ACK: processGridlet(ev, GridSimTags.GRIDLET_RESUME_ACK); break; // Moves a previously submitted Gridlet to a different res case GridSimTags.GRIDLET_MOVE: processGridletMove(ev, GridSimTags.GRIDLET_MOVE); break; // Moves a previously submitted Gridlet to a different res case GridSimTags.GRIDLET_MOVE_ACK: processGridletMove(ev, GridSimTags.GRIDLET_MOVE_ACK); break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -