Repository navigation
fix changed targets streaming #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -49,10 +49,9 @@ func (c *controller) GetChangedTargets(request *pb.GetChangedTargetsRequest, str | |
| // Start jobs for both revisions. Success or failure, the result will report to the results channel. | ||
| type graphResult struct { | ||
| // order is 0 or 1, 0 is the base (first) revision, 1 is the target (second) revision | ||
| order int | ||
| // TODO: pb.GetTargetGraphResponse is a stream, so most likely we can't use GetTargetGraphResponse as a return type and we'll want to read it fully before joining the threads | ||
| graph *pb.GetTargetGraphResponse | ||
| err error | ||
| order int | ||
| chunks []*pb.GetTargetGraphResponse | ||
| err error | ||
| } | ||
| results := make(chan graphResult, len(jobs)) | ||
|
|
||
|
|
@@ -66,42 +65,53 @@ func (c *controller) GetChangedTargets(request *pb.GetChangedTargetsRequest, str | |
| revision = request.GetSecondRevision() | ||
| } | ||
| graphReader, err := c.getGraph(jobs[idx].ctx, revision, request.GetOutputConfig()) | ||
| if err != nil { | ||
| // something's wrong with getGraph | ||
| if err != nil || graphReader == nil { | ||
| results <- graphResult{order: idx, err: err} | ||
| return | ||
| } | ||
| if graphReader == nil { | ||
| results <- graphResult{order: idx, err: nil} | ||
| return | ||
| defer graphReader.Close() | ||
|
|
||
| // Read all chunks from the stream | ||
| var chunks []*pb.GetTargetGraphResponse | ||
| for { | ||
| chunk, err := graphReader.Read() | ||
| if err == io.EOF { | ||
| break | ||
| } | ||
| if err != nil { | ||
| results <- graphResult{order: idx, err: err} | ||
| return | ||
| } | ||
| chunks = append(chunks, chunk) | ||
| } | ||
| graph, err := graphReader.Read() | ||
| results <- graphResult{order: idx, graph: graph, err: err} | ||
| results <- graphResult{order: idx, chunks: chunks} | ||
| }(i) | ||
| } | ||
|
|
||
| // Wait for both results to complete, either successfully or with an error. | ||
| for range jobs { | ||
| select { | ||
| case res := <-results: | ||
| if res.graph != nil { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. at this point, the graphreader is done or eof, so we should makr jobs[res.order].completed = true and update jobs[res.order].err accordingly |
||
| jobs[res.order].graphStreamChunks = append(jobs[res.order].graphStreamChunks, res.graph) | ||
| } | ||
| if res.graph == nil { | ||
| jobs[res.order].completed = true | ||
| } | ||
| jobs[res.order].graphStreamChunks = res.chunks | ||
| jobs[res.order].completed = true | ||
| jobs[res.order].err = res.err | ||
| if res.err == io.EOF { | ||
| res.err = nil | ||
| jobs[res.order].completed = true | ||
| jobs[res.order].err = nil | ||
| } | ||
| if res.chunks == nil && res.err == nil { | ||
| jobs[res.order].err = errors.New("no chunks returned") | ||
| } | ||
| if res.err != nil { | ||
| jobs[res.order].err = res.err | ||
|
|
||
| // one of the computations failed, if the other one has not completed yet, cancel it and wait for the result to come in, which would be a context cancelled result then | ||
| // one of the computations failed, if the other one has not | ||
| // completed yet, cancel it and wait for the result to come in, | ||
| // which would be a context cancelled result then | ||
| if res.err != nil { | ||
| other := (res.order + 1) % 2 | ||
| if !jobs[other].completed { | ||
| jobs[other].cancel() | ||
|
|
||
| // explicitly mark that this job is cancelled, so we can ignore its error later | ||
| // explicitly mark that this job is cancelled, so we can | ||
| // ignore its error later | ||
| jobs[other].cancelled = true | ||
| } | ||
| } | ||
|
|
@@ -209,7 +219,7 @@ func (c *controller) compareTargetGraphs(ctx context.Context, firstGraph, second | |
| initial := pb.CHANGE_TYPE_INDIRECT | ||
| // If we know the source file rule type, classify changes accordingly. | ||
| // Otherwise, leave as UNSPECIFIED. | ||
| // check if the target is a source file, if so, it is a direct change | ||
| // check if the target is a source file, if so, it is a direct change | ||
| isSource := newT.GetRuleType() == sourceFileRuleTypeID && sourceFileRuleTypeID != -1 | ||
| if isSource { | ||
| initial = pb.CHANGE_TYPE_DIRECT | ||
|
|
@@ -240,8 +250,8 @@ func (c *controller) compareTargetGraphs(ctx context.Context, firstGraph, second | |
| ) | ||
| changedByName[name] = &pb.ChangedTarget{ | ||
| ChangeType: initial, | ||
| OldTarget: oldTarget, | ||
| NewTarget: newTarget, | ||
| OldTarget: oldTarget, | ||
| NewTarget: newTarget, | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -287,11 +297,11 @@ func (c *controller) compareTargetGraphs(ctx context.Context, firstGraph, second | |
|
|
||
| // 5) Construct canonical metadata and emit responses. | ||
| meta := &pb.Metadata{ | ||
| TargetIdMapping: targetMapper.Invert(), | ||
| RuleTypeMapping: ruleTypeMapper.Invert(), | ||
| TagMapping: tagMapper.Invert(), | ||
| AttributeNameMapping: attrNameMapper.Invert(), | ||
| AttributeStringValueMapping: attrValMapper.Invert(), | ||
| TargetIdMapping: targetMapper.Invert(), | ||
| RuleTypeMapping: ruleTypeMapper.Invert(), | ||
| TagMapping: tagMapper.Invert(), | ||
| AttributeNameMapping: attrNameMapper.Invert(), | ||
| AttributeStringValueMapping: attrValMapper.Invert(), | ||
| } | ||
|
|
||
| // Emit changes and metadata as separate responses. | ||
|
|
||
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
graphreader only reads one chunk