Search in sources :

Example 6 with OriginMetadata

use of org.gradle.caching.internal.origin.OriginMetadata in project gradle by gradle.

the class CaptureStateAfterExecutionStep method captureStateAfterExecution.

private Optional<AfterExecutionState> captureStateAfterExecution(UnitOfWork work, BeforeExecutionContext context, BeforeExecutionState beforeExecutionState, Duration duration) {
    return operation(operationContext -> {
        try {
            Timer timer = Time.startTimer();
            ImmutableSortedMap<String, FileSystemSnapshot> outputsProducedByWork = captureOutputs(work, context, beforeExecutionState);
            long snapshotOutputDuration = timer.getElapsedMillis();
            // The origin execution time is recorded as “work duration” + “output snapshotting duration”,
            // As this is _roughly_ the amount of time that is avoided by reusing the outputs,
            // which is currently the _only_ thing this value is used for.
            Duration originExecutionTime = duration.plus(Duration.ofMillis(snapshotOutputDuration));
            OriginMetadata originMetadata = new OriginMetadata(buildInvocationScopeId.asString(), originExecutionTime);
            AfterExecutionState afterExecutionState = new DefaultAfterExecutionState(beforeExecutionState, outputsProducedByWork, originMetadata, false);
            operationContext.setResult(Operation.Result.INSTANCE);
            return Optional.of(afterExecutionState);
        } catch (OutputSnapshotter.OutputFileSnapshottingException e) {
            work.handleUnreadableOutputs(e);
            operationContext.setResult(Operation.Result.INSTANCE);
            return Optional.empty();
        }
    }, BuildOperationDescriptor.displayName("Snapshot outputs after executing " + work.getDisplayName()).details(Operation.Details.INSTANCE));
}
Also used : DefaultAfterExecutionState(org.gradle.internal.execution.history.impl.DefaultAfterExecutionState) AfterExecutionState(org.gradle.internal.execution.history.AfterExecutionState) Timer(org.gradle.internal.time.Timer) OutputSnapshotter(org.gradle.internal.execution.OutputSnapshotter) DefaultAfterExecutionState(org.gradle.internal.execution.history.impl.DefaultAfterExecutionState) Duration(java.time.Duration) OriginMetadata(org.gradle.caching.internal.origin.OriginMetadata) FileSystemSnapshot(org.gradle.internal.snapshot.FileSystemSnapshot)

Example 7 with OriginMetadata

use of org.gradle.caching.internal.origin.OriginMetadata in project gradle by gradle.

the class TarBuildCacheEntryPacker method unpack.

private UnpackResult unpack(CacheableEntity entity, TarArchiveInputStream tarInput, OriginReader readOriginAction) throws IOException {
    ImmutableMap.Builder<String, CacheableTree> treesBuilder = ImmutableMap.builder();
    entity.visitOutputTrees((name, type, root) -> treesBuilder.put(name, new CacheableTree(type, root)));
    ImmutableMap<String, CacheableTree> treesByName = treesBuilder.build();
    TarArchiveEntry tarEntry;
    OriginMetadata originMetadata = null;
    Map<String, FileSystemLocationSnapshot> snapshots = new HashMap<>();
    tarEntry = tarInput.getNextTarEntry();
    AtomicLong entries = new AtomicLong();
    while (tarEntry != null) {
        entries.incrementAndGet();
        String path = tarEntry.getName();
        if (path.equals(METADATA_PATH)) {
            // handle origin metadata
            originMetadata = readOriginAction.execute(new CloseShieldInputStream(tarInput));
            tarEntry = tarInput.getNextTarEntry();
        } else {
            // handle tree
            Matcher matcher = TREE_PATH.matcher(path);
            if (!matcher.matches()) {
                throw new IllegalStateException("Cached entry format error, invalid contents: " + path);
            }
            String treeName = unescape(matcher.group(2));
            CacheableTree tree = treesByName.get(treeName);
            if (tree == null) {
                throw new IllegalStateException(String.format("No tree '%s' registered", treeName));
            }
            boolean missing = matcher.group(1) != null;
            String childPath = matcher.group(3);
            tarEntry = unpackTree(treeName, tree.getType(), tree.getRoot(), tarInput, tarEntry, childPath, missing, snapshots, entries);
        }
    }
    if (originMetadata == null) {
        throw new IllegalStateException("Cached result format error, no origin metadata was found.");
    }
    return new UnpackResult(originMetadata, entries.get(), snapshots);
}
Also used : HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) FileSystemLocationSnapshot(org.gradle.internal.snapshot.FileSystemLocationSnapshot) OriginMetadata(org.gradle.caching.internal.origin.OriginMetadata) ImmutableMap(com.google.common.collect.ImmutableMap) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) AtomicLong(java.util.concurrent.atomic.AtomicLong) CloseShieldInputStream(org.apache.commons.io.input.CloseShieldInputStream)

Example 8 with OriginMetadata

