Search in sources :

Example 1 with Document

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

the class RevisionsCommandTest method reset.

@Test
public void reset() throws Exception {
    ns.getVersionGarbageCollector().gc(1, TimeUnit.HOURS);
    Document doc = ns.getDocumentStore().find(Collection.SETTINGS, "versionGC");
    assertNotNull(doc);
    ns.dispose();
    String output = captureSystemOut(new RevisionsCmd("reset"));
    assertTrue(output.contains("resetting recommendations and statistics"));
    MongoConnection c = connectionFactory.getConnection();
    ns = builderProvider.newBuilder().setMongoDB(c.getDB()).getNodeStore();
    doc = ns.getDocumentStore().find(Collection.SETTINGS, "versionGC");
    assertNull(doc);
}
Also used : CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Document(org.apache.jackrabbit.oak.plugins.document.Document) MongoConnection(org.apache.jackrabbit.oak.plugins.document.util.MongoConnection) Test(org.junit.Test)

Example 2 with Document

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

the class RDBDocumentStore method internalQuery.

private <T extends Document> List<T> internalQuery(Collection<T> collection, String fromKey, String toKey, List<String> excludeKeyPatterns, List<QueryCondition> conditions, int limit) {
    Connection connection = null;
    RDBTableMetaData tmd = getTable(collection);
    for (QueryCondition cond : conditions) {
        if (!INDEXEDPROPERTIES.contains(cond.getPropertyName())) {
            String message = "indexed property " + cond.getPropertyName() + " not supported, query was '" + cond + "'; supported properties are " + INDEXEDPROPERTIES;
            LOG.info(message);
            throw new DocumentStoreException(message);
        }
    }
    final Stopwatch watch = startWatch();
    int resultSize = 0;
    try (CacheChangesTracker tracker = obtainTracker(collection, fromKey, toKey)) {
        long now = System.currentTimeMillis();
        connection = this.ch.getROConnection();
        String from = collection == Collection.NODES && NodeDocument.MIN_ID_VALUE.equals(fromKey) ? null : fromKey;
        String to = collection == Collection.NODES && NodeDocument.MAX_ID_VALUE.equals(toKey) ? null : toKey;
        // OAK-6839: only populate the cache with *new* entries if the query
        // isn't open-ended (something done by GC processes)
        boolean populateCache = to != null;
        List<RDBRow> dbresult = db.query(connection, tmd, from, to, excludeKeyPatterns, conditions, limit);
        connection.commit();
        int size = dbresult.size();
        List<T> result = new ArrayList<T>(size);
        for (int i = 0; i < size; i++) {
            // free RDBRow as early as possible
            RDBRow row = dbresult.set(i, null);
            T doc = getIfCached(collection, row.getId(), row.getModcount());
            if (doc == null) {
                // parse DB contents into document if and only if it's not
                // already in the cache
                doc = convertFromDBObject(collection, row);
            } else {
                // we got a document from the cache, thus collection is NODES
                // and a tracker is present
                long lastmodified = modifiedOf(doc);
                if (lastmodified == row.getModified() && lastmodified >= 1) {
                    try (CacheLock lock = acquireLockFor(row.getId())) {
                        if (!tracker.mightBeenAffected(row.getId())) {
                            // otherwise mark it as fresh
                            ((NodeDocument) doc).markUpToDate(now);
                        }
                    }
                } else {
                    // we need a fresh document instance
                    doc = convertFromDBObject(collection, row);
                }
            }
            result.add(doc);
        }
        if (collection == Collection.NODES) {
            if (populateCache) {
                nodesCache.putNonConflictingDocs(tracker, castAsNodeDocumentList(result));
            } else {
                Map<String, ModificationStamp> invMap = Maps.newHashMap();
                for (Document doc : result) {
                    invMap.put(doc.getId(), new ModificationStamp(modcountOf(doc), modifiedOf(doc)));
                }
                nodesCache.invalidateOutdated(invMap);
            }
        }
        resultSize = result.size();
        return result;
    } catch (Exception ex) {
        LOG.error("SQL exception on query", ex);
        throw asDocumentStoreException(ex, "SQL exception on query");
    } finally {
        this.ch.closeConnection(connection);
        stats.doneQuery(watch.elapsed(TimeUnit.NANOSECONDS), collection, fromKey, toKey, !conditions.isEmpty(), resultSize, -1, false);
    }
}
Also used : DocumentStoreException(org.apache.jackrabbit.oak.plugins.document.DocumentStoreException) RDBJDBCTools.asDocumentStoreException(org.apache.jackrabbit.oak.plugins.document.rdb.RDBJDBCTools.asDocumentStoreException) Connection(java.sql.Connection) Stopwatch(com.google.common.base.Stopwatch) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) ArrayList(java.util.ArrayList) NodeDocument(org.apache.jackrabbit.oak.plugins.document.NodeDocument) NodeDocument(org.apache.jackrabbit.oak.plugins.document.NodeDocument) Document(org.apache.jackrabbit.oak.plugins.document.Document) UnsupportedEncodingException(java.io.UnsupportedEncodingException) DocumentStoreException(org.apache.jackrabbit.oak.plugins.document.DocumentStoreException) SQLException(java.sql.SQLException) IOException(java.io.IOException) RDBJDBCTools.asDocumentStoreException(org.apache.jackrabbit.oak.plugins.document.rdb.RDBJDBCTools.asDocumentStoreException) ExecutionException(java.util.concurrent.ExecutionException) ModificationStamp(org.apache.jackrabbit.oak.plugins.document.cache.ModificationStamp) CacheChangesTracker(org.apache.jackrabbit.oak.plugins.document.cache.CacheChangesTracker)

