Search in sources :

Example 1 with StoreConnection

use of org.apache.jena.tdb.StoreConnection in project jena by apache.

the class T_TransSystem method manyReaderAndOneWriter.

//@Test
public void manyReaderAndOneWriter() {
    final StoreConnection sConn = getStoreConnection();
    // Number of repeats, max pause
    Callable<?> procR = new Reader(sConn, readerSeqRepeats, readerMaxPause);
    // Number of repeats, max pause, commit. 
    Callable<?> procW_a = new Writer(sConn, writerAbortSeqRepeats, writerMaxPause, false);
    // Number of repeats, max pause, commit. 
    Callable<?> procW_c = new Writer(sConn, writerCommitSeqRepeats, writerMaxPause, true);
    // All threads start and queue on this - otherwise the thread start up as the executeor is loaded.
    // That can lead to uninterstin sequences of actions. 
    //new Semaphore(0) ;
    startPoint = null;
    int RN1 = 1;
    int RN2 = min(1, numReaderTasks);
    int RN3 = max(numReaderTasks - RN1 - RN2, 0);
    int WC1 = numWriterTasksC / 2;
    int WC2 = 2;
    int WC3 = numWriterTasksC - WC1 - WC2;
    int WA1 = numWriterTasksA / 2;
    int WA2 = numWriterTasksA - WA1;
    //System.out.println(RN1 + " " + RN2 + " " + RN3 + " " + WC1 + " " + WC2 + " " + WA1 + " " + WA2) ;
    int N = max(RN1, 0) + max(RN2, 0) + max(RN3, 0) + max(WC1, 0) + max(WC2, 0) + max(WA1, 0) + max(WA2, 0);
    //System.out.println(N) ;
    doneSignal = new CountDownLatch(N);
    // Define the query mix.
    submit(execService, procW_c, WC1, "COMMIT-");
    submit(execService, procW_a, WA1, "ABORT-");
    submit(execService, procR, RN1, "READ-");
    submit(execService, procW_c, WC2, "COMMIT-");
    submit(execService, procR, RN2, "READ-");
    submit(execService, procW_a, WA2, "ABORT-");
    submit(execService, procR, RN3, "READ-");
    submit(execService, procW_c, WC3, "COMMIT-");
    if (startPoint != null)
        // Let them all go.
        startPoint.release(4000);
    // Wait until all done.
    try {
        doneSignal.await();
    } catch (InterruptedException e) {
        e.printStackTrace(System.err);
    }
    try {
        // This is an orderly shutdown so followed by the awaitTermination
        // should wait for all threads, making the CountDownLatch unnecessary.
        // CountDownLatch added as a precaution while searching for JENA-163
        // which seems to see occasional uncleared out node journal files.
        execService.shutdown();
        if (!execService.awaitTermination(100, TimeUnit.SECONDS))
            System.err.println("Bad shutdown");
    } catch (InterruptedException e) {
        e.printStackTrace(System.err);
    }
}
Also used : StoreConnection(org.apache.jena.tdb.StoreConnection)

Example 2 with StoreConnection

use of org.apache.jena.tdb.StoreConnection in project jena by apache.

the class T_TxnDeadlockTest method test.

