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

Adding async support #29

Closed
wants to merge 2 commits into from
Closed
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
Expand All @@ -10,7 +10,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>ZooKeeperNetRecipes.Tests</RootNamespace>
<AssemblyName>ZooKeeperNetRecipes.Tests</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.5.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<FileUpgradeFlags>
</FileUpgradeFlags>
Expand Down Expand Up @@ -43,6 +43,7 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand All @@ -52,6 +53,7 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821, processorArchitecture=MSIL">
Expand Down Expand Up @@ -107,7 +109,7 @@
</BootstrapperPackage>
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
<None Include="App.config"/>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand All @@ -117,4 +119,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>
6 changes: 4 additions & 2 deletions src/dotnet/ZooKeeperNet.Recipes/ZooKeeperNet.Recipes.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
Expand All @@ -10,7 +10,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>ZooKeeperNet.Recipes</RootNamespace>
<AssemblyName>ZooKeeperNet.Recipes</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.5.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<SignAssembly>false</SignAssembly>
<AssemblyOriginatorKeyFile>..\ZooKeeperKey.snk</AssemblyOriginatorKeyFile>
Expand Down Expand Up @@ -45,6 +45,7 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand All @@ -54,6 +55,7 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821, processorArchitecture=MSIL">
Expand Down
5 changes: 1 addition & 4 deletions src/dotnet/ZooKeeperNet.Tests/App.config
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ConsoleAppender" >
<layout type="log4net.Layout.PatternLayout">
Expand Down
18 changes: 10 additions & 8 deletions src/dotnet/ZooKeeperNet.Tests/ZooKeeperNet.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
Expand All @@ -10,7 +10,7 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>ZooKeeperNet.Tests</RootNamespace>
<AssemblyName>ZooKeeperNet.Tests</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.5.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<FileUpgradeFlags>
</FileUpgradeFlags>
Expand Down Expand Up @@ -43,6 +43,7 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand All @@ -52,6 +53,7 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<ItemGroup>
<Reference Include="log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821, processorArchitecture=MSIL">
Expand Down Expand Up @@ -114,11 +116,11 @@
</ProjectReference>
</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>
<!-- 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>
17 changes: 11 additions & 6 deletions src/dotnet/ZooKeeperNet/ClientConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*
*/

using System.Threading.Tasks;

