📄 environmentsimulatorimpl.java
字号:
if(nstage < 1 || nstage >= 50) { log.error("the number of stage is too large:" + nstage); return -1; } if(stagePlacements[nstage] == null) stagePlacements[nstage] = new Hashtable(); Hashtable hastStagePlacement = stagePlacements[nstage]; //find a unoccupied placement no. int nplacement = 1; Integer intPlace; while(true) { intPlace = new Integer(nplacement); if(hashStagePlacement.containsKey(intPlace)) { nplacement ++; continue; } else break; } // //got an available placement no. //add to hash and return it log.debug("find an available placement no.:"+ nplacement); hashStagePlacement.put(intPlace, name); return nplacement; } private void deletePlacements(String name, int nstage, int nplacement) { if(nstage < 1) { log.error("the number of stage is not correct:" + nstage); return; } DefaultMutableTreeNode treenode = ga_dest.getTreeNode(strStartingName, nstage, nplacement); DefaultMutableTreeNode [] children = ga_dest.deletePath(treenode, true);//this is a recursive function Hashtable hastStagePlacement; String [] places; String strNextConnection; Integer intTemp; while(nstage != numStages) { log.debug("stage"+nstage+":placement"+nplacement); hastStagePlacement = stagePlacements[nstage]; intTemp = new Integer(nplacement); strName = (String)hashStagePlacement.get(intTemp); if(strName == null) { log.error("the name shouldn't be null:" + nplacment + nstage); return; } if(!strName.equals(name)) { log.error("the placement information in the hash is not correct" + name + "vs" + strName); return; } hashStagePlacement.remove(intTemp); //Get the next stage's placement strNextConnection = (String)XMLConfigurator.getParameter("stages|stage" + nstage +"|connection" + nplacement); log.debug(strNextConnection); //change nstage and nplacement places = strNextConnection.split(":"); nstage = Integer.parseInt(places[0].substring(5)); //the length of "placement" is 9 nplacement = Integer.parseInt(places[1].substring(9)); //the length of "placement" is 9 } } *//* public void actionPerformed(ActionEvent e) { int cpu_load = resMonitor.getAveCPULoad(); int cpu_speed = resMonitor.getCPUSpeed(); int avail_mem = resMonitor.getAvailMem(); //Check if the load is very high if(cpu_load <= DefConstants.CPU_LIGHT_LOADED) return; if(cpu_speed <= 0 || avail_mem <= 0) return; EnvironmentSimulatorPortType lastMon, nextMon; lastMon = nextMon = null; if(!timerMonBuf.equals(e.getSource())) log.info("different timer"); switch(isBottleneck(curStage)) { case DefConstants.STAY: break; case DefConstants.EXCEPTION: break; case DefConstants.MOVE: //let all instance of the stage vote try{ if((lastMon = getLastMonitorPortType()) == null) return; if(!lastMon.vote(curStage, curPlacement, cpu_load, cpu_speed, avail_mem)) { log.info("voting result is that you should stay"); return; } //Get the results from the last Monitor cpu_load = lastMon.getVoteResult_Load(curStage, curPlacement); cpu_speed = lastMon.getVoteResult_CPUSpeed(curStage, curPlacement); avail_mem = lastMon.getVoteResult_AailMem(curStage, curPlacement); } catch(Exception exception) { System.out.println(exception); return; } //Create a query expression according to the results //if the values of the results are less than 0, ignore them if(cpu_load <= 0 && cpu_speed <= 0 && avail_mem <= 0) return; if(cpu_load <= 0) cpu_load = DefConstants.GENERAL_LAST15MIN; if(cpu_speed <= 0) cpu_speed = DefConstants.GENERAL_CLOCKSPEED; if(avail_mem <= 0 ) avail_mem = DefConstants.GENERAL_RAMAVAILABLE; String strXPathExp = "//ce:Host[//ce:Host/ce:ProcessorLoad[@ce:Last1Min<"+ cpu_load +" and @ce:Last15Min>0]][//ce:Host/ce:MainMemory[@ce:RAMAvailable>="+avail_mem+"]][//ce:Host/ce:Processor[@ce:ClockSpeed>"+cpu_speed+"]]"; //Get Index service handle from XMLConfigurator String indexHandle = (String)XMLConfigurator.getParameter("stages|stage"+curStage+"|index_service" + curPlacement); log.debug(strXPathExp); log.debug(indexHandle); Element [] elementRes = QueryServiceDataByXPath.Query("MyHostScript", indexHandle, strXPathExp); if(elementRes == null) { log.error("there is no node matching the resource requirements"); return; } int length = elementRes.length; Date forRand = new Date(); Random rand = new Random(forRand.getTime()); int rand_int = Math.abs(rand.nextInt()) % length;// log.debug(rand_int); String newPlacement = elementRes[rand_int].getAttribute("ce:Name"); //Start the new service instance according to the "newPlacement" //??????????????????DO I NEED TO UPDATE THE DATABASE AT THE LAST STAGE? break; } } */ //constructTree also initilize the stagePlacements public void constructTree(GraphAlgorithm ga_config) { log.debug("starting to construct tree"); String places[] = null; String strNextConnection = null;; int nSourcePlacements = ((Integer)XMLConfigurator.getParameter("stages|stage1|numPlacements")).intValue(); int nstage, nplacement; int index; DefaultMutableTreeNode treenode, parent, root, lastnode; int numStages = ((Integer)XMLConfigurator.getParameter("numStages")).intValue(); String strFinalStagePlacement = "stage" + numStages + ":placement1"; Hashtable hashTree = new Hashtable(); nstage = 1; nplacement = 1; treenode = parent = root = null; String strURL; String strTemp; int i = 0; for(i = 1; i <= nSourcePlacements; i ++) { nplacement = i; nstage = 1; lastnode = null; do{ strURL = ((URL)XMLConfigurator.getParameter("stages|stage" + nstage + "|placement"+nplacement)).toString(); index = ga_config.findNode(Utilities.getIPAddress(strURL)); if(index < 0) { log.error("cant' find " + strURL); return; }/* if(nstage != numStages) { strTemp = (String)XMLConfigurator.getParameter("stages|stage" + nstage + "|bandwidth"+nplacement); log.debug("the bandwidth is " + strTemp); } ga_config.d[index].real_dist = (Integer.valueOf(strTemp)).intValue(); */ log.debug("stage"+nstage+":placement"+nplacement); stagePlacements[nstage][nplacement] = true; treenode = new DefaultMutableTreeNode(ga_config.d[index]); ga_config.d[index].put(treenode, nstage, nplacement); hashTree.put("stage"+nstage+":placement"+nplacement, treenode); //the last stage if(nstage == numStages) { if(lastnode != null) treenode.add(lastnode); root = treenode; break; } //Get the next stage's placement strNextConnection = (String)XMLConfigurator.getParameter("stages|stage" + nstage +"|connection" + nplacement); log.debug(strNextConnection); if(hashTree.containsKey(strNextConnection)) { parent = (DefaultMutableTreeNode)hashTree.get(strNextConnection); log.debug(((DynamicEnv)(parent.getUserObject())).name); log.debug(parent); parent.add(treenode); if(lastnode != null) treenode.add(lastnode); //treenode.setUserObject(new Integer(parent.getChildCount())); break; } //change nstage and nplacement places = strNextConnection.split(":"); nstage = Integer.parseInt(places[0].substring(5)); //the length of "placement" is 9 nplacement = Integer.parseInt(places[1].substring(9)); //the length of "placement" is 9 if(lastnode!= null) treenode.add(lastnode); lastnode = treenode; //log.debug(treenode); //log.debug(parent); }while(!hashTree.containsKey(strNextConnection)); //if(nstage == numStages) // root = treenode; } DefaultMutableTreeNode curNode, parentNode; DynamicEnv curD, parentD; curNode = parentNode = null; curD = parentD = null; int k, j; k = j = 0; DefaultMutableTreeNode [] treeTemp = new DefaultMutableTreeNode[20]; for(i = 1; i <= nSourcePlacements; i ++) { strTemp = "stage1:placement" + i; curNode = (DefaultMutableTreeNode)hashTree.get(strTemp); parentNode = (DefaultMutableTreeNode)curNode.getParent(); while(parentNode != null) { /*grandpa = (DefaultMutableTreeNode)parent.getParent(); if(grandpa == null) break; */ curD = (DynamicEnv)curNode.getUserObject(); parentD = (DynamicEnv)parentNode.getUserObject(); log.debug(" " + curD.name); if(curD.name.equals(parentD.name)) //merge curre { //preD is the parents of curD //combine these two together log.debug("name is same:" + curD.name); Enumeration e_child = curNode.children(); if(e_child != null) { for(k = 0; e_child.hasMoreElements(); k ++) treeTemp[k] = (DefaultMutableTreeNode)(e_child.nextElement()); } curNode.removeAllChildren(); curNode.removeFromParent(); if(e_child != null) { for(j = 0; j < k ; j ++) parentNode.add(treeTemp[j]); } log.debug("come to here"); curD.replace(curNode, parentNode); } curNode = parentNode; parentNode = (DefaultMutableTreeNode)curNode.getParent(); } } //Debug //Merge all treenodes that have the same name Enumeration e; DefaultMutableTreeNode tempNode, tempNode2; DynamicEnv tempD = null; tempNode = tempNode2 = null; Hashtable tempHash = new Hashtable(); for (e = root.breadthFirstEnumeration() ; e.hasMoreElements() ;) { curNode = (DefaultMutableTreeNode)(e.nextElement()); Enumeration e_child = curNode.children(); if(e_child != null) { for(; e_child.hasMoreElements();) { tempNode = (DefaultMutableTreeNode)(e_child.nextElement()); tempD = (DynamicEnv)tempNode.getUserObject(); if(tempD == null) { log.error("can't be null"); continue; } tempNode2 = (DefaultMutableTreeNode)tempHash.get(tempD.name); if(tempNode2 == null) tempHash.put(tempD.name, tempNode); else //merge { InstanceIndex [] iis = tempD.getInstanceIndex(tempNode); if(iis != null) for(int ff = 0; ff < iis.length; ff ++) tempD.put(iis[ff], tempNode2); //remove all children to the existing node Enumeration e_child_again = tempNode.children(); DefaultMutableTreeNode ttempNode; for(; e_child_again.hasMoreElements();) { ttempNode = (DefaultMutableTreeNode)(e_child_again.nextElement()); tempNode.remove(ttempNode); tempNode2.add(ttempNode); } tempNode.removeFromParent(); } } } tempHash.clear(); } DefaultMutableTreeNode node; for (e = root.breadthFirstEnumeration() ; e.hasMoreElements() ;) { node = (DefaultMutableTreeNode)(e.nextElement());// System.out.println(((Integer)(node.getUserObject())).intValue()); log.debug(" " + ((DynamicEnv)(node.getUserObject())).name); } } public synchronized void updateTree(GraphAlgorithm ga_source, GraphAlgorithm ga_dest, String strStartingName, int nstage, int nplacement) { //delete the old path from ga_config DefaultMutableTreeNode treenode = ga_dest.getTreeNode(strStartingName, nstage, nplacement); DefaultMutableTreeNode [] children = ga_dest.deletePath(treenode, -1, nplacement, stagePlacements, true);//this is a recursive function DefaultMutableTreeNode src_treenode = ga_source.getTreeNode(strStartingName, -1, -1); if(ga_dest.merge(ga_source, src_treenode) == false) { log.error("something is wrong with the merge operation"); return; } treenode = ga_dest.getTreeNode(strStartingName, -1, -1); if(children != null) { for(int i = 0; i < children.length; i ++) treenode.add(children[i]); } DefaultMutableTreeNode root = (DefaultMutableTreeNode)treenode.getRoot(); for (Enumeration e = root.breadthFirstEnumeration() ; e.hasMoreElements() ;) { DefaultMutableTreeNode treenode1 = (DefaultMutableTreeNode)(e.nextElement()); log.debug(((DynamicEnv)treenode1.getUserObject()).name); } } private int assignPlacement(DynamicEnv dNode, DefaultMutableTreeNode treenode, int nstage) { InstanceIndex ii = dNode.getInstanceIndex(treenode, nstage); if(ii == null || ii.nplacement <= 0) { int j; //stagePlacements[1][j] == true: occupied //stagePlacements[1][j] == false: unoccupied for(j = 1; j < 10 && stagePlacements[nstage][j]; j ++) ; if(j == 10) { log.error("the buffer doesn't have enough space"); return -1; } stagePlacements[nstage][j] = true; if(ii != null) { log.debug(ii.nstage + ":" + ii.nplacement); } dNode.put(treenode, nstage, j); log.debug("assiged :" + j); return j; } else { log.debug("assiged :" + ii.nplacement); return ii.nplacement; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -