16
16
17
17
package com .netflix .fenzo ;
18
18
19
+ import com .netflix .fenzo .common .ThreadFactoryBuilder ;
19
20
import com .netflix .fenzo .plugins .NoOpScaleDownOrderEvaluator ;
20
21
import com .netflix .fenzo .queues .Assignable ;
21
22
import com .netflix .fenzo .queues .QueuableTask ;
34
35
import java .util .concurrent .ExecutorService ;
35
36
import java .util .concurrent .Executors ;
36
37
import java .util .concurrent .Future ;
38
+ import java .util .concurrent .ThreadFactory ;
37
39
import java .util .concurrent .atomic .AtomicBoolean ;
38
40
import java .util .concurrent .atomic .AtomicInteger ;
41
+ import java .util .function .Supplier ;
39
42
import java .util .stream .Collectors ;
40
43
41
44
/**
@@ -103,6 +106,7 @@ public Boolean call(Double f) {
103
106
private boolean singleOfferMode =false ;
104
107
private final List <SchedulingEventListener > schedulingEventListeners = new ArrayList <>();
105
108
private int maxConcurrent = Runtime .getRuntime ().availableProcessors ();
109
+ private Supplier <Long > taskBatchSizeSupplier = () -> Long .MAX_VALUE ;
106
110
107
111
/**
108
112
* (Required) Call this method to establish a method that your task scheduler will call to notify you
@@ -442,6 +446,20 @@ public Builder withMaxConcurrent(int maxConcurrent) {
442
446
return this ;
443
447
}
444
448
449
+ /**
450
+ * Use the given supplier to determine how many successful tasks should be evaluated in the next scheduling iteration. This
451
+ * can be used to dynamically change how many successful task evaluations are done in order to increase/reduce the scheduling iteration
452
+ * duration. The default supplier implementation will return {@link Long#MAX_VALUE} such that all tasks will be
453
+ * evaluated.
454
+ *
455
+ * @param taskBatchSizeSupplier the supplier that returns the task batch size for the next scheduling iteration.
456
+ * @return this same {@code Builder}, suitable for further chaining or to build the {@link TaskSchedulingService}.
457
+ */
458
+ public Builder withTaskBatchSizeSupplier (Supplier <Long > taskBatchSizeSupplier ) {
459
+ this .taskBatchSizeSupplier = taskBatchSizeSupplier ;
460
+ return this ;
461
+ }
462
+
445
463
/**
446
464
* Creates a {@link TaskScheduler} based on the various builder methods you have chained.
447
465
*
@@ -497,7 +515,8 @@ private TaskScheduler(Builder builder) {
497
515
throw new IllegalArgumentException ("Lease reject action must be non-null" );
498
516
this .builder = builder ;
499
517
this .maxConcurrent = builder .maxConcurrent ;
500
- this .executorService = Executors .newFixedThreadPool (maxConcurrent );
518
+ ThreadFactory threadFactory = ThreadFactoryBuilder .newBuilder ().withNameFormat ("fenzo-worker-%d" ).build ();
519
+ this .executorService = Executors .newFixedThreadPool (maxConcurrent , threadFactory );
501
520
this .stateMonitor = new StateMonitor ();
502
521
this .schedulingEventListener = CompositeSchedulingEventListener .of (builder .schedulingEventListeners );
503
522
taskTracker = new TaskTracker ();
@@ -795,6 +814,8 @@ private SchedulingResult doSchedule(
795
814
Set <TaskRequest > failedTasksForAutoScaler = new HashSet <>();
796
815
Map <String , VMAssignmentResult > resultMap = new HashMap <>(avms .size ());
797
816
final SchedulingResult schedulingResult = new SchedulingResult (resultMap );
817
+ long taskBatchSize = builder .taskBatchSizeSupplier .get ();
818
+ long tasksIterationCount = 0 ;
798
819
if (avms .isEmpty ()) {
799
820
while (true ) {
800
821
final Assignable <? extends TaskRequest > taskOrFailure = taskIterator .next ();
@@ -806,6 +827,9 @@ private SchedulingResult doSchedule(
806
827
schedulingEventListener .onScheduleStart ();
807
828
try {
808
829
while (true ) {
830
+ if (tasksIterationCount >= taskBatchSize ) {
831
+ break ;
832
+ }
809
833
final Assignable <? extends TaskRequest > taskOrFailure = taskIterator .next ();
810
834
if (logger .isDebugEnabled ())
811
835
logger .debug ("TaskSched: task=" + (taskOrFailure == null ? "null" : taskOrFailure .getTask ().getId ()));
@@ -904,6 +928,7 @@ public EvalResult call() throws Exception {
904
928
logger .debug ("Task {}: found successful assignment on host {}" , task .getId (),
905
929
successfulResult .getHostname ());
906
930
successfulResult .assignResult ();
931
+ tasksIterationCount ++;
907
932
failedTasksForAutoScaler .remove (task );
908
933
schedulingEventListener .onAssignment (successfulResult );
909
934
}
0 commit comments