Example 3 with Document

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

the class MongoDocumentTraverser method getAllDocuments.

public <T extends Document> CloseableIterable<T> getAllDocuments(Collection<T> collection, Predicate<String> filter) {
    if (!disableReadOnlyCheck) {
        checkState(mongoStore.isReadOnly(), "Traverser can only be used with readOnly store");
    }
    MongoCollection<BasicDBObject> dbCollection = mongoStore.getDBCollection(collection);
    // TODO This may lead to reads being routed to secondary depending on MongoURI
    // So caller must ensure that its safe to read from secondary
    Iterable<BasicDBObject> cursor = dbCollection.withReadPreference(mongoStore.getConfiguredReadPreference(collection)).find();
    CloseableIterable<BasicDBObject> closeableCursor = CloseableIterable.wrap(cursor);
    cursor = closeableCursor;
    @SuppressWarnings("Guava") Iterable<T> result = FluentIterable.from(cursor).filter(o -> filter.test((String) o.get(Document.ID))).transform(o -> {
        T doc = mongoStore.convertFromDBObject(collection, o);
        // TODO Review the cache update approach where tracker has to track *all* docs
        if (collection == Collection.NODES) {
            NodeDocument nodeDoc = (NodeDocument) doc;
            getNodeDocCache().put(nodeDoc);
        }
        return doc;
    });
    return CloseableIterable.wrap(result, closeableCursor);
}
Also used : Collection(org.apache.jackrabbit.oak.plugins.document.Collection) CloseableIterable(org.apache.jackrabbit.oak.plugins.document.util.CloseableIterable) MongoCollection(com.mongodb.client.MongoCollection) FluentIterable(com.google.common.collect.FluentIterable) Predicate(java.util.function.Predicate) Document(org.apache.jackrabbit.oak.plugins.document.Document) BasicDBObject(com.mongodb.BasicDBObject) NodeDocumentCache(org.apache.jackrabbit.oak.plugins.document.cache.NodeDocumentCache) NodeDocument(org.apache.jackrabbit.oak.plugins.document.NodeDocument) Preconditions.checkState(com.google.common.base.Preconditions.checkState) BasicDBObject(com.mongodb.BasicDBObject) NodeDocument(org.apache.jackrabbit.oak.plugins.document.NodeDocument)

Example 4 with Document

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

the class ReadOnlyDocumentStoreWrapperTest method testPassthrough.

