passenger 5.0.9 → 5.0.10

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of passenger might be problematic. Click here for more details.

Files changed (106) hide show
  1. checksums.yaml +8 -8
  2. checksums.yaml.gz.asc +7 -7
  3. data.tar.gz.asc +7 -7
  4. data/CHANGELOG +15 -0
  5. data/CONTRIBUTORS +6 -0
  6. data/README.md +1 -1
  7. data/bin/passenger-install-apache2-module +24 -11
  8. data/bin/passenger-status +29 -14
  9. data/build/agents.rb +12 -10
  10. data/build/cxx_tests.rb +30 -30
  11. data/doc/Design and Architecture.html +1 -10
  12. data/doc/Design and Architecture.txt +1 -6
  13. data/doc/Users guide Apache.html +1 -19
  14. data/doc/Users guide Apache.txt +1 -1
  15. data/doc/Users guide Nginx.html +2 -20
  16. data/doc/Users guide Nginx.txt +2 -2
  17. data/doc/users_guide_snippets/tips.txt +0 -9
  18. data/ext/common/ApplicationPool2/ApiKey.h +158 -0
  19. data/ext/common/ApplicationPool2/BasicGroupInfo.h +81 -0
  20. data/ext/common/ApplicationPool2/BasicProcessInfo.h +106 -0
  21. data/ext/common/ApplicationPool2/Common.h +5 -44
  22. data/ext/common/ApplicationPool2/Context.h +94 -0
  23. data/ext/common/ApplicationPool2/Group.h +130 -1205
  24. data/ext/common/ApplicationPool2/Group/InitializationAndShutdown.cpp +190 -0
  25. data/ext/common/ApplicationPool2/Group/InternalUtils.cpp +329 -0
  26. data/ext/common/ApplicationPool2/Group/LifetimeAndBasics.cpp +103 -0
  27. data/ext/common/ApplicationPool2/{Pool/Debug.h → Group/Miscellaneous.cpp} +40 -38
  28. data/ext/common/ApplicationPool2/Group/OutOfBandWork.cpp +323 -0
  29. data/ext/common/ApplicationPool2/Group/ProcessListManagement.cpp +606 -0
  30. data/ext/common/ApplicationPool2/Group/SessionManagement.cpp +337 -0
  31. data/ext/common/ApplicationPool2/Group/SpawningAndRestarting.cpp +478 -0
  32. data/ext/common/ApplicationPool2/Group/StateInspection.cpp +197 -0
  33. data/ext/common/ApplicationPool2/Group/Verification.cpp +159 -0
  34. data/ext/common/ApplicationPool2/Implementation.cpp +19 -1401
  35. data/ext/common/ApplicationPool2/Options.h +5 -5
  36. data/ext/common/ApplicationPool2/Pool.h +260 -815
  37. data/ext/common/ApplicationPool2/Pool/{AnalyticsCollection.h → AnalyticsCollection.cpp} +55 -56
  38. data/ext/common/ApplicationPool2/Pool/{GarbageCollection.h → GarbageCollection.cpp} +49 -49
  39. data/ext/common/ApplicationPool2/Pool/GeneralUtils.cpp +241 -0
  40. data/ext/common/ApplicationPool2/Pool/GroupUtils.cpp +276 -0
  41. data/ext/common/ApplicationPool2/Pool/InitializationAndShutdown.cpp +145 -0
  42. data/ext/common/ApplicationPool2/Pool/Miscellaneous.cpp +244 -0
  43. data/ext/common/ApplicationPool2/Pool/ProcessUtils.cpp +330 -0
  44. data/ext/common/ApplicationPool2/Pool/StateInspection.cpp +299 -0
  45. data/ext/common/ApplicationPool2/Process.h +399 -205
  46. data/ext/common/ApplicationPool2/Session.h +70 -28
  47. data/ext/common/ApplicationPool2/Socket.h +1 -0
  48. data/ext/common/Constants.h +11 -3
  49. data/ext/common/Exceptions.h +1 -1
  50. data/ext/common/Logging.cpp +9 -4
  51. data/ext/common/Logging.h +6 -0
  52. data/ext/common/ServerKit/HttpServer.h +225 -215
  53. data/ext/common/ServerKit/Server.h +57 -57
  54. data/ext/common/SpawningKit/BackgroundIOCapturer.h +160 -0
  55. data/ext/common/SpawningKit/Config.h +107 -0
  56. data/ext/common/{ApplicationPool2 → SpawningKit}/DirectSpawner.h +17 -16
  57. data/ext/common/{ApplicationPool2 → SpawningKit}/DummySpawner.h +33 -33
  58. data/ext/common/{ApplicationPool2/SpawnerFactory.h → SpawningKit/Factory.h} +17 -17
  59. data/ext/common/{ApplicationPool2/ComponentInfo.h → SpawningKit/Options.h} +8 -21
  60. data/ext/common/SpawningKit/PipeWatcher.h +148 -0
  61. data/ext/common/{ApplicationPool2/PipeWatcher.h → SpawningKit/Result.h} +15 -33
  62. data/ext/common/{ApplicationPool2 → SpawningKit}/SmartSpawner.h +52 -57
  63. data/ext/common/{ApplicationPool2 → SpawningKit}/Spawner.h +83 -371
  64. data/ext/common/SpawningKit/UserSwitchingRules.h +265 -0
  65. data/ext/common/Utils/BufferedIO.h +24 -0
  66. data/ext/common/{ApplicationPool2/SpawnObject.h → Utils/ClassUtils.h} +24 -51
  67. data/ext/common/Utils/IOUtils.cpp +70 -0
  68. data/ext/common/Utils/IOUtils.h +19 -0
  69. data/ext/common/Utils/JsonUtils.h +113 -0
  70. data/ext/common/Utils/StrIntUtils.h +29 -0
  71. data/ext/common/Utils/json.h +1 -1
  72. data/ext/common/agents/ApiServerUtils.h +941 -0
  73. data/ext/common/agents/HelperAgent/{AdminServer.h → ApiServer.h} +163 -365
  74. data/ext/common/agents/HelperAgent/Main.cpp +86 -88
  75. data/ext/common/agents/HelperAgent/OptionParser.h +9 -10
  76. data/ext/common/agents/HelperAgent/RequestHandler/BufferBody.cpp +3 -0
  77. data/ext/common/agents/HelperAgent/RequestHandler/ForwardResponse.cpp +2 -0
  78. data/ext/common/agents/HelperAgent/RequestHandler/Hooks.cpp +1 -1
  79. data/ext/common/agents/HelperAgent/RequestHandler/SendRequest.cpp +2 -2
  80. data/ext/common/agents/LoggingAgent/ApiServer.h +279 -0
  81. data/ext/common/agents/LoggingAgent/Main.cpp +41 -51
  82. data/ext/common/agents/LoggingAgent/OptionParser.h +11 -11
  83. data/ext/common/agents/Watchdog/ApiServer.h +311 -0
  84. data/ext/common/agents/Watchdog/Main.cpp +91 -65
  85. data/helper-scripts/prespawn +2 -0
  86. data/lib/phusion_passenger.rb +1 -1
  87. data/lib/phusion_passenger/admin_tools/instance.rb +1 -1
  88. data/lib/phusion_passenger/common_library.rb +27 -14
  89. data/lib/phusion_passenger/config/{admin_command_command.rb → api_call_command.rb} +19 -16
  90. data/lib/phusion_passenger/config/detach_process_command.rb +6 -3
  91. data/lib/phusion_passenger/config/main.rb +3 -5
  92. data/lib/phusion_passenger/config/reopen_logs_command.rb +29 -7
  93. data/lib/phusion_passenger/config/restart_app_command.rb +13 -4
  94. data/lib/phusion_passenger/config/utils.rb +15 -8
  95. data/lib/phusion_passenger/constants.rb +6 -2
  96. data/lib/phusion_passenger/platform_info/apache.rb +4 -0
  97. data/lib/phusion_passenger/platform_info/apache_detector.rb +18 -3
  98. data/resources/templates/apache2/mpm_unknown.txt.erb +20 -0
  99. metadata +42 -21
  100. metadata.gz.asc +7 -7
  101. data/ext/common/ApplicationPool2/Pool/GeneralUtils.h +0 -127
  102. data/ext/common/ApplicationPool2/Pool/Inspection.h +0 -219
  103. data/ext/common/ApplicationPool2/Pool/ProcessUtils.h +0 -85
  104. data/ext/common/ApplicationPool2/SuperGroup.h +0 -706
  105. data/ext/common/agents/LoggingAgent/AdminServer.h +0 -435
  106. data/ext/common/agents/Watchdog/AdminServer.h +0 -432
@@ -0,0 +1,606 @@
1
+ /*
2
+ * Phusion Passenger - https://www.phusionpassenger.com/
3
+ * Copyright (c) 2011-2015 Phusion
4
+ *
5
+ * "Phusion Passenger" is a trademark of Hongli Lai & Ninh Bui.
6
+ *
7
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ * of this software and associated documentation files (the "Software"), to deal
9
+ * in the Software without restriction, including without limitation the rights
10
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ * copies of the Software, and to permit persons to whom the Software is
12
+ * furnished to do so, subject to the following conditions:
13
+ *
14
+ * The above copyright notice and this permission notice shall be included in
15
+ * all copies or substantial portions of the Software.
16
+ *
17
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23
+ * THE SOFTWARE.
24
+ */
25
+ #include <ApplicationPool2/Group.h>
26
+
27
+ /*************************************************************************
28
+ *
29
+ * Process list management functions for ApplicationPool2::Group
30
+ *
31
+ *************************************************************************/
32
+
33
+ namespace Passenger {
34
+ namespace ApplicationPool2 {
35
+
36
+ using namespace std;
37
+ using namespace boost;
38
+
39
+
40
+ /****************************
41
+ *
42
+ * Private methods
43
+ *
44
+ ****************************/
45
+
46
+
47
+ Process *
48
+ Group::findProcessWithStickySessionId(unsigned int id) const {
49
+ ProcessList::const_iterator it, end = enabledProcesses.end();
50
+ for (it = enabledProcesses.begin(); it != end; it++) {
51
+ Process *process = it->get();
52
+ if (process->getStickySessionId() == id) {
53
+ return process;
54
+ }
55
+ }
56
+ return NULL;
57
+ }
58
+
59
+ Process *
60
+ Group::findProcessWithStickySessionIdOrLowestBusyness(unsigned int id) const {
61
+ int leastBusyProcessIndex = -1;
62
+ int lowestBusyness = 0;
63
+ unsigned int i, size = enabledProcessBusynessLevels.size();
64
+ const int *enabledProcessBusynessLevels = &this->enabledProcessBusynessLevels[0];
65
+
66
+ for (i = 0; i < size; i++) {
67
+ Process *process = enabledProcesses[i].get();
68
+ if (process->getStickySessionId() == id) {
69
+ return process;
70
+ } else if (leastBusyProcessIndex == -1 || enabledProcessBusynessLevels[i] < lowestBusyness) {
71
+ leastBusyProcessIndex = i;
72
+ lowestBusyness = enabledProcessBusynessLevels[i];
73
+ }
74
+ }
75
+
76
+ if (leastBusyProcessIndex == -1) {
77
+ return NULL;
78
+ } else {
79
+ return enabledProcesses[leastBusyProcessIndex].get();
80
+ }
81
+ }
82
+
83
+ Process *
84
+ Group::findProcessWithLowestBusyness(const ProcessList &processes) const {
85
+ if (processes.empty()) {
86
+ return NULL;
87
+ }
88
+
89
+ int lowestBusyness = -1;
90
+ Process *leastBusyProcess = NULL;
91
+ ProcessList::const_iterator it;
92
+ ProcessList::const_iterator end = processes.end();
93
+ for (it = processes.begin(); it != end; it++) {
94
+ Process *process = (*it).get();
95
+ int busyness = process->busyness();
96
+ if (lowestBusyness == -1 || lowestBusyness > busyness) {
97
+ lowestBusyness = busyness;
98
+ leastBusyProcess = process;
99
+ }
100
+ }
101
+ return leastBusyProcess;
102
+ }
103
+
104
+ /**
105
+ * Cache-optimized version of findProcessWithLowestBusyness() for the common case.
106
+ */
107
+ Process *
108
+ Group::findEnabledProcessWithLowestBusyness() const {
109
+ if (enabledProcesses.empty()) {
110
+ return NULL;
111
+ }
112
+
113
+ int leastBusyProcessIndex = -1;
114
+ int lowestBusyness = 0;
115
+ unsigned int i, size = enabledProcessBusynessLevels.size();
116
+ const int *enabledProcessBusynessLevels = &this->enabledProcessBusynessLevels[0];
117
+
118
+ for (i = 0; i < size; i++) {
119
+ if (leastBusyProcessIndex == -1 || enabledProcessBusynessLevels[i] < lowestBusyness) {
120
+ leastBusyProcessIndex = i;
121
+ lowestBusyness = enabledProcessBusynessLevels[i];
122
+ }
123
+ }
124
+ return enabledProcesses[leastBusyProcessIndex].get();
125
+ }
126
+
127
+ /**
128
+ * Adds a process to the given list (enabledProcess, disablingProcesses, disabledProcesses)
129
+ * and sets the process->enabled flag accordingly.
130
+ * The process must currently not be in any list. This function does not fix
131
+ * getWaitlist invariants or other stuff.
132
+ */
133
+ void
134
+ Group::addProcessToList(const ProcessPtr &process, ProcessList &destination) {
135
+ destination.push_back(process);
136
+ process->setIndex(destination.size() - 1);
137
+ if (&destination == &enabledProcesses) {
138
+ process->enabled = Process::ENABLED;
139
+ enabledCount++;
140
+ enabledProcessBusynessLevels.push_back(process->busyness());
141
+ if (process->isTotallyBusy()) {
142
+ nEnabledProcessesTotallyBusy++;
143
+ }
144
+ } else if (&destination == &disablingProcesses) {
145
+ process->enabled = Process::DISABLING;
146
+ disablingCount++;
147
+ } else if (&destination == &disabledProcesses) {
148
+ assert(process->sessions == 0);
149
+ process->enabled = Process::DISABLED;
150
+ disabledCount++;
151
+ } else if (&destination == &detachedProcesses) {
152
+ assert(process->isAlive());
153
+ process->enabled = Process::DETACHED;
154
+ callAbortLongRunningConnectionsCallback(process);
155
+ } else {
156
+ P_BUG("Unknown destination list");
157
+ }
158
+ }
159
+
160
+ /**
161
+ * Removes a process to the given list (enabledProcess, disablingProcesses, disabledProcesses).
162
+ * This function does not fix getWaitlist invariants or other stuff.
163
+ */
164
+ void
165
+ Group::removeProcessFromList(const ProcessPtr &process, ProcessList &source) {
166
+ ProcessPtr p = process; // Keep an extra reference count just in case.
167
+
168
+ source.erase(source.begin() + process->getIndex());
169
+ process->setIndex(-1);
170
+
171
+ switch (process->enabled) {
172
+ case Process::ENABLED:
173
+ assert(&source == &enabledProcesses);
174
+ enabledCount--;
175
+ if (process->isTotallyBusy()) {
176
+ nEnabledProcessesTotallyBusy--;
177
+ }
178
+ break;
179
+ case Process::DISABLING:
180
+ assert(&source == &disablingProcesses);
181
+ disablingCount--;
182
+ break;
183
+ case Process::DISABLED:
184
+ assert(&source == &disabledProcesses);
185
+ disabledCount--;
186
+ break;
187
+ case Process::DETACHED:
188
+ assert(&source == &detachedProcesses);
189
+ break;
190
+ default:
191
+ P_BUG("Unknown 'enabled' state " << (int) process->enabled);
192
+ }
193
+
194
+ // Rebuild indices
195
+ ProcessList::iterator it, end = source.end();
196
+ unsigned int i = 0;
197
+ for (it = source.begin(); it != end; it++, i++) {
198
+ const ProcessPtr &process = *it;
199
+ process->setIndex(i);
200
+ }
201
+
202
+ // Rebuild enabledProcessBusynessLevels
203
+ if (&source == &enabledProcesses) {
204
+ enabledProcessBusynessLevels.clear();
205
+ for (it = source.begin(); it != end; it++, i++) {
206
+ const ProcessPtr &process = *it;
207
+ enabledProcessBusynessLevels.push_back(process->busyness());
208
+ }
209
+ enabledProcessBusynessLevels.shrink_to_fit();
210
+ }
211
+ }
212
+
213
+ void
214
+ Group::removeFromDisableWaitlist(const ProcessPtr &p, DisableResult result,
215
+ boost::container::vector<Callback> &postLockActions)
216
+ {
217
+ deque<DisableWaiter>::const_iterator it, end = disableWaitlist.end();
218
+ deque<DisableWaiter> newList;
219
+ for (it = disableWaitlist.begin(); it != end; it++) {
220
+ const DisableWaiter &waiter = *it;
221
+ const ProcessPtr process = waiter.process;
222
+ if (process == p) {
223
+ postLockActions.push_back(boost::bind(waiter.callback, p, result));
224
+ } else {
225
+ newList.push_back(waiter);
226
+ }
227
+ }
228
+ disableWaitlist = newList;
229
+ }
230
+
231
+ void
232
+ Group::clearDisableWaitlist(DisableResult result,
233
+ boost::container::vector<Callback> &postLockActions)
234
+ {
235
+ // This function may be called after processes in the disableWaitlist
236
+ // have been disabled or enabled, so do not assume any value for
237
+ // waiter.process->enabled in this function.
238
+ postLockActions.reserve(postLockActions.size() + disableWaitlist.size());
239
+ while (!disableWaitlist.empty()) {
240
+ const DisableWaiter &waiter = disableWaitlist.front();
241
+ postLockActions.push_back(boost::bind(waiter.callback, waiter.process, result));
242
+ disableWaitlist.pop_front();
243
+ }
244
+ }
245
+
246
+ void
247
+ Group::enableAllDisablingProcesses(boost::container::vector<Callback> &postLockActions) {
248
+ P_DEBUG("Enabling all DISABLING processes with result DR_ERROR");
249
+ deque<DisableWaiter>::iterator it, end = disableWaitlist.end();
250
+ for (it = disableWaitlist.begin(); it != end; it++) {
251
+ const DisableWaiter &waiter = *it;
252
+ const ProcessPtr process = waiter.process;
253
+ // A process can appear multiple times in disableWaitlist.
254
+ assert(process->enabled == Process::DISABLING
255
+ || process->enabled == Process::ENABLED);
256
+ if (process->enabled == Process::DISABLING) {
257
+ removeProcessFromList(process, disablingProcesses);
258
+ addProcessToList(process, enabledProcesses);
259
+ P_DEBUG("Enabled process " << process->inspect());
260
+ }
261
+ }
262
+ clearDisableWaitlist(DR_ERROR, postLockActions);
263
+ }
264
+
265
+ /**
266
+ * The `immediately` parameter only has effect if the detached processes checker
267
+ * thread is active. It means that, if the thread is currently sleeping, it should
268
+ * wake up immediately and perform work.
269
+ */
270
+ void
271
+ Group::startCheckingDetachedProcesses(bool immediately) {
272
+ if (!detachedProcessesCheckerActive) {
273
+ P_DEBUG("Starting detached processes checker");
274
+ getPool()->nonInterruptableThreads.create_thread(
275
+ boost::bind(&Group::detachedProcessesCheckerMain, this, shared_from_this()),
276
+ "Detached processes checker: " + getName(),
277
+ POOL_HELPER_THREAD_STACK_SIZE
278
+ );
279
+ detachedProcessesCheckerActive = true;
280
+ } else if (detachedProcessesCheckerActive && immediately) {
281
+ detachedProcessesCheckerCond.notify_all();
282
+ }
283
+ }
284
+
285
+ void
286
+ Group::detachedProcessesCheckerMain(GroupPtr self) {
287
+ TRACE_POINT();
288
+ Pool *pool = getPool();
289
+
290
+ Pool::DebugSupportPtr debug = pool->debugSupport;
291
+ if (debug != NULL && debug->detachedProcessesChecker) {
292
+ debug->debugger->send("About to start detached processes checker");
293
+ debug->messages->recv("Proceed with starting detached processes checker");
294
+ }
295
+
296
+ boost::unique_lock<boost::mutex> lock(pool->syncher);
297
+ while (true) {
298
+ assert(detachedProcessesCheckerActive);
299
+
300
+ if (getLifeStatus() == SHUT_DOWN || this_thread::interruption_requested()) {
301
+ UPDATE_TRACE_POINT();
302
+ P_DEBUG("Stopping detached processes checker");
303
+ detachedProcessesCheckerActive = false;
304
+ break;
305
+ }
306
+
307
+ UPDATE_TRACE_POINT();
308
+ if (!detachedProcesses.empty()) {
309
+ P_TRACE(2, "Checking whether any of the " << detachedProcesses.size() <<
310
+ " detached processes have exited...");
311
+ ProcessList::iterator it, end = detachedProcesses.end();
312
+ ProcessList processesToRemove;
313
+
314
+ for (it = detachedProcesses.begin(); it != end; it++) {
315
+ const ProcessPtr process = *it;
316
+ switch (process->getLifeStatus()) {
317
+ case Process::ALIVE:
318
+ if (process->canTriggerShutdown()) {
319
+ P_DEBUG("Detached process " << process->inspect() <<
320
+ " has 0 active sessions now. Triggering shutdown.");
321
+ process->triggerShutdown();
322
+ assert(process->getLifeStatus() == Process::SHUTDOWN_TRIGGERED);
323
+ }
324
+ break;
325
+ case Process::SHUTDOWN_TRIGGERED:
326
+ if (process->canCleanup()) {
327
+ P_DEBUG("Detached process " << process->inspect() << " has shut down. Cleaning up associated resources.");
328
+ process->cleanup();
329
+ assert(process->getLifeStatus() == Process::DEAD);
330
+ processesToRemove.push_back(process);
331
+ } else if (process->shutdownTimeoutExpired()) {
332
+ P_WARN("Detached process " << process->inspect() <<
333
+ " didn't shut down within " PROCESS_SHUTDOWN_TIMEOUT_DISPLAY
334
+ ". Forcefully killing it with SIGKILL.");
335
+ kill(process->getPid(), SIGKILL);
336
+ }
337
+ break;
338
+ default:
339
+ P_BUG("Unknown 'lifeStatus' state " << (int) process->getLifeStatus());
340
+ }
341
+ }
342
+
343
+ UPDATE_TRACE_POINT();
344
+ end = processesToRemove.end();
345
+ for (it = processesToRemove.begin(); it != end; it++) {
346
+ removeProcessFromList(*it, detachedProcesses);
347
+ }
348
+ }
349
+
350
+ UPDATE_TRACE_POINT();
351
+ if (detachedProcesses.empty()) {
352
+ UPDATE_TRACE_POINT();
353
+ P_DEBUG("Stopping detached processes checker");
354
+ detachedProcessesCheckerActive = false;
355
+
356
+ boost::container::vector<Callback> actions;
357
+ if (shutdownCanFinish()) {
358
+ UPDATE_TRACE_POINT();
359
+ finishShutdown(actions);
360
+ }
361
+
362
+ verifyInvariants();
363
+ verifyExpensiveInvariants();
364
+ lock.unlock();
365
+ UPDATE_TRACE_POINT();
366
+ runAllActions(actions);
367
+ break;
368
+ } else {
369
+ UPDATE_TRACE_POINT();
370
+ verifyInvariants();
371
+ verifyExpensiveInvariants();
372
+ }
373
+
374
+ // Not all processes can be shut down yet. Sleep for a while unless
375
+ // someone wakes us up.
376
+ UPDATE_TRACE_POINT();
377
+ detachedProcessesCheckerCond.timed_wait(lock,
378
+ posix_time::milliseconds(100));
379
+ }
380
+ }
381
+
382
+
383
+ /****************************
384
+ *
385
+ * Public methods
386
+ *
387
+ ****************************/
388
+
389
+
390
+ /**
391
+ * Attaches the given process to this Group and mark it as enabled. This
392
+ * function doesn't touch `getWaitlist` so be sure to fix its invariants
393
+ * afterwards if necessary, e.g. by calling `assignSessionsToGetWaiters()`.
394
+ */
395
+ AttachResult
396
+ Group::attach(const ProcessPtr &process,
397
+ boost::container::vector<Callback> &postLockActions)
398
+ {
399
+ TRACE_POINT();
400
+ assert(process->getGroup() == NULL || process->getGroup() == this);
401
+ assert(process->isAlive());
402
+ assert(isAlive());
403
+
404
+ if (processUpperLimitsReached()) {
405
+ return AR_GROUP_UPPER_LIMITS_REACHED;
406
+ } else if (poolAtFullCapacity()) {
407
+ return AR_POOL_AT_FULL_CAPACITY;
408
+ } else if (!isWaitingForCapacity() && anotherGroupIsWaitingForCapacity()) {
409
+ return AR_ANOTHER_GROUP_IS_WAITING_FOR_CAPACITY;
410
+ }
411
+
412
+ process->initializeStickySessionId(generateStickySessionId());
413
+ P_DEBUG("Attaching process " << process->inspect());
414
+ addProcessToList(process, enabledProcesses);
415
+
416
+ /* Now that there are enough resources, relevant processes in
417
+ * 'disableWaitlist' can be disabled.
418
+ */
419
+ deque<DisableWaiter>::const_iterator it, end = disableWaitlist.end();
420
+ deque<DisableWaiter> newDisableWaitlist;
421
+ for (it = disableWaitlist.begin(); it != end; it++) {
422
+ const DisableWaiter &waiter = *it;
423
+ const ProcessPtr process2 = waiter.process;
424
+ // The same process can appear multiple times in disableWaitlist.
425
+ assert(process2->enabled == Process::DISABLING
426
+ || process2->enabled == Process::DISABLED);
427
+ if (process2->sessions == 0) {
428
+ if (process2->enabled == Process::DISABLING) {
429
+ P_DEBUG("Disabling DISABLING process " << process2->inspect() <<
430
+ "; disable command succeeded immediately");
431
+ removeProcessFromList(process2, disablingProcesses);
432
+ addProcessToList(process2, disabledProcesses);
433
+ } else {
434
+ P_DEBUG("Disabling (already disabled) DISABLING process " <<
435
+ process2->inspect() << "; disable command succeeded immediately");
436
+ }
437
+ postLockActions.push_back(boost::bind(waiter.callback, process2, DR_SUCCESS));
438
+ } else {
439
+ newDisableWaitlist.push_back(waiter);
440
+ }
441
+ }
442
+ disableWaitlist = newDisableWaitlist;
443
+
444
+ // Update GC sleep timer.
445
+ wakeUpGarbageCollector();
446
+
447
+ postLockActions.push_back(boost::bind(&Group::runAttachHooks, this, process));
448
+
449
+ return AR_OK;
450
+ }
451
+
452
+ /**
453
+ * Detaches the given process from this Group. This function doesn't touch
454
+ * getWaitlist so be sure to fix its invariants afterwards if necessary.
455
+ * `pool->detachProcessUnlocked()` does that so you should usually use
456
+ * that method over this one.
457
+ */
458
+ void
459
+ Group::detach(const ProcessPtr &process, boost::container::vector<Callback> &postLockActions) {
460
+ TRACE_POINT();
461
+ assert(process->getGroup() == this);
462
+ assert(process->isAlive());
463
+ assert(isAlive());
464
+
465
+ if (process->enabled == Process::DETACHED) {
466
+ P_DEBUG("Detaching process " << process->inspect() << ", which was already being detached");
467
+ return;
468
+ }
469
+
470
+ const ProcessPtr p = process; // Keep an extra reference just in case.
471
+ P_DEBUG("Detaching process " << process->inspect());
472
+
473
+ if (process->enabled == Process::ENABLED || process->enabled == Process::DISABLING) {
474
+ assert(enabledCount > 0 || disablingCount > 0);
475
+ if (process->enabled == Process::ENABLED) {
476
+ removeProcessFromList(process, enabledProcesses);
477
+ } else {
478
+ removeProcessFromList(process, disablingProcesses);
479
+ removeFromDisableWaitlist(process, DR_NOOP, postLockActions);
480
+ }
481
+ } else {
482
+ assert(process->enabled == Process::DISABLED);
483
+ assert(!disabledProcesses.empty());
484
+ removeProcessFromList(process, disabledProcesses);
485
+ }
486
+
487
+ addProcessToList(process, detachedProcesses);
488
+ startCheckingDetachedProcesses(false);
489
+
490
+ postLockActions.push_back(boost::bind(&Group::runDetachHooks, this, process));
491
+ }
492
+
493
+ /**
494
+ * Detaches all processes from this Group. This function doesn't touch
495
+ * getWaitlist so be sure to fix its invariants afterwards if necessary.
496
+ */
497
+ void
498
+ Group::detachAll(boost::container::vector<Callback> &postLockActions) {
499
+ assert(isAlive());
500
+ P_DEBUG("Detaching all processes in group " << info.name);
501
+
502
+ foreach (ProcessPtr process, enabledProcesses) {
503
+ addProcessToList(process, detachedProcesses);
504
+ }
505
+ foreach (ProcessPtr process, disablingProcesses) {
506
+ addProcessToList(process, detachedProcesses);
507
+ }
508
+ foreach (ProcessPtr process, disabledProcesses) {
509
+ addProcessToList(process, detachedProcesses);
510
+ }
511
+
512
+ enabledProcesses.clear();
513
+ disablingProcesses.clear();
514
+ disabledProcesses.clear();
515
+ enabledProcessBusynessLevels.clear();
516
+ enabledCount = 0;
517
+ disablingCount = 0;
518
+ disabledCount = 0;
519
+ nEnabledProcessesTotallyBusy = 0;
520
+ clearDisableWaitlist(DR_NOOP, postLockActions);
521
+ startCheckingDetachedProcesses(false);
522
+ }
523
+
524
+ /**
525
+ * Marks the given process as enabled. This function doesn't touch getWaitlist
526
+ * so be sure to fix its invariants afterwards if necessary.
527
+ */
528
+ void
529
+ Group::enable(const ProcessPtr &process, boost::container::vector<Callback> &postLockActions) {
530
+ assert(process->getGroup() == this);
531
+ assert(process->isAlive());
532
+ assert(isAlive());
533
+
534
+ if (process->enabled == Process::DISABLING) {
535
+ P_DEBUG("Enabling DISABLING process " << process->inspect());
536
+ removeProcessFromList(process, disablingProcesses);
537
+ addProcessToList(process, enabledProcesses);
538
+ removeFromDisableWaitlist(process, DR_CANCELED, postLockActions);
539
+ } else if (process->enabled == Process::DISABLED) {
540
+ P_DEBUG("Enabling DISABLED process " << process->inspect());
541
+ removeProcessFromList(process, disabledProcesses);
542
+ addProcessToList(process, enabledProcesses);
543
+ } else {
544
+ P_DEBUG("Enabling ENABLED process " << process->inspect());
545
+ }
546
+ }
547
+
548
+ /**
549
+ * Marks the given process as disabled. Returns DR_SUCCESS, DR_DEFERRED
550
+ * or DR_NOOP. If the result is DR_DEFERRED, then the callback will be
551
+ * called later with the result of this action.
552
+ */
553
+ DisableResult
554
+ Group::disable(const ProcessPtr &process, const DisableCallback &callback) {
555
+ assert(process->getGroup() == this);
556
+ assert(process->isAlive());
557
+ assert(isAlive());
558
+
559
+ if (process->enabled == Process::ENABLED) {
560
+ P_DEBUG("Disabling ENABLED process " << process->inspect() <<
561
+ "; enabledCount=" << enabledCount << ", process.sessions=" << process->sessions);
562
+ assert(enabledCount >= 0);
563
+ if (enabledCount == 1 && !allowSpawn()) {
564
+ P_WARN("Cannot disable sole enabled process in group " << info.name <<
565
+ " because spawning is not allowed according to the current" <<
566
+ " configuration options");
567
+ return DR_ERROR;
568
+ } else if (enabledCount <= 1 || process->sessions > 0) {
569
+ removeProcessFromList(process, enabledProcesses);
570
+ addProcessToList(process, disablingProcesses);
571
+ disableWaitlist.push_back(DisableWaiter(process, callback));
572
+ if (enabledCount == 0) {
573
+ /* All processes are going to be disabled, so in order
574
+ * to avoid blocking requests we first spawn a new process
575
+ * and disable this process after the other one is done
576
+ * spawning. We do this irregardless of resource limits
577
+ * because this is an exceptional situation.
578
+ */
579
+ P_DEBUG("Spawning a new process to avoid the disable action from blocking requests");
580
+ spawn();
581
+ }
582
+ P_DEBUG("Deferring disable command completion");
583
+ return DR_DEFERRED;
584
+ } else {
585
+ removeProcessFromList(process, enabledProcesses);
586
+ addProcessToList(process, disabledProcesses);
587
+ P_DEBUG("Disable command succeeded immediately");
588
+ return DR_SUCCESS;
589
+ }
590
+ } else if (process->enabled == Process::DISABLING) {
591
+ assert(disablingCount > 0);
592
+ disableWaitlist.push_back(DisableWaiter(process, callback));
593
+ P_DEBUG("Disabling DISABLING process " << process->inspect() <<
594
+ info.name << "; command queued, deferring disable command completion");
595
+ return DR_DEFERRED;
596
+ } else {
597
+ assert(disabledCount > 0);
598
+ P_DEBUG("Disabling DISABLED process " << process->inspect() <<
599
+ info.name << "; disable command succeeded immediately");
600
+ return DR_NOOP;
601
+ }
602
+ }
603
+
604
+
605
+ } // namespace ApplicationPool2
606
+ } // namespace Passenger