-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Akka.Remote: improved write performance with DotNetty flush-batching #4106
Merged
Aaronontheweb
merged 26 commits into
akkadotnet:dev
from
Aaronontheweb:dotnetty-batching
Jan 21, 2020
Merged
Changes from 2 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
d4944bc
adding DotNetty write batching support
Aaronontheweb 360e816
DotNetty flush-batching (performance)
Aaronontheweb 0b474e6
stash
Aaronontheweb a91cf3e
Added batch triggers for byte size too
Aaronontheweb 10f41d7
use max frame size to determine flush limit
Aaronontheweb 25cff73
rewrote ToByteBuffer method as static with inlining
Aaronontheweb 9a96ca6
Merge branch 'dev' into dotnetty-batching
Aaronontheweb 51b83a8
Merge branch 'dev' into dotnetty-batching
Aaronontheweb 0794b06
made sure to push write down the pipeline before flush
Aaronontheweb ebe1a0d
Merge branch 'dev' into dotnetty-batching
Aaronontheweb eb8d4c0
Merge branch 'dev' into dotnetty-batching
Aaronontheweb 9df4536
Merge branch 'dev' into dotnetty-batching
Aaronontheweb 50faacd
Merge branch 'dev' into dotnetty-batching
Aaronontheweb a786bc4
Merge branch 'dev' into dotnetty-batching
Aaronontheweb eebca2d
changed semantics around how BatchWriter behaves under low traffic
Aaronontheweb 01754f3
added comment explaining why we call WriteAsync first before flush math
Aaronontheweb 5bbe95b
handle termination-flush internally
Aaronontheweb 25ba123
adding some tests to the batch writer
Aaronontheweb 61d24a9
fixed batching test
Aaronontheweb 95c6207
fixed issues with DotNetty batching spec
Aaronontheweb b1f0070
don't use built-in mechanisms to determine scheduling
Aaronontheweb 6fe16f1
debugging RemoteDeliverySpec
Aaronontheweb 82e50a6
added BatchWriterSettings class
Aaronontheweb 18dcce6
added HOCON configuration for tuning the batching system
Aaronontheweb 0384346
disable batching inside problematic Akka.Remote tests
Aaronontheweb 0944a15
fix C#7 compilation issue
Aaronontheweb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -8,14 +8,93 @@ | |
using System; | ||
using System.Net; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Akka.Util; | ||
using DotNetty.Buffers; | ||
using DotNetty.Common.Concurrency; | ||
using DotNetty.Transport.Channels; | ||
using ILoggingAdapter = Akka.Event.ILoggingAdapter; | ||
|
||
namespace Akka.Remote.Transport.DotNetty | ||
{ | ||
internal class BatchWriter : ChannelHandlerAdapter | ||
{ | ||
private readonly int _maxPendingWrites; | ||
private readonly long _maxPendingMillis; | ||
|
||
public BatchWriter(int maxPendingWrites = 30, long maxPendingMillis = 40l) | ||
{ | ||
_maxPendingWrites = maxPendingWrites; | ||
_maxPendingMillis = maxPendingMillis; | ||
} | ||
|
||
private int _currentPendingWrites = 0; | ||
|
||
public bool HasPendingWrites => _currentPendingWrites > 0; | ||
|
||
public override void HandlerAdded(IChannelHandlerContext context) | ||
{ | ||
ScheduleFlush(context); | ||
base.HandlerAdded(context); | ||
} | ||
|
||
public override Task WriteAsync(IChannelHandlerContext context, object message) | ||
{ | ||
var write = base.WriteAsync(context, message); | ||
if (++_currentPendingWrites == _maxPendingWrites) | ||
{ | ||
context.Flush(); | ||
Reset(); | ||
} | ||
|
||
return write; | ||
} | ||
|
||
void ScheduleFlush(IChannelHandlerContext context) | ||
{ | ||
// Schedule a recurring flush - only fires when there's writable data | ||
var time = TimeSpan.FromMilliseconds(_maxPendingMillis); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By default, we check to see if messages need to be flushed every 40ms - this is designed for very low-volume systems where they'll probably never meet the msgCount / max bytes threshold I set earlier. |
||
var task = new FlushTask(context, time, this); | ||
context.Executor.Schedule(task, time); | ||
} | ||
|
||
public void Reset() | ||
{ | ||
_currentPendingWrites = 0; | ||
} | ||
|
||
class FlushTask : IRunnable | ||
{ | ||
private readonly IChannelHandlerContext _context; | ||
private readonly TimeSpan _interval; | ||
private readonly BatchWriter _writer; | ||
|
||
public FlushTask(IChannelHandlerContext context, TimeSpan interval, BatchWriter writer) | ||
{ | ||
_context = context; | ||
_interval = interval; | ||
_writer = writer; | ||
} | ||
|
||
public void Run() | ||
{ | ||
if (_writer.HasPendingWrites) | ||
{ | ||
// execute a flush operation | ||
_context.Flush(); | ||
_writer.Reset(); | ||
} | ||
|
||
// channel is still writing | ||
if (_context.Channel.Active) | ||
{ | ||
_context.Executor.Schedule(this, _interval); // reschedule | ||
} | ||
} | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// INTERNAL API | ||
/// | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
A better design for doing this, potentially: since all of the messages being buffered into the
Channel
are allIByteBuf
, we can compute the length of buffered writes for this socket thus far.Therefore, it might be a better idea for us to change our buffering strategy to flushing when X amount of bytes are pending, rather than counting the number of messages.
That being said - if the write rate is pretty high and the message length is consistently small, we don't want to unnecessarily buffer those for too long either, so we might need a strategy that is more adaptive, based on how the channel is actually used.