Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1 | /** |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 2 | * Copyright 2011, Big Switch Networks, Inc. |
| 3 | * Originally created by David Erickson, Stanford University |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | * not use this file except in compliance with the License. You may obtain |
| 7 | * a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | * License for the specific language governing permissions and limitations |
| 15 | * under the License. |
| 16 | **/ |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 17 | |
| 18 | package net.floodlightcontroller.core.util; |
| 19 | |
| 20 | import java.util.concurrent.ScheduledExecutorService; |
| 21 | import java.util.concurrent.TimeUnit; |
| 22 | |
| 23 | import net.floodlightcontroller.core.annotations.LogMessageDoc; |
| 24 | |
| 25 | import org.slf4j.Logger; |
| 26 | import org.slf4j.LoggerFactory; |
| 27 | |
| 28 | /** |
| 29 | * This allows you to represent a task that should be queued for future execution |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 30 | * but where you only want the task to complete once in response to some sequence |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 31 | * of events. For example, if you get a change notification and want to reload state, |
| 32 | * you only want to reload the state once, at the end, and don't want to queue |
| 33 | * an update for every notification that might come in. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 34 | * <p/> |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 35 | * The semantics are as follows: |
| 36 | * * If the task hasn't begun yet, do not queue a new task |
| 37 | * * If the task has begun, set a bit to restart it after the current task finishes |
| 38 | */ |
| 39 | public class SingletonTask { |
Yuta HIGUCHI | 6ac8d18 | 2013-10-22 15:24:56 -0700 | [diff] [blame] | 40 | protected final static Logger logger = LoggerFactory.getLogger(SingletonTask.class); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 41 | |
| 42 | protected static class SingletonTaskContext { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 43 | protected boolean taskShouldRun = false; |
| 44 | protected boolean taskRunning = false; |
| 45 | |
| 46 | protected SingletonTaskWorker waitingTask = null; |
| 47 | } |
| 48 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 49 | protected static class SingletonTaskWorker implements Runnable { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 50 | SingletonTask parent; |
| 51 | boolean canceled = false; |
| 52 | long nextschedule = 0; |
| 53 | |
| 54 | public SingletonTaskWorker(SingletonTask parent) { |
| 55 | super(); |
| 56 | this.parent = parent; |
| 57 | } |
| 58 | |
| 59 | @Override |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 60 | @LogMessageDoc(level = "ERROR", |
| 61 | message = "Exception while executing task", |
| 62 | recommendation = LogMessageDoc.GENERIC_ACTION) |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 63 | public void run() { |
| 64 | synchronized (parent.context) { |
| 65 | if (canceled || !parent.context.taskShouldRun) |
| 66 | return; |
| 67 | |
| 68 | parent.context.taskRunning = true; |
| 69 | parent.context.taskShouldRun = false; |
| 70 | } |
| 71 | |
| 72 | try { |
| 73 | parent.task.run(); |
| 74 | } catch (Exception e) { |
| 75 | logger.error("Exception while executing task", e); |
| 76 | } |
| 77 | |
| 78 | synchronized (parent.context) { |
| 79 | parent.context.taskRunning = false; |
| 80 | |
| 81 | if (parent.context.taskShouldRun) { |
| 82 | long now = System.nanoTime(); |
| 83 | if ((nextschedule <= 0 || (nextschedule - now) <= 0)) { |
| 84 | parent.ses.execute(this); |
| 85 | } else { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 86 | parent.ses.schedule(this, |
| 87 | nextschedule - now, |
| 88 | TimeUnit.NANOSECONDS); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | protected SingletonTaskContext context = new SingletonTaskContext(); |
| 96 | protected Runnable task; |
| 97 | protected ScheduledExecutorService ses; |
| 98 | |
| 99 | |
| 100 | /** |
| 101 | * Construct a new SingletonTask for the given runnable. The context |
| 102 | * is used to manage the state of the task execution and can be shared |
| 103 | * by more than one instance of the runnable. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 104 | * |
Sho SHIMIZU | a1199fa | 2014-06-10 18:11:12 -0700 | [diff] [blame] | 105 | * @param ses |
| 106 | * @param task |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 107 | */ |
| 108 | public SingletonTask(ScheduledExecutorService ses, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 109 | Runnable task) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 110 | super(); |
| 111 | this.task = task; |
| 112 | this.ses = ses; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Schedule the task to run if there's not already a task scheduled |
| 117 | * If there is such a task waiting that has not already started, it |
| 118 | * cancel that task and reschedule it to run at the given time. If the |
| 119 | * task is already started, it will cause the task to be rescheduled once |
| 120 | * it completes to run after delay from the time of reschedule. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 121 | * |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 122 | * @param delay the delay in scheduling |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 123 | * @param unit the timeunit of the delay |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 124 | */ |
| 125 | public void reschedule(long delay, TimeUnit unit) { |
| 126 | boolean needQueue = true; |
| 127 | SingletonTaskWorker stw = null; |
| 128 | |
| 129 | synchronized (context) { |
| 130 | if (context.taskRunning || context.taskShouldRun) { |
| 131 | if (context.taskRunning) { |
| 132 | // schedule to restart at the right time |
| 133 | if (delay > 0) { |
| 134 | long now = System.nanoTime(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 135 | long then = |
| 136 | now + TimeUnit.NANOSECONDS.convert(delay, unit); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 137 | context.waitingTask.nextschedule = then; |
| 138 | } else { |
| 139 | context.waitingTask.nextschedule = 0; |
| 140 | } |
| 141 | needQueue = false; |
| 142 | } else { |
| 143 | // cancel and requeue |
| 144 | context.waitingTask.canceled = true; |
| 145 | context.waitingTask = null; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | context.taskShouldRun = true; |
| 150 | |
| 151 | if (needQueue) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 152 | stw = context.waitingTask = new SingletonTaskWorker(this); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
| 156 | if (needQueue) { |
| 157 | if (delay <= 0) |
| 158 | ses.execute(stw); |
| 159 | else |
| 160 | ses.schedule(stw, delay, unit); |
| 161 | } |
| 162 | } |
Sho SHIMIZU | a1199fa | 2014-06-10 18:11:12 -0700 | [diff] [blame] | 163 | } |