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/11/18 16:35:11 UTC

[GitHub] [ignite] ptupitsyn commented on a change in pull request #9526: .NET: Add ServiceCallContext

ptupitsyn commented on a change in pull request #9526:
URL: https://github.com/apache/ignite/pull/9526#discussion_r752405271



##########
File path: modules/platforms/dotnet/Apache.Ignite.Core/Impl/Services/ServiceContext.cs
##########
@@ -18,7 +18,10 @@
 namespace Apache.Ignite.Core.Impl.Services
 {
     using System;
+    using System.Collections.Generic;
+    using System.Collections.ObjectModel;

Review comment:
       Unused imports, please remove.

##########
File path: modules/platforms/dotnet/Apache.Ignite.Core/Services/ServiceCallContextBuilder.cs
##########
@@ -0,0 +1,79 @@
+/*
+ * 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.Core.Services
+{
+    using System;
+    using System.Collections.Generic;
+    using System.Linq;
+    using System.Text;
+    using Apache.Ignite.Core.Impl.Common;
+    using Apache.Ignite.Core.Impl.Services;
+
+    /// <summary>
+    /// Service call context builder.
+    /// </summary>
+    public class ServiceCallContextBuilder
+    {
+        /** Context attributes. */
+        private readonly Dictionary<string, byte[]> _attrs = new Dictionary<string, byte[]>();
+        
+        /// <summary>
+        /// Put string attribute.
+        /// </summary>
+        /// <param name="name">Attribute name.</param>
+        /// <param name="value">Attribute value.</param>
+        /// <returns>This for chaining.</returns>
+        public ServiceCallContextBuilder Put(string name, string value)
+        {
+            IgniteArgumentCheck.NotNullOrEmpty(name, "name");
+            IgniteArgumentCheck.NotNull(value, "value");
+
+            _attrs[name] = Encoding.UTF8.GetBytes(value);

Review comment:
       String to byte conversion should go through `BinaryUtils.StringToUtf8Bytes` to be interoperable with Java because we have two different modes for that.

##########
File path: modules/platforms/dotnet/Apache.Ignite.Core/Services/ServiceCallContextBuilder.cs
##########
@@ -0,0 +1,79 @@
+/*
+ * 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.Core.Services
+{
+    using System;
+    using System.Collections.Generic;
+    using System.Linq;
+    using System.Text;
+    using Apache.Ignite.Core.Impl.Common;
+    using Apache.Ignite.Core.Impl.Services;
+
+    /// <summary>
+    /// Service call context builder.
+    /// </summary>
+    public class ServiceCallContextBuilder
+    {
+        /** Context attributes. */
+        private readonly Dictionary<string, byte[]> _attrs = new Dictionary<string, byte[]>();
+        
+        /// <summary>
+        /// Put string attribute.
+        /// </summary>
+        /// <param name="name">Attribute name.</param>
+        /// <param name="value">Attribute value.</param>
+        /// <returns>This for chaining.</returns>
+        public ServiceCallContextBuilder Put(string name, string value)
+        {
+            IgniteArgumentCheck.NotNullOrEmpty(name, "name");
+            IgniteArgumentCheck.NotNull(value, "value");
+
+            _attrs[name] = Encoding.UTF8.GetBytes(value);
+
+            return this;
+        }
+
+        /// <summary>
+        /// Put binary attribute.
+        /// </summary>
+        /// <param name="name">Attribute name.</param>
+        /// <param name="value">Attribute value.</param>
+        /// <returns>This for chaining.</returns>
+        public ServiceCallContextBuilder Put(string name, byte[] value)

Review comment:
       Same as above:
   
   ```suggestion
           public ServiceCallContextBuilder Set(string name, byte[] value)
   ```

##########
File path: modules/platforms/dotnet/Apache.Ignite.Core/Services/IServiceContext.cs
##########
@@ -65,5 +66,13 @@ public interface IServiceContext
         /// Affinity key, possibly null.
         /// </value>
         object AffinityKey { get; }
+
+        /// <summary>
+        /// Gets context of the current service call. 
+        /// </summary>
+        /// <returns>Context of the current service call.</returns>
+        /// <seealso cref="IServiceCallContext"/>
+        [IgniteExperimental]
+        IServiceCallContext CurrentCallContext();

Review comment:
       Let's make it a property, more idiomatic:
   
   ```suggestion
           IServiceCallContext CurrentCallContext { get; }
   ```

##########
File path: modules/platforms/dotnet/Apache.Ignite.Core/Services/IServiceCallContext.cs
##########
@@ -0,0 +1,75 @@
+/*
+ * 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.Core.Services
+{
+    /// <summary>
+    /// Represents service call context.
+    /// <para />
+    /// This context is implicitly passed to the service and can be retrieved inside the service
+    /// using <see cref="IServiceContext.CurrentCallContext()"/>. It is accessible only
+    /// from the local thread during the execution of a service method.
+    /// <para />
+    /// Use <see cref="ServiceCallContextBuilder"/> to instantiate the context.
+    /// <para />
+    /// <b>Note</b>: passing the context to the service may lead to performance overhead,
+    /// so it should only be used for "middleware" tasks.
+    /// <para />
+    /// Usage example:
+    /// <code>
+    /// // Service implementation.
+    /// public class HelloServiceImpl : HelloService
+    /// {
+    ///     private IServiceContext ctx;
+    ///
+    ///     public void Init(IServiceContext ctx)
+    ///     {
+    ///         this.ctx = ctx;
+    ///     }
+    /// 
+    ///     public string Call(string msg)
+    ///     {
+    ///         return msg + ctx.CurrentCallContext().Attribute("user");
+    ///     }
+    ///     ...
+    /// }
+    /// ...
+    ///
+    /// // Call this service with context.
+    /// IServiceCallContext callCtx = new ServiceCallContextBuilder().Put("user", "John").build();
+    /// HelloService helloSvc = ignite.GetServices().GetServiceProxy&lt;HelloService&gt;("hello-service", false, callCtx);
+    /// // Print "Hello John".
+    /// Console.WriteLine( helloSvc.call("Hello ") );
+    /// </code>
+    /// </summary>
+    public interface IServiceCallContext
+    {
+        /// <summary>
+        /// Gets the string attribute.
+        /// </summary>
+        /// <param name="name">Attribute name.</param>
+        /// <returns>String attribute value.</returns>
+        string Attribute(string name);

Review comment:
       ```suggestion
           string GetAttribute(string name);
   ```

##########
File path: modules/platforms/dotnet/Apache.Ignite.Core/Services/IServiceCallContext.cs
##########
@@ -0,0 +1,75 @@
+/*
+ * 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.Core.Services
+{
+    /// <summary>
+    /// Represents service call context.
+    /// <para />
+    /// This context is implicitly passed to the service and can be retrieved inside the service
+    /// using <see cref="IServiceContext.CurrentCallContext()"/>. It is accessible only
+    /// from the local thread during the execution of a service method.
+    /// <para />
+    /// Use <see cref="ServiceCallContextBuilder"/> to instantiate the context.
+    /// <para />
+    /// <b>Note</b>: passing the context to the service may lead to performance overhead,
+    /// so it should only be used for "middleware" tasks.
+    /// <para />
+    /// Usage example:
+    /// <code>
+    /// // Service implementation.
+    /// public class HelloServiceImpl : HelloService
+    /// {
+    ///     private IServiceContext ctx;
+    ///
+    ///     public void Init(IServiceContext ctx)
+    ///     {
+    ///         this.ctx = ctx;
+    ///     }
+    /// 
+    ///     public string Call(string msg)
+    ///     {
+    ///         return msg + ctx.CurrentCallContext().Attribute("user");
+    ///     }
+    ///     ...
+    /// }
+    /// ...
+    ///
+    /// // Call this service with context.
+    /// IServiceCallContext callCtx = new ServiceCallContextBuilder().Put("user", "John").build();
+    /// HelloService helloSvc = ignite.GetServices().GetServiceProxy&lt;HelloService&gt;("hello-service", false, callCtx);
+    /// // Print "Hello John".
+    /// Console.WriteLine( helloSvc.call("Hello ") );
+    /// </code>
+    /// </summary>
+    public interface IServiceCallContext
+    {
+        /// <summary>
+        /// Gets the string attribute.
+        /// </summary>
+        /// <param name="name">Attribute name.</param>
+        /// <returns>String attribute value.</returns>
+        string Attribute(string name);
+
+        /// <summary>
+        /// Gets the binary attribute.
+        /// </summary>
+        /// <param name="name">Attribute name.</param>
+        /// <returns>Binary attribute value.</returns>
+        byte[] BinaryAttribute(string name);

Review comment:
       ```suggestion
           byte[] GetBinaryAttribute(string name);
   ```

##########
File path: modules/platforms/dotnet/Apache.Ignite.Core/Services/ServiceCallContextBuilder.cs
##########
@@ -0,0 +1,79 @@
+/*
+ * 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.Core.Services
+{
+    using System;
+    using System.Collections.Generic;
+    using System.Linq;
+    using System.Text;
+    using Apache.Ignite.Core.Impl.Common;
+    using Apache.Ignite.Core.Impl.Services;
+
+    /// <summary>
+    /// Service call context builder.
+    /// </summary>
+    public class ServiceCallContextBuilder
+    {
+        /** Context attributes. */
+        private readonly Dictionary<string, byte[]> _attrs = new Dictionary<string, byte[]>();
+        
+        /// <summary>
+        /// Put string attribute.
+        /// </summary>
+        /// <param name="name">Attribute name.</param>
+        /// <param name="value">Attribute value.</param>
+        /// <returns>This for chaining.</returns>
+        public ServiceCallContextBuilder Put(string name, string value)
+        {
+            IgniteArgumentCheck.NotNullOrEmpty(name, "name");
+            IgniteArgumentCheck.NotNull(value, "value");
+
+            _attrs[name] = Encoding.UTF8.GetBytes(value);

Review comment:
       I think it'll be simpler to let the serializer deal with this, and use a `Dictionary<string, object>`.

##########
File path: modules/platforms/dotnet/Apache.Ignite.Core/Services/ServiceCallContextBuilder.cs
##########
@@ -0,0 +1,79 @@
+/*
+ * 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.Core.Services
+{
+    using System;
+    using System.Collections.Generic;
+    using System.Linq;
+    using System.Text;
+    using Apache.Ignite.Core.Impl.Common;
+    using Apache.Ignite.Core.Impl.Services;
+
+    /// <summary>
+    /// Service call context builder.
+    /// </summary>
+    public class ServiceCallContextBuilder
+    {
+        /** Context attributes. */
+        private readonly Dictionary<string, byte[]> _attrs = new Dictionary<string, byte[]>();
+        
+        /// <summary>
+        /// Put string attribute.
+        /// </summary>
+        /// <param name="name">Attribute name.</param>
+        /// <param name="value">Attribute value.</param>
+        /// <returns>This for chaining.</returns>
+        public ServiceCallContextBuilder Put(string name, string value)

Review comment:
       To be consistent with `BinaryObjectBuilder`:
   
   ```suggestion
           public ServiceCallContextBuilder Set(string name, string value)
   ```




-- 
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