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

Store limited number of models and clear older ones #948

Merged
merged 14 commits into from
Feb 10, 2024

Conversation

MukuFlash03
Copy link
Contributor

@MukuFlash03 MukuFlash03 commented Dec 11, 2023

Clearing or trimming number of models for a particular user from the Stage_updateable_models collection.
Constant K_MODELS_COUNT defines the number of models to keep.
Comparing with write_ts of Kth model entry to decide how many models to remove which are older than this write_ts.

Currently tested with model data available in stage dataset snapshot.
DONE - Testing done by generating mock models and cleaning them up.

Mahadik, Mukul Chandrakant added 5 commits December 4, 2023 17:47
Completed marked future fixes for PR-944
Provided more detailed info regarding number of distinct users, as well  as number of predictions made for the trip_list.
Clearing or trimming number of models for a particular user from the Stage_updateable_models collection.

Constant K_MODELS_COUNT defines the number of models to keep.

Currently tested with model data available in stage dataset snapshot.

Pending - Complete testing with assertions to be added for testing.
Called the trim_model_entries function in upsert_model to delete models before inserting new ones.
commit 54659fb
Merge: cf0c9e2 1159eac
Author: K. Shankari <[email protected]>
Date:   Thu Dec 21 20:17:15 2023 -0800

    Merge pull request e-mission#951 from MukuFlash03/fix-vuln

    Bulk deletion of site-package/tests

commit 1159eac
Author: Mahadik, Mukul Chandrakant <[email protected]>
Date:   Thu Dec 21 20:43:39 2023 -0700

    Bulk deletion of site-package/tests

    Added a one line pipe command to remove all occurrences of site-packages/tests occurring in miniconda main directory.

commit cf0c9e2
Merge: d2f38bc 3be2757
Author: K. Shankari <[email protected]>
Date:   Thu Dec 21 17:47:27 2023 -0800

    Merge pull request e-mission#950 from MukuFlash03/fix-vuln

    Remove obsolete package versions

commit 3be2757
Author: Mahadik, Mukul Chandrakant <[email protected]>
Date:   Thu Dec 21 18:05:23 2023 -0700

    Remove obsolete package versions

    Cleaned up older versions for two packages:
    urllib3 - deleted stale version folders
    python - deleted tests folder

commit d2f38bc
Merge: 978a719 c1b0889
Author: K. Shankari <[email protected]>
Date:   Wed Dec 20 14:31:09 2023 -0800

    Merge pull request e-mission#949 from MukuFlash03/fix-vuln

    Fixing latest Docker image vulnerabilities

commit c1b0889
Author: Mahadik, Mukul Chandrakant <[email protected]>
Date:   Mon Dec 18 11:04:25 2023 -0700

    Upgraded Ubuntu base image

    Latest Ubuntu base image was just released officially by Docker which contains updated version of libc6 and libc-bin.

commit 07747d0
Author: Mahadik, Mukul Chandrakant <[email protected]>
Date:   Fri Dec 15 18:38:12 2023 -0700

    Fixing latest Docker image vulnerabilities

    AWS Inspector found the following vulnerable packages:

    CRITICAL
    perl

    HIGH
    nghttp2, libnghttp2-14
    cryptography, libssl3
    cryptography
    libc6, libc-bin

    Upgraded perl, libssl3, nghttp2 packages by upgrading base Ubuntu image to latest of the same LTS version - jammy (22.04).

    Cryptography package was fixed by mentioning required version to be installed using conda.

    Libc6, Libc-bin can be fixed by using apt-get upgrade but this upgrades all packages which is not recommended as a blanket upgrade fix.
Was using pre-built models for testing that were present in stage_snapshot dataset but these won't be available in the normal dataset.

Hence, took reference from emission.tests.modellingTests.TestRunGreedyModel.py for creating test models using dummy trip data.

Creating multiple such models and then checking for trimming operation success.
Copy link
Contributor

@shankari shankari left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am stopping at this point because there are multiple comments related to , please clean up this PR and re-submit for review.

.docker/setup_config.sh Outdated Show resolved Hide resolved
Dockerfile Outdated Show resolved Hide resolved
emission/storage/modifiable/builtin_model_storage.py Outdated Show resolved Hide resolved
Mahadik, Mukul Chandrakant added 4 commits January 26, 2024 12:45
Commit 31626ba included changes related to vulnerability fixes, hence reverted them.

Had added some log statements related to scalability fixes for model loading.
Removed them and will add in separate PR.
Removed redundant "esda" import added possibly during local testing.

Removed get_model_limit() as K_MODEL_COUNT is a class variable and can be accessed directly using class name.
This reverts commit 31626ba.

Earlier commit f54e85f missed out the discarded reverted changes relating to the vulnerability fixes.
Hence removing them in these.
Decided upon threshold value for model count above which redundant models will be deleted.
This was agreed upon to be 3 and is defined in the trip_model.config.json.sample file.

Necessary changes have been made in the related files to use this config value.
Copy link
Contributor

@shankari shankari left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good at a high level; but I would like some polishing changes...

emission/storage/modifiable/builtin_model_storage.py Outdated Show resolved Hide resolved
emission/storage/modifiable/builtin_model_storage.py Outdated Show resolved Hide resolved
emission/tests/storageTests/TestModelStorage.py Outdated Show resolved Hide resolved
emission/tests/storageTests/TestModelStorage.py Outdated Show resolved Hide resolved
emission/tests/storageTests/TestModelStorage.py Outdated Show resolved Hide resolved
Mahadik, Mukul Chandrakant added 2 commits February 5, 2024 13:26
- Improved logging statements to included whether model count has changed.
- Skipped check for saved test data in Test file as we clear teardown() related collections in the DB.
Did this to revert a whitespace change which was possibly due to removing a line at the end of the file.

We don't want it to be modified as it is unrelated to this set of PR changes.

Restored using this command by pointing to specific commit hash for the first instance where the file was modified.

git restore --source=79ad1cc~1 emission/analysis/classification/inference/labels/inferrers.py
Copy link
Contributor

@shankari shankari left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where in here are you checking that the models that are deleted are the oldest ones? I see that the code does that with the sort on the metadata.write_ts but I don't see a test for that. If I changed

edb.get_model_db().find(find_query).sort("metadata.write_ts", -1)

to

edb.get_model_db().find(find_query).sort("metadata.write_ts", 1)

I believe the test would still pass

emission/analysis/modelling/trip_model/config.py Outdated Show resolved Hide resolved
emission/storage/modifiable/builtin_model_storage.py Outdated Show resolved Hide resolved
Added test to ensure that only the latest models are stored by comparing the write_ts times.
@MukuFlash03
Copy link
Contributor Author

Where in here are you checking that the models that are deleted are the oldest ones? I see that the code does that with the sort on the metadata.write_ts but I don't see a test for that. If I changed

edb.get_model_db().find(find_query).sort("metadata.write_ts", -1)

to

edb.get_model_db().find(find_query).sort("metadata.write_ts", 1)

I believe the test would still pass

Thank you for pointing that out; that is indeed an important test I missed.

For testing purposes, I did change the sort(-1) to sort(1) which leads to ascending order of queried models based on write_ts.
I had the following observations :

  • Initial count assertion itself fails (which is good!) as all available models satisfy write_ts condition of $ lte < write_ts_limit .
            filter_clause = {
                "user_id" : self.user_id,
                "metadata.key" : key,
                "metadata.write_ts" : { "$lte" : write_ts_limit }
            }
  • Hence every time all models are being deleted and only one model remains (current_model_count = 1) which is the latest one being inserted.
if i <= (maximum_stored_model_count - 1):
    self.assertEqual(current_model_count, i+1)
else:
    self.assertEqual(current_model_count, maximum_stored_model_count)
  • Just to get beyond this point, I tried testing without this assertion, to observe write_ts_lists:
    • For every (maximum_stored_model_count + 1)th model added, the first maximum_stored_model_count models are deleted and the latest model is inserted and this repeats.

So, to reinforce the testing, I’m adding another assertion to confirm that only the latest top K or max_model_count models are stored by storing the write_ts times in two different lists and checking for equality of the top K entries:

- model_creation_write_ts_list : stores write_ts times each time a model is created in the for loop.
- stored_model_write_ts_list : stores write_ts times of all the already stored models in the DB, which should just have the latest models.
- The last 'maximum_stored_model_count' in model_creation_write_ts_list should match those in stored_model_write_ts_list.

@shankari
Copy link
Contributor

shankari commented Feb 9, 2024

I do not 100% understand how the first assertion tripped, but I am satisfied with the changes and am happy to merge!

@shankari shankari merged commit 911e1ec into e-mission:master Feb 10, 2024
5 checks passed
@MukuFlash03 MukuFlash03 deleted the model-cleanup branch April 18, 2024 19:18
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