/** Compares our trees, and triggers repairs for any ranges that mismatch. */
      public void run() {
        InetAddress local = FBUtilities.getLocalAddress();

        // restore partitioners (in case we were serialized)
        if (ltree.partitioner() == null) ltree.partitioner(StorageService.getPartitioner());
        if (rtree.partitioner() == null) rtree.partitioner(StorageService.getPartitioner());

        // compare trees, and collect differences
        differences.addAll(MerkleTree.difference(ltree, rtree));

        // choose a repair method based on the significance of the difference
        String format =
            "Endpoints " + local + " and " + remote + " %s for " + cfname + " on " + range;
        if (differences.isEmpty()) {
          logger.info(String.format(format, "are consistent"));
          completed(remote, cfname);
          return;
        }

        // non-0 difference: perform streaming repair
        logger.info(String.format(format, "have " + differences.size() + " range(s) out of sync"));
        try {
          performStreamingRepair();
        } catch (IOException e) {
          throw new RuntimeException(e);
        }
      }
      /** Compares our trees, and triggers repairs for any ranges that mismatch. */
      public void run() {
        // restore partitioners (in case we were serialized)
        if (r1.tree.partitioner() == null) r1.tree.partitioner(StorageService.getPartitioner());
        if (r2.tree.partitioner() == null) r2.tree.partitioner(StorageService.getPartitioner());

        // compare trees, and collect differences
        differences.addAll(MerkleTree.difference(r1.tree, r2.tree));

        // choose a repair method based on the significance of the difference
        String format =
            String.format(
                "[repair #%s] Endpoints %s and %s %%s for %s",
                getName(), r1.endpoint, r2.endpoint, cfname);
        if (differences.isEmpty()) {
          logger.info(String.format(format, "are consistent"));
          completed(this);
          return;
        }

        // non-0 difference: perform streaming repair
        logger.info(String.format(format, "have " + differences.size() + " range(s) out of sync"));
        performStreamingRepair();
      }
  @Test
  public void testDifference() {
    Range full = new Range(tok(-1), tok(-1));
    int maxsize = 16;
    mt.maxsize(maxsize);
    MerkleTree mt2 = new MerkleTree(partitioner, RECOMMENDED_DEPTH, maxsize);
    mt.init();
    mt2.init();

    TreeRange leftmost = null;
    TreeRange middle = null;
    TreeRange rightmost = null;

    // compact the leftmost, and split the rightmost
    Iterator<TreeRange> ranges = mt.invalids(full);
    leftmost = ranges.next();
    rightmost = null;
    while (ranges.hasNext()) rightmost = ranges.next();
    mt.compact(leftmost.right);
    leftmost = mt.get(leftmost.right); // leftmost is now a larger range
    mt.split(rightmost.right);

    // set the hash for the left neighbor of rightmost
    middle = mt.get(rightmost.left);
    middle.hash("arbitrary!".getBytes());
    byte depth = middle.depth;

    // add dummy hashes to the rest of both trees
    for (TreeRange range : mt.invalids(full)) range.addAll(new HIterator(range.right));
    for (TreeRange range : mt2.invalids(full)) range.addAll(new HIterator(range.right));

    // trees should disagree for leftmost, (middle.left, rightmost.right]
    List<TreeRange> diffs = MerkleTree.difference(mt, mt2);
    assertEquals(diffs + " contains wrong number of differences:", 2, diffs.size());
    assertTrue(diffs.contains(leftmost));
    assertTrue(diffs.contains(new Range(middle.left, rightmost.right)));
  }