Search in sources :

Example 26 with DocumentStoreException

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

the class RDBDataSourceFactory method forJdbcUrl.

public static DataSource forJdbcUrl(String url, String username, String passwd, String driverName) {
    // load driver class when specified
    if (driverName != null && !driverName.isEmpty()) {
        LOG.info("trying to load {}", driverName);
        try {
            Class.forName(driverName);
        } catch (ClassNotFoundException ex) {
            LOG.error("driver " + driverName + " not loaded", ex);
        }
    } else {
        // try to determine driver from JDBC URL
        String defaultDriver = RDBJDBCTools.driverForDBType(RDBJDBCTools.jdbctype(url));
        if (defaultDriver != null && !defaultDriver.isEmpty()) {
            LOG.info("trying to load {}", defaultDriver);
            try {
                Class.forName(defaultDriver);
            } catch (ClassNotFoundException ex) {
                LOG.error("driver " + defaultDriver + " not loaded", ex);
            }
        }
    }
    try {
        LOG.debug("Getting driver for " + url);
        Driver d = DriverManager.getDriver(url);
        String classname = "org.apache.tomcat.jdbc.pool.DataSource";
        try {
            Class<?> dsclazz = Class.forName(classname);
            DataSource ds = (DataSource) dsclazz.newInstance();
            dsclazz.getMethod("setDriverClassName", String.class).invoke(ds, d.getClass().getName());
            dsclazz.getMethod("setUsername", String.class).invoke(ds, username);
            dsclazz.getMethod("setPassword", String.class).invoke(ds, passwd);
            dsclazz.getMethod("setUrl", String.class).invoke(ds, url);
            String interceptors = System.getProperty("org.apache.jackrabbit.oak.plugins.document.rdb.RDBDataSourceFactory.jdbcInterceptors", "SlowQueryReport(threshold=10000);ConnectionState;StatementCache");
            if (!interceptors.isEmpty()) {
                dsclazz.getMethod("setJdbcInterceptors", String.class).invoke(ds, interceptors);
            }
            return new CloseableDataSource(ds);
        } catch (Exception ex) {
            String message = "trying to create datasource " + classname;
            LOG.info(message, ex);
            throw new DocumentStoreException(message, ex);
        }
    } catch (SQLException ex) {
        String message = "trying to obtain driver for " + url;
        LOG.info(message, ex);
        throw new DocumentStoreException(message, ex);
    }
}
Also used : DocumentStoreException(org.apache.jackrabbit.oak.plugins.document.DocumentStoreException) SQLException(java.sql.SQLException) Driver(java.sql.Driver) DocumentStoreException(org.apache.jackrabbit.oak.plugins.document.DocumentStoreException) SQLFeatureNotSupportedException(java.sql.SQLFeatureNotSupportedException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SQLException(java.sql.SQLException) DataSource(javax.sql.DataSource)

Example 27 with DocumentStoreException

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

the class RDBDocumentSerializer method asString.

/**
     * Serializes the changes in the {@link UpdateOp} into a JSON array; each
     * entry is another JSON array holding operation, key, revision, and value.
     */
public String asString(UpdateOp update) {
    StringBuilder sb = new StringBuilder("[");
    boolean needComma = false;
    for (Map.Entry<Key, Operation> change : update.getChanges().entrySet()) {
        Operation op = change.getValue();
        Key key = change.getKey();
        // exclude properties that are serialized into special columns
        if (columnProperties.contains(key.getName()) && null == key.getRevision())
            continue;
        if (needComma) {
            sb.append(",");
        }
        sb.append("[");
        if (op.type == UpdateOp.Operation.Type.INCREMENT) {
            sb.append("\"+\",");
        } else if (op.type == UpdateOp.Operation.Type.SET || op.type == UpdateOp.Operation.Type.SET_MAP_ENTRY) {
            sb.append("\"=\",");
        } else if (op.type == UpdateOp.Operation.Type.MAX) {
            sb.append("\"M\",");
        } else if (op.type == UpdateOp.Operation.Type.REMOVE || op.type == UpdateOp.Operation.Type.REMOVE_MAP_ENTRY) {
            sb.append("\"*\",");
        } else {
            throw new DocumentStoreException("Can't serialize " + update.toString() + " for JSON append");
        }
        appendJsonString(sb, key.getName());
        sb.append(",");
        Revision rev = key.getRevision();
        if (rev != null) {
            appendJsonString(sb, rev.toString());
            sb.append(",");
        }
        appendJsonValue(sb, op.value);
        sb.append("]");
        needComma = true;
    }
    return sb.append("]").toString();
}
Also used : DocumentStoreException(org.apache.jackrabbit.oak.plugins.document.DocumentStoreException) Revision(org.apache.jackrabbit.oak.plugins.document.Revision) Operation(org.apache.jackrabbit.oak.plugins.document.UpdateOp.Operation) Map(java.util.Map) TreeMap(java.util.TreeMap) Key(org.apache.jackrabbit.oak.plugins.document.UpdateOp.Key)

Example 28 with DocumentStoreException

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

the class RDBVersionGCSupport method getOldestDeletedOnceTimestamp.

@Override
public long getOldestDeletedOnceTimestamp(Clock clock, long precisionMs) {
    long modifiedMs = Long.MIN_VALUE;
    LOG.debug("getOldestDeletedOnceTimestamp() <- start");
    try {
        modifiedMs = store.getMinValue(Collection.NODES, NodeDocument.MODIFIED_IN_SECS, null, null, RDBDocumentStore.EMPTY_KEY_PATTERN, Collections.singletonList(new QueryCondition(NodeDocument.DELETED_ONCE, "=", 1)));
    } catch (DocumentStoreException ex) {
        LOG.debug("getMinValue(MODIFIED)", ex);
    }
    if (modifiedMs > 0) {
        LOG.debug("getOldestDeletedOnceTimestamp() -> {}", Utils.timestampToString(modifiedMs));
        return modifiedMs;
    } else {
        LOG.debug("getOldestDeletedOnceTimestamp() -> none found, return current time");
        return clock.getTime();
    }
}
Also used : DocumentStoreException(org.apache.jackrabbit.oak.plugins.document.DocumentStoreException) QueryCondition(org.apache.jackrabbit.oak.plugins.document.rdb.RDBDocumentStore.QueryCondition)

Example 29 with DocumentStoreException

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

the class MongoDocumentStore method queryInternal.

@SuppressWarnings("unchecked")
@Nonnull
<T extends Document> List<T> queryInternal(Collection<T> collection, String fromKey, String toKey, String indexedProperty, long startValue, int limit, long maxQueryTime) {
    log("query", fromKey, toKey, indexedProperty, startValue, limit);
    DBCollection dbCollection = getDBCollection(collection);
    QueryBuilder queryBuilder = QueryBuilder.start(Document.ID);
    queryBuilder.greaterThan(fromKey);
    queryBuilder.lessThan(toKey);
    DBObject hint = new BasicDBObject(NodeDocument.ID, 1);
    if (indexedProperty != null) {
        if (NodeDocument.DELETED_ONCE.equals(indexedProperty)) {
            if (startValue != 1) {
                throw new DocumentStoreException("unsupported value for property " + NodeDocument.DELETED_ONCE);
            }
            queryBuilder.and(indexedProperty);
            queryBuilder.is(true);
        } else {
            queryBuilder.and(indexedProperty);
            queryBuilder.greaterThanEquals(startValue);
            if (NodeDocument.MODIFIED_IN_SECS.equals(indexedProperty) && canUseModifiedTimeIdx(startValue)) {
                hint = new BasicDBObject(NodeDocument.MODIFIED_IN_SECS, -1);
            }
        }
    }
    DBObject query = queryBuilder.get();
    String parentId = Utils.getParentIdFromLowerLimit(fromKey);
    long lockTime = -1;
    final Stopwatch watch = startWatch();
    boolean isSlaveOk = false;
    int resultSize = 0;
    CacheChangesTracker cacheChangesTracker = null;
    if (parentId != null && collection == Collection.NODES) {
        cacheChangesTracker = nodesCache.registerTracker(fromKey, toKey);
    }
    try {
        DBCursor cursor = dbCollection.find(query).sort(BY_ID_ASC);
        if (!disableIndexHint && !hasModifiedIdCompoundIndex) {
            cursor.hint(hint);
        }
        if (maxQueryTime > 0) {
            // OAK-2614: set maxTime if maxQueryTimeMS > 0
            cursor.maxTime(maxQueryTime, TimeUnit.MILLISECONDS);
        }
        ReadPreference readPreference = getMongoReadPreference(collection, parentId, null, getDefaultReadPreference(collection));
        if (readPreference.isSlaveOk()) {
            isSlaveOk = true;
            LOG.trace("Routing call to secondary for fetching children from [{}] to [{}]", fromKey, toKey);
        }
        cursor.setReadPreference(readPreference);
        List<T> list;
        try {
            list = new ArrayList<T>();
            for (int i = 0; i < limit && cursor.hasNext(); i++) {
                DBObject o = cursor.next();
                T doc = convertFromDBObject(collection, o);
                list.add(doc);
            }
            resultSize = list.size();
        } finally {
            cursor.close();
        }
        if (cacheChangesTracker != null) {
            nodesCache.putNonConflictingDocs(cacheChangesTracker, (List<NodeDocument>) list);
        }
        return list;
    } finally {
        if (cacheChangesTracker != null) {
            cacheChangesTracker.close();
        }
        stats.doneQuery(watch.elapsed(TimeUnit.NANOSECONDS), collection, fromKey, toKey, indexedProperty != null, resultSize, lockTime, isSlaveOk);
    }
}
Also used : DocumentStoreException(org.apache.jackrabbit.oak.plugins.document.DocumentStoreException) ReadPreference(com.mongodb.ReadPreference) 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) DBCollection(com.mongodb.DBCollection) BasicDBObject(com.mongodb.BasicDBObject) DBCursor(com.mongodb.DBCursor) CacheChangesTracker(org.apache.jackrabbit.oak.plugins.document.cache.CacheChangesTracker) Nonnull(javax.annotation.Nonnull)

Example 30 with DocumentStoreException

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

the class RetryReadIT method retry.

@Test
public void retry() {
    // must survive two consecutive failures. -> 2 retries
    store.failRead = 2;
    NodeDocument doc = store.find(NODES, Utils.getIdFromPath("/foo"));
    assertNull(doc);
    // previous result is cached and will not fail
    store.failRead = 3;
    doc = store.find(NODES, Utils.getIdFromPath("/foo"));
    assertNull(doc);
    // must fail with three consecutive failures on unknown path
    try {
        store.find(NODES, Utils.getIdFromPath("/bar"));
        fail("must fail with DocumentStoreException");
    } catch (DocumentStoreException e) {
    // expected
    }
}
Also used : DocumentStoreException(org.apache.jackrabbit.oak.plugins.document.DocumentStoreException) NodeDocument(org.apache.jackrabbit.oak.plugins.document.NodeDocument) Test(org.junit.Test) AbstractMongoConnectionTest(org.apache.jackrabbit.oak.plugins.document.AbstractMongoConnectionTest)

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