//@Test
public void test() {
    final StoreConnection storeConnection = StoreConnection.make(Location.mem());
    //ExecutorService executor = Executors.newCachedThreadPool()  ;     // Not seen blocking. 
    // 4 blocks maybe 1 in 4 times
    // 8 blocks (quad core) 2 in 3 times.
    ExecutorService executor = Executors.newFixedThreadPool(8);
    final AtomicInteger nbQuadruplesAdded = new AtomicInteger();
    final CountDownLatch doneSignal = new CountDownLatch(CONCURRENT_RANDOM_OPERATIONS);
    for (int i = 0; i < CONCURRENT_RANDOM_OPERATIONS; i++) {
        executor.execute(new Runnable() {

            @Override
            public void run() {
                try {
                    if (numberGenerator.nextInt(2) == 0) {
                        DatasetGraphTxn txnGraph = storeConnection.begin(ReadWrite.WRITE);
                        txnGraph.add(new Quad(NodeFactory.createURI("http://jena-example.org/" + numberGenerator.nextInt()), NodeFactory.createURI("http://jena-example.org/" + numberGenerator.nextInt()), NodeFactory.createURI("http://jena-example.org/" + numberGenerator.nextInt()), NodeFactory.createURI("http://jena-example.org/" + numberGenerator.nextInt())));
                        txnGraph.commit();
                        txnGraph.end();
                        nbQuadruplesAdded.incrementAndGet();
                    } else {
                        DatasetGraphTxn txnGraph = storeConnection.begin(ReadWrite.READ);
                        txnGraph.find(Node.ANY, Node.ANY, Node.ANY, Node.ANY);
                        //Iterator<Quad> iter = txnGraph.find(Node.ANY, Node.ANY, Node.ANY, Node.ANY);
                        //Iter.count(iter) ; // Consume
                        txnGraph.end();
                    }
                } finally {
                    doneSignal.countDown();
                }
            }
        });
    }
    // shutdown is orderly so sync'ing up before the shutdown is nice but not needed.
    try {
        doneSignal.await();
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    } finally {
        executor.shutdown();
    }
    DatasetGraphTxn txnGraph = storeConnection.begin(ReadWrite.READ);
    Iterator<Quad> result = txnGraph.find(Node.ANY, Node.ANY, Node.ANY, Node.ANY);
    long count = 0;
    while (result.hasNext()) {
        result.next();
        count++;
    }
    txnGraph.end();
    StoreConnection.release(storeConnection.getLocation());
    //System.out.println() ;
    System.out.println("FINISHED");
//        // This is unsafe - the quad adds may generate duplicates (ity's unlikly 4 random number reoccur but it's possible). 
//        Assert.assertEquals(count, nbQuadruplesAdded.get());
}
Also used : StoreConnection(org.apache.jena.tdb.StoreConnection) Quad(org.apache.jena.sparql.core.Quad) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DatasetGraphTxn(org.apache.jena.tdb.transaction.DatasetGraphTxn) ExecutorService(java.util.concurrent.ExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 3 with StoreConnection

use of org.apache.jena.tdb.StoreConnection in project jena by apache.

the class dumpnodetable method exec.

@Override
protected void exec() {
    List<String> tripleIndexes = Arrays.asList(Names.tripleIndexes);
    List<String> quadIndexes = Arrays.asList(Names.quadIndexes);
    Location loc = modLocation.getLocation();
    StoreConnection sConn = StoreConnection.make(loc);
    DatasetGraphTDB dsg = sConn.getBaseDataset();
    NodeTable nodeTable = dsg.getQuadTable().getNodeTupleTable().getNodeTable();
    dump(System.out, nodeTable);
}
Also used : StoreConnection(org.apache.jena.tdb.StoreConnection) NodeTable(org.apache.jena.tdb.store.nodetable.NodeTable) Location(org.apache.jena.tdb.base.file.Location) ModLocation(tdb.cmdline.ModLocation) DatasetGraphTDB(org.apache.jena.tdb.store.DatasetGraphTDB)

Example 4 with StoreConnection

use of org.apache.jena.tdb.StoreConnection in project jena by apache.

the class AbstractStoreConnections method store_6.

@Test
public void store_6() {
    // Transaction - release - reattach
    // This tests that the dataset is sync'ed when going into transactional mode.
    StoreConnection sConn = getStoreConnection();
    Location loc = sConn.getLocation();
    DatasetGraphTxn dsgTxn = sConn.begin(ReadWrite.WRITE);
    dsgTxn.add(q1);
    assertTrue(dsgTxn.contains(q1));
    dsgTxn.commit();
    dsgTxn.end();
    sConn.forceRecoverFromJournal();
    assertTrue(sConn.getBaseDataset().contains(q1));
    StoreConnection.release(loc);
    sConn = StoreConnection.make(loc);
    DatasetGraph dsg2 = sConn.getBaseDataset();
    assertTrue(dsg2.contains(q1));
    DatasetGraphTxn dsgTxn2 = sConn.begin(ReadWrite.READ);
    assertTrue(dsgTxn2.contains(q1));
    dsgTxn2.end();
}
Also used : StoreConnection(org.apache.jena.tdb.StoreConnection) DatasetGraphTxn(org.apache.jena.tdb.transaction.DatasetGraphTxn) Location(org.apache.jena.tdb.base.file.Location) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) Test(org.junit.Test) ConfigTest(org.apache.jena.tdb.ConfigTest) BaseTest(org.apache.jena.atlas.junit.BaseTest)

