You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ignite.apache.org by GitBox <gi...@apache.org> on 2021/09/07 03:30:03 UTC

[GitHub] [ignite-3] isapego commented on a change in pull request #306: IGNITE-15356 .NET: Add basic thin client

isapego commented on a change in pull request #306:
URL: https://github.com/apache/ignite-3/pull/306#discussion_r703147468



##########
File path: modules/platforms/dotnet/Apache.Ignite/Internal/Buffers/PooledArrayBufferWriter.cs
##########
@@ -0,0 +1,211 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+namespace Apache.Ignite.Internal.Buffers
+{
+    using System;
+    using System.Buffers;
+    using System.Diagnostics;
+    using System.Net;
+    using MessagePack;
+
+    /// <summary>
+    /// Pooled buffer writer: integrates <see cref="MessagePackWriter"/> with <see cref="ArrayPool{T}"/>,
+    /// and adds the logic to prepend messages with size and other data (opcode, request id).
+    /// <para />
+    /// We reserve some bytes for the prefix because message size, op code and request ID are not known initially.
+    /// <para />
+    /// There are two ways to use <see cref="MessagePackWriter"/>: with a <see cref="SequencePool"/>,
+    /// or with a <see cref="IBufferWriter{T}"/>. SequencePool approach uses buffer pooling too, but still allocates
+    /// the final array with <see cref="MessagePackWriter.FlushAndGetArray"/>. We want to avoid all array allocations,
+    /// so we implement our own <see cref="IBufferWriter{T}"/> here.
+    /// <para />
+    /// Based on <see cref="ArrayBufferWriter{T}"/>, but uses <see cref="ArrayPool{T}.Shared"/> to allocate arrays.
+    /// <para />
+    /// Not a struct because <see cref="GetMessageWriter"/> will cause boxing.
+    /// </summary>
+    internal sealed class PooledArrayBufferWriter : IBufferWriter<byte>, IDisposable
+    {
+        /** Reserved prefix size. */
+        private const int ReservedPrefixSize = 4 + 4 + 9; // Size (4 bytes) + OpCode (4 bytes) + RequestId (9 bytes)/
+
+        /** Underlying pooled array. */
+        private byte[] _buffer;
+
+        /** Index within the array. */
+        private int _index;
+
+        /** Index within the array. */
+        private int _index2;

Review comment:
       Probably needs a better name.

##########
File path: modules/platforms/dotnet/Apache.Ignite/Internal/Proto/MessagePackReaderExtensions.cs
##########
@@ -0,0 +1,138 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+namespace Apache.Ignite.Internal.Proto
+{
+    using System;
+    using System.Buffers;
+    using System.Buffers.Binary;
+    using System.Diagnostics;
+    using System.Runtime.CompilerServices;
+    using MessagePack;
+
+    /// <summary>
+    /// Extension methods for <see cref="MessagePackReader"/>.
+    /// </summary>
+    internal static class MessagePackReaderExtensions
+    {
+        /// <summary>
+        /// Reads an object with specified type.
+        /// </summary>
+        /// <param name="reader">Reader.</param>
+        /// <param name="type">Type.</param>
+        /// <returns>Resulting object.</returns>
+        public static object? ReadObject(this ref MessagePackReader reader, ClientDataType type)
+        {
+            switch (type)
+            {
+                case ClientDataType.Int8:
+                    return reader.ReadByte();
+
+                case ClientDataType.Int16:
+                    return reader.ReadInt16();
+
+                case ClientDataType.Int32:
+                    return reader.ReadInt32();
+
+                case ClientDataType.Int64:
+                    return reader.ReadInt64();
+
+                case ClientDataType.Float:
+                    return reader.ReadSingle();
+
+                case ClientDataType.Double:
+                    return reader.ReadDouble();
+
+                case ClientDataType.Uuid:
+                    return reader.ReadGuid();
+
+                case ClientDataType.String:
+                    return reader.ReadString();
+
+                default:
+                    throw new IgniteClientException("Unsupported type: " + type);
+            }
+        }
+
+        /// <summary>
+        /// Skips multiple elements.
+        /// </summary>
+        /// <param name="reader">Reader.</param>
+        /// <param name="count">Element count to skip.</param>
+        public static void Skip(this ref MessagePackReader reader, int count)
+        {
+            for (var i = 0; i < count; i++)
+            {
+                reader.Skip();
+            }
+        }
+
+        /// <summary>
+        /// Reads a Guid value.
+        /// </summary>
+        /// <param name="reader">Reader.</param>
+        /// <returns>Guid.</returns>
+        public static Guid ReadGuid(this ref MessagePackReader reader)
+        {
+            const int guidSize = 16;
+
+            ValidateExtensionType(ref reader, ClientMessagePackType.Uuid, guidSize);
+
+            ReadOnlySequence<byte> seq = reader.ReadRaw(guidSize);
+            ReadOnlySpan<byte> jBytes = seq.FirstSpan;
+
+            Debug.Assert(jBytes.Length == guidSize, "jBytes.Length == 16");
+
+            // Hoist range checks.
+            byte d = jBytes[15];
+            byte e = jBytes[14];
+            byte f = jBytes[13];
+            byte g = jBytes[12];
+            byte h = jBytes[11];
+            byte i = jBytes[10];
+            byte j = jBytes[9];
+            byte k = jBytes[8];
+
+            int a = BinaryPrimitives.ReadInt32BigEndian(jBytes[4..]);
+            short b = BinaryPrimitives.ReadInt16BigEndian(jBytes[2..]);
+            short c = BinaryPrimitives.ReadInt16BigEndian(jBytes);
+
+            return new Guid(a, b, c, d, e, f, g, h, i, j, k);

Review comment:
       Why GUID serialization format is that weird?

##########
File path: modules/platforms/dotnet/Apache.Ignite.Tests/Buffers/PooledArrayBufferWriterTests.cs
##########
@@ -0,0 +1,103 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+namespace Apache.Ignite.Tests.Buffers
+{
+    using Internal.Buffers;
+    using NUnit.Framework;
+
+    /// <summary>
+    /// Tests for <see cref="PooledArrayBufferWriter"/>.
+    /// </summary>
+    public class PooledArrayBufferWriterTests
+    {
+        [Test]
+        public void TestBufferWriterPrependsMessageLength()
+        {
+            using var bufferWriter = new PooledArrayBufferWriter();
+
+            // With payload.
+            var writer = bufferWriter.GetMessageWriter();
+
+            writer.Write(1);
+            writer.Write("A");
+            writer.Flush();
+
+            var res = bufferWriter.GetWrittenMemory().ToArray();
+
+            var expectedBytes = new byte[]
+            {
+                // 4 bytes BE length + 1 byte int + 1 byte fixstr + 1 byte char.
+                0, 0, 0, 3, 1, 0xa1, (byte)'A'
+            };
+
+            CollectionAssert.AreEqual(expectedBytes, res);
+        }
+
+        [Test]
+        public void TestBufferWriterPrependsPrefixAndMessageLength()
+        {
+            using var bufferWriter = new PooledArrayBufferWriter();
+
+            var writer = bufferWriter.GetMessageWriter();
+            writer.Write(1);
+            writer.Write("A");
+            writer.Flush();
+
+            var prefixWriter = bufferWriter.GetPrefixWriter(3);
+            prefixWriter.Write(7);
+            prefixWriter.Write(8);
+            prefixWriter.Write(9);
+            writer.Flush();

Review comment:
       Why `writer` and not `prefixWriter` here?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org