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