-
Notifications
You must be signed in to change notification settings - Fork 123
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
rule: parallelize projects calling UAST not under exchanges #767
Closed
erizocosmico
wants to merge
1
commit into
src-d:master
from
erizocosmico:feature/parallel-uast-project
Closed
Changes from all commits
Commits
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 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 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 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 |
---|---|---|
@@ -0,0 +1,239 @@ | ||
package rule | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"strings" | ||
"sync" | ||
|
||
opentracing "github.com/opentracing/opentracing-go" | ||
"gopkg.in/src-d/go-mysql-server.v0/sql" | ||
"gopkg.in/src-d/go-mysql-server.v0/sql/plan" | ||
) | ||
|
||
type parallelProject struct { | ||
*plan.Project | ||
parallelism int | ||
} | ||
|
||
func newParallelProject( | ||
projection []sql.Expression, | ||
child sql.Node, | ||
parallelism int, | ||
) *parallelProject { | ||
return ¶llelProject{ | ||
plan.NewProject(projection, child), | ||
parallelism, | ||
} | ||
} | ||
|
||
func (p *parallelProject) RowIter(ctx *sql.Context) (sql.RowIter, error) { | ||
span, ctx := ctx.Span( | ||
"plan.Project", | ||
opentracing.Tag{ | ||
Key: "projections", | ||
Value: len(p.Projections), | ||
}, | ||
opentracing.Tag{ | ||
Key: "parallelism", | ||
Value: p.parallelism, | ||
}, | ||
) | ||
|
||
iter, err := p.Child.RowIter(ctx) | ||
if err != nil { | ||
span.Finish() | ||
return nil, err | ||
} | ||
|
||
return sql.NewSpanIter( | ||
span, | ||
newParallelIter(p.Projections, iter, ctx, p.parallelism), | ||
), nil | ||
} | ||
|
||
func (p *parallelProject) TransformUp(f sql.TransformNodeFunc) (sql.Node, error) { | ||
child, err := p.Child.TransformUp(f) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return f(newParallelProject(p.Projections, child, p.parallelism)) | ||
} | ||
|
||
func (p *parallelProject) TransformExpressionsUp(f sql.TransformExprFunc) (sql.Node, error) { | ||
var exprs = make([]sql.Expression, len(p.Projections)) | ||
for i, e := range p.Projections { | ||
expr, err := e.TransformUp(f) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
exprs[i] = expr | ||
} | ||
|
||
child, err := p.Child.TransformExpressionsUp(f) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return newParallelProject(exprs, child, p.parallelism), nil | ||
} | ||
|
||
func (p *parallelProject) String() string { | ||
pr := sql.NewTreePrinter() | ||
var exprs = make([]string, len(p.Projections)) | ||
for i, expr := range p.Projections { | ||
exprs[i] = expr.String() | ||
} | ||
|
||
_ = pr.WriteNode( | ||
"gitbase.ParallelProject(%s, parallelism=%d)", | ||
strings.Join(exprs, ", "), | ||
p.parallelism, | ||
) | ||
|
||
_ = pr.WriteChildren(p.Child.String()) | ||
return pr.String() | ||
} | ||
|
||
type parallelIter struct { | ||
projections []sql.Expression | ||
child sql.RowIter | ||
ctx *sql.Context | ||
parallelism int | ||
|
||
cancel context.CancelFunc | ||
rows chan sql.Row | ||
errors chan error | ||
done bool | ||
|
||
mut sync.Mutex | ||
finished bool | ||
} | ||
|
||
func newParallelIter( | ||
projections []sql.Expression, | ||
child sql.RowIter, | ||
ctx *sql.Context, | ||
parallelism int, | ||
) *parallelIter { | ||
var cancel context.CancelFunc | ||
ctx.Context, cancel = context.WithCancel(ctx.Context) | ||
|
||
return ¶llelIter{ | ||
projections: projections, | ||
child: child, | ||
ctx: ctx, | ||
parallelism: parallelism, | ||
cancel: cancel, | ||
errors: make(chan error, parallelism), | ||
} | ||
} | ||
|
||
func (i *parallelIter) Next() (sql.Row, error) { | ||
if i.done { | ||
return nil, io.EOF | ||
} | ||
|
||
if i.rows == nil { | ||
i.rows = make(chan sql.Row, i.parallelism) | ||
go i.start() | ||
} | ||
|
||
select { | ||
case row, ok := <-i.rows: | ||
if !ok { | ||
i.close() | ||
return nil, io.EOF | ||
} | ||
return row, nil | ||
case err := <-i.errors: | ||
i.close() | ||
return nil, err | ||
} | ||
} | ||
|
||
func (i *parallelIter) nextRow() (sql.Row, bool) { | ||
i.mut.Lock() | ||
defer i.mut.Unlock() | ||
|
||
if i.finished { | ||
return nil, true | ||
} | ||
|
||
row, err := i.child.Next() | ||
if err != nil { | ||
if err == io.EOF { | ||
i.finished = true | ||
} else { | ||
i.errors <- err | ||
} | ||
return nil, true | ||
} | ||
|
||
return row, false | ||
} | ||
|
||
func (i *parallelIter) start() { | ||
var wg sync.WaitGroup | ||
wg.Add(i.parallelism) | ||
for j := 0; j < i.parallelism; j++ { | ||
go func() { | ||
defer wg.Done() | ||
|
||
for { | ||
select { | ||
case <-i.ctx.Done(): | ||
i.errors <- context.Canceled | ||
return | ||
default: | ||
} | ||
|
||
row, stop := i.nextRow() | ||
if stop { | ||
return | ||
} | ||
|
||
row, err := project(i.ctx, i.projections, row) | ||
if err != nil { | ||
i.errors <- err | ||
return | ||
} | ||
|
||
i.rows <- row | ||
} | ||
}() | ||
} | ||
|
||
wg.Wait() | ||
close(i.rows) | ||
} | ||
|
||
func (i *parallelIter) close() { | ||
if !i.done { | ||
i.cancel() | ||
i.done = true | ||
} | ||
} | ||
|
||
func (i *parallelIter) Close() error { | ||
i.close() | ||
return i.child.Close() | ||
} | ||
|
||
func project( | ||
s *sql.Context, | ||
projections []sql.Expression, | ||
row sql.Row, | ||
) (sql.Row, error) { | ||
var fields []interface{} | ||
for _, expr := range projections { | ||
f, err := expr.Eval(s, row) | ||
if err != nil { | ||
return nil, err | ||
} | ||
fields = append(fields, f) | ||
} | ||
return sql.NewRow(fields...), nil | ||
} |
This file contains 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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package rule | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"runtime" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"gopkg.in/src-d/go-mysql-server.v0/mem" | ||
"gopkg.in/src-d/go-mysql-server.v0/sql" | ||
"gopkg.in/src-d/go-mysql-server.v0/sql/expression" | ||
"gopkg.in/src-d/go-mysql-server.v0/sql/plan" | ||
) | ||
|
||
func TestParallelProject(t *testing.T) { | ||
require := require.New(t) | ||
ctx := sql.NewEmptyContext() | ||
child := mem.NewTable("test", sql.Schema{ | ||
{Name: "col1", Type: sql.Text, Nullable: true}, | ||
{Name: "col2", Type: sql.Text, Nullable: true}, | ||
}) | ||
|
||
var input, expected []sql.Row | ||
for i := 1; i < 500; i++ { | ||
input = append(input, sql.Row{ | ||
fmt.Sprintf("col1_%d", i), fmt.Sprintf("col2_%d", i), | ||
}) | ||
|
||
expected = append(expected, sql.Row{fmt.Sprintf("col2_%d", i)}) | ||
} | ||
|
||
for _, row := range input { | ||
require.NoError(child.Insert(sql.NewEmptyContext(), row)) | ||
} | ||
|
||
p := newParallelProject( | ||
[]sql.Expression{expression.NewGetField(1, sql.Text, "col2", true)}, | ||
plan.NewResolvedTable(child), | ||
runtime.NumCPU(), | ||
) | ||
|
||
iter, err := p.RowIter(ctx) | ||
require.NoError(err) | ||
|
||
rows, err := sql.RowIterToRows(iter) | ||
require.NoError(err) | ||
require.ElementsMatch(expected, rows) | ||
|
||
_, err = iter.Next() | ||
require.Equal(io.EOF, err) | ||
} |
Oops, something went wrong.
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.
I think we need to wrap this
if
by mutexThere 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.
Nope,
Next
is never used in parallel