Skip to content
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

fix: undelete all key bug #2970

Merged
merged 1 commit into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion bcs-services/bcs-bscp/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ bcs-bscp
bin
.codecc
.idea
.vscode
.vscode
__debug_bin
5 changes: 1 addition & 4 deletions bcs-services/bcs-bscp/cmd/data-service/service/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,10 +465,7 @@ func (s *Service) UnDeleteKv(ctx context.Context, req *pbds.UnDeleteKvReq) (*pbb
}
}

kvState = []string{
string(table.KvStateDelete),
}
if err = s.dao.Kv().UpdateSelectedKVStates(kt, tx, req.Attachment.BizId, req.Attachment.AppId, kvState,
if err = s.dao.Kv().UpdateStateWithTx(kt, tx, req.Attachment.BizId, req.Attachment.AppId, req.Spec.Key,
table.KvStateUnchange); err != nil {
if rErr := tx.Rollback(); rErr != nil {
logs.Errorf("transaction rollback failed, err: %v, rid: %s", rErr, kt.Rid)
Expand Down
26 changes: 26 additions & 0 deletions bcs-services/bcs-bscp/pkg/dal/dao/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ type Kv interface {
// UpdateSelectedKVStates updates the states of selected kv pairs using a transaction
UpdateSelectedKVStates(kit *kit.Kit, tx *gen.QueryTx, bizID, appID uint32, targetKVStates []string,
newKVStates table.KvState) error
// UpdateStateWithTx updates the state of a kv pair with transaction
UpdateStateWithTx(kit *kit.Kit, tx *gen.QueryTx, bizID, appID uint32, key string, state table.KvState) error
// DeleteByStateWithTx deletes kv pairs with a specific state using a transaction
DeleteByStateWithTx(kit *kit.Kit, tx *gen.QueryTx, kv *table.Kv) error
}
Expand Down Expand Up @@ -415,6 +417,30 @@ func (dao *kvDao) UpdateSelectedKVStates(kit *kit.Kit, tx *gen.QueryTx, bizID, a
return nil
}

// UpdateStateWithTx updates the state of a kv pair with transaction
func (dao *kvDao) UpdateStateWithTx(kit *kit.Kit, tx *gen.QueryTx, bizID, appID uint32, key string,
state table.KvState) error {
if bizID <= 0 {
return errors.New("biz id should be set")
}

if appID <= 0 {
return errors.New("app id should be set")
}

if key == "" {
return errors.New("key cannot be empty")
}

if state.String() == "" {
return errors.New("state cannot be empty")
}

m := tx.Kv
_, err := m.WithContext(kit.Ctx).Where(m.BizID.Eq(bizID), m.AppID.Eq(appID), m.Key.Eq(key)).Update(m.KvState, state)
return err
}

// ListAllByAppID list all Kv by appID
func (dao *kvDao) ListAllByAppID(kit *kit.Kit, appID uint32, bizID uint32, kvState []string) ([]*table.Kv, error) {
if appID == 0 {
Expand Down
15 changes: 15 additions & 0 deletions bcs-services/bcs-bscp/pkg/dal/table/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,18 @@ const (
// KvStateUnchange 不变
KvStateUnchange KvState = "UNCHANGE"
)

// String get string value of KvState
func (k KvState) String() string {
return string(k)
}

// Validate validate kv state is valid or not.
func (k KvState) Validate() error {
switch k {
case KvStateAdd, KvStateDelete, KvStateRevise, KvStateUnchange:
return nil
default:
return errors.New("invalid kv state")
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading