Search in sources :

Example 1 with OperationFailedException

use of org.opendaylight.yangtools.yang.common.OperationFailedException in project genius by opendaylight.

the class IdManager method allocateIdFromLocalPool.

private List<Long> allocateIdFromLocalPool(String parentPoolName, String localPoolName, String idKey, long size) throws OperationFailedException, IdManagerException {
    LOG.debug("Allocating id from local pool {}. Parent pool {}. Idkey {}", localPoolName, parentPoolName, idKey);
    String uniqueIdKey = idUtils.getUniqueKey(parentPoolName, idKey);
    CompletableFuture<List<Long>> futureIdValues = new CompletableFuture<>();
    CompletableFuture<List<Long>> existingFutureIdValue = idUtils.putAllocatedIdsIfAbsent(uniqueIdKey, futureIdValues);
    if (existingFutureIdValue != null) {
        try {
            return existingFutureIdValue.get();
        } catch (InterruptedException | ExecutionException e) {
            LOG.warn("Could not obtain id from existing futureIdValue for idKey {} and pool {}.", idKey, parentPoolName);
            throw new IdManagerException(e.getMessage(), e);
        }
    }
    try {
        List<Long> newIdValuesList = checkForIdInIdEntries(parentPoolName, idKey, uniqueIdKey, futureIdValues, false);
        if (!newIdValuesList.isEmpty()) {
            return newIdValuesList;
        }
        // This get will not help in concurrent reads. Hence the same read needs to be done again.
        IdLocalPool localIdPool = getOrCreateLocalIdPool(parentPoolName, localPoolName);
        LOG.debug("Got pool {}", localIdPool);
        long newIdValue = -1;
        localPoolName = localPoolName.intern();
        if (size == 1) {
            newIdValue = getIdFromLocalPoolCache(localIdPool, parentPoolName);
            newIdValuesList.add(newIdValue);
        } else {
            getRangeOfIds(parentPoolName, localPoolName, size, newIdValuesList, localIdPool, newIdValue);
        }
        LOG.debug("The newIdValues {} for the idKey {}", newIdValuesList, idKey);
        idUtils.putReleaseIdLatch(uniqueIdKey, new CountDownLatch(1));
        UpdateIdEntryJob job = new UpdateIdEntryJob(parentPoolName, localPoolName, idKey, newIdValuesList, txRunner, idUtils, lockManager);
        jobCoordinator.enqueueJob(parentPoolName, job, IdUtils.RETRY_COUNT);
        futureIdValues.complete(newIdValuesList);
        return newIdValuesList;
    } catch (OperationFailedException | IdManagerException e) {
        idUtils.unlock(lockManager, uniqueIdKey);
        throw e;
    }
}
Also used : UpdateIdEntryJob(org.opendaylight.genius.idmanager.jobs.UpdateIdEntryJob) OperationFailedException(org.opendaylight.yangtools.yang.common.OperationFailedException) CountDownLatch(java.util.concurrent.CountDownLatch) CompletableFuture(java.util.concurrent.CompletableFuture) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with OperationFailedException

use of org.opendaylight.yangtools.yang.common.OperationFailedException in project genius by opendaylight.

the class IdManager method getRangeOfIds.

private void getRangeOfIds(String parentPoolName, String localPoolName, long size, List<Long> newIdValuesList, IdLocalPool localIdPool, long newIdValue) throws ReadFailedException, IdManagerException {
    InstanceIdentifier<IdPool> parentIdPoolInstanceIdentifier1 = idUtils.getIdPoolInstance(parentPoolName);
    IdPool parentIdPool = singleTxDB.syncRead(CONFIGURATION, parentIdPoolInstanceIdentifier1);
    long totalAvailableIdCount = localIdPool.getAvailableIds().getAvailableIdCount() + localIdPool.getReleasedIds().getAvailableIdCount();
    AvailableIdsHolderBuilder availableParentIds = idUtils.getAvailableIdsHolderBuilder(parentIdPool);
    ReleasedIdsHolderBuilder releasedParentIds = idUtils.getReleaseIdsHolderBuilder(parentIdPool);
    totalAvailableIdCount = totalAvailableIdCount + releasedParentIds.getAvailableIdCount() + idUtils.getAvailableIdsCount(availableParentIds);
    if (totalAvailableIdCount > size) {
        while (size > 0) {
            try {
                newIdValue = getIdFromLocalPoolCache(localIdPool, parentPoolName);
            } catch (OperationFailedException e) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Releasing IDs to pool {}", localPoolName);
                }
                // Releasing the IDs added in newIdValuesList since
                // a null list would be returned now, as the
                // requested size of list IDs exceeds the number of
                // available IDs.
                updateDelayedEntriesInLocalCache(newIdValuesList, parentPoolName, localIdPool);
            }
            newIdValuesList.add(newIdValue);
            size--;
        }
    } else {
        throw new IdManagerException(String.format("Ids exhausted for pool : %s", parentPoolName));
    }
}
Also used : AvailableIdsHolderBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.id.pool.AvailableIdsHolderBuilder) ReleasedIdsHolderBuilder(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.id.pool.ReleasedIdsHolderBuilder) OperationFailedException(org.opendaylight.yangtools.yang.common.OperationFailedException) IdPool(org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.IdPool)

Aggregations

OperationFailedException (org.opendaylight.yangtools.yang.common.OperationFailedException)2 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutionException (java.util.concurrent.ExecutionException)1 UpdateIdEntryJob (org.opendaylight.genius.idmanager.jobs.UpdateIdEntryJob)1 IdPool (org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.IdPool)1 AvailableIdsHolderBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.id.pool.AvailableIdsHolderBuilder)1 ReleasedIdsHolderBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.id.pool.ReleasedIdsHolderBuilder)1