Add a retention period setting for model experiment runs

What does this MR do and why?

Administrators can set a retention period for model experiment runs in Admin > Settings > General > Model experiments and through the application settings API. The default 0 keeps every run (open decision 7), and the setting has no effect yet: a later MR adds the background operation that deletes old runs that no model version uses. When an administrator turns retention on, GitLab records the time, so runs that exist on that day get the full period from that day.

Design notes
  • Storage: a new ml_settings JSONB column on application_settings, with the JSON schema app/validators/json_schemas/application_setting_ml_settings.json. No existing JSONB column holds settings for model experiments or the model registry, so doc/development/application_settings.md asks for a new one. One migration adds the column and two check constraints: jsonb_typeof(ml_settings) = 'object', like 20261014233244_add_o11y_settings_to_application_settings.rb, and octet_length(ml_settings::text) <= 65536, which spec/db/open_ended_columns_spec.rb requires.
  • ml_experiment_tracking_run_retention_days: integer from 0 to 36500, default 0. It is in ApplicationSettingsHelper#visible_attributes, so the Admin form and the API accept it. Changing the default later needs a data migration that writes an explicit value, because existing rows store {} and the application sets the start time only on save.
  • ml_experiment_tracking_run_retention_enabled_at: the start time of the period, in the same column. A before_save callback in ApplicationSetting sets it only when the period changes, so the Admin area, the API and the Rails console behave the same:
    • 0 to a positive value: set to the current time.
    • A positive value to another positive value: kept.
    • A positive value to 0: cleared. Turning retention on again starts a new full period.
  • The start time is not in visible_attributes. The Admin form and the API ignore it when a request sends it, and the API does not return it. Under the field, the Admin area shows the first deletion date: the start time plus one period.
  • How the later operation uses the start time: a run is eligible when the later of its created_at, its updated_at and the start time is more than one period ago. The start time is one value for the instance, so the operation checks start time + period <= now once and keeps its per-run scope on created_at and updated_at.
  • Retention policies, all with enforcing: false, removed from spec/support/database/retention-policy-missing-allowlist.yml:
    • ml_candidates, ml_experiments: delete_rows, 730 days, paused by the ml_experiment_tracking_run_retention_days application setting.
    • ml_candidate_metrics, ml_candidate_params, ml_candidate_metadata, ml_experiment_metadata: transient_data, deleted with their run or experiment. The window matches the parent table, like todo_review_links.yml.
    • ml_models, ml_model_versions, ml_model_metadata, ml_model_version_metadata: indefinite_retention. Users manage these registry entries.
  • config/application_setting_columns/ml_settings.yml sets clusterwide: false, the default that the application settings guide recommends.
  • No feature flag: nothing reads the setting yet, and the default 0 changes nothing. Rollback is a revert.
  • Not in this MR: the background operation and its cron entry, the feature flag around it, any index, the user and administrator docs, the deprecation entry, and the in-product alert.

References

Screenshots or screen recordings

New Model experiments section on Admin > Settings > General, after Account and limit. Master has no such section.

Retention off (default) Retention on
Model experiments section with the period set to 0 Model experiments section with the period set to 730 and the first deletion date
How the screenshots were made

A throwaway view spec rendered the section with the compiled CSS, and headless Chrome took the screenshots, so the page chrome around the section is missing.

How to set up and validate locally

  1. Run bin/rails db:migrate.
  2. Sign in as an administrator and go to http://gdk.test:3000/admin/application_settings/general.
  3. Expand Model experiments. Expect: Run retention period (days) is 0, with no deletion date.
  4. Set the period to 730 and select Save changes. Expect: "Deletion starts on <today plus 730 days> at the earliest." appears under the field.
  5. Set the period to 30 and save. Expect: the date changes to today plus 30 days, because the start day stays the same.
  6. Set the period to 0 and save. Expect: the deletion date line disappears.
API and negative checks
# Expect "ml_experiment_tracking_run_retention_days": 730, and no start time in the response
curl --request PUT --header "PRIVATE-TOKEN: <admin token>" \
  "http://gdk.test:3000/api/v4/application/settings?ml_experiment_tracking_run_retention_days=730&ml_experiment_tracking_run_retention_enabled_at=2020-01-01" \
  | jq '{ml_experiment_tracking_run_retention_days, ml_experiment_tracking_run_retention_enabled_at}'

# Expect 400 Bad Request, also for 36501
curl --request PUT --header "PRIVATE-TOKEN: <admin token>" \
  "http://gdk.test:3000/api/v4/application/settings?ml_experiment_tracking_run_retention_days=-1"

In the Rails console, Gitlab::CurrentSettings.ml_experiment_tracking_run_retention_enabled_at returns the time of the first request, not 2020-01-01.

Migration output

The change adds one column with a constant default and two check constraints to application_settings, which has one row. It adds or changes no queries.

Up:

main: == 20261113150059 AddMlSettingsToApplicationSettings: migrating ===============
main: -- transaction_open?(nil)
main:    -> 0.0000s
main: -- add_column(:application_settings, :ml_settings, :jsonb, {default: {}, null: false, if_not_exists: true})
main:    -> 0.2073s
main: -- transaction_open?(nil)
main:    -> 0.0000s
main: -- transaction_open?(nil)
main:    -> 0.0000s
main: -- execute("ALTER TABLE application_settings\nADD CONSTRAINT check_application_settings_ml_settings_is_hash\nCHECK ( (jsonb_typeof(ml_settings) = 'object') )\nNOT VALID;\n")
main:    -> 0.0008s
main: -- execute("ALTER TABLE application_settings VALIDATE CONSTRAINT check_application_settings_ml_settings_is_hash;")
main:    -> 0.0005s
main: -- transaction_open?(nil)
main:    -> 0.0000s
main: -- transaction_open?(nil)
main:    -> 0.0000s
main: -- execute("ALTER TABLE application_settings\nADD CONSTRAINT check_application_settings_ml_settings_size\nCHECK ( octet_length(ml_settings::text) <= 65536 )\nNOT VALID;\n")
main:    -> 0.0005s
main: -- execute("ALTER TABLE application_settings VALIDATE CONSTRAINT check_application_settings_ml_settings_size;")
main:    -> 0.0005s
main: == 20261113150059 AddMlSettingsToApplicationSettings: migrated (0.2309s) ======

Down:

main: == 20261113150059 AddMlSettingsToApplicationSettings: reverting ===============
main: -- transaction_open?(nil)
main:    -> 0.0000s
main: -- transaction_open?(nil)
main:    -> 0.0000s
main: -- execute("            ALTER TABLE application_settings\n            DROP CONSTRAINT IF EXISTS check_application_settings_ml_settings_size\n")
main:    -> 0.0011s
main: -- transaction_open?(nil)
main:    -> 0.0000s
main: -- transaction_open?(nil)
main:    -> 0.0000s
main: -- execute("            ALTER TABLE application_settings\n            DROP CONSTRAINT IF EXISTS check_application_settings_ml_settings_is_hash\n")
main:    -> 0.0005s
main: -- transaction_open?(nil)
main:    -> 0.0000s
main: -- remove_column(:application_settings, :ml_settings, {if_exists: true})
main:    -> 0.1949s
main: == 20261113150059 AddMlSettingsToApplicationSettings: reverted (0.2101s) ======

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Eduardo Bonet

Merge request reports

Loading
Loading