📄 griduserfailureex02.java
字号:
// This is different from the FAILED case, because // in that case, the gridlet should be resubmited as soon // as possible. As oppossed to that, this gridlet should // not be resubmited before its resubmission time. ((GridletSubmission) GridletSubmittedList_.get(pos)).setSubmitted(true); System.out.println(super.get_name() + ": Receiving Gridlet #" + gl.getGridletID() + " with status Failed_resource_unavailable at time = " + GridSim.clock() + " from resource " + GridSim.getEntityName(gl.getResourceID()) + "(resID: " + gl.getResourceID() + "). Resubmission time will be: " + resubmissionTime + GridSim.clock()); // Now, we have to inform the GIS about this failure, so it // can keep the list of resources up-to-date. informGIS(gl.getResourceID()); // Now, schedule an event to itself to submit the gridlet Integer glID_Int = new Integer(gl.getGridletID()); // This event includes the gridletID, so that the user // will try to submit only this gridlet super.send(super.get_id(), resubmissionTime, SUBMIT_GRIDLET, glID_Int); } } // else if else { System.out.println(super.get_name() + ": Receiving Gridlet #" + gl.getGridletID() + " with status " + gl.getGridletStatusString() + " at time = " + GridSim.clock() + " from resource " + GridSim.getEntityName(gl.getResourceID()) + " resID: " + gl.getResourceID()); } } // if (obj instanceof Gridlet) } /** * Prints the Gridlet objects * @param list the list of gridlets * @param name the name of the user * @param detail if we want the gridlet's history or not * @param gridletLatencyTime array containing the latencies of gridlets. * Latencies are from the moment when the gridlet is sent, till * the moment they are back at the user. * They take into account the last submission of a gridlet * (when the gridlet is successfully run) */ private void printGridletList(GridletList list, String name, boolean detail, double gridletLatencyTime[]) { int size = list.size(); Gridlet gridlet = null; String indent = " "; StringBuffer buffer = new StringBuffer(1000); buffer.append("\n\n============== OUTPUT for " + name + " ==========="); buffer.append("\nGridlet ID" + indent + "STATUS" + indent + "Resource ID" + indent + indent + "Cost" + indent + indent + "CPU Time" + indent + indent + "Latency"); // a loop to print the overall result int i = 0; boolean header = true; for (i = 0; i < size; i++) { gridlet = (Gridlet) list.get(i); buffer.append("\n"); buffer.append(indent + gridlet.getGridletID() + indent + indent); buffer.append( gridlet.getGridletStatusString() ); buffer.append(indent + indent + gridlet.getResourceID() + indent + gridlet.getProcessingCost() + indent + gridlet.getActualCPUTime() + indent + gridletLatencyTime[gridlet.getGridletID()]); if (i != 0) { header = false; } writeFin(name, gridlet.getGridletID(), GridSim.getEntityName(gridlet.getResourceID()), gridlet.getProcessingCost(), gridlet.getActualCPUTime(), GridSim.clock(), header); } if (detail == true) { // a loop to print each Gridlet's history for (i = 0; i < size; i++) { gridlet = (Gridlet) list.get(i); buffer.append( gridlet.getGridletHistory() ); buffer.append("Gridlet #" + gridlet.getGridletID()); buffer.append(", length = " + gridlet.getGridletLength() + ", finished so far = " + gridlet.getGridletFinishedSoFar()); buffer.append("==========================================="); } } buffer.append("\n===================================================="); System.out.println( buffer.toString() ); } /** * This method will show you on how to create Gridlets * @param userID owner ID of a Gridlet * @param numGridlet number of Gridlet to be created */ private void createGridlet(int userID, int numGridlet) { for (int i = 0; i < numGridlet; i++) { // Creates a Gridlet Gridlet gl = new Gridlet(i, gridletLength, gridletInput, gridletOutput); gl.setUserID(userID); // Originally, gridlets are created to be submitted // as soon as possible (the 0.0 param) GridletSubmission gst = new GridletSubmission(gl, false); // add this gridlet into a list this.GridletSubmittedList_.add(gst); } } /** * Gets a list of received Gridlets * @return a list of received/completed Gridlets */ public GridletList getGridletList() { return GridletReceiveList_; } /** * Tells you the possition of this gridlet in the GridletSubmittedList_ * @param gl the gridlet * @return the position of this gridlet in the list of submitted gridlets */ private int findGridletInGridletSubmittedList(Gridlet gl) { Gridlet g = null; GridletSubmission gst = null; for (int i = 0; i< GridletSubmittedList_.size(); i++) { gst = (GridletSubmission)GridletSubmittedList_.get(i); g = gst.getGridlet(); if ( g.getGridletID() == gl.getGridletID() ) return i; } return -1; } /** This function resets the gridlet into its original values of * length and gridletFinishedSoFar * @param gl the gridlet to be resetted */ private void resetGridlet (Gridlet gl) { gl.setGridletLength(gridletLength); gl.setGridletFinishedSoFar(0); } /** * Write some data into the final results file. * @param user user name * @param glID gridlet id * @param resName Name of the resource * @param cost the processing cost of the gridlet * @param cpu the cpu time * @param clock Current time * @param header write a row header or not * */ private void writeFin(String user, int glID, String resName, double cost, double cpu, double clock, boolean header) { if (trace_flag == false) { return; } // Write into a results file FileWriter fwriter = null; try { fwriter = new FileWriter(user, true); } catch (Exception ex) { ex.printStackTrace(); System.out.println( "Unwanted errors while opening file " + user); } try { if (header == true) { fwriter.write( "\n\nGridletID \t Resource \t Cost \t CPU time \t Latency\n"); } fwriter.write(glID + "\t" + resName + "\t" + cost + "\t"+ cpu + "\t" + + clock + "\n"); } catch (Exception ex) { ex.printStackTrace(); System.out.println( "Unwanted errors while writing on file " + user); } try { fwriter.close(); } catch (Exception ex) { ex.printStackTrace(); System.out.println( "Unwanted errors while closing file " + user); } } /** * Write some data into a results file. * @param user user name * @param event Values: "Sending" or "Receive" a gridlet * @param glID gridlet id * @param resName Name of the resource * @param status Status of the gridlet * @param clock Current time */ private void write(String user, String event, int glID, String resName, String status, double clock) { if (trace_flag == false) { return; } // Write into a results file FileWriter fwriter = null; try { fwriter = new FileWriter(super.get_name(), true); } catch (Exception ex) { ex.printStackTrace(); System.out.println( "Unwanted errors while opening file " + super.get_name()); } try { fwriter.write(event + "\t\t" + glID + "\t" + resName + "\t" + status + "\t\t" + clock + "\n"); } catch (Exception ex) { ex.printStackTrace(); System.out.println( "Unwanted errors while writing on file " + super.get_name()); } try { fwriter.close(); } catch (Exception ex) { ex.printStackTrace(); System.out.println( "Unwanted errors while closing file " + super.get_name()); } } /** * This function informs the GIS of this user about the failure of a resource * @param resID the id of the resource which has failed */ private void informGIS(int resID) { Integer resID_Int = new Integer(resID); super.send(super.output, 0.0, AbstractGIS.NOTIFY_GIS_RESOURCE_FAILURE, new IO_data(resID_Int, Link.DEFAULT_MTU, super.getRegionalGISId()) ); } /** * Initialize the results files (put headers over each column) * */ private void initializeResultsFile() { if (trace_flag == false) { return; } // Initialize the results file FileWriter fwriter = null; try { fwriter = new FileWriter(super.get_name(), true); } catch (Exception ex) { ex.printStackTrace(); System.out.println( "Unwanted errors while opening file " + super.get_name() + " or " + super.get_name()+"_Fin"); } try { fwriter.write( "Event \t GridletID \t Resource \t GridletStatus \t\t Clock\n"); } catch (Exception ex) { ex.printStackTrace(); System.out.println( "Unwanted errors while writing on file " + super.get_name()+ " or " + super.get_name()+"_Fin"); } try { fwriter.close(); } catch (Exception ex) { ex.printStackTrace(); System.out.println( "Unwanted errors while closing file " + super.get_name()+ " or " + super.get_name()+"_Fin"); } } /** * This function retrieves a list of available resources from the GIS. * @return an array containing the ids of the resources */ private int[] getResList() { Object[] resList = super.getLocalResourceList(); int resourceID[] = null; // if we have any resource if ((resList != null) && (resList.length != 0)) { resourceID = new int[resList.length]; for (int x = 0; x < resList.length; x++) { // Resource list contains list of resource IDs resourceID[x] = ((Integer) resList[x]).intValue(); if (trace_flag == true) { System.out.println(super.get_name() + ": resource[" + x + "] = " + resourceID[x]); } } } return resourceID; }} // end class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -