📄 advancereservation.java
字号:
} /** * Querys to a resource regarding to list of busy time during a period of * time. Each object inside ArrayList is <tt>long array[3]</tt>, with: * <ul> * <li> array[0] = start time * <li> array[1] = duration time * <li> array[2] = number of PEs * </ul> * * @param resourceID a resource ID * @param from starting time in milliseconds * @param to ending time in milliseconds * @return ArrayList object or <tt>null</tt> if error occurs * @pre resourceID != null * @pre from > 0 * @pre to > 0 * @post $none */ public ArrayList queryBusyTime(Integer resourceID, long from, long to) { if (resourceID == null) { return null; } return queryBusyTime(resourceID.intValue(), from, to ); } /** * Gets a reservation object based on the given booking ID * @param bookingID a reservation booking ID * @return ARObject object or <tt>null</tt> if an error occurs * @pre bookingID != null * @post $none */ public ARObject getReservation(String bookingID) { int[] id = parseBookingID(bookingID); if (id == null) { return null; } ARObject obj = searchBooking(id[0], id[1]); return obj; } /** * Queries the overall status of a reservation. * A return value for this method uses one of * of GridSimTags.AR_STATUS_XXXX tags, where XXXX = specific tag name. * <p> * To find out the status for a specific Gridlet, use * {@link gridsim.GridSim#gridletStatus(int, int, int)} method instead. * * @param bookingID this reservation booking ID * @return an integer tag that denotes success or failure. * @see gridsim.GridSimTags * @see gridsim.GridSim#gridletStatus(int, int, int) * @see gridsim.GridSim#gridletStatus(Gridlet, int) * @pre bookingID != null * @post $none */ public int queryReservation(String bookingID) { // id[0] = resID, [1] = reservID, [2] = trans ID int[] id = parseBookingID(bookingID); if (id == null) { return GridSimTags.AR_STATUS_ERROR_INVALID_BOOKING_ID; } // search from the list first ARObject obj = searchBooking(id[0], id[1]); if (obj == null) { return GridSimTags.AR_STATUS_ERROR_INVALID_BOOKING_ID; } // if a reservation hasn't been committed else if (obj.hasCommitted() == false) { return GridSimTags.AR_STATUS_NOT_COMMITTED; } // if the reservation status is one of the final states, then no need // to enquiry a resource int status = obj.getStatus(); // if a reservation has been committed, then need to ask a resource id[2] = incrementID(); // transaction id id[3] = super.get_id(); // send to grid resource to query about the reservation id super.send(super.output, 0.0, GridSimTags.SEND_AR_QUERY, new IO_data(id, SIZE_ARRAY, id[0]) ); // waiting for a response from the GridResource FilterResult tag = new FilterResult(id[2], GridSimTags.RETURN_AR_QUERY_STATUS); // only look for this type of ack for same reservation ID Sim_event ev = new Sim_event(); super.sim_get_next(tag, ev); // get the result back from GridResource or AllocPolicy object try { int[] array = (int[]) ev.get_data(); // [0] = trans ID, [1] = result status = array[1]; } catch (Exception e) { status = GridSimTags.AR_STATUS_ERROR; } obj.setStatus(status); // put the latest status return status; } /** * Commits a reservation only <b>without</b> sending any Gridlet objects. * Once a commit has been successfull, sending the Gridlet objects must be * done by using {@link #commitReservation(String, GridletList)} or * {@link #commitReservation(String, Gridlet)}. * A return value for this method uses one of * of GridSimTags.AR_COMMIT_XXXX tags, where XXXX = specific tag name. * * @param bookingID a reservation booking ID * @return an integer tag that denotes success or failure. * @see gridsim.GridSimTags * @see gridsim.AdvanceReservation#commitReservation(String, Gridlet) * @see gridsim.AdvanceReservation#commitReservation(String, GridletList) * @pre bookingID != null * @post $none */ public int commitReservation(String bookingID) { int result = 0; // id[0] = resID, [1] = reservID, [2] = trans ID int[] id = parseBookingID(bookingID); if (id == null) { result = GridSimTags.AR_COMMIT_FAIL_INVALID_BOOKING_ID; } else { id[2] = incrementID(); // transaction id id[3] = super.get_id(); // this entity or sender id // sends to a destinated GridResource super.send(super.output, 0.0, GridSimTags.SEND_AR_COMMIT_ONLY, new IO_data(id, SIZE_ARRAY, id[0]) ); // waiting for a response from the GridResource with an unique tag // of transaction id FilterResult tag = new FilterResult(id[2], GridSimTags.RETURN_AR_COMMIT); // only look for this type of ack for same Gridlet ID Sim_event ev = new Sim_event(); super.sim_get_next(tag, ev); try { // get the result back int[] array = (int[]) ev.get_data(); //[0]=trans ID, [1]=result result = array[1]; // update the booking list and return the new result result = updateCommitList(id[0], id[1], result); } catch (Exception e) { result = GridSimTags.AR_COMMIT_ERROR; } } return result; } /** * Commits a reservation together <b>with</b> a list of Gridlet objects. * A return value for this method uses one of * of GridSimTags.AR_COMMIT_XXXX tags, where XXXX = specific tag name. * * @param bookingID a reservation booking ID * @param list a list of Gridlet objects. Each Gridlet's user ID, by * default is set to this entity ID. * @return an integer tag that denotes success or failure. * @pre bookingID != null * @pre list != null * @post $none * @see gridsim.Gridlet#setUserID(int) * @see gridsim.GridSimTags */ public int commitReservation(String bookingID, GridletList list) { // id[0] = resID, [1] = reservID, [2] = trans ID int[] id = parseBookingID(bookingID); if (id == null) { return GridSimTags.AR_COMMIT_FAIL_INVALID_BOOKING_ID; } // checks if the list is empty or not int result = 0; if (list == null) { result = GridSimTags.AR_COMMIT_FAIL; } else { int size = calculateTotalGridletSize(list); if (size == 0) { result = GridSimTags.AR_COMMIT_FAIL; } else { result = commit(id[0], id[1], list, size); } } return result; } /** * Commits a reservation together <b>with</b> a Gridlet object. * A return value for this method uses one of * of GridSimTags.AR_COMMIT_XXXX tags, where XXXX = specific tag name. * * @param bookingID a reservation booking ID * @param obj a Gridlet object. A Gridlet's user ID, by default is * set to this entity ID. * @return an integer tag that denotes success or failure. * @pre bookingID != null * @pre obj != null * @post $none * @see gridsim.Gridlet#setUserID(int) * @see gridsim.GridSimTags */ public int commitReservation(String bookingID, Gridlet obj) { // id[0] = resID, [1] = reservID, [2] = trans ID int[] id = parseBookingID(bookingID); if (id == null) { return GridSimTags.AR_COMMIT_FAIL_INVALID_BOOKING_ID; } int result = 0; if (obj == null) { result = GridSimTags.AR_COMMIT_FAIL; } else { obj.setUserID( super.get_id() ); result = commit( id[0], id[1], obj, obj.getGridletFileSize() ); } return result; } //////////////////////////////////// PRIVATE METHODS //////////////////// /** * Initialises all the private attributes * @pre $none * @post $none */ private void init() { initTime_ = GridSim.getSimulationCalendar().getTimeInMillis(); booking_ = new ArrayList(); transactionID_ = 0; } /** * Increment a transaction id for each reservation method that * communicate with a resource * @return a unique transaction id * @pre $none * @post $result > 1 */ private int incrementID() { transactionID_++; return transactionID_; } /** * Validates or checks whether one or more given parameters are valid or not * @param startTime a reservation start time * @param endTime a reservation end time * @param numPE number of PE required by a reservation * @param resID a resource ID * @param ar true if it is AR, false if it is immediate reservation * @return an error message or <tt>null</tt> if the parameters are all valid * @pre $none * @post $none */ private String validateValue(long startTime, long endTime, int numPE, int resID, boolean ar) { // current time = time to create this object + simulation time long currentTime = initTime_ + (int) (GridSim.clock() * MILLI_SEC); // checks the start time of a reservation. start time = 0 means // it is an immediate reservation rather than AR. if (ar == false && startTime < 0) { return "AR_CREATE_ERROR_INVALID_START_TIME"; } else if (ar == true && startTime < currentTime) { return "AR_CREATE_ERROR_INVALID_START_TIME"; } // checks the end time of a reservation. end time = 0 means it is an // immediate reservation rather than AR. if (ar == false && endTime < 0) { return "AR_CREATE_ERROR_INVALID_END_TIME"; } else if (ar == true) { if (endTime < currentTime || endTime < startTime) { return "AR_CREATE_ERROR_INVALID_END_TIME"; } } // if num PE less than 1 if (numPE < 1) { return "AR_CREATE_ERROR_INVALID_NUM_PE"; } // if a resource doesn't exist if (GridSim.isResourceExist(resID) == false) { return "AR_CREATE_ERROR_INVALID_RESOURCE_ID"; } // check whether a resource supports AR or not if (GridSim.resourceSupportAR(resID) == false) { return "AR_CREATE_ERROR_FAIL_RESOURCE_CANT_SUPPORT"; } // if no errors, then return null return null; } /** * Validates or checks whether one or more given parameters are valid or not * @param startTime a reservation start time * @param duration a reservation duration time * @param numPE number of PE required by a reservation * @param resID a resource ID * @param ar true if it is AR, false if it is immediate reservation * @return an error message or <tt>null</tt> if the parameters are all valid * @pre $none * @post $none */ private String validateValue(long startTime, int duration, int numPE, int resID, boolean ar) { // current time = time to create this object + simulation time long currentTime = initTime_ + (int) (GridSim.clock()*MILLI_SEC); // checks the start time of a reservation. start time = 0 means it is // an immediate reservation not AR. if (ar == false && startTime < 0) { return "AR_CREATE_ERROR_INVALID_START_TIME"; } if (ar == true && startTime < currentTime) { return "AR_CREATE_ERROR_INVALID_START_TIME"; } // checks the duration time of a reservation. duration = 0 means // it is an immediate reservation not AR. if (ar == false && duration < 0) { r
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -