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
Trigger scheduler API endpoint fails to save the results to the database on subsequent calls #683
Comments
Thanks for this report @victorgarcia98 - I researched this behavior before (just can't find it right now), and I saw that this only happened if the very same data gets written a second time. |
Great! Indeed, this shouldn't matter when running the scheduler with the same arguments (here For reproducing the error, I'm using Insomnia to make the calls to the API. If you want, I can export the requests that I'm doing to a file, which should make it easier/faster by importing them to a tool such as Postman. |
@Flix6x do you recall where I wrote about this a few weeks ago? |
@victorgarcia98 I found my previous research on the issue. I concluded that "overwriting of pre-existing data might be broken". This means overwriting with data for the same timing, but with different values. The logic which handles that might be in timely-beliefs. |
Some potentially relevant thoughts. When saving beliefs data that may contain such "changed beliefs" (from the same source, at the same belief time, about the same event, but assigning a different value), we should use a dedicated function we made (I think I would actually expect our scheduling logic (both through the API and the CLI) to generate beliefs with a unique belief time each time the logic is executed. That is, somewhere in the validation of the |
After some research, I found the following: Potential FixesMethod 1A potential fix consists of delete all the objects in the session. This can be done by setting the argument expunge_session of add_to_session to True, exposing it in the function save_to_db as an optional argument with default value False. Instead of expunging all the objects, there's the possibility to expunge just the sensor object. The issue of doing so is that we might lose the data of the Method 2Another potential fix is to create a fresh new HypothesisRelationshipOn saving the results, the Uncommitted sensorOn compute, we update the SOC depending if we compute the schedule with the same parameters or not. In case this is updated, it will requires us to commit the results to the database. That is probably why creating a new sensor instance avoid the problem, deleting the changes that have not been changed. |
Thank you for diving deeper into this. I suspect that we no longer need to keep the logic that updates sensor attributes on calling |
I'm still not able to reproduce. I am running: Versions:
PreparationIn flexmeasures.cfg:
Create
First terminalBash terminal for setting up and running the server:
Second terminalBash terminal for running a worker:
Third terminalBash terminal for running the client:
|
Thanks for such a detailed description! Running through this locally, I encountered the same error both in Python 3.9.16 and Python 3.10.6 (Postgesql v12.14 and v13.11 both showing the same). Trying with @Flix6x, we confirmed the following:
New ideas: |
In the process of using the new command
As in the scheduling, running the command works fine in the first try but fails in subsequent calls, for the same time period. Similar to the scheduling case, I tried to expunge all the objects, but it didn't fix it :( To explore this issue, I run a step-by-step debugging to check the status of the sensor instance object. To check the status of the sensor instance, we can use the following: from sqlalchemy import inspect
inspect(sensor).detached The first report >> sensor.beliefs
[<TimedBelief: at 202...3.463149)>, <TimedBelief: at 202...3.463149)>, ...] After leaving the
With this in consideration, the solution is to reattach the report
|
Two thoughts:
|
Instead, a more DRY solution is to add in the Tested this solution for both cases and it works 🎆 |
When trying to run the toy-schedule multiple times trough the API endpoint, I've encountered that the second time I call the schedule, it fails to save the results to the database, see Error below.
In order to reproduce this, I've tested to trigger the schedule twice with the toy-account (see Setup to reproduce this behavior). This error persists on server restart but is gone on a fresh new database.
A potential fix consists of delete all the objects in the session. This can be done by setting the argument
expunge_session
ofadd_to_session
toTrue
, exposing it in the functionsave_to_db
as an optional argument with default valueFalse
.Follow up
Please, let me know if this issue shows up to you, as well. In case this error happens to you as well, I would like to know if the proposed solution is on point or, requires more research to see the origin of this error.
Error
Setup to reproduce this behavior
1. Add accounts
1.0. Delete database
1.1. toy-account
flexmeasures add toy-account
1.2. admin account
flexmeasures add account --name "admin"
flexmeasures add user --username admin --email admin@admin.com --account-id 2 --roles=admin
2. Authentication
URL
HEADERS
BODY
3. Post Measurements
URL
HEADERS
BODY
4. Trigger Storage Schedule
URL
HEADERS
BODY
The text was updated successfully, but these errors were encountered: