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);
}
}
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());
}
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));
}
Aggregations