use of org.apache.druid.server.coordinator.ReplicationThrottler in project druid by druid-io.
the class LoadRule method assignReplicasForTier.
/**
* @param predicate {@link Predicate} used to pre-filter {@link ServerHolder}s retrieved from {@link DruidCluster}.
*/
private int assignReplicasForTier(final String tier, final int targetReplicantsInTier, final int currentReplicantsInTier, final DruidCoordinatorRuntimeParams params, final Predicate<ServerHolder> predicate, final DataSegment segment) {
final int numToAssign = targetReplicantsInTier - currentReplicantsInTier;
// if nothing to assign
if (numToAssign <= 0) {
return 0;
}
String noAvailability = StringUtils.format("No available [%s] servers or node capacity to assign segment[%s]! Expected Replicants[%d]", tier, segment.getId(), targetReplicantsInTier);
final List<ServerHolder> holders = getFilteredHolders(tier, params.getDruidCluster(), predicate);
// if no holders available for assignment
if (holders.isEmpty()) {
log.warn(noAvailability);
return 0;
}
final ReplicationThrottler throttler = params.getReplicationManager();
for (int numAssigned = 0; numAssigned < numToAssign; numAssigned++) {
if (!throttler.canCreateReplicant(tier)) {
log.info("Throttling replication for segment [%s] in tier [%s]", segment.getId(), tier);
return numAssigned;
}
// Retrieves from cache if available
ServerHolder holder = strategyCache.remove(tier);
// Does strategy call if not in cache
if (holder == null) {
holder = params.getBalancerStrategy().findNewSegmentHomeReplicator(segment, holders);
}
if (holder == null) {
log.warn(noAvailability);
return numAssigned;
}
holders.remove(holder);
final SegmentId segmentId = segment.getId();
final String holderHost = holder.getServer().getHost();
throttler.registerReplicantCreation(tier, segmentId, holderHost);
log.info("Assigning 'replica' for segment [%s] to server [%s] in tier [%s]", segment.getId(), holder.getServer().getName(), holder.getServer().getTier());
holder.getPeon().loadSegment(segment, () -> throttler.unregisterReplicantCreation(tier, segmentId));
}
return numToAssign;
}
Aggregations