Search in sources :

Example 1 with IdEntries

use of org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.id.pool.IdEntries in project genius by opendaylight.

the class UpdateIdEntryJob method call.

@Override
public List<ListenableFuture<Void>> call() throws TransactionCommitFailedException {
    String uniqueIdKey = idUtils.getUniqueKey(parentPoolName, idKey);
    try {
        txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx -> {
            idUtils.updateChildPool(tx, parentPoolName, localPoolName);
            if (!newIdValues.isEmpty()) {
                IdEntries newIdEntry = idUtils.createIdEntries(idKey, newIdValues);
                tx.merge(CONFIGURATION, idUtils.getIdEntriesInstanceIdentifier(parentPoolName, idKey), newIdEntry);
            } else {
                tx.delete(CONFIGURATION, idUtils.getIdEntriesInstanceIdentifier(parentPoolName, idKey));
            }
        }).get();
        LOG.info("Updated id entry with idValues {}, idKey {}, pool {}", newIdValues, idKey, localPoolName);
    } catch (InterruptedException | ExecutionException e) {
        LOG.error("Error updating id entry job", e);
    } finally {
        CountDownLatch latch = idUtils.getReleaseIdLatch(uniqueIdKey);
        if (latch != null) {
            latch.countDown();
        }
        // Once the id is written to DS, removing the id value from map.
        idUtils.removeAllocatedIds(uniqueIdKey);
        idUtils.unlock(lockManager, uniqueIdKey);
    }
    return Collections.emptyList();
}
Also used : Logger(org.slf4j.Logger) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) ManagedNewTransactionRunner(org.opendaylight.genius.infra.ManagedNewTransactionRunner) LoggerFactory(org.slf4j.LoggerFactory) CONFIGURATION(org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType.CONFIGURATION) Callable(java.util.concurrent.Callable) TransactionCommitFailedException(org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException) IdUtils(org.opendaylight.genius.idmanager.IdUtils) ArrayList(java.util.ArrayList) ExecutionException(java.util.concurrent.ExecutionException) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) IdEntries(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.id.pool.IdEntries) LockManagerService(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.lockmanager.rev160413.LockManagerService) Collections(java.util.Collections) IdEntries(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.id.pool.IdEntries) ExecutionException(java.util.concurrent.ExecutionException) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 2 with IdEntries

use of org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.id.pool.IdEntries in project genius by opendaylight.

the class IdManager method releaseIdFromLocalPool.

private void releaseIdFromLocalPool(String parentPoolName, String localPoolName, String idKey) throws ReadFailedException, IdManagerException {
    String idLatchKey = idUtils.getUniqueKey(parentPoolName, idKey);
    LOG.debug("Releasing ID {} from pool {}", idKey, localPoolName);
    CountDownLatch latch = idUtils.getReleaseIdLatch(idLatchKey);
    if (latch != null) {
        try {
            if (!latch.await(10, TimeUnit.SECONDS)) {
                LOG.warn("Timed out while releasing id {} from id pool {}", idKey, parentPoolName);
            }
        } catch (InterruptedException ignored) {
            LOG.warn("Thread interrupted while releasing id {} from id pool {}", idKey, parentPoolName);
        } finally {
            idUtils.removeReleaseIdLatch(idLatchKey);
        }
    }
    localPoolName = localPoolName.intern();
    InstanceIdentifier<IdPool> parentIdPoolInstanceIdentifier = idUtils.getIdPoolInstance(parentPoolName);
    IdPool parentIdPool = singleTxDB.syncRead(CONFIGURATION, parentIdPoolInstanceIdentifier);
    List<IdEntries> idEntries = parentIdPool.getIdEntries();
    List<IdEntries> newIdEntries = idEntries;
    if (idEntries == null) {
        throw new IdDoesNotExistException(parentPoolName, idKey);
    }
    InstanceIdentifier<IdEntries> existingId = idUtils.getIdEntry(parentIdPoolInstanceIdentifier, idKey);
    Optional<IdEntries> existingIdEntryObject = singleTxDB.syncReadOptional(CONFIGURATION, existingId);
    if (!existingIdEntryObject.isPresent()) {
        LOG.info("Specified Id key {} does not exist in id pool {}", idKey, parentPoolName);
        idUtils.unlock(lockManager, idLatchKey);
        return;
    }
    IdEntries existingIdEntry = existingIdEntryObject.get();
    List<Long> idValuesList = existingIdEntry.getIdValue();
    IdLocalPool localIdPoolCache = localPool.get(parentPoolName);
    boolean isRemoved = newIdEntries.remove(existingIdEntry);
    LOG.debug("The entry {} is removed {}", existingIdEntry, isRemoved);
    updateDelayedEntriesInLocalCache(idValuesList, parentPoolName, localIdPoolCache);
    IdHolderSyncJob poolSyncJob = new IdHolderSyncJob(localPoolName, localIdPoolCache.getReleasedIds(), txRunner, idUtils);
    jobCoordinator.enqueueJob(localPoolName, poolSyncJob, IdUtils.RETRY_COUNT);
    scheduleCleanUpTask(localIdPoolCache, parentPoolName, parentIdPool.getBlockSize());
    LOG.debug("Released id ({}, {}) from pool {}", idKey, idValuesList, localPoolName);
    // Updating id entries in the parent pool. This will be used for restart scenario
    UpdateIdEntryJob job = new UpdateIdEntryJob(parentPoolName, localPoolName, idKey, null, txRunner, idUtils, lockManager);
    jobCoordinator.enqueueJob(parentPoolName, job, IdUtils.RETRY_COUNT);
}
Also used : IdEntries(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.id.pool.IdEntries) DelayedIdEntries(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.released.ids.DelayedIdEntries) UpdateIdEntryJob(org.opendaylight.genius.idmanager.jobs.UpdateIdEntryJob) CountDownLatch(java.util.concurrent.CountDownLatch) IdPool(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.IdPool) IdHolderSyncJob(org.opendaylight.genius.idmanager.jobs.IdHolderSyncJob)