Example 5 with StoreConnection

use of org.apache.jena.tdb.StoreConnection in project jena by apache.

the class AbstractStoreConnections method store_7.

@Test
public void store_7() {
    // No transaction, plain update, then transaction.
    // This tests that the dataset is sync'ed when going into transactional mode.
    boolean nonTxnData = true;
    StoreConnection sConn = getStoreConnection();
    Location loc = sConn.getLocation();
    DatasetGraph dsg = sConn.getBaseDataset();
    if (nonTxnData) {
        dsg.add(q);
        TDB.sync(dsg);
        assertTrue(dsg.contains(q));
    }
    DatasetGraphTxn dsgTxn = sConn.begin(ReadWrite.WRITE);
    if (nonTxnData)
        assertTrue(dsgTxn.contains(q));
    dsgTxn.add(q1);
    assertTrue(dsgTxn.contains(q1));
    if (nonTxnData)
        assertTrue(dsgTxn.contains(q));
    dsgTxn.commit();
    dsgTxn.end();
    // Should have flushed to disk.
    if (nonTxnData) {
        sConn.forceRecoverFromJournal();
        assertTrue(dsg.contains(q));
    }
    assertTrue(dsg.contains(q1));
    // release via the transactional machinery
    StoreConnection.release(loc);
    sConn = null;
    StoreConnection sConn2 = StoreConnection.make(loc);
    DatasetGraph dsg2 = sConn2.getBaseDataset();
    if (nonTxnData)
        assertTrue(dsg2.contains(q));
    assertTrue(dsg2.contains(q1));
    DatasetGraphTxn dsgTxn2 = sConn2.begin(ReadWrite.READ);
    if (nonTxnData)
        assertTrue(dsgTxn2.contains(q));
    assertTrue(dsgTxn2.contains(q1));
    dsgTxn2.end();
    // Check API methods work.
    Dataset ds = TDBFactory.createDataset(loc);
    ds.begin(ReadWrite.READ);
    Model m = (q.isDefaultGraph() ? ds.getDefaultModel() : ds.getNamedModel("g"));
    assertEquals(nonTxnData ? 2 : 1, m.size());
    ds.end();
}
Also used : StoreConnection(org.apache.jena.tdb.StoreConnection) Dataset(org.apache.jena.query.Dataset) DatasetGraphTxn(org.apache.jena.tdb.transaction.DatasetGraphTxn) Model(org.apache.jena.rdf.model.Model) Location(org.apache.jena.tdb.base.file.Location) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) Test(org.junit.Test) ConfigTest(org.apache.jena.tdb.ConfigTest) BaseTest(org.apache.jena.atlas.junit.BaseTest)

Aggregations

StoreConnection (org.apache.jena.tdb.StoreConnection)39 DatasetGraphTxn (org.apache.jena.tdb.transaction.DatasetGraphTxn)32 Test (org.junit.Test)29 BaseTest (org.apache.jena.atlas.junit.BaseTest)28 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)17 ConfigTest (org.apache.jena.tdb.ConfigTest)9 Location (org.apache.jena.tdb.base.file.Location)4 DatasetGraphTDB (org.apache.jena.tdb.store.DatasetGraphTDB)3 Quad (org.apache.jena.sparql.core.Quad)2 File (java.io.File)1 FileFilter (java.io.FileFilter)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutorService (java.util.concurrent.ExecutorService)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Dataset (org.apache.jena.query.Dataset)1 Model (org.apache.jena.rdf.model.Model)1 NodeTable (org.apache.jena.tdb.store.nodetable.NodeTable)1 BeforeClass (org.junit.BeforeClass)1 ModLocation (tdb.cmdline.ModLocation)1