private static Map<Integer, Long> findLocalSources(
      Collection<IndexSnapshotRequestConfig.PartitionRanges> partitionRanges, SiteTracker tracker) {
    Set<Integer> partitions = Sets.newHashSet();
    for (IndexSnapshotRequestConfig.PartitionRanges partitionRange : partitionRanges) {
      partitions.add(partitionRange.partitionId);
    }

    Map<Integer, Long> pidToLocalHSId = Maps.newHashMap();
    List<Long> localSites = Longs.asList(tracker.getLocalSites());

    for (long hsId : localSites) {
      int pid = tracker.getPartitionForSite(hsId);
      if (partitions.contains(pid)) {
        pidToLocalHSId.put(pid, hsId);
      }
    }

    return pidToLocalHSId;
  }
Example #2
0
  private static List<Long> computeDedupedLocalSites(long txnId, SiteTracker tracker) {
    MessageDigest digest;
    try {
      digest = MessageDigest.getInstance("SHA-1");
    } catch (NoSuchAlgorithmException e) {
      throw new AssertionError(e);
    }

    /*
     * List of partitions to include if this snapshot is
     * going to be deduped. Attempts to break up the work
     * by seeding and RNG selecting
     * a random replica to do the work. Will not work in failure
     * cases, but we don't use dedupe when we want durability.
     *
     * Originally used the partition id as the seed, but it turns out
     * that nextInt(2) returns a 1 for seeds 0-4095. Now use SHA-1
     * on the txnid + partition id.
     */
    List<Long> sitesToInclude = new ArrayList<Long>();
    for (long localSite : tracker.getLocalSites()) {
      final int partitionId = tracker.getPartitionForSite(localSite);
      List<Long> sites =
          new ArrayList<Long>(tracker.getSitesForPartition(tracker.getPartitionForSite(localSite)));
      Collections.sort(sites);

      digest.update(Longs.toByteArray(txnId));
      final long seed =
          Longs.fromByteArray(Arrays.copyOf(digest.digest(Ints.toByteArray(partitionId)), 8));

      int siteIndex = new java.util.Random(seed).nextInt(sites.size());
      if (localSite == sites.get(siteIndex)) {
        sitesToInclude.add(localSite);
      }
    }
    return sitesToInclude;
  }