Minor changes
[genius.git] / idmanager / idmanager-impl / src / main / java / org / opendaylight / genius / idmanager / IdManager.java
1 /*
2  * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.genius.idmanager;
9
10 import static java.util.Comparator.comparing;
11 import static java.util.stream.Collectors.toCollection;
12 import static org.opendaylight.controller.md.sal.binding.api.WriteTransaction.CREATE_MISSING_PARENTS;
13 import static org.opendaylight.genius.infra.Datastore.CONFIGURATION;
14
15 import com.google.common.base.Optional;
16 import com.google.common.util.concurrent.Futures;
17 import com.google.common.util.concurrent.ListenableFuture;
18 import com.google.common.util.concurrent.MoreExecutors;
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.HashMap;
22 import java.util.LinkedList;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Timer;
26 import java.util.TimerTask;
27 import java.util.concurrent.CompletableFuture;
28 import java.util.concurrent.ConcurrentHashMap;
29 import java.util.concurrent.ConcurrentMap;
30 import java.util.concurrent.CountDownLatch;
31 import java.util.concurrent.ExecutionException;
32 import java.util.concurrent.TimeUnit;
33 import javax.annotation.PostConstruct;
34 import javax.annotation.PreDestroy;
35 import javax.inject.Inject;
36 import javax.inject.Singleton;
37
38 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
39 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
40 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
41 import org.opendaylight.daexim.DataImportBootReady;
42 import org.opendaylight.genius.datastoreutils.ExpectedDataObjectNotFoundException;
43 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
44 import org.opendaylight.genius.idmanager.ReleasedIdHolder.DelayedIdEntry;
45 import org.opendaylight.genius.idmanager.api.IdManagerMonitor;
46 import org.opendaylight.genius.idmanager.jobs.CleanUpJob;
47 import org.opendaylight.genius.idmanager.jobs.IdHolderSyncJob;
48 import org.opendaylight.genius.idmanager.jobs.LocalPoolCreateJob;
49 import org.opendaylight.genius.idmanager.jobs.LocalPoolDeleteJob;
50 import org.opendaylight.genius.idmanager.jobs.UpdateIdEntryJob;
51 import org.opendaylight.genius.infra.Datastore.Configuration;
52 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
53 import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
54 import org.opendaylight.genius.infra.TypedReadWriteTransaction;
55 import org.opendaylight.genius.infra.TypedWriteTransaction;
56 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
57 import org.opendaylight.serviceutils.tools.mdsal.rpc.FutureRpcResults;
58 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdInput;
59 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdOutput;
60 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdOutputBuilder;
61 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdRangeInput;
62 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdRangeOutput;
63 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdRangeOutputBuilder;
64 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolInput;
65 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolOutput;
66 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolOutputBuilder;
67 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.DeleteIdPoolInput;
68 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.DeleteIdPoolOutput;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdPools;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.ReleaseIdInput;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.ReleaseIdOutput;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.IdPool;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.IdPoolBuilder;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.IdPoolKey;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.id.pool.AvailableIdsHolder;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.id.pool.AvailableIdsHolderBuilder;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.id.pool.ChildPools;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.id.pool.IdEntries;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.id.pool.ReleasedIdsHolder;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.id.pool.ReleasedIdsHolderBuilder;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.released.ids.DelayedIdEntries;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.lockmanager.rev160413.LockManagerService;
84 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
85 import org.opendaylight.yangtools.yang.common.OperationFailedException;
86 import org.opendaylight.yangtools.yang.common.RpcResult;
87 import org.ops4j.pax.cdi.api.OsgiService;
88 import org.slf4j.Logger;
89 import org.slf4j.LoggerFactory;
90
91 @Singleton
92 public class IdManager implements IdManagerService, IdManagerMonitor {
93
94     private static final Logger LOG = LoggerFactory.getLogger(IdManager.class);
95     private static final long DEFAULT_IDLE_TIME = 24 * 60 * 60;
96
97     private final DataBroker broker;
98     private final ManagedNewTransactionRunner txRunner;
99     private final SingleTransactionDataBroker singleTxDB;
100     private final LockManagerService lockManager;
101     private final IdUtils idUtils;
102     private final JobCoordinator jobCoordinator;
103
104     private final ConcurrentMap<String, IdLocalPool> localPool;
105     private final Timer cleanJobTimer = new Timer();
106
107     @Inject
108     public IdManager(DataBroker db, LockManagerService lockManager, IdUtils idUtils,
109             @OsgiService DataImportBootReady dataImportBootReady, JobCoordinator jobCoordinator)
110                     throws ReadFailedException {
111         this.broker = db;
112         this.txRunner = new ManagedNewTransactionRunnerImpl(db);
113         this.singleTxDB = new SingleTransactionDataBroker(db);
114         this.lockManager = lockManager;
115         this.idUtils = idUtils;
116         this.jobCoordinator = jobCoordinator;
117
118         // NB: We do not "use" the DataImportBootReady, but it's presence in the OSGi
119         // Service Registry is the required "signal" that the Daexim "import on boot"
120         // has fully completed (which we want to wait for).  Therefore, making this
121         // dependent on that defers the Blueprint initialization, as we'd like to,
122         // so that we do not start giving out new IDs before an import went in.
123         // Thus, please DO NOT remove the DataImportBootReady argument, even if
124         // it appears to be (is) un-used from a Java code PoV!
125
126         this.localPool = new ConcurrentHashMap<>();
127         populateCache();
128     }
129
130     @Override
131     public Map<String, String> getLocalPoolsDetails() {
132         Map<String, String> map = new HashMap<>();
133         localPool.forEach((key, value) -> map.put(key, value.toString()));
134         return map;
135     }
136
137     @PostConstruct
138     public void start() {
139         LOG.info("{} start", getClass().getSimpleName());
140     }
141
142     @PreDestroy
143     public void close() {
144         cleanJobTimer.cancel();
145
146         LOG.info("{} close", getClass().getSimpleName());
147     }
148
149     private void populateCache() throws ReadFailedException {
150         // If IP changes during reboot, then there will be orphaned child pools.
151         InstanceIdentifier<IdPools> idPoolsInstance = idUtils.getIdPools();
152         Optional<IdPools> idPoolsOptional =
153                 singleTxDB.syncReadOptional(LogicalDatastoreType.CONFIGURATION, idPoolsInstance);
154         if (!idPoolsOptional.isPresent()) {
155             return;
156         }
157         IdPools idPools = idPoolsOptional.get();
158         List<IdPool> idPoolList = idPools.getIdPool();
159         idPoolList
160                 .stream()
161                 .filter(idPool -> idPool.getParentPoolName() != null
162                         && !idPool.getParentPoolName().isEmpty()
163                         && idUtils.getLocalPoolName(idPool.getParentPoolName())
164                                 .equals(idPool.getPoolName()))
165                 .forEach(
166                     idPool -> updateLocalIdPoolCache(idPool,
167                         idPool.getParentPoolName()));
168     }
169
170     public boolean updateLocalIdPoolCache(IdPool idPool, String parentPoolName) {
171         AvailableIdsHolder availableIdsHolder = idPool.getAvailableIdsHolder();
172         AvailableIdHolder availableIdHolder = new AvailableIdHolder(idUtils, availableIdsHolder.getStart(),
173                 availableIdsHolder.getEnd());
174         availableIdHolder.setCur(availableIdsHolder.getCursor());
175         ReleasedIdsHolder releasedIdsHolder = idPool.getReleasedIdsHolder();
176         ReleasedIdHolder releasedIdHolder = new ReleasedIdHolder(idUtils, releasedIdsHolder.getDelayedTimeSec());
177         releasedIdHolder.setAvailableIdCount(releasedIdsHolder.getAvailableIdCount());
178         List<DelayedIdEntries> delayedEntries = releasedIdsHolder.getDelayedIdEntries();
179         List<DelayedIdEntry> delayedIdEntryInCache = delayedEntries
180                 .stream()
181                 .map(delayedIdEntry -> new DelayedIdEntry(delayedIdEntry
182                         .getId(), delayedIdEntry.getReadyTimeSec()))
183                 .sorted(comparing(DelayedIdEntry::getReadyTimeSec))
184                 .collect(toCollection(ArrayList::new));
185
186         releasedIdHolder.replaceDelayedEntries(delayedIdEntryInCache);
187
188         IdLocalPool idLocalPool = new IdLocalPool(idUtils, idPool.getPoolName());
189         idLocalPool.setAvailableIds(availableIdHolder);
190         idLocalPool.setReleasedIds(releasedIdHolder);
191         localPool.put(parentPoolName, idLocalPool);
192         if (LOG.isDebugEnabled()) {
193             LOG.debug("Populating cache for {} with {}", idLocalPool.getPoolName(), idLocalPool);
194         }
195         return true;
196     }
197
198     @Override
199     public ListenableFuture<RpcResult<CreateIdPoolOutput>> createIdPool(CreateIdPoolInput input) {
200         LOG.info("createIdPool called with input {}", input);
201         long low = input.getLow();
202         long high = input.getHigh();
203         long blockSize = idUtils.computeBlockSize(low, high);
204         return FutureRpcResults.fromListenableFuture(LOG, "createIdPool", input, () -> {
205             String poolName = input.getPoolName().intern();
206             try {
207                 idUtils.lock(lockManager, poolName);
208                 return Futures.transform(txRunner.callWithNewReadWriteTransactionAndSubmit(CONFIGURATION, confTx -> {
209                     IdPool idPool = createGlobalPool(confTx, poolName, low, high, blockSize);
210                     String localPoolName = idUtils.getLocalPoolName(poolName);
211                     IdLocalPool idLocalPool = localPool.get(poolName);
212                     if (idLocalPool == null) {
213                         createLocalPool(confTx, localPoolName, idPool);
214                         idUtils.updateChildPool(confTx, idPool.getPoolName(), localPoolName);
215                     }
216                 }), unused -> new CreateIdPoolOutputBuilder().build(), MoreExecutors.directExecutor());
217             } finally {
218                 idUtils.unlock(lockManager, poolName);
219             }
220         }).build();
221     }
222
223     @Override
224     public ListenableFuture<RpcResult<AllocateIdOutput>> allocateId(AllocateIdInput input) {
225         String idKey = input.getIdKey();
226         String poolName = input.getPoolName();
227         return FutureRpcResults.fromBuilder(LOG, "allocateId", input, () -> {
228             String localPoolName = idUtils.getLocalPoolName(poolName);
229             // allocateIdFromLocalPool method returns a list of IDs with one element. This element is obtained by get(0)
230             long newIdValue = allocateIdFromLocalPool(poolName, localPoolName, idKey, 1).get(0);
231             return new AllocateIdOutputBuilder().setIdValue(newIdValue);
232         }).onFailure(e -> completeExceptionallyIfPresent(poolName, idKey, e)).build();
233     }
234
235     private void completeExceptionallyIfPresent(String poolName, String idKey, Throwable exception) {
236         CompletableFuture<List<Long>> completableFuture =
237                 idUtils.removeAllocatedIds(idUtils.getUniqueKey(poolName, idKey));
238         if (completableFuture != null) {
239             completableFuture.completeExceptionally(exception);
240         }
241     }
242
243     @Override
244     public ListenableFuture<RpcResult<AllocateIdRangeOutput>> allocateIdRange(AllocateIdRangeInput input) {
245         String idKey = input.getIdKey();
246         String poolName = input.getPoolName();
247         long size = input.getSize();
248         String localPoolName = idUtils.getLocalPoolName(poolName);
249         AllocateIdRangeOutputBuilder output = new AllocateIdRangeOutputBuilder();
250         return FutureRpcResults.fromBuilder(LOG, "allocateIdRange", input, () -> {
251             List<Long> newIdValuesList = allocateIdFromLocalPool(poolName, localPoolName, idKey, size);
252             Collections.sort(newIdValuesList);
253             output.setIdValues(newIdValuesList);
254             return output;
255         }).onFailure(e -> completeExceptionallyIfPresent(poolName, idKey, e)).build();
256     }
257
258     @Override
259     public ListenableFuture<RpcResult<DeleteIdPoolOutput>> deleteIdPool(DeleteIdPoolInput input) {
260         return FutureRpcResults.fromListenableFuture(LOG, "deleteIdPool", input, () -> {
261             String poolName = input.getPoolName().intern();
262             InstanceIdentifier<IdPool> idPoolToBeDeleted = idUtils.getIdPoolInstance(poolName);
263             synchronized (poolName) {
264                 IdPool idPool = singleTxDB.syncRead(LogicalDatastoreType.CONFIGURATION, idPoolToBeDeleted);
265                 List<ChildPools> childPoolList = idPool.getChildPools();
266                 if (childPoolList != null) {
267                     childPoolList.forEach(childPool -> deletePool(childPool.getChildPoolName()));
268                 }
269                 singleTxDB.syncDelete(LogicalDatastoreType.CONFIGURATION, idPoolToBeDeleted);
270             }
271             // TODO return the Future from a TBD asyncDelete instead.. BUT check that all callers @CheckReturnValue
272             return Futures.immediateFuture((DeleteIdPoolOutput) null);
273         }).build();
274     }
275
276     @Override
277     public ListenableFuture<RpcResult<ReleaseIdOutput>> releaseId(ReleaseIdInput input) {
278         String poolName = input.getPoolName();
279         String idKey = input.getIdKey();
280         String uniqueKey = idUtils.getUniqueKey(poolName, idKey);
281         return FutureRpcResults.fromListenableFuture(LOG, "releaseId", input, () -> {
282             idUtils.lock(lockManager, uniqueKey);
283             releaseIdFromLocalPool(poolName, idUtils.getLocalPoolName(poolName), idKey);
284             // TODO return the Future from releaseIdFromLocalPool() instead.. check all callers @CheckReturnValue
285             return Futures.immediateFuture((ReleaseIdOutput) null);
286         }).onFailureLogLevel(org.opendaylight.serviceutils.tools.mdsal.rpc.FutureRpcResults.LogLevel.NONE)
287                 .onFailure(e -> {
288                     if (e instanceof IdDoesNotExistException) {
289                         // Do not log full stack trace in case ID does not exist
290                         LOG.error("RPC releaseId() failed due to IdDoesNotExistException; input = {}", input);
291                     } else {
292                         // But for all other cases do:
293                         LOG.error("RPC releaseId() failed; input = {}", input, e);
294                     }
295                     idUtils.unlock(lockManager, uniqueKey);
296                 }).build();
297     }
298
299     private List<Long> allocateIdFromLocalPool(String parentPoolName, String localPoolName,
300             String idKey, long size) throws OperationFailedException, IdManagerException {
301         LOG.debug("Allocating id from local pool {}. Parent pool {}. Idkey {}", localPoolName, parentPoolName, idKey);
302         String uniqueIdKey = idUtils.getUniqueKey(parentPoolName, idKey);
303         CompletableFuture<List<Long>> futureIdValues = new CompletableFuture<>();
304         CompletableFuture<List<Long>> existingFutureIdValue =
305                 idUtils.putAllocatedIdsIfAbsent(uniqueIdKey, futureIdValues);
306         if (existingFutureIdValue != null) {
307             try {
308                 return existingFutureIdValue.get();
309             } catch (InterruptedException | ExecutionException e) {
310                 LOG.warn("Could not obtain id from existing futureIdValue for idKey {} and pool {}.",
311                         idKey, parentPoolName);
312                 throw new IdManagerException(e.getMessage(), e);
313             }
314         }
315         try {
316             List<Long> newIdValuesList = checkForIdInIdEntries(parentPoolName, idKey, uniqueIdKey, futureIdValues,
317                     false);
318             if (!newIdValuesList.isEmpty()) {
319                 return newIdValuesList;
320             }
321             //This get will not help in concurrent reads. Hence the same read needs to be done again.
322             IdLocalPool localIdPool = getOrCreateLocalIdPool(parentPoolName, localPoolName);
323             LOG.debug("Got pool {}", localIdPool);
324             long newIdValue = -1;
325             localPoolName = localPoolName.intern();
326             if (size == 1) {
327                 newIdValue = getIdFromLocalPoolCache(localIdPool, parentPoolName);
328                 newIdValuesList.add(newIdValue);
329             } else {
330                 getRangeOfIds(parentPoolName, localPoolName, size, newIdValuesList, localIdPool, newIdValue);
331             }
332             LOG.debug("The newIdValues {} for the idKey {}", newIdValuesList, idKey);
333             idUtils.putReleaseIdLatch(uniqueIdKey, new CountDownLatch(1));
334             UpdateIdEntryJob job = new UpdateIdEntryJob(parentPoolName, localPoolName, idKey, newIdValuesList, txRunner,
335                     idUtils, lockManager);
336             jobCoordinator.enqueueJob(parentPoolName, job, IdUtils.RETRY_COUNT);
337             futureIdValues.complete(newIdValuesList);
338             return newIdValuesList;
339         } catch (OperationFailedException | IdManagerException e) {
340             idUtils.unlock(lockManager, uniqueIdKey);
341             throw e;
342         }
343     }
344
345     private Long getIdFromLocalPoolCache(IdLocalPool localIdPool, String parentPoolName)
346             throws IdManagerException {
347         while (true) {
348             IdHolder availableIds = localIdPool.getAvailableIds();
349             if (availableIds != null) {
350                 Optional<Long> availableId = availableIds.allocateId();
351                 if (availableId.isPresent()) {
352                     IdHolderSyncJob poolSyncJob =
353                             new IdHolderSyncJob(localIdPool.getPoolName(), localIdPool.getAvailableIds(), txRunner,
354                                     idUtils);
355                     jobCoordinator.enqueueJob(localIdPool.getPoolName(), poolSyncJob, IdUtils.RETRY_COUNT);
356                     return availableId.get();
357                 }
358             }
359             IdHolder releasedIds = localIdPool.getReleasedIds();
360             Optional<Long> releasedId = releasedIds.allocateId();
361             if (releasedId.isPresent()) {
362                 IdHolderSyncJob poolSyncJob =
363                         new IdHolderSyncJob(localIdPool.getPoolName(), localIdPool.getReleasedIds(), txRunner,
364                                 idUtils);
365                 jobCoordinator.enqueueJob(localIdPool.getPoolName(), poolSyncJob, IdUtils.RETRY_COUNT);
366                 return releasedId.get();
367             }
368             long idCount = getIdBlockFromParentPool(parentPoolName, localIdPool);
369             if (idCount <= 0) {
370                 if (LOG.isDebugEnabled()) {
371                     LOG.debug("Unable to allocate Id block from global pool {}", parentPoolName);
372                 }
373                 throw new IdManagerException(String.format("Ids exhausted for pool : %s", parentPoolName));
374             }
375         }
376     }
377
378     /**
379      * Changes made to availableIds and releasedIds will not be persisted to the datastore.
380      */
381     private long getIdBlockFromParentPool(String parentPoolName, IdLocalPool localIdPool)
382             throws IdManagerException {
383         if (LOG.isDebugEnabled()) {
384             LOG.debug("Allocating block of id from parent pool {}", parentPoolName);
385         }
386         InstanceIdentifier<IdPool> idPoolInstanceIdentifier = idUtils.getIdPoolInstance(parentPoolName);
387         parentPoolName = parentPoolName.intern();
388         idUtils.lock(lockManager, parentPoolName);
389         try {
390             // Check if the childpool already got id block.
391             long availableIdCount =
392                     localIdPool.getAvailableIds().getAvailableIdCount()
393                             + localIdPool.getReleasedIds().getAvailableIdCount();
394             if (availableIdCount > 0) {
395                 return availableIdCount;
396             }
397             return txRunner.applyWithNewReadWriteTransactionAndSubmit(CONFIGURATION, confTx -> {
398                 Optional<IdPool> parentIdPool = confTx.read(idPoolInstanceIdentifier).get();
399                 if (parentIdPool.isPresent()) {
400                     return allocateIdBlockFromParentPool(localIdPool, parentIdPool.get(), confTx);
401                 } else {
402                     throw new ExpectedDataObjectNotFoundException(LogicalDatastoreType.CONFIGURATION,
403                             idPoolInstanceIdentifier);
404                 }
405             }).get();
406         } catch (InterruptedException | ExecutionException e) {
407             throw new IdManagerException("Error getting id block from parent pool", e);
408         } finally {
409             idUtils.unlock(lockManager, parentPoolName);
410         }
411     }
412
413     private long allocateIdBlockFromParentPool(IdLocalPool localPoolCache, IdPool parentIdPool,
414             TypedWriteTransaction<Configuration> confTx)
415             throws OperationFailedException, IdManagerException {
416         long idCount;
417         ReleasedIdsHolderBuilder releasedIdsBuilderParent = IdUtils.getReleaseIdsHolderBuilder(parentIdPool);
418         while (true) {
419             idCount = allocateIdBlockFromAvailableIdsHolder(localPoolCache, parentIdPool, confTx);
420             if (idCount > 0) {
421                 return idCount;
422             }
423             idCount = allocateIdBlockFromReleasedIdsHolder(localPoolCache, releasedIdsBuilderParent, parentIdPool,
424                     confTx);
425             if (idCount > 0) {
426                 return idCount;
427             }
428             idCount = getIdsFromOtherChildPools(releasedIdsBuilderParent, parentIdPool);
429             if (idCount <= 0) {
430                 if (LOG.isDebugEnabled()) {
431                     LOG.debug("Unable to allocate Id block from global pool");
432                 }
433                 throw new IdManagerException(String.format("Ids exhausted for pool : %s", parentIdPool.getPoolName()));
434             }
435         }
436     }
437
438     private long getIdsFromOtherChildPools(ReleasedIdsHolderBuilder releasedIdsBuilderParent, IdPool parentIdPool)
439             throws OperationFailedException {
440         List<ChildPools> childPoolsList = parentIdPool.getChildPools();
441         // Sorting the child pools on last accessed time so that the pool that
442         // was not accessed for a long time comes first.
443         childPoolsList.sort(comparing(ChildPools::getLastAccessTime));
444         long currentTime = System.currentTimeMillis() / 1000;
445         for (ChildPools childPools : childPoolsList) {
446             if (childPools.getLastAccessTime() + DEFAULT_IDLE_TIME > currentTime) {
447                 break;
448             }
449             if (!childPools.getChildPoolName().equals(idUtils.getLocalPoolName(parentIdPool.getPoolName()))) {
450                 InstanceIdentifier<IdPool> idPoolInstanceIdentifier = idUtils
451                         .getIdPoolInstance(childPools.getChildPoolName());
452                 IdPool otherChildPool =
453                         singleTxDB.syncRead(LogicalDatastoreType.CONFIGURATION, idPoolInstanceIdentifier);
454                 ReleasedIdsHolderBuilder releasedIds = IdUtils.getReleaseIdsHolderBuilder(otherChildPool);
455
456                 List<DelayedIdEntries> delayedIdEntriesChild = releasedIds.getDelayedIdEntries();
457                 List<DelayedIdEntries> delayedIdEntriesParent = releasedIdsBuilderParent.getDelayedIdEntries();
458                 if (delayedIdEntriesParent == null) {
459                     delayedIdEntriesParent = new LinkedList<>();
460                 }
461                 delayedIdEntriesParent.addAll(delayedIdEntriesChild);
462                 delayedIdEntriesChild.clear();
463
464                 AvailableIdsHolderBuilder availableIds = idUtils.getAvailableIdsHolderBuilder(otherChildPool);
465                 while (idUtils.isIdAvailable(availableIds)) {
466                     long cursor = availableIds.getCursor() + 1;
467                     delayedIdEntriesParent.add(idUtils.createDelayedIdEntry(cursor, currentTime));
468                     availableIds.setCursor(cursor);
469                 }
470
471                 long totalAvailableIdCount = releasedIds.getDelayedIdEntries().size()
472                         + idUtils.getAvailableIdsCount(availableIds);
473                 long count = releasedIdsBuilderParent.getAvailableIdCount() + totalAvailableIdCount;
474                 releasedIdsBuilderParent.setDelayedIdEntries(delayedIdEntriesParent).setAvailableIdCount(count);
475                 singleTxDB.syncUpdate(LogicalDatastoreType.CONFIGURATION, idPoolInstanceIdentifier,
476                         new IdPoolBuilder().withKey(new IdPoolKey(otherChildPool.getPoolName()))
477                                 .setAvailableIdsHolder(availableIds.build()).setReleasedIdsHolder(releasedIds.build())
478                                 .build());
479                 return totalAvailableIdCount;
480             }
481         }
482         return 0;
483     }
484
485     private long allocateIdBlockFromReleasedIdsHolder(IdLocalPool localIdPool,
486             ReleasedIdsHolderBuilder releasedIdsBuilderParent, IdPool parentIdPool,
487             TypedWriteTransaction<Configuration> confTx) {
488         if (releasedIdsBuilderParent.getAvailableIdCount() == 0) {
489             LOG.debug("Ids unavailable in releasedIds of parent pool {}", parentIdPool);
490             return 0;
491         }
492         List<DelayedIdEntries> delayedIdEntriesParent = releasedIdsBuilderParent.getDelayedIdEntries();
493         int idCount = Math.min(delayedIdEntriesParent.size(), parentIdPool.getBlockSize());
494         List<DelayedIdEntries> idEntriesToBeRemoved = delayedIdEntriesParent.subList(0, idCount);
495         ReleasedIdHolder releasedIds = (ReleasedIdHolder) localIdPool.getReleasedIds();
496         List<DelayedIdEntry> delayedIdEntriesLocalCache = releasedIds.getDelayedEntries();
497         List<DelayedIdEntry> delayedIdEntriesFromParentPool = idEntriesToBeRemoved
498                 .stream()
499                 .map(delayedIdEntry -> new DelayedIdEntry(delayedIdEntry
500                         .getId(), delayedIdEntry.getReadyTimeSec()))
501                 .sorted(comparing(DelayedIdEntry::getReadyTimeSec))
502                 .collect(toCollection(ArrayList::new));
503         delayedIdEntriesFromParentPool.addAll(delayedIdEntriesLocalCache);
504         releasedIds.replaceDelayedEntries(delayedIdEntriesFromParentPool);
505         releasedIds.setAvailableIdCount(releasedIds.getAvailableIdCount() + idCount);
506         localIdPool.setReleasedIds(releasedIds);
507         delayedIdEntriesParent.removeAll(idEntriesToBeRemoved);
508         releasedIdsBuilderParent.setDelayedIdEntries(delayedIdEntriesParent);
509         InstanceIdentifier<ReleasedIdsHolder> releasedIdsHolderInstanceIdentifier = InstanceIdentifier
510                 .builder(IdPools.class).child(IdPool.class,
511                         new IdPoolKey(parentIdPool.getPoolName())).child(ReleasedIdsHolder.class).build();
512         releasedIdsBuilderParent.setAvailableIdCount(releasedIdsBuilderParent.getAvailableIdCount() - idCount);
513         LOG.debug("Allocated {} ids from releasedIds of parent pool {}", idCount, parentIdPool);
514         confTx.merge(releasedIdsHolderInstanceIdentifier, releasedIdsBuilderParent.build(), CREATE_MISSING_PARENTS);
515         return idCount;
516     }
517
518     private long allocateIdBlockFromAvailableIdsHolder(IdLocalPool localIdPool, IdPool parentIdPool,
519             TypedWriteTransaction<Configuration> confTx) {
520         long idCount = 0;
521         AvailableIdsHolderBuilder availableIdsBuilderParent = idUtils.getAvailableIdsHolderBuilder(parentIdPool);
522         long end = availableIdsBuilderParent.getEnd();
523         long cur = availableIdsBuilderParent.getCursor();
524         if (!idUtils.isIdAvailable(availableIdsBuilderParent)) {
525             if (LOG.isDebugEnabled()) {
526                 LOG.debug("Ids exhausted in parent pool {}", parentIdPool);
527             }
528             return idCount;
529         }
530         // Update availableIdsHolder of Local Pool
531         idCount = Math.min(end - cur, parentIdPool.getBlockSize());
532         AvailableIdHolder availableIds = new AvailableIdHolder(idUtils, cur + 1, cur + idCount);
533         localIdPool.setAvailableIds(availableIds);
534         // Update availableIdsHolder of Global Pool
535         InstanceIdentifier<AvailableIdsHolder> availableIdsHolderInstanceIdentifier = InstanceIdentifier
536                 .builder(IdPools.class).child(IdPool.class,
537                         new IdPoolKey(parentIdPool.getPoolName())).child(AvailableIdsHolder.class).build();
538         availableIdsBuilderParent.setCursor(cur + idCount);
539         if (LOG.isDebugEnabled()) {
540             LOG.debug("Allocated {} ids from availableIds of global pool {}", idCount, parentIdPool);
541         }
542         confTx.merge(availableIdsHolderInstanceIdentifier, availableIdsBuilderParent.build(), CREATE_MISSING_PARENTS);
543         return idCount;
544     }
545
546     private void releaseIdFromLocalPool(String parentPoolName, String localPoolName, String idKey)
547             throws ReadFailedException, IdManagerException {
548         String idLatchKey = idUtils.getUniqueKey(parentPoolName, idKey);
549         LOG.debug("Releasing ID {} from pool {}", idKey, localPoolName);
550         CountDownLatch latch = idUtils.getReleaseIdLatch(idLatchKey);
551         if (latch != null) {
552             try {
553                 if (!latch.await(10, TimeUnit.SECONDS)) {
554                     LOG.warn("Timed out while releasing id {} from id pool {}", idKey, parentPoolName);
555                 }
556             } catch (InterruptedException ignored) {
557                 LOG.warn("Thread interrupted while releasing id {} from id pool {}", idKey, parentPoolName);
558             } finally {
559                 idUtils.removeReleaseIdLatch(idLatchKey);
560             }
561         }
562         localPoolName = localPoolName.intern();
563         InstanceIdentifier<IdPool> parentIdPoolInstanceIdentifier = idUtils.getIdPoolInstance(parentPoolName);
564         IdPool parentIdPool = singleTxDB.syncRead(LogicalDatastoreType.CONFIGURATION, parentIdPoolInstanceIdentifier);
565         List<IdEntries> idEntries = parentIdPool.getIdEntries();
566         if (idEntries == null) {
567             throw new IdDoesNotExistException(parentPoolName, idKey);
568         }
569         InstanceIdentifier<IdEntries> existingId = idUtils.getIdEntry(parentIdPoolInstanceIdentifier, idKey);
570         Optional<IdEntries> existingIdEntryObject =
571                 singleTxDB.syncReadOptional(LogicalDatastoreType.CONFIGURATION, existingId);
572         if (!existingIdEntryObject.isPresent()) {
573             LOG.info("Specified Id key {} does not exist in id pool {}", idKey, parentPoolName);
574             idUtils.unlock(lockManager, idLatchKey);
575             return;
576         }
577         IdEntries existingIdEntry = existingIdEntryObject.get();
578         List<Long> idValuesList = existingIdEntry.getIdValue();
579         IdLocalPool localIdPoolCache = localPool.get(parentPoolName);
580         boolean isRemoved = idEntries.remove(existingIdEntry);
581         LOG.debug("The entry {} is removed {}", existingIdEntry, isRemoved);
582         updateDelayedEntriesInLocalCache(idValuesList, parentPoolName, localIdPoolCache);
583         IdHolderSyncJob poolSyncJob = new IdHolderSyncJob(localPoolName, localIdPoolCache.getReleasedIds(), txRunner,
584                 idUtils);
585         jobCoordinator.enqueueJob(localPoolName, poolSyncJob, IdUtils.RETRY_COUNT);
586         scheduleCleanUpTask(localIdPoolCache, parentPoolName, parentIdPool.getBlockSize());
587         LOG.debug("Released id ({}, {}) from pool {}", idKey, idValuesList, localPoolName);
588         // Updating id entries in the parent pool. This will be used for restart scenario
589         UpdateIdEntryJob job = new UpdateIdEntryJob(parentPoolName, localPoolName, idKey, null, txRunner, idUtils,
590                         lockManager);
591         jobCoordinator.enqueueJob(parentPoolName, job, IdUtils.RETRY_COUNT);
592     }
593
594     private void scheduleCleanUpTask(final IdLocalPool localIdPoolCache,
595             final String parentPoolName, final int blockSize) {
596         TimerTask scheduledTask = new TimerTask() {
597             @Override
598             public void run() {
599                 CleanUpJob job =
600                         new CleanUpJob(localIdPoolCache, txRunner, broker, parentPoolName, blockSize, lockManager,
601                                 idUtils, jobCoordinator);
602                 jobCoordinator.enqueueJob(localIdPoolCache.getPoolName(), job, IdUtils.RETRY_COUNT);
603             }
604         };
605         cleanJobTimer.schedule(scheduledTask, IdUtils.DEFAULT_DELAY_TIME * 1000);
606     }
607
608     private IdPool createGlobalPool(TypedReadWriteTransaction<Configuration> confTx, String poolName, long low,
609             long high, long blockSize) throws IdManagerException {
610         IdPool idPool;
611         InstanceIdentifier<IdPool> idPoolInstanceIdentifier = idUtils.getIdPoolInstance(poolName);
612         try {
613             Optional<IdPool> existingIdPool = confTx.read(idPoolInstanceIdentifier).get();
614             if (!existingIdPool.isPresent()) {
615                 if (LOG.isDebugEnabled()) {
616                     LOG.debug("Creating new global pool {}", poolName);
617                 }
618                 idPool = idUtils.createGlobalPool(poolName, low, high, blockSize);
619                 confTx.put(idPoolInstanceIdentifier, idPool, CREATE_MISSING_PARENTS);
620             } else {
621                 idPool = existingIdPool.get();
622                 if (LOG.isDebugEnabled()) {
623                     LOG.debug("GlobalPool exists {}", idPool);
624                 }
625             }
626             return idPool;
627         } catch (ExecutionException | InterruptedException e) {
628             throw new IdManagerException("Error retrieving the existing id pool for " + poolName, e);
629         }
630     }
631
632     private IdLocalPool createLocalPool(TypedWriteTransaction<Configuration> confTx, String localPoolName,
633             IdPool idPool)
634             throws OperationFailedException, IdManagerException {
635         localPoolName = localPoolName.intern();
636         IdLocalPool idLocalPool = new IdLocalPool(idUtils, localPoolName);
637         allocateIdBlockFromParentPool(idLocalPool, idPool, confTx);
638         String parentPool = idPool.getPoolName();
639         localPool.put(parentPool, idLocalPool);
640         LocalPoolCreateJob job = new LocalPoolCreateJob(idLocalPool, txRunner, idPool.getPoolName(),
641                 idPool.getBlockSize(), idUtils);
642         jobCoordinator.enqueueJob(localPoolName, job, IdUtils.RETRY_COUNT);
643         return idLocalPool;
644     }
645
646     private void deletePool(String poolName) {
647         LocalPoolDeleteJob job = new LocalPoolDeleteJob(poolName, txRunner, idUtils);
648         jobCoordinator.enqueueJob(poolName, job, IdUtils.RETRY_COUNT);
649     }
650
651     public void poolDeleted(String parentPoolName, String poolName) {
652         IdLocalPool idLocalPool = localPool.get(parentPoolName);
653         if (idLocalPool != null) {
654             if (idLocalPool.getPoolName().equals(poolName)) {
655                 localPool.remove(parentPoolName);
656             }
657         }
658     }
659
660     private void updateDelayedEntriesInLocalCache(List<Long> idsList, String parentPoolName,
661             IdLocalPool localPoolCache) {
662         for (long idValue : idsList) {
663             localPoolCache.getReleasedIds().addId(idValue);
664         }
665         localPool.put(parentPoolName, localPoolCache);
666     }
667
668     private List<Long> checkForIdInIdEntries(String parentPoolName, String idKey, String uniqueIdKey,
669             CompletableFuture<List<Long>> futureIdValues, boolean hasExistingFutureIdValues)
670             throws IdManagerException, ReadFailedException {
671         InstanceIdentifier<IdPool> parentIdPoolInstanceIdentifier = idUtils.getIdPoolInstance(parentPoolName);
672         InstanceIdentifier<IdEntries> existingId = idUtils.getIdEntry(parentIdPoolInstanceIdentifier, idKey);
673         idUtils.lock(lockManager, uniqueIdKey);
674         List<Long> newIdValuesList = new ArrayList<>();
675         Optional<IdEntries> existingIdEntry =
676                 singleTxDB.syncReadOptional(LogicalDatastoreType.CONFIGURATION, existingId);
677         if (existingIdEntry.isPresent()) {
678             newIdValuesList = existingIdEntry.get().getIdValue();
679             LOG.debug("Existing ids {} for the key {} ", newIdValuesList, idKey);
680             // Inform other waiting threads about this new value.
681             futureIdValues.complete(newIdValuesList);
682             // This is to avoid stale entries in the map. If this thread had populated the map,
683             // then the entry should be removed.
684             if (!hasExistingFutureIdValues) {
685                 idUtils.removeAllocatedIds(uniqueIdKey);
686             }
687             idUtils.unlock(lockManager, uniqueIdKey);
688             return newIdValuesList;
689         }
690         return newIdValuesList;
691     }
692
693     private IdLocalPool getOrCreateLocalIdPool(String parentPoolName, String localPoolName) throws IdManagerException {
694         IdLocalPool localIdPool = localPool.get(parentPoolName);
695         if (localIdPool == null) {
696             idUtils.lock(lockManager, parentPoolName);
697             try {
698                 // Check if a previous thread that got the cluster-wide lock
699                 // first, has created the localPool
700                 if (localPool.get(parentPoolName) == null) {
701                     try {
702                         return txRunner.applyWithNewReadWriteTransactionAndSubmit(CONFIGURATION, confTx -> {
703                             InstanceIdentifier<IdPool> parentIdPoolInstanceIdentifier = idUtils
704                                     .getIdPoolInstance(parentPoolName);
705                             Optional<IdPool> parentIdPool = confTx.read(parentIdPoolInstanceIdentifier).get();
706                             if (parentIdPool.isPresent()) {
707                                 // Return localIdPool
708                                 return createLocalPool(confTx, localPoolName, parentIdPool.get());
709                             } else {
710                                 throw new ExpectedDataObjectNotFoundException(LogicalDatastoreType.CONFIGURATION,
711                                         parentIdPoolInstanceIdentifier);
712                             }
713                         }).get();
714                     } catch (InterruptedException | ExecutionException e) {
715                         throw new IdManagerException("Error creating a local id pool", e);
716                     }
717                 } else {
718                     localIdPool = localPool.get(parentPoolName);
719                 }
720             } finally {
721                 idUtils.unlock(lockManager, parentPoolName);
722             }
723         }
724         return localIdPool;
725     }
726
727     private void getRangeOfIds(String parentPoolName, String localPoolName, long size, List<Long> newIdValuesList,
728             IdLocalPool localIdPool, long newIdValue) throws ReadFailedException, IdManagerException {
729         InstanceIdentifier<IdPool> parentIdPoolInstanceIdentifier1 = idUtils.getIdPoolInstance(parentPoolName);
730         IdPool parentIdPool = singleTxDB.syncRead(LogicalDatastoreType.CONFIGURATION, parentIdPoolInstanceIdentifier1);
731         long totalAvailableIdCount = localIdPool.getAvailableIds().getAvailableIdCount()
732                 + localIdPool.getReleasedIds().getAvailableIdCount();
733         AvailableIdsHolderBuilder availableParentIds = idUtils.getAvailableIdsHolderBuilder(parentIdPool);
734         ReleasedIdsHolderBuilder releasedParentIds = IdUtils.getReleaseIdsHolderBuilder(parentIdPool);
735         totalAvailableIdCount = totalAvailableIdCount + releasedParentIds.getAvailableIdCount()
736                 + idUtils.getAvailableIdsCount(availableParentIds);
737         if (totalAvailableIdCount > size) {
738             while (size > 0) {
739                 try {
740                     newIdValue = getIdFromLocalPoolCache(localIdPool, parentPoolName);
741                 } catch (IdManagerException e) {
742                     if (LOG.isDebugEnabled()) {
743                         LOG.debug("Releasing IDs to pool {}", localPoolName);
744                     }
745                     // Releasing the IDs added in newIdValuesList since
746                     // a null list would be returned now, as the
747                     // requested size of list IDs exceeds the number of
748                     // available IDs.
749                     updateDelayedEntriesInLocalCache(newIdValuesList, parentPoolName, localIdPool);
750                 }
751                 newIdValuesList.add(newIdValue);
752                 size--;
753             }
754         } else {
755             throw new IdManagerException(String.format("Ids exhausted for pool : %s", parentPoolName));
756         }
757     }
758 }