Search in sources :

Example 1 with TryLockInput

use of org.opendaylight.yang.gen.v1.urn.opendaylight.genius.lockmanager.rev160413.TryLockInput in project netvirt by opendaylight.

the class VpnUtil method lockSubnet.

public static void lockSubnet(LockManagerService lockManager, String subnetId) {
    TryLockInput input = new TryLockInputBuilder().setLockName(subnetId).setTime(3000L).setTimeUnit(TimeUnits.Milliseconds).build();
    Future<RpcResult<Void>> result = lockManager.tryLock(input);
    try {
        if (result != null && result.get().isSuccessful()) {
            LOG.debug("lockSubnet: Acquired lock for {}", subnetId);
        } else {
            LOG.error("Unable to get lock for subnet {}", subnetId);
            throw new RuntimeException("Unable to get lock for subnet " + subnetId);
        }
    } catch (InterruptedException | ExecutionException e) {
        LOG.error("Unable to get lock for subnet {}", subnetId, e);
        throw new RuntimeException("Unable to get lock for subnet " + subnetId, e);
    }
}
Also used : TryLockInput(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.lockmanager.rev160413.TryLockInput) RpcResult(org.opendaylight.yangtools.yang.common.RpcResult) TryLockInputBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.lockmanager.rev160413.TryLockInputBuilder) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with TryLockInput

use of org.opendaylight.yang.gen.v1.urn.opendaylight.genius.lockmanager.rev160413.TryLockInput in project genius by opendaylight.

the class LockManagerServiceImpl method tryLock.

@Override
public Future<RpcResult<Void>> tryLock(TryLockInput input) {
    String lockName = input.getLockName();
    String owner = lockManagerUtils.getUniqueID();
    LOG.debug("Locking {}, owner {}", lockName, owner);
    long waitTime = input.getTime() == null ? DEFAULT_WAIT_TIME_IN_MILLIS * DEFAULT_RETRY_COUNT : input.getTime();
    TimeUnit timeUnit = input.getTimeUnit() == null ? TimeUnit.MILLISECONDS : lockManagerUtils.convertToTimeUnit(input.getTimeUnit());
    waitTime = timeUnit.toMillis(waitTime);
    long retryCount = waitTime / DEFAULT_WAIT_TIME_IN_MILLIS;
    InstanceIdentifier<Lock> lockInstanceIdentifier = lockManagerUtils.getLockInstanceIdentifier(lockName);
    Lock lockData = lockManagerUtils.buildLock(lockName, owner);
    RpcResultBuilder<Void> lockRpcBuilder;
    try {
        if (getLock(lockInstanceIdentifier, lockData, retryCount)) {
            lockRpcBuilder = RpcResultBuilder.success();
            LOG.debug("Acquired lock {} by owner {}", lockName, owner);
        } else {
            lockRpcBuilder = RpcResultBuilder.failed();
            LOG.error("Failed to get lock {} owner {} after {} retries", lockName, owner, retryCount);
        }
    } catch (InterruptedException e) {
        lockRpcBuilder = RpcResultBuilder.failed();
        LOG.error("Failed to get lock {} owner {}", lockName, owner, e);
    }
    return Futures.immediateFuture(lockRpcBuilder.build());
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) Lock(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.lockmanager.rev160413.locks.Lock)

Example 3 with TryLockInput

use of org.opendaylight.yang.gen.v1.urn.opendaylight.genius.lockmanager.rev160413.TryLockInput in project genius by opendaylight.

the class LockManagerTest method testTryLock.

@Test
public // tryLock() RPC will retry only specific number of times, and it will only return after that
void testTryLock() throws InterruptedException, ExecutionException, TimeoutException {
    String uniqueId = lockManagerUtils.getBladeId() + ":2";
    logCaptureRule.expectError("Failed to get lock testTryLock owner " + uniqueId + " after 3 retries");
    TryLockInput lockInput = new TryLockInputBuilder().setLockName("testTryLock").setTime(3L).setTimeUnit(TimeUnits.Seconds).build();
    assertVoidRpcSuccess(lockManager.tryLock(lockInput));
    // The second acquireLock request will retry for 3 seconds
    // and since the first lock is not unlocked, the request will fail.
    lockInput = new TryLockInputBuilder().setLockName("testTryLock").setTime(3000L).setTimeUnit(TimeUnits.Milliseconds).build();
    assertRpcErrorWithoutCausesOrMessages(lockManager.tryLock(lockInput));
    // Try to unlock the key in a separate thread before retry expires, and see
    // if lock gets acquired.
    runUnlockTimerTask("testTryLock", 2000);
    lockInput = new TryLockInputBuilder().setLockName("testTryLock").setTime(4000000L).setTimeUnit(TimeUnits.Microseconds).build();
    assertVoidRpcSuccess(lockManager.tryLock(lockInput));
}
Also used : TryLockInput(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.lockmanager.rev160413.TryLockInput) TryLockInputBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.lockmanager.rev160413.TryLockInputBuilder) AbstractConcurrentDataBrokerTest(org.opendaylight.controller.md.sal.binding.test.AbstractConcurrentDataBrokerTest) Test(org.junit.Test)

Aggregations

TryLockInput (org.opendaylight.yang.gen.v1.urn.opendaylight.genius.lockmanager.rev160413.TryLockInput)2 TryLockInputBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.genius.lockmanager.rev160413.TryLockInputBuilder)2 ExecutionException (java.util.concurrent.ExecutionException)1 TimeUnit (java.util.concurrent.TimeUnit)1 Test (org.junit.Test)1 AbstractConcurrentDataBrokerTest (org.opendaylight.controller.md.sal.binding.test.AbstractConcurrentDataBrokerTest)1 Lock (org.opendaylight.yang.gen.v1.urn.opendaylight.genius.lockmanager.rev160413.locks.Lock)1 RpcResult (org.opendaylight.yangtools.yang.common.RpcResult)1