Search in sources :

Example 1 with OriginTaskExecutionMetadata

use of org.gradle.api.internal.tasks.OriginTaskExecutionMetadata in project gradle by gradle.

the class SkipCachedTaskExecuter method execute.

@Override
public void execute(final TaskInternal task, TaskStateInternal state, TaskExecutionContext context) {
    LOGGER.debug("Determining if {} is cached already", task);
    TaskProperties taskProperties = context.getTaskProperties();
    TaskOutputCachingBuildCacheKey cacheKey = context.getBuildCacheKey();
    boolean taskOutputCachingEnabled = state.getTaskOutputCaching().isEnabled();
    SortedSet<ResolvedTaskOutputFilePropertySpec> outputProperties = null;
    if (taskOutputCachingEnabled) {
        if (task.isHasCustomActions()) {
            LOGGER.info("Custom actions are attached to {}.", task);
        }
        if (cacheKey.isValid()) {
            TaskArtifactState taskState = context.getTaskArtifactState();
            // TODO: This is really something we should do at an earlier/higher level so that the input and output
            // property values are locked in at this point.
            outputProperties = resolveProperties(taskProperties.getOutputFileProperties());
            if (taskState.isAllowedToUseCachedResults()) {
                try {
                    OriginTaskExecutionMetadata originMetadata = buildCache.load(buildCacheCommandFactory.createLoad(cacheKey, outputProperties, task, taskProperties, taskOutputChangesListener, taskState));
                    if (originMetadata != null) {
                        state.setOutcome(TaskExecutionOutcome.FROM_CACHE);
                        context.setOriginExecutionMetadata(originMetadata);
                        return;
                    }
                } catch (UnrecoverableTaskOutputUnpackingException e) {
                    // garbage among the task's outputs, thus we must fail the build
                    throw e;
                } catch (Exception e) {
                    // There was a failure during downloading, previous task outputs should bu unaffected
                    LOGGER.warn("Failed to load cache entry for {}, falling back to executing task", task, e);
                }
            } else {
                LOGGER.info("Not loading {} from cache because pulling from cache is disabled for this task", task);
            }
        } else {
            LOGGER.info("Not caching {} because no valid cache key was generated", task);
        }
    }
    delegate.execute(task, state, context);
    if (taskOutputCachingEnabled) {
        if (cacheKey.isValid()) {
            if (state.getFailure() == null) {
                try {
                    TaskArtifactState taskState = context.getTaskArtifactState();
                    Map<String, Map<String, FileContentSnapshot>> outputSnapshots = taskState.getOutputContentSnapshots();
                    buildCache.store(buildCacheCommandFactory.createStore(cacheKey, outputProperties, outputSnapshots, task, context.getExecutionTime()));
                } catch (Exception e) {
                    LOGGER.warn("Failed to store cache entry {}", cacheKey.getDisplayName(), task, e);
                }
            } else {
                LOGGER.debug("Not pushing result from {} to cache because the task failed", task);
            }
        } else {
            LOGGER.info("Not pushing results from {} to cache because no valid cache key was generated", task);
        }
    }
}
Also used : TaskArtifactState(org.gradle.api.internal.changedetection.TaskArtifactState) OriginTaskExecutionMetadata(org.gradle.api.internal.tasks.OriginTaskExecutionMetadata) TaskOutputCachingBuildCacheKey(org.gradle.caching.internal.tasks.TaskOutputCachingBuildCacheKey) ResolvedTaskOutputFilePropertySpec(org.gradle.api.internal.tasks.ResolvedTaskOutputFilePropertySpec) Map(java.util.Map) UnrecoverableTaskOutputUnpackingException(org.gradle.caching.internal.tasks.UnrecoverableTaskOutputUnpackingException) UnrecoverableTaskOutputUnpackingException(org.gradle.caching.internal.tasks.UnrecoverableTaskOutputUnpackingException)

Example 2 with OriginTaskExecutionMetadata

use of org.gradle.api.internal.tasks.OriginTaskExecutionMetadata in project gradle by gradle.

the class TaskOutputOriginFactory method createReader.

