-
Notifications
You must be signed in to change notification settings - Fork 596
add config command #181
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
Open
cheny-alf
wants to merge
21
commits into
HDT3213:master
Choose a base branch
from
cheny-alf:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
add config command #181
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
a899878
add config command
1c2c301
Merge branch 'HDT3213:master' into master
cheny-alf 2d113ee
fix test name
1faf55d
Merge remote-tracking branch 'origin/master'
b11360b
append test case
63d0c1d
append test case
058c953
append test case
98c9dc6
append test case
21034af
fix propertiesMap
bb25b07
delete break
863a548
cluster add config cmd
957bdde
update test case
007aa61
rebuild
f7cb06d
update
fc38be2
update
dc7ab61
Merge branch 'HDT3213:master' into master
cheny-alf 1bc1519
update testIsImmutableConfig
6779f06
Merge remote-tracking branch 'origin/master'
fdd4efc
Merge remote-tracking branch 'origin/master'
4860454
update
6e66d9d
update
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,180 @@ | ||
| package database | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "github.com/hdt3213/godis/config" | ||
| "github.com/hdt3213/godis/interface/redis" | ||
| "github.com/hdt3213/godis/lib/wildcard" | ||
| "github.com/hdt3213/godis/redis/protocol" | ||
| "reflect" | ||
| "strconv" | ||
| "strings" | ||
| "sync" | ||
| ) | ||
|
|
||
| func init() { | ||
|
|
||
| } | ||
|
|
||
| type configCmd struct { | ||
| name string | ||
| operation string | ||
| executor ExecFunc | ||
| } | ||
|
|
||
| var configCmdTable = make(map[string]*configCmd) | ||
|
|
||
| func ExecConfigCommand(args [][]byte) redis.Reply { | ||
| return execSubCommand(args) | ||
| } | ||
|
|
||
| func execSubCommand(args [][]byte) redis.Reply { | ||
| if len(args) == 0 { | ||
| return getAllGodisCommandReply() | ||
| } | ||
| subCommand := strings.ToUpper(string(args[1])) | ||
| switch subCommand { | ||
| case "GET": | ||
| return getConfig(args[2:]) | ||
| case "SET": | ||
| mu := &sync.Mutex{} | ||
| return setConfig(args[2:], mu) | ||
| case "RESETSTAT": | ||
| // todo add resetstat | ||
| return protocol.MakeErrReply(fmt.Sprintf("Unknown subcommand or wrong number of arguments for '%s'", subCommand)) | ||
| case "REWRITE": | ||
| // todo add rewrite | ||
| return protocol.MakeErrReply(fmt.Sprintf("Unknown subcommand or wrong number of arguments for '%s'", subCommand)) | ||
| default: | ||
| return protocol.MakeErrReply(fmt.Sprintf("Unknown subcommand or wrong number of arguments for '%s'", subCommand)) | ||
| } | ||
| } | ||
| func getConfig(args [][]byte) redis.Reply { | ||
| result := make([][]byte, 0) | ||
| propertiesMap := getPropertiesMap() | ||
| for _, arg := range args { | ||
| param := string(arg) | ||
| for key, value := range propertiesMap { | ||
| pattern, err := wildcard.CompilePattern(param) | ||
| if err != nil { | ||
| return nil | ||
| } | ||
| isMatch := pattern.IsMatch(key) | ||
| if isMatch { | ||
| result = append(result, []byte(key), []byte(value)) | ||
| } | ||
| } | ||
| } | ||
| return protocol.MakeMultiBulkReply(result) | ||
| } | ||
|
|
||
| func getPropertiesMap() map[string]string { | ||
| PropertiesMap := map[string]string{} | ||
| t := reflect.TypeOf(config.Properties) | ||
| v := reflect.ValueOf(config.Properties) | ||
| n := t.Elem().NumField() | ||
| for i := 0; i < n; i++ { | ||
| field := t.Elem().Field(i) | ||
| fieldVal := v.Elem().Field(i) | ||
| key, ok := field.Tag.Lookup("cfg") | ||
| if !ok || strings.TrimLeft(key, " ") == "" { | ||
| key = field.Name | ||
| } | ||
| var value string | ||
| switch fieldVal.Type().Kind() { | ||
| case reflect.String: | ||
| value = fieldVal.String() | ||
| case reflect.Int: | ||
| value = strconv.Itoa(int(fieldVal.Int())) | ||
| case reflect.Bool: | ||
| if fieldVal.Bool() { | ||
| value = "yes" | ||
| } else { | ||
| value = "no" | ||
| } | ||
| } | ||
| PropertiesMap[key] = value | ||
| } | ||
| return PropertiesMap | ||
| } | ||
|
|
||
| func setConfig(args [][]byte, mu *sync.Mutex) redis.Reply { | ||
| mu.Lock() | ||
| defer mu.Unlock() | ||
| if len(args)%2 != 0 { | ||
| return protocol.MakeErrReply("ERR wrong number of arguments for 'config|set' command") | ||
| } | ||
| duplicateDetectMap := make(map[string]string) | ||
| for i := 0; i < len(args); i += 2 { | ||
| parameter := string(args[i]) | ||
| value := string(args[i+1]) | ||
| if _, ok := duplicateDetectMap[parameter]; ok { | ||
| errStr := fmt.Sprintf("ERR CONFIG SET failed (possibly related to argument '%s') - duplicate parameter", parameter) | ||
| return protocol.MakeErrReply(errStr) | ||
| } | ||
| duplicateDetectMap[parameter] = value | ||
| } | ||
| properties := config.CopyProperties() | ||
| propertyMap := getPropertyMap(properties) | ||
| for parameter, value := range duplicateDetectMap { | ||
| _, ok := propertyMap[parameter] | ||
| if !ok { | ||
| return protocol.MakeErrReply(fmt.Sprintf("ERR Unknown option or number of arguments for CONFIG SET - '%s'", parameter)) | ||
| } | ||
| isMutable := config.IsMutableConfig(parameter) | ||
| if !isMutable { | ||
| return protocol.MakeErrReply(fmt.Sprintf("ERR CONFIG SET failed (possibly related to argument '%s') - can't set immutable config", parameter)) | ||
| } | ||
| err := setVal(propertyMap[parameter], parameter, value) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
| config.Properties = properties | ||
| return &protocol.OkReply{} | ||
| } | ||
|
|
||
| func getPropertyMap(properties *config.ServerProperties) map[string]*reflect.Value { | ||
| propertiesMap := make(map[string]*reflect.Value) | ||
| t := reflect.TypeOf(properties) | ||
| v := reflect.ValueOf(properties) | ||
| n := t.Elem().NumField() | ||
| for i := 0; i < n; i++ { | ||
cheny-alf marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| field := t.Elem().Field(i) | ||
| fieldVal := v.Elem().Field(i) | ||
| key, ok := field.Tag.Lookup("cfg") | ||
| if !ok { | ||
| continue | ||
| } | ||
| propertiesMap[key] = &fieldVal | ||
| } | ||
| return propertiesMap | ||
| } | ||
| func setVal(val *reflect.Value, parameter, expectVal string) redis.Reply { | ||
| switch val.Type().Kind() { | ||
| case reflect.String: | ||
| val.SetString(expectVal) | ||
| case reflect.Int: | ||
| intValue, err := strconv.ParseInt(expectVal, 10, 64) | ||
| if err != nil { | ||
| errStr := fmt.Sprintf("ERR CONFIG SET failed (possibly related to argument '%s') - argument couldn't be parsed into an integer", parameter) | ||
| return protocol.MakeErrReply(errStr) | ||
| } | ||
| val.SetInt(intValue) | ||
| case reflect.Bool: | ||
| if "yes" == expectVal { | ||
| val.SetBool(true) | ||
| } else if "no" == expectVal { | ||
| val.SetBool(false) | ||
| } else { | ||
| errStr := fmt.Sprintf("ERR CONFIG SET failed (possibly related to argument '%s') - argument couldn't be parsed into a bool", parameter) | ||
| return protocol.MakeErrReply(errStr) | ||
| } | ||
| case reflect.Slice: | ||
| if val.Elem().Kind() == reflect.String { | ||
| slice := strings.Split(expectVal, ",") | ||
| val.Set(reflect.ValueOf(slice)) | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
This file contains hidden or 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,50 @@ | ||
| package database | ||
|
|
||
| import ( | ||
| "github.com/hdt3213/godis/lib/utils" | ||
| "github.com/hdt3213/godis/redis/protocol/asserts" | ||
| _ "github.com/hdt3213/godis/redis/protocol/asserts" | ||
| "testing" | ||
| ) | ||
|
|
||
| //func init() { | ||
| // config.Properties = &config.ServerProperties{ | ||
| // AppendOnly: true, | ||
| // AppendFilename: "appendonly.aof", | ||
| // AofUseRdbPreamble: false, | ||
| // AppendFsync: aof.FsyncEverySec, | ||
| // MaxClients: 128, | ||
| // } | ||
| //} | ||
|
|
||
| func TestConfigGet(t *testing.T) { | ||
| testDB.Flush() | ||
| testMDB := NewStandaloneServer() | ||
|
|
||
| result := testMDB.Exec(nil, utils.ToCmdLine("config", "get", "maxclients")) | ||
| asserts.AssertMultiBulkReply(t, result, []string{"maxclients", "0"}) | ||
| result = testMDB.Exec(nil, utils.ToCmdLine("config", "get", "maxcli*")) | ||
| asserts.AssertMultiBulkReply(t, result, []string{"maxclients", "0"}) | ||
| result = testMDB.Exec(nil, utils.ToCmdLine("config", "get", "none")) | ||
| asserts.AssertMultiBulkReply(t, result, []string{}) | ||
| result = testMDB.Exec(nil, utils.ToCmdLine("config", "get", "maxclients", "appendonly")) | ||
| asserts.AssertMultiBulkReply(t, result, []string{"maxclients", "0", "appendonly", "yes"}) | ||
| } | ||
| func TestConfigSet(t *testing.T) { | ||
| testDB.Flush() | ||
| testMDB := NewStandaloneServer() | ||
| result := testMDB.Exec(nil, utils.ToCmdLine("config", "set", "appendfsync", "no")) | ||
| asserts.AssertOkReply(t, result) | ||
| result = testMDB.Exec(nil, utils.ToCmdLine("config", "set", "appendfsync", "no", "maxclients", "110")) | ||
| asserts.AssertOkReply(t, result) | ||
| result = testMDB.Exec(nil, utils.ToCmdLine("config", "set", "appendonly", "no")) | ||
| asserts.AssertOkReply(t, result) | ||
| result = testMDB.Exec(nil, utils.ToCmdLine("config", "set", "appendfsync", "no", "maxclients", "panic")) | ||
| asserts.AssertErrReply(t, result, "ERR CONFIG SET failed (possibly related to argument 'maxclients') - argument couldn't be parsed into an integer") | ||
| result = testMDB.Exec(nil, utils.ToCmdLine("config", "set", "appendfsync", "no", "errorConfig", "110")) | ||
| asserts.AssertErrReply(t, result, "ERR Unknown option or number of arguments for CONFIG SET - 'errorConfig'") | ||
| result = testMDB.Exec(nil, utils.ToCmdLine("config", "set", "appendfsync", "no", "maxclients")) | ||
| asserts.AssertErrReply(t, result, "ERR wrong number of arguments for 'config|set' command") | ||
| result = testMDB.Exec(nil, utils.ToCmdLine("config", "set", "appendfsync", "no", "appendfsync", "yes")) | ||
| asserts.AssertErrReply(t, result, "ERR CONFIG SET failed (possibly related to argument 'appendfsync') - duplicate parameter") | ||
| } |
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.