-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Add performance improvements for opening websocket with SQL backed storage #5296
Open
revilo464
wants to merge
1
commit into
traccar:master
Choose a base branch
from
revilo464:websocket-performance-enhancements
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 all commits
Commits
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
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
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
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
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
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 |
---|---|---|
|
@@ -30,6 +30,7 @@ | |
import jakarta.inject.Inject; | ||
import javax.sql.DataSource; | ||
import java.sql.SQLException; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
@@ -230,6 +231,17 @@ private Map<String, Object> getConditionVariables(Condition genericCondition) { | |
var condition = (Condition.LatestPositions) genericCondition; | ||
if (condition.getDeviceId() > 0) { | ||
results.put("deviceId", condition.getDeviceId()); | ||
} else if (condition.getDeviceIds() != null) { | ||
int paramMultiple = (condition.getDeviceIds().size() / 10) + 1; | ||
List<Long> deviceList = new ArrayList<>(condition.getDeviceIds()); | ||
for (int i = 0; i < paramMultiple * 10; i++) { | ||
String key = String.format("deviceId%s", i); | ||
if (i < deviceList.size()) { | ||
results.put(key, deviceList.get(i)); | ||
} else { | ||
results.put(key, 0L); | ||
} | ||
} | ||
} | ||
} | ||
return results; | ||
|
@@ -291,6 +303,18 @@ private String formatCondition(Condition genericCondition, boolean appendWhere) | |
result.append(getStorageName(Device.class)); | ||
if (condition.getDeviceId() > 0) { | ||
result.append(" WHERE id = :deviceId"); | ||
} else if (condition.getDeviceIds() != null) { | ||
result.append(" WHERE id IN ( "); | ||
int paramMultiple = (condition.getDeviceIds().size() / 10) + 1; | ||
for (int i = 0; i < paramMultiple; i++) { | ||
for (int j = 0; j < 10; j++) { | ||
result.append(String.format(":deviceId%s", j + (10 * i))); | ||
if (i != (paramMultiple - 1) || j != 9) { | ||
result.append(", "); | ||
} | ||
} | ||
} | ||
result.append(" )"); | ||
Comment on lines
+307
to
+317
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. This definitely doesn't look right. If a user has thousands of devices, they shouldn't be included here one by one. |
||
} | ||
result.append(")"); | ||
|
||
|
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
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 think the right solution is to do a single query that would combine devices query with positions.
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 agree. I’ll refactor.
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.
While working on this suggestion, I found that
org.traccar.storage.DatabaseStorage#getObjects
only instantiates objects of a given type, and the members should match the SQL ResultSet. In the case of joining Positions to Devices in the Devices query, I would only be able to instantiate one or the other, unless I created another type of object (something akin toDevicePosition
).Would this be your recommendation?
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'm not proposing to join tables, but filter positions by devices.
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.
From your first message in this thread proposing a single query that would combine devices query with positions I see no other way to do this than performing a join...
Here would be the proposed query which would seem to work:
But as mentioned the Traccar DB interface in its current form only allows returning a single type of object.
When you suggest, however, filtering positions by devices, that seems to be exactly what I'm doing with the IN operator in this PR. Your current implementation also filters positions by devices, but it does this only after creating Java objects from all of the latest positions, flooding the heap when the amount of devices in the DB is large (>40k).
Would love to implement this in a way the project would appreciate. 😃
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 don't understand why you need joins. What I'm proposing is something like this:
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.
Thanks for the clarification Anton. I understand now.
However, this again adds the issue that we can't reuse the list of devices accessible to the user and this is the most expensive part of the servlet request right now. In all of my measurements the permission query takes the majority (>80%) of the wall time. I already refactored so it would only happen once. If we cannot save the deviceIds from the permission query then we repeat it again which doubles the wall time requirement of the servlet request again.
See attached what the servlet request looks like before removing the second call. Your proposal would require the second call 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.
That's a different issue and I think it's a reasonable trade-off.