📄 argridresource.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: ARGridResource.java,v 1.19 2005/10/21 09:23:20 anthony Exp $ */package gridsim;import gridsim.*;import gridsim.net.Link;import java.util.LinkedList;import java.util.ArrayList;import eduni.simjava.Sim_event;import eduni.simjava.Sim_system;/** * ARGridResource class handles all Advanced Reservation functionalities. * All the functionalities are done by its internal or private methods.<br> * <b>NOTE:</b> It is important to set the allocation policy inside * {@link gridsim.ResourceCharacteristics} object into * {@link gridsim.ResourceCharacteristics#ADVANCE_RESERVATION}. In addition, * a resource's scheduler should be extending from * {@link gridsim.ARPolicy} class. * * @author Anthony Sulistio * @since GridSim Toolkit 3.0 * @see gridsim.GridResource * @see gridsim.GridSim * @see gridsim.ResourceCharacteristics * @see gridsim.AllocPolicy * @see gridsim.ARPolicy * @invariant $none */public class ARGridResource extends GridResource{ /** * Allocates a new GridResource object that supports Advanced Reservation. * @param name the name to be associated with this entity (as * required by Sim_entity class from simjava package) * @param baud_rate network communication or bandwidth speed * @param resource an object of ResourceCharacteristics * @param calendar an object of ResourceCalendar * @param policy a scheduler for this Grid resource. The scheduler * should be able to handle Advanced Reservations. * @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.AllocPolicy * @see gridsim.GridSim#init(int, Calendar, boolean, String[], String[], * String) * @see gridsim.GridSim#init(int, Calendar, boolean) * @pre $none * @post $none */ public ARGridResource(String name, double baud_rate, ResourceCharacteristics resource, ResourceCalendar calendar, ARPolicy policy) throws Exception { super(name, baud_rate, resource, calendar, policy); } /** * Allocates a new GridResource object that supports Advanced Reservation. * @param name the name to be associated with this entity (as * required by Sim_entity class from simjava package) * @param link the link that will be used to connect this * ARGridResource to another Entity or Router. * @param resource an object of ResourceCharacteristics * @param calendar an object of ResourceCalendar * @param policy a scheduler for this Grid resource. The scheduler * should be able to handle Advanced Reservations. * @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.AllocPolicy * @see gridsim.GridSim#init(int, Calendar, boolean, String[], String[], * String) * @see gridsim.GridSim#init(int, Calendar, boolean) * @pre $none * @post $none */ public ARGridResource(String name, Link link, ResourceCharacteristics resource, ResourceCalendar calendar, ARPolicy policy) throws Exception { super(name, link, resource, calendar, policy); } /////////////////////// PROTECTED METHOD ///////////////////////////////// /** * Processes other events or services related to Advanced Reservations. * This method overrides from a parent class. * <p> * The services or tags available for this resource are: * <ul> * <li> GridSimTags.SEND_AR_CREATE * <li> GridSimTags.SEND_AR_COMMIT_ONLY * <li> GridSimTags.SEND_AR_COMMIT_WITH_GRIDLET * <li> GridSimTags.SEND_AR_CANCEL * <li> GridSimTags.SEND_AR_QUERY * <li> GridSimTags.SEND_AR_MODIFY * <li> GridSimTags.SEND_AR_LIST_BUSY_TIME * <li> GridSimTags.SEND_AR_LIST_FREE_TIME * </ul> * * @param ev a Sim_event object * @pre ev != null * @post $none * @see gridsim.GridResource#processOtherEvent(Sim_event) */ protected void processOtherEvent(Sim_event ev) { switch ( ev.get_tag() ) { case GridSimTags.SEND_AR_CREATE: case GridSimTags.SEND_AR_CREATE_IMMEDIATE: handleCreateReservation(ev); break; case GridSimTags.SEND_AR_COMMIT_ONLY: handleCommitOnly(ev); break; case GridSimTags.SEND_AR_COMMIT_WITH_GRIDLET: handleCommitReservation(ev); break; case GridSimTags.SEND_AR_CANCEL: handleCancelReservation(ev); break; case GridSimTags.SEND_AR_QUERY: handleQueryReservation(ev); break; case GridSimTags.SEND_AR_MODIFY: handleModifyReservation(ev); break; case GridSimTags.SEND_AR_LIST_BUSY_TIME: case GridSimTags.SEND_AR_LIST_FREE_TIME: handleQueryTime(ev); break; default: /**** // NOTE: Give to the scheduler to process other tags System.out.println(super.get_name() + ".processOtherEvent(): " + "Unable to handle request from GridSimTags with " + "tag number " + ev.get_tag() ); *****/ super.policy_.processOtherEvent(ev); break; } } //////////////////////////////// PRIVATE METHODS ///////////////////////// /** * Checks whether this resource can support Advanced Reservations or not * @param src the sender ID * @param returnTag the tag ID to be returned to sender * @param transID the transaction ID of a sender * @param errorTag type of error tag * @param msg error message * @return <tt>true</tt> if all valid, <tt>false</tt> otherwise * @pre $none * @post $none */ private boolean checkResourceType(int src, int returnTag, int transID, int errorTag, String msg) { boolean result = true; if (policyType_ != ResourceCharacteristics.ADVANCE_RESERVATION) { System.out.println(super.get_name() + " : Error - " + msg + ". This resource doesn't support Advance Reservation."); result = false; int MAX = 2; // max. array size IO_data data = null; int size = -1; // size of an object to be sent // if returning create AR event, then use a different array type if (returnTag == GridSimTags.RETURN_AR_CREATE) { long[] sendArray = new long[MAX + 1]; sendArray[0] = transID; // [0] = transaction ID sendArray[1] = errorTag; // [1] = reservation ID sendArray[2] = -1; // [2] = expiry time size = (3*8) + 6; // 3 longs + overhead data = new IO_data(sendArray, size, src); } else { int[] array = new int[MAX]; array[0] = transID; // [0] = transaction ID array[1] = errorTag; // [1] = result size = (2*4) + 2; // 2 ints + overhead data = new IO_data(array, size, src); } // send back the array to sender super.send(src, 0.0, returnTag, data); } return result; } /** * Handles a query reservation request. * @param ev Sim_event object * @pre ev != null * @post $none */ private void handleQueryReservation(Sim_event ev) { int src = -1; // the sender id boolean success = false; int tag = -1; try { // id[0] = resID, [1] = reservID, [2] = trans ID, [3] = sender ID int[] obj = ( int[] ) ev.get_data(); int reservID = obj[1]; // get the reservation ID src = obj[3]; // get the sender ID int returnTag = GridSimTags.RETURN_AR_QUERY_STATUS; tag = returnTag + obj[2]; // return tag + transaction ID // check whether this resource can support AR or not success = checkResourceType(src, returnTag, obj[2], GridSimTags.AR_STATUS_ERROR, "can't query a reservation"); // if this resource doesn't support AR then exit if (success == false) { return; } else { ((ARPolicy) policy_).handleQueryReservation(reservID, src, tag); } } catch (ClassCastException c) { success = false; } catch (Exception e) { success = false; } // if there is an exception, then send back an error msg if (success == false && tag != -1) { System.out.println(super.get_name() + " : Error - can't query a " + "new reservation."); super.send( src, 0.0, GridSimTags.RETURN_AR_QUERY_STATUS, new IO_data(new Integer(GridSimTags.AR_STATUS_ERROR),SIZE,src)); } } /** * Handles a modify reservation request. * @param ev Sim_event object * @pre ev != null * @post $none */ private void handleModifyReservation(Sim_event ev) { int src = -1; // the sender id boolean success = false; int tag = -1; try { // get the data ARObject obj = (ARObject) ev.get_data(); // get the unique tag to send back to recepient int returnTag = GridSimTags.RETURN_AR_MODIFY; tag = returnTag + obj.getTransactionID(); src = obj.getUserID(); // get the sender ID // check whether this resource can support AR or not success = checkResourceType(src, returnTag, obj.getTransactionID(), GridSimTags.AR_MODIFY_FAIL_RESOURCE_CANT_SUPPORT, "can't modify an existing reservation"); // if this resource doesn't support AR then exit if (success == false) { return; } else { ((ARPolicy) policy_).handleModifyReservation(obj, src, tag); } } catch (ClassCastException c) { success = false; } catch (Exception e) { success = false; } // if there is an exception, then send back an error msg if (success == false && tag != -1) { System.out.println(super.get_name() + " : Error - can't modify an "+ " existing reservation."); super.send(src, 0.0, GridSimTags.RETURN_AR_MODIFY, new IO_data(new Integer(GridSimTags.AR_MODIFY_ERROR),SIZE,src)); } } /** * Handles a query busy/free time request. * @param ev Sim_event object * @pre ev != null * @post $none */ private void handleQueryTime(Sim_event ev) { int src = -1; // the sender id boolean success = false; int tag = -1; int transID = -1; try { ARObject obj = (ARObject) ev.get_data(); double userZone = obj.getTimeZone(); double resZone = resource_.getResourceTimeZone(); long from = obj.getStartTime(); from = AdvanceReservation.convertTimeZone(from, userZone, resZone); long to = from + obj.getDurationTime(); transID = obj.getTransactionID(); src = obj.getUserID(); tag = GridSimTags.RETURN_AR_QUERY_TIME + transID; if (policyType_ != ResourceCharacteristics.ADVANCE_RESERVATION) { success = false; } else { success = true; if (ev.get_tag() == GridSimTags.SEND_AR_LIST_FREE_TIME) { ((ARPolicy) policy_).handleQueryFreeTime(from,to,src,tag,userZone); } else if (ev.get_tag() == GridSimTags.SEND_AR_LIST_BUSY_TIME) { ((ARPolicy) policy_).handleQueryBusyTime(from,to,src,tag,userZone); } } } catch (ClassCastException c) { success = false; } catch (Exception e) { success = false; } // if there is an exception, then send back an error msg if (success == false && tag != -1) { System.out.println(super.get_name() + " : Error - can't list busy"+ " or free reservation time."); Object[] array = new Object[2]; array[0] = new Integer(transID); // [0] = transaction id array[1] = null; // [1] = list of times, i.e. null super.send(src, 0.0, GridSimTags.RETURN_AR_QUERY_TIME,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -