Skip to content

Commit 3ce7512

Browse files
authored
Removes CacheChecker. (GoogleContainerTools#95)
1 parent 025dff2 commit 3ce7512

7 files changed

Lines changed: 266 additions & 351 deletions

File tree

‎jib-core/src/main/java/com/google/cloud/tools/jib/builder/BuildAndCacheApplicationLayersStep.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import com.google.cloud.tools.jib.Timer;
2020
import com.google.cloud.tools.jib.cache.Cache;
21-
import com.google.cloud.tools.jib.cache.CacheChecker;
21+
import com.google.cloud.tools.jib.cache.CacheReader;
2222
import com.google.cloud.tools.jib.cache.CacheWriter;
2323
import com.google.cloud.tools.jib.cache.CachedLayer;
2424
import com.google.cloud.tools.jib.cache.CachedLayerType;
@@ -90,7 +90,7 @@ private ListenableFuture<CachedLayer> buildAndCacheLayerAsync(
9090
try (Timer ignored = new Timer(buildConfiguration.getBuildLogger(), description)) {
9191
// Don't build the layer if it exists already.
9292
CachedLayer cachedLayer =
93-
new CacheChecker(cache).getUpToDateLayerBySourceFiles(sourceFiles);
93+
new CacheReader(cache).getUpToDateLayerBySourceFiles(sourceFiles);
9494
if (cachedLayer != null) {
9595
return cachedLayer;
9696
}

‎jib-core/src/main/java/com/google/cloud/tools/jib/builder/PullAndCacheBaseImageLayerStep.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import com.google.cloud.tools.jib.Timer;
2020
import com.google.cloud.tools.jib.cache.Cache;
21-
import com.google.cloud.tools.jib.cache.CacheChecker;
21+
import com.google.cloud.tools.jib.cache.CacheReader;
2222
import com.google.cloud.tools.jib.cache.CacheWriter;
2323
import com.google.cloud.tools.jib.cache.CachedLayer;
2424
import com.google.cloud.tools.jib.http.Authorization;
@@ -68,7 +68,7 @@ public CachedLayer call()
6868
buildConfiguration.getBaseImageRepository());
6969

7070
// Checks if the layer already exists in the cache.
71-
CachedLayer cachedLayer = new CacheChecker(cache).getLayer(layerDigest);
71+
CachedLayer cachedLayer = new CacheReader(cache).getLayer(layerDigest);
7272
if (cachedLayer != null) {
7373
return cachedLayer;
7474
}

‎jib-core/src/main/java/com/google/cloud/tools/jib/cache/CacheChecker.java‎

Lines changed: 0 additions & 152 deletions
This file was deleted.

‎jib-core/src/main/java/com/google/cloud/tools/jib/cache/CacheReader.java‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,70 @@
1616

1717
package com.google.cloud.tools.jib.cache;
1818

19+
import com.google.cloud.tools.jib.image.DescriptorDigest;
1920
import com.google.cloud.tools.jib.image.ImageLayers;
21+
import com.google.cloud.tools.jib.image.LayerPropertyNotFoundException;
22+
import java.io.IOException;
23+
import java.io.UncheckedIOException;
24+
import java.nio.file.Files;
2025
import java.nio.file.Path;
2126
import java.nio.file.attribute.FileTime;
2227
import java.time.Instant;
2328
import java.util.List;
29+
import java.util.Optional;
2430

2531
/** Reads image content from the cache. */
2632
public class CacheReader {
2733

34+
/**
35+
* @return the last modified time for the file at {@code path}. Recursively finds the most recent
36+
* last modified time for all subfiles if the file is a directory.
37+
*/
38+
private static FileTime getLastModifiedTime(Path path) throws IOException {
39+
FileTime lastModifiedTime = Files.getLastModifiedTime(path);
40+
41+
if (Files.isReadable(path)) {
42+
try {
43+
Optional<FileTime> maxLastModifiedTime =
44+
Files.walk(path)
45+
.map(
46+
subFilePath -> {
47+
try {
48+
return Files.getLastModifiedTime(subFilePath);
49+
50+
} catch (IOException ex) {
51+
throw new UncheckedIOException(ex);
52+
}
53+
})
54+
.max(FileTime::compareTo);
55+
56+
if (!maxLastModifiedTime.isPresent()) {
57+
throw new IllegalStateException(
58+
"Could not get last modified time for all files in directory '" + path + "'");
59+
}
60+
if (maxLastModifiedTime.get().compareTo(lastModifiedTime) > 0) {
61+
lastModifiedTime = maxLastModifiedTime.get();
62+
}
63+
64+
} catch (UncheckedIOException ex) {
65+
throw ex.getCause();
66+
}
67+
}
68+
69+
return lastModifiedTime;
70+
}
71+
2872
private final Cache cache;
2973

3074
public CacheReader(Cache cache) {
3175
this.cache = cache;
3276
}
3377

78+
/** @return the cached layer with digest {@code layerDigest} */
79+
public CachedLayer getLayer(DescriptorDigest layerDigest) throws LayerPropertyNotFoundException {
80+
return cache.getMetadata().getLayers().get(layerDigest);
81+
}
82+
3483
/**
3584
* Finds the file that stores the content BLOB for an application layer.
3685
*
@@ -66,4 +115,41 @@ public Path getLayerFile(CachedLayerType layerType, List<Path> sourceFiles)
66115
throw new UnsupportedOperationException("Can only find layer files for application layers");
67116
}
68117
}
118+
119+
/**
120+
* Gets an up-to-date layer that is built from the {@code sourceFiles}.
121+
*
122+
* <p>The method returns the first up-to-date layer found. This is safe because the source files
123+
* will not have been modified since creation of any up-to-date layer (ie. all up-to-date layers
124+
* should have the same file contents)
125+
*/
126+
public CachedLayer getUpToDateLayerBySourceFiles(List<Path> sourceFiles)
127+
throws IOException, CacheMetadataCorruptedException {
128+
// Grabs all the layers that have matching source files.
129+
ImageLayers<CachedLayerWithMetadata> cachedLayersWithSourceFiles =
130+
cache.getMetadata().filterLayers().bySourceFiles(sourceFiles).filter();
131+
if (cachedLayersWithSourceFiles.isEmpty()) {
132+
return null;
133+
}
134+
135+
// Determines the latest modification time for the source files.
136+
FileTime sourceFilesLastModifiedTime = FileTime.from(Instant.MIN);
137+
for (Path path : sourceFiles) {
138+
FileTime lastModifiedTime = getLastModifiedTime(path);
139+
if (lastModifiedTime.compareTo(sourceFilesLastModifiedTime) > 0) {
140+
sourceFilesLastModifiedTime = lastModifiedTime;
141+
}
142+
}
143+
144+
// Checks if at least one of the matched layers is up-to-date.
145+
for (CachedLayerWithMetadata cachedLayer : cachedLayersWithSourceFiles) {
146+
if (sourceFilesLastModifiedTime.compareTo(cachedLayer.getMetadata().getLastModifiedTime())
147+
<= 0) {
148+
// This layer is an up-to-date layer.
149+
return cachedLayer;
150+
}
151+
}
152+
153+
return null;
154+
}
69155
}

‎jib-core/src/test/java/com/google/cloud/tools/jib/builder/BuildAndCacheApplicationLayersStepTest.java‎

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package com.google.cloud.tools.jib.builder;
1818

1919
import com.google.cloud.tools.jib.cache.Cache;
20-
import com.google.cloud.tools.jib.cache.CacheChecker;
2120
import com.google.cloud.tools.jib.cache.CacheMetadataCorruptedException;
2221
import com.google.cloud.tools.jib.cache.CacheReader;
2322
import com.google.cloud.tools.jib.cache.CachedLayer;
@@ -79,16 +78,24 @@ public void testRun()
7978
Cache cache = Cache.init(temporaryCacheDirectory);
8079

8180
// Verifies that the cached layers are up-to-date.
82-
CacheChecker cacheChecker = new CacheChecker(cache);
83-
Assert.assertFalse(
84-
cacheChecker.areSourceFilesModified(testSourceFilesConfiguration.getDependenciesFiles()));
85-
Assert.assertFalse(
86-
cacheChecker.areSourceFilesModified(testSourceFilesConfiguration.getResourcesFiles()));
87-
Assert.assertFalse(
88-
cacheChecker.areSourceFilesModified(testSourceFilesConfiguration.getClassesFiles()));
81+
CacheReader cacheReader = new CacheReader(cache);
82+
Assert.assertEquals(
83+
applicationLayers.get(0).getBlobDescriptor(),
84+
cacheReader
85+
.getUpToDateLayerBySourceFiles(testSourceFilesConfiguration.getDependenciesFiles())
86+
.getBlobDescriptor());
87+
Assert.assertEquals(
88+
applicationLayers.get(1).getBlobDescriptor(),
89+
cacheReader
90+
.getUpToDateLayerBySourceFiles(testSourceFilesConfiguration.getResourcesFiles())
91+
.getBlobDescriptor());
92+
Assert.assertEquals(
93+
applicationLayers.get(2).getBlobDescriptor(),
94+
cacheReader
95+
.getUpToDateLayerBySourceFiles(testSourceFilesConfiguration.getClassesFiles())
96+
.getBlobDescriptor());
8997

9098
// Verifies that the cache reader gets the same layers as the newest application layers.
91-
CacheReader cacheReader = new CacheReader(cache);
9299
Assert.assertEquals(
93100
applicationLayers.get(0).getContentFile(),
94101
cacheReader.getLayerFile(

0 commit comments

Comments
 (0)