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 static org.opendaylight.mdsal.common.api.LogicalDatastoreType.CONFIGURATION;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.util.Collections;
14 import java.util.List;
15 import java.util.Optional;
16 import java.util.concurrent.Callable;
17 import java.util.concurrent.ExecutionException;
18 import org.opendaylight.genius.datastoreutils.SingleTransactionDataBroker;
19 import org.opendaylight.genius.idmanager.IdLocalPool;
20 import org.opendaylight.genius.idmanager.IdManagerException;
21 import org.opendaylight.genius.idmanager.IdUtils;
22 import org.opendaylight.genius.idmanager.ReleasedIdHolder;
23 import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
24 import org.opendaylight.infrautils.jobcoordinator.JobCoordinator;
25 import org.opendaylight.mdsal.binding.api.DataBroker;
26 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
27 import org.opendaylight.mdsal.common.api.TransactionCommitFailedException;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.id.pool.ReleasedIdsHolder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.id.pool.ReleasedIdsHolderBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.lockmanager.rev160413.LockManagerService;
31 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 public class CleanUpJob implements Callable<List<? extends ListenableFuture<?>>> {
37 private static final Logger LOG = LoggerFactory.getLogger(CleanUpJob.class);
39 private final IdLocalPool idLocalPool;
40 private final ManagedNewTransactionRunner txRunner;
41 private final DataBroker broker;
42 private final String parentPoolName;
43 private final int blockSize;
44 private final LockManagerService lockManager;
45 private final IdUtils idUtils;
46 private final JobCoordinator jobCoordinator;
48 public CleanUpJob(IdLocalPool idLocalPool, ManagedNewTransactionRunner txRunner, DataBroker broker,
49 String parentPoolName, int blockSize,
50 LockManagerService lockManager, IdUtils idUtils, JobCoordinator jobCoordinator) {
51 this.idLocalPool = idLocalPool;
52 this.txRunner = txRunner;
54 this.parentPoolName = parentPoolName;
55 this.blockSize = blockSize;
56 this.lockManager = lockManager;
57 this.idUtils = idUtils;
58 this.jobCoordinator = jobCoordinator;
62 public List<ListenableFuture<Void>> call() throws Exception {
64 return Collections.emptyList();
67 private void cleanupExcessIds()
68 throws IdManagerException, TransactionCommitFailedException, ExecutionException , InterruptedException {
69 // We can update the availableCount here... and update it in DS using IdHolderSyncJob
70 long totalAvailableIdCount = idLocalPool.getAvailableIds().getAvailableIdCount()
71 + idLocalPool.getReleasedIds().getAvailableIdCount();
72 if (totalAvailableIdCount > blockSize * 2) {
73 if (LOG.isDebugEnabled()) {
74 LOG.debug("Condition for cleanUp Satisfied for localPool {} - totalAvailableIdCount {}",
75 idLocalPool, totalAvailableIdCount);
77 String parentPoolNameIntern = parentPoolName.intern();
78 InstanceIdentifier<ReleasedIdsHolder> releasedIdInstanceIdentifier
79 = idUtils.getReleasedIdsHolderInstance(parentPoolNameIntern);
80 // We need lock manager because maybe one cluster tries to read the
81 // available ids from the global pool while the other is writing. We
82 // cannot rely on DSJC because that is not cluster-aware
84 idUtils.lock(lockManager, parentPoolNameIntern);
85 Optional<ReleasedIdsHolder> releasedIdsHolder = SingleTransactionDataBroker.syncReadOptional(broker,
86 CONFIGURATION, releasedIdInstanceIdentifier);
87 if (!releasedIdsHolder.isPresent()) {
88 LOG.error("ReleasedIds not present in parent pool. Unable to cleanup excess ids");
91 if (LOG.isDebugEnabled()) {
92 LOG.debug("Releasing excesss Ids from local pool");
94 ReleasedIdHolder releasedIds = (ReleasedIdHolder) idLocalPool.getReleasedIds();
95 ReleasedIdsHolderBuilder releasedIdsParent = new ReleasedIdsHolderBuilder(releasedIdsHolder.get());
96 idUtils.freeExcessAvailableIds(releasedIds, releasedIdsParent, totalAvailableIdCount - blockSize * 2);
97 IdHolderSyncJob job = new IdHolderSyncJob(idLocalPool.getPoolName(), releasedIds, txRunner, idUtils);
98 jobCoordinator.enqueueJob(idLocalPool.getPoolName(), job, IdUtils.RETRY_COUNT);
99 SingleTransactionDataBroker.syncWrite(broker, LogicalDatastoreType.CONFIGURATION,
100 releasedIdInstanceIdentifier, releasedIdsParent.build());
102 idUtils.unlock(lockManager, parentPoolNameIntern);