From 3408fbf20c62936eda0edb54a486996d8ff5fa9c Mon Sep 17 00:00:00 2001 From: Jackie Goldstein Date: Fri, 24 Mar 2023 10:59:20 -0400 Subject: [PATCH 1/4] [batch] Add feature switches --- batch/batch/driver/main.py | 45 ++++++++++++++++++++++++- batch/batch/driver/templates/index.html | 13 +++++++ batch/sql/add-feature-switches.sql | 5 +++ batch/sql/delete-batch-tables.sql | 3 +- batch/sql/estimated-current.sql | 4 +++ build.yaml | 3 ++ 6 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 batch/sql/add-feature-switches.sql diff --git a/batch/batch/driver/main.py b/batch/batch/driver/main.py index e65ddfa7953..1261741d43a 100644 --- a/batch/batch/driver/main.py +++ b/batch/batch/driver/main.py @@ -38,7 +38,14 @@ from hailtop import aiotools, httpx from hailtop.config import get_deploy_config from hailtop.hail_logging import AccessLogger -from hailtop.utils import AsyncWorkerPool, Notice, dump_all_stacktraces, flatten, periodically_call, time_msecs +from hailtop.utils import ( + AsyncWorkerPool, + Notice, + dump_all_stacktraces, + flatten, + periodically_call, + time_msecs, +) from web_common import render_template, set_message, setup_aiohttp_jinja2, setup_common_static_routes from ..batch import cancel_batch_in_db @@ -552,6 +559,27 @@ def validate_int(session, name, value, predicate, description): return validate(session, name, i, predicate, description) +@routes.post('/configure-feature-switches') +@check_csrf_token +@auth.web_authenticated_developers_only() +async def configure_feature_switches(request, userdata): # pylint: disable=unused-argument + app = request.app + db: Database = app['db'] + post = await request.post() + + compact_billing_tables = 'compact_billing_tables' in post + + await db.execute_update( + ''' +UPDATE feature_switches SET compact_billing_tables = %s; +''', + (compact_billing_tables,), + ) + + row = await db.select_and_fetchone('SELECT * FROM feature_switches') + app['feature_switches'] = row + + @routes.post('/config-update/pool/{pool}') @check_csrf_token @auth.web_authenticated_developers_only() @@ -1335,6 +1363,16 @@ async def monitor_system(app): monitor_instances(app) +async def compact_agg_billing_project_users_table(app, db: Database): + if not app['feature_flags']['compact_billing_tables']: + return + + +async def compact_agg_billing_project_users_by_date_table(app, db: Database): + if not app['feature_flags']['compact_billing_tables']: + return + + async def scheduling_cancelling_bump(app): log.info('scheduling cancelling bump loop') app['scheduler_state_changed'].notify() @@ -1412,6 +1450,9 @@ async def on_startup(app): app['batch_headers'] = {'Authorization': f'Bearer {row["internal_token"]}'} app['frozen'] = row['frozen'] + row = await db.select_and_fetchone('SELECT * FROM feature_switches') + app['feature_switches'] = row + await refresh_globals_from_db(app, db) app['scheduler_state_changed'] = Notice() @@ -1437,6 +1478,8 @@ async def on_startup(app): task_manager.ensure_future(periodically_call(60, scheduling_cancelling_bump, app)) task_manager.ensure_future(periodically_call(15, monitor_system, app)) task_manager.ensure_future(periodically_call(5, refresh_globals_from_db, app, db)) + task_manager.ensure_future(periodically_call(60, compact_agg_billing_project_users_table, app, db)) + task_manager.ensure_future(periodically_call(60, compact_agg_billing_project_users_by_date_table, app, db)) async def on_cleanup(app): diff --git a/batch/batch/driver/templates/index.html b/batch/batch/driver/templates/index.html index 161f0292bc2..7da63380a75 100644 --- a/batch/batch/driver/templates/index.html +++ b/batch/batch/driver/templates/index.html @@ -25,6 +25,19 @@

Globals

