use of com.eightkdata.mongowp.exceptions.NotMasterException 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());
}
}
}
use of com.eightkdata.mongowp.exceptions.NotMasterException 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;
}
}
}
}
}
Aggregations