Example 3 with IdEntries

use of org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.id.pool.IdEntries in project genius by opendaylight.

the class IdManagerTest method getUpdatedActualParentPool.

private IdPool getUpdatedActualParentPool() throws ReadFailedException {
    IdPool idPoolParentFromDS = singleTxdataBroker.syncRead(LogicalDatastoreType.CONFIGURATION, InstanceIdentifier.builder(IdPools.class).child(IdPool.class, new IdPoolKey(ID_POOL_NAME)).build());
    List<ChildPools> childPool = idPoolParentFromDS.getChildPools();
    List<ChildPools> updatedChildPool = childPool.stream().map(child -> new ChildPoolsBuilder(child).setLastAccessTime(0L).build()).collect(Collectors.toList());
    List<IdEntries> idEntries = idPoolParentFromDS.getIdEntries();
    IdPoolBuilder idPoolBuilder = new IdPoolBuilder(idPoolParentFromDS);
    if (idEntries != null) {
        List<IdEntries> sortedIdEntries = idEntries.stream().sorted(comparing(IdEntries::getIdKey)).collect(Collectors.toList());
        idPoolBuilder.setIdEntries(sortedIdEntries);
    }
    IdPool actualIdPoolParent = idPoolBuilder.setChildPools(updatedChildPool).build();
    return actualIdPoolParent;
}
Also used : SingleTransactionDataBroker(org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker) Arrays(java.util.Arrays) MethodRule(org.junit.rules.MethodRule) LoggerFactory(org.slf4j.LoggerFactory) JobCoordinatorTestModule(org.opendaylight.genius.datastoreutils.testutils.JobCoordinatorTestModule) Future(java.util.concurrent.Future) GuiceRule(org.opendaylight.infrautils.inject.guice.testutils.GuiceRule) Optional(com.google.common.base.Optional) DeleteIdPoolInputBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.DeleteIdPoolInputBuilder) IdPoolKey(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.IdPoolKey) Assert.fail(org.junit.Assert.fail) CreateIdPoolInput(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolInput) ReleaseIdInput(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.ReleaseIdInput) ChildPools(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.id.pool.ChildPools) AvailableIdsHolderBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.id.pool.AvailableIdsHolderBuilder) JobCoordinatorEventsWaiter(org.opendaylight.genius.datastoreutils.testutils.JobCoordinatorEventsWaiter) LogicalDatastoreType(org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType) Set(java.util.Set) ChildPoolsBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.id.pool.ChildPoolsBuilder) Collectors(java.util.stream.Collectors) WriteTransaction(org.opendaylight.controller.md.sal.binding.api.WriteTransaction) Sets(com.google.common.collect.Sets) DataBroker(org.opendaylight.controller.md.sal.binding.api.DataBroker) AllocateIdInput(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdInput) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) LogCaptureRule(org.opendaylight.infrautils.testutils.LogCaptureRule) IdEntries(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.id.pool.IdEntries) AllocateIdOutput(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdOutput) DelayedIdEntries(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.released.ids.DelayedIdEntries) RpcResult(org.opendaylight.yangtools.yang.common.RpcResult) Executors(org.opendaylight.infrautils.utils.concurrent.Executors) AtomicReference(java.util.concurrent.atomic.AtomicReference) IdUtils(org.opendaylight.genius.idmanager.IdUtils) ArrayList(java.util.ArrayList) Inject(javax.inject.Inject) CreateIdPoolInputBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolInputBuilder) Comparator.comparing(java.util.Comparator.comparing) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) Logger(org.slf4j.Logger) IdPool(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.IdPool) Assert.assertNotNull(org.junit.Assert.assertNotNull) AssertDataObjects(org.opendaylight.mdsal.binding.testutils.AssertDataObjects) Assert.assertTrue(org.junit.Assert.assertTrue) ReleasedIdsHolder(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.id.pool.ReleasedIdsHolder) AssertionFailedError(junit.framework.AssertionFailedError) Test(org.junit.Test) ReleaseIdInputBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.ReleaseIdInputBuilder) TestableDataTreeChangeListenerModule(org.opendaylight.genius.datastoreutils.testutils.TestableDataTreeChangeListenerModule) LogRule(org.opendaylight.infrautils.testutils.LogRule) IdManagerService(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService) ExecutionException(java.util.concurrent.ExecutionException) IdPools(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdPools) DelayedIdEntriesBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.released.ids.DelayedIdEntriesBuilder) Rule(org.junit.Rule) AsyncEventsWaiter(org.opendaylight.genius.datastoreutils.testutils.AsyncEventsWaiter) InstanceIdentifier(org.opendaylight.yangtools.yang.binding.InstanceIdentifier) ReadFailedException(org.opendaylight.controller.md.sal.common.api.data.ReadFailedException) DeleteIdPoolInput(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.DeleteIdPoolInput) ReleasedIdsHolderBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.id.pool.ReleasedIdsHolderBuilder) AllocateIdInputBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdInputBuilder) AvailableIdsHolder(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.id.pool.AvailableIdsHolder) IdPoolBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.IdPoolBuilder) Collections(java.util.Collections) SECONDS(java.util.concurrent.TimeUnit.SECONDS) Assert.assertEquals(org.junit.Assert.assertEquals) RpcError(org.opendaylight.yangtools.yang.common.RpcError) ComparisonFailure(org.junit.ComparisonFailure) IdEntries(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.id.pool.IdEntries) DelayedIdEntries(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.released.ids.DelayedIdEntries) ChildPools(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.id.pool.ChildPools) IdPools(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdPools) IdPoolKey(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.IdPoolKey) IdPoolBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.IdPoolBuilder) ChildPoolsBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.id.pool.ChildPoolsBuilder) IdPool(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.IdPool)

