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.
storage: add min proposal timestamp tracker
Implement the minimum proposal timestamp tracker outlined in the [follower reads RFC]. The implementation will likely need follow-up work to reduce lock contention. The intended usage will place a call to Track and to the returned closure into the write path. Touches cockroachdb#16593. Release note: None [follower reads RFC]: cockroachdb#26362
- Loading branch information
Showing
4 changed files
with
807 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Copyright 2018 The Cockroach Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
// implied. See the License for the specific language governing | ||
// permissions and limitations under the License. | ||
|
||
// Package minprop exports a main data structure, Tracker, which checkpoints | ||
// closed timestamps and associated Raft Lease Applied indexes positions for | ||
// which (under additional conditions) it is legal to serve follower reads. It | ||
// does so by maintaining a 'next' timestamp above which new command evaluations | ||
// are forced, and by tracking when all in-flight evaluations below this | ||
// timestamp have completed (at which point a call to the Close method succeeds: | ||
// 'next' becomes closed, and a new 'next' is initialized with a future | ||
// timestamp). | ||
// | ||
// In-flight command evaluations are tracked via the Track method which acquires | ||
// a reference with the tracker, returns a minimum timestamp to be used for the | ||
// proposal evaluation, and provides a closure that releases the reference with | ||
// a lease applied index used for the proposal. | ||
package minprop |
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,212 @@ | ||
// Copyright 2018 The Cockroach Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
// implied. See the License for the specific language governing | ||
// permissions and limitations under the License. | ||
|
||
package minprop | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/cockroachdb/cockroach/pkg/util/hlc" | ||
) | ||
|
||
func Example() { | ||
ctx := context.TODO() | ||
|
||
tracker := NewTracker() | ||
|
||
fmt.Println("The newly initialized tracker has a zero closed timestamp:") | ||
fmt.Println(tracker) | ||
|
||
fmt.Println("A first command arrives on range 12 (though the range isn't known yet to the Tracker).") | ||
ts, done1 := tracker.Track(ctx) | ||
fmt.Println("All commands initially start out on the right. The command has its timestamp forwarded to", ts, ".") | ||
fmt.Println(tracker) | ||
|
||
fmt.Println("Two more commands arrive, on r1 and r12.") | ||
_, done2 := tracker.Track(ctx) | ||
_, done3 := tracker.Track(ctx) | ||
fmt.Println(tracker) | ||
|
||
fmt.Println("The command on r1 finishes evaluating at Lease Applied Index 10 and lets the Tracker know.") | ||
done2(ctx, 1, 10) | ||
fmt.Println(tracker) | ||
|
||
fmt.Println("The command on r12 also finishes quickly, at LAI 77.") | ||
done3(ctx, 12, 77) | ||
fmt.Println(tracker) | ||
|
||
fmt.Println("The system closes out a timestamp (registering 1000 as the next timestamp to close out).") | ||
closed1, mlai1 := tracker.Close(hlc.Timestamp{WallTime: 1E9}) | ||
fmt.Println("No problem: nothing is tracked on the left side; returns:", closed1, "and", mlai1) | ||
fmt.Println("Note how the items on the right have moved to the left, as they are relevant for the") | ||
fmt.Println("next call to Close.") | ||
fmt.Println(tracker) | ||
|
||
fmt.Println("Nothing happens for a while until the system tries to close out the next timestamp.") | ||
fmt.Println("However, the very first proposal is still tracked and blocks progress.") | ||
closed2, mlai2 := tracker.Close(hlc.Timestamp{WallTime: 2E9}) | ||
fmt.Println("The call returns a no-op in the form", closed2, mlai2, ".") | ||
fmt.Println(tracker) | ||
|
||
ts4, done4 := tracker.Track(ctx) | ||
fmt.Println("A new command gets tracked on r12 (and is forwarded to", ts4, "(if necessary).") | ||
fmt.Println("It terminates quickly, leaving an MLAI entry of 78 behind.") | ||
done4(ctx, 12, 78) | ||
fmt.Println(tracker) | ||
|
||
fmt.Println("Finally! The slow evaluation finishes and the command gets proposed at index 79.") | ||
fmt.Println("Note that the right now tracks a smaller value of 78. Consumers have to keep the") | ||
fmt.Println("maximum they've seen.") | ||
done1(ctx, 12, 79) | ||
fmt.Println(tracker) | ||
|
||
closed3, mlai3 := tracker.Close(hlc.Timestamp{WallTime: 3E9}) | ||
fmt.Println("The next call to Close() is successful and returns:", closed3, "and", mlai3) | ||
fmt.Println(tracker) | ||
|
||
// Output: | ||
// The newly initialized tracker has a zero closed timestamp: | ||
// | ||
// closed next | ||
// | left | right | ||
// | 0 # 0 | ||
// v v | ||
// ---------------------------------------------------------> time | ||
// | ||
// closed = 0.000000000,0 | ||
// next = 0.000000000,1 | ||
// | ||
// A first command arrives on range 12 (though the range isn't known yet to the Tracker). | ||
// All commands initially start out on the right. The command has its timestamp forwarded to 0.000000000,2 . | ||
// | ||
// closed next | ||
// | left | right | ||
// | 0 # 1 | ||
// v v | ||
// ---------------------------------------------------------> time | ||
// | ||
// closed = 0.000000000,0 | ||
// next = 0.000000000,1 | ||
// | ||
// Two more commands arrive, on r1 and r12. | ||
// | ||
// closed next | ||
// | left | right | ||
// | 0 # 3 | ||
// v v | ||
// ---------------------------------------------------------> time | ||
// | ||
// closed = 0.000000000,0 | ||
// next = 0.000000000,1 | ||
// | ||
// The command on r1 finishes evaluating at Lease Applied Index 10 and lets the Tracker know. | ||
// | ||
// closed next | ||
// | left | right | ||
// | 0 # 2 | ||
// | @ 10 (r1) | ||
// v v | ||
// ---------------------------------------------------------> time | ||
// | ||
// closed = 0.000000000,0 | ||
// next = 0.000000000,1 | ||
// | ||
// The command on r12 also finishes quickly, at LAI 77. | ||
// | ||
// closed next | ||
// | left | right | ||
// | 0 # 1 | ||
// | @ 10 (r1) | ||
// | @ 77 (r12) | ||
// v v | ||
// ---------------------------------------------------------> time | ||
// | ||
// closed = 0.000000000,0 | ||
// next = 0.000000000,1 | ||
// | ||
// The system closes out a timestamp (registering 1000 as the next timestamp to close out). | ||
// No problem: nothing is tracked on the left side; returns: 0.000000000,1 and map[] | ||
// Note how the items on the right have moved to the left, as they are relevant for the | ||
// next call to Close. | ||
// | ||
// closed next | ||
// | left | right | ||
// | 1 # 0 | ||
// | 10 @ (r1) | ||
// | 77 @ (r12) | ||
// v v | ||
// ---------------------------------------------------------> time | ||
// | ||
// closed = 0.000000000,1 | ||
// next = 1.000000000,0 | ||
// | ||
// Nothing happens for a while until the system tries to close out the next timestamp. | ||
// However, the very first proposal is still tracked and blocks progress. | ||
// The call returns a no-op in the form 0.000000000,1 map[] . | ||
// | ||
// closed next | ||
// | left | right | ||
// | 1 # 0 | ||
// | 10 @ (r1) | ||
// | 77 @ (r12) | ||
// v v | ||
// ---------------------------------------------------------> time | ||
// | ||
// closed = 0.000000000,1 | ||
// next = 1.000000000,0 | ||
// | ||
// A new command gets tracked on r12 (and is forwarded to 1.000000000,1 (if necessary). | ||
// It terminates quickly, leaving an MLAI entry of 78 behind. | ||
// | ||
// closed next | ||
// | left | right | ||
// | 1 # 0 | ||
// | 10 @ (r1) | ||
// | 77 @ (r12) | ||
// | @ 78 (r12) | ||
// v v | ||
// ---------------------------------------------------------> time | ||
// | ||
// closed = 0.000000000,1 | ||
// next = 1.000000000,0 | ||
// | ||
// Finally! The slow evaluation finishes and the command gets proposed at index 79. | ||
// Note that the right now tracks a smaller value of 78. Consumers have to keep the | ||
// maximum they've seen. | ||
// | ||
// closed next | ||
// | left | right | ||
// | 0 # 0 | ||
// | 10 @ (r1) | ||
// | @ 78 (r12) | ||
// | 79 @ (r12) | ||
// v v | ||
// ---------------------------------------------------------> time | ||
// | ||
// closed = 0.000000000,1 | ||
// next = 1.000000000,0 | ||
// | ||
// The next call to Close() is successful and returns: 1.000000000,0 and map[1:10 12:79] | ||
// | ||
// closed next | ||
// | left | right | ||
// | 0 # 0 | ||
// | 78 @ (r12) | ||
// v v | ||
// ---------------------------------------------------------> time | ||
// | ||
// closed = 1.000000000,0 | ||
// next = 3.000000000,0 | ||
} |
Oops, something went wrong.