diff --git a/go/mysql/fakesqldb/server.go b/go/mysql/fakesqldb/server.go index e2a275940d5..cd1080c862c 100644 --- a/go/mysql/fakesqldb/server.go +++ b/go/mysql/fakesqldb/server.go @@ -592,6 +592,13 @@ func (db *DB) AddQueryPattern(queryPattern string, expectedResult *sqltypes.Resu db.patternData[queryPattern] = exprResult{queryPattern: queryPattern, expr: expr, result: &result} } +// RemoveQueryPattern removes a query pattern that was previously added. +func (db *DB) RemoveQueryPattern(queryPattern string) { + db.mu.Lock() + defer db.mu.Unlock() + delete(db.patternData, queryPattern) +} + // RejectQueryPattern allows a query pattern to be rejected with an error func (db *DB) RejectQueryPattern(queryPattern, error string) { expr := regexp.MustCompile("(?is)^" + queryPattern + "$") diff --git a/go/vt/logutil/throttled.go b/go/vt/logutil/throttled.go index 4ee11912e71..fa63be328bc 100644 --- a/go/vt/logutil/throttled.go +++ b/go/vt/logutil/throttled.go @@ -54,6 +54,11 @@ var ( errorDepth = log.ErrorDepth ) +// GetLastLogTime gets the last log time for the throttled logger. +func (tl *ThrottledLogger) GetLastLogTime() time.Time { + return tl.lastlogTime +} + func (tl *ThrottledLogger) log(logF logFunc, format string, v ...any) { now := time.Now() diff --git a/go/vt/vttablet/tabletserver/schema/engine.go b/go/vt/vttablet/tabletserver/schema/engine.go index 5babed271ca..aadba5739c8 100644 --- a/go/vt/vttablet/tabletserver/schema/engine.go +++ b/go/vt/vttablet/tabletserver/schema/engine.go @@ -40,6 +40,7 @@ import ( "vitess.io/vitess/go/vt/dbconfigs" "vitess.io/vitess/go/vt/dbconnpool" "vitess.io/vitess/go/vt/log" + "vitess.io/vitess/go/vt/logutil" "vitess.io/vitess/go/vt/mysqlctl/tmutils" "vitess.io/vitess/go/vt/schema" "vitess.io/vitess/go/vt/servenv" @@ -85,9 +86,10 @@ type Engine struct { historian *historian - conns *connpool.Pool - ticks *timer.Timer - reloadTimeout time.Duration + conns *connpool.Pool + ticks *timer.Timer + reloadTimeout time.Duration + throttledLogger *logutil.ThrottledLogger // dbCreationFailed is for preventing log spam. dbCreationFailed bool @@ -109,7 +111,8 @@ func NewEngine(env tabletenv.Env) *Engine { Size: 3, IdleTimeout: env.Config().OltpReadPool.IdleTimeout, }), - ticks: timer.NewTimer(reloadTime), + ticks: timer.NewTimer(reloadTime), + throttledLogger: logutil.NewThrottledLogger("schema-tracker", 1*time.Minute), } se.schemaCopy = env.Config().SignalWhenSchemaChange _ = env.Exporter().NewGaugeDurationFunc("SchemaReloadTime", "vttablet keeps table schemas in its own memory and periodically refreshes it from MySQL. This config controls the reload time.", se.ticks.Interval) @@ -448,7 +451,7 @@ func (se *Engine) reload(ctx context.Context, includeStats bool) error { udfsChanged, err := getChangedUserDefinedFunctions(ctx, conn.Conn, shouldUseDatabase) if err != nil { - return err + se.throttledLogger.Errorf("error in getting changed UDFs: %v", err) } rec := concurrency.AllErrorRecorder{} diff --git a/go/vt/vttablet/tabletserver/schema/engine_test.go b/go/vt/vttablet/tabletserver/schema/engine_test.go index e194fa8daa6..1fd6098b384 100644 --- a/go/vt/vttablet/tabletserver/schema/engine_test.go +++ b/go/vt/vttablet/tabletserver/schema/engine_test.go @@ -1331,7 +1331,8 @@ func TestEngineReload(t *testing.T) { } // adding query pattern for udfs - db.AddQueryPattern("SELECT name.*", &sqltypes.Result{}) + udfQueryPattern := "SELECT name.*" + db.AddQueryPattern(udfQueryPattern, &sqltypes.Result{}) // Verify the list of created, altered and dropped tables seen. se.RegisterNotifier("test", func(full map[string]*Table, created, altered, dropped []*Table, _ bool) { @@ -1344,6 +1345,16 @@ func TestEngineReload(t *testing.T) { err = se.reload(context.Background(), false) require.NoError(t, err) require.NoError(t, db.LastError()) + require.Zero(t, se.throttledLogger.GetLastLogTime()) + + // Now if we remove the query pattern for udfs, schema engine shouldn't fail. + // Instead we should see a log message with the error. + db.RemoveQueryPattern(udfQueryPattern) + se.UnregisterNotifier("test") + err = se.reload(context.Background(), false) + require.NoError(t, err) + // Check for the udf error being logged. The last log time should be less than a second. + require.Less(t, time.Since(se.throttledLogger.GetLastLogTime()), 1*time.Second) } // TestEngineReload tests the vreplication specific GetTableForPos function to ensure