Skip to content

Commit

Permalink
Improve error message when read-only cache is misconfigured
Browse files Browse the repository at this point in the history
Fixes #12293
  • Loading branch information
melix committed Mar 5, 2020
1 parent 16cc7d7 commit 3f232bf
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 5 deletions.
@@ -0,0 +1,52 @@
/*
* Copyright 2020 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.integtests.resolve.rocache

import org.gradle.api.internal.DocumentationRegistry
import org.gradle.test.fixtures.server.http.MavenHttpModule
import org.gradle.test.fixtures.server.http.MavenHttpRepository

class ReadOnlyCacheValidationTest extends AbstractReadOnlyCacheDependencyResolutionTest {
@Override
List<MavenHttpModule> getModulesInReadOnlyCache(MavenHttpRepository repo) {
[]
}

void "reasonable warning if the read-only cache directory doesn't exist"() {
given:
executer.withReadOnlyCacheDir(new File(temporaryFolder.testDirectory, "nope"))

when:
run ":help"

then:
outputContains("The GRADLE_RO_DEP_CACHE environment variable was set to")
outputContains("nope which doesn't exist!")
}

void "reasonable warning if the read-only cache doesn't have the expected layout"() {
given:
executer.withReadOnlyCacheDir(temporaryFolder.createDir("oh-noes"))

when:
run ":help"

then:
outputContains("Read-only cache is configured but the directory layout isn't expected. You must have a pre-populated modules-2 directory at ")
outputContains("Please follow the instructions at ${new DocumentationRegistry().getDocumentationFor("dependency_resolution", "sub:shared-readonly-cache")}")
}
}
Expand Up @@ -18,6 +18,7 @@

import org.gradle.BuildAdapter;
import org.gradle.BuildResult;
import org.gradle.api.internal.DocumentationRegistry;
import org.gradle.api.internal.GradleInternal;
import org.gradle.api.internal.artifacts.ivyservice.ArtifactCachesProvider;
import org.gradle.api.internal.artifacts.ivyservice.DefaultArtifactCaches;
Expand Down Expand Up @@ -56,8 +57,9 @@ public UsedGradleVersions getUsedGradleVersions() {
ArtifactCachesProvider createArtifactCaches(CacheScopeMapping cacheScopeMapping,
CacheRepository cacheRepository,
ServiceRegistry registry,
ListenerManager listenerManager) {
DefaultArtifactCaches artifactCachesProvider = new DefaultArtifactCaches(cacheScopeMapping, cacheRepository, () -> registry.get(DefaultArtifactCaches.WritableArtifactCacheLockingParameters.class));
ListenerManager listenerManager,
DocumentationRegistry documentationRegistry) {
DefaultArtifactCaches artifactCachesProvider = new DefaultArtifactCaches(cacheScopeMapping, cacheRepository, () -> registry.get(DefaultArtifactCaches.WritableArtifactCacheLockingParameters.class), documentationRegistry);
listenerManager.addListener(new BuildAdapter() {
@Override
public void buildFinished(BuildResult result) {
Expand Down
Expand Up @@ -17,6 +17,9 @@

import com.google.common.collect.ImmutableList;
import org.apache.commons.lang.StringUtils;
import org.gradle.api.internal.DocumentationRegistry;
import org.gradle.api.logging.Logger;
import org.gradle.api.logging.Logging;
import org.gradle.cache.CacheRepository;
import org.gradle.cache.PersistentIndexedCache;
import org.gradle.cache.internal.CacheScopeMapping;
Expand All @@ -32,12 +35,17 @@
import java.util.Optional;

public class DefaultArtifactCaches implements ArtifactCachesProvider {
private final static Logger LOGGER = Logging.getLogger(DefaultArtifactCaches.class);

private final DefaultArtifactCacheMetadata writableCacheMetadata;
private final DefaultArtifactCacheMetadata readOnlyCacheMetadata;
private final LateInitWritableArtifactCacheLockingManager writableArtifactCacheLockingManager;
private final ReadOnlyArtifactCacheLockingManager readOnlyArtifactCacheLockingManager;

public DefaultArtifactCaches(CacheScopeMapping cacheScopeMapping, CacheRepository cacheRepository, Factory<WritableArtifactCacheLockingParameters> writableArtifactCacheLockingParametersFactory) {
public DefaultArtifactCaches(CacheScopeMapping cacheScopeMapping,
CacheRepository cacheRepository,
Factory<WritableArtifactCacheLockingParameters> writableArtifactCacheLockingParametersFactory,
DocumentationRegistry documentationRegistry) {
writableCacheMetadata = new DefaultArtifactCacheMetadata(cacheScopeMapping);
writableArtifactCacheLockingManager = new LateInitWritableArtifactCacheLockingManager(() -> {
WritableArtifactCacheLockingParameters params = writableArtifactCacheLockingParametersFactory.create();
Expand All @@ -46,14 +54,35 @@ public DefaultArtifactCaches(CacheScopeMapping cacheScopeMapping, CacheRepositor
String roCache = System.getenv(READONLY_CACHE_ENV_VAR);
if (StringUtils.isNotEmpty(roCache)) {
IncubationLogger.incubatingFeatureUsed("Shared read-only dependency cache");
readOnlyCacheMetadata = new DefaultArtifactCacheMetadata(cacheScopeMapping, new File(roCache));
readOnlyArtifactCacheLockingManager = new ReadOnlyArtifactCacheLockingManager(cacheRepository, readOnlyCacheMetadata);
File baseDir = validateReadOnlyCache(documentationRegistry, new File(roCache));
if (baseDir != null) {
readOnlyCacheMetadata = new DefaultArtifactCacheMetadata(cacheScopeMapping, baseDir);
readOnlyArtifactCacheLockingManager = new ReadOnlyArtifactCacheLockingManager(cacheRepository, readOnlyCacheMetadata);
} else {
readOnlyCacheMetadata = null;
readOnlyArtifactCacheLockingManager = null;
}
} else {
readOnlyCacheMetadata = null;
readOnlyArtifactCacheLockingManager = null;
}
}

private static File validateReadOnlyCache(DocumentationRegistry documentationRegistry, File cacheDir) {
if (!cacheDir.exists()) {
LOGGER.warn("The " + READONLY_CACHE_ENV_VAR + " environment variable was set to " + cacheDir + " which doesn't exist!");
return null;
}
File root = CacheLayout.ROOT.getPath(cacheDir);
if (!root.exists()) {
String docLink = documentationRegistry.getDocumentationFor("dependency_resolution", "sub:shared-readonly-cache");
LOGGER.warn("Read-only cache is configured but the directory layout isn't expected. You must have a pre-populated " +
CacheLayout.ROOT.getKey() + " directory at " + root + " . Please follow the instructions at " + docLink);
return null;
}
return cacheDir;
}

@Override
public ArtifactCacheMetadata getWritableCacheMetadata() {
return writableCacheMetadata;
Expand Down

0 comments on commit 3f232bf

Please sign in to comment.