use of org.apache.bookkeeper.client.BookieInfoReader.BookieInfo 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();
}
}
use of org.apache.bookkeeper.client.BookieInfoReader.BookieInfo 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();
}
use of org.apache.bookkeeper.client.BookieInfoReader.BookieInfo in project bookkeeper by apache.
the class RackawareEnsemblePlacementPolicyImpl method handleBookiesThatJoined.
@Override
public void handleBookiesThatJoined(Set<BookieSocketAddress> joinedBookies) {
// node joined
for (BookieSocketAddress addr : joinedBookies) {
try {
BookieNode node = createBookieNode(addr);
topology.add(node);
knownBookies.put(addr, node);
if (this.isWeighted) {
this.bookieInfoMap.putIfAbsent(node, new BookieInfo());
}
bookiesJoinedCounter.registerSuccessfulValue(1L);
if (LOG.isDebugEnabled()) {
LOG.debug("Cluster changed : bookie {} joined the cluster.", addr);
}
} catch (Throwable t) {
// topology.add() throws unchecked exception
LOG.error("Unexpected exception while handling joining bookie {}", addr, t);
bookiesJoinedCounter.registerFailedValue(1L);
// no need to re-throw; we want to process the rest of the bookies
// exception anyways will be caught/logged/suppressed in the ZK's event handler
}
}
}
use of org.apache.bookkeeper.client.BookieInfoReader.BookieInfo 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;
}
use of org.apache.bookkeeper.client.BookieInfoReader.BookieInfo in project bookkeeper by apache.
the class DefaultEnsemblePlacementPolicy method onClusterChanged.
@Override
public Set<BookieSocketAddress> onClusterChanged(Set<BookieSocketAddress> writableBookies, Set<BookieSocketAddress> readOnlyBookies) {
rwLock.writeLock().lock();
try {
HashSet<BookieSocketAddress> deadBookies;
deadBookies = new HashSet<BookieSocketAddress>(knownBookies);
deadBookies.removeAll(writableBookies);
// readonly bookies should not be treated as dead bookies
deadBookies.removeAll(readOnlyBookies);
if (this.isWeighted) {
for (BookieSocketAddress b : deadBookies) {
this.bookieInfoMap.remove(b);
}
@SuppressWarnings("unchecked") Collection<BookieSocketAddress> newBookies = CollectionUtils.subtract(writableBookies, knownBookies);
for (BookieSocketAddress b : newBookies) {
this.bookieInfoMap.put(b, new BookieInfo());
}
if (deadBookies.size() > 0 || newBookies.size() > 0) {
this.weightedSelection.updateMap(this.bookieInfoMap);
}
}
knownBookies = writableBookies;
return deadBookies;
} finally {
rwLock.writeLock().unlock();
}
}
Aggregations