Search in sources :

Example 1 with OplogBatch

use of com.torodb.mongodb.repl.oplogreplier.OplogBatch in project torodb by torodb.

the class ContinuousOplogFetcher method fetch.

@Override
public OplogBatch fetch() throws StopReplicationException, RollbackReplicationException {
    if (state.isClosed()) {
        return FinishedOplogBatch.getInstance();
    }
    try {
        return retrier.retry(() -> {
            try {
                if (state.isClosed()) {
                    return FinishedOplogBatch.getInstance();
                }
                state.prepareToFetch();
                MongoCursor<OplogOperation> cursor = state.getLastUsedMongoCursor();
                Batch<OplogOperation> batch = cursor.tryFetchBatch();
                if (batch == null || !batch.hasNext()) {
                    Thread.sleep(1000);
                    batch = cursor.tryFetchBatch();
                    if (batch == null || !batch.hasNext()) {
                        return NotReadyForMoreOplogBatch.getInstance();
                    }
                }
                List<OplogOperation> fetchedOps = null;
                long fetchTime = 0;
                /*
           * As we already modify the cursor by fetching the batch, we cannot retry the whole block
           * (as the cursor would be reused and the previous batch will be discarted).
           *
           * Then, if we leave the following try section with an error, we need to discard the
           * cursor, so the next iteration starts from the last batch we returned. On the other
           * hand, if we finished successfully, then we need to update the state.
           */
                boolean successful = false;
                try {
                    fetchedOps = batch.asList();
                    fetchTime = batch.getFetchTime();
                    postBatchChecks(cursor, fetchedOps);
                    OplogBatch result = new NormalOplogBatch(fetchedOps, true);
                    successful = true;
                    return result;
                } finally {
                    if (!successful) {
                        cursor.close();
                    } else {
                        assert fetchedOps != null;
                        assert fetchTime != 0;
                        state.updateState(fetchedOps, fetchTime);
                    }
                }
            } catch (RestartFetchException ex) {
                //lets choose a new reader
                state.discardReader();
                //and then try again
                throw new RollbackException(ex);
            } catch (DeadCursorException ex) {
                //lets retry the whole block with the same reader
                throw new RollbackException(ex);
            } catch (StopReplicationException | RollbackReplicationException ex) {
                //do not try again
                throw new RetrierAbortException(ex);
            } catch (MongoServerException ex) {
                //TODO: Fix this violation on the abstraction!
                LOGGER.debug("Found an unwrapped MongodbServerException");
                //lets choose a new reader
                state.discardReader();
                //rollback and hopefully use another member
                throw new RollbackException(ex);
            } catch (MongoException | MongoRuntimeException ex) {
                LOGGER.warn("Catched an error while reading the remote " + "oplog: {}", ex.getLocalizedMessage());
                //lets choose a new reader
                state.discardReader();
                //rollback and hopefully use another member
                throw new RollbackException(ex);
            }
        }, Hint.CRITICAL, Hint.TIME_SENSIBLE);
    } catch (RetrierGiveUpException ex) {
        this.close();
        throw new StopReplicationException("Stopping replication after several attepts to " + "fetch the remote oplog", ex);
    } catch (RetrierAbortException ex) {
        this.close();
        Throwable cause = ex.getCause();
        if (cause != null) {
            if (cause instanceof StopReplicationException) {
                throw (StopReplicationException) cause;
            }
            if (cause instanceof RollbackReplicationException) {
                throw (RollbackReplicationException) cause;
            }
        }
        throw new StopReplicationException("Stopping replication after a unknown abort " + "exception", ex);
    }
}
Also used : DeadCursorException(com.eightkdata.mongowp.server.api.pojos.MongoCursor.DeadCursorException) MongoException(com.eightkdata.mongowp.exceptions.MongoException) MongoRuntimeException(com.eightkdata.mongowp.server.api.MongoRuntimeException) RollbackReplicationException(com.torodb.mongodb.repl.oplogreplier.RollbackReplicationException) RollbackException(com.torodb.core.transaction.RollbackException) RetrierGiveUpException(com.torodb.core.retrier.RetrierGiveUpException) NormalOplogBatch(com.torodb.mongodb.repl.oplogreplier.NormalOplogBatch) RetrierAbortException(com.torodb.core.retrier.RetrierAbortException) FinishedOplogBatch(com.torodb.mongodb.repl.oplogreplier.FinishedOplogBatch) OplogBatch(com.torodb.mongodb.repl.oplogreplier.OplogBatch) NotReadyForMoreOplogBatch(com.torodb.mongodb.repl.oplogreplier.NotReadyForMoreOplogBatch) NormalOplogBatch(com.torodb.mongodb.repl.oplogreplier.NormalOplogBatch) OplogOperation(com.eightkdata.mongowp.server.api.oplog.OplogOperation) MongoServerException(com.mongodb.MongoServerException) StopReplicationException(com.torodb.mongodb.repl.oplogreplier.StopReplicationException)