@Test
public void testPassthrough() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
    final List<String> disallowedMethods = Lists.newArrayList("create", "update", "remove", "createOrUpdate", "findAndUpdate");
    InvocationHandler handler = new InvocationHandler() {

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            String methodName = method.getName();
            if (disallowedMethods.contains(methodName)) {
                Assert.fail(String.format("Invalid passthrough of method (%s) with params %s", method, Arrays.toString(args)));
            }
            if ("determineServerTimeDifferenceMillis".equals(methodName)) {
                return new Long(0);
            } else {
                return null;
            }
        }
    };
    DocumentStore proxyStore = (DocumentStore) Proxy.newProxyInstance(DocumentStore.class.getClassLoader(), new Class[] { DocumentStore.class }, handler);
    DocumentStore readOnlyStore = ReadOnlyDocumentStoreWrapperFactory.getInstance(proxyStore);
    Collection<? extends Document>[] collections = new Collection[] { Collection.CLUSTER_NODES, Collection.JOURNAL, Collection.NODES, Collection.SETTINGS };
    for (Collection collection : collections) {
        readOnlyStore.find(collection, null);
        readOnlyStore.find(collection, null, 0);
        readOnlyStore.query(collection, null, null, 0);
        readOnlyStore.query(collection, null, null, null, 0, 0);
        boolean uoeThrown = false;
        try {
            readOnlyStore.remove(collection, "");
        } catch (UnsupportedOperationException uoe) {
            // catch uoe thrown by read only wrapper
            uoeThrown = true;
        }
        assertTrue("remove must throw UnsupportedOperationException", uoeThrown);
        uoeThrown = false;
        try {
            readOnlyStore.remove(collection, Lists.<String>newArrayList());
        } catch (UnsupportedOperationException uoe) {
            // catch uoe thrown by read only wrapper
            uoeThrown = true;
        }
        assertTrue("remove must throw UnsupportedOperationException", uoeThrown);
        uoeThrown = false;
        try {
            readOnlyStore.remove(collection, Maps.<String, Long>newHashMap());
        } catch (UnsupportedOperationException uoe) {
            // catch uoe thrown by read only wrapper
            uoeThrown = true;
        }
        assertTrue("remove must throw UnsupportedOperationException", uoeThrown);
        uoeThrown = false;
        try {
            readOnlyStore.create(collection, null);
        } catch (UnsupportedOperationException uoe) {
            // catch uoe thrown by read only wrapper
            uoeThrown = true;
        }
        assertTrue("create must throw UnsupportedOperationException", uoeThrown);
        uoeThrown = false;
        try {
            readOnlyStore.createOrUpdate(collection, (UpdateOp) null);
        } catch (UnsupportedOperationException uoe) {
            // catch uoe thrown by read only wrapper
            uoeThrown = true;
        }
        assertTrue("createOrUpdate must throw UnsupportedOperationException", uoeThrown);
        uoeThrown = false;
        try {
            readOnlyStore.createOrUpdate(collection, Lists.<UpdateOp>newArrayList());
        } catch (UnsupportedOperationException uoe) {
            // catch uoe thrown by read only wrapper
            uoeThrown = true;
        }
        assertTrue("createOrUpdate must throw UnsupportedOperationException", uoeThrown);
        uoeThrown = false;
        try {
            readOnlyStore.findAndUpdate(collection, null);
        } catch (UnsupportedOperationException uoe) {
            // catch uoe thrown by read only wrapper
            uoeThrown = true;
        }
        assertTrue("findAndUpdate must throw UnsupportedOperationException", uoeThrown);
        readOnlyStore.invalidateCache(collection, null);
        readOnlyStore.getIfCached(collection, null);
    }
    readOnlyStore.invalidateCache();
    readOnlyStore.invalidateCache(null);
    readOnlyStore.dispose();
    readOnlyStore.setReadWriteMode(null);
    readOnlyStore.getCacheStats();
    readOnlyStore.getMetadata();
    readOnlyStore.determineServerTimeDifferenceMillis();
}
Also used : Method(java.lang.reflect.Method) Document(org.apache.jackrabbit.oak.plugins.document.Document) InvocationHandler(java.lang.reflect.InvocationHandler) DocumentStore(org.apache.jackrabbit.oak.plugins.document.DocumentStore) MemoryDocumentStore(org.apache.jackrabbit.oak.plugins.document.memory.MemoryDocumentStore) Collection(org.apache.jackrabbit.oak.plugins.document.Collection) Test(org.junit.Test)

Example 5 with Document

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

the class MongoDocumentStore method getModStamps.

/**
 * Returns the {@link Document#MOD_COUNT} and
 * {@link NodeDocument#MODIFIED_IN_SECS} values of the documents with the
 * given {@code keys}. The returned map will only contain entries for
 * existing documents. The default value is -1 if the document does not have
 * a modCount field. The same applies to the modified field.
 *
 * @param keys the keys of the documents.
 * @return map with key to modification stamp mapping.
 * @throws MongoException if the call fails
 */
