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();
}
}
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();
}
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();
}
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);
}
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();
}
}
Aggregations