Example 2 with OplogBatch

use of com.torodb.mongodb.repl.oplogreplier.OplogBatch in project torodb by torodb.

the class FilteredOplogFetcher method fetch.

@Override
public OplogBatch fetch() throws StopReplicationException, RollbackReplicationException {
    OplogBatch batch = delegate.fetch();
    List<OplogOperation> filteredBatch = batch.getOps().stream().filter(filter).collect(Collectors.toList());
    if (filteredBatch.isEmpty()) {
        if (batch.isLastOne()) {
            return FinishedOplogBatch.getInstance();
        } else {
            return NotReadyForMoreOplogBatch.getInstance();
        }
    } else {
        if (batch.isLastOne()) {
            throw new AssertionError("Batchs produced by a finished oplog fetcher cannot " + "contain ops");
        }
        return new NormalOplogBatch(filteredBatch, batch.isReadyForMore());
    }
}
Also used : NormalOplogBatch(com.torodb.mongodb.repl.oplogreplier.NormalOplogBatch) NotReadyForMoreOplogBatch(com.torodb.mongodb.repl.oplogreplier.NotReadyForMoreOplogBatch) FinishedOplogBatch(com.torodb.mongodb.repl.oplogreplier.FinishedOplogBatch) NormalOplogBatch(com.torodb.mongodb.repl.oplogreplier.NormalOplogBatch) OplogBatch(com.torodb.mongodb.repl.oplogreplier.OplogBatch) OplogOperation(com.eightkdata.mongowp.server.api.oplog.OplogOperation)

Example 3 with OplogBatch

use of com.torodb.mongodb.repl.oplogreplier.OplogBatch in project torodb by torodb.

the class ContinuousOplogFetcherTest method testMediumOplog.

@Test
public void testMediumOplog() throws Exception {
    int oplogSize = 1000;
    List<OplogOperation> oplog = createInsertStream(this::createSimpleInsert).limit(oplogSize).collect(Collectors.toList());
    oplogSupplier = () -> oplog;
    OplogOperation firstOp = oplog.get(0);
    ContinuousOplogFetcher fetcher = factory.createFetcher(firstOp.getHash(), firstOp.getOpTime());
    List<OplogOperation> recivedOplog = new ArrayList<>(oplogSize);
    OplogBatch batch = null;
    while (batch == null || !(batch.isLastOne() || batch.isReadyForMore())) {
        batch = fetcher.fetch();
        recivedOplog.addAll(batch.getOps());
    }
    assertEquals("Unexpected number of oplog entries fetched: ", oplog.size() - 1, recivedOplog.size());
    assertEquals(oplog.subList(1, oplog.size()), recivedOplog);
}
Also used : OplogBatch(com.torodb.mongodb.repl.oplogreplier.OplogBatch) InsertOplogOperation(com.eightkdata.mongowp.server.api.oplog.InsertOplogOperation) OplogOperation(com.eightkdata.mongowp.server.api.oplog.OplogOperation) Test(org.junit.Test)

Example 4 with OplogBatch

use of com.torodb.mongodb.repl.oplogreplier.OplogBatch in project torodb by torodb.

the class ContinuousOplogFetcherTest method testBigOplog.

