Search in sources :

Example 1 with Node

use of org.apache.bookkeeper.net.Node in project bookkeeper by apache.

the class RackawareEnsemblePlacementPolicyImpl method selectRandomFromRack.

/**
 * Choose random node under a given network path.
 *
 * @param netPath
 *          network path
 * @param excludeBookies
 *          exclude bookies
 * @param predicate
 *          predicate to check whether the target is a good target.
 * @param ensemble
 *          ensemble structure
 * @return chosen bookie.
 */
protected BookieNode selectRandomFromRack(String netPath, Set<Node> excludeBookies, Predicate<BookieNode> predicate, Ensemble<BookieNode> ensemble) throws BKNotEnoughBookiesException {
    WeightedRandomSelection<BookieNode> wRSelection = null;
    List<Node> leaves = new ArrayList<Node>(topology.getLeaves(netPath));
    if (!this.isWeighted) {
        Collections.shuffle(leaves);
    } else {
        if (CollectionUtils.subtract(leaves, excludeBookies).size() < 1) {
            throw new BKNotEnoughBookiesException();
        }
        wRSelection = prepareForWeightedSelection(leaves);
        if (wRSelection == null) {
            throw new BKNotEnoughBookiesException();
        }
    }
    Iterator<Node> it = leaves.iterator();
    Set<Node> bookiesSeenSoFar = new HashSet<Node>();
    while (true) {
        Node n;
        if (isWeighted) {
            if (bookiesSeenSoFar.size() == leaves.size()) {
                // Don't loop infinitely.
                break;
            }
            n = wRSelection.getNextRandom();
            bookiesSeenSoFar.add(n);
        } else {
            if (it.hasNext()) {
                n = it.next();
            } else {
                break;
            }
        }
        if (excludeBookies.contains(n)) {
            continue;
        }
        if (!(n instanceof BookieNode) || !predicate.apply((BookieNode) n, ensemble)) {
            continue;
        }
        BookieNode bn = (BookieNode) n;
        // got a good candidate
        if (ensemble.addNode(bn)) {
            // add the candidate to exclude set
            excludeBookies.add(bn);
        }
        return bn;
    }
    throw new BKNotEnoughBookiesException();
}
Also used : Node(org.apache.bookkeeper.net.Node) ArrayList(java.util.ArrayList) BKNotEnoughBookiesException(org.apache.bookkeeper.client.BKException.BKNotEnoughBookiesException) HashSet(java.util.HashSet)

Example 2 with Node

use of org.apache.bookkeeper.net.Node in project bookkeeper by apache.

the class RackawareEnsemblePlacementPolicyImpl method convertBookiesToNodes.

protected Set<Node> convertBookiesToNodes(Set<BookieSocketAddress> excludeBookies) {
    Set<Node> nodes = new HashSet<Node>();
    for (BookieSocketAddress addr : excludeBookies) {
        BookieNode bn = knownBookies.get(addr);
        if (null == bn) {
            bn = createBookieNode(addr);
        }
        nodes.add(bn);
    }
    return nodes;
}
Also used : BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) Node(org.apache.bookkeeper.net.Node) HashSet(java.util.HashSet)

Example 3 with Node

use of org.apache.bookkeeper.net.Node in project bookkeeper by apache.

the class RegionAwareEnsemblePlacementPolicy method replaceBookie.

@Override
public BookieSocketAddress replaceBookie(int ensembleSize, int writeQuorumSize, int ackQuorumSize, Map<String, byte[]> customMetadata, Set<BookieSocketAddress> currentEnsemble, BookieSocketAddress bookieToReplace, Set<BookieSocketAddress> excludeBookies) throws BKException.BKNotEnoughBookiesException {
    rwLock.readLock().lock();
    try {
        boolean enforceDurability = enforceDurabilityInReplace && !disableDurabilityFeature.isAvailable();
        int effectiveMinRegionsForDurability = enforceDurability ? minRegionsForDurability : 1;
        Set<Node> excludeNodes = convertBookiesToNodes(excludeBookies);
        RRTopologyAwareCoverageEnsemble ensemble = new RRTopologyAwareCoverageEnsemble(ensembleSize, writeQuorumSize, ackQuorumSize, REGIONID_DISTANCE_FROM_LEAVES, effectiveMinRegionsForDurability > 0 ? new HashSet<String>(perRegionPlacement.keySet()) : null, effectiveMinRegionsForDurability);
        BookieNode bookieNodeToReplace = knownBookies.get(bookieToReplace);
        if (null == bookieNodeToReplace) {
            bookieNodeToReplace = createBookieNode(bookieToReplace);
        }
        excludeNodes.add(bookieNodeToReplace);
        for (BookieSocketAddress bookieAddress : currentEnsemble) {
            if (bookieAddress.equals(bookieToReplace)) {
                continue;
            }
            BookieNode bn = knownBookies.get(bookieAddress);
            if (null == bn) {
                bn = createBookieNode(bookieAddress);
            }
            excludeNodes.add(bn);
            if (!ensemble.apply(bn, ensemble)) {
                LOG.warn("Anomalous ensemble detected");
                if (null != statsLogger) {
                    statsLogger.getCounter(REGION_AWARE_ANOMALOUS_ENSEMBLE).inc();
                }
                enforceDurability = false;
            }
            ensemble.addNode(bn);
        }
        if (LOG.isDebugEnabled()) {
            LOG.debug("Try to choose a new bookie to replace {}, excluding {}.", bookieToReplace, excludeNodes);
        }
        // pick a candidate from same rack to replace
        BookieNode candidate = replaceFromRack(bookieNodeToReplace, excludeNodes, ensemble, ensemble, enforceDurability);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Bookie {} is chosen to replace bookie {}.", candidate, bookieNodeToReplace);
        }
        return candidate.getAddr();
    } finally {
        rwLock.readLock().unlock();
    }
}
Also used : BookieSocketAddress(org.apache.bookkeeper.net.BookieSocketAddress) Node(org.apache.bookkeeper.net.Node) HashSet(java.util.HashSet)

Example 4 with Node

use of org.apache.bookkeeper.net.Node 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

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