use of org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.IdPool in project genius by opendaylight.
the class IdManager method populateCache.
private void populateCache() throws ReadFailedException {
// If IP changes during reboot, then there will be orphaned child pools.
InstanceIdentifier<IdPools> idPoolsInstance = idUtils.getIdPools();
Optional<IdPools> idPoolsOptional = singleTxDB.syncReadOptional(CONFIGURATION, idPoolsInstance);
if (!idPoolsOptional.isPresent()) {
return;
}
IdPools idPools = idPoolsOptional.get();
List<IdPool> idPoolList = idPools.getIdPool();
idPoolList.stream().filter(idPool -> idPool.getParentPoolName() != null && !idPool.getParentPoolName().isEmpty() && idUtils.getLocalPoolName(idPool.getParentPoolName()).equals(idPool.getPoolName())).forEach(idPool -> updateLocalIdPoolCache(idPool, idPool.getParentPoolName()));
}
use of org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.IdPool in project genius by opendaylight.
the class IdManager method allocateIdBlockFromAvailableIdsHolder.
private long allocateIdBlockFromAvailableIdsHolder(IdLocalPool localIdPool, IdPool parentIdPool, WriteTransaction tx) {
long idCount = 0;
AvailableIdsHolderBuilder availableIdsBuilderParent = idUtils.getAvailableIdsHolderBuilder(parentIdPool);
long end = availableIdsBuilderParent.getEnd();
long cur = availableIdsBuilderParent.getCursor();
if (!idUtils.isIdAvailable(availableIdsBuilderParent)) {
if (LOG.isDebugEnabled()) {
LOG.debug("Ids exhausted in parent pool {}", parentIdPool);
}
return idCount;
}
// Update availableIdsHolder of Local Pool
idCount = Math.min(end - cur, parentIdPool.getBlockSize());
AvailableIdHolder availableIds = new AvailableIdHolder(idUtils, cur + 1, cur + idCount);
localIdPool.setAvailableIds(availableIds);
// Update availableIdsHolder of Global Pool
InstanceIdentifier<AvailableIdsHolder> availableIdsHolderInstanceIdentifier = InstanceIdentifier.builder(IdPools.class).child(IdPool.class, new IdPoolKey(parentIdPool.getPoolName())).child(AvailableIdsHolder.class).build();
availableIdsBuilderParent.setCursor(cur + idCount);
if (LOG.isDebugEnabled()) {
LOG.debug("Allocated {} ids from availableIds of global pool {}", idCount, parentIdPool);
}
tx.merge(CONFIGURATION, availableIdsHolderInstanceIdentifier, availableIdsBuilderParent.build(), true);
return idCount;
}
use of org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.IdPool in project genius by opendaylight.
the class IdManager method getIdBlockFromParentPool.
/**
* Changes made to availableIds and releasedIds will not be persisted to the datastore.
*/
private long getIdBlockFromParentPool(String parentPoolName, IdLocalPool localIdPool) throws OperationFailedException, IdManagerException {
if (LOG.isDebugEnabled()) {
LOG.debug("Allocating block of id from parent pool {}", parentPoolName);
}
InstanceIdentifier<IdPool> idPoolInstanceIdentifier = idUtils.getIdPoolInstance(parentPoolName);
parentPoolName = parentPoolName.intern();
idUtils.lock(lockManager, parentPoolName);
long idCount = 0;
try {
// Check if the childpool already got id block.
long availableIdCount = localIdPool.getAvailableIds().getAvailableIdCount() + localIdPool.getReleasedIds().getAvailableIdCount();
if (availableIdCount > 0) {
return availableIdCount;
}
WriteTransaction tx = broker.newWriteOnlyTransaction();
IdPool parentIdPool = singleTxDB.syncRead(CONFIGURATION, idPoolInstanceIdentifier);
idCount = allocateIdBlockFromParentPool(localIdPool, parentIdPool, tx);
tx.submit().checkedGet();
} catch (IdManagerException | NullPointerException e) {
LOG.error("Error getting id block from parent pool", e);
} finally {
idUtils.unlock(lockManager, parentPoolName);
}
return idCount;
}
use of org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.IdPool in project genius by opendaylight.
the class IdManager method allocateIdBlockFromParentPool.
private long allocateIdBlockFromParentPool(IdLocalPool localPoolCache, IdPool parentIdPool, WriteTransaction tx) throws OperationFailedException, IdManagerException {
long idCount = -1;
ReleasedIdsHolderBuilder releasedIdsBuilderParent = idUtils.getReleaseIdsHolderBuilder(parentIdPool);
while (true) {
idCount = allocateIdBlockFromAvailableIdsHolder(localPoolCache, parentIdPool, tx);
if (idCount > 0) {
return idCount;
}
idCount = allocateIdBlockFromReleasedIdsHolder(localPoolCache, releasedIdsBuilderParent, parentIdPool, tx);
if (idCount > 0) {
return idCount;
}
idCount = getIdsFromOtherChildPools(releasedIdsBuilderParent, parentIdPool);
if (idCount <= 0) {
if (LOG.isDebugEnabled()) {
LOG.debug("Unable to allocate Id block from global pool");
}
throw new IdManagerException(String.format("Ids exhausted for pool : %s", parentIdPool.getPoolName()));
}
}
}
use of org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.IdPool 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);
}
Aggregations