You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by ni...@apache.org on 2020/12/23 16:17:39 UTC

[ignite] 01/01: IGNITE-13734: Register service return type on method invocation.

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

nizhikov pushed a commit to branch IGNITE-13734
in repository https://gitbox.apache.org/repos/asf/ignite.git

commit 56fb493ca9bc6b6848a2e171c98f1e6bb364491b
Author: Nikolay Izhikov <ni...@apache.org>
AuthorDate: Wed Dec 23 19:16:53 2020 +0300

    IGNITE-13734: Register service return type on method invocation.
---
 .../platform/binary/PlatformBinaryProcessor.java   |  3 +-
 .../java/org/apache/ignite/platform/Account.java   | 71 ++++++++++++++++++++++
 .../ignite/platform/PlatformDeployServiceTask.java |  8 +++
 .../Services/IJavaService.cs                       |  3 +
 .../Services/JavaServiceDynamicProxy.cs            |  5 ++
 .../Apache.Ignite.Core.Tests/Services/Model.cs     | 31 ++++++++++
 .../Services/ServiceProxyTest.cs                   | 42 ++++++++-----
 .../Services/ServiceTypeAutoResolveTest.cs         | 13 +++-
 .../Impl/Binary/BinaryProcessor.cs                 | 35 ++++++++++-
 .../Apache.Ignite.Core/Impl/Binary/Marshaller.cs   |  2 +-
 .../Impl/Services/ServiceProxySerializer.cs        | 49 ++++++---------
 .../Apache.Ignite.Core/Impl/Services/Services.cs   | 18 ++++--
 12 files changed, 226 insertions(+), 54 deletions(-)

diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/platform/binary/PlatformBinaryProcessor.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/platform/binary/PlatformBinaryProcessor.java
index 4c5c122..425c299 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/processors/platform/binary/PlatformBinaryProcessor.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/platform/binary/PlatformBinaryProcessor.java
@@ -134,10 +134,11 @@ public class PlatformBinaryProcessor extends PlatformAbstractTarget {
 
             case OP_GET_TYPE: {
                 int typeId = reader.readInt();
+                byte platformId = reader.readByte();
 
                 try {
                     String typeName = platformContext().kernalContext().marshallerContext()
-                        .getClassName(MarshallerPlatformIds.DOTNET_ID, typeId);
+                        .getClassName(platformId, typeId);
 
                     writer.writeString(typeName);
                 }
diff --git a/modules/core/src/test/java/org/apache/ignite/platform/Account.java b/modules/core/src/test/java/org/apache/ignite/platform/Account.java
new file mode 100644
index 0000000..2aac500
--- /dev/null
+++ b/modules/core/src/test/java/org/apache/ignite/platform/Account.java
@@ -0,0 +1,71 @@
+/*
+ * 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.
+ */
+
+package org.apache.ignite.platform;
+
+import java.util.Objects;
+
+/** */
+public class Account {
+    /** */
+    private String id;
+
+    /** */
+    private int amount;
+
+    /** */
+    public Account() {
+    }
+
+    public Account(String id, int amount) {
+        this.id = id;
+        this.amount = amount;
+    }
+
+    /** */
+    public String getId() {
+        return id;
+    }
+
+    /** */
+    public void setId(String id) {
+        this.id = id;
+    }
+
+    /** */
+    public int getAmount() {
+        return amount;
+    }
+
+    /** */
+    public void setAmount(int amount) {
+        this.amount = amount;
+    }
+
+    @Override public boolean equals(Object o) {
+        if (this == o)
+            return true;
+        if (o == null || getClass() != o.getClass())
+            return false;
+        Account account = (Account)o;
+        return Objects.equals(id, account.id);
+    }
+
+    @Override public int hashCode() {
+        return Objects.hash(id);
+    }
+}
diff --git a/modules/core/src/test/java/org/apache/ignite/platform/PlatformDeployServiceTask.java b/modules/core/src/test/java/org/apache/ignite/platform/PlatformDeployServiceTask.java
index cb3f8d7..df1b3b6 100644
--- a/modules/core/src/test/java/org/apache/ignite/platform/PlatformDeployServiceTask.java
+++ b/modules/core/src/test/java/org/apache/ignite/platform/PlatformDeployServiceTask.java
@@ -502,6 +502,14 @@ public class PlatformDeployServiceTask extends ComputeTaskAdapter<String, Object
         }
 
         /** */
+        public Account[] testAccounts() {
+            return new Account[] {
+                new Account("123", 42),
+                new Account("321", 0)
+            };
+        }
+
+        /** */
         public void testDateArray(Timestamp[] dates) {
             assertNotNull(dates);
             assertEquals(2, dates.length);
diff --git a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Services/IJavaService.cs b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Services/IJavaService.cs
index dfcaaff..283c931 100644
--- a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Services/IJavaService.cs
+++ b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Services/IJavaService.cs
@@ -170,6 +170,9 @@ namespace Apache.Ignite.Core.Tests.Services
 
         /** */
         Employee[] testEmployees(Employee[] emps);
+        
+        /** */
+        Account[] testAccounts();
 
         /** */
         ICollection testDepartments(ICollection deps);
diff --git a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Services/JavaServiceDynamicProxy.cs b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Services/JavaServiceDynamicProxy.cs
index 26c0637..42afcd4 100644
--- a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Services/JavaServiceDynamicProxy.cs
+++ b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Services/JavaServiceDynamicProxy.cs
@@ -319,6 +319,11 @@ namespace Apache.Ignite.Core.Tests.Services
             return _svc.testEmployees(emps);
         }
 
+        public Account[] testAccounts()
+        {
+            return _svc.testAccounts();
+        }
+
         /** <inheritDoc /> */
         public ICollection testDepartments(ICollection deps)
         {
diff --git a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Services/Model.cs b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Services/Model.cs
index 0793e87..30c6c2d 100644
--- a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Services/Model.cs
+++ b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Services/Model.cs
@@ -18,6 +18,8 @@
 // ReSharper disable once CheckNamespace
 namespace org.apache.ignite.platform
 {
+    using System;
+
     /// <summary>
     /// A class is a clone of Java class Address with the same namespace.
     /// </summary>
@@ -85,4 +87,33 @@ namespace org.apache.ignite.platform
     {
         public string Val { get; set; }
     }
+
+    /// <summary>
+    /// A class is a clone of Java class Account with the same namespace.
+    /// </summary>
+    public class Account
+    {
+        public String Id { get; set; }
+        
+        public int Amount { get; set; }
+
+        protected bool Equals(Account other)
+        {
+            return Id == other.Id;
+        }
+
+        public override bool Equals(object obj)
+        {
+            if (ReferenceEquals(null, obj)) return false;
+            if (ReferenceEquals(this, obj)) return true;
+            if (obj.GetType() != this.GetType()) return false;
+            return Equals((Account) obj);
+        }
+
+        public override int GetHashCode()
+        {
+            // ReSharper disable once NonReadonlyMemberInGetHashCode
+            return Id.GetHashCode();
+        }
+    }
 }
diff --git a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Services/ServiceProxyTest.cs b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Services/ServiceProxyTest.cs
index 53bfbbc..16bfc7e 100644
--- a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Services/ServiceProxyTest.cs
+++ b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Services/ServiceProxyTest.cs
@@ -18,6 +18,7 @@
 namespace Apache.Ignite.Core.Tests.Services
 {
     using System;
+    using System.CodeDom;
     using System.Diagnostics.CodeAnalysis;
     using System.IO;
     using System.Linq;
@@ -283,32 +284,41 @@ namespace Apache.Ignite.Core.Tests.Services
                 // 1) Write to a stream
                 inStream.WriteBool(SrvKeepBinary);  // WriteProxyMethod does not do this, but Java does
 
-                ServiceProxySerializer.WriteProxyMethod(_marsh.StartMarshal(inStream), method.Name, 
-                    method, args, PlatformType.DotNet);
+                Marshaller.RegisterSameJavaType.Value = true;
 
-                inStream.SynchronizeOutput();
+                try
+                {
+                    ServiceProxySerializer.WriteProxyMethod(_marsh.StartMarshal(inStream), method.Name,
+                        method, args, PlatformType.DotNet);
 
-                inStream.Seek(0, SeekOrigin.Begin);
+                    inStream.SynchronizeOutput();
 
-                // 2) call InvokeServiceMethod
-                string mthdName;
-                object[] mthdArgs;
+                    inStream.Seek(0, SeekOrigin.Begin);
 
-                ServiceProxySerializer.ReadProxyMethod(inStream, _marsh, out mthdName, out mthdArgs);
+                    // 2) call InvokeServiceMethod
+                    string mthdName;
+                    object[] mthdArgs;
 
-                var result = ServiceProxyInvoker.InvokeServiceMethod(_svc, mthdName, mthdArgs);
+                    ServiceProxySerializer.ReadProxyMethod(inStream, _marsh, out mthdName, out mthdArgs);
 
-                ServiceProxySerializer.WriteInvocationResult(outStream, _marsh, result.Key, result.Value);
+                    var result = ServiceProxyInvoker.InvokeServiceMethod(_svc, mthdName, mthdArgs);
 
-                var writer = _marsh.StartMarshal(outStream);
-                writer.WriteString("unused");  // fake Java exception details
-                writer.WriteString("unused");  // fake Java exception details
+                    ServiceProxySerializer.WriteInvocationResult(outStream, _marsh, result.Key, result.Value);
 
-                outStream.SynchronizeOutput();
+                    var writer = _marsh.StartMarshal(outStream);
+                    writer.WriteString("unused"); // fake Java exception details
+                    writer.WriteString("unused"); // fake Java exception details
 
-                outStream.Seek(0, SeekOrigin.Begin);
+                    outStream.SynchronizeOutput();
 
-                return ServiceProxySerializer.ReadInvocationResult(outStream, _marsh, KeepBinary);
+                    outStream.Seek(0, SeekOrigin.Begin);
+
+                    return ServiceProxySerializer.ReadInvocationResult(outStream, _marsh, KeepBinary);
+                }
+                finally 
+                {
+                    Marshaller.RegisterSameJavaType.Value = true;
+                }
             }
         }
 
diff --git a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Services/ServiceTypeAutoResolveTest.cs b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Services/ServiceTypeAutoResolveTest.cs
index c99a198..a45eb09 100644
--- a/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Services/ServiceTypeAutoResolveTest.cs
+++ b/modules/platforms/dotnet/Apache.Ignite.Core.Tests/Services/ServiceTypeAutoResolveTest.cs
@@ -97,11 +97,11 @@ namespace Apache.Ignite.Core.Tests.Services
         {
             // Deploy Java service
             var javaSvcName = TestUtils.DeployJavaService(_grid1);
-            
+
             var svc = _grid1.GetServices().GetServiceProxy<IJavaService>(javaSvcName, false);
 
             doTestService(svc);
-            
+
             Assert.IsNull(svc.testDepartments(null));
 
             var arr  = new[] {"HR", "IT"}.Select(x => new Department() {Name = x}).ToArray();
@@ -153,6 +153,15 @@ namespace Apache.Ignite.Core.Tests.Services
             Assert.NotNull(res);
             Assert.AreEqual(1, res.Count);
             Assert.AreEqual("value3", ((Value)res[new Key() {Id = 3}]).Val);
+
+            var accs = svc.testAccounts();
+
+            Assert.NotNull(accs);
+            Assert.AreEqual(2, accs.Length);
+            Assert.AreEqual("123", accs[0].Id);
+            Assert.AreEqual("321", accs[1].Id);
+            Assert.AreEqual(42, accs[0].Amount);
+            Assert.AreEqual(0, accs[1].Amount);
         }
 
         /// <summary>
diff --git a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinaryProcessor.cs b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinaryProcessor.cs
index 66380d6..e15d009 100644
--- a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinaryProcessor.cs
+++ b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/BinaryProcessor.cs
@@ -27,6 +27,12 @@ namespace Apache.Ignite.Core.Impl.Binary
     /// </summary>
     internal class BinaryProcessor : PlatformTargetAdapter, IBinaryProcessor
     {
+        /** Java platform id. See org.apache.ignite.internal.MarshallerPlatformIds in Java. */
+        private const byte JavaPlatformId = 0;
+
+        /** Type registry platform id. See org.apache.ignite.internal.MarshallerPlatformIds in Java. */
+        private const byte DotNetPlatformId = 1;
+
         /// <summary>
         /// Op codes.
         /// </summary>
@@ -162,7 +168,34 @@ namespace Apache.Ignite.Core.Impl.Binary
         /// <returns>Type or null.</returns>
         public string GetTypeName(int id)
         {
-            return DoOutInOp((int) Op.GetType, w => w.WriteInt(id), r => Marshaller.StartUnmarshal(r).ReadString());
+            try
+            {
+                return DoOutInOp((int) Op.GetType, w =>
+                {
+                    w.WriteInt(id);
+                    w.WriteByte(DotNetPlatformId);
+                }, r => Marshaller.StartUnmarshal(r).ReadString());
+            }
+            catch (BinaryObjectException)
+            {
+                if (!Marshaller.RegisterSameJavaType.Value)
+                    throw;
+            }
+
+            // Try to get java type name and register corresponding DotNet type.
+            var javaTypeName = DoOutInOp((int) Op.GetType, w =>
+            {
+                w.WriteInt(id);
+                w.WriteByte(JavaPlatformId);
+            }, r => Marshaller.StartUnmarshal(r).ReadString());
+
+            RegisterType(id, Marshaller.GetTypeName(javaTypeName), false);
+
+            return DoOutInOp((int) Op.GetType, w =>
+            {
+                w.WriteInt(id);
+                w.WriteByte(DotNetPlatformId);
+            }, r => Marshaller.StartUnmarshal(r).ReadString());
         }
 
         /// <summary>
diff --git a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/Marshaller.cs b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/Marshaller.cs
index cc9aeff..c65e77b 100644
--- a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/Marshaller.cs
+++ b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/Marshaller.cs
@@ -907,7 +907,7 @@ namespace Apache.Ignite.Core.Impl.Binary
         /// <summary>
         /// Gets the name of the type.
         /// </summary>
-        private string GetTypeName(string fullTypeName, IBinaryNameMapper mapper = null)
+        public string GetTypeName(string fullTypeName, IBinaryNameMapper mapper = null)
         {
             mapper = mapper ?? _cfg.NameMapper ?? GetDefaultNameMapper();
 
diff --git a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Services/ServiceProxySerializer.cs b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Services/ServiceProxySerializer.cs
index 16e3c47..e28f4d7 100644
--- a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Services/ServiceProxySerializer.cs
+++ b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Services/ServiceProxySerializer.cs
@@ -46,45 +46,36 @@ namespace Apache.Ignite.Core.Impl.Services
         {
             Debug.Assert(writer != null);
 
-            Marshaller.RegisterSameJavaType.Value = true;
+            writer.WriteString(methodName);
 
-            try
+            if (arguments != null)
             {
-                writer.WriteString(methodName);
+                writer.WriteBoolean(true);
+                writer.WriteInt(arguments.Length);
 
-                if (arguments != null)
+                if (platformType == PlatformType.DotNet)
                 {
-                    writer.WriteBoolean(true);
-                    writer.WriteInt(arguments.Length);
-
-                    if (platformType == PlatformType.DotNet)
+                    // Write as is for .NET.
+                    foreach (var arg in arguments)
                     {
-                        // Write as is for .NET.
-                        foreach (var arg in arguments)
-                        {
-                            writer.WriteObjectDetached(arg);
-                        }
+                        writer.WriteObjectDetached(arg);
                     }
-                    else
+                }
+                else
+                {
+                    // Other platforms do not support Serializable, need to convert arrays and collections
+                    var mParams = method != null ? method.GetParameters() : null;
+                    Debug.Assert(mParams == null || mParams.Length == arguments.Length);
+
+                    for (var i = 0; i < arguments.Length; i++)
                     {
-                        // Other platforms do not support Serializable, need to convert arrays and collections
-                        var mParams = method != null ? method.GetParameters() : null;
-                        Debug.Assert(mParams == null || mParams.Length == arguments.Length);
-
-                        for (var i = 0; i < arguments.Length; i++)
-                        {
-                            WriteArgForPlatforms(writer, mParams != null ? mParams[i].ParameterType : null,
-                                arguments[i]);
-                        }
+                        WriteArgForPlatforms(writer, mParams != null ? mParams[i].ParameterType : null,
+                            arguments[i]);
                     }
                 }
-                else
-                    writer.WriteBoolean(false);
-            }
-            finally
-            {
-                Marshaller.RegisterSameJavaType.Value = false;
             }
+            else
+                writer.WriteBoolean(false);
         }
 
         /// <summary>
diff --git a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Services/Services.cs b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Services/Services.cs
index 204d3ae..3afe34b 100644
--- a/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Services/Services.cs
+++ b/modules/platforms/dotnet/Apache.Ignite.Core/Impl/Services/Services.cs
@@ -434,10 +434,20 @@ namespace Apache.Ignite.Core.Impl.Services
         private object InvokeProxyMethod(IPlatformTargetInternal proxy, string methodName,
             MethodBase method, object[] args, PlatformType platformType)
         {
-            return DoOutInOp(OpInvokeMethod,
-                writer => ServiceProxySerializer.WriteProxyMethod(writer, methodName, method, args, platformType),
-                (stream, res) => ServiceProxySerializer.ReadInvocationResult(stream, Marshaller, _keepBinary), 
-                proxy);
+            Marshaller.RegisterSameJavaType.Value = true;
+
+            try
+            {
+                return DoOutInOp(OpInvokeMethod,
+                    writer => ServiceProxySerializer.WriteProxyMethod(writer, methodName, method, args, platformType),
+                    (stream, res) => ServiceProxySerializer.ReadInvocationResult(stream, Marshaller, _keepBinary),
+                    proxy);
+            }
+            finally
+            {
+                Marshaller.RegisterSameJavaType.Value = false;
+            }
+
         }
 
         /// <summary>