/*
 * 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.processors.cache;

import org.apache.ignite.configuration.*;
import org.apache.ignite.internal.*;
import org.apache.ignite.internal.processors.cache.query.*;
import org.apache.ignite.internal.util.typedef.*;
import org.apache.ignite.marshaller.optimized.*;

import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;

import static org.apache.ignite.cache.CacheMode.*;
import static org.apache.ignite.cache.CacheWriteSynchronizationMode.*;

/**
 * Multithreaded reduce query tests with lots of data.
 */
public class GridCacheReduceQueryMultithreadedSelfTest extends GridCacheAbstractSelfTest {
    /** */
    private static final int GRID_CNT = 5;

    /** */
    private static final int TEST_TIMEOUT = 2 * 60 * 1000;

    /** {@inheritDoc} */
    @Override protected int gridCount() {
        return GRID_CNT;
    }

    /** {@inheritDoc} */
    @Override protected long getTestTimeout() {
        return TEST_TIMEOUT;
    }

    /** {@inheritDoc} */
    @Override protected IgniteConfiguration getConfiguration(String gridName) throws Exception {
        IgniteConfiguration c = super.getConfiguration(gridName);

        c.setMarshaller(new OptimizedMarshaller(false));

        return c;
    }

    /** {@inheritDoc} */
    @Override protected CacheConfiguration cacheConfiguration(String gridName) throws Exception {
        CacheConfiguration cfg = super.cacheConfiguration(gridName);

        cfg.setCacheMode(PARTITIONED);
        cfg.setBackups(1);
        cfg.setWriteSynchronizationMode(FULL_SYNC);

        cfg.setIndexedTypes(
            String.class, Integer.class
        );

        return cfg;
    }

    /**
     * @throws Exception In case of error.
     */
    public void testReduceQuery() throws Exception {
        final int keyCnt = 5000;
        final int logFreq = 500;

        final GridCacheAdapter<String, Integer> c = internalCache(jcache());

        final CountDownLatch startLatch = new CountDownLatch(1);

        IgniteInternalFuture<?> fut1 = multithreadedAsync(new Callable() {
            @Override public Object call() throws Exception {
                for (int i = 1; i < keyCnt; i++) {
                    c.getAndPut(String.valueOf(i), i);

                    startLatch.countDown();

                    if (i % logFreq == 0)
                        info("Stored entries: " + i);
                }

                return null;
            }
        }, 1);

        // Create query.
        final CacheQuery<Map.Entry<String, Integer>> sumQry =
            c.queries().createSqlQuery(Integer.class, "_val > 0").timeout(TEST_TIMEOUT);

        final R1<Map.Entry<String, Integer>, Integer> rmtRdc = new R1<Map.Entry<String, Integer>, Integer>() {
            /** */
            private AtomicInteger sum = new AtomicInteger();

            @Override public boolean collect(Map.Entry<String, Integer> e) {
                sum.addAndGet(e.getValue());

                return true;
            }

            @Override public Integer reduce() {
                return sum.get();
            }
        };

        final AtomicBoolean stop = new AtomicBoolean();

        startLatch.await();

        IgniteInternalFuture<?> fut2 = multithreadedAsync(new Callable() {
            @Override public Object call() throws Exception {
                int cnt = 0;

                while (!stop.get()) {
                    Collection<Integer> res = sumQry.execute(rmtRdc).get();

                    int sum = F.sumInt(res);

                    cnt++;

                    assertTrue(sum > 0);

                    if (cnt % logFreq == 0) {
                        info("Reduced value: " + sum);
                        info("Executed queries: " + cnt);
                    }
                }

                return null;
            }
        }, 1);

        fut1.get();

        stop.set(true);

        fut2.get();
    }
}

@@ -2485,2253 +2485,7 @@
    %7D
-%0A%0A    /**%0A     * @throws Exception In case of error.%0A     */%0A    public void testReduceQuery() throws Exception %7B%0A        final int keyCnt = 5000;%0A        final int logFreq = 500;%0A%0A        final GridCacheAdapter%3CString, Integer%3E c = internalCache(jcache());%0A%0A        final CountDownLatch startLatch = new CountDownLatch(1);%0A%0A        IgniteInternalFuture%3C?%3E fut1 = multithreadedAsync(new Callable() %7B%0A            @Override public Object call() throws Exception %7B%0A                for (int i = 1; i %3C keyCnt; i++) %7B%0A                    c.getAndPut(String.valueOf(i), i);%0A%0A                    startLatch.countDown();%0A%0A                    if (i %25 logFreq == 0)%0A                        info(%22Stored entries: %22 + i);%0A                %7D%0A%0A                return null;%0A            %7D%0A        %7D, 1);%0A%0A        // Create query.%0A        final CacheQuery%3CMap.Entry%3CString, Integer%3E%3E sumQry =%0A            c.queries().createSqlQuery(Integer.class, %22_val %3E 0%22).timeout(TEST_TIMEOUT);%0A%0A        final R1%3CMap.Entry%3CString, Integer%3E, Integer%3E rmtRdc = new R1%3CMap.Entry%3CString, Integer%3E, Integer%3E() %7B%0A            /** */%0A            private AtomicInteger sum = new AtomicInteger();%0A%0A            @Override public boolean collect(Map.Entry%3CString, Integer%3E e) %7B%0A                sum.addAndGet(e.getValue());%0A%0A                return true;%0A            %7D%0A%0A            @Override public Integer reduce() %7B%0A                return sum.get();%0A            %7D%0A        %7D;%0A%0A        final AtomicBoolean stop = new AtomicBoolean();%0A%0A        startLatch.await();%0A%0A        IgniteInternalFuture%3C?%3E fut2 = multithreadedAsync(new Callable() %7B%0A            @Override public Object call() throws Exception %7B%0A                int cnt = 0;%0A%0A                while (!stop.get()) %7B%0A                    Collection%3CInteger%3E res = sumQry.execute(rmtRdc).get();%0A%0A                    int sum = F.sumInt(res);%0A%0A                    cnt++;%0A%0A                    assertTrue(sum %3E 0);%0A%0A                    if (cnt %25 logFreq == 0) %7B%0A                        info(%22Reduced value: %22 + sum);%0A                        info(%22Executed queries: %22 + cnt);%0A                    %7D%0A                %7D%0A%0A                return null;%0A            %7D%0A        %7D, 1);%0A%0A        fut1.get();%0A%0A        stop.set(true);%0A%0A        fut2.get();%0A    %7D
 %0A%7D%0A

Remove testReduceQuery from GridCacheReduceQueryMultithreadedSelfTest.