namespace ZooKeeperNet
{
using System;
Expand All @@ -33,7 +35,7 @@ public class ClientConnection : IClientConnection
{
private static readonly ILog LOG = LogManager.GetLogger(typeof(ClientConnection));

private static readonly TimeSpan DefaultConnectTimeout = TimeSpan.FromMilliseconds(500);
private static readonly TimeSpan DefaultConnectTimeout = TimeSpan.FromMilliseconds(500);
private static bool disableAutoWatchReset = false;
private static int maximumPacketLength = 1024 * 1024 * 4;
private static int maximumSpin = 30;
Expand Down Expand Up @@ -342,14 +344,17 @@ public void AddAuthInfo(string scheme, byte[] auth)
QueuePacket(new RequestHeader(-4, (int)OpCode.Auth), null, new AuthPacket(0, scheme, auth), null, null, null, null, null, null);
}

public ReplyHeader SubmitRequest(RequestHeader h, IRecord request, IRecord response, ZooKeeper.WatchRegistration watchRegistration)
public async Task<ReplyHeader> SubmitRequest(RequestHeader h, IRecord request, IRecord response, ZooKeeper.WatchRegistration watchRegistration)
{
ReplyHeader r = new ReplyHeader();
Packet p = QueuePacket(h, r, request, response, null, null, watchRegistration, null, null);

if (!p.WaitUntilFinishedSlim(SessionTimeout))

Task timeoutTask = Task.Delay(SessionTimeout);
if (timeoutTask == await Task.WhenAny(timeoutTask, p.PacketTask).ConfigureAwait(false))
{
throw new TimeoutException(new StringBuilder("The request ").Append(request).Append(" timed out while waiting for a response from the server.").ToString());
throw new TimeoutException(new StringBuilder("The request ").Append(request)
.Append(" timed out while waiting for a response from the server.")
.ToString());
}
return r;
}
Expand All @@ -372,7 +377,7 @@ private void InternalDispose()

try
{
SubmitRequest(new RequestHeader { Type = (int)OpCode.CloseSession }, null, null, null);
SubmitRequest(new RequestHeader { Type = (int)OpCode.CloseSession }, null, null, null).GetAwaiter().GetResult();
SpinWait spin = new SpinWait();
DateTime timeoutAt = DateTime.UtcNow.Add(SessionTimeout);
while (!producer.IsConnectionClosedByServer)
Expand Down
5 changes: 4 additions & 1 deletion src/dotnet/ZooKeeperNet/IClientConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
* limitations under the License.
*
*/

using System.Threading.Tasks;

namespace ZooKeeperNet
{
using System;
Expand Down Expand Up @@ -62,7 +65,7 @@ public interface IClientConnection : IStartable, IDisposable
/// <param name="response">The response.</param>
/// <param name="watchRegistration">The watch registration.</param>
/// <returns></returns>
ReplyHeader SubmitRequest(RequestHeader h, IRecord request, IRecord response, ZooKeeper.WatchRegistration watchRegistration);
Task<ReplyHeader> SubmitRequest(RequestHeader h, IRecord request, IRecord response, ZooKeeper.WatchRegistration watchRegistration);

/// <summary>
/// Queues the packet.
Expand Down
16 changes: 16 additions & 0 deletions src/dotnet/ZooKeeperNet/IZooKeeper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
* limitations under the License.
*
*/

using System.Threading.Tasks;

namespace ZooKeeperNet
{
using System;
Expand Down Expand Up @@ -120,6 +123,7 @@ public interface IZooKeeper
/// <param name="createMode">specifying whether the node to be created is ephemeral and/or sequential.</param>
/// <returns></returns>
string Create(string path, byte[] data, IEnumerable<ACL> acl, CreateMode createMode);
Task<string> CreateAsync(string path, byte[] data, IEnumerable<ACL> acl, CreateMode createMode);

/// <summary>
/// Delete the node with the given path. The call will succeed if such a node
Expand All @@ -142,6 +146,7 @@ public interface IZooKeeper
/// <param name="path">The path.</param>
/// <param name="version">The version.</param>
void Delete(string path, int version);
Task DeleteAsync(string path, int version);

/// <summary>
/// Return the stat of the node of the given path. Return null if no such a
Expand All @@ -156,6 +161,7 @@ public interface IZooKeeper
/// <param name="watcher">The watcher.</param>
/// <returns>the stat of the node of the given path; return null if no such a node exists.</returns>
Stat Exists(string path, IWatcher watcher);
Task<Stat> ExistsAsync(string path, IWatcher watcher);

/// <summary>
/// Return the stat of the node of the given path. Return null if no such a
Expand All @@ -175,6 +181,7 @@ public interface IZooKeeper
/// @throws InterruptedException If the server transaction is interrupted.
/// </summary>
Stat Exists(string path, bool watch);
Task<Stat> ExistsAsync(string path, bool watch);

/// <summary>
/// Return the data and the stat of the node of the given path.
Expand All @@ -195,6 +202,7 @@ public interface IZooKeeper
/// @throws IllegalArgumentException if an invalid path is specified
/// </summary>
byte[] GetData(string path, IWatcher watcher, Stat stat);
Task<byte[]> GetDataAsync(string path, IWatcher watcher, Stat stat);

/// <summary>
/// Return the data and the stat of the node of the given path.
Expand All @@ -214,6 +222,7 @@ public interface IZooKeeper
/// @throws InterruptedException If the server transaction is interrupted.
/// </summary>
byte[] GetData(string path, bool watch, Stat stat);
Task<byte[]> GetDataAsync(string path, bool watch, Stat stat);

/// <summary>
/// Set the data for the node of the given path if such a node exists and the
Expand Down Expand Up @@ -243,6 +252,7 @@ public interface IZooKeeper
/// @throws IllegalArgumentException if an invalid path is specified
/// </summary>
Stat SetData(string path, byte[] data, int version);
Task<Stat> SetDataAsync(string path, byte[] data, int version);

/// <summary>
/// Return the ACL and stat of the node of the given path.
Expand All @@ -259,6 +269,7 @@ public interface IZooKeeper
/// @throws IllegalArgumentException if an invalid path is specified
/// </summary>
IEnumerable<ACL> GetACL(string path, Stat stat);
Task<IEnumerable<ACL>> GetACLAsync(string path, Stat stat);

/// <summary>
/// Set the ACL for the node of the given path if such a node exists and the
Expand All @@ -280,6 +291,7 @@ public interface IZooKeeper
/// @throws IllegalArgumentException if an invalid path is specified
/// </summary>
Stat SetACL(string path, IEnumerable<ACL> acl, int version);
Task<Stat> SetACLAsync(string path, IEnumerable<ACL> acl, int version);

/// <summary>
/// Return the list of the children of the node of the given path.
Expand All @@ -302,8 +314,10 @@ public interface IZooKeeper
/// @throws IllegalArgumentException if an invalid path is specified
/// </summary>
IEnumerable<string> GetChildren(string path, IWatcher watcher);
Task<IEnumerable<string>> GetChildrenAsync(string path, IWatcher watcher);

IEnumerable<string> GetChildren(string path, bool watch);
Task<IEnumerable<string>> GetChildrenAsync(string path, bool watch);

/// <summary>
/// For the given znode path return the stat and children list.
Expand All @@ -329,6 +343,7 @@ public interface IZooKeeper
/// @throws IllegalArgumentException if an invalid path is specified
/// </summary>
IEnumerable<string> GetChildren(string path, IWatcher watcher, Stat stat);
Task<IEnumerable<string>> GetChildrenAsync(string path, IWatcher watcher, Stat stat);

/// <summary>
/// For the given znode path return the stat and children list.
Expand All @@ -354,6 +369,7 @@ public interface IZooKeeper
/// error code.
/// </summary>
IEnumerable<string> GetChildren(string path, bool watch, Stat stat);
Task<IEnumerable<string>> GetChildrenAsync(string path, bool watch, Stat stat);

/// <summary>
/// Close this client object. Once the client is closed, its session becomes
Expand Down
15 changes: 6 additions & 9 deletions src/dotnet/ZooKeeperNet/Packet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace ZooKeeperNet
{
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using log4net;
using Org.Apache.Jute;
using Org.Apache.Zookeeper.Proto;
Expand Down Expand Up @@ -89,18 +89,15 @@ internal Packet(RequestHeader header, ReplyHeader replyHeader, IRecord request,
}


private readonly ManualResetEventSlim mreslim = new ManualResetEventSlim(false);
public bool WaitUntilFinishedSlim(TimeSpan timeout)
private readonly TaskCompletionSource<object> packetCompletion = new TaskCompletionSource<object>();
internal Task PacketTask
{
return mreslim.Wait(timeout);
get { return packetCompletion.Task; }
}

internal bool Finished
{
set
{
mreslim.Set();
}
set { packetCompletion.TrySetResult(null); }
}

public override string ToString()
Expand Down
Loading