Search in sources :

Example 6 with OLockException

use of com.orientechnologies.common.concur.lock.OLockException in project orientdb by orientechnologies.

the class ODistributedLockManagerRequester method releaseExclusiveLock.

@Override
public void releaseExclusiveLock(final String resource, final String nodeSource) {
    if (coordinatorServer == null || coordinatorServer.equals(manager.getLocalNodeName())) {
        // THE COORDINATOR IS THE LOCAL SERVER, RELEASE IT LOCALLY
        manager.getLockManagerExecutor().releaseExclusiveLock(resource, manager.getLocalNodeName());
    } else {
        // RELEASE THE LOCK INTO THE COORDINATOR SERVER
        ODistributedServerLog.debug(this, manager.getLocalNodeName(), coordinatorServer, ODistributedServerLog.DIRECTION.OUT, "Releasing distributed lock on resource '%s'", resource);
        final Set<String> servers = new HashSet<String>();
        servers.add(coordinatorServer);
        final ODistributedResponse dResponse = manager.sendRequest(OSystemDatabase.SYSTEM_DB_NAME, null, servers, new ODistributedLockTask(resource, 0, false), manager.getNextMessageIdCounter(), ODistributedRequest.EXECUTION_MODE.RESPONSE, null, null);
        if (dResponse == null)
            throw new OLockException("Cannot release exclusive lock on resource '" + resource + "'");
        final Object result = dResponse.getPayload();
        if (result instanceof RuntimeException)
            throw (RuntimeException) result;
    }
    ODistributedServerLog.debug(this, manager.getLocalNodeName(), coordinatorServer, ODistributedServerLog.DIRECTION.OUT, "Released distributed lock on resource '%s'", resource);
}
Also used : ODistributedLockTask(com.orientechnologies.orient.server.distributed.impl.task.ODistributedLockTask) OLockException(com.orientechnologies.common.concur.lock.OLockException) HashSet(java.util.HashSet)

Example 7 with OLockException

use of com.orientechnologies.common.concur.lock.OLockException in project orientdb by orientechnologies.

the class ODistributedLockManagerRequester method acquireExclusiveLock.

@Override
public void acquireExclusiveLock(final String resource, final String nodeSource, final long timeout) {
    while (true) {
        if (coordinatorServer == null || coordinatorServer.equals(manager.getLocalNodeName())) {
            // NO MASTERS, USE LOCAL SERVER
            manager.getLockManagerExecutor().acquireExclusiveLock(resource, manager.getLocalNodeName(), timeout);
            break;
        } else {
            // SEND A DISTRIBUTED MSG TO THE COORDINATOR SERVER
            final Set<String> servers = new HashSet<String>();
            servers.add(coordinatorServer);
            ODistributedServerLog.debug(this, manager.getLocalNodeName(), coordinatorServer, ODistributedServerLog.DIRECTION.OUT, "Server '%s' is acquiring distributed lock on resource '%s'...", nodeSource, resource);
            final ODistributedResponse dResponse = manager.sendRequest(OSystemDatabase.SYSTEM_DB_NAME, null, servers, new ODistributedLockTask(resource, timeout, true), manager.getNextMessageIdCounter(), ODistributedRequest.EXECUTION_MODE.RESPONSE, null, null);
            if (dResponse == null) {
                ODistributedServerLog.warn(this, manager.getLocalNodeName(), coordinatorServer, ODistributedServerLog.DIRECTION.OUT, "Server '%s' cannot acquire distributed lock on resource '%s' (timeout=%d)...", nodeSource, resource, timeout);
                throw new OLockException("Server '" + nodeSource + "' cannot acquire exclusive lock on resource '" + resource + "' (timeout=" + timeout + ")");
            }
            final Object result = dResponse.getPayload();
            if (result instanceof ODistributedOperationException) {
                if (manager.getActiveServers().contains(coordinatorServer))
                    // WAIT ONLY IN THE CASE THE COORDINATOR IS STILL ONLINE
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                    // IGNORE IT
                    }
                if (!manager.getActiveServers().contains(coordinatorServer)) {
                    // THE COORDINATOR WAS DOWN DURING THE REQUEST, RETRY WITH ANOTHER COORDINATOR
                    coordinatorServer = manager.getCoordinatorServer();
                    continue;
                }
            } else if (result instanceof RuntimeException)
                throw (RuntimeException) result;
            break;
        }
    }
    ODistributedServerLog.debug(this, manager.getLocalNodeName(), coordinatorServer, ODistributedServerLog.DIRECTION.OUT, "Server '%s' has acquired distributed lock on resource '%s'", nodeSource, resource);
    acquiredResources.put(resource, System.currentTimeMillis());
}
Also used : ODistributedLockTask(com.orientechnologies.orient.server.distributed.impl.task.ODistributedLockTask) OLockException(com.orientechnologies.common.concur.lock.OLockException) ODistributedOperationException(com.orientechnologies.orient.server.distributed.task.ODistributedOperationException) HashSet(java.util.HashSet)

