protected <T> T networkAdminOperation( final OStorageRemoteOperation<T> operation, final String errorMessage) { OChannelBinaryAsynchClient network = null; try { // TODO:replace this api with one that get connection for only the specified url. String serverUrl = storage.getNextAvailableServerURL(false, session); do { try { network = storage.getNetwork(serverUrl); } catch (OException e) { serverUrl = storage.useNewServerURL(serverUrl); if (serverUrl == null) throw e; } } while (network == null); T res = operation.execute(network, storage.getCurrentSession()); storage.connectionManager.release(network); return res; } catch (Exception e) { if (network != null) storage.connectionManager.release(network); storage.close(true, false); throw OException.wrapException(new OStorageException(errorMessage), e); } }
/** * Drops a database from a remote server instance. * * @param iDatabaseName The database name * @param storageType Storage type between "plocal" or "memory". * @return The instance itself. Useful to execute method in chain * @throws IOException */ public synchronized OServerAdmin dropDatabase( final String iDatabaseName, final String storageType) throws IOException { boolean retry = true; while (retry) { retry = networkAdminOperation( new OStorageRemoteOperation<Boolean>() { @Override public Boolean execute( final OChannelBinaryAsynchClient network, OStorageRemoteSession session) throws IOException { try { try { storage.beginRequest( network, OChannelBinaryProtocol.REQUEST_DB_DROP, session); network.writeString(iDatabaseName); network.writeString(storageType); } finally { storage.endRequest(network); } storage.getResponse(network, session); return false; } catch (OModificationOperationProhibitedException oope) { return handleDBFreeze(); } } }, "Cannot delete the remote storage: " + storage.getName()); } final Set<OStorage> underlyingStorages = new HashSet<OStorage>(); for (OStorage s : Orient.instance().getStorages()) { if (s.getType().equals(storage.getType()) && s.getName().equals(storage.getName())) { underlyingStorages.add(s.getUnderlying()); } } for (OStorage s : underlyingStorages) { s.close(true, true); } ODatabaseRecordThreadLocal.INSTANCE.remove(); return this; }
protected ODocument sendRequest( final byte iRequest, final ODocument iPayLoad, final String iActivity) { // Using here networkOperation because the original retry logic was lik networkOperation return storage.networkOperation( new OStorageRemoteOperation<ODocument>() { @Override public ODocument execute( OChannelBinaryAsynchClient network, OStorageRemoteSession session) throws IOException { try { storage.beginRequest(network, iRequest, session); network.writeBytes(iPayLoad.toStream()); } finally { storage.endRequest(network); } try { storage.beginResponse(network, session); return new ODocument(network.readBytes()); } finally { storage.endResponse(network); } } }, "Error on executing '" + iActivity + "'"); }
/** * Releases a frozen cluster. * * @param clusterId Id of cluster to freeze * @param storageType Storage type between "plocal" or "memory". * @return * @throws IOException * @see #freezeCluster(int, String) */ public synchronized OServerAdmin releaseCluster(final int clusterId, final String storageType) throws IOException { networkAdminOperation( new OStorageRemoteOperation<Void>() { @Override public Void execute( final OChannelBinaryAsynchClient network, OStorageRemoteSession session) throws IOException { try { storage.beginRequest( network, OChannelBinaryProtocol.REQUEST_DATACLUSTER_RELEASE, session); network.writeString(storage.getName()); network.writeShort((short) clusterId); network.writeString(storageType); } finally { storage.endRequest(network); } storage.getResponse(network, session); return null; } }, "Cannot release the remote cluster " + clusterId + " on storage: " + storage.getName()); return this; }
/** * Checks if a database exists in the remote server. * * @param iDatabaseName The database name * @param storageType Storage type between "plocal" or "memory". * @return true if exists, otherwise false * @throws IOException */ public synchronized boolean existsDatabase(final String iDatabaseName, final String storageType) throws IOException { return networkAdminOperation( new OStorageRemoteOperation<Boolean>() { @Override public Boolean execute( final OChannelBinaryAsynchClient network, OStorageRemoteSession session) throws IOException { try { storage.beginRequest(network, OChannelBinaryProtocol.REQUEST_DB_EXIST, session); network.writeString(iDatabaseName); network.writeString(storageType); } finally { storage.endRequest(network); } try { storage.beginResponse(network, session); return network.readByte() == 1; } finally { storage.endResponse(network); } } }, "Error on checking existence of the remote storage: " + storage.getName()); }
/** * Connects to a remote server. * * @param iUserName Server's user name * @param iUserPassword Server's password for the user name used * @return The instance itself. Useful to execute method in chain * @throws IOException */ public synchronized OServerAdmin connect(final String iUserName, final String iUserPassword) throws IOException { networkAdminOperation( new OStorageRemoteOperation<Void>() { @Override public Void execute(OChannelBinaryAsynchClient network, OStorageRemoteSession session) throws IOException { OStorageRemoteNodeSession nodeSession = storage.getCurrentSession().getOrCreate(network.getServerURL()); try { storage.beginRequest(network, OChannelBinaryProtocol.REQUEST_CONNECT, session); storage.sendClientInfo(network, clientType, false, collectStats); String username = iUserName; String password = iUserPassword; OCredentialInterceptor ci = OSecurityManager.instance().newCredentialInterceptor(); if (ci != null) { ci.intercept(storage.getURL(), iUserName, iUserPassword); username = ci.getUsername(); password = ci.getPassword(); } network.writeString(username); network.writeString(password); } finally { storage.endRequest(network); } try { network.beginResponse(nodeSession.getSessionId(), false); int sessionId = network.readInt(); byte[] sessionToken = network.readBytes(); if (sessionToken.length == 0) { sessionToken = null; } nodeSession.setSession(sessionId, sessionToken); } finally { storage.endResponse(network); } return null; } }, "Cannot connect to the remote server/database '" + storage.getURL() + "'"); return this; }
/** * Creates a database in a remote server. * * @param iDatabaseName The database name * @param iDatabaseType 'document' or 'graph' * @param iStorageMode local or memory * @param backupPath path to incremental backup which will be used to create database (optional) * @return The instance itself. Useful to execute method in chain * @throws IOException */ public synchronized OServerAdmin createDatabase( final String iDatabaseName, final String iDatabaseType, final String iStorageMode, final String backupPath) throws IOException { if (iDatabaseName == null || iDatabaseName.length() <= 0) { final String message = "Cannot create unnamed remote storage. Check your syntax"; OLogManager.instance().error(this, message); throw new OStorageException(message); } else { networkAdminOperation( new OStorageRemoteOperation<Void>() { @Override public Void execute( final OChannelBinaryAsynchClient network, OStorageRemoteSession session) throws IOException { String storageMode; if (iStorageMode == null) storageMode = "csv"; else storageMode = iStorageMode; try { storage.beginRequest(network, OChannelBinaryProtocol.REQUEST_DB_CREATE, session); network.writeString(iDatabaseName); if (network.getSrvProtocolVersion() >= 8) network.writeString(iDatabaseType); network.writeString(storageMode); if (network.getSrvProtocolVersion() > 35) network.writeString(backupPath); } finally { storage.endRequest(network); } storage.getResponse(network, session); return null; } }, "Cannot create the remote storage: " + storage.getName()); } return this; }
/** * Freezes the database by locking it in exclusive mode. * * @param storageType Storage type between "plocal" or "memory". * @return * @throws IOException * @see #releaseDatabase(String) */ public synchronized OServerAdmin freezeDatabase(final String storageType) throws IOException { networkAdminOperation( new OStorageRemoteOperation<Void>() { @Override public Void execute(OChannelBinaryAsynchClient network, OStorageRemoteSession session) throws IOException { try { storage.beginRequest(network, OChannelBinaryProtocol.REQUEST_DB_FREEZE, session); network.writeString(storage.getName()); network.writeString(storageType); } finally { storage.endRequest(network); } storage.getResponse(network, session); return null; } }, "Cannot freeze the remote storage: " + storage.getName()); return this; }
public boolean isConnected() { return storage != null && !storage.isClosed(); }
public synchronized String getURL() { return storage != null ? storage.getURL() : null; }
public synchronized void close(boolean iForce) { storage.close(iForce, false); }
/** Close the connection if open. */ public synchronized void close() { storage.close(); }
/** * Drops a database from a remote server instance. * * @param storageType Storage type between "plocal" or "memory". * @return The instance itself. Useful to execute method in chain * @throws IOException */ public synchronized OServerAdmin dropDatabase(final String storageType) throws IOException { return dropDatabase(storage.getName(), storageType); }
/** * Checks if a database exists in the remote server. * * @param storageType Storage type between "plocal" or "memory". * @return true if exists, otherwise false * @throws IOException */ public synchronized boolean existsDatabase(final String storageType) throws IOException { return existsDatabase(storage.getName(), storageType); }
public synchronized String getStorageName() { return storage.getName(); }
/** * Creates a database in a remote server. * * @param iDatabaseType 'document' or 'graph' * @param iStorageMode local or memory * @return The instance itself. Useful to execute method in chain * @throws IOException */ public synchronized OServerAdmin createDatabase(final String iDatabaseType, String iStorageMode) throws IOException { return createDatabase(storage.getName(), iDatabaseType, iStorageMode); }