/** <inheritdoc /> */ public override PlatformMemoryStream GetStream() { if (_stream == null) _stream = base.GetStream(); else _stream.Reuse(); return _stream; }
private long ComputeTaskComplete(long taskPtr, long memPtr, long unused, void *arg) { var task = _handleRegistry.Get <IComputeTaskHolder>(taskPtr, true); if (memPtr == 0) { task.Complete(taskPtr); } else { using (PlatformMemoryStream stream = IgniteManager.Memory.Get(memPtr).GetStream()) { task.CompleteWithError(taskPtr, stream); } } return(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; } }
/// <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 { BinaryReader reader = BinaryUtils.Marshaller.StartUnmarshal(inStream); PrepareConfiguration(reader); PrepareLifecycleBeans(reader, outStream, handleRegistry); } catch (Exception e) { _startup.Error = e; throw; } }
private void ComputeJobExecute(void *target, long jobPtr, int cancel, long memPtr) { SafeCall(() => { var job = Job(jobPtr); if (memPtr == 0) { job.ExecuteLocal(cancel == 1); } else { using (PlatformMemoryStream stream = IgniteManager.Memory.Get(memPtr).GetStream()) { job.ExecuteRemote(stream, cancel == 1); } } }); }
private void ComputeTaskComplete(void *target, long taskPtr, long memPtr) { SafeCall(() => { var task = _handleRegistry.Get <IComputeTaskHolder>(taskPtr, true); if (memPtr == 0) { task.Complete(taskPtr); } else { using (PlatformMemoryStream stream = IgniteManager.Memory.Get(memPtr).GetStream()) { task.CompleteWithError(taskPtr, stream); } } }); }
public static void ExecuteJobAndWriteResults <T>(IIgniteInternal ignite, PlatformMemoryStream stream, T job, Func <T, object> execFunc) { Debug.Assert(stream != null); Debug.Assert(ignite != null); Debug.Assert(job != null); Debug.Assert(execFunc != null); // 0. Inject resources. InjectResources(ignite, job); // 1. Execute job. object res; bool success; using (PeerAssemblyResolver.GetInstance(ignite, Guid.Empty)) { try { res = execFunc(job); success = true; } catch (Exception e) { res = e; success = false; } } // 2. Try writing result to the stream. var writer = ignite.Marshaller.StartMarshal(stream); try { // 3. Marshal results. BinaryUtils.WriteInvocationResult(writer, success, res); } finally { // 4. Process metadata. ignite.Marshaller.FinishMarshal(writer); } }
public void CompleteWithError(long taskHandle, PlatformMemoryStream stream) { BinaryReader reader = _compute.Marshaller.StartUnmarshal(stream); Exception err; try { err = reader.ReadBoolean() ? reader.ReadObject <BinaryObject>().Deserialize <Exception>() : 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); }
private int CacheStoreInvoke(void *target, long objPtr, long memPtr, void *cb) { return(SafeCall(() => { var t = _handleRegistry.Get <CacheStore>(objPtr, true); IUnmanagedTarget cb0 = null; if ((long)cb != 0) { cb0 = new UnmanagedNonReleaseableTarget(_ctx, cb); } using (PlatformMemoryStream stream = IgniteManager.Memory.Get(memPtr).GetStream()) { return t.Invoke(stream, cb0, _ignite); } })); }
/// <summary> /// Writes the partitions assignment to a stream. /// </summary> /// <param name="parts">The parts.</param> /// <param name="stream">The stream.</param> /// <param name="marsh">The marshaller.</param> internal static void WritePartitions(IEnumerable <IEnumerable <IClusterNode> > parts, PlatformMemoryStream stream, Marshaller marsh) { Debug.Assert(parts != null); Debug.Assert(stream != null); Debug.Assert(marsh != null); IBinaryRawWriter writer = marsh.StartMarshal(stream); var partCnt = 0; writer.WriteInt(partCnt); // reserve size foreach (var part in parts) { if (part == null) { throw new IgniteException("IAffinityFunction.AssignPartitions() returned invalid partition: null"); } partCnt++; var nodeCnt = 0; var cntPos = stream.Position; writer.WriteInt(nodeCnt); // reserve size foreach (var node in part) { nodeCnt++; writer.WriteGuid(node.Id); } var endPos = stream.Position; stream.Seek(cntPos, SeekOrigin.Begin); stream.WriteInt(nodeCnt); stream.Seek(endPos, SeekOrigin.Begin); } stream.SynchronizeOutput(); stream.Seek(0, SeekOrigin.Begin); writer.WriteInt(partCnt); }
/// <summary> /// Perform out-in operation. /// </summary> /// <param name="type">Operation type.</param> /// <param name="outAction">Out action.</param> /// <param name="inAction">In action.</param> /// <param name="arg">Argument.</param> /// <returns>Result.</returns> protected unsafe TR DoOutInOp <TR>(int type, Action <BinaryWriter> outAction, Func <IBinaryStream, TR> inAction, void *arg) { using (PlatformMemoryStream outStream = IgniteManager.Memory.Allocate().GetStream()) { using (PlatformMemoryStream inStream = IgniteManager.Memory.Allocate().GetStream()) { BinaryWriter writer = _marsh.StartMarshal(outStream); outAction(writer); FinishMarshal(writer); UU.TargetInObjectStreamOutStream(_target, type, arg, outStream.SynchronizeOutput(), inStream.MemoryPointer); inStream.SynchronizeInput(); return(inAction(inStream)); } } }
/// <summary> /// Perform out-in operation. /// </summary> /// <param name="type">Operation type.</param> /// <param name="outAction">Out action.</param> /// <returns>Result.</returns> protected TR DoOutInOp <TR>(int type, Action <PortableWriterImpl> outAction) { using (PlatformMemoryStream outStream = IgniteManager.Memory.Allocate().Stream()) { using (PlatformMemoryStream inStream = IgniteManager.Memory.Allocate().Stream()) { PortableWriterImpl writer = _marsh.StartMarshal(outStream); outAction(writer); FinishMarshal(writer); UU.TargetInStreamOutStream(_target, type, outStream.SynchronizeOutput(), inStream.MemoryPointer); inStream.SynchronizeInput(); return(Unmarshal <TR>(inStream)); } } }
/// <summary> /// Perform simple out-in operation accepting single argument. /// </summary> /// <param name="type">Operation type.</param> /// <param name="val">Value.</param> /// <returns>Result.</returns> protected TR DoOutInOp <T1, TR>(int type, T1 val) { using (PlatformMemoryStream outStream = IgniteManager.Memory.Allocate().GetStream()) { using (PlatformMemoryStream inStream = IgniteManager.Memory.Allocate().GetStream()) { BinaryWriter writer = _marsh.StartMarshal(outStream); writer.WriteObject(val); FinishMarshal(writer); UU.TargetInStreamOutStream(_target, type, outStream.SynchronizeOutput(), inStream.MemoryPointer); inStream.SynchronizeInput(); return(Unmarshal <TR>(inStream)); } } }
private long CacheStoreInvoke(long memPtr) { using (PlatformMemoryStream stream = IgniteManager.Memory.Get(memPtr).GetStream()) { try { var store = _handleRegistry.Get <CacheStore>(stream.ReadLong(), true); return(store.Invoke(stream, _ignite)); } catch (Exception e) { stream.Reset(); _ignite.Marshaller.StartMarshal(stream).WriteObject(e); return(-1); } } }
/// <summary> /// Perform out-in operation. /// </summary> /// <param name="type">Operation type.</param> /// <param name="outAction">Out action.</param> /// <param name="inAction">In action.</param> protected void DoOutInOp(int type, Action <BinaryWriter> outAction, Action <IBinaryStream> inAction) { using (PlatformMemoryStream outStream = IgniteManager.Memory.Allocate().GetStream()) { using (PlatformMemoryStream inStream = IgniteManager.Memory.Allocate().GetStream()) { BinaryWriter writer = _marsh.StartMarshal(outStream); outAction(writer); FinishMarshal(writer); UU.TargetInStreamOutStream(_target, type, outStream.SynchronizeOutput(), inStream.MemoryPointer); inStream.SynchronizeInput(); inAction(inStream); } } }
/// <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(BinaryReader reader, PlatformMemoryStream outStream, HandleRegistry handleRegistry) { IList <LifecycleBeanHolder> beans = new List <LifecycleBeanHolder> { new LifecycleBeanHolder(new InternalLifecycleBean()) // add internal bean for events }; // 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; }
/// <summary> /// Prepare callback invoked from Java. /// </summary> /// <param name="inStream">Input stream with data.</param> /// <param name="outStream">Output stream.</param> /// <param name="handleRegistry">Handle registry.</param> /// <param name="log">Log.</param> internal static void OnPrepare(PlatformMemoryStream inStream, PlatformMemoryStream outStream, HandleRegistry handleRegistry, ILogger log) { try { BinaryReader reader = BinaryUtils.Marshaller.StartUnmarshal(inStream); PrepareConfiguration(reader, outStream, log); PrepareLifecycleHandlers(reader, outStream, handleRegistry); PrepareAffinityFunctions(reader, outStream); outStream.SynchronizeOutput(); } catch (Exception e) { _startup.Error = e; throw; } }
private long ExtensionCallbackInLongLongOutLong(void *target, int op, long arg1, long arg2) { return(SafeCall(() => { switch (op) { case OpPrepareDotNet: var inMem = IgniteManager.Memory.Get(arg1); var outMem = IgniteManager.Memory.Get(arg2); PlatformMemoryStream inStream = inMem.Stream(); PlatformMemoryStream outStream = outMem.Stream(); Ignition.OnPrepare(inStream, outStream, _handleRegistry); return 0; default: throw new InvalidOperationException("Unsupported operation type: " + op); } }, op == OpPrepareDotNet)); }
public int JobResultRemote(ComputeJobHolder job, PlatformMemoryStream stream) { // 1. Unmarshal result. BinaryReader reader = _compute.Marshaller.StartUnmarshal(stream); var nodeId = reader.ReadGuid(); Debug.Assert(nodeId.HasValue); bool cancelled = reader.ReadBoolean(); try { object err; var data = BinaryUtils.ReadInvocationResult(reader, out err); // 2. Process the result. var exception = (Exception)err; exception = exception == null ? null : new IgniteException("Compute job has failed on remote node, " + "examine InnerException for details.", exception); return((int)JobResult0(new ComputeJobResultImpl(data, exception, job.Job, nodeId.Value, cancelled))); } catch (Exception e) { Finish(default(TR), e); if (!(e is IgniteException)) { throw new IgniteException("Failed to process job result: " + e.Message, e); } throw; } }
/// <summary> /// Invokes the proxy. /// </summary> /// <param name="method">Method.</param> /// <param name="args">Arguments.</param> /// <returns> /// Invocation result. /// </returns> private object InvokeProxyMethod(MethodBase method, object[] args) { using (var inStream = new PlatformMemoryStream(_memory.Allocate())) using (var outStream = new PlatformMemoryStream(_memory.Allocate())) { // 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, null); inStream.SynchronizeOutput(); inStream.Seek(0, SeekOrigin.Begin); // 2) call InvokeServiceMethod string mthdName; object[] mthdArgs; IServiceCallContext unused; ServiceProxySerializer.ReadProxyMethod(inStream, _marsh, out mthdName, out mthdArgs, out unused); var result = ServiceProxyInvoker.InvokeServiceMethod(_svc, mthdName, mthdArgs); ServiceProxySerializer.WriteInvocationResult(outStream, _marsh, result.Key, result.Value); var writer = _marsh.StartMarshal(outStream); writer.WriteString("unused"); // fake Java exception details writer.WriteString("unused"); // fake Java exception details outStream.SynchronizeOutput(); outStream.Seek(0, SeekOrigin.Begin); return(ServiceProxySerializer.ReadInvocationResult(outStream, _marsh, KeepBinary)); } }
/// <summary> /// Execute job serializing result to the stream. /// </summary> /// <param name="cancel">Whether the job must be cancelled.</param> /// <param name="stream">Stream.</param> public void ExecuteRemote(PlatformMemoryStream stream, bool cancel) { // 1. Execute job. object res; bool success; Execute0(cancel, out res, out success); // 2. Try writing result to the stream. ClusterGroupImpl prj = _ignite.ClusterGroup; PortableWriterImpl writer = prj.Marshaller.StartMarshal(stream); try { // 3. Marshal results. PortableUtils.WriteWrappedInvocationResult(writer, success, res); } finally { // 4. Process metadata. prj.FinishMarshal(writer); } }
public void Map(PlatformMemoryStream stream) { IList <IClusterNode> subgrid; ClusterGroupImpl prj = (ClusterGroupImpl)_compute.ClusterGroup; var ignite = (Ignite)prj.Ignite; // 1. Unmarshal topology info if topology changed. var reader = prj.Marshaller.StartUnmarshal(stream); if (reader.ReadBoolean()) { long topVer = reader.ReadLong(); List <IClusterNode> nodes = new List <IClusterNode>(reader.ReadInt()); int nodesCnt = reader.ReadInt(); subgrid = new List <IClusterNode>(nodesCnt); for (int i = 0; i < nodesCnt; i++) { IClusterNode node = ignite.GetNode(reader.ReadGuid()); nodes.Add(node); if (reader.ReadBoolean()) { subgrid.Add(node); } } // Update parent projection to help other task callers avoid this overhead. // Note that there is a chance that topology changed even further and this update fails. // It means that some of subgrid nodes could have left the Grid. This is not critical // for us, because Java will handle it gracefully. prj.UpdateTopology(topVer, nodes); } else { IList <IClusterNode> nodes = prj.NodesNoRefresh(); Debug.Assert(nodes != null, "At least one topology update should have occurred."); subgrid = IgniteUtils.Shuffle(nodes); } // 2. Perform map. IDictionary <IComputeJob <T>, IClusterNode> map; Exception err; try { map = _task.Map(subgrid, _arg); err = null; } catch (Exception e) { map = null; err = e; // Java can receive another exception in case of marshalling failure but it is not important. Finish(default(TR), e); } // 3. Write map result to the output stream. stream.Reset(); BinaryWriter writer = prj.Marshaller.StartMarshal(stream); try { if (err == null) { writer.WriteBoolean(true); // Success flag. if (map == null) { writer.WriteBoolean(false); // Map produced no result. } else { writer.WriteBoolean(true); // Map produced result. _jobHandles = WriteJobs(writer, map); } } else { writer.WriteBoolean(false); // Map failed. // Write error as string because it is not important for Java, we need only to print // a message in the log. writer.WriteString("Map step failed [errType=" + err.GetType().Name + ", errMsg=" + err.Message + ']'); } } catch (Exception e) { // Something went wrong during marshaling. Finish(default(TR), e); stream.Reset(); writer.WriteBoolean(false); // Map failed. writer.WriteString(e.Message); // Write error message. } finally { prj.Marshaller.FinishMarshal(writer); } }
/// <summary> /// Invokes a store operation. /// </summary> /// <param name="stream">Input stream.</param> /// <param name="grid">Grid.</param> /// <returns>Invocation result.</returns> /// <exception cref="IgniteException">Invalid operation type: + opType</exception> public long Invoke(PlatformMemoryStream stream, Ignite grid) { return(_store.Invoke(stream, grid)); }
public void TestPlatformMemoryStream() { var stream = new PlatformMemoryStream(GetMemory()); TestStream(stream, false, () => stream.SynchronizeOutput()); }
/// <summary> /// Execute job serializing result to the stream. /// </summary> /// <param name="cancel">Whether the job must be cancelled.</param> /// <param name="stream">Stream.</param> public void ExecuteRemote(PlatformMemoryStream stream, bool cancel) { ComputeRunner.ExecuteJobAndWriteResults(_ignite, stream, _job, _ => Execute0(cancel)); }