Search in sources :

Example 1 with WeightedObject

use of org.apache.bookkeeper.client.WeightedRandomSelection.WeightedObject in project bookkeeper by apache.

the class RackawareEnsemblePlacementPolicyImpl method updateBookieInfo.

@Override
public void updateBookieInfo(Map<BookieSocketAddress, BookieInfo> bookieInfoMap) {
    if (!isWeighted) {
        LOG.info("bookieFreeDiskInfo callback called even without weighted placement policy being used.");
        return;
    }
    List<BookieNode> allBookies = new ArrayList<BookieNode>(knownBookies.values());
    // create a new map to reflect the new mapping
    Map<BookieNode, WeightedObject> map = new HashMap<BookieNode, WeightedObject>();
    for (BookieNode bookie : allBookies) {
        if (bookieInfoMap.containsKey(bookie.getAddr())) {
            map.put(bookie, bookieInfoMap.get(bookie.getAddr()));
        } else {
            map.put(bookie, new BookieInfo());
        }
    }
    rwLock.writeLock().lock();
    try {
        this.bookieInfoMap = map;
        this.weightedSelection.updateMap(this.bookieInfoMap);
    } finally {
        rwLock.writeLock().unlock();
    }
}
Also used : WeightedObject(org.apache.bookkeeper.client.WeightedRandomSelection.WeightedObject) BookieInfo(org.apache.bookkeeper.client.BookieInfoReader.BookieInfo) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList)

Example 2 with WeightedObject

use of org.apache.bookkeeper.client.WeightedRandomSelection.WeightedObject in project bookkeeper by apache.

the class RackawareEnsemblePlacementPolicyImpl method selectRandomInternal.

protected List<BookieNode> selectRandomInternal(List<BookieNode> bookiesToSelectFrom, int numBookies, Set<Node> excludeBookies, Predicate<BookieNode> predicate, Ensemble<BookieNode> ensemble) throws BKNotEnoughBookiesException {
    WeightedRandomSelection<BookieNode> wRSelection = null;
    if (bookiesToSelectFrom == null) {
        // If the list is null, we need to select from the entire knownBookies set
        wRSelection = this.weightedSelection;
        bookiesToSelectFrom = new ArrayList<BookieNode>(knownBookies.values());
    }
    if (isWeighted) {
        if (CollectionUtils.subtract(bookiesToSelectFrom, excludeBookies).size() < numBookies) {
            throw new BKNotEnoughBookiesException();
        }
        if (wRSelection == null) {
            Map<BookieNode, WeightedObject> rackMap = new HashMap<BookieNode, WeightedObject>();
            for (BookieNode n : bookiesToSelectFrom) {
                if (excludeBookies.contains(n)) {
                    continue;
                }
                if (this.bookieInfoMap.containsKey(n)) {
                    rackMap.put(n, this.bookieInfoMap.get(n));
                } else {
                    rackMap.put(n, new BookieInfo());
                }
            }
            wRSelection = new WeightedRandomSelection<BookieNode>(this.maxWeightMultiple);
            wRSelection.updateMap(rackMap);
        }
    } else {
        Collections.shuffle(bookiesToSelectFrom);
    }
    BookieNode bookie;
    List<BookieNode> newBookies = new ArrayList<BookieNode>(numBookies);
    Iterator<BookieNode> it = bookiesToSelectFrom.iterator();
    Set<BookieNode> bookiesSeenSoFar = new HashSet<BookieNode>();
    while (numBookies > 0) {
        if (isWeighted) {
            if (bookiesSeenSoFar.size() == bookiesToSelectFrom.size()) {
                // We don't want to loop infinitely.
                break;
            }
            bookie = wRSelection.getNextRandom();
            bookiesSeenSoFar.add(bookie);
        } else {
            if (it.hasNext()) {
                bookie = it.next();
            } else {
                break;
            }
        }
        if (excludeBookies.contains(bookie)) {
            continue;
        }
        // guarantee is not best effort; correctness is implied by it
        if (enforceDurability && !predicate.apply(bookie, ensemble)) {
            continue;
        }
        if (ensemble.addNode(bookie)) {
            excludeBookies.add(bookie);
            newBookies.add(bookie);
            --numBookies;
        }
    }
    if (numBookies == 0) {
        return newBookies;
    }
    LOG.warn("Failed to find {} bookies : excludeBookies {}, allBookies {}.", numBookies, excludeBookies, bookiesToSelectFrom);
    throw new BKNotEnoughBookiesException();
}
Also used : WeightedObject(org.apache.bookkeeper.client.WeightedRandomSelection.WeightedObject) BookieInfo(org.apache.bookkeeper.client.BookieInfoReader.BookieInfo) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) BKNotEnoughBookiesException(org.apache.bookkeeper.client.BKException.BKNotEnoughBookiesException) HashSet(java.util.HashSet)

Example 3 with WeightedObject

use of org.apache.bookkeeper.client.WeightedRandomSelection.WeightedObject in project bookkeeper by apache.

the class RackawareEnsemblePlacementPolicyImpl method prepareForWeightedSelection.

private WeightedRandomSelection<BookieNode> prepareForWeightedSelection(List<Node> leaves) {
    // create a map of bookieNode->freeDiskSpace for this rack. The assumption is that
    // the number of nodes in a rack is of the order of 40, so it shouldn't be too bad
    // to build it every time during a ledger creation
    Map<BookieNode, WeightedObject> rackMap = new HashMap<BookieNode, WeightedObject>();
    for (Node n : leaves) {
        if (!(n instanceof BookieNode)) {
            continue;
        }
        BookieNode bookie = (BookieNode) n;
        if (this.bookieInfoMap.containsKey(bookie)) {
            rackMap.put(bookie, this.bookieInfoMap.get(bookie));
        } else {
            rackMap.put(bookie, new BookieInfo());
        }
    }
    if (rackMap.size() == 0) {
        return null;
    }
    WeightedRandomSelection<BookieNode> wRSelection = new WeightedRandomSelection<BookieNode>(maxWeightMultiple);
    wRSelection.updateMap(rackMap);
    return wRSelection;
}
Also used : WeightedObject(org.apache.bookkeeper.client.WeightedRandomSelection.WeightedObject) BookieInfo(org.apache.bookkeeper.client.BookieInfoReader.BookieInfo) HashMap(java.util.HashMap) Node(org.apache.bookkeeper.net.Node)

Aggregations

HashMap (java.util.HashMap)3 BookieInfo (org.apache.bookkeeper.client.BookieInfoReader.BookieInfo)3 WeightedObject (org.apache.bookkeeper.client.WeightedRandomSelection.WeightedObject)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)1 BKNotEnoughBookiesException (org.apache.bookkeeper.client.BKException.BKNotEnoughBookiesException)1 Node (org.apache.bookkeeper.net.Node)1