use of org.gradle.caching.internal.origin.OriginMetadata in project gradle by gradle.

the class ExecuteActionsTaskExecuter method executeIfValid.

private TaskExecuterResult executeIfValid(TaskInternal task, TaskStateInternal state, TaskExecutionContext context, TaskExecution work) {
    ExecutionEngine.Request request = executionEngine.createRequest(work);
    context.getTaskExecutionMode().getRebuildReason().ifPresent(request::forceNonIncremental);
    request.withValidationContext(context.getValidationContext());
    Result result = request.execute();
    result.getExecutionResult().ifSuccessfulOrElse(executionResult -> state.setOutcome(TaskExecutionOutcome.valueOf(executionResult.getOutcome())), failure -> state.setOutcome(new TaskExecutionException(task, failure)));
    return new TaskExecuterResult() {

        @Override
        public Optional<OriginMetadata> getReusedOutputOriginMetadata() {
            return result.getReusedOutputOriginMetadata();
        }

        @Override
        public boolean executedIncrementally() {
            return result.getExecutionResult().map(executionResult -> executionResult.getOutcome() == ExecutionOutcome.EXECUTED_INCREMENTALLY).getOrMapFailure(throwable -> false);
        }

        @Override
        public List<String> getExecutionReasons() {
            return result.getExecutionReasons();
        }

        @Override
        public CachingState getCachingState() {
            return result.getCachingState();
        }
    };
}
Also used : TaskExecutionException(org.gradle.api.tasks.TaskExecutionException) Result(org.gradle.internal.execution.ExecutionEngine.Result) WorkValidationException(org.gradle.internal.execution.WorkValidationException) TaskInternal(org.gradle.api.internal.TaskInternal) ListenerManager(org.gradle.internal.event.ListenerManager) ExecutionOutcome(org.gradle.internal.execution.ExecutionOutcome) TaskExecutionException(org.gradle.api.tasks.TaskExecutionException) TaskExecuterResult(org.gradle.api.internal.tasks.TaskExecuterResult) TaskStateInternal(org.gradle.api.internal.tasks.TaskStateInternal) AsyncWorkTracker(org.gradle.internal.work.AsyncWorkTracker) ClassLoaderHierarchyHasher(org.gradle.internal.hash.ClassLoaderHierarchyHasher) TaskExecutionContext(org.gradle.api.internal.tasks.TaskExecutionContext) ExecutionEngine(org.gradle.internal.execution.ExecutionEngine) List(java.util.List) InputFingerprinter(org.gradle.internal.execution.fingerprint.InputFingerprinter) FileCollectionFactory(org.gradle.api.internal.file.FileCollectionFactory) ExecutionHistoryStore(org.gradle.internal.execution.history.ExecutionHistoryStore) BuildOperationExecutor(org.gradle.internal.operations.BuildOperationExecutor) TaskExecuter(org.gradle.api.internal.tasks.TaskExecuter) FileOperations(org.gradle.api.internal.file.FileOperations) Optional(java.util.Optional) TaskExecutionOutcome(org.gradle.api.internal.tasks.TaskExecutionOutcome) OriginMetadata(org.gradle.caching.internal.origin.OriginMetadata) CachingState(org.gradle.internal.execution.caching.CachingState) ReservedFileSystemLocationRegistry(org.gradle.internal.file.ReservedFileSystemLocationRegistry) ExecutionEngine(org.gradle.internal.execution.ExecutionEngine) TaskExecuterResult(org.gradle.api.internal.tasks.TaskExecuterResult) OriginMetadata(org.gradle.caching.internal.origin.OriginMetadata) Result(org.gradle.internal.execution.ExecutionEngine.Result) TaskExecuterResult(org.gradle.api.internal.tasks.TaskExecuterResult)

Aggregations

OriginMetadata (org.gradle.caching.internal.origin.OriginMetadata)8 AfterExecutionState (org.gradle.internal.execution.history.AfterExecutionState)4 Duration (java.time.Duration)3 ExecutionOutcome (org.gradle.internal.execution.ExecutionOutcome)3 ExecutionResult (org.gradle.internal.execution.ExecutionResult)3 FileSystemSnapshot (org.gradle.internal.snapshot.FileSystemSnapshot)3 ImmutableList (com.google.common.collect.ImmutableList)2 List (java.util.List)2 Optional (java.util.Optional)2 CachingState (org.gradle.internal.execution.caching.CachingState)2 ExecutionHistoryStore (org.gradle.internal.execution.history.ExecutionHistoryStore)2 ValueSnapshot (org.gradle.internal.snapshot.ValueSnapshot)2 ImplementationSnapshot (org.gradle.internal.snapshot.impl.ImplementationSnapshot)2 Timer (org.gradle.internal.time.Timer)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 ImmutableSortedMap (com.google.common.collect.ImmutableSortedMap)1 File (java.io.File)1 Formatter (java.util.Formatter)1 HashMap (java.util.HashMap)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1