Skip to content

Commit

Permalink
Merge #131385
Browse files Browse the repository at this point in the history
131385: server: add tablemetadata job cluster settings to job status api r=xinhaoz a=xinhaoz

Add the cluster setting values for the update table metadata job to the job status api.

Epic: CRDB-37558
Release note: None

Co-authored-by: Xin Hao Zhang <[email protected]>
  • Loading branch information
craig[bot] and xinhaoz committed Sep 26, 2024
2 parents 94db534 + 87f05c4 commit 7e0f3ae
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
7 changes: 7 additions & 0 deletions pkg/server/api_v2_databases_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
21 changes: 13 additions & 8 deletions pkg/server/api_v2_databases_metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
})
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/tablemetadatacache/cluster_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions pkg/sql/tablemetadatacache/update_table_metadata_cache_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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 {
Expand Down

0 comments on commit 7e0f3ae

Please sign in to comment.