Search in sources :

Example 11 with DocumentStoreException

use of org.apache.jackrabbit.oak.plugins.document.DocumentStoreException in project jackrabbit-oak by apache.

the class MongoDocumentStore method remove.

@Override
public <T extends Document> int remove(Collection<T> collection, String indexedProperty, long startValue, long endValue) throws DocumentStoreException {
    log("remove", collection, indexedProperty, startValue, endValue);
    int num = 0;
    DBCollection dbCollection = getDBCollection(collection);
    Stopwatch watch = startWatch();
    try {
        QueryBuilder queryBuilder = QueryBuilder.start(indexedProperty);
        queryBuilder.greaterThan(startValue);
        queryBuilder.lessThan(endValue);
        try {
            num = dbCollection.remove(queryBuilder.get()).getN();
        } catch (Exception e) {
            throw DocumentStoreException.convert(e, "Remove failed for " + collection + ": " + indexedProperty + " in (" + startValue + ", " + endValue + ")");
        } finally {
            if (collection == Collection.NODES) {
                // this method is currently being used only for Journal collection while GC.
                // But, to keep sanctity of the API, we need to acknowledge that Nodes collection
                // could've been used. But, in this signature, there's no useful way to invalidate
                // cache.
                // So, we use the hammer for this task
                invalidateCache();
            }
        }
    } finally {
        stats.doneRemove(watch.elapsed(TimeUnit.NANOSECONDS), collection, num);
    }
    return num;
}
Also used : DBCollection(com.mongodb.DBCollection) Stopwatch(com.google.common.base.Stopwatch) QueryBuilder(com.mongodb.QueryBuilder) MongoException(com.mongodb.MongoException) DocumentStoreException(org.apache.jackrabbit.oak.plugins.document.DocumentStoreException) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) BulkWriteException(com.mongodb.BulkWriteException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 12 with DocumentStoreException

use of org.apache.jackrabbit.oak.plugins.document.DocumentStoreException in project jackrabbit-oak by apache.

the class MemoryDocumentStore method internalCreateOrUpdate.

@CheckForNull
private <T extends Document> T internalCreateOrUpdate(Collection<T> collection, UpdateOp update, boolean checkConditions) {
    ConcurrentSkipListMap<String, T> map = getMap(collection);
    T oldDoc;
    Lock lock = rwLock.writeLock();
    lock.lock();
    try {
        // get the node if it's there
        oldDoc = map.get(update.getId());
        T doc = collection.newDocument(this);
        if (oldDoc == null) {
            if (!update.isNew()) {
                throw new DocumentStoreException("Document does not exist: " + update.getId());
            }
        } else {
            oldDoc.deepCopy(doc);
        }
        if (checkConditions && !checkConditions(doc, update.getConditions())) {
            return null;
        }
        // update the document
        UpdateUtils.applyChanges(doc, update);
        doc.seal();
        map.put(update.getId(), doc);
        return oldDoc;
    } finally {
        lock.unlock();
    }
}
Also used : DocumentStoreException(org.apache.jackrabbit.oak.plugins.document.DocumentStoreException) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) Lock(java.util.concurrent.locks.Lock) CheckForNull(javax.annotation.CheckForNull)

Example 13 with DocumentStoreException

use of org.apache.jackrabbit.oak.plugins.document.DocumentStoreException in project jackrabbit-oak by apache.

the class MemoryDocumentStore method query.

@Override
@Nonnull
public <T extends Document> List<T> query(Collection<T> collection, String fromKey, String toKey, String indexedProperty, long startValue, int limit) {
    Lock lock = rwLock.readLock();
    lock.lock();
    try {
        ConcurrentSkipListMap<String, T> map = getMap(collection);
        ConcurrentNavigableMap<String, T> sub = map.subMap(fromKey + "\0", toKey);
        ArrayList<T> list = new ArrayList<T>();
        for (T doc : sub.values()) {
            if (indexedProperty != null) {
                Object value = doc.get(indexedProperty);
                if (value instanceof Boolean) {
                    long test = ((Boolean) value) ? 1 : 0;
                    if (test < startValue) {
                        continue;
                    }
                } else if (value instanceof Long) {
                    if ((Long) value < startValue) {
                        continue;
                    }
                } else if (value != null) {
                    throw new DocumentStoreException("unexpected type for property " + indexedProperty + ": " + value.getClass());
                }
            }
            list.add(doc);
            if (list.size() >= limit) {
                break;
            }
        }
        return list;
    } finally {
        lock.unlock();
    }
}
Also used : DocumentStoreException(org.apache.jackrabbit.oak.plugins.document.DocumentStoreException) ArrayList(java.util.ArrayList) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) Lock(java.util.concurrent.locks.Lock) Nonnull(javax.annotation.Nonnull)

