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

Support AOT for basic consumer app #2176

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
69 changes: 53 additions & 16 deletions src/Confluent.Kafka/Impl/LibRdKafka.cs
Expand Up @@ -29,6 +29,9 @@
#if NET462
using System.ComponentModel;
#endif
#if NET5_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;
#endif


namespace Confluent.Kafka.Impl
Expand Down Expand Up @@ -171,7 +174,11 @@ public static string LastError
}
}

static bool SetDelegates(Type nativeMethodsClass)
static bool SetDelegates(
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)]
#endif
Type nativeMethodsClass)
{
var methods = nativeMethodsClass.GetRuntimeMethods().ToArray();

Expand Down Expand Up @@ -662,14 +669,45 @@ private static void LoadNetFrameworkDelegates(string userSpecifiedPath)

#endif

private static bool TrySetDelegates(List<Type> nativeMethodCandidateTypes)
private static bool TrySetDelegates(
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)]
#endif
Type nativeMethodCandidateType)
{
foreach (var t in nativeMethodCandidateTypes)
if (SetDelegates(nativeMethodCandidateType))
{
if (SetDelegates(t))
{
return true;
}
return true;
}

throw new DllNotFoundException("Failed to load the librdkafka native library.");
}

private static bool TrySetDelegates(
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)]
#endif
Type nativeMethodCandidateType1,
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)]
#endif
Type nativeMethodCandidateType2,
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)]
#endif
Type nativeMethodCandidateType3)
{
if (SetDelegates(nativeMethodCandidateType1))
{
return true;
}
if (SetDelegates(nativeMethodCandidateType2))
{
return true;
}
if (SetDelegates(nativeMethodCandidateType3))
{
return true;
}

throw new DllNotFoundException("Failed to load the librdkafka native library.");
Expand All @@ -687,7 +725,7 @@ private static void LoadNetStandardDelegates(string userSpecifiedPath)
}
}

TrySetDelegates(new List<Type> { typeof(NativeMethods.NativeMethods) });
TrySetDelegates(typeof(NativeMethods.NativeMethods));
}

private static void LoadOSXDelegates(string userSpecifiedPath)
Expand All @@ -700,7 +738,7 @@ private static void LoadOSXDelegates(string userSpecifiedPath)
}
}

TrySetDelegates(new List<Type> { typeof(NativeMethods.NativeMethods) });
TrySetDelegates(typeof(NativeMethods.NativeMethods));
}

private static void LoadLinuxDelegates(string userSpecifiedPath)
Expand All @@ -712,7 +750,7 @@ private static void LoadLinuxDelegates(string userSpecifiedPath)
throw new InvalidOperationException($"Failed to load librdkafka at location '{userSpecifiedPath}'. dlerror: '{PosixNative.LastError}'.");
}

TrySetDelegates(new List<Type> { typeof(NativeMethods.NativeMethods) });
TrySetDelegates(typeof(NativeMethods.NativeMethods));
}
else
{
Expand All @@ -721,16 +759,15 @@ private static void LoadLinuxDelegates(string userSpecifiedPath)
var osName = PlatformApis.GetOSName();
if (osName.Equals("alpine", StringComparison.OrdinalIgnoreCase))
{
delegates.Add(typeof(NativeMethods.NativeMethods_Alpine));
TrySetDelegates(typeof(NativeMethods.NativeMethods_Alpine));
}
else
{
delegates.Add(typeof(NativeMethods.NativeMethods_Centos7));
delegates.Add(typeof(NativeMethods.NativeMethods));
delegates.Add(typeof(NativeMethods.NativeMethods_Centos6));
TrySetDelegates(
typeof(NativeMethods.NativeMethods_Centos7),
typeof(NativeMethods.NativeMethods),
typeof(NativeMethods.NativeMethods_Centos6));
}

TrySetDelegates(delegates);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Confluent.Kafka/Impl/SafeKafkaHandle.cs
Expand Up @@ -1810,7 +1810,7 @@ internal void DeleteConsumerGroupOffsets(String group, IEnumerable<TopicPartitio
setOption_OperationTimeout(optionsPtr, options.OperationTimeout);
setOption_completionSource(optionsPtr, completionSourcePtr);

if (partitions.Where(tp => tp.Topic == null || tp.Partition == null).Count() > 0)
if (partitions.Where(tp => tp.Topic == null).Count() > 0)
{
throw new ArgumentException("Cannot delete offsets because one or more topics or partitions were specified as null.");
}
Expand Down
9 changes: 8 additions & 1 deletion src/Confluent.Kafka/Internal/Util.cs
Expand Up @@ -19,6 +19,9 @@
using SystemMarshal = System.Runtime.InteropServices.Marshal;
using SystemGCHandle = System.Runtime.InteropServices.GCHandle;
using SystemGCHandleType = System.Runtime.InteropServices.GCHandleType;
#if NET5_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;
#endif


namespace Confluent.Kafka.Internal
Expand Down Expand Up @@ -79,7 +82,11 @@ public unsafe static string PtrToStringUTF8(IntPtr strPtr, UIntPtr strLength)
return Encoding.UTF8.GetString((byte*)strPtr.ToPointer(), (int)strLength);
}

public static T PtrToStructure<T>(IntPtr ptr)
public static T PtrToStructure<
#if NET5_0_OR_GREATER
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
#endif
T>(IntPtr ptr)
{
return SystemMarshal.PtrToStructure<T>(ptr);
}
Expand Down