-
Notifications
You must be signed in to change notification settings - Fork 728
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: husharp <jinhao.hu@pingcap.com>
- Loading branch information
Showing
12 changed files
with
443 additions
and
190 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// 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 pd | ||
|
||
import ( | ||
"context" | ||
"github.com/gogo/protobuf/proto" | ||
"github.com/pingcap/kvproto/pkg/pdpb" | ||
rmpb "github.com/pingcap/kvproto/pkg/resource_manager" | ||
"github.com/pingcap/log" | ||
"go.uber.org/zap" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
// TODO: delete it when update proto | ||
const GroupSettingsPathPrefix = "/settings" | ||
|
||
// ResourceManagerClient manages resource groups. | ||
type ResourceManagerClient interface { | ||
// WatchResourceGroup watches resource groups changes. | ||
WatchResourceGroup(ctx context.Context, revision int64) (chan []*rmpb.ResourceGroup, error) | ||
} | ||
|
||
// ResourceManagerClient returns the ResourceManagerClient from current PD leader. | ||
func (c *client) resourceManagerClient() rmpb.ResourceManagerClient { | ||
if cc, ok := c.clientConns.Load(c.GetLeaderAddr()); ok { | ||
return rmpb.NewResourceManagerClient(cc.(*grpc.ClientConn)) | ||
} | ||
return nil | ||
} | ||
|
||
// WatchResourceGroup watches resource groups changes. | ||
// It returns a stream of slices of resource groups. | ||
// The first message in stream contains all current resource groups, | ||
// all subsequent messages contains new put events for all resource groups. | ||
func (c *client) WatchResourceGroup(ctx context.Context, revision int64) (chan []*rmpb.ResourceGroup, error) { | ||
configChan, err := c.WatchGlobalConfig(ctx, GroupSettingsPathPrefix, revision) | ||
resourceGroupWatcherChan := make(chan []*rmpb.ResourceGroup, 16) | ||
go func() { | ||
defer func() { | ||
if r := recover(); r != nil { | ||
log.Error("[pd] panic in ResourceManagerClient `WatchResourceGroups`", zap.Any("error", r)) | ||
return | ||
} | ||
}() | ||
for { | ||
select { | ||
case <-ctx.Done(): | ||
close(resourceGroupWatcherChan) | ||
return | ||
case res, ok := <-configChan: | ||
if !ok { | ||
close(resourceGroupWatcherChan) | ||
return | ||
} | ||
groups := make([]*rmpb.ResourceGroup, 0, len(res)) | ||
for _, item := range res { | ||
switch item.ItemKind { | ||
case pdpb.ItemKind_PUT: | ||
group := &rmpb.ResourceGroup{} | ||
if err := proto.Unmarshal([]byte(item.Value), group); err != nil { | ||
return | ||
} | ||
groups = append(groups, group) | ||
case pdpb.ItemKind_DELETE: | ||
|
||
} | ||
|
||
} | ||
resourceGroupWatcherChan <- groups | ||
} | ||
} | ||
}() | ||
return resourceGroupWatcherChan, err | ||
} |
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.