Skip to content

Commit

Permalink
Merge pull request #1269 from hinshun/ref-array
Browse files Browse the repository at this point in the history
Change result type to array of refs
  • Loading branch information
AkihiroSuda authored Dec 18, 2019
2 parents 1dfd864 + 3595740 commit f7cf482
Show file tree
Hide file tree
Showing 5 changed files with 975 additions and 181 deletions.
62 changes: 55 additions & 7 deletions frontend/gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,16 @@ func (lbf *llbBridgeForwarder) Solve(ctx context.Context, req *pb.SolveRequest)
}
ids[k] = id
}
pbRes.Result = &pb.Result_Refs{Refs: &pb.RefMap{Refs: ids}}

if req.AllowResultArrayRef {
refMap := make(map[string]*pb.Ref, len(res.Refs))
for k, id := range ids {
refMap[k] = pb.NewRef(id)
}
pbRes.Result = &pb.Result_Refs{Refs: &pb.RefMap{Refs: refMap}}
} else {
pbRes.Result = &pb.Result_RefsDeprecated{RefsDeprecated: &pb.RefMapDeprecated{Refs: ids}}
}
} else {
id := identity.NewID()
if res.Ref == nil {
Expand All @@ -484,7 +493,12 @@ func (lbf *llbBridgeForwarder) Solve(ctx context.Context, req *pb.SolveRequest)
lbf.refs[id] = res.Ref
}
defaultID = id
pbRes.Result = &pb.Result_Ref{Ref: id}

if req.AllowResultArrayRef {
pbRes.Result = &pb.Result_Ref{Ref: pb.NewRef(id)}
} else {
pbRes.Result = &pb.Result_RefDeprecated{RefDeprecated: id}
}
}
lbf.mu.Unlock()

Expand Down Expand Up @@ -635,16 +649,42 @@ func (lbf *llbBridgeForwarder) Return(ctx context.Context, in *pb.ReturnRequest)
}

switch res := in.Result.Result.(type) {
case *pb.Result_RefDeprecated:
var ids []string
if res.RefDeprecated != "" {
ids = append(ids, res.RefDeprecated)
}

ref, err := lbf.convertRef(ids)
if err != nil {
return nil, err
}
r.Ref = ref
case *pb.Result_RefsDeprecated:
m := map[string]solver.CachedResult{}
for k, v := range res.RefsDeprecated.Refs {
var ids []string
if v != "" {
ids = append(ids, v)
}

ref, err := lbf.convertRef(ids)
if err != nil {
return nil, err
}
m[k] = ref
}
r.Refs = m
case *pb.Result_Ref:
ref, err := lbf.convertRef(res.Ref)
ref, err := lbf.convertRef(res.Ref.Ids)
if err != nil {
return nil, err
}
r.Ref = ref
case *pb.Result_Refs:
m := map[string]solver.CachedResult{}
for k, v := range res.Refs.Refs {
ref, err := lbf.convertRef(v)
for k, ref := range res.Refs.Refs {
ref, err := lbf.convertRef(ref.Ids)
if err != nil {
return nil, err
}
Expand All @@ -656,16 +696,24 @@ func (lbf *llbBridgeForwarder) Return(ctx context.Context, in *pb.ReturnRequest)
}
}

func (lbf *llbBridgeForwarder) convertRef(id string) (solver.CachedResult, error) {
if id == "" {
func (lbf *llbBridgeForwarder) convertRef(ids []string) (solver.CachedResult, error) {
if len(ids) == 0 {
return nil, nil
}

if len(ids) > 1 {
return nil, errors.Errorf("return reference has multi-result array")
}

lbf.mu.Lock()
defer lbf.mu.Unlock()

id := ids[0]
r, ok := lbf.refs[id]
if !ok {
return nil, errors.Errorf("return reference %s not found", id)
}

return r, nil
}

Expand Down
42 changes: 31 additions & 11 deletions frontend/gateway/grpcclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,22 +105,22 @@ func (c *grpcClient) Run(ctx context.Context, f client.BuildFunc) (retError erro
Metadata: res.Metadata,
}
if res.Refs != nil {
m := map[string]string{}
m := map[string]*pb.Ref{}
for k, r := range res.Refs {
id, err := convertRef(r)
if err != nil {
retError = err
continue
}
m[k] = id
m[k] = pb.NewRef(id)
}
pbRes.Result = &pb.Result_Refs{Refs: &pb.RefMap{Refs: m}}
} else {
id, err := convertRef(res.Ref)
if err != nil {
retError = err
} else {
pbRes.Result = &pb.Result_Ref{Ref: id}
pbRes.Result = &pb.Result_Ref{Ref: pb.NewRef(id)}
}
}
if retError == nil {
Expand Down Expand Up @@ -280,10 +280,11 @@ func (c *grpcClient) Solve(ctx context.Context, creq client.SolveRequest) (*clie
}

req := &pb.SolveRequest{
Definition: creq.Definition,
Frontend: creq.Frontend,
FrontendOpt: creq.FrontendOpt,
AllowResultReturn: true,
Definition: creq.Definition,
Frontend: creq.Frontend,
FrontendOpt: creq.FrontendOpt,
AllowResultReturn: true,
AllowResultArrayRef: true,
// old API
ImportCacheRefsDeprecated: legacyRegistryCacheImports,
// new API
Expand All @@ -310,18 +311,37 @@ func (c *grpcClient) Solve(ctx context.Context, creq client.SolveRequest) (*clie
} else {
res.Metadata = resp.Result.Metadata
switch pbRes := resp.Result.Result.(type) {
case *pb.Result_Ref:
if id := pbRes.Ref; id != "" {
case *pb.Result_RefDeprecated:
if id := pbRes.RefDeprecated; id != "" {
res.SetRef(&reference{id: id, c: c})
}
case *pb.Result_Refs:
for k, v := range pbRes.Refs.Refs {
case *pb.Result_RefsDeprecated:
for k, v := range pbRes.RefsDeprecated.Refs {
ref := &reference{id: v, c: c}
if v == "" {
ref = nil
}
res.AddRef(k, ref)
}
case *pb.Result_Ref:
ids := pbRes.Ref.Ids
if len(ids) > 0 {
if len(ids) > 1 {
return nil, errors.Errorf("solve returned multi-result array")
}
res.SetRef(&reference{id: ids[0], c: c})
}
case *pb.Result_Refs:
for k, v := range pbRes.Refs.Refs {
var ref *reference
if len(v.Ids) > 0 {
if len(v.Ids) > 1 {
return nil, errors.Errorf("solve returned multi-result array")
}
ref = &reference{id: v.Ids[0], c: c}
}
res.AddRef(k, ref)
}
}
}

Expand Down
Loading

0 comments on commit f7cf482

Please sign in to comment.