Example 14 with DocumentStoreException

use of org.apache.jackrabbit.oak.plugins.document.DocumentStoreException in project jackrabbit-oak by apache.

the class RDBCacheConsistencyIT method runTest.

private void runTest() throws Throwable {
    addNodes(null, "/test", "/test/foo");
    final List<Throwable> exceptions = Collections.synchronizedList(new ArrayList<Throwable>());
    Thread t1 = new Thread(new Runnable() {

        @Override
        public void run() {
            String id = Utils.getIdFromPath("/test/foo");
            List<String> ids = Lists.newArrayList();
            ids.add(id);
            long v = 0;
            while (exceptions.isEmpty()) {
                try {
                    UpdateOp op = new UpdateOp(ids.get(0), false);
                    op.set("p", ++v);
                    op.set("mb", "update");
                    store.update(NODES, ids, op);
                    NodeDocument doc = store.find(NODES, id);
                    Object p = doc.get("p");
                    assertEquals("Unexpected result after update-then-find sequence, last modification of document by '" + doc.get("mb") + "' thread @" + doc.getModCount(), v, ((Long) p).longValue());
                // System.out.println("u @" + doc.getModCount() + " p=" + v + "; q=" + doc.get("q"));
                } catch (Throwable e) {
                    exceptions.add(e);
                }
            }
        }
    }, "update");
    t1.start();
    Thread t2 = new Thread(new Runnable() {

        @Override
        public void run() {
            String id = Utils.getIdFromPath("/test/foo");
            long v = 0;
            long lastWrittenV = 0;
            while (exceptions.isEmpty()) {
                try {
                    UpdateOp op = new UpdateOp(id, false);
                    op.set("q", ++v);
                    op.set("mb", "findAndUpdate");
                    NodeDocument old = store.findAndUpdate(NODES, op);
                    Object q = old.get("q");
                    if (q != null) {
                        assertEquals("Unexpected result after findAndUpdate, last modification of document by '" + old.get("mb") + "' thread @" + old.getModCount(), lastWrittenV, ((Long) q).longValue());
                    }
                    lastWrittenV = v;
                // System.out.println("f @" + old.getModCount() + " p=" + old.get("p") + "; q=" + q);
                } catch (DocumentStoreException e) {
                // System.err.println("f update of v to " + v + " failed: " + e.getMessage());
                // keep going, RDBDocumentStore might have given up due
                // to race conditions
                } catch (Throwable e) {
                    exceptions.add(e);
                }
            }
        }
    }, "findAndUpdate");
    t2.start();
    Thread t3 = new Thread(new Runnable() {

        @Override
        public void run() {
            String id = Utils.getIdFromPath("/test/foo");
            long p = 0;
            long q = 0;
            long mc = 0;
            while (exceptions.isEmpty()) {
                try {
                    NodeDocument doc = store.find(NODES, id);
                    if (doc != null) {
                        Object value = doc.get("p");
                        if (value != null) {
                            assertTrue("reader thread at @" + doc.getModCount() + ": observed property value for 'p' (incremented by 'update' thread) decreased, last change by '" + doc.get("mb") + "' thread; previous: " + p + " (at @" + mc + "), now: " + value, (Long) value >= p);
                            p = (Long) value;
                        }
                        value = doc.get("q");
                        if (value != null) {
                            assertTrue("reader thread at @" + doc.getModCount() + ": observed property value for 'q' (incremented by 'findAndUpdate' thread) decreased, last change by '" + doc.get("mb") + "' thread; previous: " + q + " (at @" + mc + "), now: " + value, (Long) value >= q);
                            q = (Long) value;
                        }
                    }
                    mc = doc.getModCount();
                } catch (Throwable e) {
                    exceptions.add(e);
                }
            }
        }
    }, "reader");
    t3.start();
    NodeDocumentCache cache = store.getNodeDocumentCache();
    // run for at most five seconds
    long end = System.currentTimeMillis() + 1000;
    String id = Utils.getIdFromPath("/test/foo");
    while (t1.isAlive() && t2.isAlive() && t3.isAlive() && System.currentTimeMillis() < end) {
        if (cache.getIfPresent(id) != null) {
            Thread.sleep(0, (int) (Math.random() * 100));
            // simulate eviction
            cache.invalidate(id);
        }
    }
    for (Throwable e : exceptions) {
        throw e;
    }
    exceptions.add(new Exception("end"));
    t1.join();
    t2.join();
    t3.join();
}
Also used : DocumentStoreException(org.apache.jackrabbit.oak.plugins.document.DocumentStoreException) NodeDocumentCache(org.apache.jackrabbit.oak.plugins.document.cache.NodeDocumentCache) UpdateOp(org.apache.jackrabbit.oak.plugins.document.UpdateOp) NodeDocument(org.apache.jackrabbit.oak.plugins.document.NodeDocument) DocumentStoreException(org.apache.jackrabbit.oak.plugins.document.DocumentStoreException) ArrayList(java.util.ArrayList) List(java.util.List)