{% endif %} +

Feature Switches

+
+
+ + + +
+
+

Instance Collections

Pools

diff --git a/batch/sql/add-feature-switches.sql b/batch/sql/add-feature-switches.sql new file mode 100644 index 00000000000..b8fdc3ebe60 --- /dev/null +++ b/batch/sql/add-feature-switches.sql @@ -0,0 +1,5 @@ +CREATE TABLE IF NOT EXISTS `feature_switches` ( + `compact_billing_tables` BOOLEAN NOT NULL +) ENGINE = InnoDB; + +INSERT INTO `feature_switches` (compact_billing_tables) VALUES (0); diff --git a/batch/sql/delete-batch-tables.sql b/batch/sql/delete-batch-tables.sql index 754f42018c3..e31c35d8037 100644 --- a/batch/sql/delete-batch-tables.sql +++ b/batch/sql/delete-batch-tables.sql @@ -79,4 +79,5 @@ DROP TABLE IF EXISTS `pools`; DROP TABLE IF EXISTS `inst_colls`; DROP TABLE IF EXISTS `latest_product_versions`; DROP TABLE IF EXISTS `resources`; -DROP TABLE IF EXISTS `regions` +DROP TABLE IF EXISTS `regions`; +DROP TABLE IF EXISTS `feature_switches`; diff --git a/batch/sql/estimated-current.sql b/batch/sql/estimated-current.sql index 5bdf4d430c8..952f1d99c90 100644 --- a/batch/sql/estimated-current.sql +++ b/batch/sql/estimated-current.sql @@ -5,6 +5,10 @@ CREATE TABLE IF NOT EXISTS `globals` ( `frozen` BOOLEAN NOT NULL DEFAULT FALSE ) ENGINE = InnoDB; +CREATE TABLE IF NOT EXISTS `feature_switches` ( + `compact_billing_tables` BOOLEAN NOT NULL +) ENGINE = InnoDB; + CREATE TABLE IF NOT EXISTS `resources` ( `resource` VARCHAR(100) NOT NULL, `rate` DOUBLE NOT NULL, diff --git a/build.yaml b/build.yaml index 0af6d2f4b0d..6b1bfe2b972 100644 --- a/build.yaml +++ b/build.yaml @@ -2282,6 +2282,9 @@ steps: - name: add-list-batches-index script: /io/sql/add-list-batches-index.sql online: true + - name: add-feature-switches + script: /io/sql/add-feature-switches.sql + online: true inputs: - from: /repo/batch/sql to: /io/sql From 824e88326a1c34a12d5337ca667ac8802794c870 Mon Sep 17 00:00:00 2001 From: Jackie Goldstein Date: Wed, 19 Jul 2023 13:44:29 -0400 Subject: [PATCH 2/4] fix --- batch/batch/driver/main.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/batch/batch/driver/main.py b/batch/batch/driver/main.py index 1261741d43a..873f0b0d030 100644 --- a/batch/batch/driver/main.py +++ b/batch/batch/driver/main.py @@ -1363,12 +1363,12 @@ async def monitor_system(app): monitor_instances(app) -async def compact_agg_billing_project_users_table(app, db: Database): +async def compact_agg_billing_project_users_table(app): if not app['feature_flags']['compact_billing_tables']: return -async def compact_agg_billing_project_users_by_date_table(app, db: Database): +async def compact_agg_billing_project_users_by_date_table(app): if not app['feature_flags']['compact_billing_tables']: return @@ -1478,8 +1478,8 @@ async def on_startup(app): task_manager.ensure_future(periodically_call(60, scheduling_cancelling_bump, app)) task_manager.ensure_future(periodically_call(15, monitor_system, app)) task_manager.ensure_future(periodically_call(5, refresh_globals_from_db, app, db)) - task_manager.ensure_future(periodically_call(60, compact_agg_billing_project_users_table, app, db)) - task_manager.ensure_future(periodically_call(60, compact_agg_billing_project_users_by_date_table, app, db)) + task_manager.ensure_future(periodically_call(60, compact_agg_billing_project_users_table, app)) + task_manager.ensure_future(periodically_call(60, compact_agg_billing_project_users_by_date_table, app)) async def on_cleanup(app): From 4f9a4aa4b79b825bc5058fc67e233d8b89774dd1 Mon Sep 17 00:00:00 2001 From: Jackie Goldstein Date: Wed, 19 Jul 2023 15:32:19 -0400 Subject: [PATCH 3/4] address comments --- batch/batch/driver/main.py | 15 ++++++++------- batch/batch/driver/templates/index.html | 2 +- batch/sql/add-feature-flags.sql | 5 +++++ batch/sql/add-feature-switches.sql | 5 ----- batch/sql/delete-batch-tables.sql | 2 +- batch/sql/estimated-current.sql | 2 +- build.yaml | 4 ++-- 7 files changed, 18 insertions(+), 17 deletions(-) create mode 100644 batch/sql/add-feature-flags.sql delete mode 100644 batch/sql/add-feature-switches.sql diff --git a/batch/batch/driver/main.py b/batch/batch/driver/main.py index 873f0b0d030..66a466c1fb5 100644 --- a/batch/batch/driver/main.py +++ b/batch/batch/driver/main.py @@ -466,6 +466,7 @@ async def get_index(request, userdata): 'total_provisioned_cores_mcpu': inst_coll_manager.global_total_provisioned_cores_mcpu, 'live_free_cores_mcpu': inst_coll_manager.global_current_version_live_free_cores_mcpu, 'frozen': app['frozen'], + 'feature_flags': app['feature_flags'], } return await render_template('batch-driver', request, userdata, 'index.html', page_context) @@ -559,10 +560,10 @@ def validate_int(session, name, value, predicate, description): return validate(session, name, i, predicate, description) -@routes.post('/configure-feature-switches') +@routes.post('/configure-feature-flags') @check_csrf_token @auth.web_authenticated_developers_only() -async def configure_feature_switches(request, userdata): # pylint: disable=unused-argument +async def configure_feature_flags(request, userdata): # pylint: disable=unused-argument app = request.app db: Database = app['db'] post = await request.post() @@ -571,13 +572,13 @@ async def configure_feature_switches(request, userdata): # pylint: disable=unus await db.execute_update( ''' -UPDATE feature_switches SET compact_billing_tables = %s; +UPDATE feature_flags SET compact_billing_tables = %s; ''', (compact_billing_tables,), ) - row = await db.select_and_fetchone('SELECT * FROM feature_switches') - app['feature_switches'] = row + row = await db.select_and_fetchone('SELECT * FROM feature_flags') + app['feature_flags'] = row @routes.post('/config-update/pool/{pool}') @@ -1450,8 +1451,8 @@ async def on_startup(app): app['batch_headers'] = {'Authorization': f'Bearer {row["internal_token"]}'} app['frozen'] = row['frozen'] - row = await db.select_and_fetchone('SELECT * FROM feature_switches') - app['feature_switches'] = row + row = await db.select_and_fetchone('SELECT * FROM feature_flags') + app['feature_flags'] = row await refresh_globals_from_db(app, db) diff --git a/batch/batch/driver/templates/index.html b/batch/batch/driver/templates/index.html index 7da63380a75..73fb7a73458 100644 --- a/batch/batch/driver/templates/index.html +++ b/batch/batch/driver/templates/index.html @@ -27,7 +27,7 @@

Globals

Feature Switches

-
+ Date: Thu, 20 Jul 2023 14:57:25 -0400 Subject: [PATCH 4/4] Switches => Flags --- batch/batch/driver/templates/index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/batch/batch/driver/templates/index.html b/batch/batch/driver/templates/index.html index 73fb7a73458..26016670cd5 100644 --- a/batch/batch/driver/templates/index.html +++ b/batch/batch/driver/templates/index.html @@ -25,7 +25,7 @@

Globals

{% endif %}
-

Feature Switches

+

Feature Flags