Skip to content

Commit

Permalink
feat(scyllaclient): allow to filter by keyspaces by type
Browse files Browse the repository at this point in the history
  • Loading branch information
Michal-Leszczynski authored and karol-kokoszka committed Sep 3, 2024
1 parent 3bfbdcb commit 0eeb8b4
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
49 changes: 40 additions & 9 deletions pkg/scyllaclient/client_scylla.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,21 +256,39 @@ func (c *Client) hosts(ctx context.Context) ([]string, error) {
return v, nil
}

// KeyspaceReplication describes keyspace replication type.
type KeyspaceReplication = string
// KeyspaceReplication describes keyspace replication.
type KeyspaceReplication string

// KeyspaceReplication enum.
const (
ReplicationAll = "all"
ReplicationVnode = "vnodes"
ReplicationTablet = "tablets"
ReplicationAll KeyspaceReplication = "all"
ReplicationVnode KeyspaceReplication = "vnodes"
ReplicationTablet KeyspaceReplication = "tablets"
)

// ReplicationKeyspaces return a list of keyspaces with given replication.
func (c *Client) ReplicationKeyspaces(ctx context.Context, replication KeyspaceReplication) ([]string, error) {
// KeyspaceType describes keyspace type.
type KeyspaceType string

// KeyspaceType enum.
const (
KeyspaceTypeAll KeyspaceType = "all"
KeyspaceTypeUser KeyspaceType = "user"
KeyspaceTypeNonLocal KeyspaceType = "non_local_strategy"
)

// AllKeyspaceTypes contains all possible KeyspaceType.
var AllKeyspaceTypes = []KeyspaceType{
KeyspaceTypeAll,
KeyspaceTypeUser,
KeyspaceTypeNonLocal,
}

// FilteredKeyspaces return a list of keyspaces with given replication.
func (c *Client) FilteredKeyspaces(ctx context.Context, ksType KeyspaceType, replication KeyspaceReplication) ([]string, error) {
resp, err := c.scyllaOps.StorageServiceKeyspacesGet(&operations.StorageServiceKeyspacesGetParams{
Context: ctx,
Replication: &replication,
Type: pointer.StringPtr(string(ksType)),
Replication: pointer.StringPtr(string(replication)),
})
if err != nil {
return nil, err
Expand All @@ -280,7 +298,20 @@ func (c *Client) ReplicationKeyspaces(ctx context.Context, replication KeyspaceR

// Keyspaces return a list of all the keyspaces.
func (c *Client) Keyspaces(ctx context.Context) ([]string, error) {
return c.ReplicationKeyspaces(ctx, ReplicationAll)
return c.FilteredKeyspaces(ctx, KeyspaceTypeAll, ReplicationAll)
}

// KeyspacesByType returns a list of keyspaces aggregated by KeyspaceType.
func (c *Client) KeyspacesByType(ctx context.Context) (map[KeyspaceType][]string, error) {
out := make(map[KeyspaceType][]string, len(AllKeyspaceTypes))
for _, t := range AllKeyspaceTypes {
ks, err := c.FilteredKeyspaces(ctx, t, ReplicationAll)
if err != nil {
return nil, errors.Wrapf(err, "query keyspaces with type %q", t)
}
out[t] = ks
}
return out, nil
}

// Tables returns a slice of table names in a given keyspace.
Expand Down
4 changes: 2 additions & 2 deletions pkg/scyllaclient/ring_describer.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,12 @@ func getTabletKs(ctx context.Context, client *Client) *strset.Set {
out := strset.New()
// Assume that errors indicate that endpoints rejected 'replication' param,
// which means that given Scylla version does not support tablet API.
tablets, err := client.ReplicationKeyspaces(ctx, ReplicationTablet)
tablets, err := client.FilteredKeyspaces(ctx, KeyspaceTypeAll, ReplicationTablet)
if err != nil {
client.logger.Info(ctx, "Couldn't list tablet keyspaces", "error", err)
return out
}
vnodes, err := client.ReplicationKeyspaces(ctx, ReplicationVnode)
vnodes, err := client.FilteredKeyspaces(ctx, KeyspaceTypeAll, ReplicationVnode)
if err != nil {
client.logger.Info(ctx, "Couldn't list vnode keyspaces", "error", err)
return out
Expand Down

0 comments on commit 0eeb8b4

Please sign in to comment.