Search in sources :

Example 16 with MongoException

use of com.mongodb.MongoException in project jackrabbit-oak by apache.

the class ReplicaSetInfo method updateReplicaStatus.

void updateReplicaStatus() {
    BasicDBObject result;
    try {
        result = getReplicaStatus();
    } catch (MongoException e) {
        LOG.error("Can't get replica status", e);
        rootRevisions = null;
        secondariesSafeTimestamp = 0;
        return;
    }
    @SuppressWarnings("unchecked") Iterable<BasicBSONObject> members = (Iterable<BasicBSONObject>) result.get("members");
    if (members == null) {
        members = Collections.emptyList();
    }
    updateRevisions(members);
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) BasicBSONObject(org.bson.BasicBSONObject) MongoException(com.mongodb.MongoException)

Example 17 with MongoException

use of com.mongodb.MongoException in project jackrabbit-oak by apache.

the class MongoCacheConsistencyTest method getFixture.

@Override
public DocumentStoreFixture getFixture() throws Exception {
    Fongo fongo = new OakFongo("fongo") {

        private String suppressedEx = null;

        @Override
        protected void afterInsert(WriteResult result) {
            maybeThrow();
        }

        @Override
        protected void afterFindAndModify(DBObject result) {
            maybeThrow();
        }

        @Override
        protected void afterUpdate(WriteResult result) {
            maybeThrow();
        }

        @Override
        protected void afterRemove(WriteResult result) {
            maybeThrow();
        }

        @Override
        protected void beforeExecuteBulkWriteOperation(boolean ordered, Boolean bypassDocumentValidation, List<?> writeRequests, WriteConcern aWriteConcern) {
            // suppress potentially set exception message because
            // fongo bulk writes call other update methods
            suppressedEx = exceptionMsg;
            exceptionMsg = null;
        }

        @Override
        protected void afterExecuteBulkWriteOperation(BulkWriteResult result) {
            exceptionMsg = suppressedEx;
            suppressedEx = null;
            maybeThrow();
        }

        private void maybeThrow() {
            if (exceptionMsg != null) {
                throw new MongoException(exceptionMsg);
            }
        }
    };
    DocumentMK.Builder builder = provider.newBuilder().setAsyncDelay(0);
    final DocumentStore store = new MongoDocumentStore(fongo.getDB("oak"), builder);
    return new DocumentStoreFixture() {

        @Override
        public String getName() {
            return "MongoDB";
        }

        @Override
        public DocumentStore createDocumentStore(int clusterId) {
            return store;
        }
    };
}
Also used : Fongo(com.github.fakemongo.Fongo) OakFongo(com.mongodb.OakFongo) MongoException(com.mongodb.MongoException) DocumentMK(org.apache.jackrabbit.oak.plugins.document.DocumentMK) OakFongo(com.mongodb.OakFongo) DBObject(com.mongodb.DBObject) BulkWriteResult(com.mongodb.BulkWriteResult) DocumentStore(org.apache.jackrabbit.oak.plugins.document.DocumentStore) WriteResult(com.mongodb.WriteResult) BulkWriteResult(com.mongodb.BulkWriteResult) DocumentStoreFixture(org.apache.jackrabbit.oak.plugins.document.DocumentStoreFixture) WriteConcern(com.mongodb.WriteConcern) List(java.util.List)

Example 18 with MongoException

use of com.mongodb.MongoException in project jackrabbit-oak by apache.

the class MongoDocumentStore method findUncachedWithRetry.

/**
     * Finds a document and performs a number of retries if the read fails with
     * an exception.
     *
     * @param collection the collection to read from.
     * @param key the key of the document to find.
     * @param docReadPref the read preference.
     * @param retries the number of retries. Must not be negative.
     * @param <T> the document type of the given collection.
     * @return the document or {@code null} if the document doesn't exist.
     */
@CheckForNull
private <T extends Document> T findUncachedWithRetry(Collection<T> collection, String key, DocumentReadPreference docReadPref, int retries) {
    checkArgument(retries >= 0, "retries must not be negative");
    if (key.equals("0:/")) {
        LOG.trace("root node");
    }
    int numAttempts = retries + 1;
    MongoException ex = null;
    for (int i = 0; i < numAttempts; i++) {
        if (i > 0) {
            LOG.warn("Retrying read of " + key);
        }
        try {
            return findUncached(collection, key, docReadPref);
        } catch (MongoException e) {
            ex = e;
        }
    }
    if (ex != null) {
        throw ex;
    } else {
        // impossible to get here
        throw new IllegalStateException();
    }
}
Also used : MongoException(com.mongodb.MongoException) CheckForNull(javax.annotation.CheckForNull)