@Nonnull
private Map<String, ModificationStamp> getModStamps(Iterable<String> keys) throws MongoException {
    // Fetch only the modCount and id
    final BasicDBObject fields = new BasicDBObject(Document.ID, 1);
    fields.put(Document.MOD_COUNT, 1);
    fields.put(NodeDocument.MODIFIED_IN_SECS, 1);
    Map<String, ModificationStamp> modCounts = Maps.newHashMap();
    nodes.withReadPreference(ReadPreference.primary()).find(Filters.in(Document.ID, keys)).projection(fields).forEach((Block<BasicDBObject>) obj -> {
        String id = (String) obj.get(Document.ID);
        Long modCount = Utils.asLong((Number) obj.get(Document.MOD_COUNT));
        if (modCount == null) {
            modCount = -1L;
        }
        Long modified = Utils.asLong((Number) obj.get(NodeDocument.MODIFIED_IN_SECS));
        if (modified == null) {
            modified = -1L;
        }
        modCounts.put(id, new ModificationStamp(modCount, modified));
    });
    return modCounts;
}
Also used : Arrays(java.util.Arrays) CacheStats(org.apache.jackrabbit.oak.cache.CacheStats) MongoDatabase(com.mongodb.client.MongoDatabase) Predicates.in(com.google.common.base.Predicates.in) Sets.difference(com.google.common.collect.Sets.difference) Key(org.apache.jackrabbit.oak.plugins.document.UpdateOp.Key) Predicates.not(com.google.common.base.Predicates.not) UpdateResult(com.mongodb.client.result.UpdateResult) Map(java.util.Map) UpdateOptions(com.mongodb.client.model.UpdateOptions) Revision(org.apache.jackrabbit.oak.plugins.document.Revision) CacheInvalidationStats(org.apache.jackrabbit.oak.plugins.document.cache.CacheInvalidationStats) Set(java.util.Set) Collection(org.apache.jackrabbit.oak.plugins.document.Collection) BulkWriteUpsert(com.mongodb.bulk.BulkWriteUpsert) Clock(org.apache.jackrabbit.oak.stats.Clock) CacheChangesTracker(org.apache.jackrabbit.oak.plugins.document.cache.CacheChangesTracker) Iterables.filter(com.google.common.collect.Iterables.filter) Iterables(com.google.common.collect.Iterables) MongoClientURI(com.mongodb.MongoClientURI) MongoCollection(com.mongodb.client.MongoCollection) BulkWriteOptions(com.mongodb.client.model.BulkWriteOptions) FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) Callable(java.util.concurrent.Callable) SD_MAX_REV_TIME_IN_SECS(org.apache.jackrabbit.oak.plugins.document.NodeDocument.SD_MAX_REV_TIME_IN_SECS) ArrayList(java.util.ArrayList) UpdateUtils(org.apache.jackrabbit.oak.plugins.document.UpdateUtils) LinkedHashMap(java.util.LinkedHashMap) Bson(org.bson.conversions.Bson) Filters(com.mongodb.client.model.Filters) Lists(com.google.common.collect.Lists) SD_TYPE(org.apache.jackrabbit.oak.plugins.document.NodeDocument.SD_TYPE) ReplicaSetInfo(org.apache.jackrabbit.oak.plugins.document.mongo.replica.ReplicaSetInfo) Nullable(javax.annotation.Nullable) DELETED_ONCE(org.apache.jackrabbit.oak.plugins.document.NodeDocument.DELETED_ONCE) Maps.filterKeys(com.google.common.collect.Maps.filterKeys) LocalChanges(org.apache.jackrabbit.oak.plugins.document.mongo.replica.LocalChanges) UpdateOneModel(com.mongodb.client.model.UpdateOneModel) BasicDBObject(com.mongodb.BasicDBObject) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) DocumentStoreStatsCollector(org.apache.jackrabbit.oak.plugins.document.DocumentStoreStatsCollector) Lock(java.util.concurrent.locks.Lock) TreeMap(java.util.TreeMap) StripedNodeDocumentLocks(org.apache.jackrabbit.oak.plugins.document.locks.StripedNodeDocumentLocks) RevisionListener(org.apache.jackrabbit.oak.plugins.document.RevisionListener) Condition.newEqualsCondition(org.apache.jackrabbit.oak.plugins.document.UpdateOp.Condition.newEqualsCondition) Date(java.util.Date) LoggerFactory(org.slf4j.LoggerFactory) MongoBulkWriteException(com.mongodb.MongoBulkWriteException) WriteModel(com.mongodb.client.model.WriteModel) Utils(org.apache.jackrabbit.oak.plugins.document.util.Utils) MongoUtils.createPartialIndex(org.apache.jackrabbit.oak.plugins.document.mongo.MongoUtils.createPartialIndex) DBObject(com.mongodb.DBObject) RevisionVector(org.apache.jackrabbit.oak.plugins.document.RevisionVector) MongoUtils.createIndex(org.apache.jackrabbit.oak.plugins.document.mongo.MongoUtils.createIndex) Function(com.google.common.base.Function) ImmutableMap(com.google.common.collect.ImmutableMap) MongoException(com.mongodb.MongoException) ReturnDocument(com.mongodb.client.model.ReturnDocument) PerfLogger(org.apache.jackrabbit.oak.commons.PerfLogger) NodeDocumentLocks(org.apache.jackrabbit.oak.plugins.document.locks.NodeDocumentLocks) List(java.util.List) DocumentStoreException.asDocumentStoreException(org.apache.jackrabbit.oak.plugins.document.DocumentStoreException.asDocumentStoreException) Block(com.mongodb.Block) JournalEntry(org.apache.jackrabbit.oak.plugins.document.JournalEntry) FindIterable(com.mongodb.client.FindIterable) MongoUtils.hasIndex(org.apache.jackrabbit.oak.plugins.document.mongo.MongoUtils.hasIndex) Entry(java.util.Map.Entry) NodeDocumentCache(org.apache.jackrabbit.oak.plugins.document.cache.NodeDocumentCache) Condition(org.apache.jackrabbit.oak.plugins.document.UpdateOp.Condition) ModificationStamp(org.apache.jackrabbit.oak.plugins.document.cache.ModificationStamp) ReadPreference(com.mongodb.ReadPreference) DocumentStoreException(org.apache.jackrabbit.oak.plugins.document.DocumentStoreException) Stopwatch(com.google.common.base.Stopwatch) MongoUtils.getDocumentStoreExceptionTypeFor(org.apache.jackrabbit.oak.plugins.document.mongo.MongoUtils.getDocumentStoreExceptionTypeFor) UpdateOp(org.apache.jackrabbit.oak.plugins.document.UpdateOp) HashMap(java.util.HashMap) Iterators(com.google.common.collect.Iterators) MODIFIED_IN_SECS(org.apache.jackrabbit.oak.plugins.document.NodeDocument.MODIFIED_IN_SECS) HashSet(java.util.HashSet) BulkWriteError(com.mongodb.bulk.BulkWriteError) ImmutableList(com.google.common.collect.ImmutableList) MongoCursor(com.mongodb.client.MongoCursor) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) NodeDocument(org.apache.jackrabbit.oak.plugins.document.NodeDocument) Nonnull(javax.annotation.Nonnull) DocumentStore(org.apache.jackrabbit.oak.plugins.document.DocumentStore) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) Maps(com.google.common.collect.Maps) CacheValue(org.apache.jackrabbit.oak.cache.CacheValue) TimeUnit(java.util.concurrent.TimeUnit) StableRevisionComparator(org.apache.jackrabbit.oak.plugins.document.StableRevisionComparator) BulkWriteResult(com.mongodb.bulk.BulkWriteResult) MongoClient(com.mongodb.MongoClient) Document(org.apache.jackrabbit.oak.plugins.document.Document) Operation(org.apache.jackrabbit.oak.plugins.document.UpdateOp.Operation) WriteConcern(com.mongodb.WriteConcern) Collections(java.util.Collections) CheckForNull(javax.annotation.CheckForNull) BasicDBObject(com.mongodb.BasicDBObject) ModificationStamp(org.apache.jackrabbit.oak.plugins.document.cache.ModificationStamp) Nonnull(javax.annotation.Nonnull)

Aggregations

Document (org.apache.jackrabbit.oak.plugins.document.Document)7 NodeDocument (org.apache.jackrabbit.oak.plugins.document.NodeDocument)4 BasicDBObject (com.mongodb.BasicDBObject)3 ArrayList (java.util.ArrayList)3 Stopwatch (com.google.common.base.Stopwatch)2 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)2 MongoCollection (com.mongodb.client.MongoCollection)2 List (java.util.List)2 Collection (org.apache.jackrabbit.oak.plugins.document.Collection)2 Test (org.junit.Test)2 Function (com.google.common.base.Function)1 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 Predicates.in (com.google.common.base.Predicates.in)1 Predicates.not (com.google.common.base.Predicates.not)1 FluentIterable (com.google.common.collect.FluentIterable)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Iterables (com.google.common.collect.Iterables)1 Iterables.filter (com.google.common.collect.Iterables.filter)1 Iterators (com.google.common.collect.Iterators)1