public int JobResultRemote(ComputeJobHolder job, PlatformMemoryStream stream)
        {
            // 1. Unmarshal result.
            PortableReaderImpl reader = _compute.Marshaller.StartUnmarshal(stream);

            Guid nodeId    = reader.ReadGuid().Value;
            bool cancelled = reader.ReadBoolean();

            try
            {
                object err;

                var data = PortableUtils.ReadWrappedInvocationResult(reader, out err);

                // 2. Process the result.
                return((int)JobResult0(new ComputeJobResultImpl(data, (Exception)err, job.Job, nodeId, cancelled)));
            }
            catch (Exception e)
            {
                Finish(default(TR), e);

                if (!(e is IgniteException))
                {
                    throw new IgniteException("Failed to process job result: " + e.Message, e);
                }

                throw;
            }
        }
        public void CompleteWithError(long taskHandle, PlatformMemoryStream stream)
        {
            PortableReaderImpl reader = _compute.Marshaller.StartUnmarshal(stream);

            Exception err;

            try
            {
                if (reader.ReadBoolean())
                {
                    PortableResultWrapper res = reader.ReadObject <PortableUserObject>()
                                                .Deserialize <PortableResultWrapper>();

                    err = (Exception)res.Result;
                }
                else
                {
                    err = ExceptionUtils.GetException(reader.ReadString(), reader.ReadString());
                }
            }
            catch (Exception e)
            {
                err = new IgniteException("Task completed with error, but it cannot be unmarshalled: " + e.Message, e);
            }

            CompleteWithError(taskHandle, err);
        }
Beispiel #3
0
        /// <summary>
        /// Kernal start callback.
        /// </summary>
        /// <param name="interopProc">Interop processor.</param>
        /// <param name="stream">Stream.</param>
        internal static void OnStart(IUnmanagedTarget interopProc, IPortableStream stream)
        {
            try
            {
                // 1. Read data and leave critical state ASAP.
                PortableReaderImpl reader = PU.Marshaller.StartUnmarshal(stream);

                // ReSharper disable once PossibleInvalidOperationException
                var name = reader.ReadString();

                // 2. Set ID and name so that Start() method can use them later.
                _startup.Name = name;

                if (Nodes.ContainsKey(new NodeKey(name)))
                {
                    throw new IgniteException("Ignite with the same name already started: " + name);
                }

                _startup.Ignite = new Ignite(_startup.Configuration, _startup.Name, interopProc, _startup.Marshaller,
                                             _startup.LifecycleBeans, _startup.Callbacks);
            }
            catch (Exception e)
            {
                // 5. Preserve exception to throw it later in the "Start" method and throw it further
                //    to abort startup in Java.
                _startup.Error = e;

                throw;
            }
        }
        /// <summary>
        /// Refresh projection nodes.
        /// </summary>
        /// <returns>Nodes.</returns>
        private IList <IClusterNode> RefreshNodes()
        {
            long oldTopVer = Interlocked.Read(ref _topVer);

            List <IClusterNode> newNodes = null;

            DoOutInOp(OpNodes, writer =>
            {
                writer.WriteLong(oldTopVer);
            }, input =>
            {
                PortableReaderImpl reader = Marshaller.StartUnmarshal(input);

                if (reader.ReadBoolean())
                {
                    // Topology has been updated.
                    long newTopVer = reader.ReadLong();

                    newNodes = IgniteUtils.ReadNodes(reader, _pred);

                    UpdateTopology(newTopVer, newNodes);
                }
            });

            if (newNodes != null)
            {
                return(newNodes);
            }

            // No topology changes.
            Debug.Assert(_nodes != null, "At least one topology update should have occurred.");

            return(_nodes);
        }
Beispiel #5
0
        /** <inheritdoc /> */
        protected override ICacheEntry <TK, TV> Read(PortableReaderImpl reader)
        {
            TK key = reader.ReadObject <TK>();
            TV val = reader.ReadObject <TV>();

            return(new CacheEntry <TK, TV>(key, val));
        }