Example 4 with IdEntries

use of org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.id.pool.IdEntries in project genius by opendaylight.

the class IdManagerTest method testMultithreadedIdAllocationFromReleaseIds.

@Test
// OK as exceptionInExecutor can't be Exception & AssertionFailedError
@SuppressWarnings("checkstyle:IllegalThrows")
public void testMultithreadedIdAllocationFromReleaseIds() throws Throwable {
    CreateIdPoolInput createIdPoolInput = new CreateIdPoolInputBuilder().setHigh(ID_HIGH).setLow(ID_LOW).setPoolName(ID_POOL_NAME).build();
    AllocateIdInput allocateIdInput = new AllocateIdInputBuilder().setIdKey(TEST_KEY1).setPoolName(ID_POOL_NAME).build();
    idManagerService.createIdPool(createIdPoolInput);
    idManagerService.allocateId(allocateIdInput);
    // Should wait for all job to complete.
    coordinatorEventsWaiter.awaitEventsConsumption();
    String localPoolName = idUtils.getLocalPoolName(ID_POOL_NAME);
    IdPool parentIdPool = new IdPoolBuilder().setPoolName(ID_POOL_NAME).setKey(new IdPoolKey(ID_POOL_NAME)).setReleasedIdsHolder(createReleaseIdHolder(Collections.emptyList())).build();
    IdPool childPool = new IdPoolBuilder().setPoolName(localPoolName).setKey(new IdPoolKey(localPoolName)).setReleasedIdsHolder(createReleaseIdHolder(Arrays.asList(1L, 2L, 3L))).setAvailableIdsHolder(createAvailableIdHolder(0L, 9L, 10L)).build();
    WriteTransaction tx = dataBroker.newWriteOnlyTransaction();
    tx.merge(LogicalDatastoreType.CONFIGURATION, getIdPoolIdentifier(ID_POOL_NAME), parentIdPool);
    tx.merge(LogicalDatastoreType.CONFIGURATION, getIdPoolIdentifier(localPoolName), childPool);
    tx.submit().get();
    // Wait for the changes to be available on the caches.
    asyncEventsWaiter.awaitEventsConsumption();
    requestIdsConcurrently(false);
    coordinatorEventsWaiter.awaitEventsConsumption();
    asyncEventsWaiter.awaitEventsConsumption();
    IdPool actualIdPoolChild = getUpdatedActualChildPool();
    IdPool actualIdPoolParent = getUpdatedActualParentPool();
    // Cannot compare the idEntries since we cannot guarantee which idKey gets what value.
    // However the allocated id values uniqueness is verified in requestIdsConcurrently method.
    AssertDataObjects.assertEqualBeans(ExpectedAllocateIdMultipleRequestsFromReleaseIds.idPoolParent().getReleasedIdsHolder(), actualIdPoolParent.getReleasedIdsHolder());
    AssertDataObjects.assertEqualBeans(ExpectedAllocateIdMultipleRequestsFromReleaseIds.idPoolChild(), actualIdPoolChild);
}
Also used : WriteTransaction(org.opendaylight.controller.md.sal.binding.api.WriteTransaction) AllocateIdInputBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdInputBuilder) AllocateIdInput(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdInput) CreateIdPoolInput(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolInput) IdPoolBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.IdPoolBuilder) IdPoolKey(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.IdPoolKey) CreateIdPoolInputBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolInputBuilder) IdPool(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.IdPool) Test(org.junit.Test)

