Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…db#41543

40953: colexec: refactor selection ops template r=yuzefovich a=yuzefovich

Selection ops template similarly to projection ops template
pre-dates our current way of using templates. This commit brings it
in line with others.

Release justification: Category 1: Non-production code changes.

Release note: None

41507: storage: remove handleRaftReady from processRequestQueue, pool RaftMessageRequests r=nvanbenschoten a=nvanbenschoten

This PR combines two optimizations that together speed up Sysbench's `oltp_insect` (in this config: cockroachdb#41465 (comment)) from a throughput of **115,308 qps** to **119,660 qps** (averaged over a series of trials), an improvement of **3.8%**. It also reduces average latency in these tests from **4.17 ms** to **4.02 ms**.

The first optimization removes the call to `handleRaftReady` from `processRequestQueue`, which addresses an existing TODO. This is a partial revert of cockroachdb#12356. Calling `handleRaftReady` from `processRequestQueue` results in less batching and redundant calls to `handleRaftReady`. This does have a negative effect on throughput, as was speculated in that PR. It also makes the schedulers hooks into Raft processing more complex.

This commit removes the explicit call to handleRaftReady from `processRequestQueue`. Instead, it opts to interact with the `raftScheduler` like `processTick` does. This simplifies the raft scheduler's interactions and improves write throughput.

The second optimization pools the allocations of `RaftMessageRequest` objects. These were showing up as the top allocator in a run of Sysbench's `oltp_insert` workload.

41517: storage: Add GetSSTables on Pebble struct, enable compaction suggester r=itsbilal a=itsbilal

This change updates the Pebble wrapper struct to implement `GetSSTables()`,
which lets the compactor suggest compactions on pebble instances. Also
remove related compactor != nil checks.

Release note: None

41543: gceworker: confirm before stopping r=RaduBerinde a=RaduBerinde

I accidentally executed the previous command in a terminal and that
happened to be `gceworker.sh stop`. It stopped the VM in the middle of
something, which was unfortunate. This commit adds a prompt, you have
to type "yes" if you really mean it. We also put this prompt for the
`delete` command and add the `--quiet` flag so we don't get the
prompts from `gcloud`.

Release note: None

Co-authored-by: Yahor Yuzefovich <[email protected]>
Co-authored-by: Nathan VanBenschoten <[email protected]>
Co-authored-by: Bilal Akhtar <[email protected]>
Co-authored-by: Radu Berinde <[email protected]>
  • Loading branch information
