forked from tikv/pd
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
api: add Rate-limit config update API (tikv#4843)
ref tikv#4666, ref tikv#4839 add Rate-limit config update API Signed-off-by: Cabinfever_B <cabinfeveroier@gmail.com> Co-authored-by: Ryan Leung <rleungx@gmail.com> Co-authored-by: Ti Chi Robot <ti-community-prow-bot@tidb.io>
- Loading branch information
1 parent
382a912
commit a66b06f
Showing
7 changed files
with
420 additions
and
61 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright 2022 TiKV Project Authors. | ||
// | ||
// 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, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package jsonutil | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
|
||
"github.com/tikv/pd/pkg/reflectutil" | ||
) | ||
|
||
// AddKeyValue is used to add a key value pair into `old` | ||
func AddKeyValue(old interface{}, key string, value interface{}) (updated bool, found bool, err error) { | ||
data, err := json.Marshal(map[string]interface{}{key: value}) | ||
if err != nil { | ||
return false, false, err | ||
} | ||
return MergeJSONObject(old, data) | ||
} | ||
|
||
// MergeJSONObject is used to merge a marshaled json object into v | ||
func MergeJSONObject(v interface{}, data []byte) (updated bool, found bool, err error) { | ||
old, _ := json.Marshal(v) | ||
if err := json.Unmarshal(data, v); err != nil { | ||
return false, false, err | ||
} | ||
new, _ := json.Marshal(v) | ||
if !bytes.Equal(old, new) { | ||
return true, true, nil | ||
} | ||
m := make(map[string]interface{}) | ||
if err := json.Unmarshal(data, &m); err != nil { | ||
return false, false, err | ||
} | ||
found = reflectutil.FindSameFieldByJSON(v, m) | ||
return false, found, nil | ||
} |
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,65 @@ | ||
// Copyright 2022 TiKV Project Authors. | ||
// | ||
// 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, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package jsonutil | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type testJSONStructLevel1 struct { | ||
Name string `json:"name"` | ||
Sub1 testJSONStructLevel2 `json:"sub1"` | ||
Sub2 testJSONStructLevel2 `json:"sub2"` | ||
} | ||
|
||
type testJSONStructLevel2 struct { | ||
SubName string `json:"sub-name"` | ||
} | ||
|
||
func TestJSONUtil(t *testing.T) { | ||
t.Parallel() | ||
re := require.New(t) | ||
father := &testJSONStructLevel1{ | ||
Name: "father", | ||
} | ||
son1 := &testJSONStructLevel2{ | ||
SubName: "son1", | ||
} | ||
update, found, err := AddKeyValue(&father, "sub1", &son1) | ||
re.NoError(err) | ||
re.True(update) | ||
re.True(found) | ||
|
||
son2 := &testJSONStructLevel2{ | ||
SubName: "son2", | ||
} | ||
|
||
update, found, err = AddKeyValue(father, "sub2", &son2) | ||
re.NoError(err) | ||
re.True(update) | ||
re.True(found) | ||
|
||
update, found, err = AddKeyValue(father, "sub3", &son2) | ||
re.NoError(err) | ||
re.False(update) | ||
re.False(found) | ||
|
||
update, found, err = AddKeyValue(father, "sub2", &son2) | ||
re.NoError(err) | ||
re.False(update) | ||
re.True(found) | ||
} |
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
Oops, something went wrong.