File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\model\AbstractCIBase.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi,
5 * Erik Ramfelt, Koichi Fujikawa, Red Hat, Inc., Seiji Sogabe,
6 * Stephen Connolly, Tom Huybrechts, Yahoo! Inc., Alan Harder, CloudBees, Inc.
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
27 package hudson.model;
28
29
30 import hudson.security.AccessControlled;
31 import hudson.slaves.ComputerListener;
32 import hudson.slaves.RetentionStrategy;
33 import org.kohsuke.stapler.StaplerFallback;
34 import org.kohsuke.stapler.StaplerProxy;
35
36 import java.io.IOException;
37 import java.util.*;
38 import java.util.concurrent.CopyOnWriteArraySet;
39 import java.util.logging.Logger;
40 import javax.annotation.CheckForNull;
41
42 import jenkins.model.Configuration;
43
44 public abstract class AbstractCIBase extends Node implements ItemGroup<TopLevelItem>, StaplerProxy, StaplerFallback, ViewGroup, AccessControlled, DescriptorByNameOwner {
45
46 public static boolean LOG_STARTUP_PERFORMANCE = Configuration.getBooleanConfigParameter("logStartupPerformance", false);
47
48 private static final Logger LOGGER = Logger.getLogger(AbstractCIBase.class.getName());
49
50 private final transient Object updateComputerLock = new Object();
51
52 /**
53 * If you are calling this on Hudson something is wrong.
54 *
55 * @deprecated
56 */
57 @Deprecated @Override
58 public String getNodeName() {
59 return "";
60 }
61
62 /**
63 * @deprecated
64 * Why are you calling a method that always returns ""?
65 */
66 public String getUrl() {
67 return "";
68 }
69
70 /* =================================================================================================================
71 * Support functions that can only be accessed through package-protected
72 * ============================================================================================================== */
73 protected void resetLabel(Label l) {
74 l.reset();
75 }
76
77 protected void setViewOwner(View v) {
78 v.owner = this;
79 }
80 protected void interruptReloadThread() {
81 ViewJob.reloadThread.interrupt();
82 }
83
84 protected void killComputer(Computer c) {
85 c.kill();
86 }
87
88 /* =================================================================================================================
89 * Package-protected, but accessed API
90 * ============================================================================================================== */
91
92 /*package*/ final CopyOnWriteArraySet<String> disabledAdministrativeMonitors = new CopyOnWriteArraySet<String>();
93
94 /* =================================================================================================================
95 * Implementation provided
96 * ============================================================================================================== */
97
98 /**
99 * Returns all {@link Node}s in the system, excluding {@link jenkins.model.Jenkins} instance itself which
100 * represents the master.
101 */
102 public abstract List<Node> getNodes();
103
104 public abstract Queue getQueue();
105
106 protected abstract Map<Node,Computer> getComputerMap();
107
108 /* =================================================================================================================
109 * Computer API uses package protection heavily
110 * ============================================================================================================== */
111
112 private void updateComputer(Node n, Map<String,Computer> byNameMap, Set<Computer> used, boolean automaticSlaveLaunch) {
113 Map<Node,Computer> computers = getComputerMap();
114 Computer c;
115 c = byNameMap.get(n.getNodeName());
116 if (c!=null) {
117 c.setNode(n); // reuse
118 } else {
119 if(n.getNumExecutors()>0) {
120 computers.put(n, c = n.createComputer());
121 if (!n.isHoldOffLaunchUntilSave() && automaticSlaveLaunch) {
122 RetentionStrategy retentionStrategy = c.getRetentionStrategy();
123 if (retentionStrategy != null) {
124 // if there is a retention strategy, it is responsible for deciding to start the computer
125 retentionStrategy.start(c);
126 } else {
127 // we should never get here, but just in case, we'll fall back to the legacy behaviour
128 c.connect(true);
129 }
130 }
131 }
132 }
133 used.add(c);
134 }
135
136 /*package*/ void removeComputer(Computer computer) {
137 Map<Node,Computer> computers = getComputerMap();
138 for (Map.Entry<Node, Computer> e : computers.entrySet()) {
139 if (e.getValue() == computer) {
140 computers.remove(e.getKey());
141 return;
142 }
143 }
144 throw new IllegalStateException("Trying to remove unknown computer");
145 }
146
147 /*package*/ @CheckForNull Computer getComputer(Node n) {
148 Map<Node,Computer> computers = getComputerMap();
149 return computers.get(n);
150 }
151
152 /**
153 * Updates Computers.
154 *
155 * <p>
156 * This method tries to reuse existing {@link Computer} objects
157 * so that we won't upset {@link Executor}s running in it.
158 */
159 protected void updateComputerList(boolean automaticSlaveLaunch) throws IOException {
160 Map<Node,Computer> computers = getComputerMap();
161 synchronized(updateComputerLock) {// just so that we don't have two code updating computer list at the same time
162 Map<String,Computer> byName = new HashMap<String,Computer>();
163 for (Computer c : computers.values()) {
164 if(c.getNode()==null)
165 continue; // this computer is gone
166 byName.put(c.getNode().getNodeName(),c);
167 }
168
169 Set<Computer> old = new HashSet<Computer>(computers.values());
170 Set<Computer> used = new HashSet<Computer>();
171
172 updateComputer(this, byName, used, automaticSlaveLaunch);
173 for (Node s : getNodes()) {
174 long start = System.currentTimeMillis();
175 updateComputer(s, byName, used, automaticSlaveLaunch);
176 if(LOG_STARTUP_PERFORMANCE)
177 LOGGER.info(String.format("Took %dms to update node %s",
178 System.currentTimeMillis()-start, s.getNodeName()));
179 }
180
181 // find out what computers are removed, and kill off all executors.
182 // when all executors exit, it will be removed from the computers map.
183 // so don't remove too quickly
184 old.removeAll(used);
185 for (Computer c : old) {
186 killComputer(c);
187 }
188 }
189 getQueue().scheduleMaintenance();
190 for (ComputerListener cl : ComputerListener.all())
191 cl.onConfigurationChange();
192 }
193
194 }
195