Search in sources :

Example 1 with UpdateIdEntryJob

use of org.opendaylight.genius.idmanager.jobs.UpdateIdEntryJob 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 2 with UpdateIdEntryJob

use of org.opendaylight.genius.idmanager.jobs.UpdateIdEntryJob 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)

Aggregations

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