use of jetbrains.exodus.core.execution.Job 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);
}
}
});
}
use of jetbrains.exodus.core.execution.Job in project xodus by JetBrains.
the class EnvironmentTestsBase method executeParallelTransaction.
protected void executeParallelTransaction(@NotNull final TransactionalExecutable runnable) {
try {
final Latch sync = Latch.create();
sync.acquire();
processor.queue(new Job() {
@Override
protected void execute() throws Throwable {
env.executeInTransaction(runnable);
sync.release();
}
});
sync.acquire();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
// ignore
}
}
use of jetbrains.exodus.core.execution.Job 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);
}
}
use of jetbrains.exodus.core.execution.Job in project xodus by JetBrains.
the class FileSystemBlobVaultOld method flushBlobs.
@Override
public void flushBlobs(@Nullable final LongHashMap<InputStream> blobStreams, @Nullable final LongHashMap<File> blobFiles, @Nullable final LongSet deferredBlobsToDelete, @NotNull final Transaction txn) throws Exception {
if (blobStreams != null) {
blobStreams.forEachEntry(new ObjectProcedureThrows<Map.Entry<Long, InputStream>, Exception>() {
@Override
public boolean execute(final Map.Entry<Long, InputStream> object) throws Exception {
final InputStream stream = object.getValue();
stream.reset();
setContent(object.getKey(), stream);
return true;
}
});
}
// if there were blob files then move them
if (blobFiles != null) {
blobFiles.forEachEntry(new ObjectProcedureThrows<Map.Entry<Long, File>, Exception>() {
@Override
public boolean execute(final Map.Entry<Long, File> object) throws Exception {
setContent(object.getKey(), object.getValue());
return true;
}
});
}
// if there are deferred blobs to delete then defer their deletion
if (deferredBlobsToDelete != null) {
final LongArrayList copy = new LongArrayList(deferredBlobsToDelete.size());
final LongIterator it = deferredBlobsToDelete.iterator();
while (it.hasNext()) {
copy.add(it.nextLong());
}
final Environment environment = txn.getEnvironment();
environment.executeTransactionSafeTask(new Runnable() {
@Override
public void run() {
DeferredIO.getJobProcessor().queueIn(new Job() {
@Override
protected void execute() {
final long[] blobHandles = copy.getInstantArray();
for (int i = 0; i < copy.size(); ++i) {
delete(blobHandles[i]);
}
}
@Override
public String getName() {
return "Delete obsolete blob files";
}
@Override
public String getGroup() {
return environment.getLocation();
}
}, environment.getEnvironmentConfig().getGcFilesDeletionDelay());
}
});
}
}
Aggregations