Search in sources :

Example 6 with OIOException

use of com.orientechnologies.common.io.OIOException in project orientdb by orientechnologies.

the class ORemoteConnectionPool method createNetworkConnection.

protected OChannelBinaryAsynchClient createNetworkConnection(String iServerURL, final OContextConfiguration clientConfiguration, Map<String, Object> iAdditionalArg) throws OIOException {
    if (iServerURL == null)
        throw new IllegalArgumentException("server url is null");
    // TRY WITH CURRENT URL IF ANY
    try {
        OLogManager.instance().debug(this, "Trying to connect to the remote host %s...", iServerURL);
        final String serverURL;
        final String databaseName;
        if (iServerURL.startsWith(OEngineRemote.PREFIX))
            iServerURL = iServerURL.substring(OEngineRemote.PREFIX.length());
        int sepPos = iServerURL.indexOf("/");
        if (sepPos > -1) {
            // REMOVE DATABASE NAME IF ANY
            serverURL = iServerURL.substring(0, sepPos);
            databaseName = iServerURL.substring(sepPos + 1);
        } else {
            serverURL = iServerURL;
            databaseName = null;
        }
        sepPos = serverURL.indexOf(":");
        final String remoteHost = serverURL.substring(0, sepPos);
        final int remotePort = Integer.parseInt(serverURL.substring(sepPos + 1));
        final OChannelBinaryAsynchClient ch = new OChannelBinaryAsynchClient(remoteHost, remotePort, databaseName, clientConfiguration, OChannelBinaryProtocol.CURRENT_PROTOCOL_VERSION, listener);
        return ch;
    } catch (OIOException e) {
        // RE-THROW IT
        throw e;
    } catch (Exception e) {
        OLogManager.instance().debug(this, "Error on connecting to %s", e, iServerURL);
        throw OException.wrapException(new OIOException("Error on connecting to " + iServerURL), e);
    }
}
Also used : OChannelBinaryAsynchClient(com.orientechnologies.orient.client.binary.OChannelBinaryAsynchClient) OIOException(com.orientechnologies.common.io.OIOException) OIOException(com.orientechnologies.common.io.OIOException) OException(com.orientechnologies.common.exception.OException)

Example 7 with OIOException

use of com.orientechnologies.common.io.OIOException in project orientdb by orientechnologies.

the class OChannelBinary method writeBytes.

public OChannelBinary writeBytes(final byte[] iContent, final int iLength) throws IOException {
    if (debug)
        OLogManager.instance().info(this, "%s - Writing bytes (4+%d=%d bytes): %s", socket.getRemoteSocketAddress(), iLength, iLength + 4, Arrays.toString(iContent));
    if (iContent == null) {
        out.writeInt(-1);
        updateMetricTransmittedBytes(OBinaryProtocol.SIZE_INT);
    } else {
        if (iLength > maxChunkSize) {
            throw OException.wrapException(new OIOException("Impossible to write a chunk of length:" + iLength + " max allowed chunk length:" + maxChunkSize + " see NETWORK_BINARY_MAX_CONTENT_LENGTH settings "), null);
        }
        out.writeInt(iLength);
        out.write(iContent, 0, iLength);
        updateMetricTransmittedBytes(OBinaryProtocol.SIZE_INT + iLength);
    }
    return this;
}
Also used : OIOException(com.orientechnologies.common.io.OIOException)

Example 8 with OIOException

use of com.orientechnologies.common.io.OIOException in project orientdb by orientechnologies.

the class OConsoleDatabaseApp method listClusters.

