Search in sources :

Example 66 with ExecutorCompletionService

use of java.util.concurrent.ExecutorCompletionService in project drools by kiegroup.

the class AbstractConcurrentInsertionsTest method testConcurrentInsertions.

protected void testConcurrentInsertions(final String drl, final int objectCount, final int threadCount, final boolean newSessionForEachThread, final boolean updateFacts) throws InterruptedException {
    final KieBase kieBase = new KieHelper().addContent(drl, ResourceType.DRL).build();
    final ExecutorService executor = Executors.newCachedThreadPool(r -> {
        final Thread t = new Thread(r);
        t.setDaemon(true);
        return t;
    });
    KieSession ksession = null;
    try {
        final Callable[] tasks = new Callable[threadCount];
        if (newSessionForEachThread) {
            for (int i = 0; i < threadCount; i++) {
                tasks[i] = getTask(objectCount, kieBase, updateFacts);
            }
        } else {
            ksession = kieBase.newKieSession();
            for (int i = 0; i < threadCount; i++) {
                tasks[i] = getTask(objectCount, ksession, false, updateFacts);
            }
        }
        final CompletionService<Boolean> ecs = new ExecutorCompletionService<>(executor);
        for (final Callable task : tasks) {
            ecs.submit(task);
        }
        int successCounter = 0;
        for (int i = 0; i < threadCount; i++) {
            try {
                if (ecs.take().get()) {
                    successCounter++;
                }
            } catch (final Exception e) {
                throw new RuntimeException(e);
            }
        }
        Assertions.assertThat(successCounter).isEqualTo(threadCount);
        if (ksession != null) {
            ksession.dispose();
        }
    } finally {
        executor.shutdown();
        if (!executor.awaitTermination(5, TimeUnit.SECONDS)) {
            executor.shutdownNow();
        }
    }
}
Also used : KieHelper(org.kie.internal.utils.KieHelper) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) Callable(java.util.concurrent.Callable) KieBase(org.kie.api.KieBase) ExecutorService(java.util.concurrent.ExecutorService) KieSession(org.kie.api.runtime.KieSession)

Example 67 with ExecutorCompletionService

use of java.util.concurrent.ExecutorCompletionService in project drools by kiegroup.

the class MultithreadTest method testSlidingTimeWindows.

@Test(timeout = 1000000)
public void testSlidingTimeWindows() {
    final String str = "package org.drools\n" + "global java.util.List list; \n" + "import " + StockTick.class.getCanonicalName() + "; \n" + "" + "declare StockTick @role(event) end\n" + "" + "rule R\n" + "when\n" + " accumulate( $st : StockTick() over window:time(400ms)\n" + "             from entry-point X,\n" + "             $c : count(1) )" + "then\n" + "   list.add( $c ); \n" + "end \n";
    final List<Exception> errors = new ArrayList<Exception>();
    final KieBaseConfiguration kbconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kbconf.setOption(EventProcessingOption.STREAM);
    final KieBase kbase = loadKnowledgeBaseFromString(kbconf, str);
    final KieSession ksession = kbase.newKieSession();
    final EntryPoint ep = ksession.getEntryPoint("X");
    final List list = new ArrayList();
    ksession.setGlobal("list", list);
    final Executor executor = Executors.newCachedThreadPool(new ThreadFactory() {

        public Thread newThread(final Runnable r) {
            final Thread t = new Thread(r);
            t.setDaemon(true);
            return t;
        }
    });
    // runs for 10 seconds
    final int RUN_TIME = 5000;
    final int THREAD_NR = 2;
    final CompletionService<Boolean> ecs = new ExecutorCompletionService<Boolean>(executor);
    ecs.submit(new Callable<Boolean>() {

        public Boolean call() throws Exception {
            try {
                ksession.fireUntilHalt();
                return true;
            } catch (final Exception e) {
                errors.add(e);
                e.printStackTrace();
                return false;
            }
        }
    });
    for (int i = 0; i < THREAD_NR; i++) {
        ecs.submit(new Callable<Boolean>() {

            public Boolean call() throws Exception {
                try {
                    final String s = Thread.currentThread().getName();
                    final long endTS = System.currentTimeMillis() + RUN_TIME;
                    int j = 0;
                    long lastTimeInserted = -1;
                    while (System.currentTimeMillis() < endTS) {
                        final long currentTimeInMillis = System.currentTimeMillis();
                        if (currentTimeInMillis > lastTimeInserted) {
                            lastTimeInserted = currentTimeInMillis;
                            ep.insert(new StockTick(j++, s, 0.0, 0));
                        }
                    }
                    return true;
                } catch (final Exception e) {
                    errors.add(e);
                    e.printStackTrace();
                    return false;
                }
            }
        });
    }
    boolean success = true;
    for (int i = 0; i < THREAD_NR; i++) {
        try {
            success = ecs.take().get() && success;
        } catch (final Exception e) {
            errors.add(e);
        }
    }
    ksession.halt();
    try {
        success = ecs.take().get() && success;
    } catch (final Exception e) {
        errors.add(e);
    }
    for (final Exception e : errors) {
        e.printStackTrace();
    }
    Assertions.assertThat(errors).isEmpty();
    Assertions.assertThat(success).isTrue();
    ksession.dispose();
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) ArrayList(java.util.ArrayList) EntryPoint(org.kie.api.runtime.rule.EntryPoint) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieBaseConfiguration(org.kie.api.KieBaseConfiguration) Executor(java.util.concurrent.Executor) StockTick(org.drools.compiler.StockTick) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 68 with ExecutorCompletionService

