Search in sources :

Example 1 with MultiThreadDelegatingJobProcessor

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

the class EntitySnapshotTests method testConcurrentPutJetPassLike.

public void testConcurrentPutJetPassLike() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
    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(new JobProcessorExceptionHandler() {

        @Override
        public void handle(JobProcessor processor, Job job, Throwable 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() throws Throwable {
                getEntityStore().executeInTransaction(new StoreTransactionalExecutable() {

                    @Override
                    public void execute(@NotNull final StoreTransaction txn) {
                        final Entity ticket = txn.newEntity("CASTicket");
                        ticket.setProperty("id", id);
                    }
                });
            }
        });
    }
    processor.waitForJobs(100);
    processor.finish();
    getEntityStore().executeInTransaction(new StoreTransactionalExecutable() {

        @Override
        public void execute(@NotNull final StoreTransaction 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 : 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) NotNull(org.jetbrains.annotations.NotNull) Job(jetbrains.exodus.core.execution.Job) JobProcessorExceptionHandler(jetbrains.exodus.core.execution.JobProcessorExceptionHandler)

Example 2 with MultiThreadDelegatingJobProcessor

use of jetbrains.exodus.core.execution.MultiThreadDelegatingJobProcessor 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)

Aggregations

Job (jetbrains.exodus.core.execution.Job)2 JobProcessor (jetbrains.exodus.core.execution.JobProcessor)2 JobProcessorExceptionHandler (jetbrains.exodus.core.execution.JobProcessorExceptionHandler)2 MultiThreadDelegatingJobProcessor (jetbrains.exodus.core.execution.MultiThreadDelegatingJobProcessor)2 NotNull (org.jetbrains.annotations.NotNull)2 LongHashSet (jetbrains.exodus.core.dataStructures.hash.LongHashSet)1 LongSet (jetbrains.exodus.core.dataStructures.hash.LongSet)1 EnvironmentConfig (jetbrains.exodus.env.EnvironmentConfig)1 EnvironmentImpl (jetbrains.exodus.env.EnvironmentImpl)1