Skip to content

Commit

Permalink
Added serialization based on Avro (#95)
Browse files Browse the repository at this point in the history
* Added first serialization based on Avro Binary codecs

* Added Json encoder for Avro

* Update documentation
  • Loading branch information
masesdevelopers authored Oct 14, 2023
1 parent 1af11f6 commit 39ed4be
Show file tree
Hide file tree
Showing 25 changed files with 1,120 additions and 9 deletions.
91 changes: 91 additions & 0 deletions src/documentation/articles/serialization.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,94 @@ public class CustomSerDes<T> : KNetSerDes<T>

> **IMPORTANT NOTE**: the type applied in the previous properties of `KafkaDbContext` shall be a generic type definition, [Entity Framework Core](https://learn.microsoft.com/it-it/ef/core/) provider for [Apache Kafka](https://kafka.apache.org/) will apply the right generic type when needed.
## **Avro** serialization

With package [MASES.EntityFrameworkCore.KNet.Serialization.Avro](https://www.nuget.org/packages/MASES.EntityFrameworkCore.KNet.Serialization.Avro/) an user can choose two different Avro serializers:
The engine comes with two different encoders:
- Binary: `KEFCoreSerDesAvroBinary`
- Json: `KEFCoreSerDesAvroJson`

### Avro schema

The following schema is the default used from the engine and can be registered in Apache Schema registry so other applications can use it to extract the data stored in the topics:

```json
{
"namespace": "MASES.EntityFrameworkCore.KNet.Serialization.Avro.Storage",
"type": "record",
"name": "AvroValueContainer",
"doc": "Represents the storage container type to be used from KEFCore",
"fields": [
{
"name": "EntityName",
"type": "string"
},
{
"name": "ClrType",
"type": "string"
},
{
"name": "Data",
"type": {
"type": "array",
"items": {
"namespace": "MASES.EntityFrameworkCore.KNet.Serialization.Avro.Storage",
"type": "record",
"name": "PropertyDataRecord",
"doc": "Represents the single container for Entity properties stored in AvroValueContainer and used from KEFCore",
"fields": [
{
"name": "PropertyIndex",
"type": "int"
},
{
"name": "PropertyName",
"type": "string"
},
{
"name": "ClrType",
"type": "string"
},
{
"name": "Value",
"type": [
"null",
"boolean",
"int",
"long",
"float",
"double",
"string"
]
}
]
}
}
}
]
}
```
The extension converted this schema into code to speedup the exection of serialization/deserialization operations.

### How to use Avro

`KafkaDbContext` contains three properties can be used to override the default types:
- **KeySerializationType**: Leave this value untouched, till now the engine uses the default serializer
- **ValueSerializationType**: set this value to `KEFCoreSerDesAvroBinary<>` or `KEFCoreSerDesAvroJson<>`
- **ValueContainerType**: set this value to `AvroValueContainer<>`

An example is:

```C#
using (context = new BloggingContext()
{
BootstrapServers = "KAFKA-SERVER:9092",
ApplicationId = "MyAppid",
DbName = "MyDBName",
ValueContainerType = typeof(AvroValueContainer<>),
ValueSerializationType = UseAvroBinary ? typeof(KEFCoreSerDesAvroBinary<>) : typeof(KEFCoreSerDesAvroJson<>),
})
{
// execute stuff here
}
```
57 changes: 57 additions & 0 deletions src/net/KEFCore.SerDes.Avro.Compiler/AvroSerializationHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2023 MASES s.r.l.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Refer to LICENSE for more information.
*/

// #define DEBUG_PERFORMANCE

#nullable enable

using Avro;

namespace MASES.EntityFrameworkCore.KNet.Serialization.Avro.Compiler;

public static class AvroSerializationHelper
{
public static void BuildSchemaClasses( string outputFolder, params string[] schemas)
{
var codegen = new CodeGen();
foreach (var schema in schemas)
{
codegen.AddSchema(schema);
}
codegen.GenerateCode();
codegen.WriteTypes(outputFolder, true);
}

public static void BuildSchemaClassesFromFiles( string outputFolder, params string[] schemaFiles)
{
var codegen = new CodeGen();
foreach (var schemaFile in schemaFiles)
{
var schema = File.ReadAllText(schemaFile);
codegen.AddSchema(schema);
}
codegen.GenerateCode();
codegen.WriteTypes(outputFolder, true);
}

public static void BuildDefaultSchema(string outputFolder)
{
BuildSchemaClassesFromFiles(outputFolder, "AvroValueContainer.avsc");
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\Common\Common.props" />
<PropertyGroup>
<OutputType>Exe</OutputType>
<ImplicitUsings>true</ImplicitUsings>
<AssemblyName>MASES.EntityFrameworkCore.KNet.Serialization.Avro.Compiler</AssemblyName>
<RootNamespace>MASES.EntityFrameworkCore.KNet.Serialization.Avro</RootNamespace>
<Title>EntityFrameworkCore KNet - Avro Serialization support for EntityFrameworkCore provider for Apache Kafka</Title>
<Description>EntityFrameworkCore KNet - Avro Serialization support for EntityFrameworkCore provider for Apache Kafka</Description>
<Product>MASES.EntityFrameworkCore.KNet.Serialization.Avro</Product>
<OutputPath>..\..\..\bin\</OutputPath>
<PackageTags>Entity Framework Core;entity-framework-core;ef;efcore;orm;avro;sql kafka apache-kafka dotnet clr netcore net5 net6 kafka connect streams producer consumer providers streamprovider confluent</PackageTags>
<Nullable>enable</Nullable>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Apache.Avro" Version="1.11.3" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<None Update="AvroValueContainer.avsc">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
54 changes: 54 additions & 0 deletions src/net/KEFCore.SerDes.Avro.Compiler/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* MIT License
*
* Copyright (c) 2022 MASES s.r.l.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

using System.Diagnostics;

namespace MASES.EntityFrameworkCore.KNet.Serialization.Avro.Compiler;

partial class Program
{
static void ReportString(string message)
{
if (Debugger.IsAttached)
{
Trace.WriteLine($"{DateTime.Now:HH::mm::ss:ffff} - {message}");
}
else
{
Console.WriteLine($"{DateTime.Now:HH::mm::ss:ffff} - {message}");
}
}

static void Main(string[] args)
{
try
{
AvroSerializationHelper.BuildDefaultSchema("Generated");
}
catch (Exception ex)
{
ReportString(ex.ToString());
}
}
}
54 changes: 54 additions & 0 deletions src/net/KEFCore.SerDes.Avro/AvroValueContainer.avsc
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"namespace": "MASES.EntityFrameworkCore.KNet.Serialization.Avro.Storage",
"type": "record",
"name": "AvroValueContainer",
"doc": "Represents the storage container type to be used from KEFCore",
"fields": [
{
"name": "EntityName",
"type": "string"
},
{
"name": "ClrType",
"type": "string"
},
{
"name": "Data",
"type": {
"type": "array",
"items": {
"namespace": "MASES.EntityFrameworkCore.KNet.Serialization.Avro.Storage",
"type": "record",
"name": "PropertyDataRecord",
"doc": "Represents the single container for Entity properties stored in AvroValueContainer and used from KEFCore",
"fields": [
{
"name": "PropertyIndex",
"type": "int"
},
{
"name": "PropertyName",
"type": "string"
},
{
"name": "ClrType",
"type": "string"
},
{
"name": "Value",
"type": [
"null",
"boolean",
"int",
"long",
"float",
"double",
"string"
]
}
]
}
}
}
]
}
99 changes: 99 additions & 0 deletions src/net/KEFCore.SerDes.Avro/AvroValueContainer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright 2023 MASES s.r.l.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Refer to LICENSE for more information.
*/

// #define DEBUG_PERFORMANCE

#nullable enable

using Avro;

namespace MASES.EntityFrameworkCore.KNet.Serialization.Avro.Storage;

/// <summary>
/// The default ValueContainer used from KEFCore
/// </summary>
/// <typeparam name="TKey">It is the key <see cref="Type"/> passed from Entity Framework associated to the Entity data will be stored in the <see cref="AvroValueContainer{TKey}"/></typeparam>
public partial class AvroValueContainer<TKey> : AvroValueContainer, IValueContainer<TKey> where TKey : notnull
{
/// <summary>
/// Initialize a new instance of <see cref="AvroValueContainer{TKey}"/>
/// </summary>
/// <remarks>It is mainly used from the JSON serializer</remarks>
public AvroValueContainer() { }
/// <summary>
/// Initialize a new instance of <see cref="AvroValueContainer{TKey}"/>
/// </summary>
/// <param name="tName">The <see cref="IEntityType"/> requesting the <see cref="AvroValueContainer{TKey}"/> for <paramref name="rData"/></param>
/// <param name="rData">The data, built from EFCore, to be stored in the <see cref="AvroValueContainer{TKey}"/></param>
/// <remarks>This constructor is mandatory and it is used from KEFCore to request a <see cref="AvroValueContainer{TKey}"/></remarks>
public AvroValueContainer(IEntityType tName, object[] rData)
{
EntityName = tName.Name;
ClrType = tName.ClrType.FullName!;
Data = new List<PropertyDataRecord>();
foreach (var item in tName.GetProperties())
{
int index = item.GetIndex();
var pRecord = new PropertyDataRecord
{
PropertyIndex = index,
PropertyName = item.Name,
ClrType = item.ClrType?.FullName,
Value = rData[index]
};
Data.Add(pRecord);
}
}
/// <inheritdoc/>
public void GetData(IEntityType tName, ref object[] array)
{
#if DEBUG_PERFORMANCE
Stopwatch fullSw = new Stopwatch();
Stopwatch newSw = new Stopwatch();
Stopwatch iterationSw = new Stopwatch();
try
{
fullSw.Start();
#endif
if (Data == null) { return; }
#if DEBUG_PERFORMANCE
newSw.Start();
#endif
array = new object[Data.Count];
#if DEBUG_PERFORMANCE
newSw.Stop();
iterationSw.Start();
#endif
for (int i = 0; i < Data.Count; i++)
{
array[i] = Data[i].Value!;
}
#if DEBUG_PERFORMANCE
iterationSw.Stop();
fullSw.Stop();
}
finally
{
if (Infrastructure.KafkaDbContext.TraceEntityTypeDataStorageGetData)
{
Infrastructure.KafkaDbContext.ReportString($"Time to GetData with length {Data.Count}: {fullSw.Elapsed} - new array took: {newSw.Elapsed} - Iteration took: {iterationSw.Elapsed}");
}
}
#endif
}
}
Loading

0 comments on commit 39ed4be

Please sign in to comment.