-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
storage: use a "distinct" batch in Replica.handleRaftReady() #7015
storage: use a "distinct" batch in Replica.handleRaftReady() #7015
Conversation
LGTM |
var err error | ||
if lastIndex, err = r.applySnapshot(batch, rd.Snapshot); err != nil { | ||
return err | ||
} | ||
// TODO(bdarnell): update coalesced heartbeat mapping with snapshot info. | ||
} | ||
// We know that all of the writes from here forward will be to distinct keys. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't the right thing to require here that none of the keys written have previously been mutated in the batch? Reads get passed through to the underlying engine (not the batch). We definitely mutate a lot of keys when we apply a snapshot, conceivably the hard state, last index, ... (maybe we don't currently, but putting this Distinct()
business in for minor gains seems like a good recipe for a future two-day debugging session).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was curious about this. Would it be more palatable to only enable this usage of a "distinct" batch if a snapshot isn't being applied?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I think so (though of course ideally we also want to use Distinct()
in applySnapshot). I'm still somewhat uncomfortable with the semantics of Distinct()
- they essentially require you to know about all prior writes to the batch. Remind me, why didn't we make Distinct()
read from the underlying batch? It was because that opened up new questions about what would happen if the batch was written to while a Distinct batch was also active?
I'm thinking maybe the best way to go about it would be making the batch inactive as long as it has an open Distinct()
batch (i.e. a write to the underlying batch would fail), and making distinctBatch.Commit()
do nothing but flush to the underlying batch (which would then have to be committed)? The upshot would be that Distinct()
wouldn't have to worry about what happened to the batch earlier, and that use of the original batch is disallowed until we're done with Distinct()
mode. I think that's congruent with our usage and avoids the weird global perspective that's currently required for reasoning about when Distinct()
applies.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My recollection is that distinctBatch
has its current semantics so that you didn't have to recall when you created it to know what writes it would see. That is simple at one level, but as you've noted it adds cognitive complexity at another. Let me take a look at making the change you suggest. I don't think it will be difficult.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, this was mostly straightforward except for one large gotcha: the current usage of Distinct()
within Replica.{Put,ConditionalPut}
would now cause a flush on every request which completely destroys the distinct optimization.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, got it figured out. I've added another commit which changes the semantics of Distinct()
. PTAL.
28c333f
to
07c227d
Compare
. Do you see any chances to consolidate some of the interplay between
|
|
A minor optimization that affects small transactions. Fixes cockroachdb#6949. name old time/op new time/op delta Insert1_Cockroach-8 212µs ± 2% 204µs ± 1% -3.55% (p=0.000 n=10+10) Update1_Cockroach-8 356µs ± 2% 343µs ± 1% -3.49% (p=0.000 n=10+9) Delete1_Cockroach-8 390µs ± 2% 389µs ± 2% ~ (p=0.529 n=10+10) TrackChoices1_Cockroach-8 280µs ± 1% 269µs ± 1% -3.61% (p=0.000 n=10+10)
A distinct batch will now read previous writes made to the parent batch, but won't read its own writes. Also make it illegal to use the parent batch while the distinct batch is open.
07c227d
to
4246eba
Compare
Review status: all files reviewed at latest revision, 2 unresolved discussions, all commit checks successful. storage/replica.go, line 1424 [r1] (raw file):
|
As you say,
|
A minor optimization that affects small transactions.
Fixes #6949.
This change is