Example 19 with MongoException

use of com.mongodb.MongoException in project mongo-java-driver by mongodb.

the class MongoIterableSubscription method requestInitialData.

@Override
void requestInitialData() {
    mongoIterable.batchSize(getBatchSize());
    mongoIterable.batchCursor(new SingleResultCallback<AsyncBatchCursor<TResult>>() {

        @Override
        public void onResult(final AsyncBatchCursor<TResult> result, final Throwable t) {
            if (t != null) {
                onError(t);
            } else if (result != null) {
                batchCursor = result;
                requestMoreData();
            } else {
                onError(new MongoException("Unexpected error, no AsyncBatchCursor returned from the MongoIterable."));
            }
        }
    });
}
Also used : MongoException(com.mongodb.MongoException) AsyncBatchCursor(com.mongodb.async.AsyncBatchCursor)

Example 20 with MongoException

use of com.mongodb.MongoException in project mongo-java-driver by mongodb.

the class Base64Codec method encode.

public String encode(final byte[] in) {
    int modulus = 0;
    int bitWorkArea = 0;
    int numEncodedBytes = (in.length / BYTES_PER_UNENCODED_BLOCK) * BYTES_PER_ENCODED_BLOCK + ((in.length % BYTES_PER_UNENCODED_BLOCK == 0) ? 0 : 4);
    byte[] buffer = new byte[numEncodedBytes];
    int pos = 0;
    for (int b : in) {
        modulus = (modulus + 1) % BYTES_PER_UNENCODED_BLOCK;
        if (b < 0) {
            b += 256;
        }
        //  BITS_PER_BYTE
        bitWorkArea = (bitWorkArea << 8) + b;
        if (0 == modulus) {
            // 3 bytes = 24 bits = 4 * 6 bits to extract
            buffer[pos++] = ENCODE_TABLE[(bitWorkArea >> 18) & SIX_BIT_MASK];
            buffer[pos++] = ENCODE_TABLE[(bitWorkArea >> 12) & SIX_BIT_MASK];
            buffer[pos++] = ENCODE_TABLE[(bitWorkArea >> 6) & SIX_BIT_MASK];
            buffer[pos++] = ENCODE_TABLE[bitWorkArea & SIX_BIT_MASK];
        }
    }
    switch(// 0-2
    modulus) {
        case // 8 bits = 6 + 2
        1:
            // top 6 bits
            buffer[pos++] = ENCODE_TABLE[(bitWorkArea >> 2) & SIX_BIT_MASK];
            // remaining 2
            buffer[pos++] = ENCODE_TABLE[(bitWorkArea << 4) & SIX_BIT_MASK];
            buffer[pos++] = PAD;
            // Last entry no need to ++
            buffer[pos] = PAD;
            break;
        case // 16 bits = 6 + 6 + 4
        2:
            buffer[pos++] = ENCODE_TABLE[(bitWorkArea >> 10) & SIX_BIT_MASK];
            buffer[pos++] = ENCODE_TABLE[(bitWorkArea >> 4) & SIX_BIT_MASK];
            buffer[pos++] = ENCODE_TABLE[(bitWorkArea << 2) & SIX_BIT_MASK];
            // Last entry no need to ++
            buffer[pos] = PAD;
            break;
        default:
            break;
    }
    try {
        return new String(buffer, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        throw new MongoException("UTF-8 Charset is not available");
    }
}
Also used : MongoException(com.mongodb.MongoException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

MongoException (com.mongodb.MongoException)42 BasicDBObject (com.mongodb.BasicDBObject)22 DBObject (com.mongodb.DBObject)21 DBCollection (com.mongodb.DBCollection)16 FailedDBOperationException (edu.umass.cs.gnscommon.exceptions.server.FailedDBOperationException)12 JSONObject (org.json.JSONObject)12 DBCursor (com.mongodb.DBCursor)8 RecordNotFoundException (edu.umass.cs.gnscommon.exceptions.server.RecordNotFoundException)4 IOException (java.io.IOException)4 UnknownHostException (java.net.UnknownHostException)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 JSONException (org.json.JSONException)4 Stopwatch (com.google.common.base.Stopwatch)3 BasicDBList (com.mongodb.BasicDBList)3 DuplicateKeyException (com.mongodb.DuplicateKeyException)3 MongoClient (com.mongodb.MongoClient)3 BulkWriteOperation (com.mongodb.BulkWriteOperation)2 CommandResult (com.mongodb.CommandResult)2 MongoClientURI (com.mongodb.MongoClientURI)2