From 87f05c41517e1312b0662d9e3460935f9d07fc5c Mon Sep 17 00:00:00 2001 From: Xin Hao Zhang Date: Wed, 25 Sep 2024 17:25:47 -0400 Subject: [PATCH] server: add tablemetadata job cluster settings to job status api Add the cluster setting values for the update table metadata job to the job status api. Epic: CRDB-37558 Release note: None --- pkg/server/api_v2_databases_metadata.go | 7 +++++++ pkg/server/api_v2_databases_metadata_test.go | 21 ++++++++++++------- .../tablemetadatacache/cluster_settings.go | 2 +- .../update_table_metadata_cache_job.go | 4 ++-- 4 files changed, 23 insertions(+), 11 deletions(-) diff --git a/pkg/server/api_v2_databases_metadata.go b/pkg/server/api_v2_databases_metadata.go index 9b4094346db1..7914c075c72a 100644 --- a/pkg/server/api_v2_databases_metadata.go +++ b/pkg/server/api_v2_databases_metadata.go @@ -850,6 +850,9 @@ func (a *apiV2Server) TableMetadataJob(w http.ResponseWriter, r *http.Request) { func (a *apiV2Server) getTableMetadataUpdateJobStatus( ctx context.Context, ) (jobStatus tmUpdateJobStatusResponse, retErr error) { + jobStatus.DataValidDuration = tablemetadatacache.DataValidDurationSetting.Get(&a.sqlServer.execCfg.Settings.SV) + jobStatus.AutomaticUpdatesEnabled = tablemetadatacache.AutomaticCacheUpdatesEnabledSetting.Get(&a.sqlServer.execCfg.Settings.SV) + query := safesql.NewQuery() query.Append(` SELECT @@ -1000,6 +1003,10 @@ type tmUpdateJobStatusResponse struct { LastStartTime *time.Time `json:"last_start_time"` LastCompletedTime *time.Time `json:"last_completed_time"` LastUpdatedTime *time.Time `json:"last_updated_time"` + // The value of tablemetadatacache.DataValidDurationSetting + DataValidDuration time.Duration `json:"data_valid_duration"` + // The value of tablemetadatacache.AutomaticCacheUpdatesEnabledSetting + AutomaticUpdatesEnabled bool `json:"automatic_updates_enabled"` } type tmJobTriggeredResponse struct { diff --git a/pkg/server/api_v2_databases_metadata_test.go b/pkg/server/api_v2_databases_metadata_test.go index c1b597312535..a0b92d712632 100644 --- a/pkg/server/api_v2_databases_metadata_test.go +++ b/pkg/server/api_v2_databases_metadata_test.go @@ -620,8 +620,7 @@ func TestGetTableMetadataUpdateJobStatus(t *testing.T) { testCluster := serverutils.StartCluster(t, 1, base.TestClusterArgs{}) ctx := context.Background() defer testCluster.Stopper().Stop(ctx) - conn := testCluster.ServerConn(0) - defer conn.Close() + conn := sqlutils.MakeSQLRunner(testCluster.ServerConn(0)) ts := testCluster.Server(0) @@ -634,21 +633,27 @@ func TestGetTableMetadataUpdateJobStatus(t *testing.T) { failed := makeApiRequest[interface{}](t, userClient, ts.AdminURL().WithPath(uri).String(), http.MethodGet) require.Equal(t, http.StatusText(http.StatusNotFound), failed) - _, e := conn.Exec(fmt.Sprintf("GRANT CONNECT ON DATABASE defaultdb TO %s", sessionUsername.Normalized())) - require.NoError(t, e) + conn.Exec(t, fmt.Sprintf("GRANT CONNECT ON DATABASE defaultdb TO %s", sessionUsername.Normalized())) mdResp := makeApiRequest[tmUpdateJobStatusResponse](t, userClient, ts.AdminURL().WithPath(uri).String(), http.MethodGet) require.Equal(t, "NOT_RUNNING", mdResp.CurrentStatus) + require.Equal(t, false, mdResp.AutomaticUpdatesEnabled) + require.Equal(t, 20*time.Minute, mdResp.DataValidDuration) - _, e = conn.Exec(fmt.Sprintf("REVOKE CONNECT ON DATABASE defaultdb FROM %s", sessionUsername.Normalized())) - require.NoError(t, e) + conn.Exec(t, fmt.Sprintf("REVOKE CONNECT ON DATABASE defaultdb FROM %s", sessionUsername.Normalized())) failed = makeApiRequest[string](t, userClient, ts.AdminURL().WithPath(uri).String(), http.MethodGet) require.Equal(t, http.StatusText(http.StatusNotFound), failed) - _, e = conn.Exec(fmt.Sprintf("GRANT admin TO %s", sessionUsername.Normalized())) - require.NoError(t, e) + conn.Exec(t, fmt.Sprintf("GRANT admin TO %s", sessionUsername.Normalized())) mdResp = makeApiRequest[tmUpdateJobStatusResponse](t, userClient, ts.AdminURL().WithPath(uri).String(), http.MethodGet) require.Equal(t, "NOT_RUNNING", mdResp.CurrentStatus) + + // Test setting changes are reflected in the response. + conn.Exec(t, "SET CLUSTER SETTING obs.tablemetadata.data_valid_duration = '10m'") + conn.Exec(t, "SET CLUSTER SETTING obs.tablemetadata.automatic_updates.enabled = true") + mdResp = makeApiRequest[tmUpdateJobStatusResponse](t, userClient, ts.AdminURL().WithPath(uri).String(), http.MethodGet) + require.Equal(t, true, mdResp.AutomaticUpdatesEnabled) + require.Equal(t, 10*time.Minute, mdResp.DataValidDuration) }) } diff --git a/pkg/sql/tablemetadatacache/cluster_settings.go b/pkg/sql/tablemetadatacache/cluster_settings.go index 97dfe723bc89..0b33108a18f4 100644 --- a/pkg/sql/tablemetadatacache/cluster_settings.go +++ b/pkg/sql/tablemetadatacache/cluster_settings.go @@ -19,7 +19,7 @@ import ( const defaultDataValidDuration = time.Minute * 20 -var tableMetadataCacheAutoUpdatesEnabled = settings.RegisterBoolSetting( +var AutomaticCacheUpdatesEnabledSetting = settings.RegisterBoolSetting( settings.ApplicationLevel, "obs.tablemetadata.automatic_updates.enabled", "enables automatic updates of the table metadata cache system.table_metadata", diff --git a/pkg/sql/tablemetadatacache/update_table_metadata_cache_job.go b/pkg/sql/tablemetadatacache/update_table_metadata_cache_job.go index 824a4b3ac718..04f050b4b4fb 100644 --- a/pkg/sql/tablemetadatacache/update_table_metadata_cache_job.go +++ b/pkg/sql/tablemetadatacache/update_table_metadata_cache_job.go @@ -66,7 +66,7 @@ func (j *tableMetadataUpdateJobResumer) Resume(ctx context.Context, execCtxI int settings := execCtx.ExecCfg().Settings // Register callbacks to signal the job to reset the timer when timer related settings change. scheduleSettingsCh := make(chan struct{}) - tableMetadataCacheAutoUpdatesEnabled.SetOnChange(&settings.SV, func(_ context.Context) { + AutomaticCacheUpdatesEnabledSetting.SetOnChange(&settings.SV, func(_ context.Context) { select { case scheduleSettingsCh <- struct{}{}: default: @@ -84,7 +84,7 @@ func (j *tableMetadataUpdateJobResumer) Resume(ctx context.Context, execCtxI int onJobReady() } for { - if tableMetadataCacheAutoUpdatesEnabled.Get(&settings.SV) { + if AutomaticCacheUpdatesEnabledSetting.Get(&settings.SV) { timer.Reset(DataValidDurationSetting.Get(&settings.SV)) } select {