Skip to content

Commit

Permalink
resource_group_client: add defined errors (#6033)
Browse files Browse the repository at this point in the history
ref #5851

Signed-off-by: Cabinfever_B <[email protected]>

Co-authored-by: Ti Chi Robot <[email protected]>
  • Loading branch information
CabinfeverB and ti-chi-bot authored Feb 22, 2023
1 parent 723fd74 commit 94d05f0
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 11 deletions.
12 changes: 11 additions & 1 deletion client/errs/errno.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

package errs

import "github.com/pingcap/errors"
import (
"github.com/pingcap/errors"
)

const (
// NotLeaderErr indicates the the non-leader member received the requests which should be received by leader.
Expand Down Expand Up @@ -61,3 +63,11 @@ var (
ErrCryptoX509KeyPair = errors.Normalize("x509 keypair error", errors.RFCCodeText("PD:crypto:ErrCryptoX509KeyPair"))
ErrCryptoAppendCertsFromPEM = errors.Normalize("cert pool append certs error", errors.RFCCodeText("PD:crypto:ErrCryptoAppendCertsFromPEM"))
)

// resource group errors
var (
ErrClientGetResourceGroup = errors.Normalize("get resource group failed, %v", errors.RFCCodeText("PD:client:ErrClientGetResourceGroup"))
ErrClientListResourceGroup = errors.Normalize("get all resource group failed, %v", errors.RFCCodeText("PD:client:ErrClientListResourceGroup"))
ErrClientResourceGroupConfigUnavailable = errors.Normalize("resource group config is unavailable, %v", errors.RFCCodeText("PD:client:ErrClientResourceGroupConfigUnavailable"))
ErrClientResourceGroupThrottled = errors.Normalize("exceeded resource group quota limitation", errors.RFCCodeText("PD:client:ErrClientResourceGroupThrottled"))
)
9 changes: 5 additions & 4 deletions client/resource_group/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/pingcap/errors"
rmpb "github.com/pingcap/kvproto/pkg/resource_manager"
"github.com/pingcap/log"
"github.com/tikv/pd/client/errs"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -200,7 +201,7 @@ func (c *ResourceGroupsController) tryGetResourceGroup(ctx context.Context, name
func (c *ResourceGroupsController) cleanUpResourceGroup(ctx context.Context) error {
groups, err := c.provider.ListResourceGroups(ctx)
if err != nil {
return err
return errs.ErrClientListResourceGroup.FastGenByArgs(err.Error())
}
latestGroups := make(map[string]struct{})
for _, group := range groups {
Expand Down Expand Up @@ -317,7 +318,7 @@ func (c *ResourceGroupsController) OnRequestWait(
) (err error) {
gc, err := c.tryGetResourceGroup(ctx, resourceGroupName)
if err != nil {
return errors.Errorf("failed to get the resource group %s", resourceGroupName)
return err
}
return gc.onRequestWait(ctx, info)
}
Expand Down Expand Up @@ -409,10 +410,10 @@ func newGroupCostController(
switch group.Mode {
case rmpb.GroupMode_RUMode:
if group.RUSettings.RU == nil || group.RUSettings.RU.Settings == nil {
return nil, errors.Errorf("the resource group is not configured")
return nil, errs.ErrClientResourceGroupConfigUnavailable.FastGenByArgs("not configured")
}
default:
return nil, errors.New("not supports the resource type")
return nil, errs.ErrClientResourceGroupConfigUnavailable.FastGenByArgs("not supports the resource type")
}

gc := &groupCostController{
Expand Down
3 changes: 2 additions & 1 deletion client/resource_group/controller/limiter.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"time"

"github.com/pingcap/log"
"github.com/tikv/pd/client/errs"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -408,7 +409,7 @@ func WaitReservations(ctx context.Context, now time.Time, reservations []*Reserv
for _, res := range reservations {
if !res.ok {
cancel()
return fmt.Errorf("[resource group controller] limiter has no enough token or needs wait too long")
return errs.ErrClientResourceGroupThrottled
}
delay := res.DelayFrom(now)
if delay > longestDelayDuration {
Expand Down
9 changes: 5 additions & 4 deletions client/resource_manager_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/pingcap/kvproto/pkg/pdpb"
rmpb "github.com/pingcap/kvproto/pkg/resource_manager"
"github.com/pingcap/log"
"github.com/tikv/pd/client/errs"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -69,11 +70,11 @@ func (c *client) ListResourceGroups(ctx context.Context) ([]*rmpb.ResourceGroup,
resp, err := c.resourceManagerClient().ListResourceGroups(ctx, req)
if err != nil {
c.gRPCErrorHandler(err)
return nil, err
return nil, errs.ErrClientListResourceGroup.FastGenByArgs(err.Error())
}
resErr := resp.GetError()
if resErr != nil {
return nil, errors.Errorf("[resource_manager] %s", resErr.Message)
return nil, errs.ErrClientListResourceGroup.FastGenByArgs(resErr.Message)
}
return resp.GetGroups(), nil
}
Expand All @@ -85,11 +86,11 @@ func (c *client) GetResourceGroup(ctx context.Context, resourceGroupName string)
resp, err := c.resourceManagerClient().GetResourceGroup(ctx, req)
if err != nil {
c.gRPCErrorHandler(err)
return nil, err
return nil, errs.ErrClientGetResourceGroup.FastGenByArgs(err.Error())
}
resErr := resp.GetError()
if resErr != nil {
return nil, errors.Errorf("[resource_manager] %s", resErr.Message)
return nil, errs.ErrClientGetResourceGroup.FastGenByArgs(resErr.Message)
}
return resp.GetGroup(), nil
}
Expand Down
2 changes: 1 addition & 1 deletion tests/mcs/resource_manager/resource_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ func (suite *resourceManagerClientTestSuite) TestBasicResourceGroupCURD() {
re.NoError(err)
re.Contains(dresp, "Success!")
_, err = cli.GetResourceGroup(suite.ctx, g.Name)
re.EqualError(err, "rpc error: code = Unknown desc = resource group not found")
re.EqualError(err, "[PD:client:ErrClientGetResourceGroup]get resource group failed, rpc error: code = Unknown desc = resource group not found")
}

// to test the deletion of persistence
Expand Down

0 comments on commit 94d05f0

Please sign in to comment.