Search in sources :

Example 1 with Entry

use of com.torodb.mongodb.commands.signatures.admin.ListCollectionsCommand.ListCollectionsResult.Entry in project torodb by torodb.

the class TransactionalDbCloner method cloneDatabase.

/**
   *
   * @param dstDb
   * @param remoteConnection
   * @param transaction
   * @param opts
   * @throws CloningException
   * @throws NotMasterException if {@link CloneOptions#getWritePermissionSupplier()
   *                            opts.getWritePermissionSupplier().get()} is evaluated to false
   */
public void cloneDatabase(@Nonnull String dstDb, @Nonnull MongoConnection remoteConnection, @Nonnull WriteMongodTransaction transaction, @Nonnull CloneOptions opts) throws CloningException, NotMasterException, MongoException {
    if (!remoteConnection.isRemote() && opts.getDbToClone().equals(dstDb)) {
        LOGGER.warn("Trying to clone a database to itself! Ignoring it");
        return;
    }
    String fromDb = opts.getDbToClone();
    CursorResult<Entry> listCollections;
    try {
        listCollections = ListCollectionsRequester.getListCollections(remoteConnection, fromDb, null);
    } catch (MongoException ex) {
        throw new CloningException("It was impossible to get information from the remote server", ex);
    }
    if (!opts.getWritePermissionSupplier().get()) {
        throw new NotMasterException("Destiny database cannot be written");
    }
    Map<String, CollectionOptions> collsToClone = Maps.newHashMap();
    for (Iterator<Entry> iterator = listCollections.getFirstBatch(); iterator.hasNext(); ) {
        Entry collEntry = iterator.next();
        String collName = collEntry.getCollectionName();
        if (opts.getCollsToIgnore().contains(collName)) {
            LOGGER.debug("Not cloning {} because is marked as an ignored collection", collName);
            continue;
        }
        if (!NamespaceUtil.isUserWritable(fromDb, collName)) {
            LOGGER.info("Not cloning {} because is a not user writable", collName);
            continue;
        }
        if (NamespaceUtil.isNormal(fromDb, collName)) {
            LOGGER.info("Not cloning {} because it is not normal", collName);
            continue;
        }
        LOGGER.info("Collection {}.{} will be cloned", fromDb, collName);
        collsToClone.put(collName, collEntry.getCollectionOptions());
    }
    if (!opts.getWritePermissionSupplier().get()) {
        throw new NotMasterException("Destiny database cannot be written " + "after get collections info");
    }
    for (Map.Entry<String, CollectionOptions> entry : collsToClone.entrySet()) {
        dropCollection(transaction, dstDb, entry.getKey());
        createCollection(transaction, dstDb, entry.getKey(), entry.getValue());
    }
    if (opts.isCloneData()) {
        for (Map.Entry<String, CollectionOptions> entry : collsToClone.entrySet()) {
            cloneCollection(dstDb, remoteConnection, transaction, opts, entry.getKey(), entry.getValue());
        }
    }
    if (opts.isCloneIndexes()) {
        for (Map.Entry<String, CollectionOptions> entry : collsToClone.entrySet()) {
            cloneIndex(dstDb, remoteConnection, transaction, opts, entry.getKey(), entry.getValue());
        }
    }
}
Also used : Entry(com.torodb.mongodb.commands.signatures.admin.ListCollectionsCommand.ListCollectionsResult.Entry) MongoException(com.eightkdata.mongowp.exceptions.MongoException) CollectionOptions(com.torodb.mongodb.commands.pojos.CollectionOptions) NotMasterException(com.eightkdata.mongowp.exceptions.NotMasterException) Map(java.util.Map)

Example 2 with Entry

use of com.torodb.mongodb.commands.signatures.admin.ListCollectionsCommand.ListCollectionsResult.Entry in project torodb by torodb.

the class AkkaDbCloner method cloneDatabase.