@ConsoleCommand(description = "Display all the configured clusters", aliases = { "clusters" }, onlineHelp = "Console-Command-List-Clusters")
public void listClusters(@ConsoleParameter(name = "[options]", optional = true, description = "Additional options, example: -v=verbose") final String options) {
    final Map<String, String> commandOptions = parseCommandOptions(options);
    if (currentDatabaseName != null) {
        message("\n\nCLUSTERS (collections)");
        final List<ODocument> resultSet = new ArrayList<ODocument>();
        int clusterId;
        long totalElements = 0;
        long totalSpaceUsed = 0;
        long totalTombstones = 0;
        long count;
        final List<String> clusters = new ArrayList<String>(currentDatabase.getClusterNames());
        Collections.sort(clusters);
        ODocument dClusters = null;
        final ODocument dCfg = getDistributedConfiguration();
        if (dCfg != null) {
            final ODocument dDatabaseCfg = dCfg.field("database");
            if (dDatabaseCfg != null) {
                dClusters = dDatabaseCfg.field("clusters");
            }
        }
        final boolean isRemote = currentDatabase.getStorage().isRemote();
        for (String clusterName : clusters) {
            try {
                final ODocument row = new ODocument();
                resultSet.add(row);
                clusterId = currentDatabase.getClusterIdByName(clusterName);
                final OCluster cluster = currentDatabase.getStorage().getClusterById(clusterId);
                final String conflictStrategy = cluster.getRecordConflictStrategy() != null ? cluster.getRecordConflictStrategy().getName() : "";
                count = currentDatabase.countClusterElements(clusterName);
                totalElements += count;
                final long spaceUsed = !isRemote ? cluster.getRecordsSize() : 0;
                totalSpaceUsed += spaceUsed;
                final long tombstones = !isRemote ? cluster.getTombstonesCount() : 0;
                totalTombstones += tombstones;
                final OClass cls = currentDatabase.getMetadata().getSchema().getClassByClusterId(clusterId);
                final String className = cls != null ? cls.getName() : null;
                row.field("NAME", clusterName);
                row.field("ID", clusterId);
                row.field("CLASS", className);
                row.field("CONFLICT-STRATEGY", conflictStrategy);
                row.field("COUNT", count);
                if (!isRemote) {
                    row.field("SPACE-USED", OFileUtils.getSizeAsString(spaceUsed));
                    if (commandOptions.containsKey("-v")) {
                        row.field("TOMBSTONES", tombstones);
                    }
                }
                if (dClusters != null) {
                    ODocument dClusterCfg = dClusters.field(clusterName);
                    if (dClusterCfg == null)
                        dClusterCfg = dClusters.field("*");
                    if (dClusterCfg != null) {
                        final List<String> servers = new ArrayList<String>((Collection<? extends String>) dClusterCfg.field("servers"));
                        final boolean newNode = servers.remove("<NEW_NODE>");
                        if (!servers.isEmpty()) {
                            row.field("OWNER_SERVER", servers.get(0));
                            if (servers.size() > 1) {
                                servers.remove(0);
                                row.field("OTHER_SERVERS", servers);
                            }
                        }
                        row.field("AUTO_DEPLOY_NEW_NODE", newNode);
                    }
                }
            } catch (Exception e) {
                if (e instanceof OIOException)
                    break;
            }
        }
        final OTableFormatter formatter = new OTableFormatter(this);
        formatter.setColumnAlignment("ID", OTableFormatter.ALIGNMENT.RIGHT);
        formatter.setColumnAlignment("COUNT", OTableFormatter.ALIGNMENT.RIGHT);
        formatter.setColumnAlignment("OWNER_SERVER", OTableFormatter.ALIGNMENT.CENTER);
        formatter.setColumnAlignment("OTHER_SERVERS", OTableFormatter.ALIGNMENT.CENTER);
        formatter.setColumnAlignment("AUTO_DEPLOY_NEW_NODE", OTableFormatter.ALIGNMENT.CENTER);
        if (!isRemote) {
            formatter.setColumnAlignment("SPACE-USED", OTableFormatter.ALIGNMENT.RIGHT);
            if (commandOptions.containsKey("-v")) {
                formatter.setColumnAlignment("TOMBSTONES", OTableFormatter.ALIGNMENT.RIGHT);
            }
        }
        final ODocument footer = new ODocument();
        footer.field("NAME", "TOTAL");
        footer.field("COUNT", totalElements);
        if (!isRemote) {
            footer.field("SPACE-USED", OFileUtils.getSizeAsString(totalSpaceUsed));
            if (commandOptions.containsKey("-v")) {
                footer.field("TOMBSTONES", totalTombstones);
            }
        }
        formatter.setFooter(footer);
        formatter.writeRecords(resultSet, -1);
        message("\n");
    } else
        message("\nNo database selected yet.");
}
Also used : OSystemException(com.orientechnologies.common.exception.OSystemException) OConfigurationException(com.orientechnologies.orient.core.exception.OConfigurationException) ORetryQueryException(com.orientechnologies.orient.core.exception.ORetryQueryException) OIOException(com.orientechnologies.common.io.OIOException) ODatabaseException(com.orientechnologies.orient.core.exception.ODatabaseException) OIOException(com.orientechnologies.common.io.OIOException) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) OCluster(com.orientechnologies.orient.core.storage.OCluster) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) ConsoleCommand(com.orientechnologies.common.console.annotation.ConsoleCommand)

Example 9 with OIOException

use of com.orientechnologies.common.io.OIOException in project orientdb by orientechnologies.

the class OStorageRemote method baseNetworkOperation.