public TaskOutputOriginReader createReader(final TaskInternal task) {
    return new TaskOutputOriginReader() {

        @Override
        public OriginTaskExecutionMetadata execute(InputStream inputStream) {
            // TODO: Replace this with something better
            Properties properties = new Properties();
            try {
                properties.load(inputStream);
            } catch (IOException e) {
                throw UncheckedException.throwAsUncheckedException(e);
            }
            if (!properties.stringPropertyNames().containsAll(METADATA_KEYS)) {
                throw new IllegalStateException("Cached result format error, corrupted origin metadata.");
            }
            LOGGER.info("Origin for {}: {}", task, properties);
            UniqueId originBuildInvocationId = UniqueId.from(properties.getProperty(BUILD_INVOCATION_ID_KEY));
            long originalExecutionTime = Long.parseLong(properties.getProperty(EXECUTION_TIME_KEY));
            return new OriginTaskExecutionMetadata(originBuildInvocationId, originalExecutionTime);
        }
    };
}
Also used : UniqueId(org.gradle.internal.id.UniqueId) InputStream(java.io.InputStream) OriginTaskExecutionMetadata(org.gradle.api.internal.tasks.OriginTaskExecutionMetadata) IOException(java.io.IOException) Properties(java.util.Properties)

Example 3 with OriginTaskExecutionMetadata

use of org.gradle.api.internal.tasks.OriginTaskExecutionMetadata in project gradle by gradle.

the class ExecuteTaskBuildOperationResult method getOriginBuildInvocationId.

@Nullable
@Override
public String getOriginBuildInvocationId() {
    OriginTaskExecutionMetadata originExecutionMetadata = ctx.getOriginExecutionMetadata();
    UniqueId originBuildInvocationId = originExecutionMetadata == null ? null : originExecutionMetadata.getBuildInvocationId();
    return originBuildInvocationId == null ? null : originBuildInvocationId.asString();
}
Also used : UniqueId(org.gradle.internal.id.UniqueId) OriginTaskExecutionMetadata(org.gradle.api.internal.tasks.OriginTaskExecutionMetadata) Nullable(javax.annotation.Nullable)

Example 4 with OriginTaskExecutionMetadata

use of org.gradle.api.internal.tasks.OriginTaskExecutionMetadata in project gradle by gradle.

the class TarTaskOutputPacker method unpack.

private UnpackResult unpack(SortedSet<ResolvedTaskOutputFilePropertySpec> propertySpecs, TarArchiveInputStream tarInput, TaskOutputOriginReader readOriginAction) throws IOException {
    Map<String, ResolvedTaskOutputFilePropertySpec> propertySpecsMap = Maps.uniqueIndex(propertySpecs, new Function<TaskFilePropertySpec, String>() {

        @Override
        public String apply(TaskFilePropertySpec propertySpec) {
            return propertySpec.getPropertyName();
        }
    });
    TarArchiveEntry tarEntry;
    OriginTaskExecutionMetadata originMetadata = null;
    ImmutableListMultimap.Builder<String, FileSnapshot> propertyFileSnapshots = ImmutableListMultimap.builder();
    long entries = 0;
    while ((tarEntry = tarInput.getNextTarEntry()) != null) {
        ++entries;
        String path = tarEntry.getName();
        if (path.equals(METADATA_PATH)) {
            // handle origin metadata
            originMetadata = readOriginAction.execute(new CloseShieldInputStream(tarInput));
        } else {
            // handle output property
            Matcher matcher = PROPERTY_PATH.matcher(path);
            if (!matcher.matches()) {
                throw new IllegalStateException("Cached result format error, invalid contents: " + path);
            }
            String propertyName = unescape(matcher.group(2));
            ResolvedTaskOutputFilePropertySpec propertySpec = propertySpecsMap.get(propertyName);
            if (propertySpec == null) {
                throw new IllegalStateException(String.format("No output property '%s' registered", propertyName));
            }
            boolean outputMissing = matcher.group(1) != null;
            String childPath = matcher.group(3);
            unpackPropertyEntry(propertySpec, tarInput, tarEntry, childPath, outputMissing, propertyFileSnapshots);
        }
    }
    if (originMetadata == null) {
        throw new IllegalStateException("Cached result format error, no origin metadata was found.");
    }
    return new UnpackResult(originMetadata, entries, propertyFileSnapshots.build());
}
Also used : Matcher(java.util.regex.Matcher) TaskFilePropertySpec(org.gradle.api.internal.tasks.TaskFilePropertySpec) TarArchiveEntry(org.apache.commons.compress.archivers.tar.TarArchiveEntry) DirectoryFileSnapshot(org.gradle.api.internal.changedetection.state.DirectoryFileSnapshot) FileSnapshot(org.gradle.api.internal.changedetection.state.FileSnapshot) RegularFileSnapshot(org.gradle.api.internal.changedetection.state.RegularFileSnapshot) OriginTaskExecutionMetadata(org.gradle.api.internal.tasks.OriginTaskExecutionMetadata) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) ResolvedTaskOutputFilePropertySpec(org.gradle.api.internal.tasks.ResolvedTaskOutputFilePropertySpec) CloseShieldInputStream(org.apache.commons.io.input.CloseShieldInputStream)

