/* * 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. */ package org.apache.ignite.internal; import java.io.Serializable; import java.util.Collection; import java.util.Collections; import java.util.List; import org.apache.ignite.Ignite; import org.apache.ignite.IgniteException; import org.apache.ignite.IgniteLogger; import org.apache.ignite.compute.ComputeJob; import org.apache.ignite.compute.ComputeJobAdapter; import org.apache.ignite.compute.ComputeJobResult; import org.apache.ignite.compute.ComputeJobResultPolicy; import org.apache.ignite.compute.ComputeJobSibling; import org.apache.ignite.compute.ComputeTaskContinuousMapper; import org.apache.ignite.compute.ComputeTaskSession; import org.apache.ignite.compute.ComputeTaskSplitAdapter; import org.apache.ignite.resources.LoggerResource; import org.apache.ignite.resources.TaskContinuousMapperResource; import org.apache.ignite.resources.TaskSessionResource; import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; import org.apache.ignite.testframework.junits.common.GridCommonTest; /** * Test continuous mapper with siblings. */ @GridCommonTest(group = "Kernal Self") public class GridContinuousJobSiblingsSelfTest extends GridCommonAbstractTest { /** */ private static final int JOB_COUNT = 10; /** * @throws Exception If test failed. */ public void testContinuousJobSiblings() throws Exception { try { Ignite ignite = startGrid(0); startGrid(1); ignite.compute().execute(TestTask.class, null); } finally { stopAllGrids(); } } /** * @throws Exception If test failed. */ public void testContinuousJobSiblingsLocalNode() throws Exception { try { Ignite ignite = startGrid(0); compute(ignite.cluster().forLocal()).execute(TestTask.class, null); } finally { stopAllGrids(); } } /** */ private static class TestTask extends ComputeTaskSplitAdapter<Object, Object> { /** */ @TaskContinuousMapperResource private ComputeTaskContinuousMapper mapper; /** */ @TaskSessionResource private ComputeTaskSession ses; /** */ private volatile int jobCnt; /** {@inheritDoc} */ @Override protected Collection<? extends ComputeJob> split(int gridSize, Object arg) { return Collections.singleton(new TestJob(++jobCnt)); } /** {@inheritDoc} */ @Override public ComputeJobResultPolicy result(ComputeJobResult res, List<ComputeJobResult> received) { if (res.getException() != null) throw new IgniteException("Job resulted in error: " + res, res.getException()); assert ses.getJobSiblings().size() == jobCnt; if (jobCnt < JOB_COUNT) { mapper.send(new TestJob(++jobCnt)); assert ses.getJobSiblings().size() == jobCnt; } return ComputeJobResultPolicy.WAIT; } /** {@inheritDoc} */ @Override public Object reduce(List<ComputeJobResult> results) { assertEquals(JOB_COUNT, results.size()); return null; } } /** */ private static class TestJob extends ComputeJobAdapter { /** */ @TaskSessionResource private ComputeTaskSession ses; /** */ @LoggerResource private IgniteLogger log; /** * @param sibCnt Siblings count to check. */ TestJob(int sibCnt) { super(sibCnt); } /** {@inheritDoc} */ @Override public Serializable execute() { assert ses != null; assert argument(0) != null; Integer sibCnt = argument(0); log.info("Executing job."); assert sibCnt != null; Collection<ComputeJobSibling> sibs = ses.getJobSiblings(); assert sibs != null; assert sibs.size() == sibCnt : "Unexpected siblings collection [expectedSize=" + sibCnt + ", siblingsCnt=" + sibs.size() + ", siblings=" + sibs + ']'; return null; } } }