use of java.util.concurrent.ExecutorCompletionService in project drools by kiegroup.

the class PhreakConcurrencyTest method testMultipleConcurrentEPs2.

@Test(timeout = 10000)
public void testMultipleConcurrentEPs2() {
    String str = "global java.util.List results\n" + "\n" + "rule \"R0\" when\n" + "    $s : String( ) from entry-point EP0\n" + "    $i : Integer( toString().equals($s) ) from entry-point EP1\n" + "    $l : Long( intValue() == $i ) from entry-point EP2\n" + "then\n" + "    results.add( $s );\n" + "end\n" + "\n" + "rule \"R1\" when\n" + "    $s : String( ) from entry-point EP1\n" + "    $i : Integer( toString().equals($s) ) from entry-point EP2\n" + "    $l : Long( intValue() == $i ) from entry-point EP0\n" + "then\n" + "    results.add( $s );\n" + "end\n" + "\n" + "rule \"R2\" when\n" + "    $s : String( ) from entry-point EP2\n" + "    $i : Integer( toString().equals($s) ) from entry-point EP0\n" + "    $l : Long( intValue() == $i ) from entry-point EP1\n" + "then\n" + "    results.add( $s );\n" + "end\n";
    KieBase kbase = loadKnowledgeBaseFromString(str);
    KieSession ksession = kbase.newKieSession();
    List<String> results = new ArrayList<String>();
    ksession.setGlobal("results", results);
    boolean success = true;
    CompletionService<Boolean> ecs = new ExecutorCompletionService<Boolean>(executor);
    for (int i = 0; i < 3; i++) {
        ecs.submit(new EPManipulator2(ksession, i));
    }
    for (int i = 0; i < 3; i++) {
        try {
            success = ecs.take().get() && success;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    assertTrue(success);
    ksession.fireAllRules();
    System.out.println(results);
    assertEquals(3, results.size());
    for (String s : results) {
        assertEquals("2", s);
    }
}
Also used : ArrayList(java.util.ArrayList) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 69 with ExecutorCompletionService

use of java.util.concurrent.ExecutorCompletionService in project drools by kiegroup.

the class Misc2Test method testFunctionInvokingFunction.

@Test
public void testFunctionInvokingFunction() throws Exception {
    // DROOLS-926
    String drl = "function boolean isOdd(int i) {\n" + "    return i % 2 == 1;\n" + "}\n" + "\n" + "function boolean isEven(int i) {\n" + "    return !isOdd(i);\n" + "}\n" + "\n" + "global java.util.List list;\n" + "\n" + "rule R when\n" + "    $i : Integer( isEven( this ) ) \n" + "then\n" + "    list.add($i);\n" + "end\n";
    final KieBase kbase = new KieHelper().addContent(drl, ResourceType.DRL).build();
    int parallelThreads = 10;
    ExecutorService executor = Executors.newFixedThreadPool(parallelThreads);
    Collection<Callable<Boolean>> solvers = new ArrayList<Callable<Boolean>>();
    for (int i = 0; i < parallelThreads; ++i) {
        solvers.add(new Callable<Boolean>() {

            @Override
            public Boolean call() throws Exception {
                KieSession ksession = kbase.newKieSession();
                List<Integer> list = new ArrayList<Integer>();
                ksession.setGlobal("list", list);
                for (int i = 0; i < 100; i++) {
                    ksession.insert(i);
                }
                ksession.fireAllRules();
                return list.size() == 50;
            }
        });
    }
    CompletionService<Boolean> ecs = new ExecutorCompletionService<Boolean>(executor);
    for (Callable<Boolean> s : solvers) {
        ecs.submit(s);
    }
    for (int i = 0; i < parallelThreads; ++i) {
        assertTrue(ecs.take().get());
    }
}
Also used : ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) Callable(java.util.concurrent.Callable) DroolsParserException(org.drools.compiler.compiler.DroolsParserException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) KieBase(org.kie.api.KieBase) ExecutorService(java.util.concurrent.ExecutorService) KieSession(org.kie.api.runtime.KieSession) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) Arrays.asList(java.util.Arrays.asList) ArrayList(java.util.ArrayList) List(java.util.List) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Test(org.junit.Test)

Aggregations

ExecutorCompletionService (java.util.concurrent.ExecutorCompletionService)69 ArrayList (java.util.ArrayList)31 ExecutorService (java.util.concurrent.ExecutorService)30 ExecutionException (java.util.concurrent.ExecutionException)27 IOException (java.io.IOException)24 Test (org.junit.Test)21 Future (java.util.concurrent.Future)18 List (java.util.List)10 InterruptedIOException (java.io.InterruptedIOException)9 Path (org.apache.hadoop.fs.Path)8 KieSession (org.kie.api.runtime.KieSession)8 Callable (java.util.concurrent.Callable)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)6 EntryPoint (org.kie.api.runtime.rule.EntryPoint)6 HashMap (java.util.HashMap)4 Executor (java.util.concurrent.Executor)4 TimeoutException (java.util.concurrent.TimeoutException)4 KieBase (org.kie.api.KieBase)4 Random (java.util.Random)3