Example 8 with OLockException

use of com.orientechnologies.common.concur.lock.OLockException in project orientdb by orientechnologies.

the class OServerCommandAuthenticatedDbAbstract method authenticate.

protected boolean authenticate(final OHttpRequest iRequest, final OHttpResponse iResponse, final List<String> iAuthenticationParts, final String iDatabaseName) throws IOException {
    ODatabaseDocument db = null;
    try {
        db = (ODatabaseDocument) server.openDatabase(iDatabaseName, iAuthenticationParts.get(0), iAuthenticationParts.get(1));
        // if (db.getUser() == null)
        // // MAYBE A PREVIOUS ROOT REALM? UN AUTHORIZE
        // return false;
        // Set user rid after authentication
        iRequest.data.currentUserId = db.getUser() == null ? "<server user>" : db.getUser().getIdentity().toString();
        // AUTHENTICATED: CREATE THE SESSION
        iRequest.sessionId = OHttpSessionManager.getInstance().createSession(iDatabaseName, iAuthenticationParts.get(0), iAuthenticationParts.get(1));
        iResponse.sessionId = iRequest.sessionId;
        return true;
    } catch (OSecurityAccessException e) {
    // WRONG USER/PASSWD
    } catch (OLockException e) {
        OLogManager.instance().error(this, "Cannot access to the database '" + iDatabaseName + "'", ODatabaseException.class, e);
    } finally {
        if (db == null) {
            // WRONG USER/PASSWD
            sendAuthorizationRequest(iRequest, iResponse, iDatabaseName);
        } else {
            db.close();
        }
    }
    return false;
}
Also used : OSecurityAccessException(com.orientechnologies.orient.core.exception.OSecurityAccessException) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OLockException(com.orientechnologies.common.concur.lock.OLockException) ODatabaseException(com.orientechnologies.orient.core.exception.ODatabaseException)

Example 9 with OLockException

use of com.orientechnologies.common.concur.lock.OLockException in project orientdb by orientechnologies.

the class ODistributedLockManagerExecutor method acquireExclusiveLock.

public void acquireExclusiveLock(final String resource, final String nodeSource, final long timeout) {
    final ODistributedLock lock = new ODistributedLock(nodeSource);
    ODistributedLock currentLock = lockManager.putIfAbsent(resource, lock);
    if (currentLock != null) {
        if (currentLock.server.equals(nodeSource)) {
            // SAME RESOURCE/SERVER, ALREADY LOCKED
            currentLock = null;
        } else {
            // TRY TO RE-LOCK IT UNTIL TIMEOUT IS EXPIRED
            final long startTime = System.currentTimeMillis();
            do {
                try {
                    ODistributedServerLog.info(this, manager.getLocalNodeName(), nodeSource, ODistributedServerLog.DIRECTION.IN, "Waiting to acquire distributed lock on resource '%s' (timeout=%d)...", resource, timeout);
                    if (timeout > 0) {
                        if (!currentLock.lock.await(timeout, TimeUnit.MILLISECONDS))
                            continue;
                    } else
                        currentLock.lock.await();
                    currentLock = lockManager.putIfAbsent(resource, lock);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    break;
                }
            } while (currentLock != null && (timeout == 0 || System.currentTimeMillis() - startTime < timeout));
        }
    }
    if (currentLock != null) {
        // CHECK THE OWNER SERVER IS ONLINE. THIS AVOIDS ANY "WALKING DEAD" LOCKS
        if (currentLock.server == null || !manager.isNodeAvailable(currentLock.server)) {
            ODistributedServerLog.info(this, manager.getLocalNodeName(), null, DIRECTION.NONE, "Forcing unlock of resource '%s' because the owner server '%s' is offline", resource, currentLock.server);
            // FORCE THE UNLOCK AND LOCK OF CURRENT REQ-ID
            lockManager.put(resource, lock);
            currentLock = null;
        }
    }
    if (ODistributedServerLog.isDebugEnabled())
        if (currentLock == null) {
            ODistributedServerLog.debug(this, manager.getLocalNodeName(), nodeSource, DIRECTION.IN, "Resource '%s' locked by server '%s'", resource, nodeSource);
        } else {
            ODistributedServerLog.debug(this, manager.getLocalNodeName(), nodeSource, DIRECTION.IN, "Cannot lock resource '%s' owned by server '%s' (timeout=%d)", resource, nodeSource, timeout);
        }
    if (currentLock != null)
        throw new OLockException("Cannot lock resource '" + resource + "' owned by server '" + nodeSource + "' (timeout=" + timeout + ")");
}
Also used : OLockException(com.orientechnologies.common.concur.lock.OLockException)