public <T> T baseNetworkOperation(final OStorageRemoteOperation<T> operation, final String errorMessage, int retry) {
    OStorageRemoteSession session = getCurrentSession();
    if (session.commandExecuting)
        throw new ODatabaseException("Cannot execute the request because an asynchronous operation is in progress. Please use a different connection");
    String serverUrl = null;
    do {
        OChannelBinaryAsynchClient network = null;
        if (serverUrl == null)
            serverUrl = getNextAvailableServerURL(false, session);
        do {
            try {
                network = getNetwork(serverUrl);
            } catch (OException e) {
                serverUrl = useNewServerURL(serverUrl);
                if (serverUrl == null)
                    throw e;
            }
        } while (network == null);
        try {
            // In case i do not have a token or i'm switching between server i've to execute a open operation.
            OStorageRemoteNodeSession nodeSession = session.getServerSession(network.getServerURL());
            if (nodeSession == null || !nodeSession.isValid()) {
                openRemoteDatabase(network);
                if (!network.tryLock()) {
                    connectionManager.release(network);
                    continue;
                }
            }
            return operation.execute(network, session);
        } catch (ODistributedRedirectException e) {
            connectionManager.release(network);
            OLogManager.instance().debug(this, "Redirecting the request from server '%s' to the server '%s' because %s", e.getFromServer(), e.toString(), e.getMessage());
            // RECONNECT TO THE SERVER SUGGESTED IN THE EXCEPTION
            serverUrl = e.getToServerAddress();
        } catch (OModificationOperationProhibitedException mope) {
            connectionManager.release(network);
            handleDBFreeze();
            serverUrl = null;
        } catch (OTokenException e) {
            connectionManager.release(network);
            session.removeServerSession(network.getServerURL());
            if (--retry <= 0)
                throw OException.wrapException(new OStorageException(errorMessage), e);
            serverUrl = null;
        } catch (OTokenSecurityException e) {
            connectionManager.release(network);
            session.removeServerSession(network.getServerURL());
            if (--retry <= 0)
                throw OException.wrapException(new OStorageException(errorMessage), e);
            serverUrl = null;
        } catch (OOfflineNodeException e) {
            connectionManager.release(network);
            // Remove the current url because the node is offline
            synchronized (serverURLs) {
                serverURLs.remove(serverUrl);
            }
            for (OStorageRemoteSession activeSession : sessions) {
                // Not thread Safe ...
                activeSession.removeServerSession(serverUrl);
            }
            serverUrl = null;
        } catch (IOException e) {
            connectionManager.release(network);
            retry = handleIOException(retry, network, e);
            serverUrl = null;
        } catch (OIOException e) {
            connectionManager.release(network);
            retry = handleIOException(retry, network, e);
            serverUrl = null;
        } catch (OException e) {
            connectionManager.release(network);
            throw e;
        } catch (Exception e) {
            connectionManager.release(network);
            throw OException.wrapException(new OStorageException(errorMessage), e);
        }
    } while (true);
}
Also used : OOfflineNodeException(com.orientechnologies.common.concur.OOfflineNodeException) OException(com.orientechnologies.common.exception.OException) OIOException(com.orientechnologies.common.io.OIOException) IOException(java.io.IOException) OIOException(com.orientechnologies.common.io.OIOException) IOException(java.io.IOException) OChannelBinaryAsynchClient(com.orientechnologies.orient.client.binary.OChannelBinaryAsynchClient) OIOException(com.orientechnologies.common.io.OIOException) OException(com.orientechnologies.common.exception.OException) NamingException(javax.naming.NamingException) OTokenException(com.orientechnologies.orient.core.metadata.security.OTokenException) ODistributedRedirectException(com.orientechnologies.orient.enterprise.channel.binary.ODistributedRedirectException) OInterruptedException(com.orientechnologies.common.concur.lock.OInterruptedException) OTokenSecurityException(com.orientechnologies.orient.enterprise.channel.binary.OTokenSecurityException) OIOException(com.orientechnologies.common.io.OIOException) IOException(java.io.IOException) OOfflineNodeException(com.orientechnologies.common.concur.OOfflineNodeException) OModificationOperationProhibitedException(com.orientechnologies.common.concur.lock.OModificationOperationProhibitedException) OTokenSecurityException(com.orientechnologies.orient.enterprise.channel.binary.OTokenSecurityException) OTokenException(com.orientechnologies.orient.core.metadata.security.OTokenException) ODistributedRedirectException(com.orientechnologies.orient.enterprise.channel.binary.ODistributedRedirectException) OModificationOperationProhibitedException(com.orientechnologies.common.concur.lock.OModificationOperationProhibitedException)

Example 10 with OIOException

use of com.orientechnologies.common.io.OIOException in project orientdb by orientechnologies.

the class ODatabaseExport method exportRecords.

