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 9 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
3 changes: 3 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,17 @@
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
{

/// <summary>
/// INTERNAL API
///
Expand Down
104 changes: 104 additions & 0 deletions src/core/Akka.Remote/Transport/DotNetty/BatchWriter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
//-----------------------------------------------------------------------
// <copyright file="BatchWriter.cs" company="Akka.NET Project">
// Copyright (C) 2009-2019 Lightbend Inc. <http://www.lightbend.com>
// Copyright (C) 2013-2019 .NET Foundation <https://github.com/akkadotnet/akka.net>
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Threading.Tasks;
using DotNetty.Buffers;
using DotNetty.Common.Concurrency;
using DotNetty.Transport.Channels;

namespace Akka.Remote.Transport.DotNetty
{
/// <summary>
/// INTERNAL API.
///
/// Responsible for batching socket writes together into fewer sys calls to the socket.
/// </summary>
internal class BatchWriter : ChannelHandlerAdapter
{
private readonly int _maxPendingWrites;
private readonly int _maxPendingMillis;
private readonly int _maxPendingBytes;

public BatchWriter(int maxPendingWrites = 20, int maxPendingMillis = 40, int maxPendingBytes = 128000)
{
_maxPendingWrites = maxPendingWrites;
_maxPendingMillis = maxPendingMillis;
_maxPendingBytes = maxPendingBytes;
}

private int _currentPendingWrites = 0;
private long _currentPendingBytes;

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

Choose a reason for hiding this comment

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

Might want to add a comment here - need to complete the prior WriteAsync call first before we call flush, otherwise the message currently being written may not be included in the flush even though it was counted against `_currentPendingWrites'

_currentPendingBytes += ((IByteBuffer)message).ReadableBytes;
_currentPendingWrites++;
if (_currentPendingWrites >= _maxPendingWrites
|| _currentPendingBytes >= _maxPendingBytes)
{
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);
var task = new FlushTask(context, time, this);
context.Executor.Schedule(task, time);
}

public void Reset()
{
_currentPendingWrites = 0;
_currentPendingBytes = 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
}
}
}
}
}
4 changes: 4 additions & 0 deletions src/core/Akka.Remote/Transport/DotNetty/DotNettyTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,8 @@ public override async Task<bool> Shutdown()
var tasks = new List<Task>();
foreach (var channel in ConnectionGroup)
{
// flush any pending writes first
channel.Flush();
tasks.Add(channel.CloseAsync());
}
var all = Task.WhenAll(tasks);
Expand Down Expand Up @@ -327,6 +329,8 @@ private void SetInitialChannelPipeline(IChannel channel)
pipeline.AddLast("FrameEncoder", new LengthFieldPrepender(Settings.ByteOrder, 4, 0, false));
}
}

pipeline.AddLast("BatchWriter", new BatchWriter(maxPendingBytes:Settings.MaxFrameSize));
}

private void SetClientPipeline(IChannel channel, Address remoteAddress)
Expand Down
12 changes: 8 additions & 4 deletions src/core/Akka.Remote/Transport/DotNetty/TcpTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System;
using System.Net;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using Akka.Actor;
using Akka.Configuration;
Expand Down Expand Up @@ -147,6 +148,7 @@ public override void ChannelActive(IChannelHandlerContext context)
{
InitOutbound(context.Channel, (IPEndPoint)context.Channel.RemoteAddress, null);
base.ChannelActive(context);

}

private void InitOutbound(IChannel channel, IPEndPoint socketAddress, object msg)
Expand All @@ -169,16 +171,17 @@ 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);
var data = ToByteBuffer(_channel, payload);
_channel.WriteAsync(data);
return true;
}
return false;
}

private IByteBuffer ToByteBuffer(ByteString payload)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static IByteBuffer ToByteBuffer(IChannel channel, ByteString payload)
{
//TODO: optimize DotNetty byte buffer usage
// (maybe custom IByteBuffer working directly on ByteString?)
Copy link
Member Author

Choose a reason for hiding this comment

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

Currently, this is the best possible implementation until we support System.Memory in Akka.NET Core. Tested using a bunch of others.

Expand All @@ -188,6 +191,7 @@ private IByteBuffer ToByteBuffer(ByteString payload)

public override void Disassociate()
{
_channel.Flush(); // flush before we close
_channel.CloseAsync();
}
}
Expand Down