use of com.orientechnologies.orient.server.distributed.impl.task.OCopyDatabaseChunkTask 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, 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 + "_server" + dManager.getLocalNodeId() + "_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, 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) {
}
}
}
Aggregations