Example 15 with DocumentStoreException

use of org.apache.jackrabbit.oak.plugins.document.DocumentStoreException in project jackrabbit-oak by apache.

the class UnlockUpgradeCommand method execute.

@Override
public void execute(String... args) throws Exception {
    OptionParser parser = new OptionParser();
    // RDB specific options
    OptionSpec<String> rdbjdbcuser = parser.accepts("rdbjdbcuser", "RDB JDBC user").withOptionalArg().defaultsTo("");
    OptionSpec<String> rdbjdbcpasswd = parser.accepts("rdbjdbcpasswd", "RDB JDBC password").withOptionalArg().defaultsTo("");
    OptionSpec<String> nonOption = parser.nonOptions("unlockUpgrade {<jdbc-uri> | <mongodb-uri>}");
    OptionSpec help = parser.acceptsAll(asList("h", "?", "help"), "show help").forHelp();
    OptionSet options = parser.parse(args);
    List<String> nonOptions = nonOption.values(options);
    if (options.has(help)) {
        parser.printHelpOn(System.out);
        return;
    }
    if (nonOptions.isEmpty()) {
        parser.printHelpOn(System.err);
        return;
    }
    DocumentStore store = null;
    try {
        String uri = nonOptions.get(0);
        if (uri.startsWith(MONGODB_PREFIX)) {
            MongoClientURI clientURI = new MongoClientURI(uri);
            if (clientURI.getDatabase() == null) {
                System.err.println("Database missing in MongoDB URI: " + clientURI.getURI());
            } else {
                MongoConnection mongo = new MongoConnection(clientURI.getURI());
                store = new MongoDocumentStore(mongo.getDB(), new DocumentMK.Builder());
            }
        } else if (uri.startsWith("jdbc")) {
            DataSource ds = RDBDataSourceFactory.forJdbcUrl(uri, rdbjdbcuser.value(options), rdbjdbcpasswd.value(options));
            store = new RDBDocumentStore(ds, new DocumentMK.Builder());
        } else {
            System.err.println("Unrecognized URI: " + uri);
        }
        if (store != null && VERSION.writeTo(store)) {
            System.out.println("Format version set to " + VERSION);
        }
    } catch (DocumentStoreException e) {
        System.err.println(e.getMessage());
    } finally {
        if (store != null) {
            store.dispose();
        }
    }
}
Also used : OptionSpec(joptsimple.OptionSpec) DocumentStoreException(org.apache.jackrabbit.oak.plugins.document.DocumentStoreException) MongoClientURI(com.mongodb.MongoClientURI) DocumentMK(org.apache.jackrabbit.oak.plugins.document.DocumentMK) OptionParser(joptsimple.OptionParser) MongoDocumentStore(org.apache.jackrabbit.oak.plugins.document.mongo.MongoDocumentStore) DataSource(javax.sql.DataSource) DocumentStore(org.apache.jackrabbit.oak.plugins.document.DocumentStore) MongoDocumentStore(org.apache.jackrabbit.oak.plugins.document.mongo.MongoDocumentStore) RDBDocumentStore(org.apache.jackrabbit.oak.plugins.document.rdb.RDBDocumentStore) RDBDocumentStore(org.apache.jackrabbit.oak.plugins.document.rdb.RDBDocumentStore) OptionSet(joptsimple.OptionSet) MongoConnection(org.apache.jackrabbit.oak.plugins.document.util.MongoConnection)

Aggregations

DocumentStoreException (org.apache.jackrabbit.oak.plugins.document.DocumentStoreException)30 IOException (java.io.IOException)11 SQLException (java.sql.SQLException)11 UnsupportedEncodingException (java.io.UnsupportedEncodingException)8 Stopwatch (com.google.common.base.Stopwatch)7 ExecutionException (java.util.concurrent.ExecutionException)6 Connection (java.sql.Connection)5 PreparedStatement (java.sql.PreparedStatement)5 ArrayList (java.util.ArrayList)5 Lock (java.util.concurrent.locks.Lock)5 CheckForNull (javax.annotation.CheckForNull)5 NodeDocument (org.apache.jackrabbit.oak.plugins.document.NodeDocument)5 Nonnull (javax.annotation.Nonnull)4 ResultSet (java.sql.ResultSet)3 Map (java.util.Map)3 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)2 UncheckedExecutionException (com.google.common.util.concurrent.UncheckedExecutionException)2 DBCollection (com.mongodb.DBCollection)2 QueryBuilder (com.mongodb.QueryBuilder)2 BatchUpdateException (java.sql.BatchUpdateException)2