From 7a33b50cc21e9ecec92cff485e352104ef50ceb7 Mon Sep 17 00:00:00 2001 From: Aaron Stannard Date: Mon, 16 Jan 2023 08:23:19 -0600 Subject: [PATCH] Revert "Add compression option for NewtonSoftJsonSerializer (#6341)" (#6349) This reverts commit d7daf56c1df5e973fec9c94d54c853c1c9ccee6b. --- .../CoreAPISpec.ApproveCore.verified.txt | 3 - .../Serialization/NewtonsoftJsonConfigSpec.cs | 3 - .../Serialization/SerializationSpec.cs | 96 ++++--------------- src/core/Akka/Configuration/Pigeon.conf | 6 -- .../Serialization/NewtonSoftJsonSerializer.cs | 77 ++------------- 5 files changed, 28 insertions(+), 157 deletions(-) diff --git a/src/core/Akka.API.Tests/CoreAPISpec.ApproveCore.verified.txt b/src/core/Akka.API.Tests/CoreAPISpec.ApproveCore.verified.txt index 89322647846..33d56906676 100644 --- a/src/core/Akka.API.Tests/CoreAPISpec.ApproveCore.verified.txt +++ b/src/core/Akka.API.Tests/CoreAPISpec.ApproveCore.verified.txt @@ -4789,7 +4789,6 @@ namespace Akka.Serialization public NewtonSoftJsonSerializer(Akka.Actor.ExtendedActorSystem system) { } public NewtonSoftJsonSerializer(Akka.Actor.ExtendedActorSystem system, Akka.Configuration.Config config) { } public NewtonSoftJsonSerializer(Akka.Actor.ExtendedActorSystem system, Akka.Serialization.NewtonSoftJsonSerializerSettings settings) { } - public bool Compressed { get; } public override bool IncludeManifest { get; } public object Serializer { get; } public Newtonsoft.Json.JsonSerializerSettings Settings { get; } @@ -4800,8 +4799,6 @@ namespace Akka.Serialization { public static readonly Akka.Serialization.NewtonSoftJsonSerializerSettings Default; public NewtonSoftJsonSerializerSettings(bool encodeTypeNames, bool preserveObjectReferences, System.Collections.Generic.IEnumerable converters, bool usePooledStringBuilder, int stringBuilderMinSize, int stringBuilderMaxSize) { } - public NewtonSoftJsonSerializerSettings(bool encodeTypeNames, bool preserveObjectReferences, System.Collections.Generic.IEnumerable converters, bool usePooledStringBuilder, int stringBuilderMinSize, int stringBuilderMaxSize, bool compressed) { } - public bool Compressed { get; } public System.Collections.Generic.IEnumerable Converters { get; } public bool EncodeTypeNames { get; } public bool PreserveObjectReferences { get; } diff --git a/src/core/Akka.Tests/Serialization/NewtonsoftJsonConfigSpec.cs b/src/core/Akka.Tests/Serialization/NewtonsoftJsonConfigSpec.cs index 76793e48f56..6eb3b190d23 100644 --- a/src/core/Akka.Tests/Serialization/NewtonsoftJsonConfigSpec.cs +++ b/src/core/Akka.Tests/Serialization/NewtonsoftJsonConfigSpec.cs @@ -29,7 +29,6 @@ public void Json_serializer_should_have_correct_defaults() Assert.Equal(2, serializer.Settings.Converters.Count); Assert.Contains(serializer.Settings.Converters, x => x is DiscriminatedUnionConverter); Assert.Contains(serializer.Settings.Converters, x => x is NewtonSoftJsonSerializer.SurrogateConverter); - Assert.False(serializer.Compressed); } } @@ -41,7 +40,6 @@ public void Json_serializer_should_allow_to_setup_custom_flags() serialization-settings.json { preserve-object-references = false encode-type-names = false - use-compression = true } } "); @@ -53,7 +51,6 @@ public void Json_serializer_should_allow_to_setup_custom_flags() Assert.Equal(2, serializer.Settings.Converters.Count); Assert.Contains(serializer.Settings.Converters, x => x is DiscriminatedUnionConverter); Assert.Contains(serializer.Settings.Converters, x => x is NewtonSoftJsonSerializer.SurrogateConverter); - Assert.True(serializer.Compressed); } } diff --git a/src/core/Akka.Tests/Serialization/SerializationSpec.cs b/src/core/Akka.Tests/Serialization/SerializationSpec.cs index c3896a8caf4..57f1ca7dae6 100644 --- a/src/core/Akka.Tests/Serialization/SerializationSpec.cs +++ b/src/core/Akka.Tests/Serialization/SerializationSpec.cs @@ -26,7 +26,7 @@ namespace Akka.Tests.Serialization { - public abstract class SerializationSpecBase : AkkaSpec + public class SerializationSpec : AkkaSpec { public class UntypedContainerMessage : IEquatable { @@ -608,10 +608,27 @@ public void Missing_custom_serializer_id_should_append_help_message() .Where(ex => ex.Message.Contains("Serializer Id [101] is not one of the internal Akka.NET serializer.")); } - public SerializationSpecBase(string config):base(config) + public SerializationSpec():base(GetConfig()) { } + private static string GetConfig() => @" + akka.actor { + serializers { + dummy = """ + typeof(DummySerializer).AssemblyQualifiedName + @""" + dummy2 = """ + typeof(DummyConfigurableSerializer).AssemblyQualifiedName + @""" + } + + serialization-bindings { + ""System.String"" = dummy + } + serialization-settings { + dummy2 { + test-key = ""test value"" + } + } + }"; + public class DummySerializer : Serializer { public readonly Config Config; @@ -692,80 +709,5 @@ public sealed class ChildClass } } } - - public class SerializationSpec: SerializationSpecBase - { - private static string GetConfig() => @" -akka.actor { - serializers { - dummy = """ + typeof(DummySerializer).AssemblyQualifiedName + @""" - dummy2 = """ + typeof(DummyConfigurableSerializer).AssemblyQualifiedName + @""" - } - - serialization-bindings { - ""System.String"" = dummy - } - serialization-settings { - dummy2 { - test-key = ""test value"" - } - } -}"; - - public SerializationSpec() : base(GetConfig()) - { - } - - [Fact(DisplayName = "Compression must be turned off")] - public void SettingTest() - { - var serializer = (NewtonSoftJsonSerializer) Sys.Serialization.FindSerializerFor(123); - serializer.Compressed.Should().BeFalse(); - } - } - - - public class CompressedSerializationSpec: SerializationSpecBase - { - private static string GetConfig() => @" -akka.actor { - serializers { - dummy = """ + typeof(DummySerializer).AssemblyQualifiedName + @""" - dummy2 = """ + typeof(DummyConfigurableSerializer).AssemblyQualifiedName + @""" - } - - serialization-bindings { - ""System.String"" = dummy - } - serialization-settings { - dummy2 { - test-key = ""test value"" - } - json { - use-compression = true - } - } -}"; - - public CompressedSerializationSpec() : base(GetConfig()) - { - } - - [Fact(DisplayName = "Compression must be turned on")] - public void SettingTest() - { - var serializer = (NewtonSoftJsonSerializer) Sys.Serialization.FindSerializerFor(123); - serializer.Compressed.Should().BeTrue(); - var bytes = serializer.ToBinary(new BigData()); - bytes.Length.Should().BeLessThan(10 * 1024); // compressed size should be less than 10Kb - var deserialized = serializer.FromBinary(bytes); - deserialized.Message.Should().Be(new string('x', 5 * 1024 * 1024)); - } - - private class BigData - { - public string Message { get; } = new string('x', 5 * 1024 * 1024); // 5 megabyte worth of chars - } - } } diff --git a/src/core/Akka/Configuration/Pigeon.conf b/src/core/Akka/Configuration/Pigeon.conf index 6c0dbcd2093..c748b84f9da 100644 --- a/src/core/Akka/Configuration/Pigeon.conf +++ b/src/core/Akka/Configuration/Pigeon.conf @@ -554,12 +554,6 @@ akka { # So after ~42k characters you might wind up # on the Large Object Heap (which may not be a bad thing...) pooled-string-builder-maxsize = 32768 - - # When set true, this will compress the serialized - # JSON string using GZip. - # Useful to avoid network congestion when your serialized - # data starts to grow to the megabyte size - use-compression = false } } } diff --git a/src/core/Akka/Serialization/NewtonSoftJsonSerializer.cs b/src/core/Akka/Serialization/NewtonSoftJsonSerializer.cs index 5378e87a970..fc1af0297fb 100644 --- a/src/core/Akka/Serialization/NewtonSoftJsonSerializer.cs +++ b/src/core/Akka/Serialization/NewtonSoftJsonSerializer.cs @@ -9,10 +9,8 @@ using System.Collections.Generic; using System.Globalization; using System.IO; -using System.IO.Compression; using System.Linq; using System.Reflection; -using System.Runtime.CompilerServices; using System.Text; using Akka.Actor; using Akka.Configuration; @@ -39,8 +37,7 @@ public sealed class NewtonSoftJsonSerializerSettings converters: Enumerable.Empty(), usePooledStringBuilder:true, stringBuilderMinSize:2048, - stringBuilderMaxSize:32768, - compressed: false); + stringBuilderMaxSize:32768); /// /// Creates a new instance of the based on a provided . @@ -67,8 +64,7 @@ public static NewtonSoftJsonSerializerSettings Create(Config config) stringBuilderMinSize:config.GetInt("pooled-string-builder-minsize", 2048), stringBuilderMaxSize: config.GetInt("pooled-string-builder-maxsize", - 32768), - compressed: config.GetBoolean("use-compression") + 32768) ); } @@ -119,11 +115,6 @@ private static IEnumerable GetConverterTypes(Config config) /// public bool UsePooledStringBuilder { get; } - /// - /// If true, serialized data are compressed using GZip to reduce size - /// - public bool Compressed { get; } - /// /// Creates a new instance of the . /// @@ -133,31 +124,7 @@ private static IEnumerable GetConverterTypes(Config config) /// Determines if string builders will be used from a pool to lower memory usage /// Starting size used for pooled string builders if enabled /// Max retained size used for pooled string builders if enabled - public NewtonSoftJsonSerializerSettings( - bool encodeTypeNames, - bool preserveObjectReferences, - IEnumerable converters, - bool usePooledStringBuilder, - int stringBuilderMinSize, - int stringBuilderMaxSize) - : this( - encodeTypeNames: encodeTypeNames, - preserveObjectReferences: preserveObjectReferences, - converters: converters, - usePooledStringBuilder: usePooledStringBuilder, - stringBuilderMinSize: stringBuilderMinSize, - stringBuilderMaxSize: stringBuilderMaxSize, - compressed: false) - { } - - public NewtonSoftJsonSerializerSettings( - bool encodeTypeNames, - bool preserveObjectReferences, - IEnumerable converters, - bool usePooledStringBuilder, - int stringBuilderMinSize, - int stringBuilderMaxSize, - bool compressed) + public NewtonSoftJsonSerializerSettings(bool encodeTypeNames, bool preserveObjectReferences, IEnumerable converters, bool usePooledStringBuilder, int stringBuilderMinSize, int stringBuilderMaxSize) { if (converters == null) throw new ArgumentNullException(nameof(converters), $"{nameof(NewtonSoftJsonSerializerSettings)} requires a sequence of converters."); @@ -168,7 +135,6 @@ private static IEnumerable GetConverterTypes(Config config) UsePooledStringBuilder = usePooledStringBuilder; StringBuilderMinSize = stringBuilderMinSize; StringBuilderMaxSize = stringBuilderMaxSize; - Compressed = compressed; } } @@ -191,8 +157,6 @@ public class NewtonSoftJsonSerializer : Serializer /// public object Serializer { get { return _serializer; } } - public bool Compressed { get; } - /// /// Initializes a new instance of the class. /// @@ -253,8 +217,6 @@ public NewtonSoftJsonSerializer(ExtendedActorSystem system, NewtonSoftJsonSerial Settings.ContractResolver = new AkkaContractResolver(); _serializer = JsonSerializer.Create(Settings); - - Compressed = settings.Compressed; } @@ -318,9 +280,9 @@ public override byte[] ToBinary(object obj) private byte[] toBinary_NewBuilder(object obj) { - var data = JsonConvert.SerializeObject(obj, Formatting.None, Settings); - var bytes = Encoding.UTF8.GetBytes(data); - return Compressed ? Compress(bytes) : bytes; + string data = JsonConvert.SerializeObject(obj, Formatting.None, Settings); + byte[] bytes = Encoding.UTF8.GetBytes(data); + return bytes; } private byte[] toBinary_PooledBuilder(object obj) @@ -342,8 +304,7 @@ private byte[] toBinary_PooledBuilder(object obj) { ser.Serialize(jw, obj); } - var bytes = Encoding.UTF8.GetBytes(tw.ToString()); - return Compressed ? Compress(bytes) : bytes; + return Encoding.UTF8.GetBytes(tw.ToString()); } } finally @@ -363,8 +324,8 @@ private byte[] toBinary_PooledBuilder(object obj) /// The object contained in the array public override object FromBinary(byte[] bytes, Type type) { - var data = Encoding.UTF8.GetString(Compressed ? Decompress(bytes) : bytes); - var res = JsonConvert.DeserializeObject(data, Settings); + string data = Encoding.UTF8.GetString(bytes); + object res = JsonConvert.DeserializeObject(data, Settings); return TranslateSurrogate(res, this, type); } @@ -406,26 +367,6 @@ private static object GetValue(string V) throw new NotSupportedException(); } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static byte[] Compress(byte[] data) - { - using var compressedStream = new MemoryStream(); - using var compressor = new GZipStream(compressedStream, CompressionMode.Compress); - compressor.Write(data, 0, data.Length); - compressor.Flush(); // It is critical to flush here - return compressedStream.ToArray(); - } - - [MethodImpl(MethodImplOptions.AggressiveInlining)] - private static byte[] Decompress(byte[] raw) - { - using var compressedStream = new MemoryStream(raw); - using var compressor = new GZipStream(compressedStream, CompressionMode.Decompress); - using var uncompressedStream = new MemoryStream(); - compressor.CopyTo(uncompressedStream); - return uncompressedStream.ToArray(); - } /// /// TBD