You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by pt...@apache.org on 2022/10/10 16:49:00 UTC

[ignite-3] branch main updated: IGNITE-17830 .NET: Fix Guid serialization to match UUID (#1187)

This is an automated email from the ASF dual-hosted git repository.

ptupitsyn pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/ignite-3.git


The following commit(s) were added to refs/heads/main by this push:
     new 3e2687b68f IGNITE-17830 .NET: Fix Guid serialization to match UUID (#1187)
3e2687b68f is described below

commit 3e2687b68f73bcd192f219d2c589c183da6540bd
Author: Pavel Tupitsyn <pt...@apache.org>
AuthorDate: Mon Oct 10 19:48:56 2022 +0300

    IGNITE-17830 .NET: Fix Guid serialization to match UUID (#1187)
    
    * Use the same little-endian UUID format in MessagePack and BinaryTuple.
    * Fix .NET Guid to match Java UUID format so that string representation is the same (important for debugging and logging).
---
 .../internal/client/proto/ClientMessagePacker.java |  4 +--
 .../client/proto/ClientMessageUnpacker.java        |  2 +-
 .../Apache.Ignite.Tests/Compute/ComputeTests.cs    |  6 ++--
 .../Proto/MessagePackExtensionsTest.cs             |  2 +-
 .../Apache.Ignite/Internal/Proto/UuidSerializer.cs | 40 ++++++++++++----------
 5 files changed, 27 insertions(+), 27 deletions(-)

diff --git a/modules/client-common/src/main/java/org/apache/ignite/internal/client/proto/ClientMessagePacker.java b/modules/client-common/src/main/java/org/apache/ignite/internal/client/proto/ClientMessagePacker.java
index 5fa5d3a5fa..929d808c20 100644
--- a/modules/client-common/src/main/java/org/apache/ignite/internal/client/proto/ClientMessagePacker.java
+++ b/modules/client-common/src/main/java/org/apache/ignite/internal/client/proto/ClientMessagePacker.java
@@ -501,8 +501,8 @@ public class ClientMessagePacker implements AutoCloseable {
 
         packExtensionTypeHeader(ClientMsgPackType.UUID, 16);
 
-        buf.writeLong(val.getMostSignificantBits());
-        buf.writeLong(val.getLeastSignificantBits());
+        buf.writeLongLE(val.getMostSignificantBits());
+        buf.writeLongLE(val.getLeastSignificantBits());
     }
 
     /**
diff --git a/modules/client-common/src/main/java/org/apache/ignite/internal/client/proto/ClientMessageUnpacker.java b/modules/client-common/src/main/java/org/apache/ignite/internal/client/proto/ClientMessageUnpacker.java
index 0bdd2541aa..eeb8c325b6 100644
--- a/modules/client-common/src/main/java/org/apache/ignite/internal/client/proto/ClientMessageUnpacker.java
+++ b/modules/client-common/src/main/java/org/apache/ignite/internal/client/proto/ClientMessageUnpacker.java
@@ -706,7 +706,7 @@ public class ClientMessageUnpacker implements AutoCloseable {
             throw new MessageSizeException("Expected 16 bytes for UUID extension, but got " + len, len);
         }
 
-        return new UUID(buf.readLong(), buf.readLong());
+        return new UUID(buf.readLongLE(), buf.readLongLE());
     }
 
     /**
diff --git a/modules/platforms/dotnet/Apache.Ignite.Tests/Compute/ComputeTests.cs b/modules/platforms/dotnet/Apache.Ignite.Tests/Compute/ComputeTests.cs
index 23ef965311..f81e83cc64 100644
--- a/modules/platforms/dotnet/Apache.Ignite.Tests/Compute/ComputeTests.cs
+++ b/modules/platforms/dotnet/Apache.Ignite.Tests/Compute/ComputeTests.cs
@@ -200,10 +200,8 @@ namespace Apache.Ignite.Tests.Compute
             await Test(BigInteger.Pow(1234, 56));
 
             await Test(Guid.Empty);
-
-            // TODO IGNITE-17830 String representation should be the same in Java and C#.
-            await Test(
-                new Guid(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }), "07080506-0102-0304-100f-0e0d0c0b0a09");
+            await Test(new Guid(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }));
+            await Test(Guid.NewGuid());
 
             async Task Test(object val, string? expectedStr = null)
             {
diff --git a/modules/platforms/dotnet/Apache.Ignite.Tests/Proto/MessagePackExtensionsTest.cs b/modules/platforms/dotnet/Apache.Ignite.Tests/Proto/MessagePackExtensionsTest.cs
index 878b5b8ccf..c49ea197d7 100644
--- a/modules/platforms/dotnet/Apache.Ignite.Tests/Proto/MessagePackExtensionsTest.cs
+++ b/modules/platforms/dotnet/Apache.Ignite.Tests/Proto/MessagePackExtensionsTest.cs
@@ -35,7 +35,7 @@ namespace Apache.Ignite.Tests.Proto
         /** Byte representation of the UUID above, serialized by Java ClientMessagePacker. */
         private static readonly sbyte[] JavaUuidBytes =
         {
-            -40, 3, 111, 36, 20, 106, 36, 74, 64, 24, -93, 108, 62, -100, -11, -76, 32, -126
+            -40, 3, 24, 64, 74, 36, 106, 20, 36, 111, -126, 32, -76, -11, -100, 62, 108, -93
         };
 
         private static readonly string?[] TestStrings =
diff --git a/modules/platforms/dotnet/Apache.Ignite/Internal/Proto/UuidSerializer.cs b/modules/platforms/dotnet/Apache.Ignite/Internal/Proto/UuidSerializer.cs
index e98046d7e8..dcdae08aa3 100644
--- a/modules/platforms/dotnet/Apache.Ignite/Internal/Proto/UuidSerializer.cs
+++ b/modules/platforms/dotnet/Apache.Ignite/Internal/Proto/UuidSerializer.cs
@@ -37,15 +37,17 @@ namespace Apache.Ignite.Internal.Proto
             var written = guid.TryWriteBytes(span);
             Debug.Assert(written, "written");
 
-            // Reverse endianness of the first part.
-            var a = BinaryPrimitives.ReverseEndianness(MemoryMarshal.Read<int>(span));
-            MemoryMarshal.Write(span, ref a);
+            // Reverse first part order: abc -> cba. Parts are little-endian on any system.
+            var a = MemoryMarshal.Read<int>(span);
+            var b = MemoryMarshal.Read<short>(span[4..]);
+            var c = MemoryMarshal.Read<short>(span[6..]);
 
-            var b = BinaryPrimitives.ReverseEndianness(MemoryMarshal.Read<short>(span[4..]));
-            MemoryMarshal.Write(span[4..], ref b);
+            MemoryMarshal.Write(span[4..8], ref a);
+            MemoryMarshal.Write(span[2..4], ref b);
+            MemoryMarshal.Write(span[..2], ref c);
 
-            var c = BinaryPrimitives.ReverseEndianness(MemoryMarshal.Read<short>(span[6..]));
-            MemoryMarshal.Write(span[6..], ref c);
+            // Reverse second part order: defghijk -> kjihgfed.
+            span[8..16].Reverse();
         }
 
         /// <summary>
@@ -55,18 +57,18 @@ namespace Apache.Ignite.Internal.Proto
         /// <returns>Guid.</returns>
         public static Guid Read(ReadOnlySpan<byte> span)
         {
-            // Hoist bounds checks.
-            var k = span[15];
-            var a = BinaryPrimitives.ReverseEndianness(MemoryMarshal.Read<int>(span));
-            var b = BinaryPrimitives.ReverseEndianness(MemoryMarshal.Read<short>(span[4..]));
-            var c = BinaryPrimitives.ReverseEndianness(MemoryMarshal.Read<short>(span[6..]));
-            var d = span[8];
-            var e = span[9];
-            var f = span[10];
-            var g = span[11];
-            var h = span[12];
-            var i = span[13];
-            var j = span[14];
+            var d = span[15];
+            var e = span[14];
+            var f = span[13];
+            var g = span[12];
+            var h = span[11];
+            var i = span[10];
+            var j = span[9];
+            var k = span[8];
+
+            var a = BinaryPrimitives.ReadInt32LittleEndian(span[4..8]);
+            var b = BinaryPrimitives.ReadInt16LittleEndian(span[2..4]);
+            var c = BinaryPrimitives.ReadInt16LittleEndian(span[..2]);
 
             return new Guid(a, b, c, d, e, f, g, h, i, j, k);
         }