Search in sources :

Example 1 with ODistributedLockTask

use of com.orientechnologies.orient.server.distributed.impl.task.ODistributedLockTask 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 2 with ODistributedLockTask

use of com.orientechnologies.orient.server.distributed.impl.task.ODistributedLockTask 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)

Aggregations

OLockException (com.orientechnologies.common.concur.lock.OLockException)2 ODistributedLockTask (com.orientechnologies.orient.server.distributed.impl.task.ODistributedLockTask)2 HashSet (java.util.HashSet)2 ODistributedOperationException (com.orientechnologies.orient.server.distributed.task.ODistributedOperationException)1