-
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
fix the issue that makes jib stuck at reading images from local docke… #4048
Open
EternalWind
wants to merge
8
commits into
GoogleContainerTools:master
Choose a base branch
from
EternalWind:master
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f97d366
fix the issue that makes jib stuck at reading images from local docke…
7c0fbb2
Merge branch 'master' into master
EternalWind 2e44cbe
Merge branch 'master' into master
EternalWind 353c4e2
Refactor the code according to review comments.
c4bafec
Merge branch 'master' into master
EternalWind 4a6346e
Merge branch 'master' into master
EternalWind a00f914
Merge branch 'master' into master
EternalWind 4f94087
Merge branch 'master' into master
EternalWind File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,7 @@ | |
import com.google.common.io.CharStreams; | ||
import java.io.BufferedInputStream; | ||
import java.io.BufferedOutputStream; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
|
@@ -246,11 +247,21 @@ public DockerImageDetails inspect(ImageReference imageReference) | |
throws IOException, InterruptedException { | ||
Process inspectProcess = | ||
docker("inspect", "-f", "{{json .}}", "--type", "image", imageReference.toString()); | ||
if (inspectProcess.waitFor() != 0) { | ||
throw new IOException( | ||
"'docker inspect' command failed with error: " + getStderrOutput(inspectProcess)); | ||
|
||
try (InputStreamReader stdout = | ||
new InputStreamReader(inspectProcess.getInputStream(), StandardCharsets.UTF_8)) { | ||
|
||
String output = CharStreams.toString(stdout); | ||
|
||
if (inspectProcess.waitFor() != 0) { | ||
throw new IOException( | ||
"'docker inspect' command failed with error: " + getStderrOutput(inspectProcess)); | ||
} | ||
|
||
return JsonTemplateMapper.readJson( | ||
new ByteArrayInputStream(output.getBytes(StandardCharsets.UTF_8)), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps you can use this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the tips. Updated the code to use the overload version you suggested. |
||
DockerImageDetails.class); | ||
} | ||
return JsonTemplateMapper.readJson(inspectProcess.getInputStream(), DockerImageDetails.class); | ||
} | ||
|
||
/** Runs a {@code docker} command. */ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 have a concern.
stdout
is an input stream, and at this point, you may not have reached "EOF" of the stream. That is, the process may be still running and writing characters to the stream after this line, which you will miss onward. And hypothetically, you can run into the same issue again.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.
Hi. According to the documentation and source code(https://github.com/google/guava/blob/master/guava/src/com/google/common/io/CharStreams.java) of CharStreams.toString(), it reads all data from the readable. Besides, this is how other parts of this class handles outputs from the docker process.
But of course please do suggest if you have better ideas in mind. ;)
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.
It's a stream, and what the doc says is a different thing.
To be correct, you need to keep consuming the stream while the process is running (i.e., looping), because you never know when the process will start/resume writing data on the other side of the stream. And after you noticed the process is terminated, you need to consume the stream one last time.
However, I'd be happy just to see an improvement. So, what I suggest is to read the stream once more after
process.waitFor()
. It's definitely an improvement.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.
lol. I do get what you mean but allow me to elaborate a bit more and you can correct me if I am wrong. :)
Reference for the behavior of InputStream.read() can be found here:
https://docs.oracle.com/javase/8/docs/api/java/io/InputStream.html#read-byte:A-
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.
That's good to know. One thing to correct though: you said "r is an InputStream which is not a Reader", but it is a Reader. Can you read the JDK code further to make sure it works too?
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.
And what's the rationale for using a Reader?
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.
Oops. My bad. It is indeed a Reader in our case. Thanks for the correction. :)
Below is the case for Reader.
As you can see, it will still exhaust the underlying Stream. Reference can be found here. https://docs.oracle.com/javase/8/docs/api/java/io/Reader.html
As for the rationale of using a Reader, I was merely following the style of existing code of the same class so that it is consistent.
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.
Cool then. Please just leave the comment "CharStreams.toString() will block until the end of stream is reached." That'll save people questioning the same concern as I did.
I'll let the repo maintainer to review and approve this. Ping them if necessary.
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.
Good idea! Added the comment as requested. :)