You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@ignite.apache.org by Maksym Ieremenko <Ma...@qoniac.com> on 2018/07/30 21:00:33 UTC

Ignite.NET tasks cancellation

Hello all,

Ignite.NET, nuget package 2.6.0, .NETFramework 4.7.2

I am quite a novice with Apache Ignite. I am looking for solution to cancel running tasks on Ignite cluster.
Based on examples I tried following code:

    public class HangingDemo
    {
        public IIgnite Ignite { get; }

        public HangingDemo(IIgnite ignite)
        {
            Ignite = ignite;
        }

        public async Task Run()
        {
            using (var cts = new CancellationTokenSource(TimeSpan.FromSeconds(1)))
            {
                var cancelledJobsCount = await Ignite.GetCompute().ExecuteAsync(new SimpleTask(), 5, cts.Token);

                // never happens !!!
                Console.WriteLine("{0} jobs were cancelled", cancelledJobsCount);
            }
        }
    }

    public class SimpleTask : ComputeTaskSplitAdapter<int, bool, int>
    {
        protected override ICollection<IComputeJob<bool>> Split(int gridSize, int jobsCount)
        {
            return Enumerable
                .Range(1, jobsCount)
                .Select(i => new SimpleJob { Id = i })
                .Cast<IComputeJob<bool>>()
                .ToList();
        }

        public override int Reduce(IList<IComputeJobResult<bool>> results)
        {
            return results.Count(i => i.Data);
        }
    }

    public class SimpleJob : IComputeJob<bool>
    {
        private bool _isCancelled;

        public int Id { get; set; }

        public bool Execute()
        {
            Console.WriteLine("Start job {0}", Id);

            Thread.Sleep(TimeSpan.FromSeconds(2));

            return Volatile.Read(ref _isCancelled);
        }

        public void Cancel()
        {
            // never happens !!!
            Volatile.Write(ref _isCancelled, true);
        }
    }

Unfortunately the program hangs and the line "Console.WriteLine("{0} jobs were cancelled", cancelledJobsCount)" will never be reached.
Console output:
Start job 5
Start job 2
Start job 4
Start job 1
Start job 3
INFO  org.apache.ignite.internal.processors.rest.protocols.tcp.GridTcpRestProtocol [] Command protocol successfully stopped: TCP binary
WARN  org.apache.ignite.internal.processors.task.GridTaskProcessor [] Will cancel unfinished tasks due to stopping of the grid [cnt=1]

Questions:

1.       Program hangs.

2.       SimpleJob.Cancel is never called.

3.       Am I on a right direction? How I can cancel already running task/job?

Thank you for your help,
Max

Re: Ignite.NET tasks cancellation

Posted by "ilya.kasnacheev" <il...@gmail.com>.
Hello!

I try running this code, and I observe
"System.Threading.Tasks.TaskCanceledException: A task was canceled." in
Run() method.

This is not quite what you are expecting, as you seem to expect jobs to be
cancelled and not task, but from the code it's logical that it will happen.

As for code hanging, are you sure you're not getting any exceptions &
shaking off async properly?

Regards,





--
Sent from: http://apache-ignite-users.70518.x6.nabble.com/