Search in sources :

Example 96 with Stopwatch

use of com.google.common.base.Stopwatch 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 97 with Stopwatch

use of com.google.common.base.Stopwatch in project jackrabbit-oak by apache.

the class MongoDocumentStore method findAndModify.

@SuppressWarnings("unchecked")
@CheckForNull
private <T extends Document> T findAndModify(Collection<T> collection, UpdateOp updateOp, boolean upsert, boolean checkConditions) {
    DBCollection dbCollection = getDBCollection(collection);
    // make sure we don't modify the original updateOp
    updateOp = updateOp.copy();
    DBObject update = createUpdate(updateOp, false);
    Lock lock = null;
    if (collection == Collection.NODES) {
        lock = nodeLocks.acquire(updateOp.getId());
    }
    final Stopwatch watch = startWatch();
    boolean newEntry = false;
    try {
        // get modCount of cached document
        Long modCount = null;
        T cachedDoc = null;
        if (collection == Collection.NODES) {
            cachedDoc = (T) nodesCache.getIfPresent(updateOp.getId());
            if (cachedDoc != null) {
                modCount = cachedDoc.getModCount();
            }
        }
        // if we have a matching modCount
        if (modCount != null) {
            QueryBuilder query = createQueryForUpdate(updateOp.getId(), updateOp.getConditions());
            query.and(Document.MOD_COUNT).is(modCount);
            WriteResult result = dbCollection.update(query.get(), update);
            if (result.getN() > 0) {
                // success, update cached document
                if (collection == Collection.NODES) {
                    NodeDocument newDoc = (NodeDocument) applyChanges(collection, cachedDoc, updateOp);
                    nodesCache.put(newDoc);
                }
                // return previously cached document
                return cachedDoc;
            }
        }
        // conditional update failed or not possible
        // perform operation and get complete document
        QueryBuilder query = createQueryForUpdate(updateOp.getId(), updateOp.getConditions());
        DBObject oldNode = dbCollection.findAndModify(query.get(), null, null, /*sort*/
        false, /*remove*/
        update, false, /*returnNew*/
        upsert);
        if (oldNode == null) {
            newEntry = true;
        }
        if (checkConditions && oldNode == null) {
            return null;
        }
        T oldDoc = convertFromDBObject(collection, oldNode);
        if (oldDoc != null) {
            if (collection == Collection.NODES) {
                NodeDocument newDoc = (NodeDocument) applyChanges(collection, oldDoc, updateOp);
                nodesCache.put(newDoc);
                updateLocalChanges(newDoc);
            }
            oldDoc.seal();
        } else if (upsert) {
            if (collection == Collection.NODES) {
                NodeDocument doc = (NodeDocument) collection.newDocument(this);
                UpdateUtils.applyChanges(doc, updateOp);
                nodesCache.putIfAbsent(doc);
                updateLocalChanges(doc);
            }
        } else {
        // updateOp without conditions and not an upsert
        // this means the document does not exist
        }
        return oldDoc;
    } catch (Exception e) {
        throw handleException(e, collection, updateOp.getId());
    } finally {
        if (lock != null) {
            lock.unlock();
        }
        stats.doneFindAndModify(watch.elapsed(TimeUnit.NANOSECONDS), collection, updateOp.getId(), newEntry, true, 0);
    }
}
Also used : DBCollection(com.mongodb.DBCollection) WriteResult(com.mongodb.WriteResult) BulkWriteResult(com.mongodb.BulkWriteResult) Stopwatch(com.google.common.base.Stopwatch) QueryBuilder(com.mongodb.QueryBuilder) NodeDocument(org.apache.jackrabbit.oak.plugins.document.NodeDocument) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) 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) Lock(java.util.concurrent.locks.Lock) CheckForNull(javax.annotation.CheckForNull)

Example 98 with Stopwatch

use of com.google.common.base.Stopwatch in project jackrabbit-oak by apache.

the class MongoDocumentStore method remove.

