Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
69832: workload: fix `simple` method for queries r=otan a=rafiss

fixes cockroachdb#69678 

The previous attempt to add this forgot to unpack the original args.
Another way to do this is
```
append([]interface{true}, args...)...
```
but that has more allocations.

The approach in this commit is based on
https://github.com/golang/go/wiki/SliceTricks

Release justification: test-only change
Release note: None

Co-authored-by: Rafi Shamim <rafi@cockroachlabs.com>
  • Loading branch information
craig[bot] and rafiss committed Sep 6, 2021
2 parents 2d752c2 + cd12405 commit e369d86
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
2 changes: 1 addition & 1 deletion pkg/cmd/roachtest/tests/tpcc.go
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,7 @@ func registerTPCC(r registry.Registry) {
Warehouses: len(regions) * warehousesPerRegion,
Duration: duration,
ExtraSetupArgs: partitionArgs,
ExtraRunArgs: `--method=noprepare --wait=false --tolerate-errors ` + partitionArgs,
ExtraRunArgs: `--method=simple --wait=false --tolerate-errors ` + partitionArgs,
Chaos: func() Chaos {
return Chaos{
Timer: Periodic{
Expand Down
28 changes: 16 additions & 12 deletions pkg/workload/sql_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,7 @@ func (h StmtHandle) Exec(ctx context.Context, args ...interface{}) (pgconn.Comma
return p.Exec(ctx, h.s.sql, args...)

case simple:
newArgs := []interface{}{pgx.QuerySimpleProtocol(true)}
return p.Exec(ctx, h.s.sql, append(newArgs, args)...)
return p.Exec(ctx, h.s.sql, prependQuerySimpleProtocol(args)...)

default:
panic("invalid method")
Expand All @@ -184,8 +183,7 @@ func (h StmtHandle) ExecTx(
return tx.Exec(ctx, h.s.sql, args...)

case simple:
newArgs := []interface{}{pgx.QuerySimpleProtocol(true)}
return tx.Exec(ctx, h.s.sql, append(newArgs, args)...)
return tx.Exec(ctx, h.s.sql, prependQuerySimpleProtocol(args)...)

default:
panic("invalid method")
Expand All @@ -206,8 +204,7 @@ func (h StmtHandle) Query(ctx context.Context, args ...interface{}) (pgx.Rows, e
return p.Query(ctx, h.s.sql, args...)

case simple:
newArgs := []interface{}{pgx.QuerySimpleProtocol(true)}
return p.Query(ctx, h.s.sql, append(newArgs, args)...)
return p.Query(ctx, h.s.sql, prependQuerySimpleProtocol(args)...)

default:
panic("invalid method")
Expand All @@ -227,8 +224,7 @@ func (h StmtHandle) QueryTx(ctx context.Context, tx pgx.Tx, args ...interface{})
return tx.Query(ctx, h.s.sql, args...)

case simple:
newArgs := []interface{}{pgx.QuerySimpleProtocol(true)}
return tx.Query(ctx, h.s.sql, append(newArgs, args)...)
return tx.Query(ctx, h.s.sql, prependQuerySimpleProtocol(args)...)

default:
panic("invalid method")
Expand All @@ -249,8 +245,7 @@ func (h StmtHandle) QueryRow(ctx context.Context, args ...interface{}) pgx.Row {
return p.QueryRow(ctx, h.s.sql, args...)

case simple:
newArgs := []interface{}{pgx.QuerySimpleProtocol(true)}
return p.QueryRow(ctx, h.s.sql, append(newArgs, args)...)
return p.QueryRow(ctx, h.s.sql, prependQuerySimpleProtocol(args)...)

default:
panic("invalid method")
Expand All @@ -271,13 +266,22 @@ func (h StmtHandle) QueryRowTx(ctx context.Context, tx pgx.Tx, args ...interface
return tx.QueryRow(ctx, h.s.sql, args...)

case simple:
newArgs := []interface{}{pgx.QuerySimpleProtocol(true)}
return tx.QueryRow(ctx, h.s.sql, append(newArgs, args)...)
return tx.QueryRow(ctx, h.s.sql, prependQuerySimpleProtocol(args)...)

default:
panic("invalid method")
}
}

// prependQuerySimpleProtocol inserts pgx.QuerySimpleProtocol(true) at the
// beginning of the slice. It is based on
// https://github.com/golang/go/wiki/SliceTricks.
func prependQuerySimpleProtocol(args []interface{}) []interface{} {
args = append(args, pgx.QuerySimpleProtocol(true))
copy(args[1:], args)
args[0] = pgx.QuerySimpleProtocol(true)
return args
}

// Appease the linter.
var _ = StmtHandle.QueryRow

0 comments on commit e369d86

Please sign in to comment.