Example 10 with OLockException

use of com.orientechnologies.common.concur.lock.OLockException in project orientdb by orientechnologies.

the class OSyncClusterTask method execute.

@Override
public Object execute(ODistributedRequestId requestId, final OServer iServer, final ODistributedServerManager iManager, final ODatabaseDocumentInternal database) throws Exception {
    if (getNodeSource() == null || !getNodeSource().equals(iManager.getLocalNodeName())) {
        if (database == null)
            throw new ODistributedException("Database instance is null");
        final String databaseName = database.getName();
        try {
            final Long lastDeployment = (Long) iManager.getConfigurationMap().get(DEPLOYCLUSTER + databaseName + "." + clusterName);
            if (lastDeployment != null && lastDeployment.longValue() == random) {
                // SKIP IT
                ODistributedServerLog.debug(this, iManager.getLocalNodeName(), getNodeSource(), DIRECTION.NONE, "Skip deploying cluster '%s' because already executed", clusterName);
                return Boolean.FALSE;
            }
            iManager.getConfigurationMap().put(DEPLOYCLUSTER + databaseName + "." + clusterName, random);
            ODistributedServerLog.info(this, iManager.getLocalNodeName(), getNodeSource(), DIRECTION.OUT, "deploying cluster %s...", databaseName);
            final File backupFile = new File(Orient.getTempPath() + "/backup_" + databaseName + "_" + clusterName + ".zip");
            if (backupFile.exists())
                backupFile.delete();
            else
                backupFile.getParentFile().mkdirs();
            backupFile.createNewFile();
            ODistributedServerLog.info(this, iManager.getLocalNodeName(), getNodeSource(), DIRECTION.OUT, "Creating backup of cluster '%s' in directory: %s...", databaseName, backupFile.getAbsolutePath());
            final OPaginatedCluster cluster = (OPaginatedCluster) database.getStorage().getClusterByName(clusterName);
            switch(mode) {
                case MERGE:
                    throw new IllegalArgumentException("Merge mode not supported");
                case FULL_REPLACE:
                    final FileOutputStream fileOutputStream = new FileOutputStream(backupFile);
                    final File completedFile = new File(backupFile.getAbsolutePath() + ".completed");
                    if (completedFile.exists())
                        completedFile.delete();
                    new Thread(new Runnable() {

                        @Override
                        public void run() {
                            Thread.currentThread().setName("OrientDB SyncCluster node=" + iManager.getLocalNodeName() + " db=" + databaseName + " cluster=" + clusterName);
                            try {
                                database.activateOnCurrentThread();
                                database.freeze();
                                try {
                                    final String fileName = cluster.getFileName();
                                    final String dbPath = iServer.getDatabaseDirectory() + databaseName;
                                    final ArrayList<String> fileNames = new ArrayList<String>();
                                    // COPY PCL AND CPM FILE
                                    fileNames.add(fileName);
                                    fileNames.add(fileName.substring(0, fileName.length() - 4) + OClusterPositionMap.DEF_EXTENSION);
                                    final OClass clazz = database.getMetadata().getSchema().getClassByClusterId(cluster.getId());
                                    if (clazz != null) {
                                        // CHECK FOR AUTO-SHARDED INDEXES
                                        final OIndex<?> asIndex = clazz.getAutoShardingIndex();
                                        if (asIndex != null) {
                                            final int partition = OCollections.indexOf(clazz.getClusterIds(), cluster.getId());
                                            final String indexName = asIndex.getName();
                                            fileNames.add(indexName + "_" + partition + OAutoShardingIndexEngine.SUBINDEX_METADATA_FILE_EXTENSION);
                                            fileNames.add(indexName + "_" + partition + OAutoShardingIndexEngine.SUBINDEX_TREE_FILE_EXTENSION);
                                            fileNames.add(indexName + "_" + partition + OAutoShardingIndexEngine.SUBINDEX_BUCKET_FILE_EXTENSION);
                                            fileNames.add(indexName + "_" + partition + OAutoShardingIndexEngine.SUBINDEX_NULL_BUCKET_FILE_EXTENSION);
                                        }
                                    }
                                    OZIPCompressionUtil.compressFiles(dbPath, fileNames.toArray(new String[fileNames.size()]), fileOutputStream, null, OGlobalConfiguration.DISTRIBUTED_DEPLOYDB_TASK_COMPRESSION.getValueAsInteger());
                                } catch (IOException e) {
                                    OLogManager.instance().error(this, "Cannot execute backup of cluster '%s.%s' for deploy cluster", e, databaseName, clusterName);
                                } finally {
                                    database.release();
                                }
                            } finally {
                                try {
                                    fileOutputStream.close();
                                } catch (IOException e) {
                                }
                                try {
                                    completedFile.createNewFile();
                                } catch (IOException e) {
                                    OLogManager.instance().error(this, "Cannot create file of backup completed: %s", e, completedFile);
                                }
                            }
                        }
                    }).start();
                    // TODO: SUPPORT BACKUP ON CLUSTER
                    final long fileSize = backupFile.length();
                    ODistributedServerLog.info(this, iManager.getLocalNodeName(), getNodeSource(), DIRECTION.OUT, "Sending the compressed cluster '%s.%s' over the NETWORK to node '%s', size=%s...", databaseName, clusterName, getNodeSource(), OFileUtils.getSizeAsString(fileSize));
                    final ODistributedDatabaseChunk chunk = new ODistributedDatabaseChunk(backupFile, 0, CHUNK_MAX_SIZE, null, false);
                    ODistributedServerLog.info(this, iManager.getLocalNodeName(), getNodeSource(), DIRECTION.OUT, "- transferring chunk #%d offset=%d size=%s...", 1, 0, OFileUtils.getSizeAsNumber(chunk.buffer.length));
                    return chunk;
            }
        } catch (OLockException e) {
            ODistributedServerLog.debug(this, iManager.getLocalNodeName(), getNodeSource(), DIRECTION.NONE, "Skip deploying cluster %s.%s because another node is doing it", databaseName, clusterName);
        } finally {
            ODistributedServerLog.info(this, iManager.getLocalNodeName(), getNodeSource(), ODistributedServerLog.DIRECTION.OUT, "Deploy cluster %s task completed", clusterName);
        }
    } else
        ODistributedServerLog.debug(this, iManager.getLocalNodeName(), getNodeSource(), DIRECTION.NONE, "Skip deploying cluster %s.%s from the same node");
    return Boolean.FALSE;
}
Also used : ArrayList(java.util.ArrayList) OPaginatedCluster(com.orientechnologies.orient.core.storage.impl.local.paginated.OPaginatedCluster) OLockException(com.orientechnologies.common.concur.lock.OLockException) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ODistributedDatabaseChunk(com.orientechnologies.orient.server.distributed.impl.ODistributedDatabaseChunk)

