-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lock cache metadata files for access #849
Conversation
JsonTemplateMapper.readJsonFromFile(cacheMetadataJsonFile, CacheMetadataTemplate.class); | ||
return CacheMetadataTranslator.fromTemplate(cacheMetadataJson, cacheDirectory); | ||
|
||
// channel is closed by inputStream.close() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should include these as methods on JsonTemplateMapper
/Blobs
? Like JsonTemplateMapper#readJsonFromFile
and Blobs#writeToFile
?
Failed on Ubuntu:
I hope this locking isn't tickling some underlying bug as the stack trace looks like JDK-8150014 (fixed in Java 9; dupes JDK-8150667, JDK-8150365). |
* @return the {@link BlobDescriptor} of the written BLOB | ||
* @throws IOException if writing the BLOB fails | ||
*/ | ||
default BlobDescriptor writeTo(Path jsonFile, boolean acquireLock) throws IOException { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I was referring to placing the method in Blobs#writeToFile
(similar to Blobs#writeToString
and Blobs#writeToByteArray
- Blobs
is intended to be static methods that operate on Blob
). This will help keep the Blob
interface simple and avoid having to use default interface methods.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I didn't think of Blobs. Will look.
public static <T extends JsonTemplate> T readJsonFromFile( | ||
Path jsonFile, Class<T> templateClass, boolean acquireLock) throws IOException { | ||
if (!acquireLock) { | ||
return objectMapper.readValue(Files.newInputStream(jsonFile), templateClass); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think here we can either:
- call out to the method without the extra
acquireLock
parameter, or - remove that other method without the
acquireLock
parameter, or - remove the
acquireLock
parameter and rename this method into something likereadJsonFromFileWithLocking
Huh failed again. In the build logs, Maven reports the version as:
|
Did you experience failure again after the new commits? |
Yeah, on MacOS this time. I don't see it locally. Maybe something fixed between 1.8.0_171 and _181. |
@Test | ||
public void testWriteToFileWithLock_newFile() throws IOException { | ||
String expected = "crepecake"; | ||
File file = File.createTempFile("blob", "bin"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For consistency in this codebase, let's prefer java.nio
for files over java.io
- so Files.createTempFile
and Files.delete
@@ -77,6 +108,7 @@ private void verifyBlobWriteTo(String expected, Blob blob) throws IOException { | |||
byte[] expectedBytes = expected.getBytes(StandardCharsets.UTF_8); | |||
Assert.assertEquals(expectedBytes.length, blobDescriptor.getSize()); | |||
|
|||
@SuppressWarnings("resource") // no leak |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can actually change the mocked OutputStream
to just Bytestreams.nullOutputStream()
to avoid the suppression.
@@ -89,4 +90,33 @@ public void testWriteJson() throws DigestException, IOException, URISyntaxExcept | |||
|
|||
Assert.assertEquals(expectedJson, jsonStream.toString()); | |||
} | |||
|
|||
@Test | |||
public void tedstReadJsonWithLock() throws IOException, URISyntaxException, DigestException { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s/tedst/test/
File file = File.createTempFile("blob", "bin"); | ||
Assert.assertTrue(file.delete()); // ensure it doesn't exist | ||
Path blobFile = Files.createTempFile("blob", "bin"); | ||
Assert.assertTrue(Files.deleteIfExists(blobFile)); // ensure it doesn't exist |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't the file exist always?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I hope so. But Files.delete()
is void, so at least this way we fail if something isn't right.
Acquire a file lock before retrieving and writing out the cache metadata file.
Note that this only serializes writing to the file but does not verify that the file has not been written to since it was initially read. The worst case is that some set of cached layers will be discarded and refetched on a subsequent build.
Fixes #848