Skip to content

Commit

Permalink
Allow pools to be given a ksuid name
Browse files Browse the repository at this point in the history
The commit fixes an issue with pool looks that made it so pools with a
ksuid-like name could not be looked up for most zed commands. Fix the
logic for lake/api.PoolID so that looking up a pool by ID is tried first
and then falls back to looking up a pool by name if nothing is found.

Closes #4431
  • Loading branch information
mattnibs committed Feb 6, 2024
1 parent 5e986d3 commit bd4893b
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 24 deletions.
7 changes: 2 additions & 5 deletions cmd/zed/use/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,9 @@ func (c *Command) Run(args []string) error {
if err != nil {
return err
}
poolID, err := lakeparse.ParseID(commitish.Pool)
poolID, err := lake.PoolID(ctx, commitish.Pool)
if err != nil {
poolID, err = lake.PoolID(ctx, commitish.Pool)
if err != nil {
return err
}
return err
}
if _, err = lake.CommitObject(ctx, poolID, commitish.Branch); err != nil {
return err
Expand Down
5 changes: 5 additions & 0 deletions lake/api/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ func (l *local) PoolID(ctx context.Context, poolName string) (ksuid.KSUID, error
if poolName == "" {
return ksuid.Nil, errors.New("no pool name provided")
}
if id, err := lakeparse.ParseID(poolName); err == nil {
if _, err := l.root.OpenPool(ctx, id); err == nil {
return id, nil
}
}
return l.root.PoolID(ctx, poolName)
}

Expand Down
5 changes: 5 additions & 0 deletions lake/api/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ func (l *remote) Root() *lake.Root {
}

func (r *remote) PoolID(ctx context.Context, poolName string) (ksuid.KSUID, error) {
if id, err := lakeparse.ParseID(poolName); err == nil {
if _, err := LookupPoolByID(ctx, r, id); err == nil {
return id, nil
}
}
config, err := LookupPoolByName(ctx, r, poolName)
if err != nil {
return ksuid.Nil, err
Expand Down
12 changes: 3 additions & 9 deletions lake/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,16 +238,10 @@ func (r *Root) PoolID(ctx context.Context, poolName string) (ksuid.KSUID, error)
if poolName == "" {
return ksuid.Nil, errors.New("no pool name given")
}
poolID, err := ksuid.Parse(poolName)
var poolRef *pools.Config
if err != nil {
poolRef = r.pools.LookupByName(ctx, poolName)
if poolRef == nil {
return ksuid.Nil, fmt.Errorf("%s: %w", poolName, pools.ErrNotFound)
}
poolID = poolRef.ID
if poolRef := r.pools.LookupByName(ctx, poolName); poolRef != nil {
return poolRef.ID, nil
}
return poolID, nil
return ksuid.Nil, fmt.Errorf("%s: %w", poolName, pools.ErrNotFound)
}

func (r *Root) CommitObject(ctx context.Context, poolID ksuid.KSUID, branchName string) (ksuid.KSUID, error) {
Expand Down
11 changes: 11 additions & 0 deletions lake/ztests/create-ksuid-name.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Test that a pool can be given a ksuid name and everything still works.
script: |
export ZED_LAKE=test
zed init -q
zed create 2WwyVrZdEITo5WkKu1YsJC4dMjU
zed use 2WwyVrZdEITo5WkKu1YsJC4dMjU
outputs:
- name: stdout
regexp: |
pool created: 2WwyVrZdEITo5WkKu1YsJC4dMjU \w{27}
Switched to branch "main" on pool "2WwyVrZdEITo5WkKu1YsJC4dMjU"
22 changes: 12 additions & 10 deletions service/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,20 @@ func (r *Request) PoolID(w *ResponseWriter, root *lake.Root) (ksuid.KSUID, bool)
if !ok {
return ksuid.Nil, false
}
id, err := lakeparse.ParseID(s)
if err != nil {
id, err = root.PoolID(r.Context(), s)
if errors.Is(err, pools.ErrNotFound) {
w.Error(err)
return ksuid.Nil, false
}
if err != nil {
w.Error(srverr.ErrInvalid("invalid path param %q: %w", s, err))
return ksuid.Nil, false
if id, err := lakeparse.ParseID(s); err == nil {
if _, err = root.OpenPool(r.Context(), id); err == nil {
return id, true
}
}
id, err := root.PoolID(r.Context(), s)
if errors.Is(err, pools.ErrNotFound) {
w.Error(err)
return ksuid.Nil, false
}
if err != nil {
w.Error(srverr.ErrInvalid("invalid path param %q: %w", s, err))
return ksuid.Nil, false
}
return id, true
}

Expand Down
12 changes: 12 additions & 0 deletions service/ztests/create-ksuid-name.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Test that a pool can be given a ksuid name and everything still works.
script: |
source service.sh
zed create 2WwyVrZdEITo5WkKu1YsJC4dMjU
zed use 2WwyVrZdEITo5WkKu1YsJC4dMjU
inputs:
- name: service.sh
outputs:
- name: stdout
regexp: |
pool created: 2WwyVrZdEITo5WkKu1YsJC4dMjU \w{27}
Switched to branch "main" on pool "2WwyVrZdEITo5WkKu1YsJC4dMjU"

0 comments on commit bd4893b

Please sign in to comment.