Search in sources :

Example 1 with OSyncClusterTask

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

the class OCommandExecutorSQLHASyncCluster method replaceCluster.

public static Object replaceCluster(final ODistributedAbstractPlugin dManager, final OServer serverInstance, final String databaseName, final String clusterName) {
    final ODistributedConfiguration cfg = dManager.getDatabaseConfiguration(databaseName);
    final String dbPath = serverInstance.getDatabaseDirectory() + databaseName;
    final String nodeName = dManager.getLocalNodeName();
    final List<String> nodesWhereClusterIsCfg = cfg.getServers(clusterName, null);
    nodesWhereClusterIsCfg.remove(nodeName);
    if (nodesWhereClusterIsCfg.isEmpty())
        throw new OCommandExecutionException("Cannot synchronize cluster '" + clusterName + "' because is not configured on any running nodes");
    final OSyncClusterTask task = new OSyncClusterTask(clusterName);
    final ODistributedResponse response = dManager.sendRequest(databaseName, null, nodesWhereClusterIsCfg, task, dManager.getNextMessageIdCounter(), ODistributedRequest.EXECUTION_MODE.RESPONSE, null, null);
    final Map<String, Object> results = (Map<String, Object>) response.getPayload();
    File tempFile = null;
    FileOutputStream out = null;
    try {
        tempFile = new File(Orient.getTempPath() + "/backup_" + databaseName + "_" + clusterName + "_toInstall.zip");
        if (tempFile.exists())
            tempFile.delete();
        else
            tempFile.getParentFile().mkdirs();
        tempFile.createNewFile();
        long fileSize = 0;
        out = new FileOutputStream(tempFile, false);
        for (Map.Entry<String, Object> r : results.entrySet()) {
            final Object value = r.getValue();
            if (value instanceof Boolean) {
                continue;
            } else if (value instanceof Throwable) {
                ODistributedServerLog.error(null, nodeName, r.getKey(), ODistributedServerLog.DIRECTION.IN, "error on installing cluster %s in %s", (Exception) value, databaseName, dbPath);
            } else if (value instanceof ODistributedDatabaseChunk) {
                ODistributedDatabaseChunk chunk = (ODistributedDatabaseChunk) value;
                // DELETE ANY PREVIOUS .COMPLETED FILE
                final File completedFile = new File(tempFile.getAbsolutePath() + ".completed");
                if (completedFile.exists())
                    completedFile.delete();
                fileSize = writeDatabaseChunk(nodeName, 1, chunk, out);
                for (int chunkNum = 2; !chunk.last; chunkNum++) {
                    final Object result = dManager.sendRequest(databaseName, null, OMultiValue.getSingletonList(r.getKey()), new OCopyDatabaseChunkTask(chunk.filePath, chunkNum, chunk.offset + chunk.buffer.length, false), dManager.getNextMessageIdCounter(), ODistributedRequest.EXECUTION_MODE.RESPONSE, null, null);
                    if (result instanceof Boolean)
                        continue;
                    else if (result instanceof Exception) {
                        ODistributedServerLog.error(null, nodeName, r.getKey(), ODistributedServerLog.DIRECTION.IN, "error on installing database %s in %s (chunk #%d)", (Exception) result, databaseName, dbPath, chunkNum);
                    } else if (result instanceof ODistributedDatabaseChunk) {
                        chunk = (ODistributedDatabaseChunk) result;
                        fileSize += writeDatabaseChunk(nodeName, chunkNum, chunk, out);
                    }
                }
                out.flush();
                // CREATE THE .COMPLETED FILE TO SIGNAL EOF
                new File(tempFile.getAbsolutePath() + ".completed").createNewFile();
            }
        }
        final String tempDirectoryPath = Orient.getTempPath() + "/backup_" + databaseName + "_" + clusterName + "_toInstall";
        final File tempDirectory = new File(tempDirectoryPath);
        tempDirectory.mkdirs();
        OZIPCompressionUtil.uncompressDirectory(new FileInputStream(tempFile), tempDirectory.getAbsolutePath(), null);
        ODatabaseDocumentInternal db = ODatabaseRecordThreadLocal.INSTANCE.getIfDefined();
        final boolean openDatabaseHere = db == null;
        if (db == null)
            db = serverInstance.openDatabase("plocal:" + dbPath, "", "", null, true);
        try {
            final OAbstractPaginatedStorage stg = (OAbstractPaginatedStorage) db.getStorage().getUnderlying();
            // TODO: FREEZE COULD IT NOT NEEDED
            stg.freeze(false);
            try {
                final OPaginatedCluster cluster = (OPaginatedCluster) stg.getClusterByName(clusterName);
                final File tempClusterFile = new File(tempDirectoryPath + "/" + clusterName + OPaginatedCluster.DEF_EXTENSION);
                cluster.replaceFile(tempClusterFile);
            } finally {
                stg.release();
            }
            db.getLocalCache().invalidate();
        } finally {
            if (openDatabaseHere)
                db.close();
        }
        return String.format("Cluster correctly replaced, transferred %d bytes", fileSize);
    } catch (Exception e) {
        ODistributedServerLog.error(null, nodeName, null, ODistributedServerLog.DIRECTION.NONE, "error on transferring database '%s' to '%s'", e, databaseName, tempFile);
        throw OException.wrapException(new ODistributedException("Error on transferring database"), e);
    } finally {
        try {
            if (out != null) {
                out.flush();
                out.close();
            }
        } catch (IOException e) {
        }
    }
}
Also used : ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal) OCopyDatabaseChunkTask(com.orientechnologies.orient.server.distributed.impl.task.OCopyDatabaseChunkTask) OPaginatedCluster(com.orientechnologies.orient.core.storage.impl.local.paginated.OPaginatedCluster) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) OAbstractPaginatedStorage(com.orientechnologies.orient.core.storage.impl.local.OAbstractPaginatedStorage) IOException(java.io.IOException) OSyncClusterTask(com.orientechnologies.orient.server.distributed.impl.task.OSyncClusterTask) OException(com.orientechnologies.common.exception.OException) OCommandSQLParsingException(com.orientechnologies.orient.core.sql.OCommandSQLParsingException) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) FileOutputStream(java.io.FileOutputStream) ODistributedDatabaseChunk(com.orientechnologies.orient.server.distributed.impl.ODistributedDatabaseChunk) Map(java.util.Map) File(java.io.File)

Aggregations

OException (com.orientechnologies.common.exception.OException)1 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)1 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)1 OCommandSQLParsingException (com.orientechnologies.orient.core.sql.OCommandSQLParsingException)1 OAbstractPaginatedStorage (com.orientechnologies.orient.core.storage.impl.local.OAbstractPaginatedStorage)1 OPaginatedCluster (com.orientechnologies.orient.core.storage.impl.local.paginated.OPaginatedCluster)1 ODistributedDatabaseChunk (com.orientechnologies.orient.server.distributed.impl.ODistributedDatabaseChunk)1 OCopyDatabaseChunkTask (com.orientechnologies.orient.server.distributed.impl.task.OCopyDatabaseChunkTask)1 OSyncClusterTask (com.orientechnologies.orient.server.distributed.impl.task.OSyncClusterTask)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 Map (java.util.Map)1