Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gradle.execution.plan;
import org.gradle.api.Action;
import org.gradle.api.NonNullApi;
import org.gradle.concurrent.ParallelismConfiguration;
import org.gradle.initialization.BuildCancellationToken;
import org.gradle.internal.Cast;
import org.gradle.internal.MutableReference;
import org.gradle.internal.build.ExecutionResult;
import org.gradle.internal.concurrent.CompositeStoppable;
import org.gradle.internal.concurrent.ExecutorFactory;
import org.gradle.internal.concurrent.ManagedExecutor;
import org.gradle.internal.concurrent.Stoppable;
import org.gradle.internal.logging.text.TreeFormatter;
import org.gradle.internal.resources.ResourceLockCoordinationService;
import org.gradle.internal.time.Time;
import org.gradle.internal.time.TimeFormatting;
import org.gradle.internal.time.Timer;
import org.gradle.internal.work.WorkerLeaseRegistry.WorkerLease;
import org.gradle.internal.work.WorkerLeaseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.annotation.Nullable;
import java.io.Closeable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
import static org.gradle.internal.resources.ResourceLockState.Disposition.FINISHED;
import static org.gradle.internal.resources.ResourceLockState.Disposition.RETRY;
@NonNullApi
public class DefaultPlanExecutor implements PlanExecutor, Stoppable {
private static final Logger LOGGER = LoggerFactory.getLogger(DefaultPlanExecutor.class);
private final int executorCount;
private final WorkerLeaseService workerLeaseService;
private final BuildCancellationToken cancellationToken;
private final ResourceLockCoordinationService coordinationService;
private final ManagedExecutor executor;
private final MergedQueues queue;
private final AtomicBoolean workersStarted = new AtomicBoolean();
public DefaultPlanExecutor(ParallelismConfiguration parallelismConfiguration, ExecutorFactory executorFactory, WorkerLeaseService workerLeaseService, BuildCancellationToken cancellationToken, ResourceLockCoordinationService coordinationService) {
this.cancellationToken = cancellationToken;
this.coordinationService = coordinationService;
int numberOfParallelExecutors = parallelismConfiguration.getMaxWorkerCount();
if (numberOfParallelExecutors < 1) {
throw new IllegalArgumentException("Not a valid number of parallel executors: " + numberOfParallelExecutors);
}
this.executorCount = numberOfParallelExecutors;
this.workerLeaseService = workerLeaseService;
this.queue = new MergedQueues(coordinationService, false);
this.executor = executorFactory.create("Execution worker");
}
@Override
public void stop() {
CompositeStoppable.stoppable(queue, executor).stop();
}
@Override
public ExecutionResult process(WorkSource workSource, Action worker) {
PlanDetails planDetails = new PlanDetails(Cast.uncheckedCast(workSource), Cast.uncheckedCast(worker));
queue.add(planDetails);
maybeStartWorkers(queue, executor);
// Run work from the plan from this thread as well, given that it will be blocked waiting for it to complete anyway
WorkerLease currentWorkerLease = workerLeaseService.getCurrentWorkerLease();
MergedQueues thisPlanOnly = new MergedQueues(coordinationService, true);
thisPlanOnly.add(planDetails);
new ExecutorWorker(thisPlanOnly, currentWorkerLease, cancellationToken, coordinationService, workerLeaseService).run();
List failures = new ArrayList<>();
awaitCompletion(workSource, currentWorkerLease, failures);
return ExecutionResult.maybeFailed(failures);
}
@Override
public void assertHealthy() {
coordinationService.withStateLock(queue::assertHealthy);
}
/**
* Blocks until all items in the queue have been processed. This method will only return when every item in the queue has either completed, failed or been skipped.
*/
private void awaitCompletion(WorkSource> workSource, WorkerLease workerLease, Collection super Throwable> failures) {
coordinationService.withStateLock(resourceLockState -> {
if (workSource.allExecutionComplete()) {
// Need to hold a worker lease in order to finish up
if (!workerLease.isLockedByCurrentThread()) {
if (!workerLease.tryLock()) {
return RETRY;
}
}
workSource.collectFailures(failures);
queue.removeFinishedPlans();
return FINISHED;
} else {
// Release worker lease (if held) while waiting for work to complete
workerLease.unlock();
return RETRY;
}
});
}
private void maybeStartWorkers(MergedQueues queue, Executor executor) {
if (workersStarted.compareAndSet(false, true)) {
LOGGER.debug("Using {} parallel executor threads", executorCount);
for (int i = 1; i < executorCount; i++) {
executor.execute(new ExecutorWorker(queue, null, cancellationToken, coordinationService, workerLeaseService));
}
}
}
private static class PlanDetails {
final WorkSource