public long exportRecords() throws IOException {
    long totalFoundRecords = 0;
    long totalExportedRecords = 0;
    int level = 1;
    listener.onMessage("\nExporting records...");
    writer.beginCollection(level, true, "records");
    int exportedClusters = 0;
    int maxClusterId = getMaxClusterId();
    for (int i = 0; exportedClusters <= maxClusterId; ++i) {
        String clusterName = database.getClusterNameById(i);
        exportedClusters++;
        long clusterExportedRecordsTot = 0;
        if (clusterName != null) {
            // CHECK IF THE CLUSTER IS INCLUDED
            if (includeClusters != null) {
                if (!includeClusters.contains(clusterName.toUpperCase()))
                    continue;
            } else if (excludeClusters != null) {
                if (excludeClusters.contains(clusterName.toUpperCase()))
                    continue;
            }
            if (excludeClusters != null && excludeClusters.contains(clusterName.toUpperCase()))
                continue;
            clusterExportedRecordsTot = database.countClusterElements(clusterName);
        } else if (includeClusters != null && !includeClusters.isEmpty())
            continue;
        listener.onMessage("\n- Cluster " + (clusterName != null ? "'" + clusterName + "'" : "NULL") + " (id=" + i + ")...");
        long clusterExportedRecordsCurrent = 0;
        if (clusterName != null) {
            ORecord rec = null;
            try {
                for (ORecordIteratorCluster<ORecord> it = database.browseCluster(clusterName); it.hasNext(); ) {
                    rec = it.next();
                    if (rec instanceof ODocument) {
                        // CHECK IF THE CLASS OF THE DOCUMENT IS INCLUDED
                        ODocument doc = (ODocument) rec;
                        final String className = doc.getClassName() != null ? doc.getClassName().toUpperCase() : null;
                        if (includeClasses != null) {
                            if (!includeClasses.contains(className))
                                continue;
                        } else if (excludeClasses != null) {
                            if (excludeClasses.contains(className))
                                continue;
                        }
                    } else if (includeClasses != null && !includeClasses.isEmpty())
                        continue;
                    if (exportRecord(clusterExportedRecordsTot, clusterExportedRecordsCurrent, rec))
                        clusterExportedRecordsCurrent++;
                }
            } catch (IOException e) {
                OLogManager.instance().error(this, "\nError on exporting record %s because of I/O problems", e, rec.getIdentity());
                // RE-THROW THE EXCEPTION UP
                throw e;
            } catch (OIOException e) {
                OLogManager.instance().error(this, "\nError on exporting record %s because of I/O problems", e, rec == null ? null : rec.getIdentity());
                // RE-THROW THE EXCEPTION UP
                throw e;
            } catch (Throwable t) {
                if (rec != null) {
                    final byte[] buffer = rec.toStream();
                    OLogManager.instance().error(this, "\nError on exporting record %s. It seems corrupted; size: %d bytes, raw content (as string):\n==========\n%s\n==========", t, rec.getIdentity(), buffer.length, new String(buffer));
                }
            }
        }
        listener.onMessage("OK (records=" + clusterExportedRecordsCurrent + "/" + clusterExportedRecordsTot + ")");
        totalExportedRecords += clusterExportedRecordsCurrent;
        totalFoundRecords += clusterExportedRecordsTot;
    }
    writer.endCollection(level, true);
    listener.onMessage("\n\nDone. Exported " + totalExportedRecords + " of total " + totalFoundRecords + " records\n");
    return totalExportedRecords;
}
Also used : ORecord(com.orientechnologies.orient.core.record.ORecord) OIOException(com.orientechnologies.common.io.OIOException) OIOException(com.orientechnologies.common.io.OIOException) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Aggregations

OIOException (com.orientechnologies.common.io.OIOException)13 OChannelBinaryAsynchClient (com.orientechnologies.orient.client.binary.OChannelBinaryAsynchClient)6 IOException (java.io.IOException)6 OException (com.orientechnologies.common.exception.OException)5 OOfflineNodeException (com.orientechnologies.common.concur.OOfflineNodeException)4 OInterruptedException (com.orientechnologies.common.concur.lock.OInterruptedException)4 OModificationOperationProhibitedException (com.orientechnologies.common.concur.lock.OModificationOperationProhibitedException)4 OTokenException (com.orientechnologies.orient.core.metadata.security.OTokenException)4 ODistributedRedirectException (com.orientechnologies.orient.enterprise.channel.binary.ODistributedRedirectException)4 OTokenSecurityException (com.orientechnologies.orient.enterprise.channel.binary.OTokenSecurityException)4 NamingException (javax.naming.NamingException)4 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)2 ConsoleCommand (com.orientechnologies.common.console.annotation.ConsoleCommand)1 OSystemException (com.orientechnologies.common.exception.OSystemException)1 OCallable (com.orientechnologies.common.util.OCallable)1 OStorageRemoteNodeSession (com.orientechnologies.orient.client.remote.OStorageRemoteNodeSession)1 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)1 OConfigurationException (com.orientechnologies.orient.core.exception.OConfigurationException)1 ODatabaseException (com.orientechnologies.orient.core.exception.ODatabaseException)1 ORetryQueryException (com.orientechnologies.orient.core.exception.ORetryQueryException)1