Search in sources :

Example 1 with OLockException

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

the class OResourcePool method getResource.

public V getResource(K key, final long maxWaitMillis, Object... additionalArgs) throws OLockException {
    // First, get permission to take or create a resource
    try {
        if (!sem.tryAcquire(maxWaitMillis, TimeUnit.MILLISECONDS))
            throw new OLockException("No more resources available in pool (max=" + maxResources + "). Requested resource: " + key);
    } catch (InterruptedException e) {
        throw new OInterruptedException("Acquiring of resources was interrupted");
    }
    V res;
    do {
        // POP A RESOURCE
        res = resources.poll();
        if (res != null) {
            // TRY TO REUSE IT
            if (listener.reuseResource(key, additionalArgs, res)) {
                // OK: REUSE IT
                break;
            } else
                res = null;
        // UNABLE TO REUSE IT: THE RESOURE WILL BE DISCARDED AND TRY WITH THE NEXT ONE, IF ANY
        }
    } while (!resources.isEmpty());
    // NO AVAILABLE RESOURCES: CREATE A NEW ONE
    try {
        if (res == null) {
            res = listener.createNewResource(key, additionalArgs);
            created.incrementAndGet();
            if (OLogManager.instance().isDebugEnabled())
                OLogManager.instance().debug(this, "pool:'%s' created new resource '%s', new resource count '%d'", this, res, created.get());
        }
        resourcesOut.add(res);
        if (OLogManager.instance().isDebugEnabled())
            OLogManager.instance().debug(this, "pool:'%s' acquired resource '%s' available %d out %d ", this, res, sem.availablePermits(), resourcesOut.size());
        return res;
    } catch (RuntimeException e) {
        sem.release();
        // PROPAGATE IT
        throw e;
    } catch (Exception e) {
        sem.release();
        throw OException.wrapException(new OLockException("Error on creation of the new resource in the pool"), e);
    }
}
Also used : OLockException(com.orientechnologies.common.concur.lock.OLockException) OInterruptedException(com.orientechnologies.common.concur.lock.OInterruptedException) OInterruptedException(com.orientechnologies.common.concur.lock.OInterruptedException) OInterruptedException(com.orientechnologies.common.concur.lock.OInterruptedException) OLockException(com.orientechnologies.common.concur.lock.OLockException) OException(com.orientechnologies.common.exception.OException)

Example 2 with OLockException

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

the class ODatabasePoolAbstract method release.

public void release(final DB iDatabase) {
    // REMOVE ANY INTENT BEFORE. THIS RESTORE ANYTHING BEFORE THE CLOSE, LIKE THE USER NAME IN CASE OF MASSIVE INSERT
    iDatabase.declareIntent(null);
    final String dbPooledName = iDatabase.getUser().getName() + "@" + iDatabase.getURL();
    final OReentrantResourcePool<String, DB> pool;
    lock();
    try {
        pool = pools.get(dbPooledName);
    } finally {
        unlock();
    }
    if (pool == null)
        throw new OLockException("Cannot release a database URL not acquired before. URL: " + iDatabase.getName());
    if (pool.returnResource(iDatabase))
        this.notifyEvictor(dbPooledName, iDatabase);
}
Also used : OLockException(com.orientechnologies.common.concur.lock.OLockException)

Example 3 with OLockException

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

the class OTransactionAbstract method lockRecord.

@Override
public OTransaction lockRecord(final OIdentifiable iRecord, final OStorage.LOCKING_STRATEGY lockingStrategy) {
    final OStorage stg = database.getStorage();
    if (!(stg.getUnderlying() instanceof OAbstractPaginatedStorage))
        throw new OLockException("Cannot lock record across remote connections");
    final ORID rid = new ORecordId(iRecord.getIdentity());
    LockedRecordMetadata lockedRecordMetadata = locks.get(rid);
    boolean addItem = false;
    if (lockedRecordMetadata == null) {
        lockedRecordMetadata = new LockedRecordMetadata(lockingStrategy);
        addItem = true;
    } else if (lockedRecordMetadata.strategy != lockingStrategy) {
        assert lockedRecordMetadata.locksCount == 0;
        lockedRecordMetadata = new LockedRecordMetadata(lockingStrategy);
        addItem = true;
    }
    if (lockingStrategy == OStorage.LOCKING_STRATEGY.EXCLUSIVE_LOCK)
        ((OAbstractPaginatedStorage) stg.getUnderlying()).acquireWriteLock(rid);
    else if (lockingStrategy == OStorage.LOCKING_STRATEGY.SHARED_LOCK)
        ((OAbstractPaginatedStorage) stg.getUnderlying()).acquireReadLock(rid);
    else
        throw new IllegalStateException("Unsupported locking strategy " + lockingStrategy);
    lockedRecordMetadata.locksCount++;
    if (addItem) {
        locks.put(rid, lockedRecordMetadata);
    }
    return this;
}
Also used : OLockException(com.orientechnologies.common.concur.lock.OLockException) OStorage(com.orientechnologies.orient.core.storage.OStorage) ORID(com.orientechnologies.orient.core.id.ORID) OAbstractPaginatedStorage(com.orientechnologies.orient.core.storage.impl.local.OAbstractPaginatedStorage) ORecordId(com.orientechnologies.orient.core.id.ORecordId)

Example 4 with OLockException

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

the class OTransactionAbstract method unlockRecord.

@Override
public OTransaction unlockRecord(final OIdentifiable iRecord) {
    final OStorage stg = database.getStorage();
    if (!(stg.getUnderlying() instanceof OAbstractPaginatedStorage))
        throw new OLockException("Cannot lock record across remote connections");
    final ORID rid = iRecord.getIdentity();
    final LockedRecordMetadata lockedRecordMetadata = locks.get(rid);
    if (lockedRecordMetadata == null || lockedRecordMetadata.locksCount == 0)
        throw new OLockException("Cannot unlock a never acquired lock");
    else if (lockedRecordMetadata.strategy == OStorage.LOCKING_STRATEGY.EXCLUSIVE_LOCK)
        ((OAbstractPaginatedStorage) stg.getUnderlying()).releaseWriteLock(rid);
    else if (lockedRecordMetadata.strategy == OStorage.LOCKING_STRATEGY.SHARED_LOCK)
        ((OAbstractPaginatedStorage) stg.getUnderlying()).releaseReadLock(rid);
    else
        throw new IllegalStateException("Unsupported locking strategy " + lockedRecordMetadata.strategy);
    lockedRecordMetadata.locksCount--;
    if (lockedRecordMetadata.locksCount == 0)
        locks.remove(rid);
    return this;
}
Also used : OLockException(com.orientechnologies.common.concur.lock.OLockException) OStorage(com.orientechnologies.orient.core.storage.OStorage) ORID(com.orientechnologies.orient.core.id.ORID) OAbstractPaginatedStorage(com.orientechnologies.orient.core.storage.impl.local.OAbstractPaginatedStorage)

Example 5 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)

Aggregations

OLockException (com.orientechnologies.common.concur.lock.OLockException)11 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 ODistributedOperationException (com.orientechnologies.orient.server.distributed.task.ODistributedOperationException)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