Search in sources :

Example 1 with Job

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

the class GarbageCollector method doCleanFiles.

private boolean doCleanFiles(@NotNull final Iterator<Long> fragmentedFiles) {
    // if there are no more files then even don't start a txn
    if (!fragmentedFiles.hasNext()) {
        return true;
    }
    final LongSet cleanedFiles = new PackedLongHashSet();
    final ReadWriteTransaction txn;
    try {
        final TransactionBase tx = useRegularTxn ? env.beginTransaction() : env.beginGCTransaction();
        // tx can be read-only, so we should manually finish it (see XD-667)
        if (tx.isReadonly()) {
            tx.abort();
            return false;
        }
        txn = (ReadWriteTransaction) tx;
    } catch (TransactionAcquireTimeoutException ignore) {
        return false;
    }
    final boolean isTxnExclusive = txn.isExclusive();
    try {
        final OOMGuard guard = new OOMGuard();
        final long started = System.currentTimeMillis();
        while (fragmentedFiles.hasNext()) {
            final long fileAddress = fragmentedFiles.next();
            cleanSingleFile(fileAddress, txn);
            cleanedFiles.add(fileAddress);
            if (!isTxnExclusive) {
                // do not process more than one file in a non-exclusive txn
                break;
            }
            if (started + ec.getGcTransactionTimeout() < System.currentTimeMillis()) {
                // break by timeout
                break;
            }
            if (guard.isItCloseToOOM()) {
                // break because of the risk of OutOfMemoryError
                break;
            }
        }
        if (!txn.forceFlush()) {
            // paranoiac check
            if (isTxnExclusive) {
                throw new ExodusException("Can't be: exclusive txn should be successfully flushed");
            }
            return false;
        }
    } catch (Throwable e) {
        throw ExodusException.toExodusException(e);
    } finally {
        txn.abort();
    }
    if (!cleanedFiles.isEmpty()) {
        for (final Long file : cleanedFiles) {
            pendingFilesToDelete.add(file);
            utilizationProfile.removeFile(file);
        }
        utilizationProfile.estimateTotalBytes();
        env.executeTransactionSafeTask(new Runnable() {

            @Override
            public void run() {
                final int filesDeletionDelay = ec.getGcFilesDeletionDelay();
                if (filesDeletionDelay == 0) {
                    for (final Long file : cleanedFiles) {
                        deletionQueue.offer(file);
                    }
                } else {
                    DeferredIO.getJobProcessor().queueIn(new Job() {

                        @Override
                        protected void execute() {
                            for (final Long file : cleanedFiles) {
                                deletionQueue.offer(file);
                            }
                        }
                    }, filesDeletionDelay);
                }
            }
        });
    }
    return true;
}
Also used : LongSet(jetbrains.exodus.core.dataStructures.hash.LongSet) PackedLongHashSet(jetbrains.exodus.core.dataStructures.hash.PackedLongHashSet) OOMGuard(jetbrains.exodus.runtime.OOMGuard) Job(jetbrains.exodus.core.execution.Job) ExodusException(jetbrains.exodus.ExodusException)

Example 2 with Job

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

the class EnvironmentLockTest method openConcurrentEnvironment.

private void openConcurrentEnvironment() {
    processor.start();
    new Job(processor) {

        @Override
        protected void execute() throws Throwable {
            final File dir = getEnvDirectory();
            try {
                env = newEnvironmentInstance(LogConfig.create(new FileDataReader(dir, 16), new FileDataWriter(dir, LOCK_ID)), new EnvironmentConfig().setLogLockTimeout(5000));
                wasOpened[0] = true;
            } catch (ExodusException e) {
                Assert.assertTrue(e.getMessage().contains(LOCK_ID));
                wasOpened[0] = false;
            }
        }
    };
}
Also used : FileDataReader(jetbrains.exodus.io.FileDataReader) FileDataWriter(jetbrains.exodus.io.FileDataWriter) Job(jetbrains.exodus.core.execution.Job) File(java.io.File) ExodusException(jetbrains.exodus.ExodusException)

Example 3 with Job

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

the class EntityTests method setOrAddPhantomLink.

