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

C# + shared FFI #134

Draft
wants to merge 2 commits into
base: ffi/dev_yuryf_glide_ffi
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion .github/workflows/csharp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: C# tests

on:
push:
branches: ["main"]
# branches: ["main"]
paths:
- csharp/**
- glide-core/src/**
Expand Down
8 changes: 7 additions & 1 deletion benchmarks/csharp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

using StackExchange.Redis;

using static Glide.ConnectionConfiguration;

public static class MainClass
{
private enum ChosenAction { GET_NON_EXISTING, GET_EXISTING, SET };
Expand Down Expand Up @@ -292,7 +294,11 @@ private static async Task run_with_parameters(int total_commands,
{
var clients = await createClients(clientCount, () =>
{
var glide_client = new AsyncClient(host, PORT, useTLS);
var config = new StandaloneClientConfigurationBuilder()
.WithAddress(host, PORT)
.WithTlsMode(useTLS ? TlsMode.SecureTls : TlsMode.NoTls)
.Build();
var glide_client = new AsyncClient(config);
return Task.FromResult<(Func<string, Task<string?>>, Func<string, string, Task>, Action)>(
(async (key) => await glide_client.GetAsync(key),
async (key, value) => await glide_client.SetAsync(key, value),
Expand Down
105 changes: 85 additions & 20 deletions csharp/lib/AsyncClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,68 @@

using System.Runtime.InteropServices;

using static Glide.ConnectionConfiguration;
using static Glide.Errors;

namespace Glide;

public class AsyncClient : IDisposable
{
#region public methods
public AsyncClient(string host, UInt32 port, bool useTLS)
public enum RequestType : uint
{
// copied from redis_request.proto
CustomCommand = 1,
GetString = 2,
SetString = 3,
Ping = 4,
Info = 5,
// to be continued ...
}

public AsyncClient(StandaloneClientConfiguration config)
{
successCallbackDelegate = SuccessCallback;
var successCallbackPointer = Marshal.GetFunctionPointerForDelegate(successCallbackDelegate);
failureCallbackDelegate = FailureCallback;
var failureCallbackPointer = Marshal.GetFunctionPointerForDelegate(failureCallbackDelegate);
clientPointer = CreateClientFfi(host, port, useTLS, successCallbackPointer, failureCallbackPointer);
if (clientPointer == IntPtr.Zero)
var configPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(ConnectionRequest)));
Marshal.StructureToPtr(config.ToRequest(), configPtr, false);
var responsePtr = CreateClientFfi(configPtr, successCallbackPointer, failureCallbackPointer);
Marshal.FreeHGlobal(configPtr);
var response = (ConnectionResponse?)Marshal.PtrToStructure(responsePtr, typeof(ConnectionResponse));

if (response == null)
{
throw new Exception("Failed creating a client");
throw new DisconnectedException("Failed creating a client");
}
clientPointer = response?.Client ?? IntPtr.Zero;
FreeConnectionResponse(responsePtr);

if (clientPointer == IntPtr.Zero || !string.IsNullOrEmpty(response?.Error))
{
throw new DisconnectedException(response?.Error ?? "Failed creating a client");
}
}

public async Task SetAsync(string key, string value)
{
var message = messageContainer.GetMessageForCall(key, value);
SetFfi(clientPointer, (ulong)message.Index, message.KeyPtr, message.ValuePtr);
Command(clientPointer, (ulong)message.Index, RequestType.SetString, (ulong)message.Args.Length, message.Args);
await message;
}

public async Task<string?> Custom(string[] args)
{
var message = messageContainer.GetMessageForCall(args);
Command(clientPointer, (ulong)message.Index, RequestType.CustomCommand, (ulong)args.Length, message.Args);
return await message;
}

public async Task<string?> GetAsync(string key)
{
var message = messageContainer.GetMessageForCall(key, null);
GetFfi(clientPointer, (ulong)message.Index, message.KeyPtr);
var message = messageContainer.GetMessageForCall(key);
Command(clientPointer, (ulong)message.Index, RequestType.GetString, (ulong)message.Args.Length, message.Args);
return await message;
}

Expand Down Expand Up @@ -62,14 +95,12 @@ private void SuccessCallback(ulong index, IntPtr str)
});
}

private void FailureCallback(ulong index)
private void FailureCallback(ulong index, IntPtr error_msg_ptr, ErrorType error_type)
{
var error = error_msg_ptr == IntPtr.Zero ? null : Marshal.PtrToStringAnsi(error_msg_ptr);
// Work needs to be offloaded from the calling thread, because otherwise we might starve the client's thread pool.
Task.Run(() =>
{
var message = messageContainer.GetMessage((int)index);
message.SetException(new Exception("Operation failed"));
});
_ = Task.Run(() => messageContainer.GetMessage((int)index)
.SetException(Errors.MakeException(error_type, error)));
}

~AsyncClient() => Dispose();
Expand All @@ -95,19 +126,53 @@ private void FailureCallback(ulong index)
#region FFI function declarations

private delegate void StringAction(ulong index, IntPtr str);
private delegate void FailureAction(ulong index);
[DllImport("libglide_rs", CallingConvention = CallingConvention.Cdecl, EntryPoint = "get")]
private static extern void GetFfi(IntPtr client, ulong index, IntPtr key);
/// <summary>
/// Glide request failure callback.
/// </summary>
/// <param name="index">Request ID</param>
/// <param name="error_msg_ptr">Error message</param>
/// <param name="errorType">Error type</param>
private delegate void FailureAction(ulong index, IntPtr error_msg_ptr, ErrorType errorType);

[DllImport("libglide_rs", CallingConvention = CallingConvention.Cdecl, EntryPoint = "set")]
private static extern void SetFfi(IntPtr client, ulong index, IntPtr key, IntPtr value);
[DllImport("libglide_rs", CallingConvention = CallingConvention.Cdecl, EntryPoint = "command")]
private static extern void Command(IntPtr client, ulong index, RequestType requestType, ulong argCount, IntPtr[] args);

private delegate void IntAction(IntPtr arg);
[DllImport("libglide_rs", CallingConvention = CallingConvention.Cdecl, EntryPoint = "create_client")]
private static extern IntPtr CreateClientFfi(String host, UInt32 port, bool useTLS, IntPtr successCallback, IntPtr failureCallback);
[DllImport("libglide_rs", CallingConvention = CallingConvention.Cdecl, EntryPoint = "create_client_using_config")]
private static extern IntPtr CreateClientFfi(IntPtr config, IntPtr successCallback, IntPtr failureCallback);

[DllImport("libglide_rs", CallingConvention = CallingConvention.Cdecl, EntryPoint = "close_client")]
private static extern void CloseClientFfi(IntPtr client);

[DllImport("libglide_rs", CallingConvention = CallingConvention.Cdecl, EntryPoint = "free_connection_response")]
private static extern void FreeConnectionResponse(IntPtr connectionResponsePtr);

internal enum ErrorType : uint
{
/// <summary>
/// Represented by <see cref="Errors.UnspecifiedException"/> for user
/// </summary>
Unspecified = 0,
/// <summary>
/// Represented by <see cref="Errors.ExecutionAbortedException"/> for user
/// </summary>
ExecAbort = 1,
/// <summary>
/// Represented by <see cref="TimeoutException"/> for user
/// </summary>
Timeout = 2,
/// <summary>
/// Represented by <see cref="Errors.DisconnectedException"/> for user
/// </summary>
Disconnect = 3,
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
internal struct ConnectionResponse
{
public IntPtr Client;
public string Error;
public ErrorType ErrorType;
}
#endregion
}
Loading
Loading