Search in sources :

Example 1 with JobProcessor

use of jetbrains.exodus.core.execution.JobProcessor in project xodus by JetBrains.

the class StoreTest method concurrentPutLikeJetPass.

private void concurrentPutLikeJetPass(@NotNull final StoreConfig config) {
    env.getEnvironmentConfig().setGcEnabled(false);
    final Store store = openStoreAutoCommit("store", config);
    final JobProcessor processor = new MultiThreadDelegatingJobProcessor("ConcurrentPutProcessor", 8) {
    };
    processor.setExceptionHandler(new JobProcessorExceptionHandler() {

        @Override
        public void handle(JobProcessor processor, Job job, Throwable t) {
            t.printStackTrace(System.out);
        }
    });
    processor.start();
    final int count = 50000;
    final LongSet keys = new LongHashSet();
    for (int i = 0; i < count; ++i) {
        processor.queue(new Job() {

            @Override
            protected void execute() {
                env.executeInTransaction(new TransactionalExecutable() {

                    @Override
                    public void execute(@NotNull final Transaction txn) {
                        final long key = randomLong();
                        store.put(txn, LongBinding.longToCompressedEntry(key), getValue());
                        if (txn.flush()) {
                            final boolean added;
                            synchronized (keys) {
                                added = keys.add(key);
                            }
                            if (!added) {
                                System.out.println("Happy birthday paradox!");
                            }
                        }
                    }
                });
            }
        });
    }
    processor.waitForJobs(10);
    processor.finish();
    assertEquals(count, keys.size());
    env.executeInTransaction(new TransactionalExecutable() {

        @Override
        public void execute(@NotNull final Transaction txn) {
            final long[] longs = keys.toLongArray();
            for (long key : longs) {
                assertEquals(getValue(), store.get(txn, LongBinding.longToCompressedEntry(key)));
            }
            Arrays.sort(longs);
            try (Cursor cursor = store.openCursor(txn)) {
                int i = 0;
                while (cursor.getNext()) {
                    assertEquals(longs[i++], LongBinding.compressedEntryToLong(cursor.getKey()));
                }
                assertEquals(count, i);
            }
        }
    });
}
Also used : MultiThreadDelegatingJobProcessor(jetbrains.exodus.core.execution.MultiThreadDelegatingJobProcessor) JobProcessor(jetbrains.exodus.core.execution.JobProcessor) LongSet(jetbrains.exodus.core.dataStructures.hash.LongSet) MultiThreadDelegatingJobProcessor(jetbrains.exodus.core.execution.MultiThreadDelegatingJobProcessor) NotNull(org.jetbrains.annotations.NotNull) LongHashSet(jetbrains.exodus.core.dataStructures.hash.LongHashSet) Job(jetbrains.exodus.core.execution.Job) JobProcessorExceptionHandler(jetbrains.exodus.core.execution.JobProcessorExceptionHandler)

Example 2 with JobProcessor

use of jetbrains.exodus.core.execution.JobProcessor in project xodus by JetBrains.

the class GarbageCollectorTestInMemory method testTextIndexLikeWithDeletionsAndConcurrentReading.

@Test
public void testTextIndexLikeWithDeletionsAndConcurrentReading() throws InterruptedException, BrokenBarrierException {
    final long started = System.currentTimeMillis();
    prepare();
    final Transaction txn = env.beginTransaction();
    final Store store = env.openStore("store", getStoreConfig(false), txn);
    final Store storeDups = env.openStore("storeDups", getStoreConfig(true), txn);
    txn.commit();
    final Throwable[] throwable = { null };
    final JobProcessor[] processors = new JobProcessor[10];
    for (int i = 0; i < processors.length; ++i) {
        processors[i] = ThreadJobProcessorPool.getOrCreateJobProcessor("test processor" + i);
        processors[i].start();
    }
    final CyclicBarrier barrier = new CyclicBarrier(processors.length + 1);
    processors[0].queue(new Job() {

        @Override
        protected void execute() throws Throwable {
            barrier.await();
            try {
                while (System.currentTimeMillis() - started < TEST_DURATION) {
                    env.executeInTransaction(new TransactionalExecutable() {

                        @Override
                        public void execute(@NotNull final Transaction txn) {
                            int randomInt = rnd.nextInt() & 0x3fffffff;
                            final int count = 4 + (randomInt) & 0x1f;
                            for (int j = 0; j < count; randomInt += ++j) {
                                final int intKey = randomInt & 0x3fff;
                                final ArrayByteIterable key = IntegerBinding.intToCompressedEntry(intKey);
                                final int valueLength = 50 + (randomInt % 100);
                                store.put(txn, key, new ArrayByteIterable(new byte[valueLength]));
                                storeDups.put(txn, key, IntegerBinding.intToEntry(randomInt % 32));
                            }
                            randomInt = (randomInt * randomInt) & 0x3fffffff;
                            for (int j = 0; j < count; randomInt += ++j) {
                                final int intKey = randomInt & 0x3fff;
                                final ArrayByteIterable key = IntegerBinding.intToCompressedEntry(intKey);
                                store.delete(txn, key);
                                try (Cursor cursor = storeDups.openCursor(txn)) {
                                    if (cursor.getSearchBoth(key, IntegerBinding.intToEntry(randomInt % 32))) {
                                        cursor.deleteCurrent();
                                    }
                                }
                            }
                        }
                    });
                    Thread.sleep(0);
                }
            } catch (Throwable t) {
                throwable[0] = t;
            }
        }
    });
    for (int i = 1; i < processors.length; ++i) {
        processors[i].queue(new Job() {

            @Override
            protected void execute() throws Throwable {
                try {
                    barrier.await();
                    while (System.currentTimeMillis() - started < TEST_DURATION) {
                        int randomInt = rnd.nextInt() & 0x3fffffff;
                        for (int j = 0; j < 100; randomInt += ++j) {
                            final int intKey = randomInt & 0x3fff;
                            final ArrayByteIterable key = IntegerBinding.intToCompressedEntry(intKey);
                            getAutoCommit(store, key);
                            getAutoCommit(storeDups, key);
                            Thread.sleep(0);
                        }
                        Thread.sleep(50);
                    }
                } catch (Throwable t) {
                    throwable[0] = t;
                }
            }
        });
    }
    barrier.await();
    for (final JobProcessor processor : processors) {
        processor.finish();
    }
    final Throwable t = throwable[0];
    if (t != null) {
        memory.dump(new File(System.getProperty("user.home"), "dump"));
        logger.error("User code exception: ", t);
        Assert.assertTrue(false);
    }
}
Also used : JobProcessor(jetbrains.exodus.core.execution.JobProcessor) Store(jetbrains.exodus.env.Store) Cursor(jetbrains.exodus.env.Cursor) NotNull(org.jetbrains.annotations.NotNull) CyclicBarrier(java.util.concurrent.CyclicBarrier) TransactionalExecutable(jetbrains.exodus.env.TransactionalExecutable) Transaction(jetbrains.exodus.env.Transaction) ArrayByteIterable(jetbrains.exodus.ArrayByteIterable) Job(jetbrains.exodus.core.execution.Job) File(java.io.File) Test(org.junit.Test)