private void setOrAddPhantomLink(final boolean setLink) {
    final PersistentEntityStoreImpl store = getEntityStore();
    store.getEnvironment().getEnvironmentConfig().setGcEnabled(false);
    store.getConfig().setDebugTestLinkedEntities(true);
    final Entity issue = store.computeInTransaction(new StoreTransactionalComputable<Entity>() {

        @Override
        public Entity compute(@NotNull StoreTransaction txn) {
            return txn.newEntity("Issue");
        }
    });
    final Entity comment = store.computeInTransaction(new StoreTransactionalComputable<Entity>() {

        @Override
        public Entity compute(@NotNull StoreTransaction txn) {
            return txn.newEntity("Comment");
        }
    });
    final CountDownLatch startBoth = new CountDownLatch(2);
    final Semaphore deleted = new Semaphore(0);
    DeferredIO.getJobProcessor().queue(new Job() {

        @Override
        protected void execute() {
            store.executeInTransaction(new StoreTransactionalExecutable() {

                @Override
                public void execute(@NotNull StoreTransaction txn) {
                    startBoth.countDown();
                    try {
                        startBoth.await();
                    } catch (InterruptedException ignore) {
                    }
                    comment.delete();
                    txn.flush();
                    deleted.release();
                }
            });
        }
    });
    final int[] i = { 0 };
    TestUtil.runWithExpectedException(new Runnable() {

        @Override
        public void run() {
            store.executeInTransaction(new StoreTransactionalExecutable() {

                @Override
                public void execute(@NotNull StoreTransaction txn) {
                    final boolean first = i[0] == 0;
                    if (first) {
                        startBoth.countDown();
                        try {
                            startBoth.await();
                        } catch (InterruptedException ignore) {
                        }
                    }
                    ++i[0];
                    if (setLink) {
                        issue.setLink("comment", comment);
                    } else {
                        issue.addLink("comment", comment);
                    }
                    if (first) {
                        deleted.acquireUninterruptibly();
                    }
                }
            });
        }
    }, PhantomLinkException.class);
    Assert.assertEquals(2, i[0]);
    store.executeInReadonlyTransaction(new StoreTransactionalExecutable() {

        @Override
        public void execute(@NotNull StoreTransaction txn) {
            Assert.assertNull(issue.getLink("comment"));
        }
    });
}
Also used : Semaphore(java.util.concurrent.Semaphore) CountDownLatch(java.util.concurrent.CountDownLatch) NotNull(org.jetbrains.annotations.NotNull) Job(jetbrains.exodus.core.execution.Job)

Example 4 with Job

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

the class StressTests method testConcurrentRead.

public void testConcurrentRead() {
    final StoreTransaction txn = getStoreTransaction();
    createData(txn);
    ThreadJobProcessor[] procs = new ThreadJobProcessor[/*3*/
    2];
    for (int i = 0; i < procs.length; i++) {
        final int ii = i;
        ThreadJobProcessor proc = new ThreadJobProcessor("Processor" + 1);
        procs[i] = proc;
        proc.start();
        for (int j = 0; j < 10; /*00*/
        ++j) {
            final int jj = j;
            new Job(proc) {

                @Override
                protected void execute() throws Throwable {
                    final StoreTransaction s = getEntityStore().beginTransaction();
                    try {
                        final long now = System.currentTimeMillis();
                        s.find("Issue", "summary", "summary0", "summary" + (100 * ii + jj + 10000)).intersect(s.find("Issue", "created", now - 50000, now)).size();
                    } finally {
                        s.commit();
                    }
                }
            };
        }
    }
    for (final ThreadJobProcessor proc : procs) {
        proc.queueFinish();
    }
    for (ThreadJobProcessor proc : procs) {
        proc.waitUntilFinished();
    }
}
Also used : Job(jetbrains.exodus.core.execution.Job) ThreadJobProcessor(jetbrains.exodus.core.execution.ThreadJobProcessor)

Example 5 with Job

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

Aggregations

Job (jetbrains.exodus.core.execution.Job)9 NotNull (org.jetbrains.annotations.NotNull)4 JobProcessor (jetbrains.exodus.core.execution.JobProcessor)3 File (java.io.File)2 ExodusException (jetbrains.exodus.ExodusException)2 LongSet (jetbrains.exodus.core.dataStructures.hash.LongSet)2 JobProcessorExceptionHandler (jetbrains.exodus.core.execution.JobProcessorExceptionHandler)2 MultiThreadDelegatingJobProcessor (jetbrains.exodus.core.execution.MultiThreadDelegatingJobProcessor)2 CountDownLatch (java.util.concurrent.CountDownLatch)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 Semaphore (java.util.concurrent.Semaphore)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 ArrayByteIterable (jetbrains.exodus.ArrayByteIterable)1 LongArrayList (jetbrains.exodus.core.dataStructures.LongArrayList)1 LongHashMap (jetbrains.exodus.core.dataStructures.hash.LongHashMap)1 LongHashSet (jetbrains.exodus.core.dataStructures.hash.LongHashSet)1 LongIterator (jetbrains.exodus.core.dataStructures.hash.LongIterator)1 PackedLongHashSet (jetbrains.exodus.core.dataStructures.hash.PackedLongHashSet)1 ThreadJobProcessor (jetbrains.exodus.core.execution.ThreadJobProcessor)1 Latch (jetbrains.exodus.core.execution.locks.Latch)1