forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
41958: storage/engine: make pebbleTimeBoundPropCollector extract intent timestamps r=itsbilal a=petermattis Fixes cockroachdb#41900 Release note: None Co-authored-by: Peter Mattis <petermattis@gmail.com>
- Loading branch information
Showing
3 changed files
with
192 additions
and
8 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// Copyright 2019 The Cockroach Authors. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the file licenses/BSL.txt. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0, included in the file | ||
// licenses/APL.txt. | ||
|
||
package engine | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"sort" | ||
"strconv" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/storage/engine/enginepb" | ||
"github.com/cockroachdb/cockroach/pkg/util/hlc" | ||
"github.com/cockroachdb/cockroach/pkg/util/leaktest" | ||
"github.com/cockroachdb/cockroach/pkg/util/protoutil" | ||
"github.com/cockroachdb/datadriven" | ||
"github.com/cockroachdb/pebble" | ||
) | ||
|
||
func TestPebbleTimeBoundPropCollector(t *testing.T) { | ||
defer leaktest.AfterTest(t)() | ||
|
||
datadriven.RunTest(t, "testdata/time_bound_props", func(d *datadriven.TestData) string { | ||
c := &pebbleTimeBoundPropCollector{} | ||
switch d.Cmd { | ||
case "build": | ||
for _, line := range strings.Split(d.Input, "\n") { | ||
parts := strings.Fields(line) | ||
if len(parts) != 2 { | ||
return fmt.Sprintf("malformed line: %s, expected: <key>/<timestamp> <value>", line) | ||
} | ||
keyParts := strings.Split(parts[0], "/") | ||
if len(keyParts) != 2 { | ||
return fmt.Sprintf("malformed key: %s, expected: <key>/<timestamp>", parts[0]) | ||
} | ||
|
||
key := []byte(keyParts[0]) | ||
timestamp, err := strconv.Atoi(keyParts[1]) | ||
if err != nil { | ||
return err.Error() | ||
} | ||
ikey := pebble.InternalKey{ | ||
UserKey: EncodeKey(MVCCKey{ | ||
Key: key, | ||
Timestamp: hlc.Timestamp{WallTime: int64(timestamp)}, | ||
}), | ||
} | ||
|
||
value := []byte(parts[1]) | ||
if timestamp == 0 { | ||
if n, err := fmt.Sscanf(string(value), "timestamp=%d", ×tamp); err != nil { | ||
return err.Error() | ||
} else if n != 1 { | ||
return fmt.Sprintf("malformed txn timestamp: %s, expected timestamp=<value>", value) | ||
} | ||
meta := &enginepb.MVCCMetadata{} | ||
meta.Timestamp.WallTime = int64(timestamp) | ||
meta.Txn = &enginepb.TxnMeta{} | ||
var err error | ||
value, err = protoutil.Marshal(meta) | ||
if err != nil { | ||
return err.Error() | ||
} | ||
} | ||
|
||
if err := c.Add(ikey, value); err != nil { | ||
return err.Error() | ||
} | ||
} | ||
|
||
// Retrieve the properties and sort them for test determinism. | ||
m := make(map[string]string) | ||
if err := c.Finish(m); err != nil { | ||
return err.Error() | ||
} | ||
var keys []string | ||
for k := range m { | ||
keys = append(keys, k) | ||
} | ||
sort.Strings(keys) | ||
|
||
var buf bytes.Buffer | ||
for _, k := range keys { | ||
fmt.Fprintf(&buf, "%s: %x\n", k, m[k]) | ||
} | ||
return buf.String() | ||
|
||
default: | ||
return fmt.Sprintf("unknown command: %s", d.Cmd) | ||
} | ||
}) | ||
} |
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,46 @@ | ||
# build | ||
# key/0 timestamp=value | ||
# key/timestamp value | ||
# ... | ||
# ---- | ||
|
||
build | ||
a/2 b | ||
a/1 c | ||
---- | ||
crdb.ts.max: 0000000000000002 | ||
crdb.ts.min: 0000000000000001 | ||
|
||
build | ||
a/0 timestamp=2 | ||
a/2 b | ||
a/1 c | ||
---- | ||
crdb.ts.max: 0000000000000002 | ||
crdb.ts.min: 0000000000000001 | ||
|
||
build | ||
a/0 timestamp=2 | ||
---- | ||
crdb.ts.max: 0000000000000002 | ||
crdb.ts.min: 0000000000000002 | ||
|
||
# Note that this case should never happen. We can't have an intent on | ||
# "a" without a corresponding timestamped value. | ||
|
||
build | ||
a/0 timestamp=2 | ||
b/0 timestamp=3 | ||
---- | ||
crdb.ts.max: 0000000000000003 | ||
crdb.ts.min: 0000000000000003 | ||
|
||
# Note that this case should never happen. The timestamp in the intent | ||
# on "a" should match the subsequent timestamped value. | ||
|
||
build | ||
a/0 timestamp=3 | ||
a/1 b | ||
---- | ||
crdb.ts.max: 0000000000000001 | ||
crdb.ts.min: 0000000000000001 |