-
Notifications
You must be signed in to change notification settings - Fork 558
Avoid memory allocation by reusing TrackerClient in DegraderStrategy #1121
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
base: master
Are you sure you want to change the base?
Avoid memory allocation by reusing TrackerClient in DegraderStrategy #1121
Conversation
|
|
||
| private List<DegraderTrackerClient> castToDegraderTrackerClients(Map<URI, TrackerClient> trackerClients) | ||
| { | ||
| List<DegraderTrackerClient> degraderTrackerClients = new ArrayList<>(trackerClients.size()); |
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.
Memory allocation followed by O(N) operations.
| } | ||
|
|
||
| List<DegraderTrackerClient> degraderTrackerClients = castToDegraderTrackerClients(trackerClients); | ||
| Collection<TrackerClient> trackerClientList = trackerClients.values(); |
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.
castToDegraderTrackerClients return an empty list if trackerClients cannot be cast to DegradedTrackerClient. This is to mimic that behavior.
| if (excludedUris == null) | ||
| { | ||
| excludedUris = new HashSet<>(); | ||
| excludedUris = Collections.emptySet(); |
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.
Avoid another unnecessary memory allocation.
| warn(_log, "Can not find hash ring to use"); | ||
| } | ||
|
|
||
| Map<URI, DegraderTrackerClient> trackerClientMap = new HashMap<>(trackerClients.size()); |
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.
Another memory allocation followed by O(N) complexity.
| } | ||
|
|
||
| private DegraderTrackerClient searchClientFromUri(URI uri, List<DegraderTrackerClient> trackerClients) | ||
| private DegraderTrackerClient searchClientFromUri(URI uri, Map<URI, TrackerClient> trackerClients) |
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.
When it is a map, we do the search in O(1) time rather than O(N).
| * Whether to force update | ||
| * @return True if we should update, and false otherwise. | ||
| */ | ||
| protected static boolean shouldUpdatePartition(long clusterGenerationId, PartitionDegraderLoadBalancerState partitionState, |
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.
Can we create a new method and call the new one from the old? This is a protected method and we'd like backwards compatibility.
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.
Kept the old one and created the overloaded version of it. The old one does the conversion (inefficient but still accessible).
shivamgupta1
left a comment
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.
LGTM
The call from SimpleLoadBalancer to DegraderStrategy passes a
Map<URI, TrackerClient>object for thegetTrackerClient()method to handle load balancing. This object was being converted toList<DegraderTrackerClient>usingcastToDegraderTrackerClients()method. However, during the load balancing operation, this casting operation was not needed. This PR aims to rectify that. The PR also have positive impact ongetRing()method which was suffering from the same process.We have updated the unit tests accordingly.