perf(gtfs): resolve lock contention in spatial queries#730
Open
AhmedAlian7 wants to merge 1 commit intoOneBusAway:mainfrom
Open
perf(gtfs): resolve lock contention in spatial queries#730AhmedAlian7 wants to merge 1 commit intoOneBusAway:mainfrom
AhmedAlian7 wants to merge 1 commit intoOneBusAway:mainfrom
Conversation
Member
|
@AhmedAlian7 please fix the merge conflicts! |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
Location-based handlers (
/stops-for-location,/routes-for-location,/trips-for-location) were acquiring a broad read lock (RLock()) that spanned the entire lifecycle of the request.This PR mitigates the contention by enforcing narrow-scoped locking. Database operations are now strictly executed outside of the read lock, and handlers no longer wrap the entire request in a locking mechanism.
Changes
gtfs_manager.goGetStopsForLocation(): Refactored to manage its own concurrency internally. It now acquires anRLock()exclusively for the fast, in-memory R-tree spatial search (queryStopsInBounds()) and immediately releases it. Subsequent database queries are executed lock-free. The returned R-tree results are safe to utilize post-unlock, as the spatial index only mutates during static data reloads (which utilize a write lock).API Handlers
Removed the encompassing
RLock()/defer RUnlock()pattern from the following handlers, replacing them with precisely scoped locks for specific in-memory accesses:stopsForLocationHandler: Narrowly scoped locks forGetAgencies()andcheckIfOutOfBounds().routesForLocationHandler: Narrowly scoped locks forGetAgencies()andcheckIfOutOfBounds().tripsForLocationHandler: Narrowly scoped locks forGetAgencies()(withinparseAndValidateRequest) andcheckIfOutOfBounds().Test (
arrivals_and_departures_for_stop_handler_test.go)GetStopsForLocation()withRLock()/RUnlock(). This prevents double-locking deadlocks since the method now handles its own internal locking.