Beispiel #6
0
        /// <summary>
        /// Creates exception according to native code class and message.
        /// </summary>
        /// <param name="clsName">Exception class name.</param>
        /// <param name="msg">Exception message.</param>
        /// <param name="reader">Error data reader.</param>
        public static Exception GetException(string clsName, string msg, PortableReaderImpl reader = null)
        {
            ExceptionFactoryDelegate ctor;

            if (EXS.TryGetValue(clsName, out ctor))
            {
                return(ctor(msg));
            }

            if (ClsNoClsDefFoundErr.Equals(clsName))
            {
                return(new IgniteException("Java class is not found (did you set IGNITE_HOME environment " +
                                           "variable?): " + msg));
            }

            if (ClsNoSuchMthdErr.Equals(clsName))
            {
                return(new IgniteException("Java class method is not found (did you set IGNITE_HOME environment " +
                                           "variable?): " + msg));
            }

            if (ClsCachePartialUpdateErr.Equals(clsName))
            {
                return(ProcessCachePartialUpdateException(msg, reader));
            }

            return(new IgniteException("Java exception occurred [class=" + clsName + ", message=" + msg + ']'));
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="ServiceDescriptor" /> class.
        /// </summary>
        /// <param name="name">Name.</param>
        /// <param name="reader">Reader.</param>
        /// <param name="services">Services.</param>
        public ServiceDescriptor(string name, PortableReaderImpl reader, IServices services)
        {
            Debug.Assert(reader != null);
            Debug.Assert(services != null);
            Debug.Assert(!string.IsNullOrEmpty(name));

            _services = services;
            Name      = name;

            CacheName       = reader.ReadString();
            MaxPerNodeCount = reader.ReadInt();
            TotalCount      = reader.ReadInt();
            OriginNodeId    = reader.ReadGuid() ?? Guid.Empty;
            AffinityKey     = reader.ReadObject <object>();

            var mapSize = reader.ReadInt();
            var snap    = new Dictionary <Guid, int>(mapSize);

            for (var i = 0; i < mapSize; i++)
            {
                snap[reader.ReadGuid() ?? Guid.Empty] = reader.ReadInt();
            }

            TopologySnapshot = snap.AsReadOnly();
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="ContinuousQueryFilterHolder"/> class.
        /// </summary>
        /// <param name="reader">The reader.</param>
        public ContinuousQueryFilterHolder(IPortableReader reader)
        {
            PortableReaderImpl rawReader = (PortableReaderImpl)reader.RawReader();

            _keyTyp       = PortableUtils.ReadPortableOrSerializable <Type>(rawReader);
            _valTyp       = PortableUtils.ReadPortableOrSerializable <Type>(rawReader);
            _filter       = PortableUtils.ReadPortableOrSerializable <object>(rawReader);
            _keepPortable = rawReader.ReadBoolean();
        }
Beispiel #9
0
        /// <summary>
        /// Read event.
        /// </summary>
        /// <param name="reader">Reader.</param>
        /// <returns>Event.</returns>
        private static ICacheEntryEvent <TK, TV> ReadEvent0 <TK, TV>(PortableReaderImpl reader)
        {
            reader.DetachNext();
            TK key = reader.ReadObject <TK>();

            reader.DetachNext();
            TV oldVal = reader.ReadObject <TV>();

            reader.DetachNext();
            TV val = reader.ReadObject <TV>();

            return(CreateEvent(key, oldVal, val));
        }
Beispiel #10
0
        /** <inheritdoc /> */
        protected override IList Read(PortableReaderImpl reader)
        {
            int cnt = reader.ReadInt();

            var res = new ArrayList(cnt);

            for (int i = 0; i < cnt; i++)
            {
                res.Add(reader.ReadObject <object>());
            }

            return(res);
        }
Beispiel #11
0
        /** <inheritDoc /> */
        public IPortableMetadata Metadata(int typeId)
        {
            return(DoOutInOp <IPortableMetadata>(OpMetadata,
                                                 writer =>
            {
                writer.WriteInt(typeId);
            },
                                                 stream =>
            {
                PortableReaderImpl reader = Marshaller.StartUnmarshal(stream, false);

                return reader.ReadBoolean() ? new PortableMetadataImpl(reader) : null;
            }
                                                 ));
        }
Beispiel #12
0
        /// <summary>
        /// Create lifecycle bean.
        /// </summary>
        /// <param name="reader">Reader.</param>
        /// <returns>Lifecycle bean.</returns>
        internal static ILifecycleBean CreateLifecycleBean(PortableReaderImpl reader)
        {
            // 1. Instantiate.
            string assemblyName = reader.ReadString();
            string clsName      = reader.ReadString();

            object bean = IgniteUtils.CreateInstance(assemblyName, clsName);

            // 2. Set properties.
            IDictionary <string, object> props = reader.ReadGenericDictionary <string, object>();

            IgniteUtils.SetProperties(bean, props);

            return(bean as ILifecycleBean);
        }
        private void FutureError(void *target, long futPtr, long memPtr)
        {
            SafeCall(() =>
            {
                IPortableStream stream = IgniteManager.Memory.Get(memPtr).Stream();

                PortableReaderImpl reader = _ignite.Marshaller.StartUnmarshal(stream);

                string errCls = reader.ReadString();
                string errMsg = reader.ReadString();

                Exception err = ExceptionUtils.GetException(errCls, errMsg, reader);

                ProcessFuture(futPtr, fut => { fut.OnError(err); });
            });
        }
Beispiel #14
0
        /// <summary>
        /// Prepare callback invoked from Java.
        /// </summary>
        /// <param name="inStream">Intput stream with data.</param>
        /// <param name="outStream">Output stream.</param>
        /// <param name="handleRegistry">Handle registry.</param>
        internal static void OnPrepare(PlatformMemoryStream inStream, PlatformMemoryStream outStream,
                                       HandleRegistry handleRegistry)
        {
            try
            {
                PortableReaderImpl reader = PU.Marshaller.StartUnmarshal(inStream);

                PrepareConfiguration(reader.ReadObject <InteropDotNetConfiguration>());

                PrepareLifecycleBeans(reader, outStream, handleRegistry);
            }
            catch (Exception e)
            {
                _startup.Error = e;

                throw;
            }
        }
Beispiel #15
0
        /// <summary>
        /// Reads events from a portable reader.
        /// </summary>
        /// <typeparam name="T">Event type.</typeparam>
        /// <param name="portableReader">Reader.</param>
        /// <returns>Resulting list or null.</returns>
        protected static List <T> ReadEvents <T>(PortableReaderImpl portableReader) where T : IEvent
        {
            var count = portableReader.RawReader().ReadInt();

            if (count == -1)
            {
                return(null);
            }

            var result = new List <T>(count);

            for (var i = 0; i < count; i++)
            {
                result.Add(EventReader.Read <T>(portableReader));
            }

            return(result);
        }
Beispiel #16
0
        /// <summary>
        /// Reads nullable list.
        /// </summary>
        /// <param name="reader">Reader.</param>
        /// <returns>List.</returns>
        private static List <object> ReadNullableList(PortableReaderImpl reader)
        {
            if (!reader.ReadBoolean())
            {
                return(null);
            }

            var size = reader.ReadInt();

            var list = new List <object>(size);

            for (int i = 0; i < size; i++)
            {
                list.Add(reader.ReadObject <object>());
            }

            return(list);
        }
Beispiel #17
0
        /// <summary>
        /// Initializes a new instance of the <see cref="StreamReceiverHolder"/> class.
        /// </summary>
        /// <param name="reader">The reader.</param>
        public StreamReceiverHolder(PortableReaderImpl reader)
        {
            var rcvType = reader.ReadByte();

            _rcv = PortableUtils.ReadPortableOrSerializable <object>(reader);

            Debug.Assert(_rcv != null);

            var type = _rcv.GetType();

            if (rcvType == RcvTransformer)
            {
                // rcv is a user ICacheEntryProcessor<K, V, A, R>, construct StreamTransformer from it.
                // (we can't marshal StreamTransformer directly, because it is generic,
                // and we do not know type arguments that user will have)
                _rcv = DelegateTypeDescriptor.GetStreamTransformerCtor(type)(_rcv);
            }

            _invoke = DelegateTypeDescriptor.GetStreamReceiver(_rcv.GetType());
        }
Beispiel #18
0
        /// <summary>
        /// Read dictionary returned by GET_ALL operation.
        /// </summary>
        /// <param name="reader">Reader.</param>
        /// <returns>Dictionary.</returns>
        private static IDictionary <TK, TV> ReadGetAllDictionary(PortableReaderImpl reader)
        {
            IPortableStream stream = reader.Stream;

            if (stream.ReadBool())
            {
                int size = stream.ReadInt();

                IDictionary <TK, TV> res = new Dictionary <TK, TV>(size);

                for (int i = 0; i < size; i++)
                {
                    TK key = reader.ReadObject <TK>();
                    TV val = reader.ReadObject <TV>();

                    res[key] = val;
                }

                return(res);
            }
            return(null);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="ServiceDescriptor" /> class.
        /// </summary>
        /// <param name="name">Name.</param>
        /// <param name="reader">Reader.</param>
        /// <param name="services">Services.</param>
        public ServiceDescriptor(string name, PortableReaderImpl reader, IServices services)
        {
            Debug.Assert(reader != null);
            Debug.Assert(services != null);
            Debug.Assert(!string.IsNullOrEmpty(name));

            _services = services;
            Name = name;

            CacheName = reader.ReadString();
            MaxPerNodeCount = reader.ReadInt();
            TotalCount = reader.ReadInt();
            OriginNodeId = reader.ReadGuid() ?? Guid.Empty;
            AffinityKey = reader.ReadObject<object>();

            var mapSize = reader.ReadInt();
            var snap = new Dictionary<Guid, int>(mapSize);

            for (var i = 0; i < mapSize; i++)
                snap[reader.ReadGuid() ?? Guid.Empty] = reader.ReadInt();

            TopologySnapshot = snap.AsReadOnly();
        }
Beispiel #20
0
        /// <summary>
        /// Prepare lifecycle beans.
        /// </summary>
        /// <param name="reader">Reader.</param>
        /// <param name="outStream">Output stream.</param>
        /// <param name="handleRegistry">Handle registry.</param>
        private static void PrepareLifecycleBeans(PortableReaderImpl reader, PlatformMemoryStream outStream,
                                                  HandleRegistry handleRegistry)
        {
            IList <LifecycleBeanHolder> beans = new List <LifecycleBeanHolder>();

            // 1. Read beans defined in Java.
            int cnt = reader.ReadInt();

            for (int i = 0; i < cnt; i++)
            {
                beans.Add(new LifecycleBeanHolder(CreateLifecycleBean(reader)));
            }

            // 2. Append beans definied in local configuration.
            ICollection <ILifecycleBean> nativeBeans = _startup.Configuration.LifecycleBeans;

            if (nativeBeans != null)
            {
                foreach (ILifecycleBean nativeBean in nativeBeans)
                {
                    beans.Add(new LifecycleBeanHolder(nativeBean));
                }
            }

            // 3. Write bean pointers to Java stream.
            outStream.WriteInt(beans.Count);

            foreach (LifecycleBeanHolder bean in beans)
            {
                outStream.WriteLong(handleRegistry.AllocateCritical(bean));
            }

            outStream.SynchronizeOutput();

            // 4. Set beans to STARTUP object.
            _startup.LifecycleBeans = beans;
        }
Beispiel #21
0
        /// <summary>
        /// Process cache partial update exception.
        /// </summary>
        /// <param name="msg">Message.</param>
        /// <param name="reader">Reader.</param>
        /// <returns></returns>
        private static Exception ProcessCachePartialUpdateException(string msg, PortableReaderImpl reader)
        {
            if (reader == null)
            {
                return(new CachePartialUpdateException(msg, new IgniteException("Failed keys are not available.")));
            }

            bool dataExists = reader.ReadBoolean();

            Debug.Assert(dataExists);

            if (reader.ReadBoolean())
            {
                bool keepPortable = reader.ReadBoolean();

                PortableReaderImpl keysReader = reader.Marshaller.StartUnmarshal(reader.Stream, keepPortable);

                try
                {
                    return(new CachePartialUpdateException(msg, ReadNullableList(keysReader)));
                }
                catch (Exception e)
                {
                    // Failed to deserialize data.
                    return(new CachePartialUpdateException(msg, e));
                }
            }

            // Was not able to write keys.
            string innerErrCls = reader.ReadString();
            string innerErrMsg = reader.ReadString();

            Exception innerErr = GetException(innerErrCls, innerErrMsg);

            return(new CachePartialUpdateException(msg, innerErr));
        }
        /// <summary>
        /// Reads nullable list.
        /// </summary>
        /// <param name="reader">Reader.</param>
        /// <returns>List.</returns>
        private static List<object> ReadNullableList(PortableReaderImpl reader)
        {
            if (!reader.ReadBoolean()) 
                return null;

            var size = reader.ReadInt();

            var list = new List<object>(size);

            for (int i = 0; i < size; i++)
                list.Add(reader.ReadObject<object>());

            return list;
        }
        /// <summary>
        /// Process cache partial update exception.
        /// </summary>
        /// <param name="msg">Message.</param>
        /// <param name="reader">Reader.</param>
        /// <returns></returns>
        private static Exception ProcessCachePartialUpdateException(string msg, PortableReaderImpl reader)
        {
            if (reader == null)
                return new CachePartialUpdateException(msg, new IgniteException("Failed keys are not available."));
            
            bool dataExists = reader.ReadBoolean();

            Debug.Assert(dataExists);

            if (reader.ReadBoolean())
            {
                bool keepPortable = reader.ReadBoolean();

                PortableReaderImpl keysReader = reader.Marshaller.StartUnmarshal(reader.Stream, keepPortable);

                try
                {
                    return new CachePartialUpdateException(msg, ReadNullableList(keysReader));
                }
                catch (Exception e)
                {
                    // Failed to deserialize data.
                    return new CachePartialUpdateException(msg, e);
                }
            }
            
            // Was not able to write keys.
            string innerErrCls = reader.ReadString();
            string innerErrMsg = reader.ReadString();

            Exception innerErr = GetException(innerErrCls, innerErrMsg);

            return new CachePartialUpdateException(msg, innerErr);
        }
        /// <summary>
        /// Creates exception according to native code class and message.
        /// </summary>
        /// <param name="clsName">Exception class name.</param>
        /// <param name="msg">Exception message.</param>
        /// <param name="reader">Error data reader.</param>
        public static Exception GetException(string clsName, string msg, PortableReaderImpl reader = null)
        {
            ExceptionFactoryDelegate ctor;

            if (EXS.TryGetValue(clsName, out ctor))
                return ctor(msg);

            if (ClsNoClsDefFoundErr.Equals(clsName))
                return new IgniteException("Java class is not found (did you set IGNITE_HOME environment " +
                    "variable?): " + msg);

            if (ClsNoSuchMthdErr.Equals(clsName))
                return new IgniteException("Java class method is not found (did you set IGNITE_HOME environment " +
                    "variable?): " + msg);

            if (ClsCachePartialUpdateErr.Equals(clsName))
                return ProcessCachePartialUpdateException(msg, reader);
            
            return new IgniteException("Java exception occurred [class=" + clsName + ", message=" + msg + ']');
        }
        public void TestHandlesExclusive([Values(true, false)] bool detached, [Values(true, false)] bool asPortable)
        {
            var marsh = new PortableMarshaller(new PortableConfiguration
            {
                TypeConfigurations = new List<PortableTypeConfiguration>
                {
                    new PortableTypeConfiguration(typeof (HandleInner)),
                    new PortableTypeConfiguration(typeof (HandleOuterExclusive))
                }
            });

            var inner = new HandleInner
            {
                Before = "inBefore",
                After = "inAfter",
                RawBefore = "inRawBefore",
                RawAfter = "inRawAfter"
            };

            var outer = new HandleOuterExclusive
            {
                Before = "outBefore",
                After = "outAfter",
                RawBefore = "outRawBefore",
                RawAfter = "outRawAfter",
                Inner = inner,
                RawInner = inner
            };

            inner.Outer = outer;
            inner.RawOuter = outer;

            var bytes = asPortable
                ? marsh.Marshal(new PortablesImpl(marsh).ToPortable<IPortableObject>(outer))
                : marsh.Marshal(outer);

            IPortableObject outerObj;

            if (detached)
            {
                var reader = new PortableReaderImpl(marsh, new Dictionary<long, IPortableTypeDescriptor>(),
                    new PortableHeapStream(bytes), PortableMode.ForcePortable, null);

                reader.DetachNext();

                outerObj = reader.Deserialize<IPortableObject>();
            }
            else
                outerObj = marsh.Unmarshal<IPortableObject>(bytes, PortableMode.ForcePortable);

            HandleOuter newOuter = outerObj.Deserialize<HandleOuter>();

            Assert.IsFalse(newOuter == newOuter.Inner.Outer);
            Assert.IsFalse(newOuter == newOuter.Inner.RawOuter);
            Assert.IsFalse(newOuter == newOuter.RawInner.RawOuter);
            Assert.IsFalse(newOuter == newOuter.RawInner.RawOuter);

            Assert.IsFalse(newOuter.Inner == newOuter.RawInner);

            Assert.IsTrue(newOuter.Inner.Outer == newOuter.Inner.RawOuter);
            Assert.IsTrue(newOuter.RawInner.Outer == newOuter.RawInner.RawOuter);

            Assert.IsTrue(newOuter.Inner == newOuter.Inner.Outer.Inner);
            Assert.IsTrue(newOuter.Inner == newOuter.Inner.Outer.RawInner);
            Assert.IsTrue(newOuter.RawInner == newOuter.RawInner.Outer.Inner);
            Assert.IsTrue(newOuter.RawInner == newOuter.RawInner.Outer.RawInner);
        }
 /// <summary>
 /// Read entry from the reader.
 /// </summary>
 /// <param name="reader">Reader.</param>
 /// <returns>Entry.</returns>
 protected abstract T Read(PortableReaderImpl reader);
 /// <summary>
 /// Reads a node from stream.
 /// </summary>
 private IClusterNode ReadNode(PortableReaderImpl r)
 {
     return(GetNode(r.ReadGuid()));
 }
Beispiel #28
0
        /// <summary>
        /// Create lifecycle bean.
        /// </summary>
        /// <param name="reader">Reader.</param>
        /// <returns>Lifecycle bean.</returns>
        internal static ILifecycleBean CreateLifecycleBean(PortableReaderImpl reader)
        {
            // 1. Instantiate.
            string assemblyName = reader.ReadString();
            string clsName = reader.ReadString();

            object bean = IgniteUtils.CreateInstance(assemblyName, clsName);

            // 2. Set properties.
            IDictionary<string, object> props = reader.ReadGenericDictionary<string, object>();

            IgniteUtils.SetProperties(bean, props);

            return bean as ILifecycleBean;
        }
        /// <summary>
        /// Reads the generic collection.
        /// </summary>
        public object ReadGeneric(PortableReaderImpl reader)
        {
            Debug.Assert(reader != null);
            Debug.Assert(_readFunc != null);

            return _readFunc(reader, null);
        }
Beispiel #30
0
        /// <summary>
        /// Prepare lifecycle beans.
        /// </summary>
        /// <param name="reader">Reader.</param>
        /// <param name="outStream">Output stream.</param>
        /// <param name="handleRegistry">Handle registry.</param>
        private static void PrepareLifecycleBeans(PortableReaderImpl reader, PlatformMemoryStream outStream, 
            HandleRegistry handleRegistry)
        {
            IList<LifecycleBeanHolder> beans = new List<LifecycleBeanHolder>();

            // 1. Read beans defined in Java.
            int cnt = reader.ReadInt();

            for (int i = 0; i < cnt; i++)
                beans.Add(new LifecycleBeanHolder(CreateLifecycleBean(reader)));

            // 2. Append beans definied in local configuration.
            ICollection<ILifecycleBean> nativeBeans = _startup.Configuration.LifecycleBeans;

            if (nativeBeans != null)
            {
                foreach (ILifecycleBean nativeBean in nativeBeans)
                    beans.Add(new LifecycleBeanHolder(nativeBean));
            }

            // 3. Write bean pointers to Java stream.
            outStream.WriteInt(beans.Count);

            foreach (LifecycleBeanHolder bean in beans)
                outStream.WriteLong(handleRegistry.AllocateCritical(bean));

            outStream.SynchronizeOutput();

            // 4. Set beans to STARTUP object.
            _startup.LifecycleBeans = beans;
        }