Skip to content
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: avoid copying marshalled RaftCommand when encoding #36392

Merged

Conversation

nvanbenschoten
Copy link
Member

Informs #36347.

This change avoids the unnecessary allocation and memory copy present in
Raft command encoding. This extra work is expensive for large commands
like AddSSTable requests. Even for smaller requests, this work was
still a serious problem because it took place under heavily contended
locks. For instance, the encoding in defaultSubmitProposalLocked took
place under the Replica mutex, which serializes all Raft proposals for
a Range. The other two locations fixed took place under the Raft mutex.
While less heavily contended, this was still slowing down the Raft
processing goroutine.

This is a less dramatic version of a change I've been working on. The
full change lifts the slice allocation and most of the RaftCommand proto
marshalling all the way out of defaultSubmitProposalLocked and out of
the Replica.mu critical section. This commit gets us part of the way
there sets the stage for the rest of the change.

Release note: None

@nvanbenschoten nvanbenschoten requested review from ajwerner and a team April 1, 2019 19:30
@cockroach-teamcity
Copy link
Member

This change is Reviewable

@nvanbenschoten
Copy link
Member Author

cc. @dt

Do you have any benchmarks that stress AddSSTable request performance?

Copy link
Contributor

@ajwerner ajwerner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

Reviewed 3 of 3 files at r1.
Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained

Informs cockroachdb#36347.

This change avoids the unnecessary allocation and memory copy present in
Raft command encoding. This extra work is expensive for large commands
like `AddSSTable` requests. Even for smaller requests, this work was
still a serious problem because it took place under heavily contended
locks. For instance, the encoding in `defaultSubmitProposalLocked` took
place under the Replica mutex, which serializes all Raft proposals for
a Range. The other two locations fixed took place under the Raft mutex.
While less heavily contended, this was still slowing down the Raft
processing goroutine.

This is a less dramatic version of a change I've been working on. The
full change lifts the slice allocation and most of the RaftCommand proto
marshalling all the way out of `defaultSubmitProposalLocked` and out of
the `Replica.mu` critical section. This commit gets us part of the way
there sets the stage for the rest of the change.

Release note: None
@nvanbenschoten nvanbenschoten force-pushed the nvanbenschoten/avoidAllocCmd branch from b38c984 to 91abab1 Compare April 1, 2019 20:04
@dt
Copy link
Member

dt commented Apr 1, 2019

I don't think we have any well targeted AddSSTable benchmarks sadly -- you could use BenchmarkClusterRestore in ccl/backupccl as I think restore has the lowest overhead of any addsstable caller, though ImportFixtureTPCC might also work?

@nvanbenschoten
Copy link
Member Author

Nice, this does show up on ImportFixtureTPCC:

name                 old time/op    new time/op    delta
ImportFixtureTPCC-4     5.75s ± 5%     5.50s ± 2%  -4.29%  (p=0.016 n=5+5)

name                 old speed      new speed      delta
ImportFixtureTPCC-4  15.4MB/s ± 5%  16.1MB/s ± 2%  +4.42%  (p=0.016 n=5+5)

cc. @danhhz

@danhhz
Copy link
Contributor

danhhz commented Apr 2, 2019

Nice, this does show up on ImportFixtureTPCC

Hell yes. 🙌 I do have a local branch where I started added benchmarks to understand the costs at various levels of AddSSTable vs just writing the data to disk, etc. I just pushed it to my fork if you want to see if it applies cleanly. https://github.com/cockroachdb/cockroach/compare/master...danhhz:fast_addsstable?expand=1

@nvanbenschoten
Copy link
Member Author

Unfortunately, it didn't rebase cleanly. Are you planning on getting that in any time soon @danhhz?

@nvanbenschoten
Copy link
Member Author

bors r+

@danhhz
Copy link
Contributor

danhhz commented Apr 2, 2019

Unfortunately, it didn't rebase cleanly. Are you planning on getting that in any time soon @danhhz?

It was worth a shot. Thanks for checking. Landing this is unfortunately quite low on my flex friday list right now

craig bot pushed a commit that referenced this pull request Apr 2, 2019
36392: storage: avoid copying marshalled RaftCommand when encoding r=nvanbenschoten a=nvanbenschoten

Informs #36347.

This change avoids the unnecessary allocation and memory copy present in
Raft command encoding. This extra work is expensive for large commands
like `AddSSTable` requests. Even for smaller requests, this work was
still a serious problem because it took place under heavily contended
locks. For instance, the encoding in `defaultSubmitProposalLocked` took
place under the Replica mutex, which serializes all Raft proposals for
a Range. The other two locations fixed took place under the Raft mutex.
While less heavily contended, this was still slowing down the Raft
processing goroutine.

This is a less dramatic version of a change I've been working on. The
full change lifts the slice allocation and most of the RaftCommand proto
marshalling all the way out of `defaultSubmitProposalLocked` and out of
the `Replica.mu` critical section. This commit gets us part of the way
there sets the stage for the rest of the change.

Release note: None

Co-authored-by: Nathan VanBenschoten <[email protected]>
@craig
Copy link
Contributor

craig bot commented Apr 2, 2019

Build succeeded

@craig craig bot merged commit 91abab1 into cockroachdb:master Apr 2, 2019
@nvanbenschoten
Copy link
Member Author

@dt should I push to get this in for 19.1?

@dt
Copy link
Member

dt commented Apr 3, 2019

@nvanbenschoten If you felt so inclined, sure! I don't think mem usage is on our current list of must-resolve 19.1 backfiller so wasn't planning on lobbying you to do so, but everyone likes faster and using less mem, so if you're comfortable backporting it, I would't complain.

@danhhz
Copy link
Contributor

danhhz commented Apr 17, 2019

I did eventually get to merging that branch and ran it on master with and without this PR.

name                                             old time/op    new time/op    delta
ImportWorkload/tpcc/warehouses=1/AddSStable-8       751ms ±25%     710ms ± 6%     ~     (p=1.000 n=5+5)

name                                             old speed      new speed      delta
ImportWorkload/tpcc/warehouses=1/AddSStable-8     113MB/s ±21%   118MB/s ± 6%     ~     (p=1.000 n=5+5)

name                                             old alloc/op   new alloc/op   delta
ImportWorkload/tpcc/warehouses=1/AddSStable-8       395MB ± 0%     311MB ± 0%  -21.24%  (p=0.029 n=4+4)

name                                             old allocs/op  new allocs/op  delta
ImportWorkload/tpcc/warehouses=1/AddSStable-8       9.31k ± 0%     9.29k ± 1%     ~     (p=0.686 n=4+4)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants