akkadotnet / Akka.Persistence.PostgreSql

Akka.Persistence.PostgreSql provider
Apache License 2.0
32 stars 35 forks source link

Issue171 snapshot serialization #172

Open rafalpiotrowski opened 1 year ago

rafalpiotrowski commented 1 year ago

Fixes #171 applied the same logic when serializing snapshot as in journal when it comes to setting manifest

Changes

Please provide a brief description of the changes here.

Checklist

For significant changes, please ensure that the following have been completed (delete if not relevant):

Latest dev Benchmarks

Include data from the relevant benchmark prior to this change here.

This PR's Benchmarks

Include data from after this change here.

Aaronontheweb commented 1 year ago

cc @CumpsD @Arkatufus

CumpsD commented 1 year ago

Pasting this here for reference what to compare to:

        protected override void WriteEvent(DbCommand command, IPersistentRepresentation e, IImmutableSet<string> tags)
        {
            var serializationResult = _serialize(e);
            var serializer = serializationResult.Serializer;
            var hasSerializer = serializer != null;

            string manifest = "";
            if (hasSerializer && serializer is SerializerWithStringManifest)
                manifest = ((SerializerWithStringManifest)serializer).Manifest(e.Payload);
            else if (hasSerializer && serializer.IncludeManifest)
                manifest = QualifiedName(e);
            else
                manifest = string.IsNullOrEmpty(e.Manifest) ? QualifiedName(e) : e.Manifest;

            AddParameter(command, "@PersistenceId", DbType.String, e.PersistenceId);
            AddParameter(command, "@SequenceNr", DbType.Int64, e.SequenceNr);
            AddParameter(command, "@Timestamp", DbType.Int64, e.Timestamp);
            AddParameter(command, "@IsDeleted", DbType.Boolean, false);
            AddParameter(command, "@Manifest", DbType.String, manifest);

            if (hasSerializer)
            {
                AddParameter(command, "@SerializerId", DbType.Int32, serializer.Identifier);
            }
            else
            {
                AddParameter(command, "@SerializerId", DbType.Int32, DBNull.Value);
            }

            command.Parameters.Add(new NpgsqlParameter("@Payload", serializationResult.DbType) { Value = serializationResult.Payload });

What I don't know, can't debug and don't know by heart, the Journal code checks with e.Payload and seems to pretty much ignore serializationResult? @Aaronontheweb or @Arkatufus will know this better :)

For what it's worth, the above Journal code can be refactored to something like this:

        protected override void WriteEvent(
            DbCommand command,
            IPersistentRepresentation e,
            IImmutableSet<string> tags)
        {
            var serializationResult = _serialize(e);
            var serializer = serializationResult.Serializer;

            var manifest = serializer switch
            {
                SerializerWithStringManifest stringManifest => stringManifest.Manifest(e.Payload),
                { IncludeManifest: true } => QualifiedName(e),
                _ => !string.IsNullOrEmpty(e.Manifest) ? e.Manifest : QualifiedName(e),
            };

            AddParameter(command, "@PersistenceId", DbType.String, e.PersistenceId);
            AddParameter(command, "@SequenceNr", DbType.Int64, e.SequenceNr);
            AddParameter(command, "@Timestamp", DbType.Int64, e.Timestamp);
            AddParameter(command, "@IsDeleted", DbType.Boolean, false);
            AddParameter(command, "@Manifest", DbType.String, manifest);
            AddParameter(command, "@SerializerId", DbType.Int32, (object?) serializer?.Identifier ?? DBNull.Value);

            command.Parameters.Add(new NpgsqlParameter("@Payload", serializationResult.DbType) { Value = serializationResult.Payload });

Might be worth it to simplify the hasSerializer stuff in this PR too

Arkatufus commented 1 year ago

That is the peculiarity of PostgreSql extension. If you set the PostgreSql data type to either JSONB or JSON, the internal Akka.NET serializer is completely ignored and the plugin will attempt to serialize the paylod using NewtonSoft.Json completely. And since the the JSON serializer already embeds type information, we do not need manifest anymore.

Arkatufus commented 1 year ago

Yes, we know that this is a very bad design, the PostgreSql extension should not offer to save the data in JSON nor JSONB to begin with.

CumpsD commented 1 year ago

It can be handy to have JSON to be honest. It's a very versatile format without any lock-in. And it makes for easy debugging ;)

Arkatufus commented 1 year ago

Except when a user complained that their production deployment is failing because a serializer kept throwing circular reference exception when they're using Hyperion for everything.