1919import com .netflix .fenzo .functions .Action0 ;
2020import com .netflix .fenzo .functions .Action1 ;
2121import com .netflix .fenzo .queues .*;
22+ import com .netflix .fenzo .queues .TaskQueue ;
2223import org .slf4j .Logger ;
2324import org .slf4j .LoggerFactory ;
2425
4950 * assigned from within this scheduling service. This service assigns the tasks before making the result
5051 * available to you via the callback. To mark tasks as running for those tasks that were running from
5152 * before this service was created, use {@link #initializeRunningTask(QueuableTask, String)}. Later, call
52- * {@link #removeTask(QueuableTask , String)} when tasks complete or they no longer need resource assignments.
53+ * {@link #removeTask(String, QAttributes , String)} when tasks complete or they no longer need resource assignments.
5354 * </LI>
5455 * </UL>
5556 */
5657public class TaskSchedulingService {
58+
59+ private static class RemoveTaskRequest {
60+ private final String taskId ;
61+ private final QAttributes qAttributes ;
62+ private final String hostname ;
63+
64+ public RemoveTaskRequest (String taskId , QAttributes qAttributes , String hostname ) {
65+ this .taskId = taskId ;
66+ this .qAttributes = qAttributes ;
67+ this .hostname = hostname ;
68+ }
69+ }
70+
5771 private static final Logger logger = LoggerFactory .getLogger (TaskSchedulingService .class );
5872 private final TaskScheduler taskScheduler ;
5973 private final Action1 <SchedulingResult > schedulingResultCallback ;
@@ -63,7 +77,7 @@ public class TaskSchedulingService {
6377 private final Action0 preHook ;
6478 private final BlockingQueue <VirtualMachineLease > leaseBlockingQueue = new LinkedBlockingQueue <>();
6579 private final BlockingQueue <Map <String , QueuableTask >> addRunningTasksQueue = new LinkedBlockingQueue <>();
66- private final BlockingQueue <Map < QueuableTask , String > > removeTasksQueue = new LinkedBlockingQueue <>();
80+ private final BlockingQueue <RemoveTaskRequest > removeTasksQueue = new LinkedBlockingQueue <>();
6781 private final BlockingQueue <Action1 <Map <TaskQueue .TaskState , Collection <QueuableTask >>>> taskMapRequest = new LinkedBlockingQueue <>(10 );
6882 private final BlockingQueue <Action1 <Map <String , Map <VMResource , Double []>>>> resStatusRequest = new LinkedBlockingQueue <>(10 );
6983 private final BlockingQueue <Action1 <List <VirtualMachineCurrentState >>> vmCurrStateRequest = new LinkedBlockingQueue <>(10 );
@@ -161,20 +175,18 @@ private void addPendingRunningTasks() {
161175
162176 private void removeTasks () {
163177 if (removeTasksQueue .peek () != null ) {
164- List <Map <QueuableTask , String >> r = new LinkedList <>();
165- removeTasksQueue .drainTo (r );
166- for (Map <QueuableTask , String > m : r ) {
167- for (Map .Entry <QueuableTask , String > e : m .entrySet ()) {
168- // remove it from the queue and call taskScheduler to unassign it if hostname is not null
169- try {
170- taskQueue .getUsageTracker ().removeTask (e .getKey ().getId (), e .getKey ().getQAttributes ());
171- } catch (TaskQueueException e1 ) {
172- // shouldn't happen since we're calling outside of scheduling iteration
173- logger .warn ("Unexpected to get exception outside of scheduling iteration: " + e1 .getMessage (), e1 );
174- }
175- if (e .getValue () != null )
176- taskScheduler .getTaskUnAssigner ().call (e .getKey ().getId (), e .getValue ());
178+ List <RemoveTaskRequest > l = new LinkedList <>();
179+ removeTasksQueue .drainTo (l );
180+ for (RemoveTaskRequest r : l ) {
181+ // remove it from the queue and call taskScheduler to unassign it if hostname is not null
182+ try {
183+ taskQueue .getUsageTracker ().removeTask (r .taskId , r .qAttributes );
184+ } catch (TaskQueueException e1 ) {
185+ // shouldn't happen since we're calling outside of scheduling iteration
186+ logger .warn ("Unexpected to get exception outside of scheduling iteration: " + e1 .getMessage (), e1 );
177187 }
188+ if (r .hostname != null )
189+ taskScheduler .getTaskUnAssigner ().call (r .taskId , r .hostname );
178190 }
179191 }
180192 }
@@ -288,12 +300,13 @@ public void initializeRunningTask(QueuableTask task, String hostname) {
288300 * not the task is already running. If the task is running, the <code>hostname</code> parameter must be set, otherwise,
289301 * it can be <code>null</code>. The actual remove operation is performed before the start of the next scheduling
290302 * iteration.
291- * @param task The task to be removed.
303+ * @param taskId The Id of the task to be removed.
304+ * @param qAttributes The queue attributes of the queue that the task belongs to
292305 * @param hostname The name of the VM where the task was assigned resources from, or, <code>null</code> if it was
293306 * not assigned any resources.
294307 */
295- public void removeTask (QueuableTask task , String hostname ) {
296- removeTasksQueue .offer (Collections . singletonMap ( task , hostname ));
308+ public void removeTask (String taskId , QAttributes qAttributes , String hostname ) {
309+ removeTasksQueue .offer (new RemoveTaskRequest ( taskId , qAttributes , hostname ));
297310 }
298311
299312 public final static class Builder {
0 commit comments