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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 44 additions & 15 deletions src/Confluent.Kafka/Impl/LibRdKafka.cs
Original file line number Diff line number Diff line change
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 @@ -173,7 +176,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 @@ -673,14 +680,37 @@ 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 (SetDelegates(nativeMethodCandidateType1))
{
return true;
}
if (SetDelegates(nativeMethodCandidateType2))
{
return true;
}

throw new DllNotFoundException("Failed to load the librdkafka native library.");
Expand All @@ -698,7 +728,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 @@ -711,7 +741,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 @@ -723,7 +753,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 @@ -732,17 +762,16 @@ 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
{
// Try to load first the shared library with GSSAPI linkage
// and then the one without.
delegates.Add(typeof(NativeMethods.NativeMethods));
delegates.Add(typeof(NativeMethods.NativeMethods_Centos8));
TrySetDelegates(
typeof(NativeMethods.NativeMethods),
typeof(NativeMethods.NativeMethods_Centos8));
}

TrySetDelegates(delegates);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Confluent.Kafka/Impl/SafeKafkaHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1819,7 +1819,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
Original file line number Diff line number Diff line change
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