Aggregations

OLockException (com.orientechnologies.common.concur.lock.OLockException)12 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)3 OSecurityAccessException (com.orientechnologies.orient.core.exception.OSecurityAccessException)3 ODatabaseException (com.orientechnologies.orient.core.exception.ODatabaseException)2 ORID (com.orientechnologies.orient.core.id.ORID)2 OStorage (com.orientechnologies.orient.core.storage.OStorage)2 OAbstractPaginatedStorage (com.orientechnologies.orient.core.storage.impl.local.OAbstractPaginatedStorage)2 ODistributedDatabaseChunk (com.orientechnologies.orient.server.distributed.impl.ODistributedDatabaseChunk)2 ODistributedLockTask (com.orientechnologies.orient.server.distributed.impl.task.ODistributedLockTask)2 HashSet (java.util.HashSet)2 OInterruptedException (com.orientechnologies.common.concur.lock.OInterruptedException)1 OException (com.orientechnologies.common.exception.OException)1 OCommandOutputListener (com.orientechnologies.orient.core.command.OCommandOutputListener)1 ORecordId (com.orientechnologies.orient.core.id.ORecordId)1 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)1 OSecurityUser (com.orientechnologies.orient.core.metadata.security.OSecurityUser)1 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)1 OPaginatedCluster (com.orientechnologies.orient.core.storage.impl.local.paginated.OPaginatedCluster)1 ODistributedStorage (com.orientechnologies.orient.server.distributed.impl.ODistributedStorage)1 ODistributedOperationException (com.orientechnologies.orient.server.distributed.task.ODistributedOperationException)1