Example 5 with IdEntries

use of org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.id.pool.IdEntries in project genius by opendaylight.

the class IdManagerTest method testMultithreadedIdAllocationFromAvailableIds.

@Test
// OK as exceptionInExecutor can't be Exception & AssertionFailedError
@SuppressWarnings("checkstyle:IllegalThrows")
public void testMultithreadedIdAllocationFromAvailableIds() throws Throwable {
    CreateIdPoolInput createIdPoolInput = new CreateIdPoolInputBuilder().setHigh(ID_HIGH).setLow(ID_LOW).setPoolName(ID_POOL_NAME).build();
    idManagerService.createIdPool(createIdPoolInput);
    requestIdsConcurrently(false);
    coordinatorEventsWaiter.awaitEventsConsumption();
    IdPool actualIdPoolChild = getUpdatedActualChildPool();
    IdPool actualIdPoolParent = getUpdatedActualParentPool();
    // Cannot compare the idEntries since we cannot guarantee which idKey gets what value.
    // However the allocated id values uniqueness is verified in requestIdsConcurrently method.
    AssertDataObjects.assertEqualBeans(ExpectedAllocateIdMultipleRequestsFromAvailableIds.idPoolParent().getAvailableIdsHolder(), actualIdPoolParent.getAvailableIdsHolder());
    AssertDataObjects.assertEqualBeans(ExpectedAllocateIdMultipleRequestsFromAvailableIds.idPoolChild(), actualIdPoolChild);
}
Also used : CreateIdPoolInput(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolInput) CreateIdPoolInputBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolInputBuilder) IdPool(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.IdPool) Test(org.junit.Test)

Aggregations

IdPool (org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.IdPool)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 Test (org.junit.Test)3 CreateIdPoolInput (org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolInput)3 CreateIdPoolInputBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolInputBuilder)3 IdEntries (org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.id.pool.IdEntries)3 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 List (java.util.List)2 ExecutionException (java.util.concurrent.ExecutionException)2 WriteTransaction (org.opendaylight.controller.md.sal.binding.api.WriteTransaction)2 IdUtils (org.opendaylight.genius.idmanager.IdUtils)2 AllocateIdInput (org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdInput)2 AllocateIdInputBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdInputBuilder)2 IdPoolBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.IdPoolBuilder)2 IdPoolKey (org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.IdPoolKey)2 DelayedIdEntries (org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.released.ids.DelayedIdEntries)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 Optional (com.google.common.base.Optional)1