Search in sources :

Example 1 with CasProvider

use of de.tudarmstadt.ukp.clarin.webanno.api.CasProvider in project webanno by webanno.

the class CasStorageServiceImplTest method testHighConcurrencyIncludingDeletion.

@Test
public void testHighConcurrencyIncludingDeletion() throws Exception {
    CasProvider initializer = () -> {
        try {
            CAS cas = createCas(mergeTypeSystems(asList(createTypeSystemDescription(), getInternalTypeSystem())));
            cas.setDocumentText(repeat("This is a test.\n", 100_000));
            return cas;
        } catch (ResourceInitializationException e) {
            throw new IOException(e);
        }
    };
    SourceDocument doc = makeSourceDocument(7l, 7l, "doc");
    String user = "annotator";
    // We interleave all the primary and secondary tasks into the main tasks list
    // Primary tasks run for a certain number of iterations
    // Secondary tasks run as long as any primary task is still running
    List<Thread> tasks = new ArrayList<>();
    List<Thread> primaryTasks = new ArrayList<>();
    List<Thread> secondaryTasks = new ArrayList<>();
    int threadGroupCount = 4;
    int iterations = 100;
    for (int n = 0; n < threadGroupCount; n++) {
        Thread rw = new ExclusiveReadWriteTask(n, doc, user, initializer, iterations);
        primaryTasks.add(rw);
        tasks.add(rw);
        Thread ro = new SharedReadOnlyTask(n, doc, user, initializer);
        secondaryTasks.add(ro);
        tasks.add(ro);
        Thread un = new UnmanagedTask(n, doc, user, initializer);
        secondaryTasks.add(un);
        tasks.add(un);
        Thread uni = new UnmanagedNonInitializingTask(n, doc, user);
        secondaryTasks.add(uni);
        tasks.add(uni);
        DeleterTask xx = new DeleterTask(n, doc, user);
        secondaryTasks.add(xx);
        tasks.add(xx);
    }
    log.info("---- Starting all threads ----");
    tasks.forEach(Thread::start);
    log.info("---- Wait for primary threads to complete ----");
    boolean done = false;
    while (!done) {
        long running = primaryTasks.stream().filter(Thread::isAlive).count();
        done = running == 0l;
        sleep(1000);
        log.info("running {}  complete {}%  rw {}  ro {}  un {}  uni {}  xx {} XX {}", running, (writeCounter.get() * 100) / (threadGroupCount * iterations), writeCounter, managedReadCounter, unmanagedReadCounter, unmanagedNonInitializingReadCounter, deleteCounter, deleteInitialCounter);
    }
    log.info("---- Wait for threads secondary threads to wrap up ----");
    rwTasksCompleted.set(true);
    for (Thread thread : secondaryTasks) {
        thread.join();
    }
    log.info("---- Test is done ----");
    assertThat(exception).isFalse();
}
Also used : SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) ArrayList(java.util.ArrayList) IOException(java.io.IOException) CasProvider(de.tudarmstadt.ukp.clarin.webanno.api.CasProvider) CAS(org.apache.uima.cas.CAS) ResourceInitializationException(org.apache.uima.resource.ResourceInitializationException) Test(org.junit.Test)

Example 2 with CasProvider

use of de.tudarmstadt.ukp.clarin.webanno.api.CasProvider in project webanno by webanno.

the class CasStorageServiceImplTest method testHighConcurrencyWithoutDeletion.

@Test
public void testHighConcurrencyWithoutDeletion() throws Exception {
    CasProvider initializer = () -> {
        try {
            CAS cas = createCas(mergeTypeSystems(asList(createTypeSystemDescription(), getInternalTypeSystem())));
            cas.setDocumentText(repeat("This is a test.\n", 100_000));
            return cas;
        } catch (ResourceInitializationException e) {
            throw new IOException(e);
        }
    };
    CasProvider badSeed = () -> {
        throw new IOException("This initializer should never be called!");
    };
    SourceDocument doc = makeSourceDocument(8l, 8l, "doc");
    String user = "annotator";
    try (CasStorageSession session = openNested()) {
        // Make sure the CAS exists so that the threads should never be forced to call the
        // the initializer
        sut.readOrCreateCas(doc, user, FORCE_CAS_UPGRADE, initializer, EXCLUSIVE_WRITE_ACCESS);
    }
    // We interleave all the primary and secondary tasks into the main tasks list
    // Primary tasks run for a certain number of iterations
    // Secondary tasks run as long as any primary task is still running
    List<Thread> tasks = new ArrayList<>();
    List<Thread> primaryTasks = new ArrayList<>();
    List<Thread> secondaryTasks = new ArrayList<>();
    int threadGroupCount = 4;
    int iterations = 100;
    for (int n = 0; n < threadGroupCount; n++) {
        ExclusiveReadWriteTask rw = new ExclusiveReadWriteTask(n, doc, user, badSeed, iterations);
        primaryTasks.add(rw);
        tasks.add(rw);
        Thread ro = new SharedReadOnlyTask(n, doc, user, badSeed);
        secondaryTasks.add(ro);
        tasks.add(ro);
        Thread un = new UnmanagedTask(n, doc, user, badSeed);
        secondaryTasks.add(un);
        tasks.add(un);
        Thread uni = new UnmanagedNonInitializingTask(n, doc, user);
        secondaryTasks.add(uni);
        tasks.add(uni);
    }
    log.info("---- Starting all threads ----");
    tasks.forEach(Thread::start);
    log.info("---- Wait for primary threads to complete ----");
    boolean done = false;
    while (!done) {
        long running = primaryTasks.stream().filter(Thread::isAlive).count();
        done = running == 0l;
        sleep(1000);
        log.info("running {}  complete {}%  rw {}  ro {}  un {}  uni {}", running, (writeCounter.get() * 100) / (threadGroupCount * iterations), writeCounter, managedReadCounter, unmanagedReadCounter, unmanagedNonInitializingReadCounter);
    }
    log.info("---- Wait for threads secondary threads to wrap up ----");
    rwTasksCompleted.set(true);
    for (Thread thread : secondaryTasks) {
        thread.join();
    }
    log.info("---- Test is done ----");
    assertThat(exception).isFalse();
}
Also used : SourceDocument(de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument) ArrayList(java.util.ArrayList) IOException(java.io.IOException) CasProvider(de.tudarmstadt.ukp.clarin.webanno.api.CasProvider) CAS(org.apache.uima.cas.CAS) ResourceInitializationException(org.apache.uima.resource.ResourceInitializationException) CasStorageSession(de.tudarmstadt.ukp.clarin.webanno.api.dao.casstorage.CasStorageSession) Test(org.junit.Test)

Aggregations

CasProvider (de.tudarmstadt.ukp.clarin.webanno.api.CasProvider)2 SourceDocument (de.tudarmstadt.ukp.clarin.webanno.model.SourceDocument)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 CAS (org.apache.uima.cas.CAS)2 ResourceInitializationException (org.apache.uima.resource.ResourceInitializationException)2 Test (org.junit.Test)2 CasStorageSession (de.tudarmstadt.ukp.clarin.webanno.api.dao.casstorage.CasStorageSession)1