Search in sources :

Example 1 with TryLockInputBuilder

use of org.opendaylight.yang.gen.v1.urn.opendaylight.genius.lockmanager.rev160413.TryLockInputBuilder 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 TryLockInputBuilder

use of org.opendaylight.yang.gen.v1.urn.opendaylight.genius.lockmanager.rev160413.TryLockInputBuilder 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 Test (org.junit.Test)1 AbstractConcurrentDataBrokerTest (org.opendaylight.controller.md.sal.binding.test.AbstractConcurrentDataBrokerTest)1 RpcResult (org.opendaylight.yangtools.yang.common.RpcResult)1