📄 simplereplicamanager.java
字号:
} /** * Manages the response of the Replica Catalogue to a delete request. If the * delete request is successful the file is deleted from the storage(s) on * the resource. A success/error message is sent to the user. * * @param ev a Sim_event object */ private void processDeleteResult(Sim_event ev, boolean isMaster) { if (ev == null) { return; } Object[] pack = (Object[]) ev.get_data(); if (pack == null) { return; } String filename = (String) pack[0]; // get file name int msg = ((Integer) pack[1]).intValue(); // get result // replica request int successTag = DataGridTags.CTLG_DELETE_REPLICA_SUCCESSFUL; int sendTag = DataGridTags.FILE_DELETE_REPLICA_RESULT; ArrayList eventList = filesWaitingForDeleteACK_; // master request if (isMaster == true) { successTag = DataGridTags.CTLG_DELETE_MASTER_SUCCESSFUL; sendTag = DataGridTags.FILE_DELETE_MASTER_RESULT; eventList = masterFilesWaitingForDeleteACK_; } // search this record from the waiting event list Object[] dataTemp = searchEvent(filename, eventList); if (dataTemp != null) { if (msg == successTag) { deleteFileFromStorage(filename, false, false); msg = DataGridTags.FILE_DELETE_SUCCESSFUL; } eventList.remove(dataTemp); // remove this request from the list // send the result back to sender sendResult(filename, sendTag, msg, ((Integer) dataTemp[1]) .intValue()); } } // -------------------ADDITIONAL METHODS ------------------------------ /** * Registers all files (as master files) present on the storage(s) when * GridSim is started. */ public void registerAllMasterFiles() { DataGridResource res = (DataGridResource) Sim_system .get_entity(super.resourceID_); AbstractRC rc = null; if (res.hasLocalRC() == true) { rc = res.getLocalRC(); } else { rc = (AbstractRC) Sim_system.get_entity(super.rcID_); } if (rc == null) { System.out.println(super.get_name() + ".registerAllMasterFiles(): " + "Warning - unable to register master files to a Replica " + "Catalogue entity."); return; } Storage tempStorage = null; for (int i = 0; i < storageList_.size(); i++) { tempStorage = (Storage) storageList_.get(i); ArrayList fileList = (ArrayList) tempStorage.getFileNameList(); for (int j = 0; j < fileList.size(); j++) { String filename = (String) fileList.get(j); File file = tempStorage.getFile(filename); // get file FileAttribute fAttr = file.getFileAttribute(); // get attribute // register before simulation starts, hence no uniqueID rc.registerOriginalFile(fAttr, super.resourceID_); } } } /** * Registers one master file. * @param file the master file to be registered */ private void registerMasterFile(File file) { // need to check whether this file is a master copy or not if (file.isMasterCopy() == false) { return; } FileAttribute fAttr = file.getFileAttribute(); Object[] data = new Object[3]; data[0] = file.getName(); // set the filename data[1] = fAttr; // set the file's attribute data[2] = super.resIdObj_; // set the resource ID // send this info to the RC entity int size = fAttr.getAttributeSize(); super.sim_schedule(outputPort_, 0, DataGridTags.CTLG_ADD_MASTER, new IO_data(data, size, super.rcID_)); } /** * Sends an event with the result of adding or deleting a file back to the * user. * * @param fileName * the name of the file that was added/deleted * @param event * the event to be sent (e.g. FILE_ADD_RESULT) * @param msg * the message to sent with it (e.g. FILE_ADD_SUCCESFUL) * @param dest * the destination of the result */ private void sendResult(String fileName, int event, int msg, int dest) { // just a safety net if (dest == -1) { return; } // send back to sender Object pack[] = new Object[2]; pack[0] = fileName; pack[1] = new Integer(msg); super.sim_schedule(outputPort_, GridSimTags.SCHEDULE_NOW, event, new IO_data(pack, DataGridTags.PKT_SIZE, dest)); } /** * When a request is sent from the user, this request is saved to a list. * And when a response is received from the Replica Catalogue this event has * to be found in this list. * * @param name of the file the event was all about. * @param list the list of event where it has to search. * @return the event or <tt>null</tt> if empty */ private Object[] searchEvent(String name, ArrayList list) { for (int i = 0; i < list.size(); i++) { Object[] dataTemp = (Object[]) list.get(i); if (name.equals((String) dataTemp[0]) == true) { return dataTemp; } } return null; } /** * Sets the unique ID of a file. When the uniqueID is returned by the * Replica Catalogue, the file is renamed. * * @see SimpleReplicaManager#processMasterAddResult(Sim_event) * @param fileName the old name of the file. * @param id the unique ID assigned by the Replica Catalogue * @return <tt>true</tt> if successful, <tt>false</tt> otherwise */ private boolean setID(String fileName, int id) { boolean result = false; int i = 0; File tempFile = null; Storage tempStorage = null; while ((i < storageList_.size()) && (tempFile == null)) { tempStorage = (Storage) storageList_.get(i); tempFile = tempStorage.getFile(fileName); if (tempFile != null) { tempFile.setRegistrationID(id); // set registration ID String newName = tempFile.getName() + id; tempStorage.renameFile(tempFile, newName); // rename filename tempFile.setName(newName); // set the new lfn result = true; } i++; } return result; } //----------------------DATA GRIDLET STUFF------------------------------- /** * Receives a DataGridlet object. * In this approach, a DataGridlet requires n files. If one or more files * are not available, then this RM will fetch them. * Only if all files are available, then this DataGridlet is ready to * be executed by a resource's scheduler. * * @param dg a DataGridlet object * @return <tt>true</tt> if successful, <tt>false</tt> otherwise. */ private boolean receiveDataGridlet(DataGridlet dg) { if (dg == null) { return false; } // get a list of required files LinkedList list = (LinkedList) (dg.getRequiredFiles()).clone(); int serviceLevel = dg.getNetServiceLevel(); // get priority // for each file, check whether it is available or not for (int i = 0; i < list.size(); i++) { String filename = (String) list.get(i); // get file name // if the file is already on the local storage, the // transfer from a remote site is not needed if (contains(filename) == true) { dg.deleteRequiredFile(filename); // delete from the list } // if the file is not available, then make a replica request else { // if the file should have higher QoS if (serviceLevel == 1) { priorityFile_.add(filename); } Object[] packet = new Object[2]; packet[0] = filename; packet[1] = super.resIdObj_; sim_schedule(outputPort_, 0, DataGridTags.CTLG_GET_REPLICA, new IO_data(packet, DataGridTags.PKT_SIZE, super.rcID_)); } } // if all files are available locally if (dg.getRequiredFiles().size() == 0) { dg.setResourceParameter(super.resourceID_, 0); policy_.gridletSubmit(dg, false); // start executing this job } else { // otherwise, put into the queue waitingDataGridlet_.add(dg); } return true; } /** * A location of the file is returned from the RC. This function sends a * request for transfering the file. It also checks the network QoS for * this file. * * @param ev a Sim_event object */ protected void receiveReplicaLocation(Sim_event ev) { if (ev == null) { return; } Object[] data = (Object[]) ev.get_data(); if (data == null) { return; } String filename = (String) data[0]; // get file name Integer resID = (Integer) data[1]; // get resource ID // make a request to transfer the given filename Object[] packet = new Object[3]; packet[0] = filename; // request for this file name packet[1] = super.resIdObj_; // from this resource // it is an urgent request if (isPriorityFile(filename) == true) { priorityFile_.remove(filename); packet[2] = new Integer(1); // high priority over the network } else { packet[2] = new Integer(0); // normal priority over the network } super.sim_schedule(outputPort_, 0, DataGridTags.FILE_REQUEST, new IO_data(packet, DataGridTags.PKT_SIZE, resID.intValue())); } /** * A requested file has been delivered by another resource. * @param file a File object * @return <tt>true</tt> if this delivery has been acknowledged, * <tt>false</tt> otherwise */ protected boolean receiveFileDelivery(File file) { if (file == null) { return false; } // add the file into the storage file.setMasterCopy(false); // set file as a replica addFile(file); for (int i = 0; i < waitingDataGridlet_.size(); i++) { DataGridlet dg = (DataGridlet) waitingDataGridlet_.get(i); dg.deleteRequiredFile(file.getName()); // if a job does not need any more files if (dg.requiresFiles() == false) { dg.setResourceParameter(super.resourceID_, 0); policy_.gridletSubmit(dg, false); // execute this job waitingDataGridlet_.remove(dg); // remove from waiting list } } return true; } /** * Checks whether the given file has a higher priority or not * @param filename a file name * @return <tt>true</tt> if this file has a higher priority, * <tt>false</tt> otherwise */ private boolean isPriorityFile(String filename) { boolean result = false; Iterator iter = priorityFile_.iterator(); while (iter.hasNext()) { String name = (String) iter.next(); // get file name if (name.equals(filename) == true) { result = true; break; } } return result; }} // end class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -