-
Notifications
You must be signed in to change notification settings - Fork 720
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
*: support keyspace group RESTful API #6229
Changes from all commits
6297b0c
dd10184
195a140
0fd5de6
67afab6
a1f7126
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// Copyright 2023 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 endpoint | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
|
||
"github.com/tikv/pd/pkg/storage/kv" | ||
"go.etcd.io/etcd/clientv3" | ||
) | ||
|
||
// KeyspaceGroup is the keyspace group. | ||
type KeyspaceGroup struct { | ||
ID uint32 `json:"id"` | ||
UserKind string `json:"user-kind"` | ||
// TODO: add `Members` field | ||
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. maybe we should call it 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. Both are ok to me. 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. up to you then 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. keep members then. I'm using members in my change. 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. ok |
||
} | ||
|
||
// KeyspaceGroupStorage is the interface for keyspace group storage. | ||
type KeyspaceGroupStorage interface { | ||
LoadKeyspaceGroups(startID uint32, limit int) ([]*KeyspaceGroup, 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. Compared to the function "LoadKeyspaceGroup(txn kv.Txn, id uint32)", if we load multiple groups here, do we need to use transaction to provide snapshot isolation consistency level? 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. Good catch! |
||
LoadKeyspaceGroup(txn kv.Txn, id uint32) (*KeyspaceGroup, 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. This is to load from single key-value pair, correct? If yes, do we still need transaction? 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. As long as we use the storage interface, it will create a txn. I think it's ok. 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. ok. |
||
SaveKeyspaceGroup(txn kv.Txn, kg *KeyspaceGroup) 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. ditto - SaveKeyspaceGroup and DeleteKeyspaceGroup 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. ditto 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. ditto |
||
DeleteKeyspaceGroup(txn kv.Txn, id uint32) error | ||
// TODO: add more interfaces. | ||
RunInTxn(ctx context.Context, f func(txn kv.Txn) error) error | ||
} | ||
|
||
var _ KeyspaceGroupStorage = (*StorageEndpoint)(nil) | ||
|
||
// LoadKeyspaceGroup loads the keyspace group by id. | ||
func (se *StorageEndpoint) LoadKeyspaceGroup(txn kv.Txn, id uint32) (*KeyspaceGroup, error) { | ||
value, err := txn.Load(KeyspaceGroupIDPath(id)) | ||
if err != nil || value == "" { | ||
return nil, err | ||
} | ||
kg := &KeyspaceGroup{} | ||
if err := json.Unmarshal([]byte(value), kg); err != nil { | ||
return nil, err | ||
} | ||
return kg, nil | ||
} | ||
|
||
// SaveKeyspaceGroup saves the keyspace group. | ||
func (se *StorageEndpoint) SaveKeyspaceGroup(txn kv.Txn, kg *KeyspaceGroup) error { | ||
key := KeyspaceGroupIDPath(kg.ID) | ||
value, err := json.Marshal(kg) | ||
if err != nil { | ||
return err | ||
} | ||
return txn.Save(key, string(value)) | ||
} | ||
|
||
// DeleteKeyspaceGroup deletes the keyspace group. | ||
func (se *StorageEndpoint) DeleteKeyspaceGroup(txn kv.Txn, id uint32) error { | ||
return txn.Remove(KeyspaceGroupIDPath(id)) | ||
} | ||
|
||
// LoadKeyspaceGroups loads keyspace groups from the start ID with limit. | ||
// If limit is 0, it will load all keyspace groups from the start ID. | ||
func (se *StorageEndpoint) LoadKeyspaceGroups(startID uint32, limit int) ([]*KeyspaceGroup, error) { | ||
prefix := KeyspaceGroupIDPath(startID) | ||
prefixEnd := clientv3.GetPrefixRangeEnd(KeyspaceGroupIDPrefix()) | ||
keys, values, err := se.LoadRange(prefix, prefixEnd, limit) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if len(keys) == 0 { | ||
return []*KeyspaceGroup{}, nil | ||
} | ||
kgs := make([]*KeyspaceGroup, 0, len(keys)) | ||
for _, value := range values { | ||
kg := &KeyspaceGroup{} | ||
if err = json.Unmarshal([]byte(value), kg); err != nil { | ||
return nil, err | ||
} | ||
kgs = append(kgs, kg) | ||
} | ||
return kgs, nil | ||
} |
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.
Shall we change to utils.TSOServiceName + "keyspace_groups"
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, I think there is no need and it's simpler. Once some other services need the group concept, we can refactor 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.
My current change is using it. Leave this refactor to me then. Your pr goest first.
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.
get it