Search in sources :

Example 6 with ODistributedStorage

use of com.orientechnologies.orient.server.distributed.impl.ODistributedStorage in project orientdb by orientechnologies.

the class OCommandExecutorSQLHASyncDatabase method execute.

/**
   * Execute the SYNC DATABASE.
   */
public Object execute(final Map<Object, Object> iArgs) {
    final ODatabaseDocumentInternal database = getDatabase();
    database.checkSecurity(ORule.ResourceGeneric.DATABASE, "sync", ORole.PERMISSION_UPDATE);
    final OStorage stg = database.getStorage();
    if (!(stg instanceof ODistributedStorage))
        throw new ODistributedException("SYNC DATABASE command cannot be executed against a non distributed server");
    final ODistributedStorage dStg = (ODistributedStorage) stg;
    final OHazelcastPlugin dManager = (OHazelcastPlugin) dStg.getDistributedManager();
    if (dManager == null || !dManager.isEnabled())
        throw new OCommandExecutionException("OrientDB is not started in distributed mode");
    final String databaseName = database.getName();
    return dManager.installDatabase(true, databaseName, false, OGlobalConfiguration.DISTRIBUTED_BACKUP_TRY_INCREMENTAL_FIRST.getValueAsBoolean());
}
Also used : ODistributedStorage(com.orientechnologies.orient.server.distributed.impl.ODistributedStorage) ODistributedException(com.orientechnologies.orient.server.distributed.ODistributedException) OStorage(com.orientechnologies.orient.core.storage.OStorage) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) OHazelcastPlugin(com.orientechnologies.orient.server.hazelcast.OHazelcastPlugin) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)

Example 7 with ODistributedStorage

use of com.orientechnologies.orient.server.distributed.impl.ODistributedStorage in project orientdb by orientechnologies.

the class OSyncDatabaseTask method execute.

@Override
public Object execute(final ODistributedRequestId requestId, final OServer iServer, final ODistributedServerManager iManager, final ODatabaseDocumentInternal database) throws Exception {
    if (!iManager.getLocalNodeName().equals(getNodeSource())) {
        if (database == null)
            throw new ODistributedException("Database instance is null");
        final String databaseName = database.getName();
        final ODistributedDatabase dDatabase = checkIfCurrentDatabaseIsNotOlder(iManager, databaseName, null);
        try {
            final Long lastDeployment = (Long) iManager.getConfigurationMap().get(DEPLOYDB + databaseName);
            if (lastDeployment != null && lastDeployment.longValue() == random) {
                // SKIP IT
                ODistributedServerLog.debug(this, iManager.getLocalNodeName(), getNodeSource(), DIRECTION.NONE, "Skip deploying database '%s' because already executed", databaseName);
                return Boolean.FALSE;
            }
            iManager.getConfigurationMap().put(DEPLOYDB + databaseName, random);
            iManager.setDatabaseStatus(getNodeSource(), databaseName, ODistributedServerManager.DB_STATUS.SYNCHRONIZING);
            // PROPAGATE THE UPDATE TO ALL THE NODES
            //        iManager.sendRequest(databaseName, null, iManager.getActiveServers(),
            //            new OUpdateDatabaseStatusTask(databaseName, ODistributedServerManager.DB_STATUS.SYNCHRONIZING.name()),
            //            iManager.getNextMessageIdCounter(), ODistributedRequest.EXECUTION_MODE.RESPONSE, null, null);
            ODistributedServerLog.info(this, iManager.getLocalNodeName(), getNodeSource(), DIRECTION.OUT, "Deploying database %s...", databaseName);
            final AtomicReference<ODistributedMomentum> momentum = new AtomicReference<ODistributedMomentum>();
            File backupFile = ((ODistributedStorage) database.getStorage()).getLastValidBackup();
            if (backupFile == null || !backupFile.exists()) {
                // CREATE A BACKUP OF DATABASE FROM SCRATCH
                backupFile = new File(Orient.getTempPath() + "/backup_" + database.getName() + ".zip");
                final int compressionRate = OGlobalConfiguration.DISTRIBUTED_DEPLOYDB_TASK_COMPRESSION.getValueAsInteger();
                if (backupFile.exists())
                    backupFile.delete();
                else
                    backupFile.getParentFile().mkdirs();
                backupFile.createNewFile();
                final FileOutputStream fileOutputStream = new FileOutputStream(backupFile);
                final File completedFile = new File(backupFile.getAbsolutePath() + ".completed");
                if (completedFile.exists())
                    completedFile.delete();
                ODistributedServerLog.info(this, iManager.getLocalNodeName(), getNodeSource(), DIRECTION.OUT, "Creating backup of database '%s' (compressionRate=%d) in directory: %s...", databaseName, compressionRate, backupFile.getAbsolutePath());
                new Thread(new Runnable() {

                    @Override
                    public void run() {
                        Thread.currentThread().setName("OrientDB SyncDatabase node=" + iManager.getLocalNodeName() + " db=" + databaseName);
                        try {
                            database.activateOnCurrentThread();
                            ODistributedServerLog.info(this, iManager.getLocalNodeName(), getNodeSource(), DIRECTION.OUT, "Compressing database '%s' %d clusters %s...", databaseName, database.getClusterNames().size(), database.getClusterNames());
                            database.backup(fileOutputStream, null, new Callable<Object>() {

                                @Override
                                public Object call() throws Exception {
                                    momentum.set(dDatabase.getSyncConfiguration().getMomentum().copy());
                                    return null;
                                }
                            }, ODistributedServerLog.isDebugEnabled() ? new OCommandOutputListener() {

                                @Override
                                public void onMessage(String iText) {
                                    if (iText.startsWith("\n"))
                                        iText = iText.substring(1);
                                    OLogManager.instance().debug(this, iText);
                                }
                            } : null, OGlobalConfiguration.DISTRIBUTED_DEPLOYDB_TASK_COMPRESSION.getValueAsInteger(), CHUNK_MAX_SIZE);
                            ODistributedServerLog.info(this, iManager.getLocalNodeName(), getNodeSource(), DIRECTION.OUT, "Backup of database '%s' completed. lastOperationId=%s...", databaseName, requestId);
                        } catch (Throwable e) {
                            OLogManager.instance().error(this, "Cannot execute backup of database '%s' for deploy database", e, databaseName);
                        } finally {
                            try {
                                fileOutputStream.close();
                            } catch (IOException e) {
                            }
                            try {
                                completedFile.createNewFile();
                            } catch (IOException e) {
                                OLogManager.instance().error(this, "Cannot create file of backup completed: %s", e, completedFile);
                            }
                        }
                    }
                }).start();
                // RECORD LAST BACKUP TO BE REUSED IN CASE ANOTHER NODE ASK FOR THE SAME IN SHORT TIME WHILE THE DB IS NOT UPDATED
                ((ODistributedStorage) database.getStorage()).setLastValidBackup(backupFile);
            } else {
                momentum.set(dDatabase.getSyncConfiguration().getMomentum().copy());
                ODistributedServerLog.info(this, iManager.getLocalNodeName(), getNodeSource(), DIRECTION.OUT, "Reusing last backup of database '%s' in directory: %s...", databaseName, backupFile.getAbsolutePath());
            }
            for (int retry = 0; momentum.get() == null && retry < 10; ++retry) Thread.sleep(300);
            final ODistributedDatabaseChunk chunk = new ODistributedDatabaseChunk(backupFile, 0, CHUNK_MAX_SIZE, momentum.get(), false);
            ODistributedServerLog.info(this, iManager.getLocalNodeName(), getNodeSource(), ODistributedServerLog.DIRECTION.OUT, "- transferring chunk #%d offset=%d size=%s lsn=%s...", 1, 0, OFileUtils.getSizeAsNumber(chunk.buffer.length), momentum.get());
            if (chunk.last)
                // NO MORE CHUNKS: SET THE NODE ONLINE (SYNCHRONIZING ENDED)
                iManager.setDatabaseStatus(iManager.getLocalNodeName(), databaseName, ODistributedServerManager.DB_STATUS.ONLINE);
            return chunk;
        } catch (OLockException e) {
            ODistributedServerLog.debug(this, iManager.getLocalNodeName(), getNodeSource(), DIRECTION.NONE, "Skip deploying database %s because another node is doing it", databaseName);
        } finally {
            ODistributedServerLog.info(this, iManager.getLocalNodeName(), getNodeSource(), ODistributedServerLog.DIRECTION.OUT, "Deploy database task completed");
        }
    } else
        ODistributedServerLog.debug(this, iManager.getLocalNodeName(), getNodeSource(), DIRECTION.NONE, "Skip deploying database from the same node");
    return Boolean.FALSE;
}
Also used : ODistributedStorage(com.orientechnologies.orient.server.distributed.impl.ODistributedStorage) AtomicReference(java.util.concurrent.atomic.AtomicReference) OLockException(com.orientechnologies.common.concur.lock.OLockException) OLockException(com.orientechnologies.common.concur.lock.OLockException) ODistributedDatabaseChunk(com.orientechnologies.orient.server.distributed.impl.ODistributedDatabaseChunk) OCommandOutputListener(com.orientechnologies.orient.core.command.OCommandOutputListener)

Example 8 with ODistributedStorage

use of com.orientechnologies.orient.server.distributed.impl.ODistributedStorage in project orientdb by orientechnologies.

the class OUpdateDatabaseConfigurationTask method execute.

@Override
public Object execute(final ODistributedRequestId msgId, final OServer iServer, ODistributedServerManager iManager, final ODatabaseDocumentInternal database) throws Exception {
    final ODistributedStorage stg = (ODistributedStorage) iManager.getStorage(databaseName);
    if (stg != null) {
        ODistributedServerLog.debug(this, iManager.getLocalNodeName(), getNodeSource(), ODistributedServerLog.DIRECTION.IN, "Replacing distributed cfg for database '%s'\nnew: %s", databaseName, configuration);
        stg.setDistributedConfiguration(new OModifiableDistributedConfiguration(configuration));
    }
    return true;
}
Also used : ODistributedStorage(com.orientechnologies.orient.server.distributed.impl.ODistributedStorage)

Aggregations

ODistributedStorage (com.orientechnologies.orient.server.distributed.impl.ODistributedStorage)8 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)5 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)5 LinkedList (java.util.LinkedList)5 Callable (java.util.concurrent.Callable)5 Future (java.util.concurrent.Future)5 OLockException (com.orientechnologies.common.concur.lock.OLockException)1 OCommandOutputListener (com.orientechnologies.orient.core.command.OCommandOutputListener)1 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)1 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)1 OStorage (com.orientechnologies.orient.core.storage.OStorage)1 ODistributedException (com.orientechnologies.orient.server.distributed.ODistributedException)1 ODistributedDatabaseChunk (com.orientechnologies.orient.server.distributed.impl.ODistributedDatabaseChunk)1 OHazelcastPlugin (com.orientechnologies.orient.server.hazelcast.OHazelcastPlugin)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1