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

📄 gridresourcewithfailure.java

📁 中間件開發详细说明:清华大学J2EE教程讲义(ppt)-Tsinghua University J2EE tutorial lectures (ppt) [上载源码成为会员下载此源码] [成为VIP会
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
        // Uncomment this to get more info on the progress of sims        // NOTE: this keeps printing at every n seconds interval.        if (record_ == true) {            System.out.println(super.get_name() +                ": receives a poll request event from "+                GridSim.getEntityName(resAv.getSrcID()) +". Clock: " +                GridSim.clock() +". Is res failed?: "+ getResourceFailed());        }        *******/        // if all the machines of the resource are out of order or not        if (getResourceFailed())        {            resAv.setAvailability(false);            super.sim_pause(GridSimTags.POLLING_TIME_USER);        }        else        {            resAv.setAvailability(true);        }        // sends back to the sender        super.send(super.output, 0.0, ev.get_tag(),                   new IO_data(resAv, Link.DEFAULT_MTU, resAv.getSrcID()));    }    /**     * This method simulates the failure of a resource.     * @param ev   a Sim_event object     * @pre ev != null     * @post $none     */    private void processFailure(Sim_event ev)    {        if (ev == null)        {            System.out.println(super.get_name() + ".processFailure(): " +                    "Error - an event is null.");            return;        }        Object obj = ev.get_data();        if (obj instanceof FailureMsg)        {            FailureMsg resFail = (FailureMsg) obj;            int numFailedMachines = (int)resFail.getNumMachines();            int numMachines = resource_.getNumMachines();            // First, we have to set ome of the machines of the resource            // as out of order.            // Then, we have to set the gridlets to FAILED or            // FAILED_RESOURCE_UNAVAILABLE            // depending on whether there are still available machines            // in this resource            if (numFailedMachines >= numMachines)            {                // This resource will not work, as none of its machines                // are running                double time;                time = resFail.getTime();                /*****************/                // Now, we keep the moment of the failure in a results file                // for this res                if (record_ == true) {                    write("Failure", numMachines);                    System.out.println(super.get_name() + ".processFailure(): " +                       "receives an event GRIDRESOURCE_FAILURE. Clock: " +                       GridSim.clock() + " which will last until clock: " +                       (GridSim.clock() + time) +                       ". There are NO working machines in this resource.");                }                /******************/                // set all the machines as failed                setMachineFailed(true, numMachines);                emptyGridletLists();                // as all the machines in this resource have failed, all                // gridlets are lost (the ones running and the ones waiting)            }//if (numFailedMachines >= numMachines)            else            {                // This resource will still work, as some of its machines                // are running                /*****************/                // Now, we keep the moment of the failure in a results file                // for this res                if (record_ == true) {                    write("Failure", numFailedMachines);                    System.out.println(super.get_name() + ".processFailure(): " +                       "receives an event GRIDRESOURCE_FAILURE. Clock: " +                       GridSim.clock() +                       ". There are STILL working machines in this resource.");                }                /******************/                // set numFailedMachines machines as failed                setMachineFailed(true, numFailedMachines);                Machine mach;                int machID;                // empty only the exec list of the machines which has failed                for(int i=0; i< numFailedMachines; i++)                {                    mach = resource_.getMachineList().getMachineInPos(i);                    machID = mach.getMachineID();                    emptyGridletLists(machID);                }            }        }    }    /**     * This method simulates the recovery of a failed machine (or machines).     * All the machines in this resource are working after calling this method.     * So, we don't allow overlapping failures.     * @param ev   a Sim_event object     * @pre ev != null     * @post $none     */    private void processRecovery(Sim_event ev)    {        int mach = resource_.getNumMachines();        int failedMach = resource_.getNumFailedMachines();        setMachineFailed(false, resource_.getNumFailedMachines());        // If all the machines of this resource were out of order,        // then we have to register again this resource to the GIS        // Only the regGIS, but not the systemGIS. This is because the        // systemGIS will always have all the res in its list. Resources are        // not removed from the systemGIS when they fail.        if (mach == failedMach)        {            int gisID = GridSim.getEntityId(regionalGISName_);            // send the registration to regGIS            super.send(super.output, GridSimTags.SCHEDULE_NOW,                       GridSimTags.REGISTER_RESOURCE,                       new IO_data(new Integer(super.get_id()), SIZE, gisID));            System.out.println(super.get_name() + ": Resource recovered." +                       " Registering the resource at the regional GIS AGAIN" +                       " after the failure at clock: " + GridSim.clock());        }        else {            System.out.println(super.get_name() +                ": Resource recovered at clock: " + GridSim.clock());        }        if (record_ == true) {            write("Recovery", 0); // Write in the results file        }    }    /**     * This method empties the lists containing gridlets in execution     * and waiting for execution.     */    private void emptyGridletLists()    {        if (policy_ instanceof AllocPolicyWithFailure)            ((AllocPolicyWithFailure) policy_).setGridletsFailed();    }    /**     * This method empties the lists containing gridlets in execution,     * we only erase the gridlets which are running in the failed machine     * @param machID the id of the machine which has failed.     * @pre ev != null     * @post $none     */    private void emptyGridletLists(int machID)    {        if (policy_ instanceof AllocPolicyWithFailure)            ((AllocPolicyWithFailure) policy_).setGridletsFailed(machID);    }    /**     * Checks whether all machines in this resource are failed or not.     * @return <tt>true</tt> if all the machines of the resource     *         are out of order at this moment.     */    private boolean getResourceFailed()    {        int numMach = resource_.getMachineList().size();        Machine mach;        boolean resFailed = true;        for(int i=0; i< numMach; i++)        {            mach = resource_.getMachineList().getMachineInPos(i);            if (mach.getFailed() == false)                resFailed = false;        }        return  resFailed;    }    /**     * Set the status of a machine of this resource.     * @param b     the new status of the machine.     *              <tt>true</tt> means the machine is failed,     *              <tt>false</tt> means the machine is working properly.     * @param numFailedMachines     the number of failed machines     *     */    private void setMachineFailed(boolean b, int numFailedMachines)    {        Machine mach;        /************/        String status = null;        if (record_ == true) {            if (b)                status = "FAILED";            else                status = "WORKING";        }        /************/        for(int i = 0; i < numFailedMachines; i++)        {            mach = resource_.getMachineList().getMachineInPos(i);            if (mach != null)            {                if (record_ == true) {                    System.out.println(super.get_name() +" - Machine: "                        + i + " is set to " + status);                    mach.setFailed(super.get_name(), b);                }                else {                    mach.setFailed(b);                }            } // end if        } // end for    }   /**    * Initializes the results files (put headers over each column)    */   private void initializeReportFile()   {        if (record_ == 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 NumMachines \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");        }   }   /**    * Write an event of this entity into a file.    * If an existing file exists, the new event will be appended at the end.    * The file name is this entity name.    *    * @param event     Values: "Removing" or "Registering" a resource    * @param numMachines    number of failed machines    */   protected void write(String event, int numMachines)   {        if (record_ == false) {            return;        }        // Write into a results file        // Now, we keep the moment of the failure in a results file for this res        FileWriter fwriter = null;        try        {           fwriter = new FileWriter(this.get_name(), true);        }        catch (Exception ex)        {           ex.printStackTrace();           System.out.println("Unwanted errors while opening file " +                              this.get_name());        }        try        {           fwriter.write(event+ " \t"+ numMachines+ " \t"+GridSim.clock()+"\n");        }        catch (Exception ex)        {           ex.printStackTrace();           System.out.println("Unwanted errors while writing on file " +                              this.get_name());        }        try        {           fwriter.close();        }        catch (Exception ex)        {           ex.printStackTrace();           System.out.println("Unwanted errors while closing file " +                              this.get_name());        }   }} // end class

⌨️ 快捷键说明

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