Hi Chandrika,

You would need to make an assumption about your jobs duration to display
remaining time or percentage. For example, the below task predicts
remaining duration assuming remaining jobs will have duration equal to the
average of the completed ones :

@ComputeTaskSessionFullSupport
public class RandomSleepTask extends ComputeTaskSplitAdapter<Integer,
Integer> {

    @TaskSessionResource private ComputeTaskSession ses;
    private static final Random random = new Random();

    @Override protected Collection<? extends ComputeJob> split(int
gridSize, Integer total) throws IgniteException {
        final AtomicInteger cntr = new AtomicInteger(0);
        final AtomicInteger duration = new AtomicInteger(0);

        ses.addAttributeListener((key, val) -> {
            if ("COMPLETE".compareTo(key.toString()) == 0) {
                int newCntr = cntr.incrementAndGet();
                int newDuration = duration.addAndGet((int)val);

                // Predict remaining duration assuming remaining jobs
will have duration equal to the average of the
                // completed ones
                int avgDuration = newDuration / newCntr;
                int pendingTime = (total - newCntr) * avgDuration;

                System.out.format(
                    "%s\tout of %s\ttasks complete, %.2f\tmore seconds
remaining\n",
                    newCntr,
                    total,
                    pendingTime / 1000.0);
            }
        }, false);

        return IntStream.range(0, total).mapToObj(i -> new ComputeJobAdapter() {
            @Override public Object execute() throws IgniteException {
                int duration = 150 + random.nextInt(100);

                try {
                    Thread.sleep(duration);
                }
                catch (InterruptedException e) {
                    throw new IgniteException(e);
                }

                ses.setAttribute("COMPLETE", duration);

                return duration;
            }
        }).collect(Collectors.toList());
    }

    @Nullable @Override public Integer reduce(List<ComputeJobResult>
results) throws IgniteException {
        return results.stream().mapToInt(ComputeJobResult::getData).sum();
    }
}

Execute it with 10 jobs:

ignite.compute().execute(RandomSleepTask.class, 10);

And the output:

1       out of 10       tasks complete, 1.36    more seconds remaining
2       out of 10       tasks complete, 1.29    more seconds remaining
3       out of 10       tasks complete, 1.18    more seconds remaining
4       out of 10       tasks complete, 1.03    more seconds remaining
5       out of 10       tasks complete, 0.90    more seconds remaining
6       out of 10       tasks complete, 0.74    more seconds remaining
7       out of 10       tasks complete, 0.56    more seconds remaining
8       out of 10       tasks complete, 0.38    more seconds remaining
9       out of 10       tasks complete, 0.19    more seconds remaining
10      out of 10       tasks complete, 0.00    more seconds remaining

Reply via email to