Skip to content

Commit

Permalink
get all keyspaces
Browse files Browse the repository at this point in the history
Signed-off-by: ystaticy <[email protected]>
  • Loading branch information
ystaticy committed Jul 13, 2023
1 parent 68287bb commit 69e48d4
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 92 deletions.
42 changes: 0 additions & 42 deletions client/keyspace_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,13 @@ import (
"github.com/opentracing/opentracing-go"
"github.com/pingcap/errors"
"github.com/pingcap/kvproto/pkg/keyspacepb"
"github.com/pingcap/log"
"github.com/tikv/pd/client/grpcutil"
"go.uber.org/zap"
)

// KeyspaceClient manages keyspace metadata.
type KeyspaceClient interface {
// LoadKeyspace load and return target keyspace's metadata.
LoadKeyspace(ctx context.Context, name string) (*keyspacepb.KeyspaceMeta, error)
// WatchKeyspaces watches keyspace meta changes.
WatchKeyspaces(ctx context.Context) (chan []*keyspacepb.KeyspaceMeta, error)
// UpdateKeyspaceState updates target keyspace's state.
UpdateKeyspaceState(ctx context.Context, id uint32, state keyspacepb.KeyspaceState) (*keyspacepb.KeyspaceMeta, error)
// GetAllKeyspaces get all keyspace's metadata.
Expand Down Expand Up @@ -77,44 +73,6 @@ func (c *client) LoadKeyspace(ctx context.Context, name string) (*keyspacepb.Key
return resp.Keyspace, nil
}

// WatchKeyspaces watches keyspace meta changes.
// It returns a stream of slices of keyspace metadata.
// The first message in stream contains all current keyspaceMeta,
// all subsequent messages contains new put events for all keyspaces.
func (c *client) WatchKeyspaces(ctx context.Context) (chan []*keyspacepb.KeyspaceMeta, error) {
keyspaceWatcherChan := make(chan []*keyspacepb.KeyspaceMeta)
req := &keyspacepb.WatchKeyspacesRequest{
Header: c.requestHeader(),
}
stream, err := c.keyspaceClient().WatchKeyspaces(ctx, req)
if err != nil {
close(keyspaceWatcherChan)
return nil, err
}
go func() {
defer func() {
close(keyspaceWatcherChan)
if r := recover(); r != nil {
log.Error("[pd] panic in keyspace client `WatchKeyspaces`", zap.Any("error", r))
return
}
}()
for {
select {
case <-ctx.Done():
return
default:
resp, err := stream.Recv()
if err != nil {
return
}
keyspaceWatcherChan <- resp.Keyspaces
}
}
}()
return keyspaceWatcherChan, err
}

// UpdateKeyspaceState attempts to update the keyspace specified by ID to the target state,
// it will also record StateChangedAt for the given keyspace if a state change took place.
// Currently, legal operations includes:
Expand Down
50 changes: 0 additions & 50 deletions tests/integrations/client/keyspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,56 +98,6 @@ func (suite *clientTestSuite) TestGetAllKeyspaces() {
}
}

func (suite *clientTestSuite) TestWatchKeyspaces() {
re := suite.Require()
initialKeyspaces := mustMakeTestKeyspaces(re, suite.srv, 10)
watchChan, err := suite.client.WatchKeyspaces(suite.ctx)
re.NoError(err)
// First batch of watchChan message should contain all existing keyspaces.
initialLoaded := <-watchChan
for i := range initialKeyspaces {
re.Contains(initialLoaded, initialKeyspaces[i])
}
// Each additional message contains extra put events.
additionalKeyspaces := mustMakeTestKeyspaces(re, suite.srv, 30)
re.NoError(err)
// Checks that all additional keyspaces are captured by watch channel.
for i := 0; i < 10; {
loadedKeyspaces := <-watchChan
re.NotEmpty(loadedKeyspaces)
for j := range loadedKeyspaces {
re.Equal(additionalKeyspaces[i+j], loadedKeyspaces[j])
}
i += len(loadedKeyspaces)
}
// Updates to state should also be captured.
expected, err := suite.srv.GetKeyspaceManager().UpdateKeyspaceState(initialKeyspaces[0].Name, keyspacepb.KeyspaceState_DISABLED, time.Now().Unix())
re.NoError(err)
loaded := <-watchChan
re.Equal([]*keyspacepb.KeyspaceMeta{expected}, loaded)
// Updates to config should also be captured.
expected, err = suite.srv.GetKeyspaceManager().UpdateKeyspaceConfig(initialKeyspaces[0].Name, []*keyspace.Mutation{
{
Op: keyspace.OpDel,
Key: testConfig1,
},
})
re.NoError(err)
loaded = <-watchChan
re.Equal([]*keyspacepb.KeyspaceMeta{expected}, loaded)
// Updates to default keyspace's config should also be captured.
expected, err = suite.srv.GetKeyspaceManager().UpdateKeyspaceConfig(utils.DefaultKeyspaceName, []*keyspace.Mutation{
{
Op: keyspace.OpPut,
Key: "config",
Value: "value",
},
})
re.NoError(err)
loaded = <-watchChan
re.Equal([]*keyspacepb.KeyspaceMeta{expected}, loaded)
}

func mustCreateKeyspaceAtState(re *require.Assertions, server *server.Server, index int, state keyspacepb.KeyspaceState) *keyspacepb.KeyspaceMeta {
manager := server.GetKeyspaceManager()
meta, err := manager.CreateKeyspace(&keyspace.CreateKeyspaceRequest{
Expand Down

0 comments on commit 69e48d4

Please sign in to comment.