Skip to content

Durable backend for Distributed Data collections #2490

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

Merged
merged 23 commits into from
Apr 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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: 2 additions & 1 deletion build.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,8 @@ Target "MultiNodeTests" <| fun _ ->
let multiNodeTestAssemblies = !! "src/**/bin/Release/Akka.Remote.Tests.MultiNode.dll" ++
"src/**/bin/Release/Akka.Cluster.Tests.MultiNode.dll" ++
"src/**/bin/Release/Akka.Cluster.Tools.Tests.MultiNode.dll" ++
"src/**/bin/Release/Akka.Cluster.Sharding.Tests.MultiNode.dll"
"src/**/bin/Release/Akka.Cluster.Sharding.Tests.MultiNode.dll" ++
"src/**/bin/Release/Akka.DistributedData.Tests.MultiNode.dll"

printfn "Using MultiNodeTestRunner: %s" multiNodeTestPath

Expand Down
18 changes: 18 additions & 0 deletions src/Akka.sln
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,17 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Akka.MultiNodeTestRunner.Sh
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Protobuf", "Protobuf", "{98CEB6FB-A277-42FA-A98E-99A8640B26FD}"
ProjectSection(SolutionItems) = preProject
core\protobuf\ClusterClientMessages.proto = core\protobuf\ClusterClientMessages.proto
core\protobuf\ClusterMessages.proto = core\protobuf\ClusterMessages.proto
core\protobuf\ClusterShardingMessages.proto = core\protobuf\ClusterShardingMessages.proto
core\protobuf\ContainerFormats.proto = core\protobuf\ContainerFormats.proto
core\protobuf\csharp_options.proto = core\protobuf\csharp_options.proto
core\protobuf\descriptor.proto = core\protobuf\descriptor.proto
core\protobuf\DistributedPubSubMessages.proto = core\protobuf\DistributedPubSubMessages.proto
core\protobuf\PersistenceMessages.proto = core\protobuf\PersistenceMessages.proto
core\protobuf\ReplicatedDataMessages.proto = core\protobuf\ReplicatedDataMessages.proto
core\protobuf\ReplicatorMessages.proto = core\protobuf\ReplicatorMessages.proto
core\protobuf\TestConductorProtocol.proto = core\protobuf\TestConductorProtocol.proto
core\protobuf\WireFormats.proto = core\protobuf\WireFormats.proto
EndProjectSection
EndProject
Expand Down Expand Up @@ -260,6 +267,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Transports", "Transports",
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Akka.Remote.Transport.Helios", "contrib\transports\Akka.Remote.Transport.Helios\Akka.Remote.Transport.Helios.csproj", "{29FEAABC-E326-450A-9008-B5FCECF0115F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Akka.DistributedData.LightningDB", "contrib\cluster\Akka.DistributedData.LightningDB\Akka.DistributedData.LightningDB.csproj", "{26103500-0DFE-498D-8FCF-EFE66BB4FD69}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug Mono|Any CPU = Debug Mono|Any CPU
Expand Down Expand Up @@ -981,6 +990,14 @@ Global
{29FEAABC-E326-450A-9008-B5FCECF0115F}.Release Mono|Any CPU.Build.0 = Release|Any CPU
{29FEAABC-E326-450A-9008-B5FCECF0115F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{29FEAABC-E326-450A-9008-B5FCECF0115F}.Release|Any CPU.Build.0 = Release|Any CPU
{26103500-0DFE-498D-8FCF-EFE66BB4FD69}.Debug Mono|Any CPU.ActiveCfg = Debug|Any CPU
{26103500-0DFE-498D-8FCF-EFE66BB4FD69}.Debug Mono|Any CPU.Build.0 = Debug|Any CPU
{26103500-0DFE-498D-8FCF-EFE66BB4FD69}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{26103500-0DFE-498D-8FCF-EFE66BB4FD69}.Debug|Any CPU.Build.0 = Debug|Any CPU
{26103500-0DFE-498D-8FCF-EFE66BB4FD69}.Release Mono|Any CPU.ActiveCfg = Release|Any CPU
{26103500-0DFE-498D-8FCF-EFE66BB4FD69}.Release Mono|Any CPU.Build.0 = Release|Any CPU
{26103500-0DFE-498D-8FCF-EFE66BB4FD69}.Release|Any CPU.ActiveCfg = Release|Any CPU
{26103500-0DFE-498D-8FCF-EFE66BB4FD69}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -1099,5 +1116,6 @@ Global
{E3549E17-7206-44F0-A322-EDC058A0498F} = {0E55F1F8-E212-43D7-A0C0-ACEA9794B0D7}
{6AD34FA4-7584-454D-ACD4-A8911E0BDB00} = {588C1513-FAB6-42C3-B6FC-3485F13620CF}
{29FEAABC-E326-450A-9008-B5FCECF0115F} = {6AD34FA4-7584-454D-ACD4-A8911E0BDB00}
{26103500-0DFE-498D-8FCF-EFE66BB4FD69} = {76F58DC4-19F1-43EF-A6E2-EC1CC8395AC5}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{26103500-0DFE-498D-8FCF-EFE66BB4FD69}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Akka.DistributedData.LightningDB</RootNamespace>
<AssemblyName>Akka.DistributedData.LightningDB</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="LightningDB, Version=0.9.7.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\LightningDB.0.9.7\lib\net45\LightningDB.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
<Reference Include="System.Collections.Immutable, Version=1.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\..\..\packages\System.Collections.Immutable.1.3.1\lib\portable-net45+win8+wp8+wpa81\System.Collections.Immutable.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="LmdbDurableStore.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\core\Akka.Cluster\Akka.Cluster.csproj">
<Project>{6ab00f61-269a-4501-b06a-026707f000a7}</Project>
<Name>Akka.Cluster</Name>
</ProjectReference>
<ProjectReference Include="..\..\..\core\Akka\Akka.csproj">
<Project>{5deddf90-37f0-48d3-a0b0-a5cbd8a7e377}</Project>
<Name>Akka</Name>
</ProjectReference>
<ProjectReference Include="..\Akka.DistributedData\Akka.DistributedData.csproj">
<Project>{59cffc88-8a73-445d-b191-281e40be9421}</Project>
<Name>Akka.DistributedData</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<EmbeddedResource Include="reference.conf" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
#region copyright
// -----------------------------------------------------------------------
// <copyright file="LmdbDurableStore.cs" company="Akka.NET project">
// Copyright (C) 2009-2016 Lightbend Inc. <http://www.lightbend.com>
// Copyright (C) 2013-2017 Akka.NET project <https://github.com/akkadotnet>
// </copyright>
// -----------------------------------------------------------------------
#endregion

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Text;
using Akka.Actor;
using Akka.Configuration;
using Akka.DistributedData.Durable;
using Akka.Event;
using Akka.Serialization;
using LightningDB;

namespace Akka.DistributedData.LightningDB
Copy link
Contributor Author

Choose a reason for hiding this comment

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

JVM version by default uses LMDB backend for storage. I think, it's better to move that dependency to a separate package however.

{
/// <summary>
/// An actor implementing the durable store for the Distributed Data <see cref="Replicator"/>
/// has to implement the protocol with the messages defined here.
///
/// At startup the <see cref="Replicator"/> creates the durable store actor and sends the
/// <see cref="LoadAll"/> message to it. It must then reply with 0 or more <see cref="LoadData"/> messages
/// followed by one <see cref="LoadAllCompleted"/> message to the <see cref="IActorContext.Sender"/> (the <see cref="Replicator"/>).
///
/// If the <see cref="LoadAll"/> fails it can throw <see cref="LoadFailedException"/> and the <see cref="Replicator"/> supervisor
/// will stop itself and the durable store.
///
/// When the <see cref="Replicator"/> needs to store a value it sends a <see cref="Store"/> message
/// to the durable store actor, which must then reply with the <see cref="StoreReply.SuccessMessage"/> or
/// <see cref="StoreReply.FailureMessage"/> to the <see cref="StoreReply.ReplyTo"/>.
/// </summary>
public sealed class LmdbDurableStore : ReceiveActor
{
public const string DatabaseName = "ddata";
private sealed class WriteBehind
{
public static readonly WriteBehind Instance = new WriteBehind();
private WriteBehind() { }
}

public static Actor.Props Props(Config config) => Actor.Props.Create(() => new LmdbDurableStore(config));

private readonly TimeSpan _writeBehindInterval;
private readonly Dictionary<string, DurableDataEnvelope> _pending = new Dictionary<string, DurableDataEnvelope>();
private readonly LightningEnvironment _environment;
private readonly ILoggingAdapter _log;
private readonly Serializer _serializer;

public LmdbDurableStore(Config config)
{
config = config.GetConfig("lmdb");
if (config == null) throw new ArgumentException("Coudln't find config for LMDB durable store. Default path: `akka.cluster.distributed-data.durable.lmdb`");

_log = Context.GetLogger();

_writeBehindInterval = config.GetString("write-behind-interval") == "off"
? TimeSpan.Zero : config.GetTimeSpan("write-behind-interval");

var mapSize = config.GetByteSize("map-size");
var dirPath = config.GetString("dir");
if (dirPath.EndsWith("ddata"))
{
dirPath = $"path-{Context.System.Name}-{Self.Path.Parent.Name}-{Cluster.Cluster.Get(Context.System).SelfAddress.Port}";
}

if (!Directory.Exists(dirPath))
{
Directory.CreateDirectory(dirPath);
}

_environment = new LightningEnvironment(dirPath, new EnvironmentConfiguration
{
MapSize = mapSize ?? (100 * 1024 * 1024),
MaxDatabases = 1
});
_environment.Open(EnvironmentOpenFlags.NoLock);

using (var tx = _environment.BeginTransaction())
using (var db = tx.OpenDatabase(configuration: new DatabaseConfiguration { Flags = DatabaseOpenFlags.Create }))
{
// just create
}

_serializer = Context.System.Serialization.FindSerializerForType(typeof(DurableDataEnvelope));

Init();
}

protected override void PostStop()
{
base.PostStop();
DoWriteBehind();
//_db.Dispose();
_environment.Dispose();
}

protected override void PostRestart(Exception reason)
{
base.PostRestart(reason);
// Load is only done on first start, not on restart
Become(Active);
}

private void Active()
{
Receive<Store>(store =>
{
var reply = store.Reply;

try
{
if (_writeBehindInterval == TimeSpan.Zero)
{
using (var tx = _environment.BeginTransaction())
using (var db = tx.OpenDatabase())
{
DbPut(tx, db, store.Key, store.Data);
tx.Commit();
}
}
else
{
if (_pending.Count > 0)
Context.System.Scheduler.ScheduleTellOnce(_writeBehindInterval, Self, WriteBehind.Instance, ActorRefs.NoSender);
_pending[store.Key] = store.Data;
}

reply?.ReplyTo.Tell(reply.SuccessMessage);
}
catch (Exception cause)
{
_log.Error(cause, "Failed to store [{0}]", store.Key);
reply?.ReplyTo.Tell(reply.FailureMessage);
}
});
Receive<WriteBehind>(_ => DoWriteBehind());
}

private void Init()
{
Receive<LoadAll>(loadAll =>
{
var t0 = System.DateTime.UtcNow;
using (var tx = _environment.BeginTransaction(TransactionBeginFlags.ReadOnly))
using (var db = tx.OpenDatabase())
using (var cursor = tx.CreateCursor(db))
{
try
{
var n = 0;
var builder = ImmutableDictionary<string, DurableDataEnvelope>.Empty.ToBuilder();
foreach (var entry in cursor)
{
n++;
var key = Encoding.UTF8.GetString(entry.Key);
var envelope = (DurableDataEnvelope)_serializer.FromBinary(entry.Value, typeof(DurableDataEnvelope));
builder.Add(new KeyValuePair<string, DurableDataEnvelope>(key, envelope));
}

if (builder.Count > 0)
{
var loadData = new LoadData(builder.ToImmutable());
Sender.Tell(loadData);
}

Sender.Tell(LoadAllCompleted.Instance);

if (_log.IsDebugEnabled)
_log.Debug("Load all of [{0}] entries took [{1}]", n, DateTime.UtcNow - t0);

Become(Active);
}
catch (Exception e)
{
throw new LoadFailedException("failed to load durable distributed-data", e);
}
}
});
}

private void DbPut(LightningTransaction tx, LightningDatabase db, string key, DurableDataEnvelope data)
{
var byteKey = Encoding.UTF8.GetBytes(key);
var byteValue = _serializer.ToBinary(data);
tx.Put(db, byteKey, byteValue);
}

private void DoWriteBehind()
{
if (_pending.Count > 0)
{
var t0 = DateTime.UtcNow;
using (var tx = _environment.BeginTransaction())
using (var db = tx.OpenDatabase())
{
try
{
foreach (var entry in _pending)
{
DbPut(tx, db, entry.Key, entry.Value);
}
tx.Commit();

if (_log.IsDebugEnabled)
{
_log.Debug("store and commit of [{0}] entries took {1} ms", _pending.Count, (DateTime.UtcNow - t0).TotalMilliseconds);
}
}
catch (Exception cause)
{
_log.Error(cause, "failed to store [{0}]", string.Join(", ", _pending.Keys));
tx.Abort();
}
finally
{
_pending.Clear();
}
}
}
}
}
}
Loading