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

Thread safety of buffers and caches #38

Closed
wants to merge 1 commit into from
Closed

Thread safety of buffers and caches #38

wants to merge 1 commit into from

Conversation

radhisat
Copy link
Contributor

@radhisat radhisat commented Jun 7, 2024

Issue #, if available:

Description of changes:
This change wraps the LinkedHashMap and ArrayList used to access IOBlocks in Synchronized data structures to make operations thread safe.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Copy link
Collaborator

@ahmarsuhail ahmarsuhail left a comment

Choose a reason for hiding this comment

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

+1, LGTM. but i don't have a lot of knowledge on synchronisation, so let's get some more reviews

return this.size() > configuration.getCapacityMultiObjects();
}
};
Collections.synchronizedMap(
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we want to consider a ConcurrentHashMap so we don't lock on the entire map?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, ConcurrentHashMap is faster but does not provide the LRU eviction functionalities. Ilya is trying out with https://github.com/google/guava/wiki/CachesExplained. If that works, we can use the Guava Cache

Copy link
Contributor

Choose a reason for hiding this comment

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

Awesome, thanks!

Copy link
Collaborator

Choose a reason for hiding this comment

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

We probably don't want to take a dependency on Guava though :/ you run into dependency conflict issues in the hadoop stack, that's why we copied pasted Preconditions from Guava

Copy link
Collaborator

Choose a reason for hiding this comment

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

We could possibly shade Guava possibly. but let's evaluate if we really need it and the discuss with Oleg

@@ -25,7 +27,8 @@ public AutoClosingCircularBuffer(int maxCapacity) {

this.oldestIndex = 0;
this.capacity = maxCapacity;
this.buffer = new ArrayList<>(maxCapacity);
this.buffer = Collections.synchronizedList(new ArrayList<>(maxCapacity));
// this.buffer = new ArrayList<>(maxCapacity);
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need this comment?

@@ -4,14 +4,16 @@
import java.io.Closeable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;

/**
* A circular buffer of fixed capacity. Closes its elements before removing them. Not thread-safe.
Copy link
Contributor

Choose a reason for hiding this comment

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

"Not thread-safe" --> Does it still stand after this change?

@radhisat radhisat closed this by deleting the head repository Sep 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants