Search in sources :

Example 66 with ODatabaseDocumentInternal

use of com.orientechnologies.orient.core.db.ODatabaseDocumentInternal in project orientdb by orientechnologies.

the class OAbstractPaginatedStorage method executeCommand.

public Object executeCommand(final OCommandRequestText iCommand, final OCommandExecutor executor) {
    if (iCommand.isIdempotent() && !executor.isIdempotent())
        throw new OCommandExecutionException("Cannot execute non idempotent command");
    long beginTime = Orient.instance().getProfiler().startChrono();
    try {
        ODatabaseDocumentInternal db = ODatabaseRecordThreadLocal.INSTANCE.get();
        // CALL BEFORE COMMAND
        Iterable<ODatabaseListener> listeners = db.getListeners();
        for (ODatabaseListener oDatabaseListener : listeners) {
            oDatabaseListener.onBeforeCommand(iCommand, executor);
        }
        boolean foundInCache = false;
        Object result = null;
        if (iCommand.isCacheableResult() && executor.isCacheable() && iCommand.getParameters() == null) {
            // TRY WITH COMMAND CACHE
            result = db.getMetadata().getCommandCache().get(db.getUser(), iCommand.getText(), iCommand.getLimit());
            if (result != null) {
                foundInCache = true;
                if (iCommand.getResultListener() != null) {
                    // INVOKE THE LISTENER IF ANY
                    if (result instanceof Collection) {
                        for (Object o : (Collection) result) iCommand.getResultListener().result(o);
                    } else
                        iCommand.getResultListener().result(result);
                    // RESET THE RESULT TO AVOID TO SEND IT TWICE
                    result = null;
                }
            }
        }
        if (!foundInCache) {
            // EXECUTE THE COMMAND
            result = executor.execute(iCommand.getParameters());
            if (result != null && iCommand.isCacheableResult() && executor.isCacheable() && (iCommand.getParameters() == null || iCommand.getParameters().isEmpty()))
                // CACHE THE COMMAND RESULT
                db.getMetadata().getCommandCache().put(db.getUser(), iCommand.getText(), result, iCommand.getLimit(), executor.getInvolvedClusters(), System.currentTimeMillis() - beginTime);
        }
        // CALL AFTER COMMAND
        for (ODatabaseListener oDatabaseListener : listeners) {
            oDatabaseListener.onAfterCommand(iCommand, executor, result);
        }
        return result;
    } catch (OException e) {
        // PASS THROUGH
        throw e;
    } catch (Exception e) {
        throw OException.wrapException(new OCommandExecutionException("Error on execution of command: " + iCommand), e);
    } finally {
        if (Orient.instance().getProfiler().isRecording()) {
            final ODatabaseDocumentInternal db = ODatabaseRecordThreadLocal.INSTANCE.getIfDefined();
            if (db != null) {
                final OSecurityUser user = db.getUser();
                final String userString = user != null ? user.toString() : null;
                Orient.instance().getProfiler().stopChrono("db." + ODatabaseRecordThreadLocal.INSTANCE.get().getName() + ".command." + iCommand.toString(), "Command executed against the database", beginTime, "db.*.command.*", null, userString);
            }
        }
    }
}
Also used : OException(com.orientechnologies.common.exception.OException) OSecurityUser(com.orientechnologies.orient.core.metadata.security.OSecurityUser) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal) ODatabaseListener(com.orientechnologies.orient.core.db.ODatabaseListener) OException(com.orientechnologies.common.exception.OException) OModificationOperationProhibitedException(com.orientechnologies.common.concur.lock.OModificationOperationProhibitedException)

Example 67 with ODatabaseDocumentInternal

use of com.orientechnologies.orient.core.db.ODatabaseDocumentInternal in project orientdb by orientechnologies.

the class OCommandExecutorSQLHAStatus method execute.

/**
   * Execute the command.
   */
public Object execute(final Map<Object, Object> iArgs) {
    final ODatabaseDocumentInternal database = getDatabase();
    database.checkSecurity(ORule.ResourceGeneric.SERVER, "status", ORole.PERMISSION_READ);
    final String dbUrl = database.getURL();
    final String path = dbUrl.substring(dbUrl.indexOf(":") + 1);
    final OServer serverInstance = OServer.getInstanceByPath(path);
    final OHazelcastPlugin dManager = (OHazelcastPlugin) serverInstance.getDistributedManager();
    if (dManager == null || !dManager.isEnabled())
        throw new OCommandExecutionException("OrientDB is not started in distributed mode");
    final String databaseName = database.getName();
    final ODistributedConfiguration cfg = dManager.getDatabaseConfiguration(databaseName);
    if (parsedStatement.outputText) {
        final StringBuilder output = new StringBuilder();
        if (parsedStatement.servers)
            output.append(ODistributedOutput.formatServerStatus(dManager, dManager.getClusterConfiguration()));
        if (parsedStatement.db)
            output.append(ODistributedOutput.formatClusterTable(dManager, databaseName, cfg, dManager.getAvailableNodes(databaseName)));
        if (parsedStatement.latency)
            output.append(ODistributedOutput.formatLatency(dManager, dManager.getClusterConfiguration()));
        if (parsedStatement.messages)
            output.append(ODistributedOutput.formatMessages(dManager, dManager.getClusterConfiguration()));
        return output.toString();
    }
    final ODocument output = new ODocument();
    if (parsedStatement.servers)
        output.field("servers", dManager.getClusterConfiguration(), OType.EMBEDDED);
    if (parsedStatement.db)
        output.field("database", cfg.getDocument(), OType.EMBEDDED);
    return output;
}
Also used : OServer(com.orientechnologies.orient.server.OServer) OCommandExecutionException(com.orientechnologies.orient.core.exception.OCommandExecutionException) OHazelcastPlugin(com.orientechnologies.orient.server.hazelcast.OHazelcastPlugin) ODistributedConfiguration(com.orientechnologies.orient.server.distributed.ODistributedConfiguration) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 68 with ODatabaseDocumentInternal

use of com.orientechnologies.orient.core.db.ODatabaseDocumentInternal 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)

Example 69 with ODatabaseDocumentInternal

use of com.orientechnologies.orient.core.db.ODatabaseDocumentInternal 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 70 with ODatabaseDocumentInternal

use of com.orientechnologies.orient.core.db.ODatabaseDocumentInternal in project orientdb by orientechnologies.

the class OHazelcastPlugin method onCreate.

@Override
public void onCreate(final ODatabaseInternal iDatabase) {
    if (!isRelatedToLocalServer(iDatabase))
        return;
    if (status != NODE_STATUS.ONLINE)
        return;
    final ODatabaseDocumentInternal currDb = ODatabaseRecordThreadLocal.INSTANCE.getIfDefined();
    try {
        final String dbName = iDatabase.getName();
        final ODocument dCfg = (ODocument) configurationMap.get(OHazelcastPlugin.CONFIG_DATABASE_PREFIX + dbName);
        if (dCfg != null && getAvailableNodes(dbName) > 0) {
            throw new ODistributedException("Cannot create the new database '" + dbName + "' because it is already present in distributed configuration");
        }
        // INIT THE STORAGE
        getStorage(dbName);
        final ODistributedConfiguration cfg = getDatabaseConfiguration(dbName);
        final ODistributedDatabaseImpl distribDatabase = messageService.registerDatabase(dbName, cfg);
        distribDatabase.resume();
        distribDatabase.setOnline();
        // TODO: TEMPORARY PATCH TO WAIT FOR DB PROPAGATION IN CFG TO ALL THE OTHER SERVERS
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            throw new ODistributedException("Error on creating database '" + dbName + "' on distributed nodes");
        }
        // WAIT UNTIL THE DATABASE HAS BEEN PROPAGATED TO ALL THE SERVERS
        final Set<String> servers = cfg.getAllConfiguredServers();
        if (servers.size() > 1) {
            int retry = 0;
            for (; retry < 100; ++retry) {
                boolean allServersAreOnline = true;
                for (String server : servers) {
                    if (!isNodeOnline(server, dbName)) {
                        allServersAreOnline = false;
                        break;
                    }
                }
                if (allServersAreOnline)
                    break;
                // WAIT FOR ANOTHER RETRY
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    throw new ODistributedException("Error on creating database '" + dbName + "' on distributed nodes");
                }
            }
            if (retry >= 100)
                ODistributedServerLog.warn(this, getLocalNodeName(), null, DIRECTION.NONE, "Timeout waiting for all nodes to be up for database %s", dbName);
        }
        onOpen(iDatabase);
    } finally {
        // RESTORE ORIGINAL DATABASE INSTANCE IN TL
        ODatabaseRecordThreadLocal.INSTANCE.set(currDb);
    }
}
Also used : OInterruptedException(com.orientechnologies.common.concur.lock.OInterruptedException) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Aggregations

ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)139 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)42 OStorage (com.orientechnologies.orient.core.storage.OStorage)31 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)26 OStorageProxy (com.orientechnologies.orient.core.storage.OStorageProxy)20 OAutoshardedStorage (com.orientechnologies.orient.core.storage.OAutoshardedStorage)18 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)17 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)16 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)14 ORID (com.orientechnologies.orient.core.id.ORID)13 IOException (java.io.IOException)13 OException (com.orientechnologies.common.exception.OException)12 ORecordId (com.orientechnologies.orient.core.id.ORecordId)11 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)10 OSchemaException (com.orientechnologies.orient.core.exception.OSchemaException)10 ORecord (com.orientechnologies.orient.core.record.ORecord)7 OMetadataInternal (com.orientechnologies.orient.core.metadata.OMetadataInternal)6 OCommandRequestText (com.orientechnologies.orient.core.command.OCommandRequestText)5 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)5 ODatabaseException (com.orientechnologies.orient.core.exception.ODatabaseException)5