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

📄 pastryregrtest.java

📁 pastry的java实现的2.0b版
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*************************************************************************"FreePastry" Peer-to-Peer Application Development Substrate Copyright 2002, Rice University. All rights reserved.Redistribution and use in source and binary forms, with or withoutmodification, are permitted provided that the following conditions aremet:- Redistributions of source code must retain the above copyrightnotice, this list of conditions and the following disclaimer.- Redistributions in binary form must reproduce the above copyrightnotice, this list of conditions and the following disclaimer in thedocumentation and/or other materials provided with the distribution.- Neither  the name  of Rice  University (RICE) nor  the names  of itscontributors may be  used to endorse or promote  products derived fromthis software without specific prior written permission.This software is provided by RICE and the contributors on an "as is"basis, without any representations or warranties of any kind, expressor implied including, but not limited to, representations orwarranties of non-infringement, merchantability or fitness for aparticular purpose. In no event shall RICE or contributors be liablefor any direct, indirect, incidental, special, exemplary, orconsequential damages (including, but not limited to, procurement ofsubstitute goods or services; loss of use, data, or profits; orbusiness interruption) however caused and on any theory of liability,whether in contract, strict liability, or tort (including negligenceor otherwise) arising in any way out of the use of this software, evenif advised of the possibility of such damage.********************************************************************************/package rice.pastry.testing;import rice.Continuation;import rice.Continuation.*;import rice.environment.Environment;import rice.p2p.commonapi.RangeCannotBeDeterminedException;import rice.pastry.*;import rice.pastry.direct.*;import rice.pastry.messaging.*;import rice.pastry.routing.*;import rice.pastry.leafset.*;import java.util.*;/** * PastryRegrTest a regression test suite for pastry. abstract class. * * @version $Id: PastryRegrTest.java 3274 2006-05-15 16:17:47Z jeffh $ * @author andrew ladd * @author peter druschel */public abstract class PastryRegrTest {  /**   * DESCRIBE THE FIELD   */  protected PastryNodeFactory factory;  /**   * DESCRIBE THE FIELD   */  public Vector pastryNodes;  /**   * of Id   */  public SortedMap pastryNodesSorted;  /**   * DESCRIBE THE FIELD   */  public SortedMap pastryNodesSortedReady;  /**   * DESCRIBE THE FIELD   */  public Vector pastryNodesLastAdded;  /**   * DESCRIBE THE FIELD   */  public boolean inConcJoin;  private Vector rtApps;  /**   * DESCRIBE THE FIELD   */  public Message lastMsg;  /**   * DESCRIBE THE FIELD   */  public Id.Distance lastDist;  /**   * DESCRIBE THE FIELD   */  public Id lastNode;  /**   * DESCRIBE THE FIELD   */  protected Environment environment;  /**   * constructor   *   * @param env DESCRIBE THE PARAMETER   */  protected PastryRegrTest(Environment env) {    this.environment = env;    pastryNodes = new Vector();    pastryNodesSorted = new TreeMap();    pastryNodesSortedReady = new TreeMap();    pastryNodesLastAdded = new Vector();    inConcJoin = false;    rtApps = new Vector();  }//  int msgCount = 0;  // abstract methods  /**   * get a node handle to bootstrap from.   *   * @param firstNode DESCRIBE THE PARAMETER   * @return The Bootstrap value   */  protected abstract NodeHandle getBootstrap(boolean firstNode);  /**   * determine whether this node is really alive.   *   * @param nh DESCRIBE THE PARAMETER   * @return The ReallyAlive value   */  protected abstract boolean isReallyAlive(NodeHandle nh);  /**   * wire protocol specific handling of the application object e.g., RMI may   * launch a new thread   *   * @param pn pastry node   * @param app newly created application   */  protected abstract void registerapp(PastryNode pn, RegrTestApp app);  /**   * send one simulated message, or return false for a real wire protocol.   *   * @return DESCRIBE THE RETURN VALUE   */  protected abstract boolean simulate();  /**   * kill a given node.   *   * @param pn DESCRIBE THE PARAMETER   */  protected abstract void killNode(PastryNode pn);  /**   * make a new pastry node   */  private void makePastryNode() {    NodeHandle bootstrap = getBootstrap(pastryNodes.size() == 0);    PastryNode pn = generateNode(bootstrap);    pastryNodes.addElement(pn);    pastryNodesSorted.put(pn.getNodeId(), pn);    pastryNodesLastAdded.clear();    pastryNodesLastAdded.addElement(pn);    RegrTestApp rta = new RegrTestApp(pn, this);    rtApps.addElement(rta);    registerapp(pn, rta);//    int msgCount = 0;//    if (bootstrap != null)//      while (simulate())//        msgCount++;    //System.out.println("created " + pn + " messages: " + msgCount);    checkLeafSet(rta);    checkRoutingTable(rta);    //System.out.println("");  }  /**   * DESCRIBE THE METHOD   *   * @param bootstrap DESCRIBE THE PARAMETER   * @return DESCRIBE THE RETURN VALUE   */  protected PastryNode generateNode(NodeHandle bootstrap) {    PastryNode pn = factory.newNode(bootstrap);    return pn;  }  /**   * make a set of num new pastry nodes, concurrently this tests concurrent node   * joins   *   * @param num the number of nodes in a set   */  private void makePastryNode(int num) {    RegrTestApp rta[] = new RegrTestApp[num];    pause(1000);    pastryNodesLastAdded.clear();    inConcJoin = true;    int n = pastryNodes.size();    // n will be a multiple of num    if (n == 0) {      num = 1;    }    for (int i = 0; i < num; i++) {      NodeHandle bootstrap;      bootstrap = getBootstrap(n == 0);      PastryNode pn = generateNode(bootstrap);      pastryNodes.addElement(pn);      pastryNodesSorted.put(pn.getNodeId(), pn);      pastryNodesLastAdded.addElement(pn);      rta[i] = new RegrTestApp(pn, this);      rtApps.addElement(rta[i]);      registerapp(pn, rta[i]);      if (bootstrap != null) {        if (n == 0) {          // we have to join the first batch of nodes          // sequentially, else we create multiple rings//          while (simulate())//            msgCount++;          // ADDED FOR WIRE PROTOCOL...          synchronized (pn) {            while (!pn.isReady()) {              try {                pn.wait(500);              } catch (InterruptedException ie) {              }            }          }        }      }    }//    int msgCount = 0;    // now simulate concurrent joins//    while (simulate())//      msgCount++;    // ADDED FOR WIRE PROTOCOL...    // wait until all nodes are ready    for (int i = 0; i < pastryNodesLastAdded.size(); i++) {      PastryNode pn = (PastryNode) pastryNodesLastAdded.get(i);      synchronized (pn) {        while (!pn.isReady()) {          try {            pn.wait(500);          } catch (InterruptedException ie) {          }        }      }    }    pause(2500);    inConcJoin = false;    for (int i = 0; i < num; i++) {      System.out.println("created " + rta[i].getNodeId());      PastryNode pn = rta[i].getPastryNode();      pastryNodesSortedReady.put(pn.getNodeId(), pn);      checkLeafSet(rta[i]);      if (!environment.getSelectorManager().isSelectorThread() && (this instanceof DirectPastryRegrTest)) {        final RegrTestApp theRta = rta[i];        final DirectPastryNode dpn = (DirectPastryNode) theRta.getPastryNode();        // check if closest entry has valid proximity        ExternalRunnable er =          new ExternalRunnable() {            protected Object execute() throws Exception {              DirectPastryNode.currentNode = dpn;              checkRoutingTable(theRta);              DirectPastryNode.currentNode = null;              return null;            }          };        try {          er.invoke(environment);        } catch (Exception e) {          e.printStackTrace();        }      } else {        checkRoutingTable(rta[i]);      }    }//    System.out.println("messages: " + msgCount);    for (int i = 0; i < rtApps.size(); i++) {      //checkLeafSet((RegrTestApp)rtApps.get(i));      //checkRoutingTable((RegrTestApp)rtApps.get(i));    }  }  // wait for a specified amount of time if we're distributed  /**   * DESCRIBE THE METHOD   *   * @param ms DESCRIBE THE PARAMETER   */  public abstract void pause(int ms);  /**   * Send messages among random message pairs. In each round, one message is   * sent from a random source node to a random destination; then, a second   * message is sent from a random source node with a random key (key is not   * necessaily the Id of an existing node)   *   * @param k the number of rounds   */  public void sendPings(int k) {    int n = rtApps.size();    for (int i = 0; i < k; i++) {      int from = environment.getRandomSource().nextInt(n);      int to = environment.getRandomSource().nextInt(n);      byte[] keyBytes = new byte[Id.IdBitLength / 8];      environment.getRandomSource().nextBytes(keyBytes);      Id key = Id.build(keyBytes);      RegrTestApp rta = (RegrTestApp) rtApps.get(from);      PastryNode pn = (PastryNode) pastryNodes.get(to);      // send to a random node      rta.sendTrace(pn.getNodeId());//      while (simulate());      // send to a random key      rta.sendTrace(key);//      while (simulate());      //System.out.println("-------------------");    }  }  /**   * verify the correctness of the leaf set   *   * @param rta DESCRIBE THE PARAMETER   */  private void checkLeafSet(RegrTestApp rta) {    LeafSet ls = rta.getLeafSet();    Id localId = rta.getNodeId();    // check size    if (ls.size() < ls.maxSize()      && (pastryNodesSorted.size() - 1) * 2 != ls.size()) {      System.out.println("checkLeafSet: incorrect size " + rta.getNodeId()        + " ls.size()=" + ls.size() + " total nodes="        + pastryNodesSorted.size() + "\n" + ls);    }    // check for correct leafset range    // ccw half    for (int i = -ls.ccwSize(); i < 0; i++) {      NodeHandle nh = ls.get(i);      if (!nh.isAlive()) {        System.out.println("checkLeafSet: dead node handle " + nh.getNodeId()          + " in leafset at " + rta.getNodeId() + "\n" + ls);      }      Id nid = ls.get(i).getNodeId();      int inBetween;      if (localId.compareTo(nid) > 0) {        // local > nid ?        inBetween = pastryNodesSorted.subMap(nid, localId).size();      } else {        inBetween = pastryNodesSorted.tailMap(nid).size()          + pastryNodesSorted.headMap(localId).size();      }      if (inBetween != -i) {        System.out.println("checkLeafSet: failure at" + rta.getNodeId() + "i="          + i + " inBetween=" + inBetween + "\n" + ls);      }    }    // cw half    for (int i = 1; i <= ls.cwSize(); i++) {      NodeHandle nh = ls.get(i);      if (!nh.isAlive()) {        System.out.println("checkLeafSet: dead node handle " + nh.getNodeId()          + " in leafset at " + rta.getNodeId() + "\n" + ls);      }      Id nid = ls.get(i).getNodeId();      int inBetween;      if (localId.compareTo(nid) < 0) {        // localId < nid?        inBetween = pastryNodesSorted.subMap(localId, nid).size();      } else {        inBetween = pastryNodesSorted.tailMap(localId).size()          + pastryNodesSorted.headMap(nid).size();      }      if (inBetween != i) {        System.out.println("checkLeafSet: failure at" + rta.getNodeId() + "i="          + i + " inBetween=" + inBetween + "\n" + ls);      }    }    // now check the replicaSet and range method    // a comparator that orders nodeIds by distance from the localId    class DistComp implements Comparator {      Id id;      /**       * Constructor for DistComp.       *       * @param id DESCRIBE THE PARAMETER

⌨️ 快捷键说明

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