📄 arsimplespaceshared.java
字号:
/* * Title: GridSim Toolkit * Description: GridSim (Grid Simulation) Toolkit for Modeling and Simulation * of Parallel and Distributed Systems such as Clusters and Grids * Licence: GPL - http://www.gnu.org/copyleft/gpl.html * * $Id: ARSimpleSpaceShared.java,v 1.11 2005/10/21 09:23:20 anthony Exp $ */package gridsim;import java.util.*;import eduni.simjava.Sim_event;import eduni.simjava.Sim_system;/** * This is a resource scheduler that handles Advanced Reservation * functionalities. This scheduler is also able to handle submitted jobs without * reserving in the first place. * <p> * As this class name suggested, this scheduler is only able to handle some * basic AR functionalities, such as: * <ul> * <li> process a new advanced reservation functionality * <li> process a new immediate reservation functionality * <li> process a cancel reservation functionality * <li> process a commit reservation functionality * <li> process a reservation status functionality * </ul> * <p> * There are some limitations on this scheduler: * <ul> * <li> only able to handle 1 Gridlet on each PE throughout the entire * reservation period. This means that if a * reservation books 3 PEs, then it should submit only 3 Gridlets. * If a reservation sends, e.g. 5 Gridlets, the remaining 2 Gridlets * will be ignored during a commit reservation phase. * <li> not able to split a Gridlet to run into more than one PE if empty * PEs are available. * <li> not able to list busy and free time for a certain period of time. * <li> not able to modify an existing reservation. * </ul> * * @author Anthony Sulistio * @since GridSim Toolkit 3.0 * @see gridsim.GridSim * @see gridsim.ResourceCharacteristics * @invariant $none */public class ARSimpleSpaceShared extends ARPolicy{ // NOTE: gridletQueueList_ is for non-AR jobs, where gridletWaitingList_ // is for AR jobs. private ResGridletList gridletQueueList_; // Queue list private ResGridletList gridletInExecList_; // Execution list private ResGridletList gridletPausedList_; // Pause list private ResGridletList gridletWaitingList_; // waiting list for AR private double lastUpdateTime_; // the last time Gridlets updated private int[] machineRating_; // list of machine ratings available private ArrayList reservList_; // a new reservation list private ArrayList expiryList_; // a list that contains expired reservations private int reservID_; // reservation ID private int commitPeriod_; // default booking/reservation commit period private final int SUCCESS = 1; // a constant to denote success private final int NOT_FOUND = -1; // a constant to denote not found private final int EMPTY = -88888888; // a constant regarding to empty val private final int EXPIRY_TIME = 2; // a constant to denote expiry time private final int PERFORM_RESERVATION = 3; // a constant /** * Creates a new scheduler that handles advanced reservations. This * scheduler uses First Come First Serve (FCFS) algorithm. * A default commit period time for a user to commit a reservation * is set to 30 minutes. * @param resourceName the GridResource entity name that will contain * this allocation policy * @param entityName this object name. The name of this entity will * be "resourceName_entityName". * @throws Exception This happens when one of the following scenarios occur: * <ul> * <li> creating this entity before initializing GridSim package * <li> this entity name is <tt>null</tt> or empty * <li> this entity has <tt>zero</tt> number of PEs (Processing * Elements). <br> * No PEs mean the Gridlets can't be processed. * A GridResource must contain one or more Machines. * A Machine must contain one or more PEs. * </ul> * @see gridsim.GridSim#init(int, Calendar, boolean) * @see gridsim.GridSim#init(int, Calendar, boolean, String[], String[], * String) * @pre resourceName != null * @pre entityName != null * @post $none */ public ARSimpleSpaceShared(String resourceName, String entityName) throws Exception { super(resourceName, entityName); commitPeriod_ = 30*60; // set the expiry time into 30 mins init(); } /** * Creates a new scheduler that handles advanced reservations. This * scheduler uses First Come First Serve (FCFS) algorithm. * @param resourceName the GridResource entity name that will contain * this allocation policy * @param entityName this object name. The name of this entity will * be "resourceName_entityName". * @param commitPeriod a default commit period time for a user to commit * a reservation (unit is in second). NOTE: once it is * set, you can not change the time again. * @throws Exception This happens when one of the following scenarios occur: * <ul> * <li> creating this entity before initializing GridSim package * <li> this entity name is <tt>null</tt> or empty * <li> this entity has <tt>zero</tt> number of PEs (Processing * Elements). <br> * No PEs mean the Gridlets can't be processed. * A GridResource must contain one or more Machines. * A Machine must contain one or more PEs. * </ul> * @see gridsim.GridSim#init(int, Calendar, boolean) * @see gridsim.GridSim#init(int, Calendar, boolean, String[], String[], * String) * @pre resourceName != null * @pre entityName != null * @post $none */ public ARSimpleSpaceShared(String resourceName, String entityName, int commitPeriod) throws Exception { super(resourceName, entityName); if (commitPeriod <= 0) { throw new Exception(resourceName + "." + entityName + ": Error -" + " Invalid expiry time."); } commitPeriod_ = commitPeriod; init(); } /** * Handles a modify reservation request (NOTE: <b>NOT YET SUPPORTED</b>). * @param obj a reservation object * @param senderID a sender or user ID * @param sendTag a tag to send to the user * @pre obj != null * @pre senderID > 0 * @post $none */ public void handleModifyReservation(ARObject obj,int senderID,int sendTag) { System.out.println(super.get_name() + ".handleModifyReservation(): not supported at the moment."); super.replyModifyReservation(senderID, sendTag, GridSimTags.AR_MODIFY_FAIL_RESOURCE_CANT_SUPPORT); } /** * Handles an immediate reservation request.<br> * NOTE: * <ul> * <li>currently able to handle a case where start time = 0 and * duration or end time > 0. * <li>this scheduler <b>is not able to handle</b> a case where * start time = 0 and duration or end time = 0. * </ul> * @param obj a reservation object * @param senderID a sender or user ID * @param sendTag a tag to send the result back to user * @pre obj != null * @pre senderID > 0 * @post $none */ public void handleImmediateReservation(ARObject obj, int senderID, int sendTag) { // check whether the requested PE is sufficient to handle or not if (obj.getNumPE() > super.totalPE_) { super.replyCreateReservation(senderID, sendTag, NOT_FOUND, GridSimTags.AR_CREATE_FAIL_RESOURCE_NOT_ENOUGH_PE); return; } // at the moment, this scheduler doesn't support immediate reservations // with duration or end time = 0. if (obj.getDurationTime() <= 0) { System.out.println(super.get_name() + ".handleImmediateReservation(): Error - can't handle " + "duration or end time = 0."); super.replyCreateReservation(senderID, sendTag, NOT_FOUND, GridSimTags.AR_CREATE_ERROR_INVALID_END_TIME); return; } // for immediate reservations, start time is the current time long startTime = super.getCurrentTime(); obj.setStartTime(startTime); // determine when it will be expired. For immediate reservation, // expiry time is the finish or end time long expTime = startTime + (obj.getDurationTime() * super.MILLI_SEC); // check the start time against the availability // case 1: if the list is empty if (reservList_.size() == 0) { acceptReservation(obj, 0, senderID, sendTag, expTime, false); } else { // case 2: if list is not empty, find the available slot int pos = findEmptySlot(startTime, expTime, obj.getNumPE()); // able to find empty slot(s) if (pos > NOT_FOUND) { acceptReservation(obj, pos, senderID, sendTag, expTime, false); } // not able to find any slots. Here pos contains a busy time tag. else { super.replyCreateReservation(senderID, sendTag, NOT_FOUND, pos); } } } /** * Handles an advanced reservation request. * @param obj a reservation object * @param senderID a sender or user ID * @param sendTag a tag to send the result back to user * @pre obj != null * @pre senderID > 0 * @post $none */ public void handleCreateReservation(ARObject obj,int senderID,int sendTag) { // check whether the requested PE is sufficient to handle or not if ( obj.getNumPE() > super.totalPE_ ) { super.replyCreateReservation(senderID, sendTag, NOT_FOUND, GridSimTags.AR_CREATE_FAIL_RESOURCE_NOT_ENOUGH_PE); return; } // convert the start time from user's time into local time long startTime = AdvanceReservation.convertTimeZone(obj.getStartTime(), obj.getTimeZone(), resource_.getResourceTimeZone()); // get the current time long currentTime = super.getCurrentTime(); // check whether the start time has passed or not if (startTime < currentTime) { super.replyCreateReservation(senderID, sendTag, NOT_FOUND, GridSimTags.AR_CREATE_ERROR_INVALID_START_TIME); return; } // then overrides the object start time into local time obj.setStartTime(startTime); // determine when it will be expired long expTime = currentTime + (commitPeriod_ * super.MILLI_SEC); // check the start time against the availability // case 1: if the list is empty if (reservList_.size() == 0) { acceptReservation(obj, 0, senderID, sendTag, expTime, true); } else { // case 2: if list is not empty, find the available slot // remember that duration time is in second. Then need to convert // into milli seconds long endTime = startTime + (obj.getDurationTime()*super.MILLI_SEC); int pos = findEmptySlot(startTime, endTime, obj.getNumPE()); // able to find empty slot(s) if (pos > NOT_FOUND) { acceptReservation(obj, pos, senderID, sendTag, expTime, true); } // not able to find any slots. Here pos contains a busy time tag. else { super.replyCreateReservation(senderID, sendTag, NOT_FOUND, pos); } } } /** * Handles a cancel reservation request for a given Gridlet ID list. * @param reservationID a reservation ID * @param senderID a sender ID * @param list a list of Gridlet IDs * @param sendTag a tag to send the result back to user * @pre reservationID > 0 * @pre senderID > 0 * @pre list != null * @post $none */ public void handleCancelReservation(int reservationID, int senderID, ArrayList list, int sendTag) { // case 1: check whether a reservation exists or not int result = 0; int index = super.searchReservation(reservList_, reservationID); if (index == NOT_FOUND) { // check on expiry list index = super.searchReservation(expiryList_, reservationID); if (index == NOT_FOUND) { result = GridSimTags.AR_CANCEL_FAIL_INVALID_BOOKING_ID; } else { result = GridSimTags.AR_CANCEL_SUCCESS; } super.replyCancelReservation(senderID, sendTag, result); return; } // get AR object ARObject ar = (ARObject) reservList_.get(index); // if in the record contains no Gridlets committed, then exit if (ar.getTotalGridlet() == 0) { result = GridSimTags.AR_CANCEL_FAIL; super.replyCancelReservation(senderID, sendTag, result); return; } try { // a loop that cancels each Gridlet in the list Integer obj = null; Iterator it = list.iterator(); while ( it.hasNext() ) { obj = (Integer) it.next(); result = cancelReservation(obj.intValue(), senderID); } } catch (Exception e) { result = GridSimTags.AR_CANCEL_ERROR; } super.replyCancelReservation(senderID, sendTag, result); } /** * Handles a cancel reservation request. All Gridlets will be cancelled.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -