-
Notifications
You must be signed in to change notification settings - Fork 277
Expand file tree
/
Copy pathModules.java
More file actions
340 lines (283 loc) · 12.9 KB
/
Copy pathModules.java
File metadata and controls
340 lines (283 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package com.google.appengine.tools.development;
import com.google.appengine.repackaged.com.google.common.collect.ImmutableList;
import com.google.appengine.repackaged.com.google.common.collect.ImmutableMap;
import com.google.appengine.tools.development.ApplicationConfigurationManager;
import com.google.appengine.tools.development.AutomaticModule;
import com.google.appengine.tools.development.DevAppServerImpl;
import com.google.appengine.tools.development.InstanceHolder;
import com.google.appengine.tools.development.LocalServerEnvironment;
import com.google.appengine.tools.development.ManualModule;
import com.google.appengine.tools.development.Module;
import com.google.appengine.tools.development.ModulesController;
import com.google.appengine.tools.development.ModulesFilterHelper;
import com.google.apphosting.api.ApiProxy.ApplicationException;
import com.google.apphosting.utils.config.AppEngineConfigException;
import com.google.apphosting.utils.config.AppEngineWebXml;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Modules implements ModulesController, ModulesFilterHelper {
private static final Logger LOGGER = Logger.getLogger(Modules.class.getName());
private final List modules;
private final Map moduleNameToModuleMap;
private final Lock dynamicConfigurationLock = new ReentrantLock();
private static final int DYNAMIC_CONFIGURATION_TIMEOUT_SECONDS = 2;
// AppScale: The number of seconds an instance is allowed to finish serving requests after it receives a shutdown
// signal.
private static final int MAX_INSTANCE_RESPONSE_TIME = 600;
static Modules createModules(ApplicationConfigurationManager applicationConfigurationManager, String serverInfo, File externalResourceDir, String address, DevAppServerImpl devAppServer) {
ImmutableList.Builder builder = ImmutableList.builder();
for(Iterator i$ = applicationConfigurationManager.getModuleConfigurationHandles().iterator(); i$.hasNext(); externalResourceDir = null) {
ApplicationConfigurationManager.ModuleConfigurationHandle moduleConfigurationHandle = (ApplicationConfigurationManager.ModuleConfigurationHandle)i$.next();
AppEngineWebXml appEngineWebXml = moduleConfigurationHandle.getModule().getAppEngineWebXml();
Object module = null;
if(!appEngineWebXml.getBasicScaling().isEmpty()) {
throw new AppEngineConfigException("Basic scaling modules are currently not supported");
}
if(!appEngineWebXml.getManualScaling().isEmpty()) {
module = new ManualModule(moduleConfigurationHandle, serverInfo, address, devAppServer, appEngineWebXml);
} else {
module = new AutomaticModule(moduleConfigurationHandle, serverInfo, externalResourceDir, address, devAppServer);
}
builder.add(module);
}
return new Modules(builder.build());
}
void shutdown() throws Exception {
// AppScale: Prevent instances from serving new requests.
for (Object uncastModule : this.modules) {
Module module = (Module) uncastModule;
JettyContainerService container = (JettyContainerService) module.getMainContainer();
container.requestShutdown();
}
LOGGER.info("Waiting for instances to finish serving requests");
long deadline = System.currentTimeMillis() + (MAX_INSTANCE_RESPONSE_TIME * 1000);
while (true) {
if (System.currentTimeMillis() > deadline) {
LOGGER.log(Level.SEVERE, "Request timeout while shutting down");
break;
}
boolean requestsInProgress = false;
for (Object uncastModule : this.modules) {
Module module = (Module) uncastModule;
JettyContainerService container = (JettyContainerService) module.getMainContainer();
if (container.getRequestCount() > 0)
requestsInProgress = true;
}
if (!requestsInProgress)
break;
Thread.sleep(500);
}
// End AppScale.
for (Object uncastModule : this.modules) {
Module module = (Module) uncastModule;
module.shutdown();
}
}
void configure(Map containerConfigProperties) throws Exception {
Iterator i$ = this.modules.iterator();
while(i$.hasNext()) {
Module module = (Module)i$.next();
module.configure(containerConfigProperties);
}
}
void createConnections() throws Exception {
Iterator i$ = this.modules.iterator();
while(i$.hasNext()) {
Module module = (Module)i$.next();
module.createConnection();
}
}
void startup() throws Exception {
Iterator i$ = this.modules.iterator();
while(i$.hasNext()) {
Module module = (Module)i$.next();
module.startup();
}
}
Module getMainModule() {
return (Module)this.modules.get(0);
}
private Modules(List modules) {
if(modules.size() < 1) {
throw new IllegalArgumentException("modules must not be empty.");
} else {
this.modules = modules;
ImmutableMap.Builder mapBuilder = ImmutableMap.builder();
Iterator i$ = this.modules.iterator();
while(i$.hasNext()) {
Module module = (Module)i$.next();
mapBuilder.put(module.getModuleName(), module);
}
this.moduleNameToModuleMap = mapBuilder.build();
}
}
LocalServerEnvironment getLocalServerEnvironment() {
return ((Module)this.modules.get(0)).getLocalServerEnvironment();
}
Module getModule(String moduleName) {
return (Module)this.moduleNameToModuleMap.get(moduleName);
}
public Iterable getModuleNames() {
return this.moduleNameToModuleMap.keySet();
}
public Iterable getVersions(String moduleName) throws ApplicationException {
return ImmutableList.of(this.getDefaultVersion(moduleName));
}
public String getDefaultVersion(String moduleName) throws ApplicationException {
Module module = this.getRequiredModule(moduleName);
return module.getMainContainer().getAppEngineWebXmlConfig().getMajorVersionId();
}
public int getNumInstances(String moduleName, String version) throws ApplicationException {
Module module = this.getRequiredModule(moduleName);
this.checkVersion(version, module);
AppEngineWebXml.ManualScaling manualScaling = this.getRequiredManualScaling(module);
return Integer.parseInt(manualScaling.getInstances());
}
public String getHostname(String moduleName, String version, int instance) throws ApplicationException {
Module module = this.getRequiredModule(moduleName);
this.checkVersion(version, module);
if(instance != -1) {
this.getRequiredManualScaling(module);
}
String hostAndPort = module.getHostAndPort(instance);
if(hostAndPort == null) {
throw new ApplicationException(3, "Instance " + instance + " not found");
} else {
return hostAndPort;
}
}
public void startModule(final String moduleName, final String version) throws ApplicationException {
this.doDynamicConfiguration("startServing", new Runnable() {
public void run() {
Modules.this.doStartModule(moduleName, version);
}
});
}
private void doStartModule(String moduleName, String version) {
Module module = this.getRequiredModule(moduleName);
this.checkVersion(version, module);
this.getRequiredManualScaling(module);
try {
module.startServing();
} catch (Exception var5) {
LOGGER.log(Level.SEVERE, "startServing failed", var5);
throw new ApplicationException(5, "startServing failed with error " + var5.getMessage());
}
}
public void stopModule(final String moduleName, final String version) throws ApplicationException {
this.doDynamicConfiguration("stopServing", new Runnable() {
public void run() {
Modules.this.doStopModule(moduleName, version);
}
});
}
private void doDynamicConfiguration(String operation, Runnable runnable) {
try {
if(this.dynamicConfigurationLock.tryLock(2L, TimeUnit.SECONDS)) {
try {
runnable.run();
} finally {
this.dynamicConfigurationLock.unlock();
}
} else {
LOGGER.log(Level.SEVERE, "stopServing timed out");
throw new ApplicationException(5, operation + " timed out");
}
} catch (InterruptedException var7) {
LOGGER.log(Level.SEVERE, "stopServing interrupted", var7);
throw new ApplicationException(5, operation + " interrupted " + var7.getMessage());
}
}
private void doStopModule(String moduleName, String version) {
Module module = this.getRequiredModule(moduleName);
this.checkVersion(version, module);
this.getRequiredManualScaling(module);
try {
module.stopServing();
} catch (Exception var5) {
LOGGER.log(Level.SEVERE, "stopServing failed", var5);
throw new ApplicationException(5, "stopServing failed with error " + var5.getMessage());
}
}
private Module getRequiredModule(String moduleName) {
Module module = (Module)this.moduleNameToModuleMap.get(moduleName);
if(module == null) {
throw new ApplicationException(1, "Module not found");
} else {
return module;
}
}
private AppEngineWebXml.ManualScaling getRequiredManualScaling(Module module) {
AppEngineWebXml.ManualScaling manualScaling = module.getMainContainer().getAppEngineWebXmlConfig().getManualScaling();
if(manualScaling.isEmpty()) {
LOGGER.warning("Module " + module.getModuleName() + " must be a manual scaling module");
throw new ApplicationException(2, "Manual scaling is required.");
} else {
return manualScaling;
}
}
private void checkVersion(String version, Module module) {
String moduleVersion = module.getMainContainer().getAppEngineWebXmlConfig().getMajorVersionId();
if(version == null || !version.equals(moduleVersion)) {
throw new ApplicationException(2, "Version not found");
}
}
public boolean acquireServingPermit(String moduleName, int instanceNumber, boolean allowQueueOnBackends) {
Module module = this.getModule(moduleName);
InstanceHolder instanceHolder = module.getInstanceHolder(instanceNumber);
return instanceHolder.acquireServingPermit();
}
public int getAndReserveFreeInstance(String moduleName) {
Module module = this.getModule(moduleName);
InstanceHolder instanceHolder = module.getAndReserveAvailableInstanceHolder();
return instanceHolder == null?-1:instanceHolder.getInstance();
}
public void returnServingPermit(String moduleName, int instance) {
}
public boolean checkInstanceExists(String moduleName, int instance) {
Module module = this.getModule(moduleName);
return module != null && module.getInstanceHolder(instance) != null;
}
public boolean checkModuleExists(String moduleName) {
return this.getModule(moduleName) != null;
}
public boolean checkModuleStopped(String serverName) {
return this.checkInstanceStopped(serverName, -1);
}
public boolean checkInstanceStopped(String moduleName, int instance) {
Module module = this.getModule(moduleName);
InstanceHolder instanceHolder = module.getInstanceHolder(instance);
return instanceHolder.isStopped();
}
public void forwardToInstance(String requestedModule, int instance, HttpServletRequest hrequest, HttpServletResponse hresponse) throws IOException, ServletException {
Module module = this.getModule(requestedModule);
InstanceHolder instanceHolder = module.getInstanceHolder(instance);
instanceHolder.getContainerService().forwardToServer(hrequest, hresponse);
}
public boolean isLoadBalancingInstance(String moduleName, int instance) {
Module module = this.getModule(moduleName);
InstanceHolder instanceHolder = module.getInstanceHolder(instance);
return instanceHolder.isLoadBalancingInstance();
}
public boolean expectsGeneratedStartRequests(String moduleName, int instance) {
Module module = this.getModule(moduleName);
InstanceHolder instanceHolder = module.getInstanceHolder(instance);
return instanceHolder.expectsGeneratedStartRequest();
}
public int getPort(String moduleName, int instance) {
Module module = this.getModule(moduleName);
InstanceHolder instanceHolder = module.getInstanceHolder(instance);
return instanceHolder.getContainerService().getPort();
}
}