2 * Copyright (c) 2016 Ericsson India Global Services Pvt Ltd. and others. All rights reserved.
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
8 package org.opendaylight.genius.idmanager.jobs;
10 import java.util.ArrayList;
11 import java.util.List;
12 import java.util.concurrent.Callable;
14 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
15 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
16 import org.opendaylight.genius.datastoreutils.DataStoreJobCoordinator;
17 import org.opendaylight.genius.idmanager.IdLocalPool;
18 import org.opendaylight.genius.idmanager.IdUtils;
19 import org.opendaylight.genius.idmanager.ReleasedIdHolder;
20 import org.opendaylight.genius.mdsalutil.MDSALUtil;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.id.pool.ReleasedIdsHolder;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.id.pool.ReleasedIdsHolderBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.lockmanager.rev160413.LockManagerService;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 import com.google.common.base.Optional;
29 import com.google.common.util.concurrent.ListenableFuture;
31 public class CleanUpJob implements Callable<List<ListenableFuture<Void>>> {
33 private static final Logger LOG = LoggerFactory.getLogger(CleanUpJob.class);
35 private final IdLocalPool idLocalPool;
36 private final DataBroker broker;
37 private final String parentPoolName;
38 private final int blockSize;
39 private final LockManagerService lockManager;
41 public CleanUpJob(IdLocalPool idLocalPool, DataBroker broker,
42 String parentPoolName, int blockSize, LockManagerService lockManager) {
44 this.idLocalPool = idLocalPool;
46 this.parentPoolName = parentPoolName;
47 this.blockSize = blockSize;
48 this.lockManager = lockManager;
52 public List<ListenableFuture<Void>> call() throws Exception {
53 List<ListenableFuture<Void>> futures = new ArrayList<>();
54 cleanupExcessIds(parentPoolName, blockSize, idLocalPool);
58 private void cleanupExcessIds(String parentPoolName, int blockSize, IdLocalPool idLocalPool) {
59 // We can update the availableCount here... and update it in DS using IdHolderSyncJob
60 long totalAvailableIdCount = idLocalPool.getAvailableIds().getAvailableIdCount() + idLocalPool.getReleasedIds().getAvailableIdCount();
61 if (totalAvailableIdCount > blockSize * 2) {
62 if (LOG.isDebugEnabled()) {
63 LOG.debug("Condition for cleanUp Satisfied for localPool {} - totalAvailableIdCount {}", idLocalPool, totalAvailableIdCount);
65 parentPoolName = parentPoolName.intern();
66 InstanceIdentifier<ReleasedIdsHolder> releasedIdInstanceIdentifier = IdUtils.getReleasedIdsHolderInstance(parentPoolName);
67 // We need lock manager because maybe one cluster tries to read the available ids from the global pool while the other is writing. We cannot rely on DSJC
68 // because that is not cluster-aware
69 IdUtils.lockPool(lockManager, parentPoolName);
71 Optional<ReleasedIdsHolder> releasedIdsHolder = MDSALUtil.read(broker, LogicalDatastoreType.CONFIGURATION, releasedIdInstanceIdentifier);
72 ReleasedIdsHolderBuilder releasedIdsParent;
73 if (!releasedIdsHolder.isPresent()) {
74 LOG.error("ReleasedIds not present in parent pool. Unable to cleanup excess ids");
77 releasedIdsParent = new ReleasedIdsHolderBuilder(releasedIdsHolder.get());
78 if (LOG.isDebugEnabled()) {
79 LOG.debug("Releasing excesss Ids from local pool");
81 ReleasedIdHolder releasedIds = (ReleasedIdHolder) idLocalPool.getReleasedIds();
82 IdUtils.freeExcessAvailableIds(releasedIds, releasedIdsParent, totalAvailableIdCount - blockSize * 2);
83 IdHolderSyncJob job = new IdHolderSyncJob(idLocalPool.getPoolName(), releasedIds, broker);
84 DataStoreJobCoordinator.getInstance().enqueueJob(idLocalPool.getPoolName(), job, IdUtils.RETRY_COUNT);
85 MDSALUtil.syncWrite(broker, LogicalDatastoreType.CONFIGURATION, releasedIdInstanceIdentifier, releasedIdsParent.build());
87 IdUtils.unlockPool(lockManager, parentPoolName);