-
Notifications
You must be signed in to change notification settings - Fork 136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(stmt): support config maximum number of stmt kept in memory #914
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2df30cc
feat(stmt): support config maximum number of stmt kept in memory
shhdgit 3e0580e
Update pkg/apiserver/statement/config.go
shhdgit 95e561b
Update pkg/apiserver/statement/config.go
shhdgit 3b3a849
Update pkg/apiserver/statement/config.go
shhdgit b180879
Merge branch 'master' into feat/stmt-max-size
ti-chi-bot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// Copyright 2021 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package statement | ||
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"strings" | ||
|
||
"github.com/thoas/go-funk" | ||
|
||
"github.com/pingcap/tidb-dashboard/pkg/apiserver/utils" | ||
) | ||
|
||
// incoming configuration field should have the gorm tag `column` used to specify global variables | ||
// sql will be built like this, gorm:"column:some_global_var" -> @@GLOBAL.some_global_var as some_global_var | ||
func buildConfigQuerySQL(config interface{}) string { | ||
var configType reflect.Type | ||
if reflect.ValueOf(config).Kind() == reflect.Ptr { | ||
configType = reflect.TypeOf(config).Elem() | ||
} else { | ||
configType = reflect.TypeOf(config) | ||
} | ||
|
||
stmts := []string{} | ||
fNum := configType.NumField() | ||
for i := 0; i < fNum; i++ { | ||
f := configType.Field(i) | ||
gormTag, ok := f.Tag.Lookup("gorm") | ||
if !ok { | ||
continue | ||
} | ||
column := utils.GetGormColumnName(gormTag) | ||
stmts = append(stmts, fmt.Sprintf("@@GLOBAL.%s AS %s", column, column)) | ||
} | ||
|
||
// skip `SQL string formatting (gosec)` lint | ||
return "SELECT " + strings.Join(stmts, ", ") // nolints | ||
} | ||
|
||
// sql will be built like this, gorm:"column:some_global_var" -> @@GLOBAL.some_global_var = some_global_var_value | ||
func buildConfigUpdateSQL(config interface{}, extract ...string) string { | ||
var configType reflect.Type | ||
var configValue reflect.Value | ||
if reflect.ValueOf(config).Kind() == reflect.Ptr { | ||
configType = reflect.TypeOf(config).Elem() | ||
configValue = reflect.ValueOf(config).Elem() | ||
} else { | ||
configType = reflect.TypeOf(config) | ||
configValue = reflect.ValueOf(config) | ||
} | ||
|
||
stmts := []string{} | ||
fNum := configType.NumField() | ||
for i := 0; i < fNum; i++ { | ||
f := configType.Field(i) | ||
// extract fields on demand | ||
if len(extract) != 0 && !funk.ContainsString(extract, f.Name) { | ||
continue | ||
} | ||
gormTag, ok := f.Tag.Lookup("gorm") | ||
if !ok { | ||
continue | ||
} | ||
|
||
val := configValue.Field(i) | ||
column := utils.GetGormColumnName(gormTag) | ||
stmts = append(stmts, fmt.Sprintf("@@GLOBAL.%s = %v", column, val)) | ||
} | ||
|
||
// skip `SQL string formatting (gosec)` lint | ||
return "SET " + strings.Join(stmts, ", ") // nolints | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright 2021 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package statement | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/pingcap/check" | ||
) | ||
|
||
func TestT(t *testing.T) { | ||
CustomVerboseFlag = true | ||
TestingT(t) | ||
} | ||
|
||
var _ = Suite(&testConfigSuite{}) | ||
|
||
type testConfigSuite struct{} | ||
|
||
type testConfig struct { | ||
Enable bool `json:"enable" gorm:"column:tidb_enable_stmt_summary"` | ||
RefreshInterval int `json:"refresh_interval" gorm:"column:tidb_stmt_summary_refresh_interval"` | ||
} | ||
|
||
func (t *testConfigSuite) Test_buildConfigQuerySQL_struct_success(c *C) { | ||
testConfigStmt := "SELECT @@GLOBAL.tidb_enable_stmt_summary as tidb_enable_stmt_summary,@@GLOBAL.tidb_stmt_summary_refresh_interval as tidb_stmt_summary_refresh_interval" | ||
c.Assert(buildConfigQuerySQL(testConfig{}), Equals, testConfigStmt) | ||
} | ||
|
||
func (t *testConfigSuite) Test_buildConfigQuerySQL_ptr_success(c *C) { | ||
testConfigStmt := "SELECT @@GLOBAL.tidb_enable_stmt_summary as tidb_enable_stmt_summary,@@GLOBAL.tidb_stmt_summary_refresh_interval as tidb_stmt_summary_refresh_interval" | ||
c.Assert(buildConfigQuerySQL(&testConfig{}), Equals, testConfigStmt) | ||
} | ||
|
||
type testConfig2 struct { | ||
Enable bool `json:"enable" gorm:"column:tidb_enable_stmt_summary"` | ||
RefreshInterval int `json:"refresh_interval"` | ||
} | ||
|
||
func (t *testConfigSuite) Test_buildConfigQuerySQL_without_gorm_tag(c *C) { | ||
testConfigStmt := "SELECT @@GLOBAL.tidb_enable_stmt_summary as tidb_enable_stmt_summary" | ||
c.Assert(buildConfigQuerySQL(&testConfig2{}), Equals, testConfigStmt) | ||
} | ||
|
||
func (t *testConfigSuite) Test_buildConfigUpdateSQL_struct_success(c *C) { | ||
testConfigStmt := "SET @@GLOBAL.tidb_enable_stmt_summary = true,@@GLOBAL.tidb_stmt_summary_refresh_interval = 1800" | ||
c.Assert(buildConfigUpdateSQL(testConfig{Enable: true, RefreshInterval: 1800}), Equals, testConfigStmt) | ||
} | ||
|
||
func (t *testConfigSuite) Test_buildConfigUpdateSQL_ptr_success(c *C) { | ||
testConfigStmt := "SET @@GLOBAL.tidb_enable_stmt_summary = true,@@GLOBAL.tidb_stmt_summary_refresh_interval = 1800" | ||
c.Assert(buildConfigUpdateSQL(&testConfig{Enable: true, RefreshInterval: 1800}), Equals, testConfigStmt) | ||
} | ||
|
||
func (t *testConfigSuite) Test_buildConfigUpdateSQL_without_gorm_tag(c *C) { | ||
testConfigStmt := "SET @@GLOBAL.tidb_enable_stmt_summary = true" | ||
c.Assert(buildConfigUpdateSQL(&testConfig2{Enable: true, RefreshInterval: 1800}), Equals, testConfigStmt) | ||
} | ||
|
||
func (t *testConfigSuite) Test_buildConfigUpdateSQL_extract_fields(c *C) { | ||
testConfigStmt := "SET @@GLOBAL.tidb_enable_stmt_summary = true" | ||
c.Assert(buildConfigUpdateSQL(&testConfig{Enable: true, RefreshInterval: 1800}, "Enable"), Equals, testConfigStmt) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,14 +73,22 @@ func registerRouter(r *gin.RouterGroup, auth *user.AuthService, s *Service) { | |
} | ||
} | ||
|
||
type EditableConfig struct { | ||
Enable bool `json:"enable" gorm:"column:tidb_enable_stmt_summary"` | ||
RefreshInterval int `json:"refresh_interval" gorm:"column:tidb_stmt_summary_refresh_interval"` | ||
HistorySize int `json:"history_size" gorm:"column:tidb_stmt_summary_history_size"` | ||
MaxSize int `json:"max_size" gorm:"column:tidb_stmt_summary_max_stmt_count"` | ||
} | ||
|
||
// @Summary Get statement configurations | ||
// @Success 200 {object} statement.Config | ||
// @Success 200 {object} statement.EditableConfig | ||
// @Router /statements/config [get] | ||
// @Security JwtAuth | ||
// @Failure 401 {object} utils.APIError "Unauthorized failure" | ||
func (s *Service) configHandler(c *gin.Context) { | ||
db := utils.GetTiDBConnection(c) | ||
cfg, err := queryStmtConfig(db) | ||
cfg := &EditableConfig{} | ||
err := db.Raw(buildConfigQuerySQL(cfg)).Find(cfg).Error | ||
if err != nil { | ||
_ = c.Error(err) | ||
return | ||
|
@@ -89,23 +97,30 @@ func (s *Service) configHandler(c *gin.Context) { | |
} | ||
|
||
// @Summary Update statement configurations | ||
// @Param request body statement.Config true "Request body" | ||
// @Param request body statement.EditableConfig true "Request body" | ||
// @Success 204 {object} string | ||
// @Router /statements/config [post] | ||
// @Security JwtAuth | ||
// @Failure 401 {object} utils.APIError "Unauthorized failure" | ||
func (s *Service) modifyConfigHandler(c *gin.Context) { | ||
var req Config | ||
if err := c.ShouldBindJSON(&req); err != nil { | ||
var err error | ||
var config EditableConfig | ||
if err = c.ShouldBindJSON(&config); err != nil { | ||
utils.MakeInvalidRequestErrorFromError(c, err) | ||
return | ||
} | ||
db := utils.GetTiDBConnection(c) | ||
err := updateStmtConfig(db, &req) | ||
|
||
if !config.Enable { | ||
err = db.Exec(buildConfigUpdateSQL(&config, "Enable")).Error | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does this Enable do? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Extract fields from struct, only the extract fields will be left in SQL. Like this:
|
||
} else { | ||
err = db.Exec(buildConfigUpdateSQL(&config)).Error | ||
} | ||
if err != nil { | ||
_ = c.Error(err) | ||
return | ||
} | ||
|
||
c.Status(http.StatusNoContent) | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright 2021 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package utils | ||
|
||
import "strings" | ||
|
||
func GetGormColumnName(gormStr string) string { | ||
// TODO: use go-gorm/gorm/schema ParseTagSetting. Prerequisite: Upgrade to the latest version | ||
columnName := strings.Split(gormStr, ":")[1] | ||
return columnName | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems that
val
may lead to security risks as it can be anything!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right...Let me think about it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we can use parameterized queries. It will be very safe.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For now,
buildConfigQuerySQL
is only used byconfigHandler
service. And reflect.Value's type is determined. Maybe we can do this refinement after #916 ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good!