@Test
public void testBigOplog() throws Exception {
    int oplogSize = 100000;
    List<OplogOperation> oplog = createInsertStream(this::createSimpleInsert).limit(oplogSize).collect(Collectors.toList());
    oplogSupplier = () -> oplog;
    OplogOperation firstOp = oplog.get(0);
    ContinuousOplogFetcher fetcher = factory.createFetcher(firstOp.getHash(), firstOp.getOpTime());
    List<OplogOperation> recivedOplog = new ArrayList<>(oplogSize);
    OplogBatch batch = null;
    while (batch == null || !(batch.isLastOne() || !batch.isReadyForMore())) {
        batch = fetcher.fetch();
        recivedOplog.addAll(batch.getOps());
    }
    assertEquals("Unexpected number of oplog entries fetched: ", oplog.size() - 1, recivedOplog.size());
    assertEquals(oplog.subList(1, oplog.size()), recivedOplog);
}
Also used : OplogBatch(com.torodb.mongodb.repl.oplogreplier.OplogBatch) InsertOplogOperation(com.eightkdata.mongowp.server.api.oplog.InsertOplogOperation) OplogOperation(com.eightkdata.mongowp.server.api.oplog.OplogOperation) Test(org.junit.Test)

Example 5 with OplogBatch

use of com.torodb.mongodb.repl.oplogreplier.OplogBatch in project torodb by torodb.

the class ContinuousOplogFetcherTest method testShortOplog.

@Test
public void testShortOplog() throws Exception {
    int oplogSize = 2;
    List<OplogOperation> oplog = createInsertStream(this::createSimpleInsert).limit(oplogSize).collect(Collectors.toList());
    oplogSupplier = () -> oplog;
    OplogOperation firstOp = oplog.get(0);
    ContinuousOplogFetcher fetcher = factory.createFetcher(firstOp.getHash(), firstOp.getOpTime());
    List<OplogOperation> recivedOplog = new ArrayList<>(oplogSize);
    OplogBatch batch = null;
    while (batch == null || !(batch.isLastOne() || batch.isReadyForMore())) {
        batch = fetcher.fetch();
        recivedOplog.addAll(batch.getOps());
    }
    assertEquals("Unexpected number of oplog entries fetched: ", oplog.size() - 1, recivedOplog.size());
    assertEquals(oplog.subList(1, oplog.size()), recivedOplog);
}
Also used : OplogBatch(com.torodb.mongodb.repl.oplogreplier.OplogBatch) InsertOplogOperation(com.eightkdata.mongowp.server.api.oplog.InsertOplogOperation) OplogOperation(com.eightkdata.mongowp.server.api.oplog.OplogOperation) Test(org.junit.Test)

Aggregations

OplogOperation (com.eightkdata.mongowp.server.api.oplog.OplogOperation)5 OplogBatch (com.torodb.mongodb.repl.oplogreplier.OplogBatch)5 InsertOplogOperation (com.eightkdata.mongowp.server.api.oplog.InsertOplogOperation)3 Test (org.junit.Test)3 FinishedOplogBatch (com.torodb.mongodb.repl.oplogreplier.FinishedOplogBatch)2 NormalOplogBatch (com.torodb.mongodb.repl.oplogreplier.NormalOplogBatch)2 NotReadyForMoreOplogBatch (com.torodb.mongodb.repl.oplogreplier.NotReadyForMoreOplogBatch)2 MongoException (com.eightkdata.mongowp.exceptions.MongoException)1 MongoRuntimeException (com.eightkdata.mongowp.server.api.MongoRuntimeException)1 DeadCursorException (com.eightkdata.mongowp.server.api.pojos.MongoCursor.DeadCursorException)1 MongoServerException (com.mongodb.MongoServerException)1 RetrierAbortException (com.torodb.core.retrier.RetrierAbortException)1 RetrierGiveUpException (com.torodb.core.retrier.RetrierGiveUpException)1 RollbackException (com.torodb.core.transaction.RollbackException)1 RollbackReplicationException (com.torodb.mongodb.repl.oplogreplier.RollbackReplicationException)1 StopReplicationException (com.torodb.mongodb.repl.oplogreplier.StopReplicationException)1