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

Improve caching. #431

Open
wants to merge 1 commit into
base: feature/config-nested
Choose a base branch
from

Conversation

b0661
Copy link
Collaborator

@b0661 b0661 commented Jan 28, 2025

  • Move the caching module to core.

Add an in memory cache that for caching function and method results
during an energy management run (optimization run). Two decorators
are provided for methods and functions.

  • Improve the file cache store by load and save functions.

Make EOS load the cache file store on startup and save it on shutdown.
Add a cyclic task that cleans the cache file store from outdated cache files.

  • Improve startup of EOSdash by EOS

Make EOS starting EOSdash adhere to path configuration given in EOS.
The whole environment from EOS is now passed to EOSdash.
Should also prevent test errors due to unwanted/ wrong config file creation.

  • Allow to check server process startup by health status

Both servers now provide a health endpoint that can be used to detect whether
the server is running. This is also used for testing now.

  • Add administration endpoints to control caching from REST API.

  • Improve test support (to be able to test caching)

    • Add system test option to pytest for running tests with "real" resources
    • Add new test fixture to start server for test class

@Lasall
Copy link
Collaborator

Lasall commented Jan 28, 2025

Very cool improvement. When I cancel the make run app with ctrl+c, I get following error on save (during runtime I used the /pvforecast endpoint with PVForecastAkkudoktor once):

.venv/bin/python src/akkudoktoreos/server/eos.py
INFO:     Started server process [606082]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on http://0.0.0.0:8503 (Press CTRL+C to quit)
^CINFO:     Shutting down
INFO:     Waiting for application shutdown.
ERROR:    Traceback (most recent call last):
  File "/media/data/build/eos-master2/.venv/lib/python3.12/site-packages/starlette/routing.py", line 693, in lifespan
    async with self.lifespan_context(app) as maybe_state:
               ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/contextlib.py", line 217, in __aexit__
    await anext(self.gen)
  File "/media/data/build/eos-master2/src/akkudoktoreos/server/eos.py", line 230, in lifespan
    CacheFileStore().save_store()
  File "/media/data/build/eos-master2/src/akkudoktoreos/core/cache.py", line 775, in save_store
    json.dump(store_to_save, f, indent=4)
  File "/usr/lib/python3.12/json/__init__.py", line 179, in dump
    for chunk in iterable:
                 ^^^^^^^^
  File "/usr/lib/python3.12/json/encoder.py", line 432, in _iterencode
    yield from _iterencode_dict(o, _current_indent_level)
  File "/usr/lib/python3.12/json/encoder.py", line 406, in _iterencode_dict
    yield from chunks
  File "/usr/lib/python3.12/json/encoder.py", line 406, in _iterencode_dict
    yield from chunks
  File "/usr/lib/python3.12/json/encoder.py", line 439, in _iterencode
    o = _default(o)
        ^^^^^^^^^^^
  File "/usr/lib/python3.12/json/encoder.py", line 180, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type builtin_function_or_method is not JSON serializable

ERROR:    Application shutdown failed. Exiting.

Subsequent attempted starts fail with:

.venv/bin/python src/akkudoktoreos/server/eos.py
INFO:     Started server process [607458]
INFO:     Waiting for application startup.
ERROR:    Traceback (most recent call last):
  File "/media/data/build/eos-master2/.venv/lib/python3.12/site-packages/starlette/routing.py", line 693, in lifespan
    async with self.lifespan_context(app) as maybe_state:
               ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/contextlib.py", line 210, in __aenter__
    return await anext(self.gen)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/media/data/build/eos-master2/src/akkudoktoreos/server/eos.py", line 222, in lifespan
    CacheFileStore().load_store()
  File "/media/data/build/eos-master2/src/akkudoktoreos/core/cache.py", line 782, in load_store
    store_loaded = json.load(f)
                   ^^^^^^^^^^^^
  File "/usr/lib/python3.12/json/__init__.py", line 293, in load
    return loads(fp.read(),
           ^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/json/decoder.py", line 338, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.12/json/decoder.py", line 356, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 6 column 25 (char 276)

ERROR:    Application startup failed. Exiting.

@b0661 b0661 force-pushed the pr_improve_caching branch from 022824c to 39d5ed8 Compare January 28, 2025 20:56
@b0661
Copy link
Collaborator Author

b0661 commented Jan 28, 2025

Added some error logging. Maybe it becomes clearer where the error sits. Conclusion: system testing has to be improved.

@b0661 b0661 force-pushed the pr_improve_caching branch from 39d5ed8 to 3909e3d Compare January 28, 2025 21:19
@Lasall
Copy link
Collaborator

Lasall commented Jan 28, 2025

Thank you! The app starts and closes now again successfully (and the cachefilestore.json tracks the cache files). It doesn't seem to load the data from cache yet however (/v1/prediction/list is empty)

@b0661 b0661 force-pushed the pr_improve_caching branch 4 times, most recently from 0def910 to fb58ec8 Compare February 2, 2025 19:26
@b0661 b0661 mentioned this pull request Feb 2, 2025
@b0661 b0661 force-pushed the pr_improve_caching branch from fb58ec8 to 24e1bf6 Compare February 7, 2025 18:57
@b0661
Copy link
Collaborator Author

b0661 commented Feb 7, 2025

It doesn't seem to load the data from cache yet however (/v1/prediction/list is empty)

Should now load cached data. Please test.

@b0661 b0661 force-pushed the pr_improve_caching branch 4 times, most recently from 8acdf95 to 88e0197 Compare February 8, 2025 02:13
* Move the caching module to core.

Add an in memory cache that for caching function and method results
during an energy management run (optimization run). Two decorators
are provided for methods and functions.

* Improve the file cache store by load and save functions.

Make EOS load the cache file store on startup and save it on shutdown.
Add a cyclic task that cleans the cache file store from outdated cache files.

* Improve startup of EOSdash by EOS

Make EOS starting EOSdash adhere to path configuration given in EOS.
The whole environment from EOS is now passed to EOSdash.
Should also prevent test errors due to unwanted/ wrong config file creation.

* Allow to check server process startup by health status

Both servers now provide a health endpoint that can be used to detect whether
the server is running. This is also used for testing now.

* Add administration endpoints to control caching from REST API.

* Improve test support (to be able to test caching)

  - Add system test option to pytest for running tests with "real" resources
  - Add new test fixture to start server for test class

Signed-off-by: Bobby Noelte <[email protected]>
@b0661 b0661 force-pushed the pr_improve_caching branch from 88e0197 to d20dc86 Compare February 8, 2025 19:12
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.

2 participants