⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 griduserfailureex02.java

📁 中間件開發详细说明:清华大学J2EE教程讲义(ppt)-Tsinghua University J2EE tutorial lectures (ppt) [上载源码成为会员下载此源码] [成为VIP会
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * Title:        GridSim Toolkit * Description:  GridSim (Grid Simulation) Toolkit for Modeling and Simulation *               of Parallel and Distributed Systems such as Clusters and Grids *               An example of how to use the failure functionality. * Licence:      GPL - http://www.gnu.org/copyleft/gpl.html * * Author: Agustin Caminero and Anthony Sulistio * Organization: UCLM (Spain) * Created on: August 2007 */import gridsim.*;import gridsim.index.AbstractGIS;import gridsim.net.Link;import java.util.Random;import java.util.ArrayList;import java.io.FileWriter;import gridsim.resFailure.RegionalGISWithFailure;import eduni.simjava.Sim_system;import eduni.simjava.Sim_event;/** * Creates a Grid User that also considers what happens if a resource fails. * @author       Agustin Caminero and Anthony Sulistio * @since        GridSim Toolkit 4.1 */public class GridUserFailureEx02 extends GridUser{    // the base for our constants (chosen at random)    private static final int BASE = 440000;    /** This constant is to tell the user when he should submit a gridlet */    private static final int SUBMIT_GRIDLET = BASE + 1;    private ArrayList GridletSubmittedList_;   // list of submitted Gridlets    private GridletList GridletReceiveList_;   // list of received Gridlets    private int NUM_GRIDLETS;    private double pollingTime_;    // The sizes of gridlets    private int gridletLength;    private int gridletInput;    private int gridletOutput;    // we keep here the time when each gridlet is submitted    private double gridletSubmissionTime [];    private double gridletLatencyTime [];    // a flag that denotes whether to trace GridSim events or not.    private boolean trace_flag;    /**     * Creates a GridUserFailure object     * @param name      this entity name     * @param link      a network link connecting this entity     * @param pollTime  the time between polls     * @param glLength  length (MI) for the gridlets of this user     * @param glIn      input file size for the gridlets of this user     * @param glOut     output file size for the gridlets of this user     * @param trace_flag  a flag that denotes whether to trace this user events     *                    or not.     * @throws java.lang.Exception happens if either name or link is empty     */    public GridUserFailureEx02(String name, Link link, double pollTime,                        int glLength, int glIn, int glOut, boolean trace_flag)                        throws Exception    {        super(name, link);        this.GridletSubmittedList_ = new ArrayList();        this.GridletReceiveList_ = new GridletList();        pollingTime_ = pollTime;        gridletLength = glLength;        gridletInput = glIn;        gridletOutput = glOut;        this.trace_flag = trace_flag;    }    /**     * Sets the number of gridlets that this user has to submit.     * Also, create the submission and reception  times arrays.     * @param gridlet_num the number of gridlets     */    public void setGridletNumber(int gridlet_num)    {        NUM_GRIDLETS = gridlet_num;        gridletSubmissionTime = new double[NUM_GRIDLETS];        gridletLatencyTime = new double[NUM_GRIDLETS];    }    /**     * Handles incoming requests to this entity.     * This method specifies the behaviours and/or scenarios of a user.     */    public void body()    {        initializeResultsFile();        createGridlet(super.get_id(), NUM_GRIDLETS);        // schedule the initial sending of gridlets.        // The sending will start in a ramdom time within 5 min        Random random = new Random();        int init_time = random.nextInt(5*60);        // sends a reminder to itself        super.send(super.get_id(), init_time, SUBMIT_GRIDLET);        System.out.println(super.get_name() +               ": initial SUBMIT_GRIDLET event will be at clock: " +               init_time + ". Current clock: " + GridSim.clock());        ////////////////////////////////////////////////////////////        // Now, we have the framework of the entity:        while (Sim_system.running())        {            Sim_event ev = new Sim_event();            super.sim_get_next(ev); // get the next event in the queue            switch (ev.get_tag())            {                // submit a gridlet                case SUBMIT_GRIDLET:                    processGridletSubmission(ev); // process the received event                    break;                // Receive a gridlet back                case GridSimTags.GRIDLET_RETURN:                    processGridletReturn(ev);                    break;                case GridSimTags.END_OF_SIMULATION:                    System.out.println("\n============== " + super.get_name() +                                       ". Ending simulation...");                    break;                default:                    System.out.println(super.get_name() +                                       ": Received an event: " + ev.get_tag());                    break;            } // switch        } //  while        // wait for few seconds before printing the output        super.sim_pause( super.get_id()*2 );        // remove I/O entities created during construction of this entity        super.terminateIOEntities();        // prints the completed gridlets        printGridletList(GridletReceiveList_, super.get_name(), false,                         gridletLatencyTime);    } // body()    /////////////////////////////////////////////////////////////////////////    /**     * This functions process the submission  of a gridlet. We have to get the     * list of available resources from the RegGIS, choose one of them, and     * submit the gridlet.     * @param ev an incoming event     */    private void processGridletSubmission(Sim_event ev)    {        if (trace_flag)        {            System.out.println(super.get_name() +                ": received an SUBMIT_GRIDLET event. Clock: " + GridSim.clock());        }        /***********        We have to submit:             - the gridlet whose id comes with the event             - all the gridlets with the "gridletSub.getSubmitted() == false"        So, set the gridletSub.getSubmitted() to false for the gridlet whose        id comes with the event        ***********/        int i = 0;        GridletSubmission gridletSub;        int resourceID[];        Random random = new Random(5);   // a random generator with a random seed        int index;        Gridlet gl;        Integer obj;        int glID;        // This is because the initial GRIDLET_SUBMIT event, at the beginning        // of sims, does not have any gridlet id. We have to submit        // all the gridlets.        if (ev.get_data() instanceof Integer)        {            obj = (Integer) ev.get_data();            glID = obj.intValue(); // get the gridlet id.        }        else {            glID = 99; // a value at random, not used at all in this case        }        while (i < GridletSubmittedList_.size())        {            gridletSub = (GridletSubmission)GridletSubmittedList_.get(i);            if ( (gridletSub.getGridlet()).getGridletID() == glID )            {                // set this gridlet whose id comes with the event as not submitted,                // so that it is submitted as soon as possible.                ((GridletSubmission) GridletSubmittedList_.get(i)).setSubmitted(false);            }            // Submit the gridlets with the "gridletSub.getSubmitted() == false"            if ( (gridletSub.getSubmitted() == false))            {                // we have to resubmit this gridlet                gl = ((GridletSubmission) GridletSubmittedList_.get(i)).getGridlet();                resourceID = getResList(); // Get list of resources from GIS                // If we have resources in the list                if ((resourceID != null) && (resourceID.length != 0))                {                    index = random.nextInt(resourceID.length);                    // make sure the gridlet will be executed from the begining                    resetGridlet(gl);                    // submits this gridlet to a resource                    super.gridletSubmit(gl, resourceID[index]);                    gridletSubmissionTime[gl.getGridletID()] = GridSim.clock();                    // set this gridlet as submitted                    ((GridletSubmission) GridletSubmittedList_.get(i)).setSubmitted(true);                    if (trace_flag)                    {                        System.out.println(super.get_name() +                               ": Sending Gridlet #" + i + " to " +                               GridSim.getEntityName(resourceID[index]) +                               " at clock: " + GridSim.clock());                        // Write into a results file                        write(super.get_name(), "Sending", gl.getGridletID(),                              GridSim.getEntityName(resourceID[index]),                              gl.getGridletStatusString(), GridSim.clock());                    }                }                // No resources available at this moment, so schedule an event                // in the future. The event wil be in 15 min (900 sec), as                // resource failures may last several hours.                // This event includes the gridletID, so that the user will                // try to submit only this gridlet                else                {                    super.send(super.get_id(), GridSimTags.SCHEDULE_NOW + 900,                               SUBMIT_GRIDLET, new Integer(gl.getGridletID()) );                }            }// if (gridletSub.getSubmitted() == false)            i++;        } // while (i < GridletSubmittedList_.size())    } // processGridletSubmission    /**     * This functions process the return of a gridlet.     * We pay attention to the status of the gridlet     * and then decide what we have to do the next     * @param ev an incoming event     */    private void processGridletReturn(Sim_event ev)    {        if (trace_flag)        {            System.out.println(super.get_name() +                ": received an GRIDLET_RETURN event. Clock: " + GridSim.clock());        }        Object obj = (Object) ev.get_data();        Gridlet gl = null;        Random random = new Random(5);   // a random generator with a random seed        if (obj instanceof Gridlet)        {            gl = (Gridlet) obj;            gridletLatencyTime[gl.getGridletID()] = GridSim.clock();            // Write into a results file            if (trace_flag)            {                write(super.get_name(), "Receiving", gl.getGridletID(),                      GridSim.getEntityName(gl.getResourceID()),                      gl.getGridletStatusString(), GridSim.clock());            }            ///////////////////////// Gridlet Success            if (gl.getGridletStatusString().compareTo("Success") == 0)            {                System.out.println(super.get_name() + ": Receiving Gridlet #" +                       gl.getGridletID() + " with status Success at time = " +                       GridSim.clock() + " from resource " +                       GridSim.getEntityName(gl.getResourceID()));                this.GridletReceiveList_.add(gl); // add into the received list                gridletLatencyTime[gl.getGridletID()] =                        gridletLatencyTime[gl.getGridletID()] -                        gridletSubmissionTime[gl.getGridletID()];                // We have received all the gridlets. So, finish the simulation.                if (GridletReceiveList_.size() == GridletSubmittedList_.size())                {                    super.finishSimulation();                }            } // if (gl.getGridletStatusString() == "Success")            //////////////////////// Gridlet Failed            else if (gl.getGridletStatusString().compareTo("Failed") == 0)            {                System.out.println(super.get_name() + ": Receiving Gridlet #" +                       gl.getGridletID() + " with status Failed at time = " +                       GridSim.clock() + " from resource " +                       GridSim.getEntityName(gl.getResourceID()));                // Send the gridlet as soon as we have resources available.                // This gridlet will be resend as soon as possible,                // in the first loop.                int pos = findGridletInGridletSubmittedList(gl);                if (pos == -1) {                    System.out.println(super.get_name() +                        ". Gridlet not found in GridletSubmittedList.");                }                else                {                    // set this gridlet as submitted, because otherwise                    // this gridlet may be submitted several times.                    // A gridlet will only be submitted when the event carrying                    // its id reaches the user                    ((GridletSubmission) GridletSubmittedList_.get(pos)).setSubmitted(true);                    // Now, schedule an event to itself to submit the gridlet                    // The gridlet will be sent as soon as possible                    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(), GridSimTags.SCHEDULE_NOW,                               SUBMIT_GRIDLET, glID_Int);                }            } // if (gl.getGridletStatusString() == "Failed")            ////////////////////////////// Gridlet Failed_resource            else if (gl.getGridletStatusString().compareTo("Failed_resource_unavailable") == 0)            {                int pos = findGridletInGridletSubmittedList(gl);                if (pos == -1) {                    System.out.println(super.get_name() +                        ". Gridlet not found in GridletSubmittedList.");                }                else                {                    // Now, set its submission time for a random time between                    // 1 and the polling time                    double resubmissionTime = random.nextDouble() * pollingTime_;                    // this is to prevent the gridlet from being submitted                    // before its resubmission time.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -