/hudson-core/src/main/java/hudson/model/LoadBalancer.java

http://github.com/hudson/hudson · Java · 157 lines · 65 code · 19 blank · 73 comment · 8 complexity · 7ee843ae10d0e61bbfb9e3083d343e50 MD5 · raw file

  1. /*
  2. * The MIT License
  3. *
  4. * Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. package hudson.model;
  25. import hudson.model.Queue.Task;
  26. import hudson.model.queue.MappingWorksheet;
  27. import hudson.model.queue.MappingWorksheet.ExecutorChunk;
  28. import hudson.model.queue.MappingWorksheet.Mapping;
  29. import hudson.util.ConsistentHash;
  30. import hudson.util.ConsistentHash.Hash;
  31. import java.util.ArrayList;
  32. import java.util.List;
  33. import java.util.logging.Logger;
  34. /**
  35. * Strategy that decides which {@link Task} gets run on which {@link Executor}.
  36. *
  37. * @author Kohsuke Kawaguchi
  38. * @since 1.301
  39. */
  40. public abstract class LoadBalancer /*implements ExtensionPoint*/ {
  41. /**
  42. * Chooses the executor(s) to carry out the build for the given task.
  43. *
  44. * <p>
  45. * This method is invoked from different threads, but the execution is serialized by the caller.
  46. * The thread that invokes this method always holds a lock to {@link Queue}, so queue contents
  47. * can be safely introspected from this method, if that information is necessary to make
  48. * decisions.
  49. *
  50. * @param task
  51. * The task whose execution is being considered. Never null.
  52. * @param worksheet
  53. * The work sheet that represents the matching that needs to be made.
  54. * The job of this method is to determine which work units on this worksheet
  55. * are executed on which executors (also on this worksheet.)
  56. *
  57. * @return
  58. * Build up the mapping by using the given worksheet and return it.
  59. * Return null if you don't want the task to be executed right now,
  60. * in which case this method will be called some time later with the same task.
  61. */
  62. public abstract Mapping map(Task task, MappingWorksheet worksheet);
  63. /**
  64. * Uses a consistent hash for scheduling.
  65. */
  66. public static final LoadBalancer CONSISTENT_HASH = new LoadBalancer() {
  67. @Override
  68. public Mapping map(Task task, MappingWorksheet ws) {
  69. // build consistent hash for each work chunk
  70. List<ConsistentHash<ExecutorChunk>> hashes = new ArrayList<ConsistentHash<ExecutorChunk>>(ws.works.size());
  71. for (int i=0; i<ws.works.size(); i++) {
  72. ConsistentHash<ExecutorChunk> hash = new ConsistentHash<ExecutorChunk>(new Hash<ExecutorChunk>() {
  73. public String hash(ExecutorChunk node) {
  74. return node.getName();
  75. }
  76. });
  77. for (ExecutorChunk ec : ws.works(i).applicableExecutorChunks())
  78. hash.add(ec,ec.size()*100);
  79. hashes.add(hash);
  80. }
  81. // do a greedy assignment
  82. Mapping m = ws.new Mapping();
  83. assert m.size()==ws.works.size(); // just so that you the reader of the source code don't get confused with the for loop index
  84. if (assignGreedily(m,task,hashes,0)) {
  85. assert m.isCompletelyValid();
  86. return m;
  87. } else
  88. return null;
  89. }
  90. private boolean assignGreedily(Mapping m, Task task, List<ConsistentHash<ExecutorChunk>> hashes, int i) {
  91. if (i==hashes.size()) return true; // fully assigned
  92. String key = task.getFullDisplayName() + (i>0 ? String.valueOf(i) : "");
  93. for (ExecutorChunk ec : hashes.get(i).list(key)) {
  94. // let's attempt this assignment
  95. m.assign(i,ec);
  96. if (m.isPartiallyValid() && assignGreedily(m,task,hashes,i+1))
  97. return true; // successful greedily allocation
  98. // otherwise 'ec' wasn't a good fit for us. try next.
  99. }
  100. // every attempt failed
  101. m.assign(i,null);
  102. return false;
  103. }
  104. };
  105. /**
  106. * Traditional implementation of this.
  107. *
  108. * @deprecated as of 1.377
  109. * The only implementation in the core now is the one based on consistent hash.
  110. */
  111. public static final LoadBalancer DEFAULT = CONSISTENT_HASH;
  112. /**
  113. * Wraps this {@link LoadBalancer} into a decorator that tests the basic sanity of the implementation.
  114. * Only override this if you find some of the checks excessive, but beware that it's like driving without a seat belt.
  115. */
  116. protected LoadBalancer sanitize() {
  117. final LoadBalancer base = this;
  118. return new LoadBalancer() {
  119. @Override
  120. public Mapping map(Task task, MappingWorksheet worksheet) {
  121. if (Queue.ifBlockedByHudsonShutdown(task)) {
  122. // if we are quieting down, don't start anything new so that
  123. // all executors will be eventually free.
  124. return null;
  125. }
  126. return base.map(task, worksheet);
  127. }
  128. /**
  129. * Double-sanitization is pointless.
  130. */
  131. @Override
  132. protected LoadBalancer sanitize() {
  133. return this;
  134. }
  135. private final Logger LOGGER = Logger.getLogger(LoadBalancer.class.getName());
  136. };
  137. }
  138. }