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

Akka.Remote: improved write performance with DotNetty flush-batching #4106

Merged
merged 26 commits into from
Jan 21, 2020
Merged
Show file tree
Hide file tree
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 Dec 17, 2019
360e816
DotNetty flush-batching (performance)
Aaronontheweb Dec 17, 2019
0b474e6
stash
Aaronontheweb Dec 18, 2019
a91cf3e
Added batch triggers for byte size too
Aaronontheweb Dec 18, 2019
10f41d7
use max frame size to determine flush limit
Aaronontheweb Dec 18, 2019
25cff73
rewrote ToByteBuffer method as static with inlining
Aaronontheweb Dec 18, 2019
9a96ca6
Merge branch 'dev' into dotnetty-batching
Aaronontheweb Dec 18, 2019
51b83a8
Merge branch 'dev' into dotnetty-batching
Aaronontheweb Dec 18, 2019
0794b06
made sure to push write down the pipeline before flush
Aaronontheweb Dec 18, 2019
ebe1a0d
Merge branch 'dev' into dotnetty-batching
Aaronontheweb Dec 20, 2019
eb8d4c0
Merge branch 'dev' into dotnetty-batching
Aaronontheweb Dec 20, 2019
9df4536
Merge branch 'dev' into dotnetty-batching
Aaronontheweb Dec 21, 2019
50faacd
Merge branch 'dev' into dotnetty-batching
Aaronontheweb Jan 13, 2020
a786bc4
Merge branch 'dev' into dotnetty-batching
Aaronontheweb Jan 20, 2020
eebca2d
changed semantics around how BatchWriter behaves under low traffic
Aaronontheweb Jan 20, 2020
01754f3
added comment explaining why we call WriteAsync first before flush math
Aaronontheweb Jan 20, 2020
5bbe95b
handle termination-flush internally
Aaronontheweb Jan 20, 2020
25ba123
adding some tests to the batch writer
Aaronontheweb Jan 21, 2020
61d24a9
fixed batching test
Aaronontheweb Jan 21, 2020
95c6207
fixed issues with DotNetty batching spec
Aaronontheweb Jan 21, 2020
b1f0070
don't use built-in mechanisms to determine scheduling
Aaronontheweb Jan 21, 2020
6fe16f1
debugging RemoteDeliverySpec
Aaronontheweb Jan 21, 2020
82e50a6
added BatchWriterSettings class
Aaronontheweb Jan 21, 2020
18dcce6
added HOCON configuration for tuning the batching system
Aaronontheweb Jan 21, 2020
0384346
disable batching inside problematic Akka.Remote tests
Aaronontheweb Jan 21, 2020
0944a15
fix C#7 compilation issue
Aaronontheweb Jan 21, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions src/core/Akka.Remote/Transport/DotNetty/AkkaLoggingHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Member Author

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 all IByteBuf, 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.

{
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);
Copy link
Member Author

Choose a reason for hiding this comment

The 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
///
Expand Down
2 changes: 2 additions & 0 deletions src/core/Akka.Remote/Transport/DotNetty/DotNettyTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,8 @@ private void SetInitialChannelPipeline(IChannel channel)
pipeline.AddLast("FrameEncoder", new LengthFieldPrepender(Settings.ByteOrder, 4, 0, false));
}
}

pipeline.AddLast("BatchWriter", new BatchWriter());
}

private void SetClientPipeline(IChannel channel, Address remoteAddress)
Expand Down
4 changes: 2 additions & 2 deletions src/core/Akka.Remote/Transport/DotNetty/TcpTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,10 @@ public TcpAssociationHandle(Address localAddress, Address remoteAddress, DotNett

public override bool Write(ByteString payload)
{
if (_channel.Open && _channel.IsWritable)
if (_channel.Open)
{
var data = ToByteBuffer(payload);
_channel.WriteAndFlushAsync(data);
_channel.WriteAsync(data);
return true;
}
return false;
Expand Down