5 people committed Oct 14, 2019
5 parents ee6d344 + f6c6c21 + 7db032e + d2f1250 + d868455 commit aa1bfce
Show file tree
Hide file tree
Showing 16 changed files with 513 additions and 347 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -1495,6 +1495,7 @@ pkg/sql/colexec/rank.eg.go: pkg/sql/colexec/rank_tmpl.go
pkg/sql/colexec/row_number.eg.go: pkg/sql/colexec/row_number_tmpl.go
pkg/sql/colexec/rowstovec.eg.go: pkg/sql/colexec/rowstovec_tmpl.go
pkg/sql/colexec/select_in.eg.go: pkg/sql/colexec/select_in_tmpl.go
pkg/sql/colexec/selection_ops.eg.go: pkg/sql/colexec/selection_ops_tmpl.go
pkg/sql/colexec/sort.eg.go: pkg/sql/colexec/sort_tmpl.go
pkg/sql/colexec/sum_agg.eg.go: pkg/sql/colexec/sum_agg_tmpl.go
pkg/sql/colexec/tuples_differ.eg.go: pkg/sql/colexec/tuples_differ_tmpl.go
Expand Down
4 changes: 1 addition & 3 deletions pkg/sql/colexec/execgen/cmd/execgen/like_ops_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ import (
`

func genLikeOps(wr io.Writer) error {
tmpl := template.New("like_sel_ops").Funcs(template.FuncMap{"buildDict": buildDict})
var err error
tmpl, err = tmpl.Parse(selTemplate)
tmpl, err := getSelectionOpsTmpl()
if err != nil {
return err
}
Expand Down
281 changes: 34 additions & 247 deletions pkg/sql/colexec/execgen/cmd/execgen/selection_ops_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,259 +12,52 @@ package main

import (
"io"
"io/ioutil"
"regexp"
"strings"
"text/template"

"github.com/cockroachdb/cockroach/pkg/col/coltypes"
)

const selTemplate = `
package colexec
import (
"bytes"
"context"
"math"
"github.com/cockroachdb/apd"
"github.com/cockroachdb/cockroach/pkg/col/coldata"
"github.com/cockroachdb/cockroach/pkg/col/coltypes"
"github.com/cockroachdb/cockroach/pkg/sql/colexec/execerror"
"github.com/cockroachdb/cockroach/pkg/sql/colexec/typeconv"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/pkg/errors"
)
{{define "opConstName"}}sel{{.Name}}{{.LTyp}}{{.RTyp}}ConstOp{{end}}
{{define "opName"}}sel{{.Name}}{{.LTyp}}{{.RTyp}}Op{{end}}
{{define "selConstLoop"}}
if sel := batch.Selection(); sel != nil {
sel = sel[:n]
for _, i := range sel {
var cmp bool
arg := {{.Global.LTyp.Get "col" "int(i)"}}
{{(.Global.Assign "cmp" "arg" "p.constArg")}}
if cmp {{if .HasNulls}}&& !nulls.NullAt(i) {{end}}{
sel[idx] = i
idx++
}
}
} else {
batch.SetSelection(true)
sel := batch.Selection()
col = {{.Global.LTyp.Slice "col" "0" "int(n)"}}
for {{.Global.LTyp.Range "i" "col"}} {
var cmp bool
arg := {{.Global.LTyp.Get "col" "i"}}
{{(.Global.Assign "cmp" "arg" "p.constArg")}}
if cmp {{if .HasNulls}}&& !nulls.NullAt(uint16(i)) {{end}}{
sel[idx] = uint16(i)
idx++
}
}
}
{{end}}
{{define "selLoop"}}
if sel := batch.Selection(); sel != nil {
sel = sel[:n]
for _, i := range sel {
var cmp bool
arg1 := {{.Global.LTyp.Get "col1" "int(i)"}}
arg2 := {{.Global.RTyp.Get "col2" "int(i)"}}
{{(.Global.Assign "cmp" "arg1" "arg2")}}
if cmp {{if .HasNulls}}&& !nulls.NullAt(i) {{end}}{
sel[idx] = i
idx++
}
}
} else {
batch.SetSelection(true)
sel := batch.Selection()
col1 = {{.Global.LTyp.Slice "col1" "0" "int(n)"}}
col1Len := {{.Global.LTyp.Len "col1"}}
col2 = {{.Global.RTyp.Slice "col2" "0" "col1Len"}}
for {{.Global.LTyp.Range "i" "col1"}} {
var cmp bool
arg1 := {{.Global.LTyp.Get "col1" "i"}}
arg2 := {{.Global.RTyp.Get "col2" "i"}}
{{(.Global.Assign "cmp" "arg1" "arg2")}}
if cmp {{if .HasNulls}}&& !nulls.NullAt(uint16(i)) {{end}}{
sel[idx] = uint16(i)
idx++
}
}
}
{{end}}
{{define "selConstOp"}}
type {{template "opConstName" .}} struct {
selConstOpBase
constArg {{.RGoType}}
}
func (p *{{template "opConstName" .}}) Next(ctx context.Context) coldata.Batch {
for {
batch := p.input.Next(ctx)
if batch.Length() == 0 {
return batch
}
vec := batch.ColVec(p.colIdx)
col := vec.{{.LTyp}}()
var idx uint16
n := batch.Length()
if vec.MaybeHasNulls() {
nulls := vec.Nulls()
{{template "selConstLoop" buildDict "Global" . "HasNulls" true }}
} else {
{{template "selConstLoop" buildDict "Global" . "HasNulls" false }}
}
if idx > 0 {
batch.SetLength(idx)
return batch
}
}
}
func (p {{template "opConstName" .}}) Init() {
p.input.Init()
}
{{end}}
{{define "selOp"}}
type {{template "opName" .}} struct {
selOpBase
}
func (p *{{template "opName" .}}) Next(ctx context.Context) coldata.Batch {
for {
batch := p.input.Next(ctx)
if batch.Length() == 0 {
return batch
}
vec1 := batch.ColVec(p.col1Idx)
vec2 := batch.ColVec(p.col2Idx)
col1 := vec1.{{.LTyp}}()
col2 := vec2.{{.RTyp}}()
n := batch.Length()
var idx uint16
if vec1.MaybeHasNulls() || vec2.MaybeHasNulls() {
nulls := vec1.Nulls().Or(vec2.Nulls())
{{template "selLoop" buildDict "Global" . "HasNulls" true }}
} else {
{{template "selLoop" buildDict "Global" . "HasNulls" false }}
}
if idx > 0 {
batch.SetLength(idx)
return batch
}
}
}
func (p {{template "opName" .}}) Init() {
p.input.Init()
}
{{end}}
{{/* The outer range is a coltypes.T (the left type). The middle range is also a
coltypes.T (the right type). The inner is the overloads associated with
those two types. */}}
{{range .}}
{{range .}}
{{range .}}
{{template "selConstOp" .}}
{{template "selOp" .}}
{{end}}
{{end}}
{{end}}
// GetSelectionConstOperator returns the appropriate constant selection operator
// for the given left and right column types and comparison.
func GetSelectionConstOperator(
leftColType *types.T,
constColType *types.T,
cmpOp tree.ComparisonOperator,
input Operator,
colIdx int,
constArg tree.Datum,
) (Operator, error) {
c, err := typeconv.GetDatumToPhysicalFn(constColType)(constArg)
func getSelectionOpsTmpl() (*template.Template, error) {
t, err := ioutil.ReadFile("pkg/sql/colexec/selection_ops_tmpl.go")
if err != nil {
return nil, err
}
selConstOpBase := selConstOpBase {
OneInputNode: NewOneInputNode(input),
colIdx: colIdx,
}
switch leftType := typeconv.FromColumnType(leftColType); leftType {
{{range $lTyp, $rTypToOverloads := . -}}
case coltypes.{{$lTyp}}:
switch rightType := typeconv.FromColumnType(constColType); rightType {
{{range $rTyp, $overloads := $rTypToOverloads}}
case coltypes.{{$rTyp}}:
switch cmpOp {
{{range $overloads -}}
case tree.{{.Name}}:
return &{{template "opConstName" .}}{selConstOpBase: selConstOpBase, constArg: c.({{.RGoType}})}, nil
{{end -}}
default:
return nil, errors.Errorf("unhandled comparison operator: %s", cmpOp)
}
{{end -}}
default:
return nil, errors.Errorf("unhandled right type: %s", rightType)
}
{{end -}}
default:
return nil, errors.Errorf("unhandled left type: %s", leftType)
}
}

// GetSelectionOperator returns the appropriate two column selection operator
// for the given left and right column types and comparison.
func GetSelectionOperator(
leftColType *types.T,
rightColType *types.T,
cmpOp tree.ComparisonOperator,
input Operator,
col1Idx int,
col2Idx int,
) (Operator, error) {
selOpBase := selOpBase {
OneInputNode: NewOneInputNode(input),
col1Idx: col1Idx,
col2Idx: col2Idx,
}
switch leftType := typeconv.FromColumnType(leftColType); leftType {
{{range $lTyp, $rTypToOverloads := . -}}
case coltypes.{{$lTyp}}:
switch rightType := typeconv.FromColumnType(rightColType); rightType {
{{range $rTyp, $overloads := $rTypToOverloads}}
case coltypes.{{$rTyp}}:
switch cmpOp {
{{range $overloads -}}
case tree.{{.Name}}:
return &{{template "opName" .}}{selOpBase: selOpBase}, nil
{{end -}}
default:
return nil, errors.Errorf("unhandled comparison operator: %s", cmpOp)
}
{{end -}}
default:
return nil, errors.Errorf("unhandled right type: %s", rightType)
}
{{end -}}
default:
return nil, errors.Errorf("unhandled left type: %s", leftType)
}
s := string(t)
s = strings.Replace(s, "_OP_CONST_NAME", "sel{{.Name}}{{.LTyp}}{{.RTyp}}ConstOp", -1)
s = strings.Replace(s, "_OP_NAME", "sel{{.Name}}{{.LTyp}}{{.RTyp}}Op", -1)
s = strings.Replace(s, "_R_GO_TYPE", "{{.RGoType}}", -1)
s = strings.Replace(s, "_L_TYP_VAR", "{{$lTyp}}", -1)
s = strings.Replace(s, "_R_TYP_VAR", "{{$rTyp}}", -1)
s = strings.Replace(s, "_L_TYP", "{{.LTyp}}", -1)
s = strings.Replace(s, "_R_TYP", "{{.RTyp}}", -1)
s = strings.Replace(s, "_NAME", "{{.Name}}", -1)

assignCmpRe := regexp.MustCompile(`_ASSIGN_CMP\((.*),(.*),(.*)\)`)
s = assignCmpRe.ReplaceAllString(s, "{{.Assign $1 $2 $3}}")

s = replaceManipulationFuncs(".LTyp", s)
s = strings.Replace(s, "_R_UNSAFEGET", "execgen.UNSAFEGET", -1)
s = strings.Replace(s, "_R_SLICE", "execgen.SLICE", -1)
s = replaceManipulationFuncs(".RTyp", s)

s = strings.Replace(s, "_HAS_NULLS", "$hasNulls", -1)
selConstLoop := makeFunctionRegex("_SEL_CONST_LOOP", 1)
s = selConstLoop.ReplaceAllString(s, `{{template "selConstLoop" buildDict "Global" $ "HasNulls" $1 "Overload" .}}`)
selLoop := makeFunctionRegex("_SEL_LOOP", 1)
s = selLoop.ReplaceAllString(s, `{{template "selLoop" buildDict "Global" $ "HasNulls" $1 "Overload" .}}`)

return template.New("selection_ops").Funcs(template.FuncMap{"buildDict": buildDict}).Parse(s)
}
`

func genSelectionOps(wr io.Writer) error {
tmpl, err := getSelectionOpsTmpl()
if err != nil {
return err
}
lTypToRTypToOverloads := make(map[coltypes.T]map[coltypes.T][]*overload)
for _, ov := range comparisonOpOverloads {
lTyp := ov.LTyp
Expand All @@ -276,12 +69,6 @@ func genSelectionOps(wr io.Writer) error {
}
rTypToOverloads[rTyp] = append(rTypToOverloads[rTyp], ov)
}
tmpl := template.New("selection_ops").Funcs(template.FuncMap{"buildDict": buildDict})
var err error
tmpl, err = tmpl.Parse(selTemplate)
if err != nil {
return err
}
return tmpl.Execute(wr, lTypToRTypToOverloads)
}

Expand Down
Loading

0 comments on commit aa1bfce

Please sign in to comment.