Search in sources :

Example 26 with ExecutorCompletionService

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

the class PhreakConcurrencyTest method testMultipleConcurrentEPs3.

@Test
public void testMultipleConcurrentEPs3() {
    final KieSession ksession = getKieSessionWith3Segments();
    List<String> results = new ArrayList<String>();
    ksession.setGlobal("results", results);
    EPManipulator3[] epManipulators = new EPManipulator3[9];
    for (int i = 0; i < 9; i++) {
        epManipulators[i] = new EPManipulator3(ksession, i + 1);
    }
    for (int deleteIndex = 0; deleteIndex < 11; deleteIndex++) {
        boolean success = true;
        CompletionService<Boolean> ecs = new ExecutorCompletionService<Boolean>(executor);
        for (int i = 0; i < 9; i++) {
            ecs.submit(epManipulators[i].setDeleteIndex(deleteIndex % 10));
        }
        for (int i = 1; i < 10; i++) {
            try {
                success = ecs.take().get() && success;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        assertTrue(success);
        new Thread() {

            public void run() {
                ksession.fireUntilHalt();
            }
        }.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        ksession.halt();
        if (deleteIndex % 10 == 0) {
            assertEquals(3, results.size());
            assertTrue(results.containsAll(asList("R1", "R2", "R3")));
        } else {
            if (!results.isEmpty()) {
                fail("Results should be empty with deleteIndex = " + deleteIndex + "; got " + results.size() + " items");
            }
        }
        results.clear();
    }
}
Also used : ArrayList(java.util.ArrayList) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 27 with ExecutorCompletionService

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

the class PhreakConcurrencyTest method testMultipleConcurrentEPs.

@Test
public void testMultipleConcurrentEPs() {
    final boolean PARALLEL = true;
    final int EP_NR = 10;
    StringBuilder sb = new StringBuilder();
    sb.append("import org.drools.compiler.StockTick;\n");
    for (int i = 0; i < EP_NR; i++) {
        sb.append("global java.util.List results").append(i).append(";\n");
    }
    sb.append("declare StockTick\n" + "    @role( event )\n" + "end\n");
    for (int i = 0; i < EP_NR; i++) {
        sb.append("rule \"R" + i + "\"\n" + "when\n" + "    $name : String( this.startsWith(\"A\") )\n" + "    $st : StockTick( company == $name, price > 10 ) from entry-point EP" + i + "\n" + "then\n" + "    results" + i + ".add( $st );\n" + "end\n");
    }
    KieBase kbase = loadKnowledgeBaseFromString(sb.toString());
    KieSession ksession = kbase.newKieSession();
    boolean success = true;
    if (PARALLEL) {
        CompletionService<Boolean> ecs = new ExecutorCompletionService<Boolean>(executor);
        for (int i = 0; i < EP_NR; i++) {
            ecs.submit(new EPManipulator(ksession, i));
        }
        for (int i = 0; i < EP_NR; i++) {
            try {
                success = ecs.take().get() && success;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    } else {
        for (int i = 0; i < EP_NR; i++) {
            try {
                success = new EPManipulator(ksession, i).call() && success;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }
    assertTrue(success);
    assertEquals(EP_NR, ksession.fireAllRules());
    for (int i = 0; i < EP_NR; i++) {
        assertEquals(1, ((List) ksession.getGlobal("results" + i)).size());
    }
    ksession.dispose();
}
Also used : KieBase(org.kie.api.KieBase) KieSession(org.kie.api.runtime.KieSession) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) Test(org.junit.Test)

Example 28 with ExecutorCompletionService

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

the class PhreakConcurrencyTest method testMultipleConcurrentEPs4.

@Test
public void testMultipleConcurrentEPs4() {
    final KieSession ksession = getKieSessionWith3Segments();
    List<String> results = new ArrayList<String>();
    ksession.setGlobal("results", results);
    EPManipulator4[] epManipulators = new EPManipulator4[9];
    CyclicBarrier barrier = new CyclicBarrier(9, new SegmentChecker(epManipulators));
    for (int i = 0; i < 9; i++) {
        epManipulators[i] = new EPManipulator4(ksession, i + 1, barrier);
    }
    new Thread() {

        public void run() {
            ksession.fireUntilHalt();
        }
    }.start();
    for (int deleteIndex = 0; deleteIndex < 11; deleteIndex++) {
        boolean success = true;
        CompletionService<Boolean> ecs = new ExecutorCompletionService<Boolean>(executor);
        for (int i = 0; i < 9; i++) {
            ecs.submit(epManipulators[i].setDeleteIndex(deleteIndex % 10));
        }
        for (int i = 0; i < 9; i++) {
            try {
                success = ecs.take().get() && success;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        assertTrue(success);
    }
    ksession.halt();
}
Also used : ArrayList(java.util.ArrayList) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) CyclicBarrier(java.util.concurrent.CyclicBarrier) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 29 with ExecutorCompletionService

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

the class PropagationListTest method test.

@Test
@Ignore
public void test() {
    final int OBJECT_NR = 1000000;
    final int THREAD_NR = 8;
    Executor executor = Executors.newFixedThreadPool(THREAD_NR, new ThreadFactory() {

        public Thread newThread(Runnable r) {
            Thread t = new Thread(r);
            t.setDaemon(true);
            return t;
        }
    });
    long[] results = new long[10];
    for (int counter = 0; counter < results.length; ) {
        final Checker checker = new Checker(THREAD_NR);
        final PropagationList propagationList = new SynchronizedPropagationList(null);
        CompletionService<Boolean> ecs = new ExecutorCompletionService<Boolean>(executor);
        long start = System.nanoTime();
        for (int i = 0; i < THREAD_NR; i++) {
            ecs.submit(getTask(OBJECT_NR, checker, propagationList, i));
        }
        try {
            Thread.sleep(1L);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        for (int i = 0; i < THREAD_NR * 20; i++) {
            // System.out.println("FLUSHING!");
            propagationList.flush();
            try {
                Thread.sleep(1L);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        boolean success = true;
        for (int i = 0; i < THREAD_NR; i++) {
            try {
                success = ecs.take().get() && success;
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        propagationList.flush();
        results[counter++] = System.nanoTime() - start;
        System.out.println("Threads DONE!");
    }
    analyzeResults(results);
}
Also used : ThreadFactory(java.util.concurrent.ThreadFactory) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) Executor(java.util.concurrent.Executor) SynchronizedPropagationList(org.drools.core.phreak.SynchronizedPropagationList) SynchronizedPropagationList(org.drools.core.phreak.SynchronizedPropagationList) PropagationList(org.drools.core.phreak.PropagationList) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 30 with ExecutorCompletionService

use of java.util.concurrent.ExecutorCompletionService in project hadoop by apache.

the class TestBatchIbr method runIbrTest.

static void runIbrTest(final long ibrInterval) throws Exception {
    final ExecutorService executor = createExecutor();
    final Random ran = new Random();
    final Configuration conf = newConf(ibrInterval);
    final MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf).numDataNodes(NUM_DATANODES).build();
    final DistributedFileSystem dfs = cluster.getFileSystem();
    try {
        final String dirPathString = "/dir";
        final Path dir = new Path(dirPathString);
        dfs.mkdirs(dir);
        // start testing
        final long testStartTime = Time.monotonicNow();
        final ExecutorCompletionService<Path> createService = new ExecutorCompletionService<>(executor);
        final AtomicLong createFileTime = new AtomicLong();
        final AtomicInteger numBlockCreated = new AtomicInteger();
        // create files
        for (int i = 0; i < NUM_FILES; i++) {
            createService.submit(new Callable<Path>() {

                @Override
                public Path call() throws Exception {
                    final long start = Time.monotonicNow();
                    try {
                        final long seed = ran.nextLong();
                        final int numBlocks = ran.nextInt(MAX_BLOCK_NUM) + 1;
                        numBlockCreated.addAndGet(numBlocks);
                        return createFile(dir, numBlocks, seed, dfs);
                    } finally {
                        createFileTime.addAndGet(Time.monotonicNow() - start);
                    }
                }
            });
        }
        // verify files
        final ExecutorCompletionService<Boolean> verifyService = new ExecutorCompletionService<>(executor);
        final AtomicLong verifyFileTime = new AtomicLong();
        for (int i = 0; i < NUM_FILES; i++) {
            final Path file = createService.take().get();
            verifyService.submit(new Callable<Boolean>() {

                @Override
                public Boolean call() throws Exception {
                    final long start = Time.monotonicNow();
                    try {
                        return verifyFile(file, dfs);
                    } finally {
                        verifyFileTime.addAndGet(Time.monotonicNow() - start);
                    }
                }
            });
        }
        for (int i = 0; i < NUM_FILES; i++) {
            Assert.assertTrue(verifyService.take().get());
        }
        final long testEndTime = Time.monotonicNow();
        LOG.info("ibrInterval=" + ibrInterval + " (" + toConfString(DFS_BLOCKREPORT_INCREMENTAL_INTERVAL_MSEC_KEY, conf) + "), numBlockCreated=" + numBlockCreated);
        LOG.info("duration=" + toSecondString(testEndTime - testStartTime) + ", createFileTime=" + toSecondString(createFileTime.get()) + ", verifyFileTime=" + toSecondString(verifyFileTime.get()));
        LOG.info("NUM_FILES=" + NUM_FILES + ", MAX_BLOCK_NUM=" + MAX_BLOCK_NUM + ", BLOCK_SIZE=" + BLOCK_SIZE + ", NUM_THREADS=" + NUM_THREADS + ", NUM_DATANODES=" + NUM_DATANODES);
        logIbrCounts(cluster.getDataNodes());
    } finally {
        executor.shutdown();
        cluster.shutdown();
    }
}
Also used : Path(org.apache.hadoop.fs.Path) MiniDFSCluster(org.apache.hadoop.hdfs.MiniDFSCluster) Configuration(org.apache.hadoop.conf.Configuration) HdfsConfiguration(org.apache.hadoop.hdfs.HdfsConfiguration) MetricsRecordBuilder(org.apache.hadoop.metrics2.MetricsRecordBuilder) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) DistributedFileSystem(org.apache.hadoop.hdfs.DistributedFileSystem) IOException(java.io.IOException) AtomicLong(java.util.concurrent.atomic.AtomicLong) Random(java.util.Random) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService)

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