Skip to content
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

HDDS-6157. More consistent synchronization in InputStreams #2965

Merged
merged 1 commit into from Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -512,7 +512,7 @@ synchronized long getBlockPosition() {
}

@Override
public void unbuffer() {
public synchronized void unbuffer() {
storePosition();
releaseClient();

Expand Down
Expand Up @@ -351,14 +351,14 @@ public boolean seekToNewSource(long targetPos) throws IOException {
}

@Override
public int available() throws IOException {
public synchronized int available() throws IOException {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to return remaining value, if multiple people are operating on this stream, with synchronised also this can give stale values.
Just out of curiosity: Any pointers in which use case having this synchronised will help

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, available() in S3AInputStream and HDFS's DFSInputStream are both synchronized.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if multiple people are operating on this stream, with synchronised also this can give stale values

Correct, that may be a design choice of InputStream in general.

However, synchronization helps not only with concurrent usage, but also sequential usage in multiple threads, as it provides visibility guarantees.

I don't have a use case for making this specific method synchronized, but nor do I for the other methods that are already synced. It's just a matter of consistency.

checkOpen();
long remaining = length - getPos();
return remaining <= Integer.MAX_VALUE ? (int) remaining : Integer.MAX_VALUE;
}

@Override
public void close() throws IOException {
public synchronized void close() throws IOException {
closed = true;
for (BlockInputStream blockStream : blockStreams) {
blockStream.close();
Expand Down Expand Up @@ -388,7 +388,7 @@ public long getRemainingOfIndex(int index) throws IOException {
}

@Override
public long skip(long n) throws IOException {
public synchronized long skip(long n) throws IOException {
if (n <= 0) {
return 0;
}
Expand All @@ -399,7 +399,7 @@ public long skip(long n) throws IOException {
}

@Override
public void unbuffer() {
public synchronized void unbuffer() {
for (BlockInputStream is : blockStreams) {
is.unbuffer();
}
Expand Down