@Override
public void cloneDatabase(String dstDb, MongoClient remoteClient, MongodServer localServer, CloneOptions opts) throws CloningException, NotMasterException, MongoException {
    Preconditions.checkState(isRunning(), "This db cloner is not running");
    if (!remoteClient.isRemote() && opts.getDbToClone().equals(dstDb)) {
        LOGGER.warn("Trying to clone a database to itself! Ignoring it");
        return;
    }
    String fromDb = opts.getDbToClone();
    CursorResult<Entry> listCollections;
    try (MongoConnection remoteConnection = remoteClient.openConnection()) {
        listCollections = ListCollectionsRequester.getListCollections(remoteConnection, fromDb, null);
    } catch (MongoException ex) {
        throw new CloningException("It was impossible to get information from the remote server", ex);
    }
    if (!opts.getWritePermissionSupplier().get()) {
        throw new NotMasterException("Destiny database cannot be written");
    }
    List<Entry> collsToClone = getCollsToClone(listCollections, fromDb, opts);
    if (!opts.getWritePermissionSupplier().get()) {
        throw new NotMasterException("Destiny database cannot be written " + "after get collections info");
    }
    try {
        for (Entry entry : collsToClone) {
            prepareCollection(localServer, dstDb, entry);
        }
    } catch (RollbackException ex) {
        throw new AssertionError("Unexpected rollback exception", ex);
    }
    Materializer materializer = ActorMaterializer.create(getActorSystem());
    try (MongoConnection remoteConnection = remoteClient.openConnection()) {
        if (opts.isCloneData()) {
            for (Entry entry : collsToClone) {
                LOGGER.info("Cloning collection data {}.{} into {}.{}", fromDb, entry.getCollectionName(), dstDb, entry.getCollectionName());
                try {
                    cloneCollection(localServer, remoteConnection, dstDb, opts, materializer, entry);
                } catch (CompletionException completionException) {
                    Throwable cause = completionException.getCause();
                    if (cause instanceof RollbackException) {
                        throw (RollbackException) cause;
                    }
                    throw completionException;
                }
            }
        }
        if (opts.isCloneIndexes()) {
            for (Entry entry : collsToClone) {
                LOGGER.info("Cloning collection indexes {}.{} into {}.{}", fromDb, entry.getCollectionName(), dstDb, entry.getCollectionName());
                try {
                    cloneIndex(localServer, dstDb, dstDb, remoteConnection, opts, entry.getCollectionName(), entry.getCollectionName());
                } catch (CompletionException completionException) {
                    Throwable cause = completionException.getCause();
                    if (cause instanceof RollbackException) {
                        throw (RollbackException) cause;
                    }
                    throw completionException;
                }
            }
        }
    }
}
Also used : MongoException(com.eightkdata.mongowp.exceptions.MongoException) RollbackException(com.torodb.core.transaction.RollbackException) Entry(com.torodb.mongodb.commands.signatures.admin.ListCollectionsCommand.ListCollectionsResult.Entry) CompletionException(java.util.concurrent.CompletionException) NotMasterException(com.eightkdata.mongowp.exceptions.NotMasterException) MongoConnection(com.eightkdata.mongowp.client.core.MongoConnection) ActorMaterializer(akka.stream.ActorMaterializer) Materializer(akka.stream.Materializer)

Example 3 with Entry

use of com.torodb.mongodb.commands.signatures.admin.ListCollectionsCommand.ListCollectionsResult.Entry in project torodb by torodb.

the class AkkaDbCloner method getCollsToClone.

private List<Entry> getCollsToClone(CursorResult<Entry> listCollections, String fromDb, CloneOptions opts) {
    List<Entry> collsToClone = new ArrayList<>();
    for (Iterator<Entry> iterator = listCollections.getFirstBatch(); iterator.hasNext(); ) {
        Entry collEntry = iterator.next();
        String collName = collEntry.getCollectionName();
        if (opts.getCollsToIgnore().contains(collName)) {
            LOGGER.debug("Not cloning {} because is marked as an ignored collection", collName);
            continue;
        }
        if (!NamespaceUtil.isUserWritable(fromDb, collName)) {
            LOGGER.info("Not cloning {} because is a not user writable", collName);
            continue;
        }
        if (NamespaceUtil.isNormal(fromDb, collName)) {
            LOGGER.info("Not cloning {} because it is not normal", collName);
            continue;
        }
        if (!opts.getCollectionFilter().test(collName)) {
            LOGGER.info("Not cloning {} because it didn't pass the given filter predicate", collName);
            continue;
        }
        LOGGER.info("Collection {}.{} will be cloned", fromDb, collName);
        collsToClone.add(collEntry);
    }
    return collsToClone;
}
Also used : Entry(com.torodb.mongodb.commands.signatures.admin.ListCollectionsCommand.ListCollectionsResult.Entry) ArrayList(java.util.ArrayList)

Aggregations

Entry (com.torodb.mongodb.commands.signatures.admin.ListCollectionsCommand.ListCollectionsResult.Entry)3 MongoException (com.eightkdata.mongowp.exceptions.MongoException)2 NotMasterException (com.eightkdata.mongowp.exceptions.NotMasterException)2 ActorMaterializer (akka.stream.ActorMaterializer)1 Materializer (akka.stream.Materializer)1 MongoConnection (com.eightkdata.mongowp.client.core.MongoConnection)1 RollbackException (com.torodb.core.transaction.RollbackException)1 CollectionOptions (com.torodb.mongodb.commands.pojos.CollectionOptions)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 CompletionException (java.util.concurrent.CompletionException)1