Skip to content

Commit 062f970

Browse files
committed
workload/ycsb: add --isolation-level flag
Informs cockroachdb#107112. This commit adds an `--isolation-level` flag to ycsb, which controls the isolation level to run the workload transactions under. If unset, the workload will run with the default isolation level of the database. Release note: None
1 parent eb0a104 commit 062f970

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

pkg/workload/connection.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,33 @@ func SanitizeUrls(gen Generator, dbOverride string, urls []string) (string, erro
107107
}
108108
return dbName, nil
109109
}
110+
111+
// SetDefaultIsolationLevel configures the provided URLs with the specified
112+
// default transaction isolation level, if any.
113+
func SetDefaultIsolationLevel(urls []string, isoLevel string) error {
114+
if isoLevel == "" {
115+
return nil
116+
}
117+
// As a convenience, replace underscores with spaces. This allows users of the
118+
// workload tool to pass --isolation-level=read_committed instead of needing
119+
// to pass --isolation-level="read committed".
120+
isoLevel = strings.ReplaceAll(isoLevel, "_", " ")
121+
// NOTE: validation of the isolation level value is done by the server during
122+
// connection establishment.
123+
return setUrlParam(urls, "default_transaction_isolation", isoLevel)
124+
}
125+
126+
// setUrlParam sets the given parameter to the given value in the provided URLs.
127+
func setUrlParam(urls []string, param, value string) error {
128+
for i := range urls {
129+
parsed, err := url.Parse(urls[i])
130+
if err != nil {
131+
return err
132+
}
133+
q := parsed.Query()
134+
q.Set(param, value)
135+
parsed.RawQuery = q.Encode()
136+
urls[i] = parsed.String()
137+
}
138+
return nil
139+
}

pkg/workload/ycsb/ycsb.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ type ycsb struct {
9393
flags workload.Flags
9494
connFlags *workload.ConnFlags
9595

96+
isoLevel string
9697
timeString bool
9798
insertHash bool
9899
zeroPadding int
@@ -125,8 +126,10 @@ var ycsbMeta = workload.Meta{
125126
g := &ycsb{}
126127
g.flags.FlagSet = pflag.NewFlagSet(`ycsb`, pflag.ContinueOnError)
127128
g.flags.Meta = map[string]workload.FlagMeta{
128-
`workload`: {RuntimeOnly: true},
129+
`isolation-level`: {RuntimeOnly: true},
130+
`workload`: {RuntimeOnly: true},
129131
}
132+
g.flags.StringVar(&g.isoLevel, `isolation-level`, ``, `Isolation level to run workload transactions under [serializable, snapshot, read_committed]. If unset, the workload will run with the default isolation level of the database.`)
130133
g.flags.BoolVar(&g.timeString, `time-string`, false, `Prepend field[0-9] data with current time in microsecond precision.`)
131134
g.flags.BoolVar(&g.insertHash, `insert-hash`, true, `Key to be hashed or ordered.`)
132135
g.flags.IntVar(&g.zeroPadding, `zero-padding`, 1, `Key using "insert-hash=false" has zeros padded to left to make this length of digits.`)
@@ -378,6 +381,9 @@ func (g *ycsb) Ops(
378381
if err != nil {
379382
return workload.QueryLoad{}, err
380383
}
384+
if err := workload.SetDefaultIsolationLevel(urls, g.isoLevel); err != nil {
385+
return workload.QueryLoad{}, err
386+
}
381387

382388
const readStmtStr = `SELECT * FROM usertable WHERE ycsb_key = $1`
383389

0 commit comments

Comments
 (0)