@Override
public <T extends Document> void remove(Collection<T> collection, List<String> keys) {
    log("remove", keys);
    DBCollection dbCollection = getDBCollection(collection);
    Stopwatch watch = startWatch();
    try {
        for (List<String> keyBatch : Lists.partition(keys, IN_CLAUSE_BATCH_SIZE)) {
            DBObject query = QueryBuilder.start(Document.ID).in(keyBatch).get();
            try {
                dbCollection.remove(query);
            } catch (Exception e) {
                throw DocumentStoreException.convert(e, "Remove failed for " + keyBatch);
            } finally {
                if (collection == Collection.NODES) {
                    for (String key : keyBatch) {
                        invalidateCache(collection, key);
                    }
                }
            }
        }
    } finally {
        stats.doneRemove(watch.elapsed(TimeUnit.NANOSECONDS), collection, keys.size());
    }
}
Also used : DBCollection(com.mongodb.DBCollection) Stopwatch(com.google.common.base.Stopwatch) DBObject(com.mongodb.DBObject) BasicDBObject(com.mongodb.BasicDBObject) 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 99 with Stopwatch

use of com.google.common.base.Stopwatch in project jackrabbit-oak by apache.

the class DataStoreCheckCommand method retrieveBlobIds.

private static void retrieveBlobIds(GarbageCollectableBlobStore blobStore, File blob) throws Exception {
    System.out.println("Starting dump of blob ids");
    Stopwatch watch = createStarted();
    Iterator<String> blobIter = blobStore.getAllChunkIds(0);
    int count = writeStrings(blobIter, blob, true);
    sort(blob);
    System.out.println(count + " blob ids found");
    System.out.println("Finished in " + watch.elapsed(TimeUnit.SECONDS) + " seconds");
}
Also used : Stopwatch(com.google.common.base.Stopwatch)

Example 100 with Stopwatch

use of com.google.common.base.Stopwatch in project jackrabbit-oak by apache.

the class DataStoreCheckCommand method checkConsistency.

private static void checkConsistency(File ids, File refs, File missing) throws IOException {
    System.out.println("Starting consistency check");
    Stopwatch watch = createStarted();
    FileLineDifferenceIterator iter = new FileLineDifferenceIterator(ids, refs, new Function<String, String>() {

        @Nullable
        @Override
        public String apply(@Nullable String input) {
            if (input != null) {
                return input.split(DELIM)[0];
            }
            return "";
        }
    });
    long candidates = writeStrings(iter, missing, true);
    System.out.println("Consistency check found " + candidates + " missing blobs");
    if (candidates > 0) {
        System.out.println("Consistency check failure for the data store");
    }
    System.out.println("Finished in " + watch.elapsed(TimeUnit.SECONDS) + " seconds");
}
Also used : Stopwatch(com.google.common.base.Stopwatch) FileLineDifferenceIterator(org.apache.jackrabbit.oak.commons.FileIOUtils.FileLineDifferenceIterator) Nullable(javax.annotation.Nullable)

Aggregations

Stopwatch (com.google.common.base.Stopwatch)296 IOException (java.io.IOException)75 ArrayList (java.util.ArrayList)29 ExecutionException (java.util.concurrent.ExecutionException)27 Test (org.junit.Test)17 Map (java.util.Map)16 File (java.io.File)15 DocumentStoreException (org.apache.jackrabbit.oak.plugins.document.DocumentStoreException)15 Path (org.apache.hadoop.fs.Path)14 HashMap (java.util.HashMap)13 List (java.util.List)12 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)11 DrillRuntimeException (org.apache.drill.common.exceptions.DrillRuntimeException)11 DBCollection (com.mongodb.DBCollection)9 ISE (io.druid.java.util.common.ISE)9 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)8 OptionsParser (com.google.devtools.common.options.OptionsParser)8 MongoException (com.mongodb.MongoException)8 Connection (java.sql.Connection)8 CountDownLatch (java.util.concurrent.CountDownLatch)8