Example 3 with JobProcessor

use of jetbrains.exodus.core.execution.JobProcessor in project xodus by JetBrains.

the class EntitySnapshotTests method testConcurrentPutJetPassLike.

public void testConcurrentPutJetPassLike() {
    getEntityStore().getConfig().setCachingDisabled(true);
    final EnvironmentImpl environment = (EnvironmentImpl) getEntityStore().getEnvironment();
    final EnvironmentConfig config = environment.getEnvironmentConfig();
    // disable GC
    config.setGcEnabled(false);
    final JobProcessor processor = new MultiThreadDelegatingJobProcessor("ConcurrentPutProcessor", 8) {
    };
    processor.setExceptionHandler((processor1, job, t) -> logger.error("Background exception", t));
    processor.start();
    final int count = 30000;
    for (int i = 0; i < count; ++i) {
        final int id = i;
        processor.queue(new Job() {

            @Override
            protected void execute() {
                getEntityStore().executeInTransaction(txn -> {
                    final Entity ticket = txn.newEntity("CASTicket");
                    ticket.setProperty("id", id);
                });
            }
        });
    }
    processor.waitForJobs(100);
    processor.finish();
    getEntityStore().executeInTransaction(txn -> {
        // System.out.println("Structure id: " + executeMethod(environment, "getLastStructureId"));
        Assert.assertEquals(count, (int) txn.getAll("CASTicket").size());
        final EntityIterable sorted = txn.sort("CASTicket", "id", true);
        Assert.assertEquals(count, (int) sorted.size());
        int i = 0;
        for (final Entity ticket : sorted) {
            final Comparable id = ticket.getProperty("id");
            Assert.assertNotNull(id);
            Assert.assertEquals(i++, id);
        }
    });
}
Also used : MultiThreadDelegatingJobProcessor(jetbrains.exodus.core.execution.MultiThreadDelegatingJobProcessor) EnvironmentConfig(jetbrains.exodus.env.EnvironmentConfig) Job(jetbrains.exodus.core.execution.Job) EnvironmentImpl(jetbrains.exodus.env.EnvironmentImpl) JobProcessor(jetbrains.exodus.core.execution.JobProcessor) Assert(org.junit.Assert) EnvironmentConfig(jetbrains.exodus.env.EnvironmentConfig) MultiThreadDelegatingJobProcessor(jetbrains.exodus.core.execution.MultiThreadDelegatingJobProcessor) JobProcessor(jetbrains.exodus.core.execution.JobProcessor) MultiThreadDelegatingJobProcessor(jetbrains.exodus.core.execution.MultiThreadDelegatingJobProcessor) EnvironmentImpl(jetbrains.exodus.env.EnvironmentImpl) Job(jetbrains.exodus.core.execution.Job)

Aggregations

Job (jetbrains.exodus.core.execution.Job)3 JobProcessor (jetbrains.exodus.core.execution.JobProcessor)3 MultiThreadDelegatingJobProcessor (jetbrains.exodus.core.execution.MultiThreadDelegatingJobProcessor)2 NotNull (org.jetbrains.annotations.NotNull)2 File (java.io.File)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 ArrayByteIterable (jetbrains.exodus.ArrayByteIterable)1 LongHashSet (jetbrains.exodus.core.dataStructures.hash.LongHashSet)1 LongSet (jetbrains.exodus.core.dataStructures.hash.LongSet)1 JobProcessorExceptionHandler (jetbrains.exodus.core.execution.JobProcessorExceptionHandler)1 Cursor (jetbrains.exodus.env.Cursor)1 EnvironmentConfig (jetbrains.exodus.env.EnvironmentConfig)1 EnvironmentImpl (jetbrains.exodus.env.EnvironmentImpl)1 Store (jetbrains.exodus.env.Store)1 Transaction (jetbrains.exodus.env.Transaction)1 TransactionalExecutable (jetbrains.exodus.env.TransactionalExecutable)1 Assert (org.junit.Assert)1 Test (org.junit.Test)1