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
9 package org.opendaylight.genius.datastoreutils;
11 import com.google.common.util.concurrent.FutureCallback;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import java.util.Iterator;
15 import java.util.List;
17 import java.util.concurrent.Callable;
18 import java.util.concurrent.ConcurrentHashMap;
19 import java.util.concurrent.Executors;
20 import java.util.concurrent.ForkJoinPool;
21 import java.util.concurrent.ScheduledExecutorService;
22 import java.util.concurrent.TimeUnit;
23 import java.util.concurrent.locks.Condition;
24 import java.util.concurrent.locks.ReentrantLock;
25 import org.opendaylight.genius.infra.LoggingThreadUncaughtExceptionHandler;
26 import org.opendaylight.genius.infra.ThreadFactoryProvider;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 public class DataStoreJobCoordinator {
32 private static final Logger LOG = LoggerFactory.getLogger(DataStoreJobCoordinator.class);
34 private static final int THREADPOOL_SIZE = Runtime.getRuntime().availableProcessors();
35 private static final long RETRY_WAIT_BASE_TIME = 100;
37 // package local instead of private for TestDataStoreJobCoordinator
38 final ForkJoinPool fjPool;
39 final Map<Integer, Map<String, JobQueue>> jobQueueMap = new ConcurrentHashMap<>();
41 private final ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(5);
42 private final ReentrantLock reentrantLock = new ReentrantLock();
43 private final Condition waitCondition = reentrantLock.newCondition();
45 private static DataStoreJobCoordinator instance;
48 instance = new DataStoreJobCoordinator();
51 public static DataStoreJobCoordinator getInstance() {
55 private DataStoreJobCoordinator() {
56 fjPool = new ForkJoinPool(
57 Math.min(/* MAX_CAP */ 0x7fff, Runtime.getRuntime().availableProcessors()),
58 ForkJoinPool.defaultForkJoinWorkerThreadFactory,
59 LoggingThreadUncaughtExceptionHandler.toLOG(LOG),
62 for (int i = 0; i < THREADPOOL_SIZE; i++) {
63 Map<String, JobQueue> jobEntriesMap = new ConcurrentHashMap<>();
64 jobQueueMap.put(i, jobEntriesMap);
67 ThreadFactoryProvider.builder()
68 .namePrefix("DataStoreJobCoordinator-JobQueueHandler")
71 .newThread(new JobQueueHandler()).start();
74 public void enqueueJob(String key, Callable<List<ListenableFuture<Void>>> mainWorker) {
75 enqueueJob(key, mainWorker, null, 0);
78 public void enqueueJob(String key, Callable<List<ListenableFuture<Void>>> mainWorker,
79 RollbackCallable rollbackWorker) {
80 enqueueJob(key, mainWorker, rollbackWorker, 0);
83 public void enqueueJob(String key, Callable<List<ListenableFuture<Void>>> mainWorker, int maxRetries) {
84 enqueueJob(key, mainWorker, null, maxRetries);
87 public void enqueueJob(AbstractDataStoreJob job) throws InvalidJobException {
89 enqueueJob(job.getJobQueueKey(), job);
93 * This is used by the external applications to enqueue a Job
94 * with an appropriate key. A JobEntry is created and queued
97 public void enqueueJob(String key, Callable<List<ListenableFuture<Void>>> mainWorker,
98 RollbackCallable rollbackWorker, int maxRetries) {
99 JobEntry jobEntry = new JobEntry(key, mainWorker, rollbackWorker, maxRetries);
100 Integer hashKey = getHashKey(key);
101 LOG.debug("Obtained Hashkey: {}, for jobkey: {}", hashKey, key);
103 Map<String, JobQueue> jobEntriesMap = jobQueueMap.get(hashKey);
104 synchronized (jobEntriesMap) {
105 JobQueue jobQueue = jobEntriesMap.get(key);
106 if (jobQueue == null) {
107 jobQueue = new JobQueue();
109 LOG.trace("Adding jobkey {} to queue {} with size {}", key, hashKey, jobEntriesMap.size());
110 jobQueue.addEntry(jobEntry);
111 jobEntriesMap.put(key, jobQueue);
113 DataStoreJobCoordinatorCounters.jobs_pending.inc();
114 DataStoreJobCoordinatorCounters.jobs_incomplete.inc();
115 DataStoreJobCoordinatorCounters.jobs_created.inc();
117 reentrantLock.lock();
119 waitCondition.signal();
121 reentrantLock.unlock();
125 public long getIncompleteTaskCount() {
126 return DataStoreJobCoordinatorCounters.jobs_incomplete.get();
130 * Cleanup the submitted job from the job queue.
132 private void clearJob(JobEntry jobEntry) {
133 Integer hashKey = getHashKey(jobEntry.getKey());
134 Map<String, JobQueue> jobEntriesMap = jobQueueMap.get(hashKey);
135 LOG.trace("About to clear jobkey {} from queue {}", jobEntry.getKey(), hashKey);
136 synchronized (jobEntriesMap) {
137 JobQueue jobQueue = jobEntriesMap.get(jobEntry.getKey());
138 jobQueue.setExecutingEntry(null);
139 if (jobQueue.getWaitingEntries().isEmpty()) {
140 LOG.trace("Clear jobkey {} from queue {}", jobEntry.getKey(), hashKey);
141 jobEntriesMap.remove(jobEntry.getKey());
144 DataStoreJobCoordinatorCounters.jobs_cleared.inc();
145 DataStoreJobCoordinatorCounters.jobs_incomplete.dec();
149 * Used to generate the hashkey in to the jobQueueMap.
151 private Integer getHashKey(String key) {
152 int code = key.hashCode();
153 return (code % THREADPOOL_SIZE + THREADPOOL_SIZE) % THREADPOOL_SIZE;
157 * JobCallback class is used as a future callback for main and rollback
158 * workers to handle success and failure.
160 private class JobCallback implements FutureCallback<List<Void>> {
161 private final JobEntry jobEntry;
163 JobCallback(JobEntry jobEntry) {
164 this.jobEntry = jobEntry;
168 * This implies that all the future instances have returned
169 * success. -- TODO: Confirm this
172 public void onSuccess(List<Void> voids) {
173 LOG.trace("Job {} completed successfully", jobEntry.getKey());
178 * This method is used to handle failure callbacks. If more
179 * retry needed, the retrycount is decremented and mainworker
180 * is executed again. After retries completed, rollbackworker
181 * is executed. If rollbackworker fails, this is a
182 * double-fault. Double fault is logged and ignored.
185 public void onFailure(Throwable throwable) {
186 LOG.warn("Job: {} failed", jobEntry, throwable);
187 if (jobEntry.getMainWorker() == null) {
188 LOG.error("Job: {} failed with Double-Fault. Bailing Out.", jobEntry);
193 int retryCount = jobEntry.decrementRetryCountAndGet();
194 if (retryCount > 0) {
195 long waitTime = RETRY_WAIT_BASE_TIME * 10 / retryCount;
196 scheduledExecutorService.schedule(() -> {
197 MainTask worker = new MainTask(jobEntry);
198 fjPool.execute(worker);
199 }, waitTime, TimeUnit.MILLISECONDS);
203 if (jobEntry.getRollbackWorker() != null) {
204 jobEntry.setMainWorker(null);
205 RollbackTask rollbackTask = new RollbackTask(jobEntry);
206 fjPool.execute(rollbackTask);
215 * RollbackTask is used to execute the RollbackCallable provided by the
216 * application in the eventuality of a failure.
218 private class RollbackTask implements Runnable {
219 private final JobEntry jobEntry;
221 RollbackTask(JobEntry jobEntry) {
222 this.jobEntry = jobEntry;
226 @SuppressWarnings("checkstyle:IllegalCatch")
228 RollbackCallable callable = jobEntry.getRollbackWorker();
229 callable.setFutures(jobEntry.getFutures());
230 List<ListenableFuture<Void>> futures = null;
233 futures = callable.call();
234 } catch (Exception e) {
235 LOG.error("Exception when executing jobEntry: {}", jobEntry, e);
238 if (futures == null || futures.isEmpty()) {
243 ListenableFuture<List<Void>> listenableFuture = Futures.allAsList(futures);
244 Futures.addCallback(listenableFuture, new JobCallback(jobEntry));
245 jobEntry.setFutures(futures);
250 * Execute the MainWorker callable.
252 private class MainTask implements Runnable {
253 private static final int LONG_JOBS_THRESHOLD = 1000; // MS
254 private final JobEntry jobEntry;
256 MainTask(JobEntry jobEntry) {
257 this.jobEntry = jobEntry;
261 @SuppressWarnings("checkstyle:illegalcatch")
263 List<ListenableFuture<Void>> futures = null;
264 long jobStartTimestamp = System.currentTimeMillis();
265 LOG.trace("Running job {}", jobEntry.getKey());
268 futures = jobEntry.getMainWorker().call();
269 long jobExecutionTime = System.currentTimeMillis() - jobStartTimestamp;
270 printJobs(jobEntry.getKey(), jobExecutionTime);
271 } catch (Exception e) {
272 LOG.error("Exception when executing jobEntry: {}", jobEntry, e);
275 if (futures == null || futures.isEmpty()) {
280 ListenableFuture<List<Void>> listenableFuture = Futures.allAsList(futures);
281 Futures.addCallback(listenableFuture, new JobCallback(jobEntry));
282 jobEntry.setFutures(futures);
285 private void printJobs(String key, long jobExecutionTime) {
286 if (jobExecutionTime > LONG_JOBS_THRESHOLD) {
287 LOG.warn("Job {} took {}ms to complete", jobEntry.getKey(), jobExecutionTime);
290 LOG.trace("Job {} took {}ms to complete", jobEntry.getKey(), jobExecutionTime);
294 private class JobQueueHandler implements Runnable {
296 @SuppressWarnings("checkstyle:illegalcatch")
298 LOG.info("Starting JobQueue Handler Thread with pool size {}", THREADPOOL_SIZE);
301 for (int i = 0; i < THREADPOOL_SIZE; i++) {
302 Map<String, JobQueue> jobEntriesMap = jobQueueMap.get(i);
303 if (jobEntriesMap.isEmpty()) {
306 LOG.trace("JobQueueHandler handling queue {} with kesy size {}. Keys: {} ", i,
307 jobEntriesMap.size(), jobEntriesMap.keySet());
309 synchronized (jobEntriesMap) {
310 Iterator<Map.Entry<String, JobQueue>> it = jobEntriesMap.entrySet().iterator();
311 while (it.hasNext()) {
312 Map.Entry<String, JobQueue> entry = it.next();
313 if (entry.getValue().getExecutingEntry() != null) {
316 JobEntry jobEntry = entry.getValue().getWaitingEntries().poll();
317 if (jobEntry != null) {
318 entry.getValue().setExecutingEntry(jobEntry);
319 MainTask worker = new MainTask(jobEntry);
320 LOG.trace("Executing job {} from queue {}", jobEntry.getKey(), i);
321 fjPool.execute(worker);
322 DataStoreJobCoordinatorCounters.jobs_pending.dec();
331 reentrantLock.lock();
333 if (isJobQueueEmpty()) {
334 waitCondition.await();
337 reentrantLock.unlock();
339 } catch (Exception e) {
340 LOG.error("Exception while executing the tasks", e);
341 } catch (Throwable e) {
342 LOG.error("Error while executing the tasks", e);
348 private boolean isJobQueueEmpty() {
349 for (int i = 0; i < THREADPOOL_SIZE; i++) {
350 Map<String, JobQueue> jobEntriesMap = jobQueueMap.get(i);
351 if (!jobEntriesMap.isEmpty()) {