Geoffrey De Smet Bin packing with Drools Planner Scheduling processes on cloud servers
Every organization has planning problems.
What is a planning problem? <ul><li>Complete goals
With limited resources
Under constraints </li></ul>
Hospital bed scheduling <ul><li>Assign each </li><ul><li>Patient </li></ul><li>To </li><ul><li>Bed </li></ul><li>Constrain...
Room requirements
Room preferences </li></ul></ul>http://www.flickr.com/photos/markhillary/2227726759/
Hospital nurse rostering <ul><li>Assign each </li><ul><li>Shift </li></ul><li>To </li><ul><li>Nurse </li></ul><li>Constrai...
Free time preferences
Skills </li></ul></ul>http://www.flickr.com/photos/glenpooh/709704564/
School timetabling <ul><li>Assign each </li><ul><li>Course </li></ul><li>To </li><ul><li>Room
Timeslot </li></ul><li>Constraints </li><ul><li>No simultaneous </li><ul><li>Per room
Per teacher
Per student </li></ul></ul></ul>http://www.flickr.com/photos/phelyan/2281095105/
Car factory order scheduling <ul><li>Assign each </li><ul><li>Car order </li></ul><li>To </li><ul><li>Assembly line </li><...
Airline routing <ul><li>Assign each </li><ul><li>Flight </li></ul><li>To </li><ul><li>Airplane
Crew </li></ul><li>Constraints </li><ul><li>Airplane/crew depart from where they arrive
Minimize mileage </li></ul></ul>http://www.flickr.com/photos/yorickr/3674349657/
Bin packing in the cloud <ul><li>Assign each </li><ul><li>Process </li></ul><li>To </li><ul><li>Server </li></ul><li>Const...
Minimize server cost </li></ul></ul>http://www.flickr.com/photos/torkildr/3462607995/
Bin packing in the cloud <ul><li>Assign each </li><ul><li>Process </li></ul><li>To </li><ul><li>Server </li></ul><li>Const...
Minimize server cost </li></ul></ul>http://www.flickr.com/photos/torkildr/3462607995/
Which processes do we assign to this server?
How did we find that solution?
First fit by decreasing size
First fit by decreasing size
First fit by decreasing size
First fit by decreasing size
Another case
Try FFD again
Try FFD again
Try FFD again
Try FFD again
FFD failure
NP complete
NP complete <ul><li>No silver bullet known </li><ul><li>Holy grail of computer science (P == NP)
Probably does not exist (P != NP) </li></ul><li>Root problem of all planning problems </li></ul>http://www.flickr.com/phot...
Organizations rarely optimize planning problems.
Multiple servers...
Multiple servers...
Multiple servers...
Multiple servers...
Multiple constraints...
Multiple constraints...
Multiple constraints...
Multiple constraints...
Reuse optimization algorithms?
<ul><li>Optimization algorithms for normal Java programmers
Open source (ASL 2.0)
Manual + examples </li></ul>
Demo Drools Planner CloudBalance example
Domain model
Server
Server public   class  Server { private   int   cpuPower ; private   int   memory ; private   int   networkBandwidth ; pri...
Process
Process
Process is a planning entity @PlanningEntity public   class  Process { private   int   requiredCpuPower ; private   int   ...
Process has a planning variable @PlanningEntity public   class  Process { ... private   Server   server ; @PlanningVariabl...
CloudBalance
Solution CloudBalance: problem facts public   class  CloudBalance implements  Solution<HardAndSoftScore> { private  List<S...
Solution CloudBalance: planning entities public   class  CloudBalance implements  Solution<HardAndSoftScore> { ... private...
Solution CloudBalance: getProblemFacts public   class  CloudBalance implements  Solution<HardAndSoftScore> { ... // Used i...
Solution CloudBalance: score public   class  CloudBalance implements  Solution<HardAndSoftScore> { ... private  HardAndSof...
Score constraints
Each Solution has 1 Score
Each Solution has 1 Score
Better score => better solution
Better score => better solution
Best score => best solution
Best score => best solution
Score calculation with Drools rule engine <ul><li>DRL </li><ul><li>Declarative (not imperative)
Like SQL, regular expressions </li></ul><li>Performance + scalability </li><ul><li>Indexing, ReteOO, ...
Delta based score calculation </li><ul><li>Solution changes => only recalculate part of score </li></ul></ul></ul>
Soft constraint: server cost rule   &quot;serverCost&quot; when // there is a server $s  : Server( $c  : cost) // there is...
Hard constraint: CPU power rule   &quot;requiredCpuPowerTotal&quot; when // there is a server $s  : Server( $cpu  : cpuPow...
Solver configuration by XML < solver > < solutionClass > ... CloudBalance</ solutionClass > < planningEntityClass > ... Pr...
Solving XmlSolverConfigurer config =  new  XmlSolverConfigurer( &quot; ... SolverConfig.xml&quot; ); Solver solver = confi...
Optimization algorithms
Brute Force
Brute Force config < solver > ... < bruteForce  /> </ solver >
Brute force scalability 6 processes = 15 ms 9 processes = 1.5 seconds * 100
Brute force scalability 12 processes = 17 minutes 6 processes = 15 ms 9 processes = 1.5 seconds * 100 * 680
Plan 1200 processes with brute force?
First Fit
First Fit config < solver > ... < constructionHeuristic > < constructionHeuristicType >FIRST_FIT</> </ constructionHeurist...
First Fit scalability
First Fit results CPU: OK RAM: OK Network: OK Cost active servers: shown
First Fit Decreasing
First Fit Decreasing config < solver > ... < constructionHeuristic > < constructionHeuristicType >FIRST_FIT_DECREASING</> ...
DifficultyComparator public   class  ProcessDifficultyComparator implements  Comparator<Process> { public   int  compare(P...
First Fit Decreasing scalability
Upcoming SlideShare
Loading in …5
×

JUDCon London 2011 - Bin packing with drools planner by example

4,422 views

Published on

Scheduling processes into cloud servers.

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
4,422
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
91
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

JUDCon London 2011 - Bin packing with drools planner by example

  1. 2. Geoffrey De Smet Bin packing with Drools Planner Scheduling processes on cloud servers
  2. 3. Every organization has planning problems.
  3. 4. What is a planning problem? <ul><li>Complete goals
  4. 5. With limited resources
  5. 6. Under constraints </li></ul>
  6. 7. Hospital bed scheduling <ul><li>Assign each </li><ul><li>Patient </li></ul><li>To </li><ul><li>Bed </li></ul><li>Constraints </li><ul><li>Length of stay
  7. 8. Room requirements
  8. 9. Room preferences </li></ul></ul>http://www.flickr.com/photos/markhillary/2227726759/
  9. 10. Hospital nurse rostering <ul><li>Assign each </li><ul><li>Shift </li></ul><li>To </li><ul><li>Nurse </li></ul><li>Constraints </li><ul><li>Employment contract
  10. 11. Free time preferences
  11. 12. Skills </li></ul></ul>http://www.flickr.com/photos/glenpooh/709704564/
  12. 13. School timetabling <ul><li>Assign each </li><ul><li>Course </li></ul><li>To </li><ul><li>Room
  13. 14. Timeslot </li></ul><li>Constraints </li><ul><li>No simultaneous </li><ul><li>Per room
  14. 15. Per teacher
  15. 16. Per student </li></ul></ul></ul>http://www.flickr.com/photos/phelyan/2281095105/
  16. 17. Car factory order scheduling <ul><li>Assign each </li><ul><li>Car order </li></ul><li>To </li><ul><li>Assembly line </li></ul><li>Constraints </li><ul><li>Assembly line specialisation </li></ul></ul>http://www.flickr.com/photos/52248755@N03/4816681486/
  17. 18. Airline routing <ul><li>Assign each </li><ul><li>Flight </li></ul><li>To </li><ul><li>Airplane
  18. 19. Crew </li></ul><li>Constraints </li><ul><li>Airplane/crew depart from where they arrive
  19. 20. Minimize mileage </li></ul></ul>http://www.flickr.com/photos/yorickr/3674349657/
  20. 21. Bin packing in the cloud <ul><li>Assign each </li><ul><li>Process </li></ul><li>To </li><ul><li>Server </li></ul><li>Constraints </li><ul><li>Hardware requirements
  21. 22. Minimize server cost </li></ul></ul>http://www.flickr.com/photos/torkildr/3462607995/
  22. 23. Bin packing in the cloud <ul><li>Assign each </li><ul><li>Process </li></ul><li>To </li><ul><li>Server </li></ul><li>Constraints </li><ul><li>Hardware requirements
  23. 24. Minimize server cost </li></ul></ul>http://www.flickr.com/photos/torkildr/3462607995/
  24. 25. Which processes do we assign to this server?
  25. 26. How did we find that solution?
  26. 27. First fit by decreasing size
  27. 28. First fit by decreasing size
  28. 29. First fit by decreasing size
  29. 30. First fit by decreasing size
  30. 31. Another case
  31. 32. Try FFD again
  32. 33. Try FFD again
  33. 34. Try FFD again
  34. 35. Try FFD again
  35. 36. FFD failure
  36. 37. NP complete
  37. 38. NP complete <ul><li>No silver bullet known </li><ul><li>Holy grail of computer science (P == NP)
  38. 39. Probably does not exist (P != NP) </li></ul><li>Root problem of all planning problems </li></ul>http://www.flickr.com/photos/annguyenphotography/3267723713/
  39. 40. Organizations rarely optimize planning problems.
  40. 41. Multiple servers...
  41. 42. Multiple servers...
  42. 43. Multiple servers...
  43. 44. Multiple servers...
  44. 45. Multiple constraints...
  45. 46. Multiple constraints...
  46. 47. Multiple constraints...
  47. 48. Multiple constraints...
  48. 49. Reuse optimization algorithms?
  49. 50. <ul><li>Optimization algorithms for normal Java programmers
  50. 51. Open source (ASL 2.0)
  51. 52. Manual + examples </li></ul>
  52. 53. Demo Drools Planner CloudBalance example
  53. 54. Domain model
  54. 55. Server
  55. 56. Server public class Server { private int cpuPower ; private int memory ; private int networkBandwidth ; private int cost ; // getters }
  56. 57. Process
  57. 58. Process
  58. 59. Process is a planning entity @PlanningEntity public class Process { private int requiredCpuPower ; private int requiredMemory ; private int requiredNetworkBandwidth ; ... // getters, clone, equals, hashcode
  59. 60. Process has a planning variable @PlanningEntity public class Process { ... private Server server ; @PlanningVariable @ValueRangeFromSolutionProperty ( propertyName = &quot;serverList&quot; ) public Server getServer() { return server ;} public void setServer(Server server) { ... } }
  60. 61. CloudBalance
  61. 62. Solution CloudBalance: problem facts public class CloudBalance implements Solution<HardAndSoftScore> { private List<Server> serverList ; public List<Server> getServerList() { return serverList ; } ... // clone, equals, hashcode
  62. 63. Solution CloudBalance: planning entities public class CloudBalance implements Solution<HardAndSoftScore> { ... private List<Process> processList ; @PlanningEntityCollectionProperty public List<Process> getProcessList() { return processList ; } ...
  63. 64. Solution CloudBalance: getProblemFacts public class CloudBalance implements Solution<HardAndSoftScore> { ... // Used in score constraints public Collection<Object> getProblemFacts() { List<Object> facts = new ArrayList<Object>(); facts.addAll( serverList ); return facts; } ...
  64. 65. Solution CloudBalance: score public class CloudBalance implements Solution<HardAndSoftScore> { ... private HardAndSoftScore score ; public HardAndSoftScore getScore() { ... } public void setScore(HardAndSoftScore score) { ... } }
  65. 66. Score constraints
  66. 67. Each Solution has 1 Score
  67. 68. Each Solution has 1 Score
  68. 69. Better score => better solution
  69. 70. Better score => better solution
  70. 71. Best score => best solution
  71. 72. Best score => best solution
  72. 73. Score calculation with Drools rule engine <ul><li>DRL </li><ul><li>Declarative (not imperative)
  73. 74. Like SQL, regular expressions </li></ul><li>Performance + scalability </li><ul><li>Indexing, ReteOO, ...
  74. 75. Delta based score calculation </li><ul><li>Solution changes => only recalculate part of score </li></ul></ul></ul>
  75. 76. Soft constraint: server cost rule &quot;serverCost&quot; when // there is a server $s : Server( $c : cost) // there is a processes on that server exists Process(server == $s ) then // lower soft score by $c insertLogical( new IntConstraintOccurrence( &quot;serverCost&quot; , ConstraintType. NEGATIVE_SOFT , $c , $s )); end
  76. 77. Hard constraint: CPU power rule &quot;requiredCpuPowerTotal&quot; when // there is a server $s : Server( $cpu : cpuPower) // with too little cpu for its processes $total : Number(intValue > $cpu ) from accumulate ( Process(server == $s , $requiredCpu : requiredCpuPower), sum( $requiredCpu ) ) then // lower hard score by ($total - $cpu) end
  77. 78. Solver configuration by XML < solver > < solutionClass > ... CloudBalance</ solutionClass > < planningEntityClass > ... Process</> < scoreDrl > ... ScoreRules.drl</ scoreDrl > < scoreDefinition > < scoreDefinitionType >HARD_AND_SOFT</> </ scoreDefinition > <!-- optimization algorithms -> ... </ solver >
  78. 79. Solving XmlSolverConfigurer config = new XmlSolverConfigurer( &quot; ... SolverConfig.xml&quot; ); Solver solver = config.buildSolver(); solver.setPlanningProblem( cloudBalance ); solver.solve(); cloudBalance = ( CloudBalance) solver.getBestSolution();
  79. 80. Optimization algorithms
  80. 81. Brute Force
  81. 82. Brute Force config < solver > ... < bruteForce /> </ solver >
  82. 83. Brute force scalability 6 processes = 15 ms 9 processes = 1.5 seconds * 100
  83. 84. Brute force scalability 12 processes = 17 minutes 6 processes = 15 ms 9 processes = 1.5 seconds * 100 * 680
  84. 85. Plan 1200 processes with brute force?
  85. 86. First Fit
  86. 87. First Fit config < solver > ... < constructionHeuristic > < constructionHeuristicType >FIRST_FIT</> </ constructionHeuristic > </ solver >
  87. 88. First Fit scalability
  88. 89. First Fit results CPU: OK RAM: OK Network: OK Cost active servers: shown
  89. 90. First Fit Decreasing
  90. 91. First Fit Decreasing config < solver > ... < constructionHeuristic > < constructionHeuristicType >FIRST_FIT_DECREASING</> </ constructionHeuristic > </ solver >
  91. 92. DifficultyComparator public class ProcessDifficultyComparator implements Comparator<Process> { public int compare(Process a, Process b) { // Compare on requiredCpuPower * requiredMemory // * requiredNetworkBandwidth } } @PlanningEntity (difficultyComparatorClass = ProcessDifficultyComparator. class ) public class Process { ... }
  92. 93. First Fit Decreasing scalability
  93. 94. First Fit Decreasing results
  94. 95. Local Search
  95. 96. Local Search comes after Construction Heuristics < solver > ... < constructionHeuristic > < constructionHeuristicType >FIRST_FIT_DECREASING</> </ constructionHeuristic > < localSearch > ... < localSearch > </ solver >
  96. 97. Local Search needs to be terminated < solver > ... < termination > < maximumMinutesSpend >20</ maximumMinutesSpend > </ termination > ... </ solver >
  97. 98. Selecting moves < localSearch > < selector > < selector > < moveFactoryClass >GenericChangeValueMoveFactory</> </ selector > < selector > < moveFactoryClass >GenericSwitchAllValuesMF</> </ selector > </ selector > ... tabu search, simulated annealing or ... </ localSearch >
  98. 99. Tabu Search < localSearch > < selector > ... </ selector > < acceptor > <!-- Untweaked standard values --> < solutionTabuSize >1000</ solutionTabuSize > < propertyTabuSize >7</ propertyTabuSize > </ acceptor > < forager > <!-- Untweaked standard value --> < minimalAcceptedSelection >1000</> </ forager > </ localSearch >
  99. 100. Or Simulated Annealing < localSearch > < selector > ... </ selector > < acceptor > <!-- Tweaked value --> < simulatedAnnealingStartingTemperature > 0hard/400soft</> </ acceptor > < forager > <!-- Untweaked standard value --> < minimalAcceptedSelection >4</> </ forager > </ localSearch >
  100. 101. Metaheuristics results
  101. 102. Cost ($) reduction <ul><li>Compared to First Fit </li><ul><li>First Fit Decreasing </li><ul><li>49 480 $ = 4 % </li></ul><li>Tabu Search </li><ul><li>160 860 $ = 14 % </li></ul><li>Simulated annealing </li><ul><li>128 950 $ = 11 % </li></ul></ul><li>Few constraints here </li><ul><li>=> low ROI </li></ul></ul>
  102. 103. Organizations rarely optimize planning problems.
  103. 104. http://www.flickr.com/photos/techbirmingham/345897594/ Organizations waste resources.
  104. 105. Real-time planning
  105. 106. Real-time paradox <ul><li>First Fit Decreasing 1200 processes </li><ul><li>8 seconds </li></ul><li>Time allowed </li><ul><li>100ms after last change </li></ul></ul>
  106. 107. Demo Real-time planning CloudBalance example
  107. 108. Summary
  108. 109. Summary <ul><li>Drools Planner optimizes planning
  109. 110. Adding constraints is easy and scalable
  110. 111. Switching/combining algorithms is easy </li></ul>
  111. 112. Try an example!
  112. 113. Q&A <ul><li>JBoss Drools Planner homepage: </li><ul><li>http://www.jboss.org/drools/drools-planner </li></ul><li>Reference manual: </li><ul><li>http://www.jboss.org/drools/documentation </li></ul><li>Download this presentation: </li><ul><li>http://www.jboss.org/drools/presentations </li></ul><li>Twitter: @geoffreydesmet
  113. 114. Google+: Geoffrey De Smet </li></ul>

×