Example 5 with OriginTaskExecutionMetadata

use of org.gradle.api.internal.tasks.OriginTaskExecutionMetadata in project gradle by gradle.

the class TaskExecutionSnapshotSerializer method read.

public HistoricalTaskExecution read(Decoder decoder) throws Exception {
    boolean successful = decoder.readBoolean();
    OriginTaskExecutionMetadata originExecutionMetadata = new OriginTaskExecutionMetadata(UniqueId.from(decoder.readString()), decoder.readLong());
    ImmutableSortedMap<String, FileCollectionSnapshot> inputFilesSnapshots = readSnapshots(decoder);
    ImmutableSortedMap<String, FileCollectionSnapshot> outputFilesSnapshots = readSnapshots(decoder);
    FileCollectionSnapshot discoveredFilesSnapshot = fileCollectionSnapshotSerializer.read(decoder);
    ImplementationSnapshot taskImplementation = readImplementation(decoder);
    // We can't use an immutable list here because some hashes can be null
    int taskActionsCount = decoder.readSmallInt();
    ImmutableList.Builder<ImplementationSnapshot> taskActionImplementationsBuilder = ImmutableList.builder();
    for (int j = 0; j < taskActionsCount; j++) {
        ImplementationSnapshot actionImpl = readImplementation(decoder);
        taskActionImplementationsBuilder.add(actionImpl);
    }
    ImmutableList<ImplementationSnapshot> taskActionImplementations = taskActionImplementationsBuilder.build();
    int cacheableOutputPropertiesCount = decoder.readSmallInt();
    ImmutableSortedSet.Builder<String> cacheableOutputPropertiesBuilder = ImmutableSortedSet.naturalOrder();
    for (int j = 0; j < cacheableOutputPropertiesCount; j++) {
        cacheableOutputPropertiesBuilder.add(decoder.readString());
    }
    ImmutableSortedSet<String> cacheableOutputProperties = cacheableOutputPropertiesBuilder.build();
    ImmutableSortedMap<String, ValueSnapshot> inputProperties = inputPropertiesSerializer.read(decoder);
    return new HistoricalTaskExecution(taskImplementation, taskActionImplementations, inputProperties, cacheableOutputProperties, inputFilesSnapshots, discoveredFilesSnapshot, outputFilesSnapshots, successful, originExecutionMetadata);
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) OriginTaskExecutionMetadata(org.gradle.api.internal.tasks.OriginTaskExecutionMetadata)

Aggregations

OriginTaskExecutionMetadata (org.gradle.api.internal.tasks.OriginTaskExecutionMetadata)5 ResolvedTaskOutputFilePropertySpec (org.gradle.api.internal.tasks.ResolvedTaskOutputFilePropertySpec)2 UniqueId (org.gradle.internal.id.UniqueId)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableListMultimap (com.google.common.collect.ImmutableListMultimap)1 ImmutableSortedSet (com.google.common.collect.ImmutableSortedSet)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Map (java.util.Map)1 Properties (java.util.Properties)1 Matcher (java.util.regex.Matcher)1 Nullable (javax.annotation.Nullable)1 TarArchiveEntry (org.apache.commons.compress.archivers.tar.TarArchiveEntry)1 CloseShieldInputStream (org.apache.commons.io.input.CloseShieldInputStream)1 TaskArtifactState (org.gradle.api.internal.changedetection.TaskArtifactState)1 DirectoryFileSnapshot (org.gradle.api.internal.changedetection.state.DirectoryFileSnapshot)1 FileSnapshot (org.gradle.api.internal.changedetection.state.FileSnapshot)1 RegularFileSnapshot (org.gradle.api.internal.changedetection.state.RegularFileSnapshot)1 TaskFilePropertySpec (org.gradle.api.internal.tasks.TaskFilePropertySpec)1 TaskOutputCachingBuildCacheKey (org.gradle.caching.internal.tasks.TaskOutputCachingBuildCacheKey)1