use of org.commonjava.indy.promote.change.event.PromoteCompleteEvent in project indy by Commonjava.
the class PromotionManager method runPathPromotions.
private PathsPromoteResult runPathPromotions(final PathsPromoteRequest request, final Set<String> pending, final List<Transfer> contents, final ValidationResult validation) {
long begin = System.currentTimeMillis();
PromotionHelper.PromotionRepoRetrievalResult checkResult = promotionHelper.checkAndRetrieveSourceAndTargetRepos(request);
if (checkResult.hasErrors()) {
return new PathsPromoteResult(request, pending, emptySet(), emptySet(), StringUtils.join(checkResult.errors, "\n"), validation);
}
final ArtifactStore targetStore = checkResult.targetStore;
StoreKey targetKey = targetStore.getKey();
logger.info("Run promotion from: {} to: {}, paths: {}", request.getSource(), targetKey, pending);
Set<Group> affectedGroups;
try {
affectedGroups = storeManager.query().getGroupsAffectedBy(targetKey);
logger.info("Calculate affected groups, target: {}, affected-groups: {}", targetKey, affectedGroups);
} catch (IndyDataException e) {
logger.error("Get affected groups failed", e);
return new PathsPromoteResult(request, pending, emptySet(), emptySet(), "Get affected groups failed, " + e.getMessage(), validation);
}
DrainingExecutorCompletionService<Set<PathTransferResult>> svc = new DrainingExecutorCompletionService<>(transferService);
int corePoolSize = transferService.getCorePoolSize();
int size = contents.size();
int batchSize = getParalleledBatchSize(size, corePoolSize);
logger.info("Execute parallel on collection, size: {}, batch: {}", size, batchSize);
Collection<Collection<Transfer>> batches = batch(contents, batchSize);
final List<String> errors = new ArrayList<>();
try {
detectOverloadVoid(() -> batches.forEach(batch -> svc.submit(newPathPromotionsJob(batch, targetStore, request, affectedGroups))));
} catch (IndyWorkflowException e) {
// might be PoolOverloadException. Log it and continue to revert any completed paths
String msg = String.format("Failed to submit all path promotion jobs. Error: %s", e.toString());
logger.error(msg, e);
errors.add(msg);
}
final Set<PathTransferResult> results = new HashSet<>();
try {
svc.drain(results::addAll);
} catch (InterruptedException | ExecutionException e) {
String msg = String.format("Error waiting for promotion of: %s to: %s", request.getSource(), request.getTarget());
logger.error(msg, e);
errors.add(msg);
}
final Set<String> completed = new HashSet<>();
final Set<String> skipped = new HashSet<>();
results.forEach(result -> {
if (result.error != null) {
errors.add(result.error);
} else if (result.skipped) {
skipped.add(result.path);
} else {
completed.add(result.path);
}
});
PathsPromoteResult result;
if (!errors.isEmpty()) {
List<String> rollbackErrors = promotionHelper.deleteFromStore(completed, targetStore);
errors.addAll(rollbackErrors);
result = new PathsPromoteResult(request, pending, emptySet(), emptySet(), StringUtils.join(errors, "\n"), validation);
} else {
result = new PathsPromoteResult(request, emptySet(), completed, skipped, null, validation);
final String name = String.format("PromoteNFCClean-method(%s)-source(%s)-target(%s)", "runPathPromotions", request.getSource(), targetStore.getKey());
final String context = String.format("Class: %s, method: %s, source: %s, target: %s", this.getClass().getName(), "runPathPromotions", request.getSource(), targetStore.getKey());
storeManager.asyncGroupAffectedBy(new StoreDataManager.ContextualTask(name, context, () -> promotionHelper.clearStoreNFC(completed, targetStore, affectedGroups)));
if (request.isFireEvents()) {
fireEvent(promoteCompleteEvent, new PathsPromoteCompleteEvent(result));
}
}
logger.info("Promotion completed, promotionId: {}, timeInSeconds: {}", request.getPromotionId(), timeInSeconds(begin));
return result;
}
Aggregations